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