]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-inline.c
Makefile.in (TREE_SSA_LIVE_H): Add vecprim.h.
[thirdparty/gcc.git] / gcc / tree-inline.c
CommitLineData
ac534736 1/* Tree inlining.
20f06221 2 Copyright 2001, 2002, 2003, 2004, 2005, 2006 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
AO
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
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
54a7b573 18along with GCC; see the file COPYING. If not, write to
366ccddb
KC
19the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20Boston, MA 02110-1301, USA. */
588d3ade
AO
21
22#include "config.h"
23#include "system.h"
4977bab6
ZW
24#include "coretypes.h"
25#include "tm.h"
69dcadff 26#include "toplev.h"
588d3ade
AO
27#include "tree.h"
28#include "tree-inline.h"
d4e4baa9
AO
29#include "rtl.h"
30#include "expr.h"
31#include "flags.h"
32#include "params.h"
33#include "input.h"
34#include "insn-config.h"
d4e4baa9
AO
35#include "varray.h"
36#include "hashtab.h"
d23c55c2 37#include "langhooks.h"
e21aff8a
SB
38#include "basic-block.h"
39#include "tree-iterator.h"
1c4a429a 40#include "cgraph.h"
ddd2d57e 41#include "intl.h"
6de9cd9a 42#include "tree-mudflap.h"
089efaa4 43#include "tree-flow.h"
18c6ada9 44#include "function.h"
e21aff8a
SB
45#include "ggc.h"
46#include "tree-flow.h"
6de9cd9a 47#include "diagnostic.h"
e21aff8a 48#include "except.h"
1eb3331e 49#include "debug.h"
e21aff8a 50#include "pointer-set.h"
19734dd8 51#include "ipa-prop.h"
d4e4baa9 52
6de9cd9a
DN
53/* I'm not real happy about this, but we need to handle gimple and
54 non-gimple trees. */
eadf906f 55#include "tree-gimple.h"
588d3ade 56
1b369fae 57/* Inlining, Cloning, Versioning, Parallelization
e21aff8a
SB
58
59 Inlining: a function body is duplicated, but the PARM_DECLs are
60 remapped into VAR_DECLs, and non-void RETURN_EXPRs become
61 MODIFY_EXPRs that store to a dedicated returned-value variable.
62 The duplicated eh_region info of the copy will later be appended
63 to the info for the caller; the eh_region info in copied throwing
64 statements and RESX_EXPRs is adjusted accordingly.
65
e21aff8a
SB
66 Cloning: (only in C++) We have one body for a con/de/structor, and
67 multiple function decls, each with a unique parameter list.
68 Duplicate the body, using the given splay tree; some parameters
69 will become constants (like 0 or 1).
70
1b369fae
RH
71 Versioning: a function body is duplicated and the result is a new
72 function rather than into blocks of an existing function as with
73 inlining. Some parameters will become constants.
74
75 Parallelization: a region of a function is duplicated resulting in
76 a new function. Variables may be replaced with complex expressions
77 to enable shared variable semantics.
78
e21aff8a
SB
79 All of these will simultaneously lookup any callgraph edges. If
80 we're going to inline the duplicated function body, and the given
81 function has some cloned callgraph nodes (one for each place this
82 function will be inlined) those callgraph edges will be duplicated.
1b369fae 83 If we're cloning the body, those callgraph edges will be
e21aff8a
SB
84 updated to point into the new body. (Note that the original
85 callgraph node and edge list will not be altered.)
86
87 See the CALL_EXPR handling case in copy_body_r (). */
88
588d3ade 89/* 0 if we should not perform inlining.
d92b4486
KH
90 1 if we should expand functions calls inline at the tree level.
91 2 if we should consider *all* functions to be inline
588d3ade
AO
92 candidates. */
93
94int flag_inline_trees = 0;
d4e4baa9
AO
95
96/* To Do:
97
98 o In order to make inlining-on-trees work, we pessimized
99 function-local static constants. In particular, they are now
100 always output, even when not addressed. Fix this by treating
101 function-local static constants just like global static
102 constants; the back-end already knows not to output them if they
103 are not needed.
104
105 o Provide heuristics to clamp inlining of recursive template
106 calls? */
107
d4e4baa9
AO
108/* Prototypes. */
109
1b369fae
RH
110static tree declare_return_variable (copy_body_data *, tree, tree, tree *);
111static tree copy_generic_body (copy_body_data *);
b3c3af2f 112static bool inlinable_function_p (tree);
1b369fae
RH
113static void remap_block (tree *, copy_body_data *);
114static tree remap_decls (tree, copy_body_data *);
115static void copy_bind_expr (tree *, int *, copy_body_data *);
6de9cd9a 116static tree mark_local_for_remap_r (tree *, int *, void *);
19114537 117static void unsave_expr_1 (tree);
6de9cd9a 118static tree unsave_r (tree *, int *, void *);
e21aff8a 119static void declare_inline_vars (tree, tree);
892c7e1e 120static void remap_save_expr (tree *, void *, int *);
acb8f212 121static void add_lexical_block (tree current_block, tree new_block);
1b369fae
RH
122static tree copy_decl_to_var (tree, copy_body_data *);
123static tree copy_decl_no_change (tree, copy_body_data *);
124static tree copy_decl_maybe_to_var (tree, copy_body_data *);
e21aff8a 125
5e20bdd7
JZ
126/* Insert a tree->tree mapping for ID. Despite the name suggests
127 that the trees should be variables, it is used for more than that. */
128
1b369fae
RH
129void
130insert_decl_map (copy_body_data *id, tree key, tree value)
5e20bdd7
JZ
131{
132 splay_tree_insert (id->decl_map, (splay_tree_key) key,
133 (splay_tree_value) value);
134
135 /* Always insert an identity map as well. If we see this same new
136 node again, we won't want to duplicate it a second time. */
137 if (key != value)
138 splay_tree_insert (id->decl_map, (splay_tree_key) value,
139 (splay_tree_value) value);
140}
141
e21aff8a 142/* Remap DECL during the copying of the BLOCK tree for the function. */
d4e4baa9 143
1b369fae
RH
144tree
145remap_decl (tree decl, copy_body_data *id)
d4e4baa9 146{
e21aff8a
SB
147 splay_tree_node n;
148 tree fn;
149
150 /* We only remap local variables in the current function. */
1b369fae 151 fn = id->src_fn;
3c2a7a6a 152
e21aff8a
SB
153 /* See if we have remapped this declaration. */
154
155 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
156
157 /* If we didn't already have an equivalent for this declaration,
158 create one now. */
d4e4baa9
AO
159 if (!n)
160 {
d4e4baa9 161 /* Make a copy of the variable or label. */
1b369fae 162 tree t = id->copy_decl (decl, id);
19734dd8 163
596b98ce
AO
164 /* Remember it, so that if we encounter this local entity again
165 we can reuse this copy. Do this early because remap_type may
166 need this decl for TYPE_STUB_DECL. */
167 insert_decl_map (id, decl, t);
168
1b369fae
RH
169 if (!DECL_P (t))
170 return t;
171
3c2a7a6a
RH
172 /* Remap types, if necessary. */
173 TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
174 if (TREE_CODE (t) == TYPE_DECL)
175 DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
3c2a7a6a
RH
176
177 /* Remap sizes as necessary. */
178 walk_tree (&DECL_SIZE (t), copy_body_r, id, NULL);
179 walk_tree (&DECL_SIZE_UNIT (t), copy_body_r, id, NULL);
d4e4baa9 180
8c27b7d4 181 /* If fields, do likewise for offset and qualifier. */
5377d5ba
RK
182 if (TREE_CODE (t) == FIELD_DECL)
183 {
184 walk_tree (&DECL_FIELD_OFFSET (t), copy_body_r, id, NULL);
185 if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
186 walk_tree (&DECL_QUALIFIER (t), copy_body_r, id, NULL);
187 }
188
5e20bdd7 189 return t;
d4e4baa9
AO
190 }
191
6de9cd9a 192 return unshare_expr ((tree) n->value);
d4e4baa9
AO
193}
194
3c2a7a6a 195static tree
1b369fae 196remap_type_1 (tree type, copy_body_data *id)
3c2a7a6a 197{
1b369fae 198 splay_tree_node node;
3c2a7a6a
RH
199 tree new, t;
200
1b369fae
RH
201 if (type == NULL)
202 return type;
203
204 /* See if we have remapped this type. */
205 node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
206 if (node)
207 return (tree) node->value;
208
209 /* The type only needs remapping if it's variably modified. */
210 if (! variably_modified_type_p (type, id->src_fn))
211 {
212 insert_decl_map (id, type, type);
213 return type;
214 }
215
ed397c43
RK
216 /* We do need a copy. build and register it now. If this is a pointer or
217 reference type, remap the designated type and make a new pointer or
218 reference type. */
219 if (TREE_CODE (type) == POINTER_TYPE)
220 {
221 new = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
222 TYPE_MODE (type),
223 TYPE_REF_CAN_ALIAS_ALL (type));
224 insert_decl_map (id, type, new);
225 return new;
226 }
227 else if (TREE_CODE (type) == REFERENCE_TYPE)
228 {
229 new = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
230 TYPE_MODE (type),
231 TYPE_REF_CAN_ALIAS_ALL (type));
232 insert_decl_map (id, type, new);
233 return new;
234 }
235 else
236 new = copy_node (type);
237
5e20bdd7 238 insert_decl_map (id, type, new);
3c2a7a6a
RH
239
240 /* This is a new type, not a copy of an old type. Need to reassociate
241 variants. We can handle everything except the main variant lazily. */
242 t = TYPE_MAIN_VARIANT (type);
243 if (type != t)
244 {
245 t = remap_type (t, id);
246 TYPE_MAIN_VARIANT (new) = t;
247 TYPE_NEXT_VARIANT (new) = TYPE_MAIN_VARIANT (t);
248 TYPE_NEXT_VARIANT (t) = new;
249 }
250 else
251 {
252 TYPE_MAIN_VARIANT (new) = new;
253 TYPE_NEXT_VARIANT (new) = NULL;
254 }
255
596b98ce
AO
256 if (TYPE_STUB_DECL (type))
257 TYPE_STUB_DECL (new) = remap_decl (TYPE_STUB_DECL (type), id);
258
3c2a7a6a
RH
259 /* Lazily create pointer and reference types. */
260 TYPE_POINTER_TO (new) = NULL;
261 TYPE_REFERENCE_TO (new) = NULL;
262
263 switch (TREE_CODE (new))
264 {
265 case INTEGER_TYPE:
266 case REAL_TYPE:
267 case ENUMERAL_TYPE:
268 case BOOLEAN_TYPE:
3c2a7a6a
RH
269 t = TYPE_MIN_VALUE (new);
270 if (t && TREE_CODE (t) != INTEGER_CST)
271 walk_tree (&TYPE_MIN_VALUE (new), copy_body_r, id, NULL);
1c9766da 272
3c2a7a6a
RH
273 t = TYPE_MAX_VALUE (new);
274 if (t && TREE_CODE (t) != INTEGER_CST)
275 walk_tree (&TYPE_MAX_VALUE (new), copy_body_r, id, NULL);
276 return new;
9f63daea 277
3c2a7a6a
RH
278 case FUNCTION_TYPE:
279 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
280 walk_tree (&TYPE_ARG_TYPES (new), copy_body_r, id, NULL);
281 return new;
282
283 case ARRAY_TYPE:
284 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
285 TYPE_DOMAIN (new) = remap_type (TYPE_DOMAIN (new), id);
286 break;
287
288 case RECORD_TYPE:
289 case UNION_TYPE:
290 case QUAL_UNION_TYPE:
52dd234b
RH
291 {
292 tree f, nf = NULL;
293
294 for (f = TYPE_FIELDS (new); f ; f = TREE_CHAIN (f))
295 {
296 t = remap_decl (f, id);
297 DECL_CONTEXT (t) = new;
298 TREE_CHAIN (t) = nf;
299 nf = t;
300 }
301 TYPE_FIELDS (new) = nreverse (nf);
302 }
3c2a7a6a
RH
303 break;
304
3c2a7a6a
RH
305 case OFFSET_TYPE:
306 default:
307 /* Shouldn't have been thought variable sized. */
1e128c5f 308 gcc_unreachable ();
3c2a7a6a
RH
309 }
310
311 walk_tree (&TYPE_SIZE (new), copy_body_r, id, NULL);
312 walk_tree (&TYPE_SIZE_UNIT (new), copy_body_r, id, NULL);
313
314 return new;
315}
316
1b369fae
RH
317tree
318remap_type (tree type, copy_body_data *id)
52dd234b
RH
319{
320 splay_tree_node node;
321
322 if (type == NULL)
323 return type;
324
325 /* See if we have remapped this type. */
326 node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
327 if (node)
328 return (tree) node->value;
329
330 /* The type only needs remapping if it's variably modified. */
1b369fae 331 if (! variably_modified_type_p (type, id->src_fn))
52dd234b
RH
332 {
333 insert_decl_map (id, type, type);
334 return type;
335 }
336
337 return remap_type_1 (type, id);
338}
339
6de9cd9a 340static tree
1b369fae 341remap_decls (tree decls, copy_body_data *id)
d4e4baa9 342{
6de9cd9a
DN
343 tree old_var;
344 tree new_decls = NULL_TREE;
d4e4baa9 345
6de9cd9a
DN
346 /* Remap its variables. */
347 for (old_var = decls; old_var; old_var = TREE_CHAIN (old_var))
d4e4baa9 348 {
6de9cd9a
DN
349 tree new_var;
350
30be951a
JH
351 /* We can not chain the local static declarations into the unexpanded_var_list
352 as we can't duplicate them or break one decl rule. Go ahead and link
353 them into unexpanded_var_list. */
1b369fae 354 if (!lang_hooks.tree_inlining.auto_var_in_fn_p (old_var, id->src_fn)
30be951a
JH
355 && !DECL_EXTERNAL (old_var))
356 {
357 cfun->unexpanded_var_list = tree_cons (NULL_TREE, old_var,
358 cfun->unexpanded_var_list);
359 continue;
360 }
361
6de9cd9a
DN
362 /* Remap the variable. */
363 new_var = remap_decl (old_var, id);
364
365 /* If we didn't remap this variable, so we can't mess with its
366 TREE_CHAIN. If we remapped this variable to the return slot, it's
367 already declared somewhere else, so don't declare it here. */
368 if (!new_var || new_var == id->retvar)
369 ;
d4e4baa9
AO
370 else
371 {
1e128c5f 372 gcc_assert (DECL_P (new_var));
6de9cd9a
DN
373 TREE_CHAIN (new_var) = new_decls;
374 new_decls = new_var;
d4e4baa9 375 }
d4e4baa9 376 }
d4e4baa9 377
6de9cd9a
DN
378 return nreverse (new_decls);
379}
380
381/* Copy the BLOCK to contain remapped versions of the variables
382 therein. And hook the new block into the block-tree. */
383
384static void
1b369fae 385remap_block (tree *block, copy_body_data *id)
6de9cd9a 386{
d436bff8
AH
387 tree old_block;
388 tree new_block;
d436bff8
AH
389 tree fn;
390
391 /* Make the new block. */
392 old_block = *block;
393 new_block = make_node (BLOCK);
394 TREE_USED (new_block) = TREE_USED (old_block);
395 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
3e2844cb 396 BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
d436bff8
AH
397 *block = new_block;
398
399 /* Remap its variables. */
6de9cd9a 400 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block), id);
d436bff8 401
1b369fae
RH
402 fn = id->dst_fn;
403
404 if (id->transform_lang_insert_block)
673fda6b 405 lang_hooks.decls.insert_block (new_block);
1b369fae 406
d436bff8 407 /* Remember the remapped block. */
6de9cd9a 408 insert_decl_map (id, old_block, new_block);
d4e4baa9
AO
409}
410
acb8f212
JH
411/* Copy the whole block tree and root it in id->block. */
412static tree
1b369fae 413remap_blocks (tree block, copy_body_data *id)
acb8f212
JH
414{
415 tree t;
416 tree new = block;
417
418 if (!block)
419 return NULL;
420
421 remap_block (&new, id);
422 gcc_assert (new != block);
423 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
424 add_lexical_block (new, remap_blocks (t, id));
425 return new;
426}
427
d4e4baa9 428static void
6de9cd9a 429copy_statement_list (tree *tp)
d4e4baa9 430{
6de9cd9a
DN
431 tree_stmt_iterator oi, ni;
432 tree new;
433
434 new = alloc_stmt_list ();
435 ni = tsi_start (new);
436 oi = tsi_start (*tp);
437 *tp = new;
438
439 for (; !tsi_end_p (oi); tsi_next (&oi))
440 tsi_link_after (&ni, tsi_stmt (oi), TSI_NEW_STMT);
441}
d4e4baa9 442
6de9cd9a 443static void
1b369fae 444copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
6de9cd9a
DN
445{
446 tree block = BIND_EXPR_BLOCK (*tp);
d4e4baa9
AO
447 /* Copy (and replace) the statement. */
448 copy_tree_r (tp, walk_subtrees, NULL);
6de9cd9a
DN
449 if (block)
450 {
451 remap_block (&block, id);
452 BIND_EXPR_BLOCK (*tp) = block;
453 }
d4e4baa9 454
6de9cd9a
DN
455 if (BIND_EXPR_VARS (*tp))
456 /* This will remap a lot of the same decls again, but this should be
457 harmless. */
458 BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), id);
d4e4baa9
AO
459}
460
e21aff8a 461/* Called from copy_body_id via walk_tree. DATA is really an
1b369fae 462 `copy_body_data *'. */
aa4a53af 463
1b369fae 464tree
46c5ad27 465copy_body_r (tree *tp, int *walk_subtrees, void *data)
d4e4baa9 466{
1b369fae
RH
467 copy_body_data *id = (copy_body_data *) data;
468 tree fn = id->src_fn;
acb8f212 469 tree new_block;
d4e4baa9 470
e21aff8a
SB
471 /* Begin by recognizing trees that we'll completely rewrite for the
472 inlining context. Our output for these trees is completely
473 different from out input (e.g. RETURN_EXPR is deleted, and morphs
474 into an edge). Further down, we'll handle trees that get
475 duplicated and/or tweaked. */
d4e4baa9 476
1b369fae
RH
477 /* When requested, RETURN_EXPRs should be transformed to just the
478 contained MODIFY_EXPR. The branch semantics of the return will
479 be handled elsewhere by manipulating the CFG rather than a statement. */
480 if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
d4e4baa9 481 {
e21aff8a 482 tree assignment = TREE_OPERAND (*tp, 0);
d4e4baa9
AO
483
484 /* If we're returning something, just turn that into an
e21aff8a
SB
485 assignment into the equivalent of the original RESULT_DECL.
486 If the "assignment" is just the result decl, the result
487 decl has already been set (e.g. a recent "foo (&result_decl,
488 ...)"); just toss the entire RETURN_EXPR. */
489 if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
490 {
491 /* Replace the RETURN_EXPR with (a copy of) the
128a79fb 492 MODIFY_EXPR hanging underneath. */
e21aff8a
SB
493 *tp = copy_node (assignment);
494 }
495 else /* Else the RETURN_EXPR returns no value. */
496 {
497 *tp = NULL;
cceb1885 498 return (tree) (void *)1;
e21aff8a 499 }
d4e4baa9 500 }
e21aff8a 501
d4e4baa9
AO
502 /* Local variables and labels need to be replaced by equivalent
503 variables. We don't want to copy static variables; there's only
504 one of those, no matter how many times we inline the containing
5377d5ba 505 function. Similarly for globals from an outer function. */
ae2bcd98 506 else if (lang_hooks.tree_inlining.auto_var_in_fn_p (*tp, fn))
d4e4baa9
AO
507 {
508 tree new_decl;
509
510 /* Remap the declaration. */
511 new_decl = remap_decl (*tp, id);
1e128c5f 512 gcc_assert (new_decl);
d4e4baa9
AO
513 /* Replace this variable with the copy. */
514 STRIP_TYPE_NOPS (new_decl);
515 *tp = new_decl;
e4cf29ae 516 *walk_subtrees = 0;
d4e4baa9 517 }
6de9cd9a
DN
518 else if (TREE_CODE (*tp) == STATEMENT_LIST)
519 copy_statement_list (tp);
d4e4baa9 520 else if (TREE_CODE (*tp) == SAVE_EXPR)
82c82743 521 remap_save_expr (tp, id->decl_map, walk_subtrees);
17acc01a
JH
522 else if (TREE_CODE (*tp) == LABEL_DECL
523 && (! DECL_CONTEXT (*tp)
1b369fae 524 || decl_function_context (*tp) == id->src_fn))
e21aff8a 525 /* These may need to be remapped for EH handling. */
17acc01a 526 *tp = remap_decl (*tp, id);
6de9cd9a
DN
527 else if (TREE_CODE (*tp) == BIND_EXPR)
528 copy_bind_expr (tp, walk_subtrees, id);
3c2a7a6a
RH
529 /* Types may need remapping as well. */
530 else if (TYPE_P (*tp))
531 *tp = remap_type (*tp, id);
532
bb04998a
RK
533 /* If this is a constant, we have to copy the node iff the type will be
534 remapped. copy_tree_r will not copy a constant. */
3cf11075 535 else if (CONSTANT_CLASS_P (*tp))
bb04998a
RK
536 {
537 tree new_type = remap_type (TREE_TYPE (*tp), id);
538
539 if (new_type == TREE_TYPE (*tp))
540 *walk_subtrees = 0;
541
542 else if (TREE_CODE (*tp) == INTEGER_CST)
543 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
544 TREE_INT_CST_HIGH (*tp));
545 else
546 {
547 *tp = copy_node (*tp);
548 TREE_TYPE (*tp) = new_type;
549 }
550 }
551
d4e4baa9
AO
552 /* Otherwise, just copy the node. Note that copy_tree_r already
553 knows not to copy VAR_DECLs, etc., so this is safe. */
554 else
555 {
e21aff8a
SB
556 /* Here we handle trees that are not completely rewritten.
557 First we detect some inlining-induced bogosities for
558 discarding. */
68594ce7
JM
559 if (TREE_CODE (*tp) == MODIFY_EXPR
560 && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
ae2bcd98 561 && (lang_hooks.tree_inlining.auto_var_in_fn_p
68594ce7 562 (TREE_OPERAND (*tp, 0), fn)))
d4e4baa9
AO
563 {
564 /* Some assignments VAR = VAR; don't generate any rtl code
565 and thus don't count as variable modification. Avoid
566 keeping bogosities like 0 = 0. */
567 tree decl = TREE_OPERAND (*tp, 0), value;
568 splay_tree_node n;
569
570 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
571 if (n)
572 {
573 value = (tree) n->value;
574 STRIP_TYPE_NOPS (value);
575 if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value))
68594ce7 576 {
b03c0b93 577 *tp = build_empty_stmt ();
68594ce7
JM
578 return copy_body_r (tp, walk_subtrees, data);
579 }
d4e4baa9
AO
580 }
581 }
1b369fae 582 else if (TREE_CODE (*tp) == INDIRECT_REF)
6de9cd9a
DN
583 {
584 /* Get rid of *& from inline substitutions that can happen when a
585 pointer argument is an ADDR_EXPR. */
81cfbbc2 586 tree decl = TREE_OPERAND (*tp, 0);
6de9cd9a
DN
587 splay_tree_node n;
588
589 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
590 if (n)
591 {
5e13fdf7 592 tree new;
30d2e943
RG
593 /* If we happen to get an ADDR_EXPR in n->value, strip
594 it manually here as we'll eventually get ADDR_EXPRs
595 which lie about their types pointed to. In this case
596 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
095ecc24
RG
597 but we absolutely rely on that. As fold_indirect_ref
598 does other useful transformations, try that first, though. */
599 tree type = TREE_TYPE (TREE_TYPE ((tree)n->value));
5e13fdf7
JH
600 new = unshare_expr ((tree)n->value);
601 *tp = fold_indirect_ref_1 (type, new);
095ecc24
RG
602 if (! *tp)
603 {
5e13fdf7
JH
604 if (TREE_CODE (new) == ADDR_EXPR)
605 *tp = TREE_OPERAND (new, 0);
095ecc24 606 else
5e13fdf7 607 *tp = build1 (INDIRECT_REF, type, new);
095ecc24 608 }
81cfbbc2
JH
609 *walk_subtrees = 0;
610 return NULL;
68594ce7
JM
611 }
612 }
613
e21aff8a
SB
614 /* Here is the "usual case". Copy this tree node, and then
615 tweak some special cases. */
1b369fae 616 copy_tree_r (tp, walk_subtrees, NULL);
19734dd8 617
acb8f212
JH
618 /* If EXPR has block defined, map it to newly constructed block.
619 When inlining we want EXPRs without block appear in the block
620 of function call. */
621 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (*tp))))
622 {
623 new_block = id->block;
624 if (TREE_BLOCK (*tp))
625 {
626 splay_tree_node n;
627 n = splay_tree_lookup (id->decl_map,
628 (splay_tree_key) TREE_BLOCK (*tp));
629 gcc_assert (n);
630 new_block = (tree) n->value;
631 }
632 TREE_BLOCK (*tp) = new_block;
633 }
68594ce7 634
e0704a46 635 if (TREE_CODE (*tp) == RESX_EXPR && id->eh_region_offset)
e21aff8a
SB
636 TREE_OPERAND (*tp, 0) =
637 build_int_cst
638 (NULL_TREE,
639 id->eh_region_offset + TREE_INT_CST_LOW (TREE_OPERAND (*tp, 0)));
18c6ada9 640
3c2a7a6a
RH
641 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
642
68594ce7
JM
643 /* The copied TARGET_EXPR has never been expanded, even if the
644 original node was expanded already. */
645 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
646 {
647 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
648 TREE_OPERAND (*tp, 3) = NULL_TREE;
649 }
84cce55d
RH
650
651 /* Variable substitution need not be simple. In particular, the
652 INDIRECT_REF substitution above. Make sure that TREE_CONSTANT
653 and friends are up-to-date. */
654 else if (TREE_CODE (*tp) == ADDR_EXPR)
655 {
656 walk_tree (&TREE_OPERAND (*tp, 0), copy_body_r, id, NULL);
127203ac 657 recompute_tree_invariant_for_addr_expr (*tp);
84cce55d
RH
658 *walk_subtrees = 0;
659 }
d4e4baa9
AO
660 }
661
662 /* Keep iterating. */
663 return NULL_TREE;
664}
665
e21aff8a
SB
666/* Copy basic block, scale profile accordingly. Edges will be taken care of
667 later */
668
669static basic_block
1b369fae 670copy_bb (copy_body_data *id, basic_block bb, int frequency_scale, int count_scale)
e21aff8a
SB
671{
672 block_stmt_iterator bsi, copy_bsi;
673 basic_block copy_basic_block;
674
675 /* create_basic_block() will append every new block to
676 basic_block_info automatically. */
cceb1885
GDR
677 copy_basic_block = create_basic_block (NULL, (void *) 0,
678 (basic_block) bb->prev_bb->aux);
e21aff8a
SB
679 copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
680 copy_basic_block->frequency = (bb->frequency
681 * frequency_scale / REG_BR_PROB_BASE);
682 copy_bsi = bsi_start (copy_basic_block);
683
684 for (bsi = bsi_start (bb);
685 !bsi_end_p (bsi); bsi_next (&bsi))
686 {
687 tree stmt = bsi_stmt (bsi);
688 tree orig_stmt = stmt;
689
690 walk_tree (&stmt, copy_body_r, id, NULL);
691
692 /* RETURN_EXPR might be removed,
693 this is signalled by making stmt pointer NULL. */
694 if (stmt)
695 {
e0704a46 696 tree call, decl;
e21aff8a 697 bsi_insert_after (&copy_bsi, stmt, BSI_NEW_STMT);
e0704a46
JH
698 call = get_call_expr_in (stmt);
699 /* We're duplicating a CALL_EXPR. Find any corresponding
700 callgraph edges and update or duplicate them. */
701 if (call && (decl = get_callee_fndecl (call)))
702 {
1b369fae
RH
703 struct cgraph_node *node;
704 struct cgraph_edge *edge;
705
706 switch (id->transform_call_graph_edges)
e0704a46 707 {
1b369fae
RH
708 case CB_CGE_DUPLICATE:
709 edge = cgraph_edge (id->src_node, orig_stmt);
ea99e0be 710 if (edge)
1b369fae 711 cgraph_clone_edge (edge, id->dst_node, stmt,
ea99e0be 712 REG_BR_PROB_BASE, 1, true);
1b369fae
RH
713 break;
714
715 case CB_CGE_MOVE_CLONES:
716 for (node = id->dst_node->next_clone;
717 node;
718 node = node->next_clone)
ea99e0be 719 {
1b369fae
RH
720 edge = cgraph_edge (node, orig_stmt);
721 gcc_assert (edge);
ea99e0be 722 edge->call_stmt = stmt;
1b369fae
RH
723 }
724 /* FALLTHRU */
725
726 case CB_CGE_MOVE:
727 edge = cgraph_edge (id->dst_node, orig_stmt);
728 if (edge)
729 edge->call_stmt = stmt;
730 break;
731
732 default:
733 gcc_unreachable ();
e0704a46
JH
734 }
735 }
e21aff8a
SB
736 /* If you think we can abort here, you are wrong.
737 There is no region 0 in tree land. */
1b369fae 738 gcc_assert (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt)
e21aff8a
SB
739 != 0);
740
741 if (tree_could_throw_p (stmt))
742 {
1b369fae 743 int region = lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt);
e21aff8a 744 /* Add an entry for the copied tree in the EH hashtable.
1b369fae 745 When cloning or versioning, use the hashtable in
e21aff8a
SB
746 cfun, and just copy the EH number. When inlining, use the
747 hashtable in the caller, and adjust the region number. */
748 if (region > 0)
749 add_stmt_to_eh_region (stmt, region + id->eh_region_offset);
750
751 /* If this tree doesn't have a region associated with it,
752 and there is a "current region,"
753 then associate this tree with the current region
754 and add edges associated with this region. */
1b369fae 755 if ((lookup_stmt_eh_region_fn (id->src_cfun,
e21aff8a
SB
756 orig_stmt) <= 0
757 && id->eh_region > 0)
758 && tree_could_throw_p (stmt))
759 add_stmt_to_eh_region (stmt, id->eh_region);
760 }
761 }
762 }
763 return copy_basic_block;
764}
765
128a79fb
KH
766/* Copy edges from BB into its copy constructed earlier, scale profile
767 accordingly. Edges will be taken care of later. Assume aux
768 pointers to point to the copies of each BB. */
e21aff8a
SB
769static void
770copy_edges_for_bb (basic_block bb, int count_scale)
771{
cceb1885 772 basic_block new_bb = (basic_block) bb->aux;
e21aff8a
SB
773 edge_iterator ei;
774 edge old_edge;
775 block_stmt_iterator bsi;
776 int flags;
777
778 /* Use the indices from the original blocks to create edges for the
779 new ones. */
780 FOR_EACH_EDGE (old_edge, ei, bb->succs)
e0704a46
JH
781 if (!(old_edge->flags & EDGE_EH))
782 {
783 edge new;
e21aff8a 784
e0704a46 785 flags = old_edge->flags;
e21aff8a 786
e0704a46
JH
787 /* Return edges do get a FALLTHRU flag when the get inlined. */
788 if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
789 && old_edge->dest->aux != EXIT_BLOCK_PTR)
790 flags |= EDGE_FALLTHRU;
cceb1885 791 new = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
e0704a46
JH
792 new->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
793 new->probability = old_edge->probability;
794 }
e21aff8a
SB
795
796 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
797 return;
798
e21aff8a
SB
799 for (bsi = bsi_start (new_bb); !bsi_end_p (bsi);)
800 {
801 tree copy_stmt;
802
803 copy_stmt = bsi_stmt (bsi);
804 update_stmt (copy_stmt);
805 /* Do this before the possible split_block. */
806 bsi_next (&bsi);
807
808 /* If this tree could throw an exception, there are two
809 cases where we need to add abnormal edge(s): the
810 tree wasn't in a region and there is a "current
811 region" in the caller; or the original tree had
812 EH edges. In both cases split the block after the tree,
813 and add abnormal edge(s) as needed; we need both
814 those from the callee and the caller.
815 We check whether the copy can throw, because the const
816 propagation can change an INDIRECT_REF which throws
817 into a COMPONENT_REF which doesn't. If the copy
818 can throw, the original could also throw. */
819
e0704a46 820 if (tree_can_throw_internal (copy_stmt))
e21aff8a
SB
821 {
822 if (!bsi_end_p (bsi))
823 /* Note that bb's predecessor edges aren't necessarily
824 right at this point; split_block doesn't care. */
825 {
826 edge e = split_block (new_bb, copy_stmt);
827 new_bb = e->dest;
828 bsi = bsi_start (new_bb);
829 }
830
831 make_eh_edges (copy_stmt);
832 }
833 }
834}
835
836/* Wrapper for remap_decl so it can be used as a callback. */
837static tree
838remap_decl_1 (tree decl, void *data)
839{
1b369fae 840 return remap_decl (decl, (copy_body_data *) data);
e21aff8a
SB
841}
842
843/* Make a copy of the body of FN so that it can be inserted inline in
844 another function. Walks FN via CFG, returns new fndecl. */
845
846static tree
1b369fae 847copy_cfg_body (copy_body_data * id, gcov_type count, int frequency,
e21aff8a
SB
848 basic_block entry_block_map, basic_block exit_block_map)
849{
1b369fae 850 tree callee_fndecl = id->src_fn;
e21aff8a 851 /* Original cfun for the callee, doesn't change. */
1b369fae 852 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
e21aff8a
SB
853 /* Copy, built by this function. */
854 struct function *new_cfun;
855 /* Place to copy from; when a copy of the function was saved off earlier,
856 use that instead of the main copy. */
857 struct function *cfun_to_copy =
858 (struct function *) ggc_alloc_cleared (sizeof (struct function));
859 basic_block bb;
860 tree new_fndecl = NULL;
e21aff8a
SB
861 int count_scale, frequency_scale;
862
1b369fae 863 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
e21aff8a 864 count_scale = (REG_BR_PROB_BASE * count
1b369fae 865 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
e21aff8a
SB
866 else
867 count_scale = 1;
868
1b369fae 869 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
e21aff8a
SB
870 frequency_scale = (REG_BR_PROB_BASE * frequency
871 /
1b369fae 872 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
e21aff8a
SB
873 else
874 frequency_scale = count_scale;
875
876 /* Register specific tree functions. */
877 tree_register_cfg_hooks ();
878
879 /* Must have a CFG here at this point. */
880 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
881 (DECL_STRUCT_FUNCTION (callee_fndecl)));
882
883 *cfun_to_copy = *DECL_STRUCT_FUNCTION (callee_fndecl);
884
1b369fae 885 id->src_cfun = cfun_to_copy;
e21aff8a 886
1b369fae
RH
887 /* If requested, create new basic_block_info and label_to_block_maps.
888 Otherwise, insert our new blocks and labels into the existing cfg. */
889 if (id->transform_new_cfg)
e21aff8a
SB
890 {
891 new_cfun =
892 (struct function *) ggc_alloc_cleared (sizeof (struct function));
893 *new_cfun = *DECL_STRUCT_FUNCTION (callee_fndecl);
894 new_cfun->cfg = NULL;
895 new_cfun->decl = new_fndecl = copy_node (callee_fndecl);
896 new_cfun->ib_boundaries_block = (varray_type) 0;
897 DECL_STRUCT_FUNCTION (new_fndecl) = new_cfun;
898 push_cfun (new_cfun);
899 init_empty_tree_cfg ();
900
901 ENTRY_BLOCK_PTR->count =
1b369fae 902 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
e21aff8a
SB
903 REG_BR_PROB_BASE);
904 ENTRY_BLOCK_PTR->frequency =
1b369fae 905 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
e21aff8a
SB
906 frequency_scale / REG_BR_PROB_BASE);
907 EXIT_BLOCK_PTR->count =
1b369fae 908 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
e21aff8a
SB
909 REG_BR_PROB_BASE);
910 EXIT_BLOCK_PTR->frequency =
1b369fae 911 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
e21aff8a
SB
912 frequency_scale / REG_BR_PROB_BASE);
913
914 entry_block_map = ENTRY_BLOCK_PTR;
915 exit_block_map = EXIT_BLOCK_PTR;
916 }
917
918 ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
919 EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
920
e21aff8a
SB
921 /* Duplicate any exception-handling regions. */
922 if (cfun->eh)
923 {
1b369fae 924 if (id->transform_new_cfg)
e21aff8a 925 init_eh_for_function ();
1b369fae 926 id->eh_region_offset
fad41cd7
RH
927 = duplicate_eh_regions (cfun_to_copy, remap_decl_1, id,
928 0, id->eh_region);
e21aff8a
SB
929 }
930 /* Use aux pointers to map the original blocks to copy. */
931 FOR_EACH_BB_FN (bb, cfun_to_copy)
932 bb->aux = copy_bb (id, bb, frequency_scale, count_scale);
933 /* Now that we've duplicated the blocks, duplicate their edges. */
934 FOR_ALL_BB_FN (bb, cfun_to_copy)
935 copy_edges_for_bb (bb, count_scale);
936 FOR_ALL_BB_FN (bb, cfun_to_copy)
937 bb->aux = NULL;
938
1b369fae 939 if (id->transform_new_cfg)
e21aff8a
SB
940 pop_cfun ();
941
942 return new_fndecl;
943}
944
d4e4baa9
AO
945/* Make a copy of the body of FN so that it can be inserted inline in
946 another function. */
947
948static tree
1b369fae 949copy_generic_body (copy_body_data *id)
d4e4baa9
AO
950{
951 tree body;
1b369fae 952 tree fndecl = id->src_fn;
d4e4baa9 953
e21aff8a 954 body = DECL_SAVED_TREE (fndecl);
d4e4baa9
AO
955 walk_tree (&body, copy_body_r, id, NULL);
956
957 return body;
958}
959
e21aff8a 960static tree
1b369fae 961copy_body (copy_body_data *id, gcov_type count, int frequency,
e21aff8a
SB
962 basic_block entry_block_map, basic_block exit_block_map)
963{
1b369fae 964 tree fndecl = id->src_fn;
e21aff8a
SB
965 tree body;
966
967 /* If this body has a CFG, walk CFG and copy. */
968 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
969 body = copy_cfg_body (id, count, frequency, entry_block_map, exit_block_map);
970
971 return body;
972}
973
04482133
AO
974/* Return true if VALUE is an ADDR_EXPR of an automatic variable
975 defined in function FN, or of a data member thereof. */
976
977static bool
978self_inlining_addr_expr (tree value, tree fn)
979{
980 tree var;
981
982 if (TREE_CODE (value) != ADDR_EXPR)
983 return false;
984
985 var = get_base_address (TREE_OPERAND (value, 0));
e21aff8a 986
04482133
AO
987 return var && lang_hooks.tree_inlining.auto_var_in_fn_p (var, fn);
988}
989
6de9cd9a 990static void
1b369fae 991setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
e21aff8a 992 basic_block bb, tree *vars)
6de9cd9a
DN
993{
994 tree init_stmt;
995 tree var;
e21aff8a 996 tree var_sub;
6de9cd9a
DN
997
998 /* If the parameter is never assigned to, we may not need to
999 create a new variable here at all. Instead, we may be able
1000 to just use the argument value. */
1001 if (TREE_READONLY (p)
1002 && !TREE_ADDRESSABLE (p)
1003 && value && !TREE_SIDE_EFFECTS (value))
1004 {
84936f6f
RH
1005 /* We may produce non-gimple trees by adding NOPs or introduce
1006 invalid sharing when operand is not really constant.
1007 It is not big deal to prohibit constant propagation here as
1008 we will constant propagate in DOM1 pass anyway. */
1009 if (is_gimple_min_invariant (value)
04482133
AO
1010 && lang_hooks.types_compatible_p (TREE_TYPE (value), TREE_TYPE (p))
1011 /* We have to be very careful about ADDR_EXPR. Make sure
1012 the base variable isn't a local variable of the inlined
1013 function, e.g., when doing recursive inlining, direct or
1014 mutually-recursive or whatever, which is why we don't
1015 just test whether fn == current_function_decl. */
1016 && ! self_inlining_addr_expr (value, fn))
6de9cd9a 1017 {
6de9cd9a
DN
1018 insert_decl_map (id, p, value);
1019 return;
1020 }
1021 }
1022
5377d5ba
RK
1023 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
1024 here since the type of this decl must be visible to the calling
8c27b7d4 1025 function. */
1b369fae 1026 var = copy_decl_to_var (p, id);
e21aff8a
SB
1027
1028 /* See if the frontend wants to pass this by invisible reference. If
1029 so, our new VAR_DECL will have REFERENCE_TYPE, and we need to
1030 replace uses of the PARM_DECL with dereferences. */
1031 if (TREE_TYPE (var) != TREE_TYPE (p)
1032 && POINTER_TYPE_P (TREE_TYPE (var))
1033 && TREE_TYPE (TREE_TYPE (var)) == TREE_TYPE (p))
1034 {
1035 insert_decl_map (id, var, var);
1036 var_sub = build_fold_indirect_ref (var);
1037 }
1038 else
1039 var_sub = var;
6de9cd9a 1040
6de9cd9a
DN
1041 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
1042 that way, when the PARM_DECL is encountered, it will be
1043 automatically replaced by the VAR_DECL. */
e21aff8a 1044 insert_decl_map (id, p, var_sub);
6de9cd9a
DN
1045
1046 /* Declare this new variable. */
1047 TREE_CHAIN (var) = *vars;
1048 *vars = var;
1049
1050 /* Make gimplifier happy about this variable. */
84936f6f 1051 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
6de9cd9a
DN
1052
1053 /* Even if P was TREE_READONLY, the new VAR should not be.
1054 In the original code, we would have constructed a
1055 temporary, and then the function body would have never
1056 changed the value of P. However, now, we will be
1057 constructing VAR directly. The constructor body may
1058 change its value multiple times as it is being
1059 constructed. Therefore, it must not be TREE_READONLY;
1060 the back-end assumes that TREE_READONLY variable is
1061 assigned to only once. */
1062 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
1063 TREE_READONLY (var) = 0;
1064
1065 /* Initialize this VAR_DECL from the equivalent argument. Convert
1066 the argument to the proper type in case it was promoted. */
1067 if (value)
1068 {
e072ae27 1069 tree rhs = fold_convert (TREE_TYPE (var), value);
e21aff8a 1070 block_stmt_iterator bsi = bsi_last (bb);
6de9cd9a
DN
1071
1072 if (rhs == error_mark_node)
1073 return;
1074
1075 /* We want to use MODIFY_EXPR, not INIT_EXPR here so that we
1076 keep our trees in gimple form. */
b4257cfc 1077 init_stmt = build2 (MODIFY_EXPR, TREE_TYPE (var), var, rhs);
6de9cd9a
DN
1078
1079 /* If we did not create a gimple value and we did not create a gimple
1080 cast of a gimple value, then we will need to gimplify INIT_STMTS
1081 at the end. Note that is_gimple_cast only checks the outer
128a79fb 1082 tree code, not its operand. Thus the explicit check that its
6de9cd9a
DN
1083 operand is a gimple value. */
1084 if (!is_gimple_val (rhs)
1085 && (!is_gimple_cast (rhs)
1086 || !is_gimple_val (TREE_OPERAND (rhs, 0))))
e21aff8a 1087 gimplify_stmt (&init_stmt);
52f66176
RK
1088
1089 /* If VAR represents a zero-sized variable, it's possible that the
1090 assignment statment may result in no gimple statements. */
047f4b2c
AP
1091 if (init_stmt)
1092 bsi_insert_after (&bsi, init_stmt, BSI_NEW_STMT);
6de9cd9a
DN
1093 }
1094}
1095
d4e4baa9
AO
1096/* Generate code to initialize the parameters of the function at the
1097 top of the stack in ID from the ARGS (presented as a TREE_LIST). */
1098
e21aff8a 1099static void
1b369fae 1100initialize_inlined_parameters (copy_body_data *id, tree args, tree static_chain,
e21aff8a 1101 tree fn, basic_block bb)
d4e4baa9 1102{
d4e4baa9
AO
1103 tree parms;
1104 tree a;
1105 tree p;
d436bff8 1106 tree vars = NULL_TREE;
d5123bae 1107 int argnum = 0;
d4e4baa9
AO
1108
1109 /* Figure out what the parameters are. */
18c6ada9 1110 parms = DECL_ARGUMENTS (fn);
d4e4baa9 1111
d4e4baa9
AO
1112 /* Loop through the parameter declarations, replacing each with an
1113 equivalent VAR_DECL, appropriately initialized. */
4838c5ee
AO
1114 for (p = parms, a = args; p;
1115 a = a ? TREE_CHAIN (a) : a, p = TREE_CHAIN (p))
d4e4baa9 1116 {
d4e4baa9
AO
1117 tree value;
1118
d5123bae
MS
1119 ++argnum;
1120
d4e4baa9 1121 /* Find the initializer. */
ae2bcd98 1122 value = lang_hooks.tree_inlining.convert_parm_for_inlining
d5123bae 1123 (p, a ? TREE_VALUE (a) : NULL_TREE, fn, argnum);
4838c5ee 1124
e21aff8a 1125 setup_one_parameter (id, p, value, fn, bb, &vars);
6de9cd9a 1126 }
4838c5ee 1127
6de9cd9a
DN
1128 /* Initialize the static chain. */
1129 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
ea99e0be 1130 gcc_assert (fn != current_function_decl);
6de9cd9a
DN
1131 if (p)
1132 {
1133 /* No static chain? Seems like a bug in tree-nested.c. */
1e128c5f 1134 gcc_assert (static_chain);
4838c5ee 1135
e21aff8a 1136 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
4838c5ee
AO
1137 }
1138
e21aff8a 1139 declare_inline_vars (id->block, vars);
d4e4baa9
AO
1140}
1141
e21aff8a
SB
1142/* Declare a return variable to replace the RESULT_DECL for the
1143 function we are calling. An appropriate DECL_STMT is returned.
1144 The USE_STMT is filled to contain a use of the declaration to
1145 indicate the return value of the function.
1146
1147 RETURN_SLOT_ADDR, if non-null, was a fake parameter that
7740f00d
RH
1148 took the address of the result. MODIFY_DEST, if non-null, was the LHS of
1149 the MODIFY_EXPR to which this call is the RHS.
1150
1151 The return value is a (possibly null) value that is the result of the
1152 function as seen by the callee. *USE_P is a (possibly null) value that
1153 holds the result as seen by the caller. */
d4e4baa9 1154
d436bff8 1155static tree
1b369fae 1156declare_return_variable (copy_body_data *id, tree return_slot_addr,
7740f00d 1157 tree modify_dest, tree *use_p)
d4e4baa9 1158{
1b369fae
RH
1159 tree callee = id->src_fn;
1160 tree caller = id->dst_fn;
7740f00d
RH
1161 tree result = DECL_RESULT (callee);
1162 tree callee_type = TREE_TYPE (result);
1163 tree caller_type = TREE_TYPE (TREE_TYPE (callee));
1164 tree var, use;
d4e4baa9
AO
1165
1166 /* We don't need to do anything for functions that don't return
1167 anything. */
7740f00d 1168 if (!result || VOID_TYPE_P (callee_type))
d4e4baa9 1169 {
6de9cd9a 1170 *use_p = NULL_TREE;
d4e4baa9
AO
1171 return NULL_TREE;
1172 }
1173
cc77ae10 1174 /* If there was a return slot, then the return value is the
7740f00d
RH
1175 dereferenced address of that object. */
1176 if (return_slot_addr)
1177 {
1178 /* The front end shouldn't have used both return_slot_addr and
1179 a modify expression. */
1e128c5f 1180 gcc_assert (!modify_dest);
cc77ae10
JM
1181 if (DECL_BY_REFERENCE (result))
1182 var = return_slot_addr;
1183 else
1184 var = build_fold_indirect_ref (return_slot_addr);
22918034
AP
1185 if (TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
1186 && !DECL_COMPLEX_GIMPLE_REG_P (result)
1187 && DECL_P (var))
1188 DECL_COMPLEX_GIMPLE_REG_P (var) = 0;
7740f00d
RH
1189 use = NULL;
1190 goto done;
1191 }
1192
1193 /* All types requiring non-trivial constructors should have been handled. */
1e128c5f 1194 gcc_assert (!TREE_ADDRESSABLE (callee_type));
7740f00d
RH
1195
1196 /* Attempt to avoid creating a new temporary variable. */
1197 if (modify_dest)
1198 {
1199 bool use_it = false;
1200
1201 /* We can't use MODIFY_DEST if there's type promotion involved. */
1202 if (!lang_hooks.types_compatible_p (caller_type, callee_type))
1203 use_it = false;
1204
1205 /* ??? If we're assigning to a variable sized type, then we must
1206 reuse the destination variable, because we've no good way to
1207 create variable sized temporaries at this point. */
1208 else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
1209 use_it = true;
1210
1211 /* If the callee cannot possibly modify MODIFY_DEST, then we can
1212 reuse it as the result of the call directly. Don't do this if
1213 it would promote MODIFY_DEST to addressable. */
e2f9fe42
RH
1214 else if (TREE_ADDRESSABLE (result))
1215 use_it = false;
1216 else
1217 {
1218 tree base_m = get_base_address (modify_dest);
1219
1220 /* If the base isn't a decl, then it's a pointer, and we don't
1221 know where that's going to go. */
1222 if (!DECL_P (base_m))
1223 use_it = false;
1224 else if (is_global_var (base_m))
1225 use_it = false;
1d327c16
JM
1226 else if (TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
1227 && !DECL_COMPLEX_GIMPLE_REG_P (result)
1228 && DECL_COMPLEX_GIMPLE_REG_P (base_m))
1229 use_it = false;
e2f9fe42
RH
1230 else if (!TREE_ADDRESSABLE (base_m))
1231 use_it = true;
1232 }
7740f00d
RH
1233
1234 if (use_it)
1235 {
1236 var = modify_dest;
1237 use = NULL;
1238 goto done;
1239 }
1240 }
1241
1e128c5f 1242 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
7740f00d 1243
1b369fae 1244 var = copy_decl_to_var (result, id);
e21aff8a 1245
7740f00d
RH
1246 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1247 DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list
1248 = tree_cons (NULL_TREE, var,
1249 DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list);
1250
6de9cd9a 1251 /* Do not have the rest of GCC warn about this variable as it should
471854f8 1252 not be visible to the user. */
6de9cd9a 1253 TREE_NO_WARNING (var) = 1;
d4e4baa9 1254
7740f00d
RH
1255 /* Build the use expr. If the return type of the function was
1256 promoted, convert it back to the expected type. */
1257 use = var;
1258 if (!lang_hooks.types_compatible_p (TREE_TYPE (var), caller_type))
1259 use = fold_convert (caller_type, var);
1260
1261 done:
d4e4baa9
AO
1262 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
1263 way, when the RESULT_DECL is encountered, it will be
1264 automatically replaced by the VAR_DECL. */
5e20bdd7 1265 insert_decl_map (id, result, var);
d4e4baa9 1266
6de9cd9a
DN
1267 /* Remember this so we can ignore it in remap_decls. */
1268 id->retvar = var;
1269
7740f00d
RH
1270 *use_p = use;
1271 return var;
d4e4baa9
AO
1272}
1273
0e9e1e0a 1274/* Returns nonzero if a function can be inlined as a tree. */
4838c5ee 1275
b3c3af2f
SB
1276bool
1277tree_inlinable_function_p (tree fn)
4838c5ee 1278{
b3c3af2f 1279 return inlinable_function_p (fn);
4838c5ee
AO
1280}
1281
f08545a8 1282static const char *inline_forbidden_reason;
c986baf6 1283
c986baf6 1284static tree
f08545a8 1285inline_forbidden_p_1 (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED,
edeb3871 1286 void *fnp)
c986baf6 1287{
f08545a8 1288 tree node = *nodep;
edeb3871 1289 tree fn = (tree) fnp;
f08545a8 1290 tree t;
c986baf6 1291
f08545a8
JH
1292 switch (TREE_CODE (node))
1293 {
1294 case CALL_EXPR:
3197c4fd
AS
1295 /* Refuse to inline alloca call unless user explicitly forced so as
1296 this may change program's memory overhead drastically when the
1297 function using alloca is called in loop. In GCC present in
1298 SPEC2000 inlining into schedule_block cause it to require 2GB of
1299 RAM instead of 256MB. */
f08545a8
JH
1300 if (alloca_call_p (node)
1301 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1302 {
ddd2d57e 1303 inline_forbidden_reason
dee15844 1304 = G_("function %q+F can never be inlined because it uses "
ddd2d57e 1305 "alloca (override using the always_inline attribute)");
f08545a8
JH
1306 return node;
1307 }
1308 t = get_callee_fndecl (node);
1309 if (! t)
1310 break;
84f5e1b1 1311
f08545a8
JH
1312 /* We cannot inline functions that call setjmp. */
1313 if (setjmp_call_p (t))
1314 {
ddd2d57e 1315 inline_forbidden_reason
dee15844 1316 = G_("function %q+F can never be inlined because it uses setjmp");
f08545a8
JH
1317 return node;
1318 }
1319
6de9cd9a 1320 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
3197c4fd 1321 switch (DECL_FUNCTION_CODE (t))
f08545a8 1322 {
3197c4fd
AS
1323 /* We cannot inline functions that take a variable number of
1324 arguments. */
1325 case BUILT_IN_VA_START:
1326 case BUILT_IN_STDARG_START:
1327 case BUILT_IN_NEXT_ARG:
1328 case BUILT_IN_VA_END:
6de9cd9a 1329 inline_forbidden_reason
dee15844 1330 = G_("function %q+F can never be inlined because it "
6de9cd9a
DN
1331 "uses variable argument lists");
1332 return node;
1333
3197c4fd 1334 case BUILT_IN_LONGJMP:
6de9cd9a
DN
1335 /* We can't inline functions that call __builtin_longjmp at
1336 all. The non-local goto machinery really requires the
1337 destination be in a different function. If we allow the
1338 function calling __builtin_longjmp to be inlined into the
1339 function calling __builtin_setjmp, Things will Go Awry. */
1340 inline_forbidden_reason
dee15844 1341 = G_("function %q+F can never be inlined because "
6de9cd9a
DN
1342 "it uses setjmp-longjmp exception handling");
1343 return node;
1344
1345 case BUILT_IN_NONLOCAL_GOTO:
1346 /* Similarly. */
1347 inline_forbidden_reason
dee15844 1348 = G_("function %q+F can never be inlined because "
6de9cd9a
DN
1349 "it uses non-local goto");
1350 return node;
f08545a8 1351
4b284111
JJ
1352 case BUILT_IN_RETURN:
1353 case BUILT_IN_APPLY_ARGS:
1354 /* If a __builtin_apply_args caller would be inlined,
1355 it would be saving arguments of the function it has
1356 been inlined into. Similarly __builtin_return would
1357 return from the function the inline has been inlined into. */
1358 inline_forbidden_reason
dee15844 1359 = G_("function %q+F can never be inlined because "
4b284111
JJ
1360 "it uses __builtin_return or __builtin_apply_args");
1361 return node;
1362
3197c4fd
AS
1363 default:
1364 break;
1365 }
f08545a8
JH
1366 break;
1367
f08545a8
JH
1368 case GOTO_EXPR:
1369 t = TREE_OPERAND (node, 0);
1370
1371 /* We will not inline a function which uses computed goto. The
1372 addresses of its local labels, which may be tucked into
1373 global storage, are of course not constant across
1374 instantiations, which causes unexpected behavior. */
1375 if (TREE_CODE (t) != LABEL_DECL)
1376 {
ddd2d57e 1377 inline_forbidden_reason
dee15844 1378 = G_("function %q+F can never be inlined "
ddd2d57e 1379 "because it contains a computed goto");
f08545a8
JH
1380 return node;
1381 }
6de9cd9a 1382 break;
f08545a8 1383
6de9cd9a
DN
1384 case LABEL_EXPR:
1385 t = TREE_OPERAND (node, 0);
1386 if (DECL_NONLOCAL (t))
f08545a8 1387 {
6de9cd9a
DN
1388 /* We cannot inline a function that receives a non-local goto
1389 because we cannot remap the destination label used in the
1390 function that is performing the non-local goto. */
ddd2d57e 1391 inline_forbidden_reason
dee15844 1392 = G_("function %q+F can never be inlined "
6de9cd9a 1393 "because it receives a non-local goto");
ed397c43 1394 return node;
f08545a8 1395 }
f08545a8
JH
1396 break;
1397
1398 case RECORD_TYPE:
1399 case UNION_TYPE:
1400 /* We cannot inline a function of the form
1401
1402 void F (int i) { struct S { int ar[i]; } s; }
1403
1404 Attempting to do so produces a catch-22.
1405 If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
1406 UNION_TYPE nodes, then it goes into infinite recursion on a
1407 structure containing a pointer to its own type. If it doesn't,
1408 then the type node for S doesn't get adjusted properly when
0e61db61 1409 F is inlined.
27b892b4
RK
1410
1411 ??? This is likely no longer true, but it's too late in the 4.0
1412 cycle to try to find out. This should be checked for 4.1. */
f08545a8 1413 for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t))
5377d5ba 1414 if (variably_modified_type_p (TREE_TYPE (t), NULL))
f08545a8 1415 {
ddd2d57e 1416 inline_forbidden_reason
dee15844 1417 = G_("function %q+F can never be inlined "
ddd2d57e 1418 "because it uses variable sized variables");
f08545a8
JH
1419 return node;
1420 }
6de9cd9a 1421
f08545a8
JH
1422 default:
1423 break;
1424 }
1425
1426 return NULL_TREE;
84f5e1b1
RH
1427}
1428
f08545a8 1429/* Return subexpression representing possible alloca call, if any. */
84f5e1b1 1430static tree
f08545a8 1431inline_forbidden_p (tree fndecl)
84f5e1b1 1432{
070588f0 1433 location_t saved_loc = input_location;
e21aff8a
SB
1434 block_stmt_iterator bsi;
1435 basic_block bb;
1436 tree ret = NULL_TREE;
1437
1438 FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (fndecl))
1439 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1440 {
1441 ret = walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
1442 inline_forbidden_p_1, fndecl);
1443 if (ret)
1444 goto egress;
1445 }
ed397c43 1446
e21aff8a 1447egress:
070588f0 1448 input_location = saved_loc;
d1a74aa7 1449 return ret;
84f5e1b1
RH
1450}
1451
b3c3af2f
SB
1452/* Returns nonzero if FN is a function that does not have any
1453 fundamental inline blocking properties. */
d4e4baa9 1454
b3c3af2f
SB
1455static bool
1456inlinable_function_p (tree fn)
d4e4baa9 1457{
b3c3af2f 1458 bool inlinable = true;
d4e4baa9
AO
1459
1460 /* If we've already decided this function shouldn't be inlined,
1461 there's no need to check again. */
1462 if (DECL_UNINLINABLE (fn))
b3c3af2f 1463 return false;
d4e4baa9 1464
d58b7c2d
MM
1465 /* See if there is any language-specific reason it cannot be
1466 inlined. (It is important that this hook be called early because
b3c3af2f
SB
1467 in C++ it may result in template instantiation.)
1468 If the function is not inlinable for language-specific reasons,
1469 it is left up to the langhook to explain why. */
ae2bcd98 1470 inlinable = !lang_hooks.tree_inlining.cannot_inline_tree_fn (&fn);
46c5ad27 1471
b3c3af2f
SB
1472 /* If we don't have the function body available, we can't inline it.
1473 However, this should not be recorded since we also get here for
1474 forward declared inline functions. Therefore, return at once. */
1475 if (!DECL_SAVED_TREE (fn))
1476 return false;
1477
1478 /* If we're not inlining at all, then we cannot inline this function. */
1479 else if (!flag_inline_trees)
1480 inlinable = false;
1481
1482 /* Only try to inline functions if DECL_INLINE is set. This should be
1483 true for all functions declared `inline', and for all other functions
1484 as well with -finline-functions.
1485
1486 Don't think of disregarding DECL_INLINE when flag_inline_trees == 2;
1487 it's the front-end that must set DECL_INLINE in this case, because
1488 dwarf2out loses if a function that does not have DECL_INLINE set is
1489 inlined anyway. That is why we have both DECL_INLINE and
1490 DECL_DECLARED_INLINE_P. */
1491 /* FIXME: When flag_inline_trees dies, the check for flag_unit_at_a_time
1492 here should be redundant. */
1493 else if (!DECL_INLINE (fn) && !flag_unit_at_a_time)
1494 inlinable = false;
a0c8285b 1495
f08545a8 1496 else if (inline_forbidden_p (fn))
b3c3af2f
SB
1497 {
1498 /* See if we should warn about uninlinable functions. Previously,
1499 some of these warnings would be issued while trying to expand
1500 the function inline, but that would cause multiple warnings
1501 about functions that would for example call alloca. But since
1502 this a property of the function, just one warning is enough.
1503 As a bonus we can now give more details about the reason why a
1504 function is not inlinable.
1505 We only warn for functions declared `inline' by the user. */
1506 bool do_warning = (warn_inline
1507 && DECL_INLINE (fn)
1508 && DECL_DECLARED_INLINE_P (fn)
1509 && !DECL_IN_SYSTEM_HEADER (fn));
1510
aa4a53af 1511 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
dee15844 1512 sorry (inline_forbidden_reason, fn);
2d327012 1513 else if (do_warning)
d2fcbf6f 1514 warning (OPT_Winline, inline_forbidden_reason, fn);
b3c3af2f
SB
1515
1516 inlinable = false;
1517 }
d4e4baa9
AO
1518
1519 /* Squirrel away the result so that we don't have to check again. */
b3c3af2f 1520 DECL_UNINLINABLE (fn) = !inlinable;
d4e4baa9 1521
b3c3af2f
SB
1522 return inlinable;
1523}
1524
e5c4f28a
RG
1525/* Estimate the cost of a memory move. Use machine dependent
1526 word size and take possible memcpy call into account. */
1527
1528int
1529estimate_move_cost (tree type)
1530{
1531 HOST_WIDE_INT size;
1532
1533 size = int_size_in_bytes (type);
1534
1535 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO)
1536 /* Cost of a memcpy call, 3 arguments and the call. */
1537 return 4;
1538 else
1539 return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
1540}
1541
6de9cd9a
DN
1542/* Used by estimate_num_insns. Estimate number of instructions seen
1543 by given statement. */
aa4a53af 1544
6de9cd9a
DN
1545static tree
1546estimate_num_insns_1 (tree *tp, int *walk_subtrees, void *data)
1547{
cceb1885 1548 int *count = (int *) data;
6de9cd9a
DN
1549 tree x = *tp;
1550
6615c446 1551 if (IS_TYPE_OR_DECL_P (x))
6de9cd9a
DN
1552 {
1553 *walk_subtrees = 0;
1554 return NULL;
1555 }
1556 /* Assume that constants and references counts nothing. These should
1557 be majorized by amount of operations among them we count later
1558 and are common target of CSE and similar optimizations. */
6615c446 1559 else if (CONSTANT_CLASS_P (x) || REFERENCE_CLASS_P (x))
6de9cd9a 1560 return NULL;
ed397c43 1561
6de9cd9a 1562 switch (TREE_CODE (x))
9f63daea 1563 {
6de9cd9a
DN
1564 /* Containers have no cost. */
1565 case TREE_LIST:
1566 case TREE_VEC:
1567 case BLOCK:
1568 case COMPONENT_REF:
1569 case BIT_FIELD_REF:
1570 case INDIRECT_REF:
16630a2c
DN
1571 case ALIGN_INDIRECT_REF:
1572 case MISALIGNED_INDIRECT_REF:
6de9cd9a
DN
1573 case ARRAY_REF:
1574 case ARRAY_RANGE_REF:
0f59171d 1575 case OBJ_TYPE_REF:
6de9cd9a
DN
1576 case EXC_PTR_EXPR: /* ??? */
1577 case FILTER_EXPR: /* ??? */
1578 case COMPOUND_EXPR:
1579 case BIND_EXPR:
6de9cd9a
DN
1580 case WITH_CLEANUP_EXPR:
1581 case NOP_EXPR:
1582 case VIEW_CONVERT_EXPR:
1583 case SAVE_EXPR:
6de9cd9a 1584 case ADDR_EXPR:
6de9cd9a 1585 case COMPLEX_EXPR:
61fcaeec 1586 case RANGE_EXPR:
6de9cd9a
DN
1587 case CASE_LABEL_EXPR:
1588 case SSA_NAME:
1589 case CATCH_EXPR:
1590 case EH_FILTER_EXPR:
1591 case STATEMENT_LIST:
1592 case ERROR_MARK:
1593 case NON_LVALUE_EXPR:
6de9cd9a
DN
1594 case FDESC_EXPR:
1595 case VA_ARG_EXPR:
1596 case TRY_CATCH_EXPR:
1597 case TRY_FINALLY_EXPR:
1598 case LABEL_EXPR:
1599 case GOTO_EXPR:
1600 case RETURN_EXPR:
1601 case EXIT_EXPR:
1602 case LOOP_EXPR:
6de9cd9a 1603 case PHI_NODE:
d25cee4d 1604 case WITH_SIZE_EXPR:
aaf46ef9 1605 case OMP_CLAUSE:
50674e96 1606 case OMP_RETURN_EXPR:
6de9cd9a 1607 break;
aa4a53af 1608
6de9cd9a
DN
1609 /* We don't account constants for now. Assume that the cost is amortized
1610 by operations that do use them. We may re-consider this decision once
128a79fb 1611 we are able to optimize the tree before estimating its size and break
6de9cd9a
DN
1612 out static initializers. */
1613 case IDENTIFIER_NODE:
1614 case INTEGER_CST:
1615 case REAL_CST:
1616 case COMPLEX_CST:
1617 case VECTOR_CST:
1618 case STRING_CST:
1619 *walk_subtrees = 0;
1620 return NULL;
3a5b9284 1621
e5c4f28a
RG
1622 /* Try to estimate the cost of assignments. We have three cases to
1623 deal with:
1624 1) Simple assignments to registers;
1625 2) Stores to things that must live in memory. This includes
1626 "normal" stores to scalars, but also assignments of large
1627 structures, or constructors of big arrays;
1628 3) TARGET_EXPRs.
1629
1630 Let us look at the first two cases, assuming we have "a = b + C":
1631 <modify_expr <var_decl "a"> <plus_expr <var_decl "b"> <constant C>>
1632 If "a" is a GIMPLE register, the assignment to it is free on almost
1633 any target, because "a" usually ends up in a real register. Hence
1634 the only cost of this expression comes from the PLUS_EXPR, and we
1635 can ignore the MODIFY_EXPR.
1636 If "a" is not a GIMPLE register, the assignment to "a" will most
1637 likely be a real store, so the cost of the MODIFY_EXPR is the cost
1638 of moving something into "a", which we compute using the function
1639 estimate_move_cost.
1640
1641 The third case deals with TARGET_EXPRs, for which the semantics are
1642 that a temporary is assigned, unless the TARGET_EXPR itself is being
1643 assigned to something else. In the latter case we do not need the
1644 temporary. E.g. in <modify_expr <var_decl "a"> <target_expr>>, the
1645 MODIFY_EXPR is free. */
6de9cd9a 1646 case INIT_EXPR:
6de9cd9a 1647 case MODIFY_EXPR:
e5c4f28a
RG
1648 /* Is the right and side a TARGET_EXPR? */
1649 if (TREE_CODE (TREE_OPERAND (x, 1)) == TARGET_EXPR)
1650 break;
1651 /* ... fall through ... */
1652
3a5b9284 1653 case TARGET_EXPR:
e5c4f28a
RG
1654 x = TREE_OPERAND (x, 0);
1655 /* Is this an assignments to a register? */
1656 if (is_gimple_reg (x))
1657 break;
1658 /* Otherwise it's a store, so fall through to compute the move cost. */
e21aff8a 1659
6de9cd9a 1660 case CONSTRUCTOR:
e5c4f28a 1661 *count += estimate_move_cost (TREE_TYPE (x));
6de9cd9a
DN
1662 break;
1663
e5c4f28a
RG
1664 /* Assign cost of 1 to usual operations.
1665 ??? We may consider mapping RTL costs to this. */
6de9cd9a 1666 case COND_EXPR:
4151978d 1667 case VEC_COND_EXPR:
6de9cd9a
DN
1668
1669 case PLUS_EXPR:
1670 case MINUS_EXPR:
1671 case MULT_EXPR:
1672
1673 case FIX_TRUNC_EXPR:
1674 case FIX_CEIL_EXPR:
1675 case FIX_FLOOR_EXPR:
1676 case FIX_ROUND_EXPR:
1677
1678 case NEGATE_EXPR:
1679 case FLOAT_EXPR:
1680 case MIN_EXPR:
1681 case MAX_EXPR:
1682 case ABS_EXPR:
1683
1684 case LSHIFT_EXPR:
1685 case RSHIFT_EXPR:
1686 case LROTATE_EXPR:
1687 case RROTATE_EXPR:
a6b46ba2
DN
1688 case VEC_LSHIFT_EXPR:
1689 case VEC_RSHIFT_EXPR:
6de9cd9a
DN
1690
1691 case BIT_IOR_EXPR:
1692 case BIT_XOR_EXPR:
1693 case BIT_AND_EXPR:
1694 case BIT_NOT_EXPR:
1695
1696 case TRUTH_ANDIF_EXPR:
1697 case TRUTH_ORIF_EXPR:
1698 case TRUTH_AND_EXPR:
1699 case TRUTH_OR_EXPR:
1700 case TRUTH_XOR_EXPR:
1701 case TRUTH_NOT_EXPR:
1702
1703 case LT_EXPR:
1704 case LE_EXPR:
1705 case GT_EXPR:
1706 case GE_EXPR:
1707 case EQ_EXPR:
1708 case NE_EXPR:
1709 case ORDERED_EXPR:
1710 case UNORDERED_EXPR:
1711
1712 case UNLT_EXPR:
1713 case UNLE_EXPR:
1714 case UNGT_EXPR:
1715 case UNGE_EXPR:
1716 case UNEQ_EXPR:
d1a7edaf 1717 case LTGT_EXPR:
6de9cd9a
DN
1718
1719 case CONVERT_EXPR:
1720
1721 case CONJ_EXPR:
1722
1723 case PREDECREMENT_EXPR:
1724 case PREINCREMENT_EXPR:
1725 case POSTDECREMENT_EXPR:
1726 case POSTINCREMENT_EXPR:
1727
1728 case SWITCH_EXPR:
1729
1730 case ASM_EXPR:
1731
16630a2c
DN
1732 case REALIGN_LOAD_EXPR:
1733
61d3cdbb
DN
1734 case REDUC_MAX_EXPR:
1735 case REDUC_MIN_EXPR:
1736 case REDUC_PLUS_EXPR:
20f06221
DN
1737 case WIDEN_SUM_EXPR:
1738 case DOT_PROD_EXPR:
1739
1740 case WIDEN_MULT_EXPR:
61d3cdbb 1741
6de9cd9a 1742 case RESX_EXPR:
e36f6190 1743 *count += 1;
6de9cd9a
DN
1744 break;
1745
1ea7e6ad 1746 /* Few special cases of expensive operations. This is useful
6de9cd9a
DN
1747 to avoid inlining on functions having too many of these. */
1748 case TRUNC_DIV_EXPR:
1749 case CEIL_DIV_EXPR:
1750 case FLOOR_DIV_EXPR:
1751 case ROUND_DIV_EXPR:
1752 case EXACT_DIV_EXPR:
1753 case TRUNC_MOD_EXPR:
1754 case CEIL_MOD_EXPR:
1755 case FLOOR_MOD_EXPR:
1756 case ROUND_MOD_EXPR:
1757 case RDIV_EXPR:
1758 *count += 10;
1759 break;
1760 case CALL_EXPR:
1761 {
1762 tree decl = get_callee_fndecl (x);
e5c4f28a 1763 tree arg;
6de9cd9a 1764
8c96cd51 1765 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6de9cd9a
DN
1766 switch (DECL_FUNCTION_CODE (decl))
1767 {
1768 case BUILT_IN_CONSTANT_P:
1769 *walk_subtrees = 0;
1770 return NULL_TREE;
1771 case BUILT_IN_EXPECT:
1772 return NULL_TREE;
1773 default:
1774 break;
1775 }
e5c4f28a 1776
c7f599d0
JH
1777 /* Our cost must be kept in sync with cgraph_estimate_size_after_inlining
1778 that does use function declaration to figure out the arguments. */
1779 if (!decl)
1780 {
1781 for (arg = TREE_OPERAND (x, 1); arg; arg = TREE_CHAIN (arg))
1782 *count += estimate_move_cost (TREE_TYPE (TREE_VALUE (arg)));
1783 }
1784 else
1785 {
1786 for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
1787 *count += estimate_move_cost (TREE_TYPE (arg));
1788 }
e5c4f28a
RG
1789
1790 *count += PARAM_VALUE (PARAM_INLINE_CALL_COST);
6de9cd9a
DN
1791 break;
1792 }
88f4034b
DN
1793
1794 case OMP_PARALLEL:
1795 case OMP_FOR:
1796 case OMP_SECTIONS:
1797 case OMP_SINGLE:
1798 case OMP_SECTION:
1799 case OMP_MASTER:
1800 case OMP_ORDERED:
1801 case OMP_CRITICAL:
1802 case OMP_ATOMIC:
1803 /* OpenMP directives are generally very expensive. */
1804 *count += 40;
1805 break;
1806
6de9cd9a 1807 default:
1e128c5f 1808 gcc_unreachable ();
6de9cd9a
DN
1809 }
1810 return NULL;
1811}
1812
1813/* Estimate number of instructions that will be created by expanding EXPR. */
aa4a53af 1814
6de9cd9a
DN
1815int
1816estimate_num_insns (tree expr)
1817{
1818 int num = 0;
e21aff8a
SB
1819 struct pointer_set_t *visited_nodes;
1820 basic_block bb;
1821 block_stmt_iterator bsi;
1822 struct function *my_function;
1823
1824 /* If we're given an entire function, walk the CFG. */
1825 if (TREE_CODE (expr) == FUNCTION_DECL)
1826 {
1827 my_function = DECL_STRUCT_FUNCTION (expr);
1828 gcc_assert (my_function && my_function->cfg);
1829 visited_nodes = pointer_set_create ();
1830 FOR_EACH_BB_FN (bb, my_function)
1831 {
1832 for (bsi = bsi_start (bb);
1833 !bsi_end_p (bsi);
1834 bsi_next (&bsi))
1835 {
1836 walk_tree (bsi_stmt_ptr (bsi), estimate_num_insns_1,
1837 &num, visited_nodes);
1838 }
1839 }
1840 pointer_set_destroy (visited_nodes);
1841 }
1842 else
1843 walk_tree_without_duplicates (&expr, estimate_num_insns_1, &num);
1844
6de9cd9a
DN
1845 return num;
1846}
1847
426357ea
KH
1848typedef struct function *function_p;
1849
1850DEF_VEC_P(function_p);
1851DEF_VEC_ALLOC_P(function_p,heap);
1852
e21aff8a 1853/* Initialized with NOGC, making this poisonous to the garbage collector. */
426357ea 1854static VEC(function_p,heap) *cfun_stack;
e21aff8a
SB
1855
1856void
1857push_cfun (struct function *new_cfun)
1858{
426357ea 1859 VEC_safe_push (function_p, heap, cfun_stack, cfun);
e21aff8a
SB
1860 cfun = new_cfun;
1861}
1862
1863void
1864pop_cfun (void)
1865{
426357ea 1866 cfun = VEC_pop (function_p, cfun_stack);
e21aff8a
SB
1867}
1868
1869/* Install new lexical TREE_BLOCK underneath 'current_block'. */
1870static void
1871add_lexical_block (tree current_block, tree new_block)
1872{
1873 tree *blk_p;
1874
1875 /* Walk to the last sub-block. */
1876 for (blk_p = &BLOCK_SUBBLOCKS (current_block);
1877 *blk_p;
1878 blk_p = &TREE_CHAIN (*blk_p))
1879 ;
1880 *blk_p = new_block;
1881 BLOCK_SUPERCONTEXT (new_block) = current_block;
e21aff8a
SB
1882}
1883
d4e4baa9
AO
1884/* If *TP is a CALL_EXPR, replace it with its inline expansion. */
1885
e21aff8a
SB
1886static bool
1887expand_call_inline (basic_block bb, tree stmt, tree *tp, void *data)
d4e4baa9 1888{
1b369fae 1889 copy_body_data *id;
d4e4baa9 1890 tree t;
6de9cd9a 1891 tree use_retvar;
d436bff8 1892 tree fn;
d4e4baa9 1893 splay_tree st;
4977bab6
ZW
1894 tree args;
1895 tree return_slot_addr;
7740f00d 1896 tree modify_dest;
6de9cd9a 1897 location_t saved_location;
e21aff8a 1898 struct cgraph_edge *cg_edge;
dc0bfe6a 1899 const char *reason;
e21aff8a
SB
1900 basic_block return_block;
1901 edge e;
1902 block_stmt_iterator bsi, stmt_bsi;
1903 bool successfully_inlined = FALSE;
1904 tree t_step;
1905 tree var;
e21aff8a 1906 tree decl;
d4e4baa9
AO
1907
1908 /* See what we've got. */
1b369fae 1909 id = (copy_body_data *) data;
d4e4baa9
AO
1910 t = *tp;
1911
6de9cd9a
DN
1912 /* Set input_location here so we get the right instantiation context
1913 if we call instantiate_decl from inlinable_function_p. */
1914 saved_location = input_location;
1915 if (EXPR_HAS_LOCATION (t))
1916 input_location = EXPR_LOCATION (t);
1917
d4e4baa9
AO
1918 /* From here on, we're only interested in CALL_EXPRs. */
1919 if (TREE_CODE (t) != CALL_EXPR)
6de9cd9a 1920 goto egress;
d4e4baa9
AO
1921
1922 /* First, see if we can figure out what function is being called.
1923 If we cannot, then there is no hope of inlining the function. */
1924 fn = get_callee_fndecl (t);
1925 if (!fn)
6de9cd9a 1926 goto egress;
d4e4baa9 1927
b58b1157 1928 /* Turn forward declarations into real ones. */
d4d1ebc1 1929 fn = cgraph_node (fn)->decl;
b58b1157 1930
a1a0fd4e
AO
1931 /* If fn is a declaration of a function in a nested scope that was
1932 globally declared inline, we don't set its DECL_INITIAL.
1933 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
1934 C++ front-end uses it for cdtors to refer to their internal
1935 declarations, that are not real functions. Fortunately those
1936 don't have trees to be saved, so we can tell by checking their
1937 DECL_SAVED_TREE. */
1938 if (! DECL_INITIAL (fn)
1939 && DECL_ABSTRACT_ORIGIN (fn)
1940 && DECL_SAVED_TREE (DECL_ABSTRACT_ORIGIN (fn)))
1941 fn = DECL_ABSTRACT_ORIGIN (fn);
1942
18c6ada9
JH
1943 /* Objective C and fortran still calls tree_rest_of_compilation directly.
1944 Kill this check once this is fixed. */
1b369fae 1945 if (!id->dst_node->analyzed)
6de9cd9a 1946 goto egress;
18c6ada9 1947
1b369fae 1948 cg_edge = cgraph_edge (id->dst_node, stmt);
18c6ada9
JH
1949
1950 /* Constant propagation on argument done during previous inlining
1951 may create new direct call. Produce an edge for it. */
e21aff8a 1952 if (!cg_edge)
18c6ada9
JH
1953 {
1954 struct cgraph_node *dest = cgraph_node (fn);
1955
6de9cd9a
DN
1956 /* We have missing edge in the callgraph. This can happen in one case
1957 where previous inlining turned indirect call into direct call by
1958 constant propagating arguments. In all other cases we hit a bug
1959 (incorrect node sharing is most common reason for missing edges. */
70f3cc30 1960 gcc_assert (dest->needed || !flag_unit_at_a_time);
1b369fae 1961 cgraph_create_edge (id->dst_node, dest, stmt,
e42922b1 1962 bb->count, bb->loop_depth)->inline_failed
18c6ada9 1963 = N_("originally indirect function call not considered for inlining");
6de9cd9a 1964 goto egress;
18c6ada9
JH
1965 }
1966
d4e4baa9
AO
1967 /* Don't try to inline functions that are not well-suited to
1968 inlining. */
e21aff8a 1969 if (!cgraph_inline_p (cg_edge, &reason))
a833faa5 1970 {
7fac66d4
JH
1971 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
1972 /* Avoid warnings during early inline pass. */
1973 && (!flag_unit_at_a_time || cgraph_global_info_ready))
2d327012 1974 {
dee15844 1975 sorry ("inlining failed in call to %q+F: %s", fn, reason);
2d327012
JH
1976 sorry ("called from here");
1977 }
1978 else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
1979 && !DECL_IN_SYSTEM_HEADER (fn)
09ebcffa 1980 && strlen (reason)
d63db217
JH
1981 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
1982 /* Avoid warnings during early inline pass. */
1983 && (!flag_unit_at_a_time || cgraph_global_info_ready))
a833faa5 1984 {
dee15844
JM
1985 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
1986 fn, reason);
3176a0c2 1987 warning (OPT_Winline, "called from here");
a833faa5 1988 }
6de9cd9a 1989 goto egress;
a833faa5 1990 }
ea99e0be 1991 fn = cg_edge->callee->decl;
d4e4baa9 1992
18c6ada9 1993#ifdef ENABLE_CHECKING
1b369fae 1994 if (cg_edge->callee->decl != id->dst_node->decl)
e21aff8a 1995 verify_cgraph_node (cg_edge->callee);
18c6ada9
JH
1996#endif
1997
e21aff8a
SB
1998 /* We will be inlining this callee. */
1999
2000 id->eh_region = lookup_stmt_eh_region (stmt);
2001
2002 /* Split the block holding the CALL_EXPR. */
2003
2004 e = split_block (bb, stmt);
2005 bb = e->src;
2006 return_block = e->dest;
2007 remove_edge (e);
2008
2009 /* split_block splits before the statement, work around this by moving
2010 the call into the first half_bb. Not pretty, but seems easier than
2011 doing the CFG manipulation by hand when the CALL_EXPR is in the last
2012 statement in BB. */
2013 stmt_bsi = bsi_last (bb);
2014 bsi = bsi_start (return_block);
2015 if (!bsi_end_p (bsi))
2016 bsi_move_before (&stmt_bsi, &bsi);
2017 else
2018 {
2019 tree stmt = bsi_stmt (stmt_bsi);
736432ee 2020 bsi_remove (&stmt_bsi, false);
e21aff8a
SB
2021 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
2022 }
2023 stmt_bsi = bsi_start (return_block);
742a37d5 2024
d436bff8
AH
2025 /* Build a block containing code to initialize the arguments, the
2026 actual inline expansion of the body, and a label for the return
2027 statements within the function to jump to. The type of the
2028 statement expression is the return type of the function call. */
e21aff8a
SB
2029 id->block = make_node (BLOCK);
2030 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
3e2844cb 2031 BLOCK_SOURCE_LOCATION (id->block) = input_location;
e21aff8a
SB
2032 add_lexical_block (TREE_BLOCK (stmt), id->block);
2033
d4e4baa9
AO
2034 /* Local declarations will be replaced by their equivalents in this
2035 map. */
2036 st = id->decl_map;
2037 id->decl_map = splay_tree_new (splay_tree_compare_pointers,
2038 NULL, NULL);
2039
2040 /* Initialize the parameters. */
4977bab6 2041 args = TREE_OPERAND (t, 1);
4977bab6 2042
e21aff8a 2043 /* Record the function we are about to inline. */
1b369fae
RH
2044 id->src_fn = fn;
2045 id->src_node = cg_edge->callee;
2046
2047 initialize_inlined_parameters (id, args, TREE_OPERAND (t, 2), fn, bb);
d4e4baa9 2048
ea99e0be 2049 if (DECL_INITIAL (fn))
acb8f212
JH
2050 add_lexical_block (id->block, remap_blocks (DECL_INITIAL (fn), id));
2051
d4e4baa9
AO
2052 /* Return statements in the function body will be replaced by jumps
2053 to the RET_LABEL. */
d4e4baa9 2054
1e128c5f
GB
2055 gcc_assert (DECL_INITIAL (fn));
2056 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
23700f65 2057
7740f00d 2058 /* Find the lhs to which the result of this call is assigned. */
fa47911c
JM
2059 return_slot_addr = NULL;
2060 if (TREE_CODE (stmt) == MODIFY_EXPR)
81bafd36 2061 {
fa47911c 2062 modify_dest = TREE_OPERAND (stmt, 0);
81bafd36
ILT
2063
2064 /* The function which we are inlining might not return a value,
2065 in which case we should issue a warning that the function
2066 does not return a value. In that case the optimizers will
2067 see that the variable to which the value is assigned was not
2068 initialized. We do not want to issue a warning about that
2069 uninitialized variable. */
2070 if (DECL_P (modify_dest))
2071 TREE_NO_WARNING (modify_dest) = 1;
fa47911c
JM
2072 if (CALL_EXPR_RETURN_SLOT_OPT (t))
2073 {
2074 return_slot_addr = build_fold_addr_expr (modify_dest);
2075 modify_dest = NULL;
2076 }
81bafd36 2077 }
7740f00d
RH
2078 else
2079 modify_dest = NULL;
2080
d4e4baa9 2081 /* Declare the return variable for the function. */
e21aff8a
SB
2082 decl = declare_return_variable (id, return_slot_addr,
2083 modify_dest, &use_retvar);
2084 /* Do this only if declare_return_variable created a new one. */
2085 if (decl && !return_slot_addr && decl != modify_dest)
2086 declare_inline_vars (id->block, decl);
d4e4baa9 2087
e21aff8a
SB
2088 /* This is it. Duplicate the callee body. Assume callee is
2089 pre-gimplified. Note that we must not alter the caller
2090 function in any way before this point, as this CALL_EXPR may be
2091 a self-referential call; if we're calling ourselves, we need to
2092 duplicate our body before altering anything. */
2093 copy_body (id, bb->count, bb->frequency, bb, return_block);
50aadcbc 2094
acb8f212 2095 /* Add local vars in this inlined callee to caller. */
1b369fae 2096 t_step = id->src_cfun->unexpanded_var_list;
acb8f212
JH
2097 for (; t_step; t_step = TREE_CHAIN (t_step))
2098 {
2099 var = TREE_VALUE (t_step);
2100 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
2101 cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
2102 cfun->unexpanded_var_list);
2103 else
2104 cfun->unexpanded_var_list = tree_cons (NULL_TREE, remap_decl (var, id),
2105 cfun->unexpanded_var_list);
2106 }
2107
d4e4baa9
AO
2108 /* Clean up. */
2109 splay_tree_delete (id->decl_map);
2110 id->decl_map = st;
2111
84936f6f 2112 /* If the inlined function returns a result that we care about,
e21aff8a
SB
2113 clobber the CALL_EXPR with a reference to the return variable. */
2114 if (use_retvar && (TREE_CODE (bsi_stmt (stmt_bsi)) != CALL_EXPR))
2115 {
2116 *tp = use_retvar;
2117 maybe_clean_or_replace_eh_stmt (stmt, stmt);
2118 }
6de9cd9a 2119 else
e21aff8a
SB
2120 /* We're modifying a TSI owned by gimple_expand_calls_inline();
2121 tsi_delink() will leave the iterator in a sane state. */
736432ee 2122 bsi_remove (&stmt_bsi, true);
d4e4baa9 2123
e21aff8a
SB
2124 bsi_next (&bsi);
2125 if (bsi_end_p (bsi))
2126 tree_purge_dead_eh_edges (return_block);
84936f6f 2127
e21aff8a
SB
2128 /* If the value of the new expression is ignored, that's OK. We
2129 don't warn about this for CALL_EXPRs, so we shouldn't warn about
2130 the equivalent inlined version either. */
2131 TREE_USED (*tp) = 1;
84936f6f 2132
1eb3331e
DB
2133 /* Output the inlining info for this abstract function, since it has been
2134 inlined. If we don't do this now, we can lose the information about the
2135 variables in the function when the blocks get blown away as soon as we
2136 remove the cgraph node. */
e21aff8a 2137 (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
84936f6f 2138
e72fcfe8 2139 /* Update callgraph if needed. */
e21aff8a 2140 cgraph_remove_node (cg_edge->callee);
e72fcfe8 2141
e21aff8a
SB
2142 /* Declare the 'auto' variables added with this inlined body. */
2143 record_vars (BLOCK_VARS (id->block));
2144 id->block = NULL_TREE;
e21aff8a 2145 successfully_inlined = TRUE;
742a37d5 2146
6de9cd9a
DN
2147 egress:
2148 input_location = saved_location;
e21aff8a 2149 return successfully_inlined;
d4e4baa9 2150}
6de9cd9a 2151
e21aff8a
SB
2152/* Expand call statements reachable from STMT_P.
2153 We can only have CALL_EXPRs as the "toplevel" tree code or nested
2154 in a MODIFY_EXPR. See tree-gimple.c:get_call_expr_in(). We can
2155 unfortunately not use that function here because we need a pointer
2156 to the CALL_EXPR, not the tree itself. */
2157
2158static bool
1b369fae 2159gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
6de9cd9a 2160{
e21aff8a 2161 block_stmt_iterator bsi;
6de9cd9a 2162
e21aff8a
SB
2163 /* Register specific tree functions. */
2164 tree_register_cfg_hooks ();
2165 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
6de9cd9a 2166 {
e21aff8a
SB
2167 tree *expr_p = bsi_stmt_ptr (bsi);
2168 tree stmt = *expr_p;
2169
2170 if (TREE_CODE (*expr_p) == MODIFY_EXPR)
2171 expr_p = &TREE_OPERAND (*expr_p, 1);
2172 if (TREE_CODE (*expr_p) == WITH_SIZE_EXPR)
2173 expr_p = &TREE_OPERAND (*expr_p, 0);
2174 if (TREE_CODE (*expr_p) == CALL_EXPR)
2175 if (expand_call_inline (bb, stmt, expr_p, id))
2176 return true;
6de9cd9a 2177 }
e21aff8a 2178 return false;
6de9cd9a
DN
2179}
2180
d4e4baa9
AO
2181/* Expand calls to inline functions in the body of FN. */
2182
2183void
46c5ad27 2184optimize_inline_calls (tree fn)
d4e4baa9 2185{
1b369fae 2186 copy_body_data id;
d4e4baa9 2187 tree prev_fn;
e21aff8a 2188 basic_block bb;
c5b6f18e
MM
2189 /* There is no point in performing inlining if errors have already
2190 occurred -- and we might crash if we try to inline invalid
2191 code. */
2192 if (errorcount || sorrycount)
2193 return;
2194
d4e4baa9
AO
2195 /* Clear out ID. */
2196 memset (&id, 0, sizeof (id));
2197
1b369fae
RH
2198 id.src_node = id.dst_node = cgraph_node (fn);
2199 id.dst_fn = fn;
d4e4baa9
AO
2200 /* Or any functions that aren't finished yet. */
2201 prev_fn = NULL_TREE;
2202 if (current_function_decl)
2203 {
1b369fae 2204 id.dst_fn = current_function_decl;
d4e4baa9
AO
2205 prev_fn = current_function_decl;
2206 }
1b369fae
RH
2207
2208 id.copy_decl = copy_decl_maybe_to_var;
2209 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2210 id.transform_new_cfg = false;
2211 id.transform_return_to_modify = true;
2212 id.transform_lang_insert_block = false;
2213
e21aff8a 2214 push_gimplify_context ();
d4e4baa9 2215
e21aff8a
SB
2216 /* Reach the trees by walking over the CFG, and note the
2217 enclosing basic-blocks in the call edges. */
2218 /* We walk the blocks going forward, because inlined function bodies
2219 will split id->current_basic_block, and the new blocks will
2220 follow it; we'll trudge through them, processing their CALL_EXPRs
2221 along the way. */
2222 FOR_EACH_BB (bb)
2223 gimple_expand_calls_inline (bb, &id);
d4e4baa9 2224
e21aff8a
SB
2225 pop_gimplify_context (NULL);
2226 /* Renumber the (code) basic_blocks consecutively. */
2227 compact_blocks ();
2228 /* Renumber the lexical scoping (non-code) blocks consecutively. */
2229 number_blocks (fn);
6de9cd9a 2230
18c6ada9
JH
2231#ifdef ENABLE_CHECKING
2232 {
2233 struct cgraph_edge *e;
2234
1b369fae 2235 verify_cgraph_node (id.dst_node);
18c6ada9
JH
2236
2237 /* Double check that we inlined everything we are supposed to inline. */
1b369fae 2238 for (e = id.dst_node->callees; e; e = e->next_callee)
1e128c5f 2239 gcc_assert (e->inline_failed);
18c6ada9
JH
2240 }
2241#endif
e21aff8a
SB
2242 /* We need to rescale frequencies again to peak at REG_BR_PROB_BASE
2243 as inlining loops might increase the maximum. */
2244 if (ENTRY_BLOCK_PTR->count)
2245 counts_to_freqs ();
2246 fold_cond_expr_cond ();
d4e4baa9
AO
2247}
2248
aa4a53af
RK
2249/* FN is a function that has a complete body, and CLONE is a function whose
2250 body is to be set to a copy of FN, mapping argument declarations according
2251 to the ARG_MAP splay_tree. */
d4e4baa9
AO
2252
2253void
46c5ad27 2254clone_body (tree clone, tree fn, void *arg_map)
d4e4baa9 2255{
1b369fae 2256 copy_body_data id;
d4e4baa9 2257
aa4a53af 2258 /* Clone the body, as if we were making an inline call. But, remap the
e21aff8a 2259 parameters in the callee to the parameters of caller. */
d4e4baa9 2260 memset (&id, 0, sizeof (id));
1b369fae
RH
2261 id.src_fn = fn;
2262 id.dst_fn = clone;
2263 id.src_cfun = DECL_STRUCT_FUNCTION (fn);
d4e4baa9
AO
2264 id.decl_map = (splay_tree)arg_map;
2265
1b369fae
RH
2266 id.copy_decl = copy_decl_no_change;
2267 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2268 id.transform_new_cfg = true;
2269 id.transform_return_to_modify = false;
2270 id.transform_lang_insert_block = true;
d4e4baa9 2271
e21aff8a
SB
2272 /* We're not inside any EH region. */
2273 id.eh_region = -1;
2274
d4e4baa9 2275 /* Actually copy the body. */
e21aff8a 2276 append_to_statement_list_force (copy_generic_body (&id), &DECL_SAVED_TREE (clone));
d4e4baa9
AO
2277}
2278
d4e4baa9
AO
2279/* Passed to walk_tree. Copies the node pointed to, if appropriate. */
2280
2281tree
46c5ad27 2282copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
d4e4baa9
AO
2283{
2284 enum tree_code code = TREE_CODE (*tp);
2285
2286 /* We make copies of most nodes. */
2287 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
d4e4baa9
AO
2288 || code == TREE_LIST
2289 || code == TREE_VEC
8843c120
DN
2290 || code == TYPE_DECL
2291 || code == OMP_CLAUSE)
d4e4baa9
AO
2292 {
2293 /* Because the chain gets clobbered when we make a copy, we save it
2294 here. */
2295 tree chain = TREE_CHAIN (*tp);
6de9cd9a 2296 tree new;
d4e4baa9
AO
2297
2298 /* Copy the node. */
6de9cd9a
DN
2299 new = copy_node (*tp);
2300
2301 /* Propagate mudflap marked-ness. */
2302 if (flag_mudflap && mf_marked_p (*tp))
2303 mf_mark (new);
2304
2305 *tp = new;
d4e4baa9
AO
2306
2307 /* Now, restore the chain, if appropriate. That will cause
2308 walk_tree to walk into the chain as well. */
50674e96
DN
2309 if (code == PARM_DECL
2310 || code == TREE_LIST
aaf46ef9 2311 || code == OMP_CLAUSE)
d4e4baa9
AO
2312 TREE_CHAIN (*tp) = chain;
2313
2314 /* For now, we don't update BLOCKs when we make copies. So, we
6de9cd9a
DN
2315 have to nullify all BIND_EXPRs. */
2316 if (TREE_CODE (*tp) == BIND_EXPR)
2317 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
d4e4baa9 2318 }
4038c495
GB
2319 else if (code == CONSTRUCTOR)
2320 {
2321 /* CONSTRUCTOR nodes need special handling because
2322 we need to duplicate the vector of elements. */
2323 tree new;
2324
2325 new = copy_node (*tp);
2326
2327 /* Propagate mudflap marked-ness. */
2328 if (flag_mudflap && mf_marked_p (*tp))
2329 mf_mark (new);
9f63daea 2330
4038c495
GB
2331 CONSTRUCTOR_ELTS (new) = VEC_copy (constructor_elt, gc,
2332 CONSTRUCTOR_ELTS (*tp));
2333 *tp = new;
2334 }
6615c446 2335 else if (TREE_CODE_CLASS (code) == tcc_type)
d4e4baa9 2336 *walk_subtrees = 0;
6615c446 2337 else if (TREE_CODE_CLASS (code) == tcc_declaration)
6de9cd9a 2338 *walk_subtrees = 0;
a396f8ae
GK
2339 else if (TREE_CODE_CLASS (code) == tcc_constant)
2340 *walk_subtrees = 0;
1e128c5f
GB
2341 else
2342 gcc_assert (code != STATEMENT_LIST);
d4e4baa9
AO
2343 return NULL_TREE;
2344}
2345
2346/* The SAVE_EXPR pointed to by TP is being copied. If ST contains
aa4a53af 2347 information indicating to what new SAVE_EXPR this one should be mapped,
e21aff8a
SB
2348 use that one. Otherwise, create a new node and enter it in ST. FN is
2349 the function into which the copy will be placed. */
d4e4baa9 2350
892c7e1e 2351static void
82c82743 2352remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
d4e4baa9
AO
2353{
2354 splay_tree st = (splay_tree) st_;
2355 splay_tree_node n;
5e20bdd7 2356 tree t;
d4e4baa9
AO
2357
2358 /* See if we already encountered this SAVE_EXPR. */
2359 n = splay_tree_lookup (st, (splay_tree_key) *tp);
d92b4486 2360
d4e4baa9
AO
2361 /* If we didn't already remap this SAVE_EXPR, do so now. */
2362 if (!n)
2363 {
5e20bdd7 2364 t = copy_node (*tp);
d4e4baa9 2365
d4e4baa9 2366 /* Remember this SAVE_EXPR. */
5e20bdd7 2367 splay_tree_insert (st, (splay_tree_key) *tp, (splay_tree_value) t);
350ebd54 2368 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
1593ad2e 2369 splay_tree_insert (st, (splay_tree_key) t, (splay_tree_value) t);
d4e4baa9
AO
2370 }
2371 else
5e20bdd7
JZ
2372 {
2373 /* We've already walked into this SAVE_EXPR; don't do it again. */
2374 *walk_subtrees = 0;
2375 t = (tree) n->value;
2376 }
d4e4baa9
AO
2377
2378 /* Replace this SAVE_EXPR with the copy. */
5e20bdd7 2379 *tp = t;
d4e4baa9 2380}
d436bff8 2381
aa4a53af
RK
2382/* Called via walk_tree. If *TP points to a DECL_STMT for a local label,
2383 copies the declaration and enters it in the splay_tree in DATA (which is
1b369fae 2384 really an `copy_body_data *'). */
6de9cd9a
DN
2385
2386static tree
2387mark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
2388 void *data)
2389{
1b369fae 2390 copy_body_data *id = (copy_body_data *) data;
6de9cd9a
DN
2391
2392 /* Don't walk into types. */
350fae66
RK
2393 if (TYPE_P (*tp))
2394 *walk_subtrees = 0;
6de9cd9a 2395
350fae66 2396 else if (TREE_CODE (*tp) == LABEL_EXPR)
6de9cd9a 2397 {
350fae66 2398 tree decl = TREE_OPERAND (*tp, 0);
6de9cd9a 2399
350fae66 2400 /* Copy the decl and remember the copy. */
1b369fae 2401 insert_decl_map (id, decl, id->copy_decl (decl, id));
6de9cd9a
DN
2402 }
2403
2404 return NULL_TREE;
2405}
2406
19114537
EC
2407/* Perform any modifications to EXPR required when it is unsaved. Does
2408 not recurse into EXPR's subtrees. */
2409
2410static void
2411unsave_expr_1 (tree expr)
2412{
2413 switch (TREE_CODE (expr))
2414 {
2415 case TARGET_EXPR:
2416 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
2417 It's OK for this to happen if it was part of a subtree that
2418 isn't immediately expanded, such as operand 2 of another
2419 TARGET_EXPR. */
2420 if (TREE_OPERAND (expr, 1))
2421 break;
2422
2423 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
2424 TREE_OPERAND (expr, 3) = NULL_TREE;
2425 break;
2426
2427 default:
2428 break;
2429 }
2430}
2431
6de9cd9a
DN
2432/* Called via walk_tree when an expression is unsaved. Using the
2433 splay_tree pointed to by ST (which is really a `splay_tree'),
2434 remaps all local declarations to appropriate replacements. */
d436bff8
AH
2435
2436static tree
6de9cd9a 2437unsave_r (tree *tp, int *walk_subtrees, void *data)
d436bff8 2438{
1b369fae 2439 copy_body_data *id = (copy_body_data *) data;
6de9cd9a
DN
2440 splay_tree st = id->decl_map;
2441 splay_tree_node n;
2442
2443 /* Only a local declaration (variable or label). */
2444 if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
2445 || TREE_CODE (*tp) == LABEL_DECL)
2446 {
2447 /* Lookup the declaration. */
2448 n = splay_tree_lookup (st, (splay_tree_key) *tp);
9f63daea 2449
6de9cd9a
DN
2450 /* If it's there, remap it. */
2451 if (n)
2452 *tp = (tree) n->value;
2453 }
aa4a53af 2454
6de9cd9a
DN
2455 else if (TREE_CODE (*tp) == STATEMENT_LIST)
2456 copy_statement_list (tp);
2457 else if (TREE_CODE (*tp) == BIND_EXPR)
2458 copy_bind_expr (tp, walk_subtrees, id);
2459 else if (TREE_CODE (*tp) == SAVE_EXPR)
82c82743 2460 remap_save_expr (tp, st, walk_subtrees);
d436bff8 2461 else
6de9cd9a
DN
2462 {
2463 copy_tree_r (tp, walk_subtrees, NULL);
2464
2465 /* Do whatever unsaving is required. */
2466 unsave_expr_1 (*tp);
2467 }
2468
2469 /* Keep iterating. */
2470 return NULL_TREE;
d436bff8
AH
2471}
2472
19114537
EC
2473/* Copies everything in EXPR and replaces variables, labels
2474 and SAVE_EXPRs local to EXPR. */
6de9cd9a
DN
2475
2476tree
19114537 2477unsave_expr_now (tree expr)
6de9cd9a 2478{
1b369fae 2479 copy_body_data id;
6de9cd9a
DN
2480
2481 /* There's nothing to do for NULL_TREE. */
2482 if (expr == 0)
2483 return expr;
2484
2485 /* Set up ID. */
2486 memset (&id, 0, sizeof (id));
1b369fae
RH
2487 id.src_fn = current_function_decl;
2488 id.dst_fn = current_function_decl;
6de9cd9a
DN
2489 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2490
1b369fae
RH
2491 id.copy_decl = copy_decl_no_change;
2492 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2493 id.transform_new_cfg = false;
2494 id.transform_return_to_modify = false;
2495 id.transform_lang_insert_block = false;
2496
6de9cd9a
DN
2497 /* Walk the tree once to find local labels. */
2498 walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
2499
2500 /* Walk the tree again, copying, remapping, and unsaving. */
2501 walk_tree (&expr, unsave_r, &id, NULL);
2502
2503 /* Clean up. */
2504 splay_tree_delete (id.decl_map);
2505
2506 return expr;
2507}
2508
2509/* Allow someone to determine if SEARCH is a child of TOP from gdb. */
aa4a53af 2510
6de9cd9a
DN
2511static tree
2512debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
2513{
2514 if (*tp == data)
2515 return (tree) data;
2516 else
2517 return NULL;
2518}
2519
6de9cd9a
DN
2520bool
2521debug_find_tree (tree top, tree search)
2522{
2523 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
2524}
2525
e21aff8a 2526
6de9cd9a
DN
2527/* Declare the variables created by the inliner. Add all the variables in
2528 VARS to BIND_EXPR. */
2529
2530static void
e21aff8a 2531declare_inline_vars (tree block, tree vars)
6de9cd9a 2532{
84936f6f
RH
2533 tree t;
2534 for (t = vars; t; t = TREE_CHAIN (t))
2535 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
6de9cd9a 2536
e21aff8a
SB
2537 if (block)
2538 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
2539}
2540
19734dd8
RL
2541
2542/* Copy NODE (which must be a DECL). The DECL originally was in the FROM_FN,
1b369fae
RH
2543 but now it will be in the TO_FN. PARM_TO_VAR means enable PARM_DECL to
2544 VAR_DECL translation. */
19734dd8 2545
1b369fae
RH
2546static tree
2547copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
19734dd8 2548{
19734dd8
RL
2549 /* Don't generate debug information for the copy if we wouldn't have
2550 generated it for the copy either. */
2551 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
2552 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
2553
2554 /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
2555 declaration inspired this copy. */
2556 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
2557
2558 /* The new variable/label has no RTL, yet. */
68a976f2
RL
2559 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
2560 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
19734dd8
RL
2561 SET_DECL_RTL (copy, NULL_RTX);
2562
2563 /* These args would always appear unused, if not for this. */
2564 TREE_USED (copy) = 1;
2565
2566 /* Set the context for the new declaration. */
2567 if (!DECL_CONTEXT (decl))
2568 /* Globals stay global. */
2569 ;
1b369fae 2570 else if (DECL_CONTEXT (decl) != id->src_fn)
19734dd8
RL
2571 /* Things that weren't in the scope of the function we're inlining
2572 from aren't in the scope we're inlining to, either. */
2573 ;
2574 else if (TREE_STATIC (decl))
2575 /* Function-scoped static variables should stay in the original
2576 function. */
2577 ;
2578 else
2579 /* Ordinary automatic local variables are now in the scope of the
2580 new function. */
1b369fae 2581 DECL_CONTEXT (copy) = id->dst_fn;
19734dd8
RL
2582
2583 return copy;
2584}
2585
1b369fae
RH
2586static tree
2587copy_decl_to_var (tree decl, copy_body_data *id)
2588{
2589 tree copy, type;
2590
2591 gcc_assert (TREE_CODE (decl) == PARM_DECL
2592 || TREE_CODE (decl) == RESULT_DECL);
2593
2594 type = TREE_TYPE (decl);
2595
2596 copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
2597 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
2598 TREE_READONLY (copy) = TREE_READONLY (decl);
2599 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
2600 DECL_COMPLEX_GIMPLE_REG_P (copy) = DECL_COMPLEX_GIMPLE_REG_P (decl);
2601
2602 return copy_decl_for_dup_finish (id, decl, copy);
2603}
2604
2605static tree
2606copy_decl_no_change (tree decl, copy_body_data *id)
2607{
2608 tree copy;
2609
2610 copy = copy_node (decl);
2611
2612 /* The COPY is not abstract; it will be generated in DST_FN. */
2613 DECL_ABSTRACT (copy) = 0;
2614 lang_hooks.dup_lang_specific_decl (copy);
2615
2616 /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
2617 been taken; it's for internal bookkeeping in expand_goto_internal. */
2618 if (TREE_CODE (copy) == LABEL_DECL)
2619 {
2620 TREE_ADDRESSABLE (copy) = 0;
2621 LABEL_DECL_UID (copy) = -1;
2622 }
2623
2624 return copy_decl_for_dup_finish (id, decl, copy);
2625}
2626
2627static tree
2628copy_decl_maybe_to_var (tree decl, copy_body_data *id)
2629{
2630 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
2631 return copy_decl_to_var (decl, id);
2632 else
2633 return copy_decl_no_change (decl, id);
2634}
2635
19734dd8
RL
2636/* Return a copy of the function's argument tree. */
2637static tree
1b369fae 2638copy_arguments_for_versioning (tree orig_parm, copy_body_data * id)
19734dd8
RL
2639{
2640 tree *arg_copy, *parg;
2641
2642 arg_copy = &orig_parm;
2643 for (parg = arg_copy; *parg; parg = &TREE_CHAIN (*parg))
2644 {
2645 tree new = remap_decl (*parg, id);
2646 lang_hooks.dup_lang_specific_decl (new);
2647 TREE_CHAIN (new) = TREE_CHAIN (*parg);
2648 *parg = new;
2649 }
2650 return orig_parm;
2651}
2652
2653/* Return a copy of the function's static chain. */
2654static tree
1b369fae 2655copy_static_chain (tree static_chain, copy_body_data * id)
19734dd8
RL
2656{
2657 tree *chain_copy, *pvar;
2658
2659 chain_copy = &static_chain;
2660 for (pvar = chain_copy; *pvar; pvar = &TREE_CHAIN (*pvar))
2661 {
2662 tree new = remap_decl (*pvar, id);
2663 lang_hooks.dup_lang_specific_decl (new);
2664 TREE_CHAIN (new) = TREE_CHAIN (*pvar);
2665 *pvar = new;
2666 }
2667 return static_chain;
2668}
2669
2670/* Return true if the function is allowed to be versioned.
2671 This is a guard for the versioning functionality. */
2672bool
2673tree_versionable_function_p (tree fndecl)
2674{
2675 if (fndecl == NULL_TREE)
2676 return false;
2677 /* ??? There are cases where a function is
2678 uninlinable but can be versioned. */
2679 if (!tree_inlinable_function_p (fndecl))
2680 return false;
2681
2682 return true;
2683}
2684
2685/* Create a copy of a function's tree.
2686 OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
2687 of the original function and the new copied function
2688 respectively. In case we want to replace a DECL
2689 tree with another tree while duplicating the function's
2690 body, TREE_MAP represents the mapping between these
ea99e0be
JH
2691 trees. If UPDATE_CLONES is set, the call_stmt fields
2692 of edges of clones of the function will be updated. */
19734dd8 2693void
ea99e0be
JH
2694tree_function_versioning (tree old_decl, tree new_decl, varray_type tree_map,
2695 bool update_clones)
19734dd8
RL
2696{
2697 struct cgraph_node *old_version_node;
2698 struct cgraph_node *new_version_node;
1b369fae 2699 copy_body_data id;
19734dd8
RL
2700 tree p, new_fndecl;
2701 unsigned i;
2702 struct ipa_replace_map *replace_info;
2703 basic_block old_entry_block;
2704 tree t_step;
2705
2706 gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
2707 && TREE_CODE (new_decl) == FUNCTION_DECL);
2708 DECL_POSSIBLY_INLINED (old_decl) = 1;
2709
2710 old_version_node = cgraph_node (old_decl);
2711 new_version_node = cgraph_node (new_decl);
2712
2713 allocate_struct_function (new_decl);
2714 /* Cfun points to the new allocated function struct at this point. */
2715 cfun->function_end_locus = DECL_SOURCE_LOCATION (new_decl);
2716
2717 DECL_ARTIFICIAL (new_decl) = 1;
2718 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
2719
2720 /* Generate a new name for the new version. */
ea99e0be 2721 if (!update_clones)
1b369fae 2722 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
19734dd8
RL
2723 /* Create a new SYMBOL_REF rtx for the new name. */
2724 if (DECL_RTL (old_decl) != NULL)
2725 {
2726 SET_DECL_RTL (new_decl, copy_rtx (DECL_RTL (old_decl)));
2727 XEXP (DECL_RTL (new_decl), 0) =
2728 gen_rtx_SYMBOL_REF (GET_MODE (XEXP (DECL_RTL (old_decl), 0)),
2729 IDENTIFIER_POINTER (DECL_NAME (new_decl)));
2730 }
2731
2732 /* Prepare the data structures for the tree copy. */
2733 memset (&id, 0, sizeof (id));
2734
19734dd8 2735 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
1b369fae
RH
2736 id.src_fn = old_decl;
2737 id.dst_fn = new_decl;
2738 id.src_node = old_version_node;
2739 id.dst_node = new_version_node;
2740 id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
19734dd8 2741
1b369fae
RH
2742 id.copy_decl = copy_decl_no_change;
2743 id.transform_call_graph_edges
2744 = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
2745 id.transform_new_cfg = true;
2746 id.transform_return_to_modify = false;
2747 id.transform_lang_insert_block = false;
2748
19734dd8
RL
2749 current_function_decl = new_decl;
2750
2751 /* Copy the function's static chain. */
2752 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
2753 if (p)
2754 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
2755 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
2756 &id);
2757 /* Copy the function's arguments. */
2758 if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
2759 DECL_ARGUMENTS (new_decl) =
2760 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id);
2761
2762 /* If there's a tree_map, prepare for substitution. */
2763 if (tree_map)
2764 for (i = 0; i < VARRAY_ACTIVE_SIZE (tree_map); i++)
2765 {
2766 replace_info = VARRAY_GENERIC_PTR (tree_map, i);
1b369fae 2767 if (replace_info->replace_p)
19734dd8
RL
2768 insert_decl_map (&id, replace_info->old_tree,
2769 replace_info->new_tree);
19734dd8
RL
2770 }
2771
1b369fae 2772 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
19734dd8
RL
2773
2774 /* Renumber the lexical scoping (non-code) blocks consecutively. */
1b369fae 2775 number_blocks (id.dst_fn);
19734dd8
RL
2776
2777 if (DECL_STRUCT_FUNCTION (old_decl)->unexpanded_var_list != NULL_TREE)
2778 /* Add local vars. */
2779 for (t_step = DECL_STRUCT_FUNCTION (old_decl)->unexpanded_var_list;
2780 t_step; t_step = TREE_CHAIN (t_step))
2781 {
2782 tree var = TREE_VALUE (t_step);
2783 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
2784 cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
2785 cfun->unexpanded_var_list);
2786 else
2787 cfun->unexpanded_var_list =
2788 tree_cons (NULL_TREE, remap_decl (var, &id),
2789 cfun->unexpanded_var_list);
2790 }
2791
2792 /* Copy the Function's body. */
2793 old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
2794 (DECL_STRUCT_FUNCTION (old_decl));
2795 new_fndecl = copy_body (&id,
2796 old_entry_block->count,
2797 old_entry_block->frequency, NULL, NULL);
2798
2799 DECL_SAVED_TREE (new_decl) = DECL_SAVED_TREE (new_fndecl);
2800
2801 DECL_STRUCT_FUNCTION (new_decl)->cfg =
2802 DECL_STRUCT_FUNCTION (new_fndecl)->cfg;
2803 DECL_STRUCT_FUNCTION (new_decl)->eh = DECL_STRUCT_FUNCTION (new_fndecl)->eh;
2804 DECL_STRUCT_FUNCTION (new_decl)->ib_boundaries_block =
2805 DECL_STRUCT_FUNCTION (new_fndecl)->ib_boundaries_block;
2806 DECL_STRUCT_FUNCTION (new_decl)->last_label_uid =
2807 DECL_STRUCT_FUNCTION (new_fndecl)->last_label_uid;
2808
2809 if (DECL_RESULT (old_decl) != NULL_TREE)
2810 {
2811 tree *res_decl = &DECL_RESULT (old_decl);
2812 DECL_RESULT (new_decl) = remap_decl (*res_decl, &id);
2813 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
2814 }
2815
2816 current_function_decl = NULL;
2817 /* Renumber the lexical scoping (non-code) blocks consecutively. */
2818 number_blocks (new_decl);
2819
2820 /* Clean up. */
2821 splay_tree_delete (id.decl_map);
2822 fold_cond_expr_cond ();
2823 return;
2824}
2825
52dd234b
RH
2826/* Duplicate a type, fields and all. */
2827
2828tree
2829build_duplicate_type (tree type)
2830{
1b369fae 2831 struct copy_body_data id;
52dd234b
RH
2832
2833 memset (&id, 0, sizeof (id));
1b369fae
RH
2834 id.src_fn = current_function_decl;
2835 id.dst_fn = current_function_decl;
2836 id.src_cfun = cfun;
52dd234b
RH
2837 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2838
2839 type = remap_type_1 (type, &id);
2840
2841 splay_tree_delete (id.decl_map);
2842
2843 return type;
2844}