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