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