* cgraph.h (cgraph_decide_inlining_incrementally): Kill.
[official-gcc.git] / gcc / tree-inline.c
blob813f18db307dd76d976c82b0dcfa7be08b88c1e8
1 /* Tree inlining.
2 Copyright 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3 Contributed by Alexandre Oliva <aoliva@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "toplev.h"
27 #include "tree.h"
28 #include "tree-inline.h"
29 #include "rtl.h"
30 #include "expr.h"
31 #include "flags.h"
32 #include "params.h"
33 #include "input.h"
34 #include "insn-config.h"
35 #include "varray.h"
36 #include "hashtab.h"
37 #include "langhooks.h"
38 #include "basic-block.h"
39 #include "tree-iterator.h"
40 #include "cgraph.h"
41 #include "intl.h"
42 #include "tree-mudflap.h"
43 #include "tree-flow.h"
44 #include "function.h"
45 #include "ggc.h"
46 #include "tree-flow.h"
47 #include "diagnostic.h"
48 #include "except.h"
49 #include "debug.h"
50 #include "pointer-set.h"
51 #include "ipa-prop.h"
52 #include "value-prof.h"
53 #include "tree-pass.h"
55 /* I'm not real happy about this, but we need to handle gimple and
56 non-gimple trees. */
57 #include "tree-gimple.h"
59 /* Inlining, Cloning, Versioning, Parallelization
61 Inlining: a function body is duplicated, but the PARM_DECLs are
62 remapped into VAR_DECLs, and non-void RETURN_EXPRs become
63 GIMPLE_MODIFY_STMTs that store to a dedicated returned-value variable.
64 The duplicated eh_region info of the copy will later be appended
65 to the info for the caller; the eh_region info in copied throwing
66 statements and RESX_EXPRs is adjusted accordingly.
68 Cloning: (only in C++) We have one body for a con/de/structor, and
69 multiple function decls, each with a unique parameter list.
70 Duplicate the body, using the given splay tree; some parameters
71 will become constants (like 0 or 1).
73 Versioning: a function body is duplicated and the result is a new
74 function rather than into blocks of an existing function as with
75 inlining. Some parameters will become constants.
77 Parallelization: a region of a function is duplicated resulting in
78 a new function. Variables may be replaced with complex expressions
79 to enable shared variable semantics.
81 All of these will simultaneously lookup any callgraph edges. If
82 we're going to inline the duplicated function body, and the given
83 function has some cloned callgraph nodes (one for each place this
84 function will be inlined) those callgraph edges will be duplicated.
85 If we're cloning the body, those callgraph edges will be
86 updated to point into the new body. (Note that the original
87 callgraph node and edge list will not be altered.)
89 See the CALL_EXPR handling case in copy_body_r (). */
91 /* 0 if we should not perform inlining.
92 1 if we should expand functions calls inline at the tree level.
93 2 if we should consider *all* functions to be inline
94 candidates. */
96 int flag_inline_trees = 0;
98 /* To Do:
100 o In order to make inlining-on-trees work, we pessimized
101 function-local static constants. In particular, they are now
102 always output, even when not addressed. Fix this by treating
103 function-local static constants just like global static
104 constants; the back-end already knows not to output them if they
105 are not needed.
107 o Provide heuristics to clamp inlining of recursive template
108 calls? */
110 /* Prototypes. */
112 static tree declare_return_variable (copy_body_data *, tree, tree, tree *);
113 static tree copy_generic_body (copy_body_data *);
114 static bool inlinable_function_p (tree);
115 static void remap_block (tree *, copy_body_data *);
116 static tree remap_decls (tree, copy_body_data *);
117 static void copy_bind_expr (tree *, int *, copy_body_data *);
118 static tree mark_local_for_remap_r (tree *, int *, void *);
119 static void unsave_expr_1 (tree);
120 static tree unsave_r (tree *, int *, void *);
121 static void declare_inline_vars (tree, tree);
122 static void remap_save_expr (tree *, void *, int *);
123 static void add_lexical_block (tree current_block, tree new_block);
124 static tree copy_decl_to_var (tree, copy_body_data *);
125 static tree copy_result_decl_to_var (tree, copy_body_data *);
126 static tree copy_decl_no_change (tree, copy_body_data *);
127 static tree copy_decl_maybe_to_var (tree, copy_body_data *);
129 /* Insert a tree->tree mapping for ID. Despite the name suggests
130 that the trees should be variables, it is used for more than that. */
132 void
133 insert_decl_map (copy_body_data *id, tree key, tree value)
135 splay_tree_insert (id->decl_map, (splay_tree_key) key,
136 (splay_tree_value) value);
138 /* Always insert an identity map as well. If we see this same new
139 node again, we won't want to duplicate it a second time. */
140 if (key != value)
141 splay_tree_insert (id->decl_map, (splay_tree_key) value,
142 (splay_tree_value) value);
145 /* Construct new SSA name for old NAME. ID is the inline context. */
147 static tree
148 remap_ssa_name (tree name, copy_body_data *id)
150 tree new;
151 splay_tree_node n;
153 gcc_assert (TREE_CODE (name) == SSA_NAME);
155 n = splay_tree_lookup (id->decl_map, (splay_tree_key) name);
156 if (n)
157 return (tree) n->value;
159 /* Do not set DEF_STMT yet as statement is not copied yet. We do that
160 in copy_bb. */
161 new = remap_decl (SSA_NAME_VAR (name), id);
162 /* We might've substituted constant or another SSA_NAME for
163 the variable.
165 Replace the SSA name representing RESULT_DECL by variable during
166 inlining: this saves us from need to introduce PHI node in a case
167 return value is just partly initialized. */
168 if ((TREE_CODE (new) == VAR_DECL || TREE_CODE (new) == PARM_DECL)
169 && (TREE_CODE (SSA_NAME_VAR (name)) != RESULT_DECL
170 || !id->transform_return_to_modify))
172 new = make_ssa_name (new, NULL);
173 insert_decl_map (id, name, new);
174 if (IS_EMPTY_STMT (SSA_NAME_DEF_STMT (name)))
176 SSA_NAME_DEF_STMT (new) = build_empty_stmt ();
177 if (gimple_default_def (id->src_cfun, SSA_NAME_VAR (name)) == name)
178 set_default_def (SSA_NAME_VAR (new), new);
180 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new)
181 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
182 TREE_TYPE (new) = TREE_TYPE (SSA_NAME_VAR (new));
184 else
185 insert_decl_map (id, name, new);
186 return new;
189 /* Remap DECL during the copying of the BLOCK tree for the function. */
191 tree
192 remap_decl (tree decl, copy_body_data *id)
194 splay_tree_node n;
195 tree fn;
197 /* We only remap local variables in the current function. */
198 fn = id->src_fn;
200 /* See if we have remapped this declaration. */
202 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
204 /* If we didn't already have an equivalent for this declaration,
205 create one now. */
206 if (!n)
208 /* Make a copy of the variable or label. */
209 tree t = id->copy_decl (decl, id);
211 /* Remember it, so that if we encounter this local entity again
212 we can reuse this copy. Do this early because remap_type may
213 need this decl for TYPE_STUB_DECL. */
214 insert_decl_map (id, decl, t);
216 if (!DECL_P (t))
217 return t;
219 /* Remap types, if necessary. */
220 TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
221 if (TREE_CODE (t) == TYPE_DECL)
222 DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
224 /* Remap sizes as necessary. */
225 walk_tree (&DECL_SIZE (t), copy_body_r, id, NULL);
226 walk_tree (&DECL_SIZE_UNIT (t), copy_body_r, id, NULL);
228 /* If fields, do likewise for offset and qualifier. */
229 if (TREE_CODE (t) == FIELD_DECL)
231 walk_tree (&DECL_FIELD_OFFSET (t), copy_body_r, id, NULL);
232 if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
233 walk_tree (&DECL_QUALIFIER (t), copy_body_r, id, NULL);
236 if (cfun && gimple_in_ssa_p (cfun)
237 && (TREE_CODE (t) == VAR_DECL
238 || TREE_CODE (t) == RESULT_DECL || TREE_CODE (t) == PARM_DECL))
240 tree def = gimple_default_def (id->src_cfun, decl);
241 get_var_ann (t);
242 if (TREE_CODE (decl) != PARM_DECL && def)
244 tree map = remap_ssa_name (def, id);
245 /* Watch out RESULT_DECLs whose SSA names map directly
246 to them. */
247 if (TREE_CODE (map) == SSA_NAME)
248 set_default_def (t, map);
250 add_referenced_var (t);
252 return t;
255 return unshare_expr ((tree) n->value);
258 static tree
259 remap_type_1 (tree type, copy_body_data *id)
261 splay_tree_node node;
262 tree new, t;
264 if (type == NULL)
265 return type;
267 /* See if we have remapped this type. */
268 node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
269 if (node)
270 return (tree) node->value;
272 /* The type only needs remapping if it's variably modified. */
273 if (! variably_modified_type_p (type, id->src_fn))
275 insert_decl_map (id, type, type);
276 return type;
279 /* We do need a copy. build and register it now. If this is a pointer or
280 reference type, remap the designated type and make a new pointer or
281 reference type. */
282 if (TREE_CODE (type) == POINTER_TYPE)
284 new = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
285 TYPE_MODE (type),
286 TYPE_REF_CAN_ALIAS_ALL (type));
287 insert_decl_map (id, type, new);
288 return new;
290 else if (TREE_CODE (type) == REFERENCE_TYPE)
292 new = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
293 TYPE_MODE (type),
294 TYPE_REF_CAN_ALIAS_ALL (type));
295 insert_decl_map (id, type, new);
296 return new;
298 else
299 new = copy_node (type);
301 insert_decl_map (id, type, new);
303 /* This is a new type, not a copy of an old type. Need to reassociate
304 variants. We can handle everything except the main variant lazily. */
305 t = TYPE_MAIN_VARIANT (type);
306 if (type != t)
308 t = remap_type (t, id);
309 TYPE_MAIN_VARIANT (new) = t;
310 TYPE_NEXT_VARIANT (new) = TYPE_MAIN_VARIANT (t);
311 TYPE_NEXT_VARIANT (t) = new;
313 else
315 TYPE_MAIN_VARIANT (new) = new;
316 TYPE_NEXT_VARIANT (new) = NULL;
319 if (TYPE_STUB_DECL (type))
320 TYPE_STUB_DECL (new) = remap_decl (TYPE_STUB_DECL (type), id);
322 /* Lazily create pointer and reference types. */
323 TYPE_POINTER_TO (new) = NULL;
324 TYPE_REFERENCE_TO (new) = NULL;
326 switch (TREE_CODE (new))
328 case INTEGER_TYPE:
329 case REAL_TYPE:
330 case ENUMERAL_TYPE:
331 case BOOLEAN_TYPE:
332 t = TYPE_MIN_VALUE (new);
333 if (t && TREE_CODE (t) != INTEGER_CST)
334 walk_tree (&TYPE_MIN_VALUE (new), copy_body_r, id, NULL);
336 t = TYPE_MAX_VALUE (new);
337 if (t && TREE_CODE (t) != INTEGER_CST)
338 walk_tree (&TYPE_MAX_VALUE (new), copy_body_r, id, NULL);
339 return new;
341 case FUNCTION_TYPE:
342 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
343 walk_tree (&TYPE_ARG_TYPES (new), copy_body_r, id, NULL);
344 return new;
346 case ARRAY_TYPE:
347 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
348 TYPE_DOMAIN (new) = remap_type (TYPE_DOMAIN (new), id);
349 break;
351 case RECORD_TYPE:
352 case UNION_TYPE:
353 case QUAL_UNION_TYPE:
355 tree f, nf = NULL;
357 for (f = TYPE_FIELDS (new); f ; f = TREE_CHAIN (f))
359 t = remap_decl (f, id);
360 DECL_CONTEXT (t) = new;
361 TREE_CHAIN (t) = nf;
362 nf = t;
364 TYPE_FIELDS (new) = nreverse (nf);
366 break;
368 case OFFSET_TYPE:
369 default:
370 /* Shouldn't have been thought variable sized. */
371 gcc_unreachable ();
374 walk_tree (&TYPE_SIZE (new), copy_body_r, id, NULL);
375 walk_tree (&TYPE_SIZE_UNIT (new), copy_body_r, id, NULL);
377 return new;
380 tree
381 remap_type (tree type, copy_body_data *id)
383 splay_tree_node node;
385 if (type == NULL)
386 return type;
388 /* See if we have remapped this type. */
389 node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
390 if (node)
391 return (tree) node->value;
393 /* The type only needs remapping if it's variably modified. */
394 if (! variably_modified_type_p (type, id->src_fn))
396 insert_decl_map (id, type, type);
397 return type;
400 return remap_type_1 (type, id);
403 static tree
404 remap_decls (tree decls, copy_body_data *id)
406 tree old_var;
407 tree new_decls = NULL_TREE;
409 /* Remap its variables. */
410 for (old_var = decls; old_var; old_var = TREE_CHAIN (old_var))
412 tree new_var;
414 /* We can not chain the local static declarations into the unexpanded_var_list
415 as we can't duplicate them or break one decl rule. Go ahead and link
416 them into unexpanded_var_list. */
417 if (!lang_hooks.tree_inlining.auto_var_in_fn_p (old_var, id->src_fn)
418 && !DECL_EXTERNAL (old_var))
420 cfun->unexpanded_var_list = tree_cons (NULL_TREE, old_var,
421 cfun->unexpanded_var_list);
422 continue;
425 /* Remap the variable. */
426 new_var = remap_decl (old_var, id);
428 /* If we didn't remap this variable, so we can't mess with its
429 TREE_CHAIN. If we remapped this variable to the return slot, it's
430 already declared somewhere else, so don't declare it here. */
431 if (!new_var || new_var == id->retvar)
433 else
435 gcc_assert (DECL_P (new_var));
436 TREE_CHAIN (new_var) = new_decls;
437 new_decls = new_var;
441 return nreverse (new_decls);
444 /* Copy the BLOCK to contain remapped versions of the variables
445 therein. And hook the new block into the block-tree. */
447 static void
448 remap_block (tree *block, copy_body_data *id)
450 tree old_block;
451 tree new_block;
452 tree fn;
454 /* Make the new block. */
455 old_block = *block;
456 new_block = make_node (BLOCK);
457 TREE_USED (new_block) = TREE_USED (old_block);
458 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
459 BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
460 *block = new_block;
462 /* Remap its variables. */
463 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block), id);
465 fn = id->dst_fn;
467 if (id->transform_lang_insert_block)
468 lang_hooks.decls.insert_block (new_block);
470 /* Remember the remapped block. */
471 insert_decl_map (id, old_block, new_block);
474 /* Copy the whole block tree and root it in id->block. */
475 static tree
476 remap_blocks (tree block, copy_body_data *id)
478 tree t;
479 tree new = block;
481 if (!block)
482 return NULL;
484 remap_block (&new, id);
485 gcc_assert (new != block);
486 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
487 add_lexical_block (new, remap_blocks (t, id));
488 return new;
491 static void
492 copy_statement_list (tree *tp)
494 tree_stmt_iterator oi, ni;
495 tree new;
497 new = alloc_stmt_list ();
498 ni = tsi_start (new);
499 oi = tsi_start (*tp);
500 *tp = new;
502 for (; !tsi_end_p (oi); tsi_next (&oi))
503 tsi_link_after (&ni, tsi_stmt (oi), TSI_NEW_STMT);
506 static void
507 copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
509 tree block = BIND_EXPR_BLOCK (*tp);
510 /* Copy (and replace) the statement. */
511 copy_tree_r (tp, walk_subtrees, NULL);
512 if (block)
514 remap_block (&block, id);
515 BIND_EXPR_BLOCK (*tp) = block;
518 if (BIND_EXPR_VARS (*tp))
519 /* This will remap a lot of the same decls again, but this should be
520 harmless. */
521 BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), id);
524 /* Called from copy_body_id via walk_tree. DATA is really an
525 `copy_body_data *'. */
527 tree
528 copy_body_r (tree *tp, int *walk_subtrees, void *data)
530 copy_body_data *id = (copy_body_data *) data;
531 tree fn = id->src_fn;
532 tree new_block;
534 /* Begin by recognizing trees that we'll completely rewrite for the
535 inlining context. Our output for these trees is completely
536 different from out input (e.g. RETURN_EXPR is deleted, and morphs
537 into an edge). Further down, we'll handle trees that get
538 duplicated and/or tweaked. */
540 /* When requested, RETURN_EXPRs should be transformed to just the
541 contained GIMPLE_MODIFY_STMT. The branch semantics of the return will
542 be handled elsewhere by manipulating the CFG rather than a statement. */
543 if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
545 tree assignment = TREE_OPERAND (*tp, 0);
547 /* If we're returning something, just turn that into an
548 assignment into the equivalent of the original RESULT_DECL.
549 If the "assignment" is just the result decl, the result
550 decl has already been set (e.g. a recent "foo (&result_decl,
551 ...)"); just toss the entire RETURN_EXPR. */
552 if (assignment && TREE_CODE (assignment) == GIMPLE_MODIFY_STMT)
554 /* Replace the RETURN_EXPR with (a copy of) the
555 GIMPLE_MODIFY_STMT hanging underneath. */
556 *tp = copy_node (assignment);
558 else /* Else the RETURN_EXPR returns no value. */
560 *tp = NULL;
561 return (tree) (void *)1;
564 else if (TREE_CODE (*tp) == SSA_NAME)
566 *tp = remap_ssa_name (*tp, id);
567 *walk_subtrees = 0;
568 return NULL;
571 /* Local variables and labels need to be replaced by equivalent
572 variables. We don't want to copy static variables; there's only
573 one of those, no matter how many times we inline the containing
574 function. Similarly for globals from an outer function. */
575 else if (lang_hooks.tree_inlining.auto_var_in_fn_p (*tp, fn))
577 tree new_decl;
579 /* Remap the declaration. */
580 new_decl = remap_decl (*tp, id);
581 gcc_assert (new_decl);
582 /* Replace this variable with the copy. */
583 STRIP_TYPE_NOPS (new_decl);
584 *tp = new_decl;
585 *walk_subtrees = 0;
587 else if (TREE_CODE (*tp) == STATEMENT_LIST)
588 copy_statement_list (tp);
589 else if (TREE_CODE (*tp) == SAVE_EXPR)
590 remap_save_expr (tp, id->decl_map, walk_subtrees);
591 else if (TREE_CODE (*tp) == LABEL_DECL
592 && (! DECL_CONTEXT (*tp)
593 || decl_function_context (*tp) == id->src_fn))
594 /* These may need to be remapped for EH handling. */
595 *tp = remap_decl (*tp, id);
596 else if (TREE_CODE (*tp) == BIND_EXPR)
597 copy_bind_expr (tp, walk_subtrees, id);
598 /* Types may need remapping as well. */
599 else if (TYPE_P (*tp))
600 *tp = remap_type (*tp, id);
602 /* If this is a constant, we have to copy the node iff the type will be
603 remapped. copy_tree_r will not copy a constant. */
604 else if (CONSTANT_CLASS_P (*tp))
606 tree new_type = remap_type (TREE_TYPE (*tp), id);
608 if (new_type == TREE_TYPE (*tp))
609 *walk_subtrees = 0;
611 else if (TREE_CODE (*tp) == INTEGER_CST)
612 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
613 TREE_INT_CST_HIGH (*tp));
614 else
616 *tp = copy_node (*tp);
617 TREE_TYPE (*tp) = new_type;
621 /* Otherwise, just copy the node. Note that copy_tree_r already
622 knows not to copy VAR_DECLs, etc., so this is safe. */
623 else
625 /* Here we handle trees that are not completely rewritten.
626 First we detect some inlining-induced bogosities for
627 discarding. */
628 if (TREE_CODE (*tp) == GIMPLE_MODIFY_STMT
629 && GIMPLE_STMT_OPERAND (*tp, 0) == GIMPLE_STMT_OPERAND (*tp, 1)
630 && (lang_hooks.tree_inlining.auto_var_in_fn_p
631 (GIMPLE_STMT_OPERAND (*tp, 0), fn)))
633 /* Some assignments VAR = VAR; don't generate any rtl code
634 and thus don't count as variable modification. Avoid
635 keeping bogosities like 0 = 0. */
636 tree decl = GIMPLE_STMT_OPERAND (*tp, 0), value;
637 splay_tree_node n;
639 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
640 if (n)
642 value = (tree) n->value;
643 STRIP_TYPE_NOPS (value);
644 if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value))
646 *tp = build_empty_stmt ();
647 return copy_body_r (tp, walk_subtrees, data);
651 else if (TREE_CODE (*tp) == INDIRECT_REF)
653 /* Get rid of *& from inline substitutions that can happen when a
654 pointer argument is an ADDR_EXPR. */
655 tree decl = TREE_OPERAND (*tp, 0);
656 splay_tree_node n;
658 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
659 if (n)
661 tree new;
662 tree old;
663 /* If we happen to get an ADDR_EXPR in n->value, strip
664 it manually here as we'll eventually get ADDR_EXPRs
665 which lie about their types pointed to. In this case
666 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
667 but we absolutely rely on that. As fold_indirect_ref
668 does other useful transformations, try that first, though. */
669 tree type = TREE_TYPE (TREE_TYPE ((tree)n->value));
670 new = unshare_expr ((tree)n->value);
671 old = *tp;
672 *tp = fold_indirect_ref_1 (type, new);
673 if (! *tp)
675 if (TREE_CODE (new) == ADDR_EXPR)
676 *tp = TREE_OPERAND (new, 0);
677 else
679 *tp = build1 (INDIRECT_REF, type, new);
680 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
683 *walk_subtrees = 0;
684 return NULL;
688 /* Here is the "usual case". Copy this tree node, and then
689 tweak some special cases. */
690 copy_tree_r (tp, walk_subtrees, NULL);
692 /* Global variables we didn't seen yet needs to go into referenced
693 vars. */
694 if (gimple_in_ssa_p (cfun) && TREE_CODE (*tp) == VAR_DECL)
695 add_referenced_var (*tp);
697 /* If EXPR has block defined, map it to newly constructed block.
698 When inlining we want EXPRs without block appear in the block
699 of function call. */
700 if (EXPR_P (*tp) || GIMPLE_STMT_P (*tp))
702 new_block = id->block;
703 if (TREE_BLOCK (*tp))
705 splay_tree_node n;
706 n = splay_tree_lookup (id->decl_map,
707 (splay_tree_key) TREE_BLOCK (*tp));
708 gcc_assert (n);
709 new_block = (tree) n->value;
711 TREE_BLOCK (*tp) = new_block;
714 if (TREE_CODE (*tp) == RESX_EXPR && id->eh_region_offset)
715 TREE_OPERAND (*tp, 0) =
716 build_int_cst
717 (NULL_TREE,
718 id->eh_region_offset + TREE_INT_CST_LOW (TREE_OPERAND (*tp, 0)));
720 if (!GIMPLE_TUPLE_P (*tp))
721 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
723 /* The copied TARGET_EXPR has never been expanded, even if the
724 original node was expanded already. */
725 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
727 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
728 TREE_OPERAND (*tp, 3) = NULL_TREE;
731 /* Variable substitution need not be simple. In particular, the
732 INDIRECT_REF substitution above. Make sure that TREE_CONSTANT
733 and friends are up-to-date. */
734 else if (TREE_CODE (*tp) == ADDR_EXPR)
736 walk_tree (&TREE_OPERAND (*tp, 0), copy_body_r, id, NULL);
737 /* Handle the case where we substituted an INDIRECT_REF
738 into the operand of the ADDR_EXPR. */
739 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
740 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
741 else
742 recompute_tree_invariant_for_addr_expr (*tp);
743 *walk_subtrees = 0;
747 /* Keep iterating. */
748 return NULL_TREE;
751 /* Copy basic block, scale profile accordingly. Edges will be taken care of
752 later */
754 static basic_block
755 copy_bb (copy_body_data *id, basic_block bb, int frequency_scale, int count_scale)
757 block_stmt_iterator bsi, copy_bsi;
758 basic_block copy_basic_block;
760 /* create_basic_block() will append every new block to
761 basic_block_info automatically. */
762 copy_basic_block = create_basic_block (NULL, (void *) 0,
763 (basic_block) bb->prev_bb->aux);
764 copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
765 copy_basic_block->frequency = (bb->frequency
766 * frequency_scale / REG_BR_PROB_BASE);
767 copy_bsi = bsi_start (copy_basic_block);
769 for (bsi = bsi_start (bb);
770 !bsi_end_p (bsi); bsi_next (&bsi))
772 tree stmt = bsi_stmt (bsi);
773 tree orig_stmt = stmt;
775 walk_tree (&stmt, copy_body_r, id, NULL);
777 /* RETURN_EXPR might be removed,
778 this is signalled by making stmt pointer NULL. */
779 if (stmt)
781 tree call, decl;
783 gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun, orig_stmt);
785 /* With return slot optimization we can end up with
786 non-gimple (foo *)&this->m, fix that here. */
787 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
788 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == NOP_EXPR
789 && !is_gimple_val (TREE_OPERAND (GIMPLE_STMT_OPERAND (stmt, 1), 0)))
790 gimplify_stmt (&stmt);
792 bsi_insert_after (&copy_bsi, stmt, BSI_NEW_STMT);
794 /* Process new statement. gimplify_stmt possibly turned statement
795 into multiple statements, we need to process all of them. */
796 while (!bsi_end_p (copy_bsi))
798 stmt = bsi_stmt (copy_bsi);
799 call = get_call_expr_in (stmt);
801 /* Statements produced by inlining can be unfolded, especially
802 when we constant propagated some operands. We can't fold
803 them right now for two reasons:
804 1) folding require SSA_NAME_DEF_STMTs to be correct
805 2) we can't change function calls to builtins.
806 So we just mark statement for later folding. We mark
807 all new statements, instead just statements that has changed
808 by some nontrivial substitution so even statements made
809 foldable indirectly are updated. If this turns out to be
810 expensive, copy_body can be told to watch for nontrivial
811 changes. */
812 if (id->statements_to_fold)
813 pointer_set_insert (id->statements_to_fold, stmt);
814 /* We're duplicating a CALL_EXPR. Find any corresponding
815 callgraph edges and update or duplicate them. */
816 if (call && (decl = get_callee_fndecl (call)))
818 struct cgraph_node *node;
819 struct cgraph_edge *edge;
821 switch (id->transform_call_graph_edges)
823 case CB_CGE_DUPLICATE:
824 edge = cgraph_edge (id->src_node, orig_stmt);
825 if (edge)
826 cgraph_clone_edge (edge, id->dst_node, stmt,
827 REG_BR_PROB_BASE, 1, true);
828 break;
830 case CB_CGE_MOVE_CLONES:
831 for (node = id->dst_node->next_clone;
832 node;
833 node = node->next_clone)
835 edge = cgraph_edge (node, orig_stmt);
836 gcc_assert (edge);
837 cgraph_set_call_stmt (edge, stmt);
839 /* FALLTHRU */
841 case CB_CGE_MOVE:
842 edge = cgraph_edge (id->dst_node, orig_stmt);
843 if (edge)
844 cgraph_set_call_stmt (edge, stmt);
845 break;
847 default:
848 gcc_unreachable ();
851 /* If you think we can abort here, you are wrong.
852 There is no region 0 in tree land. */
853 gcc_assert (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt)
854 != 0);
856 if (tree_could_throw_p (stmt))
858 int region = lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt);
859 /* Add an entry for the copied tree in the EH hashtable.
860 When cloning or versioning, use the hashtable in
861 cfun, and just copy the EH number. When inlining, use the
862 hashtable in the caller, and adjust the region number. */
863 if (region > 0)
864 add_stmt_to_eh_region (stmt, region + id->eh_region_offset);
866 /* If this tree doesn't have a region associated with it,
867 and there is a "current region,"
868 then associate this tree with the current region
869 and add edges associated with this region. */
870 if ((lookup_stmt_eh_region_fn (id->src_cfun,
871 orig_stmt) <= 0
872 && id->eh_region > 0)
873 && tree_could_throw_p (stmt))
874 add_stmt_to_eh_region (stmt, id->eh_region);
876 if (gimple_in_ssa_p (cfun))
878 ssa_op_iter i;
879 tree def;
881 find_new_referenced_vars (bsi_stmt_ptr (copy_bsi));
882 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
883 if (TREE_CODE (def) == SSA_NAME)
884 SSA_NAME_DEF_STMT (def) = stmt;
886 bsi_next (&copy_bsi);
888 copy_bsi = bsi_last (copy_basic_block);
891 return copy_basic_block;
894 /* Inserting Single Entry Multiple Exit region in SSA form into code in SSA
895 form is quite easy, since dominator relationship for old basic blocks does
896 not change.
898 There is however exception where inlining might change dominator relation
899 across EH edges from basic block within inlined functions destinating
900 to landing pads in function we inline into.
902 The function mark PHI_RESULT of such PHI nodes for renaming; it is
903 safe the EH edges are abnormal and SSA_NAME_OCCURS_IN_ABNORMAL_PHI
904 must be set. This means, that there will be no overlapping live ranges
905 for the underlying symbol.
907 This might change in future if we allow redirecting of EH edges and
908 we might want to change way build CFG pre-inlining to include
909 all the possible edges then. */
910 static void
911 update_ssa_across_eh_edges (basic_block bb)
913 edge e;
914 edge_iterator ei;
916 FOR_EACH_EDGE (e, ei, bb->succs)
917 if (!e->dest->aux
918 || ((basic_block)e->dest->aux)->index == ENTRY_BLOCK)
920 tree phi;
922 gcc_assert (e->flags & EDGE_EH);
923 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
925 gcc_assert (SSA_NAME_OCCURS_IN_ABNORMAL_PHI
926 (PHI_RESULT (phi)));
927 mark_sym_for_renaming
928 (SSA_NAME_VAR (PHI_RESULT (phi)));
933 /* Copy edges from BB into its copy constructed earlier, scale profile
934 accordingly. Edges will be taken care of later. Assume aux
935 pointers to point to the copies of each BB. */
936 static void
937 copy_edges_for_bb (basic_block bb, int count_scale)
939 basic_block new_bb = (basic_block) bb->aux;
940 edge_iterator ei;
941 edge old_edge;
942 block_stmt_iterator bsi;
943 int flags;
945 /* Use the indices from the original blocks to create edges for the
946 new ones. */
947 FOR_EACH_EDGE (old_edge, ei, bb->succs)
948 if (!(old_edge->flags & EDGE_EH))
950 edge new;
952 flags = old_edge->flags;
954 /* Return edges do get a FALLTHRU flag when the get inlined. */
955 if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
956 && old_edge->dest->aux != EXIT_BLOCK_PTR)
957 flags |= EDGE_FALLTHRU;
958 new = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
959 new->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
960 new->probability = old_edge->probability;
963 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
964 return;
966 for (bsi = bsi_start (new_bb); !bsi_end_p (bsi);)
968 tree copy_stmt;
970 copy_stmt = bsi_stmt (bsi);
971 update_stmt (copy_stmt);
972 if (gimple_in_ssa_p (cfun))
973 mark_symbols_for_renaming (copy_stmt);
974 /* Do this before the possible split_block. */
975 bsi_next (&bsi);
977 /* If this tree could throw an exception, there are two
978 cases where we need to add abnormal edge(s): the
979 tree wasn't in a region and there is a "current
980 region" in the caller; or the original tree had
981 EH edges. In both cases split the block after the tree,
982 and add abnormal edge(s) as needed; we need both
983 those from the callee and the caller.
984 We check whether the copy can throw, because the const
985 propagation can change an INDIRECT_REF which throws
986 into a COMPONENT_REF which doesn't. If the copy
987 can throw, the original could also throw. */
989 if (tree_can_throw_internal (copy_stmt))
991 if (!bsi_end_p (bsi))
992 /* Note that bb's predecessor edges aren't necessarily
993 right at this point; split_block doesn't care. */
995 edge e = split_block (new_bb, copy_stmt);
997 new_bb = e->dest;
998 new_bb->aux = e->src->aux;
999 bsi = bsi_start (new_bb);
1002 make_eh_edges (copy_stmt);
1004 if (gimple_in_ssa_p (cfun))
1005 update_ssa_across_eh_edges (bb_for_stmt (copy_stmt));
1010 /* Copy the PHIs. All blocks and edges are copied, some blocks
1011 was possibly split and new outgoing EH edges inserted.
1012 BB points to the block of original function and AUX pointers links
1013 the original and newly copied blocks. */
1015 static void
1016 copy_phis_for_bb (basic_block bb, copy_body_data *id)
1018 basic_block new_bb = bb->aux;
1019 edge_iterator ei;
1020 tree phi;
1022 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1024 tree res = PHI_RESULT (phi);
1025 tree new_res = res;
1026 tree new_phi;
1027 edge new_edge;
1029 if (is_gimple_reg (res))
1031 walk_tree (&new_res, copy_body_r, id, NULL);
1032 SSA_NAME_DEF_STMT (new_res)
1033 = new_phi = create_phi_node (new_res, new_bb);
1034 FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
1036 edge old_edge = find_edge (new_edge->src->aux, bb);
1037 tree arg = PHI_ARG_DEF_FROM_EDGE (phi, old_edge);
1038 tree new_arg = arg;
1040 walk_tree (&new_arg, copy_body_r, id, NULL);
1041 gcc_assert (new_arg);
1042 add_phi_arg (new_phi, new_arg, new_edge);
1048 /* Wrapper for remap_decl so it can be used as a callback. */
1049 static tree
1050 remap_decl_1 (tree decl, void *data)
1052 return remap_decl (decl, (copy_body_data *) data);
1055 /* Build struct function and associated datastructures for the new clone
1056 NEW_FNDECL to be build. CALLEE_FNDECL is the original */
1058 static void
1059 initialize_cfun (tree new_fndecl, tree callee_fndecl, gcov_type count,
1060 int frequency)
1062 struct function *new_cfun
1063 = (struct function *) ggc_alloc_cleared (sizeof (struct function));
1064 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1065 int count_scale, frequency_scale;
1067 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1068 count_scale = (REG_BR_PROB_BASE * count
1069 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1070 else
1071 count_scale = 1;
1073 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
1074 frequency_scale = (REG_BR_PROB_BASE * frequency
1076 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
1077 else
1078 frequency_scale = count_scale;
1080 /* Register specific tree functions. */
1081 tree_register_cfg_hooks ();
1082 *new_cfun = *DECL_STRUCT_FUNCTION (callee_fndecl);
1083 new_cfun->funcdef_no = get_next_funcdef_no ();
1084 VALUE_HISTOGRAMS (new_cfun) = NULL;
1085 new_cfun->unexpanded_var_list = NULL;
1086 new_cfun->cfg = NULL;
1087 new_cfun->decl = new_fndecl /*= copy_node (callee_fndecl)*/;
1088 new_cfun->ib_boundaries_block = NULL;
1089 DECL_STRUCT_FUNCTION (new_fndecl) = new_cfun;
1090 push_cfun (new_cfun);
1091 init_empty_tree_cfg ();
1093 ENTRY_BLOCK_PTR->count =
1094 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
1095 REG_BR_PROB_BASE);
1096 ENTRY_BLOCK_PTR->frequency =
1097 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
1098 frequency_scale / REG_BR_PROB_BASE);
1099 EXIT_BLOCK_PTR->count =
1100 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
1101 REG_BR_PROB_BASE);
1102 EXIT_BLOCK_PTR->frequency =
1103 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
1104 frequency_scale / REG_BR_PROB_BASE);
1105 if (src_cfun->eh)
1106 init_eh_for_function ();
1108 if (src_cfun->gimple_df)
1110 init_tree_ssa ();
1111 cfun->gimple_df->in_ssa_p = true;
1112 init_ssa_operands ();
1114 pop_cfun ();
1117 /* Make a copy of the body of FN so that it can be inserted inline in
1118 another function. Walks FN via CFG, returns new fndecl. */
1120 static tree
1121 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency,
1122 basic_block entry_block_map, basic_block exit_block_map)
1124 tree callee_fndecl = id->src_fn;
1125 /* Original cfun for the callee, doesn't change. */
1126 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1127 struct function *cfun_to_copy;
1128 basic_block bb;
1129 tree new_fndecl = NULL;
1130 int count_scale, frequency_scale;
1131 int last;
1133 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1134 count_scale = (REG_BR_PROB_BASE * count
1135 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1136 else
1137 count_scale = 1;
1139 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
1140 frequency_scale = (REG_BR_PROB_BASE * frequency
1142 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
1143 else
1144 frequency_scale = count_scale;
1146 /* Register specific tree functions. */
1147 tree_register_cfg_hooks ();
1149 /* Must have a CFG here at this point. */
1150 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
1151 (DECL_STRUCT_FUNCTION (callee_fndecl)));
1153 cfun_to_copy = id->src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1156 ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
1157 EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
1158 entry_block_map->aux = ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
1159 exit_block_map->aux = EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
1161 /* Duplicate any exception-handling regions. */
1162 if (cfun->eh)
1164 id->eh_region_offset
1165 = duplicate_eh_regions (cfun_to_copy, remap_decl_1, id,
1166 0, id->eh_region);
1168 /* Use aux pointers to map the original blocks to copy. */
1169 FOR_EACH_BB_FN (bb, cfun_to_copy)
1171 basic_block new = copy_bb (id, bb, frequency_scale, count_scale);
1172 bb->aux = new;
1173 new->aux = bb;
1176 last = n_basic_blocks;
1177 /* Now that we've duplicated the blocks, duplicate their edges. */
1178 FOR_ALL_BB_FN (bb, cfun_to_copy)
1179 copy_edges_for_bb (bb, count_scale);
1180 if (gimple_in_ssa_p (cfun))
1181 FOR_ALL_BB_FN (bb, cfun_to_copy)
1182 copy_phis_for_bb (bb, id);
1183 FOR_ALL_BB_FN (bb, cfun_to_copy)
1185 ((basic_block)bb->aux)->aux = NULL;
1186 bb->aux = NULL;
1188 /* Zero out AUX fields of newly created block during EH edge
1189 insertion. */
1190 for (; last < n_basic_blocks; last++)
1191 BASIC_BLOCK (last)->aux = NULL;
1192 entry_block_map->aux = NULL;
1193 exit_block_map->aux = NULL;
1195 return new_fndecl;
1198 /* Make a copy of the body of FN so that it can be inserted inline in
1199 another function. */
1201 static tree
1202 copy_generic_body (copy_body_data *id)
1204 tree body;
1205 tree fndecl = id->src_fn;
1207 body = DECL_SAVED_TREE (fndecl);
1208 walk_tree (&body, copy_body_r, id, NULL);
1210 return body;
1213 static tree
1214 copy_body (copy_body_data *id, gcov_type count, int frequency,
1215 basic_block entry_block_map, basic_block exit_block_map)
1217 tree fndecl = id->src_fn;
1218 tree body;
1220 /* If this body has a CFG, walk CFG and copy. */
1221 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
1222 body = copy_cfg_body (id, count, frequency, entry_block_map, exit_block_map);
1224 return body;
1227 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
1228 defined in function FN, or of a data member thereof. */
1230 static bool
1231 self_inlining_addr_expr (tree value, tree fn)
1233 tree var;
1235 if (TREE_CODE (value) != ADDR_EXPR)
1236 return false;
1238 var = get_base_address (TREE_OPERAND (value, 0));
1240 return var && lang_hooks.tree_inlining.auto_var_in_fn_p (var, fn);
1243 static void
1244 setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
1245 basic_block bb, tree *vars)
1247 tree init_stmt;
1248 tree var;
1249 tree var_sub;
1250 tree rhs = value ? fold_convert (TREE_TYPE (p), value) : NULL;
1251 tree def = (gimple_in_ssa_p (cfun)
1252 ? gimple_default_def (id->src_cfun, p) : NULL);
1254 /* If the parameter is never assigned to, has no SSA_NAMEs created,
1255 we may not need to create a new variable here at all. Instead, we may
1256 be able to just use the argument value. */
1257 if (TREE_READONLY (p)
1258 && !TREE_ADDRESSABLE (p)
1259 && value && !TREE_SIDE_EFFECTS (value)
1260 && !def)
1262 /* We may produce non-gimple trees by adding NOPs or introduce
1263 invalid sharing when operand is not really constant.
1264 It is not big deal to prohibit constant propagation here as
1265 we will constant propagate in DOM1 pass anyway. */
1266 if (is_gimple_min_invariant (value)
1267 && lang_hooks.types_compatible_p (TREE_TYPE (value), TREE_TYPE (p))
1268 /* We have to be very careful about ADDR_EXPR. Make sure
1269 the base variable isn't a local variable of the inlined
1270 function, e.g., when doing recursive inlining, direct or
1271 mutually-recursive or whatever, which is why we don't
1272 just test whether fn == current_function_decl. */
1273 && ! self_inlining_addr_expr (value, fn))
1275 insert_decl_map (id, p, value);
1276 return;
1280 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
1281 here since the type of this decl must be visible to the calling
1282 function. */
1283 var = copy_decl_to_var (p, id);
1284 if (gimple_in_ssa_p (cfun) && TREE_CODE (var) == VAR_DECL)
1286 get_var_ann (var);
1287 add_referenced_var (var);
1290 /* See if the frontend wants to pass this by invisible reference. If
1291 so, our new VAR_DECL will have REFERENCE_TYPE, and we need to
1292 replace uses of the PARM_DECL with dereferences. */
1293 if (TREE_TYPE (var) != TREE_TYPE (p)
1294 && POINTER_TYPE_P (TREE_TYPE (var))
1295 && TREE_TYPE (TREE_TYPE (var)) == TREE_TYPE (p))
1297 insert_decl_map (id, var, var);
1298 var_sub = build_fold_indirect_ref (var);
1300 else
1301 var_sub = var;
1303 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
1304 that way, when the PARM_DECL is encountered, it will be
1305 automatically replaced by the VAR_DECL. */
1306 insert_decl_map (id, p, var_sub);
1308 /* Declare this new variable. */
1309 TREE_CHAIN (var) = *vars;
1310 *vars = var;
1312 /* Make gimplifier happy about this variable. */
1313 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1315 /* Even if P was TREE_READONLY, the new VAR should not be.
1316 In the original code, we would have constructed a
1317 temporary, and then the function body would have never
1318 changed the value of P. However, now, we will be
1319 constructing VAR directly. The constructor body may
1320 change its value multiple times as it is being
1321 constructed. Therefore, it must not be TREE_READONLY;
1322 the back-end assumes that TREE_READONLY variable is
1323 assigned to only once. */
1324 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
1325 TREE_READONLY (var) = 0;
1327 /* If there is no setup required and we are in SSA, take the easy route
1328 replacing all SSA names representing the function parameter by the
1329 SSA name passed to function.
1331 We need to construct map for the variable anyway as it might be used
1332 in different SSA names when parameter is set in function.
1334 FIXME: This usually kills the last connection in between inlined
1335 function parameter and the actual value in debug info. Can we do
1336 better here? If we just inserted the statement, copy propagation
1337 would kill it anyway as it always did in older versions of GCC.
1339 We might want to introduce a notion that single SSA_NAME might
1340 represent multiple variables for purposes of debugging. */
1341 if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p)
1342 && (TREE_CODE (rhs) == SSA_NAME
1343 || is_gimple_min_invariant (rhs))
1344 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
1346 insert_decl_map (id, def, rhs);
1347 return;
1350 /* Initialize this VAR_DECL from the equivalent argument. Convert
1351 the argument to the proper type in case it was promoted. */
1352 if (value)
1354 block_stmt_iterator bsi = bsi_last (bb);
1356 if (rhs == error_mark_node)
1358 insert_decl_map (id, p, var_sub);
1359 return;
1362 STRIP_USELESS_TYPE_CONVERSION (rhs);
1364 /* We want to use GIMPLE_MODIFY_STMT, not INIT_EXPR here so that we
1365 keep our trees in gimple form. */
1366 if (def && gimple_in_ssa_p (cfun) && is_gimple_reg (p))
1368 def = remap_ssa_name (def, id);
1369 init_stmt = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (var), def, rhs);
1370 SSA_NAME_DEF_STMT (def) = init_stmt;
1371 SSA_NAME_IS_DEFAULT_DEF (def) = 0;
1372 set_default_def (var, NULL);
1374 else
1375 init_stmt = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (var), var, rhs);
1377 /* If we did not create a gimple value and we did not create a gimple
1378 cast of a gimple value, then we will need to gimplify INIT_STMTS
1379 at the end. Note that is_gimple_cast only checks the outer
1380 tree code, not its operand. Thus the explicit check that its
1381 operand is a gimple value. */
1382 if ((!is_gimple_val (rhs)
1383 && (!is_gimple_cast (rhs)
1384 || !is_gimple_val (TREE_OPERAND (rhs, 0))))
1385 || !is_gimple_reg (var))
1387 tree_stmt_iterator i;
1389 push_gimplify_context ();
1390 gimplify_stmt (&init_stmt);
1391 if (gimple_in_ssa_p (cfun)
1392 && init_stmt && TREE_CODE (init_stmt) == STATEMENT_LIST)
1394 /* The replacement can expose previously unreferenced
1395 variables. */
1396 for (i = tsi_start (init_stmt); !tsi_end_p (i); tsi_next (&i))
1397 find_new_referenced_vars (tsi_stmt_ptr (i));
1399 pop_gimplify_context (NULL);
1402 /* If VAR represents a zero-sized variable, it's possible that the
1403 assignment statment may result in no gimple statements. */
1404 if (init_stmt)
1405 bsi_insert_after (&bsi, init_stmt, BSI_NEW_STMT);
1406 if (gimple_in_ssa_p (cfun))
1407 for (;!bsi_end_p (bsi); bsi_next (&bsi))
1408 mark_symbols_for_renaming (bsi_stmt (bsi));
1412 /* Generate code to initialize the parameters of the function at the
1413 top of the stack in ID from the ARGS (presented as a TREE_LIST). */
1415 static void
1416 initialize_inlined_parameters (copy_body_data *id, tree args, tree static_chain,
1417 tree fn, basic_block bb)
1419 tree parms;
1420 tree a;
1421 tree p;
1422 tree vars = NULL_TREE;
1423 int argnum = 0;
1425 /* Figure out what the parameters are. */
1426 parms = DECL_ARGUMENTS (fn);
1428 /* Loop through the parameter declarations, replacing each with an
1429 equivalent VAR_DECL, appropriately initialized. */
1430 for (p = parms, a = args; p;
1431 a = a ? TREE_CHAIN (a) : a, p = TREE_CHAIN (p))
1433 tree value;
1435 ++argnum;
1437 /* Find the initializer. */
1438 value = lang_hooks.tree_inlining.convert_parm_for_inlining
1439 (p, a ? TREE_VALUE (a) : NULL_TREE, fn, argnum);
1441 setup_one_parameter (id, p, value, fn, bb, &vars);
1444 /* Initialize the static chain. */
1445 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
1446 gcc_assert (fn != current_function_decl);
1447 if (p)
1449 /* No static chain? Seems like a bug in tree-nested.c. */
1450 gcc_assert (static_chain);
1452 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
1455 declare_inline_vars (id->block, vars);
1458 /* Declare a return variable to replace the RESULT_DECL for the
1459 function we are calling. An appropriate DECL_STMT is returned.
1460 The USE_STMT is filled to contain a use of the declaration to
1461 indicate the return value of the function.
1463 RETURN_SLOT, if non-null is place where to store the result. It
1464 is set only for CALL_EXPR_RETURN_SLOT_OPT. MODIFY_DEST, if non-null,
1465 was the LHS of the GIMPLE_MODIFY_STMT to which this call is the RHS.
1467 The return value is a (possibly null) value that is the result of the
1468 function as seen by the callee. *USE_P is a (possibly null) value that
1469 holds the result as seen by the caller. */
1471 static tree
1472 declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest,
1473 tree *use_p)
1475 tree callee = id->src_fn;
1476 tree caller = id->dst_fn;
1477 tree result = DECL_RESULT (callee);
1478 tree callee_type = TREE_TYPE (result);
1479 tree caller_type = TREE_TYPE (TREE_TYPE (callee));
1480 tree var, use;
1482 /* We don't need to do anything for functions that don't return
1483 anything. */
1484 if (!result || VOID_TYPE_P (callee_type))
1486 *use_p = NULL_TREE;
1487 return NULL_TREE;
1490 /* If there was a return slot, then the return value is the
1491 dereferenced address of that object. */
1492 if (return_slot)
1494 /* The front end shouldn't have used both return_slot and
1495 a modify expression. */
1496 gcc_assert (!modify_dest);
1497 if (DECL_BY_REFERENCE (result))
1499 tree return_slot_addr = build_fold_addr_expr (return_slot);
1500 STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
1502 /* We are going to construct *&return_slot and we can't do that
1503 for variables believed to be not addressable.
1505 FIXME: This check possibly can match, because values returned
1506 via return slot optimization are not believed to have address
1507 taken by alias analysis. */
1508 gcc_assert (TREE_CODE (return_slot) != SSA_NAME);
1509 if (gimple_in_ssa_p (cfun))
1511 HOST_WIDE_INT bitsize;
1512 HOST_WIDE_INT bitpos;
1513 tree offset;
1514 enum machine_mode mode;
1515 int unsignedp;
1516 int volatilep;
1517 tree base;
1518 base = get_inner_reference (return_slot, &bitsize, &bitpos,
1519 &offset,
1520 &mode, &unsignedp, &volatilep,
1521 false);
1522 if (TREE_CODE (base) == INDIRECT_REF)
1523 base = TREE_OPERAND (base, 0);
1524 if (TREE_CODE (base) == SSA_NAME)
1525 base = SSA_NAME_VAR (base);
1526 mark_sym_for_renaming (base);
1528 var = return_slot_addr;
1530 else
1532 var = return_slot;
1533 gcc_assert (TREE_CODE (var) != SSA_NAME);
1535 if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
1536 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
1537 && !DECL_GIMPLE_REG_P (result)
1538 && DECL_P (var))
1539 DECL_GIMPLE_REG_P (var) = 0;
1540 use = NULL;
1541 goto done;
1544 /* All types requiring non-trivial constructors should have been handled. */
1545 gcc_assert (!TREE_ADDRESSABLE (callee_type));
1547 /* Attempt to avoid creating a new temporary variable. */
1548 if (modify_dest
1549 && TREE_CODE (modify_dest) != SSA_NAME)
1551 bool use_it = false;
1553 /* We can't use MODIFY_DEST if there's type promotion involved. */
1554 if (!lang_hooks.types_compatible_p (caller_type, callee_type))
1555 use_it = false;
1557 /* ??? If we're assigning to a variable sized type, then we must
1558 reuse the destination variable, because we've no good way to
1559 create variable sized temporaries at this point. */
1560 else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
1561 use_it = true;
1563 /* If the callee cannot possibly modify MODIFY_DEST, then we can
1564 reuse it as the result of the call directly. Don't do this if
1565 it would promote MODIFY_DEST to addressable. */
1566 else if (TREE_ADDRESSABLE (result))
1567 use_it = false;
1568 else
1570 tree base_m = get_base_address (modify_dest);
1572 /* If the base isn't a decl, then it's a pointer, and we don't
1573 know where that's going to go. */
1574 if (!DECL_P (base_m))
1575 use_it = false;
1576 else if (is_global_var (base_m))
1577 use_it = false;
1578 else if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
1579 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
1580 && !DECL_GIMPLE_REG_P (result)
1581 && DECL_GIMPLE_REG_P (base_m))
1582 use_it = false;
1583 else if (!TREE_ADDRESSABLE (base_m))
1584 use_it = true;
1587 if (use_it)
1589 var = modify_dest;
1590 use = NULL;
1591 goto done;
1595 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
1597 var = copy_result_decl_to_var (result, id);
1598 if (gimple_in_ssa_p (cfun))
1600 get_var_ann (var);
1601 add_referenced_var (var);
1604 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1605 DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list
1606 = tree_cons (NULL_TREE, var,
1607 DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list);
1609 /* Do not have the rest of GCC warn about this variable as it should
1610 not be visible to the user. */
1611 TREE_NO_WARNING (var) = 1;
1613 declare_inline_vars (id->block, var);
1615 /* Build the use expr. If the return type of the function was
1616 promoted, convert it back to the expected type. */
1617 use = var;
1618 if (!lang_hooks.types_compatible_p (TREE_TYPE (var), caller_type))
1619 use = fold_convert (caller_type, var);
1621 STRIP_USELESS_TYPE_CONVERSION (use);
1623 if (DECL_BY_REFERENCE (result))
1624 var = build_fold_addr_expr (var);
1626 done:
1627 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
1628 way, when the RESULT_DECL is encountered, it will be
1629 automatically replaced by the VAR_DECL. */
1630 insert_decl_map (id, result, var);
1632 /* Remember this so we can ignore it in remap_decls. */
1633 id->retvar = var;
1635 *use_p = use;
1636 return var;
1639 /* Returns nonzero if a function can be inlined as a tree. */
1641 bool
1642 tree_inlinable_function_p (tree fn)
1644 return inlinable_function_p (fn);
1647 static const char *inline_forbidden_reason;
1649 static tree
1650 inline_forbidden_p_1 (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED,
1651 void *fnp)
1653 tree node = *nodep;
1654 tree fn = (tree) fnp;
1655 tree t;
1657 switch (TREE_CODE (node))
1659 case CALL_EXPR:
1660 /* Refuse to inline alloca call unless user explicitly forced so as
1661 this may change program's memory overhead drastically when the
1662 function using alloca is called in loop. In GCC present in
1663 SPEC2000 inlining into schedule_block cause it to require 2GB of
1664 RAM instead of 256MB. */
1665 if (alloca_call_p (node)
1666 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1668 inline_forbidden_reason
1669 = G_("function %q+F can never be inlined because it uses "
1670 "alloca (override using the always_inline attribute)");
1671 return node;
1673 t = get_callee_fndecl (node);
1674 if (! t)
1675 break;
1677 /* We cannot inline functions that call setjmp. */
1678 if (setjmp_call_p (t))
1680 inline_forbidden_reason
1681 = G_("function %q+F can never be inlined because it uses setjmp");
1682 return node;
1685 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
1686 switch (DECL_FUNCTION_CODE (t))
1688 /* We cannot inline functions that take a variable number of
1689 arguments. */
1690 case BUILT_IN_VA_START:
1691 case BUILT_IN_STDARG_START:
1692 case BUILT_IN_NEXT_ARG:
1693 case BUILT_IN_VA_END:
1694 inline_forbidden_reason
1695 = G_("function %q+F can never be inlined because it "
1696 "uses variable argument lists");
1697 return node;
1699 case BUILT_IN_LONGJMP:
1700 /* We can't inline functions that call __builtin_longjmp at
1701 all. The non-local goto machinery really requires the
1702 destination be in a different function. If we allow the
1703 function calling __builtin_longjmp to be inlined into the
1704 function calling __builtin_setjmp, Things will Go Awry. */
1705 inline_forbidden_reason
1706 = G_("function %q+F can never be inlined because "
1707 "it uses setjmp-longjmp exception handling");
1708 return node;
1710 case BUILT_IN_NONLOCAL_GOTO:
1711 /* Similarly. */
1712 inline_forbidden_reason
1713 = G_("function %q+F can never be inlined because "
1714 "it uses non-local goto");
1715 return node;
1717 case BUILT_IN_RETURN:
1718 case BUILT_IN_APPLY_ARGS:
1719 /* If a __builtin_apply_args caller would be inlined,
1720 it would be saving arguments of the function it has
1721 been inlined into. Similarly __builtin_return would
1722 return from the function the inline has been inlined into. */
1723 inline_forbidden_reason
1724 = G_("function %q+F can never be inlined because "
1725 "it uses __builtin_return or __builtin_apply_args");
1726 return node;
1728 default:
1729 break;
1731 break;
1733 case GOTO_EXPR:
1734 t = TREE_OPERAND (node, 0);
1736 /* We will not inline a function which uses computed goto. The
1737 addresses of its local labels, which may be tucked into
1738 global storage, are of course not constant across
1739 instantiations, which causes unexpected behavior. */
1740 if (TREE_CODE (t) != LABEL_DECL)
1742 inline_forbidden_reason
1743 = G_("function %q+F can never be inlined "
1744 "because it contains a computed goto");
1745 return node;
1747 break;
1749 case LABEL_EXPR:
1750 t = TREE_OPERAND (node, 0);
1751 if (DECL_NONLOCAL (t))
1753 /* We cannot inline a function that receives a non-local goto
1754 because we cannot remap the destination label used in the
1755 function that is performing the non-local goto. */
1756 inline_forbidden_reason
1757 = G_("function %q+F can never be inlined "
1758 "because it receives a non-local goto");
1759 return node;
1761 break;
1763 case RECORD_TYPE:
1764 case UNION_TYPE:
1765 /* We cannot inline a function of the form
1767 void F (int i) { struct S { int ar[i]; } s; }
1769 Attempting to do so produces a catch-22.
1770 If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
1771 UNION_TYPE nodes, then it goes into infinite recursion on a
1772 structure containing a pointer to its own type. If it doesn't,
1773 then the type node for S doesn't get adjusted properly when
1774 F is inlined.
1776 ??? This is likely no longer true, but it's too late in the 4.0
1777 cycle to try to find out. This should be checked for 4.1. */
1778 for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t))
1779 if (variably_modified_type_p (TREE_TYPE (t), NULL))
1781 inline_forbidden_reason
1782 = G_("function %q+F can never be inlined "
1783 "because it uses variable sized variables");
1784 return node;
1787 default:
1788 break;
1791 return NULL_TREE;
1794 /* Return subexpression representing possible alloca call, if any. */
1795 static tree
1796 inline_forbidden_p (tree fndecl)
1798 location_t saved_loc = input_location;
1799 block_stmt_iterator bsi;
1800 basic_block bb;
1801 tree ret = NULL_TREE;
1803 FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (fndecl))
1804 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1806 ret = walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
1807 inline_forbidden_p_1, fndecl);
1808 if (ret)
1809 goto egress;
1812 egress:
1813 input_location = saved_loc;
1814 return ret;
1817 /* Returns nonzero if FN is a function that does not have any
1818 fundamental inline blocking properties. */
1820 static bool
1821 inlinable_function_p (tree fn)
1823 bool inlinable = true;
1825 /* If we've already decided this function shouldn't be inlined,
1826 there's no need to check again. */
1827 if (DECL_UNINLINABLE (fn))
1828 return false;
1830 /* See if there is any language-specific reason it cannot be
1831 inlined. (It is important that this hook be called early because
1832 in C++ it may result in template instantiation.)
1833 If the function is not inlinable for language-specific reasons,
1834 it is left up to the langhook to explain why. */
1835 inlinable = !lang_hooks.tree_inlining.cannot_inline_tree_fn (&fn);
1837 /* If we don't have the function body available, we can't inline it.
1838 However, this should not be recorded since we also get here for
1839 forward declared inline functions. Therefore, return at once. */
1840 if (!DECL_SAVED_TREE (fn))
1841 return false;
1843 /* If we're not inlining at all, then we cannot inline this function. */
1844 else if (!flag_inline_trees)
1845 inlinable = false;
1847 /* Only try to inline functions if DECL_INLINE is set. This should be
1848 true for all functions declared `inline', and for all other functions
1849 as well with -finline-functions.
1851 Don't think of disregarding DECL_INLINE when flag_inline_trees == 2;
1852 it's the front-end that must set DECL_INLINE in this case, because
1853 dwarf2out loses if a function that does not have DECL_INLINE set is
1854 inlined anyway. That is why we have both DECL_INLINE and
1855 DECL_DECLARED_INLINE_P. */
1856 /* FIXME: When flag_inline_trees dies, the check for flag_unit_at_a_time
1857 here should be redundant. */
1858 else if (!DECL_INLINE (fn) && !flag_unit_at_a_time)
1859 inlinable = false;
1861 else if (inline_forbidden_p (fn))
1863 /* See if we should warn about uninlinable functions. Previously,
1864 some of these warnings would be issued while trying to expand
1865 the function inline, but that would cause multiple warnings
1866 about functions that would for example call alloca. But since
1867 this a property of the function, just one warning is enough.
1868 As a bonus we can now give more details about the reason why a
1869 function is not inlinable.
1870 We only warn for functions declared `inline' by the user. */
1871 bool do_warning = (warn_inline
1872 && DECL_INLINE (fn)
1873 && DECL_DECLARED_INLINE_P (fn)
1874 && !DECL_IN_SYSTEM_HEADER (fn));
1876 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1877 sorry (inline_forbidden_reason, fn);
1878 else if (do_warning)
1879 warning (OPT_Winline, inline_forbidden_reason, fn);
1881 inlinable = false;
1884 /* Squirrel away the result so that we don't have to check again. */
1885 DECL_UNINLINABLE (fn) = !inlinable;
1887 return inlinable;
1890 /* Estimate the cost of a memory move. Use machine dependent
1891 word size and take possible memcpy call into account. */
1894 estimate_move_cost (tree type)
1896 HOST_WIDE_INT size;
1898 size = int_size_in_bytes (type);
1900 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO)
1901 /* Cost of a memcpy call, 3 arguments and the call. */
1902 return 4;
1903 else
1904 return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
1907 /* Used by estimate_num_insns. Estimate number of instructions seen
1908 by given statement. */
1910 static tree
1911 estimate_num_insns_1 (tree *tp, int *walk_subtrees, void *data)
1913 int *count = (int *) data;
1914 tree x = *tp;
1916 if (IS_TYPE_OR_DECL_P (x))
1918 *walk_subtrees = 0;
1919 return NULL;
1921 /* Assume that constants and references counts nothing. These should
1922 be majorized by amount of operations among them we count later
1923 and are common target of CSE and similar optimizations. */
1924 else if (CONSTANT_CLASS_P (x) || REFERENCE_CLASS_P (x))
1925 return NULL;
1927 switch (TREE_CODE (x))
1929 /* Containers have no cost. */
1930 case TREE_LIST:
1931 case TREE_VEC:
1932 case BLOCK:
1933 case COMPONENT_REF:
1934 case BIT_FIELD_REF:
1935 case INDIRECT_REF:
1936 case ALIGN_INDIRECT_REF:
1937 case MISALIGNED_INDIRECT_REF:
1938 case ARRAY_REF:
1939 case ARRAY_RANGE_REF:
1940 case OBJ_TYPE_REF:
1941 case EXC_PTR_EXPR: /* ??? */
1942 case FILTER_EXPR: /* ??? */
1943 case COMPOUND_EXPR:
1944 case BIND_EXPR:
1945 case WITH_CLEANUP_EXPR:
1946 case NOP_EXPR:
1947 case VIEW_CONVERT_EXPR:
1948 case SAVE_EXPR:
1949 case ADDR_EXPR:
1950 case COMPLEX_EXPR:
1951 case RANGE_EXPR:
1952 case CASE_LABEL_EXPR:
1953 case SSA_NAME:
1954 case CATCH_EXPR:
1955 case EH_FILTER_EXPR:
1956 case STATEMENT_LIST:
1957 case ERROR_MARK:
1958 case NON_LVALUE_EXPR:
1959 case FDESC_EXPR:
1960 case VA_ARG_EXPR:
1961 case TRY_CATCH_EXPR:
1962 case TRY_FINALLY_EXPR:
1963 case LABEL_EXPR:
1964 case GOTO_EXPR:
1965 case RETURN_EXPR:
1966 case EXIT_EXPR:
1967 case LOOP_EXPR:
1968 case PHI_NODE:
1969 case WITH_SIZE_EXPR:
1970 case OMP_CLAUSE:
1971 case OMP_RETURN:
1972 case OMP_CONTINUE:
1973 break;
1975 /* We don't account constants for now. Assume that the cost is amortized
1976 by operations that do use them. We may re-consider this decision once
1977 we are able to optimize the tree before estimating its size and break
1978 out static initializers. */
1979 case IDENTIFIER_NODE:
1980 case INTEGER_CST:
1981 case REAL_CST:
1982 case COMPLEX_CST:
1983 case VECTOR_CST:
1984 case STRING_CST:
1985 *walk_subtrees = 0;
1986 return NULL;
1988 /* Try to estimate the cost of assignments. We have three cases to
1989 deal with:
1990 1) Simple assignments to registers;
1991 2) Stores to things that must live in memory. This includes
1992 "normal" stores to scalars, but also assignments of large
1993 structures, or constructors of big arrays;
1994 3) TARGET_EXPRs.
1996 Let us look at the first two cases, assuming we have "a = b + C":
1997 <GIMPLE_MODIFY_STMT <var_decl "a">
1998 <plus_expr <var_decl "b"> <constant C>>
1999 If "a" is a GIMPLE register, the assignment to it is free on almost
2000 any target, because "a" usually ends up in a real register. Hence
2001 the only cost of this expression comes from the PLUS_EXPR, and we
2002 can ignore the GIMPLE_MODIFY_STMT.
2003 If "a" is not a GIMPLE register, the assignment to "a" will most
2004 likely be a real store, so the cost of the GIMPLE_MODIFY_STMT is the cost
2005 of moving something into "a", which we compute using the function
2006 estimate_move_cost.
2008 The third case deals with TARGET_EXPRs, for which the semantics are
2009 that a temporary is assigned, unless the TARGET_EXPR itself is being
2010 assigned to something else. In the latter case we do not need the
2011 temporary. E.g. in:
2012 <GIMPLE_MODIFY_STMT <var_decl "a"> <target_expr>>, the
2013 GIMPLE_MODIFY_STMT is free. */
2014 case INIT_EXPR:
2015 case GIMPLE_MODIFY_STMT:
2016 /* Is the right and side a TARGET_EXPR? */
2017 if (TREE_CODE (GENERIC_TREE_OPERAND (x, 1)) == TARGET_EXPR)
2018 break;
2019 /* ... fall through ... */
2021 case TARGET_EXPR:
2022 x = GENERIC_TREE_OPERAND (x, 0);
2023 /* Is this an assignments to a register? */
2024 if (is_gimple_reg (x))
2025 break;
2026 /* Otherwise it's a store, so fall through to compute the move cost. */
2028 case CONSTRUCTOR:
2029 *count += estimate_move_cost (TREE_TYPE (x));
2030 break;
2032 /* Assign cost of 1 to usual operations.
2033 ??? We may consider mapping RTL costs to this. */
2034 case COND_EXPR:
2035 case VEC_COND_EXPR:
2037 case PLUS_EXPR:
2038 case MINUS_EXPR:
2039 case MULT_EXPR:
2041 case FIX_TRUNC_EXPR:
2043 case NEGATE_EXPR:
2044 case FLOAT_EXPR:
2045 case MIN_EXPR:
2046 case MAX_EXPR:
2047 case ABS_EXPR:
2049 case LSHIFT_EXPR:
2050 case RSHIFT_EXPR:
2051 case LROTATE_EXPR:
2052 case RROTATE_EXPR:
2053 case VEC_LSHIFT_EXPR:
2054 case VEC_RSHIFT_EXPR:
2056 case BIT_IOR_EXPR:
2057 case BIT_XOR_EXPR:
2058 case BIT_AND_EXPR:
2059 case BIT_NOT_EXPR:
2061 case TRUTH_ANDIF_EXPR:
2062 case TRUTH_ORIF_EXPR:
2063 case TRUTH_AND_EXPR:
2064 case TRUTH_OR_EXPR:
2065 case TRUTH_XOR_EXPR:
2066 case TRUTH_NOT_EXPR:
2068 case LT_EXPR:
2069 case LE_EXPR:
2070 case GT_EXPR:
2071 case GE_EXPR:
2072 case EQ_EXPR:
2073 case NE_EXPR:
2074 case ORDERED_EXPR:
2075 case UNORDERED_EXPR:
2077 case UNLT_EXPR:
2078 case UNLE_EXPR:
2079 case UNGT_EXPR:
2080 case UNGE_EXPR:
2081 case UNEQ_EXPR:
2082 case LTGT_EXPR:
2084 case CONVERT_EXPR:
2086 case CONJ_EXPR:
2088 case PREDECREMENT_EXPR:
2089 case PREINCREMENT_EXPR:
2090 case POSTDECREMENT_EXPR:
2091 case POSTINCREMENT_EXPR:
2093 case SWITCH_EXPR:
2095 case ASM_EXPR:
2097 case REALIGN_LOAD_EXPR:
2099 case REDUC_MAX_EXPR:
2100 case REDUC_MIN_EXPR:
2101 case REDUC_PLUS_EXPR:
2102 case WIDEN_SUM_EXPR:
2103 case DOT_PROD_EXPR:
2104 case VEC_WIDEN_MULT_HI_EXPR:
2105 case VEC_WIDEN_MULT_LO_EXPR:
2106 case VEC_UNPACK_HI_EXPR:
2107 case VEC_UNPACK_LO_EXPR:
2108 case VEC_PACK_MOD_EXPR:
2109 case VEC_PACK_SAT_EXPR:
2111 case WIDEN_MULT_EXPR:
2113 case VEC_EXTRACT_EVEN_EXPR:
2114 case VEC_EXTRACT_ODD_EXPR:
2115 case VEC_INTERLEAVE_HIGH_EXPR:
2116 case VEC_INTERLEAVE_LOW_EXPR:
2118 case RESX_EXPR:
2119 *count += 1;
2120 break;
2122 /* Few special cases of expensive operations. This is useful
2123 to avoid inlining on functions having too many of these. */
2124 case TRUNC_DIV_EXPR:
2125 case CEIL_DIV_EXPR:
2126 case FLOOR_DIV_EXPR:
2127 case ROUND_DIV_EXPR:
2128 case EXACT_DIV_EXPR:
2129 case TRUNC_MOD_EXPR:
2130 case CEIL_MOD_EXPR:
2131 case FLOOR_MOD_EXPR:
2132 case ROUND_MOD_EXPR:
2133 case RDIV_EXPR:
2134 *count += 10;
2135 break;
2136 case CALL_EXPR:
2138 tree decl = get_callee_fndecl (x);
2139 tree arg;
2141 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2142 switch (DECL_FUNCTION_CODE (decl))
2144 case BUILT_IN_CONSTANT_P:
2145 *walk_subtrees = 0;
2146 return NULL_TREE;
2147 case BUILT_IN_EXPECT:
2148 return NULL_TREE;
2149 default:
2150 break;
2153 /* Our cost must be kept in sync with cgraph_estimate_size_after_inlining
2154 that does use function declaration to figure out the arguments. */
2155 if (!decl)
2157 for (arg = TREE_OPERAND (x, 1); arg; arg = TREE_CHAIN (arg))
2158 *count += estimate_move_cost (TREE_TYPE (TREE_VALUE (arg)));
2160 else
2162 for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
2163 *count += estimate_move_cost (TREE_TYPE (arg));
2166 *count += PARAM_VALUE (PARAM_INLINE_CALL_COST);
2167 break;
2170 case OMP_PARALLEL:
2171 case OMP_FOR:
2172 case OMP_SECTIONS:
2173 case OMP_SINGLE:
2174 case OMP_SECTION:
2175 case OMP_MASTER:
2176 case OMP_ORDERED:
2177 case OMP_CRITICAL:
2178 case OMP_ATOMIC:
2179 /* OpenMP directives are generally very expensive. */
2180 *count += 40;
2181 break;
2183 default:
2184 gcc_unreachable ();
2186 return NULL;
2189 /* Estimate number of instructions that will be created by expanding EXPR. */
2192 estimate_num_insns (tree expr)
2194 int num = 0;
2195 struct pointer_set_t *visited_nodes;
2196 basic_block bb;
2197 block_stmt_iterator bsi;
2198 struct function *my_function;
2200 /* If we're given an entire function, walk the CFG. */
2201 if (TREE_CODE (expr) == FUNCTION_DECL)
2203 my_function = DECL_STRUCT_FUNCTION (expr);
2204 gcc_assert (my_function && my_function->cfg);
2205 visited_nodes = pointer_set_create ();
2206 FOR_EACH_BB_FN (bb, my_function)
2208 for (bsi = bsi_start (bb);
2209 !bsi_end_p (bsi);
2210 bsi_next (&bsi))
2212 walk_tree (bsi_stmt_ptr (bsi), estimate_num_insns_1,
2213 &num, visited_nodes);
2216 pointer_set_destroy (visited_nodes);
2218 else
2219 walk_tree_without_duplicates (&expr, estimate_num_insns_1, &num);
2221 return num;
2224 typedef struct function *function_p;
2226 DEF_VEC_P(function_p);
2227 DEF_VEC_ALLOC_P(function_p,heap);
2229 /* Initialized with NOGC, making this poisonous to the garbage collector. */
2230 static VEC(function_p,heap) *cfun_stack;
2232 void
2233 push_cfun (struct function *new_cfun)
2235 VEC_safe_push (function_p, heap, cfun_stack, cfun);
2236 cfun = new_cfun;
2239 void
2240 pop_cfun (void)
2242 cfun = VEC_pop (function_p, cfun_stack);
2245 /* Install new lexical TREE_BLOCK underneath 'current_block'. */
2246 static void
2247 add_lexical_block (tree current_block, tree new_block)
2249 tree *blk_p;
2251 /* Walk to the last sub-block. */
2252 for (blk_p = &BLOCK_SUBBLOCKS (current_block);
2253 *blk_p;
2254 blk_p = &TREE_CHAIN (*blk_p))
2256 *blk_p = new_block;
2257 BLOCK_SUPERCONTEXT (new_block) = current_block;
2260 /* If *TP is a CALL_EXPR, replace it with its inline expansion. */
2262 static bool
2263 expand_call_inline (basic_block bb, tree stmt, tree *tp, void *data)
2265 copy_body_data *id;
2266 tree t;
2267 tree use_retvar;
2268 tree fn;
2269 splay_tree st;
2270 tree args;
2271 tree return_slot;
2272 tree modify_dest;
2273 location_t saved_location;
2274 struct cgraph_edge *cg_edge;
2275 const char *reason;
2276 basic_block return_block;
2277 edge e;
2278 block_stmt_iterator bsi, stmt_bsi;
2279 bool successfully_inlined = FALSE;
2280 bool purge_dead_abnormal_edges;
2281 tree t_step;
2282 tree var;
2284 /* See what we've got. */
2285 id = (copy_body_data *) data;
2286 t = *tp;
2288 /* Set input_location here so we get the right instantiation context
2289 if we call instantiate_decl from inlinable_function_p. */
2290 saved_location = input_location;
2291 if (EXPR_HAS_LOCATION (t))
2292 input_location = EXPR_LOCATION (t);
2294 /* From here on, we're only interested in CALL_EXPRs. */
2295 if (TREE_CODE (t) != CALL_EXPR)
2296 goto egress;
2298 /* First, see if we can figure out what function is being called.
2299 If we cannot, then there is no hope of inlining the function. */
2300 fn = get_callee_fndecl (t);
2301 if (!fn)
2302 goto egress;
2304 /* Turn forward declarations into real ones. */
2305 fn = cgraph_node (fn)->decl;
2307 /* If fn is a declaration of a function in a nested scope that was
2308 globally declared inline, we don't set its DECL_INITIAL.
2309 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
2310 C++ front-end uses it for cdtors to refer to their internal
2311 declarations, that are not real functions. Fortunately those
2312 don't have trees to be saved, so we can tell by checking their
2313 DECL_SAVED_TREE. */
2314 if (! DECL_INITIAL (fn)
2315 && DECL_ABSTRACT_ORIGIN (fn)
2316 && DECL_SAVED_TREE (DECL_ABSTRACT_ORIGIN (fn)))
2317 fn = DECL_ABSTRACT_ORIGIN (fn);
2319 /* Objective C and fortran still calls tree_rest_of_compilation directly.
2320 Kill this check once this is fixed. */
2321 if (!id->dst_node->analyzed)
2322 goto egress;
2324 cg_edge = cgraph_edge (id->dst_node, stmt);
2326 /* Constant propagation on argument done during previous inlining
2327 may create new direct call. Produce an edge for it. */
2328 if (!cg_edge)
2330 struct cgraph_node *dest = cgraph_node (fn);
2332 /* We have missing edge in the callgraph. This can happen in one case
2333 where previous inlining turned indirect call into direct call by
2334 constant propagating arguments. In all other cases we hit a bug
2335 (incorrect node sharing is most common reason for missing edges. */
2336 gcc_assert (dest->needed || !flag_unit_at_a_time);
2337 cgraph_create_edge (id->dst_node, dest, stmt,
2338 bb->count, bb->loop_depth)->inline_failed
2339 = N_("originally indirect function call not considered for inlining");
2340 goto egress;
2343 /* Don't try to inline functions that are not well-suited to
2344 inlining. */
2345 if (!cgraph_inline_p (cg_edge, &reason))
2347 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
2348 /* Avoid warnings during early inline pass. */
2349 && (!flag_unit_at_a_time || cgraph_global_info_ready))
2351 sorry ("inlining failed in call to %q+F: %s", fn, reason);
2352 sorry ("called from here");
2354 else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
2355 && !DECL_IN_SYSTEM_HEADER (fn)
2356 && strlen (reason)
2357 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
2358 /* Avoid warnings during early inline pass. */
2359 && (!flag_unit_at_a_time || cgraph_global_info_ready))
2361 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
2362 fn, reason);
2363 warning (OPT_Winline, "called from here");
2365 goto egress;
2367 fn = cg_edge->callee->decl;
2369 #ifdef ENABLE_CHECKING
2370 if (cg_edge->callee->decl != id->dst_node->decl)
2371 verify_cgraph_node (cg_edge->callee);
2372 #endif
2374 /* We will be inlining this callee. */
2375 id->eh_region = lookup_stmt_eh_region (stmt);
2377 /* Split the block holding the CALL_EXPR. */
2378 e = split_block (bb, stmt);
2379 bb = e->src;
2380 return_block = e->dest;
2381 remove_edge (e);
2383 /* split_block splits after the statement; work around this by
2384 moving the call into the second block manually. Not pretty,
2385 but seems easier than doing the CFG manipulation by hand
2386 when the CALL_EXPR is in the last statement of BB. */
2387 stmt_bsi = bsi_last (bb);
2388 bsi_remove (&stmt_bsi, false);
2390 /* If the CALL_EXPR was in the last statement of BB, it may have
2391 been the source of abnormal edges. In this case, schedule
2392 the removal of dead abnormal edges. */
2393 bsi = bsi_start (return_block);
2394 if (bsi_end_p (bsi))
2396 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
2397 purge_dead_abnormal_edges = true;
2399 else
2401 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
2402 purge_dead_abnormal_edges = false;
2405 stmt_bsi = bsi_start (return_block);
2407 /* Build a block containing code to initialize the arguments, the
2408 actual inline expansion of the body, and a label for the return
2409 statements within the function to jump to. The type of the
2410 statement expression is the return type of the function call. */
2411 id->block = make_node (BLOCK);
2412 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
2413 BLOCK_SOURCE_LOCATION (id->block) = input_location;
2414 add_lexical_block (TREE_BLOCK (stmt), id->block);
2416 /* Local declarations will be replaced by their equivalents in this
2417 map. */
2418 st = id->decl_map;
2419 id->decl_map = splay_tree_new (splay_tree_compare_pointers,
2420 NULL, NULL);
2422 /* Initialize the parameters. */
2423 args = TREE_OPERAND (t, 1);
2425 /* Record the function we are about to inline. */
2426 id->src_fn = fn;
2427 id->src_node = cg_edge->callee;
2428 id->src_cfun = DECL_STRUCT_FUNCTION (fn);
2430 initialize_inlined_parameters (id, args, TREE_OPERAND (t, 2), fn, bb);
2432 if (DECL_INITIAL (fn))
2433 add_lexical_block (id->block, remap_blocks (DECL_INITIAL (fn), id));
2435 /* Return statements in the function body will be replaced by jumps
2436 to the RET_LABEL. */
2438 gcc_assert (DECL_INITIAL (fn));
2439 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
2441 /* Find the lhs to which the result of this call is assigned. */
2442 return_slot = NULL;
2443 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
2445 modify_dest = GIMPLE_STMT_OPERAND (stmt, 0);
2447 /* The function which we are inlining might not return a value,
2448 in which case we should issue a warning that the function
2449 does not return a value. In that case the optimizers will
2450 see that the variable to which the value is assigned was not
2451 initialized. We do not want to issue a warning about that
2452 uninitialized variable. */
2453 if (DECL_P (modify_dest))
2454 TREE_NO_WARNING (modify_dest) = 1;
2455 if (CALL_EXPR_RETURN_SLOT_OPT (t))
2457 return_slot = modify_dest;
2458 modify_dest = NULL;
2461 else
2462 modify_dest = NULL;
2464 /* Declare the return variable for the function. */
2465 declare_return_variable (id, return_slot,
2466 modify_dest, &use_retvar);
2468 /* This is it. Duplicate the callee body. Assume callee is
2469 pre-gimplified. Note that we must not alter the caller
2470 function in any way before this point, as this CALL_EXPR may be
2471 a self-referential call; if we're calling ourselves, we need to
2472 duplicate our body before altering anything. */
2473 copy_body (id, bb->count, bb->frequency, bb, return_block);
2475 /* Add local vars in this inlined callee to caller. */
2476 t_step = id->src_cfun->unexpanded_var_list;
2477 for (; t_step; t_step = TREE_CHAIN (t_step))
2479 var = TREE_VALUE (t_step);
2480 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
2481 cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
2482 cfun->unexpanded_var_list);
2483 else
2484 cfun->unexpanded_var_list = tree_cons (NULL_TREE, remap_decl (var, id),
2485 cfun->unexpanded_var_list);
2488 /* Clean up. */
2489 splay_tree_delete (id->decl_map);
2490 id->decl_map = st;
2492 /* If the inlined function returns a result that we care about,
2493 clobber the CALL_EXPR with a reference to the return variable. */
2494 if (use_retvar && (TREE_CODE (bsi_stmt (stmt_bsi)) != CALL_EXPR))
2496 *tp = use_retvar;
2497 if (gimple_in_ssa_p (cfun))
2499 update_stmt (stmt);
2500 mark_symbols_for_renaming (stmt);
2502 maybe_clean_or_replace_eh_stmt (stmt, stmt);
2504 else
2505 /* We're modifying a TSI owned by gimple_expand_calls_inline();
2506 tsi_delink() will leave the iterator in a sane state. */
2508 /* Handle case of inlining function that miss return statement so
2509 return value becomes undefined. */
2510 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
2511 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == SSA_NAME)
2513 tree name = TREE_OPERAND (stmt, 0);
2514 tree var = SSA_NAME_VAR (TREE_OPERAND (stmt, 0));
2515 tree def = gimple_default_def (cfun, var);
2517 /* If the variable is used undefined, make this name undefined via
2518 move. */
2519 if (def)
2521 TREE_OPERAND (stmt, 1) = def;
2522 update_stmt (stmt);
2524 /* Otherwise make this variable undefined. */
2525 else
2527 bsi_remove (&stmt_bsi, true);
2528 set_default_def (var, name);
2529 SSA_NAME_DEF_STMT (name) = build_empty_stmt ();
2532 else
2533 bsi_remove (&stmt_bsi, true);
2536 if (purge_dead_abnormal_edges)
2537 tree_purge_dead_abnormal_call_edges (return_block);
2539 /* If the value of the new expression is ignored, that's OK. We
2540 don't warn about this for CALL_EXPRs, so we shouldn't warn about
2541 the equivalent inlined version either. */
2542 TREE_USED (*tp) = 1;
2544 /* Output the inlining info for this abstract function, since it has been
2545 inlined. If we don't do this now, we can lose the information about the
2546 variables in the function when the blocks get blown away as soon as we
2547 remove the cgraph node. */
2548 (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
2550 /* Update callgraph if needed. */
2551 cgraph_remove_node (cg_edge->callee);
2553 id->block = NULL_TREE;
2554 successfully_inlined = TRUE;
2556 egress:
2557 input_location = saved_location;
2558 return successfully_inlined;
2561 /* Expand call statements reachable from STMT_P.
2562 We can only have CALL_EXPRs as the "toplevel" tree code or nested
2563 in a GIMPLE_MODIFY_STMT. See tree-gimple.c:get_call_expr_in(). We can
2564 unfortunately not use that function here because we need a pointer
2565 to the CALL_EXPR, not the tree itself. */
2567 static bool
2568 gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
2570 block_stmt_iterator bsi;
2572 /* Register specific tree functions. */
2573 tree_register_cfg_hooks ();
2574 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2576 tree *expr_p = bsi_stmt_ptr (bsi);
2577 tree stmt = *expr_p;
2579 if (TREE_CODE (*expr_p) == GIMPLE_MODIFY_STMT)
2580 expr_p = &GIMPLE_STMT_OPERAND (*expr_p, 1);
2581 if (TREE_CODE (*expr_p) == WITH_SIZE_EXPR)
2582 expr_p = &TREE_OPERAND (*expr_p, 0);
2583 if (TREE_CODE (*expr_p) == CALL_EXPR)
2584 if (expand_call_inline (bb, stmt, expr_p, id))
2585 return true;
2587 return false;
2590 /* Walk all basic blocks created after FIRST and try to fold every statement
2591 in the STATEMENTS pointer set. */
2592 static void
2593 fold_marked_statements (int first, struct pointer_set_t *statements)
2595 for (;first < n_basic_blocks;first++)
2596 if (BASIC_BLOCK (first))
2598 block_stmt_iterator bsi;
2599 for (bsi = bsi_start (BASIC_BLOCK (first));
2600 !bsi_end_p (bsi); bsi_next (&bsi))
2601 if (pointer_set_contains (statements, bsi_stmt (bsi)))
2603 tree old_stmt = bsi_stmt (bsi);
2604 if (fold_stmt (bsi_stmt_ptr (bsi)))
2606 update_stmt (bsi_stmt (bsi));
2607 if (maybe_clean_or_replace_eh_stmt (old_stmt, bsi_stmt (bsi)))
2608 tree_purge_dead_eh_edges (BASIC_BLOCK (first));
2614 /* Expand calls to inline functions in the body of FN. */
2616 unsigned int
2617 optimize_inline_calls (tree fn)
2619 copy_body_data id;
2620 tree prev_fn;
2621 basic_block bb;
2622 int last = n_basic_blocks;
2623 /* There is no point in performing inlining if errors have already
2624 occurred -- and we might crash if we try to inline invalid
2625 code. */
2626 if (errorcount || sorrycount)
2627 return 0;
2629 /* Clear out ID. */
2630 memset (&id, 0, sizeof (id));
2632 id.src_node = id.dst_node = cgraph_node (fn);
2633 id.dst_fn = fn;
2634 /* Or any functions that aren't finished yet. */
2635 prev_fn = NULL_TREE;
2636 if (current_function_decl)
2638 id.dst_fn = current_function_decl;
2639 prev_fn = current_function_decl;
2642 id.copy_decl = copy_decl_maybe_to_var;
2643 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2644 id.transform_new_cfg = false;
2645 id.transform_return_to_modify = true;
2646 id.transform_lang_insert_block = false;
2647 id.statements_to_fold = pointer_set_create ();
2649 push_gimplify_context ();
2651 /* Reach the trees by walking over the CFG, and note the
2652 enclosing basic-blocks in the call edges. */
2653 /* We walk the blocks going forward, because inlined function bodies
2654 will split id->current_basic_block, and the new blocks will
2655 follow it; we'll trudge through them, processing their CALL_EXPRs
2656 along the way. */
2657 FOR_EACH_BB (bb)
2658 gimple_expand_calls_inline (bb, &id);
2660 pop_gimplify_context (NULL);
2661 /* Renumber the (code) basic_blocks consecutively. */
2662 compact_blocks ();
2663 /* Renumber the lexical scoping (non-code) blocks consecutively. */
2664 number_blocks (fn);
2666 #ifdef ENABLE_CHECKING
2668 struct cgraph_edge *e;
2670 verify_cgraph_node (id.dst_node);
2672 /* Double check that we inlined everything we are supposed to inline. */
2673 for (e = id.dst_node->callees; e; e = e->next_callee)
2674 gcc_assert (e->inline_failed);
2676 #endif
2677 /* We need to rescale frequencies again to peak at REG_BR_PROB_BASE
2678 as inlining loops might increase the maximum. */
2679 if (ENTRY_BLOCK_PTR->count)
2680 counts_to_freqs ();
2682 /* We are not going to maintain the cgraph edges up to date.
2683 Kill it so it won't confuse us. */
2684 cgraph_node_remove_callees (id.dst_node);
2686 fold_marked_statements (last, id.statements_to_fold);
2687 pointer_set_destroy (id.statements_to_fold);
2688 fold_cond_expr_cond ();
2689 /* We make no attempts to keep dominance info up-to-date. */
2690 free_dominance_info (CDI_DOMINATORS);
2691 free_dominance_info (CDI_POST_DOMINATORS);
2692 /* It would be nice to check SSA/CFG/statement consistency here, but it is
2693 not possible yet - the IPA passes might make various functions to not
2694 throw and they don't care to proactively update local EH info. This is
2695 done later in fixup_cfg pass that also execute the verification. */
2696 return (TODO_update_ssa | TODO_cleanup_cfg
2697 | (gimple_in_ssa_p (cfun) ? TODO_remove_unused_locals : 0));
2700 /* FN is a function that has a complete body, and CLONE is a function whose
2701 body is to be set to a copy of FN, mapping argument declarations according
2702 to the ARG_MAP splay_tree. */
2704 void
2705 clone_body (tree clone, tree fn, void *arg_map)
2707 copy_body_data id;
2709 /* Clone the body, as if we were making an inline call. But, remap the
2710 parameters in the callee to the parameters of caller. */
2711 memset (&id, 0, sizeof (id));
2712 id.src_fn = fn;
2713 id.dst_fn = clone;
2714 id.src_cfun = DECL_STRUCT_FUNCTION (fn);
2715 id.decl_map = (splay_tree)arg_map;
2717 id.copy_decl = copy_decl_no_change;
2718 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2719 id.transform_new_cfg = true;
2720 id.transform_return_to_modify = false;
2721 id.transform_lang_insert_block = true;
2723 /* We're not inside any EH region. */
2724 id.eh_region = -1;
2726 /* Actually copy the body. */
2727 append_to_statement_list_force (copy_generic_body (&id), &DECL_SAVED_TREE (clone));
2730 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
2732 tree
2733 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2735 enum tree_code code = TREE_CODE (*tp);
2736 enum tree_code_class cl = TREE_CODE_CLASS (code);
2738 /* We make copies of most nodes. */
2739 if (IS_EXPR_CODE_CLASS (cl)
2740 || IS_GIMPLE_STMT_CODE_CLASS (cl)
2741 || code == TREE_LIST
2742 || code == TREE_VEC
2743 || code == TYPE_DECL
2744 || code == OMP_CLAUSE)
2746 /* Because the chain gets clobbered when we make a copy, we save it
2747 here. */
2748 tree chain = NULL_TREE, new;
2750 if (!GIMPLE_TUPLE_P (*tp))
2751 chain = TREE_CHAIN (*tp);
2753 /* Copy the node. */
2754 new = copy_node (*tp);
2756 /* Propagate mudflap marked-ness. */
2757 if (flag_mudflap && mf_marked_p (*tp))
2758 mf_mark (new);
2760 *tp = new;
2762 /* Now, restore the chain, if appropriate. That will cause
2763 walk_tree to walk into the chain as well. */
2764 if (code == PARM_DECL
2765 || code == TREE_LIST
2766 || code == OMP_CLAUSE)
2767 TREE_CHAIN (*tp) = chain;
2769 /* For now, we don't update BLOCKs when we make copies. So, we
2770 have to nullify all BIND_EXPRs. */
2771 if (TREE_CODE (*tp) == BIND_EXPR)
2772 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
2774 else if (code == CONSTRUCTOR)
2776 /* CONSTRUCTOR nodes need special handling because
2777 we need to duplicate the vector of elements. */
2778 tree new;
2780 new = copy_node (*tp);
2782 /* Propagate mudflap marked-ness. */
2783 if (flag_mudflap && mf_marked_p (*tp))
2784 mf_mark (new);
2786 CONSTRUCTOR_ELTS (new) = VEC_copy (constructor_elt, gc,
2787 CONSTRUCTOR_ELTS (*tp));
2788 *tp = new;
2790 else if (TREE_CODE_CLASS (code) == tcc_type)
2791 *walk_subtrees = 0;
2792 else if (TREE_CODE_CLASS (code) == tcc_declaration)
2793 *walk_subtrees = 0;
2794 else if (TREE_CODE_CLASS (code) == tcc_constant)
2795 *walk_subtrees = 0;
2796 else
2797 gcc_assert (code != STATEMENT_LIST);
2798 return NULL_TREE;
2801 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
2802 information indicating to what new SAVE_EXPR this one should be mapped,
2803 use that one. Otherwise, create a new node and enter it in ST. FN is
2804 the function into which the copy will be placed. */
2806 static void
2807 remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
2809 splay_tree st = (splay_tree) st_;
2810 splay_tree_node n;
2811 tree t;
2813 /* See if we already encountered this SAVE_EXPR. */
2814 n = splay_tree_lookup (st, (splay_tree_key) *tp);
2816 /* If we didn't already remap this SAVE_EXPR, do so now. */
2817 if (!n)
2819 t = copy_node (*tp);
2821 /* Remember this SAVE_EXPR. */
2822 splay_tree_insert (st, (splay_tree_key) *tp, (splay_tree_value) t);
2823 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
2824 splay_tree_insert (st, (splay_tree_key) t, (splay_tree_value) t);
2826 else
2828 /* We've already walked into this SAVE_EXPR; don't do it again. */
2829 *walk_subtrees = 0;
2830 t = (tree) n->value;
2833 /* Replace this SAVE_EXPR with the copy. */
2834 *tp = t;
2837 /* Called via walk_tree. If *TP points to a DECL_STMT for a local label,
2838 copies the declaration and enters it in the splay_tree in DATA (which is
2839 really an `copy_body_data *'). */
2841 static tree
2842 mark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
2843 void *data)
2845 copy_body_data *id = (copy_body_data *) data;
2847 /* Don't walk into types. */
2848 if (TYPE_P (*tp))
2849 *walk_subtrees = 0;
2851 else if (TREE_CODE (*tp) == LABEL_EXPR)
2853 tree decl = TREE_OPERAND (*tp, 0);
2855 /* Copy the decl and remember the copy. */
2856 insert_decl_map (id, decl, id->copy_decl (decl, id));
2859 return NULL_TREE;
2862 /* Perform any modifications to EXPR required when it is unsaved. Does
2863 not recurse into EXPR's subtrees. */
2865 static void
2866 unsave_expr_1 (tree expr)
2868 switch (TREE_CODE (expr))
2870 case TARGET_EXPR:
2871 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
2872 It's OK for this to happen if it was part of a subtree that
2873 isn't immediately expanded, such as operand 2 of another
2874 TARGET_EXPR. */
2875 if (TREE_OPERAND (expr, 1))
2876 break;
2878 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
2879 TREE_OPERAND (expr, 3) = NULL_TREE;
2880 break;
2882 default:
2883 break;
2887 /* Called via walk_tree when an expression is unsaved. Using the
2888 splay_tree pointed to by ST (which is really a `splay_tree'),
2889 remaps all local declarations to appropriate replacements. */
2891 static tree
2892 unsave_r (tree *tp, int *walk_subtrees, void *data)
2894 copy_body_data *id = (copy_body_data *) data;
2895 splay_tree st = id->decl_map;
2896 splay_tree_node n;
2898 /* Only a local declaration (variable or label). */
2899 if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
2900 || TREE_CODE (*tp) == LABEL_DECL)
2902 /* Lookup the declaration. */
2903 n = splay_tree_lookup (st, (splay_tree_key) *tp);
2905 /* If it's there, remap it. */
2906 if (n)
2907 *tp = (tree) n->value;
2910 else if (TREE_CODE (*tp) == STATEMENT_LIST)
2911 copy_statement_list (tp);
2912 else if (TREE_CODE (*tp) == BIND_EXPR)
2913 copy_bind_expr (tp, walk_subtrees, id);
2914 else if (TREE_CODE (*tp) == SAVE_EXPR)
2915 remap_save_expr (tp, st, walk_subtrees);
2916 else
2918 copy_tree_r (tp, walk_subtrees, NULL);
2920 /* Do whatever unsaving is required. */
2921 unsave_expr_1 (*tp);
2924 /* Keep iterating. */
2925 return NULL_TREE;
2928 /* Copies everything in EXPR and replaces variables, labels
2929 and SAVE_EXPRs local to EXPR. */
2931 tree
2932 unsave_expr_now (tree expr)
2934 copy_body_data id;
2936 /* There's nothing to do for NULL_TREE. */
2937 if (expr == 0)
2938 return expr;
2940 /* Set up ID. */
2941 memset (&id, 0, sizeof (id));
2942 id.src_fn = current_function_decl;
2943 id.dst_fn = current_function_decl;
2944 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2946 id.copy_decl = copy_decl_no_change;
2947 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2948 id.transform_new_cfg = false;
2949 id.transform_return_to_modify = false;
2950 id.transform_lang_insert_block = false;
2952 /* Walk the tree once to find local labels. */
2953 walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
2955 /* Walk the tree again, copying, remapping, and unsaving. */
2956 walk_tree (&expr, unsave_r, &id, NULL);
2958 /* Clean up. */
2959 splay_tree_delete (id.decl_map);
2961 return expr;
2964 /* Allow someone to determine if SEARCH is a child of TOP from gdb. */
2966 static tree
2967 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
2969 if (*tp == data)
2970 return (tree) data;
2971 else
2972 return NULL;
2975 bool
2976 debug_find_tree (tree top, tree search)
2978 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
2982 /* Declare the variables created by the inliner. Add all the variables in
2983 VARS to BIND_EXPR. */
2985 static void
2986 declare_inline_vars (tree block, tree vars)
2988 tree t;
2989 for (t = vars; t; t = TREE_CHAIN (t))
2991 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
2992 gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
2993 cfun->unexpanded_var_list =
2994 tree_cons (NULL_TREE, t,
2995 cfun->unexpanded_var_list);
2998 if (block)
2999 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
3003 /* Copy NODE (which must be a DECL). The DECL originally was in the FROM_FN,
3004 but now it will be in the TO_FN. PARM_TO_VAR means enable PARM_DECL to
3005 VAR_DECL translation. */
3007 static tree
3008 copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
3010 /* Don't generate debug information for the copy if we wouldn't have
3011 generated it for the copy either. */
3012 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
3013 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
3015 /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
3016 declaration inspired this copy. */
3017 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
3019 /* The new variable/label has no RTL, yet. */
3020 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
3021 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
3022 SET_DECL_RTL (copy, NULL_RTX);
3024 /* These args would always appear unused, if not for this. */
3025 TREE_USED (copy) = 1;
3027 /* Set the context for the new declaration. */
3028 if (!DECL_CONTEXT (decl))
3029 /* Globals stay global. */
3031 else if (DECL_CONTEXT (decl) != id->src_fn)
3032 /* Things that weren't in the scope of the function we're inlining
3033 from aren't in the scope we're inlining to, either. */
3035 else if (TREE_STATIC (decl))
3036 /* Function-scoped static variables should stay in the original
3037 function. */
3039 else
3040 /* Ordinary automatic local variables are now in the scope of the
3041 new function. */
3042 DECL_CONTEXT (copy) = id->dst_fn;
3044 return copy;
3047 static tree
3048 copy_decl_to_var (tree decl, copy_body_data *id)
3050 tree copy, type;
3052 gcc_assert (TREE_CODE (decl) == PARM_DECL
3053 || TREE_CODE (decl) == RESULT_DECL);
3055 type = TREE_TYPE (decl);
3057 copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
3058 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
3059 TREE_READONLY (copy) = TREE_READONLY (decl);
3060 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
3061 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
3063 return copy_decl_for_dup_finish (id, decl, copy);
3066 /* Like copy_decl_to_var, but create a return slot object instead of a
3067 pointer variable for return by invisible reference. */
3069 static tree
3070 copy_result_decl_to_var (tree decl, copy_body_data *id)
3072 tree copy, type;
3074 gcc_assert (TREE_CODE (decl) == PARM_DECL
3075 || TREE_CODE (decl) == RESULT_DECL);
3077 type = TREE_TYPE (decl);
3078 if (DECL_BY_REFERENCE (decl))
3079 type = TREE_TYPE (type);
3081 copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
3082 TREE_READONLY (copy) = TREE_READONLY (decl);
3083 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
3084 if (!DECL_BY_REFERENCE (decl))
3086 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
3087 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
3090 return copy_decl_for_dup_finish (id, decl, copy);
3094 static tree
3095 copy_decl_no_change (tree decl, copy_body_data *id)
3097 tree copy;
3099 copy = copy_node (decl);
3101 /* The COPY is not abstract; it will be generated in DST_FN. */
3102 DECL_ABSTRACT (copy) = 0;
3103 lang_hooks.dup_lang_specific_decl (copy);
3105 /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
3106 been taken; it's for internal bookkeeping in expand_goto_internal. */
3107 if (TREE_CODE (copy) == LABEL_DECL)
3109 TREE_ADDRESSABLE (copy) = 0;
3110 LABEL_DECL_UID (copy) = -1;
3113 return copy_decl_for_dup_finish (id, decl, copy);
3116 static tree
3117 copy_decl_maybe_to_var (tree decl, copy_body_data *id)
3119 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
3120 return copy_decl_to_var (decl, id);
3121 else
3122 return copy_decl_no_change (decl, id);
3125 /* Return a copy of the function's argument tree. */
3126 static tree
3127 copy_arguments_for_versioning (tree orig_parm, copy_body_data * id)
3129 tree *arg_copy, *parg;
3131 arg_copy = &orig_parm;
3132 for (parg = arg_copy; *parg; parg = &TREE_CHAIN (*parg))
3134 tree new = remap_decl (*parg, id);
3135 lang_hooks.dup_lang_specific_decl (new);
3136 TREE_CHAIN (new) = TREE_CHAIN (*parg);
3137 *parg = new;
3139 return orig_parm;
3142 /* Return a copy of the function's static chain. */
3143 static tree
3144 copy_static_chain (tree static_chain, copy_body_data * id)
3146 tree *chain_copy, *pvar;
3148 chain_copy = &static_chain;
3149 for (pvar = chain_copy; *pvar; pvar = &TREE_CHAIN (*pvar))
3151 tree new = remap_decl (*pvar, id);
3152 lang_hooks.dup_lang_specific_decl (new);
3153 TREE_CHAIN (new) = TREE_CHAIN (*pvar);
3154 *pvar = new;
3156 return static_chain;
3159 /* Return true if the function is allowed to be versioned.
3160 This is a guard for the versioning functionality. */
3161 bool
3162 tree_versionable_function_p (tree fndecl)
3164 if (fndecl == NULL_TREE)
3165 return false;
3166 /* ??? There are cases where a function is
3167 uninlinable but can be versioned. */
3168 if (!tree_inlinable_function_p (fndecl))
3169 return false;
3171 return true;
3174 /* Create a copy of a function's tree.
3175 OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
3176 of the original function and the new copied function
3177 respectively. In case we want to replace a DECL
3178 tree with another tree while duplicating the function's
3179 body, TREE_MAP represents the mapping between these
3180 trees. If UPDATE_CLONES is set, the call_stmt fields
3181 of edges of clones of the function will be updated. */
3182 void
3183 tree_function_versioning (tree old_decl, tree new_decl, varray_type tree_map,
3184 bool update_clones)
3186 struct cgraph_node *old_version_node;
3187 struct cgraph_node *new_version_node;
3188 copy_body_data id;
3189 tree p;
3190 unsigned i;
3191 struct ipa_replace_map *replace_info;
3192 basic_block old_entry_block;
3193 tree t_step;
3194 tree old_current_function_decl = current_function_decl;
3196 gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
3197 && TREE_CODE (new_decl) == FUNCTION_DECL);
3198 DECL_POSSIBLY_INLINED (old_decl) = 1;
3200 old_version_node = cgraph_node (old_decl);
3201 new_version_node = cgraph_node (new_decl);
3203 DECL_ARTIFICIAL (new_decl) = 1;
3204 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
3206 /* Prepare the data structures for the tree copy. */
3207 memset (&id, 0, sizeof (id));
3209 /* Generate a new name for the new version. */
3210 if (!update_clones)
3212 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
3213 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
3214 SET_DECL_RTL (new_decl, NULL_RTX);
3215 id.statements_to_fold = pointer_set_create ();
3218 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
3219 id.src_fn = old_decl;
3220 id.dst_fn = new_decl;
3221 id.src_node = old_version_node;
3222 id.dst_node = new_version_node;
3223 id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
3225 id.copy_decl = copy_decl_no_change;
3226 id.transform_call_graph_edges
3227 = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
3228 id.transform_new_cfg = true;
3229 id.transform_return_to_modify = false;
3230 id.transform_lang_insert_block = false;
3232 current_function_decl = new_decl;
3233 old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
3234 (DECL_STRUCT_FUNCTION (old_decl));
3235 initialize_cfun (new_decl, old_decl,
3236 old_entry_block->count,
3237 old_entry_block->frequency);
3238 push_cfun (DECL_STRUCT_FUNCTION (new_decl));
3240 /* Copy the function's static chain. */
3241 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
3242 if (p)
3243 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
3244 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
3245 &id);
3246 /* Copy the function's arguments. */
3247 if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
3248 DECL_ARGUMENTS (new_decl) =
3249 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id);
3251 /* If there's a tree_map, prepare for substitution. */
3252 if (tree_map)
3253 for (i = 0; i < VARRAY_ACTIVE_SIZE (tree_map); i++)
3255 replace_info = VARRAY_GENERIC_PTR (tree_map, i);
3256 if (replace_info->replace_p)
3257 insert_decl_map (&id, replace_info->old_tree,
3258 replace_info->new_tree);
3261 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
3263 /* Renumber the lexical scoping (non-code) blocks consecutively. */
3264 number_blocks (id.dst_fn);
3266 if (DECL_STRUCT_FUNCTION (old_decl)->unexpanded_var_list != NULL_TREE)
3267 /* Add local vars. */
3268 for (t_step = DECL_STRUCT_FUNCTION (old_decl)->unexpanded_var_list;
3269 t_step; t_step = TREE_CHAIN (t_step))
3271 tree var = TREE_VALUE (t_step);
3272 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
3273 cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
3274 cfun->unexpanded_var_list);
3275 else
3276 cfun->unexpanded_var_list =
3277 tree_cons (NULL_TREE, remap_decl (var, &id),
3278 cfun->unexpanded_var_list);
3281 /* Copy the Function's body. */
3282 copy_body (&id, old_entry_block->count, old_entry_block->frequency, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR);
3284 if (DECL_RESULT (old_decl) != NULL_TREE)
3286 tree *res_decl = &DECL_RESULT (old_decl);
3287 DECL_RESULT (new_decl) = remap_decl (*res_decl, &id);
3288 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
3291 /* Renumber the lexical scoping (non-code) blocks consecutively. */
3292 number_blocks (new_decl);
3294 /* Clean up. */
3295 splay_tree_delete (id.decl_map);
3296 if (!update_clones)
3298 fold_marked_statements (0, id.statements_to_fold);
3299 pointer_set_destroy (id.statements_to_fold);
3300 fold_cond_expr_cond ();
3302 if (gimple_in_ssa_p (cfun))
3304 free_dominance_info (CDI_DOMINATORS);
3305 free_dominance_info (CDI_POST_DOMINATORS);
3306 if (!update_clones)
3307 delete_unreachable_blocks ();
3308 update_ssa (TODO_update_ssa);
3309 if (!update_clones)
3311 fold_cond_expr_cond ();
3312 if (need_ssa_update_p ())
3313 update_ssa (TODO_update_ssa);
3316 free_dominance_info (CDI_DOMINATORS);
3317 free_dominance_info (CDI_POST_DOMINATORS);
3318 pop_cfun ();
3319 current_function_decl = old_current_function_decl;
3320 gcc_assert (!current_function_decl
3321 || DECL_STRUCT_FUNCTION (current_function_decl) == cfun);
3322 return;
3325 /* Duplicate a type, fields and all. */
3327 tree
3328 build_duplicate_type (tree type)
3330 struct copy_body_data id;
3332 memset (&id, 0, sizeof (id));
3333 id.src_fn = current_function_decl;
3334 id.dst_fn = current_function_decl;
3335 id.src_cfun = cfun;
3336 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
3338 type = remap_type_1 (type, &id);
3340 splay_tree_delete (id.decl_map);
3342 return type;