]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/optimize.c
pt.c (determine_specialization): Ignore artificial functions.
[thirdparty/gcc.git] / gcc / cp / optimize.c
CommitLineData
46e8c075 1/* Perform optimizations on tree structure.
1f84ec23 2 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
46e8c075
MM
3 Written by Mark Michell (mark@codesourcery.com).
4
06ceef4e 5This file is part of GNU CC.
46e8c075 6
06ceef4e
RK
7GNU CC is free software; you can redistribute it and/or modify it
8under 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
12GNU CC is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
9c96f3f8 16
06ceef4e
RK
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
46e8c075
MM
21
22#include "config.h"
23#include "system.h"
24#include "tree.h"
25#include "cp-tree.h"
26#include "rtl.h"
27#include "insn-config.h"
574a0ef5 28#include "input.h"
46e8c075 29#include "integrate.h"
9c96f3f8 30#include "toplev.h"
46e8c075 31#include "varray.h"
b3bae5e2 32#include "ggc.h"
b850de4f 33#include "params.h"
11fe225a 34#include "hashtab.h"
46e8c075
MM
35
36/* To Do:
37
46e8c075
MM
38 o In order to make inlining-on-trees work, we pessimized
39 function-local static constants. In particular, they are now
40 always output, even when not addressed. Fix this by treating
41 function-local static constants just like global static
42 constants; the back-end already knows not to output them if they
43 are not needed.
9c96f3f8 44
46e8c075 45 o Provide heuristics to clamp inlining of recursive template
3431f196 46 calls? */
390f4e9a 47
46e8c075
MM
48/* Data required for function inlining. */
49
50typedef struct inline_data
51{
52 /* A stack of the functions we are inlining. For example, if we are
53 compiling `f', which calls `g', which calls `h', and we are
54 inlining the body of `h', the stack will contain, `h', followed
b850de4f
MM
55 by `g', followed by `f'. The first few elements of the stack may
56 contain other functions that we know we should not recurse into,
57 even though they are not directly being inlined. */
46e8c075 58 varray_type fns;
b850de4f
MM
59 /* The index of the first element of FNS that really represents an
60 inlined function. */
61 unsigned first_inlined_fn;
db9b2174
MM
62 /* The label to jump to when a return statement is encountered. If
63 this value is NULL, then return statements will simply be
64 remapped as return statements, rather than as jumps. */
46e8c075
MM
65 tree ret_label;
66 /* The map from local declarations in the inlined function to
67 equivalents in the function into which it is being inlined. */
68 splay_tree decl_map;
390f4e9a
MM
69 /* Nonzero if we are currently within the cleanup for a
70 TARGET_EXPR. */
71 int in_target_cleanup_p;
738e2810
MM
72 /* A stack of the TARGET_EXPRs that we are currently processing. */
73 varray_type target_exprs;
b3bae5e2
JJ
74 /* A list of the functions current function has inlined. */
75 varray_type inlined_fns;
b850de4f
MM
76 /* The approximate number of statements we have inlined in the
77 current call stack. */
78 int inlined_stmts;
79 /* We use the same mechanism to build clones that we do to perform
80 inlining. However, there are a few places where we need to
81 distinguish between those two situations. This flag is true nif
82 we are cloning, rather than inlining. */
83 bool cloning_p;
11fe225a
ZW
84 /* Hash table used to prevent walk_tree from visiting the same node
85 umpteen million times. */
86 htab_t tree_pruner;
46e8c075
MM
87} inline_data;
88
89/* Prototypes. */
90
158991b7
KG
91static tree initialize_inlined_parameters PARAMS ((inline_data *, tree, tree));
92static tree declare_return_variable PARAMS ((inline_data *, tree *));
93static tree copy_body_r PARAMS ((tree *, int *, void *));
94static tree copy_body PARAMS ((inline_data *));
95static tree expand_call_inline PARAMS ((tree *, int *, void *));
96static void expand_calls_inline PARAMS ((tree *, inline_data *));
97static int inlinable_function_p PARAMS ((tree, inline_data *));
98static tree remap_decl PARAMS ((tree, inline_data *));
99static void remap_block PARAMS ((tree, tree, inline_data *));
100static void copy_scope_stmt PARAMS ((tree *, int *, inline_data *));
101static tree calls_setjmp_r PARAMS ((tree *, int *, void *));
d60e5448 102static void update_cloned_parm PARAMS ((tree, tree));
390f4e9a 103
b850de4f
MM
104/* The approximate number of instructions per statement. This number
105 need not be particularly accurate; it is used only to make
106 decisions about when a function is too big to inline. */
107#define INSNS_PER_STMT (10)
108
390f4e9a
MM
109/* Remap DECL during the copying of the BLOCK tree for the function.
110 DATA is really an `inline_data *'. */
111
112static tree
113remap_decl (decl, id)
114 tree decl;
115 inline_data *id;
116{
117 splay_tree_node n;
118 tree fn;
119
120 /* We only remap local variables in the current function. */
121 fn = VARRAY_TOP_TREE (id->fns);
122 if (!nonstatic_local_decl_p (decl) || DECL_CONTEXT (decl) != fn)
123 return NULL_TREE;
124
125 /* See if we have remapped this declaration. */
126 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
127 /* If we didn't already have an equivalent for this declaration,
128 create one now. */
129 if (!n)
130 {
131 tree t;
9c96f3f8 132
390f4e9a 133 /* Make a copy of the variable or label. */
9c96f3f8 134 t = copy_decl_for_inlining (decl, fn,
390f4e9a 135 VARRAY_TREE (id->fns, 0));
aa1eac0c
JW
136
137 /* The decl T could be a dynamic array or other variable size type,
138 in which case some fields need to be remapped because they may
139 contain SAVE_EXPRs. */
ee94fce6
MM
140 walk_tree (&DECL_SIZE (t), copy_body_r, id, NULL);
141 walk_tree (&DECL_SIZE_UNIT (t), copy_body_r, id, NULL);
aa1eac0c
JW
142 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
143 && TYPE_DOMAIN (TREE_TYPE (t)))
cba6a0b2
MM
144 {
145 TREE_TYPE (t) = copy_node (TREE_TYPE (t));
9c96f3f8 146 TYPE_DOMAIN (TREE_TYPE (t))
cba6a0b2
MM
147 = copy_node (TYPE_DOMAIN (TREE_TYPE (t)));
148 walk_tree (&TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t))),
ee94fce6 149 copy_body_r, id, NULL);
cba6a0b2 150 }
aa1eac0c 151
390f4e9a
MM
152 /* Remember it, so that if we encounter this local entity
153 again we can reuse this copy. */
9c96f3f8
AJ
154 n = splay_tree_insert (id->decl_map,
155 (splay_tree_key) decl,
390f4e9a
MM
156 (splay_tree_value) t);
157 }
9c96f3f8 158
390f4e9a
MM
159 return (tree) n->value;
160}
161
162/* Copy the SCOPE_STMT_BLOCK associated with SCOPE_STMT to contain
163 remapped versions of the variables therein. And hook the new block
164 into the block-tree. If non-NULL, the DECLS are declarations to
165 add to use instead of the BLOCK_VARS in the old block. */
166
167static void
168remap_block (scope_stmt, decls, id)
169 tree scope_stmt;
170 tree decls;
171 inline_data *id;
172{
173 /* We cannot do this in the cleanup for a TARGET_EXPR since we do
174 not know whether or not expand_expr will actually write out the
175 code we put there. If it does not, then we'll have more BLOCKs
176 than block-notes, and things will go awry. At some point, we
177 should make the back-end handle BLOCK notes in a tidier way,
178 without requiring a strict correspondence to the block-tree; then
179 this check can go. */
180 if (id->in_target_cleanup_p)
181 {
182 SCOPE_STMT_BLOCK (scope_stmt) = NULL_TREE;
183 return;
184 }
185
186 /* If this is the beginning of a scope, remap the associated BLOCK. */
187 if (SCOPE_BEGIN_P (scope_stmt) && SCOPE_STMT_BLOCK (scope_stmt))
188 {
189 tree old_block;
190 tree new_block;
191 tree old_var;
a97901e6 192 tree fn;
390f4e9a
MM
193
194 /* Make the new block. */
195 old_block = SCOPE_STMT_BLOCK (scope_stmt);
196 new_block = make_node (BLOCK);
197 TREE_USED (new_block) = TREE_USED (old_block);
198 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
199 SCOPE_STMT_BLOCK (scope_stmt) = new_block;
200
201 /* Remap its variables. */
9c96f3f8
AJ
202 for (old_var = decls ? decls : BLOCK_VARS (old_block);
203 old_var;
390f4e9a
MM
204 old_var = TREE_CHAIN (old_var))
205 {
206 tree new_var;
207
208 /* Remap the variable. */
209 new_var = remap_decl (old_var, id);
db9b2174
MM
210 /* If we didn't remap this variable, so we can't mess with
211 its TREE_CHAIN. If we remapped this variable to
212 something other than a declaration (say, if we mapped it
213 to a constant), then we must similarly omit any mention
214 of it here. */
215 if (!new_var || !DECL_P (new_var))
390f4e9a
MM
216 ;
217 else
218 {
219 TREE_CHAIN (new_var) = BLOCK_VARS (new_block);
220 BLOCK_VARS (new_block) = new_var;
221 }
222 }
223 /* We put the BLOCK_VARS in reverse order; fix that now. */
224 BLOCK_VARS (new_block) = nreverse (BLOCK_VARS (new_block));
a97901e6 225 fn = VARRAY_TREE (id->fns, 0);
b850de4f
MM
226 if (id->cloning_p)
227 /* We're building a clone; DECL_INITIAL is still
228 error_mark_node, and current_binding_level is the parm
229 binding level. */
a901fa64 230 insert_block (new_block);
db9b2174 231 else
a901fa64
JM
232 {
233 /* Attach this new block after the DECL_INITIAL block for the
234 function into which this block is being inlined. In
235 rest_of_compilation we will straighten out the BLOCK tree. */
236 tree *first_block;
237 if (DECL_INITIAL (fn))
238 first_block = &BLOCK_CHAIN (DECL_INITIAL (fn));
239 else
240 first_block = &DECL_INITIAL (fn);
241 BLOCK_CHAIN (new_block) = *first_block;
242 *first_block = new_block;
243 }
390f4e9a
MM
244 /* Remember the remapped block. */
245 splay_tree_insert (id->decl_map,
246 (splay_tree_key) old_block,
247 (splay_tree_value) new_block);
248 }
249 /* If this is the end of a scope, set the SCOPE_STMT_BLOCK to be the
250 remapped block. */
251 else if (SCOPE_END_P (scope_stmt) && SCOPE_STMT_BLOCK (scope_stmt))
252 {
253 splay_tree_node n;
254
255 /* Find this block in the table of remapped things. */
9c96f3f8 256 n = splay_tree_lookup (id->decl_map,
390f4e9a
MM
257 (splay_tree_key) SCOPE_STMT_BLOCK (scope_stmt));
258 my_friendly_assert (n != NULL, 19991203);
259 SCOPE_STMT_BLOCK (scope_stmt) = (tree) n->value;
390f4e9a
MM
260 }
261}
262
263/* Copy the SCOPE_STMT pointed to by TP. */
264
265static void
266copy_scope_stmt (tp, walk_subtrees, id)
267 tree *tp;
268 int *walk_subtrees;
269 inline_data *id;
270{
271 tree block;
272
273 /* Remember whether or not this statement was nullified. When
274 making a copy, copy_tree_r always sets SCOPE_NULLIFIED_P (and
275 doesn't copy the SCOPE_STMT_BLOCK) to free callers from having to
276 deal with copying BLOCKs if they do not wish to do so. */
277 block = SCOPE_STMT_BLOCK (*tp);
278 /* Copy (and replace) the statement. */
279 copy_tree_r (tp, walk_subtrees, NULL);
280 /* Restore the SCOPE_STMT_BLOCK. */
281 SCOPE_STMT_BLOCK (*tp) = block;
282
283 /* Remap the associated block. */
284 remap_block (*tp, NULL_TREE, id);
285}
46e8c075
MM
286
287/* Called from copy_body via walk_tree. DATA is really an
288 `inline_data *'. */
289
290static tree
291copy_body_r (tp, walk_subtrees, data)
292 tree *tp;
293 int *walk_subtrees;
294 void *data;
295{
296 inline_data* id;
297 tree fn;
298
299 /* Set up. */
300 id = (inline_data *) data;
390f4e9a 301 fn = VARRAY_TOP_TREE (id->fns);
46e8c075
MM
302
303 /* All automatic variables should have a DECL_CONTEXT indicating
304 what function they come from. */
305 if ((TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == LABEL_DECL)
306 && DECL_NAMESPACE_SCOPE_P (*tp))
307 my_friendly_assert (DECL_EXTERNAL (*tp) || TREE_STATIC (*tp),
308 19991113);
309
310 /* If this is a RETURN_STMT, change it into an EXPR_STMT and a
311 GOTO_STMT with the RET_LABEL as its target. */
db9b2174 312 if (TREE_CODE (*tp) == RETURN_STMT && id->ret_label)
46e8c075
MM
313 {
314 tree return_stmt = *tp;
315 tree goto_stmt;
316
317 /* Build the GOTO_STMT. */
0dfdeca6 318 goto_stmt = build_stmt (GOTO_STMT, id->ret_label);
46e8c075
MM
319 TREE_CHAIN (goto_stmt) = TREE_CHAIN (return_stmt);
320
321 /* If we're returning something, just turn that into an
9c96f3f8 322 assignment into the equivalent of the original
46e8c075
MM
323 RESULT_DECL. */
324 if (RETURN_EXPR (return_stmt))
325 {
9c96f3f8 326 *tp = build_stmt (EXPR_STMT,
0dfdeca6 327 RETURN_EXPR (return_stmt));
5eec2847 328 STMT_IS_FULL_EXPR_P (*tp) = 1;
46e8c075
MM
329 /* And then jump to the end of the function. */
330 TREE_CHAIN (*tp) = goto_stmt;
331 }
332 /* If we're not returning anything just do the jump. */
333 else
334 *tp = goto_stmt;
335 }
336 /* Local variables and labels need to be replaced by equivalent
337 variables. We don't want to copy static variables; there's only
338 one of those, no matter how many times we inline the containing
339 function. */
340 else if (nonstatic_local_decl_p (*tp) && DECL_CONTEXT (*tp) == fn)
341 {
390f4e9a 342 tree new_decl;
46e8c075 343
390f4e9a
MM
344 /* Remap the declaration. */
345 new_decl = remap_decl (*tp, id);
346 my_friendly_assert (new_decl != NULL_TREE, 19991203);
46e8c075 347 /* Replace this variable with the copy. */
27c58e25 348 STRIP_TYPE_NOPS (new_decl);
390f4e9a 349 *tp = new_decl;
46e8c075 350 }
9c96f3f8 351 else if (nonstatic_local_decl_p (*tp)
ef97bf6b
MM
352 && DECL_CONTEXT (*tp) != VARRAY_TREE (id->fns, 0))
353 my_friendly_abort (0);
46e8c075 354 else if (TREE_CODE (*tp) == SAVE_EXPR)
9c96f3f8 355 remap_save_expr (tp, id->decl_map, VARRAY_TREE (id->fns, 0),
d7d5e42f 356 walk_subtrees);
46e8c075 357 else if (TREE_CODE (*tp) == UNSAVE_EXPR)
da4ac6f8 358 /* UNSAVE_EXPRs should not be generated until expansion time. */
46e8c075 359 my_friendly_abort (19991113);
390f4e9a
MM
360 /* For a SCOPE_STMT, we must copy the associated block so that we
361 can write out debugging information for the inlined variables. */
362 else if (TREE_CODE (*tp) == SCOPE_STMT && !id->in_target_cleanup_p)
363 copy_scope_stmt (tp, walk_subtrees, id);
46e8c075
MM
364 /* Otherwise, just copy the node. Note that copy_tree_r already
365 knows not to copy VAR_DECLs, etc., so this is safe. */
366 else
367 {
368 copy_tree_r (tp, walk_subtrees, NULL);
369
370 /* The copied TARGET_EXPR has never been expanded, even if the
371 original node was expanded already. */
372 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
ef97bf6b
MM
373 {
374 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
375 TREE_OPERAND (*tp, 3) = NULL_TREE;
376 }
6001735e
JJ
377 else if (TREE_CODE (*tp) == MODIFY_EXPR
378 && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
379 && nonstatic_local_decl_p (TREE_OPERAND (*tp, 0))
380 && DECL_CONTEXT (TREE_OPERAND (*tp, 0)) == fn)
381 {
64b9f199
JJ
382 /* Some assignments VAR = VAR; don't generate any rtl code
383 and thus don't count as variable modification. Avoid
6001735e
JJ
384 keeping bogosities like 0 = 0. */
385 tree decl = TREE_OPERAND (*tp, 0), value;
386 splay_tree_node n;
387
388 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
389 if (n)
390 {
391 value = (tree) n->value;
392 STRIP_TYPE_NOPS (value);
393 if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value))
394 *tp = value;
395 }
396 }
46e8c075
MM
397 }
398
399 /* Keep iterating. */
400 return NULL_TREE;
401}
402
403/* Make a copy of the body of FN so that it can be inserted inline in
404 another function. */
405
406static tree
407copy_body (id)
408 inline_data *id;
409{
410 tree body;
411
390f4e9a 412 body = DECL_SAVED_TREE (VARRAY_TOP_TREE (id->fns));
ee94fce6 413 walk_tree (&body, copy_body_r, id, NULL);
46e8c075
MM
414
415 return body;
416}
417
418/* Generate code to initialize the parameters of the function at the
419 top of the stack in ID from the ARGS (presented as a TREE_LIST). */
420
421static tree
92e8164f 422initialize_inlined_parameters (id, args, fn)
46e8c075
MM
423 inline_data *id;
424 tree args;
92e8164f 425 tree fn;
46e8c075 426{
46e8c075
MM
427 tree init_stmts;
428 tree parms;
429 tree a;
430 tree p;
431
432 /* Figure out what the parameters are. */
46e8c075
MM
433 parms = DECL_ARGUMENTS (fn);
434
435 /* Start with no initializations whatsoever. */
436 init_stmts = NULL_TREE;
437
438 /* Loop through the parameter declarations, replacing each with an
439 equivalent VAR_DECL, appropriately initialized. */
440 for (p = parms, a = args; p; a = TREE_CHAIN (a), p = TREE_CHAIN (p))
441 {
442 tree init_stmt;
443 tree var;
27c58e25 444 tree value;
9c96f3f8 445
27c58e25
MM
446 /* Find the initializer. */
447 value = TREE_VALUE (a);
448 /* If the parameter is never assigned to, we may not need to
449 create a new variable here at all. Instead, we may be able
450 to just use the argument value. */
9c96f3f8 451 if (TREE_READONLY (p)
04dfcc8f
MM
452 && !TREE_ADDRESSABLE (p)
453 && !TREE_SIDE_EFFECTS (value))
27c58e25
MM
454 {
455 /* Simplify the value, if possible. */
456 value = fold (decl_constant_value (value));
9c96f3f8 457
27c58e25
MM
458 /* We can't risk substituting complex expressions. They
459 might contain variables that will be assigned to later.
460 Theoretically, we could check the expression to see if
461 all of the variables that determine its value are
462 read-only, but we don't bother. */
463 if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value))
464 {
465 /* If this is a declaration, wrap it a NOP_EXPR so that
466 we don't try to put the VALUE on the list of
467 BLOCK_VARS. */
468 if (DECL_P (value))
469 value = build1 (NOP_EXPR, TREE_TYPE (value), value);
470
471 splay_tree_insert (id->decl_map,
472 (splay_tree_key) p,
473 (splay_tree_value) value);
474 continue;
475 }
476 }
9c96f3f8 477
46e8c075
MM
478 /* Make an equivalent VAR_DECL. */
479 var = copy_decl_for_inlining (p, fn, VARRAY_TREE (id->fns, 0));
480 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
481 that way, when the PARM_DECL is encountered, it will be
482 automatically replaced by the VAR_DECL. */
9c96f3f8 483 splay_tree_insert (id->decl_map,
46e8c075
MM
484 (splay_tree_key) p,
485 (splay_tree_value) var);
5062dbd5
JM
486
487 /* Declare this new variable. */
0dfdeca6 488 init_stmt = build_stmt (DECL_STMT, var);
5062dbd5
JM
489 TREE_CHAIN (init_stmt) = init_stmts;
490 init_stmts = init_stmt;
491
46e8c075
MM
492 /* Initialize this VAR_DECL from the equivalent argument. If
493 the argument is an object, created via a constructor or copy,
494 this will not result in an extra copy: the TARGET_EXPR
495 representing the argument will be bound to VAR, and the
496 object will be constructed in VAR. */
5062dbd5
JM
497 if (! TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
498 DECL_INITIAL (var) = value;
499 else
500 {
0dfdeca6
BC
501 init_stmt = build_stmt (EXPR_STMT,
502 build (INIT_EXPR, TREE_TYPE (p),
503 var, value));
5062dbd5
JM
504 /* Add this initialization to the list. Note that we want the
505 declaration *after* the initialization because we are going
506 to reverse all the initialization statements below. */
507 TREE_CHAIN (init_stmt) = init_stmts;
508 init_stmts = init_stmt;
509 }
46e8c075
MM
510 }
511
512 /* The initialization statements have been built up in reverse
513 order. Straighten them out now. */
514 return nreverse (init_stmts);
515}
516
517/* Declare a return variable to replace the RESULT_DECL for the
518 function we are calling. An appropriate DECL_STMT is returned.
519 The USE_STMT is filled in to contain a use of the declaration to
520 indicate the return value of the function. */
521
522static tree
523declare_return_variable (id, use_stmt)
524 struct inline_data *id;
525 tree *use_stmt;
526{
390f4e9a 527 tree fn = VARRAY_TOP_TREE (id->fns);
46e8c075
MM
528 tree result = DECL_RESULT (fn);
529 tree var;
738e2810 530 int aggregate_return_p;
46e8c075
MM
531
532 /* We don't need to do anything for functions that don't return
533 anything. */
b72801e2 534 if (!result || VOID_TYPE_P (TREE_TYPE (result)))
46e8c075
MM
535 {
536 *use_stmt = NULL_TREE;
537 return NULL_TREE;
538 }
539
738e2810
MM
540 /* Figure out whether or not FN returns an aggregate. */
541 aggregate_return_p = IS_AGGR_TYPE (TREE_TYPE (result));
542
543 /* If FN returns an aggregate then the caller will always create the
544 temporary (using a TARGET_EXPR) and the call will be the
545 initializing expression for the TARGET_EXPR. If we were just to
546 create a new VAR_DECL here, then the result of this function
547 would be copied (bitwise) into the variable initialized by the
548 TARGET_EXPR. That's incorrect, so we must transform any
549 references to the RESULT into references to the target. */
550 if (aggregate_return_p)
551 {
b3bae5e2 552 my_friendly_assert (VARRAY_ACTIVE_SIZE (id->target_exprs) != 0,
738e2810
MM
553 20000430);
554 var = TREE_OPERAND (VARRAY_TOP_TREE (id->target_exprs), 0);
9c96f3f8
AJ
555 my_friendly_assert
556 (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (var),
9edc3913
MM
557 TREE_TYPE (result)),
558 20000430);
738e2810
MM
559 }
560 /* Otherwise, make an appropriate copy. */
561 else
562 var = copy_decl_for_inlining (result, fn, VARRAY_TREE (id->fns, 0));
563
46e8c075
MM
564 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
565 way, when the RESULT_DECL is encountered, it will be
566 automatically replaced by the VAR_DECL. */
9c96f3f8 567 splay_tree_insert (id->decl_map,
46e8c075
MM
568 (splay_tree_key) result,
569 (splay_tree_value) var);
570
571 /* Build the USE_STMT. */
0dfdeca6 572 *use_stmt = build_stmt (EXPR_STMT, var);
46e8c075 573
738e2810
MM
574 /* Build the declaration statement if FN does not return an
575 aggregate. */
576 if (!aggregate_return_p)
0dfdeca6 577 return build_stmt (DECL_STMT, var);
738e2810
MM
578 /* If FN does return an aggregate, there's no need to declare the
579 return variable; we're using a variable in our caller's frame. */
580 else
581 return NULL_TREE;
46e8c075
MM
582}
583
584/* Returns non-zero if FN is a function that can be inlined. */
585
586static int
587inlinable_function_p (fn, id)
588 tree fn;
589 inline_data *id;
590{
591 int inlinable;
592
593 /* If we've already decided this function shouldn't be inlined,
594 there's no need to check again. */
595 if (DECL_UNINLINABLE (fn))
596 return 0;
597
598 /* Assume it is not inlinable. */
599 inlinable = 0;
600
390f4e9a
MM
601 /* If we're not inlining things, then nothing is inlinable. */
602 if (!flag_inline_trees)
603 ;
46e8c075
MM
604 /* If the function was not declared `inline', then we don't inline
605 it. */
390f4e9a 606 else if (!DECL_INLINE (fn))
46e8c075 607 ;
46e8c075
MM
608 /* We can't inline varargs functions. */
609 else if (varargs_function_p (fn))
610 ;
b850de4f
MM
611 /* We can't inline functions that are too big. */
612 else if (DECL_NUM_STMTS (fn) * INSNS_PER_STMT > MAX_INLINE_INSNS)
613 ;
46e8c075 614 /* All is well. We can inline this function. Traditionally, GCC
89a75b6d
JM
615 has refused to inline functions using alloca, or functions whose
616 values are returned in a PARALLEL, and a few other such obscure
617 conditions. We are not equally constrained at the tree level. */
46e8c075
MM
618 else
619 inlinable = 1;
620
621 /* Squirrel away the result so that we don't have to check again. */
622 DECL_UNINLINABLE (fn) = !inlinable;
623
b850de4f
MM
624 /* Even if this function is not itself too big to inline, it might
625 be that we've done so much inlining already that we don't want to
626 risk inlining any more. */
627 if ((DECL_NUM_STMTS (fn) + id->inlined_stmts) * INSNS_PER_STMT
628 > MAX_INLINE_INSNS)
629 inlinable = 0;
630
16d53b64
MM
631 /* We can inline a template instantiation only if it's fully
632 instantiated. */
9c96f3f8
AJ
633 if (inlinable
634 && DECL_TEMPLATE_INFO (fn)
16d53b64
MM
635 && TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (fn)))
636 {
637 fn = instantiate_decl (fn, /*defer_ok=*/0);
638 inlinable = !TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (fn));
639 }
640
641 /* If we don't have the function body available, we can't inline
642 it. */
643 if (!DECL_SAVED_TREE (fn))
644 inlinable = 0;
645
46e8c075 646 /* Don't do recursive inlining, either. We don't record this in
b3bae5e2 647 DECL_UNINLINABLE; we may be able to inline this function later. */
46e8c075
MM
648 if (inlinable)
649 {
650 size_t i;
651
b3bae5e2 652 for (i = 0; i < VARRAY_ACTIVE_SIZE (id->fns); ++i)
46e8c075 653 if (VARRAY_TREE (id->fns, i) == fn)
b3bae5e2
JJ
654 return 0;
655
656 if (inlinable && DECL_LANG_SPECIFIC (fn) && DECL_INLINED_FNS (fn))
657 {
8e4ce833
JJ
658 int j;
659 tree inlined_fns = DECL_INLINED_FNS (fn);
b3bae5e2 660
8e4ce833
JJ
661 for (j = 0; j < TREE_VEC_LENGTH (inlined_fns); ++j)
662 if (TREE_VEC_ELT (inlined_fns, j) == VARRAY_TREE (id->fns, 0))
b3bae5e2
JJ
663 return 0;
664 }
46e8c075
MM
665 }
666
46e8c075
MM
667 /* Return the result. */
668 return inlinable;
669}
670
8890f3d8 671/* If *TP is a CALL_EXPR, replace it with its inline expansion. */
46e8c075
MM
672
673static tree
674expand_call_inline (tp, walk_subtrees, data)
675 tree *tp;
676 int *walk_subtrees;
677 void *data;
678{
679 inline_data *id;
680 tree t;
681 tree expr;
682 tree chain;
683 tree fn;
390f4e9a 684 tree scope_stmt;
46e8c075 685 tree use_stmt;
92e8164f 686 tree arg_inits;
071f9809 687 tree *inlined_body;
46e8c075
MM
688 splay_tree st;
689
390f4e9a
MM
690 /* See what we've got. */
691 id = (inline_data *) data;
9c96f3f8 692 t = *tp;
390f4e9a 693
390f4e9a
MM
694 /* Recurse, but letting recursive invocations know that we are
695 inside the body of a TARGET_EXPR. */
696 if (TREE_CODE (*tp) == TARGET_EXPR)
697 {
5afb79e7 698 int i, len = first_rtl_op (TARGET_EXPR);
390f4e9a
MM
699
700 /* We're walking our own subtrees. */
701 *walk_subtrees = 0;
702
738e2810
MM
703 /* Push *TP on the stack of pending TARGET_EXPRs. */
704 VARRAY_PUSH_TREE (id->target_exprs, *tp);
705
390f4e9a
MM
706 /* Actually walk over them. This loop is the body of
707 walk_trees, omitting the case where the TARGET_EXPR
708 itself is handled. */
5afb79e7 709 for (i = 0; i < len; ++i)
390f4e9a
MM
710 {
711 if (i == 2)
712 ++id->in_target_cleanup_p;
ee94fce6 713 walk_tree (&TREE_OPERAND (*tp, i), expand_call_inline, data,
11fe225a 714 id->tree_pruner);
390f4e9a
MM
715 if (i == 2)
716 --id->in_target_cleanup_p;
717 }
718
738e2810
MM
719 /* We're done with this TARGET_EXPR now. */
720 VARRAY_POP (id->target_exprs);
721
390f4e9a
MM
722 return NULL_TREE;
723 }
724
46ccf50a 725 if (TYPE_P (t))
612164eb
KL
726 /* Because types were not copied in copy_body, CALL_EXPRs beneath
727 them should not be expanded. This can happen if the type is a
728 dynamic array type, for example. */
729 *walk_subtrees = 0;
730
390f4e9a 731 /* From here on, we're only interested in CALL_EXPRs. */
46e8c075
MM
732 if (TREE_CODE (t) != CALL_EXPR)
733 return NULL_TREE;
734
735 /* First, see if we can figure out what function is being called.
736 If we cannot, then there is no hope of inlining the function. */
737 fn = get_callee_fndecl (t);
738 if (!fn)
739 return NULL_TREE;
740
741 /* Don't try to inline functions that are not well-suited to
742 inlining. */
46e8c075
MM
743 if (!inlinable_function_p (fn, id))
744 return NULL_TREE;
745
574a0ef5
MM
746 /* Set the current filename and line number to the function we are
747 inlining so that when we create new _STMT nodes here they get
748 line numbers corresponding to the function we are calling. We
749 wrap the whole inlined body in an EXPR_WITH_FILE_AND_LINE as well
750 because individual statements don't record the filename. */
751 push_srcloc (fn->decl.filename, fn->decl.linenum);
752
46e8c075
MM
753 /* Build a statement-expression containing code to initialize the
754 arguments, the actual inline expansion of the body, and a label
755 for the return statements within the function to jump to. The
756 type of the statement expression is the return type of the
757 function call. */
758 expr = build_min (STMT_EXPR, TREE_TYPE (TREE_TYPE (fn)), NULL_TREE);
759
46e8c075
MM
760 /* Local declarations will be replaced by their equivalents in this
761 map. */
762 st = id->decl_map;
763 id->decl_map = splay_tree_new (splay_tree_compare_pointers,
764 NULL, NULL);
765
766 /* Initialize the parameters. */
92e8164f
MM
767 arg_inits = initialize_inlined_parameters (id, TREE_OPERAND (t, 1), fn);
768 /* Expand any inlined calls in the initializers. Do this before we
769 push FN on the stack of functions we are inlining; we want to
770 inline calls to FN that appear in the initializers for the
771 parameters. */
772 expand_calls_inline (&arg_inits, id);
773 /* And add them to the tree. */
774 STMT_EXPR_STMT (expr) = chainon (STMT_EXPR_STMT (expr), arg_inits);
775
776 /* Record the function we are about to inline so that we can avoid
777 recursing into it. */
778 VARRAY_PUSH_TREE (id->fns, fn);
779
b3bae5e2
JJ
780 /* Record the function we are about to inline if optimize_function
781 has not been called on it yet and we don't have it in the list. */
782 if (DECL_LANG_SPECIFIC (fn) && !DECL_INLINED_FNS (fn))
783 {
784 int i;
785
786 for (i = VARRAY_ACTIVE_SIZE (id->inlined_fns) - 1; i >= 0; i--)
787 if (VARRAY_TREE (id->inlined_fns, i) == fn)
788 break;
789 if (i < 0)
790 VARRAY_PUSH_TREE (id->inlined_fns, fn);
791 }
792
92e8164f
MM
793 /* Return statements in the function body will be replaced by jumps
794 to the RET_LABEL. */
795 id->ret_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
796 DECL_CONTEXT (id->ret_label) = VARRAY_TREE (id->fns, 0);
797
390f4e9a
MM
798 /* Create a block to put the parameters in. We have to do this
799 after the parameters have been remapped because remapping
800 parameters is different from remapping ordinary variables. */
0dfdeca6 801 scope_stmt = build_stmt (SCOPE_STMT, DECL_INITIAL (fn));
390f4e9a
MM
802 SCOPE_BEGIN_P (scope_stmt) = 1;
803 SCOPE_NO_CLEANUPS_P (scope_stmt) = 1;
804 remap_block (scope_stmt, DECL_ARGUMENTS (fn), id);
805 TREE_CHAIN (scope_stmt) = STMT_EXPR_STMT (expr);
806 STMT_EXPR_STMT (expr) = scope_stmt;
46e8c075 807
8cb360d3 808 /* Tell the debugging backends that this block represents the
a97901e6 809 outermost scope of the inlined function. */
8cb360d3
JM
810 if (SCOPE_STMT_BLOCK (scope_stmt))
811 BLOCK_ABSTRACT_ORIGIN (SCOPE_STMT_BLOCK (scope_stmt)) = DECL_ORIGIN (fn);
812
46e8c075
MM
813 /* Declare the return variable for the function. */
814 STMT_EXPR_STMT (expr)
9c96f3f8 815 = chainon (STMT_EXPR_STMT (expr),
46e8c075 816 declare_return_variable (id, &use_stmt));
9c96f3f8 817
46e8c075
MM
818 /* After we've initialized the parameters, we insert the body of the
819 function itself. */
071f9809
MM
820 inlined_body = &STMT_EXPR_STMT (expr);
821 while (*inlined_body)
822 inlined_body = &TREE_CHAIN (*inlined_body);
823 *inlined_body = copy_body (id);
46e8c075 824
390f4e9a 825 /* Close the block for the parameters. */
0dfdeca6 826 scope_stmt = build_stmt (SCOPE_STMT, DECL_INITIAL (fn));
390f4e9a 827 SCOPE_NO_CLEANUPS_P (scope_stmt) = 1;
9c96f3f8 828 my_friendly_assert (DECL_INITIAL (fn)
390f4e9a
MM
829 && TREE_CODE (DECL_INITIAL (fn)) == BLOCK,
830 19991203);
831 remap_block (scope_stmt, NULL_TREE, id);
832 STMT_EXPR_STMT (expr)
833 = chainon (STMT_EXPR_STMT (expr), scope_stmt);
834
3431f196
JW
835 /* After the body of the function comes the RET_LABEL. This must come
836 before we evaluate the returned value below, because that evalulation
837 may cause RTL to be generated. */
838 STMT_EXPR_STMT (expr)
9c96f3f8 839 = chainon (STMT_EXPR_STMT (expr),
0dfdeca6 840 build_stmt (LABEL_STMT, id->ret_label));
3431f196 841
46e8c075
MM
842 /* Finally, mention the returned value so that the value of the
843 statement-expression is the returned value of the function. */
844 STMT_EXPR_STMT (expr) = chainon (STMT_EXPR_STMT (expr), use_stmt);
845
846 /* Clean up. */
847 splay_tree_delete (id->decl_map);
848 id->decl_map = st;
849
46e8c075
MM
850 /* The new expression has side-effects if the old one did. */
851 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (t);
5679f678
MM
852
853 /* Replace the call by the inlined body. Wrap it in an
854 EXPR_WITH_FILE_LOCATION so that we'll get debugging line notes
855 pointing to the right place. */
856 chain = TREE_CHAIN (*tp);
857 *tp = build_expr_wfl (expr, DECL_SOURCE_FILE (fn), DECL_SOURCE_LINE (fn),
858 /*col=*/0);
859 EXPR_WFL_EMIT_LINE_NOTE (*tp) = 1;
860 TREE_CHAIN (*tp) = chain;
574a0ef5 861 pop_srcloc ();
5679f678 862
46e8c075
MM
863 /* If the value of the new expression is ignored, that's OK. We
864 don't warn about this for CALL_EXPRs, so we shouldn't warn about
865 the equivalent inlined version either. */
5679f678 866 TREE_USED (*tp) = 1;
46e8c075 867
b850de4f
MM
868 /* Our function now has more statements than it did before. */
869 DECL_NUM_STMTS (VARRAY_TREE (id->fns, 0)) += DECL_NUM_STMTS (fn);
e59d6709 870 id->inlined_stmts += DECL_NUM_STMTS (fn);
b850de4f 871
46e8c075 872 /* Recurse into the body of the just inlined function. */
071f9809 873 expand_calls_inline (inlined_body, id);
390f4e9a 874 VARRAY_POP (id->fns);
46e8c075 875
b850de4f
MM
876 /* If we've returned to the top level, clear out the record of how
877 much inlining has been done. */
878 if (VARRAY_ACTIVE_SIZE (id->fns) == id->first_inlined_fn)
879 id->inlined_stmts = 0;
880
46e8c075
MM
881 /* Don't walk into subtrees. We've already handled them above. */
882 *walk_subtrees = 0;
883
884 /* Keep iterating. */
885 return NULL_TREE;
886}
887
888/* Walk over the entire tree *TP, replacing CALL_EXPRs with inline
889 expansions as appropriate. */
890
891static void
892expand_calls_inline (tp, id)
893 tree *tp;
894 inline_data *id;
895{
896 /* Search through *TP, replacing all calls to inline functions by
11fe225a
ZW
897 appropriate equivalents. Use walk_tree in no-duplicates mode
898 to avoid exponential time complexity. (We can't just use
899 walk_tree_without_duplicates, because of the special TARGET_EXPR
900 handling in expand_calls. The hash table is set up in
901 optimize_function. */
902 walk_tree (tp, expand_call_inline, id, id->tree_pruner);
46e8c075
MM
903}
904
905/* Optimize the body of FN. */
906
907void
908optimize_function (fn)
909 tree fn;
910{
b2244c65
MM
911 /* While in this function, we may choose to go off and compile
912 another function. For example, we might instantiate a function
913 in the hopes of inlining it. Normally, that wouldn't trigger any
914 actual RTL code-generation -- but it will if the template is
915 actually needed. (For example, if it's address is taken, or if
916 some other function already refers to the template.) If
917 code-generation occurs, then garbage collection will occur, so we
918 must protect ourselves, just as we do while building up the body
919 of the function. */
920 ++function_depth;
921
46e8c075
MM
922 /* Expand calls to inline functions. */
923 if (flag_inline_trees)
924 {
925 inline_data id;
390f4e9a
MM
926 tree prev_fn;
927 struct saved_scope *s;
46e8c075
MM
928
929 /* Clear out ID. */
486837a7 930 memset (&id, 0, sizeof (id));
46e8c075
MM
931
932 /* Don't allow recursion into FN. */
933 VARRAY_TREE_INIT (id.fns, 32, "fns");
390f4e9a
MM
934 VARRAY_PUSH_TREE (id.fns, fn);
935 /* Or any functions that aren't finished yet. */
936 prev_fn = NULL_TREE;
937 if (current_function_decl)
938 {
939 VARRAY_PUSH_TREE (id.fns, current_function_decl);
940 prev_fn = current_function_decl;
941 }
942 for (s = scope_chain; s; s = s->prev)
943 if (s->function_decl && s->function_decl != prev_fn)
944 {
945 VARRAY_PUSH_TREE (id.fns, s->function_decl);
946 prev_fn = s->function_decl;
947 }
5afb79e7 948
738e2810
MM
949 /* Create the stack of TARGET_EXPRs. */
950 VARRAY_TREE_INIT (id.target_exprs, 32, "target_exprs");
951
b3bae5e2
JJ
952 /* Create the list of functions this call will inline. */
953 VARRAY_TREE_INIT (id.inlined_fns, 32, "inlined_fns");
954
b850de4f
MM
955 /* Keep track of the low-water mark, i.e., the point where
956 the first real inlining is represented in ID.FNS. */
957 id.first_inlined_fn = VARRAY_ACTIVE_SIZE (id.fns);
958
46e8c075
MM
959 /* Replace all calls to inline functions with the bodies of those
960 functions. */
11fe225a
ZW
961 id.tree_pruner = htab_create (37, htab_hash_pointer,
962 htab_eq_pointer, NULL);
46e8c075
MM
963 expand_calls_inline (&DECL_SAVED_TREE (fn), &id);
964
965 /* Clean up. */
11fe225a 966 htab_delete (id.tree_pruner);
46e8c075 967 VARRAY_FREE (id.fns);
738e2810 968 VARRAY_FREE (id.target_exprs);
b3bae5e2
JJ
969 if (DECL_LANG_SPECIFIC (fn))
970 {
8e4ce833
JJ
971 tree ifn = make_tree_vec (VARRAY_ACTIVE_SIZE (id.inlined_fns));
972
973 memcpy (&TREE_VEC_ELT (ifn, 0), &VARRAY_TREE (id.inlined_fns, 0),
974 VARRAY_ACTIVE_SIZE (id.inlined_fns) * sizeof (tree));
b3bae5e2
JJ
975 DECL_INLINED_FNS (fn) = ifn;
976 }
977 VARRAY_FREE (id.inlined_fns);
46e8c075 978 }
b2244c65
MM
979
980 /* Undo the call to ggc_push_context above. */
981 --function_depth;
46e8c075 982}
95fabfd3
MM
983
984/* Called from calls_setjmp_p via walk_tree. */
985
986static tree
987calls_setjmp_r (tp, walk_subtrees, data)
988 tree *tp;
989 int *walk_subtrees ATTRIBUTE_UNUSED;
990 void *data ATTRIBUTE_UNUSED;
991{
95fabfd3
MM
992 /* We're only interested in FUNCTION_DECLS. */
993 if (TREE_CODE (*tp) != FUNCTION_DECL)
994 return NULL_TREE;
995
c9fff01f 996 return setjmp_call_p (*tp) ? *tp : NULL_TREE;
95fabfd3
MM
997}
998
999/* Returns non-zero if FN calls `setjmp' or some other function that
1000 can return more than once. This function is conservative; it may
1001 occasionally return a non-zero value even when FN does not actually
1002 call `setjmp'. */
1003
1004int
1005calls_setjmp_p (fn)
1006 tree fn;
1007{
9c96f3f8
AJ
1008 return walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
1009 calls_setjmp_r,
ee94fce6 1010 NULL) != NULL_TREE;
95fabfd3
MM
1011}
1012
d60e5448
MM
1013/* CLONED_PARM is a copy of CLONE, generated for a cloned constructor
1014 or destructor. Update it to ensure that the source-position for
1015 the cloned parameter matches that for the original, and that the
1016 debugging generation code will be able to find the original PARM. */
1017
1018static void
1019update_cloned_parm (parm, cloned_parm)
1020 tree parm;
1021 tree cloned_parm;
1022{
1023 DECL_ABSTRACT_ORIGIN (cloned_parm) = parm;
1024
1025 /* The name may have changed from the declaration. */
1026 DECL_NAME (cloned_parm) = DECL_NAME (parm);
1027 DECL_SOURCE_FILE (cloned_parm) = DECL_SOURCE_FILE (parm);
1028 DECL_SOURCE_LINE (cloned_parm) = DECL_SOURCE_LINE (parm);
1029
1030}
1031
db9b2174
MM
1032/* FN is a function that has a complete body. Clone the body as
1033 necessary. Returns non-zero if there's no longer any need to
1034 process the main body. */
1035
1036int
1037maybe_clone_body (fn)
1038 tree fn;
1039{
1040 inline_data id;
1041 tree clone;
02a1a68c 1042 int first = 1;
db9b2174 1043
db9b2174
MM
1044 /* We only clone constructors and destructors. */
1045 if (!DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
1046 && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
1047 return 0;
1048
5daf7c0a
JM
1049 /* Emit the DWARF1 abstract instance. */
1050 note_deferral_of_defined_inline_function (fn);
1051
db9b2174
MM
1052 /* We know that any clones immediately follow FN in the TYPE_METHODS
1053 list. */
1054 for (clone = TREE_CHAIN (fn);
1055 clone && DECL_CLONED_FUNCTION_P (clone);
02a1a68c 1056 clone = TREE_CHAIN (clone), first = 0)
db9b2174
MM
1057 {
1058 tree parm;
1059 tree clone_parm;
1060 int parmno;
1061
1062 /* Update CLONE's source position information to match FN's. */
1063 DECL_SOURCE_FILE (clone) = DECL_SOURCE_FILE (fn);
1064 DECL_SOURCE_LINE (clone) = DECL_SOURCE_LINE (fn);
99389463 1065 DECL_INLINE (clone) = DECL_INLINE (fn);
79065db2 1066 DECL_DECLARED_INLINE_P (clone) = DECL_DECLARED_INLINE_P (fn);
3ec6bad3
MM
1067 DECL_COMDAT (clone) = DECL_COMDAT (fn);
1068 DECL_WEAK (clone) = DECL_WEAK (fn);
1069 DECL_ONE_ONLY (clone) = DECL_ONE_ONLY (fn);
1070 DECL_SECTION_NAME (clone) = DECL_SECTION_NAME (fn);
459c43ad
MM
1071 DECL_USE_TEMPLATE (clone) = DECL_USE_TEMPLATE (fn);
1072 DECL_EXTERNAL (clone) = DECL_EXTERNAL (fn);
1073 DECL_INTERFACE_KNOWN (clone) = DECL_INTERFACE_KNOWN (fn);
1074 DECL_NOT_REALLY_EXTERN (clone) = DECL_NOT_REALLY_EXTERN (fn);
b96ada87 1075 TREE_PUBLIC (clone) = TREE_PUBLIC (fn);
db9b2174 1076
02a1a68c
NS
1077 /* Adjust the parameter names and locations. */
1078 parm = DECL_ARGUMENTS (fn);
1079 clone_parm = DECL_ARGUMENTS (clone);
d60e5448
MM
1080 /* Update the `this' parameter, which is always first.
1081 Sometimes, we end update the `this' parameter twice because
1082 we process it again in the loop below. That is harmless. */
1083 update_cloned_parm (parm, clone_parm);
02a1a68c
NS
1084 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1085 parm = TREE_CHAIN (parm);
1086 if (DECL_HAS_VTT_PARM_P (fn))
1087 parm = TREE_CHAIN (parm);
1088 if (DECL_HAS_VTT_PARM_P (clone))
1089 clone_parm = TREE_CHAIN (clone_parm);
1090 for (; parm;
1091 parm = TREE_CHAIN (parm), clone_parm = TREE_CHAIN (clone_parm))
1092 {
d60e5448
MM
1093 /* Update this paramter. */
1094 update_cloned_parm (parm, clone_parm);
02a1a68c
NS
1095 /* We should only give unused information for one clone. */
1096 if (!first)
1097 TREE_USED (clone_parm) = 1;
1098 }
1099
db9b2174
MM
1100 /* Start processing the function. */
1101 push_to_top_level ();
1102 start_function (NULL_TREE, clone, NULL_TREE, SF_PRE_PARSED);
db9b2174
MM
1103
1104 /* Just clone the body, as if we were making an inline call.
1105 But, remap the parameters in the callee to the parameters of
1106 caller. If there's an in-charge parameter, map it to an
1107 appropriate constant. */
1108 memset (&id, 0, sizeof (id));
1109 VARRAY_TREE_INIT (id.fns, 2, "fns");
1110 VARRAY_PUSH_TREE (id.fns, clone);
1111 VARRAY_PUSH_TREE (id.fns, fn);
1112
b850de4f
MM
1113 /* Cloning is treated slightly differently from inlining. Set
1114 CLONING_P so that its clear which operation we're performing. */
1115 id.cloning_p = true;
1116
db9b2174
MM
1117 /* Remap the parameters. */
1118 id.decl_map = splay_tree_new (splay_tree_compare_pointers,
1119 NULL, NULL);
1120 for (parmno = 0,
1121 parm = DECL_ARGUMENTS (fn),
1122 clone_parm = DECL_ARGUMENTS (clone);
1123 parm;
1124 ++parmno,
1125 parm = TREE_CHAIN (parm))
1126 {
1127 /* Map the in-charge parameter to an appropriate constant. */
1128 if (DECL_HAS_IN_CHARGE_PARM_P (fn) && parmno == 1)
1129 {
1130 tree in_charge;
298d6f60 1131 in_charge = in_charge_arg_for_name (DECL_NAME (clone));
db9b2174
MM
1132 splay_tree_insert (id.decl_map,
1133 (splay_tree_key) parm,
3ec6bad3 1134 (splay_tree_value) in_charge);
e0fff4b3
JM
1135 }
1136 else if (DECL_ARTIFICIAL (parm)
1137 && DECL_NAME (parm) == vtt_parm_identifier)
1138 {
3ec6bad3
MM
1139 /* For a subobject constructor or destructor, the next
1140 argument is the VTT parameter. Remap the VTT_PARM
1141 from the CLONE to this parameter. */
e0fff4b3 1142 if (DECL_HAS_VTT_PARM_P (clone))
3ec6bad3 1143 {
5daf7c0a 1144 DECL_ABSTRACT_ORIGIN (clone_parm) = parm;
3ec6bad3 1145 splay_tree_insert (id.decl_map,
e0fff4b3 1146 (splay_tree_key) parm,
3ec6bad3 1147 (splay_tree_value) clone_parm);
3ec6bad3
MM
1148 clone_parm = TREE_CHAIN (clone_parm);
1149 }
1150 /* Otherwise, map the VTT parameter to `NULL'. */
e0fff4b3 1151 else
3ec6bad3
MM
1152 {
1153 splay_tree_insert (id.decl_map,
e0fff4b3 1154 (splay_tree_key) parm,
3ec6bad3 1155 (splay_tree_value) null_pointer_node);
3ec6bad3 1156 }
db9b2174
MM
1157 }
1158 /* Map other parameters to their equivalents in the cloned
1159 function. */
1160 else
1161 {
1162 splay_tree_insert (id.decl_map,
1163 (splay_tree_key) parm,
1164 (splay_tree_value) clone_parm);
1165 clone_parm = TREE_CHAIN (clone_parm);
1166 }
1167 }
1168
1169 /* Actually copy the body. */
1170 TREE_CHAIN (DECL_SAVED_TREE (clone)) = copy_body (&id);
1171
9b7949d5
MM
1172 /* There are as many statements in the clone as in the
1173 original. */
1174 DECL_NUM_STMTS (clone) = DECL_NUM_STMTS (fn);
1175
db9b2174
MM
1176 /* Clean up. */
1177 splay_tree_delete (id.decl_map);
1178 VARRAY_FREE (id.fns);
1179
1180 /* Now, expand this function into RTL, if appropriate. */
5daf7c0a
JM
1181 finish_function (0);
1182 BLOCK_ABSTRACT_ORIGIN (DECL_INITIAL (clone)) = DECL_INITIAL (fn);
1183 expand_body (clone);
db9b2174
MM
1184 pop_from_top_level ();
1185 }
9c96f3f8 1186
db9b2174
MM
1187 /* We don't need to process the original function any further. */
1188 return 1;
1189}