1 /* Perform optimizations on tree structure.
3 Copyright (C) 1998, 1999 Free Software Foundation, Inc.
4 Written by Mark Michell (mark@codesourcery.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
28 #include "insn-config.h"
29 #include "integrate.h"
34 o Provide debugging information for inlined function bodies.
36 o In order to make inlining-on-trees work, we pessimized
37 function-local static constants. In particular, they are now
38 always output, even when not addressed. Fix this by treating
39 function-local static constants just like global static
40 constants; the back-end already knows not to output them if they
43 o Provide heuristics to clamp inlining of recursive template
46 /* Data required for function inlining. */
48 typedef struct inline_data
50 /* A stack of the functions we are inlining. For example, if we are
51 compiling `f', which calls `g', which calls `h', and we are
52 inlining the body of `h', the stack will contain, `h', followed
53 by `g', followed by `f'. */
55 /* The top of the FNS stack. */
57 /* The label to jump to when a return statement is encountered. */
59 /* The map from local declarations in the inlined function to
60 equivalents in the function into which it is being inlined. */
66 static tree initialize_inlined_parameters PROTO((inline_data *, tree));
67 static tree declare_return_variable PROTO((inline_data *, tree *));
68 static tree copy_body_r PROTO((tree *, int *, void *));
69 static tree copy_body PROTO((inline_data *));
70 static tree expand_call_inline PROTO((tree *, int *, void *));
71 static void expand_calls_inline PROTO((tree *, inline_data *));
72 static int inlinable_function_p PROTO((tree, inline_data *));
74 /* Called from copy_body via walk_tree. DATA is really an
78 copy_body_r (tp, walk_subtrees, data)
87 id = (inline_data *) data;
88 fn = VARRAY_TREE (id->fns, id->fns_top - 1);
90 /* All automatic variables should have a DECL_CONTEXT indicating
91 what function they come from. */
92 if ((TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == LABEL_DECL)
93 && DECL_NAMESPACE_SCOPE_P (*tp))
94 my_friendly_assert (DECL_EXTERNAL (*tp) || TREE_STATIC (*tp),
97 /* If this is a RETURN_STMT, change it into an EXPR_STMT and a
98 GOTO_STMT with the RET_LABEL as its target. */
99 if (TREE_CODE (*tp) == RETURN_STMT)
101 tree return_stmt = *tp;
104 /* Build the GOTO_STMT. */
105 goto_stmt = build_min_nt (GOTO_STMT, id->ret_label);
106 TREE_CHAIN (goto_stmt) = TREE_CHAIN (return_stmt);
108 /* If we're returning something, just turn that into an
109 assignment into the equivalent of the original
111 if (RETURN_EXPR (return_stmt))
113 *tp = build_min_nt (EXPR_STMT,
114 RETURN_EXPR (return_stmt));
115 /* And then jump to the end of the function. */
116 TREE_CHAIN (*tp) = goto_stmt;
118 /* If we're not returning anything just do the jump. */
122 /* Local variables and labels need to be replaced by equivalent
123 variables. We don't want to copy static variables; there's only
124 one of those, no matter how many times we inline the containing
126 else if (nonstatic_local_decl_p (*tp) && DECL_CONTEXT (*tp) == fn)
130 /* Look up the declaration. */
131 n = splay_tree_lookup (id->decl_map, (splay_tree_key) *tp);
133 /* If we didn't already have an equivalent for this declaration,
139 /* Make a copy of the variable or label. */
140 t = copy_decl_for_inlining (*tp, fn,
141 VARRAY_TREE (id->fns, 0));
142 /* Remember it, so that if we encounter this local entity
143 again we can reuse this copy. */
144 n = splay_tree_insert (id->decl_map,
145 (splay_tree_key) *tp,
146 (splay_tree_value) t);
149 /* Replace this variable with the copy. */
150 *tp = (tree) n->value;
152 else if (TREE_CODE (*tp) == SAVE_EXPR)
153 remap_save_expr (tp, id->decl_map, VARRAY_TREE (id->fns, 0));
154 else if (TREE_CODE (*tp) == UNSAVE_EXPR)
155 my_friendly_abort (19991113);
156 /* Otherwise, just copy the node. Note that copy_tree_r already
157 knows not to copy VAR_DECLs, etc., so this is safe. */
160 copy_tree_r (tp, walk_subtrees, NULL);
162 /* The copied TARGET_EXPR has never been expanded, even if the
163 original node was expanded already. */
164 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
165 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
166 /* Similarly, if we're copying a CALL_EXPR, the RTL for the
167 result is no longer valid. */
168 else if (TREE_CODE (*tp) == CALL_EXPR)
169 CALL_EXPR_RTL (*tp) = NULL_RTX;
172 /* Keep iterating. */
176 /* Make a copy of the body of FN so that it can be inserted inline in
185 body = DECL_SAVED_TREE (VARRAY_TREE (id->fns, id->fns_top - 1));
186 walk_tree (&body, copy_body_r, id);
191 /* Generate code to initialize the parameters of the function at the
192 top of the stack in ID from the ARGS (presented as a TREE_LIST). */
195 initialize_inlined_parameters (id, args)
205 /* Figure out what the parameters are. */
206 fn = VARRAY_TREE (id->fns, id->fns_top - 1);
207 parms = DECL_ARGUMENTS (fn);
209 /* Start with no initializations whatsoever. */
210 init_stmts = NULL_TREE;
212 /* Loop through the parameter declarations, replacing each with an
213 equivalent VAR_DECL, appropriately initialized. */
214 for (p = parms, a = args; p; a = TREE_CHAIN (a), p = TREE_CHAIN (p))
219 /* Make an equivalent VAR_DECL. */
220 var = copy_decl_for_inlining (p, fn, VARRAY_TREE (id->fns, 0));
221 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
222 that way, when the PARM_DECL is encountered, it will be
223 automatically replaced by the VAR_DECL. */
224 splay_tree_insert (id->decl_map,
226 (splay_tree_value) var);
227 /* Initialize this VAR_DECL from the equivalent argument. If
228 the argument is an object, created via a constructor or copy,
229 this will not result in an extra copy: the TARGET_EXPR
230 representing the argument will be bound to VAR, and the
231 object will be constructed in VAR. */
232 init_stmt = build_min_nt (EXPR_STMT,
233 build (INIT_EXPR, TREE_TYPE (p),
234 var, TREE_VALUE (a)));
235 /* Declare this new variable. Note that we do this *after* the
236 initialization because we are going to reverse all the
237 initialization statements below. */
238 TREE_CHAIN (init_stmt) = build_min_nt (DECL_STMT, var);
239 /* Add this initialization to the list. */
240 TREE_CHAIN (TREE_CHAIN (init_stmt)) = init_stmts;
241 init_stmts = init_stmt;
244 /* The initialization statements have been built up in reverse
245 order. Straighten them out now. */
246 return nreverse (init_stmts);
249 /* Declare a return variable to replace the RESULT_DECL for the
250 function we are calling. An appropriate DECL_STMT is returned.
251 The USE_STMT is filled in to contain a use of the declaration to
252 indicate the return value of the function. */
255 declare_return_variable (id, use_stmt)
256 struct inline_data *id;
259 tree fn = VARRAY_TREE (id->fns, id->fns_top - 1);
260 tree result = DECL_RESULT (fn);
263 /* We don't need to do anything for functions that don't return
265 if (!result || same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (result)),
268 *use_stmt = NULL_TREE;
272 /* Make an appropriate copy. */
273 var = copy_decl_for_inlining (result, fn, VARRAY_TREE (id->fns, 0));
274 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
275 way, when the RESULT_DECL is encountered, it will be
276 automatically replaced by the VAR_DECL. */
277 splay_tree_insert (id->decl_map,
278 (splay_tree_key) result,
279 (splay_tree_value) var);
281 /* Build the USE_STMT. */
282 *use_stmt = build_min_nt (EXPR_STMT, var);
284 /* Build the declaration statement. */
285 return build_min_nt (DECL_STMT, var);
288 /* Returns non-zero if FN is a function that can be inlined. */
291 inlinable_function_p (fn, id)
297 /* If we've already decided this function shouldn't be inlined,
298 there's no need to check again. */
299 if (DECL_UNINLINABLE (fn))
302 /* Assume it is not inlinable. */
305 /* If the function was not declared `inline', then we don't inline
307 if (!DECL_INLINE (fn))
309 /* If we don't have the function body available, we can't inline
311 else if (!DECL_SAVED_TREE (fn))
313 /* We can't inline varargs functions. */
314 else if (varargs_function_p (fn))
316 /* All is well. We can inline this function. Traditionally, GCC
317 has refused to inline functions using setjmp or alloca, or
318 functions whose values are returned in a PARALLEL, and a few
319 other such obscure conditions. We are not equally constrained at
324 /* Squirrel away the result so that we don't have to check again. */
325 DECL_UNINLINABLE (fn) = !inlinable;
327 /* Don't do recursive inlining, either. We don't record this in
328 DECL_UNLINABLE; we may be able to inline this function later. */
333 for (i = 0; i < id->fns_top; ++i)
334 if (VARRAY_TREE (id->fns, i) == fn)
338 /* We can inline a template instantiation only if its fully
341 && DECL_TEMPLATE_INFO (fn)
342 && TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (fn)))
344 fn = instantiate_decl (fn);
345 inlinable = !TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (fn));
348 /* Return the result. */
352 /* If *TP is CALL_EXPR, replace it with its inline expansion. */
355 expand_call_inline (tp, walk_subtrees, data)
368 /* We're only interested in CALL_EXPRs. */
370 if (TREE_CODE (t) != CALL_EXPR)
373 /* First, see if we can figure out what function is being called.
374 If we cannot, then there is no hope of inlining the function. */
375 fn = get_callee_fndecl (t);
379 /* Don't try to inline functions that are not well-suited to
381 id = (inline_data *) data;
382 if (!inlinable_function_p (fn, id))
385 /* Return statements in the function body will be replaced by jumps
387 id->ret_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
388 DECL_CONTEXT (id->ret_label) = VARRAY_TREE (id->fns, 0);
390 /* Build a statement-expression containing code to initialize the
391 arguments, the actual inline expansion of the body, and a label
392 for the return statements within the function to jump to. The
393 type of the statement expression is the return type of the
395 expr = build_min (STMT_EXPR, TREE_TYPE (TREE_TYPE (fn)), NULL_TREE);
397 /* Record the function we are about to inline so that we can avoid
398 recursing into it. */
399 if (id->fns_top > id->fns->num_elements)
400 VARRAY_GROW (id->fns, 2 * id->fns->num_elements);
401 VARRAY_TREE (id->fns, id->fns_top++) = fn;
403 /* Local declarations will be replaced by their equivalents in this
406 id->decl_map = splay_tree_new (splay_tree_compare_pointers,
409 /* Initialize the parameters. */
410 STMT_EXPR_STMT (expr)
411 = initialize_inlined_parameters (id, TREE_OPERAND (t, 1));
413 /* Declare the return variable for the function. */
414 STMT_EXPR_STMT (expr)
415 = chainon (STMT_EXPR_STMT (expr),
416 declare_return_variable (id, &use_stmt));
418 /* After we've initialized the parameters, we insert the body of the
420 STMT_EXPR_STMT (expr)
421 = chainon (STMT_EXPR_STMT (expr), copy_body (id));
423 /* Finally, mention the returned value so that the value of the
424 statement-expression is the returned value of the function. */
425 STMT_EXPR_STMT (expr) = chainon (STMT_EXPR_STMT (expr), use_stmt);
428 splay_tree_delete (id->decl_map);
431 /* After the body of the function comes the RET_LABEL. */
432 STMT_EXPR_STMT (expr)
433 = chainon (STMT_EXPR_STMT (expr),
434 build_min_nt (LABEL_STMT, id->ret_label));
436 /* The new expression has side-effects if the old one did. */
437 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (t);
438 /* If the value of the new expression is ignored, that's OK. We
439 don't warn about this for CALL_EXPRs, so we shouldn't warn about
440 the equivalent inlined version either. */
441 TREE_USED (expr) = 1;
443 /* Replace the call by the inlined body. */
444 chain = TREE_CHAIN (*tp);
446 TREE_CHAIN (expr) = chain;
448 /* Recurse into the body of the just inlined function. */
449 expand_calls_inline (tp, id);
452 /* Don't walk into subtrees. We've already handled them above. */
455 /* Keep iterating. */
459 /* Walk over the entire tree *TP, replacing CALL_EXPRs with inline
460 expansions as appropriate. */
463 expand_calls_inline (tp, id)
467 /* Search through *TP, replacing all calls to inline functions by
468 appropriate equivalents. */
469 walk_tree (tp, expand_call_inline, id);
472 /* Optimize the body of FN. */
475 optimize_function (fn)
478 /* Expand calls to inline functions. */
479 if (flag_inline_trees)
484 bzero (&id, sizeof (id));
486 /* Don't allow recursion into FN. */
487 VARRAY_TREE_INIT (id.fns, 32, "fns");
488 VARRAY_TREE (id.fns, id.fns_top++) = fn;
490 /* Replace all calls to inline functions with the bodies of those
492 expand_calls_inline (&DECL_SAVED_TREE (fn), &id);
495 VARRAY_FREE (id.fns);