c-common.c (c_expand_decl): Remove.
[official-gcc.git] / gcc / tree-inline.c
blobd083a5467b879c3f521acd94444f873305099dce
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_tree;
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 unshare_expr (*n);
166 /* Do not set DEF_STMT yet as statement is not copied yet. We do that
167 in copy_bb. */
168 new_tree = 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_tree) == VAR_DECL || TREE_CODE (new_tree) == PARM_DECL)
177 && (TREE_CODE (SSA_NAME_VAR (name)) != RESULT_DECL
178 || !id->transform_return_to_modify))
180 new_tree = make_ssa_name (new_tree, NULL);
181 insert_decl_map (id, name, new_tree);
182 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_tree)
183 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
184 TREE_TYPE (new_tree) = TREE_TYPE (SSA_NAME_VAR (new_tree));
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_tree,
206 fold_convert (TREE_TYPE (new_tree),
207 integer_zero_node));
208 gsi_insert_after (&gsi, init_stmt, GSI_NEW_STMT);
209 SSA_NAME_IS_DEFAULT_DEF (new_tree) = 0;
211 else
213 SSA_NAME_DEF_STMT (new_tree) = 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_tree), new_tree);
220 else
221 insert_decl_map (id, name, new_tree);
222 return new_tree;
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_tree, 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_tree = 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_tree);
309 return new_tree;
311 else if (TREE_CODE (type) == REFERENCE_TYPE)
313 new_tree = 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_tree);
317 return new_tree;
319 else
320 new_tree = copy_node (type);
322 insert_decl_map (id, type, new_tree);
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_tree) = t;
331 TYPE_NEXT_VARIANT (new_tree) = TYPE_NEXT_VARIANT (t);
332 TYPE_NEXT_VARIANT (t) = new_tree;
334 else
336 TYPE_MAIN_VARIANT (new_tree) = new_tree;
337 TYPE_NEXT_VARIANT (new_tree) = NULL;
340 if (TYPE_STUB_DECL (type))
341 TYPE_STUB_DECL (new_tree) = remap_decl (TYPE_STUB_DECL (type), id);
343 /* Lazily create pointer and reference types. */
344 TYPE_POINTER_TO (new_tree) = NULL;
345 TYPE_REFERENCE_TO (new_tree) = NULL;
347 switch (TREE_CODE (new_tree))
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_tree);
355 if (t && TREE_CODE (t) != INTEGER_CST)
356 walk_tree (&TYPE_MIN_VALUE (new_tree), copy_tree_body_r, id, NULL);
358 t = TYPE_MAX_VALUE (new_tree);
359 if (t && TREE_CODE (t) != INTEGER_CST)
360 walk_tree (&TYPE_MAX_VALUE (new_tree), copy_tree_body_r, id, NULL);
361 return new_tree;
363 case FUNCTION_TYPE:
364 TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
365 walk_tree (&TYPE_ARG_TYPES (new_tree), copy_tree_body_r, id, NULL);
366 return new_tree;
368 case ARRAY_TYPE:
369 TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
370 TYPE_DOMAIN (new_tree) = remap_type (TYPE_DOMAIN (new_tree), 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_tree); f ; f = TREE_CHAIN (f))
381 t = remap_decl (f, id);
382 DECL_CONTEXT (t) = new_tree;
383 TREE_CHAIN (t) = nf;
384 nf = t;
386 TYPE_FIELDS (new_tree) = 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_tree), copy_tree_body_r, id, NULL);
397 walk_tree (&TYPE_SIZE_UNIT (new_tree), copy_tree_body_r, id, NULL);
399 return new_tree;
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_tree = block;
509 if (!block)
510 return NULL;
512 remap_block (&new_tree, id);
513 gcc_assert (new_tree != block);
514 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
515 add_lexical_block (new_tree, remap_blocks (t, id));
516 return new_tree;
519 static void
520 copy_statement_list (tree *tp)
522 tree_stmt_iterator oi, ni;
523 tree new_tree;
525 new_tree = alloc_stmt_list ();
526 ni = tsi_start (new_tree);
527 oi = tsi_start (*tp);
528 *tp = new_tree;
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_tree, 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_tree = unshare_expr (*n);
693 old = *tp;
694 *tp = gimple_fold_indirect_ref (new_tree);
695 if (!*tp)
697 if (TREE_CODE (new_tree) == ADDR_EXPR)
699 *tp = fold_indirect_ref_1 (type, new_tree);
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_tree, 0);
706 else
708 *tp = build1 (INDIRECT_REF, type, new_tree);
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_tree;
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_tree = unshare_expr (*n);
921 old = *tp;
922 *tp = gimple_fold_indirect_ref (new_tree);
923 if (! *tp)
925 if (TREE_CODE (new_tree) == ADDR_EXPR)
927 *tp = fold_indirect_ref_1 (type, new_tree);
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_tree, 0);
934 else
936 *tp = build1 (INDIRECT_REF, type, new_tree);
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 case GIMPLE_OMP_CRITICAL:
1160 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1161 copy
1162 = gimple_build_omp_critical (s1, gimple_omp_critical_name (stmt));
1163 break;
1165 default:
1166 gcc_unreachable ();
1169 else
1171 if (gimple_assign_copy_p (stmt)
1172 && gimple_assign_lhs (stmt) == gimple_assign_rhs1 (stmt)
1173 && auto_var_in_fn_p (gimple_assign_lhs (stmt), id->src_fn))
1175 /* Here we handle statements that are not completely rewritten.
1176 First we detect some inlining-induced bogosities for
1177 discarding. */
1179 /* Some assignments VAR = VAR; don't generate any rtl code
1180 and thus don't count as variable modification. Avoid
1181 keeping bogosities like 0 = 0. */
1182 tree decl = gimple_assign_lhs (stmt), value;
1183 tree *n;
1185 n = (tree *) pointer_map_contains (id->decl_map, decl);
1186 if (n)
1188 value = *n;
1189 STRIP_TYPE_NOPS (value);
1190 if (TREE_CONSTANT (value) || TREE_READONLY (value))
1191 return gimple_build_nop ();
1195 /* Create a new deep copy of the statement. */
1196 copy = gimple_copy (stmt);
1199 /* If STMT has a block defined, map it to the newly constructed
1200 block. When inlining we want statements without a block to
1201 appear in the block of the function call. */
1202 new_block = id->block;
1203 if (gimple_block (copy))
1205 tree *n;
1206 n = (tree *) pointer_map_contains (id->decl_map, gimple_block (copy));
1207 gcc_assert (n);
1208 new_block = *n;
1211 gimple_set_block (copy, new_block);
1213 /* Remap all the operands in COPY. */
1214 memset (&wi, 0, sizeof (wi));
1215 wi.info = id;
1216 walk_gimple_op (copy, remap_gimple_op_r, &wi);
1218 /* We have to handle EH region remapping of GIMPLE_RESX specially because
1219 the region number is not an operand. */
1220 if (gimple_code (stmt) == GIMPLE_RESX && id->eh_region_offset)
1222 gimple_resx_set_region (copy, gimple_resx_region (stmt) + id->eh_region_offset);
1224 return copy;
1228 /* Copy basic block, scale profile accordingly. Edges will be taken care of
1229 later */
1231 static basic_block
1232 copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
1233 gcov_type count_scale)
1235 gimple_stmt_iterator gsi, copy_gsi;
1236 basic_block copy_basic_block;
1237 tree decl;
1239 /* create_basic_block() will append every new block to
1240 basic_block_info automatically. */
1241 copy_basic_block = create_basic_block (NULL, (void *) 0,
1242 (basic_block) bb->prev_bb->aux);
1243 copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
1245 /* We are going to rebuild frequencies from scratch. These values
1246 have just small importance to drive canonicalize_loop_headers. */
1247 copy_basic_block->frequency = ((gcov_type)bb->frequency
1248 * frequency_scale / REG_BR_PROB_BASE);
1250 if (copy_basic_block->frequency > BB_FREQ_MAX)
1251 copy_basic_block->frequency = BB_FREQ_MAX;
1253 copy_gsi = gsi_start_bb (copy_basic_block);
1255 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1257 gimple stmt = gsi_stmt (gsi);
1258 gimple orig_stmt = stmt;
1260 id->regimplify = false;
1261 stmt = remap_gimple_stmt (stmt, id);
1262 if (gimple_nop_p (stmt))
1263 continue;
1265 gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun, orig_stmt);
1267 /* With return slot optimization we can end up with
1268 non-gimple (foo *)&this->m, fix that here. */
1269 if (is_gimple_assign (stmt)
1270 && gimple_assign_rhs_code (stmt) == NOP_EXPR
1271 && !is_gimple_val (gimple_assign_rhs1 (stmt)))
1273 tree new_rhs;
1274 new_rhs = force_gimple_operand_gsi (&copy_gsi,
1275 gimple_assign_rhs1 (stmt),
1276 true, NULL, true, GSI_SAME_STMT);
1277 gimple_assign_set_rhs1 (stmt, new_rhs);
1279 else if (id->regimplify)
1280 gimple_regimplify_operands (stmt, &copy_gsi);
1282 gsi_insert_after (&copy_gsi, stmt, GSI_NEW_STMT);
1284 /* Process the new statement. The call to gimple_regimplify_operands
1285 possibly turned the statement into multiple statements, we
1286 need to process all of them. */
1287 while (!gsi_end_p (copy_gsi))
1289 if (is_gimple_call (stmt)
1290 && gimple_call_va_arg_pack_p (stmt)
1291 && id->gimple_call)
1293 /* __builtin_va_arg_pack () should be replaced by
1294 all arguments corresponding to ... in the caller. */
1295 tree p;
1296 gimple new_call;
1297 VEC(tree, heap) *argarray;
1298 size_t nargs = gimple_call_num_args (id->gimple_call);
1299 size_t n;
1301 for (p = DECL_ARGUMENTS (id->src_fn); p; p = TREE_CHAIN (p))
1302 nargs--;
1304 /* Create the new array of arguments. */
1305 n = nargs + gimple_call_num_args (stmt);
1306 argarray = VEC_alloc (tree, heap, n);
1307 VEC_safe_grow (tree, heap, argarray, n);
1309 /* Copy all the arguments before '...' */
1310 memcpy (VEC_address (tree, argarray),
1311 gimple_call_arg_ptr (stmt, 0),
1312 gimple_call_num_args (stmt) * sizeof (tree));
1314 /* Append the arguments passed in '...' */
1315 memcpy (VEC_address(tree, argarray) + gimple_call_num_args (stmt),
1316 gimple_call_arg_ptr (id->gimple_call, 0)
1317 + (gimple_call_num_args (id->gimple_call) - nargs),
1318 nargs * sizeof (tree));
1320 new_call = gimple_build_call_vec (gimple_call_fn (stmt),
1321 argarray);
1323 VEC_free (tree, heap, argarray);
1325 /* Copy all GIMPLE_CALL flags, location and block, except
1326 GF_CALL_VA_ARG_PACK. */
1327 gimple_call_copy_flags (new_call, stmt);
1328 gimple_call_set_va_arg_pack (new_call, false);
1329 gimple_set_location (new_call, gimple_location (stmt));
1330 gimple_set_block (new_call, gimple_block (stmt));
1331 gimple_call_set_lhs (new_call, gimple_call_lhs (stmt));
1333 gsi_replace (&copy_gsi, new_call, false);
1334 gimple_set_bb (stmt, NULL);
1335 stmt = new_call;
1337 else if (is_gimple_call (stmt)
1338 && id->gimple_call
1339 && (decl = gimple_call_fndecl (stmt))
1340 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1341 && DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_ARG_PACK_LEN)
1343 /* __builtin_va_arg_pack_len () should be replaced by
1344 the number of anonymous arguments. */
1345 size_t nargs = gimple_call_num_args (id->gimple_call);
1346 tree count, p;
1347 gimple new_stmt;
1349 for (p = DECL_ARGUMENTS (id->src_fn); p; p = TREE_CHAIN (p))
1350 nargs--;
1352 count = build_int_cst (integer_type_node, nargs);
1353 new_stmt = gimple_build_assign (gimple_call_lhs (stmt), count);
1354 gsi_replace (&copy_gsi, new_stmt, false);
1355 stmt = new_stmt;
1358 /* Statements produced by inlining can be unfolded, especially
1359 when we constant propagated some operands. We can't fold
1360 them right now for two reasons:
1361 1) folding require SSA_NAME_DEF_STMTs to be correct
1362 2) we can't change function calls to builtins.
1363 So we just mark statement for later folding. We mark
1364 all new statements, instead just statements that has changed
1365 by some nontrivial substitution so even statements made
1366 foldable indirectly are updated. If this turns out to be
1367 expensive, copy_body can be told to watch for nontrivial
1368 changes. */
1369 if (id->statements_to_fold)
1370 pointer_set_insert (id->statements_to_fold, stmt);
1372 /* We're duplicating a CALL_EXPR. Find any corresponding
1373 callgraph edges and update or duplicate them. */
1374 if (is_gimple_call (stmt))
1376 struct cgraph_node *node;
1377 struct cgraph_edge *edge;
1379 switch (id->transform_call_graph_edges)
1381 case CB_CGE_DUPLICATE:
1382 edge = cgraph_edge (id->src_node, orig_stmt);
1383 if (edge)
1384 cgraph_clone_edge (edge, id->dst_node, stmt,
1385 REG_BR_PROB_BASE, 1,
1386 edge->frequency, true);
1387 break;
1389 case CB_CGE_MOVE_CLONES:
1390 for (node = id->dst_node->next_clone;
1391 node;
1392 node = node->next_clone)
1394 edge = cgraph_edge (node, orig_stmt);
1395 if (edge)
1396 cgraph_set_call_stmt (edge, stmt);
1398 /* FALLTHRU */
1400 case CB_CGE_MOVE:
1401 edge = cgraph_edge (id->dst_node, orig_stmt);
1402 if (edge)
1403 cgraph_set_call_stmt (edge, stmt);
1404 break;
1406 default:
1407 gcc_unreachable ();
1411 /* If you think we can abort here, you are wrong.
1412 There is no region 0 in gimple. */
1413 gcc_assert (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt) != 0);
1415 if (stmt_could_throw_p (stmt)
1416 /* When we are cloning for inlining, we are supposed to
1417 construct a clone that calls precisely the same functions
1418 as original. However IPA optimizers might've proved
1419 earlier some function calls as non-trapping that might
1420 render some basic blocks dead that might become
1421 unreachable.
1423 We can't update SSA with unreachable blocks in CFG and thus
1424 we prevent the scenario by preserving even the "dead" eh
1425 edges until the point they are later removed by
1426 fixup_cfg pass. */
1427 || (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES
1428 && lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt) > 0))
1430 int region = lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt);
1432 /* Add an entry for the copied tree in the EH hashtable.
1433 When cloning or versioning, use the hashtable in
1434 cfun, and just copy the EH number. When inlining, use the
1435 hashtable in the caller, and adjust the region number. */
1436 if (region > 0)
1437 add_stmt_to_eh_region (stmt, region + id->eh_region_offset);
1439 /* If this tree doesn't have a region associated with it,
1440 and there is a "current region,"
1441 then associate this tree with the current region
1442 and add edges associated with this region. */
1443 if (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt) <= 0
1444 && id->eh_region > 0
1445 && stmt_could_throw_p (stmt))
1446 add_stmt_to_eh_region (stmt, id->eh_region);
1449 if (gimple_in_ssa_p (cfun))
1451 ssa_op_iter i;
1452 tree def;
1454 find_new_referenced_vars (gsi_stmt (copy_gsi));
1455 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
1456 if (TREE_CODE (def) == SSA_NAME)
1457 SSA_NAME_DEF_STMT (def) = stmt;
1460 gsi_next (&copy_gsi);
1463 copy_gsi = gsi_last_bb (copy_basic_block);
1466 return copy_basic_block;
1469 /* Inserting Single Entry Multiple Exit region in SSA form into code in SSA
1470 form is quite easy, since dominator relationship for old basic blocks does
1471 not change.
1473 There is however exception where inlining might change dominator relation
1474 across EH edges from basic block within inlined functions destinating
1475 to landing pads in function we inline into.
1477 The function fills in PHI_RESULTs of such PHI nodes if they refer
1478 to gimple regs. Otherwise, the function mark PHI_RESULT of such
1479 PHI nodes for renaming. For non-gimple regs, renaming is safe: the
1480 EH edges are abnormal and SSA_NAME_OCCURS_IN_ABNORMAL_PHI must be
1481 set, and this means that there will be no overlapping live ranges
1482 for the underlying symbol.
1484 This might change in future if we allow redirecting of EH edges and
1485 we might want to change way build CFG pre-inlining to include
1486 all the possible edges then. */
1487 static void
1488 update_ssa_across_abnormal_edges (basic_block bb, basic_block ret_bb,
1489 bool can_throw, bool nonlocal_goto)
1491 edge e;
1492 edge_iterator ei;
1494 FOR_EACH_EDGE (e, ei, bb->succs)
1495 if (!e->dest->aux
1496 || ((basic_block)e->dest->aux)->index == ENTRY_BLOCK)
1498 gimple phi;
1499 gimple_stmt_iterator si;
1501 gcc_assert (e->flags & EDGE_ABNORMAL);
1503 if (!nonlocal_goto)
1504 gcc_assert (e->flags & EDGE_EH);
1506 if (!can_throw)
1507 gcc_assert (!(e->flags & EDGE_EH));
1509 for (si = gsi_start_phis (e->dest); !gsi_end_p (si); gsi_next (&si))
1511 edge re;
1513 phi = gsi_stmt (si);
1515 /* There shouldn't be any PHI nodes in the ENTRY_BLOCK. */
1516 gcc_assert (!e->dest->aux);
1518 gcc_assert (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)));
1520 if (!is_gimple_reg (PHI_RESULT (phi)))
1522 mark_sym_for_renaming (SSA_NAME_VAR (PHI_RESULT (phi)));
1523 continue;
1526 re = find_edge (ret_bb, e->dest);
1527 gcc_assert (re);
1528 gcc_assert ((re->flags & (EDGE_EH | EDGE_ABNORMAL))
1529 == (e->flags & (EDGE_EH | EDGE_ABNORMAL)));
1531 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1532 USE_FROM_PTR (PHI_ARG_DEF_PTR_FROM_EDGE (phi, re)));
1538 /* Copy edges from BB into its copy constructed earlier, scale profile
1539 accordingly. Edges will be taken care of later. Assume aux
1540 pointers to point to the copies of each BB. */
1542 static void
1543 copy_edges_for_bb (basic_block bb, gcov_type count_scale, basic_block ret_bb)
1545 basic_block new_bb = (basic_block) bb->aux;
1546 edge_iterator ei;
1547 edge old_edge;
1548 gimple_stmt_iterator si;
1549 int flags;
1551 /* Use the indices from the original blocks to create edges for the
1552 new ones. */
1553 FOR_EACH_EDGE (old_edge, ei, bb->succs)
1554 if (!(old_edge->flags & EDGE_EH))
1556 edge new_edge;
1558 flags = old_edge->flags;
1560 /* Return edges do get a FALLTHRU flag when the get inlined. */
1561 if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
1562 && old_edge->dest->aux != EXIT_BLOCK_PTR)
1563 flags |= EDGE_FALLTHRU;
1564 new_edge = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
1565 new_edge->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
1566 new_edge->probability = old_edge->probability;
1569 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
1570 return;
1572 for (si = gsi_start_bb (new_bb); !gsi_end_p (si);)
1574 gimple copy_stmt;
1575 bool can_throw, nonlocal_goto;
1577 copy_stmt = gsi_stmt (si);
1578 update_stmt (copy_stmt);
1579 if (gimple_in_ssa_p (cfun))
1580 mark_symbols_for_renaming (copy_stmt);
1582 /* Do this before the possible split_block. */
1583 gsi_next (&si);
1585 /* If this tree could throw an exception, there are two
1586 cases where we need to add abnormal edge(s): the
1587 tree wasn't in a region and there is a "current
1588 region" in the caller; or the original tree had
1589 EH edges. In both cases split the block after the tree,
1590 and add abnormal edge(s) as needed; we need both
1591 those from the callee and the caller.
1592 We check whether the copy can throw, because the const
1593 propagation can change an INDIRECT_REF which throws
1594 into a COMPONENT_REF which doesn't. If the copy
1595 can throw, the original could also throw. */
1596 can_throw = stmt_can_throw_internal (copy_stmt);
1597 nonlocal_goto = stmt_can_make_abnormal_goto (copy_stmt);
1599 if (can_throw || nonlocal_goto)
1601 if (!gsi_end_p (si))
1602 /* Note that bb's predecessor edges aren't necessarily
1603 right at this point; split_block doesn't care. */
1605 edge e = split_block (new_bb, copy_stmt);
1607 new_bb = e->dest;
1608 new_bb->aux = e->src->aux;
1609 si = gsi_start_bb (new_bb);
1613 if (can_throw)
1614 make_eh_edges (copy_stmt);
1616 if (nonlocal_goto)
1617 make_abnormal_goto_edges (gimple_bb (copy_stmt), true);
1619 if ((can_throw || nonlocal_goto)
1620 && gimple_in_ssa_p (cfun))
1621 update_ssa_across_abnormal_edges (gimple_bb (copy_stmt), ret_bb,
1622 can_throw, nonlocal_goto);
1626 /* Copy the PHIs. All blocks and edges are copied, some blocks
1627 was possibly split and new outgoing EH edges inserted.
1628 BB points to the block of original function and AUX pointers links
1629 the original and newly copied blocks. */
1631 static void
1632 copy_phis_for_bb (basic_block bb, copy_body_data *id)
1634 basic_block const new_bb = (basic_block) bb->aux;
1635 edge_iterator ei;
1636 gimple phi;
1637 gimple_stmt_iterator si;
1639 for (si = gsi_start (phi_nodes (bb)); !gsi_end_p (si); gsi_next (&si))
1641 tree res, new_res;
1642 gimple new_phi;
1643 edge new_edge;
1645 phi = gsi_stmt (si);
1646 res = PHI_RESULT (phi);
1647 new_res = res;
1648 if (is_gimple_reg (res))
1650 walk_tree (&new_res, copy_tree_body_r, id, NULL);
1651 SSA_NAME_DEF_STMT (new_res)
1652 = new_phi = create_phi_node (new_res, new_bb);
1653 FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
1655 edge const old_edge
1656 = find_edge ((basic_block) new_edge->src->aux, bb);
1657 tree arg = PHI_ARG_DEF_FROM_EDGE (phi, old_edge);
1658 tree new_arg = arg;
1659 tree block = id->block;
1660 id->block = NULL_TREE;
1661 walk_tree (&new_arg, copy_tree_body_r, id, NULL);
1662 id->block = block;
1663 gcc_assert (new_arg);
1664 /* With return slot optimization we can end up with
1665 non-gimple (foo *)&this->m, fix that here. */
1666 if (TREE_CODE (new_arg) != SSA_NAME
1667 && TREE_CODE (new_arg) != FUNCTION_DECL
1668 && !is_gimple_val (new_arg))
1670 gimple_seq stmts = NULL;
1671 new_arg = force_gimple_operand (new_arg, &stmts, true, NULL);
1672 gsi_insert_seq_on_edge_immediate (new_edge, stmts);
1674 add_phi_arg (new_phi, new_arg, new_edge);
1681 /* Wrapper for remap_decl so it can be used as a callback. */
1683 static tree
1684 remap_decl_1 (tree decl, void *data)
1686 return remap_decl (decl, (copy_body_data *) data);
1689 /* Build struct function and associated datastructures for the new clone
1690 NEW_FNDECL to be build. CALLEE_FNDECL is the original */
1692 static void
1693 initialize_cfun (tree new_fndecl, tree callee_fndecl, gcov_type count,
1694 int frequency)
1696 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1697 gcov_type count_scale, frequency_scale;
1699 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1700 count_scale = (REG_BR_PROB_BASE * count
1701 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1702 else
1703 count_scale = 1;
1705 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
1706 frequency_scale = (REG_BR_PROB_BASE * frequency
1708 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
1709 else
1710 frequency_scale = count_scale;
1712 /* Register specific tree functions. */
1713 gimple_register_cfg_hooks ();
1715 /* Get clean struct function. */
1716 push_struct_function (new_fndecl);
1718 /* We will rebuild these, so just sanity check that they are empty. */
1719 gcc_assert (VALUE_HISTOGRAMS (cfun) == NULL);
1720 gcc_assert (cfun->local_decls == NULL);
1721 gcc_assert (cfun->cfg == NULL);
1722 gcc_assert (cfun->decl == new_fndecl);
1724 /* No need to copy; this is initialized later in compilation. */
1725 gcc_assert (!src_cfun->calls_setjmp);
1726 gcc_assert (!src_cfun->calls_alloca);
1728 /* Copy items we preserve during clonning. */
1729 cfun->static_chain_decl = src_cfun->static_chain_decl;
1730 cfun->nonlocal_goto_save_area = src_cfun->nonlocal_goto_save_area;
1731 cfun->function_end_locus = src_cfun->function_end_locus;
1732 cfun->curr_properties = src_cfun->curr_properties;
1733 cfun->last_verified = src_cfun->last_verified;
1734 if (src_cfun->ipa_transforms_to_apply)
1735 cfun->ipa_transforms_to_apply = VEC_copy (ipa_opt_pass, heap,
1736 src_cfun->ipa_transforms_to_apply);
1737 cfun->va_list_gpr_size = src_cfun->va_list_gpr_size;
1738 cfun->va_list_fpr_size = src_cfun->va_list_fpr_size;
1739 cfun->function_frequency = src_cfun->function_frequency;
1740 cfun->has_nonlocal_label = src_cfun->has_nonlocal_label;
1741 cfun->stdarg = src_cfun->stdarg;
1742 cfun->dont_save_pending_sizes_p = src_cfun->dont_save_pending_sizes_p;
1743 cfun->after_inlining = src_cfun->after_inlining;
1744 cfun->returns_struct = src_cfun->returns_struct;
1745 cfun->returns_pcc_struct = src_cfun->returns_pcc_struct;
1746 cfun->after_tree_profile = src_cfun->after_tree_profile;
1748 init_empty_tree_cfg ();
1750 ENTRY_BLOCK_PTR->count =
1751 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
1752 REG_BR_PROB_BASE);
1753 ENTRY_BLOCK_PTR->frequency =
1754 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
1755 frequency_scale / REG_BR_PROB_BASE);
1756 EXIT_BLOCK_PTR->count =
1757 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
1758 REG_BR_PROB_BASE);
1759 EXIT_BLOCK_PTR->frequency =
1760 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
1761 frequency_scale / REG_BR_PROB_BASE);
1762 if (src_cfun->eh)
1763 init_eh_for_function ();
1765 if (src_cfun->gimple_df)
1767 init_tree_ssa (cfun);
1768 cfun->gimple_df->in_ssa_p = true;
1769 init_ssa_operands ();
1771 pop_cfun ();
1774 /* Make a copy of the body of FN so that it can be inserted inline in
1775 another function. Walks FN via CFG, returns new fndecl. */
1777 static tree
1778 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency,
1779 basic_block entry_block_map, basic_block exit_block_map)
1781 tree callee_fndecl = id->src_fn;
1782 /* Original cfun for the callee, doesn't change. */
1783 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1784 struct function *cfun_to_copy;
1785 basic_block bb;
1786 tree new_fndecl = NULL;
1787 gcov_type count_scale, frequency_scale;
1788 int last;
1790 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1791 count_scale = (REG_BR_PROB_BASE * count
1792 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1793 else
1794 count_scale = 1;
1796 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
1797 frequency_scale = (REG_BR_PROB_BASE * frequency
1799 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
1800 else
1801 frequency_scale = count_scale;
1803 /* Register specific tree functions. */
1804 gimple_register_cfg_hooks ();
1806 /* Must have a CFG here at this point. */
1807 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
1808 (DECL_STRUCT_FUNCTION (callee_fndecl)));
1810 cfun_to_copy = id->src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1812 ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
1813 EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
1814 entry_block_map->aux = ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
1815 exit_block_map->aux = EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
1817 /* Duplicate any exception-handling regions. */
1818 if (cfun->eh)
1820 id->eh_region_offset
1821 = duplicate_eh_regions (cfun_to_copy, remap_decl_1, id,
1822 0, id->eh_region);
1825 /* Use aux pointers to map the original blocks to copy. */
1826 FOR_EACH_BB_FN (bb, cfun_to_copy)
1828 basic_block new_bb = copy_bb (id, bb, frequency_scale, count_scale);
1829 bb->aux = new_bb;
1830 new_bb->aux = bb;
1833 last = last_basic_block;
1835 /* Now that we've duplicated the blocks, duplicate their edges. */
1836 FOR_ALL_BB_FN (bb, cfun_to_copy)
1837 copy_edges_for_bb (bb, count_scale, exit_block_map);
1839 if (gimple_in_ssa_p (cfun))
1840 FOR_ALL_BB_FN (bb, cfun_to_copy)
1841 copy_phis_for_bb (bb, id);
1843 FOR_ALL_BB_FN (bb, cfun_to_copy)
1845 ((basic_block)bb->aux)->aux = NULL;
1846 bb->aux = NULL;
1849 /* Zero out AUX fields of newly created block during EH edge
1850 insertion. */
1851 for (; last < last_basic_block; last++)
1852 BASIC_BLOCK (last)->aux = NULL;
1853 entry_block_map->aux = NULL;
1854 exit_block_map->aux = NULL;
1856 return new_fndecl;
1859 static tree
1860 copy_body (copy_body_data *id, gcov_type count, int frequency,
1861 basic_block entry_block_map, basic_block exit_block_map)
1863 tree fndecl = id->src_fn;
1864 tree body;
1866 /* If this body has a CFG, walk CFG and copy. */
1867 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
1868 body = copy_cfg_body (id, count, frequency, entry_block_map, exit_block_map);
1870 return body;
1873 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
1874 defined in function FN, or of a data member thereof. */
1876 static bool
1877 self_inlining_addr_expr (tree value, tree fn)
1879 tree var;
1881 if (TREE_CODE (value) != ADDR_EXPR)
1882 return false;
1884 var = get_base_address (TREE_OPERAND (value, 0));
1886 return var && auto_var_in_fn_p (var, fn);
1889 static void
1890 insert_init_stmt (basic_block bb, gimple init_stmt)
1892 gimple_stmt_iterator si = gsi_last_bb (bb);
1893 gimple_stmt_iterator i;
1894 gimple_seq seq = gimple_seq_alloc ();
1895 struct gimplify_ctx gctx;
1897 push_gimplify_context (&gctx);
1899 i = gsi_start (seq);
1900 gimple_regimplify_operands (init_stmt, &i);
1902 if (init_stmt
1903 && !gimple_seq_empty_p (seq))
1905 /* The replacement can expose previously unreferenced
1906 variables. */
1907 if (gimple_in_ssa_p (cfun))
1908 for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
1909 find_new_referenced_vars (gsi_stmt (i));
1911 /* Insert the gimplified sequence needed for INIT_STMT
1912 after SI. INIT_STMT will be inserted after SEQ. */
1913 gsi_insert_seq_after (&si, seq, GSI_NEW_STMT);
1916 pop_gimplify_context (NULL);
1918 /* If VAR represents a zero-sized variable, it's possible that the
1919 assignment statement may result in no gimple statements. */
1920 if (init_stmt)
1921 gsi_insert_after (&si, init_stmt, GSI_NEW_STMT);
1923 if (gimple_in_ssa_p (cfun))
1924 for (;!gsi_end_p (si); gsi_next (&si))
1925 mark_symbols_for_renaming (gsi_stmt (si));
1928 /* Initialize parameter P with VALUE. If needed, produce init statement
1929 at the end of BB. When BB is NULL, we return init statement to be
1930 output later. */
1931 static gimple
1932 setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
1933 basic_block bb, tree *vars)
1935 gimple init_stmt = NULL;
1936 tree var;
1937 tree rhs = value;
1938 tree def = (gimple_in_ssa_p (cfun)
1939 ? gimple_default_def (id->src_cfun, p) : NULL);
1941 if (value
1942 && value != error_mark_node
1943 && !useless_type_conversion_p (TREE_TYPE (p), TREE_TYPE (value)))
1945 if (fold_convertible_p (TREE_TYPE (p), value))
1946 rhs = fold_build1 (NOP_EXPR, TREE_TYPE (p), value);
1947 else
1948 /* ??? For valid (GIMPLE) programs we should not end up here.
1949 Still if something has gone wrong and we end up with truly
1950 mismatched types here, fall back to using a VIEW_CONVERT_EXPR
1951 to not leak invalid GIMPLE to the following passes. */
1952 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (p), value);
1955 /* If the parameter is never assigned to, has no SSA_NAMEs created,
1956 we may not need to create a new variable here at all. Instead, we may
1957 be able to just use the argument value. */
1958 if (TREE_READONLY (p)
1959 && !TREE_ADDRESSABLE (p)
1960 && value && !TREE_SIDE_EFFECTS (value)
1961 && !def)
1963 /* We may produce non-gimple trees by adding NOPs or introduce
1964 invalid sharing when operand is not really constant.
1965 It is not big deal to prohibit constant propagation here as
1966 we will constant propagate in DOM1 pass anyway. */
1967 if (is_gimple_min_invariant (value)
1968 && useless_type_conversion_p (TREE_TYPE (p),
1969 TREE_TYPE (value))
1970 /* We have to be very careful about ADDR_EXPR. Make sure
1971 the base variable isn't a local variable of the inlined
1972 function, e.g., when doing recursive inlining, direct or
1973 mutually-recursive or whatever, which is why we don't
1974 just test whether fn == current_function_decl. */
1975 && ! self_inlining_addr_expr (value, fn))
1977 insert_decl_map (id, p, value);
1978 return NULL;
1982 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
1983 here since the type of this decl must be visible to the calling
1984 function. */
1985 var = copy_decl_to_var (p, id);
1986 if (gimple_in_ssa_p (cfun) && TREE_CODE (var) == VAR_DECL)
1988 get_var_ann (var);
1989 add_referenced_var (var);
1992 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
1993 that way, when the PARM_DECL is encountered, it will be
1994 automatically replaced by the VAR_DECL. */
1995 insert_decl_map (id, p, var);
1997 /* Declare this new variable. */
1998 TREE_CHAIN (var) = *vars;
1999 *vars = var;
2001 /* Make gimplifier happy about this variable. */
2002 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2004 /* Even if P was TREE_READONLY, the new VAR should not be.
2005 In the original code, we would have constructed a
2006 temporary, and then the function body would have never
2007 changed the value of P. However, now, we will be
2008 constructing VAR directly. The constructor body may
2009 change its value multiple times as it is being
2010 constructed. Therefore, it must not be TREE_READONLY;
2011 the back-end assumes that TREE_READONLY variable is
2012 assigned to only once. */
2013 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
2014 TREE_READONLY (var) = 0;
2016 /* If there is no setup required and we are in SSA, take the easy route
2017 replacing all SSA names representing the function parameter by the
2018 SSA name passed to function.
2020 We need to construct map for the variable anyway as it might be used
2021 in different SSA names when parameter is set in function.
2023 FIXME: This usually kills the last connection in between inlined
2024 function parameter and the actual value in debug info. Can we do
2025 better here? If we just inserted the statement, copy propagation
2026 would kill it anyway as it always did in older versions of GCC.
2028 We might want to introduce a notion that single SSA_NAME might
2029 represent multiple variables for purposes of debugging. */
2030 if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p)
2031 && (TREE_CODE (rhs) == SSA_NAME
2032 || is_gimple_min_invariant (rhs))
2033 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
2035 insert_decl_map (id, def, rhs);
2036 return NULL;
2039 /* If the value of argument is never used, don't care about initializing
2040 it. */
2041 if (gimple_in_ssa_p (cfun) && !def && is_gimple_reg (p))
2043 gcc_assert (!value || !TREE_SIDE_EFFECTS (value));
2044 return NULL;
2047 /* Initialize this VAR_DECL from the equivalent argument. Convert
2048 the argument to the proper type in case it was promoted. */
2049 if (value)
2051 if (rhs == error_mark_node)
2053 insert_decl_map (id, p, var);
2054 return NULL;
2057 STRIP_USELESS_TYPE_CONVERSION (rhs);
2059 /* We want to use MODIFY_EXPR, not INIT_EXPR here so that we
2060 keep our trees in gimple form. */
2061 if (def && gimple_in_ssa_p (cfun) && is_gimple_reg (p))
2063 def = remap_ssa_name (def, id);
2064 init_stmt = gimple_build_assign (def, rhs);
2065 SSA_NAME_IS_DEFAULT_DEF (def) = 0;
2066 set_default_def (var, NULL);
2068 else
2069 init_stmt = gimple_build_assign (var, rhs);
2071 if (bb && init_stmt)
2072 insert_init_stmt (bb, init_stmt);
2074 return init_stmt;
2077 /* Generate code to initialize the parameters of the function at the
2078 top of the stack in ID from the GIMPLE_CALL STMT. */
2080 static void
2081 initialize_inlined_parameters (copy_body_data *id, gimple stmt,
2082 tree fn, basic_block bb)
2084 tree parms;
2085 size_t i;
2086 tree p;
2087 tree vars = NULL_TREE;
2088 tree static_chain = gimple_call_chain (stmt);
2090 /* Figure out what the parameters are. */
2091 parms = DECL_ARGUMENTS (fn);
2093 /* Loop through the parameter declarations, replacing each with an
2094 equivalent VAR_DECL, appropriately initialized. */
2095 for (p = parms, i = 0; p; p = TREE_CHAIN (p), i++)
2097 tree val;
2098 val = i < gimple_call_num_args (stmt) ? gimple_call_arg (stmt, i) : NULL;
2099 setup_one_parameter (id, p, val, fn, bb, &vars);
2102 /* Initialize the static chain. */
2103 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
2104 gcc_assert (fn != current_function_decl);
2105 if (p)
2107 /* No static chain? Seems like a bug in tree-nested.c. */
2108 gcc_assert (static_chain);
2110 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
2113 declare_inline_vars (id->block, vars);
2117 /* Declare a return variable to replace the RESULT_DECL for the
2118 function we are calling. An appropriate DECL_STMT is returned.
2119 The USE_STMT is filled to contain a use of the declaration to
2120 indicate the return value of the function.
2122 RETURN_SLOT, if non-null is place where to store the result. It
2123 is set only for CALL_EXPR_RETURN_SLOT_OPT. MODIFY_DEST, if non-null,
2124 was the LHS of the MODIFY_EXPR to which this call is the RHS.
2126 The return value is a (possibly null) value that is the result of the
2127 function as seen by the callee. *USE_P is a (possibly null) value that
2128 holds the result as seen by the caller. */
2130 static tree
2131 declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest,
2132 tree *use_p)
2134 tree callee = id->src_fn;
2135 tree caller = id->dst_fn;
2136 tree result = DECL_RESULT (callee);
2137 tree callee_type = TREE_TYPE (result);
2138 tree caller_type = TREE_TYPE (TREE_TYPE (callee));
2139 tree var, use;
2141 /* We don't need to do anything for functions that don't return
2142 anything. */
2143 if (!result || VOID_TYPE_P (callee_type))
2145 *use_p = NULL_TREE;
2146 return NULL_TREE;
2149 /* If there was a return slot, then the return value is the
2150 dereferenced address of that object. */
2151 if (return_slot)
2153 /* The front end shouldn't have used both return_slot and
2154 a modify expression. */
2155 gcc_assert (!modify_dest);
2156 if (DECL_BY_REFERENCE (result))
2158 tree return_slot_addr = build_fold_addr_expr (return_slot);
2159 STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
2161 /* We are going to construct *&return_slot and we can't do that
2162 for variables believed to be not addressable.
2164 FIXME: This check possibly can match, because values returned
2165 via return slot optimization are not believed to have address
2166 taken by alias analysis. */
2167 gcc_assert (TREE_CODE (return_slot) != SSA_NAME);
2168 if (gimple_in_ssa_p (cfun))
2170 HOST_WIDE_INT bitsize;
2171 HOST_WIDE_INT bitpos;
2172 tree offset;
2173 enum machine_mode mode;
2174 int unsignedp;
2175 int volatilep;
2176 tree base;
2177 base = get_inner_reference (return_slot, &bitsize, &bitpos,
2178 &offset,
2179 &mode, &unsignedp, &volatilep,
2180 false);
2181 if (TREE_CODE (base) == INDIRECT_REF)
2182 base = TREE_OPERAND (base, 0);
2183 if (TREE_CODE (base) == SSA_NAME)
2184 base = SSA_NAME_VAR (base);
2185 mark_sym_for_renaming (base);
2187 var = return_slot_addr;
2189 else
2191 var = return_slot;
2192 gcc_assert (TREE_CODE (var) != SSA_NAME);
2193 TREE_ADDRESSABLE (var) |= TREE_ADDRESSABLE (result);
2195 if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
2196 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
2197 && !DECL_GIMPLE_REG_P (result)
2198 && DECL_P (var))
2199 DECL_GIMPLE_REG_P (var) = 0;
2200 use = NULL;
2201 goto done;
2204 /* All types requiring non-trivial constructors should have been handled. */
2205 gcc_assert (!TREE_ADDRESSABLE (callee_type));
2207 /* Attempt to avoid creating a new temporary variable. */
2208 if (modify_dest
2209 && TREE_CODE (modify_dest) != SSA_NAME)
2211 bool use_it = false;
2213 /* We can't use MODIFY_DEST if there's type promotion involved. */
2214 if (!useless_type_conversion_p (callee_type, caller_type))
2215 use_it = false;
2217 /* ??? If we're assigning to a variable sized type, then we must
2218 reuse the destination variable, because we've no good way to
2219 create variable sized temporaries at this point. */
2220 else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
2221 use_it = true;
2223 /* If the callee cannot possibly modify MODIFY_DEST, then we can
2224 reuse it as the result of the call directly. Don't do this if
2225 it would promote MODIFY_DEST to addressable. */
2226 else if (TREE_ADDRESSABLE (result))
2227 use_it = false;
2228 else
2230 tree base_m = get_base_address (modify_dest);
2232 /* If the base isn't a decl, then it's a pointer, and we don't
2233 know where that's going to go. */
2234 if (!DECL_P (base_m))
2235 use_it = false;
2236 else if (is_global_var (base_m))
2237 use_it = false;
2238 else if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
2239 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
2240 && !DECL_GIMPLE_REG_P (result)
2241 && DECL_GIMPLE_REG_P (base_m))
2242 use_it = false;
2243 else if (!TREE_ADDRESSABLE (base_m))
2244 use_it = true;
2247 if (use_it)
2249 var = modify_dest;
2250 use = NULL;
2251 goto done;
2255 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
2257 var = copy_result_decl_to_var (result, id);
2258 if (gimple_in_ssa_p (cfun))
2260 get_var_ann (var);
2261 add_referenced_var (var);
2264 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2265 DECL_STRUCT_FUNCTION (caller)->local_decls
2266 = tree_cons (NULL_TREE, var,
2267 DECL_STRUCT_FUNCTION (caller)->local_decls);
2269 /* Do not have the rest of GCC warn about this variable as it should
2270 not be visible to the user. */
2271 TREE_NO_WARNING (var) = 1;
2273 declare_inline_vars (id->block, var);
2275 /* Build the use expr. If the return type of the function was
2276 promoted, convert it back to the expected type. */
2277 use = var;
2278 if (!useless_type_conversion_p (caller_type, TREE_TYPE (var)))
2279 use = fold_convert (caller_type, var);
2281 STRIP_USELESS_TYPE_CONVERSION (use);
2283 if (DECL_BY_REFERENCE (result))
2284 var = build_fold_addr_expr (var);
2286 done:
2287 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
2288 way, when the RESULT_DECL is encountered, it will be
2289 automatically replaced by the VAR_DECL. */
2290 insert_decl_map (id, result, var);
2292 /* Remember this so we can ignore it in remap_decls. */
2293 id->retvar = var;
2295 *use_p = use;
2296 return var;
2299 /* Returns nonzero if a function can be inlined as a tree. */
2301 bool
2302 tree_inlinable_function_p (tree fn)
2304 bool ret = inlinable_function_p (fn);
2306 if (getenv ("TUPLES_INLINE"))
2307 fprintf (stderr, "Function %s is %sinlinable\n", get_name (fn),
2308 ret ? "" : "not ");
2310 return ret;
2313 static const char *inline_forbidden_reason;
2315 /* A callback for walk_gimple_seq to handle tree operands. Returns
2316 NULL_TREE if a function can be inlined, otherwise sets the reason
2317 why not and returns a tree representing the offending operand. */
2319 static tree
2320 inline_forbidden_p_op (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED,
2321 void *fnp ATTRIBUTE_UNUSED)
2323 tree node = *nodep;
2324 tree t;
2326 if (TREE_CODE (node) == RECORD_TYPE || TREE_CODE (node) == UNION_TYPE)
2328 /* We cannot inline a function of the form
2330 void F (int i) { struct S { int ar[i]; } s; }
2332 Attempting to do so produces a catch-22.
2333 If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
2334 UNION_TYPE nodes, then it goes into infinite recursion on a
2335 structure containing a pointer to its own type. If it doesn't,
2336 then the type node for S doesn't get adjusted properly when
2337 F is inlined.
2339 ??? This is likely no longer true, but it's too late in the 4.0
2340 cycle to try to find out. This should be checked for 4.1. */
2341 for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t))
2342 if (variably_modified_type_p (TREE_TYPE (t), NULL))
2344 inline_forbidden_reason
2345 = G_("function %q+F can never be inlined "
2346 "because it uses variable sized variables");
2347 return node;
2351 return NULL_TREE;
2355 /* A callback for walk_gimple_seq to handle statements. Returns
2356 non-NULL iff a function can not be inlined. Also sets the reason
2357 why. */
2359 static tree
2360 inline_forbidden_p_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2361 struct walk_stmt_info *wip)
2363 tree fn = (tree) wip->info;
2364 tree t;
2365 gimple stmt = gsi_stmt (*gsi);
2367 switch (gimple_code (stmt))
2369 case GIMPLE_CALL:
2370 /* Refuse to inline alloca call unless user explicitly forced so as
2371 this may change program's memory overhead drastically when the
2372 function using alloca is called in loop. In GCC present in
2373 SPEC2000 inlining into schedule_block cause it to require 2GB of
2374 RAM instead of 256MB. */
2375 if (gimple_alloca_call_p (stmt)
2376 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
2378 inline_forbidden_reason
2379 = G_("function %q+F can never be inlined because it uses "
2380 "alloca (override using the always_inline attribute)");
2381 *handled_ops_p = true;
2382 return fn;
2385 t = gimple_call_fndecl (stmt);
2386 if (t == NULL_TREE)
2387 break;
2389 /* We cannot inline functions that call setjmp. */
2390 if (setjmp_call_p (t))
2392 inline_forbidden_reason
2393 = G_("function %q+F can never be inlined because it uses setjmp");
2394 *handled_ops_p = true;
2395 return t;
2398 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
2399 switch (DECL_FUNCTION_CODE (t))
2401 /* We cannot inline functions that take a variable number of
2402 arguments. */
2403 case BUILT_IN_VA_START:
2404 case BUILT_IN_NEXT_ARG:
2405 case BUILT_IN_VA_END:
2406 inline_forbidden_reason
2407 = G_("function %q+F can never be inlined because it "
2408 "uses variable argument lists");
2409 *handled_ops_p = true;
2410 return t;
2412 case BUILT_IN_LONGJMP:
2413 /* We can't inline functions that call __builtin_longjmp at
2414 all. The non-local goto machinery really requires the
2415 destination be in a different function. If we allow the
2416 function calling __builtin_longjmp to be inlined into the
2417 function calling __builtin_setjmp, Things will Go Awry. */
2418 inline_forbidden_reason
2419 = G_("function %q+F can never be inlined because "
2420 "it uses setjmp-longjmp exception handling");
2421 *handled_ops_p = true;
2422 return t;
2424 case BUILT_IN_NONLOCAL_GOTO:
2425 /* Similarly. */
2426 inline_forbidden_reason
2427 = G_("function %q+F can never be inlined because "
2428 "it uses non-local goto");
2429 *handled_ops_p = true;
2430 return t;
2432 case BUILT_IN_RETURN:
2433 case BUILT_IN_APPLY_ARGS:
2434 /* If a __builtin_apply_args caller would be inlined,
2435 it would be saving arguments of the function it has
2436 been inlined into. Similarly __builtin_return would
2437 return from the function the inline has been inlined into. */
2438 inline_forbidden_reason
2439 = G_("function %q+F can never be inlined because "
2440 "it uses __builtin_return or __builtin_apply_args");
2441 *handled_ops_p = true;
2442 return t;
2444 default:
2445 break;
2447 break;
2449 case GIMPLE_GOTO:
2450 t = gimple_goto_dest (stmt);
2452 /* We will not inline a function which uses computed goto. The
2453 addresses of its local labels, which may be tucked into
2454 global storage, are of course not constant across
2455 instantiations, which causes unexpected behavior. */
2456 if (TREE_CODE (t) != LABEL_DECL)
2458 inline_forbidden_reason
2459 = G_("function %q+F can never be inlined "
2460 "because it contains a computed goto");
2461 *handled_ops_p = true;
2462 return t;
2464 break;
2466 case GIMPLE_LABEL:
2467 t = gimple_label_label (stmt);
2468 if (DECL_NONLOCAL (t))
2470 /* We cannot inline a function that receives a non-local goto
2471 because we cannot remap the destination label used in the
2472 function that is performing the non-local goto. */
2473 inline_forbidden_reason
2474 = G_("function %q+F can never be inlined "
2475 "because it receives a non-local goto");
2476 *handled_ops_p = true;
2477 return t;
2479 break;
2481 default:
2482 break;
2485 *handled_ops_p = false;
2486 return NULL_TREE;
2490 static tree
2491 inline_forbidden_p_2 (tree *nodep, int *walk_subtrees,
2492 void *fnp)
2494 tree node = *nodep;
2495 tree fn = (tree) fnp;
2497 if (TREE_CODE (node) == LABEL_DECL && DECL_CONTEXT (node) == fn)
2499 inline_forbidden_reason
2500 = G_("function %q+F can never be inlined "
2501 "because it saves address of local label in a static variable");
2502 return node;
2505 if (TYPE_P (node))
2506 *walk_subtrees = 0;
2508 return NULL_TREE;
2511 /* Return true if FNDECL is a function that cannot be inlined into
2512 another one. */
2514 static bool
2515 inline_forbidden_p (tree fndecl)
2517 location_t saved_loc = input_location;
2518 struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
2519 tree step;
2520 struct walk_stmt_info wi;
2521 struct pointer_set_t *visited_nodes;
2522 basic_block bb;
2523 bool forbidden_p = false;
2525 visited_nodes = pointer_set_create ();
2526 memset (&wi, 0, sizeof (wi));
2527 wi.info = (void *) fndecl;
2528 wi.pset = visited_nodes;
2530 FOR_EACH_BB_FN (bb, fun)
2532 gimple ret;
2533 gimple_seq seq = bb_seq (bb);
2534 ret = walk_gimple_seq (seq, inline_forbidden_p_stmt,
2535 inline_forbidden_p_op, &wi);
2536 forbidden_p = (ret != NULL);
2537 if (forbidden_p)
2538 goto egress;
2541 for (step = fun->local_decls; step; step = TREE_CHAIN (step))
2543 tree decl = TREE_VALUE (step);
2544 if (TREE_CODE (decl) == VAR_DECL
2545 && TREE_STATIC (decl)
2546 && !DECL_EXTERNAL (decl)
2547 && DECL_INITIAL (decl))
2549 tree ret;
2550 ret = walk_tree_without_duplicates (&DECL_INITIAL (decl),
2551 inline_forbidden_p_2, fndecl);
2552 forbidden_p = (ret != NULL);
2553 if (forbidden_p)
2554 goto egress;
2558 egress:
2559 pointer_set_destroy (visited_nodes);
2560 input_location = saved_loc;
2561 return forbidden_p;
2564 /* Returns nonzero if FN is a function that does not have any
2565 fundamental inline blocking properties. */
2567 static bool
2568 inlinable_function_p (tree fn)
2570 bool inlinable = true;
2571 bool do_warning;
2572 tree always_inline;
2574 /* If we've already decided this function shouldn't be inlined,
2575 there's no need to check again. */
2576 if (DECL_UNINLINABLE (fn))
2577 return false;
2579 /* We only warn for functions declared `inline' by the user. */
2580 do_warning = (warn_inline
2581 && DECL_DECLARED_INLINE_P (fn)
2582 && !DECL_IN_SYSTEM_HEADER (fn));
2584 always_inline = lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn));
2586 if (flag_no_inline
2587 && always_inline == NULL)
2589 if (do_warning)
2590 warning (OPT_Winline, "function %q+F can never be inlined because it "
2591 "is suppressed using -fno-inline", fn);
2592 inlinable = false;
2595 /* Don't auto-inline anything that might not be bound within
2596 this unit of translation. */
2597 else if (!DECL_DECLARED_INLINE_P (fn)
2598 && DECL_REPLACEABLE_P (fn))
2599 inlinable = false;
2601 else if (!function_attribute_inlinable_p (fn))
2603 if (do_warning)
2604 warning (OPT_Winline, "function %q+F can never be inlined because it "
2605 "uses attributes conflicting with inlining", fn);
2606 inlinable = false;
2609 else if (inline_forbidden_p (fn))
2611 /* See if we should warn about uninlinable functions. Previously,
2612 some of these warnings would be issued while trying to expand
2613 the function inline, but that would cause multiple warnings
2614 about functions that would for example call alloca. But since
2615 this a property of the function, just one warning is enough.
2616 As a bonus we can now give more details about the reason why a
2617 function is not inlinable. */
2618 if (always_inline)
2619 sorry (inline_forbidden_reason, fn);
2620 else if (do_warning)
2621 warning (OPT_Winline, inline_forbidden_reason, fn);
2623 inlinable = false;
2626 /* Squirrel away the result so that we don't have to check again. */
2627 DECL_UNINLINABLE (fn) = !inlinable;
2629 return inlinable;
2632 /* Estimate the cost of a memory move. Use machine dependent
2633 word size and take possible memcpy call into account. */
2636 estimate_move_cost (tree type)
2638 HOST_WIDE_INT size;
2640 size = int_size_in_bytes (type);
2642 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO (!optimize_size))
2643 /* Cost of a memcpy call, 3 arguments and the call. */
2644 return 4;
2645 else
2646 return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
2649 /* Returns cost of operation CODE, according to WEIGHTS */
2651 static int
2652 estimate_operator_cost (enum tree_code code, eni_weights *weights)
2654 switch (code)
2656 /* These are "free" conversions, or their presumed cost
2657 is folded into other operations. */
2658 case RANGE_EXPR:
2659 CASE_CONVERT:
2660 case COMPLEX_EXPR:
2661 case PAREN_EXPR:
2662 return 0;
2664 /* Assign cost of 1 to usual operations.
2665 ??? We may consider mapping RTL costs to this. */
2666 case COND_EXPR:
2667 case VEC_COND_EXPR:
2669 case PLUS_EXPR:
2670 case POINTER_PLUS_EXPR:
2671 case MINUS_EXPR:
2672 case MULT_EXPR:
2674 case FIXED_CONVERT_EXPR:
2675 case FIX_TRUNC_EXPR:
2677 case NEGATE_EXPR:
2678 case FLOAT_EXPR:
2679 case MIN_EXPR:
2680 case MAX_EXPR:
2681 case ABS_EXPR:
2683 case LSHIFT_EXPR:
2684 case RSHIFT_EXPR:
2685 case LROTATE_EXPR:
2686 case RROTATE_EXPR:
2687 case VEC_LSHIFT_EXPR:
2688 case VEC_RSHIFT_EXPR:
2690 case BIT_IOR_EXPR:
2691 case BIT_XOR_EXPR:
2692 case BIT_AND_EXPR:
2693 case BIT_NOT_EXPR:
2695 case TRUTH_ANDIF_EXPR:
2696 case TRUTH_ORIF_EXPR:
2697 case TRUTH_AND_EXPR:
2698 case TRUTH_OR_EXPR:
2699 case TRUTH_XOR_EXPR:
2700 case TRUTH_NOT_EXPR:
2702 case LT_EXPR:
2703 case LE_EXPR:
2704 case GT_EXPR:
2705 case GE_EXPR:
2706 case EQ_EXPR:
2707 case NE_EXPR:
2708 case ORDERED_EXPR:
2709 case UNORDERED_EXPR:
2711 case UNLT_EXPR:
2712 case UNLE_EXPR:
2713 case UNGT_EXPR:
2714 case UNGE_EXPR:
2715 case UNEQ_EXPR:
2716 case LTGT_EXPR:
2718 case CONJ_EXPR:
2720 case PREDECREMENT_EXPR:
2721 case PREINCREMENT_EXPR:
2722 case POSTDECREMENT_EXPR:
2723 case POSTINCREMENT_EXPR:
2725 case REALIGN_LOAD_EXPR:
2727 case REDUC_MAX_EXPR:
2728 case REDUC_MIN_EXPR:
2729 case REDUC_PLUS_EXPR:
2730 case WIDEN_SUM_EXPR:
2731 case WIDEN_MULT_EXPR:
2732 case DOT_PROD_EXPR:
2734 case VEC_WIDEN_MULT_HI_EXPR:
2735 case VEC_WIDEN_MULT_LO_EXPR:
2736 case VEC_UNPACK_HI_EXPR:
2737 case VEC_UNPACK_LO_EXPR:
2738 case VEC_UNPACK_FLOAT_HI_EXPR:
2739 case VEC_UNPACK_FLOAT_LO_EXPR:
2740 case VEC_PACK_TRUNC_EXPR:
2741 case VEC_PACK_SAT_EXPR:
2742 case VEC_PACK_FIX_TRUNC_EXPR:
2743 case VEC_EXTRACT_EVEN_EXPR:
2744 case VEC_EXTRACT_ODD_EXPR:
2745 case VEC_INTERLEAVE_HIGH_EXPR:
2746 case VEC_INTERLEAVE_LOW_EXPR:
2748 return 1;
2750 /* Few special cases of expensive operations. This is useful
2751 to avoid inlining on functions having too many of these. */
2752 case TRUNC_DIV_EXPR:
2753 case CEIL_DIV_EXPR:
2754 case FLOOR_DIV_EXPR:
2755 case ROUND_DIV_EXPR:
2756 case EXACT_DIV_EXPR:
2757 case TRUNC_MOD_EXPR:
2758 case CEIL_MOD_EXPR:
2759 case FLOOR_MOD_EXPR:
2760 case ROUND_MOD_EXPR:
2761 case RDIV_EXPR:
2762 return weights->div_mod_cost;
2764 default:
2765 /* We expect a copy assignment with no operator. */
2766 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS);
2767 return 0;
2772 /* Estimate number of instructions that will be created by expanding
2773 the statements in the statement sequence STMTS.
2774 WEIGHTS contains weights attributed to various constructs. */
2776 static
2777 int estimate_num_insns_seq (gimple_seq stmts, eni_weights *weights)
2779 int cost;
2780 gimple_stmt_iterator gsi;
2782 cost = 0;
2783 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
2784 cost += estimate_num_insns (gsi_stmt (gsi), weights);
2786 return cost;
2790 /* Estimate number of instructions that will be created by expanding STMT.
2791 WEIGHTS contains weights attributed to various constructs. */
2794 estimate_num_insns (gimple stmt, eni_weights *weights)
2796 unsigned cost, i;
2797 enum gimple_code code = gimple_code (stmt);
2798 tree lhs;
2800 switch (code)
2802 case GIMPLE_ASSIGN:
2803 /* Try to estimate the cost of assignments. We have three cases to
2804 deal with:
2805 1) Simple assignments to registers;
2806 2) Stores to things that must live in memory. This includes
2807 "normal" stores to scalars, but also assignments of large
2808 structures, or constructors of big arrays;
2810 Let us look at the first two cases, assuming we have "a = b + C":
2811 <GIMPLE_ASSIGN <var_decl "a">
2812 <plus_expr <var_decl "b"> <constant C>>
2813 If "a" is a GIMPLE register, the assignment to it is free on almost
2814 any target, because "a" usually ends up in a real register. Hence
2815 the only cost of this expression comes from the PLUS_EXPR, and we
2816 can ignore the GIMPLE_ASSIGN.
2817 If "a" is not a GIMPLE register, the assignment to "a" will most
2818 likely be a real store, so the cost of the GIMPLE_ASSIGN is the cost
2819 of moving something into "a", which we compute using the function
2820 estimate_move_cost. */
2821 lhs = gimple_assign_lhs (stmt);
2822 if (is_gimple_reg (lhs))
2823 cost = 0;
2824 else
2825 cost = estimate_move_cost (TREE_TYPE (lhs));
2827 cost += estimate_operator_cost (gimple_assign_rhs_code (stmt), weights);
2828 break;
2830 case GIMPLE_COND:
2831 cost = 1 + estimate_operator_cost (gimple_cond_code (stmt), weights);
2832 break;
2834 case GIMPLE_SWITCH:
2835 /* Take into account cost of the switch + guess 2 conditional jumps for
2836 each case label.
2838 TODO: once the switch expansion logic is sufficiently separated, we can
2839 do better job on estimating cost of the switch. */
2840 cost = gimple_switch_num_labels (stmt) * 2;
2841 break;
2843 case GIMPLE_CALL:
2845 tree decl = gimple_call_fndecl (stmt);
2846 tree addr = gimple_call_fn (stmt);
2847 tree funtype = TREE_TYPE (addr);
2849 if (POINTER_TYPE_P (funtype))
2850 funtype = TREE_TYPE (funtype);
2852 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
2853 cost = weights->target_builtin_call_cost;
2854 else
2855 cost = weights->call_cost;
2857 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2858 switch (DECL_FUNCTION_CODE (decl))
2860 case BUILT_IN_CONSTANT_P:
2861 return 0;
2862 case BUILT_IN_EXPECT:
2863 cost = 0;
2864 break;
2866 /* Prefetch instruction is not expensive. */
2867 case BUILT_IN_PREFETCH:
2868 cost = weights->target_builtin_call_cost;
2869 break;
2871 default:
2872 break;
2875 if (decl)
2876 funtype = TREE_TYPE (decl);
2878 /* Our cost must be kept in sync with
2879 cgraph_estimate_size_after_inlining that does use function
2880 declaration to figure out the arguments. */
2881 if (decl && DECL_ARGUMENTS (decl))
2883 tree arg;
2884 for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
2885 cost += estimate_move_cost (TREE_TYPE (arg));
2887 else if (funtype && prototype_p (funtype))
2889 tree t;
2890 for (t = TYPE_ARG_TYPES (funtype); t; t = TREE_CHAIN (t))
2891 cost += estimate_move_cost (TREE_VALUE (t));
2893 else
2895 for (i = 0; i < gimple_call_num_args (stmt); i++)
2897 tree arg = gimple_call_arg (stmt, i);
2898 cost += estimate_move_cost (TREE_TYPE (arg));
2902 break;
2905 case GIMPLE_GOTO:
2906 case GIMPLE_LABEL:
2907 case GIMPLE_NOP:
2908 case GIMPLE_PHI:
2909 case GIMPLE_RETURN:
2910 case GIMPLE_CHANGE_DYNAMIC_TYPE:
2911 case GIMPLE_PREDICT:
2912 return 0;
2914 case GIMPLE_ASM:
2915 case GIMPLE_RESX:
2916 return 1;
2918 case GIMPLE_BIND:
2919 return estimate_num_insns_seq (gimple_bind_body (stmt), weights);
2921 case GIMPLE_EH_FILTER:
2922 return estimate_num_insns_seq (gimple_eh_filter_failure (stmt), weights);
2924 case GIMPLE_CATCH:
2925 return estimate_num_insns_seq (gimple_catch_handler (stmt), weights);
2927 case GIMPLE_TRY:
2928 return (estimate_num_insns_seq (gimple_try_eval (stmt), weights)
2929 + estimate_num_insns_seq (gimple_try_cleanup (stmt), weights));
2931 /* OpenMP directives are generally very expensive. */
2933 case GIMPLE_OMP_RETURN:
2934 case GIMPLE_OMP_SECTIONS_SWITCH:
2935 case GIMPLE_OMP_ATOMIC_STORE:
2936 case GIMPLE_OMP_CONTINUE:
2937 /* ...except these, which are cheap. */
2938 return 0;
2940 case GIMPLE_OMP_ATOMIC_LOAD:
2941 return weights->omp_cost;
2943 case GIMPLE_OMP_FOR:
2944 return (weights->omp_cost
2945 + estimate_num_insns_seq (gimple_omp_body (stmt), weights)
2946 + estimate_num_insns_seq (gimple_omp_for_pre_body (stmt), weights));
2948 case GIMPLE_OMP_PARALLEL:
2949 case GIMPLE_OMP_TASK:
2950 case GIMPLE_OMP_CRITICAL:
2951 case GIMPLE_OMP_MASTER:
2952 case GIMPLE_OMP_ORDERED:
2953 case GIMPLE_OMP_SECTION:
2954 case GIMPLE_OMP_SECTIONS:
2955 case GIMPLE_OMP_SINGLE:
2956 return (weights->omp_cost
2957 + estimate_num_insns_seq (gimple_omp_body (stmt), weights));
2959 default:
2960 gcc_unreachable ();
2963 return cost;
2966 /* Estimate number of instructions that will be created by expanding
2967 function FNDECL. WEIGHTS contains weights attributed to various
2968 constructs. */
2971 estimate_num_insns_fn (tree fndecl, eni_weights *weights)
2973 struct function *my_function = DECL_STRUCT_FUNCTION (fndecl);
2974 gimple_stmt_iterator bsi;
2975 basic_block bb;
2976 int n = 0;
2978 gcc_assert (my_function && my_function->cfg);
2979 FOR_EACH_BB_FN (bb, my_function)
2981 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2982 n += estimate_num_insns (gsi_stmt (bsi), weights);
2985 return n;
2989 /* Initializes weights used by estimate_num_insns. */
2991 void
2992 init_inline_once (void)
2994 eni_inlining_weights.call_cost = PARAM_VALUE (PARAM_INLINE_CALL_COST);
2995 eni_inlining_weights.target_builtin_call_cost = 1;
2996 eni_inlining_weights.div_mod_cost = 10;
2997 eni_inlining_weights.omp_cost = 40;
2999 eni_size_weights.call_cost = 1;
3000 eni_size_weights.target_builtin_call_cost = 1;
3001 eni_size_weights.div_mod_cost = 1;
3002 eni_size_weights.omp_cost = 40;
3004 /* Estimating time for call is difficult, since we have no idea what the
3005 called function does. In the current uses of eni_time_weights,
3006 underestimating the cost does less harm than overestimating it, so
3007 we choose a rather small value here. */
3008 eni_time_weights.call_cost = 10;
3009 eni_time_weights.target_builtin_call_cost = 10;
3010 eni_time_weights.div_mod_cost = 10;
3011 eni_time_weights.omp_cost = 40;
3014 /* Estimate the number of instructions in a gimple_seq. */
3017 count_insns_seq (gimple_seq seq, eni_weights *weights)
3019 gimple_stmt_iterator gsi;
3020 int n = 0;
3021 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
3022 n += estimate_num_insns (gsi_stmt (gsi), weights);
3024 return n;
3028 /* Install new lexical TREE_BLOCK underneath 'current_block'. */
3030 static void
3031 add_lexical_block (tree current_block, tree new_block)
3033 tree *blk_p;
3035 /* Walk to the last sub-block. */
3036 for (blk_p = &BLOCK_SUBBLOCKS (current_block);
3037 *blk_p;
3038 blk_p = &BLOCK_CHAIN (*blk_p))
3040 *blk_p = new_block;
3041 BLOCK_SUPERCONTEXT (new_block) = current_block;
3044 /* Fetch callee declaration from the call graph edge going from NODE and
3045 associated with STMR call statement. Return NULL_TREE if not found. */
3046 static tree
3047 get_indirect_callee_fndecl (struct cgraph_node *node, gimple stmt)
3049 struct cgraph_edge *cs;
3051 cs = cgraph_edge (node, stmt);
3052 if (cs)
3053 return cs->callee->decl;
3055 return NULL_TREE;
3058 /* If STMT is a GIMPLE_CALL, replace it with its inline expansion. */
3060 static bool
3061 expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
3063 tree retvar, use_retvar;
3064 tree fn;
3065 struct pointer_map_t *st;
3066 tree return_slot;
3067 tree modify_dest;
3068 location_t saved_location;
3069 struct cgraph_edge *cg_edge;
3070 const char *reason;
3071 basic_block return_block;
3072 edge e;
3073 gimple_stmt_iterator gsi, stmt_gsi;
3074 bool successfully_inlined = FALSE;
3075 bool purge_dead_abnormal_edges;
3076 tree t_step;
3077 tree var;
3079 /* Set input_location here so we get the right instantiation context
3080 if we call instantiate_decl from inlinable_function_p. */
3081 saved_location = input_location;
3082 if (gimple_has_location (stmt))
3083 input_location = gimple_location (stmt);
3085 /* From here on, we're only interested in CALL_EXPRs. */
3086 if (gimple_code (stmt) != GIMPLE_CALL)
3087 goto egress;
3089 /* First, see if we can figure out what function is being called.
3090 If we cannot, then there is no hope of inlining the function. */
3091 fn = gimple_call_fndecl (stmt);
3092 if (!fn)
3094 fn = get_indirect_callee_fndecl (id->dst_node, stmt);
3095 if (!fn)
3096 goto egress;
3099 /* Turn forward declarations into real ones. */
3100 fn = cgraph_node (fn)->decl;
3102 /* If FN is a declaration of a function in a nested scope that was
3103 globally declared inline, we don't set its DECL_INITIAL.
3104 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
3105 C++ front-end uses it for cdtors to refer to their internal
3106 declarations, that are not real functions. Fortunately those
3107 don't have trees to be saved, so we can tell by checking their
3108 gimple_body. */
3109 if (!DECL_INITIAL (fn)
3110 && DECL_ABSTRACT_ORIGIN (fn)
3111 && gimple_has_body_p (DECL_ABSTRACT_ORIGIN (fn)))
3112 fn = DECL_ABSTRACT_ORIGIN (fn);
3114 /* Objective C and fortran still calls tree_rest_of_compilation directly.
3115 Kill this check once this is fixed. */
3116 if (!id->dst_node->analyzed)
3117 goto egress;
3119 cg_edge = cgraph_edge (id->dst_node, stmt);
3121 /* Constant propagation on argument done during previous inlining
3122 may create new direct call. Produce an edge for it. */
3123 if (!cg_edge)
3125 struct cgraph_node *dest = cgraph_node (fn);
3127 /* We have missing edge in the callgraph. This can happen in one case
3128 where previous inlining turned indirect call into direct call by
3129 constant propagating arguments. In all other cases we hit a bug
3130 (incorrect node sharing is most common reason for missing edges. */
3131 gcc_assert (dest->needed);
3132 cgraph_create_edge (id->dst_node, dest, stmt,
3133 bb->count, CGRAPH_FREQ_BASE,
3134 bb->loop_depth)->inline_failed
3135 = N_("originally indirect function call not considered for inlining");
3136 if (dump_file)
3138 fprintf (dump_file, "Created new direct edge to %s",
3139 cgraph_node_name (dest));
3141 goto egress;
3144 /* Don't try to inline functions that are not well-suited to
3145 inlining. */
3146 if (!cgraph_inline_p (cg_edge, &reason))
3148 /* If this call was originally indirect, we do not want to emit any
3149 inlining related warnings or sorry messages because there are no
3150 guarantees regarding those. */
3151 if (cg_edge->indirect_call)
3152 goto egress;
3154 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
3155 /* Avoid warnings during early inline pass. */
3156 && cgraph_global_info_ready)
3158 sorry ("inlining failed in call to %q+F: %s", fn, reason);
3159 sorry ("called from here");
3161 else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
3162 && !DECL_IN_SYSTEM_HEADER (fn)
3163 && strlen (reason)
3164 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
3165 /* Avoid warnings during early inline pass. */
3166 && cgraph_global_info_ready)
3168 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
3169 fn, reason);
3170 warning (OPT_Winline, "called from here");
3172 goto egress;
3174 fn = cg_edge->callee->decl;
3176 #ifdef ENABLE_CHECKING
3177 if (cg_edge->callee->decl != id->dst_node->decl)
3178 verify_cgraph_node (cg_edge->callee);
3179 #endif
3181 /* We will be inlining this callee. */
3182 id->eh_region = lookup_stmt_eh_region (stmt);
3184 /* Split the block holding the GIMPLE_CALL. */
3185 e = split_block (bb, stmt);
3186 bb = e->src;
3187 return_block = e->dest;
3188 remove_edge (e);
3190 /* split_block splits after the statement; work around this by
3191 moving the call into the second block manually. Not pretty,
3192 but seems easier than doing the CFG manipulation by hand
3193 when the GIMPLE_CALL is in the last statement of BB. */
3194 stmt_gsi = gsi_last_bb (bb);
3195 gsi_remove (&stmt_gsi, false);
3197 /* If the GIMPLE_CALL was in the last statement of BB, it may have
3198 been the source of abnormal edges. In this case, schedule
3199 the removal of dead abnormal edges. */
3200 gsi = gsi_start_bb (return_block);
3201 if (gsi_end_p (gsi))
3203 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
3204 purge_dead_abnormal_edges = true;
3206 else
3208 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
3209 purge_dead_abnormal_edges = false;
3212 stmt_gsi = gsi_start_bb (return_block);
3214 /* Build a block containing code to initialize the arguments, the
3215 actual inline expansion of the body, and a label for the return
3216 statements within the function to jump to. The type of the
3217 statement expression is the return type of the function call. */
3218 id->block = make_node (BLOCK);
3219 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
3220 BLOCK_SOURCE_LOCATION (id->block) = input_location;
3221 add_lexical_block (gimple_block (stmt), id->block);
3223 /* Local declarations will be replaced by their equivalents in this
3224 map. */
3225 st = id->decl_map;
3226 id->decl_map = pointer_map_create ();
3228 /* Record the function we are about to inline. */
3229 id->src_fn = fn;
3230 id->src_node = cg_edge->callee;
3231 id->src_cfun = DECL_STRUCT_FUNCTION (fn);
3232 id->gimple_call = stmt;
3234 gcc_assert (!id->src_cfun->after_inlining);
3236 id->entry_bb = bb;
3237 if (lookup_attribute ("cold", DECL_ATTRIBUTES (fn)))
3239 gimple_stmt_iterator si = gsi_last_bb (bb);
3240 gsi_insert_after (&si, gimple_build_predict (PRED_COLD_FUNCTION,
3241 NOT_TAKEN),
3242 GSI_NEW_STMT);
3244 initialize_inlined_parameters (id, stmt, fn, bb);
3246 if (DECL_INITIAL (fn))
3247 add_lexical_block (id->block, remap_blocks (DECL_INITIAL (fn), id));
3249 /* Return statements in the function body will be replaced by jumps
3250 to the RET_LABEL. */
3251 gcc_assert (DECL_INITIAL (fn));
3252 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
3254 /* Find the LHS to which the result of this call is assigned. */
3255 return_slot = NULL;
3256 if (gimple_call_lhs (stmt))
3258 modify_dest = gimple_call_lhs (stmt);
3260 /* The function which we are inlining might not return a value,
3261 in which case we should issue a warning that the function
3262 does not return a value. In that case the optimizers will
3263 see that the variable to which the value is assigned was not
3264 initialized. We do not want to issue a warning about that
3265 uninitialized variable. */
3266 if (DECL_P (modify_dest))
3267 TREE_NO_WARNING (modify_dest) = 1;
3269 if (gimple_call_return_slot_opt_p (stmt))
3271 return_slot = modify_dest;
3272 modify_dest = NULL;
3275 else
3276 modify_dest = NULL;
3278 /* If we are inlining a call to the C++ operator new, we don't want
3279 to use type based alias analysis on the return value. Otherwise
3280 we may get confused if the compiler sees that the inlined new
3281 function returns a pointer which was just deleted. See bug
3282 33407. */
3283 if (DECL_IS_OPERATOR_NEW (fn))
3285 return_slot = NULL;
3286 modify_dest = NULL;
3289 /* Declare the return variable for the function. */
3290 retvar = declare_return_variable (id, return_slot, modify_dest, &use_retvar);
3292 if (DECL_IS_OPERATOR_NEW (fn))
3294 gcc_assert (TREE_CODE (retvar) == VAR_DECL
3295 && POINTER_TYPE_P (TREE_TYPE (retvar)));
3296 DECL_NO_TBAA_P (retvar) = 1;
3299 /* This is it. Duplicate the callee body. Assume callee is
3300 pre-gimplified. Note that we must not alter the caller
3301 function in any way before this point, as this CALL_EXPR may be
3302 a self-referential call; if we're calling ourselves, we need to
3303 duplicate our body before altering anything. */
3304 copy_body (id, bb->count, bb->frequency, bb, return_block);
3306 /* Add local vars in this inlined callee to caller. */
3307 t_step = id->src_cfun->local_decls;
3308 for (; t_step; t_step = TREE_CHAIN (t_step))
3310 var = TREE_VALUE (t_step);
3311 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
3312 cfun->local_decls = tree_cons (NULL_TREE, var,
3313 cfun->local_decls);
3314 else
3315 cfun->local_decls = tree_cons (NULL_TREE, remap_decl (var, id),
3316 cfun->local_decls);
3319 /* Clean up. */
3320 pointer_map_destroy (id->decl_map);
3321 id->decl_map = st;
3323 /* If the inlined function returns a result that we care about,
3324 substitute the GIMPLE_CALL with an assignment of the return
3325 variable to the LHS of the call. That is, if STMT was
3326 'a = foo (...)', substitute the call with 'a = USE_RETVAR'. */
3327 if (use_retvar && gimple_call_lhs (stmt))
3329 gimple old_stmt = stmt;
3330 stmt = gimple_build_assign (gimple_call_lhs (stmt), use_retvar);
3331 gsi_replace (&stmt_gsi, stmt, false);
3332 if (gimple_in_ssa_p (cfun))
3334 update_stmt (stmt);
3335 mark_symbols_for_renaming (stmt);
3337 maybe_clean_or_replace_eh_stmt (old_stmt, stmt);
3339 else
3341 /* Handle the case of inlining a function with no return
3342 statement, which causes the return value to become undefined. */
3343 if (gimple_call_lhs (stmt)
3344 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
3346 tree name = gimple_call_lhs (stmt);
3347 tree var = SSA_NAME_VAR (name);
3348 tree def = gimple_default_def (cfun, var);
3350 if (def)
3352 /* If the variable is used undefined, make this name
3353 undefined via a move. */
3354 stmt = gimple_build_assign (gimple_call_lhs (stmt), def);
3355 gsi_replace (&stmt_gsi, stmt, true);
3356 update_stmt (stmt);
3358 else
3360 /* Otherwise make this variable undefined. */
3361 gsi_remove (&stmt_gsi, true);
3362 set_default_def (var, name);
3363 SSA_NAME_DEF_STMT (name) = gimple_build_nop ();
3366 else
3367 gsi_remove (&stmt_gsi, true);
3370 if (purge_dead_abnormal_edges)
3371 gimple_purge_dead_abnormal_call_edges (return_block);
3373 /* If the value of the new expression is ignored, that's OK. We
3374 don't warn about this for CALL_EXPRs, so we shouldn't warn about
3375 the equivalent inlined version either. */
3376 if (is_gimple_assign (stmt))
3378 gcc_assert (gimple_assign_single_p (stmt)
3379 || CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)));
3380 TREE_USED (gimple_assign_rhs1 (stmt)) = 1;
3383 /* Output the inlining info for this abstract function, since it has been
3384 inlined. If we don't do this now, we can lose the information about the
3385 variables in the function when the blocks get blown away as soon as we
3386 remove the cgraph node. */
3387 (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
3389 /* Update callgraph if needed. */
3390 cgraph_remove_node (cg_edge->callee);
3392 id->block = NULL_TREE;
3393 successfully_inlined = TRUE;
3395 egress:
3396 input_location = saved_location;
3397 return successfully_inlined;
3400 /* Expand call statements reachable from STMT_P.
3401 We can only have CALL_EXPRs as the "toplevel" tree code or nested
3402 in a MODIFY_EXPR. See tree-gimple.c:get_call_expr_in(). We can
3403 unfortunately not use that function here because we need a pointer
3404 to the CALL_EXPR, not the tree itself. */
3406 static bool
3407 gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
3409 gimple_stmt_iterator gsi;
3411 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3413 gimple stmt = gsi_stmt (gsi);
3415 if (is_gimple_call (stmt)
3416 && expand_call_inline (bb, stmt, id))
3417 return true;
3420 return false;
3424 /* Walk all basic blocks created after FIRST and try to fold every statement
3425 in the STATEMENTS pointer set. */
3427 static void
3428 fold_marked_statements (int first, struct pointer_set_t *statements)
3430 for (; first < n_basic_blocks; first++)
3431 if (BASIC_BLOCK (first))
3433 gimple_stmt_iterator gsi;
3435 for (gsi = gsi_start_bb (BASIC_BLOCK (first));
3436 !gsi_end_p (gsi);
3437 gsi_next (&gsi))
3438 if (pointer_set_contains (statements, gsi_stmt (gsi)))
3440 gimple old_stmt = gsi_stmt (gsi);
3442 if (fold_stmt (&gsi))
3444 /* Re-read the statement from GSI as fold_stmt() may
3445 have changed it. */
3446 gimple new_stmt = gsi_stmt (gsi);
3447 update_stmt (new_stmt);
3449 if (is_gimple_call (old_stmt))
3450 cgraph_update_edges_for_call_stmt (old_stmt, new_stmt);
3452 if (maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt))
3453 gimple_purge_dead_eh_edges (BASIC_BLOCK (first));
3459 /* Return true if BB has at least one abnormal outgoing edge. */
3461 static inline bool
3462 has_abnormal_outgoing_edge_p (basic_block bb)
3464 edge e;
3465 edge_iterator ei;
3467 FOR_EACH_EDGE (e, ei, bb->succs)
3468 if (e->flags & EDGE_ABNORMAL)
3469 return true;
3471 return false;
3474 /* Expand calls to inline functions in the body of FN. */
3476 unsigned int
3477 optimize_inline_calls (tree fn)
3479 copy_body_data id;
3480 tree prev_fn;
3481 basic_block bb;
3482 int last = n_basic_blocks;
3483 struct gimplify_ctx gctx;
3485 /* There is no point in performing inlining if errors have already
3486 occurred -- and we might crash if we try to inline invalid
3487 code. */
3488 if (errorcount || sorrycount)
3489 return 0;
3491 /* Clear out ID. */
3492 memset (&id, 0, sizeof (id));
3494 id.src_node = id.dst_node = cgraph_node (fn);
3495 id.dst_fn = fn;
3496 /* Or any functions that aren't finished yet. */
3497 prev_fn = NULL_TREE;
3498 if (current_function_decl)
3500 id.dst_fn = current_function_decl;
3501 prev_fn = current_function_decl;
3504 id.copy_decl = copy_decl_maybe_to_var;
3505 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
3506 id.transform_new_cfg = false;
3507 id.transform_return_to_modify = true;
3508 id.transform_lang_insert_block = NULL;
3509 id.statements_to_fold = pointer_set_create ();
3511 push_gimplify_context (&gctx);
3513 /* We make no attempts to keep dominance info up-to-date. */
3514 free_dominance_info (CDI_DOMINATORS);
3515 free_dominance_info (CDI_POST_DOMINATORS);
3517 /* Register specific gimple functions. */
3518 gimple_register_cfg_hooks ();
3520 /* Reach the trees by walking over the CFG, and note the
3521 enclosing basic-blocks in the call edges. */
3522 /* We walk the blocks going forward, because inlined function bodies
3523 will split id->current_basic_block, and the new blocks will
3524 follow it; we'll trudge through them, processing their CALL_EXPRs
3525 along the way. */
3526 FOR_EACH_BB (bb)
3527 gimple_expand_calls_inline (bb, &id);
3529 pop_gimplify_context (NULL);
3531 #ifdef ENABLE_CHECKING
3533 struct cgraph_edge *e;
3535 verify_cgraph_node (id.dst_node);
3537 /* Double check that we inlined everything we are supposed to inline. */
3538 for (e = id.dst_node->callees; e; e = e->next_callee)
3539 gcc_assert (e->inline_failed);
3541 #endif
3543 /* Fold the statements before compacting/renumbering the basic blocks. */
3544 fold_marked_statements (last, id.statements_to_fold);
3545 pointer_set_destroy (id.statements_to_fold);
3547 /* Renumber the (code) basic_blocks consecutively. */
3548 compact_blocks ();
3549 /* Renumber the lexical scoping (non-code) blocks consecutively. */
3550 number_blocks (fn);
3552 /* We are not going to maintain the cgraph edges up to date.
3553 Kill it so it won't confuse us. */
3554 cgraph_node_remove_callees (id.dst_node);
3556 fold_cond_expr_cond ();
3558 /* It would be nice to check SSA/CFG/statement consistency here, but it is
3559 not possible yet - the IPA passes might make various functions to not
3560 throw and they don't care to proactively update local EH info. This is
3561 done later in fixup_cfg pass that also execute the verification. */
3562 return (TODO_update_ssa
3563 | TODO_cleanup_cfg
3564 | (gimple_in_ssa_p (cfun) ? TODO_remove_unused_locals : 0)
3565 | (profile_status != PROFILE_ABSENT ? TODO_rebuild_frequencies : 0));
3568 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
3570 tree
3571 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
3573 enum tree_code code = TREE_CODE (*tp);
3574 enum tree_code_class cl = TREE_CODE_CLASS (code);
3576 /* We make copies of most nodes. */
3577 if (IS_EXPR_CODE_CLASS (cl)
3578 || code == TREE_LIST
3579 || code == TREE_VEC
3580 || code == TYPE_DECL
3581 || code == OMP_CLAUSE)
3583 /* Because the chain gets clobbered when we make a copy, we save it
3584 here. */
3585 tree chain = NULL_TREE, new_tree;
3587 chain = TREE_CHAIN (*tp);
3589 /* Copy the node. */
3590 new_tree = copy_node (*tp);
3592 /* Propagate mudflap marked-ness. */
3593 if (flag_mudflap && mf_marked_p (*tp))
3594 mf_mark (new_tree);
3596 *tp = new_tree;
3598 /* Now, restore the chain, if appropriate. That will cause
3599 walk_tree to walk into the chain as well. */
3600 if (code == PARM_DECL
3601 || code == TREE_LIST
3602 || code == OMP_CLAUSE)
3603 TREE_CHAIN (*tp) = chain;
3605 /* For now, we don't update BLOCKs when we make copies. So, we
3606 have to nullify all BIND_EXPRs. */
3607 if (TREE_CODE (*tp) == BIND_EXPR)
3608 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
3610 else if (code == CONSTRUCTOR)
3612 /* CONSTRUCTOR nodes need special handling because
3613 we need to duplicate the vector of elements. */
3614 tree new_tree;
3616 new_tree = copy_node (*tp);
3618 /* Propagate mudflap marked-ness. */
3619 if (flag_mudflap && mf_marked_p (*tp))
3620 mf_mark (new_tree);
3622 CONSTRUCTOR_ELTS (new_tree) = VEC_copy (constructor_elt, gc,
3623 CONSTRUCTOR_ELTS (*tp));
3624 *tp = new_tree;
3626 else if (TREE_CODE_CLASS (code) == tcc_type)
3627 *walk_subtrees = 0;
3628 else if (TREE_CODE_CLASS (code) == tcc_declaration)
3629 *walk_subtrees = 0;
3630 else if (TREE_CODE_CLASS (code) == tcc_constant)
3631 *walk_subtrees = 0;
3632 else
3633 gcc_assert (code != STATEMENT_LIST);
3634 return NULL_TREE;
3637 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
3638 information indicating to what new SAVE_EXPR this one should be mapped,
3639 use that one. Otherwise, create a new node and enter it in ST. FN is
3640 the function into which the copy will be placed. */
3642 static void
3643 remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
3645 struct pointer_map_t *st = (struct pointer_map_t *) st_;
3646 tree *n;
3647 tree t;
3649 /* See if we already encountered this SAVE_EXPR. */
3650 n = (tree *) pointer_map_contains (st, *tp);
3652 /* If we didn't already remap this SAVE_EXPR, do so now. */
3653 if (!n)
3655 t = copy_node (*tp);
3657 /* Remember this SAVE_EXPR. */
3658 *pointer_map_insert (st, *tp) = t;
3659 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
3660 *pointer_map_insert (st, t) = t;
3662 else
3664 /* We've already walked into this SAVE_EXPR; don't do it again. */
3665 *walk_subtrees = 0;
3666 t = *n;
3669 /* Replace this SAVE_EXPR with the copy. */
3670 *tp = t;
3673 /* Called via walk_tree. If *TP points to a DECL_STMT for a local label,
3674 copies the declaration and enters it in the splay_tree in DATA (which is
3675 really an `copy_body_data *'). */
3677 static tree
3678 mark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
3679 void *data)
3681 copy_body_data *id = (copy_body_data *) data;
3683 /* Don't walk into types. */
3684 if (TYPE_P (*tp))
3685 *walk_subtrees = 0;
3687 else if (TREE_CODE (*tp) == LABEL_EXPR)
3689 tree decl = TREE_OPERAND (*tp, 0);
3691 /* Copy the decl and remember the copy. */
3692 insert_decl_map (id, decl, id->copy_decl (decl, id));
3695 return NULL_TREE;
3698 /* Perform any modifications to EXPR required when it is unsaved. Does
3699 not recurse into EXPR's subtrees. */
3701 static void
3702 unsave_expr_1 (tree expr)
3704 switch (TREE_CODE (expr))
3706 case TARGET_EXPR:
3707 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
3708 It's OK for this to happen if it was part of a subtree that
3709 isn't immediately expanded, such as operand 2 of another
3710 TARGET_EXPR. */
3711 if (TREE_OPERAND (expr, 1))
3712 break;
3714 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
3715 TREE_OPERAND (expr, 3) = NULL_TREE;
3716 break;
3718 default:
3719 break;
3723 /* Called via walk_tree when an expression is unsaved. Using the
3724 splay_tree pointed to by ST (which is really a `splay_tree'),
3725 remaps all local declarations to appropriate replacements. */
3727 static tree
3728 unsave_r (tree *tp, int *walk_subtrees, void *data)
3730 copy_body_data *id = (copy_body_data *) data;
3731 struct pointer_map_t *st = id->decl_map;
3732 tree *n;
3734 /* Only a local declaration (variable or label). */
3735 if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
3736 || TREE_CODE (*tp) == LABEL_DECL)
3738 /* Lookup the declaration. */
3739 n = (tree *) pointer_map_contains (st, *tp);
3741 /* If it's there, remap it. */
3742 if (n)
3743 *tp = *n;
3746 else if (TREE_CODE (*tp) == STATEMENT_LIST)
3747 gcc_unreachable ();
3748 else if (TREE_CODE (*tp) == BIND_EXPR)
3749 copy_bind_expr (tp, walk_subtrees, id);
3750 else if (TREE_CODE (*tp) == SAVE_EXPR)
3751 remap_save_expr (tp, st, walk_subtrees);
3752 else
3754 copy_tree_r (tp, walk_subtrees, NULL);
3756 /* Do whatever unsaving is required. */
3757 unsave_expr_1 (*tp);
3760 /* Keep iterating. */
3761 return NULL_TREE;
3764 /* Copies everything in EXPR and replaces variables, labels
3765 and SAVE_EXPRs local to EXPR. */
3767 tree
3768 unsave_expr_now (tree expr)
3770 copy_body_data id;
3772 /* There's nothing to do for NULL_TREE. */
3773 if (expr == 0)
3774 return expr;
3776 /* Set up ID. */
3777 memset (&id, 0, sizeof (id));
3778 id.src_fn = current_function_decl;
3779 id.dst_fn = current_function_decl;
3780 id.decl_map = pointer_map_create ();
3782 id.copy_decl = copy_decl_no_change;
3783 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
3784 id.transform_new_cfg = false;
3785 id.transform_return_to_modify = false;
3786 id.transform_lang_insert_block = NULL;
3788 /* Walk the tree once to find local labels. */
3789 walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
3791 /* Walk the tree again, copying, remapping, and unsaving. */
3792 walk_tree (&expr, unsave_r, &id, NULL);
3794 /* Clean up. */
3795 pointer_map_destroy (id.decl_map);
3797 return expr;
3800 /* Called via walk_gimple_seq. If *GSIP points to a GIMPLE_LABEL for a local
3801 label, copies the declaration and enters it in the splay_tree in DATA (which
3802 is really a 'copy_body_data *'. */
3804 static tree
3805 mark_local_labels_stmt (gimple_stmt_iterator *gsip,
3806 bool *handled_ops_p ATTRIBUTE_UNUSED,
3807 struct walk_stmt_info *wi)
3809 copy_body_data *id = (copy_body_data *) wi->info;
3810 gimple stmt = gsi_stmt (*gsip);
3812 if (gimple_code (stmt) == GIMPLE_LABEL)
3814 tree decl = gimple_label_label (stmt);
3816 /* Copy the decl and remember the copy. */
3817 insert_decl_map (id, decl, id->copy_decl (decl, id));
3820 return NULL_TREE;
3824 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
3825 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
3826 remaps all local declarations to appropriate replacements in gimple
3827 operands. */
3829 static tree
3830 replace_locals_op (tree *tp, int *walk_subtrees, void *data)
3832 struct walk_stmt_info *wi = (struct walk_stmt_info*) data;
3833 copy_body_data *id = (copy_body_data *) wi->info;
3834 struct pointer_map_t *st = id->decl_map;
3835 tree *n;
3836 tree expr = *tp;
3838 /* Only a local declaration (variable or label). */
3839 if ((TREE_CODE (expr) == VAR_DECL
3840 && !TREE_STATIC (expr))
3841 || TREE_CODE (expr) == LABEL_DECL)
3843 /* Lookup the declaration. */
3844 n = (tree *) pointer_map_contains (st, expr);
3846 /* If it's there, remap it. */
3847 if (n)
3848 *tp = *n;
3849 *walk_subtrees = 0;
3851 else if (TREE_CODE (expr) == STATEMENT_LIST
3852 || TREE_CODE (expr) == BIND_EXPR
3853 || TREE_CODE (expr) == SAVE_EXPR)
3854 gcc_unreachable ();
3855 else if (TREE_CODE (expr) == TARGET_EXPR)
3857 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
3858 It's OK for this to happen if it was part of a subtree that
3859 isn't immediately expanded, such as operand 2 of another
3860 TARGET_EXPR. */
3861 if (!TREE_OPERAND (expr, 1))
3863 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
3864 TREE_OPERAND (expr, 3) = NULL_TREE;
3868 /* Keep iterating. */
3869 return NULL_TREE;
3873 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
3874 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
3875 remaps all local declarations to appropriate replacements in gimple
3876 statements. */
3878 static tree
3879 replace_locals_stmt (gimple_stmt_iterator *gsip,
3880 bool *handled_ops_p ATTRIBUTE_UNUSED,
3881 struct walk_stmt_info *wi)
3883 copy_body_data *id = (copy_body_data *) wi->info;
3884 gimple stmt = gsi_stmt (*gsip);
3886 if (gimple_code (stmt) == GIMPLE_BIND)
3888 tree block = gimple_bind_block (stmt);
3890 if (block)
3892 remap_block (&block, id);
3893 gimple_bind_set_block (stmt, block);
3896 /* This will remap a lot of the same decls again, but this should be
3897 harmless. */
3898 if (gimple_bind_vars (stmt))
3899 gimple_bind_set_vars (stmt, remap_decls (gimple_bind_vars (stmt), id));
3902 /* Keep iterating. */
3903 return NULL_TREE;
3907 /* Copies everything in SEQ and replaces variables and labels local to
3908 current_function_decl. */
3910 gimple_seq
3911 copy_gimple_seq_and_replace_locals (gimple_seq seq)
3913 copy_body_data id;
3914 struct walk_stmt_info wi;
3915 struct pointer_set_t *visited;
3916 gimple_seq copy;
3918 /* There's nothing to do for NULL_TREE. */
3919 if (seq == NULL)
3920 return seq;
3922 /* Set up ID. */
3923 memset (&id, 0, sizeof (id));
3924 id.src_fn = current_function_decl;
3925 id.dst_fn = current_function_decl;
3926 id.decl_map = pointer_map_create ();
3928 id.copy_decl = copy_decl_no_change;
3929 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
3930 id.transform_new_cfg = false;
3931 id.transform_return_to_modify = false;
3932 id.transform_lang_insert_block = NULL;
3934 /* Walk the tree once to find local labels. */
3935 memset (&wi, 0, sizeof (wi));
3936 visited = pointer_set_create ();
3937 wi.info = &id;
3938 wi.pset = visited;
3939 walk_gimple_seq (seq, mark_local_labels_stmt, NULL, &wi);
3940 pointer_set_destroy (visited);
3942 copy = gimple_seq_copy (seq);
3944 /* Walk the copy, remapping decls. */
3945 memset (&wi, 0, sizeof (wi));
3946 wi.info = &id;
3947 walk_gimple_seq (copy, replace_locals_stmt, replace_locals_op, &wi);
3949 /* Clean up. */
3950 pointer_map_destroy (id.decl_map);
3952 return copy;
3956 /* Allow someone to determine if SEARCH is a child of TOP from gdb. */
3958 static tree
3959 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
3961 if (*tp == data)
3962 return (tree) data;
3963 else
3964 return NULL;
3967 bool
3968 debug_find_tree (tree top, tree search)
3970 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
3974 /* Declare the variables created by the inliner. Add all the variables in
3975 VARS to BIND_EXPR. */
3977 static void
3978 declare_inline_vars (tree block, tree vars)
3980 tree t;
3981 for (t = vars; t; t = TREE_CHAIN (t))
3983 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
3984 gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
3985 cfun->local_decls = tree_cons (NULL_TREE, t, cfun->local_decls);
3988 if (block)
3989 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
3992 /* Copy NODE (which must be a DECL). The DECL originally was in the FROM_FN,
3993 but now it will be in the TO_FN. PARM_TO_VAR means enable PARM_DECL to
3994 VAR_DECL translation. */
3996 static tree
3997 copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
3999 /* Don't generate debug information for the copy if we wouldn't have
4000 generated it for the copy either. */
4001 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
4002 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
4004 /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
4005 declaration inspired this copy. */
4006 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
4008 /* The new variable/label has no RTL, yet. */
4009 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
4010 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
4011 SET_DECL_RTL (copy, NULL_RTX);
4013 /* These args would always appear unused, if not for this. */
4014 TREE_USED (copy) = 1;
4016 /* Set the context for the new declaration. */
4017 if (!DECL_CONTEXT (decl))
4018 /* Globals stay global. */
4020 else if (DECL_CONTEXT (decl) != id->src_fn)
4021 /* Things that weren't in the scope of the function we're inlining
4022 from aren't in the scope we're inlining to, either. */
4024 else if (TREE_STATIC (decl))
4025 /* Function-scoped static variables should stay in the original
4026 function. */
4028 else
4029 /* Ordinary automatic local variables are now in the scope of the
4030 new function. */
4031 DECL_CONTEXT (copy) = id->dst_fn;
4033 return copy;
4036 static tree
4037 copy_decl_to_var (tree decl, copy_body_data *id)
4039 tree copy, type;
4041 gcc_assert (TREE_CODE (decl) == PARM_DECL
4042 || TREE_CODE (decl) == RESULT_DECL);
4044 type = TREE_TYPE (decl);
4046 copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
4047 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4048 TREE_READONLY (copy) = TREE_READONLY (decl);
4049 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
4050 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
4051 DECL_NO_TBAA_P (copy) = DECL_NO_TBAA_P (decl);
4053 return copy_decl_for_dup_finish (id, decl, copy);
4056 /* Like copy_decl_to_var, but create a return slot object instead of a
4057 pointer variable for return by invisible reference. */
4059 static tree
4060 copy_result_decl_to_var (tree decl, copy_body_data *id)
4062 tree copy, type;
4064 gcc_assert (TREE_CODE (decl) == PARM_DECL
4065 || TREE_CODE (decl) == RESULT_DECL);
4067 type = TREE_TYPE (decl);
4068 if (DECL_BY_REFERENCE (decl))
4069 type = TREE_TYPE (type);
4071 copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
4072 TREE_READONLY (copy) = TREE_READONLY (decl);
4073 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
4074 if (!DECL_BY_REFERENCE (decl))
4076 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4077 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
4078 DECL_NO_TBAA_P (copy) = DECL_NO_TBAA_P (decl);
4081 return copy_decl_for_dup_finish (id, decl, copy);
4084 tree
4085 copy_decl_no_change (tree decl, copy_body_data *id)
4087 tree copy;
4089 copy = copy_node (decl);
4091 /* The COPY is not abstract; it will be generated in DST_FN. */
4092 DECL_ABSTRACT (copy) = 0;
4093 lang_hooks.dup_lang_specific_decl (copy);
4095 /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
4096 been taken; it's for internal bookkeeping in expand_goto_internal. */
4097 if (TREE_CODE (copy) == LABEL_DECL)
4099 TREE_ADDRESSABLE (copy) = 0;
4100 LABEL_DECL_UID (copy) = -1;
4103 return copy_decl_for_dup_finish (id, decl, copy);
4106 static tree
4107 copy_decl_maybe_to_var (tree decl, copy_body_data *id)
4109 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
4110 return copy_decl_to_var (decl, id);
4111 else
4112 return copy_decl_no_change (decl, id);
4115 /* Return a copy of the function's argument tree. */
4116 static tree
4117 copy_arguments_for_versioning (tree orig_parm, copy_body_data * id,
4118 bitmap args_to_skip, tree *vars)
4120 tree arg, *parg;
4121 tree new_parm = NULL;
4122 int i = 0;
4124 parg = &new_parm;
4126 for (arg = orig_parm; arg; arg = TREE_CHAIN (arg), i++)
4127 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
4129 tree new_tree = remap_decl (arg, id);
4130 lang_hooks.dup_lang_specific_decl (new_tree);
4131 *parg = new_tree;
4132 parg = &TREE_CHAIN (new_tree);
4134 else
4136 /* Make an equivalent VAR_DECL. If the argument was used
4137 as temporary variable later in function, the uses will be
4138 replaced by local variable. */
4139 tree var = copy_decl_to_var (arg, id);
4140 get_var_ann (var);
4141 add_referenced_var (var);
4142 insert_decl_map (id, arg, var);
4143 /* Declare this new variable. */
4144 TREE_CHAIN (var) = *vars;
4145 *vars = var;
4147 return new_parm;
4150 /* Return a copy of the function's static chain. */
4151 static tree
4152 copy_static_chain (tree static_chain, copy_body_data * id)
4154 tree *chain_copy, *pvar;
4156 chain_copy = &static_chain;
4157 for (pvar = chain_copy; *pvar; pvar = &TREE_CHAIN (*pvar))
4159 tree new_tree = remap_decl (*pvar, id);
4160 lang_hooks.dup_lang_specific_decl (new_tree);
4161 TREE_CHAIN (new_tree) = TREE_CHAIN (*pvar);
4162 *pvar = new_tree;
4164 return static_chain;
4167 /* Return true if the function is allowed to be versioned.
4168 This is a guard for the versioning functionality. */
4169 bool
4170 tree_versionable_function_p (tree fndecl)
4172 if (fndecl == NULL_TREE)
4173 return false;
4174 /* ??? There are cases where a function is
4175 uninlinable but can be versioned. */
4176 if (!tree_inlinable_function_p (fndecl))
4177 return false;
4179 return true;
4182 /* Create a copy of a function's tree.
4183 OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
4184 of the original function and the new copied function
4185 respectively. In case we want to replace a DECL
4186 tree with another tree while duplicating the function's
4187 body, TREE_MAP represents the mapping between these
4188 trees. If UPDATE_CLONES is set, the call_stmt fields
4189 of edges of clones of the function will be updated. */
4190 void
4191 tree_function_versioning (tree old_decl, tree new_decl, varray_type tree_map,
4192 bool update_clones, bitmap args_to_skip)
4194 struct cgraph_node *old_version_node;
4195 struct cgraph_node *new_version_node;
4196 copy_body_data id;
4197 tree p;
4198 unsigned i;
4199 struct ipa_replace_map *replace_info;
4200 basic_block old_entry_block;
4201 VEC (gimple, heap) *init_stmts = VEC_alloc (gimple, heap, 10);
4203 tree t_step;
4204 tree old_current_function_decl = current_function_decl;
4205 tree vars = NULL_TREE;
4207 gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
4208 && TREE_CODE (new_decl) == FUNCTION_DECL);
4209 DECL_POSSIBLY_INLINED (old_decl) = 1;
4211 old_version_node = cgraph_node (old_decl);
4212 new_version_node = cgraph_node (new_decl);
4214 DECL_ARTIFICIAL (new_decl) = 1;
4215 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
4217 /* Prepare the data structures for the tree copy. */
4218 memset (&id, 0, sizeof (id));
4220 /* Generate a new name for the new version. */
4221 if (!update_clones)
4223 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
4224 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
4225 SET_DECL_RTL (new_decl, NULL_RTX);
4226 id.statements_to_fold = pointer_set_create ();
4229 id.decl_map = pointer_map_create ();
4230 id.src_fn = old_decl;
4231 id.dst_fn = new_decl;
4232 id.src_node = old_version_node;
4233 id.dst_node = new_version_node;
4234 id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
4236 id.copy_decl = copy_decl_no_change;
4237 id.transform_call_graph_edges
4238 = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
4239 id.transform_new_cfg = true;
4240 id.transform_return_to_modify = false;
4241 id.transform_lang_insert_block = NULL;
4243 current_function_decl = new_decl;
4244 old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
4245 (DECL_STRUCT_FUNCTION (old_decl));
4246 initialize_cfun (new_decl, old_decl,
4247 old_entry_block->count,
4248 old_entry_block->frequency);
4249 push_cfun (DECL_STRUCT_FUNCTION (new_decl));
4251 /* Copy the function's static chain. */
4252 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
4253 if (p)
4254 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
4255 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
4256 &id);
4257 /* Copy the function's arguments. */
4258 if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
4259 DECL_ARGUMENTS (new_decl) =
4260 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id,
4261 args_to_skip, &vars);
4263 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
4265 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4266 number_blocks (id.dst_fn);
4268 /* If there's a tree_map, prepare for substitution. */
4269 if (tree_map)
4270 for (i = 0; i < VARRAY_ACTIVE_SIZE (tree_map); i++)
4272 gimple init;
4273 replace_info
4274 = (struct ipa_replace_map *) VARRAY_GENERIC_PTR (tree_map, i);
4275 if (replace_info->replace_p)
4277 tree op = replace_info->new_tree;
4279 STRIP_NOPS (op);
4281 if (TREE_CODE (op) == VIEW_CONVERT_EXPR)
4282 op = TREE_OPERAND (op, 0);
4284 if (TREE_CODE (op) == ADDR_EXPR)
4286 op = TREE_OPERAND (op, 0);
4287 while (handled_component_p (op))
4288 op = TREE_OPERAND (op, 0);
4289 if (TREE_CODE (op) == VAR_DECL)
4290 add_referenced_var (op);
4292 gcc_assert (TREE_CODE (replace_info->old_tree) == PARM_DECL);
4293 init = setup_one_parameter (&id, replace_info->old_tree,
4294 replace_info->new_tree, id.src_fn,
4295 NULL,
4296 &vars);
4297 if (init)
4298 VEC_safe_push (gimple, heap, init_stmts, init);
4302 declare_inline_vars (DECL_INITIAL (new_decl), vars);
4303 if (DECL_STRUCT_FUNCTION (old_decl)->local_decls != NULL_TREE)
4304 /* Add local vars. */
4305 for (t_step = DECL_STRUCT_FUNCTION (old_decl)->local_decls;
4306 t_step; t_step = TREE_CHAIN (t_step))
4308 tree var = TREE_VALUE (t_step);
4309 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
4310 cfun->local_decls = tree_cons (NULL_TREE, var, cfun->local_decls);
4311 else
4312 cfun->local_decls =
4313 tree_cons (NULL_TREE, remap_decl (var, &id),
4314 cfun->local_decls);
4317 /* Copy the Function's body. */
4318 copy_body (&id, old_entry_block->count, old_entry_block->frequency, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR);
4320 if (DECL_RESULT (old_decl) != NULL_TREE)
4322 tree *res_decl = &DECL_RESULT (old_decl);
4323 DECL_RESULT (new_decl) = remap_decl (*res_decl, &id);
4324 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
4327 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4328 number_blocks (new_decl);
4330 if (VEC_length (gimple, init_stmts))
4332 basic_block bb = split_edge (single_succ_edge (ENTRY_BLOCK_PTR));
4333 while (VEC_length (gimple, init_stmts))
4334 insert_init_stmt (bb, VEC_pop (gimple, init_stmts));
4337 /* Clean up. */
4338 pointer_map_destroy (id.decl_map);
4339 if (!update_clones)
4341 fold_marked_statements (0, id.statements_to_fold);
4342 pointer_set_destroy (id.statements_to_fold);
4343 fold_cond_expr_cond ();
4345 if (gimple_in_ssa_p (cfun))
4347 free_dominance_info (CDI_DOMINATORS);
4348 free_dominance_info (CDI_POST_DOMINATORS);
4349 if (!update_clones)
4350 delete_unreachable_blocks ();
4351 update_ssa (TODO_update_ssa);
4352 if (!update_clones)
4354 fold_cond_expr_cond ();
4355 if (need_ssa_update_p ())
4356 update_ssa (TODO_update_ssa);
4359 free_dominance_info (CDI_DOMINATORS);
4360 free_dominance_info (CDI_POST_DOMINATORS);
4361 VEC_free (gimple, heap, init_stmts);
4362 pop_cfun ();
4363 current_function_decl = old_current_function_decl;
4364 gcc_assert (!current_function_decl
4365 || DECL_STRUCT_FUNCTION (current_function_decl) == cfun);
4366 return;
4369 /* Duplicate a type, fields and all. */
4371 tree
4372 build_duplicate_type (tree type)
4374 struct copy_body_data id;
4376 memset (&id, 0, sizeof (id));
4377 id.src_fn = current_function_decl;
4378 id.dst_fn = current_function_decl;
4379 id.src_cfun = cfun;
4380 id.decl_map = pointer_map_create ();
4381 id.copy_decl = copy_decl_no_change;
4383 type = remap_type_1 (type, &id);
4385 pointer_map_destroy (id.decl_map);
4387 TYPE_CANONICAL (type) = type;
4389 return type;
4392 /* Return whether it is safe to inline a function because it used different
4393 target specific options or different optimization options. */
4394 bool
4395 tree_can_inline_p (tree caller, tree callee)
4397 #if 0
4398 /* This causes a regression in SPEC in that it prevents a cold function from
4399 inlining a hot function. Perhaps this should only apply to functions
4400 that the user declares hot/cold/optimize explicitly. */
4402 /* Don't inline a function with a higher optimization level than the
4403 caller, or with different space constraints (hot/cold functions). */
4404 tree caller_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (caller);
4405 tree callee_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee);
4407 if (caller_tree != callee_tree)
4409 struct cl_optimization *caller_opt
4410 = TREE_OPTIMIZATION ((caller_tree)
4411 ? caller_tree
4412 : optimization_default_node);
4414 struct cl_optimization *callee_opt
4415 = TREE_OPTIMIZATION ((callee_tree)
4416 ? callee_tree
4417 : optimization_default_node);
4419 if ((caller_opt->optimize > callee_opt->optimize)
4420 || (caller_opt->optimize_size != callee_opt->optimize_size))
4421 return false;
4423 #endif
4425 /* Allow the backend to decide if inlining is ok. */
4426 return targetm.target_option.can_inline_p (caller, callee);