From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7548 invoked by alias); 1 Jul 2002 18:29:55 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 7526 invoked from network); 1 Jul 2002 18:29:50 -0000 Received: from unknown (HELO executor.cambridge.redhat.com) (195.224.55.237) by sources.redhat.com with SMTP; 1 Jul 2002 18:29:50 -0000 Received: from cuddles.cambridge.redhat.com (vpn50-1.rdu.redhat.com [172.16.50.1]) by executor.cambridge.redhat.com (Postfix) with ESMTP id 45146ABAFC; Mon, 1 Jul 2002 19:29:44 +0100 (BST) Received: (from aph@localhost) by cuddles.cambridge.redhat.com (8.11.6/8.11.0) id g61IToC16027; Mon, 1 Jul 2002 19:29:50 +0100 X-Authentication-Warning: cuddles.cambridge.redhat.com: aph set sender to aph@cambridge.redhat.com using -f From: Andrew Haley Message-ID: <15648.40862.670360.728844@cuddles.cambridge.redhat.com> Date: Mon, 01 Jul 2002 11:29:00 -0000 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="i0GnLXmr/X" Content-Transfer-Encoding: 7bit To: "gcc-patches@gcc.gnu.org" , "java@gcc.gnu.org" Subject: Java inliner In-Reply-To: <15648.40350.493795.623862@cuddles.cambridge.redhat.com> References: <3D07096A.5010102@bothner.com> <5620000.1023887664@warlock.codesourcery.com> <15648.40350.493795.623862@cuddles.cambridge.redhat.com> X-SW-Source: 2002-07/txt/msg00023.txt.bz2 --i0GnLXmr/X Content-Type: text/plain; charset=us-ascii Content-Description: message body text Content-Transfer-Encoding: 7bit Content-length: 568 Tree inlining, tested with no regressions, builds working libgcj on i686-linux-gnu. It's not yet perfectly integrated into the compiler in that it's still sensitive to the order in which methods are compiled. However, even with that limitation it's still useful. This can be fixed at some later date by reorganizing the way that gcj does things and deferring code generation until we have trees for everything. Also, it's only hooked into the source compiler. This patch fixes a performance regression that was caused by the RTL inliner being disabled. Andrew. --i0GnLXmr/X Content-Type: text/plain Content-Disposition: inline; filename="java-tree-inline" Content-Transfer-Encoding: 7bit Content-length: 55730 2002-06-28 Andrew Haley * java-tree-inline.c: New file. * parse.y (source_end_java_method): Call java_optimize_inline. (java_expand_method_bodies): Save method's tree in DECL_SAVED_TREE. (add_stmt_to_compound): Keep track of the number of statments. * lang.c (java_init): Enable flag_inline_trees. (java_post_options): If flag_inline_functions is on, enable flag_inline_trees instread. (decl_constant_value): New. (java_tree_inlining_walk_subtrees): New. * java-tree.h (DECL_NUM_STMTS): New macro. (java_optimize_inline): Declare. * expr.c (java_expand_expr): Allow a BLOCK to return a value. Handle a LABEL_EXPR> * decl.c (build_result_decl): If we already have a DECL_RESULT don't make another. (dump_function): New. (java_optimize_inline): New. (dump_function): New. * Make-lang.in: Add java/java-tree-inline.o. 2002-06-26 Kaveh R. Ghazi Index: java/Make-lang.in =================================================================== RCS file: /cvs/gcc/gcc/gcc/java/Make-lang.in,v retrieving revision 1.88 diff -p -2 -c -r1.88 Make-lang.in *** java/Make-lang.in 11 Jun 2002 14:58:09 -0000 1.88 --- java/Make-lang.in 1 Jul 2002 18:12:19 -0000 *************** JAVA_OBJS = java/parse.o java/class.o ja *** 110,114 **** java/mangle_name.o java/builtins.o \ java/jcf-write.o java/buffer.o java/check-init.o java/jcf-depend.o \ ! java/jcf-path.o java/xref.o java/boehm.o mkdeps.o GCJH_OBJS = java/gjavah.o java/jcf-io.o java/jcf-depend.o java/jcf-path.o \ --- 110,114 ---- java/mangle_name.o java/builtins.o \ java/jcf-write.o java/buffer.o java/check-init.o java/jcf-depend.o \ ! java/jcf-path.o java/xref.o java/boehm.o java/java-tree-inline.o mkdeps.o GCJH_OBJS = java/gjavah.o java/jcf-io.o java/jcf-depend.o java/jcf-path.o \ *************** java/expr.o: java/expr.c $(CONFIG_H) $(J *** 289,292 **** --- 289,296 ---- java/java-except.h java/java-except.h java/parse.h toplev.h \ $(SYSTEM_H) $(GGC_H) gt-java-expr.h + java/java-tree-inline.o: $(CONFIG_H) $(SYSTEM_H) $(TREE_H) $(RTL_H) \ + expr.h flags.h params.h input.h insn-config.h $(INTEGRATE_H) \ + $(VARRAY_H) $(HASHTAB_H) $(SPLAY_TREE_H) toplev.h langhooks.h \ + $(C_COMMON_H) tree-inline.h java/jcf-depend.o: java/jcf-depend.c $(CONFIG_H) $(SYSTEM_H) java/jcf.h java/jcf-parse.o: java/jcf-parse.c $(CONFIG_H) $(JAVA_TREE_H) flags.h \ Index: java/decl.c =================================================================== RCS file: /cvs/gcc/gcc/gcc/java/decl.c,v retrieving revision 1.129 diff -p -2 -c -r1.129 decl.c *** java/decl.c 21 Jun 2002 20:18:21 -0000 1.129 --- java/decl.c 1 Jul 2002 18:12:20 -0000 *************** The Free Software Foundation is independ *** 42,48 **** #include "java-except.h" #include "ggc.h" #if defined (DEBUG_JAVA_BINDING_LEVELS) ! extern void indent PROTO((void)); #endif --- 42,50 ---- #include "java-except.h" #include "ggc.h" + #include "timevar.h" + #include "tree-inline.h" #if defined (DEBUG_JAVA_BINDING_LEVELS) ! extern void indent PARAMS ((void)); #endif *************** static tree create_primitive_vtable PARA *** 54,57 **** --- 56,60 ---- static tree check_local_named_variable PARAMS ((tree, tree, int, int *)); static tree check_local_unnamed_variable PARAMS ((tree, tree, tree)); + static void dump_function PARAMS ((enum tree_dump_index, tree)); /* Set to non-zero value in order to emit class initilization code *************** build_result_decl (fndecl) *** 1659,1667 **** { tree restype = TREE_TYPE (TREE_TYPE (fndecl)); ! /* To be compatible with C_PROMOTING_INTEGER_TYPE_P in cc1/cc1plus. */ ! if (INTEGRAL_TYPE_P (restype) ! && TYPE_PRECISION (restype) < TYPE_PRECISION (integer_type_node)) ! restype = integer_type_node; ! return (DECL_RESULT (fndecl) = build_decl (RESULT_DECL, NULL_TREE, restype)); } --- 1662,1677 ---- { tree restype = TREE_TYPE (TREE_TYPE (fndecl)); ! tree result = DECL_RESULT (fndecl); ! if (! result) ! { ! /* To be compatible with C_PROMOTING_INTEGER_TYPE_P in cc1/cc1plus. */ ! if (INTEGRAL_TYPE_P (restype) ! && TYPE_PRECISION (restype) < TYPE_PRECISION (integer_type_node)) ! restype = integer_type_node; ! result = build_decl (RESULT_DECL, NULL_TREE, restype); ! DECL_CONTEXT (result) = fndecl; ! DECL_RESULT (fndecl) = result; ! } ! return result; } *************** end_java_method () *** 1820,1823 **** --- 1830,1884 ---- current_function_decl = NULL_TREE; + } + + /* Dump FUNCTION_DECL FN as tree dump PHASE. */ + + static void + dump_function (phase, fn) + enum tree_dump_index phase; + tree fn; + { + FILE *stream; + int flags; + + stream = dump_begin (phase, &flags); + if (stream) + { + dump_node (fn, TDF_SLIM | flags, stream); + dump_end (phase, stream); + } + } + + void java_optimize_inline (fndecl) + tree fndecl; + { + if (flag_inline_trees) + { + int uninlinable; + /* First, cache whether the current function is inlinable. Some + predicates depend on cfun and current_function_decl to + function completely. */ + timevar_push (TV_INTEGRATION); + uninlinable = ! tree_inlinable_function_p (fndecl); + + #if 0 + if (! uninlinable + /* Save function tree for inlining. Should return 0 if the + language does not support function deferring or the + function could not be deferred. */ + && defer_fn (fndecl)) + { + /* Let the back-end know that this function exists. */ + (*debug_hooks->deferred_inline_function) (fndecl); + timevar_pop (TV_INTEGRATION); + return; + } + #endif + + /* Then, inline any functions called in it. */ + optimize_inline_calls (fndecl); + timevar_pop (TV_INTEGRATION); + dump_function (TDI_inlined, fndecl); + } } Index: java/expr.c =================================================================== RCS file: /cvs/gcc/gcc/gcc/java/expr.c,v retrieving revision 1.148 diff -p -2 -c -r1.148 expr.c *** java/expr.c 25 Jun 2002 13:27:19 -0000 1.148 --- java/expr.c 1 Jul 2002 18:12:22 -0000 *************** java_expand_expr (exp, target, tmode, mo *** 2536,2539 **** --- 2536,2540 ---- { tree local; + rtx last; tree body = BLOCK_EXPR_BODY (exp); /* Set to 1 or more when we found a static class *************** java_expand_expr (exp, target, tmode, mo *** 2569,2577 **** body = TREE_OPERAND (body, 1); } ! expand_expr (body, const0_rtx, VOIDmode, 0); emit_queue (); expand_end_bindings (getdecls (), 1, 0); poplevel (1, 1, 0); ! return const0_rtx; } return const0_rtx; --- 2570,2578 ---- body = TREE_OPERAND (body, 1); } ! last = expand_expr (body, NULL_RTX, VOIDmode, 0); emit_queue (); expand_end_bindings (getdecls (), 1, 0); poplevel (1, 1, 0); ! return last; } return const0_rtx; *************** java_expand_expr (exp, target, tmode, mo *** 2629,2632 **** --- 2630,2638 ---- return expand_expr (build_exception_object_ref (TREE_TYPE (exp)), target, tmode, modifier); + + case LABEL_EXPR: + /* Used only by expanded inline functions. */ + expand_label (TREE_OPERAND (exp, 0)); + return const0_rtx; default: Index: java/java-tree-inline.c =================================================================== RCS file: java/java-tree-inline.c diff -N java/java-tree-inline.c *** java/java-tree-inline.c 1 Jan 1970 00:00:00 -0000 --- java/java-tree-inline.c 1 Jul 2002 18:12:24 -0000 *************** *** 0 **** --- 1,1275 ---- + /* Inliner for Java trees. + Copyright 2001, 2002 Free Software Foundation, Inc. + Contributed by Andrew Haley + Based on work by by Alexandre Oliva + + This file is part of GNU CC. + + GNU CC is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + GNU CC is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU CC; see the file COPYING. If not, write to + the Free Software Foundation, 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + + #include "config.h" + #include "system.h" + #include "toplev.h" + #include "tree.h" + #include "tree-inline.h" + #include "rtl.h" + #include "expr.h" + #include "flags.h" + #include "params.h" + #include "input.h" + #include "insn-config.h" + #include "integrate.h" + #include "varray.h" + #include "hashtab.h" + #include "splay-tree.h" + #include "langhooks.h" + #include "parse.h" + + /* This will be be generalized to other languages, but the shared + function-as-trees infrastructure doesn't yet exist. */ + #include "java-tree.h" + + /* 0 if we should not perform inlining. + 1 if we should expand functions calls inline at the tree level. + 2 if we should consider *all* functions to be inline + candidates. */ + + int flag_inline_trees = 0; + + /* Data required for function inlining. */ + + typedef struct inline_data + { + /* A stack of the functions we are inlining. For example, if we are + compiling `f', which calls `g', which calls `h', and we are + inlining the body of `h', the stack will contain, `h', followed + by `g', followed by `f'. The first few elements of the stack may + contain other functions that we know we should not recurse into, + even though they are not directly being inlined. */ + varray_type fns; + /* The index of the first element of FNS that really represents an + inlined function. */ + unsigned first_inlined_fn; + /* The label to jump to when a return statement is encountered. If + this value is NULL, then return statements will simply be + remapped as return statements, rather than as jumps. */ + tree ret_label; + /* The map from local declarations in the inlined function to + equivalents in the function into which it is being inlined. */ + splay_tree decl_map; + /* A list of the functions current function has inlined. */ + varray_type inlined_fns; + /* The approximate number of statements we have inlined in the + current call stack. */ + int inlined_stmts; + /* We use the same mechanism to build clones that we do to perform + inlining. However, there are a few places where we need to + distinguish between those two situations. This flag is true if + we are cloning, rather than inlining. */ + bool cloning_p; + /* Hash table used to prevent walk_tree from visiting the same node + umpteen million times. */ + htab_t tree_pruner; + } inline_data; + + /* Prototypes. */ + + static tree initialize_inlined_parameters PARAMS ((inline_data *, tree, tree, tree)); + static tree declare_return_variable PARAMS ((inline_data *, tree *)); + static tree copy_body_r PARAMS ((tree *, int *, void *)); + static tree copy_body PARAMS ((inline_data *)); + static tree expand_call_inline PARAMS ((tree *, int *, void *)); + static void expand_calls_inline PARAMS ((tree *, inline_data *)); + static int inlinable_function_p PARAMS ((tree, inline_data *)); + static tree remap_decl PARAMS ((tree, inline_data *)); + static void remap_block PARAMS ((tree *, tree, inline_data *)); + static tree add_stmt_to_compound PARAMS ((tree, tree, tree)); + + /* The approximate number of instructions per statement. This number + need not be particularly accurate; it is used only to make + decisions about when a function is too big to inline. */ + #define INSNS_PER_STMT (10) + + /* Remap DECL during the copying of the BLOCK tree for the function. */ + + static tree + remap_decl (decl, id) + tree decl; + inline_data *id; + { + splay_tree_node n; + tree fn; + + /* We only remap local variables in the current function. */ + fn = VARRAY_TOP_TREE (id->fns); + if (! (*lang_hooks.tree_inlining.auto_var_in_fn_p) (decl, fn)) + return NULL_TREE; + + /* See if we have remapped this declaration. */ + n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl); + /* If we didn't already have an equivalent for this declaration, + create one now. */ + if (!n) + { + tree t; + + /* Make a copy of the variable or label. */ + t = copy_decl_for_inlining (decl, fn, + VARRAY_TREE (id->fns, 0)); + + /* The decl T could be a dynamic array or other variable size type, + in which case some fields need to be remapped because they may + contain SAVE_EXPRs. */ + if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE + && TYPE_DOMAIN (TREE_TYPE (t))) + { + TREE_TYPE (t) = copy_node (TREE_TYPE (t)); + TYPE_DOMAIN (TREE_TYPE (t)) + = copy_node (TYPE_DOMAIN (TREE_TYPE (t))); + walk_tree (&TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t))), + copy_body_r, id, NULL); + } + + /* Remember it, so that if we encounter this local entity + again we can reuse this copy. */ + n = splay_tree_insert (id->decl_map, + (splay_tree_key) decl, + (splay_tree_value) t); + } + + return (tree) n->value; + } + + /* Copy the BLOCK to contain remapped versions of the variables + therein. And hook the new block into the block-tree. */ + + static void + remap_block (block, decls, id) + tree *block; + tree decls; + inline_data *id; + { + tree old_block; + tree new_block; + tree old_var; + tree fn; + + /* Make the new block. */ + old_block = *block; + new_block = make_node (BLOCK); + TREE_USED (new_block) = TREE_USED (old_block); + BLOCK_ABSTRACT_ORIGIN (new_block) = old_block; + BLOCK_SUBBLOCKS (new_block) = BLOCK_SUBBLOCKS (old_block); + TREE_SIDE_EFFECTS (new_block) = TREE_SIDE_EFFECTS (old_block); + TREE_TYPE (new_block) = TREE_TYPE (old_block); + *block = new_block; + + /* Remap its variables. */ + for (old_var = decls ? decls : BLOCK_VARS (old_block); + old_var; + old_var = TREE_CHAIN (old_var)) + { + tree new_var; + + /* Remap the variable. */ + new_var = remap_decl (old_var, id); + /* If we didn't remap this variable, so we can't mess with + its TREE_CHAIN. If we remapped this variable to + something other than a declaration (say, if we mapped it + to a constant), then we must similarly omit any mention + of it here. */ + if (!new_var || !DECL_P (new_var)) + ; + else + { + TREE_CHAIN (new_var) = BLOCK_VARS (new_block); + BLOCK_VARS (new_block) = new_var; + } + } + /* We put the BLOCK_VARS in reverse order; fix that now. */ + BLOCK_VARS (new_block) = nreverse (BLOCK_VARS (new_block)); + fn = VARRAY_TREE (id->fns, 0); + /* Remember the remapped block. */ + splay_tree_insert (id->decl_map, + (splay_tree_key) old_block, + (splay_tree_value) new_block); + } + + static tree + copy_body_r (tp, walk_subtrees, data) + tree *tp; + int *walk_subtrees; + void *data; + { + inline_data* id; + tree fn; + + /* Set up. */ + id = (inline_data *) data; + fn = VARRAY_TOP_TREE (id->fns); + + #if 0 + /* All automatic variables should have a DECL_CONTEXT indicating + what function they come from. */ + if ((TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == LABEL_DECL) + && DECL_NAMESPACE_SCOPE_P (*tp)) + if (! DECL_EXTERNAL (*tp) && ! TREE_STATIC (*tp)) + abort (); + #endif + + if (TREE_CODE (*tp) == BLOCK) + remap_block (tp, NULL_TREE, id); + /* If this is a RETURN_EXPR, change it into an assignment and a + GOTO_EXPR with the RET_LABEL as its target. */ + if (TREE_CODE (*tp) == RETURN_EXPR && id->ret_label) + { + tree return_stmt = *tp; + tree goto_stmt; + + goto_stmt = build1 (GOTO_EXPR, void_type_node, id->ret_label); + TREE_SIDE_EFFECTS (goto_stmt) = 1; + + { + /* If we're returning something, just turn that into an + assignment into the equivalent of the original + RESULT_DECL. */ + + tree assignment = TREE_OPERAND (return_stmt, 0); + if (assignment) + { + copy_body_r (&assignment, walk_subtrees, data); + *tp = build (COMPOUND_EXPR, void_type_node, assignment, goto_stmt); + TREE_SIDE_EFFECTS (*tp) = 1; + } + else + { + /* If we're not returning anything just do the jump. */ + *tp = goto_stmt; + } + } + } + /* Local variables and labels need to be replaced by equivalent + variables. We don't want to copy static variables; there's only + one of those, no matter how many times we inline the containing + function. */ + else if ((*lang_hooks.tree_inlining.auto_var_in_fn_p) (*tp, fn)) + { + tree new_decl; + + /* Remap the declaration. */ + new_decl = remap_decl (*tp, id); + if (! new_decl) + abort (); + /* Replace this variable with the copy. */ + STRIP_TYPE_NOPS (new_decl); + *tp = new_decl; + } + #if 0 + else if (nonstatic_local_decl_p (*tp) + && DECL_CONTEXT (*tp) != VARRAY_TREE (id->fns, 0)) + abort (); + #endif + else if (TREE_CODE (*tp) == SAVE_EXPR) + remap_save_expr (tp, id->decl_map, VARRAY_TREE (id->fns, 0), + walk_subtrees); + else if (TREE_CODE (*tp) == UNSAVE_EXPR) + /* UNSAVE_EXPRs should not be generated until expansion time. */ + abort (); + else if (TREE_CODE (*tp) == LABELED_BLOCK_EXPR) + { + /* We need a new copy of this labeled block; the EXIT_BLOCK_EXPR + will refer to it, so save a copy ready for remapping. We + save it in the decl_map, although it isn't a decl. */ + tree new_block = copy_node (*tp); + splay_tree_insert (id->decl_map, + (splay_tree_key) *tp, + (splay_tree_value) new_block); + *tp = new_block; + } + else if (TREE_CODE (*tp) == EXIT_BLOCK_EXPR) + { + splay_tree_node n + = splay_tree_lookup (id->decl_map, + (splay_tree_key) TREE_OPERAND (*tp, 0)); + /* We _must_ have seen the enclosing LABELED_BLOCK_EXPR. */ + if (! n) + abort (); + *tp = copy_node (*tp); + TREE_OPERAND (*tp, 0) = (tree) n->value; + } + /* Otherwise, just copy the node. Note that copy_tree_r already + knows not to copy VAR_DECLs, etc., so this is safe. */ + else + { + copy_tree_r (tp, walk_subtrees, NULL); + + /* The copied TARGET_EXPR has never been expanded, even if the + original node was expanded already. */ + if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3)) + { + TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3); + TREE_OPERAND (*tp, 3) = NULL_TREE; + } + else if (TREE_CODE (*tp) == MODIFY_EXPR + && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1) + && ((*lang_hooks.tree_inlining.auto_var_in_fn_p) + (TREE_OPERAND (*tp, 0), fn))) + { + /* Some assignments VAR = VAR; don't generate any rtl code + and thus don't count as variable modification. Avoid + keeping bogosities like 0 = 0. */ + tree decl = TREE_OPERAND (*tp, 0), value; + splay_tree_node n; + + n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl); + if (n) + { + value = (tree) n->value; + STRIP_TYPE_NOPS (value); + if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value)) + *tp = value; + } + } + } + + /* Keep iterating. */ + return NULL_TREE; + } + + /* Make a copy of the body of FN so that it can be inserted inline in + another function. */ + + static tree + copy_body (id) + inline_data *id; + { + tree body; + + body = DECL_SAVED_TREE (VARRAY_TOP_TREE (id->fns)); + walk_tree (&body, copy_body_r, id, NULL); + + return body; + } + + /* Generate code to initialize the parameters of the function at the + top of the stack in ID from the ARGS (presented as a TREE_LIST). */ + + static tree + initialize_inlined_parameters (id, args, fn, block) + inline_data *id; + tree args; + tree fn; + tree block; + { + tree init_stmts; + tree vars; + tree parms; + tree a; + tree p; + + /* Figure out what the parameters are. */ + parms = DECL_ARGUMENTS (fn); + + /* Start with no initializations whatsoever. */ + init_stmts = NULL_TREE; + vars = NULL_TREE; + + /* Loop through the parameter declarations, replacing each with an + equivalent VAR_DECL, appropriately initialized. */ + for (p = parms, a = args; p; + a = a ? TREE_CHAIN (a) : a, p = TREE_CHAIN (p)) + { + tree var; + tree value; + tree cleanup; + + /* Find the initializer. */ + value = (*lang_hooks.tree_inlining.convert_parm_for_inlining) + (p, a ? TREE_VALUE (a) : NULL_TREE, fn); + + /* If the parameter is never assigned to, we may not need to + create a new variable here at all. Instead, we may be able + to just use the argument value. */ + if (TREE_READONLY (p) + && !TREE_ADDRESSABLE (p) + && value && !TREE_SIDE_EFFECTS (value)) + { + /* Simplify the value, if possible. */ + value = fold (DECL_P (value) ? decl_constant_value (value) : value); + + /* We can't risk substituting complex expressions. They + might contain variables that will be assigned to later. + Theoretically, we could check the expression to see if + all of the variables that determine its value are + read-only, but we don't bother. */ + if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value)) + { + /* If this is a declaration, wrap it a NOP_EXPR so that + we don't try to put the VALUE on the list of + BLOCK_VARS. */ + if (DECL_P (value)) + value = build1 (NOP_EXPR, TREE_TYPE (value), value); + + splay_tree_insert (id->decl_map, + (splay_tree_key) p, + (splay_tree_value) value); + continue; + } + } + + /* Make an equivalent VAR_DECL. */ + var = copy_decl_for_inlining (p, fn, VARRAY_TREE (id->fns, 0)); + /* Register the VAR_DECL as the equivalent for the PARM_DECL; + that way, when the PARM_DECL is encountered, it will be + automatically replaced by the VAR_DECL. */ + splay_tree_insert (id->decl_map, + (splay_tree_key) p, + (splay_tree_value) var); + + /* Declare this new variable. */ + TREE_CHAIN (var) = vars; + vars = var; + + /* Initialize this VAR_DECL from the equivalent argument. If + the argument is an object, created via a constructor or copy, + this will not result in an extra copy: the TARGET_EXPR + representing the argument will be bound to VAR, and the + object will be constructed in VAR. */ + if (! TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p))) + { + tree assignment = build (MODIFY_EXPR, TREE_TYPE (p), var, value); + init_stmts = add_stmt_to_compound (init_stmts, TREE_TYPE (p), + assignment); + } + else + { + /* Java objects don't ever need constructing when being + passed as arguments because only call by reference is + supported. */ + abort (); + } + + /* See if we need to clean up the declaration. */ + cleanup = (*lang_hooks.maybe_build_cleanup) (var); + if (cleanup) + { + abort (); + } + } + + BLOCK_VARS (block) = nreverse (vars); + return init_stmts; + } + + /* Declare a return variable to replace the RESULT_DECL for the + function we are calling. An appropriate decl is returned. */ + + static tree + declare_return_variable (id, var) + struct inline_data *id; + tree *var; + { + tree fn = VARRAY_TOP_TREE (id->fns); + tree result = DECL_RESULT (fn); + int need_return_decl = 1; + + /* We don't need to do anything for functions that don't return + anything. */ + if (!result || VOID_TYPE_P (TREE_TYPE (result))) + { + *var = NULL_TREE; + return NULL_TREE; + } + + *var = ((*lang_hooks.tree_inlining.copy_res_decl_for_inlining) + (result, fn, VARRAY_TREE (id->fns, 0), id->decl_map, + &need_return_decl, NULL_TREE)); + + /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that + way, when the RESULT_DECL is encountered, it will be + automatically replaced by the VAR_DECL. */ + splay_tree_insert (id->decl_map, + (splay_tree_key) result, + (splay_tree_value) *var); + + DECL_IGNORED_P (*var) = 1; + + /* Build the declaration statement if FN does not return an + aggregate. */ + if (need_return_decl) + return *var; + /* If FN does return an aggregate, there's no need to declare the + return variable; we're using a variable in our caller's frame. */ + else + return NULL_TREE; + } + + /* Returns non-zero if a function can be inlined as a tree. */ + + int + tree_inlinable_function_p (fn) + tree fn; + { + return inlinable_function_p (fn, NULL); + } + + /* Returns non-zero if FN is a function that can be inlined into the + inlining context ID_. If ID_ is NULL, check whether the function + can be inlined at all. */ + + static int + inlinable_function_p (fn, id) + tree fn; + inline_data *id; + { + int inlinable; + int currfn_insns; + + /* If we've already decided this function shouldn't be inlined, + there's no need to check again. */ + if (DECL_UNINLINABLE (fn)) + return 0; + + /* Assume it is not inlinable. */ + inlinable = 0; + + /* The number of instructions (estimated) of current function. */ + currfn_insns = DECL_NUM_STMTS (fn) * INSNS_PER_STMT; + + /* If we're not inlining things, then nothing is inlinable. */ + if (! flag_inline_trees) + ; + /* If we're not inlining all functions and the function was not + declared `inline', we don't inline it. Don't think of + disregarding DECL_INLINE when flag_inline_trees == 2; it's the + front-end that must set DECL_INLINE in this case, because + dwarf2out loses if a function is inlined that doesn't have + DECL_INLINE set. */ + else if (! DECL_INLINE (fn)) + ; + /* We can't inline functions that are too big. Only allow a single + function to be of MAX_INLINE_INSNS_SINGLE size. Make special + allowance for extern inline functions, though. */ + else if (! (*lang_hooks.tree_inlining.disregard_inline_limits) (fn) + && currfn_insns > MAX_INLINE_INSNS_SINGLE) + ; + /* All is well. We can inline this function. Traditionally, GCC + has refused to inline functions using alloca, or functions whose + values are returned in a PARALLEL, and a few other such obscure + conditions. We are not equally constrained at the tree level. */ + else + inlinable = 1; + + /* Squirrel away the result so that we don't have to check again. */ + DECL_UNINLINABLE (fn) = ! inlinable; + + /* In case we don't disregard the inlining limits and we basically + can inline this function, investigate further. */ + if (! (*lang_hooks.tree_inlining.disregard_inline_limits) (fn) + && inlinable) + { + int sum_insns = (id ? id->inlined_stmts : 0) * INSNS_PER_STMT + + currfn_insns; + /* In the extreme case that we have exceeded the recursive inlining + limit by a huge factor (128), we just say no. Should not happen + in real life. */ + if (sum_insns > MAX_INLINE_INSNS * 128) + inlinable = 0; + /* If we did not hit the extreme limit, we use a linear function + with slope -1/MAX_INLINE_SLOPE to exceedingly decrease the + allowable size. We always allow a size of MIN_INLINE_INSNS + though. */ + else if ((sum_insns > MAX_INLINE_INSNS) + && (currfn_insns > MIN_INLINE_INSNS)) + { + int max_curr = MAX_INLINE_INSNS_SINGLE + - (sum_insns - MAX_INLINE_INSNS) / MAX_INLINE_SLOPE; + if (currfn_insns > max_curr) + inlinable = 0; + } + } + + if (inlinable && (*lang_hooks.tree_inlining.cannot_inline_tree_fn) (&fn)) + inlinable = 0; + + /* If we don't have the function body available, we can't inline + it. */ + if (! DECL_SAVED_TREE (fn)) + inlinable = 0; + + /* Check again, language hooks may have modified it. */ + if (! inlinable || DECL_UNINLINABLE (fn)) + return 0; + + /* Don't do recursive inlining, either. We don't record this in + DECL_UNINLINABLE; we may be able to inline this function later. */ + if (id) + { + size_t i; + + for (i = 0; i < VARRAY_ACTIVE_SIZE (id->fns); ++i) + if (VARRAY_TREE (id->fns, i) == fn) + return 0; + + if (DECL_INLINED_FNS (fn)) + { + int j; + tree inlined_fns = DECL_INLINED_FNS (fn); + + for (j = 0; j < TREE_VEC_LENGTH (inlined_fns); ++j) + if (TREE_VEC_ELT (inlined_fns, j) == VARRAY_TREE (id->fns, 0)) + return 0; + } + } + + /* Return the result. */ + return inlinable; + } + + /* If *TP is a CALL_EXPR, replace it with its inline expansion. */ + + static tree + expand_call_inline (tp, walk_subtrees, data) + tree *tp; + int *walk_subtrees; + void *data; + { + inline_data *id; + tree t; + tree expr; + tree stmt; + tree fn; + tree retvar; + tree arg_inits; + tree *inlined_body; + splay_tree st; + + /* See what we've got. */ + id = (inline_data *) data; + t = *tp; + + if (TREE_CODE (*tp) == TARGET_EXPR) + { + abort (); + } + + if (TYPE_P (t)) + /* Because types were not copied in copy_body, CALL_EXPRs beneath + them should not be expanded. This can happen if the type is a + dynamic array type, for example. */ + *walk_subtrees = 0; + + /* From here on, we're only interested in CALL_EXPRs. */ + if (TREE_CODE (t) != CALL_EXPR) + return NULL_TREE; + + /* First, see if we can figure out what function is being called. + If we cannot, then there is no hope of inlining the function. */ + fn = get_callee_fndecl (t); + if (!fn) + return NULL_TREE; + + /* If fn is a declaration of a function in a nested scope that was + globally declared inline, we don't set its DECL_INITIAL. + However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the + C++ front-end uses it for cdtors to refer to their internal + declarations, that are not real functions. Fortunately those + don't have trees to be saved, so we can tell by checking their + DECL_SAVED_TREE. */ + if (! DECL_INITIAL (fn) + && DECL_ABSTRACT_ORIGIN (fn) + && DECL_SAVED_TREE (DECL_ABSTRACT_ORIGIN (fn))) + fn = DECL_ABSTRACT_ORIGIN (fn); + + /* Don't try to inline functions that are not well-suited to + inlining. */ + if (!inlinable_function_p (fn, id)) + return NULL_TREE; + + if (! (*lang_hooks.tree_inlining.start_inlining) (fn)) + return NULL_TREE; + + #if 0 + /* Useful if you want to know eactly what is being inlined. */ + { + const char *name = IDENTIFIER_POINTER (DECL_NAME (fn)); + FILE *f = fopen ("/tmp/foo", "a"); + fprintf (f, "inlining %s in %s\n", name, + IDENTIFIER_POINTER (DECL_NAME (current_function_decl))); + fclose (f); + } + #endif + + /* Set the current filename and line number to the function we are + inlining so that when we create new _EXPR nodes here they get + line numbers corresponding to the function we are calling. We + wrap the whole inlined body in an EXPR_WITH_FILE_AND_LINE as well + because individual statements don't record the filename. */ + push_srcloc (DECL_SOURCE_FILE (fn), DECL_SOURCE_LINE (fn)); + + /* Build a block containing code to initialize the arguments, the + actual inline expansion of the body, and a label for the return + statements within the function to jump to. The type of the + statement expression is the return type of the function call. */ + stmt = NULL; + expr = build (BLOCK, TREE_TYPE (TREE_TYPE (fn)), stmt); + + /* Local declarations will be replaced by their equivalents in this + map. */ + st = id->decl_map; + id->decl_map = splay_tree_new (splay_tree_compare_pointers, + NULL, NULL); + + /* Initialize the parameters. */ + arg_inits = initialize_inlined_parameters (id, TREE_OPERAND (t, 1), fn, expr); + + if (arg_inits) + { + /* Expand any inlined calls in the initializers. Do this before we + push FN on the stack of functions we are inlining; we want to + inline calls to FN that appear in the initializers for the + parameters. */ + expand_calls_inline (&arg_inits, id); + + /* And add them to the tree. */ + BLOCK_EXPR_BODY (expr) = add_stmt_to_compound (BLOCK_EXPR_BODY (expr), + TREE_TYPE (arg_inits), + arg_inits); + } + + /* Record the function we are about to inline so that we can avoid + recursing into it. */ + VARRAY_PUSH_TREE (id->fns, fn); + + /* Record the function we are about to inline if optimize_function + has not been called on it yet and we don't have it in the list. */ + if (! DECL_INLINED_FNS (fn)) + { + int i; + + for (i = VARRAY_ACTIVE_SIZE (id->inlined_fns) - 1; i >= 0; i--) + if (VARRAY_TREE (id->inlined_fns, i) == fn) + break; + if (i < 0) + VARRAY_PUSH_TREE (id->inlined_fns, fn); + } + + /* Return statements in the function body will be replaced by jumps + to the RET_LABEL. */ + id->ret_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); + DECL_CONTEXT (id->ret_label) = VARRAY_TREE (id->fns, 0); + + if (! DECL_INITIAL (fn) + || TREE_CODE (DECL_INITIAL (fn)) != BLOCK) + abort (); + + { + /* Declare the return variable for the function. */ + tree decl = declare_return_variable (id, &retvar); + if (retvar) + { + tree *next = &BLOCK_VARS (expr); + while (*next) + next = &TREE_CHAIN (*next); + *next = decl; + } + } + + /* After we've initialized the parameters, we insert the body of the + function itself. */ + { + tree new_body = copy_body (id); + TREE_TYPE (new_body) = TREE_TYPE (TREE_TYPE (fn)); + BLOCK_EXPR_BODY (expr) + = add_stmt_to_compound (BLOCK_EXPR_BODY (expr), + TREE_TYPE (new_body), new_body); + inlined_body = &BLOCK_EXPR_BODY (expr); + } + + /* After the body of the function comes the RET_LABEL. This must come + before we evaluate the returned value below, because that evalulation + may cause RTL to be generated. */ + { + tree label = build1 (LABEL_EXPR, void_type_node, id->ret_label); + BLOCK_EXPR_BODY (expr) + = add_stmt_to_compound (BLOCK_EXPR_BODY (expr), void_type_node, label); + TREE_SIDE_EFFECTS (label) = TREE_SIDE_EFFECTS (t); + } + + /* Finally, mention the returned value so that the value of the + statement-expression is the returned value of the function. */ + if (retvar) + BLOCK_EXPR_BODY (expr) + = add_stmt_to_compound (BLOCK_EXPR_BODY (expr), + TREE_TYPE (retvar), retvar); + + /* Clean up. */ + splay_tree_delete (id->decl_map); + id->decl_map = st; + + /* The new expression has side-effects if the old one did. */ + TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (t); + + /* Replace the call by the inlined body. Wrap it in an + EXPR_WITH_FILE_LOCATION so that we'll get debugging line notes + pointing to the right place. */ + *tp = build_expr_wfl (expr, DECL_SOURCE_FILE (fn), + DECL_SOURCE_LINE_FIRST(fn), + /*col=*/0); + EXPR_WFL_EMIT_LINE_NOTE (*tp) = 1; + pop_srcloc (); + + /* If the value of the new expression is ignored, that's OK. We + don't warn about this for CALL_EXPRs, so we shouldn't warn about + the equivalent inlined version either. */ + TREE_USED (*tp) = 1; + + /* Our function now has more statements than it did before. */ + DECL_NUM_STMTS (VARRAY_TREE (id->fns, 0)) += DECL_NUM_STMTS (fn); + /* For accounting, subtract one for the saved call/ret. */ + id->inlined_stmts += DECL_NUM_STMTS (fn) - 1; + + /* Recurse into the body of the just inlined function. */ + expand_calls_inline (inlined_body, id); + VARRAY_POP (id->fns); + + /* If we've returned to the top level, clear out the record of how + much inlining has been done. */ + if (VARRAY_ACTIVE_SIZE (id->fns) == id->first_inlined_fn) + id->inlined_stmts = 0; + + /* Don't walk into subtrees. We've already handled them above. */ + *walk_subtrees = 0; + + (*lang_hooks.tree_inlining.end_inlining) (fn); + + /* Keep iterating. */ + return NULL_TREE; + } + + /* Walk over the entire tree *TP, replacing CALL_EXPRs with inline + expansions as appropriate. */ + + static void + expand_calls_inline (tp, id) + tree *tp; + inline_data *id; + { + /* Search through *TP, replacing all calls to inline functions by + appropriate equivalents. Use walk_tree in no-duplicates mode + to avoid exponential time complexity. (We can't just use + walk_tree_without_duplicates, because of the special TARGET_EXPR + handling in expand_calls. The hash table is set up in + optimize_function. */ + walk_tree (tp, expand_call_inline, id, id->tree_pruner); + } + + /* Expand calls to inline functions in the body of FN. */ + + void + optimize_inline_calls (fn) + tree fn; + { + inline_data id; + tree prev_fn; + + /* Clear out ID. */ + memset (&id, 0, sizeof (id)); + + /* Don't allow recursion into FN. */ + VARRAY_TREE_INIT (id.fns, 32, "fns"); + VARRAY_PUSH_TREE (id.fns, fn); + /* Or any functions that aren't finished yet. */ + prev_fn = NULL_TREE; + if (current_function_decl) + { + VARRAY_PUSH_TREE (id.fns, current_function_decl); + prev_fn = current_function_decl; + } + + prev_fn = ((*lang_hooks.tree_inlining.add_pending_fn_decls) + (&id.fns, prev_fn)); + + /* Create the list of functions this call will inline. */ + VARRAY_TREE_INIT (id.inlined_fns, 32, "inlined_fns"); + + /* Keep track of the low-water mark, i.e., the point where the first + real inlining is represented in ID.FNS. */ + id.first_inlined_fn = VARRAY_ACTIVE_SIZE (id.fns); + + /* Replace all calls to inline functions with the bodies of those + functions. */ + id.tree_pruner = htab_create (37, htab_hash_pointer, + htab_eq_pointer, NULL); + expand_calls_inline (&DECL_SAVED_TREE (fn), &id); + + /* Clean up. */ + htab_delete (id.tree_pruner); + if (DECL_LANG_SPECIFIC (fn)) + { + tree ifn = make_tree_vec (VARRAY_ACTIVE_SIZE (id.inlined_fns)); + + memcpy (&TREE_VEC_ELT (ifn, 0), &VARRAY_TREE (id.inlined_fns, 0), + VARRAY_ACTIVE_SIZE (id.inlined_fns) * sizeof (tree)); + DECL_INLINED_FNS (fn) = ifn; + } + } + + /* FN is a function that has a complete body, and CLONE is a function + whose body is to be set to a copy of FN, mapping argument + declarations according to the ARG_MAP splay_tree. */ + + void + clone_body (clone, fn, arg_map) + tree clone, fn; + void *arg_map; + { + inline_data id; + + /* Clone the body, as if we were making an inline call. But, remap + the parameters in the callee to the parameters of caller. If + there's an in-charge parameter, map it to an appropriate + constant. */ + memset (&id, 0, sizeof (id)); + VARRAY_TREE_INIT (id.fns, 2, "fns"); + VARRAY_PUSH_TREE (id.fns, clone); + VARRAY_PUSH_TREE (id.fns, fn); + id.decl_map = (splay_tree)arg_map; + + /* Cloning is treated slightly differently from inlining. Set + CLONING_P so that it's clear which operation we're performing. */ + id.cloning_p = true; + + /* Actually copy the body. */ + TREE_CHAIN (DECL_SAVED_TREE (clone)) = copy_body (&id); + } + + /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. + FUNC is called with the DATA and the address of each sub-tree. If + FUNC returns a non-NULL value, the traversal is aborted, and the + value returned by FUNC is returned. If HTAB is non-NULL it is used + to record the nodes visited, and to avoid visiting a node more than + once. */ + + tree + walk_tree (tp, func, data, htab_) + tree *tp; + walk_tree_fn func; + void *data; + void *htab_; + { + htab_t htab = (htab_t) htab_; + enum tree_code code; + int walk_subtrees; + tree result; + + #define WALK_SUBTREE(NODE) \ + do \ + { \ + result = walk_tree (&(NODE), func, data, htab); \ + if (result) \ + return result; \ + } \ + while (0) + + #define WALK_SUBTREE_TAIL(NODE) \ + do \ + { \ + tp = & (NODE); \ + goto tail_recurse; \ + } \ + while (0) + + tail_recurse: + /* Skip empty subtrees. */ + if (!*tp) + return NULL_TREE; + + if (htab) + { + void **slot; + + /* Don't walk the same tree twice, if the user has requested + that we avoid doing so. */ + if (htab_find (htab, *tp)) + return NULL_TREE; + /* If we haven't already seen this node, add it to the table. */ + slot = htab_find_slot (htab, *tp, INSERT); + *slot = *tp; + } + + /* Call the function. */ + walk_subtrees = 1; + result = (*func) (tp, &walk_subtrees, data); + + /* If we found something, return it. */ + if (result) + return result; + + code = TREE_CODE (*tp); + + /* Handle common cases up front. */ + if (code != EXIT_BLOCK_EXPR + && code != SAVE_EXPR + && (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)) + || TREE_CODE_CLASS (code) == 'r' + || TREE_CODE_CLASS (code) == 's')) + { + int i, len; + + /* Walk over all the sub-trees of this operand. */ + len = first_rtl_op (code); + /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same. + But, we only want to walk once. */ + if (code == TARGET_EXPR + && TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) + --len; + /* Go through the subtrees. We need to do this in forward order so + that the scope of a FOR_EXPR is handled properly. */ + for (i = 0; i < len; ++i) + WALK_SUBTREE (TREE_OPERAND (*tp, i)); + + /* We didn't find what we were looking for. */ + return NULL_TREE; + } + else if (TREE_CODE_CLASS (code) == 'd') + { + WALK_SUBTREE_TAIL (TREE_TYPE (*tp)); + } + + result = (*lang_hooks.tree_inlining.walk_subtrees) (tp, &walk_subtrees, func, + data, htab); + if (result || ! walk_subtrees) + return result; + + /* Not one of the easy cases. We must explicitly go through the + children. */ + switch (code) + { + case ERROR_MARK: + case IDENTIFIER_NODE: + case INTEGER_CST: + case REAL_CST: + case VECTOR_CST: + case STRING_CST: + case REAL_TYPE: + case COMPLEX_TYPE: + case VECTOR_TYPE: + case VOID_TYPE: + case BOOLEAN_TYPE: + case UNION_TYPE: + case ENUMERAL_TYPE: + case BLOCK: + case RECORD_TYPE: + /* None of thse have subtrees other than those already walked + above. */ + break; + + case POINTER_TYPE: + case REFERENCE_TYPE: + WALK_SUBTREE_TAIL (TREE_TYPE (*tp)); + break; + + case TREE_LIST: + WALK_SUBTREE (TREE_VALUE (*tp)); + WALK_SUBTREE_TAIL (TREE_CHAIN (*tp)); + break; + + case TREE_VEC: + { + int len = TREE_VEC_LENGTH (*tp); + + if (len == 0) + break; + + /* Walk all elements but the first. */ + while (--len) + WALK_SUBTREE (TREE_VEC_ELT (*tp, len)); + + /* Now walk the first one as a tail call. */ + WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0)); + } + + case COMPLEX_CST: + WALK_SUBTREE (TREE_REALPART (*tp)); + WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp)); + + case CONSTRUCTOR: + WALK_SUBTREE_TAIL (CONSTRUCTOR_ELTS (*tp)); + + case METHOD_TYPE: + WALK_SUBTREE (TYPE_METHOD_BASETYPE (*tp)); + /* Fall through. */ + + case FUNCTION_TYPE: + WALK_SUBTREE (TREE_TYPE (*tp)); + { + tree arg = TYPE_ARG_TYPES (*tp); + + /* We never want to walk into default arguments. */ + for (; arg; arg = TREE_CHAIN (arg)) + WALK_SUBTREE (TREE_VALUE (arg)); + } + break; + + case ARRAY_TYPE: + WALK_SUBTREE (TREE_TYPE (*tp)); + WALK_SUBTREE_TAIL (TYPE_DOMAIN (*tp)); + + case INTEGER_TYPE: + WALK_SUBTREE (TYPE_MIN_VALUE (*tp)); + WALK_SUBTREE_TAIL (TYPE_MAX_VALUE (*tp)); + + case OFFSET_TYPE: + WALK_SUBTREE (TREE_TYPE (*tp)); + WALK_SUBTREE_TAIL (TYPE_OFFSET_BASETYPE (*tp)); + + case EXIT_BLOCK_EXPR: + WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 1)); + + case SAVE_EXPR: + WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0)); + + default: + abort (); + } + + /* We didn't find what we were looking for. */ + return NULL_TREE; + + #undef WALK_SUBTREE + } + + /* Like walk_tree, but does not walk duplicate nodes more than + once. */ + + tree + walk_tree_without_duplicates (tp, func, data) + tree *tp; + walk_tree_fn func; + void *data; + { + tree result; + htab_t htab; + + htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL); + result = walk_tree (tp, func, data, htab); + htab_delete (htab); + return result; + } + + /* Passed to walk_tree. Copies the node pointed to, if appropriate. */ + + tree + copy_tree_r (tp, walk_subtrees, data) + tree *tp; + int *walk_subtrees; + void *data ATTRIBUTE_UNUSED; + { + enum tree_code code = TREE_CODE (*tp); + + /* We make copies of most nodes. */ + if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)) + || TREE_CODE_CLASS (code) == 'r' + || TREE_CODE_CLASS (code) == 'c' + || TREE_CODE_CLASS (code) == 's' + || code == TREE_LIST + || code == TREE_VEC + || (*lang_hooks.tree_inlining.tree_chain_matters_p) (*tp)) + { + /* Because the chain gets clobbered when we make a copy, we save it + here. */ + tree chain = TREE_CHAIN (*tp); + + /* Copy the node. */ + *tp = copy_node (*tp); + + /* Now, restore the chain, if appropriate. That will cause + walk_tree to walk into the chain as well. */ + if (code == PARM_DECL || code == TREE_LIST + || (*lang_hooks.tree_inlining.tree_chain_matters_p) (*tp)) + TREE_CHAIN (*tp) = chain; + } + else if (TREE_CODE_CLASS (code) == 't') + /* There's no need to copy types, or anything beneath them. */ + *walk_subtrees = 0; + + return NULL_TREE; + } + + /* The SAVE_EXPR pointed to by TP is being copied. If ST contains + information indicating to what new SAVE_EXPR this one should be + mapped, use that one. Otherwise, create a new node and enter it in + ST. FN is the function into which the copy will be placed. */ + + void + remap_save_expr (tp, st_, fn, walk_subtrees) + tree *tp; + void *st_; + tree fn; + int *walk_subtrees; + { + splay_tree st = (splay_tree) st_; + splay_tree_node n; + + /* See if we already encountered this SAVE_EXPR. */ + n = splay_tree_lookup (st, (splay_tree_key) *tp); + + if (n && (tree)n->value == error_mark_node) + abort (); + + /* If we didn't already remap this SAVE_EXPR, do so now. */ + if (!n) + { + tree t = copy_node (*tp); + + /* The SAVE_EXPR is now part of the function into which we + are inlining this body. */ + SAVE_EXPR_CONTEXT (t) = fn; + /* And we haven't evaluated it yet. */ + SAVE_EXPR_RTL (t) = NULL_RTX; + /* Remember this SAVE_EXPR. */ + n = splay_tree_insert (st, + (splay_tree_key) *tp, + (splay_tree_value) t); + /* Make sure we don't remap an already-remapped SAVE_EXPR. */ + splay_tree_insert (st, (splay_tree_key) t, + (splay_tree_value) error_mark_node); + } + else + /* We've already walked into this SAVE_EXPR, so we needn't do it + again. */ + *walk_subtrees = 0; + + /* Replace this SAVE_EXPR with the copy. */ + *tp = (tree) n->value; + } + + /* Add STMT to EXISTING if possible, otherwise create a new + COMPOUND_EXPR and add STMT to it. */ + + static tree + add_stmt_to_compound (existing, type, stmt) + tree existing, type, stmt; + { + if (!stmt) + return existing; + else if (existing) + return build (COMPOUND_EXPR, type, existing, stmt); + else + return stmt; + } + Index: java/java-tree.h =================================================================== RCS file: /cvs/gcc/gcc/gcc/java/java-tree.h,v retrieving revision 1.153 diff -p -2 -c -r1.153 java-tree.h *** java/java-tree.h 11 Jun 2002 17:31:10 -0000 1.153 --- java/java-tree.h 1 Jul 2002 18:12:24 -0000 *************** union lang_tree_node *** 903,906 **** --- 903,912 ---- #define DECL_FIELD_FINAL_WFL(NODE) \ (DECL_LANG_SPECIFIC(NODE)->u.v.wfl) + /* In a FUNCTION_DECL for which DECL_BUILT_IN does not hold, this is + the approximate number of statements in this function. There is + no need for this number to be exact; it is only used in various + heuristics regarding optimization. */ + #define DECL_NUM_STMTS(NODE) \ + (FUNCTION_DECL_CHECK (NODE)->decl.u1.i) /* True if NODE is a local variable final. */ #define LOCAL_FINAL_P(NODE) (DECL_LANG_SPECIFIC (NODE) && DECL_FINAL (NODE)) *************** extern void append_gpp_mangled_name PARA *** 1274,1277 **** --- 1280,1286 ---- extern void add_predefined_file PARAMS ((tree)); extern int predefined_filename_p PARAMS ((tree)); + + extern void java_optimize_inline PARAMS ((tree)); + extern tree decl_constant_value PARAMS ((tree)); #if defined(RTX_CODE) && defined (HAVE_MACHINE_MODES) Index: java/lang.c =================================================================== RCS file: /cvs/gcc/gcc/gcc/java/lang.c,v retrieving revision 1.103 diff -p -2 -c -r1.103 lang.c *** java/lang.c 10 Jun 2002 05:11:42 -0000 1.103 --- java/lang.c 1 Jul 2002 18:12:25 -0000 *************** The Free Software Foundation is independ *** 41,44 **** --- 41,45 ---- #include "ggc.h" #include "diagnostic.h" + #include "tree-inline.h" struct string_option *************** static int process_option_with_no PARAMS *** 62,65 **** --- 63,71 ---- const struct string_option *, int)); + static tree java_tree_inlining_walk_subtrees PARAMS ((tree *, + int *, + walk_tree_fn, + void *, + void *)); #ifndef TARGET_OBJECT_SUFFIX *************** struct language_function GTY(()) *** 263,266 **** --- 269,275 ---- #define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE java_signed_or_unsigned_type + #undef LANG_HOOKS_TREE_INLINING_WALK_SUBTREES + #define LANG_HOOKS_TREE_INLINING_WALK_SUBTREES java_tree_inlining_walk_subtrees + /* Each front end provides its own. */ const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER; *************** java_init (filename) *** 494,497 **** --- 503,509 ---- #endif + if (flag_inline_functions) + flag_inline_trees = 1; + /* Open input file. */ *************** static void *** 784,792 **** java_post_options () { ! /* Turn off RTL inliner unless -finline-functions was really specified. */ ! if (flag_really_inline == 0) { ! flag_no_inline = 1; ! flag_inline_functions = 0; } } --- 796,881 ---- java_post_options () { ! /* Use tree inlining if possible. Function instrumentation is only ! done in the RTL level, so we disable tree inlining. */ ! if (! flag_instrument_function_entry_exit) ! { ! if (!flag_no_inline) ! flag_no_inline = 1; ! if (flag_inline_functions) ! { ! flag_inline_trees = 2; ! flag_inline_functions = 0; ! } ! } ! ! } ! ! /* Return either DECL or its known constant value (if it has one). */ ! ! tree ! decl_constant_value (decl) ! tree decl; ! { ! if (/* Don't change a variable array bound or initial value to a constant ! in a place where a variable is invalid. */ ! current_function_decl != 0 ! && ! TREE_THIS_VOLATILE (decl) ! && TREE_READONLY (decl) ! && DECL_INITIAL (decl) != 0 ! && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK ! /* This is invalid if initial value is not constant. ! If it has either a function call, a memory reference, ! or a variable, then re-evaluating it could give different results. */ ! && TREE_CONSTANT (DECL_INITIAL (decl)) ! /* Check for cases where this is sub-optimal, even though valid. */ ! && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR) ! return DECL_INITIAL (decl); ! return decl; ! } ! ! /* Walk the language specific tree nodes during inlining. */ ! ! static tree ! java_tree_inlining_walk_subtrees (tp,subtrees,func,data,htab) ! tree *tp ATTRIBUTE_UNUSED; ! int *subtrees ATTRIBUTE_UNUSED; ! walk_tree_fn func ATTRIBUTE_UNUSED; ! void *data ATTRIBUTE_UNUSED; ! void *htab ATTRIBUTE_UNUSED; ! { ! enum tree_code code; ! tree result; ! ! #define WALK_SUBTREE(NODE) \ ! do \ ! { \ ! result = walk_tree (&(NODE), func, data, htab); \ ! if (result) \ ! return result; \ ! } \ ! while (0) ! ! tree t = *tp; ! if (!t) ! return NULL_TREE; ! ! code = TREE_CODE (t); ! switch (code) { ! case BLOCK: ! if (BLOCK_EXPR_BODY (t)) ! { ! tree *prev = &BLOCK_EXPR_BODY (*tp); ! while (*prev) ! { ! WALK_SUBTREE (*prev); ! prev = &TREE_CHAIN (*prev); ! } ! } ! return NULL_TREE; ! break; ! ! default: ! return NULL_TREE; } } Index: java/parse.y =================================================================== RCS file: /cvs/gcc/gcc/gcc/java/parse.y,v retrieving revision 1.389 diff -p -2 -c -r1.389 parse.y *** java/parse.y 27 Jun 2002 01:53:42 -0000 1.389 --- java/parse.y 1 Jul 2002 18:12:33 -0000 *************** definitions and other extensions. */ *** 68,71 **** --- 68,72 ---- #include "ggc.h" #include "debug.h" + #include "tree-inline.h" #ifndef DIR_SEPARATOR *************** source_end_java_method () *** 7479,7482 **** --- 7480,7485 ---- dump_java_tree (TDI_original, fndecl); + java_optimize_inline (fndecl); + /* Generate function's code */ if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) *************** add_stmt_to_compound (existing, type, st *** 7540,7543 **** --- 7543,7550 ---- tree existing, type, stmt; { + /* Keep track of this for inlining. */ + if (current_function_decl) + ++DECL_NUM_STMTS (current_function_decl); + if (existing) return build (COMPOUND_EXPR, type, existing, stmt); *************** java_expand_method_bodies (class) *** 8129,8132 **** --- 8136,8144 ---- current_function_decl = decl; + /* Save the function for inlining. */ + if (flag_inline_trees) + DECL_SAVED_TREE (decl) = + BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)); + /* It's time to assign the variable flagging static class initialization based on which classes invoked static methods --i0GnLXmr/X--