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