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