tree-inline.c (remap_gimple_stmt): Do not assing a BLOCK to a stmt that didn't have...
[gcc.git] / gcc / tree-inline.c
1 /* Tree inlining.
2 Copyright (C) 2001-2013 Free Software Foundation, Inc.
3 Contributed by Alexandre Oliva <aoliva@redhat.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "diagnostic-core.h"
26 #include "tree.h"
27 #include "tree-inline.h"
28 #include "flags.h"
29 #include "params.h"
30 #include "input.h"
31 #include "insn-config.h"
32 #include "hashtab.h"
33 #include "langhooks.h"
34 #include "basic-block.h"
35 #include "tree-iterator.h"
36 #include "cgraph.h"
37 #include "intl.h"
38 #include "tree-mudflap.h"
39 #include "tree-flow.h"
40 #include "function.h"
41 #include "tree-flow.h"
42 #include "tree-pretty-print.h"
43 #include "except.h"
44 #include "debug.h"
45 #include "pointer-set.h"
46 #include "ipa-prop.h"
47 #include "value-prof.h"
48 #include "tree-pass.h"
49 #include "target.h"
50
51 #include "rtl.h" /* FIXME: For asm_str_count. */
52
53 /* I'm not real happy about this, but we need to handle gimple and
54 non-gimple trees. */
55 #include "gimple.h"
56
57 /* Inlining, Cloning, Versioning, Parallelization
58
59 Inlining: a function body is duplicated, but the PARM_DECLs are
60 remapped into VAR_DECLs, and non-void RETURN_EXPRs become
61 MODIFY_EXPRs that store to a dedicated returned-value variable.
62 The duplicated eh_region info of the copy will later be appended
63 to the info for the caller; the eh_region info in copied throwing
64 statements and RESX statements are adjusted accordingly.
65
66 Cloning: (only in C++) We have one body for a con/de/structor, and
67 multiple function decls, each with a unique parameter list.
68 Duplicate the body, using the given splay tree; some parameters
69 will become constants (like 0 or 1).
70
71 Versioning: a function body is duplicated and the result is a new
72 function rather than into blocks of an existing function as with
73 inlining. Some parameters will become constants.
74
75 Parallelization: a region of a function is duplicated resulting in
76 a new function. Variables may be replaced with complex expressions
77 to enable shared variable semantics.
78
79 All of these will simultaneously lookup any callgraph edges. If
80 we're going to inline the duplicated function body, and the given
81 function has some cloned callgraph nodes (one for each place this
82 function will be inlined) those callgraph edges will be duplicated.
83 If we're cloning the body, those callgraph edges will be
84 updated to point into the new body. (Note that the original
85 callgraph node and edge list will not be altered.)
86
87 See the CALL_EXPR handling case in copy_tree_body_r (). */
88
89 /* To Do:
90
91 o In order to make inlining-on-trees work, we pessimized
92 function-local static constants. In particular, they are now
93 always output, even when not addressed. Fix this by treating
94 function-local static constants just like global static
95 constants; the back-end already knows not to output them if they
96 are not needed.
97
98 o Provide heuristics to clamp inlining of recursive template
99 calls? */
100
101
102 /* Weights that estimate_num_insns uses to estimate the size of the
103 produced code. */
104
105 eni_weights eni_size_weights;
106
107 /* Weights that estimate_num_insns uses to estimate the time necessary
108 to execute the produced code. */
109
110 eni_weights eni_time_weights;
111
112 /* Prototypes. */
113
114 static tree declare_return_variable (copy_body_data *, tree, tree, basic_block);
115 static void remap_block (tree *, copy_body_data *);
116 static void copy_bind_expr (tree *, int *, copy_body_data *);
117 static tree mark_local_for_remap_r (tree *, int *, void *);
118 static void unsave_expr_1 (tree);
119 static tree unsave_r (tree *, int *, void *);
120 static void declare_inline_vars (tree, tree);
121 static void remap_save_expr (tree *, void *, int *);
122 static void prepend_lexical_block (tree current_block, tree new_block);
123 static tree copy_decl_to_var (tree, copy_body_data *);
124 static tree copy_result_decl_to_var (tree, copy_body_data *);
125 static tree copy_decl_maybe_to_var (tree, copy_body_data *);
126 static gimple remap_gimple_stmt (gimple, copy_body_data *);
127 static bool delete_unreachable_blocks_update_callgraph (copy_body_data *id);
128
129 /* Insert a tree->tree mapping for ID. Despite the name suggests
130 that the trees should be variables, it is used for more than that. */
131
132 void
133 insert_decl_map (copy_body_data *id, tree key, tree value)
134 {
135 *pointer_map_insert (id->decl_map, key) = value;
136
137 /* Always insert an identity map as well. If we see this same new
138 node again, we won't want to duplicate it a second time. */
139 if (key != value)
140 *pointer_map_insert (id->decl_map, value) = value;
141 }
142
143 /* Insert a tree->tree mapping for ID. This is only used for
144 variables. */
145
146 static void
147 insert_debug_decl_map (copy_body_data *id, tree key, tree value)
148 {
149 if (!gimple_in_ssa_p (id->src_cfun))
150 return;
151
152 if (!MAY_HAVE_DEBUG_STMTS)
153 return;
154
155 if (!target_for_debug_bind (key))
156 return;
157
158 gcc_assert (TREE_CODE (key) == PARM_DECL);
159 gcc_assert (TREE_CODE (value) == VAR_DECL);
160
161 if (!id->debug_map)
162 id->debug_map = pointer_map_create ();
163
164 *pointer_map_insert (id->debug_map, key) = value;
165 }
166
167 /* If nonzero, we're remapping the contents of inlined debug
168 statements. If negative, an error has occurred, such as a
169 reference to a variable that isn't available in the inlined
170 context. */
171 static int processing_debug_stmt = 0;
172
173 /* Construct new SSA name for old NAME. ID is the inline context. */
174
175 static tree
176 remap_ssa_name (tree name, copy_body_data *id)
177 {
178 tree new_tree, var;
179 tree *n;
180
181 gcc_assert (TREE_CODE (name) == SSA_NAME);
182
183 n = (tree *) pointer_map_contains (id->decl_map, name);
184 if (n)
185 return unshare_expr (*n);
186
187 if (processing_debug_stmt)
188 {
189 if (SSA_NAME_IS_DEFAULT_DEF (name)
190 && TREE_CODE (SSA_NAME_VAR (name)) == PARM_DECL
191 && id->entry_bb == NULL
192 && single_succ_p (ENTRY_BLOCK_PTR))
193 {
194 tree vexpr = make_node (DEBUG_EXPR_DECL);
195 gimple def_temp;
196 gimple_stmt_iterator gsi;
197 tree val = SSA_NAME_VAR (name);
198
199 n = (tree *) pointer_map_contains (id->decl_map, val);
200 if (n != NULL)
201 val = *n;
202 if (TREE_CODE (val) != PARM_DECL)
203 {
204 processing_debug_stmt = -1;
205 return name;
206 }
207 def_temp = gimple_build_debug_source_bind (vexpr, val, NULL);
208 DECL_ARTIFICIAL (vexpr) = 1;
209 TREE_TYPE (vexpr) = TREE_TYPE (name);
210 DECL_MODE (vexpr) = DECL_MODE (SSA_NAME_VAR (name));
211 gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR));
212 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
213 return vexpr;
214 }
215
216 processing_debug_stmt = -1;
217 return name;
218 }
219
220 /* Remap anonymous SSA names or SSA names of anonymous decls. */
221 var = SSA_NAME_VAR (name);
222 if (!var
223 || (!SSA_NAME_IS_DEFAULT_DEF (name)
224 && TREE_CODE (var) == VAR_DECL
225 && !VAR_DECL_IS_VIRTUAL_OPERAND (var)
226 && DECL_ARTIFICIAL (var)
227 && DECL_IGNORED_P (var)
228 && !DECL_NAME (var)))
229 {
230 struct ptr_info_def *pi;
231 new_tree = make_ssa_name (remap_type (TREE_TYPE (name), id), NULL);
232 if (!var && SSA_NAME_IDENTIFIER (name))
233 SET_SSA_NAME_VAR_OR_IDENTIFIER (new_tree, SSA_NAME_IDENTIFIER (name));
234 insert_decl_map (id, name, new_tree);
235 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_tree)
236 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
237 /* At least IPA points-to info can be directly transferred. */
238 if (id->src_cfun->gimple_df
239 && id->src_cfun->gimple_df->ipa_pta
240 && (pi = SSA_NAME_PTR_INFO (name))
241 && !pi->pt.anything)
242 {
243 struct ptr_info_def *new_pi = get_ptr_info (new_tree);
244 new_pi->pt = pi->pt;
245 }
246 return new_tree;
247 }
248
249 /* Do not set DEF_STMT yet as statement is not copied yet. We do that
250 in copy_bb. */
251 new_tree = remap_decl (var, id);
252
253 /* We might've substituted constant or another SSA_NAME for
254 the variable.
255
256 Replace the SSA name representing RESULT_DECL by variable during
257 inlining: this saves us from need to introduce PHI node in a case
258 return value is just partly initialized. */
259 if ((TREE_CODE (new_tree) == VAR_DECL || TREE_CODE (new_tree) == PARM_DECL)
260 && (!SSA_NAME_VAR (name)
261 || TREE_CODE (SSA_NAME_VAR (name)) != RESULT_DECL
262 || !id->transform_return_to_modify))
263 {
264 struct ptr_info_def *pi;
265 new_tree = make_ssa_name (new_tree, NULL);
266 insert_decl_map (id, name, new_tree);
267 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_tree)
268 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
269 /* At least IPA points-to info can be directly transferred. */
270 if (id->src_cfun->gimple_df
271 && id->src_cfun->gimple_df->ipa_pta
272 && (pi = SSA_NAME_PTR_INFO (name))
273 && !pi->pt.anything)
274 {
275 struct ptr_info_def *new_pi = get_ptr_info (new_tree);
276 new_pi->pt = pi->pt;
277 }
278 if (SSA_NAME_IS_DEFAULT_DEF (name))
279 {
280 /* By inlining function having uninitialized variable, we might
281 extend the lifetime (variable might get reused). This cause
282 ICE in the case we end up extending lifetime of SSA name across
283 abnormal edge, but also increase register pressure.
284
285 We simply initialize all uninitialized vars by 0 except
286 for case we are inlining to very first BB. We can avoid
287 this for all BBs that are not inside strongly connected
288 regions of the CFG, but this is expensive to test. */
289 if (id->entry_bb
290 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name)
291 && (!SSA_NAME_VAR (name)
292 || TREE_CODE (SSA_NAME_VAR (name)) != PARM_DECL)
293 && (id->entry_bb != EDGE_SUCC (ENTRY_BLOCK_PTR, 0)->dest
294 || EDGE_COUNT (id->entry_bb->preds) != 1))
295 {
296 gimple_stmt_iterator gsi = gsi_last_bb (id->entry_bb);
297 gimple init_stmt;
298 tree zero = build_zero_cst (TREE_TYPE (new_tree));
299
300 init_stmt = gimple_build_assign (new_tree, zero);
301 gsi_insert_after (&gsi, init_stmt, GSI_NEW_STMT);
302 SSA_NAME_IS_DEFAULT_DEF (new_tree) = 0;
303 }
304 else
305 {
306 SSA_NAME_DEF_STMT (new_tree) = gimple_build_nop ();
307 set_ssa_default_def (cfun, SSA_NAME_VAR (new_tree), new_tree);
308 }
309 }
310 }
311 else
312 insert_decl_map (id, name, new_tree);
313 return new_tree;
314 }
315
316 /* Remap DECL during the copying of the BLOCK tree for the function. */
317
318 tree
319 remap_decl (tree decl, copy_body_data *id)
320 {
321 tree *n;
322
323 /* We only remap local variables in the current function. */
324
325 /* See if we have remapped this declaration. */
326
327 n = (tree *) pointer_map_contains (id->decl_map, decl);
328
329 if (!n && processing_debug_stmt)
330 {
331 processing_debug_stmt = -1;
332 return decl;
333 }
334
335 /* If we didn't already have an equivalent for this declaration,
336 create one now. */
337 if (!n)
338 {
339 /* Make a copy of the variable or label. */
340 tree t = id->copy_decl (decl, id);
341
342 /* Remember it, so that if we encounter this local entity again
343 we can reuse this copy. Do this early because remap_type may
344 need this decl for TYPE_STUB_DECL. */
345 insert_decl_map (id, decl, t);
346
347 if (!DECL_P (t))
348 return t;
349
350 /* Remap types, if necessary. */
351 TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
352 if (TREE_CODE (t) == TYPE_DECL)
353 DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
354
355 /* Remap sizes as necessary. */
356 walk_tree (&DECL_SIZE (t), copy_tree_body_r, id, NULL);
357 walk_tree (&DECL_SIZE_UNIT (t), copy_tree_body_r, id, NULL);
358
359 /* If fields, do likewise for offset and qualifier. */
360 if (TREE_CODE (t) == FIELD_DECL)
361 {
362 walk_tree (&DECL_FIELD_OFFSET (t), copy_tree_body_r, id, NULL);
363 if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
364 walk_tree (&DECL_QUALIFIER (t), copy_tree_body_r, id, NULL);
365 }
366
367 return t;
368 }
369
370 if (id->do_not_unshare)
371 return *n;
372 else
373 return unshare_expr (*n);
374 }
375
376 static tree
377 remap_type_1 (tree type, copy_body_data *id)
378 {
379 tree new_tree, t;
380
381 /* We do need a copy. build and register it now. If this is a pointer or
382 reference type, remap the designated type and make a new pointer or
383 reference type. */
384 if (TREE_CODE (type) == POINTER_TYPE)
385 {
386 new_tree = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
387 TYPE_MODE (type),
388 TYPE_REF_CAN_ALIAS_ALL (type));
389 if (TYPE_ATTRIBUTES (type) || TYPE_QUALS (type))
390 new_tree = build_type_attribute_qual_variant (new_tree,
391 TYPE_ATTRIBUTES (type),
392 TYPE_QUALS (type));
393 insert_decl_map (id, type, new_tree);
394 return new_tree;
395 }
396 else if (TREE_CODE (type) == REFERENCE_TYPE)
397 {
398 new_tree = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
399 TYPE_MODE (type),
400 TYPE_REF_CAN_ALIAS_ALL (type));
401 if (TYPE_ATTRIBUTES (type) || TYPE_QUALS (type))
402 new_tree = build_type_attribute_qual_variant (new_tree,
403 TYPE_ATTRIBUTES (type),
404 TYPE_QUALS (type));
405 insert_decl_map (id, type, new_tree);
406 return new_tree;
407 }
408 else
409 new_tree = copy_node (type);
410
411 insert_decl_map (id, type, new_tree);
412
413 /* This is a new type, not a copy of an old type. Need to reassociate
414 variants. We can handle everything except the main variant lazily. */
415 t = TYPE_MAIN_VARIANT (type);
416 if (type != t)
417 {
418 t = remap_type (t, id);
419 TYPE_MAIN_VARIANT (new_tree) = t;
420 TYPE_NEXT_VARIANT (new_tree) = TYPE_NEXT_VARIANT (t);
421 TYPE_NEXT_VARIANT (t) = new_tree;
422 }
423 else
424 {
425 TYPE_MAIN_VARIANT (new_tree) = new_tree;
426 TYPE_NEXT_VARIANT (new_tree) = NULL;
427 }
428
429 if (TYPE_STUB_DECL (type))
430 TYPE_STUB_DECL (new_tree) = remap_decl (TYPE_STUB_DECL (type), id);
431
432 /* Lazily create pointer and reference types. */
433 TYPE_POINTER_TO (new_tree) = NULL;
434 TYPE_REFERENCE_TO (new_tree) = NULL;
435
436 switch (TREE_CODE (new_tree))
437 {
438 case INTEGER_TYPE:
439 case REAL_TYPE:
440 case FIXED_POINT_TYPE:
441 case ENUMERAL_TYPE:
442 case BOOLEAN_TYPE:
443 t = TYPE_MIN_VALUE (new_tree);
444 if (t && TREE_CODE (t) != INTEGER_CST)
445 walk_tree (&TYPE_MIN_VALUE (new_tree), copy_tree_body_r, id, NULL);
446
447 t = TYPE_MAX_VALUE (new_tree);
448 if (t && TREE_CODE (t) != INTEGER_CST)
449 walk_tree (&TYPE_MAX_VALUE (new_tree), copy_tree_body_r, id, NULL);
450 return new_tree;
451
452 case FUNCTION_TYPE:
453 TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
454 walk_tree (&TYPE_ARG_TYPES (new_tree), copy_tree_body_r, id, NULL);
455 return new_tree;
456
457 case ARRAY_TYPE:
458 TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
459 TYPE_DOMAIN (new_tree) = remap_type (TYPE_DOMAIN (new_tree), id);
460 break;
461
462 case RECORD_TYPE:
463 case UNION_TYPE:
464 case QUAL_UNION_TYPE:
465 {
466 tree f, nf = NULL;
467
468 for (f = TYPE_FIELDS (new_tree); f ; f = DECL_CHAIN (f))
469 {
470 t = remap_decl (f, id);
471 DECL_CONTEXT (t) = new_tree;
472 DECL_CHAIN (t) = nf;
473 nf = t;
474 }
475 TYPE_FIELDS (new_tree) = nreverse (nf);
476 }
477 break;
478
479 case OFFSET_TYPE:
480 default:
481 /* Shouldn't have been thought variable sized. */
482 gcc_unreachable ();
483 }
484
485 walk_tree (&TYPE_SIZE (new_tree), copy_tree_body_r, id, NULL);
486 walk_tree (&TYPE_SIZE_UNIT (new_tree), copy_tree_body_r, id, NULL);
487
488 return new_tree;
489 }
490
491 tree
492 remap_type (tree type, copy_body_data *id)
493 {
494 tree *node;
495 tree tmp;
496
497 if (type == NULL)
498 return type;
499
500 /* See if we have remapped this type. */
501 node = (tree *) pointer_map_contains (id->decl_map, type);
502 if (node)
503 return *node;
504
505 /* The type only needs remapping if it's variably modified. */
506 if (! variably_modified_type_p (type, id->src_fn))
507 {
508 insert_decl_map (id, type, type);
509 return type;
510 }
511
512 id->remapping_type_depth++;
513 tmp = remap_type_1 (type, id);
514 id->remapping_type_depth--;
515
516 return tmp;
517 }
518
519 /* Decide if DECL can be put into BLOCK_NONLOCAL_VARs. */
520
521 static bool
522 can_be_nonlocal (tree decl, copy_body_data *id)
523 {
524 /* We can not duplicate function decls. */
525 if (TREE_CODE (decl) == FUNCTION_DECL)
526 return true;
527
528 /* Local static vars must be non-local or we get multiple declaration
529 problems. */
530 if (TREE_CODE (decl) == VAR_DECL
531 && !auto_var_in_fn_p (decl, id->src_fn))
532 return true;
533
534 return false;
535 }
536
537 static tree
538 remap_decls (tree decls, vec<tree, va_gc> **nonlocalized_list,
539 copy_body_data *id)
540 {
541 tree old_var;
542 tree new_decls = NULL_TREE;
543
544 /* Remap its variables. */
545 for (old_var = decls; old_var; old_var = DECL_CHAIN (old_var))
546 {
547 tree new_var;
548
549 if (can_be_nonlocal (old_var, id))
550 {
551 /* We need to add this variable to the local decls as otherwise
552 nothing else will do so. */
553 if (TREE_CODE (old_var) == VAR_DECL
554 && ! DECL_EXTERNAL (old_var))
555 add_local_decl (cfun, old_var);
556 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
557 && !DECL_IGNORED_P (old_var)
558 && nonlocalized_list)
559 vec_safe_push (*nonlocalized_list, old_var);
560 continue;
561 }
562
563 /* Remap the variable. */
564 new_var = remap_decl (old_var, id);
565
566 /* If we didn't remap this variable, we can't mess with its
567 TREE_CHAIN. If we remapped this variable to the return slot, it's
568 already declared somewhere else, so don't declare it here. */
569
570 if (new_var == id->retvar)
571 ;
572 else if (!new_var)
573 {
574 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
575 && !DECL_IGNORED_P (old_var)
576 && nonlocalized_list)
577 vec_safe_push (*nonlocalized_list, old_var);
578 }
579 else
580 {
581 gcc_assert (DECL_P (new_var));
582 DECL_CHAIN (new_var) = new_decls;
583 new_decls = new_var;
584
585 /* Also copy value-expressions. */
586 if (TREE_CODE (new_var) == VAR_DECL
587 && DECL_HAS_VALUE_EXPR_P (new_var))
588 {
589 tree tem = DECL_VALUE_EXPR (new_var);
590 bool old_regimplify = id->regimplify;
591 id->remapping_type_depth++;
592 walk_tree (&tem, copy_tree_body_r, id, NULL);
593 id->remapping_type_depth--;
594 id->regimplify = old_regimplify;
595 SET_DECL_VALUE_EXPR (new_var, tem);
596 }
597 }
598 }
599
600 return nreverse (new_decls);
601 }
602
603 /* Copy the BLOCK to contain remapped versions of the variables
604 therein. And hook the new block into the block-tree. */
605
606 static void
607 remap_block (tree *block, copy_body_data *id)
608 {
609 tree old_block;
610 tree new_block;
611
612 /* Make the new block. */
613 old_block = *block;
614 new_block = make_node (BLOCK);
615 TREE_USED (new_block) = TREE_USED (old_block);
616 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
617 BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
618 BLOCK_NONLOCALIZED_VARS (new_block)
619 = vec_safe_copy (BLOCK_NONLOCALIZED_VARS (old_block));
620 *block = new_block;
621
622 /* Remap its variables. */
623 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block),
624 &BLOCK_NONLOCALIZED_VARS (new_block),
625 id);
626
627 if (id->transform_lang_insert_block)
628 id->transform_lang_insert_block (new_block);
629
630 /* Remember the remapped block. */
631 insert_decl_map (id, old_block, new_block);
632 }
633
634 /* Copy the whole block tree and root it in id->block. */
635 static tree
636 remap_blocks (tree block, copy_body_data *id)
637 {
638 tree t;
639 tree new_tree = block;
640
641 if (!block)
642 return NULL;
643
644 remap_block (&new_tree, id);
645 gcc_assert (new_tree != block);
646 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
647 prepend_lexical_block (new_tree, remap_blocks (t, id));
648 /* Blocks are in arbitrary order, but make things slightly prettier and do
649 not swap order when producing a copy. */
650 BLOCK_SUBBLOCKS (new_tree) = blocks_nreverse (BLOCK_SUBBLOCKS (new_tree));
651 return new_tree;
652 }
653
654 static void
655 copy_statement_list (tree *tp)
656 {
657 tree_stmt_iterator oi, ni;
658 tree new_tree;
659
660 new_tree = alloc_stmt_list ();
661 ni = tsi_start (new_tree);
662 oi = tsi_start (*tp);
663 TREE_TYPE (new_tree) = TREE_TYPE (*tp);
664 *tp = new_tree;
665
666 for (; !tsi_end_p (oi); tsi_next (&oi))
667 {
668 tree stmt = tsi_stmt (oi);
669 if (TREE_CODE (stmt) == STATEMENT_LIST)
670 /* This copy is not redundant; tsi_link_after will smash this
671 STATEMENT_LIST into the end of the one we're building, and we
672 don't want to do that with the original. */
673 copy_statement_list (&stmt);
674 tsi_link_after (&ni, stmt, TSI_CONTINUE_LINKING);
675 }
676 }
677
678 static void
679 copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
680 {
681 tree block = BIND_EXPR_BLOCK (*tp);
682 /* Copy (and replace) the statement. */
683 copy_tree_r (tp, walk_subtrees, NULL);
684 if (block)
685 {
686 remap_block (&block, id);
687 BIND_EXPR_BLOCK (*tp) = block;
688 }
689
690 if (BIND_EXPR_VARS (*tp))
691 /* This will remap a lot of the same decls again, but this should be
692 harmless. */
693 BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), NULL, id);
694 }
695
696
697 /* Create a new gimple_seq by remapping all the statements in BODY
698 using the inlining information in ID. */
699
700 static gimple_seq
701 remap_gimple_seq (gimple_seq body, copy_body_data *id)
702 {
703 gimple_stmt_iterator si;
704 gimple_seq new_body = NULL;
705
706 for (si = gsi_start (body); !gsi_end_p (si); gsi_next (&si))
707 {
708 gimple new_stmt = remap_gimple_stmt (gsi_stmt (si), id);
709 gimple_seq_add_stmt (&new_body, new_stmt);
710 }
711
712 return new_body;
713 }
714
715
716 /* Copy a GIMPLE_BIND statement STMT, remapping all the symbols in its
717 block using the mapping information in ID. */
718
719 static gimple
720 copy_gimple_bind (gimple stmt, copy_body_data *id)
721 {
722 gimple new_bind;
723 tree new_block, new_vars;
724 gimple_seq body, new_body;
725
726 /* Copy the statement. Note that we purposely don't use copy_stmt
727 here because we need to remap statements as we copy. */
728 body = gimple_bind_body (stmt);
729 new_body = remap_gimple_seq (body, id);
730
731 new_block = gimple_bind_block (stmt);
732 if (new_block)
733 remap_block (&new_block, id);
734
735 /* This will remap a lot of the same decls again, but this should be
736 harmless. */
737 new_vars = gimple_bind_vars (stmt);
738 if (new_vars)
739 new_vars = remap_decls (new_vars, NULL, id);
740
741 new_bind = gimple_build_bind (new_vars, new_body, new_block);
742
743 return new_bind;
744 }
745
746
747 /* Remap the GIMPLE operand pointed to by *TP. DATA is really a
748 'struct walk_stmt_info *'. DATA->INFO is a 'copy_body_data *'.
749 WALK_SUBTREES is used to indicate walk_gimple_op whether to keep
750 recursing into the children nodes of *TP. */
751
752 static tree
753 remap_gimple_op_r (tree *tp, int *walk_subtrees, void *data)
754 {
755 struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
756 copy_body_data *id = (copy_body_data *) wi_p->info;
757 tree fn = id->src_fn;
758
759 if (TREE_CODE (*tp) == SSA_NAME)
760 {
761 *tp = remap_ssa_name (*tp, id);
762 *walk_subtrees = 0;
763 return NULL;
764 }
765 else if (auto_var_in_fn_p (*tp, fn))
766 {
767 /* Local variables and labels need to be replaced by equivalent
768 variables. We don't want to copy static variables; there's
769 only one of those, no matter how many times we inline the
770 containing function. Similarly for globals from an outer
771 function. */
772 tree new_decl;
773
774 /* Remap the declaration. */
775 new_decl = remap_decl (*tp, id);
776 gcc_assert (new_decl);
777 /* Replace this variable with the copy. */
778 STRIP_TYPE_NOPS (new_decl);
779 /* ??? The C++ frontend uses void * pointer zero to initialize
780 any other type. This confuses the middle-end type verification.
781 As cloned bodies do not go through gimplification again the fixup
782 there doesn't trigger. */
783 if (TREE_CODE (new_decl) == INTEGER_CST
784 && !useless_type_conversion_p (TREE_TYPE (*tp), TREE_TYPE (new_decl)))
785 new_decl = fold_convert (TREE_TYPE (*tp), new_decl);
786 *tp = new_decl;
787 *walk_subtrees = 0;
788 }
789 else if (TREE_CODE (*tp) == STATEMENT_LIST)
790 gcc_unreachable ();
791 else if (TREE_CODE (*tp) == SAVE_EXPR)
792 gcc_unreachable ();
793 else if (TREE_CODE (*tp) == LABEL_DECL
794 && (!DECL_CONTEXT (*tp)
795 || decl_function_context (*tp) == id->src_fn))
796 /* These may need to be remapped for EH handling. */
797 *tp = remap_decl (*tp, id);
798 else if (TREE_CODE (*tp) == FIELD_DECL)
799 {
800 /* If the enclosing record type is variably_modified_type_p, the field
801 has already been remapped. Otherwise, it need not be. */
802 tree *n = (tree *) pointer_map_contains (id->decl_map, *tp);
803 if (n)
804 *tp = *n;
805 *walk_subtrees = 0;
806 }
807 else if (TYPE_P (*tp))
808 /* Types may need remapping as well. */
809 *tp = remap_type (*tp, id);
810 else if (CONSTANT_CLASS_P (*tp))
811 {
812 /* If this is a constant, we have to copy the node iff the type
813 will be remapped. copy_tree_r will not copy a constant. */
814 tree new_type = remap_type (TREE_TYPE (*tp), id);
815
816 if (new_type == TREE_TYPE (*tp))
817 *walk_subtrees = 0;
818
819 else if (TREE_CODE (*tp) == INTEGER_CST)
820 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
821 TREE_INT_CST_HIGH (*tp));
822 else
823 {
824 *tp = copy_node (*tp);
825 TREE_TYPE (*tp) = new_type;
826 }
827 }
828 else
829 {
830 /* Otherwise, just copy the node. Note that copy_tree_r already
831 knows not to copy VAR_DECLs, etc., so this is safe. */
832
833 if (TREE_CODE (*tp) == MEM_REF)
834 {
835 tree ptr = TREE_OPERAND (*tp, 0);
836 tree type = remap_type (TREE_TYPE (*tp), id);
837 tree old = *tp;
838
839 /* We need to re-canonicalize MEM_REFs from inline substitutions
840 that can happen when a pointer argument is an ADDR_EXPR.
841 Recurse here manually to allow that. */
842 walk_tree (&ptr, remap_gimple_op_r, data, NULL);
843 *tp = fold_build2 (MEM_REF, type,
844 ptr, TREE_OPERAND (*tp, 1));
845 TREE_THIS_NOTRAP (*tp) = TREE_THIS_NOTRAP (old);
846 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
847 TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
848 TREE_NO_WARNING (*tp) = TREE_NO_WARNING (old);
849 *walk_subtrees = 0;
850 return NULL;
851 }
852
853 /* Here is the "usual case". Copy this tree node, and then
854 tweak some special cases. */
855 copy_tree_r (tp, walk_subtrees, NULL);
856
857 if (TREE_CODE (*tp) != OMP_CLAUSE)
858 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
859
860 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
861 {
862 /* The copied TARGET_EXPR has never been expanded, even if the
863 original node was expanded already. */
864 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
865 TREE_OPERAND (*tp, 3) = NULL_TREE;
866 }
867 else if (TREE_CODE (*tp) == ADDR_EXPR)
868 {
869 /* Variable substitution need not be simple. In particular,
870 the MEM_REF substitution above. Make sure that
871 TREE_CONSTANT and friends are up-to-date. */
872 int invariant = is_gimple_min_invariant (*tp);
873 walk_tree (&TREE_OPERAND (*tp, 0), remap_gimple_op_r, data, NULL);
874 recompute_tree_invariant_for_addr_expr (*tp);
875
876 /* If this used to be invariant, but is not any longer,
877 then regimplification is probably needed. */
878 if (invariant && !is_gimple_min_invariant (*tp))
879 id->regimplify = true;
880
881 *walk_subtrees = 0;
882 }
883 }
884
885 /* Update the TREE_BLOCK for the cloned expr. */
886 if (EXPR_P (*tp))
887 {
888 tree new_block = id->remapping_type_depth == 0 ? id->block : NULL;
889 tree old_block = TREE_BLOCK (*tp);
890 if (old_block)
891 {
892 tree *n;
893 n = (tree *) pointer_map_contains (id->decl_map,
894 TREE_BLOCK (*tp));
895 if (n)
896 new_block = *n;
897 }
898 TREE_SET_BLOCK (*tp, new_block);
899 }
900
901 /* Keep iterating. */
902 return NULL_TREE;
903 }
904
905
906 /* Called from copy_body_id via walk_tree. DATA is really a
907 `copy_body_data *'. */
908
909 tree
910 copy_tree_body_r (tree *tp, int *walk_subtrees, void *data)
911 {
912 copy_body_data *id = (copy_body_data *) data;
913 tree fn = id->src_fn;
914 tree new_block;
915
916 /* Begin by recognizing trees that we'll completely rewrite for the
917 inlining context. Our output for these trees is completely
918 different from out input (e.g. RETURN_EXPR is deleted, and morphs
919 into an edge). Further down, we'll handle trees that get
920 duplicated and/or tweaked. */
921
922 /* When requested, RETURN_EXPRs should be transformed to just the
923 contained MODIFY_EXPR. The branch semantics of the return will
924 be handled elsewhere by manipulating the CFG rather than a statement. */
925 if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
926 {
927 tree assignment = TREE_OPERAND (*tp, 0);
928
929 /* If we're returning something, just turn that into an
930 assignment into the equivalent of the original RESULT_DECL.
931 If the "assignment" is just the result decl, the result
932 decl has already been set (e.g. a recent "foo (&result_decl,
933 ...)"); just toss the entire RETURN_EXPR. */
934 if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
935 {
936 /* Replace the RETURN_EXPR with (a copy of) the
937 MODIFY_EXPR hanging underneath. */
938 *tp = copy_node (assignment);
939 }
940 else /* Else the RETURN_EXPR returns no value. */
941 {
942 *tp = NULL;
943 return (tree) (void *)1;
944 }
945 }
946 else if (TREE_CODE (*tp) == SSA_NAME)
947 {
948 *tp = remap_ssa_name (*tp, id);
949 *walk_subtrees = 0;
950 return NULL;
951 }
952
953 /* Local variables and labels need to be replaced by equivalent
954 variables. We don't want to copy static variables; there's only
955 one of those, no matter how many times we inline the containing
956 function. Similarly for globals from an outer function. */
957 else if (auto_var_in_fn_p (*tp, fn))
958 {
959 tree new_decl;
960
961 /* Remap the declaration. */
962 new_decl = remap_decl (*tp, id);
963 gcc_assert (new_decl);
964 /* Replace this variable with the copy. */
965 STRIP_TYPE_NOPS (new_decl);
966 *tp = new_decl;
967 *walk_subtrees = 0;
968 }
969 else if (TREE_CODE (*tp) == STATEMENT_LIST)
970 copy_statement_list (tp);
971 else if (TREE_CODE (*tp) == SAVE_EXPR
972 || TREE_CODE (*tp) == TARGET_EXPR)
973 remap_save_expr (tp, id->decl_map, walk_subtrees);
974 else if (TREE_CODE (*tp) == LABEL_DECL
975 && (! DECL_CONTEXT (*tp)
976 || decl_function_context (*tp) == id->src_fn))
977 /* These may need to be remapped for EH handling. */
978 *tp = remap_decl (*tp, id);
979 else if (TREE_CODE (*tp) == BIND_EXPR)
980 copy_bind_expr (tp, walk_subtrees, id);
981 /* Types may need remapping as well. */
982 else if (TYPE_P (*tp))
983 *tp = remap_type (*tp, id);
984
985 /* If this is a constant, we have to copy the node iff the type will be
986 remapped. copy_tree_r will not copy a constant. */
987 else if (CONSTANT_CLASS_P (*tp))
988 {
989 tree new_type = remap_type (TREE_TYPE (*tp), id);
990
991 if (new_type == TREE_TYPE (*tp))
992 *walk_subtrees = 0;
993
994 else if (TREE_CODE (*tp) == INTEGER_CST)
995 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
996 TREE_INT_CST_HIGH (*tp));
997 else
998 {
999 *tp = copy_node (*tp);
1000 TREE_TYPE (*tp) = new_type;
1001 }
1002 }
1003
1004 /* Otherwise, just copy the node. Note that copy_tree_r already
1005 knows not to copy VAR_DECLs, etc., so this is safe. */
1006 else
1007 {
1008 /* Here we handle trees that are not completely rewritten.
1009 First we detect some inlining-induced bogosities for
1010 discarding. */
1011 if (TREE_CODE (*tp) == MODIFY_EXPR
1012 && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
1013 && (auto_var_in_fn_p (TREE_OPERAND (*tp, 0), fn)))
1014 {
1015 /* Some assignments VAR = VAR; don't generate any rtl code
1016 and thus don't count as variable modification. Avoid
1017 keeping bogosities like 0 = 0. */
1018 tree decl = TREE_OPERAND (*tp, 0), value;
1019 tree *n;
1020
1021 n = (tree *) pointer_map_contains (id->decl_map, decl);
1022 if (n)
1023 {
1024 value = *n;
1025 STRIP_TYPE_NOPS (value);
1026 if (TREE_CONSTANT (value) || TREE_READONLY (value))
1027 {
1028 *tp = build_empty_stmt (EXPR_LOCATION (*tp));
1029 return copy_tree_body_r (tp, walk_subtrees, data);
1030 }
1031 }
1032 }
1033 else if (TREE_CODE (*tp) == INDIRECT_REF)
1034 {
1035 /* Get rid of *& from inline substitutions that can happen when a
1036 pointer argument is an ADDR_EXPR. */
1037 tree decl = TREE_OPERAND (*tp, 0);
1038 tree *n;
1039
1040 n = (tree *) pointer_map_contains (id->decl_map, decl);
1041 if (n)
1042 {
1043 tree new_tree;
1044 tree old;
1045 /* If we happen to get an ADDR_EXPR in n->value, strip
1046 it manually here as we'll eventually get ADDR_EXPRs
1047 which lie about their types pointed to. In this case
1048 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
1049 but we absolutely rely on that. As fold_indirect_ref
1050 does other useful transformations, try that first, though. */
1051 tree type = TREE_TYPE (TREE_TYPE (*n));
1052 if (id->do_not_unshare)
1053 new_tree = *n;
1054 else
1055 new_tree = unshare_expr (*n);
1056 old = *tp;
1057 *tp = gimple_fold_indirect_ref (new_tree);
1058 if (! *tp)
1059 {
1060 if (TREE_CODE (new_tree) == ADDR_EXPR)
1061 {
1062 *tp = fold_indirect_ref_1 (EXPR_LOCATION (new_tree),
1063 type, new_tree);
1064 /* ??? We should either assert here or build
1065 a VIEW_CONVERT_EXPR instead of blindly leaking
1066 incompatible types to our IL. */
1067 if (! *tp)
1068 *tp = TREE_OPERAND (new_tree, 0);
1069 }
1070 else
1071 {
1072 *tp = build1 (INDIRECT_REF, type, new_tree);
1073 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
1074 TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
1075 TREE_READONLY (*tp) = TREE_READONLY (old);
1076 TREE_THIS_NOTRAP (*tp) = TREE_THIS_NOTRAP (old);
1077 }
1078 }
1079 *walk_subtrees = 0;
1080 return NULL;
1081 }
1082 }
1083 else if (TREE_CODE (*tp) == MEM_REF)
1084 {
1085 /* We need to re-canonicalize MEM_REFs from inline substitutions
1086 that can happen when a pointer argument is an ADDR_EXPR. */
1087 tree decl = TREE_OPERAND (*tp, 0);
1088 tree *n;
1089
1090 n = (tree *) pointer_map_contains (id->decl_map, decl);
1091 if (n)
1092 {
1093 tree old = *tp;
1094 *tp = fold_build2 (MEM_REF, TREE_TYPE (*tp),
1095 unshare_expr (*n), TREE_OPERAND (*tp, 1));
1096 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
1097 TREE_NO_WARNING (*tp) = TREE_NO_WARNING (old);
1098 *walk_subtrees = 0;
1099 return NULL;
1100 }
1101 }
1102
1103 /* Here is the "usual case". Copy this tree node, and then
1104 tweak some special cases. */
1105 copy_tree_r (tp, walk_subtrees, NULL);
1106
1107 /* If EXPR has block defined, map it to newly constructed block.
1108 When inlining we want EXPRs without block appear in the block
1109 of function call if we are not remapping a type. */
1110 if (EXPR_P (*tp))
1111 {
1112 new_block = id->remapping_type_depth == 0 ? id->block : NULL;
1113 if (TREE_BLOCK (*tp))
1114 {
1115 tree *n;
1116 n = (tree *) pointer_map_contains (id->decl_map,
1117 TREE_BLOCK (*tp));
1118 if (n)
1119 new_block = *n;
1120 }
1121 TREE_SET_BLOCK (*tp, new_block);
1122 }
1123
1124 if (TREE_CODE (*tp) != OMP_CLAUSE)
1125 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
1126
1127 /* The copied TARGET_EXPR has never been expanded, even if the
1128 original node was expanded already. */
1129 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
1130 {
1131 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
1132 TREE_OPERAND (*tp, 3) = NULL_TREE;
1133 }
1134
1135 /* Variable substitution need not be simple. In particular, the
1136 INDIRECT_REF substitution above. Make sure that TREE_CONSTANT
1137 and friends are up-to-date. */
1138 else if (TREE_CODE (*tp) == ADDR_EXPR)
1139 {
1140 int invariant = is_gimple_min_invariant (*tp);
1141 walk_tree (&TREE_OPERAND (*tp, 0), copy_tree_body_r, id, NULL);
1142
1143 /* Handle the case where we substituted an INDIRECT_REF
1144 into the operand of the ADDR_EXPR. */
1145 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
1146 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
1147 else
1148 recompute_tree_invariant_for_addr_expr (*tp);
1149
1150 /* If this used to be invariant, but is not any longer,
1151 then regimplification is probably needed. */
1152 if (invariant && !is_gimple_min_invariant (*tp))
1153 id->regimplify = true;
1154
1155 *walk_subtrees = 0;
1156 }
1157 }
1158
1159 /* Keep iterating. */
1160 return NULL_TREE;
1161 }
1162
1163 /* Helper for remap_gimple_stmt. Given an EH region number for the
1164 source function, map that to the duplicate EH region number in
1165 the destination function. */
1166
1167 static int
1168 remap_eh_region_nr (int old_nr, copy_body_data *id)
1169 {
1170 eh_region old_r, new_r;
1171 void **slot;
1172
1173 old_r = get_eh_region_from_number_fn (id->src_cfun, old_nr);
1174 slot = pointer_map_contains (id->eh_map, old_r);
1175 new_r = (eh_region) *slot;
1176
1177 return new_r->index;
1178 }
1179
1180 /* Similar, but operate on INTEGER_CSTs. */
1181
1182 static tree
1183 remap_eh_region_tree_nr (tree old_t_nr, copy_body_data *id)
1184 {
1185 int old_nr, new_nr;
1186
1187 old_nr = tree_low_cst (old_t_nr, 0);
1188 new_nr = remap_eh_region_nr (old_nr, id);
1189
1190 return build_int_cst (integer_type_node, new_nr);
1191 }
1192
1193 /* Helper for copy_bb. Remap statement STMT using the inlining
1194 information in ID. Return the new statement copy. */
1195
1196 static gimple
1197 remap_gimple_stmt (gimple stmt, copy_body_data *id)
1198 {
1199 gimple copy = NULL;
1200 struct walk_stmt_info wi;
1201 bool skip_first = false;
1202
1203 /* Begin by recognizing trees that we'll completely rewrite for the
1204 inlining context. Our output for these trees is completely
1205 different from out input (e.g. RETURN_EXPR is deleted, and morphs
1206 into an edge). Further down, we'll handle trees that get
1207 duplicated and/or tweaked. */
1208
1209 /* When requested, GIMPLE_RETURNs should be transformed to just the
1210 contained GIMPLE_ASSIGN. The branch semantics of the return will
1211 be handled elsewhere by manipulating the CFG rather than the
1212 statement. */
1213 if (gimple_code (stmt) == GIMPLE_RETURN && id->transform_return_to_modify)
1214 {
1215 tree retval = gimple_return_retval (stmt);
1216
1217 /* If we're returning something, just turn that into an
1218 assignment into the equivalent of the original RESULT_DECL.
1219 If RETVAL is just the result decl, the result decl has
1220 already been set (e.g. a recent "foo (&result_decl, ...)");
1221 just toss the entire GIMPLE_RETURN. */
1222 if (retval
1223 && (TREE_CODE (retval) != RESULT_DECL
1224 && (TREE_CODE (retval) != SSA_NAME
1225 || ! SSA_NAME_VAR (retval)
1226 || TREE_CODE (SSA_NAME_VAR (retval)) != RESULT_DECL)))
1227 {
1228 copy = gimple_build_assign (id->retvar, retval);
1229 /* id->retvar is already substituted. Skip it on later remapping. */
1230 skip_first = true;
1231 }
1232 else
1233 return gimple_build_nop ();
1234 }
1235 else if (gimple_has_substatements (stmt))
1236 {
1237 gimple_seq s1, s2;
1238
1239 /* When cloning bodies from the C++ front end, we will be handed bodies
1240 in High GIMPLE form. Handle here all the High GIMPLE statements that
1241 have embedded statements. */
1242 switch (gimple_code (stmt))
1243 {
1244 case GIMPLE_BIND:
1245 copy = copy_gimple_bind (stmt, id);
1246 break;
1247
1248 case GIMPLE_CATCH:
1249 s1 = remap_gimple_seq (gimple_catch_handler (stmt), id);
1250 copy = gimple_build_catch (gimple_catch_types (stmt), s1);
1251 break;
1252
1253 case GIMPLE_EH_FILTER:
1254 s1 = remap_gimple_seq (gimple_eh_filter_failure (stmt), id);
1255 copy = gimple_build_eh_filter (gimple_eh_filter_types (stmt), s1);
1256 break;
1257
1258 case GIMPLE_TRY:
1259 s1 = remap_gimple_seq (gimple_try_eval (stmt), id);
1260 s2 = remap_gimple_seq (gimple_try_cleanup (stmt), id);
1261 copy = gimple_build_try (s1, s2, gimple_try_kind (stmt));
1262 break;
1263
1264 case GIMPLE_WITH_CLEANUP_EXPR:
1265 s1 = remap_gimple_seq (gimple_wce_cleanup (stmt), id);
1266 copy = gimple_build_wce (s1);
1267 break;
1268
1269 case GIMPLE_OMP_PARALLEL:
1270 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1271 copy = gimple_build_omp_parallel
1272 (s1,
1273 gimple_omp_parallel_clauses (stmt),
1274 gimple_omp_parallel_child_fn (stmt),
1275 gimple_omp_parallel_data_arg (stmt));
1276 break;
1277
1278 case GIMPLE_OMP_TASK:
1279 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1280 copy = gimple_build_omp_task
1281 (s1,
1282 gimple_omp_task_clauses (stmt),
1283 gimple_omp_task_child_fn (stmt),
1284 gimple_omp_task_data_arg (stmt),
1285 gimple_omp_task_copy_fn (stmt),
1286 gimple_omp_task_arg_size (stmt),
1287 gimple_omp_task_arg_align (stmt));
1288 break;
1289
1290 case GIMPLE_OMP_FOR:
1291 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1292 s2 = remap_gimple_seq (gimple_omp_for_pre_body (stmt), id);
1293 copy = gimple_build_omp_for (s1, gimple_omp_for_clauses (stmt),
1294 gimple_omp_for_collapse (stmt), s2);
1295 {
1296 size_t i;
1297 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1298 {
1299 gimple_omp_for_set_index (copy, i,
1300 gimple_omp_for_index (stmt, i));
1301 gimple_omp_for_set_initial (copy, i,
1302 gimple_omp_for_initial (stmt, i));
1303 gimple_omp_for_set_final (copy, i,
1304 gimple_omp_for_final (stmt, i));
1305 gimple_omp_for_set_incr (copy, i,
1306 gimple_omp_for_incr (stmt, i));
1307 gimple_omp_for_set_cond (copy, i,
1308 gimple_omp_for_cond (stmt, i));
1309 }
1310 }
1311 break;
1312
1313 case GIMPLE_OMP_MASTER:
1314 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1315 copy = gimple_build_omp_master (s1);
1316 break;
1317
1318 case GIMPLE_OMP_ORDERED:
1319 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1320 copy = gimple_build_omp_ordered (s1);
1321 break;
1322
1323 case GIMPLE_OMP_SECTION:
1324 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1325 copy = gimple_build_omp_section (s1);
1326 break;
1327
1328 case GIMPLE_OMP_SECTIONS:
1329 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1330 copy = gimple_build_omp_sections
1331 (s1, gimple_omp_sections_clauses (stmt));
1332 break;
1333
1334 case GIMPLE_OMP_SINGLE:
1335 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1336 copy = gimple_build_omp_single
1337 (s1, gimple_omp_single_clauses (stmt));
1338 break;
1339
1340 case GIMPLE_OMP_CRITICAL:
1341 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1342 copy
1343 = gimple_build_omp_critical (s1, gimple_omp_critical_name (stmt));
1344 break;
1345
1346 case GIMPLE_TRANSACTION:
1347 s1 = remap_gimple_seq (gimple_transaction_body (stmt), id);
1348 copy = gimple_build_transaction (s1, gimple_transaction_label (stmt));
1349 gimple_transaction_set_subcode (copy, gimple_transaction_subcode (stmt));
1350 break;
1351
1352 default:
1353 gcc_unreachable ();
1354 }
1355 }
1356 else
1357 {
1358 if (gimple_assign_copy_p (stmt)
1359 && gimple_assign_lhs (stmt) == gimple_assign_rhs1 (stmt)
1360 && auto_var_in_fn_p (gimple_assign_lhs (stmt), id->src_fn))
1361 {
1362 /* Here we handle statements that are not completely rewritten.
1363 First we detect some inlining-induced bogosities for
1364 discarding. */
1365
1366 /* Some assignments VAR = VAR; don't generate any rtl code
1367 and thus don't count as variable modification. Avoid
1368 keeping bogosities like 0 = 0. */
1369 tree decl = gimple_assign_lhs (stmt), value;
1370 tree *n;
1371
1372 n = (tree *) pointer_map_contains (id->decl_map, decl);
1373 if (n)
1374 {
1375 value = *n;
1376 STRIP_TYPE_NOPS (value);
1377 if (TREE_CONSTANT (value) || TREE_READONLY (value))
1378 return gimple_build_nop ();
1379 }
1380 }
1381
1382 if (gimple_debug_bind_p (stmt))
1383 {
1384 copy = gimple_build_debug_bind (gimple_debug_bind_get_var (stmt),
1385 gimple_debug_bind_get_value (stmt),
1386 stmt);
1387 id->debug_stmts.safe_push (copy);
1388 return copy;
1389 }
1390 if (gimple_debug_source_bind_p (stmt))
1391 {
1392 copy = gimple_build_debug_source_bind
1393 (gimple_debug_source_bind_get_var (stmt),
1394 gimple_debug_source_bind_get_value (stmt), stmt);
1395 id->debug_stmts.safe_push (copy);
1396 return copy;
1397 }
1398
1399 /* Create a new deep copy of the statement. */
1400 copy = gimple_copy (stmt);
1401
1402 /* Remap the region numbers for __builtin_eh_{pointer,filter},
1403 RESX and EH_DISPATCH. */
1404 if (id->eh_map)
1405 switch (gimple_code (copy))
1406 {
1407 case GIMPLE_CALL:
1408 {
1409 tree r, fndecl = gimple_call_fndecl (copy);
1410 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
1411 switch (DECL_FUNCTION_CODE (fndecl))
1412 {
1413 case BUILT_IN_EH_COPY_VALUES:
1414 r = gimple_call_arg (copy, 1);
1415 r = remap_eh_region_tree_nr (r, id);
1416 gimple_call_set_arg (copy, 1, r);
1417 /* FALLTHRU */
1418
1419 case BUILT_IN_EH_POINTER:
1420 case BUILT_IN_EH_FILTER:
1421 r = gimple_call_arg (copy, 0);
1422 r = remap_eh_region_tree_nr (r, id);
1423 gimple_call_set_arg (copy, 0, r);
1424 break;
1425
1426 default:
1427 break;
1428 }
1429
1430 /* Reset alias info if we didn't apply measures to
1431 keep it valid over inlining by setting DECL_PT_UID. */
1432 if (!id->src_cfun->gimple_df
1433 || !id->src_cfun->gimple_df->ipa_pta)
1434 gimple_call_reset_alias_info (copy);
1435 }
1436 break;
1437
1438 case GIMPLE_RESX:
1439 {
1440 int r = gimple_resx_region (copy);
1441 r = remap_eh_region_nr (r, id);
1442 gimple_resx_set_region (copy, r);
1443 }
1444 break;
1445
1446 case GIMPLE_EH_DISPATCH:
1447 {
1448 int r = gimple_eh_dispatch_region (copy);
1449 r = remap_eh_region_nr (r, id);
1450 gimple_eh_dispatch_set_region (copy, r);
1451 }
1452 break;
1453
1454 default:
1455 break;
1456 }
1457 }
1458
1459 /* If STMT has a block defined, map it to the newly constructed
1460 block. */
1461 if (gimple_block (copy))
1462 {
1463 tree *n;
1464 n = (tree *) pointer_map_contains (id->decl_map, gimple_block (copy));
1465 gcc_assert (n);
1466 gimple_set_block (copy, *n);
1467 }
1468
1469 if (gimple_debug_bind_p (copy) || gimple_debug_source_bind_p (copy))
1470 return copy;
1471
1472 /* Remap all the operands in COPY. */
1473 memset (&wi, 0, sizeof (wi));
1474 wi.info = id;
1475 if (skip_first)
1476 walk_tree (gimple_op_ptr (copy, 1), remap_gimple_op_r, &wi, NULL);
1477 else
1478 walk_gimple_op (copy, remap_gimple_op_r, &wi);
1479
1480 /* Clear the copied virtual operands. We are not remapping them here
1481 but are going to recreate them from scratch. */
1482 if (gimple_has_mem_ops (copy))
1483 {
1484 gimple_set_vdef (copy, NULL_TREE);
1485 gimple_set_vuse (copy, NULL_TREE);
1486 }
1487
1488 return copy;
1489 }
1490
1491
1492 /* Copy basic block, scale profile accordingly. Edges will be taken care of
1493 later */
1494
1495 static basic_block
1496 copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
1497 gcov_type count_scale)
1498 {
1499 gimple_stmt_iterator gsi, copy_gsi, seq_gsi;
1500 basic_block copy_basic_block;
1501 tree decl;
1502 gcov_type freq;
1503 basic_block prev;
1504
1505 /* Search for previous copied basic block. */
1506 prev = bb->prev_bb;
1507 while (!prev->aux)
1508 prev = prev->prev_bb;
1509
1510 /* create_basic_block() will append every new block to
1511 basic_block_info automatically. */
1512 copy_basic_block = create_basic_block (NULL, (void *) 0,
1513 (basic_block) prev->aux);
1514 copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
1515
1516 /* We are going to rebuild frequencies from scratch. These values
1517 have just small importance to drive canonicalize_loop_headers. */
1518 freq = ((gcov_type)bb->frequency * frequency_scale / REG_BR_PROB_BASE);
1519
1520 /* We recompute frequencies after inlining, so this is quite safe. */
1521 if (freq > BB_FREQ_MAX)
1522 freq = BB_FREQ_MAX;
1523 copy_basic_block->frequency = freq;
1524
1525 copy_gsi = gsi_start_bb (copy_basic_block);
1526
1527 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1528 {
1529 gimple stmt = gsi_stmt (gsi);
1530 gimple orig_stmt = stmt;
1531
1532 id->regimplify = false;
1533 stmt = remap_gimple_stmt (stmt, id);
1534 if (gimple_nop_p (stmt))
1535 continue;
1536
1537 gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun, orig_stmt);
1538 seq_gsi = copy_gsi;
1539
1540 /* With return slot optimization we can end up with
1541 non-gimple (foo *)&this->m, fix that here. */
1542 if (is_gimple_assign (stmt)
1543 && gimple_assign_rhs_code (stmt) == NOP_EXPR
1544 && !is_gimple_val (gimple_assign_rhs1 (stmt)))
1545 {
1546 tree new_rhs;
1547 new_rhs = force_gimple_operand_gsi (&seq_gsi,
1548 gimple_assign_rhs1 (stmt),
1549 true, NULL, false,
1550 GSI_CONTINUE_LINKING);
1551 gimple_assign_set_rhs1 (stmt, new_rhs);
1552 id->regimplify = false;
1553 }
1554
1555 gsi_insert_after (&seq_gsi, stmt, GSI_NEW_STMT);
1556
1557 if (id->regimplify)
1558 gimple_regimplify_operands (stmt, &seq_gsi);
1559
1560 /* If copy_basic_block has been empty at the start of this iteration,
1561 call gsi_start_bb again to get at the newly added statements. */
1562 if (gsi_end_p (copy_gsi))
1563 copy_gsi = gsi_start_bb (copy_basic_block);
1564 else
1565 gsi_next (&copy_gsi);
1566
1567 /* Process the new statement. The call to gimple_regimplify_operands
1568 possibly turned the statement into multiple statements, we
1569 need to process all of them. */
1570 do
1571 {
1572 tree fn;
1573
1574 stmt = gsi_stmt (copy_gsi);
1575 if (is_gimple_call (stmt)
1576 && gimple_call_va_arg_pack_p (stmt)
1577 && id->gimple_call)
1578 {
1579 /* __builtin_va_arg_pack () should be replaced by
1580 all arguments corresponding to ... in the caller. */
1581 tree p;
1582 gimple new_call;
1583 vec<tree> argarray;
1584 size_t nargs = gimple_call_num_args (id->gimple_call);
1585 size_t n;
1586
1587 for (p = DECL_ARGUMENTS (id->src_fn); p; p = DECL_CHAIN (p))
1588 nargs--;
1589
1590 /* Create the new array of arguments. */
1591 n = nargs + gimple_call_num_args (stmt);
1592 argarray.create (n);
1593 argarray.safe_grow_cleared (n);
1594
1595 /* Copy all the arguments before '...' */
1596 memcpy (argarray.address (),
1597 gimple_call_arg_ptr (stmt, 0),
1598 gimple_call_num_args (stmt) * sizeof (tree));
1599
1600 /* Append the arguments passed in '...' */
1601 memcpy (argarray.address () + gimple_call_num_args (stmt),
1602 gimple_call_arg_ptr (id->gimple_call, 0)
1603 + (gimple_call_num_args (id->gimple_call) - nargs),
1604 nargs * sizeof (tree));
1605
1606 new_call = gimple_build_call_vec (gimple_call_fn (stmt),
1607 argarray);
1608
1609 argarray.release ();
1610
1611 /* Copy all GIMPLE_CALL flags, location and block, except
1612 GF_CALL_VA_ARG_PACK. */
1613 gimple_call_copy_flags (new_call, stmt);
1614 gimple_call_set_va_arg_pack (new_call, false);
1615 gimple_set_location (new_call, gimple_location (stmt));
1616 gimple_set_block (new_call, gimple_block (stmt));
1617 gimple_call_set_lhs (new_call, gimple_call_lhs (stmt));
1618
1619 gsi_replace (&copy_gsi, new_call, false);
1620 stmt = new_call;
1621 }
1622 else if (is_gimple_call (stmt)
1623 && id->gimple_call
1624 && (decl = gimple_call_fndecl (stmt))
1625 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1626 && DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_ARG_PACK_LEN)
1627 {
1628 /* __builtin_va_arg_pack_len () should be replaced by
1629 the number of anonymous arguments. */
1630 size_t nargs = gimple_call_num_args (id->gimple_call);
1631 tree count, p;
1632 gimple new_stmt;
1633
1634 for (p = DECL_ARGUMENTS (id->src_fn); p; p = DECL_CHAIN (p))
1635 nargs--;
1636
1637 count = build_int_cst (integer_type_node, nargs);
1638 new_stmt = gimple_build_assign (gimple_call_lhs (stmt), count);
1639 gsi_replace (&copy_gsi, new_stmt, false);
1640 stmt = new_stmt;
1641 }
1642
1643 /* Statements produced by inlining can be unfolded, especially
1644 when we constant propagated some operands. We can't fold
1645 them right now for two reasons:
1646 1) folding require SSA_NAME_DEF_STMTs to be correct
1647 2) we can't change function calls to builtins.
1648 So we just mark statement for later folding. We mark
1649 all new statements, instead just statements that has changed
1650 by some nontrivial substitution so even statements made
1651 foldable indirectly are updated. If this turns out to be
1652 expensive, copy_body can be told to watch for nontrivial
1653 changes. */
1654 if (id->statements_to_fold)
1655 pointer_set_insert (id->statements_to_fold, stmt);
1656
1657 /* We're duplicating a CALL_EXPR. Find any corresponding
1658 callgraph edges and update or duplicate them. */
1659 if (is_gimple_call (stmt))
1660 {
1661 struct cgraph_edge *edge;
1662 int flags;
1663
1664 switch (id->transform_call_graph_edges)
1665 {
1666 case CB_CGE_DUPLICATE:
1667 edge = cgraph_edge (id->src_node, orig_stmt);
1668 if (edge)
1669 {
1670 int edge_freq = edge->frequency;
1671 edge = cgraph_clone_edge (edge, id->dst_node, stmt,
1672 gimple_uid (stmt),
1673 REG_BR_PROB_BASE, CGRAPH_FREQ_BASE,
1674 true);
1675 /* We could also just rescale the frequency, but
1676 doing so would introduce roundoff errors and make
1677 verifier unhappy. */
1678 edge->frequency
1679 = compute_call_stmt_bb_frequency (id->dst_node->symbol.decl,
1680 copy_basic_block);
1681 if (dump_file
1682 && profile_status_for_function (cfun) != PROFILE_ABSENT
1683 && (edge_freq > edge->frequency + 10
1684 || edge_freq < edge->frequency - 10))
1685 {
1686 fprintf (dump_file, "Edge frequency estimated by "
1687 "cgraph %i diverge from inliner's estimate %i\n",
1688 edge_freq,
1689 edge->frequency);
1690 fprintf (dump_file,
1691 "Orig bb: %i, orig bb freq %i, new bb freq %i\n",
1692 bb->index,
1693 bb->frequency,
1694 copy_basic_block->frequency);
1695 }
1696 stmt = cgraph_redirect_edge_call_stmt_to_callee (edge);
1697 }
1698 break;
1699
1700 case CB_CGE_MOVE_CLONES:
1701 cgraph_set_call_stmt_including_clones (id->dst_node,
1702 orig_stmt, stmt);
1703 edge = cgraph_edge (id->dst_node, stmt);
1704 break;
1705
1706 case CB_CGE_MOVE:
1707 edge = cgraph_edge (id->dst_node, orig_stmt);
1708 if (edge)
1709 cgraph_set_call_stmt (edge, stmt);
1710 break;
1711
1712 default:
1713 gcc_unreachable ();
1714 }
1715
1716 /* Constant propagation on argument done during inlining
1717 may create new direct call. Produce an edge for it. */
1718 if ((!edge
1719 || (edge->indirect_inlining_edge
1720 && id->transform_call_graph_edges == CB_CGE_MOVE_CLONES))
1721 && id->dst_node->analyzed
1722 && (fn = gimple_call_fndecl (stmt)) != NULL)
1723 {
1724 struct cgraph_node *dest = cgraph_get_node (fn);
1725
1726 /* We have missing edge in the callgraph. This can happen
1727 when previous inlining turned an indirect call into a
1728 direct call by constant propagating arguments or we are
1729 producing dead clone (for further cloning). In all
1730 other cases we hit a bug (incorrect node sharing is the
1731 most common reason for missing edges). */
1732 gcc_assert (!dest->analyzed
1733 || dest->symbol.address_taken
1734 || !id->src_node->analyzed
1735 || !id->dst_node->analyzed);
1736 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES)
1737 cgraph_create_edge_including_clones
1738 (id->dst_node, dest, orig_stmt, stmt, bb->count,
1739 compute_call_stmt_bb_frequency (id->dst_node->symbol.decl,
1740 copy_basic_block),
1741 CIF_ORIGINALLY_INDIRECT_CALL);
1742 else
1743 cgraph_create_edge (id->dst_node, dest, stmt,
1744 bb->count,
1745 compute_call_stmt_bb_frequency
1746 (id->dst_node->symbol.decl,
1747 copy_basic_block))->inline_failed
1748 = CIF_ORIGINALLY_INDIRECT_CALL;
1749 if (dump_file)
1750 {
1751 fprintf (dump_file, "Created new direct edge to %s\n",
1752 cgraph_node_name (dest));
1753 }
1754 }
1755
1756 flags = gimple_call_flags (stmt);
1757 if (flags & ECF_MAY_BE_ALLOCA)
1758 cfun->calls_alloca = true;
1759 if (flags & ECF_RETURNS_TWICE)
1760 cfun->calls_setjmp = true;
1761 }
1762
1763 maybe_duplicate_eh_stmt_fn (cfun, stmt, id->src_cfun, orig_stmt,
1764 id->eh_map, id->eh_lp_nr);
1765
1766 if (gimple_in_ssa_p (cfun) && !is_gimple_debug (stmt))
1767 {
1768 ssa_op_iter i;
1769 tree def;
1770
1771 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
1772 if (TREE_CODE (def) == SSA_NAME)
1773 SSA_NAME_DEF_STMT (def) = stmt;
1774 }
1775
1776 gsi_next (&copy_gsi);
1777 }
1778 while (!gsi_end_p (copy_gsi));
1779
1780 copy_gsi = gsi_last_bb (copy_basic_block);
1781 }
1782
1783 return copy_basic_block;
1784 }
1785
1786 /* Inserting Single Entry Multiple Exit region in SSA form into code in SSA
1787 form is quite easy, since dominator relationship for old basic blocks does
1788 not change.
1789
1790 There is however exception where inlining might change dominator relation
1791 across EH edges from basic block within inlined functions destinating
1792 to landing pads in function we inline into.
1793
1794 The function fills in PHI_RESULTs of such PHI nodes if they refer
1795 to gimple regs. Otherwise, the function mark PHI_RESULT of such
1796 PHI nodes for renaming. For non-gimple regs, renaming is safe: the
1797 EH edges are abnormal and SSA_NAME_OCCURS_IN_ABNORMAL_PHI must be
1798 set, and this means that there will be no overlapping live ranges
1799 for the underlying symbol.
1800
1801 This might change in future if we allow redirecting of EH edges and
1802 we might want to change way build CFG pre-inlining to include
1803 all the possible edges then. */
1804 static void
1805 update_ssa_across_abnormal_edges (basic_block bb, basic_block ret_bb,
1806 bool can_throw, bool nonlocal_goto)
1807 {
1808 edge e;
1809 edge_iterator ei;
1810
1811 FOR_EACH_EDGE (e, ei, bb->succs)
1812 if (!e->dest->aux
1813 || ((basic_block)e->dest->aux)->index == ENTRY_BLOCK)
1814 {
1815 gimple phi;
1816 gimple_stmt_iterator si;
1817
1818 if (!nonlocal_goto)
1819 gcc_assert (e->flags & EDGE_EH);
1820
1821 if (!can_throw)
1822 gcc_assert (!(e->flags & EDGE_EH));
1823
1824 for (si = gsi_start_phis (e->dest); !gsi_end_p (si); gsi_next (&si))
1825 {
1826 edge re;
1827
1828 phi = gsi_stmt (si);
1829
1830 /* There shouldn't be any PHI nodes in the ENTRY_BLOCK. */
1831 gcc_assert (!e->dest->aux);
1832
1833 gcc_assert ((e->flags & EDGE_EH)
1834 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)));
1835
1836 if (virtual_operand_p (PHI_RESULT (phi)))
1837 {
1838 mark_virtual_operands_for_renaming (cfun);
1839 continue;
1840 }
1841
1842 re = find_edge (ret_bb, e->dest);
1843 gcc_assert (re);
1844 gcc_assert ((re->flags & (EDGE_EH | EDGE_ABNORMAL))
1845 == (e->flags & (EDGE_EH | EDGE_ABNORMAL)));
1846
1847 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1848 USE_FROM_PTR (PHI_ARG_DEF_PTR_FROM_EDGE (phi, re)));
1849 }
1850 }
1851 }
1852
1853
1854 /* Copy edges from BB into its copy constructed earlier, scale profile
1855 accordingly. Edges will be taken care of later. Assume aux
1856 pointers to point to the copies of each BB. Return true if any
1857 debug stmts are left after a statement that must end the basic block. */
1858
1859 static bool
1860 copy_edges_for_bb (basic_block bb, gcov_type count_scale, basic_block ret_bb)
1861 {
1862 basic_block new_bb = (basic_block) bb->aux;
1863 edge_iterator ei;
1864 edge old_edge;
1865 gimple_stmt_iterator si;
1866 int flags;
1867 bool need_debug_cleanup = false;
1868
1869 /* Use the indices from the original blocks to create edges for the
1870 new ones. */
1871 FOR_EACH_EDGE (old_edge, ei, bb->succs)
1872 if (!(old_edge->flags & EDGE_EH))
1873 {
1874 edge new_edge;
1875
1876 flags = old_edge->flags;
1877
1878 /* Return edges do get a FALLTHRU flag when the get inlined. */
1879 if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
1880 && old_edge->dest->aux != EXIT_BLOCK_PTR)
1881 flags |= EDGE_FALLTHRU;
1882 new_edge = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
1883 new_edge->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
1884 new_edge->probability = old_edge->probability;
1885 }
1886
1887 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
1888 return false;
1889
1890 for (si = gsi_start_bb (new_bb); !gsi_end_p (si);)
1891 {
1892 gimple copy_stmt;
1893 bool can_throw, nonlocal_goto;
1894
1895 copy_stmt = gsi_stmt (si);
1896 if (!is_gimple_debug (copy_stmt))
1897 update_stmt (copy_stmt);
1898
1899 /* Do this before the possible split_block. */
1900 gsi_next (&si);
1901
1902 /* If this tree could throw an exception, there are two
1903 cases where we need to add abnormal edge(s): the
1904 tree wasn't in a region and there is a "current
1905 region" in the caller; or the original tree had
1906 EH edges. In both cases split the block after the tree,
1907 and add abnormal edge(s) as needed; we need both
1908 those from the callee and the caller.
1909 We check whether the copy can throw, because the const
1910 propagation can change an INDIRECT_REF which throws
1911 into a COMPONENT_REF which doesn't. If the copy
1912 can throw, the original could also throw. */
1913 can_throw = stmt_can_throw_internal (copy_stmt);
1914 nonlocal_goto = stmt_can_make_abnormal_goto (copy_stmt);
1915
1916 if (can_throw || nonlocal_goto)
1917 {
1918 if (!gsi_end_p (si))
1919 {
1920 while (!gsi_end_p (si) && is_gimple_debug (gsi_stmt (si)))
1921 gsi_next (&si);
1922 if (gsi_end_p (si))
1923 need_debug_cleanup = true;
1924 }
1925 if (!gsi_end_p (si))
1926 /* Note that bb's predecessor edges aren't necessarily
1927 right at this point; split_block doesn't care. */
1928 {
1929 edge e = split_block (new_bb, copy_stmt);
1930
1931 new_bb = e->dest;
1932 new_bb->aux = e->src->aux;
1933 si = gsi_start_bb (new_bb);
1934 }
1935 }
1936
1937 if (gimple_code (copy_stmt) == GIMPLE_EH_DISPATCH)
1938 make_eh_dispatch_edges (copy_stmt);
1939 else if (can_throw)
1940 make_eh_edges (copy_stmt);
1941
1942 if (nonlocal_goto)
1943 make_abnormal_goto_edges (gimple_bb (copy_stmt), true);
1944
1945 if ((can_throw || nonlocal_goto)
1946 && gimple_in_ssa_p (cfun))
1947 update_ssa_across_abnormal_edges (gimple_bb (copy_stmt), ret_bb,
1948 can_throw, nonlocal_goto);
1949 }
1950 return need_debug_cleanup;
1951 }
1952
1953 /* Copy the PHIs. All blocks and edges are copied, some blocks
1954 was possibly split and new outgoing EH edges inserted.
1955 BB points to the block of original function and AUX pointers links
1956 the original and newly copied blocks. */
1957
1958 static void
1959 copy_phis_for_bb (basic_block bb, copy_body_data *id)
1960 {
1961 basic_block const new_bb = (basic_block) bb->aux;
1962 edge_iterator ei;
1963 gimple phi;
1964 gimple_stmt_iterator si;
1965 edge new_edge;
1966 bool inserted = false;
1967
1968 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
1969 {
1970 tree res, new_res;
1971 gimple new_phi;
1972
1973 phi = gsi_stmt (si);
1974 res = PHI_RESULT (phi);
1975 new_res = res;
1976 if (!virtual_operand_p (res))
1977 {
1978 walk_tree (&new_res, copy_tree_body_r, id, NULL);
1979 new_phi = create_phi_node (new_res, new_bb);
1980 FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
1981 {
1982 edge old_edge = find_edge ((basic_block) new_edge->src->aux, bb);
1983 tree arg;
1984 tree new_arg;
1985 edge_iterator ei2;
1986 location_t locus;
1987
1988 /* When doing partial cloning, we allow PHIs on the entry block
1989 as long as all the arguments are the same. Find any input
1990 edge to see argument to copy. */
1991 if (!old_edge)
1992 FOR_EACH_EDGE (old_edge, ei2, bb->preds)
1993 if (!old_edge->src->aux)
1994 break;
1995
1996 arg = PHI_ARG_DEF_FROM_EDGE (phi, old_edge);
1997 new_arg = arg;
1998 walk_tree (&new_arg, copy_tree_body_r, id, NULL);
1999 gcc_assert (new_arg);
2000 /* With return slot optimization we can end up with
2001 non-gimple (foo *)&this->m, fix that here. */
2002 if (TREE_CODE (new_arg) != SSA_NAME
2003 && TREE_CODE (new_arg) != FUNCTION_DECL
2004 && !is_gimple_val (new_arg))
2005 {
2006 gimple_seq stmts = NULL;
2007 new_arg = force_gimple_operand (new_arg, &stmts, true, NULL);
2008 gsi_insert_seq_on_edge (new_edge, stmts);
2009 inserted = true;
2010 }
2011 locus = gimple_phi_arg_location_from_edge (phi, old_edge);
2012 if (LOCATION_BLOCK (locus))
2013 {
2014 tree *n;
2015 n = (tree *) pointer_map_contains (id->decl_map,
2016 LOCATION_BLOCK (locus));
2017 gcc_assert (n);
2018 locus = COMBINE_LOCATION_DATA (line_table, locus, *n);
2019 }
2020 else
2021 locus = LOCATION_LOCUS (locus);
2022
2023 add_phi_arg (new_phi, new_arg, new_edge, locus);
2024 }
2025 }
2026 }
2027
2028 /* Commit the delayed edge insertions. */
2029 if (inserted)
2030 FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
2031 gsi_commit_one_edge_insert (new_edge, NULL);
2032 }
2033
2034
2035 /* Wrapper for remap_decl so it can be used as a callback. */
2036
2037 static tree
2038 remap_decl_1 (tree decl, void *data)
2039 {
2040 return remap_decl (decl, (copy_body_data *) data);
2041 }
2042
2043 /* Build struct function and associated datastructures for the new clone
2044 NEW_FNDECL to be build. CALLEE_FNDECL is the original. Function changes
2045 the cfun to the function of new_fndecl (and current_function_decl too). */
2046
2047 static void
2048 initialize_cfun (tree new_fndecl, tree callee_fndecl, gcov_type count)
2049 {
2050 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2051 gcov_type count_scale;
2052
2053 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
2054 count_scale = (REG_BR_PROB_BASE * count
2055 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
2056 else
2057 count_scale = REG_BR_PROB_BASE;
2058
2059 /* Register specific tree functions. */
2060 gimple_register_cfg_hooks ();
2061
2062 /* Get clean struct function. */
2063 push_struct_function (new_fndecl);
2064
2065 /* We will rebuild these, so just sanity check that they are empty. */
2066 gcc_assert (VALUE_HISTOGRAMS (cfun) == NULL);
2067 gcc_assert (cfun->local_decls == NULL);
2068 gcc_assert (cfun->cfg == NULL);
2069 gcc_assert (cfun->decl == new_fndecl);
2070
2071 /* Copy items we preserve during cloning. */
2072 cfun->static_chain_decl = src_cfun->static_chain_decl;
2073 cfun->nonlocal_goto_save_area = src_cfun->nonlocal_goto_save_area;
2074 cfun->function_end_locus = src_cfun->function_end_locus;
2075 cfun->curr_properties = src_cfun->curr_properties & ~PROP_loops;
2076 cfun->last_verified = src_cfun->last_verified;
2077 cfun->va_list_gpr_size = src_cfun->va_list_gpr_size;
2078 cfun->va_list_fpr_size = src_cfun->va_list_fpr_size;
2079 cfun->has_nonlocal_label = src_cfun->has_nonlocal_label;
2080 cfun->stdarg = src_cfun->stdarg;
2081 cfun->after_inlining = src_cfun->after_inlining;
2082 cfun->can_throw_non_call_exceptions
2083 = src_cfun->can_throw_non_call_exceptions;
2084 cfun->can_delete_dead_exceptions = src_cfun->can_delete_dead_exceptions;
2085 cfun->returns_struct = src_cfun->returns_struct;
2086 cfun->returns_pcc_struct = src_cfun->returns_pcc_struct;
2087
2088 init_empty_tree_cfg ();
2089
2090 profile_status_for_function (cfun) = profile_status_for_function (src_cfun);
2091 ENTRY_BLOCK_PTR->count =
2092 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
2093 REG_BR_PROB_BASE);
2094 ENTRY_BLOCK_PTR->frequency
2095 = ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency;
2096 EXIT_BLOCK_PTR->count =
2097 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
2098 REG_BR_PROB_BASE);
2099 EXIT_BLOCK_PTR->frequency =
2100 EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency;
2101 if (src_cfun->eh)
2102 init_eh_for_function ();
2103
2104 if (src_cfun->gimple_df)
2105 {
2106 init_tree_ssa (cfun);
2107 cfun->gimple_df->in_ssa_p = true;
2108 init_ssa_operands (cfun);
2109 }
2110 }
2111
2112 /* Helper function for copy_cfg_body. Move debug stmts from the end
2113 of NEW_BB to the beginning of successor basic blocks when needed. If the
2114 successor has multiple predecessors, reset them, otherwise keep
2115 their value. */
2116
2117 static void
2118 maybe_move_debug_stmts_to_successors (copy_body_data *id, basic_block new_bb)
2119 {
2120 edge e;
2121 edge_iterator ei;
2122 gimple_stmt_iterator si = gsi_last_nondebug_bb (new_bb);
2123
2124 if (gsi_end_p (si)
2125 || gsi_one_before_end_p (si)
2126 || !(stmt_can_throw_internal (gsi_stmt (si))
2127 || stmt_can_make_abnormal_goto (gsi_stmt (si))))
2128 return;
2129
2130 FOR_EACH_EDGE (e, ei, new_bb->succs)
2131 {
2132 gimple_stmt_iterator ssi = gsi_last_bb (new_bb);
2133 gimple_stmt_iterator dsi = gsi_after_labels (e->dest);
2134 while (is_gimple_debug (gsi_stmt (ssi)))
2135 {
2136 gimple stmt = gsi_stmt (ssi), new_stmt;
2137 tree var;
2138 tree value;
2139
2140 /* For the last edge move the debug stmts instead of copying
2141 them. */
2142 if (ei_one_before_end_p (ei))
2143 {
2144 si = ssi;
2145 gsi_prev (&ssi);
2146 if (!single_pred_p (e->dest) && gimple_debug_bind_p (stmt))
2147 gimple_debug_bind_reset_value (stmt);
2148 gsi_remove (&si, false);
2149 gsi_insert_before (&dsi, stmt, GSI_SAME_STMT);
2150 continue;
2151 }
2152
2153 if (gimple_debug_bind_p (stmt))
2154 {
2155 var = gimple_debug_bind_get_var (stmt);
2156 if (single_pred_p (e->dest))
2157 {
2158 value = gimple_debug_bind_get_value (stmt);
2159 value = unshare_expr (value);
2160 }
2161 else
2162 value = NULL_TREE;
2163 new_stmt = gimple_build_debug_bind (var, value, stmt);
2164 }
2165 else if (gimple_debug_source_bind_p (stmt))
2166 {
2167 var = gimple_debug_source_bind_get_var (stmt);
2168 value = gimple_debug_source_bind_get_value (stmt);
2169 new_stmt = gimple_build_debug_source_bind (var, value, stmt);
2170 }
2171 else
2172 gcc_unreachable ();
2173 gsi_insert_before (&dsi, new_stmt, GSI_SAME_STMT);
2174 id->debug_stmts.safe_push (new_stmt);
2175 gsi_prev (&ssi);
2176 }
2177 }
2178 }
2179
2180 /* Make a copy of the body of FN so that it can be inserted inline in
2181 another function. Walks FN via CFG, returns new fndecl. */
2182
2183 static tree
2184 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency_scale,
2185 basic_block entry_block_map, basic_block exit_block_map,
2186 bitmap blocks_to_copy, basic_block new_entry)
2187 {
2188 tree callee_fndecl = id->src_fn;
2189 /* Original cfun for the callee, doesn't change. */
2190 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2191 struct function *cfun_to_copy;
2192 basic_block bb;
2193 tree new_fndecl = NULL;
2194 bool need_debug_cleanup = false;
2195 gcov_type count_scale;
2196 int last;
2197 int incoming_frequency = 0;
2198 gcov_type incoming_count = 0;
2199
2200 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
2201 count_scale = (REG_BR_PROB_BASE * count
2202 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
2203 else
2204 count_scale = REG_BR_PROB_BASE;
2205
2206 /* Register specific tree functions. */
2207 gimple_register_cfg_hooks ();
2208
2209 /* If we are inlining just region of the function, make sure to connect new entry
2210 to ENTRY_BLOCK_PTR. Since new entry can be part of loop, we must compute
2211 frequency and probability of ENTRY_BLOCK_PTR based on the frequencies and
2212 probabilities of edges incoming from nonduplicated region. */
2213 if (new_entry)
2214 {
2215 edge e;
2216 edge_iterator ei;
2217
2218 FOR_EACH_EDGE (e, ei, new_entry->preds)
2219 if (!e->src->aux)
2220 {
2221 incoming_frequency += EDGE_FREQUENCY (e);
2222 incoming_count += e->count;
2223 }
2224 incoming_count = incoming_count * count_scale / REG_BR_PROB_BASE;
2225 incoming_frequency
2226 = incoming_frequency * frequency_scale / REG_BR_PROB_BASE;
2227 ENTRY_BLOCK_PTR->count = incoming_count;
2228 ENTRY_BLOCK_PTR->frequency = incoming_frequency;
2229 }
2230
2231 /* Must have a CFG here at this point. */
2232 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
2233 (DECL_STRUCT_FUNCTION (callee_fndecl)));
2234
2235 cfun_to_copy = id->src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2236
2237 ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
2238 EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
2239 entry_block_map->aux = ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
2240 exit_block_map->aux = EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
2241
2242 /* Duplicate any exception-handling regions. */
2243 if (cfun->eh)
2244 id->eh_map = duplicate_eh_regions (cfun_to_copy, NULL, id->eh_lp_nr,
2245 remap_decl_1, id);
2246
2247 /* Use aux pointers to map the original blocks to copy. */
2248 FOR_EACH_BB_FN (bb, cfun_to_copy)
2249 if (!blocks_to_copy || bitmap_bit_p (blocks_to_copy, bb->index))
2250 {
2251 basic_block new_bb = copy_bb (id, bb, frequency_scale, count_scale);
2252 bb->aux = new_bb;
2253 new_bb->aux = bb;
2254 }
2255
2256 last = last_basic_block;
2257
2258 /* Now that we've duplicated the blocks, duplicate their edges. */
2259 FOR_ALL_BB_FN (bb, cfun_to_copy)
2260 if (!blocks_to_copy
2261 || (bb->index > 0 && bitmap_bit_p (blocks_to_copy, bb->index)))
2262 need_debug_cleanup |= copy_edges_for_bb (bb, count_scale, exit_block_map);
2263
2264 if (new_entry)
2265 {
2266 edge e = make_edge (entry_block_map, (basic_block)new_entry->aux, EDGE_FALLTHRU);
2267 e->probability = REG_BR_PROB_BASE;
2268 e->count = incoming_count;
2269 }
2270
2271 if (gimple_in_ssa_p (cfun))
2272 FOR_ALL_BB_FN (bb, cfun_to_copy)
2273 if (!blocks_to_copy
2274 || (bb->index > 0 && bitmap_bit_p (blocks_to_copy, bb->index)))
2275 copy_phis_for_bb (bb, id);
2276
2277 FOR_ALL_BB_FN (bb, cfun_to_copy)
2278 if (bb->aux)
2279 {
2280 if (need_debug_cleanup
2281 && bb->index != ENTRY_BLOCK
2282 && bb->index != EXIT_BLOCK)
2283 maybe_move_debug_stmts_to_successors (id, (basic_block) bb->aux);
2284 ((basic_block)bb->aux)->aux = NULL;
2285 bb->aux = NULL;
2286 }
2287
2288 /* Zero out AUX fields of newly created block during EH edge
2289 insertion. */
2290 for (; last < last_basic_block; last++)
2291 {
2292 if (need_debug_cleanup)
2293 maybe_move_debug_stmts_to_successors (id, BASIC_BLOCK (last));
2294 BASIC_BLOCK (last)->aux = NULL;
2295 }
2296 entry_block_map->aux = NULL;
2297 exit_block_map->aux = NULL;
2298
2299 if (id->eh_map)
2300 {
2301 pointer_map_destroy (id->eh_map);
2302 id->eh_map = NULL;
2303 }
2304
2305 return new_fndecl;
2306 }
2307
2308 /* Copy the debug STMT using ID. We deal with these statements in a
2309 special way: if any variable in their VALUE expression wasn't
2310 remapped yet, we won't remap it, because that would get decl uids
2311 out of sync, causing codegen differences between -g and -g0. If
2312 this arises, we drop the VALUE expression altogether. */
2313
2314 static void
2315 copy_debug_stmt (gimple stmt, copy_body_data *id)
2316 {
2317 tree t, *n;
2318 struct walk_stmt_info wi;
2319
2320 if (gimple_block (stmt))
2321 {
2322 n = (tree *) pointer_map_contains (id->decl_map, gimple_block (stmt));
2323 gimple_set_block (stmt, n ? *n : id->block);
2324 }
2325
2326 /* Remap all the operands in COPY. */
2327 memset (&wi, 0, sizeof (wi));
2328 wi.info = id;
2329
2330 processing_debug_stmt = 1;
2331
2332 if (gimple_debug_source_bind_p (stmt))
2333 t = gimple_debug_source_bind_get_var (stmt);
2334 else
2335 t = gimple_debug_bind_get_var (stmt);
2336
2337 if (TREE_CODE (t) == PARM_DECL && id->debug_map
2338 && (n = (tree *) pointer_map_contains (id->debug_map, t)))
2339 {
2340 gcc_assert (TREE_CODE (*n) == VAR_DECL);
2341 t = *n;
2342 }
2343 else if (TREE_CODE (t) == VAR_DECL
2344 && !is_global_var (t)
2345 && !pointer_map_contains (id->decl_map, t))
2346 /* T is a non-localized variable. */;
2347 else
2348 walk_tree (&t, remap_gimple_op_r, &wi, NULL);
2349
2350 if (gimple_debug_bind_p (stmt))
2351 {
2352 gimple_debug_bind_set_var (stmt, t);
2353
2354 if (gimple_debug_bind_has_value_p (stmt))
2355 walk_tree (gimple_debug_bind_get_value_ptr (stmt),
2356 remap_gimple_op_r, &wi, NULL);
2357
2358 /* Punt if any decl couldn't be remapped. */
2359 if (processing_debug_stmt < 0)
2360 gimple_debug_bind_reset_value (stmt);
2361 }
2362 else if (gimple_debug_source_bind_p (stmt))
2363 {
2364 gimple_debug_source_bind_set_var (stmt, t);
2365 walk_tree (gimple_debug_source_bind_get_value_ptr (stmt),
2366 remap_gimple_op_r, &wi, NULL);
2367 /* When inlining and source bind refers to one of the optimized
2368 away parameters, change the source bind into normal debug bind
2369 referring to the corresponding DEBUG_EXPR_DECL that should have
2370 been bound before the call stmt. */
2371 t = gimple_debug_source_bind_get_value (stmt);
2372 if (t != NULL_TREE
2373 && TREE_CODE (t) == PARM_DECL
2374 && id->gimple_call)
2375 {
2376 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (id->src_fn);
2377 unsigned int i;
2378 if (debug_args != NULL)
2379 {
2380 for (i = 0; i < vec_safe_length (*debug_args); i += 2)
2381 if ((**debug_args)[i] == DECL_ORIGIN (t)
2382 && TREE_CODE ((**debug_args)[i + 1]) == DEBUG_EXPR_DECL)
2383 {
2384 t = (**debug_args)[i + 1];
2385 stmt->gsbase.subcode = GIMPLE_DEBUG_BIND;
2386 gimple_debug_bind_set_value (stmt, t);
2387 break;
2388 }
2389 }
2390 }
2391 }
2392
2393 processing_debug_stmt = 0;
2394
2395 update_stmt (stmt);
2396 }
2397
2398 /* Process deferred debug stmts. In order to give values better odds
2399 of being successfully remapped, we delay the processing of debug
2400 stmts until all other stmts that might require remapping are
2401 processed. */
2402
2403 static void
2404 copy_debug_stmts (copy_body_data *id)
2405 {
2406 size_t i;
2407 gimple stmt;
2408
2409 if (!id->debug_stmts.exists ())
2410 return;
2411
2412 FOR_EACH_VEC_ELT (id->debug_stmts, i, stmt)
2413 copy_debug_stmt (stmt, id);
2414
2415 id->debug_stmts.release ();
2416 }
2417
2418 /* Make a copy of the body of SRC_FN so that it can be inserted inline in
2419 another function. */
2420
2421 static tree
2422 copy_tree_body (copy_body_data *id)
2423 {
2424 tree fndecl = id->src_fn;
2425 tree body = DECL_SAVED_TREE (fndecl);
2426
2427 walk_tree (&body, copy_tree_body_r, id, NULL);
2428
2429 return body;
2430 }
2431
2432 /* Make a copy of the body of FN so that it can be inserted inline in
2433 another function. */
2434
2435 static tree
2436 copy_body (copy_body_data *id, gcov_type count, int frequency_scale,
2437 basic_block entry_block_map, basic_block exit_block_map,
2438 bitmap blocks_to_copy, basic_block new_entry)
2439 {
2440 tree fndecl = id->src_fn;
2441 tree body;
2442
2443 /* If this body has a CFG, walk CFG and copy. */
2444 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
2445 body = copy_cfg_body (id, count, frequency_scale, entry_block_map, exit_block_map,
2446 blocks_to_copy, new_entry);
2447 copy_debug_stmts (id);
2448
2449 return body;
2450 }
2451
2452 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
2453 defined in function FN, or of a data member thereof. */
2454
2455 static bool
2456 self_inlining_addr_expr (tree value, tree fn)
2457 {
2458 tree var;
2459
2460 if (TREE_CODE (value) != ADDR_EXPR)
2461 return false;
2462
2463 var = get_base_address (TREE_OPERAND (value, 0));
2464
2465 return var && auto_var_in_fn_p (var, fn);
2466 }
2467
2468 /* Append to BB a debug annotation that binds VAR to VALUE, inheriting
2469 lexical block and line number information from base_stmt, if given,
2470 or from the last stmt of the block otherwise. */
2471
2472 static gimple
2473 insert_init_debug_bind (copy_body_data *id,
2474 basic_block bb, tree var, tree value,
2475 gimple base_stmt)
2476 {
2477 gimple note;
2478 gimple_stmt_iterator gsi;
2479 tree tracked_var;
2480
2481 if (!gimple_in_ssa_p (id->src_cfun))
2482 return NULL;
2483
2484 if (!MAY_HAVE_DEBUG_STMTS)
2485 return NULL;
2486
2487 tracked_var = target_for_debug_bind (var);
2488 if (!tracked_var)
2489 return NULL;
2490
2491 if (bb)
2492 {
2493 gsi = gsi_last_bb (bb);
2494 if (!base_stmt && !gsi_end_p (gsi))
2495 base_stmt = gsi_stmt (gsi);
2496 }
2497
2498 note = gimple_build_debug_bind (tracked_var, value, base_stmt);
2499
2500 if (bb)
2501 {
2502 if (!gsi_end_p (gsi))
2503 gsi_insert_after (&gsi, note, GSI_SAME_STMT);
2504 else
2505 gsi_insert_before (&gsi, note, GSI_SAME_STMT);
2506 }
2507
2508 return note;
2509 }
2510
2511 static void
2512 insert_init_stmt (copy_body_data *id, basic_block bb, gimple init_stmt)
2513 {
2514 /* If VAR represents a zero-sized variable, it's possible that the
2515 assignment statement may result in no gimple statements. */
2516 if (init_stmt)
2517 {
2518 gimple_stmt_iterator si = gsi_last_bb (bb);
2519
2520 /* We can end up with init statements that store to a non-register
2521 from a rhs with a conversion. Handle that here by forcing the
2522 rhs into a temporary. gimple_regimplify_operands is not
2523 prepared to do this for us. */
2524 if (!is_gimple_debug (init_stmt)
2525 && !is_gimple_reg (gimple_assign_lhs (init_stmt))
2526 && is_gimple_reg_type (TREE_TYPE (gimple_assign_lhs (init_stmt)))
2527 && gimple_assign_rhs_class (init_stmt) == GIMPLE_UNARY_RHS)
2528 {
2529 tree rhs = build1 (gimple_assign_rhs_code (init_stmt),
2530 gimple_expr_type (init_stmt),
2531 gimple_assign_rhs1 (init_stmt));
2532 rhs = force_gimple_operand_gsi (&si, rhs, true, NULL_TREE, false,
2533 GSI_NEW_STMT);
2534 gimple_assign_set_rhs_code (init_stmt, TREE_CODE (rhs));
2535 gimple_assign_set_rhs1 (init_stmt, rhs);
2536 }
2537 gsi_insert_after (&si, init_stmt, GSI_NEW_STMT);
2538 gimple_regimplify_operands (init_stmt, &si);
2539
2540 if (!is_gimple_debug (init_stmt) && MAY_HAVE_DEBUG_STMTS)
2541 {
2542 tree def = gimple_assign_lhs (init_stmt);
2543 insert_init_debug_bind (id, bb, def, def, init_stmt);
2544 }
2545 }
2546 }
2547
2548 /* Initialize parameter P with VALUE. If needed, produce init statement
2549 at the end of BB. When BB is NULL, we return init statement to be
2550 output later. */
2551 static gimple
2552 setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
2553 basic_block bb, tree *vars)
2554 {
2555 gimple init_stmt = NULL;
2556 tree var;
2557 tree rhs = value;
2558 tree def = (gimple_in_ssa_p (cfun)
2559 ? ssa_default_def (id->src_cfun, p) : NULL);
2560
2561 if (value
2562 && value != error_mark_node
2563 && !useless_type_conversion_p (TREE_TYPE (p), TREE_TYPE (value)))
2564 {
2565 /* If we can match up types by promotion/demotion do so. */
2566 if (fold_convertible_p (TREE_TYPE (p), value))
2567 rhs = fold_convert (TREE_TYPE (p), value);
2568 else
2569 {
2570 /* ??? For valid programs we should not end up here.
2571 Still if we end up with truly mismatched types here, fall back
2572 to using a VIEW_CONVERT_EXPR or a literal zero to not leak invalid
2573 GIMPLE to the following passes. */
2574 if (!is_gimple_reg_type (TREE_TYPE (value))
2575 || TYPE_SIZE (TREE_TYPE (p)) == TYPE_SIZE (TREE_TYPE (value)))
2576 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (p), value);
2577 else
2578 rhs = build_zero_cst (TREE_TYPE (p));
2579 }
2580 }
2581
2582 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
2583 here since the type of this decl must be visible to the calling
2584 function. */
2585 var = copy_decl_to_var (p, id);
2586
2587 /* Declare this new variable. */
2588 DECL_CHAIN (var) = *vars;
2589 *vars = var;
2590
2591 /* Make gimplifier happy about this variable. */
2592 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2593
2594 /* If the parameter is never assigned to, has no SSA_NAMEs created,
2595 we would not need to create a new variable here at all, if it
2596 weren't for debug info. Still, we can just use the argument
2597 value. */
2598 if (TREE_READONLY (p)
2599 && !TREE_ADDRESSABLE (p)
2600 && value && !TREE_SIDE_EFFECTS (value)
2601 && !def)
2602 {
2603 /* We may produce non-gimple trees by adding NOPs or introduce
2604 invalid sharing when operand is not really constant.
2605 It is not big deal to prohibit constant propagation here as
2606 we will constant propagate in DOM1 pass anyway. */
2607 if (is_gimple_min_invariant (value)
2608 && useless_type_conversion_p (TREE_TYPE (p),
2609 TREE_TYPE (value))
2610 /* We have to be very careful about ADDR_EXPR. Make sure
2611 the base variable isn't a local variable of the inlined
2612 function, e.g., when doing recursive inlining, direct or
2613 mutually-recursive or whatever, which is why we don't
2614 just test whether fn == current_function_decl. */
2615 && ! self_inlining_addr_expr (value, fn))
2616 {
2617 insert_decl_map (id, p, value);
2618 insert_debug_decl_map (id, p, var);
2619 return insert_init_debug_bind (id, bb, var, value, NULL);
2620 }
2621 }
2622
2623 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
2624 that way, when the PARM_DECL is encountered, it will be
2625 automatically replaced by the VAR_DECL. */
2626 insert_decl_map (id, p, var);
2627
2628 /* Even if P was TREE_READONLY, the new VAR should not be.
2629 In the original code, we would have constructed a
2630 temporary, and then the function body would have never
2631 changed the value of P. However, now, we will be
2632 constructing VAR directly. The constructor body may
2633 change its value multiple times as it is being
2634 constructed. Therefore, it must not be TREE_READONLY;
2635 the back-end assumes that TREE_READONLY variable is
2636 assigned to only once. */
2637 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
2638 TREE_READONLY (var) = 0;
2639
2640 /* If there is no setup required and we are in SSA, take the easy route
2641 replacing all SSA names representing the function parameter by the
2642 SSA name passed to function.
2643
2644 We need to construct map for the variable anyway as it might be used
2645 in different SSA names when parameter is set in function.
2646
2647 Do replacement at -O0 for const arguments replaced by constant.
2648 This is important for builtin_constant_p and other construct requiring
2649 constant argument to be visible in inlined function body. */
2650 if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p)
2651 && (optimize
2652 || (TREE_READONLY (p)
2653 && is_gimple_min_invariant (rhs)))
2654 && (TREE_CODE (rhs) == SSA_NAME
2655 || is_gimple_min_invariant (rhs))
2656 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
2657 {
2658 insert_decl_map (id, def, rhs);
2659 return insert_init_debug_bind (id, bb, var, rhs, NULL);
2660 }
2661
2662 /* If the value of argument is never used, don't care about initializing
2663 it. */
2664 if (optimize && gimple_in_ssa_p (cfun) && !def && is_gimple_reg (p))
2665 {
2666 gcc_assert (!value || !TREE_SIDE_EFFECTS (value));
2667 return insert_init_debug_bind (id, bb, var, rhs, NULL);
2668 }
2669
2670 /* Initialize this VAR_DECL from the equivalent argument. Convert
2671 the argument to the proper type in case it was promoted. */
2672 if (value)
2673 {
2674 if (rhs == error_mark_node)
2675 {
2676 insert_decl_map (id, p, var);
2677 return insert_init_debug_bind (id, bb, var, rhs, NULL);
2678 }
2679
2680 STRIP_USELESS_TYPE_CONVERSION (rhs);
2681
2682 /* If we are in SSA form properly remap the default definition
2683 or assign to a dummy SSA name if the parameter is unused and
2684 we are not optimizing. */
2685 if (gimple_in_ssa_p (cfun) && is_gimple_reg (p))
2686 {
2687 if (def)
2688 {
2689 def = remap_ssa_name (def, id);
2690 init_stmt = gimple_build_assign (def, rhs);
2691 SSA_NAME_IS_DEFAULT_DEF (def) = 0;
2692 set_ssa_default_def (cfun, var, NULL);
2693 }
2694 else if (!optimize)
2695 {
2696 def = make_ssa_name (var, NULL);
2697 init_stmt = gimple_build_assign (def, rhs);
2698 }
2699 }
2700 else
2701 init_stmt = gimple_build_assign (var, rhs);
2702
2703 if (bb && init_stmt)
2704 insert_init_stmt (id, bb, init_stmt);
2705 }
2706 return init_stmt;
2707 }
2708
2709 /* Generate code to initialize the parameters of the function at the
2710 top of the stack in ID from the GIMPLE_CALL STMT. */
2711
2712 static void
2713 initialize_inlined_parameters (copy_body_data *id, gimple stmt,
2714 tree fn, basic_block bb)
2715 {
2716 tree parms;
2717 size_t i;
2718 tree p;
2719 tree vars = NULL_TREE;
2720 tree static_chain = gimple_call_chain (stmt);
2721
2722 /* Figure out what the parameters are. */
2723 parms = DECL_ARGUMENTS (fn);
2724
2725 /* Loop through the parameter declarations, replacing each with an
2726 equivalent VAR_DECL, appropriately initialized. */
2727 for (p = parms, i = 0; p; p = DECL_CHAIN (p), i++)
2728 {
2729 tree val;
2730 val = i < gimple_call_num_args (stmt) ? gimple_call_arg (stmt, i) : NULL;
2731 setup_one_parameter (id, p, val, fn, bb, &vars);
2732 }
2733 /* After remapping parameters remap their types. This has to be done
2734 in a second loop over all parameters to appropriately remap
2735 variable sized arrays when the size is specified in a
2736 parameter following the array. */
2737 for (p = parms, i = 0; p; p = DECL_CHAIN (p), i++)
2738 {
2739 tree *varp = (tree *) pointer_map_contains (id->decl_map, p);
2740 if (varp
2741 && TREE_CODE (*varp) == VAR_DECL)
2742 {
2743 tree def = (gimple_in_ssa_p (cfun) && is_gimple_reg (p)
2744 ? ssa_default_def (id->src_cfun, p) : NULL);
2745 tree var = *varp;
2746 TREE_TYPE (var) = remap_type (TREE_TYPE (var), id);
2747 /* Also remap the default definition if it was remapped
2748 to the default definition of the parameter replacement
2749 by the parameter setup. */
2750 if (def)
2751 {
2752 tree *defp = (tree *) pointer_map_contains (id->decl_map, def);
2753 if (defp
2754 && TREE_CODE (*defp) == SSA_NAME
2755 && SSA_NAME_VAR (*defp) == var)
2756 TREE_TYPE (*defp) = TREE_TYPE (var);
2757 }
2758 }
2759 }
2760
2761 /* Initialize the static chain. */
2762 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
2763 gcc_assert (fn != current_function_decl);
2764 if (p)
2765 {
2766 /* No static chain? Seems like a bug in tree-nested.c. */
2767 gcc_assert (static_chain);
2768
2769 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
2770 }
2771
2772 declare_inline_vars (id->block, vars);
2773 }
2774
2775
2776 /* Declare a return variable to replace the RESULT_DECL for the
2777 function we are calling. An appropriate DECL_STMT is returned.
2778 The USE_STMT is filled to contain a use of the declaration to
2779 indicate the return value of the function.
2780
2781 RETURN_SLOT, if non-null is place where to store the result. It
2782 is set only for CALL_EXPR_RETURN_SLOT_OPT. MODIFY_DEST, if non-null,
2783 was the LHS of the MODIFY_EXPR to which this call is the RHS.
2784
2785 The return value is a (possibly null) value that holds the result
2786 as seen by the caller. */
2787
2788 static tree
2789 declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest,
2790 basic_block entry_bb)
2791 {
2792 tree callee = id->src_fn;
2793 tree result = DECL_RESULT (callee);
2794 tree callee_type = TREE_TYPE (result);
2795 tree caller_type;
2796 tree var, use;
2797
2798 /* Handle type-mismatches in the function declaration return type
2799 vs. the call expression. */
2800 if (modify_dest)
2801 caller_type = TREE_TYPE (modify_dest);
2802 else
2803 caller_type = TREE_TYPE (TREE_TYPE (callee));
2804
2805 /* We don't need to do anything for functions that don't return anything. */
2806 if (VOID_TYPE_P (callee_type))
2807 return NULL_TREE;
2808
2809 /* If there was a return slot, then the return value is the
2810 dereferenced address of that object. */
2811 if (return_slot)
2812 {
2813 /* The front end shouldn't have used both return_slot and
2814 a modify expression. */
2815 gcc_assert (!modify_dest);
2816 if (DECL_BY_REFERENCE (result))
2817 {
2818 tree return_slot_addr = build_fold_addr_expr (return_slot);
2819 STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
2820
2821 /* We are going to construct *&return_slot and we can't do that
2822 for variables believed to be not addressable.
2823
2824 FIXME: This check possibly can match, because values returned
2825 via return slot optimization are not believed to have address
2826 taken by alias analysis. */
2827 gcc_assert (TREE_CODE (return_slot) != SSA_NAME);
2828 var = return_slot_addr;
2829 }
2830 else
2831 {
2832 var = return_slot;
2833 gcc_assert (TREE_CODE (var) != SSA_NAME);
2834 TREE_ADDRESSABLE (var) |= TREE_ADDRESSABLE (result);
2835 }
2836 if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
2837 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
2838 && !DECL_GIMPLE_REG_P (result)
2839 && DECL_P (var))
2840 DECL_GIMPLE_REG_P (var) = 0;
2841 use = NULL;
2842 goto done;
2843 }
2844
2845 /* All types requiring non-trivial constructors should have been handled. */
2846 gcc_assert (!TREE_ADDRESSABLE (callee_type));
2847
2848 /* Attempt to avoid creating a new temporary variable. */
2849 if (modify_dest
2850 && TREE_CODE (modify_dest) != SSA_NAME)
2851 {
2852 bool use_it = false;
2853
2854 /* We can't use MODIFY_DEST if there's type promotion involved. */
2855 if (!useless_type_conversion_p (callee_type, caller_type))
2856 use_it = false;
2857
2858 /* ??? If we're assigning to a variable sized type, then we must
2859 reuse the destination variable, because we've no good way to
2860 create variable sized temporaries at this point. */
2861 else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
2862 use_it = true;
2863
2864 /* If the callee cannot possibly modify MODIFY_DEST, then we can
2865 reuse it as the result of the call directly. Don't do this if
2866 it would promote MODIFY_DEST to addressable. */
2867 else if (TREE_ADDRESSABLE (result))
2868 use_it = false;
2869 else
2870 {
2871 tree base_m = get_base_address (modify_dest);
2872
2873 /* If the base isn't a decl, then it's a pointer, and we don't
2874 know where that's going to go. */
2875 if (!DECL_P (base_m))
2876 use_it = false;
2877 else if (is_global_var (base_m))
2878 use_it = false;
2879 else if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
2880 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
2881 && !DECL_GIMPLE_REG_P (result)
2882 && DECL_GIMPLE_REG_P (base_m))
2883 use_it = false;
2884 else if (!TREE_ADDRESSABLE (base_m))
2885 use_it = true;
2886 }
2887
2888 if (use_it)
2889 {
2890 var = modify_dest;
2891 use = NULL;
2892 goto done;
2893 }
2894 }
2895
2896 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
2897
2898 var = copy_result_decl_to_var (result, id);
2899 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2900
2901 /* Do not have the rest of GCC warn about this variable as it should
2902 not be visible to the user. */
2903 TREE_NO_WARNING (var) = 1;
2904
2905 declare_inline_vars (id->block, var);
2906
2907 /* Build the use expr. If the return type of the function was
2908 promoted, convert it back to the expected type. */
2909 use = var;
2910 if (!useless_type_conversion_p (caller_type, TREE_TYPE (var)))
2911 {
2912 /* If we can match up types by promotion/demotion do so. */
2913 if (fold_convertible_p (caller_type, var))
2914 use = fold_convert (caller_type, var);
2915 else
2916 {
2917 /* ??? For valid programs we should not end up here.
2918 Still if we end up with truly mismatched types here, fall back
2919 to using a MEM_REF to not leak invalid GIMPLE to the following
2920 passes. */
2921 /* Prevent var from being written into SSA form. */
2922 if (TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE
2923 || TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE)
2924 DECL_GIMPLE_REG_P (var) = false;
2925 else if (is_gimple_reg_type (TREE_TYPE (var)))
2926 TREE_ADDRESSABLE (var) = true;
2927 use = fold_build2 (MEM_REF, caller_type,
2928 build_fold_addr_expr (var),
2929 build_int_cst (ptr_type_node, 0));
2930 }
2931 }
2932
2933 STRIP_USELESS_TYPE_CONVERSION (use);
2934
2935 if (DECL_BY_REFERENCE (result))
2936 {
2937 TREE_ADDRESSABLE (var) = 1;
2938 var = build_fold_addr_expr (var);
2939 }
2940
2941 done:
2942 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
2943 way, when the RESULT_DECL is encountered, it will be
2944 automatically replaced by the VAR_DECL.
2945
2946 When returning by reference, ensure that RESULT_DECL remaps to
2947 gimple_val. */
2948 if (DECL_BY_REFERENCE (result)
2949 && !is_gimple_val (var))
2950 {
2951 tree temp = create_tmp_var (TREE_TYPE (result), "retvalptr");
2952 insert_decl_map (id, result, temp);
2953 /* When RESULT_DECL is in SSA form, we need to remap and initialize
2954 it's default_def SSA_NAME. */
2955 if (gimple_in_ssa_p (id->src_cfun)
2956 && is_gimple_reg (result))
2957 {
2958 temp = make_ssa_name (temp, NULL);
2959 insert_decl_map (id, ssa_default_def (id->src_cfun, result), temp);
2960 }
2961 insert_init_stmt (id, entry_bb, gimple_build_assign (temp, var));
2962 }
2963 else
2964 insert_decl_map (id, result, var);
2965
2966 /* Remember this so we can ignore it in remap_decls. */
2967 id->retvar = var;
2968
2969 return use;
2970 }
2971
2972 /* Callback through walk_tree. Determine if a DECL_INITIAL makes reference
2973 to a local label. */
2974
2975 static tree
2976 has_label_address_in_static_1 (tree *nodep, int *walk_subtrees, void *fnp)
2977 {
2978 tree node = *nodep;
2979 tree fn = (tree) fnp;
2980
2981 if (TREE_CODE (node) == LABEL_DECL && DECL_CONTEXT (node) == fn)
2982 return node;
2983
2984 if (TYPE_P (node))
2985 *walk_subtrees = 0;
2986
2987 return NULL_TREE;
2988 }
2989
2990 /* Determine if the function can be copied. If so return NULL. If
2991 not return a string describng the reason for failure. */
2992
2993 static const char *
2994 copy_forbidden (struct function *fun, tree fndecl)
2995 {
2996 const char *reason = fun->cannot_be_copied_reason;
2997 tree decl;
2998 unsigned ix;
2999
3000 /* Only examine the function once. */
3001 if (fun->cannot_be_copied_set)
3002 return reason;
3003
3004 /* We cannot copy a function that receives a non-local goto
3005 because we cannot remap the destination label used in the
3006 function that is performing the non-local goto. */
3007 /* ??? Actually, this should be possible, if we work at it.
3008 No doubt there's just a handful of places that simply
3009 assume it doesn't happen and don't substitute properly. */
3010 if (fun->has_nonlocal_label)
3011 {
3012 reason = G_("function %q+F can never be copied "
3013 "because it receives a non-local goto");
3014 goto fail;
3015 }
3016
3017 FOR_EACH_LOCAL_DECL (fun, ix, decl)
3018 if (TREE_CODE (decl) == VAR_DECL
3019 && TREE_STATIC (decl)
3020 && !DECL_EXTERNAL (decl)
3021 && DECL_INITIAL (decl)
3022 && walk_tree_without_duplicates (&DECL_INITIAL (decl),
3023 has_label_address_in_static_1,
3024 fndecl))
3025 {
3026 reason = G_("function %q+F can never be copied because it saves "
3027 "address of local label in a static variable");
3028 goto fail;
3029 }
3030
3031 fail:
3032 fun->cannot_be_copied_reason = reason;
3033 fun->cannot_be_copied_set = true;
3034 return reason;
3035 }
3036
3037
3038 static const char *inline_forbidden_reason;
3039
3040 /* A callback for walk_gimple_seq to handle statements. Returns non-null
3041 iff a function can not be inlined. Also sets the reason why. */
3042
3043 static tree
3044 inline_forbidden_p_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
3045 struct walk_stmt_info *wip)
3046 {
3047 tree fn = (tree) wip->info;
3048 tree t;
3049 gimple stmt = gsi_stmt (*gsi);
3050
3051 switch (gimple_code (stmt))
3052 {
3053 case GIMPLE_CALL:
3054 /* Refuse to inline alloca call unless user explicitly forced so as
3055 this may change program's memory overhead drastically when the
3056 function using alloca is called in loop. In GCC present in
3057 SPEC2000 inlining into schedule_block cause it to require 2GB of
3058 RAM instead of 256MB. Don't do so for alloca calls emitted for
3059 VLA objects as those can't cause unbounded growth (they're always
3060 wrapped inside stack_save/stack_restore regions. */
3061 if (gimple_alloca_call_p (stmt)
3062 && !gimple_call_alloca_for_var_p (stmt)
3063 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
3064 {
3065 inline_forbidden_reason
3066 = G_("function %q+F can never be inlined because it uses "
3067 "alloca (override using the always_inline attribute)");
3068 *handled_ops_p = true;
3069 return fn;
3070 }
3071
3072 t = gimple_call_fndecl (stmt);
3073 if (t == NULL_TREE)
3074 break;
3075
3076 /* We cannot inline functions that call setjmp. */
3077 if (setjmp_call_p (t))
3078 {
3079 inline_forbidden_reason
3080 = G_("function %q+F can never be inlined because it uses setjmp");
3081 *handled_ops_p = true;
3082 return t;
3083 }
3084
3085 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
3086 switch (DECL_FUNCTION_CODE (t))
3087 {
3088 /* We cannot inline functions that take a variable number of
3089 arguments. */
3090 case BUILT_IN_VA_START:
3091 case BUILT_IN_NEXT_ARG:
3092 case BUILT_IN_VA_END:
3093 inline_forbidden_reason
3094 = G_("function %q+F can never be inlined because it "
3095 "uses variable argument lists");
3096 *handled_ops_p = true;
3097 return t;
3098
3099 case BUILT_IN_LONGJMP:
3100 /* We can't inline functions that call __builtin_longjmp at
3101 all. The non-local goto machinery really requires the
3102 destination be in a different function. If we allow the
3103 function calling __builtin_longjmp to be inlined into the
3104 function calling __builtin_setjmp, Things will Go Awry. */
3105 inline_forbidden_reason
3106 = G_("function %q+F can never be inlined because "
3107 "it uses setjmp-longjmp exception handling");
3108 *handled_ops_p = true;
3109 return t;
3110
3111 case BUILT_IN_NONLOCAL_GOTO:
3112 /* Similarly. */
3113 inline_forbidden_reason
3114 = G_("function %q+F can never be inlined because "
3115 "it uses non-local goto");
3116 *handled_ops_p = true;
3117 return t;
3118
3119 case BUILT_IN_RETURN:
3120 case BUILT_IN_APPLY_ARGS:
3121 /* If a __builtin_apply_args caller would be inlined,
3122 it would be saving arguments of the function it has
3123 been inlined into. Similarly __builtin_return would
3124 return from the function the inline has been inlined into. */
3125 inline_forbidden_reason
3126 = G_("function %q+F can never be inlined because "
3127 "it uses __builtin_return or __builtin_apply_args");
3128 *handled_ops_p = true;
3129 return t;
3130
3131 default:
3132 break;
3133 }
3134 break;
3135
3136 case GIMPLE_GOTO:
3137 t = gimple_goto_dest (stmt);
3138
3139 /* We will not inline a function which uses computed goto. The
3140 addresses of its local labels, which may be tucked into
3141 global storage, are of course not constant across
3142 instantiations, which causes unexpected behavior. */
3143 if (TREE_CODE (t) != LABEL_DECL)
3144 {
3145 inline_forbidden_reason
3146 = G_("function %q+F can never be inlined "
3147 "because it contains a computed goto");
3148 *handled_ops_p = true;
3149 return t;
3150 }
3151 break;
3152
3153 default:
3154 break;
3155 }
3156
3157 *handled_ops_p = false;
3158 return NULL_TREE;
3159 }
3160
3161 /* Return true if FNDECL is a function that cannot be inlined into
3162 another one. */
3163
3164 static bool
3165 inline_forbidden_p (tree fndecl)
3166 {
3167 struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
3168 struct walk_stmt_info wi;
3169 struct pointer_set_t *visited_nodes;
3170 basic_block bb;
3171 bool forbidden_p = false;
3172
3173 /* First check for shared reasons not to copy the code. */
3174 inline_forbidden_reason = copy_forbidden (fun, fndecl);
3175 if (inline_forbidden_reason != NULL)
3176 return true;
3177
3178 /* Next, walk the statements of the function looking for
3179 constraucts we can't handle, or are non-optimal for inlining. */
3180 visited_nodes = pointer_set_create ();
3181 memset (&wi, 0, sizeof (wi));
3182 wi.info = (void *) fndecl;
3183 wi.pset = visited_nodes;
3184
3185 FOR_EACH_BB_FN (bb, fun)
3186 {
3187 gimple ret;
3188 gimple_seq seq = bb_seq (bb);
3189 ret = walk_gimple_seq (seq, inline_forbidden_p_stmt, NULL, &wi);
3190 forbidden_p = (ret != NULL);
3191 if (forbidden_p)
3192 break;
3193 }
3194
3195 pointer_set_destroy (visited_nodes);
3196 return forbidden_p;
3197 }
3198 \f
3199 /* Return false if the function FNDECL cannot be inlined on account of its
3200 attributes, true otherwise. */
3201 static bool
3202 function_attribute_inlinable_p (const_tree fndecl)
3203 {
3204 if (targetm.attribute_table)
3205 {
3206 const_tree a;
3207
3208 for (a = DECL_ATTRIBUTES (fndecl); a; a = TREE_CHAIN (a))
3209 {
3210 const_tree name = TREE_PURPOSE (a);
3211 int i;
3212
3213 for (i = 0; targetm.attribute_table[i].name != NULL; i++)
3214 if (is_attribute_p (targetm.attribute_table[i].name, name))
3215 return targetm.function_attribute_inlinable_p (fndecl);
3216 }
3217 }
3218
3219 return true;
3220 }
3221
3222 /* Returns nonzero if FN is a function that does not have any
3223 fundamental inline blocking properties. */
3224
3225 bool
3226 tree_inlinable_function_p (tree fn)
3227 {
3228 bool inlinable = true;
3229 bool do_warning;
3230 tree always_inline;
3231
3232 /* If we've already decided this function shouldn't be inlined,
3233 there's no need to check again. */
3234 if (DECL_UNINLINABLE (fn))
3235 return false;
3236
3237 /* We only warn for functions declared `inline' by the user. */
3238 do_warning = (warn_inline
3239 && DECL_DECLARED_INLINE_P (fn)
3240 && !DECL_NO_INLINE_WARNING_P (fn)
3241 && !DECL_IN_SYSTEM_HEADER (fn));
3242
3243 always_inline = lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn));
3244
3245 if (flag_no_inline
3246 && always_inline == NULL)
3247 {
3248 if (do_warning)
3249 warning (OPT_Winline, "function %q+F can never be inlined because it "
3250 "is suppressed using -fno-inline", fn);
3251 inlinable = false;
3252 }
3253
3254 else if (!function_attribute_inlinable_p (fn))
3255 {
3256 if (do_warning)
3257 warning (OPT_Winline, "function %q+F can never be inlined because it "
3258 "uses attributes conflicting with inlining", fn);
3259 inlinable = false;
3260 }
3261
3262 else if (inline_forbidden_p (fn))
3263 {
3264 /* See if we should warn about uninlinable functions. Previously,
3265 some of these warnings would be issued while trying to expand
3266 the function inline, but that would cause multiple warnings
3267 about functions that would for example call alloca. But since
3268 this a property of the function, just one warning is enough.
3269 As a bonus we can now give more details about the reason why a
3270 function is not inlinable. */
3271 if (always_inline)
3272 error (inline_forbidden_reason, fn);
3273 else if (do_warning)
3274 warning (OPT_Winline, inline_forbidden_reason, fn);
3275
3276 inlinable = false;
3277 }
3278
3279 /* Squirrel away the result so that we don't have to check again. */
3280 DECL_UNINLINABLE (fn) = !inlinable;
3281
3282 return inlinable;
3283 }
3284
3285 /* Estimate the cost of a memory move. Use machine dependent
3286 word size and take possible memcpy call into account. */
3287
3288 int
3289 estimate_move_cost (tree type)
3290 {
3291 HOST_WIDE_INT size;
3292
3293 gcc_assert (!VOID_TYPE_P (type));
3294
3295 if (TREE_CODE (type) == VECTOR_TYPE)
3296 {
3297 enum machine_mode inner = TYPE_MODE (TREE_TYPE (type));
3298 enum machine_mode simd
3299 = targetm.vectorize.preferred_simd_mode (inner);
3300 int simd_mode_size = GET_MODE_SIZE (simd);
3301 return ((GET_MODE_SIZE (TYPE_MODE (type)) + simd_mode_size - 1)
3302 / simd_mode_size);
3303 }
3304
3305 size = int_size_in_bytes (type);
3306
3307 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO (!optimize_size))
3308 /* Cost of a memcpy call, 3 arguments and the call. */
3309 return 4;
3310 else
3311 return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
3312 }
3313
3314 /* Returns cost of operation CODE, according to WEIGHTS */
3315
3316 static int
3317 estimate_operator_cost (enum tree_code code, eni_weights *weights,
3318 tree op1 ATTRIBUTE_UNUSED, tree op2)
3319 {
3320 switch (code)
3321 {
3322 /* These are "free" conversions, or their presumed cost
3323 is folded into other operations. */
3324 case RANGE_EXPR:
3325 CASE_CONVERT:
3326 case COMPLEX_EXPR:
3327 case PAREN_EXPR:
3328 case VIEW_CONVERT_EXPR:
3329 return 0;
3330
3331 /* Assign cost of 1 to usual operations.
3332 ??? We may consider mapping RTL costs to this. */
3333 case COND_EXPR:
3334 case VEC_COND_EXPR:
3335 case VEC_PERM_EXPR:
3336
3337 case PLUS_EXPR:
3338 case POINTER_PLUS_EXPR:
3339 case MINUS_EXPR:
3340 case MULT_EXPR:
3341 case MULT_HIGHPART_EXPR:
3342 case FMA_EXPR:
3343
3344 case ADDR_SPACE_CONVERT_EXPR:
3345 case FIXED_CONVERT_EXPR:
3346 case FIX_TRUNC_EXPR:
3347
3348 case NEGATE_EXPR:
3349 case FLOAT_EXPR:
3350 case MIN_EXPR:
3351 case MAX_EXPR:
3352 case ABS_EXPR:
3353
3354 case LSHIFT_EXPR:
3355 case RSHIFT_EXPR:
3356 case LROTATE_EXPR:
3357 case RROTATE_EXPR:
3358 case VEC_LSHIFT_EXPR:
3359 case VEC_RSHIFT_EXPR:
3360
3361 case BIT_IOR_EXPR:
3362 case BIT_XOR_EXPR:
3363 case BIT_AND_EXPR:
3364 case BIT_NOT_EXPR:
3365
3366 case TRUTH_ANDIF_EXPR:
3367 case TRUTH_ORIF_EXPR:
3368 case TRUTH_AND_EXPR:
3369 case TRUTH_OR_EXPR:
3370 case TRUTH_XOR_EXPR:
3371 case TRUTH_NOT_EXPR:
3372
3373 case LT_EXPR:
3374 case LE_EXPR:
3375 case GT_EXPR:
3376 case GE_EXPR:
3377 case EQ_EXPR:
3378 case NE_EXPR:
3379 case ORDERED_EXPR:
3380 case UNORDERED_EXPR:
3381
3382 case UNLT_EXPR:
3383 case UNLE_EXPR:
3384 case UNGT_EXPR:
3385 case UNGE_EXPR:
3386 case UNEQ_EXPR:
3387 case LTGT_EXPR:
3388
3389 case CONJ_EXPR:
3390
3391 case PREDECREMENT_EXPR:
3392 case PREINCREMENT_EXPR:
3393 case POSTDECREMENT_EXPR:
3394 case POSTINCREMENT_EXPR:
3395
3396 case REALIGN_LOAD_EXPR:
3397
3398 case REDUC_MAX_EXPR:
3399 case REDUC_MIN_EXPR:
3400 case REDUC_PLUS_EXPR:
3401 case WIDEN_SUM_EXPR:
3402 case WIDEN_MULT_EXPR:
3403 case DOT_PROD_EXPR:
3404 case WIDEN_MULT_PLUS_EXPR:
3405 case WIDEN_MULT_MINUS_EXPR:
3406 case WIDEN_LSHIFT_EXPR:
3407
3408 case VEC_WIDEN_MULT_HI_EXPR:
3409 case VEC_WIDEN_MULT_LO_EXPR:
3410 case VEC_WIDEN_MULT_EVEN_EXPR:
3411 case VEC_WIDEN_MULT_ODD_EXPR:
3412 case VEC_UNPACK_HI_EXPR:
3413 case VEC_UNPACK_LO_EXPR:
3414 case VEC_UNPACK_FLOAT_HI_EXPR:
3415 case VEC_UNPACK_FLOAT_LO_EXPR:
3416 case VEC_PACK_TRUNC_EXPR:
3417 case VEC_PACK_SAT_EXPR:
3418 case VEC_PACK_FIX_TRUNC_EXPR:
3419 case VEC_WIDEN_LSHIFT_HI_EXPR:
3420 case VEC_WIDEN_LSHIFT_LO_EXPR:
3421
3422 return 1;
3423
3424 /* Few special cases of expensive operations. This is useful
3425 to avoid inlining on functions having too many of these. */
3426 case TRUNC_DIV_EXPR:
3427 case CEIL_DIV_EXPR:
3428 case FLOOR_DIV_EXPR:
3429 case ROUND_DIV_EXPR:
3430 case EXACT_DIV_EXPR:
3431 case TRUNC_MOD_EXPR:
3432 case CEIL_MOD_EXPR:
3433 case FLOOR_MOD_EXPR:
3434 case ROUND_MOD_EXPR:
3435 case RDIV_EXPR:
3436 if (TREE_CODE (op2) != INTEGER_CST)
3437 return weights->div_mod_cost;
3438 return 1;
3439
3440 default:
3441 /* We expect a copy assignment with no operator. */
3442 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS);
3443 return 0;
3444 }
3445 }
3446
3447
3448 /* Estimate number of instructions that will be created by expanding
3449 the statements in the statement sequence STMTS.
3450 WEIGHTS contains weights attributed to various constructs. */
3451
3452 static
3453 int estimate_num_insns_seq (gimple_seq stmts, eni_weights *weights)
3454 {
3455 int cost;
3456 gimple_stmt_iterator gsi;
3457
3458 cost = 0;
3459 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
3460 cost += estimate_num_insns (gsi_stmt (gsi), weights);
3461
3462 return cost;
3463 }
3464
3465
3466 /* Estimate number of instructions that will be created by expanding STMT.
3467 WEIGHTS contains weights attributed to various constructs. */
3468
3469 int
3470 estimate_num_insns (gimple stmt, eni_weights *weights)
3471 {
3472 unsigned cost, i;
3473 enum gimple_code code = gimple_code (stmt);
3474 tree lhs;
3475 tree rhs;
3476
3477 switch (code)
3478 {
3479 case GIMPLE_ASSIGN:
3480 /* Try to estimate the cost of assignments. We have three cases to
3481 deal with:
3482 1) Simple assignments to registers;
3483 2) Stores to things that must live in memory. This includes
3484 "normal" stores to scalars, but also assignments of large
3485 structures, or constructors of big arrays;
3486
3487 Let us look at the first two cases, assuming we have "a = b + C":
3488 <GIMPLE_ASSIGN <var_decl "a">
3489 <plus_expr <var_decl "b"> <constant C>>
3490 If "a" is a GIMPLE register, the assignment to it is free on almost
3491 any target, because "a" usually ends up in a real register. Hence
3492 the only cost of this expression comes from the PLUS_EXPR, and we
3493 can ignore the GIMPLE_ASSIGN.
3494 If "a" is not a GIMPLE register, the assignment to "a" will most
3495 likely be a real store, so the cost of the GIMPLE_ASSIGN is the cost
3496 of moving something into "a", which we compute using the function
3497 estimate_move_cost. */
3498 if (gimple_clobber_p (stmt))
3499 return 0; /* ={v} {CLOBBER} stmt expands to nothing. */
3500
3501 lhs = gimple_assign_lhs (stmt);
3502 rhs = gimple_assign_rhs1 (stmt);
3503
3504 cost = 0;
3505
3506 /* Account for the cost of moving to / from memory. */
3507 if (gimple_store_p (stmt))
3508 cost += estimate_move_cost (TREE_TYPE (lhs));
3509 if (gimple_assign_load_p (stmt))
3510 cost += estimate_move_cost (TREE_TYPE (rhs));
3511
3512 cost += estimate_operator_cost (gimple_assign_rhs_code (stmt), weights,
3513 gimple_assign_rhs1 (stmt),
3514 get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
3515 == GIMPLE_BINARY_RHS
3516 ? gimple_assign_rhs2 (stmt) : NULL);
3517 break;
3518
3519 case GIMPLE_COND:
3520 cost = 1 + estimate_operator_cost (gimple_cond_code (stmt), weights,
3521 gimple_op (stmt, 0),
3522 gimple_op (stmt, 1));
3523 break;
3524
3525 case GIMPLE_SWITCH:
3526 /* Take into account cost of the switch + guess 2 conditional jumps for
3527 each case label.
3528
3529 TODO: once the switch expansion logic is sufficiently separated, we can
3530 do better job on estimating cost of the switch. */
3531 if (weights->time_based)
3532 cost = floor_log2 (gimple_switch_num_labels (stmt)) * 2;
3533 else
3534 cost = gimple_switch_num_labels (stmt) * 2;
3535 break;
3536
3537 case GIMPLE_CALL:
3538 {
3539 tree decl = gimple_call_fndecl (stmt);
3540 struct cgraph_node *node = NULL;
3541
3542 /* Do not special case builtins where we see the body.
3543 This just confuse inliner. */
3544 if (!decl || !(node = cgraph_get_node (decl)) || node->analyzed)
3545 ;
3546 /* For buitins that are likely expanded to nothing or
3547 inlined do not account operand costs. */
3548 else if (is_simple_builtin (decl))
3549 return 0;
3550 else if (is_inexpensive_builtin (decl))
3551 return weights->target_builtin_call_cost;
3552 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3553 {
3554 /* We canonicalize x * x to pow (x, 2.0) with -ffast-math, so
3555 specialize the cheap expansion we do here.
3556 ??? This asks for a more general solution. */
3557 switch (DECL_FUNCTION_CODE (decl))
3558 {
3559 case BUILT_IN_POW:
3560 case BUILT_IN_POWF:
3561 case BUILT_IN_POWL:
3562 if (TREE_CODE (gimple_call_arg (stmt, 1)) == REAL_CST
3563 && REAL_VALUES_EQUAL
3564 (TREE_REAL_CST (gimple_call_arg (stmt, 1)), dconst2))
3565 return estimate_operator_cost (MULT_EXPR, weights,
3566 gimple_call_arg (stmt, 0),
3567 gimple_call_arg (stmt, 0));
3568 break;
3569
3570 default:
3571 break;
3572 }
3573 }
3574
3575 cost = node ? weights->call_cost : weights->indirect_call_cost;
3576 if (gimple_call_lhs (stmt))
3577 cost += estimate_move_cost (TREE_TYPE (gimple_call_lhs (stmt)));
3578 for (i = 0; i < gimple_call_num_args (stmt); i++)
3579 {
3580 tree arg = gimple_call_arg (stmt, i);
3581 cost += estimate_move_cost (TREE_TYPE (arg));
3582 }
3583 break;
3584 }
3585
3586 case GIMPLE_RETURN:
3587 return weights->return_cost;
3588
3589 case GIMPLE_GOTO:
3590 case GIMPLE_LABEL:
3591 case GIMPLE_NOP:
3592 case GIMPLE_PHI:
3593 case GIMPLE_PREDICT:
3594 case GIMPLE_DEBUG:
3595 return 0;
3596
3597 case GIMPLE_ASM:
3598 return asm_str_count (gimple_asm_string (stmt));
3599
3600 case GIMPLE_RESX:
3601 /* This is either going to be an external function call with one
3602 argument, or two register copy statements plus a goto. */
3603 return 2;
3604
3605 case GIMPLE_EH_DISPATCH:
3606 /* ??? This is going to turn into a switch statement. Ideally
3607 we'd have a look at the eh region and estimate the number of
3608 edges involved. */
3609 return 10;
3610
3611 case GIMPLE_BIND:
3612 return estimate_num_insns_seq (gimple_bind_body (stmt), weights);
3613
3614 case GIMPLE_EH_FILTER:
3615 return estimate_num_insns_seq (gimple_eh_filter_failure (stmt), weights);
3616
3617 case GIMPLE_CATCH:
3618 return estimate_num_insns_seq (gimple_catch_handler (stmt), weights);
3619
3620 case GIMPLE_TRY:
3621 return (estimate_num_insns_seq (gimple_try_eval (stmt), weights)
3622 + estimate_num_insns_seq (gimple_try_cleanup (stmt), weights));
3623
3624 /* OpenMP directives are generally very expensive. */
3625
3626 case GIMPLE_OMP_RETURN:
3627 case GIMPLE_OMP_SECTIONS_SWITCH:
3628 case GIMPLE_OMP_ATOMIC_STORE:
3629 case GIMPLE_OMP_CONTINUE:
3630 /* ...except these, which are cheap. */
3631 return 0;
3632
3633 case GIMPLE_OMP_ATOMIC_LOAD:
3634 return weights->omp_cost;
3635
3636 case GIMPLE_OMP_FOR:
3637 return (weights->omp_cost
3638 + estimate_num_insns_seq (gimple_omp_body (stmt), weights)
3639 + estimate_num_insns_seq (gimple_omp_for_pre_body (stmt), weights));
3640
3641 case GIMPLE_OMP_PARALLEL:
3642 case GIMPLE_OMP_TASK:
3643 case GIMPLE_OMP_CRITICAL:
3644 case GIMPLE_OMP_MASTER:
3645 case GIMPLE_OMP_ORDERED:
3646 case GIMPLE_OMP_SECTION:
3647 case GIMPLE_OMP_SECTIONS:
3648 case GIMPLE_OMP_SINGLE:
3649 return (weights->omp_cost
3650 + estimate_num_insns_seq (gimple_omp_body (stmt), weights));
3651
3652 case GIMPLE_TRANSACTION:
3653 return (weights->tm_cost
3654 + estimate_num_insns_seq (gimple_transaction_body (stmt),
3655 weights));
3656
3657 default:
3658 gcc_unreachable ();
3659 }
3660
3661 return cost;
3662 }
3663
3664 /* Estimate number of instructions that will be created by expanding
3665 function FNDECL. WEIGHTS contains weights attributed to various
3666 constructs. */
3667
3668 int
3669 estimate_num_insns_fn (tree fndecl, eni_weights *weights)
3670 {
3671 struct function *my_function = DECL_STRUCT_FUNCTION (fndecl);
3672 gimple_stmt_iterator bsi;
3673 basic_block bb;
3674 int n = 0;
3675
3676 gcc_assert (my_function && my_function->cfg);
3677 FOR_EACH_BB_FN (bb, my_function)
3678 {
3679 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
3680 n += estimate_num_insns (gsi_stmt (bsi), weights);
3681 }
3682
3683 return n;
3684 }
3685
3686
3687 /* Initializes weights used by estimate_num_insns. */
3688
3689 void
3690 init_inline_once (void)
3691 {
3692 eni_size_weights.call_cost = 1;
3693 eni_size_weights.indirect_call_cost = 3;
3694 eni_size_weights.target_builtin_call_cost = 1;
3695 eni_size_weights.div_mod_cost = 1;
3696 eni_size_weights.omp_cost = 40;
3697 eni_size_weights.tm_cost = 10;
3698 eni_size_weights.time_based = false;
3699 eni_size_weights.return_cost = 1;
3700
3701 /* Estimating time for call is difficult, since we have no idea what the
3702 called function does. In the current uses of eni_time_weights,
3703 underestimating the cost does less harm than overestimating it, so
3704 we choose a rather small value here. */
3705 eni_time_weights.call_cost = 10;
3706 eni_time_weights.indirect_call_cost = 15;
3707 eni_time_weights.target_builtin_call_cost = 1;
3708 eni_time_weights.div_mod_cost = 10;
3709 eni_time_weights.omp_cost = 40;
3710 eni_time_weights.tm_cost = 40;
3711 eni_time_weights.time_based = true;
3712 eni_time_weights.return_cost = 2;
3713 }
3714
3715 /* Estimate the number of instructions in a gimple_seq. */
3716
3717 int
3718 count_insns_seq (gimple_seq seq, eni_weights *weights)
3719 {
3720 gimple_stmt_iterator gsi;
3721 int n = 0;
3722 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
3723 n += estimate_num_insns (gsi_stmt (gsi), weights);
3724
3725 return n;
3726 }
3727
3728
3729 /* Install new lexical TREE_BLOCK underneath 'current_block'. */
3730
3731 static void
3732 prepend_lexical_block (tree current_block, tree new_block)
3733 {
3734 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (current_block);
3735 BLOCK_SUBBLOCKS (current_block) = new_block;
3736 BLOCK_SUPERCONTEXT (new_block) = current_block;
3737 }
3738
3739 /* Add local variables from CALLEE to CALLER. */
3740
3741 static inline void
3742 add_local_variables (struct function *callee, struct function *caller,
3743 copy_body_data *id)
3744 {
3745 tree var;
3746 unsigned ix;
3747
3748 FOR_EACH_LOCAL_DECL (callee, ix, var)
3749 if (!can_be_nonlocal (var, id))
3750 {
3751 tree new_var = remap_decl (var, id);
3752
3753 /* Remap debug-expressions. */
3754 if (TREE_CODE (new_var) == VAR_DECL
3755 && DECL_DEBUG_EXPR_IS_FROM (new_var)
3756 && new_var != var)
3757 {
3758 tree tem = DECL_DEBUG_EXPR (var);
3759 bool old_regimplify = id->regimplify;
3760 id->remapping_type_depth++;
3761 walk_tree (&tem, copy_tree_body_r, id, NULL);
3762 id->remapping_type_depth--;
3763 id->regimplify = old_regimplify;
3764 SET_DECL_DEBUG_EXPR (new_var, tem);
3765 }
3766 add_local_decl (caller, new_var);
3767 }
3768 }
3769
3770 /* If STMT is a GIMPLE_CALL, replace it with its inline expansion. */
3771
3772 static bool
3773 expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
3774 {
3775 tree use_retvar;
3776 tree fn;
3777 struct pointer_map_t *st, *dst;
3778 tree return_slot;
3779 tree modify_dest;
3780 location_t saved_location;
3781 struct cgraph_edge *cg_edge;
3782 cgraph_inline_failed_t reason;
3783 basic_block return_block;
3784 edge e;
3785 gimple_stmt_iterator gsi, stmt_gsi;
3786 bool successfully_inlined = FALSE;
3787 bool purge_dead_abnormal_edges;
3788
3789 /* Set input_location here so we get the right instantiation context
3790 if we call instantiate_decl from inlinable_function_p. */
3791 /* FIXME: instantiate_decl isn't called by inlinable_function_p. */
3792 saved_location = input_location;
3793 input_location = gimple_location (stmt);
3794
3795 /* From here on, we're only interested in CALL_EXPRs. */
3796 if (gimple_code (stmt) != GIMPLE_CALL)
3797 goto egress;
3798
3799 cg_edge = cgraph_edge (id->dst_node, stmt);
3800 gcc_checking_assert (cg_edge);
3801 /* First, see if we can figure out what function is being called.
3802 If we cannot, then there is no hope of inlining the function. */
3803 if (cg_edge->indirect_unknown_callee)
3804 goto egress;
3805 fn = cg_edge->callee->symbol.decl;
3806 gcc_checking_assert (fn);
3807
3808 /* If FN is a declaration of a function in a nested scope that was
3809 globally declared inline, we don't set its DECL_INITIAL.
3810 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
3811 C++ front-end uses it for cdtors to refer to their internal
3812 declarations, that are not real functions. Fortunately those
3813 don't have trees to be saved, so we can tell by checking their
3814 gimple_body. */
3815 if (!DECL_INITIAL (fn)
3816 && DECL_ABSTRACT_ORIGIN (fn)
3817 && gimple_has_body_p (DECL_ABSTRACT_ORIGIN (fn)))
3818 fn = DECL_ABSTRACT_ORIGIN (fn);
3819
3820 /* Don't try to inline functions that are not well-suited to inlining. */
3821 if (cg_edge->inline_failed)
3822 {
3823 reason = cg_edge->inline_failed;
3824 /* If this call was originally indirect, we do not want to emit any
3825 inlining related warnings or sorry messages because there are no
3826 guarantees regarding those. */
3827 if (cg_edge->indirect_inlining_edge)
3828 goto egress;
3829
3830 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
3831 /* For extern inline functions that get redefined we always
3832 silently ignored always_inline flag. Better behaviour would
3833 be to be able to keep both bodies and use extern inline body
3834 for inlining, but we can't do that because frontends overwrite
3835 the body. */
3836 && !cg_edge->callee->local.redefined_extern_inline
3837 /* Avoid warnings during early inline pass. */
3838 && cgraph_global_info_ready
3839 /* PR 20090218-1_0.c. Body can be provided by another module. */
3840 && (reason != CIF_BODY_NOT_AVAILABLE || !flag_generate_lto))
3841 {
3842 error ("inlining failed in call to always_inline %q+F: %s", fn,
3843 cgraph_inline_failed_string (reason));
3844 error ("called from here");
3845 }
3846 else if (warn_inline
3847 && DECL_DECLARED_INLINE_P (fn)
3848 && !DECL_NO_INLINE_WARNING_P (fn)
3849 && !DECL_IN_SYSTEM_HEADER (fn)
3850 && reason != CIF_UNSPECIFIED
3851 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
3852 /* Do not warn about not inlined recursive calls. */
3853 && !cgraph_edge_recursive_p (cg_edge)
3854 /* Avoid warnings during early inline pass. */
3855 && cgraph_global_info_ready)
3856 {
3857 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
3858 fn, _(cgraph_inline_failed_string (reason)));
3859 warning (OPT_Winline, "called from here");
3860 }
3861 goto egress;
3862 }
3863 fn = cg_edge->callee->symbol.decl;
3864
3865 #ifdef ENABLE_CHECKING
3866 if (cg_edge->callee->symbol.decl != id->dst_node->symbol.decl)
3867 verify_cgraph_node (cg_edge->callee);
3868 #endif
3869
3870 /* We will be inlining this callee. */
3871 id->eh_lp_nr = lookup_stmt_eh_lp (stmt);
3872
3873 /* Update the callers EH personality. */
3874 if (DECL_FUNCTION_PERSONALITY (cg_edge->callee->symbol.decl))
3875 DECL_FUNCTION_PERSONALITY (cg_edge->caller->symbol.decl)
3876 = DECL_FUNCTION_PERSONALITY (cg_edge->callee->symbol.decl);
3877
3878 /* Split the block holding the GIMPLE_CALL. */
3879 e = split_block (bb, stmt);
3880 bb = e->src;
3881 return_block = e->dest;
3882 remove_edge (e);
3883
3884 /* split_block splits after the statement; work around this by
3885 moving the call into the second block manually. Not pretty,
3886 but seems easier than doing the CFG manipulation by hand
3887 when the GIMPLE_CALL is in the last statement of BB. */
3888 stmt_gsi = gsi_last_bb (bb);
3889 gsi_remove (&stmt_gsi, false);
3890
3891 /* If the GIMPLE_CALL was in the last statement of BB, it may have
3892 been the source of abnormal edges. In this case, schedule
3893 the removal of dead abnormal edges. */
3894 gsi = gsi_start_bb (return_block);
3895 if (gsi_end_p (gsi))
3896 {
3897 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
3898 purge_dead_abnormal_edges = true;
3899 }
3900 else
3901 {
3902 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
3903 purge_dead_abnormal_edges = false;
3904 }
3905
3906 stmt_gsi = gsi_start_bb (return_block);
3907
3908 /* Build a block containing code to initialize the arguments, the
3909 actual inline expansion of the body, and a label for the return
3910 statements within the function to jump to. The type of the
3911 statement expression is the return type of the function call. */
3912 id->block = make_node (BLOCK);
3913 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
3914 BLOCK_SOURCE_LOCATION (id->block) = input_location;
3915 if (gimple_block (stmt))
3916 prepend_lexical_block (gimple_block (stmt), id->block);
3917
3918 /* Local declarations will be replaced by their equivalents in this
3919 map. */
3920 st = id->decl_map;
3921 id->decl_map = pointer_map_create ();
3922 dst = id->debug_map;
3923 id->debug_map = NULL;
3924
3925 /* Record the function we are about to inline. */
3926 id->src_fn = fn;
3927 id->src_node = cg_edge->callee;
3928 id->src_cfun = DECL_STRUCT_FUNCTION (fn);
3929 id->gimple_call = stmt;
3930
3931 gcc_assert (!id->src_cfun->after_inlining);
3932
3933 id->entry_bb = bb;
3934 if (lookup_attribute ("cold", DECL_ATTRIBUTES (fn)))
3935 {
3936 gimple_stmt_iterator si = gsi_last_bb (bb);
3937 gsi_insert_after (&si, gimple_build_predict (PRED_COLD_FUNCTION,
3938 NOT_TAKEN),
3939 GSI_NEW_STMT);
3940 }
3941 initialize_inlined_parameters (id, stmt, fn, bb);
3942
3943 if (DECL_INITIAL (fn))
3944 {
3945 tree *var;
3946
3947 prepend_lexical_block (id->block, remap_blocks (DECL_INITIAL (fn), id));
3948 gcc_checking_assert (BLOCK_SUBBLOCKS (id->block)
3949 && (BLOCK_CHAIN (BLOCK_SUBBLOCKS (id->block))
3950 == NULL_TREE));
3951 /* Move vars for PARM_DECLs from DECL_INITIAL block to id->block,
3952 otherwise for DWARF DW_TAG_formal_parameter will not be children of
3953 DW_TAG_inlined_subroutine, but of a DW_TAG_lexical_block
3954 under it. The parameters can be then evaluated in the debugger,
3955 but don't show in backtraces. */
3956 for (var = &BLOCK_VARS (BLOCK_SUBBLOCKS (id->block)); *var; )
3957 if (TREE_CODE (DECL_ORIGIN (*var)) == PARM_DECL)
3958 {
3959 tree v = *var;
3960 *var = TREE_CHAIN (v);
3961 TREE_CHAIN (v) = BLOCK_VARS (id->block);
3962 BLOCK_VARS (id->block) = v;
3963 }
3964 else
3965 var = &TREE_CHAIN (*var);
3966 }
3967
3968 /* Return statements in the function body will be replaced by jumps
3969 to the RET_LABEL. */
3970 gcc_assert (DECL_INITIAL (fn));
3971 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
3972
3973 /* Find the LHS to which the result of this call is assigned. */
3974 return_slot = NULL;
3975 if (gimple_call_lhs (stmt))
3976 {
3977 modify_dest = gimple_call_lhs (stmt);
3978
3979 /* The function which we are inlining might not return a value,
3980 in which case we should issue a warning that the function
3981 does not return a value. In that case the optimizers will
3982 see that the variable to which the value is assigned was not
3983 initialized. We do not want to issue a warning about that
3984 uninitialized variable. */
3985 if (DECL_P (modify_dest))
3986 TREE_NO_WARNING (modify_dest) = 1;
3987
3988 if (gimple_call_return_slot_opt_p (stmt))
3989 {
3990 return_slot = modify_dest;
3991 modify_dest = NULL;
3992 }
3993 }
3994 else
3995 modify_dest = NULL;
3996
3997 /* If we are inlining a call to the C++ operator new, we don't want
3998 to use type based alias analysis on the return value. Otherwise
3999 we may get confused if the compiler sees that the inlined new
4000 function returns a pointer which was just deleted. See bug
4001 33407. */
4002 if (DECL_IS_OPERATOR_NEW (fn))
4003 {
4004 return_slot = NULL;
4005 modify_dest = NULL;
4006 }
4007
4008 /* Declare the return variable for the function. */
4009 use_retvar = declare_return_variable (id, return_slot, modify_dest, bb);
4010
4011 /* Add local vars in this inlined callee to caller. */
4012 add_local_variables (id->src_cfun, cfun, id);
4013
4014 if (dump_file && (dump_flags & TDF_DETAILS))
4015 {
4016 fprintf (dump_file, "Inlining ");
4017 print_generic_expr (dump_file, id->src_fn, 0);
4018 fprintf (dump_file, " to ");
4019 print_generic_expr (dump_file, id->dst_fn, 0);
4020 fprintf (dump_file, " with frequency %i\n", cg_edge->frequency);
4021 }
4022
4023 /* This is it. Duplicate the callee body. Assume callee is
4024 pre-gimplified. Note that we must not alter the caller
4025 function in any way before this point, as this CALL_EXPR may be
4026 a self-referential call; if we're calling ourselves, we need to
4027 duplicate our body before altering anything. */
4028 copy_body (id, bb->count,
4029 cg_edge->frequency * REG_BR_PROB_BASE / CGRAPH_FREQ_BASE,
4030 bb, return_block, NULL, NULL);
4031
4032 /* Reset the escaped solution. */
4033 if (cfun->gimple_df)
4034 pt_solution_reset (&cfun->gimple_df->escaped);
4035
4036 /* Clean up. */
4037 if (id->debug_map)
4038 {
4039 pointer_map_destroy (id->debug_map);
4040 id->debug_map = dst;
4041 }
4042 pointer_map_destroy (id->decl_map);
4043 id->decl_map = st;
4044
4045 /* Unlink the calls virtual operands before replacing it. */
4046 unlink_stmt_vdef (stmt);
4047
4048 /* If the inlined function returns a result that we care about,
4049 substitute the GIMPLE_CALL with an assignment of the return
4050 variable to the LHS of the call. That is, if STMT was
4051 'a = foo (...)', substitute the call with 'a = USE_RETVAR'. */
4052 if (use_retvar && gimple_call_lhs (stmt))
4053 {
4054 gimple old_stmt = stmt;
4055 stmt = gimple_build_assign (gimple_call_lhs (stmt), use_retvar);
4056 gsi_replace (&stmt_gsi, stmt, false);
4057 maybe_clean_or_replace_eh_stmt (old_stmt, stmt);
4058 }
4059 else
4060 {
4061 /* Handle the case of inlining a function with no return
4062 statement, which causes the return value to become undefined. */
4063 if (gimple_call_lhs (stmt)
4064 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
4065 {
4066 tree name = gimple_call_lhs (stmt);
4067 tree var = SSA_NAME_VAR (name);
4068 tree def = ssa_default_def (cfun, var);
4069
4070 if (def)
4071 {
4072 /* If the variable is used undefined, make this name
4073 undefined via a move. */
4074 stmt = gimple_build_assign (gimple_call_lhs (stmt), def);
4075 gsi_replace (&stmt_gsi, stmt, true);
4076 }
4077 else
4078 {
4079 /* Otherwise make this variable undefined. */
4080 gsi_remove (&stmt_gsi, true);
4081 set_ssa_default_def (cfun, var, name);
4082 SSA_NAME_DEF_STMT (name) = gimple_build_nop ();
4083 }
4084 }
4085 else
4086 gsi_remove (&stmt_gsi, true);
4087 }
4088
4089 if (purge_dead_abnormal_edges)
4090 {
4091 gimple_purge_dead_eh_edges (return_block);
4092 gimple_purge_dead_abnormal_call_edges (return_block);
4093 }
4094
4095 /* If the value of the new expression is ignored, that's OK. We
4096 don't warn about this for CALL_EXPRs, so we shouldn't warn about
4097 the equivalent inlined version either. */
4098 if (is_gimple_assign (stmt))
4099 {
4100 gcc_assert (gimple_assign_single_p (stmt)
4101 || CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)));
4102 TREE_USED (gimple_assign_rhs1 (stmt)) = 1;
4103 }
4104
4105 /* Output the inlining info for this abstract function, since it has been
4106 inlined. If we don't do this now, we can lose the information about the
4107 variables in the function when the blocks get blown away as soon as we
4108 remove the cgraph node. */
4109 (*debug_hooks->outlining_inline_function) (cg_edge->callee->symbol.decl);
4110
4111 /* Update callgraph if needed. */
4112 cgraph_remove_node (cg_edge->callee);
4113
4114 id->block = NULL_TREE;
4115 successfully_inlined = TRUE;
4116
4117 egress:
4118 input_location = saved_location;
4119 return successfully_inlined;
4120 }
4121
4122 /* Expand call statements reachable from STMT_P.
4123 We can only have CALL_EXPRs as the "toplevel" tree code or nested
4124 in a MODIFY_EXPR. */
4125
4126 static bool
4127 gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
4128 {
4129 gimple_stmt_iterator gsi;
4130
4131 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4132 {
4133 gimple stmt = gsi_stmt (gsi);
4134
4135 if (is_gimple_call (stmt)
4136 && expand_call_inline (bb, stmt, id))
4137 return true;
4138 }
4139
4140 return false;
4141 }
4142
4143
4144 /* Walk all basic blocks created after FIRST and try to fold every statement
4145 in the STATEMENTS pointer set. */
4146
4147 static void
4148 fold_marked_statements (int first, struct pointer_set_t *statements)
4149 {
4150 for (; first < n_basic_blocks; first++)
4151 if (BASIC_BLOCK (first))
4152 {
4153 gimple_stmt_iterator gsi;
4154
4155 for (gsi = gsi_start_bb (BASIC_BLOCK (first));
4156 !gsi_end_p (gsi);
4157 gsi_next (&gsi))
4158 if (pointer_set_contains (statements, gsi_stmt (gsi)))
4159 {
4160 gimple old_stmt = gsi_stmt (gsi);
4161 tree old_decl = is_gimple_call (old_stmt) ? gimple_call_fndecl (old_stmt) : 0;
4162
4163 if (old_decl && DECL_BUILT_IN (old_decl))
4164 {
4165 /* Folding builtins can create multiple instructions,
4166 we need to look at all of them. */
4167 gimple_stmt_iterator i2 = gsi;
4168 gsi_prev (&i2);
4169 if (fold_stmt (&gsi))
4170 {
4171 gimple new_stmt;
4172 /* If a builtin at the end of a bb folded into nothing,
4173 the following loop won't work. */
4174 if (gsi_end_p (gsi))
4175 {
4176 cgraph_update_edges_for_call_stmt (old_stmt,
4177 old_decl, NULL);
4178 break;
4179 }
4180 if (gsi_end_p (i2))
4181 i2 = gsi_start_bb (BASIC_BLOCK (first));
4182 else
4183 gsi_next (&i2);
4184 while (1)
4185 {
4186 new_stmt = gsi_stmt (i2);
4187 update_stmt (new_stmt);
4188 cgraph_update_edges_for_call_stmt (old_stmt, old_decl,
4189 new_stmt);
4190
4191 if (new_stmt == gsi_stmt (gsi))
4192 {
4193 /* It is okay to check only for the very last
4194 of these statements. If it is a throwing
4195 statement nothing will change. If it isn't
4196 this can remove EH edges. If that weren't
4197 correct then because some intermediate stmts
4198 throw, but not the last one. That would mean
4199 we'd have to split the block, which we can't
4200 here and we'd loose anyway. And as builtins
4201 probably never throw, this all
4202 is mood anyway. */
4203 if (maybe_clean_or_replace_eh_stmt (old_stmt,
4204 new_stmt))
4205 gimple_purge_dead_eh_edges (BASIC_BLOCK (first));
4206 break;
4207 }
4208 gsi_next (&i2);
4209 }
4210 }
4211 }
4212 else if (fold_stmt (&gsi))
4213 {
4214 /* Re-read the statement from GSI as fold_stmt() may
4215 have changed it. */
4216 gimple new_stmt = gsi_stmt (gsi);
4217 update_stmt (new_stmt);
4218
4219 if (is_gimple_call (old_stmt)
4220 || is_gimple_call (new_stmt))
4221 cgraph_update_edges_for_call_stmt (old_stmt, old_decl,
4222 new_stmt);
4223
4224 if (maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt))
4225 gimple_purge_dead_eh_edges (BASIC_BLOCK (first));
4226 }
4227 }
4228 }
4229 }
4230
4231 /* Return true if BB has at least one abnormal outgoing edge. */
4232
4233 static inline bool
4234 has_abnormal_outgoing_edge_p (basic_block bb)
4235 {
4236 edge e;
4237 edge_iterator ei;
4238
4239 FOR_EACH_EDGE (e, ei, bb->succs)
4240 if (e->flags & EDGE_ABNORMAL)
4241 return true;
4242
4243 return false;
4244 }
4245
4246 /* Expand calls to inline functions in the body of FN. */
4247
4248 unsigned int
4249 optimize_inline_calls (tree fn)
4250 {
4251 copy_body_data id;
4252 basic_block bb;
4253 int last = n_basic_blocks;
4254 struct gimplify_ctx gctx;
4255 bool inlined_p = false;
4256
4257 /* Clear out ID. */
4258 memset (&id, 0, sizeof (id));
4259
4260 id.src_node = id.dst_node = cgraph_get_node (fn);
4261 gcc_assert (id.dst_node->analyzed);
4262 id.dst_fn = fn;
4263 /* Or any functions that aren't finished yet. */
4264 if (current_function_decl)
4265 id.dst_fn = current_function_decl;
4266
4267 id.copy_decl = copy_decl_maybe_to_var;
4268 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4269 id.transform_new_cfg = false;
4270 id.transform_return_to_modify = true;
4271 id.transform_lang_insert_block = NULL;
4272 id.statements_to_fold = pointer_set_create ();
4273
4274 push_gimplify_context (&gctx);
4275
4276 /* We make no attempts to keep dominance info up-to-date. */
4277 free_dominance_info (CDI_DOMINATORS);
4278 free_dominance_info (CDI_POST_DOMINATORS);
4279
4280 /* Register specific gimple functions. */
4281 gimple_register_cfg_hooks ();
4282
4283 /* Reach the trees by walking over the CFG, and note the
4284 enclosing basic-blocks in the call edges. */
4285 /* We walk the blocks going forward, because inlined function bodies
4286 will split id->current_basic_block, and the new blocks will
4287 follow it; we'll trudge through them, processing their CALL_EXPRs
4288 along the way. */
4289 FOR_EACH_BB (bb)
4290 inlined_p |= gimple_expand_calls_inline (bb, &id);
4291
4292 pop_gimplify_context (NULL);
4293
4294 #ifdef ENABLE_CHECKING
4295 {
4296 struct cgraph_edge *e;
4297
4298 verify_cgraph_node (id.dst_node);
4299
4300 /* Double check that we inlined everything we are supposed to inline. */
4301 for (e = id.dst_node->callees; e; e = e->next_callee)
4302 gcc_assert (e->inline_failed);
4303 }
4304 #endif
4305
4306 /* Fold queued statements. */
4307 fold_marked_statements (last, id.statements_to_fold);
4308 pointer_set_destroy (id.statements_to_fold);
4309
4310 gcc_assert (!id.debug_stmts.exists ());
4311
4312 /* If we didn't inline into the function there is nothing to do. */
4313 if (!inlined_p)
4314 return 0;
4315
4316 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4317 number_blocks (fn);
4318
4319 delete_unreachable_blocks_update_callgraph (&id);
4320 #ifdef ENABLE_CHECKING
4321 verify_cgraph_node (id.dst_node);
4322 #endif
4323
4324 /* It would be nice to check SSA/CFG/statement consistency here, but it is
4325 not possible yet - the IPA passes might make various functions to not
4326 throw and they don't care to proactively update local EH info. This is
4327 done later in fixup_cfg pass that also execute the verification. */
4328 return (TODO_update_ssa
4329 | TODO_cleanup_cfg
4330 | (gimple_in_ssa_p (cfun) ? TODO_remove_unused_locals : 0)
4331 | (gimple_in_ssa_p (cfun) ? TODO_update_address_taken : 0)
4332 | (profile_status != PROFILE_ABSENT ? TODO_rebuild_frequencies : 0));
4333 }
4334
4335 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
4336
4337 tree
4338 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
4339 {
4340 enum tree_code code = TREE_CODE (*tp);
4341 enum tree_code_class cl = TREE_CODE_CLASS (code);
4342
4343 /* We make copies of most nodes. */
4344 if (IS_EXPR_CODE_CLASS (cl)
4345 || code == TREE_LIST
4346 || code == TREE_VEC
4347 || code == TYPE_DECL
4348 || code == OMP_CLAUSE)
4349 {
4350 /* Because the chain gets clobbered when we make a copy, we save it
4351 here. */
4352 tree chain = NULL_TREE, new_tree;
4353
4354 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
4355 chain = TREE_CHAIN (*tp);
4356
4357 /* Copy the node. */
4358 new_tree = copy_node (*tp);
4359
4360 /* Propagate mudflap marked-ness. */
4361 if (flag_mudflap && mf_marked_p (*tp))
4362 mf_mark (new_tree);
4363
4364 *tp = new_tree;
4365
4366 /* Now, restore the chain, if appropriate. That will cause
4367 walk_tree to walk into the chain as well. */
4368 if (code == PARM_DECL
4369 || code == TREE_LIST
4370 || code == OMP_CLAUSE)
4371 TREE_CHAIN (*tp) = chain;
4372
4373 /* For now, we don't update BLOCKs when we make copies. So, we
4374 have to nullify all BIND_EXPRs. */
4375 if (TREE_CODE (*tp) == BIND_EXPR)
4376 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
4377 }
4378 else if (code == CONSTRUCTOR)
4379 {
4380 /* CONSTRUCTOR nodes need special handling because
4381 we need to duplicate the vector of elements. */
4382 tree new_tree;
4383
4384 new_tree = copy_node (*tp);
4385
4386 /* Propagate mudflap marked-ness. */
4387 if (flag_mudflap && mf_marked_p (*tp))
4388 mf_mark (new_tree);
4389
4390 CONSTRUCTOR_ELTS (new_tree) = vec_safe_copy (CONSTRUCTOR_ELTS (*tp));
4391 *tp = new_tree;
4392 }
4393 else if (code == STATEMENT_LIST)
4394 /* We used to just abort on STATEMENT_LIST, but we can run into them
4395 with statement-expressions (c++/40975). */
4396 copy_statement_list (tp);
4397 else if (TREE_CODE_CLASS (code) == tcc_type)
4398 *walk_subtrees = 0;
4399 else if (TREE_CODE_CLASS (code) == tcc_declaration)
4400 *walk_subtrees = 0;
4401 else if (TREE_CODE_CLASS (code) == tcc_constant)
4402 *walk_subtrees = 0;
4403 return NULL_TREE;
4404 }
4405
4406 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
4407 information indicating to what new SAVE_EXPR this one should be mapped,
4408 use that one. Otherwise, create a new node and enter it in ST. FN is
4409 the function into which the copy will be placed. */
4410
4411 static void
4412 remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
4413 {
4414 struct pointer_map_t *st = (struct pointer_map_t *) st_;
4415 tree *n;
4416 tree t;
4417
4418 /* See if we already encountered this SAVE_EXPR. */
4419 n = (tree *) pointer_map_contains (st, *tp);
4420
4421 /* If we didn't already remap this SAVE_EXPR, do so now. */
4422 if (!n)
4423 {
4424 t = copy_node (*tp);
4425
4426 /* Remember this SAVE_EXPR. */
4427 *pointer_map_insert (st, *tp) = t;
4428 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
4429 *pointer_map_insert (st, t) = t;
4430 }
4431 else
4432 {
4433 /* We've already walked into this SAVE_EXPR; don't do it again. */
4434 *walk_subtrees = 0;
4435 t = *n;
4436 }
4437
4438 /* Replace this SAVE_EXPR with the copy. */
4439 *tp = t;
4440 }
4441
4442 /* Called via walk_tree. If *TP points to a DECL_STMT for a local label,
4443 copies the declaration and enters it in the splay_tree in DATA (which is
4444 really an `copy_body_data *'). */
4445
4446 static tree
4447 mark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
4448 void *data)
4449 {
4450 copy_body_data *id = (copy_body_data *) data;
4451
4452 /* Don't walk into types. */
4453 if (TYPE_P (*tp))
4454 *walk_subtrees = 0;
4455
4456 else if (TREE_CODE (*tp) == LABEL_EXPR)
4457 {
4458 tree decl = TREE_OPERAND (*tp, 0);
4459
4460 /* Copy the decl and remember the copy. */
4461 insert_decl_map (id, decl, id->copy_decl (decl, id));
4462 }
4463
4464 return NULL_TREE;
4465 }
4466
4467 /* Perform any modifications to EXPR required when it is unsaved. Does
4468 not recurse into EXPR's subtrees. */
4469
4470 static void
4471 unsave_expr_1 (tree expr)
4472 {
4473 switch (TREE_CODE (expr))
4474 {
4475 case TARGET_EXPR:
4476 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
4477 It's OK for this to happen if it was part of a subtree that
4478 isn't immediately expanded, such as operand 2 of another
4479 TARGET_EXPR. */
4480 if (TREE_OPERAND (expr, 1))
4481 break;
4482
4483 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
4484 TREE_OPERAND (expr, 3) = NULL_TREE;
4485 break;
4486
4487 default:
4488 break;
4489 }
4490 }
4491
4492 /* Called via walk_tree when an expression is unsaved. Using the
4493 splay_tree pointed to by ST (which is really a `splay_tree'),
4494 remaps all local declarations to appropriate replacements. */
4495
4496 static tree
4497 unsave_r (tree *tp, int *walk_subtrees, void *data)
4498 {
4499 copy_body_data *id = (copy_body_data *) data;
4500 struct pointer_map_t *st = id->decl_map;
4501 tree *n;
4502
4503 /* Only a local declaration (variable or label). */
4504 if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
4505 || TREE_CODE (*tp) == LABEL_DECL)
4506 {
4507 /* Lookup the declaration. */
4508 n = (tree *) pointer_map_contains (st, *tp);
4509
4510 /* If it's there, remap it. */
4511 if (n)
4512 *tp = *n;
4513 }
4514
4515 else if (TREE_CODE (*tp) == STATEMENT_LIST)
4516 gcc_unreachable ();
4517 else if (TREE_CODE (*tp) == BIND_EXPR)
4518 copy_bind_expr (tp, walk_subtrees, id);
4519 else if (TREE_CODE (*tp) == SAVE_EXPR
4520 || TREE_CODE (*tp) == TARGET_EXPR)
4521 remap_save_expr (tp, st, walk_subtrees);
4522 else
4523 {
4524 copy_tree_r (tp, walk_subtrees, NULL);
4525
4526 /* Do whatever unsaving is required. */
4527 unsave_expr_1 (*tp);
4528 }
4529
4530 /* Keep iterating. */
4531 return NULL_TREE;
4532 }
4533
4534 /* Copies everything in EXPR and replaces variables, labels
4535 and SAVE_EXPRs local to EXPR. */
4536
4537 tree
4538 unsave_expr_now (tree expr)
4539 {
4540 copy_body_data id;
4541
4542 /* There's nothing to do for NULL_TREE. */
4543 if (expr == 0)
4544 return expr;
4545
4546 /* Set up ID. */
4547 memset (&id, 0, sizeof (id));
4548 id.src_fn = current_function_decl;
4549 id.dst_fn = current_function_decl;
4550 id.decl_map = pointer_map_create ();
4551 id.debug_map = NULL;
4552
4553 id.copy_decl = copy_decl_no_change;
4554 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4555 id.transform_new_cfg = false;
4556 id.transform_return_to_modify = false;
4557 id.transform_lang_insert_block = NULL;
4558
4559 /* Walk the tree once to find local labels. */
4560 walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
4561
4562 /* Walk the tree again, copying, remapping, and unsaving. */
4563 walk_tree (&expr, unsave_r, &id, NULL);
4564
4565 /* Clean up. */
4566 pointer_map_destroy (id.decl_map);
4567 if (id.debug_map)
4568 pointer_map_destroy (id.debug_map);
4569
4570 return expr;
4571 }
4572
4573 /* Called via walk_gimple_seq. If *GSIP points to a GIMPLE_LABEL for a local
4574 label, copies the declaration and enters it in the splay_tree in DATA (which
4575 is really a 'copy_body_data *'. */
4576
4577 static tree
4578 mark_local_labels_stmt (gimple_stmt_iterator *gsip,
4579 bool *handled_ops_p ATTRIBUTE_UNUSED,
4580 struct walk_stmt_info *wi)
4581 {
4582 copy_body_data *id = (copy_body_data *) wi->info;
4583 gimple stmt = gsi_stmt (*gsip);
4584
4585 if (gimple_code (stmt) == GIMPLE_LABEL)
4586 {
4587 tree decl = gimple_label_label (stmt);
4588
4589 /* Copy the decl and remember the copy. */
4590 insert_decl_map (id, decl, id->copy_decl (decl, id));
4591 }
4592
4593 return NULL_TREE;
4594 }
4595
4596
4597 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
4598 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
4599 remaps all local declarations to appropriate replacements in gimple
4600 operands. */
4601
4602 static tree
4603 replace_locals_op (tree *tp, int *walk_subtrees, void *data)
4604 {
4605 struct walk_stmt_info *wi = (struct walk_stmt_info*) data;
4606 copy_body_data *id = (copy_body_data *) wi->info;
4607 struct pointer_map_t *st = id->decl_map;
4608 tree *n;
4609 tree expr = *tp;
4610
4611 /* Only a local declaration (variable or label). */
4612 if ((TREE_CODE (expr) == VAR_DECL
4613 && !TREE_STATIC (expr))
4614 || TREE_CODE (expr) == LABEL_DECL)
4615 {
4616 /* Lookup the declaration. */
4617 n = (tree *) pointer_map_contains (st, expr);
4618
4619 /* If it's there, remap it. */
4620 if (n)
4621 *tp = *n;
4622 *walk_subtrees = 0;
4623 }
4624 else if (TREE_CODE (expr) == STATEMENT_LIST
4625 || TREE_CODE (expr) == BIND_EXPR
4626 || TREE_CODE (expr) == SAVE_EXPR)
4627 gcc_unreachable ();
4628 else if (TREE_CODE (expr) == TARGET_EXPR)
4629 {
4630 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
4631 It's OK for this to happen if it was part of a subtree that
4632 isn't immediately expanded, such as operand 2 of another
4633 TARGET_EXPR. */
4634 if (!TREE_OPERAND (expr, 1))
4635 {
4636 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
4637 TREE_OPERAND (expr, 3) = NULL_TREE;
4638 }
4639 }
4640
4641 /* Keep iterating. */
4642 return NULL_TREE;
4643 }
4644
4645
4646 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
4647 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
4648 remaps all local declarations to appropriate replacements in gimple
4649 statements. */
4650
4651 static tree
4652 replace_locals_stmt (gimple_stmt_iterator *gsip,
4653 bool *handled_ops_p ATTRIBUTE_UNUSED,
4654 struct walk_stmt_info *wi)
4655 {
4656 copy_body_data *id = (copy_body_data *) wi->info;
4657 gimple stmt = gsi_stmt (*gsip);
4658
4659 if (gimple_code (stmt) == GIMPLE_BIND)
4660 {
4661 tree block = gimple_bind_block (stmt);
4662
4663 if (block)
4664 {
4665 remap_block (&block, id);
4666 gimple_bind_set_block (stmt, block);
4667 }
4668
4669 /* This will remap a lot of the same decls again, but this should be
4670 harmless. */
4671 if (gimple_bind_vars (stmt))
4672 gimple_bind_set_vars (stmt, remap_decls (gimple_bind_vars (stmt),
4673 NULL, id));
4674 }
4675
4676 /* Keep iterating. */
4677 return NULL_TREE;
4678 }
4679
4680
4681 /* Copies everything in SEQ and replaces variables and labels local to
4682 current_function_decl. */
4683
4684 gimple_seq
4685 copy_gimple_seq_and_replace_locals (gimple_seq seq)
4686 {
4687 copy_body_data id;
4688 struct walk_stmt_info wi;
4689 struct pointer_set_t *visited;
4690 gimple_seq copy;
4691
4692 /* There's nothing to do for NULL_TREE. */
4693 if (seq == NULL)
4694 return seq;
4695
4696 /* Set up ID. */
4697 memset (&id, 0, sizeof (id));
4698 id.src_fn = current_function_decl;
4699 id.dst_fn = current_function_decl;
4700 id.decl_map = pointer_map_create ();
4701 id.debug_map = NULL;
4702
4703 id.copy_decl = copy_decl_no_change;
4704 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4705 id.transform_new_cfg = false;
4706 id.transform_return_to_modify = false;
4707 id.transform_lang_insert_block = NULL;
4708
4709 /* Walk the tree once to find local labels. */
4710 memset (&wi, 0, sizeof (wi));
4711 visited = pointer_set_create ();
4712 wi.info = &id;
4713 wi.pset = visited;
4714 walk_gimple_seq (seq, mark_local_labels_stmt, NULL, &wi);
4715 pointer_set_destroy (visited);
4716
4717 copy = gimple_seq_copy (seq);
4718
4719 /* Walk the copy, remapping decls. */
4720 memset (&wi, 0, sizeof (wi));
4721 wi.info = &id;
4722 walk_gimple_seq (copy, replace_locals_stmt, replace_locals_op, &wi);
4723
4724 /* Clean up. */
4725 pointer_map_destroy (id.decl_map);
4726 if (id.debug_map)
4727 pointer_map_destroy (id.debug_map);
4728
4729 return copy;
4730 }
4731
4732
4733 /* Allow someone to determine if SEARCH is a child of TOP from gdb. */
4734
4735 static tree
4736 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
4737 {
4738 if (*tp == data)
4739 return (tree) data;
4740 else
4741 return NULL;
4742 }
4743
4744 DEBUG_FUNCTION bool
4745 debug_find_tree (tree top, tree search)
4746 {
4747 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
4748 }
4749
4750
4751 /* Declare the variables created by the inliner. Add all the variables in
4752 VARS to BIND_EXPR. */
4753
4754 static void
4755 declare_inline_vars (tree block, tree vars)
4756 {
4757 tree t;
4758 for (t = vars; t; t = DECL_CHAIN (t))
4759 {
4760 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
4761 gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
4762 add_local_decl (cfun, t);
4763 }
4764
4765 if (block)
4766 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
4767 }
4768
4769 /* Copy NODE (which must be a DECL). The DECL originally was in the FROM_FN,
4770 but now it will be in the TO_FN. PARM_TO_VAR means enable PARM_DECL to
4771 VAR_DECL translation. */
4772
4773 static tree
4774 copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
4775 {
4776 /* Don't generate debug information for the copy if we wouldn't have
4777 generated it for the copy either. */
4778 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
4779 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
4780
4781 /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
4782 declaration inspired this copy. */
4783 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
4784
4785 /* The new variable/label has no RTL, yet. */
4786 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
4787 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
4788 SET_DECL_RTL (copy, 0);
4789
4790 /* These args would always appear unused, if not for this. */
4791 TREE_USED (copy) = 1;
4792
4793 /* Set the context for the new declaration. */
4794 if (!DECL_CONTEXT (decl))
4795 /* Globals stay global. */
4796 ;
4797 else if (DECL_CONTEXT (decl) != id->src_fn)
4798 /* Things that weren't in the scope of the function we're inlining
4799 from aren't in the scope we're inlining to, either. */
4800 ;
4801 else if (TREE_STATIC (decl))
4802 /* Function-scoped static variables should stay in the original
4803 function. */
4804 ;
4805 else
4806 /* Ordinary automatic local variables are now in the scope of the
4807 new function. */
4808 DECL_CONTEXT (copy) = id->dst_fn;
4809
4810 return copy;
4811 }
4812
4813 static tree
4814 copy_decl_to_var (tree decl, copy_body_data *id)
4815 {
4816 tree copy, type;
4817
4818 gcc_assert (TREE_CODE (decl) == PARM_DECL
4819 || TREE_CODE (decl) == RESULT_DECL);
4820
4821 type = TREE_TYPE (decl);
4822
4823 copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
4824 VAR_DECL, DECL_NAME (decl), type);
4825 if (DECL_PT_UID_SET_P (decl))
4826 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
4827 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4828 TREE_READONLY (copy) = TREE_READONLY (decl);
4829 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
4830 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
4831
4832 return copy_decl_for_dup_finish (id, decl, copy);
4833 }
4834
4835 /* Like copy_decl_to_var, but create a return slot object instead of a
4836 pointer variable for return by invisible reference. */
4837
4838 static tree
4839 copy_result_decl_to_var (tree decl, copy_body_data *id)
4840 {
4841 tree copy, type;
4842
4843 gcc_assert (TREE_CODE (decl) == PARM_DECL
4844 || TREE_CODE (decl) == RESULT_DECL);
4845
4846 type = TREE_TYPE (decl);
4847 if (DECL_BY_REFERENCE (decl))
4848 type = TREE_TYPE (type);
4849
4850 copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
4851 VAR_DECL, DECL_NAME (decl), type);
4852 if (DECL_PT_UID_SET_P (decl))
4853 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
4854 TREE_READONLY (copy) = TREE_READONLY (decl);
4855 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
4856 if (!DECL_BY_REFERENCE (decl))
4857 {
4858 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4859 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
4860 }
4861
4862 return copy_decl_for_dup_finish (id, decl, copy);
4863 }
4864
4865 tree
4866 copy_decl_no_change (tree decl, copy_body_data *id)
4867 {
4868 tree copy;
4869
4870 copy = copy_node (decl);
4871
4872 /* The COPY is not abstract; it will be generated in DST_FN. */
4873 DECL_ABSTRACT (copy) = 0;
4874 lang_hooks.dup_lang_specific_decl (copy);
4875
4876 /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
4877 been taken; it's for internal bookkeeping in expand_goto_internal. */
4878 if (TREE_CODE (copy) == LABEL_DECL)
4879 {
4880 TREE_ADDRESSABLE (copy) = 0;
4881 LABEL_DECL_UID (copy) = -1;
4882 }
4883
4884 return copy_decl_for_dup_finish (id, decl, copy);
4885 }
4886
4887 static tree
4888 copy_decl_maybe_to_var (tree decl, copy_body_data *id)
4889 {
4890 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
4891 return copy_decl_to_var (decl, id);
4892 else
4893 return copy_decl_no_change (decl, id);
4894 }
4895
4896 /* Return a copy of the function's argument tree. */
4897 static tree
4898 copy_arguments_for_versioning (tree orig_parm, copy_body_data * id,
4899 bitmap args_to_skip, tree *vars)
4900 {
4901 tree arg, *parg;
4902 tree new_parm = NULL;
4903 int i = 0;
4904
4905 parg = &new_parm;
4906
4907 for (arg = orig_parm; arg; arg = DECL_CHAIN (arg), i++)
4908 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
4909 {
4910 tree new_tree = remap_decl (arg, id);
4911 if (TREE_CODE (new_tree) != PARM_DECL)
4912 new_tree = id->copy_decl (arg, id);
4913 lang_hooks.dup_lang_specific_decl (new_tree);
4914 *parg = new_tree;
4915 parg = &DECL_CHAIN (new_tree);
4916 }
4917 else if (!pointer_map_contains (id->decl_map, arg))
4918 {
4919 /* Make an equivalent VAR_DECL. If the argument was used
4920 as temporary variable later in function, the uses will be
4921 replaced by local variable. */
4922 tree var = copy_decl_to_var (arg, id);
4923 insert_decl_map (id, arg, var);
4924 /* Declare this new variable. */
4925 DECL_CHAIN (var) = *vars;
4926 *vars = var;
4927 }
4928 return new_parm;
4929 }
4930
4931 /* Return a copy of the function's static chain. */
4932 static tree
4933 copy_static_chain (tree static_chain, copy_body_data * id)
4934 {
4935 tree *chain_copy, *pvar;
4936
4937 chain_copy = &static_chain;
4938 for (pvar = chain_copy; *pvar; pvar = &DECL_CHAIN (*pvar))
4939 {
4940 tree new_tree = remap_decl (*pvar, id);
4941 lang_hooks.dup_lang_specific_decl (new_tree);
4942 DECL_CHAIN (new_tree) = DECL_CHAIN (*pvar);
4943 *pvar = new_tree;
4944 }
4945 return static_chain;
4946 }
4947
4948 /* Return true if the function is allowed to be versioned.
4949 This is a guard for the versioning functionality. */
4950
4951 bool
4952 tree_versionable_function_p (tree fndecl)
4953 {
4954 return (!lookup_attribute ("noclone", DECL_ATTRIBUTES (fndecl))
4955 && copy_forbidden (DECL_STRUCT_FUNCTION (fndecl), fndecl) == NULL);
4956 }
4957
4958 /* Delete all unreachable basic blocks and update callgraph.
4959 Doing so is somewhat nontrivial because we need to update all clones and
4960 remove inline function that become unreachable. */
4961
4962 static bool
4963 delete_unreachable_blocks_update_callgraph (copy_body_data *id)
4964 {
4965 bool changed = false;
4966 basic_block b, next_bb;
4967
4968 find_unreachable_blocks ();
4969
4970 /* Delete all unreachable basic blocks. */
4971
4972 for (b = ENTRY_BLOCK_PTR->next_bb; b != EXIT_BLOCK_PTR; b = next_bb)
4973 {
4974 next_bb = b->next_bb;
4975
4976 if (!(b->flags & BB_REACHABLE))
4977 {
4978 gimple_stmt_iterator bsi;
4979
4980 for (bsi = gsi_start_bb (b); !gsi_end_p (bsi); gsi_next (&bsi))
4981 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_CALL)
4982 {
4983 struct cgraph_edge *e;
4984 struct cgraph_node *node;
4985
4986 if ((e = cgraph_edge (id->dst_node, gsi_stmt (bsi))) != NULL)
4987 {
4988 if (!e->inline_failed)
4989 cgraph_remove_node_and_inline_clones (e->callee, id->dst_node);
4990 else
4991 cgraph_remove_edge (e);
4992 }
4993 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES
4994 && id->dst_node->clones)
4995 for (node = id->dst_node->clones; node != id->dst_node;)
4996 {
4997 if ((e = cgraph_edge (node, gsi_stmt (bsi))) != NULL)
4998 {
4999 if (!e->inline_failed)
5000 cgraph_remove_node_and_inline_clones (e->callee, id->dst_node);
5001 else
5002 cgraph_remove_edge (e);
5003 }
5004
5005 if (node->clones)
5006 node = node->clones;
5007 else if (node->next_sibling_clone)
5008 node = node->next_sibling_clone;
5009 else
5010 {
5011 while (node != id->dst_node && !node->next_sibling_clone)
5012 node = node->clone_of;
5013 if (node != id->dst_node)
5014 node = node->next_sibling_clone;
5015 }
5016 }
5017 }
5018 delete_basic_block (b);
5019 changed = true;
5020 }
5021 }
5022
5023 return changed;
5024 }
5025
5026 /* Update clone info after duplication. */
5027
5028 static void
5029 update_clone_info (copy_body_data * id)
5030 {
5031 struct cgraph_node *node;
5032 if (!id->dst_node->clones)
5033 return;
5034 for (node = id->dst_node->clones; node != id->dst_node;)
5035 {
5036 /* First update replace maps to match the new body. */
5037 if (node->clone.tree_map)
5038 {
5039 unsigned int i;
5040 for (i = 0; i < vec_safe_length (node->clone.tree_map); i++)
5041 {
5042 struct ipa_replace_map *replace_info;
5043 replace_info = (*node->clone.tree_map)[i];
5044 walk_tree (&replace_info->old_tree, copy_tree_body_r, id, NULL);
5045 walk_tree (&replace_info->new_tree, copy_tree_body_r, id, NULL);
5046 }
5047 }
5048 if (node->clones)
5049 node = node->clones;
5050 else if (node->next_sibling_clone)
5051 node = node->next_sibling_clone;
5052 else
5053 {
5054 while (node != id->dst_node && !node->next_sibling_clone)
5055 node = node->clone_of;
5056 if (node != id->dst_node)
5057 node = node->next_sibling_clone;
5058 }
5059 }
5060 }
5061
5062 /* Create a copy of a function's tree.
5063 OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
5064 of the original function and the new copied function
5065 respectively. In case we want to replace a DECL
5066 tree with another tree while duplicating the function's
5067 body, TREE_MAP represents the mapping between these
5068 trees. If UPDATE_CLONES is set, the call_stmt fields
5069 of edges of clones of the function will be updated.
5070
5071 If non-NULL ARGS_TO_SKIP determine function parameters to remove
5072 from new version.
5073 If SKIP_RETURN is true, the new version will return void.
5074 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
5075 If non_NULL NEW_ENTRY determine new entry BB of the clone.
5076 */
5077 void
5078 tree_function_versioning (tree old_decl, tree new_decl,
5079 vec<ipa_replace_map_p, va_gc> *tree_map,
5080 bool update_clones, bitmap args_to_skip,
5081 bool skip_return, bitmap blocks_to_copy,
5082 basic_block new_entry)
5083 {
5084 struct cgraph_node *old_version_node;
5085 struct cgraph_node *new_version_node;
5086 copy_body_data id;
5087 tree p;
5088 unsigned i;
5089 struct ipa_replace_map *replace_info;
5090 basic_block old_entry_block, bb;
5091 vec<gimple> init_stmts;
5092 init_stmts.create (10);
5093 tree vars = NULL_TREE;
5094
5095 gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
5096 && TREE_CODE (new_decl) == FUNCTION_DECL);
5097 DECL_POSSIBLY_INLINED (old_decl) = 1;
5098
5099 old_version_node = cgraph_get_node (old_decl);
5100 gcc_checking_assert (old_version_node);
5101 new_version_node = cgraph_get_node (new_decl);
5102 gcc_checking_assert (new_version_node);
5103
5104 /* Copy over debug args. */
5105 if (DECL_HAS_DEBUG_ARGS_P (old_decl))
5106 {
5107 vec<tree, va_gc> **new_debug_args, **old_debug_args;
5108 gcc_checking_assert (decl_debug_args_lookup (new_decl) == NULL);
5109 DECL_HAS_DEBUG_ARGS_P (new_decl) = 0;
5110 old_debug_args = decl_debug_args_lookup (old_decl);
5111 if (old_debug_args)
5112 {
5113 new_debug_args = decl_debug_args_insert (new_decl);
5114 *new_debug_args = vec_safe_copy (*old_debug_args);
5115 }
5116 }
5117
5118 /* Output the inlining info for this abstract function, since it has been
5119 inlined. If we don't do this now, we can lose the information about the
5120 variables in the function when the blocks get blown away as soon as we
5121 remove the cgraph node. */
5122 (*debug_hooks->outlining_inline_function) (old_decl);
5123
5124 DECL_ARTIFICIAL (new_decl) = 1;
5125 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
5126 DECL_FUNCTION_PERSONALITY (new_decl) = DECL_FUNCTION_PERSONALITY (old_decl);
5127
5128 /* Prepare the data structures for the tree copy. */
5129 memset (&id, 0, sizeof (id));
5130
5131 /* Generate a new name for the new version. */
5132 id.statements_to_fold = pointer_set_create ();
5133
5134 id.decl_map = pointer_map_create ();
5135 id.debug_map = NULL;
5136 id.src_fn = old_decl;
5137 id.dst_fn = new_decl;
5138 id.src_node = old_version_node;
5139 id.dst_node = new_version_node;
5140 id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
5141 if (id.src_node->ipa_transforms_to_apply.exists ())
5142 {
5143 vec<ipa_opt_pass> old_transforms_to_apply
5144 = id.dst_node->ipa_transforms_to_apply;
5145 unsigned int i;
5146
5147 id.dst_node->ipa_transforms_to_apply
5148 = id.src_node->ipa_transforms_to_apply.copy ();
5149 for (i = 0; i < old_transforms_to_apply.length (); i++)
5150 id.dst_node->ipa_transforms_to_apply.safe_push (old_transforms_to_apply[i]);
5151 old_transforms_to_apply.release ();
5152 }
5153
5154 id.copy_decl = copy_decl_no_change;
5155 id.transform_call_graph_edges
5156 = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
5157 id.transform_new_cfg = true;
5158 id.transform_return_to_modify = false;
5159 id.transform_lang_insert_block = NULL;
5160
5161 old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
5162 (DECL_STRUCT_FUNCTION (old_decl));
5163 initialize_cfun (new_decl, old_decl,
5164 old_entry_block->count);
5165 DECL_STRUCT_FUNCTION (new_decl)->gimple_df->ipa_pta
5166 = id.src_cfun->gimple_df->ipa_pta;
5167
5168 /* Copy the function's static chain. */
5169 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
5170 if (p)
5171 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
5172 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
5173 &id);
5174
5175 /* If there's a tree_map, prepare for substitution. */
5176 if (tree_map)
5177 for (i = 0; i < tree_map->length (); i++)
5178 {
5179 gimple init;
5180 replace_info = (*tree_map)[i];
5181 if (replace_info->replace_p)
5182 {
5183 if (!replace_info->old_tree)
5184 {
5185 int i = replace_info->parm_num;
5186 tree parm;
5187 for (parm = DECL_ARGUMENTS (old_decl); i; parm = DECL_CHAIN (parm))
5188 i --;
5189 replace_info->old_tree = parm;
5190 }
5191 gcc_assert (TREE_CODE (replace_info->old_tree) == PARM_DECL);
5192 init = setup_one_parameter (&id, replace_info->old_tree,
5193 replace_info->new_tree, id.src_fn,
5194 NULL,
5195 &vars);
5196 if (init)
5197 init_stmts.safe_push (init);
5198 }
5199 }
5200 /* Copy the function's arguments. */
5201 if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
5202 DECL_ARGUMENTS (new_decl) =
5203 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id,
5204 args_to_skip, &vars);
5205
5206 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
5207 BLOCK_SUPERCONTEXT (DECL_INITIAL (new_decl)) = new_decl;
5208
5209 declare_inline_vars (DECL_INITIAL (new_decl), vars);
5210
5211 if (!vec_safe_is_empty (DECL_STRUCT_FUNCTION (old_decl)->local_decls))
5212 /* Add local vars. */
5213 add_local_variables (DECL_STRUCT_FUNCTION (old_decl), cfun, &id);
5214
5215 if (DECL_RESULT (old_decl) == NULL_TREE)
5216 ;
5217 else if (skip_return && !VOID_TYPE_P (TREE_TYPE (DECL_RESULT (old_decl))))
5218 {
5219 DECL_RESULT (new_decl)
5220 = build_decl (DECL_SOURCE_LOCATION (DECL_RESULT (old_decl)),
5221 RESULT_DECL, NULL_TREE, void_type_node);
5222 DECL_CONTEXT (DECL_RESULT (new_decl)) = new_decl;
5223 cfun->returns_struct = 0;
5224 cfun->returns_pcc_struct = 0;
5225 }
5226 else
5227 {
5228 tree old_name;
5229 DECL_RESULT (new_decl) = remap_decl (DECL_RESULT (old_decl), &id);
5230 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
5231 if (gimple_in_ssa_p (id.src_cfun)
5232 && DECL_BY_REFERENCE (DECL_RESULT (old_decl))
5233 && (old_name = ssa_default_def (id.src_cfun, DECL_RESULT (old_decl))))
5234 {
5235 tree new_name = make_ssa_name (DECL_RESULT (new_decl), NULL);
5236 insert_decl_map (&id, old_name, new_name);
5237 SSA_NAME_DEF_STMT (new_name) = gimple_build_nop ();
5238 set_ssa_default_def (cfun, DECL_RESULT (new_decl), new_name);
5239 }
5240 }
5241
5242 /* Copy the Function's body. */
5243 copy_body (&id, old_entry_block->count, REG_BR_PROB_BASE,
5244 ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, blocks_to_copy, new_entry);
5245
5246 /* Renumber the lexical scoping (non-code) blocks consecutively. */
5247 number_blocks (new_decl);
5248
5249 /* We want to create the BB unconditionally, so that the addition of
5250 debug stmts doesn't affect BB count, which may in the end cause
5251 codegen differences. */
5252 bb = split_edge (single_succ_edge (ENTRY_BLOCK_PTR));
5253 while (init_stmts.length ())
5254 insert_init_stmt (&id, bb, init_stmts.pop ());
5255 update_clone_info (&id);
5256
5257 /* Remap the nonlocal_goto_save_area, if any. */
5258 if (cfun->nonlocal_goto_save_area)
5259 {
5260 struct walk_stmt_info wi;
5261
5262 memset (&wi, 0, sizeof (wi));
5263 wi.info = &id;
5264 walk_tree (&cfun->nonlocal_goto_save_area, remap_gimple_op_r, &wi, NULL);
5265 }
5266
5267 /* Clean up. */
5268 pointer_map_destroy (id.decl_map);
5269 if (id.debug_map)
5270 pointer_map_destroy (id.debug_map);
5271 free_dominance_info (CDI_DOMINATORS);
5272 free_dominance_info (CDI_POST_DOMINATORS);
5273
5274 fold_marked_statements (0, id.statements_to_fold);
5275 pointer_set_destroy (id.statements_to_fold);
5276 fold_cond_expr_cond ();
5277 delete_unreachable_blocks_update_callgraph (&id);
5278 if (id.dst_node->analyzed)
5279 cgraph_rebuild_references ();
5280 update_ssa (TODO_update_ssa);
5281
5282 /* After partial cloning we need to rescale frequencies, so they are
5283 within proper range in the cloned function. */
5284 if (new_entry)
5285 {
5286 struct cgraph_edge *e;
5287 rebuild_frequencies ();
5288
5289 new_version_node->count = ENTRY_BLOCK_PTR->count;
5290 for (e = new_version_node->callees; e; e = e->next_callee)
5291 {
5292 basic_block bb = gimple_bb (e->call_stmt);
5293 e->frequency = compute_call_stmt_bb_frequency (current_function_decl,
5294 bb);
5295 e->count = bb->count;
5296 }
5297 for (e = new_version_node->indirect_calls; e; e = e->next_callee)
5298 {
5299 basic_block bb = gimple_bb (e->call_stmt);
5300 e->frequency = compute_call_stmt_bb_frequency (current_function_decl,
5301 bb);
5302 e->count = bb->count;
5303 }
5304 }
5305
5306 free_dominance_info (CDI_DOMINATORS);
5307 free_dominance_info (CDI_POST_DOMINATORS);
5308
5309 gcc_assert (!id.debug_stmts.exists ());
5310 init_stmts.release ();
5311 pop_cfun ();
5312 return;
5313 }
5314
5315 /* EXP is CALL_EXPR present in a GENERIC expression tree. Try to integrate
5316 the callee and return the inlined body on success. */
5317
5318 tree
5319 maybe_inline_call_in_expr (tree exp)
5320 {
5321 tree fn = get_callee_fndecl (exp);
5322
5323 /* We can only try to inline "const" functions. */
5324 if (fn && TREE_READONLY (fn) && DECL_SAVED_TREE (fn))
5325 {
5326 struct pointer_map_t *decl_map = pointer_map_create ();
5327 call_expr_arg_iterator iter;
5328 copy_body_data id;
5329 tree param, arg, t;
5330
5331 /* Remap the parameters. */
5332 for (param = DECL_ARGUMENTS (fn), arg = first_call_expr_arg (exp, &iter);
5333 param;
5334 param = DECL_CHAIN (param), arg = next_call_expr_arg (&iter))
5335 *pointer_map_insert (decl_map, param) = arg;
5336
5337 memset (&id, 0, sizeof (id));
5338 id.src_fn = fn;
5339 id.dst_fn = current_function_decl;
5340 id.src_cfun = DECL_STRUCT_FUNCTION (fn);
5341 id.decl_map = decl_map;
5342
5343 id.copy_decl = copy_decl_no_change;
5344 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5345 id.transform_new_cfg = false;
5346 id.transform_return_to_modify = true;
5347 id.transform_lang_insert_block = NULL;
5348
5349 /* Make sure not to unshare trees behind the front-end's back
5350 since front-end specific mechanisms may rely on sharing. */
5351 id.regimplify = false;
5352 id.do_not_unshare = true;
5353
5354 /* We're not inside any EH region. */
5355 id.eh_lp_nr = 0;
5356
5357 t = copy_tree_body (&id);
5358 pointer_map_destroy (decl_map);
5359
5360 /* We can only return something suitable for use in a GENERIC
5361 expression tree. */
5362 if (TREE_CODE (t) == MODIFY_EXPR)
5363 return TREE_OPERAND (t, 1);
5364 }
5365
5366 return NULL_TREE;
5367 }
5368
5369 /* Duplicate a type, fields and all. */
5370
5371 tree
5372 build_duplicate_type (tree type)
5373 {
5374 struct copy_body_data id;
5375
5376 memset (&id, 0, sizeof (id));
5377 id.src_fn = current_function_decl;
5378 id.dst_fn = current_function_decl;
5379 id.src_cfun = cfun;
5380 id.decl_map = pointer_map_create ();
5381 id.debug_map = NULL;
5382 id.copy_decl = copy_decl_no_change;
5383
5384 type = remap_type_1 (type, &id);
5385
5386 pointer_map_destroy (id.decl_map);
5387 if (id.debug_map)
5388 pointer_map_destroy (id.debug_map);
5389
5390 TYPE_CANONICAL (type) = type;
5391
5392 return type;
5393 }