]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-inline.c
string_view (_S_max_size): Remove.
[thirdparty/gcc.git] / gcc / tree-inline.c
CommitLineData
ac534736 1/* Tree inlining.
d1e082c2 2 Copyright (C) 2001-2013 Free Software Foundation, Inc.
588d3ade
AO
3 Contributed by Alexandre Oliva <aoliva@redhat.com>
4
54a7b573 5This file is part of GCC.
588d3ade 6
54a7b573 7GCC is free software; you can redistribute it and/or modify
588d3ade 8it under the terms of the GNU General Public License as published by
9dcd6f09 9the Free Software Foundation; either version 3, or (at your option)
588d3ade
AO
10any later version.
11
54a7b573 12GCC is distributed in the hope that it will be useful,
588d3ade
AO
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
588d3ade
AO
20
21#include "config.h"
22#include "system.h"
4977bab6
ZW
23#include "coretypes.h"
24#include "tm.h"
718f9c0f 25#include "diagnostic-core.h"
588d3ade 26#include "tree.h"
d8a2d370
DN
27#include "stor-layout.h"
28#include "calls.h"
588d3ade 29#include "tree-inline.h"
d4e4baa9
AO
30#include "flags.h"
31#include "params.h"
32#include "input.h"
33#include "insn-config.h"
d4e4baa9 34#include "hashtab.h"
d23c55c2 35#include "langhooks.h"
e21aff8a
SB
36#include "basic-block.h"
37#include "tree-iterator.h"
ddd2d57e 38#include "intl.h"
18f429e2 39#include "gimple.h"
45b0be94 40#include "gimplify.h"
5be5c238 41#include "gimple-iterator.h"
18f429e2 42#include "gimplify-me.h"
5be5c238 43#include "gimple-walk.h"
442b4905
AM
44#include "gimple-ssa.h"
45#include "tree-cfg.h"
46#include "tree-phinodes.h"
47#include "ssa-iterators.h"
d8a2d370 48#include "stringpool.h"
442b4905
AM
49#include "tree-ssanames.h"
50#include "tree-into-ssa.h"
d8a2d370 51#include "expr.h"
442b4905 52#include "tree-dfa.h"
7a300452 53#include "tree-ssa.h"
18c6ada9 54#include "function.h"
cf835838 55#include "tree-pretty-print.h"
e21aff8a 56#include "except.h"
1eb3331e 57#include "debug.h"
e21aff8a 58#include "pointer-set.h"
19734dd8 59#include "ipa-prop.h"
6946b3f7 60#include "value-prof.h"
110cfe1c 61#include "tree-pass.h"
18177c7e 62#include "target.h"
a9e0d843 63#include "cfgloop.h"
d4e4baa9 64
2eb79bbb
SB
65#include "rtl.h" /* FIXME: For asm_str_count. */
66
6de9cd9a
DN
67/* I'm not real happy about this, but we need to handle gimple and
68 non-gimple trees. */
588d3ade 69
1b369fae 70/* Inlining, Cloning, Versioning, Parallelization
e21aff8a
SB
71
72 Inlining: a function body is duplicated, but the PARM_DECLs are
73 remapped into VAR_DECLs, and non-void RETURN_EXPRs become
726a989a 74 MODIFY_EXPRs that store to a dedicated returned-value variable.
e21aff8a
SB
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
1d65f45c 77 statements and RESX statements are adjusted accordingly.
e21aff8a 78
e21aff8a
SB
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
1b369fae
RH
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
e21aff8a
SB
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.
1b369fae 96 If we're cloning the body, those callgraph edges will be
e21aff8a
SB
97 updated to point into the new body. (Note that the original
98 callgraph node and edge list will not be altered.)
99
726a989a 100 See the CALL_EXPR handling case in copy_tree_body_r (). */
e21aff8a 101
d4e4baa9
AO
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
7f9bc51b 114
7f9bc51b
ZD
115/* Weights that estimate_num_insns uses to estimate the size of the
116 produced code. */
117
118eni_weights eni_size_weights;
119
120/* Weights that estimate_num_insns uses to estimate the time necessary
121 to execute the produced code. */
122
123eni_weights eni_time_weights;
124
d4e4baa9
AO
125/* Prototypes. */
126
6938f93f 127static tree declare_return_variable (copy_body_data *, tree, tree, basic_block);
1b369fae 128static void remap_block (tree *, copy_body_data *);
1b369fae 129static void copy_bind_expr (tree *, int *, copy_body_data *);
e21aff8a 130static void declare_inline_vars (tree, tree);
892c7e1e 131static void remap_save_expr (tree *, void *, int *);
4a283090 132static void prepend_lexical_block (tree current_block, tree new_block);
1b369fae 133static tree copy_decl_to_var (tree, copy_body_data *);
c08cd4c1 134static tree copy_result_decl_to_var (tree, copy_body_data *);
1b369fae 135static tree copy_decl_maybe_to_var (tree, copy_body_data *);
726a989a 136static gimple remap_gimple_stmt (gimple, copy_body_data *);
078c3644 137static bool delete_unreachable_blocks_update_callgraph (copy_body_data *id);
e21aff8a 138
5e20bdd7
JZ
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
1b369fae
RH
142void
143insert_decl_map (copy_body_data *id, tree key, tree value)
5e20bdd7 144{
6be42dd4 145 *pointer_map_insert (id->decl_map, key) = value;
5e20bdd7
JZ
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)
6be42dd4 150 *pointer_map_insert (id->decl_map, value) = value;
5e20bdd7
JZ
151}
152
b5b8b0ac
AO
153/* Insert a tree->tree mapping for ID. This is only used for
154 variables. */
155
156static void
157insert_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
082ab5ff
JJ
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. */
181static int processing_debug_stmt = 0;
182
110cfe1c
JH
183/* Construct new SSA name for old NAME. ID is the inline context. */
184
185static tree
186remap_ssa_name (tree name, copy_body_data *id)
187{
70b5e7dc 188 tree new_tree, var;
6be42dd4 189 tree *n;
110cfe1c
JH
190
191 gcc_assert (TREE_CODE (name) == SSA_NAME);
192
6be42dd4 193 n = (tree *) pointer_map_contains (id->decl_map, name);
110cfe1c 194 if (n)
129a37fc 195 return unshare_expr (*n);
110cfe1c 196
082ab5ff
JJ
197 if (processing_debug_stmt)
198 {
67386041
RG
199 if (SSA_NAME_IS_DEFAULT_DEF (name)
200 && TREE_CODE (SSA_NAME_VAR (name)) == PARM_DECL
ddb555ed
JJ
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
082ab5ff
JJ
226 processing_debug_stmt = -1;
227 return name;
228 }
229
70b5e7dc
RG
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
110cfe1c
JH
259 /* Do not set DEF_STMT yet as statement is not copied yet. We do that
260 in copy_bb. */
70b5e7dc 261 new_tree = remap_decl (var, id);
726a989a 262
110cfe1c 263 /* We might've substituted constant or another SSA_NAME for
b8698a0f 264 the variable.
110cfe1c
JH
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. */
82d6e6fc 269 if ((TREE_CODE (new_tree) == VAR_DECL || TREE_CODE (new_tree) == PARM_DECL)
70b5e7dc
RG
270 && (!SSA_NAME_VAR (name)
271 || TREE_CODE (SSA_NAME_VAR (name)) != RESULT_DECL
110cfe1c
JH
272 || !id->transform_return_to_modify))
273 {
25a6a873 274 struct ptr_info_def *pi;
82d6e6fc
KG
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)
110cfe1c 278 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
25a6a873
RG
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 }
32244553 288 if (SSA_NAME_IS_DEFAULT_DEF (name))
045685a9
JH
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
fa10beec 293 abnormal edge, but also increase register pressure.
045685a9 294
726a989a
RB
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
dcad005d 300 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name)
70b5e7dc
RG
301 && (!SSA_NAME_VAR (name)
302 || TREE_CODE (SSA_NAME_VAR (name)) != PARM_DECL)
0723b99a 303 && (id->entry_bb != EDGE_SUCC (ENTRY_BLOCK_PTR, 0)->dest
045685a9
JH
304 || EDGE_COUNT (id->entry_bb->preds) != 1))
305 {
726a989a
RB
306 gimple_stmt_iterator gsi = gsi_last_bb (id->entry_bb);
307 gimple init_stmt;
e8160c9a 308 tree zero = build_zero_cst (TREE_TYPE (new_tree));
b8698a0f 309
e8160c9a 310 init_stmt = gimple_build_assign (new_tree, zero);
726a989a 311 gsi_insert_after (&gsi, init_stmt, GSI_NEW_STMT);
82d6e6fc 312 SSA_NAME_IS_DEFAULT_DEF (new_tree) = 0;
045685a9
JH
313 }
314 else
315 {
82d6e6fc 316 SSA_NAME_DEF_STMT (new_tree) = gimple_build_nop ();
32244553 317 set_ssa_default_def (cfun, SSA_NAME_VAR (new_tree), new_tree);
045685a9
JH
318 }
319 }
110cfe1c
JH
320 }
321 else
82d6e6fc
KG
322 insert_decl_map (id, name, new_tree);
323 return new_tree;
110cfe1c
JH
324}
325
e21aff8a 326/* Remap DECL during the copying of the BLOCK tree for the function. */
d4e4baa9 327
1b369fae
RH
328tree
329remap_decl (tree decl, copy_body_data *id)
d4e4baa9 330{
6be42dd4 331 tree *n;
e21aff8a
SB
332
333 /* We only remap local variables in the current function. */
3c2a7a6a 334
e21aff8a
SB
335 /* See if we have remapped this declaration. */
336
6be42dd4 337 n = (tree *) pointer_map_contains (id->decl_map, decl);
e21aff8a 338
b5b8b0ac
AO
339 if (!n && processing_debug_stmt)
340 {
341 processing_debug_stmt = -1;
342 return decl;
343 }
344
e21aff8a
SB
345 /* If we didn't already have an equivalent for this declaration,
346 create one now. */
d4e4baa9
AO
347 if (!n)
348 {
d4e4baa9 349 /* Make a copy of the variable or label. */
1b369fae 350 tree t = id->copy_decl (decl, id);
b8698a0f 351
596b98ce
AO
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
1b369fae
RH
357 if (!DECL_P (t))
358 return t;
359
3c2a7a6a
RH
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);
3c2a7a6a
RH
364
365 /* Remap sizes as necessary. */
726a989a
RB
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);
d4e4baa9 368
8c27b7d4 369 /* If fields, do likewise for offset and qualifier. */
5377d5ba
RK
370 if (TREE_CODE (t) == FIELD_DECL)
371 {
726a989a 372 walk_tree (&DECL_FIELD_OFFSET (t), copy_tree_body_r, id, NULL);
5377d5ba 373 if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
726a989a 374 walk_tree (&DECL_QUALIFIER (t), copy_tree_body_r, id, NULL);
5377d5ba
RK
375 }
376
5e20bdd7 377 return t;
d4e4baa9
AO
378 }
379
f82a627c
EB
380 if (id->do_not_unshare)
381 return *n;
382 else
383 return unshare_expr (*n);
d4e4baa9
AO
384}
385
3c2a7a6a 386static tree
1b369fae 387remap_type_1 (tree type, copy_body_data *id)
3c2a7a6a 388{
82d6e6fc 389 tree new_tree, t;
3c2a7a6a 390
ed397c43
RK
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 {
82d6e6fc 396 new_tree = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
ed397c43
RK
397 TYPE_MODE (type),
398 TYPE_REF_CAN_ALIAS_ALL (type));
040c6d51
MM
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));
82d6e6fc
KG
403 insert_decl_map (id, type, new_tree);
404 return new_tree;
ed397c43
RK
405 }
406 else if (TREE_CODE (type) == REFERENCE_TYPE)
407 {
82d6e6fc 408 new_tree = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
ed397c43
RK
409 TYPE_MODE (type),
410 TYPE_REF_CAN_ALIAS_ALL (type));
040c6d51
MM
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));
82d6e6fc
KG
415 insert_decl_map (id, type, new_tree);
416 return new_tree;
ed397c43
RK
417 }
418 else
82d6e6fc 419 new_tree = copy_node (type);
ed397c43 420
82d6e6fc 421 insert_decl_map (id, type, new_tree);
3c2a7a6a
RH
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);
82d6e6fc
KG
429 TYPE_MAIN_VARIANT (new_tree) = t;
430 TYPE_NEXT_VARIANT (new_tree) = TYPE_NEXT_VARIANT (t);
431 TYPE_NEXT_VARIANT (t) = new_tree;
3c2a7a6a
RH
432 }
433 else
434 {
82d6e6fc
KG
435 TYPE_MAIN_VARIANT (new_tree) = new_tree;
436 TYPE_NEXT_VARIANT (new_tree) = NULL;
3c2a7a6a
RH
437 }
438
596b98ce 439 if (TYPE_STUB_DECL (type))
82d6e6fc 440 TYPE_STUB_DECL (new_tree) = remap_decl (TYPE_STUB_DECL (type), id);
596b98ce 441
3c2a7a6a 442 /* Lazily create pointer and reference types. */
82d6e6fc
KG
443 TYPE_POINTER_TO (new_tree) = NULL;
444 TYPE_REFERENCE_TO (new_tree) = NULL;
3c2a7a6a 445
82d6e6fc 446 switch (TREE_CODE (new_tree))
3c2a7a6a
RH
447 {
448 case INTEGER_TYPE:
449 case REAL_TYPE:
325217ed 450 case FIXED_POINT_TYPE:
3c2a7a6a
RH
451 case ENUMERAL_TYPE:
452 case BOOLEAN_TYPE:
82d6e6fc 453 t = TYPE_MIN_VALUE (new_tree);
3c2a7a6a 454 if (t && TREE_CODE (t) != INTEGER_CST)
82d6e6fc 455 walk_tree (&TYPE_MIN_VALUE (new_tree), copy_tree_body_r, id, NULL);
1c9766da 456
82d6e6fc 457 t = TYPE_MAX_VALUE (new_tree);
3c2a7a6a 458 if (t && TREE_CODE (t) != INTEGER_CST)
82d6e6fc
KG
459 walk_tree (&TYPE_MAX_VALUE (new_tree), copy_tree_body_r, id, NULL);
460 return new_tree;
9f63daea 461
3c2a7a6a 462 case FUNCTION_TYPE:
82d6e6fc
KG
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;
3c2a7a6a
RH
466
467 case ARRAY_TYPE:
82d6e6fc
KG
468 TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
469 TYPE_DOMAIN (new_tree) = remap_type (TYPE_DOMAIN (new_tree), id);
3c2a7a6a
RH
470 break;
471
472 case RECORD_TYPE:
473 case UNION_TYPE:
474 case QUAL_UNION_TYPE:
52dd234b
RH
475 {
476 tree f, nf = NULL;
477
910ad8de 478 for (f = TYPE_FIELDS (new_tree); f ; f = DECL_CHAIN (f))
52dd234b
RH
479 {
480 t = remap_decl (f, id);
82d6e6fc 481 DECL_CONTEXT (t) = new_tree;
910ad8de 482 DECL_CHAIN (t) = nf;
52dd234b
RH
483 nf = t;
484 }
82d6e6fc 485 TYPE_FIELDS (new_tree) = nreverse (nf);
52dd234b 486 }
3c2a7a6a
RH
487 break;
488
3c2a7a6a
RH
489 case OFFSET_TYPE:
490 default:
491 /* Shouldn't have been thought variable sized. */
1e128c5f 492 gcc_unreachable ();
3c2a7a6a
RH
493 }
494
82d6e6fc
KG
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);
3c2a7a6a 497
82d6e6fc 498 return new_tree;
3c2a7a6a
RH
499}
500
1b369fae
RH
501tree
502remap_type (tree type, copy_body_data *id)
52dd234b 503{
6be42dd4 504 tree *node;
4f5c64b8 505 tree tmp;
52dd234b
RH
506
507 if (type == NULL)
508 return type;
509
510 /* See if we have remapped this type. */
6be42dd4 511 node = (tree *) pointer_map_contains (id->decl_map, type);
52dd234b 512 if (node)
6be42dd4 513 return *node;
52dd234b
RH
514
515 /* The type only needs remapping if it's variably modified. */
1b369fae 516 if (! variably_modified_type_p (type, id->src_fn))
52dd234b
RH
517 {
518 insert_decl_map (id, type, type);
519 return type;
520 }
521
4f5c64b8
RG
522 id->remapping_type_depth++;
523 tmp = remap_type_1 (type, id);
524 id->remapping_type_depth--;
525
526 return tmp;
52dd234b
RH
527}
528
526d73ab 529/* Decide if DECL can be put into BLOCK_NONLOCAL_VARs. */
b8698a0f 530
526d73ab
JH
531static bool
532can_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
5f564b8f 544 return false;
526d73ab
JH
545}
546
6de9cd9a 547static tree
8318d4ce 548remap_decls (tree decls, vec<tree, va_gc> **nonlocalized_list,
9771b263 549 copy_body_data *id)
d4e4baa9 550{
6de9cd9a
DN
551 tree old_var;
552 tree new_decls = NULL_TREE;
d4e4baa9 553
6de9cd9a 554 /* Remap its variables. */
910ad8de 555 for (old_var = decls; old_var; old_var = DECL_CHAIN (old_var))
d4e4baa9 556 {
6de9cd9a
DN
557 tree new_var;
558
526d73ab 559 if (can_be_nonlocal (old_var, id))
30be951a 560 {
5f564b8f
MM
561 /* We need to add this variable to the local decls as otherwise
562 nothing else will do so. */
526d73ab 563 if (TREE_CODE (old_var) == VAR_DECL
5f564b8f 564 && ! DECL_EXTERNAL (old_var))
c021f10b 565 add_local_decl (cfun, old_var);
9e6aced0 566 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
526d73ab
JH
567 && !DECL_IGNORED_P (old_var)
568 && nonlocalized_list)
8318d4ce 569 vec_safe_push (*nonlocalized_list, old_var);
30be951a
JH
570 continue;
571 }
572
6de9cd9a
DN
573 /* Remap the variable. */
574 new_var = remap_decl (old_var, id);
575
726a989a 576 /* If we didn't remap this variable, we can't mess with its
6de9cd9a
DN
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. */
b8698a0f 579
526d73ab 580 if (new_var == id->retvar)
6de9cd9a 581 ;
526d73ab
JH
582 else if (!new_var)
583 {
9e6aced0 584 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
526d73ab
JH
585 && !DECL_IGNORED_P (old_var)
586 && nonlocalized_list)
8318d4ce 587 vec_safe_push (*nonlocalized_list, old_var);
526d73ab 588 }
d4e4baa9
AO
589 else
590 {
1e128c5f 591 gcc_assert (DECL_P (new_var));
910ad8de 592 DECL_CHAIN (new_var) = new_decls;
6de9cd9a 593 new_decls = new_var;
60a5d78a
JJ
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 }
d4e4baa9 607 }
d4e4baa9 608 }
d4e4baa9 609
6de9cd9a
DN
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
616static void
1b369fae 617remap_block (tree *block, copy_body_data *id)
6de9cd9a 618{
d436bff8
AH
619 tree old_block;
620 tree new_block;
d436bff8
AH
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;
3e2844cb 627 BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
526d73ab 628 BLOCK_NONLOCALIZED_VARS (new_block)
9771b263 629 = vec_safe_copy (BLOCK_NONLOCALIZED_VARS (old_block));
d436bff8
AH
630 *block = new_block;
631
632 /* Remap its variables. */
526d73ab 633 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block),
8318d4ce 634 &BLOCK_NONLOCALIZED_VARS (new_block),
526d73ab 635 id);
d436bff8 636
1b369fae 637 if (id->transform_lang_insert_block)
9ff420f1 638 id->transform_lang_insert_block (new_block);
1b369fae 639
d436bff8 640 /* Remember the remapped block. */
6de9cd9a 641 insert_decl_map (id, old_block, new_block);
d4e4baa9
AO
642}
643
acb8f212
JH
644/* Copy the whole block tree and root it in id->block. */
645static tree
1b369fae 646remap_blocks (tree block, copy_body_data *id)
acb8f212
JH
647{
648 tree t;
82d6e6fc 649 tree new_tree = block;
acb8f212
JH
650
651 if (!block)
652 return NULL;
653
82d6e6fc
KG
654 remap_block (&new_tree, id);
655 gcc_assert (new_tree != block);
acb8f212 656 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
4a283090
JH
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));
82d6e6fc 661 return new_tree;
acb8f212
JH
662}
663
3e492e9c
RB
664/* Remap the block tree rooted at BLOCK to nothing. */
665static void
666remap_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
d4e4baa9 674static void
6de9cd9a 675copy_statement_list (tree *tp)
d4e4baa9 676{
6de9cd9a 677 tree_stmt_iterator oi, ni;
82d6e6fc 678 tree new_tree;
6de9cd9a 679
82d6e6fc
KG
680 new_tree = alloc_stmt_list ();
681 ni = tsi_start (new_tree);
6de9cd9a 682 oi = tsi_start (*tp);
b1d82db0 683 TREE_TYPE (new_tree) = TREE_TYPE (*tp);
82d6e6fc 684 *tp = new_tree;
6de9cd9a
DN
685
686 for (; !tsi_end_p (oi); tsi_next (&oi))
a406865a
RG
687 {
688 tree stmt = tsi_stmt (oi);
62e36382
JM
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);
a406865a
RG
694 tsi_link_after (&ni, stmt, TSI_CONTINUE_LINKING);
695 }
6de9cd9a 696}
d4e4baa9 697
6de9cd9a 698static void
1b369fae 699copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
6de9cd9a
DN
700{
701 tree block = BIND_EXPR_BLOCK (*tp);
d4e4baa9
AO
702 /* Copy (and replace) the statement. */
703 copy_tree_r (tp, walk_subtrees, NULL);
6de9cd9a
DN
704 if (block)
705 {
706 remap_block (&block, id);
707 BIND_EXPR_BLOCK (*tp) = block;
708 }
d4e4baa9 709
6de9cd9a 710 if (BIND_EXPR_VARS (*tp))
60a5d78a
JJ
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);
d4e4baa9
AO
714}
715
726a989a
RB
716
717/* Create a new gimple_seq by remapping all the statements in BODY
718 using the inlining information in ID. */
719
b34fd25c 720static gimple_seq
726a989a
RB
721remap_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
739static gimple
740copy_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)
526d73ab 759 new_vars = remap_decls (new_vars, NULL, id);
726a989a
RB
760
761 new_bind = gimple_build_bind (new_vars, new_body, new_block);
762
763 return new_bind;
764}
765
78bbd765
EB
766/* Return true if DECL is a parameter or a SSA_NAME for a parameter. */
767
768static bool
769is_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}
726a989a
RB
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
786static tree
787remap_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);
211ca15c
RG
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);
726a989a
RB
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);
37c59e69
EB
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 }
726a989a
RB
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. */
41a58a92 866
70f34814 867 if (TREE_CODE (*tp) == MEM_REF)
726a989a 868 {
70f34814 869 /* We need to re-canonicalize MEM_REFs from inline substitutions
93e452ed
RG
870 that can happen when a pointer argument is an ADDR_EXPR.
871 Recurse here manually to allow that. */
78bbd765
EB
872 tree ptr = TREE_OPERAND (*tp, 0);
873 tree type = remap_type (TREE_TYPE (*tp), id);
874 tree old = *tp;
93e452ed 875 walk_tree (&ptr, remap_gimple_op_r, data, NULL);
78bbd765 876 *tp = fold_build2 (MEM_REF, type, ptr, TREE_OPERAND (*tp, 1));
0de204de 877 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
31a47f1a 878 TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
93e452ed 879 TREE_NO_WARNING (*tp) = TREE_NO_WARNING (old);
78bbd765
EB
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;
93e452ed
RG
887 *walk_subtrees = 0;
888 return NULL;
726a989a
RB
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
41a58a92
RG
895 if (TREE_CODE (*tp) != OMP_CLAUSE)
896 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
897
726a989a
RB
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,
70f34814 908 the MEM_REF substitution above. Make sure that
5368224f 909 TREE_CONSTANT and friends are up-to-date. */
726a989a 910 int invariant = is_gimple_min_invariant (*tp);
f1071b12 911 walk_tree (&TREE_OPERAND (*tp, 0), remap_gimple_op_r, data, NULL);
70f34814 912 recompute_tree_invariant_for_addr_expr (*tp);
726a989a
RB
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
5368224f
DC
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
726a989a
RB
939 /* Keep iterating. */
940 return NULL_TREE;
941}
942
943
944/* Called from copy_body_id via walk_tree. DATA is really a
1b369fae 945 `copy_body_data *'. */
aa4a53af 946
1b369fae 947tree
726a989a 948copy_tree_body_r (tree *tp, int *walk_subtrees, void *data)
d4e4baa9 949{
1b369fae
RH
950 copy_body_data *id = (copy_body_data *) data;
951 tree fn = id->src_fn;
acb8f212 952 tree new_block;
d4e4baa9 953
e21aff8a
SB
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. */
d4e4baa9 959
1b369fae 960 /* When requested, RETURN_EXPRs should be transformed to just the
726a989a 961 contained MODIFY_EXPR. The branch semantics of the return will
1b369fae
RH
962 be handled elsewhere by manipulating the CFG rather than a statement. */
963 if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
d4e4baa9 964 {
e21aff8a 965 tree assignment = TREE_OPERAND (*tp, 0);
d4e4baa9
AO
966
967 /* If we're returning something, just turn that into an
e21aff8a
SB
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. */
726a989a 972 if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
e21aff8a
SB
973 {
974 /* Replace the RETURN_EXPR with (a copy of) the
726a989a 975 MODIFY_EXPR hanging underneath. */
e21aff8a
SB
976 *tp = copy_node (assignment);
977 }
978 else /* Else the RETURN_EXPR returns no value. */
979 {
980 *tp = NULL;
cceb1885 981 return (tree) (void *)1;
e21aff8a 982 }
d4e4baa9 983 }
110cfe1c
JH
984 else if (TREE_CODE (*tp) == SSA_NAME)
985 {
986 *tp = remap_ssa_name (*tp, id);
987 *walk_subtrees = 0;
988 return NULL;
989 }
e21aff8a 990
d4e4baa9
AO
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
5377d5ba 994 function. Similarly for globals from an outer function. */
50886bf1 995 else if (auto_var_in_fn_p (*tp, fn))
d4e4baa9
AO
996 {
997 tree new_decl;
998
999 /* Remap the declaration. */
1000 new_decl = remap_decl (*tp, id);
1e128c5f 1001 gcc_assert (new_decl);
d4e4baa9
AO
1002 /* Replace this variable with the copy. */
1003 STRIP_TYPE_NOPS (new_decl);
1004 *tp = new_decl;
e4cf29ae 1005 *walk_subtrees = 0;
d4e4baa9 1006 }
6de9cd9a
DN
1007 else if (TREE_CODE (*tp) == STATEMENT_LIST)
1008 copy_statement_list (tp);
a406865a
RG
1009 else if (TREE_CODE (*tp) == SAVE_EXPR
1010 || TREE_CODE (*tp) == TARGET_EXPR)
82c82743 1011 remap_save_expr (tp, id->decl_map, walk_subtrees);
17acc01a
JH
1012 else if (TREE_CODE (*tp) == LABEL_DECL
1013 && (! DECL_CONTEXT (*tp)
1b369fae 1014 || decl_function_context (*tp) == id->src_fn))
e21aff8a 1015 /* These may need to be remapped for EH handling. */
17acc01a 1016 *tp = remap_decl (*tp, id);
6de9cd9a
DN
1017 else if (TREE_CODE (*tp) == BIND_EXPR)
1018 copy_bind_expr (tp, walk_subtrees, id);
3c2a7a6a
RH
1019 /* Types may need remapping as well. */
1020 else if (TYPE_P (*tp))
1021 *tp = remap_type (*tp, id);
1022
bb04998a
RK
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. */
3cf11075 1025 else if (CONSTANT_CLASS_P (*tp))
bb04998a
RK
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
d4e4baa9
AO
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 {
e21aff8a
SB
1046 /* Here we handle trees that are not completely rewritten.
1047 First we detect some inlining-induced bogosities for
1048 discarding. */
726a989a
RB
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)))
d4e4baa9
AO
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. */
726a989a 1056 tree decl = TREE_OPERAND (*tp, 0), value;
6be42dd4 1057 tree *n;
d4e4baa9 1058
6be42dd4 1059 n = (tree *) pointer_map_contains (id->decl_map, decl);
d4e4baa9
AO
1060 if (n)
1061 {
6be42dd4 1062 value = *n;
d4e4baa9 1063 STRIP_TYPE_NOPS (value);
becfd6e5 1064 if (TREE_CONSTANT (value) || TREE_READONLY (value))
68594ce7 1065 {
c2255bc4 1066 *tp = build_empty_stmt (EXPR_LOCATION (*tp));
726a989a 1067 return copy_tree_body_r (tp, walk_subtrees, data);
68594ce7 1068 }
d4e4baa9
AO
1069 }
1070 }
1b369fae 1071 else if (TREE_CODE (*tp) == INDIRECT_REF)
6de9cd9a
DN
1072 {
1073 /* Get rid of *& from inline substitutions that can happen when a
1074 pointer argument is an ADDR_EXPR. */
81cfbbc2 1075 tree decl = TREE_OPERAND (*tp, 0);
78bbd765 1076 tree *n = (tree *) pointer_map_contains (id->decl_map, decl);
6de9cd9a
DN
1077 if (n)
1078 {
30d2e943
RG
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,
095ecc24
RG
1083 but we absolutely rely on that. As fold_indirect_ref
1084 does other useful transformations, try that first, though. */
78bbd765
EB
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);
095ecc24
RG
1089 if (! *tp)
1090 {
78bbd765 1091 if (TREE_CODE (ptr) == ADDR_EXPR)
de4af523 1092 {
78bbd765
EB
1093 *tp
1094 = fold_indirect_ref_1 (EXPR_LOCATION (ptr), type, ptr);
de4af523
JJ
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)
78bbd765 1099 *tp = TREE_OPERAND (ptr, 0);
de4af523 1100 }
095ecc24 1101 else
d84b37b0 1102 {
78bbd765 1103 *tp = build1 (INDIRECT_REF, type, ptr);
d84b37b0 1104 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
955f6531 1105 TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
a61c3633 1106 TREE_READONLY (*tp) = TREE_READONLY (old);
78bbd765
EB
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;
d84b37b0 1114 }
095ecc24 1115 }
81cfbbc2
JH
1116 *walk_subtrees = 0;
1117 return NULL;
68594ce7
JM
1118 }
1119 }
70f34814
RG
1120 else if (TREE_CODE (*tp) == MEM_REF)
1121 {
54714c68
RB
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. */
78bbd765
EB
1125 tree ptr = TREE_OPERAND (*tp, 0);
1126 tree type = remap_type (TREE_TYPE (*tp), id);
1127 tree old = *tp;
54714c68 1128 walk_tree (&ptr, copy_tree_body_r, data, NULL);
78bbd765 1129 *tp = fold_build2 (MEM_REF, type, ptr, TREE_OPERAND (*tp, 1));
54714c68
RB
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);
78bbd765
EB
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;
54714c68
RB
1140 *walk_subtrees = 0;
1141 return NULL;
70f34814 1142 }
68594ce7 1143
e21aff8a
SB
1144 /* Here is the "usual case". Copy this tree node, and then
1145 tweak some special cases. */
1b369fae 1146 copy_tree_r (tp, walk_subtrees, NULL);
110cfe1c 1147
acb8f212
JH
1148 /* If EXPR has block defined, map it to newly constructed block.
1149 When inlining we want EXPRs without block appear in the block
ee0192a2 1150 of function call if we are not remapping a type. */
726a989a 1151 if (EXPR_P (*tp))
acb8f212 1152 {
ee0192a2 1153 new_block = id->remapping_type_depth == 0 ? id->block : NULL;
acb8f212
JH
1154 if (TREE_BLOCK (*tp))
1155 {
6be42dd4
RG
1156 tree *n;
1157 n = (tree *) pointer_map_contains (id->decl_map,
1158 TREE_BLOCK (*tp));
60a5d78a
JJ
1159 if (n)
1160 new_block = *n;
acb8f212 1161 }
5368224f 1162 TREE_SET_BLOCK (*tp, new_block);
acb8f212 1163 }
68594ce7 1164
726a989a 1165 if (TREE_CODE (*tp) != OMP_CLAUSE)
07beea0d 1166 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
3c2a7a6a 1167
68594ce7
JM
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 }
84cce55d
RH
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 {
ad6003f2 1181 int invariant = is_gimple_min_invariant (*tp);
726a989a
RB
1182 walk_tree (&TREE_OPERAND (*tp, 0), copy_tree_body_r, id, NULL);
1183
8e85fd14
RG
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);
726a989a 1190
416c991f
JJ
1191 /* If this used to be invariant, but is not any longer,
1192 then regimplification is probably needed. */
ad6003f2 1193 if (invariant && !is_gimple_min_invariant (*tp))
416c991f 1194 id->regimplify = true;
726a989a 1195
84cce55d
RH
1196 *walk_subtrees = 0;
1197 }
d4e4baa9
AO
1198 }
1199
1200 /* Keep iterating. */
1201 return NULL_TREE;
1202}
1203
1d65f45c
RH
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
1208static int
1209remap_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
1223static tree
1224remap_eh_region_tree_nr (tree old_t_nr, copy_body_data *id)
1225{
1226 int old_nr, new_nr;
1227
9439e9a1 1228 old_nr = tree_to_shwi (old_t_nr);
1d65f45c
RH
1229 new_nr = remap_eh_region_nr (old_nr, id);
1230
9f616812 1231 return build_int_cst (integer_type_node, new_nr);
1d65f45c 1232}
726a989a
RB
1233
1234/* Helper for copy_bb. Remap statement STMT using the inlining
1235 information in ID. Return the new statement copy. */
1236
1237static gimple
1238remap_gimple_stmt (gimple stmt, copy_body_data *id)
1239{
1240 gimple copy = NULL;
1241 struct walk_stmt_info wi;
5a6e26b7 1242 bool skip_first = false;
726a989a
RB
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. */
6938f93f
JH
1263 if (retval
1264 && (TREE_CODE (retval) != RESULT_DECL
1265 && (TREE_CODE (retval) != SSA_NAME
70b5e7dc 1266 || ! SSA_NAME_VAR (retval)
6938f93f 1267 || TREE_CODE (SSA_NAME_VAR (retval)) != RESULT_DECL)))
5a6e26b7
JH
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 }
726a989a
RB
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);
b8698a0f 1302 copy = gimple_build_try (s1, s2, gimple_try_kind (stmt));
726a989a
RB
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);
74bf76ed
JJ
1334 copy = gimple_build_omp_for (s1, gimple_omp_for_kind (stmt),
1335 gimple_omp_for_clauses (stmt),
726a989a
RB
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
acf0174b
JJ
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
726a989a
RB
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
acf0174b
JJ
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
05a26161
JJ
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
0a35513e
AH
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
726a989a
RB
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
4029a5e0
JJ
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
b5b8b0ac
AO
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);
9771b263 1464 id->debug_stmts.safe_push (copy);
b5b8b0ac
AO
1465 return copy;
1466 }
ddb555ed
JJ
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);
9771b263 1472 id->debug_stmts.safe_push (copy);
ddb555ed
JJ
1473 return copy;
1474 }
1d65f45c
RH
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 }
d086d311 1506
25a6a873
RG
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);
1d65f45c
RH
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 }
726a989a
RB
1534 }
1535
1536 /* If STMT has a block defined, map it to the newly constructed
16917761 1537 block. */
726a989a
RB
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);
16917761 1543 gimple_set_block (copy, *n);
726a989a
RB
1544 }
1545
ddb555ed 1546 if (gimple_debug_bind_p (copy) || gimple_debug_source_bind_p (copy))
b5b8b0ac
AO
1547 return copy;
1548
726a989a
RB
1549 /* Remap all the operands in COPY. */
1550 memset (&wi, 0, sizeof (wi));
1551 wi.info = id;
5a6e26b7
JH
1552 if (skip_first)
1553 walk_tree (gimple_op_ptr (copy, 1), remap_gimple_op_r, &wi, NULL);
1554 else
b8698a0f 1555 walk_gimple_op (copy, remap_gimple_op_r, &wi);
726a989a 1556
5006671f
RG
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
726a989a
RB
1565 return copy;
1566}
1567
1568
e21aff8a
SB
1569/* Copy basic block, scale profile accordingly. Edges will be taken care of
1570 later */
1571
1572static basic_block
0178d644
VR
1573copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
1574 gcov_type count_scale)
e21aff8a 1575{
c2a4718a 1576 gimple_stmt_iterator gsi, copy_gsi, seq_gsi;
e21aff8a 1577 basic_block copy_basic_block;
726a989a 1578 tree decl;
0d63a740 1579 gcov_type freq;
91382288
JH
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;
e21aff8a
SB
1586
1587 /* create_basic_block() will append every new block to
1588 basic_block_info automatically. */
cceb1885 1589 copy_basic_block = create_basic_block (NULL, (void *) 0,
91382288 1590 (basic_block) prev->aux);
8b47039c 1591 copy_basic_block->count = apply_scale (bb->count, count_scale);
45a80bb9 1592
726a989a
RB
1593 /* We are going to rebuild frequencies from scratch. These values
1594 have just small importance to drive canonicalize_loop_headers. */
8b47039c 1595 freq = apply_scale ((gcov_type)bb->frequency, frequency_scale);
726a989a 1596
0d63a740
JH
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;
e21aff8a 1601
726a989a
RB
1602 copy_gsi = gsi_start_bb (copy_basic_block);
1603
1604 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
e21aff8a 1605 {
726a989a
RB
1606 gimple stmt = gsi_stmt (gsi);
1607 gimple orig_stmt = stmt;
e21aff8a 1608
416c991f 1609 id->regimplify = false;
726a989a
RB
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);
c2a4718a 1615 seq_gsi = copy_gsi;
726a989a
RB
1616
1617 /* With return slot optimization we can end up with
1618 non-gimple (foo *)&this->m, fix that here. */
4c29307d
JJ
1619 if (is_gimple_assign (stmt)
1620 && gimple_assign_rhs_code (stmt) == NOP_EXPR
1621 && !is_gimple_val (gimple_assign_rhs1 (stmt)))
e21aff8a 1622 {
726a989a 1623 tree new_rhs;
c2a4718a 1624 new_rhs = force_gimple_operand_gsi (&seq_gsi,
4a2b7f24 1625 gimple_assign_rhs1 (stmt),
cf1bcf06
EB
1626 true, NULL, false,
1627 GSI_CONTINUE_LINKING);
726a989a 1628 gimple_assign_set_rhs1 (stmt, new_rhs);
c2a4718a 1629 id->regimplify = false;
726a989a 1630 }
2b65dae5 1631
c2a4718a
JJ
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);
110cfe1c 1643
726a989a
RB
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. */
c2a4718a 1647 do
726a989a 1648 {
9187e02d
JH
1649 tree fn;
1650
c2a4718a 1651 stmt = gsi_stmt (copy_gsi);
726a989a
RB
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;
9771b263 1660 vec<tree> argarray;
726a989a
RB
1661 size_t nargs = gimple_call_num_args (id->gimple_call);
1662 size_t n;
1663
910ad8de 1664 for (p = DECL_ARGUMENTS (id->src_fn); p; p = DECL_CHAIN (p))
726a989a
RB
1665 nargs--;
1666
1667 /* Create the new array of arguments. */
1668 n = nargs + gimple_call_num_args (stmt);
9771b263
DN
1669 argarray.create (n);
1670 argarray.safe_grow_cleared (n);
726a989a
RB
1671
1672 /* Copy all the arguments before '...' */
9771b263 1673 memcpy (argarray.address (),
726a989a
RB
1674 gimple_call_arg_ptr (stmt, 0),
1675 gimple_call_num_args (stmt) * sizeof (tree));
1676
1677 /* Append the arguments passed in '...' */
9771b263 1678 memcpy (argarray.address () + gimple_call_num_args (stmt),
726a989a
RB
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
9771b263 1686 argarray.release ();
726a989a
RB
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)
e0704a46 1704 {
726a989a
RB
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
910ad8de 1711 for (p = DECL_ARGUMENTS (id->src_fn); p; p = DECL_CHAIN (p))
726a989a
RB
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 }
b8a00a4d 1719
726a989a
RB
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 {
9b2a5ef7 1738 struct cgraph_edge *edge;
f618d33e 1739 int flags;
6ef5231b 1740
726a989a 1741 switch (id->transform_call_graph_edges)
e0704a46 1742 {
9b2a5ef7
RH
1743 case CB_CGE_DUPLICATE:
1744 edge = cgraph_edge (id->src_node, orig_stmt);
1745 if (edge)
0d63a740
JH
1746 {
1747 int edge_freq = edge->frequency;
042ae7d2
JH
1748 int new_freq;
1749 struct cgraph_edge *old_edge = edge;
0d63a740
JH
1750 edge = cgraph_clone_edge (edge, id->dst_node, stmt,
1751 gimple_uid (stmt),
1752 REG_BR_PROB_BASE, CGRAPH_FREQ_BASE,
898b8927 1753 true);
0d63a740
JH
1754 /* We could also just rescale the frequency, but
1755 doing so would introduce roundoff errors and make
1756 verifier unhappy. */
67348ccc 1757 new_freq = compute_call_stmt_bb_frequency (id->dst_node->decl,
042ae7d2
JH
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)
0d63a740 1763 {
042ae7d2
JH
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 }
67348ccc 1782 ipa_clone_ref (ref, id->dst_node, stmt);
042ae7d2
JH
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 }
0d63a740
JH
1802 }
1803 }
9b2a5ef7
RH
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 ();
110cfe1c 1820 }
f618d33e 1821
9b2a5ef7
RH
1822 /* Constant propagation on argument done during inlining
1823 may create new direct call. Produce an edge for it. */
b8698a0f 1824 if ((!edge
e33c6cd6 1825 || (edge->indirect_inlining_edge
9b2a5ef7 1826 && id->transform_call_graph_edges == CB_CGE_MOVE_CLONES))
67348ccc 1827 && id->dst_node->definition
9b2a5ef7
RH
1828 && (fn = gimple_call_fndecl (stmt)) != NULL)
1829 {
581985d7 1830 struct cgraph_node *dest = cgraph_get_node (fn);
9b2a5ef7
RH
1831
1832 /* We have missing edge in the callgraph. This can happen
1833 when previous inlining turned an indirect call into a
0e3776db 1834 direct call by constant propagating arguments or we are
20a6bb58 1835 producing dead clone (for further cloning). In all
9b2a5ef7
RH
1836 other cases we hit a bug (incorrect node sharing is the
1837 most common reason for missing edges). */
67348ccc
DM
1838 gcc_assert (!dest->definition
1839 || dest->address_taken
1840 || !id->src_node->definition
1841 || !id->dst_node->definition);
9b2a5ef7
RH
1842 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES)
1843 cgraph_create_edge_including_clones
47cb0d7d 1844 (id->dst_node, dest, orig_stmt, stmt, bb->count,
67348ccc 1845 compute_call_stmt_bb_frequency (id->dst_node->decl,
0d63a740 1846 copy_basic_block),
898b8927 1847 CIF_ORIGINALLY_INDIRECT_CALL);
9b2a5ef7
RH
1848 else
1849 cgraph_create_edge (id->dst_node, dest, stmt,
47cb0d7d
JH
1850 bb->count,
1851 compute_call_stmt_bb_frequency
67348ccc 1852 (id->dst_node->decl,
960bfb69 1853 copy_basic_block))->inline_failed
9b2a5ef7
RH
1854 = CIF_ORIGINALLY_INDIRECT_CALL;
1855 if (dump_file)
1856 {
91382288 1857 fprintf (dump_file, "Created new direct edge to %s\n",
fec39fa6 1858 dest->name ());
9b2a5ef7
RH
1859 }
1860 }
9187e02d 1861
f618d33e 1862 flags = gimple_call_flags (stmt);
f618d33e
MJ
1863 if (flags & ECF_MAY_BE_ALLOCA)
1864 cfun->calls_alloca = true;
1865 if (flags & ECF_RETURNS_TWICE)
1866 cfun->calls_setjmp = true;
726a989a 1867 }
e21aff8a 1868
1d65f45c
RH
1869 maybe_duplicate_eh_stmt_fn (cfun, stmt, id->src_cfun, orig_stmt,
1870 id->eh_map, id->eh_lp_nr);
726a989a 1871
b5b8b0ac 1872 if (gimple_in_ssa_p (cfun) && !is_gimple_debug (stmt))
726a989a
RB
1873 {
1874 ssa_op_iter i;
1875 tree def;
1876
726a989a
RB
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);
e21aff8a 1883 }
c2a4718a 1884 while (!gsi_end_p (copy_gsi));
726a989a
RB
1885
1886 copy_gsi = gsi_last_bb (copy_basic_block);
e21aff8a 1887 }
726a989a 1888
e21aff8a
SB
1889 return copy_basic_block;
1890}
1891
110cfe1c
JH
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
5305a4cb 1898 to landing pads in function we inline into.
110cfe1c 1899
e9705dc5
AO
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
110cfe1c
JH
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. */
1910static void
e9705dc5
AO
1911update_ssa_across_abnormal_edges (basic_block bb, basic_block ret_bb,
1912 bool can_throw, bool nonlocal_goto)
110cfe1c
JH
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 {
726a989a
RB
1921 gimple phi;
1922 gimple_stmt_iterator si;
110cfe1c 1923
e9705dc5
AO
1924 if (!nonlocal_goto)
1925 gcc_assert (e->flags & EDGE_EH);
726a989a 1926
e9705dc5
AO
1927 if (!can_throw)
1928 gcc_assert (!(e->flags & EDGE_EH));
726a989a
RB
1929
1930 for (si = gsi_start_phis (e->dest); !gsi_end_p (si); gsi_next (&si))
110cfe1c 1931 {
e9705dc5
AO
1932 edge re;
1933
726a989a
RB
1934 phi = gsi_stmt (si);
1935
3f8825c0
RB
1936 /* For abnormal goto/call edges the receiver can be the
1937 ENTRY_BLOCK. Do not assert this cannot happen. */
e9705dc5 1938
496a4ef5
JH
1939 gcc_assert ((e->flags & EDGE_EH)
1940 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)));
e9705dc5 1941
e9705dc5 1942 re = find_edge (ret_bb, e->dest);
0107dca2 1943 gcc_checking_assert (re);
e9705dc5
AO
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)));
110cfe1c
JH
1949 }
1950 }
1951}
1952
726a989a 1953
128a79fb
KH
1954/* Copy edges from BB into its copy constructed earlier, scale profile
1955 accordingly. Edges will be taken care of later. Assume aux
90a7788b
JJ
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. */
726a989a 1958
90a7788b 1959static bool
92e776e9
RB
1960copy_edges_for_bb (basic_block bb, gcov_type count_scale, basic_block ret_bb,
1961 bool can_make_abnormal_goto)
e21aff8a 1962{
cceb1885 1963 basic_block new_bb = (basic_block) bb->aux;
e21aff8a
SB
1964 edge_iterator ei;
1965 edge old_edge;
726a989a 1966 gimple_stmt_iterator si;
e21aff8a 1967 int flags;
90a7788b 1968 bool need_debug_cleanup = false;
e21aff8a
SB
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)
e0704a46
JH
1973 if (!(old_edge->flags & EDGE_EH))
1974 {
82d6e6fc 1975 edge new_edge;
e21aff8a 1976
e0704a46 1977 flags = old_edge->flags;
e21aff8a 1978
e0704a46
JH
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;
82d6e6fc 1983 new_edge = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
8b47039c 1984 new_edge->count = apply_scale (old_edge->count, count_scale);
82d6e6fc 1985 new_edge->probability = old_edge->probability;
e0704a46 1986 }
e21aff8a
SB
1987
1988 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
90a7788b 1989 return false;
e21aff8a 1990
726a989a 1991 for (si = gsi_start_bb (new_bb); !gsi_end_p (si);)
e21aff8a 1992 {
726a989a 1993 gimple copy_stmt;
e9705dc5 1994 bool can_throw, nonlocal_goto;
e21aff8a 1995
726a989a 1996 copy_stmt = gsi_stmt (si);
b5b8b0ac 1997 if (!is_gimple_debug (copy_stmt))
f9a21e13 1998 update_stmt (copy_stmt);
726a989a 1999
e21aff8a 2000 /* Do this before the possible split_block. */
726a989a 2001 gsi_next (&si);
e21aff8a
SB
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. */
726a989a 2014 can_throw = stmt_can_throw_internal (copy_stmt);
a6f30e66 2015 nonlocal_goto = stmt_can_make_abnormal_goto (copy_stmt);
e9705dc5
AO
2016
2017 if (can_throw || nonlocal_goto)
e21aff8a 2018 {
90a7788b
JJ
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 }
726a989a 2026 if (!gsi_end_p (si))
e21aff8a
SB
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);
110cfe1c 2031
e21aff8a 2032 new_bb = e->dest;
110cfe1c 2033 new_bb->aux = e->src->aux;
726a989a 2034 si = gsi_start_bb (new_bb);
e21aff8a 2035 }
e9705dc5 2036 }
e21aff8a 2037
1d65f45c
RH
2038 if (gimple_code (copy_stmt) == GIMPLE_EH_DISPATCH)
2039 make_eh_dispatch_edges (copy_stmt);
2040 else if (can_throw)
e9705dc5 2041 make_eh_edges (copy_stmt);
110cfe1c 2042
a6f30e66
RB
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;
e9705dc5 2047 if (nonlocal_goto)
726a989a 2048 make_abnormal_goto_edges (gimple_bb (copy_stmt), true);
e9705dc5
AO
2049
2050 if ((can_throw || nonlocal_goto)
2051 && gimple_in_ssa_p (cfun))
726a989a 2052 update_ssa_across_abnormal_edges (gimple_bb (copy_stmt), ret_bb,
e9705dc5 2053 can_throw, nonlocal_goto);
110cfe1c 2054 }
90a7788b 2055 return need_debug_cleanup;
110cfe1c
JH
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
2063static void
2064copy_phis_for_bb (basic_block bb, copy_body_data *id)
2065{
3d9a9f94 2066 basic_block const new_bb = (basic_block) bb->aux;
110cfe1c 2067 edge_iterator ei;
726a989a
RB
2068 gimple phi;
2069 gimple_stmt_iterator si;
6a78fd06
RG
2070 edge new_edge;
2071 bool inserted = false;
110cfe1c 2072
355a7673 2073 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
110cfe1c 2074 {
726a989a
RB
2075 tree res, new_res;
2076 gimple new_phi;
110cfe1c 2077
726a989a
RB
2078 phi = gsi_stmt (si);
2079 res = PHI_RESULT (phi);
2080 new_res = res;
ea057359 2081 if (!virtual_operand_p (res))
110cfe1c 2082 {
726a989a 2083 walk_tree (&new_res, copy_tree_body_r, id, NULL);
dcc748dd 2084 new_phi = create_phi_node (new_res, new_bb);
110cfe1c
JH
2085 FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
2086 {
8b3057b3
JH
2087 edge old_edge = find_edge ((basic_block) new_edge->src->aux, bb);
2088 tree arg;
2089 tree new_arg;
8b3057b3 2090 edge_iterator ei2;
5368224f 2091 location_t locus;
8b3057b3 2092
20a6bb58 2093 /* When doing partial cloning, we allow PHIs on the entry block
8b3057b3
JH
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;
726a989a 2103 walk_tree (&new_arg, copy_tree_body_r, id, NULL);
110cfe1c 2104 gcc_assert (new_arg);
36b6e793
JJ
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 {
726a989a
RB
2111 gimple_seq stmts = NULL;
2112 new_arg = force_gimple_operand (new_arg, &stmts, true, NULL);
6a78fd06
RG
2113 gsi_insert_seq_on_edge (new_edge, stmts);
2114 inserted = true;
36b6e793 2115 }
5368224f 2116 locus = gimple_phi_arg_location_from_edge (phi, old_edge);
5368224f
DC
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);
ef6179d1
DC
2123 if (*n)
2124 locus = COMBINE_LOCATION_DATA (line_table, locus, *n);
2125 else
2126 locus = LOCATION_LOCUS (locus);
5368224f 2127 }
16917761
RB
2128 else
2129 locus = LOCATION_LOCUS (locus);
5368224f 2130
16917761 2131 add_phi_arg (new_phi, new_arg, new_edge, locus);
110cfe1c 2132 }
e21aff8a
SB
2133 }
2134 }
6a78fd06
RG
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);
e21aff8a
SB
2140}
2141
726a989a 2142
e21aff8a 2143/* Wrapper for remap_decl so it can be used as a callback. */
726a989a 2144
e21aff8a
SB
2145static tree
2146remap_decl_1 (tree decl, void *data)
2147{
1b369fae 2148 return remap_decl (decl, (copy_body_data *) data);
e21aff8a
SB
2149}
2150
110cfe1c 2151/* Build struct function and associated datastructures for the new clone
af16bc76
MJ
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). */
110cfe1c
JH
2154
2155static void
0d63a740 2156initialize_cfun (tree new_fndecl, tree callee_fndecl, gcov_type count)
110cfe1c 2157{
110cfe1c 2158 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
0d63a740 2159 gcov_type count_scale;
110cfe1c 2160
49bde175
JH
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
110cfe1c 2166 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
8b47039c
TJ
2167 count_scale
2168 = GCOV_COMPUTE_SCALE (count,
2169 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
110cfe1c 2170 else
0d63a740 2171 count_scale = REG_BR_PROB_BASE;
110cfe1c
JH
2172
2173 /* Register specific tree functions. */
726a989a 2174 gimple_register_cfg_hooks ();
39ecc018
JH
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
20a6bb58 2185 /* Copy items we preserve during cloning. */
39ecc018
JH
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;
a9e0d843 2189 cfun->curr_properties = src_cfun->curr_properties;
39ecc018 2190 cfun->last_verified = src_cfun->last_verified;
39ecc018
JH
2191 cfun->va_list_gpr_size = src_cfun->va_list_gpr_size;
2192 cfun->va_list_fpr_size = src_cfun->va_list_fpr_size;
39ecc018
JH
2193 cfun->has_nonlocal_label = src_cfun->has_nonlocal_label;
2194 cfun->stdarg = src_cfun->stdarg;
39ecc018 2195 cfun->after_inlining = src_cfun->after_inlining;
8f4f502f
EB
2196 cfun->can_throw_non_call_exceptions
2197 = src_cfun->can_throw_non_call_exceptions;
9510c5af 2198 cfun->can_delete_dead_exceptions = src_cfun->can_delete_dead_exceptions;
39ecc018
JH
2199 cfun->returns_struct = src_cfun->returns_struct;
2200 cfun->returns_pcc_struct = src_cfun->returns_pcc_struct;
39ecc018 2201
110cfe1c
JH
2202 init_empty_tree_cfg ();
2203
0d63a740 2204 profile_status_for_function (cfun) = profile_status_for_function (src_cfun);
110cfe1c
JH
2205 ENTRY_BLOCK_PTR->count =
2206 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
2207 REG_BR_PROB_BASE);
0d63a740
JH
2208 ENTRY_BLOCK_PTR->frequency
2209 = ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency;
110cfe1c
JH
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 =
0d63a740 2214 EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency;
110cfe1c
JH
2215 if (src_cfun->eh)
2216 init_eh_for_function ();
2217
2218 if (src_cfun->gimple_df)
2219 {
5db9ba0c 2220 init_tree_ssa (cfun);
110cfe1c 2221 cfun->gimple_df->in_ssa_p = true;
3828719a 2222 init_ssa_operands (cfun);
110cfe1c 2223 }
110cfe1c
JH
2224}
2225
90a7788b
JJ
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
2231static void
2232maybe_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);
ddb555ed 2260 if (!single_pred_p (e->dest) && gimple_debug_bind_p (stmt))
90a7788b
JJ
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
ddb555ed 2267 if (gimple_debug_bind_p (stmt))
90a7788b 2268 {
ddb555ed
JJ
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);
90a7788b
JJ
2284 }
2285 else
ddb555ed 2286 gcc_unreachable ();
90a7788b 2287 gsi_insert_before (&dsi, new_stmt, GSI_SAME_STMT);
9771b263 2288 id->debug_stmts.safe_push (new_stmt);
90a7788b
JJ
2289 gsi_prev (&ssi);
2290 }
2291 }
2292}
2293
a9e0d843
RB
2294/* Make a copy of the sub-loops of SRC_PARENT and place them
2295 as siblings of DEST_PARENT. */
2296
2297static void
f3b331d1 2298copy_loops (copy_body_data *id,
a9e0d843
RB
2299 struct loop *dest_parent, struct loop *src_parent)
2300{
2301 struct loop *src_loop = src_parent->inner;
2302 while (src_loop)
2303 {
f3b331d1
JJ
2304 if (!id->blocks_to_copy
2305 || bitmap_bit_p (id->blocks_to_copy, src_loop->header->index))
a9e0d843
RB
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. */
9f8e7a96
RB
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 }
a9e0d843
RB
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. */
0fc822d0 2326 place_new_loop (cfun, dest_loop);
a9e0d843
RB
2327 flow_loop_tree_node_add (dest_parent, dest_loop);
2328
f3b331d1
JJ
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
a9e0d843 2340 /* Recurse. */
f3b331d1 2341 copy_loops (id, dest_loop, src_loop);
a9e0d843
RB
2342 }
2343 src_loop = src_loop->next;
2344 }
2345}
2346
042ae7d2
JH
2347/* Call cgraph_redirect_edge_call_stmt_to_callee on all calls in BB */
2348
2349void
2350redirect_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
eb4b92c1
TJ
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
2370void
2371freqs_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
e21aff8a
SB
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
2390static tree
0d63a740 2391copy_cfg_body (copy_body_data * id, gcov_type count, int frequency_scale,
91382288 2392 basic_block entry_block_map, basic_block exit_block_map,
f3b331d1 2393 basic_block new_entry)
e21aff8a 2394{
1b369fae 2395 tree callee_fndecl = id->src_fn;
e21aff8a 2396 /* Original cfun for the callee, doesn't change. */
1b369fae 2397 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
110cfe1c 2398 struct function *cfun_to_copy;
e21aff8a
SB
2399 basic_block bb;
2400 tree new_fndecl = NULL;
90a7788b 2401 bool need_debug_cleanup = false;
0d63a740 2402 gcov_type count_scale;
110cfe1c 2403 int last;
20a6bb58
JH
2404 int incoming_frequency = 0;
2405 gcov_type incoming_count = 0;
e21aff8a 2406
eb4b92c1
TJ
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
1b369fae 2425 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
8b47039c
TJ
2426 count_scale
2427 = GCOV_COMPUTE_SCALE (count,
2428 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
e21aff8a 2429 else
0d63a740 2430 count_scale = REG_BR_PROB_BASE;
e21aff8a
SB
2431
2432 /* Register specific tree functions. */
726a989a 2433 gimple_register_cfg_hooks ();
e21aff8a 2434
b35366ce
JH
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
20a6bb58 2438 probabilities of edges incoming from nonduplicated region. */
b35366ce
JH
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 {
20a6bb58
JH
2447 incoming_frequency += EDGE_FREQUENCY (e);
2448 incoming_count += e->count;
b35366ce 2449 }
8b47039c 2450 incoming_count = apply_scale (incoming_count, count_scale);
20a6bb58 2451 incoming_frequency
8b47039c 2452 = apply_scale ((gcov_type)incoming_frequency, frequency_scale);
20a6bb58
JH
2453 ENTRY_BLOCK_PTR->count = incoming_count;
2454 ENTRY_BLOCK_PTR->frequency = incoming_frequency;
b35366ce
JH
2455 }
2456
e21aff8a
SB
2457 /* Must have a CFG here at this point. */
2458 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
2459 (DECL_STRUCT_FUNCTION (callee_fndecl)));
2460
110cfe1c
JH
2461 cfun_to_copy = id->src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2462
e21aff8a
SB
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;
110cfe1c
JH
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);
e21aff8a 2467
e21aff8a
SB
2468 /* Duplicate any exception-handling regions. */
2469 if (cfun->eh)
1d65f45c
RH
2470 id->eh_map = duplicate_eh_regions (cfun_to_copy, NULL, id->eh_lp_nr,
2471 remap_decl_1, id);
726a989a 2472
e21aff8a
SB
2473 /* Use aux pointers to map the original blocks to copy. */
2474 FOR_EACH_BB_FN (bb, cfun_to_copy)
f3b331d1 2475 if (!id->blocks_to_copy || bitmap_bit_p (id->blocks_to_copy, bb->index))
91382288
JH
2476 {
2477 basic_block new_bb = copy_bb (id, bb, frequency_scale, count_scale);
2478 bb->aux = new_bb;
2479 new_bb->aux = bb;
a9e0d843 2480 new_bb->loop_father = entry_block_map->loop_father;
91382288 2481 }
110cfe1c 2482
7c57be85 2483 last = last_basic_block;
726a989a 2484
e21aff8a 2485 /* Now that we've duplicated the blocks, duplicate their edges. */
92e776e9
RB
2486 bool can_make_abormal_goto
2487 = id->gimple_call && stmt_can_make_abnormal_goto (id->gimple_call);
e21aff8a 2488 FOR_ALL_BB_FN (bb, cfun_to_copy)
f3b331d1
JJ
2489 if (!id->blocks_to_copy
2490 || (bb->index > 0 && bitmap_bit_p (id->blocks_to_copy, bb->index)))
92e776e9
RB
2491 need_debug_cleanup |= copy_edges_for_bb (bb, count_scale, exit_block_map,
2492 can_make_abormal_goto);
726a989a 2493
91382288 2494 if (new_entry)
110cfe1c 2495 {
b35366ce 2496 edge e = make_edge (entry_block_map, (basic_block)new_entry->aux, EDGE_FALLTHRU);
91382288 2497 e->probability = REG_BR_PROB_BASE;
20a6bb58 2498 e->count = incoming_count;
110cfe1c 2499 }
726a989a 2500
a9e0d843 2501 /* Duplicate the loop tree, if available and wanted. */
0fc822d0 2502 if (loops_for_fn (src_cfun) != NULL
a9e0d843
RB
2503 && current_loops != NULL)
2504 {
f3b331d1 2505 copy_loops (id, entry_block_map->loop_father,
0fc822d0 2506 get_loop (src_cfun, 0));
a9e0d843
RB
2507 /* Defer to cfgcleanup to update loop-father fields of basic-blocks. */
2508 loops_state_set (LOOPS_NEED_FIXUP);
2509 }
2510
9f8e7a96
RB
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
8b3057b3
JH
2516 if (gimple_in_ssa_p (cfun))
2517 FOR_ALL_BB_FN (bb, cfun_to_copy)
f3b331d1
JJ
2518 if (!id->blocks_to_copy
2519 || (bb->index > 0 && bitmap_bit_p (id->blocks_to_copy, bb->index)))
8b3057b3
JH
2520 copy_phis_for_bb (bb, id);
2521
91382288
JH
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);
042ae7d2
JH
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);
91382288
JH
2533 ((basic_block)bb->aux)->aux = NULL;
2534 bb->aux = NULL;
2535 }
2536
110cfe1c
JH
2537 /* Zero out AUX fields of newly created block during EH edge
2538 insertion. */
7c57be85 2539 for (; last < last_basic_block; last++)
90a7788b
JJ
2540 {
2541 if (need_debug_cleanup)
2542 maybe_move_debug_stmts_to_successors (id, BASIC_BLOCK (last));
2543 BASIC_BLOCK (last)->aux = NULL;
042ae7d2
JH
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));
90a7788b 2548 }
110cfe1c
JH
2549 entry_block_map->aux = NULL;
2550 exit_block_map->aux = NULL;
e21aff8a 2551
1d65f45c
RH
2552 if (id->eh_map)
2553 {
2554 pointer_map_destroy (id->eh_map);
2555 id->eh_map = NULL;
2556 }
2557
e21aff8a
SB
2558 return new_fndecl;
2559}
2560
b5b8b0ac
AO
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
2567static void
2568copy_debug_stmt (gimple stmt, copy_body_data *id)
2569{
2570 tree t, *n;
2571 struct walk_stmt_info wi;
2572
b5b8b0ac
AO
2573 if (gimple_block (stmt))
2574 {
b5b8b0ac 2575 n = (tree *) pointer_map_contains (id->decl_map, gimple_block (stmt));
16917761 2576 gimple_set_block (stmt, n ? *n : id->block);
b5b8b0ac 2577 }
b5b8b0ac
AO
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
ddb555ed
JJ
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);
b5b8b0ac
AO
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 }
d17af147 2596 else if (TREE_CODE (t) == VAR_DECL
5f564b8f
MM
2597 && !is_global_var (t)
2598 && !pointer_map_contains (id->decl_map, t))
d17af147 2599 /* T is a non-localized variable. */;
b5b8b0ac
AO
2600 else
2601 walk_tree (&t, remap_gimple_op_r, &wi, NULL);
2602
ddb555ed
JJ
2603 if (gimple_debug_bind_p (stmt))
2604 {
2605 gimple_debug_bind_set_var (stmt, t);
b5b8b0ac 2606
ddb555ed
JJ
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);
b5b8b0ac 2610
ddb555ed
JJ
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);
878eef4a
JJ
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 {
9771b263 2629 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (id->src_fn);
878eef4a
JJ
2630 unsigned int i;
2631 if (debug_args != NULL)
2632 {
9771b263
DN
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)
878eef4a 2636 {
9771b263 2637 t = (**debug_args)[i + 1];
878eef4a
JJ
2638 stmt->gsbase.subcode = GIMPLE_DEBUG_BIND;
2639 gimple_debug_bind_set_value (stmt, t);
2640 break;
2641 }
2642 }
2643 }
ddb555ed 2644 }
b5b8b0ac
AO
2645
2646 processing_debug_stmt = 0;
2647
2648 update_stmt (stmt);
b5b8b0ac
AO
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
2656static void
2657copy_debug_stmts (copy_body_data *id)
2658{
2659 size_t i;
2660 gimple stmt;
2661
9771b263 2662 if (!id->debug_stmts.exists ())
b5b8b0ac
AO
2663 return;
2664
9771b263 2665 FOR_EACH_VEC_ELT (id->debug_stmts, i, stmt)
b5b8b0ac
AO
2666 copy_debug_stmt (stmt, id);
2667
9771b263 2668 id->debug_stmts.release ();
b5b8b0ac
AO
2669}
2670
f82a627c
EB
2671/* Make a copy of the body of SRC_FN so that it can be inserted inline in
2672 another function. */
2673
2674static tree
2675copy_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
b5b8b0ac
AO
2685/* Make a copy of the body of FN so that it can be inserted inline in
2686 another function. */
2687
e21aff8a 2688static tree
0d63a740 2689copy_body (copy_body_data *id, gcov_type count, int frequency_scale,
91382288 2690 basic_block entry_block_map, basic_block exit_block_map,
f3b331d1 2691 basic_block new_entry)
e21aff8a 2692{
1b369fae 2693 tree fndecl = id->src_fn;
e21aff8a
SB
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)));
91382288 2698 body = copy_cfg_body (id, count, frequency_scale, entry_block_map, exit_block_map,
f3b331d1 2699 new_entry);
b5b8b0ac 2700 copy_debug_stmts (id);
e21aff8a
SB
2701
2702 return body;
2703}
2704
04482133
AO
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
2708static bool
2709self_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));
e21aff8a 2717
50886bf1 2718 return var && auto_var_in_fn_p (var, fn);
04482133
AO
2719}
2720
b5b8b0ac
AO
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
2725static gimple
2726insert_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
6de9cd9a 2764static void
b5b8b0ac 2765insert_init_stmt (copy_body_data *id, basic_block bb, gimple init_stmt)
0f1961a2 2766{
0f1961a2
JH
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)
c2a4718a
JJ
2770 {
2771 gimple_stmt_iterator si = gsi_last_bb (bb);
0f1961a2 2772
bfb0b886
RG
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. */
b5b8b0ac
AO
2777 if (!is_gimple_debug (init_stmt)
2778 && !is_gimple_reg (gimple_assign_lhs (init_stmt))
bfb0b886
RG
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 }
c2a4718a
JJ
2790 gsi_insert_after (&si, init_stmt, GSI_NEW_STMT);
2791 gimple_regimplify_operands (init_stmt, &si);
b5b8b0ac
AO
2792
2793 if (!is_gimple_debug (init_stmt) && MAY_HAVE_DEBUG_STMTS)
2794 {
70b5e7dc
RG
2795 tree def = gimple_assign_lhs (init_stmt);
2796 insert_init_debug_bind (id, bb, def, def, init_stmt);
b5b8b0ac 2797 }
c2a4718a 2798 }
0f1961a2
JH
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. */
2804static gimple
1b369fae 2805setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
e21aff8a 2806 basic_block bb, tree *vars)
6de9cd9a 2807{
0f1961a2 2808 gimple init_stmt = NULL;
6de9cd9a 2809 tree var;
f4088621 2810 tree rhs = value;
110cfe1c 2811 tree def = (gimple_in_ssa_p (cfun)
32244553 2812 ? ssa_default_def (id->src_cfun, p) : NULL);
6de9cd9a 2813
f4088621
RG
2814 if (value
2815 && value != error_mark_node
2816 && !useless_type_conversion_p (TREE_TYPE (p), TREE_TYPE (value)))
c54e3854 2817 {
c4ac6e94 2818 /* If we can match up types by promotion/demotion do so. */
c54e3854 2819 if (fold_convertible_p (TREE_TYPE (p), value))
c4ac6e94 2820 rhs = fold_convert (TREE_TYPE (p), value);
c54e3854 2821 else
c4ac6e94
RG
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 }
c54e3854 2833 }
f4088621 2834
b5b8b0ac
AO
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
b5b8b0ac 2840 /* Declare this new variable. */
910ad8de 2841 DECL_CHAIN (var) = *vars;
b5b8b0ac
AO
2842 *vars = var;
2843
2844 /* Make gimplifier happy about this variable. */
2845 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2846
110cfe1c 2847 /* If the parameter is never assigned to, has no SSA_NAMEs created,
b5b8b0ac
AO
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. */
6de9cd9a
DN
2851 if (TREE_READONLY (p)
2852 && !TREE_ADDRESSABLE (p)
110cfe1c
JH
2853 && value && !TREE_SIDE_EFFECTS (value)
2854 && !def)
6de9cd9a 2855 {
84936f6f
RH
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)
f4088621
RG
2861 && useless_type_conversion_p (TREE_TYPE (p),
2862 TREE_TYPE (value))
04482133
AO
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))
6de9cd9a 2869 {
6de9cd9a 2870 insert_decl_map (id, p, value);
b5b8b0ac
AO
2871 insert_debug_decl_map (id, p, var);
2872 return insert_init_debug_bind (id, bb, var, value, NULL);
6de9cd9a
DN
2873 }
2874 }
2875
6de9cd9a
DN
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. */
7c7d3047 2879 insert_decl_map (id, p, var);
6de9cd9a 2880
6de9cd9a
DN
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
110cfe1c
JH
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
8454d27e
JH
2900 Do replacement at -O0 for const arguments replaced by constant.
2901 This is important for builtin_constant_p and other construct requiring
b5b8b0ac 2902 constant argument to be visible in inlined function body. */
110cfe1c 2903 if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p)
8454d27e
JH
2904 && (optimize
2905 || (TREE_READONLY (p)
2906 && is_gimple_min_invariant (rhs)))
110cfe1c 2907 && (TREE_CODE (rhs) == SSA_NAME
9b718f81
JH
2908 || is_gimple_min_invariant (rhs))
2909 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
110cfe1c
JH
2910 {
2911 insert_decl_map (id, def, rhs);
b5b8b0ac 2912 return insert_init_debug_bind (id, bb, var, rhs, NULL);
110cfe1c
JH
2913 }
2914
f6f2da7d
JH
2915 /* If the value of argument is never used, don't care about initializing
2916 it. */
1cf5abb3 2917 if (optimize && gimple_in_ssa_p (cfun) && !def && is_gimple_reg (p))
f6f2da7d
JH
2918 {
2919 gcc_assert (!value || !TREE_SIDE_EFFECTS (value));
b5b8b0ac 2920 return insert_init_debug_bind (id, bb, var, rhs, NULL);
f6f2da7d
JH
2921 }
2922
6de9cd9a
DN
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 {
6de9cd9a 2927 if (rhs == error_mark_node)
110cfe1c 2928 {
7c7d3047 2929 insert_decl_map (id, p, var);
b5b8b0ac 2930 return insert_init_debug_bind (id, bb, var, rhs, NULL);
110cfe1c 2931 }
afe08db5 2932
73dab33b 2933 STRIP_USELESS_TYPE_CONVERSION (rhs);
6de9cd9a 2934
6b18b1a3 2935 /* If we are in SSA form properly remap the default definition
27eb31c9
RG
2936 or assign to a dummy SSA name if the parameter is unused and
2937 we are not optimizing. */
6b18b1a3 2938 if (gimple_in_ssa_p (cfun) && is_gimple_reg (p))
110cfe1c 2939 {
6b18b1a3
RG
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;
32244553 2945 set_ssa_default_def (cfun, var, NULL);
6b18b1a3 2946 }
27eb31c9
RG
2947 else if (!optimize)
2948 {
2949 def = make_ssa_name (var, NULL);
2950 init_stmt = gimple_build_assign (def, rhs);
2951 }
110cfe1c
JH
2952 }
2953 else
726a989a 2954 init_stmt = gimple_build_assign (var, rhs);
6de9cd9a 2955
0f1961a2 2956 if (bb && init_stmt)
b5b8b0ac 2957 insert_init_stmt (id, bb, init_stmt);
6de9cd9a 2958 }
0f1961a2 2959 return init_stmt;
6de9cd9a
DN
2960}
2961
d4e4baa9 2962/* Generate code to initialize the parameters of the function at the
726a989a 2963 top of the stack in ID from the GIMPLE_CALL STMT. */
d4e4baa9 2964
e21aff8a 2965static void
726a989a 2966initialize_inlined_parameters (copy_body_data *id, gimple stmt,
e21aff8a 2967 tree fn, basic_block bb)
d4e4baa9 2968{
d4e4baa9 2969 tree parms;
726a989a 2970 size_t i;
d4e4baa9 2971 tree p;
d436bff8 2972 tree vars = NULL_TREE;
726a989a 2973 tree static_chain = gimple_call_chain (stmt);
d4e4baa9
AO
2974
2975 /* Figure out what the parameters are. */
18c6ada9 2976 parms = DECL_ARGUMENTS (fn);
d4e4baa9 2977
d4e4baa9
AO
2978 /* Loop through the parameter declarations, replacing each with an
2979 equivalent VAR_DECL, appropriately initialized. */
910ad8de 2980 for (p = parms, i = 0; p; p = DECL_CHAIN (p), i++)
726a989a
RB
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 }
ea184343
RG
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. */
910ad8de 2990 for (p = parms, i = 0; p; p = DECL_CHAIN (p), i++)
ea184343
RG
2991 {
2992 tree *varp = (tree *) pointer_map_contains (id->decl_map, p);
2993 if (varp
2994 && TREE_CODE (*varp) == VAR_DECL)
2995 {
72aa3dca 2996 tree def = (gimple_in_ssa_p (cfun) && is_gimple_reg (p)
32244553 2997 ? ssa_default_def (id->src_cfun, p) : NULL);
72aa3dca
RG
2998 tree var = *varp;
2999 TREE_TYPE (var) = remap_type (TREE_TYPE (var), id);
ea184343
RG
3000 /* Also remap the default definition if it was remapped
3001 to the default definition of the parameter replacement
3002 by the parameter setup. */
72aa3dca 3003 if (def)
ea184343
RG
3004 {
3005 tree *defp = (tree *) pointer_map_contains (id->decl_map, def);
3006 if (defp
3007 && TREE_CODE (*defp) == SSA_NAME
72aa3dca
RG
3008 && SSA_NAME_VAR (*defp) == var)
3009 TREE_TYPE (*defp) = TREE_TYPE (var);
ea184343
RG
3010 }
3011 }
3012 }
4838c5ee 3013
6de9cd9a
DN
3014 /* Initialize the static chain. */
3015 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
ea99e0be 3016 gcc_assert (fn != current_function_decl);
6de9cd9a
DN
3017 if (p)
3018 {
3019 /* No static chain? Seems like a bug in tree-nested.c. */
1e128c5f 3020 gcc_assert (static_chain);
4838c5ee 3021
e21aff8a 3022 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
4838c5ee
AO
3023 }
3024
e21aff8a 3025 declare_inline_vars (id->block, vars);
d4e4baa9
AO
3026}
3027
726a989a 3028
e21aff8a
SB
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
110cfe1c
JH
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,
726a989a 3036 was the LHS of the MODIFY_EXPR to which this call is the RHS.
7740f00d 3037
0f900dfa
JJ
3038 The return value is a (possibly null) value that holds the result
3039 as seen by the caller. */
d4e4baa9 3040
d436bff8 3041static tree
6938f93f
JH
3042declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest,
3043 basic_block entry_bb)
d4e4baa9 3044{
1b369fae 3045 tree callee = id->src_fn;
7740f00d
RH
3046 tree result = DECL_RESULT (callee);
3047 tree callee_type = TREE_TYPE (result);
ea2edf88 3048 tree caller_type;
7740f00d 3049 tree var, use;
d4e4baa9 3050
ea2edf88
RG
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
1a2c27e9
EB
3058 /* We don't need to do anything for functions that don't return anything. */
3059 if (VOID_TYPE_P (callee_type))
0f900dfa 3060 return NULL_TREE;
d4e4baa9 3061
cc77ae10 3062 /* If there was a return slot, then the return value is the
7740f00d 3063 dereferenced address of that object. */
110cfe1c 3064 if (return_slot)
7740f00d 3065 {
110cfe1c 3066 /* The front end shouldn't have used both return_slot and
7740f00d 3067 a modify expression. */
1e128c5f 3068 gcc_assert (!modify_dest);
cc77ae10 3069 if (DECL_BY_REFERENCE (result))
110cfe1c
JH
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
b8698a0f 3075 for variables believed to be not addressable.
110cfe1c
JH
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);
110cfe1c
JH
3081 var = return_slot_addr;
3082 }
cc77ae10 3083 else
110cfe1c
JH
3084 {
3085 var = return_slot;
3086 gcc_assert (TREE_CODE (var) != SSA_NAME);
b5ca517c 3087 TREE_ADDRESSABLE (var) |= TREE_ADDRESSABLE (result);
110cfe1c 3088 }
0890b981
AP
3089 if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
3090 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
3091 && !DECL_GIMPLE_REG_P (result)
22918034 3092 && DECL_P (var))
0890b981 3093 DECL_GIMPLE_REG_P (var) = 0;
7740f00d
RH
3094 use = NULL;
3095 goto done;
3096 }
3097
3098 /* All types requiring non-trivial constructors should have been handled. */
1e128c5f 3099 gcc_assert (!TREE_ADDRESSABLE (callee_type));
7740f00d
RH
3100
3101 /* Attempt to avoid creating a new temporary variable. */
110cfe1c
JH
3102 if (modify_dest
3103 && TREE_CODE (modify_dest) != SSA_NAME)
7740f00d
RH
3104 {
3105 bool use_it = false;
3106
3107 /* We can't use MODIFY_DEST if there's type promotion involved. */
f4088621 3108 if (!useless_type_conversion_p (callee_type, caller_type))
7740f00d
RH
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. */
e2f9fe42
RH
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;
0890b981
AP
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))
1d327c16 3136 use_it = false;
e2f9fe42
RH
3137 else if (!TREE_ADDRESSABLE (base_m))
3138 use_it = true;
3139 }
7740f00d
RH
3140
3141 if (use_it)
3142 {
3143 var = modify_dest;
3144 use = NULL;
3145 goto done;
3146 }
3147 }
3148
1e128c5f 3149 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
7740f00d 3150
c08cd4c1 3151 var = copy_result_decl_to_var (result, id);
7740f00d 3152 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
7740f00d 3153
6de9cd9a 3154 /* Do not have the rest of GCC warn about this variable as it should
471854f8 3155 not be visible to the user. */
6de9cd9a 3156 TREE_NO_WARNING (var) = 1;
d4e4baa9 3157
c08cd4c1
JM
3158 declare_inline_vars (id->block, var);
3159
7740f00d
RH
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;
f4088621 3163 if (!useless_type_conversion_p (caller_type, TREE_TYPE (var)))
c4ac6e94
RG
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 }
b8698a0f 3185
73dab33b 3186 STRIP_USELESS_TYPE_CONVERSION (use);
7740f00d 3187
c08cd4c1 3188 if (DECL_BY_REFERENCE (result))
32848948
RG
3189 {
3190 TREE_ADDRESSABLE (var) = 1;
3191 var = build_fold_addr_expr (var);
3192 }
c08cd4c1 3193
7740f00d 3194 done:
d4e4baa9
AO
3195 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
3196 way, when the RESULT_DECL is encountered, it will be
6938f93f
JH
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);
6b18b1a3
RG
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);
32244553 3212 insert_decl_map (id, ssa_default_def (id->src_cfun, result), temp);
6b18b1a3 3213 }
6938f93f
JH
3214 insert_init_stmt (id, entry_bb, gimple_build_assign (temp, var));
3215 }
3216 else
3217 insert_decl_map (id, result, var);
d4e4baa9 3218
6de9cd9a
DN
3219 /* Remember this so we can ignore it in remap_decls. */
3220 id->retvar = var;
3221
0f900dfa 3222 return use;
d4e4baa9
AO
3223}
3224
27dbd3ac
RH
3225/* Callback through walk_tree. Determine if a DECL_INITIAL makes reference
3226 to a local label. */
4838c5ee 3227
27dbd3ac
RH
3228static tree
3229has_label_address_in_static_1 (tree *nodep, int *walk_subtrees, void *fnp)
4838c5ee 3230{
27dbd3ac
RH
3231 tree node = *nodep;
3232 tree fn = (tree) fnp;
726a989a 3233
27dbd3ac
RH
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}
726a989a 3242
27dbd3ac
RH
3243/* Determine if the function can be copied. If so return NULL. If
3244 not return a string describng the reason for failure. */
3245
3246static const char *
3247copy_forbidden (struct function *fun, tree fndecl)
3248{
3249 const char *reason = fun->cannot_be_copied_reason;
c021f10b
NF
3250 tree decl;
3251 unsigned ix;
27dbd3ac
RH
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
c021f10b
NF
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 }
27dbd3ac
RH
3283
3284 fail:
3285 fun->cannot_be_copied_reason = reason;
3286 fun->cannot_be_copied_set = true;
3287 return reason;
3288}
3289
3290
3291static 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. */
c986baf6 3295
c986baf6 3296static tree
726a989a
RB
3297inline_forbidden_p_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
3298 struct walk_stmt_info *wip)
c986baf6 3299{
726a989a 3300 tree fn = (tree) wip->info;
f08545a8 3301 tree t;
726a989a 3302 gimple stmt = gsi_stmt (*gsi);
c986baf6 3303
726a989a 3304 switch (gimple_code (stmt))
f08545a8 3305 {
726a989a 3306 case GIMPLE_CALL:
3197c4fd
AS
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
63d2a353
MM
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. */
726a989a 3314 if (gimple_alloca_call_p (stmt)
63d2a353 3315 && !gimple_call_alloca_for_var_p (stmt)
f08545a8
JH
3316 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
3317 {
ddd2d57e 3318 inline_forbidden_reason
dee15844 3319 = G_("function %q+F can never be inlined because it uses "
ddd2d57e 3320 "alloca (override using the always_inline attribute)");
726a989a
RB
3321 *handled_ops_p = true;
3322 return fn;
f08545a8 3323 }
726a989a
RB
3324
3325 t = gimple_call_fndecl (stmt);
3326 if (t == NULL_TREE)
f08545a8 3327 break;
84f5e1b1 3328
f08545a8
JH
3329 /* We cannot inline functions that call setjmp. */
3330 if (setjmp_call_p (t))
3331 {
ddd2d57e 3332 inline_forbidden_reason
dee15844 3333 = G_("function %q+F can never be inlined because it uses setjmp");
726a989a
RB
3334 *handled_ops_p = true;
3335 return t;
f08545a8
JH
3336 }
3337
6de9cd9a 3338 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
3197c4fd 3339 switch (DECL_FUNCTION_CODE (t))
f08545a8 3340 {
3197c4fd
AS
3341 /* We cannot inline functions that take a variable number of
3342 arguments. */
3343 case BUILT_IN_VA_START:
3197c4fd
AS
3344 case BUILT_IN_NEXT_ARG:
3345 case BUILT_IN_VA_END:
6de9cd9a 3346 inline_forbidden_reason
dee15844 3347 = G_("function %q+F can never be inlined because it "
6de9cd9a 3348 "uses variable argument lists");
726a989a
RB
3349 *handled_ops_p = true;
3350 return t;
6de9cd9a 3351
3197c4fd 3352 case BUILT_IN_LONGJMP:
6de9cd9a
DN
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
dee15844 3359 = G_("function %q+F can never be inlined because "
6de9cd9a 3360 "it uses setjmp-longjmp exception handling");
726a989a
RB
3361 *handled_ops_p = true;
3362 return t;
6de9cd9a
DN
3363
3364 case BUILT_IN_NONLOCAL_GOTO:
3365 /* Similarly. */
3366 inline_forbidden_reason
dee15844 3367 = G_("function %q+F can never be inlined because "
6de9cd9a 3368 "it uses non-local goto");
726a989a
RB
3369 *handled_ops_p = true;
3370 return t;
f08545a8 3371
4b284111
JJ
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
dee15844 3379 = G_("function %q+F can never be inlined because "
4b284111 3380 "it uses __builtin_return or __builtin_apply_args");
726a989a
RB
3381 *handled_ops_p = true;
3382 return t;
4b284111 3383
3197c4fd
AS
3384 default:
3385 break;
3386 }
f08545a8
JH
3387 break;
3388
726a989a
RB
3389 case GIMPLE_GOTO:
3390 t = gimple_goto_dest (stmt);
f08545a8
JH
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 {
ddd2d57e 3398 inline_forbidden_reason
dee15844 3399 = G_("function %q+F can never be inlined "
ddd2d57e 3400 "because it contains a computed goto");
726a989a
RB
3401 *handled_ops_p = true;
3402 return t;
f08545a8 3403 }
6de9cd9a 3404 break;
f08545a8 3405
f08545a8
JH
3406 default:
3407 break;
3408 }
3409
726a989a 3410 *handled_ops_p = false;
f08545a8 3411 return NULL_TREE;
84f5e1b1
RH
3412}
3413
726a989a
RB
3414/* Return true if FNDECL is a function that cannot be inlined into
3415 another one. */
3416
3417static bool
f08545a8 3418inline_forbidden_p (tree fndecl)
84f5e1b1 3419{
2092ee7d 3420 struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
726a989a
RB
3421 struct walk_stmt_info wi;
3422 struct pointer_set_t *visited_nodes;
3423 basic_block bb;
3424 bool forbidden_p = false;
3425
27dbd3ac
RH
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. */
726a989a
RB
3433 visited_nodes = pointer_set_create ();
3434 memset (&wi, 0, sizeof (wi));
3435 wi.info = (void *) fndecl;
3436 wi.pset = visited_nodes;
e21aff8a 3437
2092ee7d 3438 FOR_EACH_BB_FN (bb, fun)
726a989a
RB
3439 {
3440 gimple ret;
3441 gimple_seq seq = bb_seq (bb);
27dbd3ac 3442 ret = walk_gimple_seq (seq, inline_forbidden_p_stmt, NULL, &wi);
726a989a
RB
3443 forbidden_p = (ret != NULL);
3444 if (forbidden_p)
27dbd3ac 3445 break;
2092ee7d
JJ
3446 }
3447
726a989a 3448 pointer_set_destroy (visited_nodes);
726a989a 3449 return forbidden_p;
84f5e1b1 3450}
6399c0ab
SB
3451\f
3452/* Return false if the function FNDECL cannot be inlined on account of its
3453 attributes, true otherwise. */
3454static bool
3455function_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}
84f5e1b1 3474
b3c3af2f
SB
3475/* Returns nonzero if FN is a function that does not have any
3476 fundamental inline blocking properties. */
d4e4baa9 3477
27dbd3ac
RH
3478bool
3479tree_inlinable_function_p (tree fn)
d4e4baa9 3480{
b3c3af2f 3481 bool inlinable = true;
18177c7e
RG
3482 bool do_warning;
3483 tree always_inline;
d4e4baa9
AO
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))
b3c3af2f 3488 return false;
d4e4baa9 3489
18177c7e
RG
3490 /* We only warn for functions declared `inline' by the user. */
3491 do_warning = (warn_inline
18177c7e 3492 && DECL_DECLARED_INLINE_P (fn)
0494626a 3493 && !DECL_NO_INLINE_WARNING_P (fn)
18177c7e
RG
3494 && !DECL_IN_SYSTEM_HEADER (fn));
3495
3496 always_inline = lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn));
3497
e90acd93 3498 if (flag_no_inline
18177c7e
RG
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
18177c7e
RG
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 }
46c5ad27 3514
f08545a8 3515 else if (inline_forbidden_p (fn))
b3c3af2f
SB
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
18177c7e
RG
3523 function is not inlinable. */
3524 if (always_inline)
c9fc06dc 3525 error (inline_forbidden_reason, fn);
2d327012 3526 else if (do_warning)
d2fcbf6f 3527 warning (OPT_Winline, inline_forbidden_reason, fn);
b3c3af2f
SB
3528
3529 inlinable = false;
3530 }
d4e4baa9
AO
3531
3532 /* Squirrel away the result so that we don't have to check again. */
b3c3af2f 3533 DECL_UNINLINABLE (fn) = !inlinable;
d4e4baa9 3534
b3c3af2f
SB
3535 return inlinable;
3536}
3537
e5c4f28a
RG
3538/* Estimate the cost of a memory move. Use machine dependent
3539 word size and take possible memcpy call into account. */
3540
3541int
3542estimate_move_cost (tree type)
3543{
3544 HOST_WIDE_INT size;
3545
078c3644
JH
3546 gcc_assert (!VOID_TYPE_P (type));
3547
c204d113
L
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
e5c4f28a
RG
3558 size = int_size_in_bytes (type);
3559
e04ad03d 3560 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO (!optimize_size))
e5c4f28a
RG
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
726a989a 3567/* Returns cost of operation CODE, according to WEIGHTS */
7f9bc51b 3568
726a989a 3569static int
02f0b13a
JH
3570estimate_operator_cost (enum tree_code code, eni_weights *weights,
3571 tree op1 ATTRIBUTE_UNUSED, tree op2)
6de9cd9a 3572{
726a989a 3573 switch (code)
6de9cd9a 3574 {
726a989a
RB
3575 /* These are "free" conversions, or their presumed cost
3576 is folded into other operations. */
61fcaeec 3577 case RANGE_EXPR:
1a87cf0c 3578 CASE_CONVERT:
726a989a
RB
3579 case COMPLEX_EXPR:
3580 case PAREN_EXPR:
d4d92cd3 3581 case VIEW_CONVERT_EXPR:
726a989a 3582 return 0;
6de9cd9a 3583
e5c4f28a
RG
3584 /* Assign cost of 1 to usual operations.
3585 ??? We may consider mapping RTL costs to this. */
6de9cd9a 3586 case COND_EXPR:
4151978d 3587 case VEC_COND_EXPR:
2205ed25 3588 case VEC_PERM_EXPR:
6de9cd9a
DN
3589
3590 case PLUS_EXPR:
5be014d5 3591 case POINTER_PLUS_EXPR:
6de9cd9a
DN
3592 case MINUS_EXPR:
3593 case MULT_EXPR:
98449720 3594 case MULT_HIGHPART_EXPR:
16949072 3595 case FMA_EXPR:
6de9cd9a 3596
09e881c9 3597 case ADDR_SPACE_CONVERT_EXPR:
325217ed 3598 case FIXED_CONVERT_EXPR:
6de9cd9a 3599 case FIX_TRUNC_EXPR:
6de9cd9a
DN
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:
a6b46ba2
DN
3611 case VEC_LSHIFT_EXPR:
3612 case VEC_RSHIFT_EXPR:
6de9cd9a
DN
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:
d1a7edaf 3640 case LTGT_EXPR:
6de9cd9a 3641
6de9cd9a
DN
3642 case CONJ_EXPR:
3643
3644 case PREDECREMENT_EXPR:
3645 case PREINCREMENT_EXPR:
3646 case POSTDECREMENT_EXPR:
3647 case POSTINCREMENT_EXPR:
3648
16630a2c
DN
3649 case REALIGN_LOAD_EXPR:
3650
61d3cdbb
DN
3651 case REDUC_MAX_EXPR:
3652 case REDUC_MIN_EXPR:
3653 case REDUC_PLUS_EXPR:
20f06221 3654 case WIDEN_SUM_EXPR:
726a989a
RB
3655 case WIDEN_MULT_EXPR:
3656 case DOT_PROD_EXPR:
0354c0c7
BS
3657 case WIDEN_MULT_PLUS_EXPR:
3658 case WIDEN_MULT_MINUS_EXPR:
36ba4aae 3659 case WIDEN_LSHIFT_EXPR:
726a989a 3660
89d67cca
DN
3661 case VEC_WIDEN_MULT_HI_EXPR:
3662 case VEC_WIDEN_MULT_LO_EXPR:
3f30a9a6
RH
3663 case VEC_WIDEN_MULT_EVEN_EXPR:
3664 case VEC_WIDEN_MULT_ODD_EXPR:
89d67cca
DN
3665 case VEC_UNPACK_HI_EXPR:
3666 case VEC_UNPACK_LO_EXPR:
d9987fb4
UB
3667 case VEC_UNPACK_FLOAT_HI_EXPR:
3668 case VEC_UNPACK_FLOAT_LO_EXPR:
8115817b 3669 case VEC_PACK_TRUNC_EXPR:
89d67cca 3670 case VEC_PACK_SAT_EXPR:
d9987fb4 3671 case VEC_PACK_FIX_TRUNC_EXPR:
36ba4aae
IR
3672 case VEC_WIDEN_LSHIFT_HI_EXPR:
3673 case VEC_WIDEN_LSHIFT_LO_EXPR:
98b44b0e 3674
726a989a 3675 return 1;
6de9cd9a 3676
1ea7e6ad 3677 /* Few special cases of expensive operations. This is useful
6de9cd9a
DN
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:
02f0b13a
JH
3689 if (TREE_CODE (op2) != INTEGER_CST)
3690 return weights->div_mod_cost;
3691 return 1;
726a989a
RB
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
3705static
3706int 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
3722int
3723estimate_num_insns (gimple stmt, eni_weights *weights)
3724{
3725 unsigned cost, i;
3726 enum gimple_code code = gimple_code (stmt);
3727 tree lhs;
02f0b13a 3728 tree rhs;
726a989a
RB
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. */
bccc50d4
JJ
3751 if (gimple_clobber_p (stmt))
3752 return 0; /* ={v} {CLOBBER} stmt expands to nothing. */
3753
726a989a 3754 lhs = gimple_assign_lhs (stmt);
02f0b13a
JH
3755 rhs = gimple_assign_rhs1 (stmt);
3756
c12d9242 3757 cost = 0;
726a989a 3758
c12d9242
RB
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))
02f0b13a
JH
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);
726a989a
RB
3770 break;
3771
3772 case GIMPLE_COND:
02f0b13a
JH
3773 cost = 1 + estimate_operator_cost (gimple_cond_code (stmt), weights,
3774 gimple_op (stmt, 0),
3775 gimple_op (stmt, 1));
726a989a
RB
3776 break;
3777
3778 case GIMPLE_SWITCH:
3779 /* Take into account cost of the switch + guess 2 conditional jumps for
b8698a0f 3780 each case label.
726a989a
RB
3781
3782 TODO: once the switch expansion logic is sufficiently separated, we can
3783 do better job on estimating cost of the switch. */
02f0b13a
JH
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;
6de9cd9a 3788 break;
726a989a
RB
3789
3790 case GIMPLE_CALL:
6de9cd9a 3791 {
726a989a 3792 tree decl = gimple_call_fndecl (stmt);
d2d668fb 3793 struct cgraph_node *node = NULL;
6de9cd9a 3794
9bb2f479
JH
3795 /* Do not special case builtins where we see the body.
3796 This just confuse inliner. */
67348ccc 3797 if (!decl || !(node = cgraph_get_node (decl)) || node->definition)
e9f7ad79 3798 ;
9bb2f479
JH
3799 /* For buitins that are likely expanded to nothing or
3800 inlined do not account operand costs. */
3801 else if (is_simple_builtin (decl))
bec922f0
SL
3802 return 0;
3803 else if (is_inexpensive_builtin (decl))
9bb2f479 3804 return weights->target_builtin_call_cost;
e9f7ad79
RG
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 }
b8698a0f 3827
d2d668fb 3828 cost = node ? weights->call_cost : weights->indirect_call_cost;
3c04921b
RG
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++)
c7f599d0 3832 {
3c04921b
RG
3833 tree arg = gimple_call_arg (stmt, i);
3834 cost += estimate_move_cost (TREE_TYPE (arg));
c7f599d0 3835 }
6de9cd9a
DN
3836 break;
3837 }
88f4034b 3838
9bb2f479
JH
3839 case GIMPLE_RETURN:
3840 return weights->return_cost;
3841
726a989a
RB
3842 case GIMPLE_GOTO:
3843 case GIMPLE_LABEL:
3844 case GIMPLE_NOP:
3845 case GIMPLE_PHI:
726a989a 3846 case GIMPLE_PREDICT:
b5b8b0ac 3847 case GIMPLE_DEBUG:
726a989a
RB
3848 return 0;
3849
3850 case GIMPLE_ASM:
cc4029ee
AK
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 }
726a989a 3859
1d65f45c
RH
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
726a989a
RB
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:
acf0174b 3905 case GIMPLE_OMP_TASKGROUP:
726a989a
RB
3906 case GIMPLE_OMP_ORDERED:
3907 case GIMPLE_OMP_SECTION:
3908 case GIMPLE_OMP_SECTIONS:
3909 case GIMPLE_OMP_SINGLE:
acf0174b
JJ
3910 case GIMPLE_OMP_TARGET:
3911 case GIMPLE_OMP_TEAMS:
726a989a
RB
3912 return (weights->omp_cost
3913 + estimate_num_insns_seq (gimple_omp_body (stmt), weights));
88f4034b 3914
0a35513e
AH
3915 case GIMPLE_TRANSACTION:
3916 return (weights->tm_cost
3917 + estimate_num_insns_seq (gimple_transaction_body (stmt),
3918 weights));
3919
6de9cd9a 3920 default:
1e128c5f 3921 gcc_unreachable ();
6de9cd9a 3922 }
726a989a
RB
3923
3924 return cost;
6de9cd9a
DN
3925}
3926
726a989a
RB
3927/* Estimate number of instructions that will be created by expanding
3928 function FNDECL. WEIGHTS contains weights attributed to various
3929 constructs. */
aa4a53af 3930
6de9cd9a 3931int
726a989a 3932estimate_num_insns_fn (tree fndecl, eni_weights *weights)
6de9cd9a 3933{
726a989a
RB
3934 struct function *my_function = DECL_STRUCT_FUNCTION (fndecl);
3935 gimple_stmt_iterator bsi;
e21aff8a 3936 basic_block bb;
726a989a 3937 int n = 0;
e21aff8a 3938
726a989a
RB
3939 gcc_assert (my_function && my_function->cfg);
3940 FOR_EACH_BB_FN (bb, my_function)
e21aff8a 3941 {
726a989a
RB
3942 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
3943 n += estimate_num_insns (gsi_stmt (bsi), weights);
e21aff8a 3944 }
e21aff8a 3945
726a989a 3946 return n;
7f9bc51b
ZD
3947}
3948
726a989a 3949
7f9bc51b
ZD
3950/* Initializes weights used by estimate_num_insns. */
3951
3952void
3953init_inline_once (void)
3954{
7f9bc51b 3955 eni_size_weights.call_cost = 1;
d2d668fb 3956 eni_size_weights.indirect_call_cost = 3;
625a2efb 3957 eni_size_weights.target_builtin_call_cost = 1;
7f9bc51b 3958 eni_size_weights.div_mod_cost = 1;
7f9bc51b 3959 eni_size_weights.omp_cost = 40;
0a35513e 3960 eni_size_weights.tm_cost = 10;
02f0b13a 3961 eni_size_weights.time_based = false;
9bb2f479 3962 eni_size_weights.return_cost = 1;
7f9bc51b
ZD
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
ea2c620c 3967 we choose a rather small value here. */
7f9bc51b 3968 eni_time_weights.call_cost = 10;
d2d668fb 3969 eni_time_weights.indirect_call_cost = 15;
9bb2f479 3970 eni_time_weights.target_builtin_call_cost = 1;
7f9bc51b 3971 eni_time_weights.div_mod_cost = 10;
7f9bc51b 3972 eni_time_weights.omp_cost = 40;
0a35513e 3973 eni_time_weights.tm_cost = 40;
02f0b13a 3974 eni_time_weights.time_based = true;
9bb2f479 3975 eni_time_weights.return_cost = 2;
6de9cd9a
DN
3976}
3977
726a989a
RB
3978/* Estimate the number of instructions in a gimple_seq. */
3979
3980int
3981count_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
e21aff8a 3992/* Install new lexical TREE_BLOCK underneath 'current_block'. */
726a989a 3993
e21aff8a 3994static void
4a283090 3995prepend_lexical_block (tree current_block, tree new_block)
e21aff8a 3996{
4a283090
JH
3997 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (current_block);
3998 BLOCK_SUBBLOCKS (current_block) = new_block;
e21aff8a 3999 BLOCK_SUPERCONTEXT (new_block) = current_block;
e21aff8a
SB
4000}
4001
c021f10b
NF
4002/* Add local variables from CALLEE to CALLER. */
4003
4004static inline void
4005add_local_variables (struct function *callee, struct function *caller,
ae0379fc 4006 copy_body_data *id)
c021f10b
NF
4007{
4008 tree var;
4009 unsigned ix;
4010
4011 FOR_EACH_LOCAL_DECL (callee, ix, var)
ae0379fc 4012 if (!can_be_nonlocal (var, id))
42694189
JJ
4013 {
4014 tree new_var = remap_decl (var, id);
4015
4016 /* Remap debug-expressions. */
4017 if (TREE_CODE (new_var) == VAR_DECL
839b422f 4018 && DECL_HAS_DEBUG_EXPR_P (var)
42694189
JJ
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);
839b422f 4028 DECL_HAS_DEBUG_EXPR_P (new_var) = 1;
42694189
JJ
4029 }
4030 add_local_decl (caller, new_var);
4031 }
c021f10b
NF
4032}
4033
726a989a 4034/* If STMT is a GIMPLE_CALL, replace it with its inline expansion. */
d4e4baa9 4035
e21aff8a 4036static bool
726a989a 4037expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
d4e4baa9 4038{
0f900dfa 4039 tree use_retvar;
d436bff8 4040 tree fn;
b5b8b0ac 4041 struct pointer_map_t *st, *dst;
110cfe1c 4042 tree return_slot;
7740f00d 4043 tree modify_dest;
6de9cd9a 4044 location_t saved_location;
e21aff8a 4045 struct cgraph_edge *cg_edge;
61a05df1 4046 cgraph_inline_failed_t reason;
e21aff8a
SB
4047 basic_block return_block;
4048 edge e;
726a989a 4049 gimple_stmt_iterator gsi, stmt_gsi;
e21aff8a 4050 bool successfully_inlined = FALSE;
4f6c2131 4051 bool purge_dead_abnormal_edges;
d4e4baa9 4052
6de9cd9a
DN
4053 /* Set input_location here so we get the right instantiation context
4054 if we call instantiate_decl from inlinable_function_p. */
532aafad 4055 /* FIXME: instantiate_decl isn't called by inlinable_function_p. */
6de9cd9a 4056 saved_location = input_location;
035775c8 4057 input_location = gimple_location (stmt);
6de9cd9a 4058
d4e4baa9 4059 /* From here on, we're only interested in CALL_EXPRs. */
726a989a 4060 if (gimple_code (stmt) != GIMPLE_CALL)
6de9cd9a 4061 goto egress;
d4e4baa9 4062
db09f943
MJ
4063 cg_edge = cgraph_edge (id->dst_node, stmt);
4064 gcc_checking_assert (cg_edge);
d4e4baa9
AO
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. */
db09f943 4067 if (cg_edge->indirect_unknown_callee)
3949c4a7 4068 goto egress;
67348ccc 4069 fn = cg_edge->callee->decl;
db09f943 4070 gcc_checking_assert (fn);
b58b1157 4071
726a989a 4072 /* If FN is a declaration of a function in a nested scope that was
a1a0fd4e
AO
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
726a989a
RB
4078 gimple_body. */
4079 if (!DECL_INITIAL (fn)
a1a0fd4e 4080 && DECL_ABSTRACT_ORIGIN (fn)
39ecc018 4081 && gimple_has_body_p (DECL_ABSTRACT_ORIGIN (fn)))
a1a0fd4e
AO
4082 fn = DECL_ABSTRACT_ORIGIN (fn);
4083
8f4f502f 4084 /* Don't try to inline functions that are not well-suited to inlining. */
9c8305f8 4085 if (cg_edge->inline_failed)
a833faa5 4086 {
9c8305f8 4087 reason = cg_edge->inline_failed;
3e293154
MJ
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. */
e33c6cd6 4091 if (cg_edge->indirect_inlining_edge)
3e293154
MJ
4092 goto egress;
4093
7fac66d4 4094 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
bfc61b40
JH
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
df9dda2d
ST
4101 /* During early inline pass, report only when optimization is
4102 not turned on. */
4103 && (cgraph_global_info_ready
4104 || !optimize)
c9fc06dc
CB
4105 /* PR 20090218-1_0.c. Body can be provided by another module. */
4106 && (reason != CIF_BODY_NOT_AVAILABLE || !flag_generate_lto))
2d327012 4107 {
c9fc06dc
CB
4108 error ("inlining failed in call to always_inline %q+F: %s", fn,
4109 cgraph_inline_failed_string (reason));
4110 error ("called from here");
2d327012 4111 }
ff7037dc
EB
4112 else if (warn_inline
4113 && DECL_DECLARED_INLINE_P (fn)
4114 && !DECL_NO_INLINE_WARNING_P (fn)
2d327012 4115 && !DECL_IN_SYSTEM_HEADER (fn)
61a05df1 4116 && reason != CIF_UNSPECIFIED
d63db217 4117 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
d7d1d041
RG
4118 /* Do not warn about not inlined recursive calls. */
4119 && !cgraph_edge_recursive_p (cg_edge)
d63db217 4120 /* Avoid warnings during early inline pass. */
7e8b322a 4121 && cgraph_global_info_ready)
a833faa5 4122 {
dee15844 4123 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
49c8958b 4124 fn, _(cgraph_inline_failed_string (reason)));
3176a0c2 4125 warning (OPT_Winline, "called from here");
a833faa5 4126 }
6de9cd9a 4127 goto egress;
a833faa5 4128 }
67348ccc 4129 fn = cg_edge->callee->decl;
a2e2a668 4130 cgraph_get_body (cg_edge->callee);
d4e4baa9 4131
18c6ada9 4132#ifdef ENABLE_CHECKING
67348ccc 4133 if (cg_edge->callee->decl != id->dst_node->decl)
e21aff8a 4134 verify_cgraph_node (cg_edge->callee);
18c6ada9
JH
4135#endif
4136
e21aff8a 4137 /* We will be inlining this callee. */
1d65f45c 4138 id->eh_lp_nr = lookup_stmt_eh_lp (stmt);
e21aff8a 4139
f9417da1 4140 /* Update the callers EH personality. */
67348ccc
DM
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);
f9417da1 4144
726a989a 4145 /* Split the block holding the GIMPLE_CALL. */
e21aff8a
SB
4146 e = split_block (bb, stmt);
4147 bb = e->src;
4148 return_block = e->dest;
4149 remove_edge (e);
4150
4f6c2131
EB
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
726a989a
RB
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);
4f6c2131 4157
726a989a 4158 /* If the GIMPLE_CALL was in the last statement of BB, it may have
4f6c2131
EB
4159 been the source of abnormal edges. In this case, schedule
4160 the removal of dead abnormal edges. */
726a989a
RB
4161 gsi = gsi_start_bb (return_block);
4162 if (gsi_end_p (gsi))
e21aff8a 4163 {
726a989a 4164 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
4f6c2131 4165 purge_dead_abnormal_edges = true;
e21aff8a 4166 }
4f6c2131
EB
4167 else
4168 {
726a989a 4169 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
4f6c2131
EB
4170 purge_dead_abnormal_edges = false;
4171 }
4172
726a989a 4173 stmt_gsi = gsi_start_bb (return_block);
742a37d5 4174
d436bff8
AH
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
3e492e9c
RB
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. */
5368224f 4185 if (gimple_block (stmt))
3e492e9c
RB
4186 {
4187 id->block = make_node (BLOCK);
4188 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
a9d5a059 4189 BLOCK_SOURCE_LOCATION (id->block) = LOCATION_LOCUS (input_location);
3e492e9c
RB
4190 prepend_lexical_block (gimple_block (stmt), id->block);
4191 }
e21aff8a 4192
d4e4baa9
AO
4193 /* Local declarations will be replaced by their equivalents in this
4194 map. */
4195 st = id->decl_map;
6be42dd4 4196 id->decl_map = pointer_map_create ();
b5b8b0ac
AO
4197 dst = id->debug_map;
4198 id->debug_map = NULL;
d4e4baa9 4199
e21aff8a 4200 /* Record the function we are about to inline. */
1b369fae
RH
4201 id->src_fn = fn;
4202 id->src_node = cg_edge->callee;
110cfe1c 4203 id->src_cfun = DECL_STRUCT_FUNCTION (fn);
726a989a 4204 id->gimple_call = stmt;
1b369fae 4205
3c8da8a5
AO
4206 gcc_assert (!id->src_cfun->after_inlining);
4207
045685a9 4208 id->entry_bb = bb;
7299cb99
JH
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 }
726a989a 4216 initialize_inlined_parameters (id, stmt, fn, bb);
d4e4baa9 4217
ea99e0be 4218 if (DECL_INITIAL (fn))
94645a02 4219 {
3e492e9c
RB
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);
94645a02 4247 }
acb8f212 4248
d4e4baa9
AO
4249 /* Return statements in the function body will be replaced by jumps
4250 to the RET_LABEL. */
1e128c5f
GB
4251 gcc_assert (DECL_INITIAL (fn));
4252 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
23700f65 4253
726a989a 4254 /* Find the LHS to which the result of this call is assigned. */
110cfe1c 4255 return_slot = NULL;
726a989a 4256 if (gimple_call_lhs (stmt))
81bafd36 4257 {
726a989a 4258 modify_dest = gimple_call_lhs (stmt);
81bafd36
ILT
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;
726a989a
RB
4268
4269 if (gimple_call_return_slot_opt_p (stmt))
fa47911c 4270 {
110cfe1c 4271 return_slot = modify_dest;
fa47911c
JM
4272 modify_dest = NULL;
4273 }
81bafd36 4274 }
7740f00d
RH
4275 else
4276 modify_dest = NULL;
4277
1ea193c2
ILT
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
d4e4baa9 4289 /* Declare the return variable for the function. */
6938f93f 4290 use_retvar = declare_return_variable (id, return_slot, modify_dest, bb);
1ea193c2 4291
acb8f212 4292 /* Add local vars in this inlined callee to caller. */
ae0379fc 4293 add_local_variables (id->src_cfun, cfun, id);
acb8f212 4294
0d63a740
JH
4295 if (dump_file && (dump_flags & TDF_DETAILS))
4296 {
4297 fprintf (dump_file, "Inlining ");
b8698a0f 4298 print_generic_expr (dump_file, id->src_fn, 0);
0d63a740 4299 fprintf (dump_file, " to ");
b8698a0f 4300 print_generic_expr (dump_file, id->dst_fn, 0);
0d63a740
JH
4301 fprintf (dump_file, " with frequency %i\n", cg_edge->frequency);
4302 }
4303
eb50f5f4
JH
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. */
0d63a740 4309 copy_body (id, bb->count,
8b47039c 4310 GCOV_COMPUTE_SCALE (cg_edge->frequency, CGRAPH_FREQ_BASE),
f3b331d1 4311 bb, return_block, NULL);
eb50f5f4 4312
d086d311 4313 /* Reset the escaped solution. */
6b8ed145 4314 if (cfun->gimple_df)
d086d311 4315 pt_solution_reset (&cfun->gimple_df->escaped);
6b8ed145 4316
d4e4baa9 4317 /* Clean up. */
b5b8b0ac
AO
4318 if (id->debug_map)
4319 {
4320 pointer_map_destroy (id->debug_map);
4321 id->debug_map = dst;
4322 }
6be42dd4 4323 pointer_map_destroy (id->decl_map);
d4e4baa9
AO
4324 id->decl_map = st;
4325
5006671f
RG
4326 /* Unlink the calls virtual operands before replacing it. */
4327 unlink_stmt_vdef (stmt);
4328
84936f6f 4329 /* If the inlined function returns a result that we care about,
726a989a
RB
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))
e21aff8a 4334 {
726a989a
RB
4335 gimple old_stmt = stmt;
4336 stmt = gimple_build_assign (gimple_call_lhs (stmt), use_retvar);
4337 gsi_replace (&stmt_gsi, stmt, false);
726a989a 4338 maybe_clean_or_replace_eh_stmt (old_stmt, stmt);
e21aff8a 4339 }
6de9cd9a 4340 else
110cfe1c 4341 {
726a989a
RB
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)
110cfe1c 4346 {
726a989a
RB
4347 tree name = gimple_call_lhs (stmt);
4348 tree var = SSA_NAME_VAR (name);
32244553 4349 tree def = ssa_default_def (cfun, var);
110cfe1c 4350
110cfe1c
JH
4351 if (def)
4352 {
726a989a
RB
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);
110cfe1c 4357 }
110cfe1c
JH
4358 else
4359 {
726a989a
RB
4360 /* Otherwise make this variable undefined. */
4361 gsi_remove (&stmt_gsi, true);
32244553 4362 set_ssa_default_def (cfun, var, name);
726a989a 4363 SSA_NAME_DEF_STMT (name) = gimple_build_nop ();
110cfe1c
JH
4364 }
4365 }
4366 else
726a989a 4367 gsi_remove (&stmt_gsi, true);
110cfe1c 4368 }
d4e4baa9 4369
4f6c2131 4370 if (purge_dead_abnormal_edges)
30fd5881
EB
4371 {
4372 gimple_purge_dead_eh_edges (return_block);
4373 gimple_purge_dead_abnormal_call_edges (return_block);
4374 }
84936f6f 4375
e21aff8a
SB
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. */
726a989a
RB
4379 if (is_gimple_assign (stmt))
4380 {
4381 gcc_assert (gimple_assign_single_p (stmt)
1a87cf0c 4382 || CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)));
726a989a
RB
4383 TREE_USED (gimple_assign_rhs1 (stmt)) = 1;
4384 }
84936f6f 4385
1eb3331e
DB
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. */
3e492e9c 4390 if (gimple_block (stmt))
67348ccc 4391 (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
84936f6f 4392
e72fcfe8 4393 /* Update callgraph if needed. */
e21aff8a 4394 cgraph_remove_node (cg_edge->callee);
e72fcfe8 4395
e21aff8a 4396 id->block = NULL_TREE;
e21aff8a 4397 successfully_inlined = TRUE;
742a37d5 4398
6de9cd9a
DN
4399 egress:
4400 input_location = saved_location;
e21aff8a 4401 return successfully_inlined;
d4e4baa9 4402}
6de9cd9a 4403
e21aff8a
SB
4404/* Expand call statements reachable from STMT_P.
4405 We can only have CALL_EXPRs as the "toplevel" tree code or nested
0a35513e 4406 in a MODIFY_EXPR. */
e21aff8a
SB
4407
4408static bool
1b369fae 4409gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
6de9cd9a 4410{
726a989a 4411 gimple_stmt_iterator gsi;
6de9cd9a 4412
726a989a 4413 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6de9cd9a 4414 {
726a989a 4415 gimple stmt = gsi_stmt (gsi);
e21aff8a 4416
726a989a
RB
4417 if (is_gimple_call (stmt)
4418 && expand_call_inline (bb, stmt, id))
4419 return true;
6de9cd9a 4420 }
726a989a 4421
e21aff8a 4422 return false;
6de9cd9a
DN
4423}
4424
726a989a 4425
b8a00a4d
JH
4426/* Walk all basic blocks created after FIRST and try to fold every statement
4427 in the STATEMENTS pointer set. */
726a989a 4428
b8a00a4d
JH
4429static void
4430fold_marked_statements (int first, struct pointer_set_t *statements)
4431{
0cae8d31 4432 for (; first < n_basic_blocks_for_fn (cfun); first++)
b8a00a4d
JH
4433 if (BASIC_BLOCK (first))
4434 {
726a989a
RB
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)))
9477eb38 4441 {
726a989a 4442 gimple old_stmt = gsi_stmt (gsi);
4b685e14 4443 tree old_decl = is_gimple_call (old_stmt) ? gimple_call_fndecl (old_stmt) : 0;
2bafad93 4444
44e10129
MM
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;
a9d24544
JJ
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 }
44e10129
MM
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))
9477eb38 4495 {
726a989a
RB
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
4b685e14
JH
4501 if (is_gimple_call (old_stmt)
4502 || is_gimple_call (new_stmt))
44e10129
MM
4503 cgraph_update_edges_for_call_stmt (old_stmt, old_decl,
4504 new_stmt);
726a989a
RB
4505
4506 if (maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt))
4507 gimple_purge_dead_eh_edges (BASIC_BLOCK (first));
9477eb38
JH
4508 }
4509 }
b8a00a4d
JH
4510 }
4511}
4512
d4e4baa9
AO
4513/* Expand calls to inline functions in the body of FN. */
4514
873aa8f5 4515unsigned int
46c5ad27 4516optimize_inline_calls (tree fn)
d4e4baa9 4517{
1b369fae 4518 copy_body_data id;
e21aff8a 4519 basic_block bb;
0cae8d31 4520 int last = n_basic_blocks_for_fn (cfun);
d406b663 4521 struct gimplify_ctx gctx;
5d7b099c 4522 bool inlined_p = false;
d406b663 4523
d4e4baa9
AO
4524 /* Clear out ID. */
4525 memset (&id, 0, sizeof (id));
4526
581985d7 4527 id.src_node = id.dst_node = cgraph_get_node (fn);
67348ccc 4528 gcc_assert (id.dst_node->definition);
1b369fae 4529 id.dst_fn = fn;
d4e4baa9 4530 /* Or any functions that aren't finished yet. */
d4e4baa9 4531 if (current_function_decl)
0f900dfa 4532 id.dst_fn = current_function_decl;
1b369fae
RH
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;
78bbd765 4538 id.transform_parameter = true;
9ff420f1 4539 id.transform_lang_insert_block = NULL;
b8a00a4d 4540 id.statements_to_fold = pointer_set_create ();
1b369fae 4541
d406b663 4542 push_gimplify_context (&gctx);
d4e4baa9 4543
672987e8
ZD
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
726a989a
RB
4548 /* Register specific gimple functions. */
4549 gimple_register_cfg_hooks ();
4550
e21aff8a
SB
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)
5d7b099c 4558 inlined_p |= gimple_expand_calls_inline (bb, &id);
d4e4baa9 4559
e21aff8a 4560 pop_gimplify_context (NULL);
6de9cd9a 4561
18c6ada9
JH
4562#ifdef ENABLE_CHECKING
4563 {
4564 struct cgraph_edge *e;
4565
1b369fae 4566 verify_cgraph_node (id.dst_node);
18c6ada9
JH
4567
4568 /* Double check that we inlined everything we are supposed to inline. */
1b369fae 4569 for (e = id.dst_node->callees; e; e = e->next_callee)
1e128c5f 4570 gcc_assert (e->inline_failed);
18c6ada9
JH
4571 }
4572#endif
b8698a0f 4573
5d7b099c 4574 /* Fold queued statements. */
a9eafe81
AP
4575 fold_marked_statements (last, id.statements_to_fold);
4576 pointer_set_destroy (id.statements_to_fold);
b8698a0f 4577
9771b263 4578 gcc_assert (!id.debug_stmts.exists ());
b5b8b0ac 4579
5d7b099c
RG
4580 /* If we didn't inline into the function there is nothing to do. */
4581 if (!inlined_p)
4582 return 0;
4583
a9eafe81
AP
4584 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4585 number_blocks (fn);
b8a00a4d 4586
078c3644
JH
4587 delete_unreachable_blocks_update_callgraph (&id);
4588#ifdef ENABLE_CHECKING
4589 verify_cgraph_node (id.dst_node);
4590#endif
726a989a 4591
110cfe1c
JH
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. */
726a989a
RB
4596 return (TODO_update_ssa
4597 | TODO_cleanup_cfg
45a80bb9 4598 | (gimple_in_ssa_p (cfun) ? TODO_remove_unused_locals : 0)
5d7b099c 4599 | (gimple_in_ssa_p (cfun) ? TODO_update_address_taken : 0)
45a80bb9 4600 | (profile_status != PROFILE_ABSENT ? TODO_rebuild_frequencies : 0));
d4e4baa9
AO
4601}
4602
d4e4baa9
AO
4603/* Passed to walk_tree. Copies the node pointed to, if appropriate. */
4604
4605tree
46c5ad27 4606copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
d4e4baa9
AO
4607{
4608 enum tree_code code = TREE_CODE (*tp);
07beea0d 4609 enum tree_code_class cl = TREE_CODE_CLASS (code);
d4e4baa9
AO
4610
4611 /* We make copies of most nodes. */
07beea0d 4612 if (IS_EXPR_CODE_CLASS (cl)
d4e4baa9
AO
4613 || code == TREE_LIST
4614 || code == TREE_VEC
8843c120
DN
4615 || code == TYPE_DECL
4616 || code == OMP_CLAUSE)
d4e4baa9
AO
4617 {
4618 /* Because the chain gets clobbered when we make a copy, we save it
4619 here. */
82d6e6fc 4620 tree chain = NULL_TREE, new_tree;
07beea0d 4621
81f653d6
NF
4622 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
4623 chain = TREE_CHAIN (*tp);
d4e4baa9
AO
4624
4625 /* Copy the node. */
82d6e6fc 4626 new_tree = copy_node (*tp);
6de9cd9a 4627
82d6e6fc 4628 *tp = new_tree;
d4e4baa9
AO
4629
4630 /* Now, restore the chain, if appropriate. That will cause
4631 walk_tree to walk into the chain as well. */
50674e96
DN
4632 if (code == PARM_DECL
4633 || code == TREE_LIST
aaf46ef9 4634 || code == OMP_CLAUSE)
d4e4baa9
AO
4635 TREE_CHAIN (*tp) = chain;
4636
4637 /* For now, we don't update BLOCKs when we make copies. So, we
6de9cd9a
DN
4638 have to nullify all BIND_EXPRs. */
4639 if (TREE_CODE (*tp) == BIND_EXPR)
4640 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
d4e4baa9 4641 }
4038c495
GB
4642 else if (code == CONSTRUCTOR)
4643 {
4644 /* CONSTRUCTOR nodes need special handling because
4645 we need to duplicate the vector of elements. */
82d6e6fc 4646 tree new_tree;
4038c495 4647
82d6e6fc 4648 new_tree = copy_node (*tp);
9771b263 4649 CONSTRUCTOR_ELTS (new_tree) = vec_safe_copy (CONSTRUCTOR_ELTS (*tp));
82d6e6fc 4650 *tp = new_tree;
4038c495 4651 }
3533b943 4652 else if (code == STATEMENT_LIST)
deb5046b
JM
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);
6615c446 4656 else if (TREE_CODE_CLASS (code) == tcc_type)
d4e4baa9 4657 *walk_subtrees = 0;
6615c446 4658 else if (TREE_CODE_CLASS (code) == tcc_declaration)
6de9cd9a 4659 *walk_subtrees = 0;
a396f8ae
GK
4660 else if (TREE_CODE_CLASS (code) == tcc_constant)
4661 *walk_subtrees = 0;
d4e4baa9
AO
4662 return NULL_TREE;
4663}
4664
4665/* The SAVE_EXPR pointed to by TP is being copied. If ST contains
aa4a53af 4666 information indicating to what new SAVE_EXPR this one should be mapped,
e21aff8a
SB
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. */
d4e4baa9 4669
892c7e1e 4670static void
82c82743 4671remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
d4e4baa9 4672{
6be42dd4
RG
4673 struct pointer_map_t *st = (struct pointer_map_t *) st_;
4674 tree *n;
5e20bdd7 4675 tree t;
d4e4baa9
AO
4676
4677 /* See if we already encountered this SAVE_EXPR. */
6be42dd4 4678 n = (tree *) pointer_map_contains (st, *tp);
d92b4486 4679
d4e4baa9
AO
4680 /* If we didn't already remap this SAVE_EXPR, do so now. */
4681 if (!n)
4682 {
5e20bdd7 4683 t = copy_node (*tp);
d4e4baa9 4684
d4e4baa9 4685 /* Remember this SAVE_EXPR. */
6be42dd4 4686 *pointer_map_insert (st, *tp) = t;
350ebd54 4687 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
6be42dd4 4688 *pointer_map_insert (st, t) = t;
d4e4baa9
AO
4689 }
4690 else
5e20bdd7
JZ
4691 {
4692 /* We've already walked into this SAVE_EXPR; don't do it again. */
4693 *walk_subtrees = 0;
6be42dd4 4694 t = *n;
5e20bdd7 4695 }
d4e4baa9
AO
4696
4697 /* Replace this SAVE_EXPR with the copy. */
5e20bdd7 4698 *tp = t;
d4e4baa9 4699}
d436bff8 4700
726a989a
RB
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
4705static tree
4706mark_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
4730static tree
4731replace_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
4779static tree
4780replace_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))
9771b263
DN
4800 gimple_bind_set_vars (stmt, remap_decls (gimple_bind_vars (stmt),
4801 NULL, id));
726a989a
RB
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
4812gimple_seq
4813copy_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 ();
b5b8b0ac 4829 id.debug_map = NULL;
726a989a
RB
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;
78bbd765 4835 id.transform_parameter = false;
726a989a
RB
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);
b5b8b0ac
AO
4855 if (id.debug_map)
4856 pointer_map_destroy (id.debug_map);
726a989a
RB
4857
4858 return copy;
4859}
4860
4861
6de9cd9a 4862/* Allow someone to determine if SEARCH is a child of TOP from gdb. */
aa4a53af 4863
6de9cd9a
DN
4864static tree
4865debug_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
24e47c76 4873DEBUG_FUNCTION bool
6de9cd9a
DN
4874debug_find_tree (tree top, tree search)
4875{
4876 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
4877}
4878
e21aff8a 4879
6de9cd9a
DN
4880/* Declare the variables created by the inliner. Add all the variables in
4881 VARS to BIND_EXPR. */
4882
4883static void
e21aff8a 4884declare_inline_vars (tree block, tree vars)
6de9cd9a 4885{
84936f6f 4886 tree t;
910ad8de 4887 for (t = vars; t; t = DECL_CHAIN (t))
9659ce8b
JH
4888 {
4889 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
4890 gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
c021f10b 4891 add_local_decl (cfun, t);
9659ce8b 4892 }
6de9cd9a 4893
e21aff8a
SB
4894 if (block)
4895 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
4896}
4897
19734dd8 4898/* Copy NODE (which must be a DECL). The DECL originally was in the FROM_FN,
1b369fae
RH
4899 but now it will be in the TO_FN. PARM_TO_VAR means enable PARM_DECL to
4900 VAR_DECL translation. */
19734dd8 4901
1b369fae
RH
4902static tree
4903copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
19734dd8 4904{
19734dd8
RL
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
b8698a0f 4911 declaration inspired this copy. */
19734dd8
RL
4912 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
4913
4914 /* The new variable/label has no RTL, yet. */
68a976f2
RL
4915 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
4916 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
2eb79bbb 4917 SET_DECL_RTL (copy, 0);
b8698a0f 4918
19734dd8
RL
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 ;
1b369fae 4926 else if (DECL_CONTEXT (decl) != id->src_fn)
19734dd8
RL
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. */
1b369fae 4937 DECL_CONTEXT (copy) = id->dst_fn;
19734dd8
RL
4938
4939 return copy;
4940}
4941
1b369fae
RH
4942static tree
4943copy_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
c2255bc4
AH
4952 copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
4953 VAR_DECL, DECL_NAME (decl), type);
25a6a873
RG
4954 if (DECL_PT_UID_SET_P (decl))
4955 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
1b369fae
RH
4956 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4957 TREE_READONLY (copy) = TREE_READONLY (decl);
4958 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
0890b981 4959 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
1b369fae
RH
4960
4961 return copy_decl_for_dup_finish (id, decl, copy);
4962}
4963
c08cd4c1
JM
4964/* Like copy_decl_to_var, but create a return slot object instead of a
4965 pointer variable for return by invisible reference. */
4966
4967static tree
4968copy_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
c2255bc4
AH
4979 copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
4980 VAR_DECL, DECL_NAME (decl), type);
25a6a873
RG
4981 if (DECL_PT_UID_SET_P (decl))
4982 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
c08cd4c1
JM
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);
0890b981 4988 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
c08cd4c1
JM
4989 }
4990
4991 return copy_decl_for_dup_finish (id, decl, copy);
4992}
4993
9ff420f1 4994tree
1b369fae
RH
4995copy_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
5016static tree
5017copy_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
19734dd8
RL
5025/* Return a copy of the function's argument tree. */
5026static tree
c6f7cfc1
JH
5027copy_arguments_for_versioning (tree orig_parm, copy_body_data * id,
5028 bitmap args_to_skip, tree *vars)
19734dd8 5029{
c6f7cfc1
JH
5030 tree arg, *parg;
5031 tree new_parm = NULL;
5032 int i = 0;
19734dd8 5033
c6f7cfc1
JH
5034 parg = &new_parm;
5035
910ad8de 5036 for (arg = orig_parm; arg; arg = DECL_CHAIN (arg), i++)
c6f7cfc1
JH
5037 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
5038 {
5039 tree new_tree = remap_decl (arg, id);
d7da5cc8
MJ
5040 if (TREE_CODE (new_tree) != PARM_DECL)
5041 new_tree = id->copy_decl (arg, id);
c6f7cfc1
JH
5042 lang_hooks.dup_lang_specific_decl (new_tree);
5043 *parg = new_tree;
910ad8de 5044 parg = &DECL_CHAIN (new_tree);
c6f7cfc1 5045 }
eb50f5f4 5046 else if (!pointer_map_contains (id->decl_map, arg))
c6f7cfc1
JH
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);
c6f7cfc1
JH
5052 insert_decl_map (id, arg, var);
5053 /* Declare this new variable. */
910ad8de 5054 DECL_CHAIN (var) = *vars;
c6f7cfc1
JH
5055 *vars = var;
5056 }
5057 return new_parm;
19734dd8
RL
5058}
5059
5060/* Return a copy of the function's static chain. */
5061static tree
1b369fae 5062copy_static_chain (tree static_chain, copy_body_data * id)
19734dd8
RL
5063{
5064 tree *chain_copy, *pvar;
5065
5066 chain_copy = &static_chain;
910ad8de 5067 for (pvar = chain_copy; *pvar; pvar = &DECL_CHAIN (*pvar))
19734dd8 5068 {
82d6e6fc
KG
5069 tree new_tree = remap_decl (*pvar, id);
5070 lang_hooks.dup_lang_specific_decl (new_tree);
910ad8de 5071 DECL_CHAIN (new_tree) = DECL_CHAIN (*pvar);
82d6e6fc 5072 *pvar = new_tree;
19734dd8
RL
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. */
27dbd3ac 5079
19734dd8
RL
5080bool
5081tree_versionable_function_p (tree fndecl)
5082{
86631ea3
MJ
5083 return (!lookup_attribute ("noclone", DECL_ATTRIBUTES (fndecl))
5084 && copy_forbidden (DECL_STRUCT_FUNCTION (fndecl), fndecl) == NULL);
19734dd8
RL
5085}
5086
9187e02d
JH
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. */
9f5e9983 5090
9187e02d
JH
5091static bool
5092delete_unreachable_blocks_update_callgraph (copy_body_data *id)
9f5e9983 5093{
9187e02d
JH
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))
042ae7d2
JH
5110 {
5111 struct cgraph_edge *e;
5112 struct cgraph_node *node;
9187e02d 5113
67348ccc 5114 ipa_remove_stmt_references (id->dst_node, gsi_stmt (bsi));
042ae7d2
JH
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;)
9187e02d 5127 {
67348ccc 5128 ipa_remove_stmt_references (node, gsi_stmt (bsi));
042ae7d2
JH
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;
9187e02d 5142 else
042ae7d2
JH
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 }
9187e02d 5149 }
042ae7d2 5150 }
9187e02d
JH
5151 delete_basic_block (b);
5152 changed = true;
5153 }
5154 }
5155
9187e02d 5156 return changed;
9f5e9983
JJ
5157}
5158
08ad1d6d
JH
5159/* Update clone info after duplication. */
5160
5161static void
5162update_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;
9771b263 5173 for (i = 0; i < vec_safe_length (node->clone.tree_map); i++)
08ad1d6d
JH
5174 {
5175 struct ipa_replace_map *replace_info;
9771b263 5176 replace_info = (*node->clone.tree_map)[i];
08ad1d6d
JH
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
19734dd8
RL
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
b8698a0f
L
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
ea99e0be 5201 trees. If UPDATE_CLONES is set, the call_stmt fields
91382288
JH
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.
1a2c27e9 5206 If SKIP_RETURN is true, the new version will return void.
91382288
JH
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*/
19734dd8 5210void
27dbd3ac 5211tree_function_versioning (tree old_decl, tree new_decl,
9771b263 5212 vec<ipa_replace_map_p, va_gc> *tree_map,
91382288 5213 bool update_clones, bitmap args_to_skip,
1a2c27e9
EB
5214 bool skip_return, bitmap blocks_to_copy,
5215 basic_block new_entry)
19734dd8
RL
5216{
5217 struct cgraph_node *old_version_node;
5218 struct cgraph_node *new_version_node;
1b369fae 5219 copy_body_data id;
110cfe1c 5220 tree p;
19734dd8
RL
5221 unsigned i;
5222 struct ipa_replace_map *replace_info;
b5b8b0ac 5223 basic_block old_entry_block, bb;
07687835 5224 stack_vec<gimple, 10> init_stmts;
0f1961a2 5225 tree vars = NULL_TREE;
19734dd8
RL
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
fe660d7b
MJ
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);
19734dd8 5235
ddb555ed
JJ
5236 /* Copy over debug args. */
5237 if (DECL_HAS_DEBUG_ARGS_P (old_decl))
5238 {
9771b263 5239 vec<tree, va_gc> **new_debug_args, **old_debug_args;
ddb555ed
JJ
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);
9771b263 5246 *new_debug_args = vec_safe_copy (*old_debug_args);
ddb555ed
JJ
5247 }
5248 }
5249
a3aadcc5
JH
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
19734dd8
RL
5256 DECL_ARTIFICIAL (new_decl) = 1;
5257 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
c0c123ef
JH
5258 if (DECL_ORIGIN (old_decl) == old_decl)
5259 old_version_node->used_as_abstract_origin = true;
f9417da1 5260 DECL_FUNCTION_PERSONALITY (new_decl) = DECL_FUNCTION_PERSONALITY (old_decl);
19734dd8 5261
3d283195
JH
5262 /* Prepare the data structures for the tree copy. */
5263 memset (&id, 0, sizeof (id));
5264
19734dd8 5265 /* Generate a new name for the new version. */
9187e02d 5266 id.statements_to_fold = pointer_set_create ();
b5b8b0ac 5267
6be42dd4 5268 id.decl_map = pointer_map_create ();
b5b8b0ac 5269 id.debug_map = NULL;
1b369fae
RH
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);
4029a5e0 5275 id.blocks_to_copy = blocks_to_copy;
9771b263 5276 if (id.src_node->ipa_transforms_to_apply.exists ())
0e3776db 5277 {
9771b263
DN
5278 vec<ipa_opt_pass> old_transforms_to_apply
5279 = id.dst_node->ipa_transforms_to_apply;
0e3776db
JH
5280 unsigned int i;
5281
9771b263
DN
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 ();
0e3776db 5287 }
b8698a0f 5288
1b369fae
RH
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;
78bbd765 5294 id.transform_parameter = false;
9ff420f1 5295 id.transform_lang_insert_block = NULL;
1b369fae 5296
110cfe1c
JH
5297 old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
5298 (DECL_STRUCT_FUNCTION (old_decl));
c0c123ef
JH
5299 DECL_RESULT (new_decl) = DECL_RESULT (old_decl);
5300 DECL_ARGUMENTS (new_decl) = DECL_ARGUMENTS (old_decl);
110cfe1c 5301 initialize_cfun (new_decl, old_decl,
0d63a740 5302 old_entry_block->count);
1755aad0
RG
5303 DECL_STRUCT_FUNCTION (new_decl)->gimple_df->ipa_pta
5304 = id.src_cfun->gimple_df->ipa_pta;
b8698a0f 5305
19734dd8
RL
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);
b8698a0f 5312
19734dd8
RL
5313 /* If there's a tree_map, prepare for substitution. */
5314 if (tree_map)
9771b263 5315 for (i = 0; i < tree_map->length (); i++)
19734dd8 5316 {
0f1961a2 5317 gimple init;
9771b263 5318 replace_info = (*tree_map)[i];
1b369fae 5319 if (replace_info->replace_p)
00fc2333 5320 {
922f15c2
JH
5321 if (!replace_info->old_tree)
5322 {
5323 int i = replace_info->parm_num;
5324 tree parm;
0e8853ee
JH
5325 tree req_type;
5326
910ad8de 5327 for (parm = DECL_ARGUMENTS (old_decl); i; parm = DECL_CHAIN (parm))
922f15c2
JH
5328 i --;
5329 replace_info->old_tree = parm;
0e8853ee
JH
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);
922f15c2 5361 }
00fc2333 5362 }
19734dd8 5363 }
eb50f5f4
JH
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);
b8698a0f 5369
eb50f5f4 5370 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
474086eb 5371 BLOCK_SUPERCONTEXT (DECL_INITIAL (new_decl)) = new_decl;
b8698a0f 5372
0f1961a2 5373 declare_inline_vars (DECL_INITIAL (new_decl), vars);
9187e02d 5374
9771b263 5375 if (!vec_safe_is_empty (DECL_STRUCT_FUNCTION (old_decl)->local_decls))
19734dd8 5376 /* Add local vars. */
ae0379fc 5377 add_local_variables (DECL_STRUCT_FUNCTION (old_decl), cfun, &id);
b8698a0f 5378
90dda0e9 5379 if (DECL_RESULT (old_decl) == NULL_TREE)
1a2c27e9 5380 ;
90dda0e9 5381 else if (skip_return && !VOID_TYPE_P (TREE_TYPE (DECL_RESULT (old_decl))))
1a2c27e9
EB
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
19734dd8 5391 {
6ff38230
RG
5392 tree old_name;
5393 DECL_RESULT (new_decl) = remap_decl (DECL_RESULT (old_decl), &id);
19734dd8 5394 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
6ff38230
RG
5395 if (gimple_in_ssa_p (id.src_cfun)
5396 && DECL_BY_REFERENCE (DECL_RESULT (old_decl))
32244553 5397 && (old_name = ssa_default_def (id.src_cfun, DECL_RESULT (old_decl))))
6ff38230
RG
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 ();
32244553 5402 set_ssa_default_def (cfun, DECL_RESULT (new_decl), new_name);
6ff38230 5403 }
19734dd8 5404 }
b8698a0f 5405
a9e0d843 5406 /* Set up the destination functions loop tree. */
0fc822d0 5407 if (loops_for_fn (DECL_STRUCT_FUNCTION (old_decl)) != NULL)
a9e0d843
RB
5408 {
5409 cfun->curr_properties &= ~PROP_loops;
5410 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
5411 cfun->curr_properties |= PROP_loops;
5412 }
5413
6ff38230
RG
5414 /* Copy the Function's body. */
5415 copy_body (&id, old_entry_block->count, REG_BR_PROB_BASE,
f3b331d1 5416 ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, new_entry);
6ff38230 5417
19734dd8
RL
5418 /* Renumber the lexical scoping (non-code) blocks consecutively. */
5419 number_blocks (new_decl);
5420
b5b8b0ac
AO
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));
9771b263
DN
5425 while (init_stmts.length ())
5426 insert_init_stmt (&id, bb, init_stmts.pop ());
08ad1d6d 5427 update_clone_info (&id);
0f1961a2 5428
27dbd3ac
RH
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
19734dd8 5439 /* Clean up. */
6be42dd4 5440 pointer_map_destroy (id.decl_map);
b5b8b0ac
AO
5441 if (id.debug_map)
5442 pointer_map_destroy (id.debug_map);
5006671f
RG
5443 free_dominance_info (CDI_DOMINATORS);
5444 free_dominance_info (CDI_POST_DOMINATORS);
9187e02d
JH
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);
67348ccc 5450 if (id.dst_node->definition)
99b766fc 5451 cgraph_rebuild_references ();
9187e02d 5452 update_ssa (TODO_update_ssa);
b35366ce
JH
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);
02ec6988
MJ
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);
b35366ce
JH
5474 e->count = bb->count;
5475 }
5476 }
5477
9187e02d
JH
5478 free_dominance_info (CDI_DOMINATORS);
5479 free_dominance_info (CDI_POST_DOMINATORS);
5480
9771b263 5481 gcc_assert (!id.debug_stmts.exists ());
110cfe1c 5482 pop_cfun ();
19734dd8
RL
5483 return;
5484}
5485
f82a627c
EB
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
5489tree
5490maybe_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;
910ad8de 5505 param = DECL_CHAIN (param), arg = next_call_expr_arg (&iter))
f82a627c
EB
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;
78bbd765 5518 id.transform_parameter = true;
267ffce3 5519 id.transform_lang_insert_block = NULL;
f82a627c
EB
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. */
1d65f45c 5527 id.eh_lp_nr = 0;
f82a627c
EB
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
52dd234b
RH
5541/* Duplicate a type, fields and all. */
5542
5543tree
5544build_duplicate_type (tree type)
5545{
1b369fae 5546 struct copy_body_data id;
52dd234b
RH
5547
5548 memset (&id, 0, sizeof (id));
1b369fae
RH
5549 id.src_fn = current_function_decl;
5550 id.dst_fn = current_function_decl;
5551 id.src_cfun = cfun;
6be42dd4 5552 id.decl_map = pointer_map_create ();
b5b8b0ac 5553 id.debug_map = NULL;
4009f2e7 5554 id.copy_decl = copy_decl_no_change;
52dd234b
RH
5555
5556 type = remap_type_1 (type, &id);
5557
6be42dd4 5558 pointer_map_destroy (id.decl_map);
b5b8b0ac
AO
5559 if (id.debug_map)
5560 pointer_map_destroy (id.debug_map);
52dd234b 5561
f31c9f09
DG
5562 TYPE_CANONICAL (type) = type;
5563
52dd234b
RH
5564 return type;
5565}