2008-07-28 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / tree-inline.c
blob511270bffb366ca6e25a39d35db38ca49d6d399f
1 /* Tree inlining.
2 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Alexandre Oliva <aoliva@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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"
54 #include "target.h"
55 #include "integrate.h"
57 /* I'm not real happy about this, but we need to handle gimple and
58 non-gimple trees. */
59 #include "gimple.h"
61 /* Inlining, Cloning, Versioning, Parallelization
63 Inlining: a function body is duplicated, but the PARM_DECLs are
64 remapped into VAR_DECLs, and non-void RETURN_EXPRs become
65 MODIFY_EXPRs that store to a dedicated returned-value variable.
66 The duplicated eh_region info of the copy will later be appended
67 to the info for the caller; the eh_region info in copied throwing
68 statements and RESX_EXPRs is adjusted accordingly.
70 Cloning: (only in C++) We have one body for a con/de/structor, and
71 multiple function decls, each with a unique parameter list.
72 Duplicate the body, using the given splay tree; some parameters
73 will become constants (like 0 or 1).
75 Versioning: a function body is duplicated and the result is a new
76 function rather than into blocks of an existing function as with
77 inlining. Some parameters will become constants.
79 Parallelization: a region of a function is duplicated resulting in
80 a new function. Variables may be replaced with complex expressions
81 to enable shared variable semantics.
83 All of these will simultaneously lookup any callgraph edges. If
84 we're going to inline the duplicated function body, and the given
85 function has some cloned callgraph nodes (one for each place this
86 function will be inlined) those callgraph edges will be duplicated.
87 If we're cloning the body, those callgraph edges will be
88 updated to point into the new body. (Note that the original
89 callgraph node and edge list will not be altered.)
91 See the CALL_EXPR handling case in copy_tree_body_r (). */
93 /* To Do:
95 o In order to make inlining-on-trees work, we pessimized
96 function-local static constants. In particular, they are now
97 always output, even when not addressed. Fix this by treating
98 function-local static constants just like global static
99 constants; the back-end already knows not to output them if they
100 are not needed.
102 o Provide heuristics to clamp inlining of recursive template
103 calls? */
106 /* Weights that estimate_num_insns uses for heuristics in inlining. */
108 eni_weights eni_inlining_weights;
110 /* Weights that estimate_num_insns uses to estimate the size of the
111 produced code. */
113 eni_weights eni_size_weights;
115 /* Weights that estimate_num_insns uses to estimate the time necessary
116 to execute the produced code. */
118 eni_weights eni_time_weights;
120 /* Prototypes. */
122 static tree declare_return_variable (copy_body_data *, tree, tree, tree *);
123 static bool inlinable_function_p (tree);
124 static void remap_block (tree *, copy_body_data *);
125 static tree remap_decls (tree, copy_body_data *);
126 static void copy_bind_expr (tree *, int *, copy_body_data *);
127 static tree mark_local_for_remap_r (tree *, int *, void *);
128 static void unsave_expr_1 (tree);
129 static tree unsave_r (tree *, int *, void *);
130 static void declare_inline_vars (tree, tree);
131 static void remap_save_expr (tree *, void *, int *);
132 static void add_lexical_block (tree current_block, tree new_block);
133 static tree copy_decl_to_var (tree, copy_body_data *);
134 static tree copy_result_decl_to_var (tree, copy_body_data *);
135 static tree copy_decl_maybe_to_var (tree, copy_body_data *);
136 static gimple remap_gimple_stmt (gimple, copy_body_data *);
138 /* Insert a tree->tree mapping for ID. Despite the name suggests
139 that the trees should be variables, it is used for more than that. */
141 void
142 insert_decl_map (copy_body_data *id, tree key, tree value)
144 *pointer_map_insert (id->decl_map, key) = value;
146 /* Always insert an identity map as well. If we see this same new
147 node again, we won't want to duplicate it a second time. */
148 if (key != value)
149 *pointer_map_insert (id->decl_map, value) = value;
152 /* Construct new SSA name for old NAME. ID is the inline context. */
154 static tree
155 remap_ssa_name (tree name, copy_body_data *id)
157 tree new;
158 tree *n;
160 gcc_assert (TREE_CODE (name) == SSA_NAME);
162 n = (tree *) pointer_map_contains (id->decl_map, name);
163 if (n)
164 return *n;
166 /* Do not set DEF_STMT yet as statement is not copied yet. We do that
167 in copy_bb. */
168 new = remap_decl (SSA_NAME_VAR (name), id);
170 /* We might've substituted constant or another SSA_NAME for
171 the variable.
173 Replace the SSA name representing RESULT_DECL by variable during
174 inlining: this saves us from need to introduce PHI node in a case
175 return value is just partly initialized. */
176 if ((TREE_CODE (new) == VAR_DECL || TREE_CODE (new) == PARM_DECL)
177 && (TREE_CODE (SSA_NAME_VAR (name)) != RESULT_DECL
178 || !id->transform_return_to_modify))
180 new = make_ssa_name (new, NULL);
181 insert_decl_map (id, name, new);
182 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new)
183 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
184 TREE_TYPE (new) = TREE_TYPE (SSA_NAME_VAR (new));
185 if (gimple_nop_p (SSA_NAME_DEF_STMT (name)))
187 /* By inlining function having uninitialized variable, we might
188 extend the lifetime (variable might get reused). This cause
189 ICE in the case we end up extending lifetime of SSA name across
190 abnormal edge, but also increase register pressure.
192 We simply initialize all uninitialized vars by 0 except
193 for case we are inlining to very first BB. We can avoid
194 this for all BBs that are not inside strongly connected
195 regions of the CFG, but this is expensive to test. */
196 if (id->entry_bb
197 && is_gimple_reg (SSA_NAME_VAR (name))
198 && TREE_CODE (SSA_NAME_VAR (name)) != PARM_DECL
199 && (id->entry_bb != EDGE_SUCC (ENTRY_BLOCK_PTR, 0)->dest
200 || EDGE_COUNT (id->entry_bb->preds) != 1))
202 gimple_stmt_iterator gsi = gsi_last_bb (id->entry_bb);
203 gimple init_stmt;
205 init_stmt = gimple_build_assign (new,
206 fold_convert (TREE_TYPE (new),
207 integer_zero_node));
208 gsi_insert_after (&gsi, init_stmt, GSI_NEW_STMT);
209 SSA_NAME_IS_DEFAULT_DEF (new) = 0;
211 else
213 SSA_NAME_DEF_STMT (new) = gimple_build_nop ();
214 if (gimple_default_def (id->src_cfun, SSA_NAME_VAR (name))
215 == name)
216 set_default_def (SSA_NAME_VAR (new), new);
220 else
221 insert_decl_map (id, name, new);
222 return new;
225 /* Remap DECL during the copying of the BLOCK tree for the function. */
227 tree
228 remap_decl (tree decl, copy_body_data *id)
230 tree *n;
231 tree fn;
233 /* We only remap local variables in the current function. */
234 fn = id->src_fn;
236 /* See if we have remapped this declaration. */
238 n = (tree *) pointer_map_contains (id->decl_map, decl);
240 /* If we didn't already have an equivalent for this declaration,
241 create one now. */
242 if (!n)
244 /* Make a copy of the variable or label. */
245 tree t = id->copy_decl (decl, id);
247 /* Remember it, so that if we encounter this local entity again
248 we can reuse this copy. Do this early because remap_type may
249 need this decl for TYPE_STUB_DECL. */
250 insert_decl_map (id, decl, t);
252 if (!DECL_P (t))
253 return t;
255 /* Remap types, if necessary. */
256 TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
257 if (TREE_CODE (t) == TYPE_DECL)
258 DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
260 /* Remap sizes as necessary. */
261 walk_tree (&DECL_SIZE (t), copy_tree_body_r, id, NULL);
262 walk_tree (&DECL_SIZE_UNIT (t), copy_tree_body_r, id, NULL);
264 /* If fields, do likewise for offset and qualifier. */
265 if (TREE_CODE (t) == FIELD_DECL)
267 walk_tree (&DECL_FIELD_OFFSET (t), copy_tree_body_r, id, NULL);
268 if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
269 walk_tree (&DECL_QUALIFIER (t), copy_tree_body_r, id, NULL);
272 if (cfun && gimple_in_ssa_p (cfun)
273 && (TREE_CODE (t) == VAR_DECL
274 || TREE_CODE (t) == RESULT_DECL || TREE_CODE (t) == PARM_DECL))
276 tree def = gimple_default_def (id->src_cfun, decl);
277 get_var_ann (t);
278 if (TREE_CODE (decl) != PARM_DECL && def)
280 tree map = remap_ssa_name (def, id);
281 /* Watch out RESULT_DECLs whose SSA names map directly
282 to them. */
283 if (TREE_CODE (map) == SSA_NAME
284 && gimple_nop_p (SSA_NAME_DEF_STMT (map)))
285 set_default_def (t, map);
287 add_referenced_var (t);
289 return t;
292 return unshare_expr (*n);
295 static tree
296 remap_type_1 (tree type, copy_body_data *id)
298 tree new, t;
300 /* We do need a copy. build and register it now. If this is a pointer or
301 reference type, remap the designated type and make a new pointer or
302 reference type. */
303 if (TREE_CODE (type) == POINTER_TYPE)
305 new = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
306 TYPE_MODE (type),
307 TYPE_REF_CAN_ALIAS_ALL (type));
308 insert_decl_map (id, type, new);
309 return new;
311 else if (TREE_CODE (type) == REFERENCE_TYPE)
313 new = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
314 TYPE_MODE (type),
315 TYPE_REF_CAN_ALIAS_ALL (type));
316 insert_decl_map (id, type, new);
317 return new;
319 else
320 new = copy_node (type);
322 insert_decl_map (id, type, new);
324 /* This is a new type, not a copy of an old type. Need to reassociate
325 variants. We can handle everything except the main variant lazily. */
326 t = TYPE_MAIN_VARIANT (type);
327 if (type != t)
329 t = remap_type (t, id);
330 TYPE_MAIN_VARIANT (new) = t;
331 TYPE_NEXT_VARIANT (new) = TYPE_NEXT_VARIANT (t);
332 TYPE_NEXT_VARIANT (t) = new;
334 else
336 TYPE_MAIN_VARIANT (new) = new;
337 TYPE_NEXT_VARIANT (new) = NULL;
340 if (TYPE_STUB_DECL (type))
341 TYPE_STUB_DECL (new) = remap_decl (TYPE_STUB_DECL (type), id);
343 /* Lazily create pointer and reference types. */
344 TYPE_POINTER_TO (new) = NULL;
345 TYPE_REFERENCE_TO (new) = NULL;
347 switch (TREE_CODE (new))
349 case INTEGER_TYPE:
350 case REAL_TYPE:
351 case FIXED_POINT_TYPE:
352 case ENUMERAL_TYPE:
353 case BOOLEAN_TYPE:
354 t = TYPE_MIN_VALUE (new);
355 if (t && TREE_CODE (t) != INTEGER_CST)
356 walk_tree (&TYPE_MIN_VALUE (new), copy_tree_body_r, id, NULL);
358 t = TYPE_MAX_VALUE (new);
359 if (t && TREE_CODE (t) != INTEGER_CST)
360 walk_tree (&TYPE_MAX_VALUE (new), copy_tree_body_r, id, NULL);
361 return new;
363 case FUNCTION_TYPE:
364 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
365 walk_tree (&TYPE_ARG_TYPES (new), copy_tree_body_r, id, NULL);
366 return new;
368 case ARRAY_TYPE:
369 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
370 TYPE_DOMAIN (new) = remap_type (TYPE_DOMAIN (new), id);
371 break;
373 case RECORD_TYPE:
374 case UNION_TYPE:
375 case QUAL_UNION_TYPE:
377 tree f, nf = NULL;
379 for (f = TYPE_FIELDS (new); f ; f = TREE_CHAIN (f))
381 t = remap_decl (f, id);
382 DECL_CONTEXT (t) = new;
383 TREE_CHAIN (t) = nf;
384 nf = t;
386 TYPE_FIELDS (new) = nreverse (nf);
388 break;
390 case OFFSET_TYPE:
391 default:
392 /* Shouldn't have been thought variable sized. */
393 gcc_unreachable ();
396 walk_tree (&TYPE_SIZE (new), copy_tree_body_r, id, NULL);
397 walk_tree (&TYPE_SIZE_UNIT (new), copy_tree_body_r, id, NULL);
399 return new;
402 tree
403 remap_type (tree type, copy_body_data *id)
405 tree *node;
406 tree tmp;
408 if (type == NULL)
409 return type;
411 /* See if we have remapped this type. */
412 node = (tree *) pointer_map_contains (id->decl_map, type);
413 if (node)
414 return *node;
416 /* The type only needs remapping if it's variably modified. */
417 if (! variably_modified_type_p (type, id->src_fn))
419 insert_decl_map (id, type, type);
420 return type;
423 id->remapping_type_depth++;
424 tmp = remap_type_1 (type, id);
425 id->remapping_type_depth--;
427 return tmp;
430 static tree
431 remap_decls (tree decls, copy_body_data *id)
433 tree old_var;
434 tree new_decls = NULL_TREE;
436 /* Remap its variables. */
437 for (old_var = decls; old_var; old_var = TREE_CHAIN (old_var))
439 tree new_var;
441 /* We cannot chain the local static declarations into the local_decls
442 as we can't duplicate them or break one decl rule. Go ahead
443 and link them into local_decls. */
445 if (!auto_var_in_fn_p (old_var, id->src_fn)
446 && !DECL_EXTERNAL (old_var))
448 cfun->local_decls = tree_cons (NULL_TREE, old_var,
449 cfun->local_decls);
450 continue;
453 /* Remap the variable. */
454 new_var = remap_decl (old_var, id);
456 /* If we didn't remap this variable, we can't mess with its
457 TREE_CHAIN. If we remapped this variable to the return slot, it's
458 already declared somewhere else, so don't declare it here. */
459 if (!new_var || new_var == id->retvar)
461 else
463 gcc_assert (DECL_P (new_var));
464 TREE_CHAIN (new_var) = new_decls;
465 new_decls = new_var;
469 return nreverse (new_decls);
472 /* Copy the BLOCK to contain remapped versions of the variables
473 therein. And hook the new block into the block-tree. */
475 static void
476 remap_block (tree *block, copy_body_data *id)
478 tree old_block;
479 tree new_block;
480 tree fn;
482 /* Make the new block. */
483 old_block = *block;
484 new_block = make_node (BLOCK);
485 TREE_USED (new_block) = TREE_USED (old_block);
486 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
487 BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
488 *block = new_block;
490 /* Remap its variables. */
491 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block), id);
493 fn = id->dst_fn;
495 if (id->transform_lang_insert_block)
496 id->transform_lang_insert_block (new_block);
498 /* Remember the remapped block. */
499 insert_decl_map (id, old_block, new_block);
502 /* Copy the whole block tree and root it in id->block. */
503 static tree
504 remap_blocks (tree block, copy_body_data *id)
506 tree t;
507 tree new = block;
509 if (!block)
510 return NULL;
512 remap_block (&new, id);
513 gcc_assert (new != block);
514 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
515 add_lexical_block (new, remap_blocks (t, id));
516 return new;
519 static void
520 copy_statement_list (tree *tp)
522 tree_stmt_iterator oi, ni;
523 tree new;
525 new = alloc_stmt_list ();
526 ni = tsi_start (new);
527 oi = tsi_start (*tp);
528 *tp = new;
530 for (; !tsi_end_p (oi); tsi_next (&oi))
531 tsi_link_after (&ni, tsi_stmt (oi), TSI_NEW_STMT);
534 static void
535 copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
537 tree block = BIND_EXPR_BLOCK (*tp);
538 /* Copy (and replace) the statement. */
539 copy_tree_r (tp, walk_subtrees, NULL);
540 if (block)
542 remap_block (&block, id);
543 BIND_EXPR_BLOCK (*tp) = block;
546 if (BIND_EXPR_VARS (*tp))
547 /* This will remap a lot of the same decls again, but this should be
548 harmless. */
549 BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), id);
553 /* Create a new gimple_seq by remapping all the statements in BODY
554 using the inlining information in ID. */
556 gimple_seq
557 remap_gimple_seq (gimple_seq body, copy_body_data *id)
559 gimple_stmt_iterator si;
560 gimple_seq new_body = NULL;
562 for (si = gsi_start (body); !gsi_end_p (si); gsi_next (&si))
564 gimple new_stmt = remap_gimple_stmt (gsi_stmt (si), id);
565 gimple_seq_add_stmt (&new_body, new_stmt);
568 return new_body;
572 /* Copy a GIMPLE_BIND statement STMT, remapping all the symbols in its
573 block using the mapping information in ID. */
575 static gimple
576 copy_gimple_bind (gimple stmt, copy_body_data *id)
578 gimple new_bind;
579 tree new_block, new_vars;
580 gimple_seq body, new_body;
582 /* Copy the statement. Note that we purposely don't use copy_stmt
583 here because we need to remap statements as we copy. */
584 body = gimple_bind_body (stmt);
585 new_body = remap_gimple_seq (body, id);
587 new_block = gimple_bind_block (stmt);
588 if (new_block)
589 remap_block (&new_block, id);
591 /* This will remap a lot of the same decls again, but this should be
592 harmless. */
593 new_vars = gimple_bind_vars (stmt);
594 if (new_vars)
595 new_vars = remap_decls (new_vars, id);
597 new_bind = gimple_build_bind (new_vars, new_body, new_block);
599 return new_bind;
603 /* Remap the GIMPLE operand pointed to by *TP. DATA is really a
604 'struct walk_stmt_info *'. DATA->INFO is a 'copy_body_data *'.
605 WALK_SUBTREES is used to indicate walk_gimple_op whether to keep
606 recursing into the children nodes of *TP. */
608 static tree
609 remap_gimple_op_r (tree *tp, int *walk_subtrees, void *data)
611 struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
612 copy_body_data *id = (copy_body_data *) wi_p->info;
613 tree fn = id->src_fn;
615 if (TREE_CODE (*tp) == SSA_NAME)
617 *tp = remap_ssa_name (*tp, id);
618 *walk_subtrees = 0;
619 return NULL;
621 else if (auto_var_in_fn_p (*tp, fn))
623 /* Local variables and labels need to be replaced by equivalent
624 variables. We don't want to copy static variables; there's
625 only one of those, no matter how many times we inline the
626 containing function. Similarly for globals from an outer
627 function. */
628 tree new_decl;
630 /* Remap the declaration. */
631 new_decl = remap_decl (*tp, id);
632 gcc_assert (new_decl);
633 /* Replace this variable with the copy. */
634 STRIP_TYPE_NOPS (new_decl);
635 *tp = new_decl;
636 *walk_subtrees = 0;
638 else if (TREE_CODE (*tp) == STATEMENT_LIST)
639 gcc_unreachable ();
640 else if (TREE_CODE (*tp) == SAVE_EXPR)
641 gcc_unreachable ();
642 else if (TREE_CODE (*tp) == LABEL_DECL
643 && (!DECL_CONTEXT (*tp)
644 || decl_function_context (*tp) == id->src_fn))
645 /* These may need to be remapped for EH handling. */
646 *tp = remap_decl (*tp, id);
647 else if (TYPE_P (*tp))
648 /* Types may need remapping as well. */
649 *tp = remap_type (*tp, id);
650 else if (CONSTANT_CLASS_P (*tp))
652 /* If this is a constant, we have to copy the node iff the type
653 will be remapped. copy_tree_r will not copy a constant. */
654 tree new_type = remap_type (TREE_TYPE (*tp), id);
656 if (new_type == TREE_TYPE (*tp))
657 *walk_subtrees = 0;
659 else if (TREE_CODE (*tp) == INTEGER_CST)
660 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
661 TREE_INT_CST_HIGH (*tp));
662 else
664 *tp = copy_node (*tp);
665 TREE_TYPE (*tp) = new_type;
668 else
670 /* Otherwise, just copy the node. Note that copy_tree_r already
671 knows not to copy VAR_DECLs, etc., so this is safe. */
672 if (TREE_CODE (*tp) == INDIRECT_REF)
674 /* Get rid of *& from inline substitutions that can happen when a
675 pointer argument is an ADDR_EXPR. */
676 tree decl = TREE_OPERAND (*tp, 0);
677 tree *n;
679 n = (tree *) pointer_map_contains (id->decl_map, decl);
680 if (n)
682 tree type, new, old;
684 /* If we happen to get an ADDR_EXPR in n->value, strip
685 it manually here as we'll eventually get ADDR_EXPRs
686 which lie about their types pointed to. In this case
687 build_fold_indirect_ref wouldn't strip the
688 INDIRECT_REF, but we absolutely rely on that. As
689 fold_indirect_ref does other useful transformations,
690 try that first, though. */
691 type = TREE_TYPE (TREE_TYPE (*n));
692 new = unshare_expr (*n);
693 old = *tp;
694 *tp = gimple_fold_indirect_ref (new);
695 if (!*tp)
697 if (TREE_CODE (new) == ADDR_EXPR)
699 *tp = fold_indirect_ref_1 (type, new);
700 /* ??? We should either assert here or build
701 a VIEW_CONVERT_EXPR instead of blindly leaking
702 incompatible types to our IL. */
703 if (! *tp)
704 *tp = TREE_OPERAND (new, 0);
706 else
708 *tp = build1 (INDIRECT_REF, type, new);
709 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
712 *walk_subtrees = 0;
713 return NULL;
717 /* Here is the "usual case". Copy this tree node, and then
718 tweak some special cases. */
719 copy_tree_r (tp, walk_subtrees, NULL);
721 /* Global variables we haven't seen yet need to go into referenced
722 vars. If not referenced from types only. */
723 if (gimple_in_ssa_p (cfun)
724 && TREE_CODE (*tp) == VAR_DECL
725 && id->remapping_type_depth == 0)
726 add_referenced_var (*tp);
728 /* We should never have TREE_BLOCK set on non-statements. */
729 if (EXPR_P (*tp))
730 gcc_assert (!TREE_BLOCK (*tp));
732 if (TREE_CODE (*tp) != OMP_CLAUSE)
733 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
735 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
737 /* The copied TARGET_EXPR has never been expanded, even if the
738 original node was expanded already. */
739 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
740 TREE_OPERAND (*tp, 3) = NULL_TREE;
742 else if (TREE_CODE (*tp) == ADDR_EXPR)
744 /* Variable substitution need not be simple. In particular,
745 the INDIRECT_REF substitution above. Make sure that
746 TREE_CONSTANT and friends are up-to-date. But make sure
747 to not improperly set TREE_BLOCK on some sub-expressions. */
748 int invariant = is_gimple_min_invariant (*tp);
749 tree block = id->block;
750 id->block = NULL_TREE;
751 walk_tree (&TREE_OPERAND (*tp, 0), copy_tree_body_r, id, NULL);
752 id->block = block;
754 /* Handle the case where we substituted an INDIRECT_REF
755 into the operand of the ADDR_EXPR. */
756 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
757 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
758 else
759 recompute_tree_invariant_for_addr_expr (*tp);
761 /* If this used to be invariant, but is not any longer,
762 then regimplification is probably needed. */
763 if (invariant && !is_gimple_min_invariant (*tp))
764 id->regimplify = true;
766 *walk_subtrees = 0;
770 /* Keep iterating. */
771 return NULL_TREE;
775 /* Called from copy_body_id via walk_tree. DATA is really a
776 `copy_body_data *'. */
778 tree
779 copy_tree_body_r (tree *tp, int *walk_subtrees, void *data)
781 copy_body_data *id = (copy_body_data *) data;
782 tree fn = id->src_fn;
783 tree new_block;
785 /* Begin by recognizing trees that we'll completely rewrite for the
786 inlining context. Our output for these trees is completely
787 different from out input (e.g. RETURN_EXPR is deleted, and morphs
788 into an edge). Further down, we'll handle trees that get
789 duplicated and/or tweaked. */
791 /* When requested, RETURN_EXPRs should be transformed to just the
792 contained MODIFY_EXPR. The branch semantics of the return will
793 be handled elsewhere by manipulating the CFG rather than a statement. */
794 if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
796 tree assignment = TREE_OPERAND (*tp, 0);
798 /* If we're returning something, just turn that into an
799 assignment into the equivalent of the original RESULT_DECL.
800 If the "assignment" is just the result decl, the result
801 decl has already been set (e.g. a recent "foo (&result_decl,
802 ...)"); just toss the entire RETURN_EXPR. */
803 if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
805 /* Replace the RETURN_EXPR with (a copy of) the
806 MODIFY_EXPR hanging underneath. */
807 *tp = copy_node (assignment);
809 else /* Else the RETURN_EXPR returns no value. */
811 *tp = NULL;
812 return (tree) (void *)1;
815 else if (TREE_CODE (*tp) == SSA_NAME)
817 *tp = remap_ssa_name (*tp, id);
818 *walk_subtrees = 0;
819 return NULL;
822 /* Local variables and labels need to be replaced by equivalent
823 variables. We don't want to copy static variables; there's only
824 one of those, no matter how many times we inline the containing
825 function. Similarly for globals from an outer function. */
826 else if (auto_var_in_fn_p (*tp, fn))
828 tree new_decl;
830 /* Remap the declaration. */
831 new_decl = remap_decl (*tp, id);
832 gcc_assert (new_decl);
833 /* Replace this variable with the copy. */
834 STRIP_TYPE_NOPS (new_decl);
835 *tp = new_decl;
836 *walk_subtrees = 0;
838 else if (TREE_CODE (*tp) == STATEMENT_LIST)
839 copy_statement_list (tp);
840 else if (TREE_CODE (*tp) == SAVE_EXPR)
841 remap_save_expr (tp, id->decl_map, walk_subtrees);
842 else if (TREE_CODE (*tp) == LABEL_DECL
843 && (! DECL_CONTEXT (*tp)
844 || decl_function_context (*tp) == id->src_fn))
845 /* These may need to be remapped for EH handling. */
846 *tp = remap_decl (*tp, id);
847 else if (TREE_CODE (*tp) == BIND_EXPR)
848 copy_bind_expr (tp, walk_subtrees, id);
849 /* Types may need remapping as well. */
850 else if (TYPE_P (*tp))
851 *tp = remap_type (*tp, id);
853 /* If this is a constant, we have to copy the node iff the type will be
854 remapped. copy_tree_r will not copy a constant. */
855 else if (CONSTANT_CLASS_P (*tp))
857 tree new_type = remap_type (TREE_TYPE (*tp), id);
859 if (new_type == TREE_TYPE (*tp))
860 *walk_subtrees = 0;
862 else if (TREE_CODE (*tp) == INTEGER_CST)
863 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
864 TREE_INT_CST_HIGH (*tp));
865 else
867 *tp = copy_node (*tp);
868 TREE_TYPE (*tp) = new_type;
872 /* Otherwise, just copy the node. Note that copy_tree_r already
873 knows not to copy VAR_DECLs, etc., so this is safe. */
874 else
876 /* Here we handle trees that are not completely rewritten.
877 First we detect some inlining-induced bogosities for
878 discarding. */
879 if (TREE_CODE (*tp) == MODIFY_EXPR
880 && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
881 && (auto_var_in_fn_p (TREE_OPERAND (*tp, 0), fn)))
883 /* Some assignments VAR = VAR; don't generate any rtl code
884 and thus don't count as variable modification. Avoid
885 keeping bogosities like 0 = 0. */
886 tree decl = TREE_OPERAND (*tp, 0), value;
887 tree *n;
889 n = (tree *) pointer_map_contains (id->decl_map, decl);
890 if (n)
892 value = *n;
893 STRIP_TYPE_NOPS (value);
894 if (TREE_CONSTANT (value) || TREE_READONLY (value))
896 *tp = build_empty_stmt ();
897 return copy_tree_body_r (tp, walk_subtrees, data);
901 else if (TREE_CODE (*tp) == INDIRECT_REF)
903 /* Get rid of *& from inline substitutions that can happen when a
904 pointer argument is an ADDR_EXPR. */
905 tree decl = TREE_OPERAND (*tp, 0);
906 tree *n;
908 n = (tree *) pointer_map_contains (id->decl_map, decl);
909 if (n)
911 tree new;
912 tree old;
913 /* If we happen to get an ADDR_EXPR in n->value, strip
914 it manually here as we'll eventually get ADDR_EXPRs
915 which lie about their types pointed to. In this case
916 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
917 but we absolutely rely on that. As fold_indirect_ref
918 does other useful transformations, try that first, though. */
919 tree type = TREE_TYPE (TREE_TYPE (*n));
920 new = unshare_expr (*n);
921 old = *tp;
922 *tp = gimple_fold_indirect_ref (new);
923 if (! *tp)
925 if (TREE_CODE (new) == ADDR_EXPR)
927 *tp = fold_indirect_ref_1 (type, new);
928 /* ??? We should either assert here or build
929 a VIEW_CONVERT_EXPR instead of blindly leaking
930 incompatible types to our IL. */
931 if (! *tp)
932 *tp = TREE_OPERAND (new, 0);
934 else
936 *tp = build1 (INDIRECT_REF, type, new);
937 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
938 TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
941 *walk_subtrees = 0;
942 return NULL;
946 /* Here is the "usual case". Copy this tree node, and then
947 tweak some special cases. */
948 copy_tree_r (tp, walk_subtrees, NULL);
950 /* Global variables we haven't seen yet needs to go into referenced
951 vars. If not referenced from types only. */
952 if (gimple_in_ssa_p (cfun)
953 && TREE_CODE (*tp) == VAR_DECL
954 && id->remapping_type_depth == 0)
955 add_referenced_var (*tp);
957 /* If EXPR has block defined, map it to newly constructed block.
958 When inlining we want EXPRs without block appear in the block
959 of function call. */
960 if (EXPR_P (*tp))
962 new_block = id->block;
963 if (TREE_BLOCK (*tp))
965 tree *n;
966 n = (tree *) pointer_map_contains (id->decl_map,
967 TREE_BLOCK (*tp));
968 gcc_assert (n);
969 new_block = *n;
971 TREE_BLOCK (*tp) = new_block;
974 if (TREE_CODE (*tp) == RESX_EXPR && id->eh_region_offset)
975 TREE_OPERAND (*tp, 0) =
976 build_int_cst (NULL_TREE,
977 id->eh_region_offset
978 + TREE_INT_CST_LOW (TREE_OPERAND (*tp, 0)));
980 if (TREE_CODE (*tp) != OMP_CLAUSE)
981 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
983 /* The copied TARGET_EXPR has never been expanded, even if the
984 original node was expanded already. */
985 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
987 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
988 TREE_OPERAND (*tp, 3) = NULL_TREE;
991 /* Variable substitution need not be simple. In particular, the
992 INDIRECT_REF substitution above. Make sure that TREE_CONSTANT
993 and friends are up-to-date. */
994 else if (TREE_CODE (*tp) == ADDR_EXPR)
996 int invariant = is_gimple_min_invariant (*tp);
997 walk_tree (&TREE_OPERAND (*tp, 0), copy_tree_body_r, id, NULL);
999 /* Handle the case where we substituted an INDIRECT_REF
1000 into the operand of the ADDR_EXPR. */
1001 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
1002 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
1003 else
1004 recompute_tree_invariant_for_addr_expr (*tp);
1006 /* If this used to be invariant, but is not any longer,
1007 then regimplification is probably needed. */
1008 if (invariant && !is_gimple_min_invariant (*tp))
1009 id->regimplify = true;
1011 *walk_subtrees = 0;
1015 /* Keep iterating. */
1016 return NULL_TREE;
1020 /* Helper for copy_bb. Remap statement STMT using the inlining
1021 information in ID. Return the new statement copy. */
1023 static gimple
1024 remap_gimple_stmt (gimple stmt, copy_body_data *id)
1026 gimple copy = NULL;
1027 struct walk_stmt_info wi;
1028 tree new_block;
1030 /* Begin by recognizing trees that we'll completely rewrite for the
1031 inlining context. Our output for these trees is completely
1032 different from out input (e.g. RETURN_EXPR is deleted, and morphs
1033 into an edge). Further down, we'll handle trees that get
1034 duplicated and/or tweaked. */
1036 /* When requested, GIMPLE_RETURNs should be transformed to just the
1037 contained GIMPLE_ASSIGN. The branch semantics of the return will
1038 be handled elsewhere by manipulating the CFG rather than the
1039 statement. */
1040 if (gimple_code (stmt) == GIMPLE_RETURN && id->transform_return_to_modify)
1042 tree retval = gimple_return_retval (stmt);
1044 /* If we're returning something, just turn that into an
1045 assignment into the equivalent of the original RESULT_DECL.
1046 If RETVAL is just the result decl, the result decl has
1047 already been set (e.g. a recent "foo (&result_decl, ...)");
1048 just toss the entire GIMPLE_RETURN. */
1049 if (retval && TREE_CODE (retval) != RESULT_DECL)
1050 copy = gimple_build_assign (id->retvar, retval);
1051 else
1052 return gimple_build_nop ();
1054 else if (gimple_has_substatements (stmt))
1056 gimple_seq s1, s2;
1058 /* When cloning bodies from the C++ front end, we will be handed bodies
1059 in High GIMPLE form. Handle here all the High GIMPLE statements that
1060 have embedded statements. */
1061 switch (gimple_code (stmt))
1063 case GIMPLE_BIND:
1064 copy = copy_gimple_bind (stmt, id);
1065 break;
1067 case GIMPLE_CATCH:
1068 s1 = remap_gimple_seq (gimple_catch_handler (stmt), id);
1069 copy = gimple_build_catch (gimple_catch_types (stmt), s1);
1070 break;
1072 case GIMPLE_EH_FILTER:
1073 s1 = remap_gimple_seq (gimple_eh_filter_failure (stmt), id);
1074 copy = gimple_build_eh_filter (gimple_eh_filter_types (stmt), s1);
1075 break;
1077 case GIMPLE_TRY:
1078 s1 = remap_gimple_seq (gimple_try_eval (stmt), id);
1079 s2 = remap_gimple_seq (gimple_try_cleanup (stmt), id);
1080 copy = gimple_build_try (s1, s2, gimple_try_kind (stmt));
1081 break;
1083 case GIMPLE_WITH_CLEANUP_EXPR:
1084 s1 = remap_gimple_seq (gimple_wce_cleanup (stmt), id);
1085 copy = gimple_build_wce (s1);
1086 break;
1088 case GIMPLE_OMP_PARALLEL:
1089 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1090 copy = gimple_build_omp_parallel
1091 (s1,
1092 gimple_omp_parallel_clauses (stmt),
1093 gimple_omp_parallel_child_fn (stmt),
1094 gimple_omp_parallel_data_arg (stmt));
1095 break;
1097 case GIMPLE_OMP_TASK:
1098 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1099 copy = gimple_build_omp_task
1100 (s1,
1101 gimple_omp_task_clauses (stmt),
1102 gimple_omp_task_child_fn (stmt),
1103 gimple_omp_task_data_arg (stmt),
1104 gimple_omp_task_copy_fn (stmt),
1105 gimple_omp_task_arg_size (stmt),
1106 gimple_omp_task_arg_align (stmt));
1107 break;
1109 case GIMPLE_OMP_FOR:
1110 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1111 s2 = remap_gimple_seq (gimple_omp_for_pre_body (stmt), id);
1112 copy = gimple_build_omp_for (s1, gimple_omp_for_clauses (stmt),
1113 gimple_omp_for_collapse (stmt), s2);
1115 size_t i;
1116 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1118 gimple_omp_for_set_index (copy, i,
1119 gimple_omp_for_index (stmt, i));
1120 gimple_omp_for_set_initial (copy, i,
1121 gimple_omp_for_initial (stmt, i));
1122 gimple_omp_for_set_final (copy, i,
1123 gimple_omp_for_final (stmt, i));
1124 gimple_omp_for_set_incr (copy, i,
1125 gimple_omp_for_incr (stmt, i));
1126 gimple_omp_for_set_cond (copy, i,
1127 gimple_omp_for_cond (stmt, i));
1130 break;
1132 case GIMPLE_OMP_MASTER:
1133 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1134 copy = gimple_build_omp_master (s1);
1135 break;
1137 case GIMPLE_OMP_ORDERED:
1138 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1139 copy = gimple_build_omp_ordered (s1);
1140 break;
1142 case GIMPLE_OMP_SECTION:
1143 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1144 copy = gimple_build_omp_section (s1);
1145 break;
1147 case GIMPLE_OMP_SECTIONS:
1148 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1149 copy = gimple_build_omp_sections
1150 (s1, gimple_omp_sections_clauses (stmt));
1151 break;
1153 case GIMPLE_OMP_SINGLE:
1154 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1155 copy = gimple_build_omp_single
1156 (s1, gimple_omp_single_clauses (stmt));
1157 break;
1159 default:
1160 gcc_unreachable ();
1163 else
1165 if (gimple_assign_copy_p (stmt)
1166 && gimple_assign_lhs (stmt) == gimple_assign_rhs1 (stmt)
1167 && auto_var_in_fn_p (gimple_assign_lhs (stmt), id->src_fn))
1169 /* Here we handle statements that are not completely rewritten.
1170 First we detect some inlining-induced bogosities for
1171 discarding. */
1173 /* Some assignments VAR = VAR; don't generate any rtl code
1174 and thus don't count as variable modification. Avoid
1175 keeping bogosities like 0 = 0. */
1176 tree decl = gimple_assign_lhs (stmt), value;
1177 tree *n;
1179 n = (tree *) pointer_map_contains (id->decl_map, decl);
1180 if (n)
1182 value = *n;
1183 STRIP_TYPE_NOPS (value);
1184 if (TREE_CONSTANT (value) || TREE_READONLY (value))
1185 return gimple_build_nop ();
1189 /* Create a new deep copy of the statement. */
1190 copy = gimple_copy (stmt);
1193 /* If STMT has a block defined, map it to the newly constructed
1194 block. When inlining we want statements without a block to
1195 appear in the block of the function call. */
1196 new_block = id->block;
1197 if (gimple_block (copy))
1199 tree *n;
1200 n = (tree *) pointer_map_contains (id->decl_map, gimple_block (copy));
1201 gcc_assert (n);
1202 new_block = *n;
1205 gimple_set_block (copy, new_block);
1207 /* Remap all the operands in COPY. */
1208 memset (&wi, 0, sizeof (wi));
1209 wi.info = id;
1210 walk_gimple_op (copy, remap_gimple_op_r, &wi);
1212 /* We have to handle EH region remapping of GIMPLE_RESX specially because
1213 the region number is not an operand. */
1214 if (gimple_code (stmt) == GIMPLE_RESX && id->eh_region_offset)
1216 gimple_resx_set_region (copy, gimple_resx_region (stmt) + id->eh_region_offset);
1218 return copy;
1222 /* Copy basic block, scale profile accordingly. Edges will be taken care of
1223 later */
1225 static basic_block
1226 copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
1227 gcov_type count_scale)
1229 gimple_stmt_iterator gsi, copy_gsi;
1230 basic_block copy_basic_block;
1231 tree decl;
1233 /* create_basic_block() will append every new block to
1234 basic_block_info automatically. */
1235 copy_basic_block = create_basic_block (NULL, (void *) 0,
1236 (basic_block) bb->prev_bb->aux);
1237 copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
1239 /* We are going to rebuild frequencies from scratch. These values
1240 have just small importance to drive canonicalize_loop_headers. */
1241 copy_basic_block->frequency = ((gcov_type)bb->frequency
1242 * frequency_scale / REG_BR_PROB_BASE);
1244 if (copy_basic_block->frequency > BB_FREQ_MAX)
1245 copy_basic_block->frequency = BB_FREQ_MAX;
1247 copy_gsi = gsi_start_bb (copy_basic_block);
1249 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1251 gimple stmt = gsi_stmt (gsi);
1252 gimple orig_stmt = stmt;
1254 id->regimplify = false;
1255 stmt = remap_gimple_stmt (stmt, id);
1256 if (gimple_nop_p (stmt))
1257 continue;
1259 gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun, orig_stmt);
1261 /* With return slot optimization we can end up with
1262 non-gimple (foo *)&this->m, fix that here. */
1263 if ((is_gimple_assign (stmt)
1264 && gimple_assign_rhs_code (stmt) == NOP_EXPR
1265 && !is_gimple_val (gimple_assign_rhs1 (stmt)))
1266 || id->regimplify)
1268 tree new_rhs;
1269 new_rhs = force_gimple_operand_gsi (&copy_gsi,
1270 gimple_assign_rhs1 (stmt),
1271 true, NULL, true, GSI_SAME_STMT);
1272 gimple_assign_set_rhs1 (stmt, new_rhs);
1274 else if (id->regimplify)
1275 gimple_regimplify_operands (stmt, &copy_gsi);
1277 gsi_insert_after (&copy_gsi, stmt, GSI_NEW_STMT);
1279 /* Process the new statement. The call to gimple_regimplify_operands
1280 possibly turned the statement into multiple statements, we
1281 need to process all of them. */
1282 while (!gsi_end_p (copy_gsi))
1284 if (is_gimple_call (stmt)
1285 && gimple_call_va_arg_pack_p (stmt)
1286 && id->gimple_call)
1288 /* __builtin_va_arg_pack () should be replaced by
1289 all arguments corresponding to ... in the caller. */
1290 tree p;
1291 gimple new_call;
1292 VEC(tree, heap) *argarray;
1293 size_t nargs = gimple_call_num_args (id->gimple_call);
1294 size_t n;
1296 for (p = DECL_ARGUMENTS (id->src_fn); p; p = TREE_CHAIN (p))
1297 nargs--;
1299 /* Create the new array of arguments. */
1300 n = nargs + gimple_call_num_args (stmt);
1301 argarray = VEC_alloc (tree, heap, n);
1302 VEC_safe_grow (tree, heap, argarray, n);
1304 /* Copy all the arguments before '...' */
1305 memcpy (VEC_address (tree, argarray),
1306 gimple_call_arg_ptr (stmt, 0),
1307 gimple_call_num_args (stmt) * sizeof (tree));
1309 /* Append the arguments passed in '...' */
1310 memcpy (VEC_address(tree, argarray) + gimple_call_num_args (stmt),
1311 gimple_call_arg_ptr (id->gimple_call, 0)
1312 + (gimple_call_num_args (id->gimple_call) - nargs),
1313 nargs * sizeof (tree));
1315 new_call = gimple_build_call_vec (gimple_call_fn (stmt),
1316 argarray);
1318 VEC_free (tree, heap, argarray);
1320 /* Copy all GIMPLE_CALL flags, location and block, except
1321 GF_CALL_VA_ARG_PACK. */
1322 gimple_call_copy_flags (new_call, stmt);
1323 gimple_call_set_va_arg_pack (new_call, false);
1324 gimple_set_location (new_call, gimple_location (stmt));
1325 gimple_set_block (new_call, gimple_block (stmt));
1326 gimple_call_set_lhs (new_call, gimple_call_lhs (stmt));
1328 gsi_replace (&copy_gsi, new_call, false);
1329 stmt = new_call;
1331 else if (is_gimple_call (stmt)
1332 && id->gimple_call
1333 && (decl = gimple_call_fndecl (stmt))
1334 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1335 && DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_ARG_PACK_LEN)
1337 /* __builtin_va_arg_pack_len () should be replaced by
1338 the number of anonymous arguments. */
1339 size_t nargs = gimple_call_num_args (id->gimple_call);
1340 tree count, p;
1341 gimple new_stmt;
1343 for (p = DECL_ARGUMENTS (id->src_fn); p; p = TREE_CHAIN (p))
1344 nargs--;
1346 count = build_int_cst (integer_type_node, nargs);
1347 new_stmt = gimple_build_assign (gimple_call_lhs (stmt), count);
1348 gsi_replace (&copy_gsi, new_stmt, false);
1349 stmt = new_stmt;
1352 /* Statements produced by inlining can be unfolded, especially
1353 when we constant propagated some operands. We can't fold
1354 them right now for two reasons:
1355 1) folding require SSA_NAME_DEF_STMTs to be correct
1356 2) we can't change function calls to builtins.
1357 So we just mark statement for later folding. We mark
1358 all new statements, instead just statements that has changed
1359 by some nontrivial substitution so even statements made
1360 foldable indirectly are updated. If this turns out to be
1361 expensive, copy_body can be told to watch for nontrivial
1362 changes. */
1363 if (id->statements_to_fold)
1364 pointer_set_insert (id->statements_to_fold, stmt);
1366 /* We're duplicating a CALL_EXPR. Find any corresponding
1367 callgraph edges and update or duplicate them. */
1368 if (is_gimple_call (stmt))
1370 struct cgraph_node *node;
1371 struct cgraph_edge *edge;
1373 switch (id->transform_call_graph_edges)
1375 case CB_CGE_DUPLICATE:
1376 edge = cgraph_edge (id->src_node, orig_stmt);
1377 if (edge)
1378 cgraph_clone_edge (edge, id->dst_node, stmt,
1379 REG_BR_PROB_BASE, 1,
1380 edge->frequency, true);
1381 break;
1383 case CB_CGE_MOVE_CLONES:
1384 for (node = id->dst_node->next_clone;
1385 node;
1386 node = node->next_clone)
1388 edge = cgraph_edge (node, orig_stmt);
1389 if (edge)
1390 cgraph_set_call_stmt (edge, stmt);
1392 /* FALLTHRU */
1394 case CB_CGE_MOVE:
1395 edge = cgraph_edge (id->dst_node, orig_stmt);
1396 if (edge)
1397 cgraph_set_call_stmt (edge, stmt);
1398 break;
1400 default:
1401 gcc_unreachable ();
1405 /* If you think we can abort here, you are wrong.
1406 There is no region 0 in gimple. */
1407 gcc_assert (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt) != 0);
1409 if (stmt_could_throw_p (stmt)
1410 /* When we are cloning for inlining, we are supposed to
1411 construct a clone that calls precisely the same functions
1412 as original. However IPA optimizers might've proved
1413 earlier some function calls as non-trapping that might
1414 render some basic blocks dead that might become
1415 unreachable.
1417 We can't update SSA with unreachable blocks in CFG and thus
1418 we prevent the scenario by preserving even the "dead" eh
1419 edges until the point they are later removed by
1420 fixup_cfg pass. */
1421 || (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES
1422 && lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt) > 0))
1424 int region = lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt);
1426 /* Add an entry for the copied tree in the EH hashtable.
1427 When cloning or versioning, use the hashtable in
1428 cfun, and just copy the EH number. When inlining, use the
1429 hashtable in the caller, and adjust the region number. */
1430 if (region > 0)
1431 add_stmt_to_eh_region (stmt, region + id->eh_region_offset);
1433 /* If this tree doesn't have a region associated with it,
1434 and there is a "current region,"
1435 then associate this tree with the current region
1436 and add edges associated with this region. */
1437 if (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt) <= 0
1438 && id->eh_region > 0
1439 && stmt_could_throw_p (stmt))
1440 add_stmt_to_eh_region (stmt, id->eh_region);
1443 if (gimple_in_ssa_p (cfun))
1445 ssa_op_iter i;
1446 tree def;
1448 find_new_referenced_vars (gsi_stmt (copy_gsi));
1449 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
1450 if (TREE_CODE (def) == SSA_NAME)
1451 SSA_NAME_DEF_STMT (def) = stmt;
1454 gsi_next (&copy_gsi);
1457 copy_gsi = gsi_last_bb (copy_basic_block);
1460 return copy_basic_block;
1463 /* Inserting Single Entry Multiple Exit region in SSA form into code in SSA
1464 form is quite easy, since dominator relationship for old basic blocks does
1465 not change.
1467 There is however exception where inlining might change dominator relation
1468 across EH edges from basic block within inlined functions destinating
1469 to landing pads in function we inline into.
1471 The function fills in PHI_RESULTs of such PHI nodes if they refer
1472 to gimple regs. Otherwise, the function mark PHI_RESULT of such
1473 PHI nodes for renaming. For non-gimple regs, renaming is safe: the
1474 EH edges are abnormal and SSA_NAME_OCCURS_IN_ABNORMAL_PHI must be
1475 set, and this means that there will be no overlapping live ranges
1476 for the underlying symbol.
1478 This might change in future if we allow redirecting of EH edges and
1479 we might want to change way build CFG pre-inlining to include
1480 all the possible edges then. */
1481 static void
1482 update_ssa_across_abnormal_edges (basic_block bb, basic_block ret_bb,
1483 bool can_throw, bool nonlocal_goto)
1485 edge e;
1486 edge_iterator ei;
1488 FOR_EACH_EDGE (e, ei, bb->succs)
1489 if (!e->dest->aux
1490 || ((basic_block)e->dest->aux)->index == ENTRY_BLOCK)
1492 gimple phi;
1493 gimple_stmt_iterator si;
1495 gcc_assert (e->flags & EDGE_ABNORMAL);
1497 if (!nonlocal_goto)
1498 gcc_assert (e->flags & EDGE_EH);
1500 if (!can_throw)
1501 gcc_assert (!(e->flags & EDGE_EH));
1503 for (si = gsi_start_phis (e->dest); !gsi_end_p (si); gsi_next (&si))
1505 edge re;
1507 phi = gsi_stmt (si);
1509 /* There shouldn't be any PHI nodes in the ENTRY_BLOCK. */
1510 gcc_assert (!e->dest->aux);
1512 gcc_assert (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)));
1514 if (!is_gimple_reg (PHI_RESULT (phi)))
1516 mark_sym_for_renaming (SSA_NAME_VAR (PHI_RESULT (phi)));
1517 continue;
1520 re = find_edge (ret_bb, e->dest);
1521 gcc_assert (re);
1522 gcc_assert ((re->flags & (EDGE_EH | EDGE_ABNORMAL))
1523 == (e->flags & (EDGE_EH | EDGE_ABNORMAL)));
1525 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1526 USE_FROM_PTR (PHI_ARG_DEF_PTR_FROM_EDGE (phi, re)));
1532 /* Copy edges from BB into its copy constructed earlier, scale profile
1533 accordingly. Edges will be taken care of later. Assume aux
1534 pointers to point to the copies of each BB. */
1536 static void
1537 copy_edges_for_bb (basic_block bb, gcov_type count_scale, basic_block ret_bb)
1539 basic_block new_bb = (basic_block) bb->aux;
1540 edge_iterator ei;
1541 edge old_edge;
1542 gimple_stmt_iterator si;
1543 int flags;
1545 /* Use the indices from the original blocks to create edges for the
1546 new ones. */
1547 FOR_EACH_EDGE (old_edge, ei, bb->succs)
1548 if (!(old_edge->flags & EDGE_EH))
1550 edge new;
1552 flags = old_edge->flags;
1554 /* Return edges do get a FALLTHRU flag when the get inlined. */
1555 if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
1556 && old_edge->dest->aux != EXIT_BLOCK_PTR)
1557 flags |= EDGE_FALLTHRU;
1558 new = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
1559 new->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
1560 new->probability = old_edge->probability;
1563 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
1564 return;
1566 for (si = gsi_start_bb (new_bb); !gsi_end_p (si);)
1568 gimple copy_stmt;
1569 bool can_throw, nonlocal_goto;
1571 copy_stmt = gsi_stmt (si);
1572 update_stmt (copy_stmt);
1573 if (gimple_in_ssa_p (cfun))
1574 mark_symbols_for_renaming (copy_stmt);
1576 /* Do this before the possible split_block. */
1577 gsi_next (&si);
1579 /* If this tree could throw an exception, there are two
1580 cases where we need to add abnormal edge(s): the
1581 tree wasn't in a region and there is a "current
1582 region" in the caller; or the original tree had
1583 EH edges. In both cases split the block after the tree,
1584 and add abnormal edge(s) as needed; we need both
1585 those from the callee and the caller.
1586 We check whether the copy can throw, because the const
1587 propagation can change an INDIRECT_REF which throws
1588 into a COMPONENT_REF which doesn't. If the copy
1589 can throw, the original could also throw. */
1590 can_throw = stmt_can_throw_internal (copy_stmt);
1591 nonlocal_goto = stmt_can_make_abnormal_goto (copy_stmt);
1593 if (can_throw || nonlocal_goto)
1595 if (!gsi_end_p (si))
1596 /* Note that bb's predecessor edges aren't necessarily
1597 right at this point; split_block doesn't care. */
1599 edge e = split_block (new_bb, copy_stmt);
1601 new_bb = e->dest;
1602 new_bb->aux = e->src->aux;
1603 si = gsi_start_bb (new_bb);
1607 if (can_throw)
1608 make_eh_edges (copy_stmt);
1610 if (nonlocal_goto)
1611 make_abnormal_goto_edges (gimple_bb (copy_stmt), true);
1613 if ((can_throw || nonlocal_goto)
1614 && gimple_in_ssa_p (cfun))
1615 update_ssa_across_abnormal_edges (gimple_bb (copy_stmt), ret_bb,
1616 can_throw, nonlocal_goto);
1620 /* Copy the PHIs. All blocks and edges are copied, some blocks
1621 was possibly split and new outgoing EH edges inserted.
1622 BB points to the block of original function and AUX pointers links
1623 the original and newly copied blocks. */
1625 static void
1626 copy_phis_for_bb (basic_block bb, copy_body_data *id)
1628 basic_block const new_bb = (basic_block) bb->aux;
1629 edge_iterator ei;
1630 gimple phi;
1631 gimple_stmt_iterator si;
1633 for (si = gsi_start (phi_nodes (bb)); !gsi_end_p (si); gsi_next (&si))
1635 tree res, new_res;
1636 gimple new_phi;
1637 edge new_edge;
1639 phi = gsi_stmt (si);
1640 res = PHI_RESULT (phi);
1641 new_res = res;
1642 if (is_gimple_reg (res))
1644 walk_tree (&new_res, copy_tree_body_r, id, NULL);
1645 SSA_NAME_DEF_STMT (new_res)
1646 = new_phi = create_phi_node (new_res, new_bb);
1647 FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
1649 edge const old_edge
1650 = find_edge ((basic_block) new_edge->src->aux, bb);
1651 tree arg = PHI_ARG_DEF_FROM_EDGE (phi, old_edge);
1652 tree new_arg = arg;
1653 tree block = id->block;
1654 id->block = NULL_TREE;
1655 walk_tree (&new_arg, copy_tree_body_r, id, NULL);
1656 id->block = block;
1657 gcc_assert (new_arg);
1658 /* With return slot optimization we can end up with
1659 non-gimple (foo *)&this->m, fix that here. */
1660 if (TREE_CODE (new_arg) != SSA_NAME
1661 && TREE_CODE (new_arg) != FUNCTION_DECL
1662 && !is_gimple_val (new_arg))
1664 gimple_seq stmts = NULL;
1665 new_arg = force_gimple_operand (new_arg, &stmts, true, NULL);
1666 gsi_insert_seq_on_edge_immediate (new_edge, stmts);
1668 add_phi_arg (new_phi, new_arg, new_edge);
1675 /* Wrapper for remap_decl so it can be used as a callback. */
1677 static tree
1678 remap_decl_1 (tree decl, void *data)
1680 return remap_decl (decl, (copy_body_data *) data);
1683 /* Build struct function and associated datastructures for the new clone
1684 NEW_FNDECL to be build. CALLEE_FNDECL is the original */
1686 static void
1687 initialize_cfun (tree new_fndecl, tree callee_fndecl, gcov_type count,
1688 int frequency)
1690 struct function *new_cfun
1691 = (struct function *) ggc_alloc_cleared (sizeof (struct function));
1692 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1693 gcov_type count_scale, frequency_scale;
1695 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1696 count_scale = (REG_BR_PROB_BASE * count
1697 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1698 else
1699 count_scale = 1;
1701 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
1702 frequency_scale = (REG_BR_PROB_BASE * frequency
1704 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
1705 else
1706 frequency_scale = count_scale;
1708 /* Register specific tree functions. */
1709 gimple_register_cfg_hooks ();
1710 *new_cfun = *DECL_STRUCT_FUNCTION (callee_fndecl);
1711 new_cfun->funcdef_no = get_next_funcdef_no ();
1712 VALUE_HISTOGRAMS (new_cfun) = NULL;
1713 new_cfun->local_decls = NULL;
1714 new_cfun->cfg = NULL;
1715 new_cfun->decl = new_fndecl /*= copy_node (callee_fndecl)*/;
1716 DECL_STRUCT_FUNCTION (new_fndecl) = new_cfun;
1717 push_cfun (new_cfun);
1718 init_empty_tree_cfg ();
1720 ENTRY_BLOCK_PTR->count =
1721 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
1722 REG_BR_PROB_BASE);
1723 ENTRY_BLOCK_PTR->frequency =
1724 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
1725 frequency_scale / REG_BR_PROB_BASE);
1726 EXIT_BLOCK_PTR->count =
1727 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
1728 REG_BR_PROB_BASE);
1729 EXIT_BLOCK_PTR->frequency =
1730 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
1731 frequency_scale / REG_BR_PROB_BASE);
1732 if (src_cfun->eh)
1733 init_eh_for_function ();
1735 if (src_cfun->gimple_df)
1737 init_tree_ssa (cfun);
1738 cfun->gimple_df->in_ssa_p = true;
1739 init_ssa_operands ();
1741 pop_cfun ();
1744 /* Make a copy of the body of FN so that it can be inserted inline in
1745 another function. Walks FN via CFG, returns new fndecl. */
1747 static tree
1748 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency,
1749 basic_block entry_block_map, basic_block exit_block_map)
1751 tree callee_fndecl = id->src_fn;
1752 /* Original cfun for the callee, doesn't change. */
1753 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1754 struct function *cfun_to_copy;
1755 basic_block bb;
1756 tree new_fndecl = NULL;
1757 gcov_type count_scale, frequency_scale;
1758 int last;
1760 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1761 count_scale = (REG_BR_PROB_BASE * count
1762 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1763 else
1764 count_scale = 1;
1766 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
1767 frequency_scale = (REG_BR_PROB_BASE * frequency
1769 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
1770 else
1771 frequency_scale = count_scale;
1773 /* Register specific tree functions. */
1774 gimple_register_cfg_hooks ();
1776 /* Must have a CFG here at this point. */
1777 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
1778 (DECL_STRUCT_FUNCTION (callee_fndecl)));
1780 cfun_to_copy = id->src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1782 ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
1783 EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
1784 entry_block_map->aux = ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
1785 exit_block_map->aux = EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
1787 /* Duplicate any exception-handling regions. */
1788 if (cfun->eh)
1790 id->eh_region_offset
1791 = duplicate_eh_regions (cfun_to_copy, remap_decl_1, id,
1792 0, id->eh_region);
1795 /* Use aux pointers to map the original blocks to copy. */
1796 FOR_EACH_BB_FN (bb, cfun_to_copy)
1798 basic_block new = copy_bb (id, bb, frequency_scale, count_scale);
1799 bb->aux = new;
1800 new->aux = bb;
1803 last = last_basic_block;
1805 /* Now that we've duplicated the blocks, duplicate their edges. */
1806 FOR_ALL_BB_FN (bb, cfun_to_copy)
1807 copy_edges_for_bb (bb, count_scale, exit_block_map);
1809 if (gimple_in_ssa_p (cfun))
1810 FOR_ALL_BB_FN (bb, cfun_to_copy)
1811 copy_phis_for_bb (bb, id);
1813 FOR_ALL_BB_FN (bb, cfun_to_copy)
1815 ((basic_block)bb->aux)->aux = NULL;
1816 bb->aux = NULL;
1819 /* Zero out AUX fields of newly created block during EH edge
1820 insertion. */
1821 for (; last < last_basic_block; last++)
1822 BASIC_BLOCK (last)->aux = NULL;
1823 entry_block_map->aux = NULL;
1824 exit_block_map->aux = NULL;
1826 return new_fndecl;
1829 static tree
1830 copy_body (copy_body_data *id, gcov_type count, int frequency,
1831 basic_block entry_block_map, basic_block exit_block_map)
1833 tree fndecl = id->src_fn;
1834 tree body;
1836 /* If this body has a CFG, walk CFG and copy. */
1837 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
1838 body = copy_cfg_body (id, count, frequency, entry_block_map, exit_block_map);
1840 return body;
1843 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
1844 defined in function FN, or of a data member thereof. */
1846 static bool
1847 self_inlining_addr_expr (tree value, tree fn)
1849 tree var;
1851 if (TREE_CODE (value) != ADDR_EXPR)
1852 return false;
1854 var = get_base_address (TREE_OPERAND (value, 0));
1856 return var && auto_var_in_fn_p (var, fn);
1859 static void
1860 setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
1861 basic_block bb, tree *vars)
1863 gimple init_stmt;
1864 tree var;
1865 tree rhs = value;
1866 tree def = (gimple_in_ssa_p (cfun)
1867 ? gimple_default_def (id->src_cfun, p) : NULL);
1869 if (value
1870 && value != error_mark_node
1871 && !useless_type_conversion_p (TREE_TYPE (p), TREE_TYPE (value)))
1873 if (fold_convertible_p (TREE_TYPE (p), value))
1874 rhs = fold_build1 (NOP_EXPR, TREE_TYPE (p), value);
1875 else
1876 /* ??? For valid (GIMPLE) programs we should not end up here.
1877 Still if something has gone wrong and we end up with truly
1878 mismatched types here, fall back to using a VIEW_CONVERT_EXPR
1879 to not leak invalid GIMPLE to the following passes. */
1880 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (p), value);
1883 /* If the parameter is never assigned to, has no SSA_NAMEs created,
1884 we may not need to create a new variable here at all. Instead, we may
1885 be able to just use the argument value. */
1886 if (TREE_READONLY (p)
1887 && !TREE_ADDRESSABLE (p)
1888 && value && !TREE_SIDE_EFFECTS (value)
1889 && !def)
1891 /* We may produce non-gimple trees by adding NOPs or introduce
1892 invalid sharing when operand is not really constant.
1893 It is not big deal to prohibit constant propagation here as
1894 we will constant propagate in DOM1 pass anyway. */
1895 if (is_gimple_min_invariant (value)
1896 && useless_type_conversion_p (TREE_TYPE (p),
1897 TREE_TYPE (value))
1898 /* We have to be very careful about ADDR_EXPR. Make sure
1899 the base variable isn't a local variable of the inlined
1900 function, e.g., when doing recursive inlining, direct or
1901 mutually-recursive or whatever, which is why we don't
1902 just test whether fn == current_function_decl. */
1903 && ! self_inlining_addr_expr (value, fn))
1905 insert_decl_map (id, p, value);
1906 return;
1910 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
1911 here since the type of this decl must be visible to the calling
1912 function. */
1913 var = copy_decl_to_var (p, id);
1914 if (gimple_in_ssa_p (cfun) && TREE_CODE (var) == VAR_DECL)
1916 get_var_ann (var);
1917 add_referenced_var (var);
1920 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
1921 that way, when the PARM_DECL is encountered, it will be
1922 automatically replaced by the VAR_DECL. */
1923 insert_decl_map (id, p, var);
1925 /* Declare this new variable. */
1926 TREE_CHAIN (var) = *vars;
1927 *vars = var;
1929 /* Make gimplifier happy about this variable. */
1930 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1932 /* Even if P was TREE_READONLY, the new VAR should not be.
1933 In the original code, we would have constructed a
1934 temporary, and then the function body would have never
1935 changed the value of P. However, now, we will be
1936 constructing VAR directly. The constructor body may
1937 change its value multiple times as it is being
1938 constructed. Therefore, it must not be TREE_READONLY;
1939 the back-end assumes that TREE_READONLY variable is
1940 assigned to only once. */
1941 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
1942 TREE_READONLY (var) = 0;
1944 /* If there is no setup required and we are in SSA, take the easy route
1945 replacing all SSA names representing the function parameter by the
1946 SSA name passed to function.
1948 We need to construct map for the variable anyway as it might be used
1949 in different SSA names when parameter is set in function.
1951 FIXME: This usually kills the last connection in between inlined
1952 function parameter and the actual value in debug info. Can we do
1953 better here? If we just inserted the statement, copy propagation
1954 would kill it anyway as it always did in older versions of GCC.
1956 We might want to introduce a notion that single SSA_NAME might
1957 represent multiple variables for purposes of debugging. */
1958 if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p)
1959 && (TREE_CODE (rhs) == SSA_NAME
1960 || is_gimple_min_invariant (rhs))
1961 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
1963 insert_decl_map (id, def, rhs);
1964 return;
1967 /* If the value of argument is never used, don't care about initializing
1968 it. */
1969 if (gimple_in_ssa_p (cfun) && !def && is_gimple_reg (p))
1971 gcc_assert (!value || !TREE_SIDE_EFFECTS (value));
1972 return;
1975 /* Initialize this VAR_DECL from the equivalent argument. Convert
1976 the argument to the proper type in case it was promoted. */
1977 if (value)
1979 gimple_stmt_iterator si = gsi_last_bb (bb);
1981 if (rhs == error_mark_node)
1983 insert_decl_map (id, p, var);
1984 return;
1987 STRIP_USELESS_TYPE_CONVERSION (rhs);
1989 /* We want to use MODIFY_EXPR, not INIT_EXPR here so that we
1990 keep our trees in gimple form. */
1991 if (def && gimple_in_ssa_p (cfun) && is_gimple_reg (p))
1993 def = remap_ssa_name (def, id);
1994 init_stmt = gimple_build_assign (def, rhs);
1995 SSA_NAME_IS_DEFAULT_DEF (def) = 0;
1996 set_default_def (var, NULL);
1998 else
1999 init_stmt = gimple_build_assign (var, rhs);
2001 /* If we did not create a gimple value and we did not create a gimple
2002 cast of a gimple value, then we will need to gimplify INIT_STMT
2003 at the end. Note that is_gimple_cast only checks the outer
2004 tree code, not its operand. Thus the explicit check that its
2005 operand is a gimple value. */
2006 if ((!is_gimple_val (rhs)
2007 && (!is_gimple_cast (rhs)
2008 || !is_gimple_val (TREE_OPERAND (rhs, 0))))
2009 || !is_gimple_reg (var))
2011 gimple_stmt_iterator i;
2012 gimple_seq seq = gimple_seq_alloc ();
2013 struct gimplify_ctx gctx;
2015 push_gimplify_context (&gctx);
2017 i = gsi_start (seq);
2018 gimple_regimplify_operands (init_stmt, &i);
2020 if (gimple_in_ssa_p (cfun)
2021 && init_stmt
2022 && !gimple_seq_empty_p (seq))
2024 /* The replacement can expose previously unreferenced
2025 variables. */
2026 for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
2027 find_new_referenced_vars (gsi_stmt (i));
2029 /* Insert the gimplified sequence needed for INIT_STMT
2030 after SI. INIT_STMT will be inserted after SEQ. */
2031 gsi_insert_seq_after (&si, seq, GSI_NEW_STMT);
2034 pop_gimplify_context (NULL);
2037 /* If VAR represents a zero-sized variable, it's possible that the
2038 assignment statement may result in no gimple statements. */
2039 if (init_stmt)
2040 gsi_insert_after (&si, init_stmt, GSI_NEW_STMT);
2042 if (gimple_in_ssa_p (cfun))
2043 for (;!gsi_end_p (si); gsi_next (&si))
2044 mark_symbols_for_renaming (gsi_stmt (si));
2048 /* Generate code to initialize the parameters of the function at the
2049 top of the stack in ID from the GIMPLE_CALL STMT. */
2051 static void
2052 initialize_inlined_parameters (copy_body_data *id, gimple stmt,
2053 tree fn, basic_block bb)
2055 tree parms;
2056 size_t i;
2057 tree p;
2058 tree vars = NULL_TREE;
2059 tree static_chain = gimple_call_chain (stmt);
2061 /* Figure out what the parameters are. */
2062 parms = DECL_ARGUMENTS (fn);
2064 /* Loop through the parameter declarations, replacing each with an
2065 equivalent VAR_DECL, appropriately initialized. */
2066 for (p = parms, i = 0; p; p = TREE_CHAIN (p), i++)
2068 tree val;
2069 val = i < gimple_call_num_args (stmt) ? gimple_call_arg (stmt, i) : NULL;
2070 setup_one_parameter (id, p, val, fn, bb, &vars);
2073 /* Initialize the static chain. */
2074 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
2075 gcc_assert (fn != current_function_decl);
2076 if (p)
2078 /* No static chain? Seems like a bug in tree-nested.c. */
2079 gcc_assert (static_chain);
2081 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
2084 declare_inline_vars (id->block, vars);
2088 /* Declare a return variable to replace the RESULT_DECL for the
2089 function we are calling. An appropriate DECL_STMT is returned.
2090 The USE_STMT is filled to contain a use of the declaration to
2091 indicate the return value of the function.
2093 RETURN_SLOT, if non-null is place where to store the result. It
2094 is set only for CALL_EXPR_RETURN_SLOT_OPT. MODIFY_DEST, if non-null,
2095 was the LHS of the MODIFY_EXPR to which this call is the RHS.
2097 The return value is a (possibly null) value that is the result of the
2098 function as seen by the callee. *USE_P is a (possibly null) value that
2099 holds the result as seen by the caller. */
2101 static tree
2102 declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest,
2103 tree *use_p)
2105 tree callee = id->src_fn;
2106 tree caller = id->dst_fn;
2107 tree result = DECL_RESULT (callee);
2108 tree callee_type = TREE_TYPE (result);
2109 tree caller_type = TREE_TYPE (TREE_TYPE (callee));
2110 tree var, use;
2112 /* We don't need to do anything for functions that don't return
2113 anything. */
2114 if (!result || VOID_TYPE_P (callee_type))
2116 *use_p = NULL_TREE;
2117 return NULL_TREE;
2120 /* If there was a return slot, then the return value is the
2121 dereferenced address of that object. */
2122 if (return_slot)
2124 /* The front end shouldn't have used both return_slot and
2125 a modify expression. */
2126 gcc_assert (!modify_dest);
2127 if (DECL_BY_REFERENCE (result))
2129 tree return_slot_addr = build_fold_addr_expr (return_slot);
2130 STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
2132 /* We are going to construct *&return_slot and we can't do that
2133 for variables believed to be not addressable.
2135 FIXME: This check possibly can match, because values returned
2136 via return slot optimization are not believed to have address
2137 taken by alias analysis. */
2138 gcc_assert (TREE_CODE (return_slot) != SSA_NAME);
2139 if (gimple_in_ssa_p (cfun))
2141 HOST_WIDE_INT bitsize;
2142 HOST_WIDE_INT bitpos;
2143 tree offset;
2144 enum machine_mode mode;
2145 int unsignedp;
2146 int volatilep;
2147 tree base;
2148 base = get_inner_reference (return_slot, &bitsize, &bitpos,
2149 &offset,
2150 &mode, &unsignedp, &volatilep,
2151 false);
2152 if (TREE_CODE (base) == INDIRECT_REF)
2153 base = TREE_OPERAND (base, 0);
2154 if (TREE_CODE (base) == SSA_NAME)
2155 base = SSA_NAME_VAR (base);
2156 mark_sym_for_renaming (base);
2158 var = return_slot_addr;
2160 else
2162 var = return_slot;
2163 gcc_assert (TREE_CODE (var) != SSA_NAME);
2164 TREE_ADDRESSABLE (var) |= TREE_ADDRESSABLE (result);
2166 if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
2167 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
2168 && !DECL_GIMPLE_REG_P (result)
2169 && DECL_P (var))
2170 DECL_GIMPLE_REG_P (var) = 0;
2171 use = NULL;
2172 goto done;
2175 /* All types requiring non-trivial constructors should have been handled. */
2176 gcc_assert (!TREE_ADDRESSABLE (callee_type));
2178 /* Attempt to avoid creating a new temporary variable. */
2179 if (modify_dest
2180 && TREE_CODE (modify_dest) != SSA_NAME)
2182 bool use_it = false;
2184 /* We can't use MODIFY_DEST if there's type promotion involved. */
2185 if (!useless_type_conversion_p (callee_type, caller_type))
2186 use_it = false;
2188 /* ??? If we're assigning to a variable sized type, then we must
2189 reuse the destination variable, because we've no good way to
2190 create variable sized temporaries at this point. */
2191 else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
2192 use_it = true;
2194 /* If the callee cannot possibly modify MODIFY_DEST, then we can
2195 reuse it as the result of the call directly. Don't do this if
2196 it would promote MODIFY_DEST to addressable. */
2197 else if (TREE_ADDRESSABLE (result))
2198 use_it = false;
2199 else
2201 tree base_m = get_base_address (modify_dest);
2203 /* If the base isn't a decl, then it's a pointer, and we don't
2204 know where that's going to go. */
2205 if (!DECL_P (base_m))
2206 use_it = false;
2207 else if (is_global_var (base_m))
2208 use_it = false;
2209 else if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
2210 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
2211 && !DECL_GIMPLE_REG_P (result)
2212 && DECL_GIMPLE_REG_P (base_m))
2213 use_it = false;
2214 else if (!TREE_ADDRESSABLE (base_m))
2215 use_it = true;
2218 if (use_it)
2220 var = modify_dest;
2221 use = NULL;
2222 goto done;
2226 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
2228 var = copy_result_decl_to_var (result, id);
2229 if (gimple_in_ssa_p (cfun))
2231 get_var_ann (var);
2232 add_referenced_var (var);
2235 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2236 DECL_STRUCT_FUNCTION (caller)->local_decls
2237 = tree_cons (NULL_TREE, var,
2238 DECL_STRUCT_FUNCTION (caller)->local_decls);
2240 /* Do not have the rest of GCC warn about this variable as it should
2241 not be visible to the user. */
2242 TREE_NO_WARNING (var) = 1;
2244 declare_inline_vars (id->block, var);
2246 /* Build the use expr. If the return type of the function was
2247 promoted, convert it back to the expected type. */
2248 use = var;
2249 if (!useless_type_conversion_p (caller_type, TREE_TYPE (var)))
2250 use = fold_convert (caller_type, var);
2252 STRIP_USELESS_TYPE_CONVERSION (use);
2254 if (DECL_BY_REFERENCE (result))
2255 var = build_fold_addr_expr (var);
2257 done:
2258 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
2259 way, when the RESULT_DECL is encountered, it will be
2260 automatically replaced by the VAR_DECL. */
2261 insert_decl_map (id, result, var);
2263 /* Remember this so we can ignore it in remap_decls. */
2264 id->retvar = var;
2266 *use_p = use;
2267 return var;
2270 /* Returns nonzero if a function can be inlined as a tree. */
2272 bool
2273 tree_inlinable_function_p (tree fn)
2275 bool ret = inlinable_function_p (fn);
2277 if (getenv ("TUPLES_INLINE"))
2278 fprintf (stderr, "Function %s is %sinlinable\n", get_name (fn),
2279 ret ? "" : "not ");
2281 return ret;
2284 static const char *inline_forbidden_reason;
2286 /* A callback for walk_gimple_seq to handle tree operands. Returns
2287 NULL_TREE if a function can be inlined, otherwise sets the reason
2288 why not and returns a tree representing the offending operand. */
2290 static tree
2291 inline_forbidden_p_op (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED,
2292 void *fnp ATTRIBUTE_UNUSED)
2294 tree node = *nodep;
2295 tree t;
2297 if (TREE_CODE (node) == RECORD_TYPE || TREE_CODE (node) == UNION_TYPE)
2299 /* We cannot inline a function of the form
2301 void F (int i) { struct S { int ar[i]; } s; }
2303 Attempting to do so produces a catch-22.
2304 If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
2305 UNION_TYPE nodes, then it goes into infinite recursion on a
2306 structure containing a pointer to its own type. If it doesn't,
2307 then the type node for S doesn't get adjusted properly when
2308 F is inlined.
2310 ??? This is likely no longer true, but it's too late in the 4.0
2311 cycle to try to find out. This should be checked for 4.1. */
2312 for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t))
2313 if (variably_modified_type_p (TREE_TYPE (t), NULL))
2315 inline_forbidden_reason
2316 = G_("function %q+F can never be inlined "
2317 "because it uses variable sized variables");
2318 return node;
2322 return NULL_TREE;
2326 /* A callback for walk_gimple_seq to handle statements. Returns
2327 non-NULL iff a function can not be inlined. Also sets the reason
2328 why. */
2330 static tree
2331 inline_forbidden_p_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2332 struct walk_stmt_info *wip)
2334 tree fn = (tree) wip->info;
2335 tree t;
2336 gimple stmt = gsi_stmt (*gsi);
2338 switch (gimple_code (stmt))
2340 case GIMPLE_CALL:
2341 /* Refuse to inline alloca call unless user explicitly forced so as
2342 this may change program's memory overhead drastically when the
2343 function using alloca is called in loop. In GCC present in
2344 SPEC2000 inlining into schedule_block cause it to require 2GB of
2345 RAM instead of 256MB. */
2346 if (gimple_alloca_call_p (stmt)
2347 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
2349 inline_forbidden_reason
2350 = G_("function %q+F can never be inlined because it uses "
2351 "alloca (override using the always_inline attribute)");
2352 *handled_ops_p = true;
2353 return fn;
2356 t = gimple_call_fndecl (stmt);
2357 if (t == NULL_TREE)
2358 break;
2360 /* We cannot inline functions that call setjmp. */
2361 if (setjmp_call_p (t))
2363 inline_forbidden_reason
2364 = G_("function %q+F can never be inlined because it uses setjmp");
2365 *handled_ops_p = true;
2366 return t;
2369 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
2370 switch (DECL_FUNCTION_CODE (t))
2372 /* We cannot inline functions that take a variable number of
2373 arguments. */
2374 case BUILT_IN_VA_START:
2375 case BUILT_IN_NEXT_ARG:
2376 case BUILT_IN_VA_END:
2377 inline_forbidden_reason
2378 = G_("function %q+F can never be inlined because it "
2379 "uses variable argument lists");
2380 *handled_ops_p = true;
2381 return t;
2383 case BUILT_IN_LONGJMP:
2384 /* We can't inline functions that call __builtin_longjmp at
2385 all. The non-local goto machinery really requires the
2386 destination be in a different function. If we allow the
2387 function calling __builtin_longjmp to be inlined into the
2388 function calling __builtin_setjmp, Things will Go Awry. */
2389 inline_forbidden_reason
2390 = G_("function %q+F can never be inlined because "
2391 "it uses setjmp-longjmp exception handling");
2392 *handled_ops_p = true;
2393 return t;
2395 case BUILT_IN_NONLOCAL_GOTO:
2396 /* Similarly. */
2397 inline_forbidden_reason
2398 = G_("function %q+F can never be inlined because "
2399 "it uses non-local goto");
2400 *handled_ops_p = true;
2401 return t;
2403 case BUILT_IN_RETURN:
2404 case BUILT_IN_APPLY_ARGS:
2405 /* If a __builtin_apply_args caller would be inlined,
2406 it would be saving arguments of the function it has
2407 been inlined into. Similarly __builtin_return would
2408 return from the function the inline has been inlined into. */
2409 inline_forbidden_reason
2410 = G_("function %q+F can never be inlined because "
2411 "it uses __builtin_return or __builtin_apply_args");
2412 *handled_ops_p = true;
2413 return t;
2415 default:
2416 break;
2418 break;
2420 case GIMPLE_GOTO:
2421 t = gimple_goto_dest (stmt);
2423 /* We will not inline a function which uses computed goto. The
2424 addresses of its local labels, which may be tucked into
2425 global storage, are of course not constant across
2426 instantiations, which causes unexpected behavior. */
2427 if (TREE_CODE (t) != LABEL_DECL)
2429 inline_forbidden_reason
2430 = G_("function %q+F can never be inlined "
2431 "because it contains a computed goto");
2432 *handled_ops_p = true;
2433 return t;
2435 break;
2437 case GIMPLE_LABEL:
2438 t = gimple_label_label (stmt);
2439 if (DECL_NONLOCAL (t))
2441 /* We cannot inline a function that receives a non-local goto
2442 because we cannot remap the destination label used in the
2443 function that is performing the non-local goto. */
2444 inline_forbidden_reason
2445 = G_("function %q+F can never be inlined "
2446 "because it receives a non-local goto");
2447 *handled_ops_p = true;
2448 return t;
2450 break;
2452 default:
2453 break;
2456 *handled_ops_p = false;
2457 return NULL_TREE;
2461 static tree
2462 inline_forbidden_p_2 (tree *nodep, int *walk_subtrees,
2463 void *fnp)
2465 tree node = *nodep;
2466 tree fn = (tree) fnp;
2468 if (TREE_CODE (node) == LABEL_DECL && DECL_CONTEXT (node) == fn)
2470 inline_forbidden_reason
2471 = G_("function %q+F can never be inlined "
2472 "because it saves address of local label in a static variable");
2473 return node;
2476 if (TYPE_P (node))
2477 *walk_subtrees = 0;
2479 return NULL_TREE;
2482 /* Return true if FNDECL is a function that cannot be inlined into
2483 another one. */
2485 static bool
2486 inline_forbidden_p (tree fndecl)
2488 location_t saved_loc = input_location;
2489 struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
2490 tree step;
2491 struct walk_stmt_info wi;
2492 struct pointer_set_t *visited_nodes;
2493 basic_block bb;
2494 bool forbidden_p = false;
2496 visited_nodes = pointer_set_create ();
2497 memset (&wi, 0, sizeof (wi));
2498 wi.info = (void *) fndecl;
2499 wi.pset = visited_nodes;
2501 FOR_EACH_BB_FN (bb, fun)
2503 gimple ret;
2504 gimple_seq seq = bb_seq (bb);
2505 ret = walk_gimple_seq (seq, inline_forbidden_p_stmt,
2506 inline_forbidden_p_op, &wi);
2507 forbidden_p = (ret != NULL);
2508 if (forbidden_p)
2509 goto egress;
2512 for (step = fun->local_decls; step; step = TREE_CHAIN (step))
2514 tree decl = TREE_VALUE (step);
2515 if (TREE_CODE (decl) == VAR_DECL
2516 && TREE_STATIC (decl)
2517 && !DECL_EXTERNAL (decl)
2518 && DECL_INITIAL (decl))
2520 tree ret;
2521 ret = walk_tree_without_duplicates (&DECL_INITIAL (decl),
2522 inline_forbidden_p_2, fndecl);
2523 forbidden_p = (ret != NULL);
2524 if (forbidden_p)
2525 goto egress;
2529 egress:
2530 pointer_set_destroy (visited_nodes);
2531 input_location = saved_loc;
2532 return forbidden_p;
2535 /* Returns nonzero if FN is a function that does not have any
2536 fundamental inline blocking properties. */
2538 static bool
2539 inlinable_function_p (tree fn)
2541 bool inlinable = true;
2542 bool do_warning;
2543 tree always_inline;
2545 /* If we've already decided this function shouldn't be inlined,
2546 there's no need to check again. */
2547 if (DECL_UNINLINABLE (fn))
2548 return false;
2550 /* We only warn for functions declared `inline' by the user. */
2551 do_warning = (warn_inline
2552 && DECL_DECLARED_INLINE_P (fn)
2553 && !DECL_IN_SYSTEM_HEADER (fn));
2555 always_inline = lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn));
2557 if (flag_really_no_inline
2558 && always_inline == NULL)
2560 if (do_warning)
2561 warning (OPT_Winline, "function %q+F can never be inlined because it "
2562 "is suppressed using -fno-inline", fn);
2563 inlinable = false;
2566 /* Don't auto-inline anything that might not be bound within
2567 this unit of translation. */
2568 else if (!DECL_DECLARED_INLINE_P (fn)
2569 && DECL_REPLACEABLE_P (fn))
2570 inlinable = false;
2572 else if (!function_attribute_inlinable_p (fn))
2574 if (do_warning)
2575 warning (OPT_Winline, "function %q+F can never be inlined because it "
2576 "uses attributes conflicting with inlining", fn);
2577 inlinable = false;
2580 /* If we don't have the function body available, we can't inline it.
2581 However, this should not be recorded since we also get here for
2582 forward declared inline functions. Therefore, return at once. */
2583 if (!gimple_body (fn))
2584 return false;
2586 else if (inline_forbidden_p (fn))
2588 /* See if we should warn about uninlinable functions. Previously,
2589 some of these warnings would be issued while trying to expand
2590 the function inline, but that would cause multiple warnings
2591 about functions that would for example call alloca. But since
2592 this a property of the function, just one warning is enough.
2593 As a bonus we can now give more details about the reason why a
2594 function is not inlinable. */
2595 if (always_inline)
2596 sorry (inline_forbidden_reason, fn);
2597 else if (do_warning)
2598 warning (OPT_Winline, inline_forbidden_reason, fn);
2600 inlinable = false;
2603 /* Squirrel away the result so that we don't have to check again. */
2604 DECL_UNINLINABLE (fn) = !inlinable;
2606 return inlinable;
2609 /* Estimate the cost of a memory move. Use machine dependent
2610 word size and take possible memcpy call into account. */
2613 estimate_move_cost (tree type)
2615 HOST_WIDE_INT size;
2617 size = int_size_in_bytes (type);
2619 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO)
2620 /* Cost of a memcpy call, 3 arguments and the call. */
2621 return 4;
2622 else
2623 return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
2626 /* Returns cost of operation CODE, according to WEIGHTS */
2628 static int
2629 estimate_operator_cost (enum tree_code code, eni_weights *weights)
2631 switch (code)
2633 /* These are "free" conversions, or their presumed cost
2634 is folded into other operations. */
2635 case RANGE_EXPR:
2636 case CONVERT_EXPR:
2637 case COMPLEX_EXPR:
2638 case PAREN_EXPR:
2639 case NOP_EXPR:
2640 return 0;
2642 /* Assign cost of 1 to usual operations.
2643 ??? We may consider mapping RTL costs to this. */
2644 case COND_EXPR:
2645 case VEC_COND_EXPR:
2647 case PLUS_EXPR:
2648 case POINTER_PLUS_EXPR:
2649 case MINUS_EXPR:
2650 case MULT_EXPR:
2652 case FIXED_CONVERT_EXPR:
2653 case FIX_TRUNC_EXPR:
2655 case NEGATE_EXPR:
2656 case FLOAT_EXPR:
2657 case MIN_EXPR:
2658 case MAX_EXPR:
2659 case ABS_EXPR:
2661 case LSHIFT_EXPR:
2662 case RSHIFT_EXPR:
2663 case LROTATE_EXPR:
2664 case RROTATE_EXPR:
2665 case VEC_LSHIFT_EXPR:
2666 case VEC_RSHIFT_EXPR:
2668 case BIT_IOR_EXPR:
2669 case BIT_XOR_EXPR:
2670 case BIT_AND_EXPR:
2671 case BIT_NOT_EXPR:
2673 case TRUTH_ANDIF_EXPR:
2674 case TRUTH_ORIF_EXPR:
2675 case TRUTH_AND_EXPR:
2676 case TRUTH_OR_EXPR:
2677 case TRUTH_XOR_EXPR:
2678 case TRUTH_NOT_EXPR:
2680 case LT_EXPR:
2681 case LE_EXPR:
2682 case GT_EXPR:
2683 case GE_EXPR:
2684 case EQ_EXPR:
2685 case NE_EXPR:
2686 case ORDERED_EXPR:
2687 case UNORDERED_EXPR:
2689 case UNLT_EXPR:
2690 case UNLE_EXPR:
2691 case UNGT_EXPR:
2692 case UNGE_EXPR:
2693 case UNEQ_EXPR:
2694 case LTGT_EXPR:
2696 case CONJ_EXPR:
2698 case PREDECREMENT_EXPR:
2699 case PREINCREMENT_EXPR:
2700 case POSTDECREMENT_EXPR:
2701 case POSTINCREMENT_EXPR:
2703 case REALIGN_LOAD_EXPR:
2705 case REDUC_MAX_EXPR:
2706 case REDUC_MIN_EXPR:
2707 case REDUC_PLUS_EXPR:
2708 case WIDEN_SUM_EXPR:
2709 case WIDEN_MULT_EXPR:
2710 case DOT_PROD_EXPR:
2712 case VEC_WIDEN_MULT_HI_EXPR:
2713 case VEC_WIDEN_MULT_LO_EXPR:
2714 case VEC_UNPACK_HI_EXPR:
2715 case VEC_UNPACK_LO_EXPR:
2716 case VEC_UNPACK_FLOAT_HI_EXPR:
2717 case VEC_UNPACK_FLOAT_LO_EXPR:
2718 case VEC_PACK_TRUNC_EXPR:
2719 case VEC_PACK_SAT_EXPR:
2720 case VEC_PACK_FIX_TRUNC_EXPR:
2721 case VEC_EXTRACT_EVEN_EXPR:
2722 case VEC_EXTRACT_ODD_EXPR:
2723 case VEC_INTERLEAVE_HIGH_EXPR:
2724 case VEC_INTERLEAVE_LOW_EXPR:
2726 return 1;
2728 /* Few special cases of expensive operations. This is useful
2729 to avoid inlining on functions having too many of these. */
2730 case TRUNC_DIV_EXPR:
2731 case CEIL_DIV_EXPR:
2732 case FLOOR_DIV_EXPR:
2733 case ROUND_DIV_EXPR:
2734 case EXACT_DIV_EXPR:
2735 case TRUNC_MOD_EXPR:
2736 case CEIL_MOD_EXPR:
2737 case FLOOR_MOD_EXPR:
2738 case ROUND_MOD_EXPR:
2739 case RDIV_EXPR:
2740 return weights->div_mod_cost;
2742 default:
2743 /* We expect a copy assignment with no operator. */
2744 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS);
2745 return 0;
2750 /* Estimate number of instructions that will be created by expanding
2751 the statements in the statement sequence STMTS.
2752 WEIGHTS contains weights attributed to various constructs. */
2754 static
2755 int estimate_num_insns_seq (gimple_seq stmts, eni_weights *weights)
2757 int cost;
2758 gimple_stmt_iterator gsi;
2760 cost = 0;
2761 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
2762 cost += estimate_num_insns (gsi_stmt (gsi), weights);
2764 return cost;
2768 /* Estimate number of instructions that will be created by expanding STMT.
2769 WEIGHTS contains weights attributed to various constructs. */
2772 estimate_num_insns (gimple stmt, eni_weights *weights)
2774 unsigned cost, i;
2775 enum gimple_code code = gimple_code (stmt);
2776 tree lhs;
2778 switch (code)
2780 case GIMPLE_ASSIGN:
2781 /* Try to estimate the cost of assignments. We have three cases to
2782 deal with:
2783 1) Simple assignments to registers;
2784 2) Stores to things that must live in memory. This includes
2785 "normal" stores to scalars, but also assignments of large
2786 structures, or constructors of big arrays;
2788 Let us look at the first two cases, assuming we have "a = b + C":
2789 <GIMPLE_ASSIGN <var_decl "a">
2790 <plus_expr <var_decl "b"> <constant C>>
2791 If "a" is a GIMPLE register, the assignment to it is free on almost
2792 any target, because "a" usually ends up in a real register. Hence
2793 the only cost of this expression comes from the PLUS_EXPR, and we
2794 can ignore the GIMPLE_ASSIGN.
2795 If "a" is not a GIMPLE register, the assignment to "a" will most
2796 likely be a real store, so the cost of the GIMPLE_ASSIGN is the cost
2797 of moving something into "a", which we compute using the function
2798 estimate_move_cost. */
2799 lhs = gimple_assign_lhs (stmt);
2800 if (is_gimple_reg (lhs))
2801 cost = 0;
2802 else
2803 cost = estimate_move_cost (TREE_TYPE (lhs));
2805 cost += estimate_operator_cost (gimple_assign_rhs_code (stmt), weights);
2806 break;
2808 case GIMPLE_COND:
2809 cost = 1 + estimate_operator_cost (gimple_cond_code (stmt), weights);
2810 break;
2812 case GIMPLE_SWITCH:
2813 /* Take into account cost of the switch + guess 2 conditional jumps for
2814 each case label.
2816 TODO: once the switch expansion logic is sufficiently separated, we can
2817 do better job on estimating cost of the switch. */
2818 cost = gimple_switch_num_labels (stmt) * 2;
2819 break;
2821 case GIMPLE_CALL:
2823 tree decl = gimple_call_fndecl (stmt);
2824 tree addr = gimple_call_fn (stmt);
2825 tree funtype = TREE_TYPE (addr);
2827 if (POINTER_TYPE_P (funtype))
2828 funtype = TREE_TYPE (funtype);
2830 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
2831 cost = weights->target_builtin_call_cost;
2832 else
2833 cost = weights->call_cost;
2835 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2836 switch (DECL_FUNCTION_CODE (decl))
2838 case BUILT_IN_CONSTANT_P:
2839 return 0;
2840 case BUILT_IN_EXPECT:
2841 cost = 0;
2842 break;
2844 /* Prefetch instruction is not expensive. */
2845 case BUILT_IN_PREFETCH:
2846 cost = weights->target_builtin_call_cost;
2847 break;
2849 default:
2850 break;
2853 if (decl)
2854 funtype = TREE_TYPE (decl);
2856 /* Our cost must be kept in sync with
2857 cgraph_estimate_size_after_inlining that does use function
2858 declaration to figure out the arguments. */
2859 if (decl && DECL_ARGUMENTS (decl))
2861 tree arg;
2862 for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
2863 cost += estimate_move_cost (TREE_TYPE (arg));
2865 else if (funtype && prototype_p (funtype))
2867 tree t;
2868 for (t = TYPE_ARG_TYPES (funtype); t; t = TREE_CHAIN (t))
2869 cost += estimate_move_cost (TREE_VALUE (t));
2871 else
2873 for (i = 0; i < gimple_call_num_args (stmt); i++)
2875 tree arg = gimple_call_arg (stmt, i);
2876 cost += estimate_move_cost (TREE_TYPE (arg));
2880 break;
2883 case GIMPLE_GOTO:
2884 case GIMPLE_LABEL:
2885 case GIMPLE_NOP:
2886 case GIMPLE_PHI:
2887 case GIMPLE_RETURN:
2888 case GIMPLE_CHANGE_DYNAMIC_TYPE:
2889 case GIMPLE_PREDICT:
2890 return 0;
2892 case GIMPLE_ASM:
2893 case GIMPLE_RESX:
2894 return 1;
2896 case GIMPLE_BIND:
2897 return estimate_num_insns_seq (gimple_bind_body (stmt), weights);
2899 case GIMPLE_EH_FILTER:
2900 return estimate_num_insns_seq (gimple_eh_filter_failure (stmt), weights);
2902 case GIMPLE_CATCH:
2903 return estimate_num_insns_seq (gimple_catch_handler (stmt), weights);
2905 case GIMPLE_TRY:
2906 return (estimate_num_insns_seq (gimple_try_eval (stmt), weights)
2907 + estimate_num_insns_seq (gimple_try_cleanup (stmt), weights));
2909 /* OpenMP directives are generally very expensive. */
2911 case GIMPLE_OMP_RETURN:
2912 case GIMPLE_OMP_SECTIONS_SWITCH:
2913 case GIMPLE_OMP_ATOMIC_STORE:
2914 case GIMPLE_OMP_CONTINUE:
2915 /* ...except these, which are cheap. */
2916 return 0;
2918 case GIMPLE_OMP_ATOMIC_LOAD:
2919 return weights->omp_cost;
2921 case GIMPLE_OMP_FOR:
2922 return (weights->omp_cost
2923 + estimate_num_insns_seq (gimple_omp_body (stmt), weights)
2924 + estimate_num_insns_seq (gimple_omp_for_pre_body (stmt), weights));
2926 case GIMPLE_OMP_PARALLEL:
2927 case GIMPLE_OMP_TASK:
2928 case GIMPLE_OMP_CRITICAL:
2929 case GIMPLE_OMP_MASTER:
2930 case GIMPLE_OMP_ORDERED:
2931 case GIMPLE_OMP_SECTION:
2932 case GIMPLE_OMP_SECTIONS:
2933 case GIMPLE_OMP_SINGLE:
2934 return (weights->omp_cost
2935 + estimate_num_insns_seq (gimple_omp_body (stmt), weights));
2937 default:
2938 gcc_unreachable ();
2941 return cost;
2944 /* Estimate number of instructions that will be created by expanding
2945 function FNDECL. WEIGHTS contains weights attributed to various
2946 constructs. */
2949 estimate_num_insns_fn (tree fndecl, eni_weights *weights)
2951 struct function *my_function = DECL_STRUCT_FUNCTION (fndecl);
2952 gimple_stmt_iterator bsi;
2953 basic_block bb;
2954 int n = 0;
2956 gcc_assert (my_function && my_function->cfg);
2957 FOR_EACH_BB_FN (bb, my_function)
2959 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2960 n += estimate_num_insns (gsi_stmt (bsi), weights);
2963 return n;
2967 /* Initializes weights used by estimate_num_insns. */
2969 void
2970 init_inline_once (void)
2972 eni_inlining_weights.call_cost = PARAM_VALUE (PARAM_INLINE_CALL_COST);
2973 eni_inlining_weights.target_builtin_call_cost = 1;
2974 eni_inlining_weights.div_mod_cost = 10;
2975 eni_inlining_weights.omp_cost = 40;
2977 eni_size_weights.call_cost = 1;
2978 eni_size_weights.target_builtin_call_cost = 1;
2979 eni_size_weights.div_mod_cost = 1;
2980 eni_size_weights.omp_cost = 40;
2982 /* Estimating time for call is difficult, since we have no idea what the
2983 called function does. In the current uses of eni_time_weights,
2984 underestimating the cost does less harm than overestimating it, so
2985 we choose a rather small value here. */
2986 eni_time_weights.call_cost = 10;
2987 eni_time_weights.target_builtin_call_cost = 10;
2988 eni_time_weights.div_mod_cost = 10;
2989 eni_time_weights.omp_cost = 40;
2992 /* Estimate the number of instructions in a gimple_seq. */
2995 count_insns_seq (gimple_seq seq, eni_weights *weights)
2997 gimple_stmt_iterator gsi;
2998 int n = 0;
2999 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
3000 n += estimate_num_insns (gsi_stmt (gsi), weights);
3002 return n;
3006 /* Install new lexical TREE_BLOCK underneath 'current_block'. */
3008 static void
3009 add_lexical_block (tree current_block, tree new_block)
3011 tree *blk_p;
3013 /* Walk to the last sub-block. */
3014 for (blk_p = &BLOCK_SUBBLOCKS (current_block);
3015 *blk_p;
3016 blk_p = &BLOCK_CHAIN (*blk_p))
3018 *blk_p = new_block;
3019 BLOCK_SUPERCONTEXT (new_block) = current_block;
3022 /* Fetch callee declaration from the call graph edge going from NODE and
3023 associated with STMR call statement. Return NULL_TREE if not found. */
3024 static tree
3025 get_indirect_callee_fndecl (struct cgraph_node *node, gimple stmt)
3027 struct cgraph_edge *cs;
3029 cs = cgraph_edge (node, stmt);
3030 if (cs)
3031 return cs->callee->decl;
3033 return NULL_TREE;
3036 /* If STMT is a GIMPLE_CALL, replace it with its inline expansion. */
3038 static bool
3039 expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
3041 tree retvar, use_retvar;
3042 tree fn;
3043 struct pointer_map_t *st;
3044 tree return_slot;
3045 tree modify_dest;
3046 location_t saved_location;
3047 struct cgraph_edge *cg_edge;
3048 const char *reason;
3049 basic_block return_block;
3050 edge e;
3051 gimple_stmt_iterator gsi, stmt_gsi;
3052 bool successfully_inlined = FALSE;
3053 bool purge_dead_abnormal_edges;
3054 tree t_step;
3055 tree var;
3057 /* Set input_location here so we get the right instantiation context
3058 if we call instantiate_decl from inlinable_function_p. */
3059 saved_location = input_location;
3060 if (gimple_has_location (stmt))
3061 input_location = gimple_location (stmt);
3063 /* From here on, we're only interested in CALL_EXPRs. */
3064 if (gimple_code (stmt) != GIMPLE_CALL)
3065 goto egress;
3067 /* First, see if we can figure out what function is being called.
3068 If we cannot, then there is no hope of inlining the function. */
3069 fn = gimple_call_fndecl (stmt);
3070 if (!fn)
3072 fn = get_indirect_callee_fndecl (id->dst_node, stmt);
3073 if (!fn)
3074 goto egress;
3077 /* Turn forward declarations into real ones. */
3078 fn = cgraph_node (fn)->decl;
3080 /* If FN is a declaration of a function in a nested scope that was
3081 globally declared inline, we don't set its DECL_INITIAL.
3082 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
3083 C++ front-end uses it for cdtors to refer to their internal
3084 declarations, that are not real functions. Fortunately those
3085 don't have trees to be saved, so we can tell by checking their
3086 gimple_body. */
3087 if (!DECL_INITIAL (fn)
3088 && DECL_ABSTRACT_ORIGIN (fn)
3089 && gimple_body (DECL_ABSTRACT_ORIGIN (fn)))
3090 fn = DECL_ABSTRACT_ORIGIN (fn);
3092 /* Objective C and fortran still calls tree_rest_of_compilation directly.
3093 Kill this check once this is fixed. */
3094 if (!id->dst_node->analyzed)
3095 goto egress;
3097 cg_edge = cgraph_edge (id->dst_node, stmt);
3099 /* Constant propagation on argument done during previous inlining
3100 may create new direct call. Produce an edge for it. */
3101 if (!cg_edge)
3103 struct cgraph_node *dest = cgraph_node (fn);
3105 /* We have missing edge in the callgraph. This can happen in one case
3106 where previous inlining turned indirect call into direct call by
3107 constant propagating arguments. In all other cases we hit a bug
3108 (incorrect node sharing is most common reason for missing edges. */
3109 gcc_assert (dest->needed);
3110 cgraph_create_edge (id->dst_node, dest, stmt,
3111 bb->count, CGRAPH_FREQ_BASE,
3112 bb->loop_depth)->inline_failed
3113 = N_("originally indirect function call not considered for inlining");
3114 if (dump_file)
3116 fprintf (dump_file, "Created new direct edge to %s",
3117 cgraph_node_name (dest));
3119 goto egress;
3122 /* Don't try to inline functions that are not well-suited to
3123 inlining. */
3124 if (!cgraph_inline_p (cg_edge, &reason))
3126 /* If this call was originally indirect, we do not want to emit any
3127 inlining related warnings or sorry messages because there are no
3128 guarantees regarding those. */
3129 if (cg_edge->indirect_call)
3130 goto egress;
3132 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
3133 /* Avoid warnings during early inline pass. */
3134 && cgraph_global_info_ready)
3136 sorry ("inlining failed in call to %q+F: %s", fn, reason);
3137 sorry ("called from here");
3139 else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
3140 && !DECL_IN_SYSTEM_HEADER (fn)
3141 && strlen (reason)
3142 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
3143 /* Avoid warnings during early inline pass. */
3144 && cgraph_global_info_ready)
3146 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
3147 fn, reason);
3148 warning (OPT_Winline, "called from here");
3150 goto egress;
3152 fn = cg_edge->callee->decl;
3154 #ifdef ENABLE_CHECKING
3155 if (cg_edge->callee->decl != id->dst_node->decl)
3156 verify_cgraph_node (cg_edge->callee);
3157 #endif
3159 /* We will be inlining this callee. */
3160 id->eh_region = lookup_stmt_eh_region (stmt);
3162 /* Split the block holding the GIMPLE_CALL. */
3163 e = split_block (bb, stmt);
3164 bb = e->src;
3165 return_block = e->dest;
3166 remove_edge (e);
3168 /* split_block splits after the statement; work around this by
3169 moving the call into the second block manually. Not pretty,
3170 but seems easier than doing the CFG manipulation by hand
3171 when the GIMPLE_CALL is in the last statement of BB. */
3172 stmt_gsi = gsi_last_bb (bb);
3173 gsi_remove (&stmt_gsi, false);
3175 /* If the GIMPLE_CALL was in the last statement of BB, it may have
3176 been the source of abnormal edges. In this case, schedule
3177 the removal of dead abnormal edges. */
3178 gsi = gsi_start_bb (return_block);
3179 if (gsi_end_p (gsi))
3181 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
3182 purge_dead_abnormal_edges = true;
3184 else
3186 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
3187 purge_dead_abnormal_edges = false;
3190 stmt_gsi = gsi_start_bb (return_block);
3192 /* Build a block containing code to initialize the arguments, the
3193 actual inline expansion of the body, and a label for the return
3194 statements within the function to jump to. The type of the
3195 statement expression is the return type of the function call. */
3196 id->block = make_node (BLOCK);
3197 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
3198 BLOCK_SOURCE_LOCATION (id->block) = input_location;
3199 add_lexical_block (gimple_block (stmt), id->block);
3201 /* Local declarations will be replaced by their equivalents in this
3202 map. */
3203 st = id->decl_map;
3204 id->decl_map = pointer_map_create ();
3206 /* Record the function we are about to inline. */
3207 id->src_fn = fn;
3208 id->src_node = cg_edge->callee;
3209 id->src_cfun = DECL_STRUCT_FUNCTION (fn);
3210 id->gimple_call = stmt;
3212 gcc_assert (!id->src_cfun->after_inlining);
3214 id->entry_bb = bb;
3215 initialize_inlined_parameters (id, stmt, fn, bb);
3217 if (DECL_INITIAL (fn))
3218 add_lexical_block (id->block, remap_blocks (DECL_INITIAL (fn), id));
3220 /* Return statements in the function body will be replaced by jumps
3221 to the RET_LABEL. */
3222 gcc_assert (DECL_INITIAL (fn));
3223 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
3225 /* Find the LHS to which the result of this call is assigned. */
3226 return_slot = NULL;
3227 if (gimple_call_lhs (stmt))
3229 modify_dest = gimple_call_lhs (stmt);
3231 /* The function which we are inlining might not return a value,
3232 in which case we should issue a warning that the function
3233 does not return a value. In that case the optimizers will
3234 see that the variable to which the value is assigned was not
3235 initialized. We do not want to issue a warning about that
3236 uninitialized variable. */
3237 if (DECL_P (modify_dest))
3238 TREE_NO_WARNING (modify_dest) = 1;
3240 if (gimple_call_return_slot_opt_p (stmt))
3242 return_slot = modify_dest;
3243 modify_dest = NULL;
3246 else
3247 modify_dest = NULL;
3249 /* If we are inlining a call to the C++ operator new, we don't want
3250 to use type based alias analysis on the return value. Otherwise
3251 we may get confused if the compiler sees that the inlined new
3252 function returns a pointer which was just deleted. See bug
3253 33407. */
3254 if (DECL_IS_OPERATOR_NEW (fn))
3256 return_slot = NULL;
3257 modify_dest = NULL;
3260 /* Declare the return variable for the function. */
3261 retvar = declare_return_variable (id, return_slot, modify_dest, &use_retvar);
3263 if (DECL_IS_OPERATOR_NEW (fn))
3265 gcc_assert (TREE_CODE (retvar) == VAR_DECL
3266 && POINTER_TYPE_P (TREE_TYPE (retvar)));
3267 DECL_NO_TBAA_P (retvar) = 1;
3270 /* This is it. Duplicate the callee body. Assume callee is
3271 pre-gimplified. Note that we must not alter the caller
3272 function in any way before this point, as this CALL_EXPR may be
3273 a self-referential call; if we're calling ourselves, we need to
3274 duplicate our body before altering anything. */
3275 copy_body (id, bb->count, bb->frequency, bb, return_block);
3277 /* Add local vars in this inlined callee to caller. */
3278 t_step = id->src_cfun->local_decls;
3279 for (; t_step; t_step = TREE_CHAIN (t_step))
3281 var = TREE_VALUE (t_step);
3282 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
3283 cfun->local_decls = tree_cons (NULL_TREE, var,
3284 cfun->local_decls);
3285 else
3286 cfun->local_decls = tree_cons (NULL_TREE, remap_decl (var, id),
3287 cfun->local_decls);
3290 /* Clean up. */
3291 pointer_map_destroy (id->decl_map);
3292 id->decl_map = st;
3294 /* If the inlined function returns a result that we care about,
3295 substitute the GIMPLE_CALL with an assignment of the return
3296 variable to the LHS of the call. That is, if STMT was
3297 'a = foo (...)', substitute the call with 'a = USE_RETVAR'. */
3298 if (use_retvar && gimple_call_lhs (stmt))
3300 gimple old_stmt = stmt;
3301 stmt = gimple_build_assign (gimple_call_lhs (stmt), use_retvar);
3302 gsi_replace (&stmt_gsi, stmt, false);
3303 if (gimple_in_ssa_p (cfun))
3305 update_stmt (stmt);
3306 mark_symbols_for_renaming (stmt);
3308 maybe_clean_or_replace_eh_stmt (old_stmt, stmt);
3310 else
3312 /* Handle the case of inlining a function with no return
3313 statement, which causes the return value to become undefined. */
3314 if (gimple_call_lhs (stmt)
3315 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
3317 tree name = gimple_call_lhs (stmt);
3318 tree var = SSA_NAME_VAR (name);
3319 tree def = gimple_default_def (cfun, var);
3321 if (def)
3323 /* If the variable is used undefined, make this name
3324 undefined via a move. */
3325 stmt = gimple_build_assign (gimple_call_lhs (stmt), def);
3326 gsi_replace (&stmt_gsi, stmt, true);
3327 update_stmt (stmt);
3329 else
3331 /* Otherwise make this variable undefined. */
3332 gsi_remove (&stmt_gsi, true);
3333 set_default_def (var, name);
3334 SSA_NAME_DEF_STMT (name) = gimple_build_nop ();
3337 else
3338 gsi_remove (&stmt_gsi, true);
3341 if (purge_dead_abnormal_edges)
3342 gimple_purge_dead_abnormal_call_edges (return_block);
3344 /* If the value of the new expression is ignored, that's OK. We
3345 don't warn about this for CALL_EXPRs, so we shouldn't warn about
3346 the equivalent inlined version either. */
3347 if (is_gimple_assign (stmt))
3349 gcc_assert (gimple_assign_single_p (stmt)
3350 || gimple_assign_rhs_code (stmt) == NOP_EXPR
3351 || gimple_assign_rhs_code (stmt) == CONVERT_EXPR);
3352 TREE_USED (gimple_assign_rhs1 (stmt)) = 1;
3355 /* Output the inlining info for this abstract function, since it has been
3356 inlined. If we don't do this now, we can lose the information about the
3357 variables in the function when the blocks get blown away as soon as we
3358 remove the cgraph node. */
3359 (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
3361 /* Update callgraph if needed. */
3362 cgraph_remove_node (cg_edge->callee);
3364 id->block = NULL_TREE;
3365 successfully_inlined = TRUE;
3367 egress:
3368 input_location = saved_location;
3369 return successfully_inlined;
3372 /* Expand call statements reachable from STMT_P.
3373 We can only have CALL_EXPRs as the "toplevel" tree code or nested
3374 in a MODIFY_EXPR. See tree-gimple.c:get_call_expr_in(). We can
3375 unfortunately not use that function here because we need a pointer
3376 to the CALL_EXPR, not the tree itself. */
3378 static bool
3379 gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
3381 gimple_stmt_iterator gsi;
3383 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3385 gimple stmt = gsi_stmt (gsi);
3387 if (is_gimple_call (stmt)
3388 && expand_call_inline (bb, stmt, id))
3389 return true;
3392 return false;
3396 /* Walk all basic blocks created after FIRST and try to fold every statement
3397 in the STATEMENTS pointer set. */
3399 static void
3400 fold_marked_statements (int first, struct pointer_set_t *statements)
3402 for (; first < n_basic_blocks; first++)
3403 if (BASIC_BLOCK (first))
3405 gimple_stmt_iterator gsi;
3407 for (gsi = gsi_start_bb (BASIC_BLOCK (first));
3408 !gsi_end_p (gsi);
3409 gsi_next (&gsi))
3410 if (pointer_set_contains (statements, gsi_stmt (gsi)))
3412 gimple old_stmt = gsi_stmt (gsi);
3414 if (fold_stmt (&gsi))
3416 /* Re-read the statement from GSI as fold_stmt() may
3417 have changed it. */
3418 gimple new_stmt = gsi_stmt (gsi);
3419 update_stmt (new_stmt);
3421 if (is_gimple_call (old_stmt))
3422 cgraph_update_edges_for_call_stmt (old_stmt, new_stmt);
3424 if (maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt))
3425 gimple_purge_dead_eh_edges (BASIC_BLOCK (first));
3431 /* Return true if BB has at least one abnormal outgoing edge. */
3433 static inline bool
3434 has_abnormal_outgoing_edge_p (basic_block bb)
3436 edge e;
3437 edge_iterator ei;
3439 FOR_EACH_EDGE (e, ei, bb->succs)
3440 if (e->flags & EDGE_ABNORMAL)
3441 return true;
3443 return false;
3446 /* Expand calls to inline functions in the body of FN. */
3448 unsigned int
3449 optimize_inline_calls (tree fn)
3451 copy_body_data id;
3452 tree prev_fn;
3453 basic_block bb;
3454 int last = n_basic_blocks;
3455 struct gimplify_ctx gctx;
3457 /* There is no point in performing inlining if errors have already
3458 occurred -- and we might crash if we try to inline invalid
3459 code. */
3460 if (errorcount || sorrycount)
3461 return 0;
3463 /* Clear out ID. */
3464 memset (&id, 0, sizeof (id));
3466 id.src_node = id.dst_node = cgraph_node (fn);
3467 id.dst_fn = fn;
3468 /* Or any functions that aren't finished yet. */
3469 prev_fn = NULL_TREE;
3470 if (current_function_decl)
3472 id.dst_fn = current_function_decl;
3473 prev_fn = current_function_decl;
3476 id.copy_decl = copy_decl_maybe_to_var;
3477 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
3478 id.transform_new_cfg = false;
3479 id.transform_return_to_modify = true;
3480 id.transform_lang_insert_block = NULL;
3481 id.statements_to_fold = pointer_set_create ();
3483 push_gimplify_context (&gctx);
3485 /* We make no attempts to keep dominance info up-to-date. */
3486 free_dominance_info (CDI_DOMINATORS);
3487 free_dominance_info (CDI_POST_DOMINATORS);
3489 /* Register specific gimple functions. */
3490 gimple_register_cfg_hooks ();
3492 /* Reach the trees by walking over the CFG, and note the
3493 enclosing basic-blocks in the call edges. */
3494 /* We walk the blocks going forward, because inlined function bodies
3495 will split id->current_basic_block, and the new blocks will
3496 follow it; we'll trudge through them, processing their CALL_EXPRs
3497 along the way. */
3498 FOR_EACH_BB (bb)
3499 gimple_expand_calls_inline (bb, &id);
3501 pop_gimplify_context (NULL);
3503 #ifdef ENABLE_CHECKING
3505 struct cgraph_edge *e;
3507 verify_cgraph_node (id.dst_node);
3509 /* Double check that we inlined everything we are supposed to inline. */
3510 for (e = id.dst_node->callees; e; e = e->next_callee)
3511 gcc_assert (e->inline_failed);
3513 #endif
3515 /* Fold the statements before compacting/renumbering the basic blocks. */
3516 fold_marked_statements (last, id.statements_to_fold);
3517 pointer_set_destroy (id.statements_to_fold);
3519 /* Renumber the (code) basic_blocks consecutively. */
3520 compact_blocks ();
3521 /* Renumber the lexical scoping (non-code) blocks consecutively. */
3522 number_blocks (fn);
3524 /* We are not going to maintain the cgraph edges up to date.
3525 Kill it so it won't confuse us. */
3526 cgraph_node_remove_callees (id.dst_node);
3528 fold_cond_expr_cond ();
3530 /* It would be nice to check SSA/CFG/statement consistency here, but it is
3531 not possible yet - the IPA passes might make various functions to not
3532 throw and they don't care to proactively update local EH info. This is
3533 done later in fixup_cfg pass that also execute the verification. */
3534 return (TODO_update_ssa
3535 | TODO_cleanup_cfg
3536 | (gimple_in_ssa_p (cfun) ? TODO_remove_unused_locals : 0)
3537 | (profile_status != PROFILE_ABSENT ? TODO_rebuild_frequencies : 0));
3540 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
3542 tree
3543 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
3545 enum tree_code code = TREE_CODE (*tp);
3546 enum tree_code_class cl = TREE_CODE_CLASS (code);
3548 /* We make copies of most nodes. */
3549 if (IS_EXPR_CODE_CLASS (cl)
3550 || code == TREE_LIST
3551 || code == TREE_VEC
3552 || code == TYPE_DECL
3553 || code == OMP_CLAUSE)
3555 /* Because the chain gets clobbered when we make a copy, we save it
3556 here. */
3557 tree chain = NULL_TREE, new;
3559 chain = TREE_CHAIN (*tp);
3561 /* Copy the node. */
3562 new = copy_node (*tp);
3564 /* Propagate mudflap marked-ness. */
3565 if (flag_mudflap && mf_marked_p (*tp))
3566 mf_mark (new);
3568 *tp = new;
3570 /* Now, restore the chain, if appropriate. That will cause
3571 walk_tree to walk into the chain as well. */
3572 if (code == PARM_DECL
3573 || code == TREE_LIST
3574 || code == OMP_CLAUSE)
3575 TREE_CHAIN (*tp) = chain;
3577 /* For now, we don't update BLOCKs when we make copies. So, we
3578 have to nullify all BIND_EXPRs. */
3579 if (TREE_CODE (*tp) == BIND_EXPR)
3580 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
3582 else if (code == CONSTRUCTOR)
3584 /* CONSTRUCTOR nodes need special handling because
3585 we need to duplicate the vector of elements. */
3586 tree new;
3588 new = copy_node (*tp);
3590 /* Propagate mudflap marked-ness. */
3591 if (flag_mudflap && mf_marked_p (*tp))
3592 mf_mark (new);
3594 CONSTRUCTOR_ELTS (new) = VEC_copy (constructor_elt, gc,
3595 CONSTRUCTOR_ELTS (*tp));
3596 *tp = new;
3598 else if (TREE_CODE_CLASS (code) == tcc_type)
3599 *walk_subtrees = 0;
3600 else if (TREE_CODE_CLASS (code) == tcc_declaration)
3601 *walk_subtrees = 0;
3602 else if (TREE_CODE_CLASS (code) == tcc_constant)
3603 *walk_subtrees = 0;
3604 else
3605 gcc_assert (code != STATEMENT_LIST);
3606 return NULL_TREE;
3609 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
3610 information indicating to what new SAVE_EXPR this one should be mapped,
3611 use that one. Otherwise, create a new node and enter it in ST. FN is
3612 the function into which the copy will be placed. */
3614 static void
3615 remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
3617 struct pointer_map_t *st = (struct pointer_map_t *) st_;
3618 tree *n;
3619 tree t;
3621 /* See if we already encountered this SAVE_EXPR. */
3622 n = (tree *) pointer_map_contains (st, *tp);
3624 /* If we didn't already remap this SAVE_EXPR, do so now. */
3625 if (!n)
3627 t = copy_node (*tp);
3629 /* Remember this SAVE_EXPR. */
3630 *pointer_map_insert (st, *tp) = t;
3631 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
3632 *pointer_map_insert (st, t) = t;
3634 else
3636 /* We've already walked into this SAVE_EXPR; don't do it again. */
3637 *walk_subtrees = 0;
3638 t = *n;
3641 /* Replace this SAVE_EXPR with the copy. */
3642 *tp = t;
3645 /* Called via walk_tree. If *TP points to a DECL_STMT for a local label,
3646 copies the declaration and enters it in the splay_tree in DATA (which is
3647 really an `copy_body_data *'). */
3649 static tree
3650 mark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
3651 void *data)
3653 copy_body_data *id = (copy_body_data *) data;
3655 /* Don't walk into types. */
3656 if (TYPE_P (*tp))
3657 *walk_subtrees = 0;
3659 else if (TREE_CODE (*tp) == LABEL_EXPR)
3661 tree decl = TREE_OPERAND (*tp, 0);
3663 /* Copy the decl and remember the copy. */
3664 insert_decl_map (id, decl, id->copy_decl (decl, id));
3667 return NULL_TREE;
3670 /* Perform any modifications to EXPR required when it is unsaved. Does
3671 not recurse into EXPR's subtrees. */
3673 static void
3674 unsave_expr_1 (tree expr)
3676 switch (TREE_CODE (expr))
3678 case TARGET_EXPR:
3679 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
3680 It's OK for this to happen if it was part of a subtree that
3681 isn't immediately expanded, such as operand 2 of another
3682 TARGET_EXPR. */
3683 if (TREE_OPERAND (expr, 1))
3684 break;
3686 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
3687 TREE_OPERAND (expr, 3) = NULL_TREE;
3688 break;
3690 default:
3691 break;
3695 /* Called via walk_tree when an expression is unsaved. Using the
3696 splay_tree pointed to by ST (which is really a `splay_tree'),
3697 remaps all local declarations to appropriate replacements. */
3699 static tree
3700 unsave_r (tree *tp, int *walk_subtrees, void *data)
3702 copy_body_data *id = (copy_body_data *) data;
3703 struct pointer_map_t *st = id->decl_map;
3704 tree *n;
3706 /* Only a local declaration (variable or label). */
3707 if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
3708 || TREE_CODE (*tp) == LABEL_DECL)
3710 /* Lookup the declaration. */
3711 n = (tree *) pointer_map_contains (st, *tp);
3713 /* If it's there, remap it. */
3714 if (n)
3715 *tp = *n;
3718 else if (TREE_CODE (*tp) == STATEMENT_LIST)
3719 gcc_unreachable ();
3720 else if (TREE_CODE (*tp) == BIND_EXPR)
3721 copy_bind_expr (tp, walk_subtrees, id);
3722 else if (TREE_CODE (*tp) == SAVE_EXPR)
3723 remap_save_expr (tp, st, walk_subtrees);
3724 else
3726 copy_tree_r (tp, walk_subtrees, NULL);
3728 /* Do whatever unsaving is required. */
3729 unsave_expr_1 (*tp);
3732 /* Keep iterating. */
3733 return NULL_TREE;
3736 /* Copies everything in EXPR and replaces variables, labels
3737 and SAVE_EXPRs local to EXPR. */
3739 tree
3740 unsave_expr_now (tree expr)
3742 copy_body_data id;
3744 /* There's nothing to do for NULL_TREE. */
3745 if (expr == 0)
3746 return expr;
3748 /* Set up ID. */
3749 memset (&id, 0, sizeof (id));
3750 id.src_fn = current_function_decl;
3751 id.dst_fn = current_function_decl;
3752 id.decl_map = pointer_map_create ();
3754 id.copy_decl = copy_decl_no_change;
3755 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
3756 id.transform_new_cfg = false;
3757 id.transform_return_to_modify = false;
3758 id.transform_lang_insert_block = NULL;
3760 /* Walk the tree once to find local labels. */
3761 walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
3763 /* Walk the tree again, copying, remapping, and unsaving. */
3764 walk_tree (&expr, unsave_r, &id, NULL);
3766 /* Clean up. */
3767 pointer_map_destroy (id.decl_map);
3769 return expr;
3772 /* Called via walk_gimple_seq. If *GSIP points to a GIMPLE_LABEL for a local
3773 label, copies the declaration and enters it in the splay_tree in DATA (which
3774 is really a 'copy_body_data *'. */
3776 static tree
3777 mark_local_labels_stmt (gimple_stmt_iterator *gsip,
3778 bool *handled_ops_p ATTRIBUTE_UNUSED,
3779 struct walk_stmt_info *wi)
3781 copy_body_data *id = (copy_body_data *) wi->info;
3782 gimple stmt = gsi_stmt (*gsip);
3784 if (gimple_code (stmt) == GIMPLE_LABEL)
3786 tree decl = gimple_label_label (stmt);
3788 /* Copy the decl and remember the copy. */
3789 insert_decl_map (id, decl, id->copy_decl (decl, id));
3792 return NULL_TREE;
3796 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
3797 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
3798 remaps all local declarations to appropriate replacements in gimple
3799 operands. */
3801 static tree
3802 replace_locals_op (tree *tp, int *walk_subtrees, void *data)
3804 struct walk_stmt_info *wi = (struct walk_stmt_info*) data;
3805 copy_body_data *id = (copy_body_data *) wi->info;
3806 struct pointer_map_t *st = id->decl_map;
3807 tree *n;
3808 tree expr = *tp;
3810 /* Only a local declaration (variable or label). */
3811 if ((TREE_CODE (expr) == VAR_DECL
3812 && !TREE_STATIC (expr))
3813 || TREE_CODE (expr) == LABEL_DECL)
3815 /* Lookup the declaration. */
3816 n = (tree *) pointer_map_contains (st, expr);
3818 /* If it's there, remap it. */
3819 if (n)
3820 *tp = *n;
3821 *walk_subtrees = 0;
3823 else if (TREE_CODE (expr) == STATEMENT_LIST
3824 || TREE_CODE (expr) == BIND_EXPR
3825 || TREE_CODE (expr) == SAVE_EXPR)
3826 gcc_unreachable ();
3827 else if (TREE_CODE (expr) == TARGET_EXPR)
3829 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
3830 It's OK for this to happen if it was part of a subtree that
3831 isn't immediately expanded, such as operand 2 of another
3832 TARGET_EXPR. */
3833 if (!TREE_OPERAND (expr, 1))
3835 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
3836 TREE_OPERAND (expr, 3) = NULL_TREE;
3840 /* Keep iterating. */
3841 return NULL_TREE;
3845 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
3846 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
3847 remaps all local declarations to appropriate replacements in gimple
3848 statements. */
3850 static tree
3851 replace_locals_stmt (gimple_stmt_iterator *gsip,
3852 bool *handled_ops_p ATTRIBUTE_UNUSED,
3853 struct walk_stmt_info *wi)
3855 copy_body_data *id = (copy_body_data *) wi->info;
3856 gimple stmt = gsi_stmt (*gsip);
3858 if (gimple_code (stmt) == GIMPLE_BIND)
3860 tree block = gimple_bind_block (stmt);
3862 if (block)
3864 remap_block (&block, id);
3865 gimple_bind_set_block (stmt, block);
3868 /* This will remap a lot of the same decls again, but this should be
3869 harmless. */
3870 if (gimple_bind_vars (stmt))
3871 gimple_bind_set_vars (stmt, remap_decls (gimple_bind_vars (stmt), id));
3874 /* Keep iterating. */
3875 return NULL_TREE;
3879 /* Copies everything in SEQ and replaces variables and labels local to
3880 current_function_decl. */
3882 gimple_seq
3883 copy_gimple_seq_and_replace_locals (gimple_seq seq)
3885 copy_body_data id;
3886 struct walk_stmt_info wi;
3887 struct pointer_set_t *visited;
3888 gimple_seq copy;
3890 /* There's nothing to do for NULL_TREE. */
3891 if (seq == NULL)
3892 return seq;
3894 /* Set up ID. */
3895 memset (&id, 0, sizeof (id));
3896 id.src_fn = current_function_decl;
3897 id.dst_fn = current_function_decl;
3898 id.decl_map = pointer_map_create ();
3900 id.copy_decl = copy_decl_no_change;
3901 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
3902 id.transform_new_cfg = false;
3903 id.transform_return_to_modify = false;
3904 id.transform_lang_insert_block = NULL;
3906 /* Walk the tree once to find local labels. */
3907 memset (&wi, 0, sizeof (wi));
3908 visited = pointer_set_create ();
3909 wi.info = &id;
3910 wi.pset = visited;
3911 walk_gimple_seq (seq, mark_local_labels_stmt, NULL, &wi);
3912 pointer_set_destroy (visited);
3914 copy = gimple_seq_copy (seq);
3916 /* Walk the copy, remapping decls. */
3917 memset (&wi, 0, sizeof (wi));
3918 wi.info = &id;
3919 walk_gimple_seq (copy, replace_locals_stmt, replace_locals_op, &wi);
3921 /* Clean up. */
3922 pointer_map_destroy (id.decl_map);
3924 return copy;
3928 /* Allow someone to determine if SEARCH is a child of TOP from gdb. */
3930 static tree
3931 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
3933 if (*tp == data)
3934 return (tree) data;
3935 else
3936 return NULL;
3939 bool
3940 debug_find_tree (tree top, tree search)
3942 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
3946 /* Declare the variables created by the inliner. Add all the variables in
3947 VARS to BIND_EXPR. */
3949 static void
3950 declare_inline_vars (tree block, tree vars)
3952 tree t;
3953 for (t = vars; t; t = TREE_CHAIN (t))
3955 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
3956 gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
3957 cfun->local_decls = tree_cons (NULL_TREE, t, cfun->local_decls);
3960 if (block)
3961 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
3964 /* Copy NODE (which must be a DECL). The DECL originally was in the FROM_FN,
3965 but now it will be in the TO_FN. PARM_TO_VAR means enable PARM_DECL to
3966 VAR_DECL translation. */
3968 static tree
3969 copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
3971 /* Don't generate debug information for the copy if we wouldn't have
3972 generated it for the copy either. */
3973 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
3974 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
3976 /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
3977 declaration inspired this copy. */
3978 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
3980 /* The new variable/label has no RTL, yet. */
3981 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
3982 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
3983 SET_DECL_RTL (copy, NULL_RTX);
3985 /* These args would always appear unused, if not for this. */
3986 TREE_USED (copy) = 1;
3988 /* Set the context for the new declaration. */
3989 if (!DECL_CONTEXT (decl))
3990 /* Globals stay global. */
3992 else if (DECL_CONTEXT (decl) != id->src_fn)
3993 /* Things that weren't in the scope of the function we're inlining
3994 from aren't in the scope we're inlining to, either. */
3996 else if (TREE_STATIC (decl))
3997 /* Function-scoped static variables should stay in the original
3998 function. */
4000 else
4001 /* Ordinary automatic local variables are now in the scope of the
4002 new function. */
4003 DECL_CONTEXT (copy) = id->dst_fn;
4005 return copy;
4008 static tree
4009 copy_decl_to_var (tree decl, copy_body_data *id)
4011 tree copy, type;
4013 gcc_assert (TREE_CODE (decl) == PARM_DECL
4014 || TREE_CODE (decl) == RESULT_DECL);
4016 type = TREE_TYPE (decl);
4018 copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
4019 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4020 TREE_READONLY (copy) = TREE_READONLY (decl);
4021 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
4022 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
4023 DECL_NO_TBAA_P (copy) = DECL_NO_TBAA_P (decl);
4025 return copy_decl_for_dup_finish (id, decl, copy);
4028 /* Like copy_decl_to_var, but create a return slot object instead of a
4029 pointer variable for return by invisible reference. */
4031 static tree
4032 copy_result_decl_to_var (tree decl, copy_body_data *id)
4034 tree copy, type;
4036 gcc_assert (TREE_CODE (decl) == PARM_DECL
4037 || TREE_CODE (decl) == RESULT_DECL);
4039 type = TREE_TYPE (decl);
4040 if (DECL_BY_REFERENCE (decl))
4041 type = TREE_TYPE (type);
4043 copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
4044 TREE_READONLY (copy) = TREE_READONLY (decl);
4045 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
4046 if (!DECL_BY_REFERENCE (decl))
4048 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4049 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
4050 DECL_NO_TBAA_P (copy) = DECL_NO_TBAA_P (decl);
4053 return copy_decl_for_dup_finish (id, decl, copy);
4056 tree
4057 copy_decl_no_change (tree decl, copy_body_data *id)
4059 tree copy;
4061 copy = copy_node (decl);
4063 /* The COPY is not abstract; it will be generated in DST_FN. */
4064 DECL_ABSTRACT (copy) = 0;
4065 lang_hooks.dup_lang_specific_decl (copy);
4067 /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
4068 been taken; it's for internal bookkeeping in expand_goto_internal. */
4069 if (TREE_CODE (copy) == LABEL_DECL)
4071 TREE_ADDRESSABLE (copy) = 0;
4072 LABEL_DECL_UID (copy) = -1;
4075 return copy_decl_for_dup_finish (id, decl, copy);
4078 static tree
4079 copy_decl_maybe_to_var (tree decl, copy_body_data *id)
4081 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
4082 return copy_decl_to_var (decl, id);
4083 else
4084 return copy_decl_no_change (decl, id);
4087 /* Return a copy of the function's argument tree. */
4088 static tree
4089 copy_arguments_for_versioning (tree orig_parm, copy_body_data * id)
4091 tree *arg_copy, *parg;
4093 arg_copy = &orig_parm;
4094 for (parg = arg_copy; *parg; parg = &TREE_CHAIN (*parg))
4096 tree new = remap_decl (*parg, id);
4097 lang_hooks.dup_lang_specific_decl (new);
4098 TREE_CHAIN (new) = TREE_CHAIN (*parg);
4099 *parg = new;
4101 return orig_parm;
4104 /* Return a copy of the function's static chain. */
4105 static tree
4106 copy_static_chain (tree static_chain, copy_body_data * id)
4108 tree *chain_copy, *pvar;
4110 chain_copy = &static_chain;
4111 for (pvar = chain_copy; *pvar; pvar = &TREE_CHAIN (*pvar))
4113 tree new = remap_decl (*pvar, id);
4114 lang_hooks.dup_lang_specific_decl (new);
4115 TREE_CHAIN (new) = TREE_CHAIN (*pvar);
4116 *pvar = new;
4118 return static_chain;
4121 /* Return true if the function is allowed to be versioned.
4122 This is a guard for the versioning functionality. */
4123 bool
4124 tree_versionable_function_p (tree fndecl)
4126 if (fndecl == NULL_TREE)
4127 return false;
4128 /* ??? There are cases where a function is
4129 uninlinable but can be versioned. */
4130 if (!tree_inlinable_function_p (fndecl))
4131 return false;
4133 return true;
4136 /* Create a copy of a function's tree.
4137 OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
4138 of the original function and the new copied function
4139 respectively. In case we want to replace a DECL
4140 tree with another tree while duplicating the function's
4141 body, TREE_MAP represents the mapping between these
4142 trees. If UPDATE_CLONES is set, the call_stmt fields
4143 of edges of clones of the function will be updated. */
4144 void
4145 tree_function_versioning (tree old_decl, tree new_decl, varray_type tree_map,
4146 bool update_clones)
4148 struct cgraph_node *old_version_node;
4149 struct cgraph_node *new_version_node;
4150 copy_body_data id;
4151 tree p;
4152 unsigned i;
4153 struct ipa_replace_map *replace_info;
4154 basic_block old_entry_block;
4155 tree t_step;
4156 tree old_current_function_decl = current_function_decl;
4158 gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
4159 && TREE_CODE (new_decl) == FUNCTION_DECL);
4160 DECL_POSSIBLY_INLINED (old_decl) = 1;
4162 old_version_node = cgraph_node (old_decl);
4163 new_version_node = cgraph_node (new_decl);
4165 DECL_ARTIFICIAL (new_decl) = 1;
4166 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
4168 /* Prepare the data structures for the tree copy. */
4169 memset (&id, 0, sizeof (id));
4171 /* Generate a new name for the new version. */
4172 if (!update_clones)
4174 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
4175 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
4176 SET_DECL_RTL (new_decl, NULL_RTX);
4177 id.statements_to_fold = pointer_set_create ();
4180 id.decl_map = pointer_map_create ();
4181 id.src_fn = old_decl;
4182 id.dst_fn = new_decl;
4183 id.src_node = old_version_node;
4184 id.dst_node = new_version_node;
4185 id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
4187 id.copy_decl = copy_decl_no_change;
4188 id.transform_call_graph_edges
4189 = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
4190 id.transform_new_cfg = true;
4191 id.transform_return_to_modify = false;
4192 id.transform_lang_insert_block = NULL;
4194 current_function_decl = new_decl;
4195 old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
4196 (DECL_STRUCT_FUNCTION (old_decl));
4197 initialize_cfun (new_decl, old_decl,
4198 old_entry_block->count,
4199 old_entry_block->frequency);
4200 push_cfun (DECL_STRUCT_FUNCTION (new_decl));
4202 /* Copy the function's static chain. */
4203 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
4204 if (p)
4205 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
4206 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
4207 &id);
4208 /* Copy the function's arguments. */
4209 if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
4210 DECL_ARGUMENTS (new_decl) =
4211 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id);
4213 /* If there's a tree_map, prepare for substitution. */
4214 if (tree_map)
4215 for (i = 0; i < VARRAY_ACTIVE_SIZE (tree_map); i++)
4217 replace_info
4218 = (struct ipa_replace_map *) VARRAY_GENERIC_PTR (tree_map, i);
4219 if (replace_info->replace_p)
4220 insert_decl_map (&id, replace_info->old_tree,
4221 replace_info->new_tree);
4224 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
4226 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4227 number_blocks (id.dst_fn);
4229 if (DECL_STRUCT_FUNCTION (old_decl)->local_decls != NULL_TREE)
4230 /* Add local vars. */
4231 for (t_step = DECL_STRUCT_FUNCTION (old_decl)->local_decls;
4232 t_step; t_step = TREE_CHAIN (t_step))
4234 tree var = TREE_VALUE (t_step);
4235 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
4236 cfun->local_decls = tree_cons (NULL_TREE, var, cfun->local_decls);
4237 else
4238 cfun->local_decls =
4239 tree_cons (NULL_TREE, remap_decl (var, &id),
4240 cfun->local_decls);
4243 /* Copy the Function's body. */
4244 copy_body (&id, old_entry_block->count, old_entry_block->frequency, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR);
4246 if (DECL_RESULT (old_decl) != NULL_TREE)
4248 tree *res_decl = &DECL_RESULT (old_decl);
4249 DECL_RESULT (new_decl) = remap_decl (*res_decl, &id);
4250 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
4253 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4254 number_blocks (new_decl);
4256 /* Clean up. */
4257 pointer_map_destroy (id.decl_map);
4258 if (!update_clones)
4260 fold_marked_statements (0, id.statements_to_fold);
4261 pointer_set_destroy (id.statements_to_fold);
4262 fold_cond_expr_cond ();
4264 if (gimple_in_ssa_p (cfun))
4266 free_dominance_info (CDI_DOMINATORS);
4267 free_dominance_info (CDI_POST_DOMINATORS);
4268 if (!update_clones)
4269 delete_unreachable_blocks ();
4270 update_ssa (TODO_update_ssa);
4271 if (!update_clones)
4273 fold_cond_expr_cond ();
4274 if (need_ssa_update_p ())
4275 update_ssa (TODO_update_ssa);
4278 free_dominance_info (CDI_DOMINATORS);
4279 free_dominance_info (CDI_POST_DOMINATORS);
4280 pop_cfun ();
4281 current_function_decl = old_current_function_decl;
4282 gcc_assert (!current_function_decl
4283 || DECL_STRUCT_FUNCTION (current_function_decl) == cfun);
4284 return;
4287 /* Duplicate a type, fields and all. */
4289 tree
4290 build_duplicate_type (tree type)
4292 struct copy_body_data id;
4294 memset (&id, 0, sizeof (id));
4295 id.src_fn = current_function_decl;
4296 id.dst_fn = current_function_decl;
4297 id.src_cfun = cfun;
4298 id.decl_map = pointer_map_create ();
4299 id.copy_decl = copy_decl_no_change;
4301 type = remap_type_1 (type, &id);
4303 pointer_map_destroy (id.decl_map);
4305 TYPE_CANONICAL (type) = type;
4307 return type;
4310 /* Return whether it is safe to inline a function because it used different
4311 target specific options or different optimization options. */
4312 bool
4313 tree_can_inline_p (tree caller, tree callee)
4315 /* Don't inline a function with a higher optimization level than the
4316 caller, or with different space constraints (hot/cold functions). */
4317 tree caller_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (caller);
4318 tree callee_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee);
4320 if (caller_tree != callee_tree)
4322 struct cl_optimization *caller_opt
4323 = TREE_OPTIMIZATION ((caller_tree)
4324 ? caller_tree
4325 : optimization_default_node);
4327 struct cl_optimization *callee_opt
4328 = TREE_OPTIMIZATION ((callee_tree)
4329 ? callee_tree
4330 : optimization_default_node);
4332 if ((caller_opt->optimize > callee_opt->optimize)
4333 || (caller_opt->optimize_size != callee_opt->optimize_size))
4334 return false;
4337 /* Allow the backend to decide if inlining is ok. */
4338 return targetm.target_option.can_inline_p (caller, callee);