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