8893b26236ab24b39aece29db00d76a2938c7e88
[gcc.git] / gcc / tree-inline.c
1 /* Control and data flow functions for trees.
2 Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
3 Contributed by Alexandre Oliva <aoliva@redhat.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "toplev.h"
27 #include "tree.h"
28 #include "tree-inline.h"
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"
35 #include "integrate.h"
36 #include "varray.h"
37 #include "hashtab.h"
38 #include "splay-tree.h"
39 #include "langhooks.h"
40 #include "cgraph.h"
41
42 /* This should be eventually be generalized to other languages, but
43 this would require a shared function-as-trees infrastructure. */
44 #ifndef INLINER_FOR_JAVA
45 #include "c-common.h"
46 #else /* INLINER_FOR_JAVA */
47 #include "parse.h"
48 #include "java-tree.h"
49 #endif /* INLINER_FOR_JAVA */
50
51 /* 0 if we should not perform inlining.
52 1 if we should expand functions calls inline at the tree level.
53 2 if we should consider *all* functions to be inline
54 candidates. */
55
56 int flag_inline_trees = 0;
57
58 /* To Do:
59
60 o In order to make inlining-on-trees work, we pessimized
61 function-local static constants. In particular, they are now
62 always output, even when not addressed. Fix this by treating
63 function-local static constants just like global static
64 constants; the back-end already knows not to output them if they
65 are not needed.
66
67 o Provide heuristics to clamp inlining of recursive template
68 calls? */
69
70 /* Data required for function inlining. */
71
72 typedef struct inline_data
73 {
74 /* A stack of the functions we are inlining. For example, if we are
75 compiling `f', which calls `g', which calls `h', and we are
76 inlining the body of `h', the stack will contain, `h', followed
77 by `g', followed by `f'. The first few elements of the stack may
78 contain other functions that we know we should not recurse into,
79 even though they are not directly being inlined. */
80 varray_type fns;
81 /* The index of the first element of FNS that really represents an
82 inlined function. */
83 unsigned first_inlined_fn;
84 /* The label to jump to when a return statement is encountered. If
85 this value is NULL, then return statements will simply be
86 remapped as return statements, rather than as jumps. */
87 tree ret_label;
88 /* The map from local declarations in the inlined function to
89 equivalents in the function into which it is being inlined. */
90 splay_tree decl_map;
91 /* Nonzero if we are currently within the cleanup for a
92 TARGET_EXPR. */
93 int in_target_cleanup_p;
94 /* A list of the functions current function has inlined. */
95 varray_type inlined_fns;
96 /* The approximate number of instructions we have inlined in the
97 current call stack. */
98 int inlined_insns;
99 /* We use the same mechanism to build clones that we do to perform
100 inlining. However, there are a few places where we need to
101 distinguish between those two situations. This flag is true if
102 we are cloning, rather than inlining. */
103 bool cloning_p;
104 /* Hash table used to prevent walk_tree from visiting the same node
105 umpteen million times. */
106 htab_t tree_pruner;
107 /* Decl of function we are inlining into. */
108 tree decl;
109 tree current_decl;
110 } inline_data;
111
112 /* Prototypes. */
113
114 static tree declare_return_variable (inline_data *, tree, tree *);
115 static tree copy_body_r (tree *, int *, void *);
116 static tree copy_body (inline_data *);
117 static tree expand_call_inline (tree *, int *, void *);
118 static void expand_calls_inline (tree *, inline_data *);
119 static bool inlinable_function_p (tree);
120 static int limits_allow_inlining (tree, inline_data *);
121 static tree remap_decl (tree, inline_data *);
122 #ifndef INLINER_FOR_JAVA
123 static tree initialize_inlined_parameters (inline_data *, tree, tree);
124 static void remap_block (tree, tree, inline_data *);
125 static void copy_scope_stmt (tree *, int *, inline_data *);
126 #else /* INLINER_FOR_JAVA */
127 static tree initialize_inlined_parameters (inline_data *, tree, tree, tree);
128 static void remap_block (tree *, tree, inline_data *);
129 static tree add_stmt_to_compound (tree, tree, tree);
130 #endif /* INLINER_FOR_JAVA */
131 static tree find_alloca_call_1 (tree *, int *, void *);
132 static tree find_alloca_call (tree);
133 static tree find_builtin_longjmp_call_1 (tree *, int *, void *);
134 static tree find_builtin_longjmp_call (tree);
135
136 /* Remap DECL during the copying of the BLOCK tree for the function. */
137
138 static tree
139 remap_decl (tree decl, inline_data *id)
140 {
141 splay_tree_node n;
142 tree fn;
143
144 /* We only remap local variables in the current function. */
145 fn = VARRAY_TOP_TREE (id->fns);
146 if (! (*lang_hooks.tree_inlining.auto_var_in_fn_p) (decl, fn))
147 return NULL_TREE;
148
149 /* See if we have remapped this declaration. */
150 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
151 /* If we didn't already have an equivalent for this declaration,
152 create one now. */
153 if (!n)
154 {
155 tree t;
156
157 /* Make a copy of the variable or label. */
158 t = copy_decl_for_inlining (decl, fn,
159 VARRAY_TREE (id->fns, 0));
160
161 /* The decl T could be a dynamic array or other variable size type,
162 in which case some fields need to be remapped because they may
163 contain SAVE_EXPRs. */
164 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
165 && TYPE_DOMAIN (TREE_TYPE (t)))
166 {
167 TREE_TYPE (t) = copy_node (TREE_TYPE (t));
168 TYPE_DOMAIN (TREE_TYPE (t))
169 = copy_node (TYPE_DOMAIN (TREE_TYPE (t)));
170 walk_tree (&TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t))),
171 copy_body_r, id, NULL);
172 }
173
174 #ifndef INLINER_FOR_JAVA
175 if (! DECL_NAME (t) && TREE_TYPE (t)
176 && (*lang_hooks.tree_inlining.anon_aggr_type_p) (TREE_TYPE (t)))
177 {
178 /* For a VAR_DECL of anonymous type, we must also copy the
179 member VAR_DECLS here and rechain the
180 DECL_ANON_UNION_ELEMS. */
181 tree members = NULL;
182 tree src;
183
184 for (src = DECL_ANON_UNION_ELEMS (t); src;
185 src = TREE_CHAIN (src))
186 {
187 tree member = remap_decl (TREE_VALUE (src), id);
188
189 if (TREE_PURPOSE (src))
190 abort ();
191 members = tree_cons (NULL, member, members);
192 }
193 DECL_ANON_UNION_ELEMS (t) = nreverse (members);
194 }
195 #endif /* not INLINER_FOR_JAVA */
196
197 /* Remember it, so that if we encounter this local entity
198 again we can reuse this copy. */
199 n = splay_tree_insert (id->decl_map,
200 (splay_tree_key) decl,
201 (splay_tree_value) t);
202 }
203
204 return (tree) n->value;
205 }
206
207 #ifndef INLINER_FOR_JAVA
208 /* Copy the SCOPE_STMT_BLOCK associated with SCOPE_STMT to contain
209 remapped versions of the variables therein. And hook the new block
210 into the block-tree. If non-NULL, the DECLS are declarations to
211 add to use instead of the BLOCK_VARS in the old block. */
212 #else /* INLINER_FOR_JAVA */
213 /* Copy the BLOCK to contain remapped versions of the variables
214 therein. And hook the new block into the block-tree. */
215 #endif /* INLINER_FOR_JAVA */
216
217 static void
218 #ifndef INLINER_FOR_JAVA
219 remap_block (tree scope_stmt, tree decls, inline_data *id)
220 #else /* INLINER_FOR_JAVA */
221 remap_block (tree *block, tree decls, inline_data *id)
222 #endif /* INLINER_FOR_JAVA */
223 {
224 #ifndef INLINER_FOR_JAVA
225 /* We cannot do this in the cleanup for a TARGET_EXPR since we do
226 not know whether or not expand_expr will actually write out the
227 code we put there. If it does not, then we'll have more BLOCKs
228 than block-notes, and things will go awry. At some point, we
229 should make the back-end handle BLOCK notes in a tidier way,
230 without requiring a strict correspondence to the block-tree; then
231 this check can go. */
232 if (id->in_target_cleanup_p)
233 {
234 SCOPE_STMT_BLOCK (scope_stmt) = NULL_TREE;
235 return;
236 }
237
238 /* If this is the beginning of a scope, remap the associated BLOCK. */
239 if (SCOPE_BEGIN_P (scope_stmt) && SCOPE_STMT_BLOCK (scope_stmt))
240 {
241 tree old_block;
242 tree new_block;
243 tree old_var;
244 tree fn;
245
246 /* Make the new block. */
247 old_block = SCOPE_STMT_BLOCK (scope_stmt);
248 new_block = make_node (BLOCK);
249 TREE_USED (new_block) = TREE_USED (old_block);
250 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
251 SCOPE_STMT_BLOCK (scope_stmt) = new_block;
252
253 /* Remap its variables. */
254 for (old_var = decls ? decls : BLOCK_VARS (old_block);
255 old_var;
256 old_var = TREE_CHAIN (old_var))
257 {
258 tree new_var;
259
260 /* Remap the variable. */
261 new_var = remap_decl (old_var, id);
262 /* If we didn't remap this variable, so we can't mess with
263 its TREE_CHAIN. If we remapped this variable to
264 something other than a declaration (say, if we mapped it
265 to a constant), then we must similarly omit any mention
266 of it here. */
267 if (!new_var || !DECL_P (new_var))
268 ;
269 else
270 {
271 TREE_CHAIN (new_var) = BLOCK_VARS (new_block);
272 BLOCK_VARS (new_block) = new_var;
273 }
274 }
275 /* We put the BLOCK_VARS in reverse order; fix that now. */
276 BLOCK_VARS (new_block) = nreverse (BLOCK_VARS (new_block));
277 fn = VARRAY_TREE (id->fns, 0);
278 if (id->cloning_p)
279 /* We're building a clone; DECL_INITIAL is still
280 error_mark_node, and current_binding_level is the parm
281 binding level. */
282 (*lang_hooks.decls.insert_block) (new_block);
283 else
284 {
285 /* Attach this new block after the DECL_INITIAL block for the
286 function into which this block is being inlined. In
287 rest_of_compilation we will straighten out the BLOCK tree. */
288 tree *first_block;
289 if (DECL_INITIAL (fn))
290 first_block = &BLOCK_CHAIN (DECL_INITIAL (fn));
291 else
292 first_block = &DECL_INITIAL (fn);
293 BLOCK_CHAIN (new_block) = *first_block;
294 *first_block = new_block;
295 }
296 /* Remember the remapped block. */
297 splay_tree_insert (id->decl_map,
298 (splay_tree_key) old_block,
299 (splay_tree_value) new_block);
300 }
301 /* If this is the end of a scope, set the SCOPE_STMT_BLOCK to be the
302 remapped block. */
303 else if (SCOPE_END_P (scope_stmt) && SCOPE_STMT_BLOCK (scope_stmt))
304 {
305 splay_tree_node n;
306
307 /* Find this block in the table of remapped things. */
308 n = splay_tree_lookup (id->decl_map,
309 (splay_tree_key) SCOPE_STMT_BLOCK (scope_stmt));
310 if (! n)
311 abort ();
312 SCOPE_STMT_BLOCK (scope_stmt) = (tree) n->value;
313 }
314 #else /* INLINER_FOR_JAVA */
315 tree old_block;
316 tree new_block;
317 tree old_var;
318 tree fn;
319
320 /* Make the new block. */
321 old_block = *block;
322 new_block = make_node (BLOCK);
323 TREE_USED (new_block) = TREE_USED (old_block);
324 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
325 BLOCK_SUBBLOCKS (new_block) = BLOCK_SUBBLOCKS (old_block);
326 TREE_SIDE_EFFECTS (new_block) = TREE_SIDE_EFFECTS (old_block);
327 TREE_TYPE (new_block) = TREE_TYPE (old_block);
328 *block = new_block;
329
330 /* Remap its variables. */
331 for (old_var = decls ? decls : BLOCK_VARS (old_block);
332 old_var;
333 old_var = TREE_CHAIN (old_var))
334 {
335 tree new_var;
336
337 /* All local class initialization flags go in the outermost
338 scope. */
339 if (LOCAL_CLASS_INITIALIZATION_FLAG_P (old_var))
340 {
341 /* We may already have one. */
342 if (! splay_tree_lookup (id->decl_map, (splay_tree_key) old_var))
343 {
344 tree outermost_block;
345 new_var = remap_decl (old_var, id);
346 DECL_ABSTRACT_ORIGIN (new_var) = NULL;
347 outermost_block = DECL_SAVED_TREE (current_function_decl);
348 TREE_CHAIN (new_var) = BLOCK_VARS (outermost_block);
349 BLOCK_VARS (outermost_block) = new_var;
350 }
351 continue;
352 }
353
354 /* Remap the variable. */
355 new_var = remap_decl (old_var, id);
356 /* If we didn't remap this variable, so we can't mess with
357 its TREE_CHAIN. If we remapped this variable to
358 something other than a declaration (say, if we mapped it
359 to a constant), then we must similarly omit any mention
360 of it here. */
361 if (!new_var || !DECL_P (new_var))
362 ;
363 else
364 {
365 TREE_CHAIN (new_var) = BLOCK_VARS (new_block);
366 BLOCK_VARS (new_block) = new_var;
367 }
368 }
369 /* We put the BLOCK_VARS in reverse order; fix that now. */
370 BLOCK_VARS (new_block) = nreverse (BLOCK_VARS (new_block));
371 fn = VARRAY_TREE (id->fns, 0);
372 /* Remember the remapped block. */
373 splay_tree_insert (id->decl_map,
374 (splay_tree_key) old_block,
375 (splay_tree_value) new_block);
376 #endif /* INLINER_FOR_JAVA */
377 }
378
379 #ifndef INLINER_FOR_JAVA
380 /* Copy the SCOPE_STMT pointed to by TP. */
381
382 static void
383 copy_scope_stmt (tree *tp, int *walk_subtrees, inline_data *id)
384 {
385 tree block;
386
387 /* Remember whether or not this statement was nullified. When
388 making a copy, copy_tree_r always sets SCOPE_NULLIFIED_P (and
389 doesn't copy the SCOPE_STMT_BLOCK) to free callers from having to
390 deal with copying BLOCKs if they do not wish to do so. */
391 block = SCOPE_STMT_BLOCK (*tp);
392 /* Copy (and replace) the statement. */
393 copy_tree_r (tp, walk_subtrees, NULL);
394 /* Restore the SCOPE_STMT_BLOCK. */
395 SCOPE_STMT_BLOCK (*tp) = block;
396
397 /* Remap the associated block. */
398 remap_block (*tp, NULL_TREE, id);
399 }
400 #endif /* not INLINER_FOR_JAVA */
401
402 /* Called from copy_body via walk_tree. DATA is really an
403 `inline_data *'. */
404 static tree
405 copy_body_r (tree *tp, int *walk_subtrees, void *data)
406 {
407 inline_data* id;
408 tree fn;
409
410 /* Set up. */
411 id = (inline_data *) data;
412 fn = VARRAY_TOP_TREE (id->fns);
413
414 #if 0
415 /* All automatic variables should have a DECL_CONTEXT indicating
416 what function they come from. */
417 if ((TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == LABEL_DECL)
418 && DECL_NAMESPACE_SCOPE_P (*tp))
419 if (! DECL_EXTERNAL (*tp) && ! TREE_STATIC (*tp))
420 abort ();
421 #endif
422
423 #ifdef INLINER_FOR_JAVA
424 if (TREE_CODE (*tp) == BLOCK)
425 remap_block (tp, NULL_TREE, id);
426 #endif
427
428 /* If this is a RETURN_STMT, change it into an EXPR_STMT and a
429 GOTO_STMT with the RET_LABEL as its target. */
430 #ifndef INLINER_FOR_JAVA
431 if (TREE_CODE (*tp) == RETURN_STMT && id->ret_label)
432 #else /* INLINER_FOR_JAVA */
433 if (TREE_CODE (*tp) == RETURN_EXPR && id->ret_label)
434 #endif /* INLINER_FOR_JAVA */
435 {
436 tree return_stmt = *tp;
437 tree goto_stmt;
438
439 /* Build the GOTO_STMT. */
440 #ifndef INLINER_FOR_JAVA
441 goto_stmt = build_stmt (GOTO_STMT, id->ret_label);
442 TREE_CHAIN (goto_stmt) = TREE_CHAIN (return_stmt);
443 GOTO_FAKE_P (goto_stmt) = 1;
444 #else /* INLINER_FOR_JAVA */
445 tree assignment = TREE_OPERAND (return_stmt, 0);
446 goto_stmt = build1 (GOTO_EXPR, void_type_node, id->ret_label);
447 TREE_SIDE_EFFECTS (goto_stmt) = 1;
448 #endif /* INLINER_FOR_JAVA */
449
450 /* If we're returning something, just turn that into an
451 assignment into the equivalent of the original
452 RESULT_DECL. */
453 #ifndef INLINER_FOR_JAVA
454 if (RETURN_STMT_EXPR (return_stmt))
455 {
456 *tp = build_stmt (EXPR_STMT,
457 RETURN_STMT_EXPR (return_stmt));
458 STMT_IS_FULL_EXPR_P (*tp) = 1;
459 /* And then jump to the end of the function. */
460 TREE_CHAIN (*tp) = goto_stmt;
461 }
462 #else /* INLINER_FOR_JAVA */
463 if (assignment)
464 {
465 copy_body_r (&assignment, walk_subtrees, data);
466 *tp = build (COMPOUND_EXPR, void_type_node, assignment, goto_stmt);
467 TREE_SIDE_EFFECTS (*tp) = 1;
468 }
469 #endif /* INLINER_FOR_JAVA */
470 /* If we're not returning anything just do the jump. */
471 else
472 *tp = goto_stmt;
473 }
474 /* Local variables and labels need to be replaced by equivalent
475 variables. We don't want to copy static variables; there's only
476 one of those, no matter how many times we inline the containing
477 function. */
478 else if ((*lang_hooks.tree_inlining.auto_var_in_fn_p) (*tp, fn))
479 {
480 tree new_decl;
481
482 /* Remap the declaration. */
483 new_decl = remap_decl (*tp, id);
484 if (! new_decl)
485 abort ();
486 /* Replace this variable with the copy. */
487 STRIP_TYPE_NOPS (new_decl);
488 *tp = new_decl;
489 }
490 #if 0
491 else if (nonstatic_local_decl_p (*tp)
492 && DECL_CONTEXT (*tp) != VARRAY_TREE (id->fns, 0))
493 abort ();
494 #endif
495 else if (TREE_CODE (*tp) == SAVE_EXPR)
496 remap_save_expr (tp, id->decl_map, VARRAY_TREE (id->fns, 0),
497 walk_subtrees);
498 else if (TREE_CODE (*tp) == UNSAVE_EXPR)
499 /* UNSAVE_EXPRs should not be generated until expansion time. */
500 abort ();
501 #ifndef INLINER_FOR_JAVA
502 /* For a SCOPE_STMT, we must copy the associated block so that we
503 can write out debugging information for the inlined variables. */
504 else if (TREE_CODE (*tp) == SCOPE_STMT && !id->in_target_cleanup_p)
505 copy_scope_stmt (tp, walk_subtrees, id);
506 #else /* INLINER_FOR_JAVA */
507 else if (TREE_CODE (*tp) == LABELED_BLOCK_EXPR)
508 {
509 /* We need a new copy of this labeled block; the EXIT_BLOCK_EXPR
510 will refer to it, so save a copy ready for remapping. We
511 save it in the decl_map, although it isn't a decl. */
512 tree new_block = copy_node (*tp);
513 splay_tree_insert (id->decl_map,
514 (splay_tree_key) *tp,
515 (splay_tree_value) new_block);
516 *tp = new_block;
517 }
518 else if (TREE_CODE (*tp) == EXIT_BLOCK_EXPR)
519 {
520 splay_tree_node n
521 = splay_tree_lookup (id->decl_map,
522 (splay_tree_key) TREE_OPERAND (*tp, 0));
523 /* We _must_ have seen the enclosing LABELED_BLOCK_EXPR. */
524 if (! n)
525 abort ();
526 *tp = copy_node (*tp);
527 TREE_OPERAND (*tp, 0) = (tree) n->value;
528 }
529 #endif /* INLINER_FOR_JAVA */
530 /* Otherwise, just copy the node. Note that copy_tree_r already
531 knows not to copy VAR_DECLs, etc., so this is safe. */
532 else
533 {
534 if (TREE_CODE (*tp) == MODIFY_EXPR
535 && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
536 && ((*lang_hooks.tree_inlining.auto_var_in_fn_p)
537 (TREE_OPERAND (*tp, 0), fn)))
538 {
539 /* Some assignments VAR = VAR; don't generate any rtl code
540 and thus don't count as variable modification. Avoid
541 keeping bogosities like 0 = 0. */
542 tree decl = TREE_OPERAND (*tp, 0), value;
543 splay_tree_node n;
544
545 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
546 if (n)
547 {
548 value = (tree) n->value;
549 STRIP_TYPE_NOPS (value);
550 if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value))
551 {
552 *tp = value;
553 return copy_body_r (tp, walk_subtrees, data);
554 }
555 }
556 }
557 else if (TREE_CODE (*tp) == ADDR_EXPR
558 && ((*lang_hooks.tree_inlining.auto_var_in_fn_p)
559 (TREE_OPERAND (*tp, 0), fn)))
560 {
561 /* Get rid of &* from inline substitutions. It can occur when
562 someone takes the address of a parm or return slot passed by
563 invisible reference. */
564 tree decl = TREE_OPERAND (*tp, 0), value;
565 splay_tree_node n;
566
567 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
568 if (n)
569 {
570 value = (tree) n->value;
571 if (TREE_CODE (value) == INDIRECT_REF)
572 {
573 *tp = convert (TREE_TYPE (*tp), TREE_OPERAND (value, 0));
574 return copy_body_r (tp, walk_subtrees, data);
575 }
576 }
577 }
578
579 copy_tree_r (tp, walk_subtrees, NULL);
580
581 /* The copied TARGET_EXPR has never been expanded, even if the
582 original node was expanded already. */
583 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
584 {
585 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
586 TREE_OPERAND (*tp, 3) = NULL_TREE;
587 }
588 }
589
590 /* Keep iterating. */
591 return NULL_TREE;
592 }
593
594 /* Make a copy of the body of FN so that it can be inserted inline in
595 another function. */
596
597 static tree
598 copy_body (inline_data *id)
599 {
600 tree body;
601
602 body = DECL_SAVED_TREE (VARRAY_TOP_TREE (id->fns));
603 walk_tree (&body, copy_body_r, id, NULL);
604
605 return body;
606 }
607
608 /* Generate code to initialize the parameters of the function at the
609 top of the stack in ID from the ARGS (presented as a TREE_LIST). */
610
611 static tree
612 #ifndef INLINER_FOR_JAVA
613 initialize_inlined_parameters (inline_data *id, tree args, tree fn)
614 #else /* INLINER_FOR_JAVA */
615 initialize_inlined_parameters (inline_data *id, tree args, tree fn, tree block)
616 #endif /* INLINER_FOR_JAVA */
617 {
618 tree init_stmts;
619 tree parms;
620 tree a;
621 tree p;
622 #ifdef INLINER_FOR_JAVA
623 tree vars = NULL_TREE;
624 #endif /* INLINER_FOR_JAVA */
625
626 /* Figure out what the parameters are. */
627 parms = DECL_ARGUMENTS (fn);
628
629 /* Start with no initializations whatsoever. */
630 init_stmts = NULL_TREE;
631
632 /* Loop through the parameter declarations, replacing each with an
633 equivalent VAR_DECL, appropriately initialized. */
634 for (p = parms, a = args; p;
635 a = a ? TREE_CHAIN (a) : a, p = TREE_CHAIN (p))
636 {
637 #ifndef INLINER_FOR_JAVA
638 tree init_stmt;
639 tree cleanup;
640 #endif /* not INLINER_FOR_JAVA */
641 tree var;
642 tree value;
643 tree var_sub;
644
645 /* Find the initializer. */
646 value = (*lang_hooks.tree_inlining.convert_parm_for_inlining)
647 (p, a ? TREE_VALUE (a) : NULL_TREE, fn);
648
649 /* If the parameter is never assigned to, we may not need to
650 create a new variable here at all. Instead, we may be able
651 to just use the argument value. */
652 if (TREE_READONLY (p)
653 && !TREE_ADDRESSABLE (p)
654 && value && !TREE_SIDE_EFFECTS (value))
655 {
656 /* Simplify the value, if possible. */
657 value = fold (DECL_P (value) ? decl_constant_value (value) : value);
658
659 /* We can't risk substituting complex expressions. They
660 might contain variables that will be assigned to later.
661 Theoretically, we could check the expression to see if
662 all of the variables that determine its value are
663 read-only, but we don't bother. */
664 if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value))
665 {
666 /* If this is a declaration, wrap it a NOP_EXPR so that
667 we don't try to put the VALUE on the list of
668 BLOCK_VARS. */
669 if (DECL_P (value))
670 value = build1 (NOP_EXPR, TREE_TYPE (value), value);
671
672 /* If this is a constant, make sure it has the right type. */
673 else if (TREE_TYPE (value) != TREE_TYPE (p))
674 value = fold (build1 (NOP_EXPR, TREE_TYPE (p), value));
675
676 splay_tree_insert (id->decl_map,
677 (splay_tree_key) p,
678 (splay_tree_value) value);
679 continue;
680 }
681 }
682
683 /* Make an equivalent VAR_DECL. */
684 var = copy_decl_for_inlining (p, fn, VARRAY_TREE (id->fns, 0));
685
686 /* See if the frontend wants to pass this by invisible reference. If
687 so, our new VAR_DECL will have REFERENCE_TYPE, and we need to
688 replace uses of the PARM_DECL with dereferences. */
689 if (TREE_TYPE (var) != TREE_TYPE (p)
690 && POINTER_TYPE_P (TREE_TYPE (var))
691 && TREE_TYPE (TREE_TYPE (var)) == TREE_TYPE (p))
692 var_sub = build1 (INDIRECT_REF, TREE_TYPE (p), var);
693 else
694 var_sub = var;
695
696 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
697 that way, when the PARM_DECL is encountered, it will be
698 automatically replaced by the VAR_DECL. */
699 splay_tree_insert (id->decl_map,
700 (splay_tree_key) p,
701 (splay_tree_value) var_sub);
702
703 /* Declare this new variable. */
704 #ifndef INLINER_FOR_JAVA
705 init_stmt = build_stmt (DECL_STMT, var);
706 TREE_CHAIN (init_stmt) = init_stmts;
707 init_stmts = init_stmt;
708 #else /* INLINER_FOR_JAVA */
709 TREE_CHAIN (var) = vars;
710 vars = var;
711 #endif /* INLINER_FOR_JAVA */
712
713 /* Initialize this VAR_DECL from the equivalent argument. If
714 the argument is an object, created via a constructor or copy,
715 this will not result in an extra copy: the TARGET_EXPR
716 representing the argument will be bound to VAR, and the
717 object will be constructed in VAR. */
718 if (! TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
719 #ifndef INLINER_FOR_JAVA
720 DECL_INITIAL (var) = value;
721 else
722 {
723 /* Even if P was TREE_READONLY, the new VAR should not be.
724 In the original code, we would have constructed a
725 temporary, and then the function body would have never
726 changed the value of P. However, now, we will be
727 constructing VAR directly. The constructor body may
728 change its value multiple times as it is being
729 constructed. Therefore, it must not be TREE_READONLY;
730 the back-end assumes that TREE_READONLY variable is
731 assigned to only once. */
732 TREE_READONLY (var) = 0;
733
734 /* Build a run-time initialization. */
735 init_stmt = build_stmt (EXPR_STMT,
736 build (INIT_EXPR, TREE_TYPE (p),
737 var, value));
738 /* Add this initialization to the list. Note that we want the
739 declaration *after* the initialization because we are going
740 to reverse all the initialization statements below. */
741 TREE_CHAIN (init_stmt) = init_stmts;
742 init_stmts = init_stmt;
743 }
744
745 /* See if we need to clean up the declaration. */
746 cleanup = (*lang_hooks.maybe_build_cleanup) (var);
747 if (cleanup)
748 {
749 tree cleanup_stmt;
750 /* Build the cleanup statement. */
751 cleanup_stmt = build_stmt (CLEANUP_STMT, var, cleanup);
752 /* Add it to the *front* of the list; the list will be
753 reversed below. */
754 TREE_CHAIN (cleanup_stmt) = init_stmts;
755 init_stmts = cleanup_stmt;
756 }
757 #else /* INLINER_FOR_JAVA */
758 {
759 tree assignment = build (MODIFY_EXPR, TREE_TYPE (p), var, value);
760 init_stmts = add_stmt_to_compound (init_stmts, TREE_TYPE (p),
761 assignment);
762 }
763 else
764 {
765 /* Java objects don't ever need constructing when being
766 passed as arguments because only call by reference is
767 supported. */
768 abort ();
769 }
770 #endif /* INLINER_FOR_JAVA */
771 }
772
773 #ifndef INLINER_FOR_JAVA
774 /* Evaluate trailing arguments. */
775 for (; a; a = TREE_CHAIN (a))
776 {
777 tree init_stmt;
778 tree value = TREE_VALUE (a);
779
780 if (! value || ! TREE_SIDE_EFFECTS (value))
781 continue;
782
783 init_stmt = build_stmt (EXPR_STMT, value);
784 TREE_CHAIN (init_stmt) = init_stmts;
785 init_stmts = init_stmt;
786 }
787
788 /* The initialization statements have been built up in reverse
789 order. Straighten them out now. */
790 return nreverse (init_stmts);
791 #else /* INLINER_FOR_JAVA */
792 BLOCK_VARS (block) = nreverse (vars);
793 return init_stmts;
794 #endif /* INLINER_FOR_JAVA */
795 }
796
797 /* Declare a return variable to replace the RESULT_DECL for the
798 function we are calling. An appropriate DECL_STMT is returned.
799 The USE_STMT is filled in to contain a use of the declaration to
800 indicate the return value of the function. */
801
802 #ifndef INLINER_FOR_JAVA
803 static tree
804 declare_return_variable (struct inline_data *id, tree return_slot_addr,
805 tree *use_stmt)
806 #else /* INLINER_FOR_JAVA */
807 static tree
808 declare_return_variable (struct inline_data *id, tree return_slot_addr,
809 tree *var)
810 #endif /* INLINER_FOR_JAVA */
811 {
812 tree fn = VARRAY_TOP_TREE (id->fns);
813 tree result = DECL_RESULT (fn);
814 #ifndef INLINER_FOR_JAVA
815 tree var;
816 #endif /* not INLINER_FOR_JAVA */
817 int need_return_decl = 1;
818
819 /* We don't need to do anything for functions that don't return
820 anything. */
821 if (!result || VOID_TYPE_P (TREE_TYPE (result)))
822 {
823 #ifndef INLINER_FOR_JAVA
824 *use_stmt = NULL_TREE;
825 #else /* INLINER_FOR_JAVA */
826 *var = NULL_TREE;
827 #endif /* INLINER_FOR_JAVA */
828 return NULL_TREE;
829 }
830
831 #ifndef INLINER_FOR_JAVA
832 var = ((*lang_hooks.tree_inlining.copy_res_decl_for_inlining)
833 (result, fn, VARRAY_TREE (id->fns, 0), id->decl_map,
834 &need_return_decl, return_slot_addr));
835
836 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
837 way, when the RESULT_DECL is encountered, it will be
838 automatically replaced by the VAR_DECL. */
839 splay_tree_insert (id->decl_map,
840 (splay_tree_key) result,
841 (splay_tree_value) var);
842
843 /* Build the USE_STMT. If the return type of the function was
844 promoted, convert it back to the expected type. */
845 if (TREE_TYPE (var) == TREE_TYPE (TREE_TYPE (fn)))
846 *use_stmt = build_stmt (EXPR_STMT, var);
847 else
848 *use_stmt = build_stmt (EXPR_STMT,
849 build1 (NOP_EXPR, TREE_TYPE (TREE_TYPE (fn)),
850 var));
851 TREE_ADDRESSABLE (*use_stmt) = 1;
852
853 /* Build the declaration statement if FN does not return an
854 aggregate. */
855 if (need_return_decl)
856 return build_stmt (DECL_STMT, var);
857 #else /* INLINER_FOR_JAVA */
858 *var = ((*lang_hooks.tree_inlining.copy_res_decl_for_inlining)
859 (result, fn, VARRAY_TREE (id->fns, 0), id->decl_map,
860 &need_return_decl, return_slot_addr));
861
862 splay_tree_insert (id->decl_map,
863 (splay_tree_key) result,
864 (splay_tree_value) *var);
865 DECL_IGNORED_P (*var) = 1;
866 if (need_return_decl)
867 return *var;
868 #endif /* INLINER_FOR_JAVA */
869 /* If FN does return an aggregate, there's no need to declare the
870 return variable; we're using a variable in our caller's frame. */
871 else
872 return NULL_TREE;
873 }
874
875 /* Returns nonzero if a function can be inlined as a tree. */
876
877 bool
878 tree_inlinable_function_p (tree fn)
879 {
880 return inlinable_function_p (fn);
881 }
882
883 /* If *TP is possibly call to alloca, return nonzero. */
884 static tree
885 find_alloca_call_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
886 void *data ATTRIBUTE_UNUSED)
887 {
888 if (alloca_call_p (*tp))
889 return *tp;
890 return NULL;
891 }
892
893 /* Return subexpression representing possible alloca call, if any. */
894 static tree
895 find_alloca_call (tree exp)
896 {
897 location_t saved_loc = input_location;
898 tree ret = walk_tree_without_duplicates
899 (&exp, find_alloca_call_1, NULL);
900 input_location = saved_loc;
901 return ret;
902 }
903
904 static tree
905 find_builtin_longjmp_call_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
906 void *data ATTRIBUTE_UNUSED)
907 {
908 tree exp = *tp, decl;
909
910 if (TREE_CODE (exp) == CALL_EXPR
911 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
912 && (decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0),
913 TREE_CODE (decl) == FUNCTION_DECL)
914 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
915 && DECL_FUNCTION_CODE (decl) == BUILT_IN_LONGJMP)
916 return decl;
917
918 return NULL;
919 }
920
921 static tree
922 find_builtin_longjmp_call (tree exp)
923 {
924 location_t saved_loc = input_location;
925 tree ret = walk_tree_without_duplicates
926 (&exp, find_builtin_longjmp_call_1, NULL);
927 input_location = saved_loc;
928 return ret;
929 }
930
931 /* Returns nonzero if FN is a function that does not have any
932 fundamental inline blocking properties. */
933
934 static bool
935 inlinable_function_p (tree fn)
936 {
937 bool inlinable = true;
938 bool calls_builtin_longjmp = false;
939 bool calls_alloca = false;
940
941 /* If we've already decided this function shouldn't be inlined,
942 there's no need to check again. */
943 if (DECL_UNINLINABLE (fn))
944 return false;
945
946 /* See if there is any language-specific reason it cannot be
947 inlined. (It is important that this hook be called early because
948 in C++ it may result in template instantiation.)
949 If the function is not inlinable for language-specific reasons,
950 it is left up to the langhook to explain why. */
951 inlinable = !(*lang_hooks.tree_inlining.cannot_inline_tree_fn) (&fn);
952
953 /* If we don't have the function body available, we can't inline it.
954 However, this should not be recorded since we also get here for
955 forward declared inline functions. Therefore, return at once. */
956 if (!DECL_SAVED_TREE (fn))
957 return false;
958
959 /* If we're not inlining at all, then we cannot inline this function. */
960 else if (!flag_inline_trees)
961 inlinable = false;
962
963 /* Only try to inline functions if DECL_INLINE is set. This should be
964 true for all functions declared `inline', and for all other functions
965 as well with -finline-functions.
966
967 Don't think of disregarding DECL_INLINE when flag_inline_trees == 2;
968 it's the front-end that must set DECL_INLINE in this case, because
969 dwarf2out loses if a function that does not have DECL_INLINE set is
970 inlined anyway. That is why we have both DECL_INLINE and
971 DECL_DECLARED_INLINE_P. */
972 /* FIXME: When flag_inline_trees dies, the check for flag_unit_at_a_time
973 here should be redundant. */
974 else if (!DECL_INLINE (fn) && !flag_unit_at_a_time)
975 inlinable = false;
976
977 #ifdef INLINER_FOR_JAVA
978 /* Synchronized methods can't be inlined. This is a bug. */
979 else if (METHOD_SYNCHRONIZED (fn))
980 inlinable = false;
981 #endif /* INLINER_FOR_JAVA */
982
983 /* We can't inline functions that call __builtin_longjmp at all.
984 The non-local goto machinery really requires the destination
985 be in a different function. If we allow the function calling
986 __builtin_longjmp to be inlined into the function calling
987 __builtin_setjmp, Things will Go Awry. */
988 /* ??? Need front end help to identify "regular" non-local goto. */
989 else if (find_builtin_longjmp_call (DECL_SAVED_TREE (fn)))
990 calls_builtin_longjmp = true;
991
992 /* Refuse to inline alloca call unless user explicitly forced so as this
993 may change program's memory overhead drastically when the function
994 using alloca is called in loop. In GCC present in SPEC2000 inlining
995 into schedule_block cause it to require 2GB of ram instead of 256MB. */
996 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)) == NULL
997 && find_alloca_call (DECL_SAVED_TREE (fn)))
998 calls_alloca = true;
999
1000 if (calls_builtin_longjmp || calls_alloca)
1001 {
1002 /* See if we should warn about uninlinable functions. Previously,
1003 some of these warnings would be issued while trying to expand
1004 the function inline, but that would cause multiple warnings
1005 about functions that would for example call alloca. But since
1006 this a property of the function, just one warning is enough.
1007 As a bonus we can now give more details about the reason why a
1008 function is not inlinable.
1009 We only warn for functions declared `inline' by the user. */
1010 bool do_warning = (warn_inline
1011 && DECL_INLINE (fn)
1012 && DECL_DECLARED_INLINE_P (fn)
1013 && !DECL_IN_SYSTEM_HEADER (fn));
1014
1015 if (do_warning && calls_builtin_longjmp)
1016 warning ("%Hfunction '%F' can never be inlined because it uses "
1017 "setjmp-longjmp exception handling",
1018 &DECL_SOURCE_LOCATION (fn), fn);
1019 if (do_warning && calls_alloca)
1020 warning ("%Hfunction '%F' can never be inlined because it uses "
1021 "setjmp-longjmp exception handling",
1022 &DECL_SOURCE_LOCATION (fn), fn);
1023
1024 inlinable = false;
1025 }
1026
1027 /* Squirrel away the result so that we don't have to check again. */
1028 DECL_UNINLINABLE (fn) = !inlinable;
1029
1030 return inlinable;
1031 }
1032
1033 /* We can't inline functions that are too big. Only allow a single
1034 function to be of MAX_INLINE_INSNS_SINGLE size. Make special
1035 allowance for extern inline functions, though.
1036
1037 Return nonzero if the function FN can be inlined into the inlining
1038 context ID. */
1039
1040 static int
1041 limits_allow_inlining (tree fn, inline_data *id)
1042 {
1043 int estimated_insns = 0;
1044 size_t i;
1045
1046 /* Don't even bother if the function is not inlinable. */
1047 if (!inlinable_function_p (fn))
1048 return 0;
1049
1050 /* Investigate the size of the function. Return at once
1051 if the function body size is too large. */
1052 if (!(*lang_hooks.tree_inlining.disregard_inline_limits) (fn))
1053 {
1054 int currfn_max_inline_insns;
1055
1056 /* If we haven't already done so, get an estimate of the number of
1057 instructions that will be produces when expanding this function. */
1058 if (!DECL_ESTIMATED_INSNS (fn))
1059 DECL_ESTIMATED_INSNS (fn)
1060 = (*lang_hooks.tree_inlining.estimate_num_insns) (fn);
1061 estimated_insns = DECL_ESTIMATED_INSNS (fn);
1062
1063 /* We may be here either because fn is declared inline or because
1064 we use -finline-functions. For the second case, we are more
1065 restrictive.
1066
1067 FIXME: -finline-functions should imply -funit-at-a-time, it's
1068 about equally expensive but unit-at-a-time produces
1069 better code. */
1070 currfn_max_inline_insns = DECL_DECLARED_INLINE_P (fn) ?
1071 MAX_INLINE_INSNS_SINGLE : MAX_INLINE_INSNS_AUTO;
1072
1073 /* If the function is too big to be inlined, adieu. */
1074 if (estimated_insns > currfn_max_inline_insns)
1075 return 0;
1076
1077 /* We now know that we don't disregard the inlining limits and that
1078 we basically should be able to inline this function.
1079 We always allow inlining functions if we estimate that they are
1080 smaller than MIN_INLINE_INSNS. Otherwise, investigate further. */
1081 if (estimated_insns > MIN_INLINE_INSNS)
1082 {
1083 int sum_insns = (id ? id->inlined_insns : 0) + estimated_insns;
1084
1085 /* In the extreme case that we have exceeded the recursive inlining
1086 limit by a huge factor (128), we just say no.
1087
1088 FIXME: Should not happen in real life, but people have reported
1089 that it actually does!? */
1090 if (sum_insns > MAX_INLINE_INSNS * 128)
1091 return 0;
1092
1093 /* If we did not hit the extreme limit, we use a linear function
1094 with slope -1/MAX_INLINE_SLOPE to exceedingly decrease the
1095 allowable size. */
1096 else if (sum_insns > MAX_INLINE_INSNS)
1097 {
1098 if (estimated_insns > currfn_max_inline_insns
1099 - (sum_insns - MAX_INLINE_INSNS) / MAX_INLINE_SLOPE)
1100 return 0;
1101 }
1102 }
1103 }
1104
1105 /* Don't allow recursive inlining. */
1106 for (i = 0; i < VARRAY_ACTIVE_SIZE (id->fns); ++i)
1107 if (VARRAY_TREE (id->fns, i) == fn)
1108 return 0;
1109
1110 if (DECL_INLINED_FNS (fn))
1111 {
1112 int j;
1113 tree inlined_fns = DECL_INLINED_FNS (fn);
1114
1115 for (j = 0; j < TREE_VEC_LENGTH (inlined_fns); ++j)
1116 if (TREE_VEC_ELT (inlined_fns, j) == VARRAY_TREE (id->fns, 0))
1117 return 0;
1118 }
1119
1120 /* Go ahead, this function can be inlined. */
1121 return 1;
1122 }
1123
1124 /* If *TP is a CALL_EXPR, replace it with its inline expansion. */
1125
1126 static tree
1127 expand_call_inline (tree *tp, int *walk_subtrees, void *data)
1128 {
1129 inline_data *id;
1130 tree t;
1131 tree expr;
1132 tree stmt;
1133 #ifndef INLINER_FOR_JAVA
1134 tree chain;
1135 tree scope_stmt;
1136 tree use_stmt;
1137 #else /* INLINER_FOR_JAVA */
1138 tree retvar;
1139 #endif /* INLINER_FOR_JAVA */
1140 tree fn;
1141 tree arg_inits;
1142 tree *inlined_body;
1143 splay_tree st;
1144 tree args;
1145 tree return_slot_addr;
1146
1147 /* See what we've got. */
1148 id = (inline_data *) data;
1149 t = *tp;
1150
1151 /* Recurse, but letting recursive invocations know that we are
1152 inside the body of a TARGET_EXPR. */
1153 if (TREE_CODE (*tp) == TARGET_EXPR)
1154 {
1155 #ifndef INLINER_FOR_JAVA
1156 int i, len = first_rtl_op (TARGET_EXPR);
1157
1158 /* We're walking our own subtrees. */
1159 *walk_subtrees = 0;
1160
1161 /* Actually walk over them. This loop is the body of
1162 walk_trees, omitting the case where the TARGET_EXPR
1163 itself is handled. */
1164 for (i = 0; i < len; ++i)
1165 {
1166 if (i == 2)
1167 ++id->in_target_cleanup_p;
1168 walk_tree (&TREE_OPERAND (*tp, i), expand_call_inline, data,
1169 id->tree_pruner);
1170 if (i == 2)
1171 --id->in_target_cleanup_p;
1172 }
1173
1174 return NULL_TREE;
1175 #else /* INLINER_FOR_JAVA */
1176 abort ();
1177 #endif /* INLINER_FOR_JAVA */
1178 }
1179 else if (TREE_CODE (t) == EXPR_WITH_FILE_LOCATION)
1180 {
1181 /* We're walking the subtree directly. */
1182 *walk_subtrees = 0;
1183 /* Update the source position. */
1184 push_srcloc (EXPR_WFL_FILENAME (t), EXPR_WFL_LINENO (t));
1185 walk_tree (&EXPR_WFL_NODE (t), expand_call_inline, data,
1186 id->tree_pruner);
1187 /* Restore the original source position. */
1188 pop_srcloc ();
1189
1190 return NULL_TREE;
1191 }
1192
1193 if (TYPE_P (t))
1194 /* Because types were not copied in copy_body, CALL_EXPRs beneath
1195 them should not be expanded. This can happen if the type is a
1196 dynamic array type, for example. */
1197 *walk_subtrees = 0;
1198
1199 /* From here on, we're only interested in CALL_EXPRs. */
1200 if (TREE_CODE (t) != CALL_EXPR)
1201 return NULL_TREE;
1202
1203 /* First, see if we can figure out what function is being called.
1204 If we cannot, then there is no hope of inlining the function. */
1205 fn = get_callee_fndecl (t);
1206 if (!fn)
1207 return NULL_TREE;
1208
1209 /* Turn forward declarations into real ones. */
1210 if (flag_unit_at_a_time)
1211 fn = cgraph_node (fn)->decl;
1212
1213 /* If fn is a declaration of a function in a nested scope that was
1214 globally declared inline, we don't set its DECL_INITIAL.
1215 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
1216 C++ front-end uses it for cdtors to refer to their internal
1217 declarations, that are not real functions. Fortunately those
1218 don't have trees to be saved, so we can tell by checking their
1219 DECL_SAVED_TREE. */
1220 if (! DECL_INITIAL (fn)
1221 && DECL_ABSTRACT_ORIGIN (fn)
1222 && DECL_SAVED_TREE (DECL_ABSTRACT_ORIGIN (fn)))
1223 fn = DECL_ABSTRACT_ORIGIN (fn);
1224
1225 /* Don't try to inline functions that are not well-suited to
1226 inlining. */
1227 if ((flag_unit_at_a_time
1228 && (!DECL_SAVED_TREE (fn) || !cgraph_inline_p (id->current_decl, fn)))
1229 || (!flag_unit_at_a_time && !limits_allow_inlining (fn, id)))
1230 {
1231 if (warn_inline && DECL_INLINE (fn) && DECL_DECLARED_INLINE_P (fn)
1232 && !DECL_IN_SYSTEM_HEADER (fn))
1233 {
1234 warning ("%Hinlining failed in call to '%F'",
1235 &DECL_SOURCE_LOCATION (fn), fn);
1236 warning ("called from here");
1237 }
1238 return NULL_TREE;
1239 }
1240
1241 if (! (*lang_hooks.tree_inlining.start_inlining) (fn))
1242 return NULL_TREE;
1243
1244 /* Set the current filename and line number to the function we are
1245 inlining so that when we create new _STMT nodes here they get
1246 line numbers corresponding to the function we are calling. We
1247 wrap the whole inlined body in an EXPR_WITH_FILE_AND_LINE as well
1248 because individual statements don't record the filename. */
1249 push_srcloc (DECL_SOURCE_FILE (fn), DECL_SOURCE_LINE (fn));
1250
1251 #ifndef INLINER_FOR_JAVA
1252 /* Build a statement-expression containing code to initialize the
1253 arguments, the actual inline expansion of the body, and a label
1254 for the return statements within the function to jump to. The
1255 type of the statement expression is the return type of the
1256 function call. */
1257 expr = build1 (STMT_EXPR, TREE_TYPE (TREE_TYPE (fn)), make_node (COMPOUND_STMT));
1258 /* There is no scope associated with the statement-expression. */
1259 STMT_EXPR_NO_SCOPE (expr) = 1;
1260 stmt = STMT_EXPR_STMT (expr);
1261 #else /* INLINER_FOR_JAVA */
1262 /* Build a block containing code to initialize the arguments, the
1263 actual inline expansion of the body, and a label for the return
1264 statements within the function to jump to. The type of the
1265 statement expression is the return type of the function call. */
1266 stmt = NULL;
1267 expr = build (BLOCK, TREE_TYPE (TREE_TYPE (fn)), stmt);
1268 #endif /* INLINER_FOR_JAVA */
1269
1270 /* Local declarations will be replaced by their equivalents in this
1271 map. */
1272 st = id->decl_map;
1273 id->decl_map = splay_tree_new (splay_tree_compare_pointers,
1274 NULL, NULL);
1275
1276 /* Initialize the parameters. */
1277 args = TREE_OPERAND (t, 1);
1278 return_slot_addr = NULL_TREE;
1279 if (CALL_EXPR_HAS_RETURN_SLOT_ADDR (t))
1280 {
1281 return_slot_addr = TREE_VALUE (args);
1282 args = TREE_CHAIN (args);
1283 }
1284
1285 #ifndef INLINER_FOR_JAVA
1286 arg_inits = initialize_inlined_parameters (id, args, fn);
1287 /* Expand any inlined calls in the initializers. Do this before we
1288 push FN on the stack of functions we are inlining; we want to
1289 inline calls to FN that appear in the initializers for the
1290 parameters. */
1291 expand_calls_inline (&arg_inits, id);
1292 /* And add them to the tree. */
1293 COMPOUND_BODY (stmt) = chainon (COMPOUND_BODY (stmt), arg_inits);
1294 #else /* INLINER_FOR_JAVA */
1295 arg_inits = initialize_inlined_parameters (id, args, fn, expr);
1296 if (arg_inits)
1297 {
1298 /* Expand any inlined calls in the initializers. Do this before we
1299 push FN on the stack of functions we are inlining; we want to
1300 inline calls to FN that appear in the initializers for the
1301 parameters. */
1302 expand_calls_inline (&arg_inits, id);
1303
1304 /* And add them to the tree. */
1305 BLOCK_EXPR_BODY (expr) = add_stmt_to_compound (BLOCK_EXPR_BODY (expr),
1306 TREE_TYPE (arg_inits),
1307 arg_inits);
1308 }
1309 #endif /* INLINER_FOR_JAVA */
1310
1311 /* Record the function we are about to inline so that we can avoid
1312 recursing into it. */
1313 VARRAY_PUSH_TREE (id->fns, fn);
1314
1315 /* Record the function we are about to inline if optimize_function
1316 has not been called on it yet and we don't have it in the list. */
1317 if (! DECL_INLINED_FNS (fn))
1318 {
1319 int i;
1320
1321 for (i = VARRAY_ACTIVE_SIZE (id->inlined_fns) - 1; i >= 0; i--)
1322 if (VARRAY_TREE (id->inlined_fns, i) == fn)
1323 break;
1324 if (i < 0)
1325 VARRAY_PUSH_TREE (id->inlined_fns, fn);
1326 }
1327
1328 /* Return statements in the function body will be replaced by jumps
1329 to the RET_LABEL. */
1330 id->ret_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
1331 DECL_CONTEXT (id->ret_label) = VARRAY_TREE (id->fns, 0);
1332
1333 if (! DECL_INITIAL (fn)
1334 || TREE_CODE (DECL_INITIAL (fn)) != BLOCK)
1335 abort ();
1336
1337 #ifndef INLINER_FOR_JAVA
1338 /* Create a block to put the parameters in. We have to do this
1339 after the parameters have been remapped because remapping
1340 parameters is different from remapping ordinary variables. */
1341 scope_stmt = build_stmt (SCOPE_STMT, DECL_INITIAL (fn));
1342 SCOPE_BEGIN_P (scope_stmt) = 1;
1343 SCOPE_NO_CLEANUPS_P (scope_stmt) = 1;
1344 remap_block (scope_stmt, DECL_ARGUMENTS (fn), id);
1345 TREE_CHAIN (scope_stmt) = COMPOUND_BODY (stmt);
1346 COMPOUND_BODY (stmt) = scope_stmt;
1347
1348 /* Tell the debugging backends that this block represents the
1349 outermost scope of the inlined function. */
1350 if (SCOPE_STMT_BLOCK (scope_stmt))
1351 BLOCK_ABSTRACT_ORIGIN (SCOPE_STMT_BLOCK (scope_stmt)) = DECL_ORIGIN (fn);
1352
1353 /* Declare the return variable for the function. */
1354 COMPOUND_BODY (stmt)
1355 = chainon (COMPOUND_BODY (stmt),
1356 declare_return_variable (id, return_slot_addr, &use_stmt));
1357 #else /* INLINER_FOR_JAVA */
1358 {
1359 /* Declare the return variable for the function. */
1360 tree decl = declare_return_variable (id, return_slot_addr, &retvar);
1361 if (retvar)
1362 {
1363 tree *next = &BLOCK_VARS (expr);
1364 while (*next)
1365 next = &TREE_CHAIN (*next);
1366 *next = decl;
1367 }
1368 }
1369 #endif /* INLINER_FOR_JAVA */
1370
1371 /* After we've initialized the parameters, we insert the body of the
1372 function itself. */
1373 #ifndef INLINER_FOR_JAVA
1374 inlined_body = &COMPOUND_BODY (stmt);
1375 while (*inlined_body)
1376 inlined_body = &TREE_CHAIN (*inlined_body);
1377 *inlined_body = copy_body (id);
1378 #else /* INLINER_FOR_JAVA */
1379 {
1380 tree new_body;
1381 java_inlining_map_static_initializers (fn, id->decl_map);
1382 new_body = copy_body (id);
1383 TREE_TYPE (new_body) = TREE_TYPE (TREE_TYPE (fn));
1384 BLOCK_EXPR_BODY (expr)
1385 = add_stmt_to_compound (BLOCK_EXPR_BODY (expr),
1386 TREE_TYPE (new_body), new_body);
1387 inlined_body = &BLOCK_EXPR_BODY (expr);
1388 }
1389 #endif /* INLINER_FOR_JAVA */
1390
1391 /* After the body of the function comes the RET_LABEL. This must come
1392 before we evaluate the returned value below, because that evaluation
1393 may cause RTL to be generated. */
1394 #ifndef INLINER_FOR_JAVA
1395 COMPOUND_BODY (stmt)
1396 = chainon (COMPOUND_BODY (stmt),
1397 build_stmt (LABEL_STMT, id->ret_label));
1398 #else /* INLINER_FOR_JAVA */
1399 {
1400 tree label = build1 (LABEL_EXPR, void_type_node, id->ret_label);
1401 BLOCK_EXPR_BODY (expr)
1402 = add_stmt_to_compound (BLOCK_EXPR_BODY (expr), void_type_node, label);
1403 TREE_SIDE_EFFECTS (label) = TREE_SIDE_EFFECTS (t);
1404 }
1405 #endif /* INLINER_FOR_JAVA */
1406
1407 /* Finally, mention the returned value so that the value of the
1408 statement-expression is the returned value of the function. */
1409 #ifndef INLINER_FOR_JAVA
1410 COMPOUND_BODY (stmt) = chainon (COMPOUND_BODY (stmt), use_stmt);
1411
1412 /* Close the block for the parameters. */
1413 scope_stmt = build_stmt (SCOPE_STMT, DECL_INITIAL (fn));
1414 SCOPE_NO_CLEANUPS_P (scope_stmt) = 1;
1415 remap_block (scope_stmt, NULL_TREE, id);
1416 COMPOUND_BODY (stmt)
1417 = chainon (COMPOUND_BODY (stmt), scope_stmt);
1418 #else /* INLINER_FOR_JAVA */
1419 if (retvar)
1420 {
1421 /* Mention the retvar. If the return type of the function was
1422 promoted, convert it back to the expected type. */
1423 if (TREE_TYPE (TREE_TYPE (fn)) != TREE_TYPE (retvar))
1424 retvar = build1 (NOP_EXPR, TREE_TYPE (TREE_TYPE (fn)), retvar);
1425 BLOCK_EXPR_BODY (expr)
1426 = add_stmt_to_compound (BLOCK_EXPR_BODY (expr),
1427 TREE_TYPE (retvar), retvar);
1428 }
1429
1430 java_inlining_merge_static_initializers (fn, id->decl_map);
1431 #endif /* INLINER_FOR_JAVA */
1432
1433 /* Clean up. */
1434 splay_tree_delete (id->decl_map);
1435 id->decl_map = st;
1436
1437 /* The new expression has side-effects if the old one did. */
1438 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (t);
1439
1440 /* Replace the call by the inlined body. Wrap it in an
1441 EXPR_WITH_FILE_LOCATION so that we'll get debugging line notes
1442 pointing to the right place. */
1443 #ifndef INLINER_FOR_JAVA
1444 chain = TREE_CHAIN (*tp);
1445 #endif /* INLINER_FOR_JAVA */
1446 *tp = build_expr_wfl (expr, DECL_SOURCE_FILE (fn), DECL_SOURCE_LINE (fn),
1447 /*col=*/0);
1448 EXPR_WFL_EMIT_LINE_NOTE (*tp) = 1;
1449 #ifndef INLINER_FOR_JAVA
1450 TREE_CHAIN (*tp) = chain;
1451 #endif /* not INLINER_FOR_JAVA */
1452 pop_srcloc ();
1453
1454 /* If the value of the new expression is ignored, that's OK. We
1455 don't warn about this for CALL_EXPRs, so we shouldn't warn about
1456 the equivalent inlined version either. */
1457 TREE_USED (*tp) = 1;
1458
1459 /* Our function now has more statements than it did before. */
1460 DECL_ESTIMATED_INSNS (VARRAY_TREE (id->fns, 0)) += DECL_ESTIMATED_INSNS (fn);
1461 /* For accounting, subtract one for the saved call/ret. */
1462 id->inlined_insns += DECL_ESTIMATED_INSNS (fn) - 1;
1463
1464 /* Update callgraph if needed. */
1465 if (id->decl && flag_unit_at_a_time)
1466 {
1467 cgraph_remove_call (id->decl, fn);
1468 cgraph_create_edges (id->decl, *inlined_body);
1469 }
1470
1471 /* Recurse into the body of the just inlined function. */
1472 {
1473 tree old_decl = id->current_decl;
1474 id->current_decl = fn;
1475 expand_calls_inline (inlined_body, id);
1476 id->current_decl = old_decl;
1477 }
1478 VARRAY_POP (id->fns);
1479
1480 /* If we've returned to the top level, clear out the record of how
1481 much inlining has been done. */
1482 if (VARRAY_ACTIVE_SIZE (id->fns) == id->first_inlined_fn)
1483 id->inlined_insns = 0;
1484
1485 /* Don't walk into subtrees. We've already handled them above. */
1486 *walk_subtrees = 0;
1487
1488 (*lang_hooks.tree_inlining.end_inlining) (fn);
1489
1490 /* Keep iterating. */
1491 return NULL_TREE;
1492 }
1493 /* Walk over the entire tree *TP, replacing CALL_EXPRs with inline
1494 expansions as appropriate. */
1495
1496 static void
1497 expand_calls_inline (tree *tp, inline_data *id)
1498 {
1499 /* Search through *TP, replacing all calls to inline functions by
1500 appropriate equivalents. Use walk_tree in no-duplicates mode
1501 to avoid exponential time complexity. (We can't just use
1502 walk_tree_without_duplicates, because of the special TARGET_EXPR
1503 handling in expand_calls. The hash table is set up in
1504 optimize_function. */
1505 walk_tree (tp, expand_call_inline, id, id->tree_pruner);
1506 }
1507
1508 /* Expand calls to inline functions in the body of FN. */
1509
1510 void
1511 optimize_inline_calls (tree fn)
1512 {
1513 inline_data id;
1514 tree prev_fn;
1515
1516 /* Clear out ID. */
1517 memset (&id, 0, sizeof (id));
1518
1519 id.decl = fn;
1520 id.current_decl = fn;
1521 /* Don't allow recursion into FN. */
1522 VARRAY_TREE_INIT (id.fns, 32, "fns");
1523 VARRAY_PUSH_TREE (id.fns, fn);
1524 if (!DECL_ESTIMATED_INSNS (fn))
1525 DECL_ESTIMATED_INSNS (fn)
1526 = (*lang_hooks.tree_inlining.estimate_num_insns) (fn);
1527 /* Or any functions that aren't finished yet. */
1528 prev_fn = NULL_TREE;
1529 if (current_function_decl)
1530 {
1531 VARRAY_PUSH_TREE (id.fns, current_function_decl);
1532 prev_fn = current_function_decl;
1533 }
1534
1535 prev_fn = ((*lang_hooks.tree_inlining.add_pending_fn_decls)
1536 (&id.fns, prev_fn));
1537
1538 /* Create the list of functions this call will inline. */
1539 VARRAY_TREE_INIT (id.inlined_fns, 32, "inlined_fns");
1540
1541 /* Keep track of the low-water mark, i.e., the point where the first
1542 real inlining is represented in ID.FNS. */
1543 id.first_inlined_fn = VARRAY_ACTIVE_SIZE (id.fns);
1544
1545 /* Replace all calls to inline functions with the bodies of those
1546 functions. */
1547 id.tree_pruner = htab_create (37, htab_hash_pointer,
1548 htab_eq_pointer, NULL);
1549 expand_calls_inline (&DECL_SAVED_TREE (fn), &id);
1550
1551 /* Clean up. */
1552 htab_delete (id.tree_pruner);
1553 if (DECL_LANG_SPECIFIC (fn))
1554 {
1555 tree ifn = make_tree_vec (VARRAY_ACTIVE_SIZE (id.inlined_fns));
1556
1557 if (VARRAY_ACTIVE_SIZE (id.inlined_fns))
1558 memcpy (&TREE_VEC_ELT (ifn, 0), &VARRAY_TREE (id.inlined_fns, 0),
1559 VARRAY_ACTIVE_SIZE (id.inlined_fns) * sizeof (tree));
1560 DECL_INLINED_FNS (fn) = ifn;
1561 }
1562 }
1563
1564 /* FN is a function that has a complete body, and CLONE is a function
1565 whose body is to be set to a copy of FN, mapping argument
1566 declarations according to the ARG_MAP splay_tree. */
1567
1568 void
1569 clone_body (tree clone, tree fn, void *arg_map)
1570 {
1571 inline_data id;
1572
1573 /* Clone the body, as if we were making an inline call. But, remap
1574 the parameters in the callee to the parameters of caller. If
1575 there's an in-charge parameter, map it to an appropriate
1576 constant. */
1577 memset (&id, 0, sizeof (id));
1578 VARRAY_TREE_INIT (id.fns, 2, "fns");
1579 VARRAY_PUSH_TREE (id.fns, clone);
1580 VARRAY_PUSH_TREE (id.fns, fn);
1581 id.decl_map = (splay_tree)arg_map;
1582
1583 /* Cloning is treated slightly differently from inlining. Set
1584 CLONING_P so that it's clear which operation we're performing. */
1585 id.cloning_p = true;
1586
1587 /* Actually copy the body. */
1588 TREE_CHAIN (DECL_SAVED_TREE (clone)) = copy_body (&id);
1589 }
1590
1591 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal.
1592 FUNC is called with the DATA and the address of each sub-tree. If
1593 FUNC returns a non-NULL value, the traversal is aborted, and the
1594 value returned by FUNC is returned. If HTAB is non-NULL it is used
1595 to record the nodes visited, and to avoid visiting a node more than
1596 once. */
1597
1598 tree
1599 walk_tree (tree *tp, walk_tree_fn func, void *data, void *htab_)
1600 {
1601 htab_t htab = (htab_t) htab_;
1602 enum tree_code code;
1603 int walk_subtrees;
1604 tree result;
1605
1606 #define WALK_SUBTREE(NODE) \
1607 do \
1608 { \
1609 result = walk_tree (&(NODE), func, data, htab); \
1610 if (result) \
1611 return result; \
1612 } \
1613 while (0)
1614
1615 #define WALK_SUBTREE_TAIL(NODE) \
1616 do \
1617 { \
1618 tp = & (NODE); \
1619 goto tail_recurse; \
1620 } \
1621 while (0)
1622
1623 tail_recurse:
1624 /* Skip empty subtrees. */
1625 if (!*tp)
1626 return NULL_TREE;
1627
1628 if (htab)
1629 {
1630 void **slot;
1631
1632 /* Don't walk the same tree twice, if the user has requested
1633 that we avoid doing so. */
1634 slot = htab_find_slot (htab, *tp, INSERT);
1635 if (*slot)
1636 return NULL_TREE;
1637 *slot = *tp;
1638 }
1639
1640 /* Call the function. */
1641 walk_subtrees = 1;
1642 result = (*func) (tp, &walk_subtrees, data);
1643
1644 /* If we found something, return it. */
1645 if (result)
1646 return result;
1647
1648 code = TREE_CODE (*tp);
1649
1650 #ifndef INLINER_FOR_JAVA
1651 /* Even if we didn't, FUNC may have decided that there was nothing
1652 interesting below this point in the tree. */
1653 if (!walk_subtrees)
1654 {
1655 if (STATEMENT_CODE_P (code) || code == TREE_LIST
1656 || (*lang_hooks.tree_inlining.tree_chain_matters_p) (*tp))
1657 /* But we still need to check our siblings. */
1658 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
1659 else
1660 return NULL_TREE;
1661 }
1662
1663 /* Handle common cases up front. */
1664 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
1665 #else /* INLINER_FOR_JAVA */
1666 if (code != EXIT_BLOCK_EXPR
1667 && code != SAVE_EXPR
1668 && IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
1669 #endif /* INLINER_FOR_JAVA */
1670 {
1671 int i, len;
1672
1673 #ifndef INLINER_FOR_JAVA
1674 /* Set lineno here so we get the right instantiation context
1675 if we call instantiate_decl from inlinable_function_p. */
1676 if (STATEMENT_CODE_P (code) && !STMT_LINENO_FOR_FN_P (*tp))
1677 input_line = STMT_LINENO (*tp);
1678 #endif /* not INLINER_FOR_JAVA */
1679
1680 /* Walk over all the sub-trees of this operand. */
1681 len = first_rtl_op (code);
1682 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
1683 But, we only want to walk once. */
1684 if (code == TARGET_EXPR
1685 && TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1))
1686 --len;
1687 /* Go through the subtrees. We need to do this in forward order so
1688 that the scope of a FOR_EXPR is handled properly. */
1689 for (i = 0; i < len; ++i)
1690 WALK_SUBTREE (TREE_OPERAND (*tp, i));
1691
1692 #ifndef INLINER_FOR_JAVA
1693 /* For statements, we also walk the chain so that we cover the
1694 entire statement tree. */
1695 if (STATEMENT_CODE_P (code))
1696 {
1697 if (code == DECL_STMT
1698 && DECL_STMT_DECL (*tp)
1699 && DECL_P (DECL_STMT_DECL (*tp)))
1700 {
1701 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
1702 into declarations that are just mentioned, rather than
1703 declared; they don't really belong to this part of the tree.
1704 And, we can see cycles: the initializer for a declaration can
1705 refer to the declaration itself. */
1706 WALK_SUBTREE (DECL_INITIAL (DECL_STMT_DECL (*tp)));
1707 WALK_SUBTREE (DECL_SIZE (DECL_STMT_DECL (*tp)));
1708 WALK_SUBTREE (DECL_SIZE_UNIT (DECL_STMT_DECL (*tp)));
1709 }
1710
1711 /* This can be tail-recursion optimized if we write it this way. */
1712 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
1713 }
1714
1715 #endif /* not INLINER_FOR_JAVA */
1716 /* We didn't find what we were looking for. */
1717 return NULL_TREE;
1718 }
1719 else if (TREE_CODE_CLASS (code) == 'd')
1720 {
1721 WALK_SUBTREE_TAIL (TREE_TYPE (*tp));
1722 }
1723 else if (TREE_CODE_CLASS (code) == 't')
1724 {
1725 WALK_SUBTREE (TYPE_SIZE (*tp));
1726 WALK_SUBTREE (TYPE_SIZE_UNIT (*tp));
1727 /* Also examine various special fields, below. */
1728 }
1729
1730 result = (*lang_hooks.tree_inlining.walk_subtrees) (tp, &walk_subtrees, func,
1731 data, htab);
1732 if (result || ! walk_subtrees)
1733 return result;
1734
1735 /* Not one of the easy cases. We must explicitly go through the
1736 children. */
1737 switch (code)
1738 {
1739 case ERROR_MARK:
1740 case IDENTIFIER_NODE:
1741 case INTEGER_CST:
1742 case REAL_CST:
1743 case VECTOR_CST:
1744 case STRING_CST:
1745 case REAL_TYPE:
1746 case COMPLEX_TYPE:
1747 case VECTOR_TYPE:
1748 case VOID_TYPE:
1749 case BOOLEAN_TYPE:
1750 case UNION_TYPE:
1751 case ENUMERAL_TYPE:
1752 case BLOCK:
1753 case RECORD_TYPE:
1754 case CHAR_TYPE:
1755 /* None of thse have subtrees other than those already walked
1756 above. */
1757 break;
1758
1759 case POINTER_TYPE:
1760 case REFERENCE_TYPE:
1761 WALK_SUBTREE_TAIL (TREE_TYPE (*tp));
1762 break;
1763
1764 case TREE_LIST:
1765 WALK_SUBTREE (TREE_VALUE (*tp));
1766 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
1767 break;
1768
1769 case TREE_VEC:
1770 {
1771 int len = TREE_VEC_LENGTH (*tp);
1772
1773 if (len == 0)
1774 break;
1775
1776 /* Walk all elements but the first. */
1777 while (--len)
1778 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
1779
1780 /* Now walk the first one as a tail call. */
1781 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
1782 }
1783
1784 case COMPLEX_CST:
1785 WALK_SUBTREE (TREE_REALPART (*tp));
1786 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
1787
1788 case CONSTRUCTOR:
1789 WALK_SUBTREE_TAIL (CONSTRUCTOR_ELTS (*tp));
1790
1791 case METHOD_TYPE:
1792 WALK_SUBTREE (TYPE_METHOD_BASETYPE (*tp));
1793 /* Fall through. */
1794
1795 case FUNCTION_TYPE:
1796 WALK_SUBTREE (TREE_TYPE (*tp));
1797 {
1798 tree arg = TYPE_ARG_TYPES (*tp);
1799
1800 /* We never want to walk into default arguments. */
1801 for (; arg; arg = TREE_CHAIN (arg))
1802 WALK_SUBTREE (TREE_VALUE (arg));
1803 }
1804 break;
1805
1806 case ARRAY_TYPE:
1807 WALK_SUBTREE (TREE_TYPE (*tp));
1808 WALK_SUBTREE_TAIL (TYPE_DOMAIN (*tp));
1809
1810 case INTEGER_TYPE:
1811 WALK_SUBTREE (TYPE_MIN_VALUE (*tp));
1812 WALK_SUBTREE_TAIL (TYPE_MAX_VALUE (*tp));
1813
1814 case OFFSET_TYPE:
1815 WALK_SUBTREE (TREE_TYPE (*tp));
1816 WALK_SUBTREE_TAIL (TYPE_OFFSET_BASETYPE (*tp));
1817
1818 #ifdef INLINER_FOR_JAVA
1819 case EXIT_BLOCK_EXPR:
1820 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 1));
1821
1822 case SAVE_EXPR:
1823 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
1824 #endif /* INLINER_FOR_JAVA */
1825
1826 default:
1827 abort ();
1828 }
1829
1830 /* We didn't find what we were looking for. */
1831 return NULL_TREE;
1832
1833 #undef WALK_SUBTREE
1834 #undef WALK_SUBTREE_TAIL
1835 }
1836
1837 /* Like walk_tree, but does not walk duplicate nodes more than
1838 once. */
1839
1840 tree
1841 walk_tree_without_duplicates (tree *tp, walk_tree_fn func, void *data)
1842 {
1843 tree result;
1844 htab_t htab;
1845
1846 htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1847 result = walk_tree (tp, func, data, htab);
1848 htab_delete (htab);
1849 return result;
1850 }
1851
1852 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
1853
1854 tree
1855 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1856 {
1857 enum tree_code code = TREE_CODE (*tp);
1858
1859 /* We make copies of most nodes. */
1860 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
1861 || TREE_CODE_CLASS (code) == 'c'
1862 || code == TREE_LIST
1863 || code == TREE_VEC
1864 || (*lang_hooks.tree_inlining.tree_chain_matters_p) (*tp))
1865 {
1866 /* Because the chain gets clobbered when we make a copy, we save it
1867 here. */
1868 tree chain = TREE_CHAIN (*tp);
1869
1870 /* Copy the node. */
1871 *tp = copy_node (*tp);
1872
1873 /* Now, restore the chain, if appropriate. That will cause
1874 walk_tree to walk into the chain as well. */
1875 if (code == PARM_DECL || code == TREE_LIST
1876 #ifndef INLINER_FOR_JAVA
1877 || (*lang_hooks.tree_inlining.tree_chain_matters_p) (*tp)
1878 || STATEMENT_CODE_P (code))
1879 TREE_CHAIN (*tp) = chain;
1880
1881 /* For now, we don't update BLOCKs when we make copies. So, we
1882 have to nullify all scope-statements. */
1883 if (TREE_CODE (*tp) == SCOPE_STMT)
1884 SCOPE_STMT_BLOCK (*tp) = NULL_TREE;
1885 #else /* INLINER_FOR_JAVA */
1886 || (*lang_hooks.tree_inlining.tree_chain_matters_p) (*tp))
1887 TREE_CHAIN (*tp) = chain;
1888 #endif /* INLINER_FOR_JAVA */
1889 }
1890 else if (TREE_CODE_CLASS (code) == 't' && !variably_modified_type_p (*tp))
1891 /* Types only need to be copied if they are variably modified. */
1892 *walk_subtrees = 0;
1893
1894 return NULL_TREE;
1895 }
1896
1897 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
1898 information indicating to what new SAVE_EXPR this one should be
1899 mapped, use that one. Otherwise, create a new node and enter it in
1900 ST. FN is the function into which the copy will be placed. */
1901
1902 void
1903 remap_save_expr (tree *tp, void *st_, tree fn, int *walk_subtrees)
1904 {
1905 splay_tree st = (splay_tree) st_;
1906 splay_tree_node n;
1907
1908 /* See if we already encountered this SAVE_EXPR. */
1909 n = splay_tree_lookup (st, (splay_tree_key) *tp);
1910
1911 /* If we didn't already remap this SAVE_EXPR, do so now. */
1912 if (!n)
1913 {
1914 tree t = copy_node (*tp);
1915
1916 /* The SAVE_EXPR is now part of the function into which we
1917 are inlining this body. */
1918 SAVE_EXPR_CONTEXT (t) = fn;
1919 /* And we haven't evaluated it yet. */
1920 SAVE_EXPR_RTL (t) = NULL_RTX;
1921 /* Remember this SAVE_EXPR. */
1922 n = splay_tree_insert (st,
1923 (splay_tree_key) *tp,
1924 (splay_tree_value) t);
1925 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
1926 splay_tree_insert (st, (splay_tree_key) t,
1927 (splay_tree_value) error_mark_node);
1928 }
1929 else
1930 /* We've already walked into this SAVE_EXPR, so we needn't do it
1931 again. */
1932 *walk_subtrees = 0;
1933
1934 /* Replace this SAVE_EXPR with the copy. */
1935 *tp = (tree) n->value;
1936 }
1937
1938 #ifdef INLINER_FOR_JAVA
1939 /* Add STMT to EXISTING if possible, otherwise create a new
1940 COMPOUND_EXPR and add STMT to it. */
1941
1942 static tree
1943 add_stmt_to_compound (tree existing, tree type, tree stmt)
1944 {
1945 if (!stmt)
1946 return existing;
1947 else if (existing)
1948 return build (COMPOUND_EXPR, type, existing, stmt);
1949 else
1950 return stmt;
1951 }
1952
1953 #endif /* INLINER_FOR_JAVA */