1 /* Perform optimizations on tree structure.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 Written by Mark Michell (mark@codesourcery.com).
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
29 #include "insn-config.h"
31 #include "integrate.h"
37 #include "tree-inline.h"
41 static tree calls_setjmp_r (tree *, int *, void *);
42 static void update_cloned_parm (tree, tree);
43 static void dump_function (enum tree_dump_index, tree);
45 /* Optimize the body of FN. */
48 optimize_function (tree fn)
50 dump_function (TDI_original, fn);
53 /* We do not inline thunks, as (a) the backend tries to optimize
54 the call to the thunkee, (b) tree based inlining breaks that
55 optimization, (c) virtual functions are rarely inlineable,
56 and (d) TARGET_ASM_OUTPUT_MI_THUNK is there to DTRT anyway. */
57 && !DECL_THUNK_P (fn))
59 optimize_inline_calls (fn);
60 dump_function (TDI_inlined, fn);
63 dump_function (TDI_optimized, fn);
66 /* Called from calls_setjmp_p via walk_tree. */
69 calls_setjmp_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
70 void *data ATTRIBUTE_UNUSED)
72 /* We're only interested in FUNCTION_DECLS. */
73 if (TREE_CODE (*tp) != FUNCTION_DECL)
76 return setjmp_call_p (*tp) ? *tp : NULL_TREE;
79 /* Returns nonzero if FN calls `setjmp' or some other function that
80 can return more than once. This function is conservative; it may
81 occasionally return a nonzero value even when FN does not actually
85 calls_setjmp_p (tree fn)
87 return walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
92 /* CLONED_PARM is a copy of CLONE, generated for a cloned constructor
93 or destructor. Update it to ensure that the source-position for
94 the cloned parameter matches that for the original, and that the
95 debugging generation code will be able to find the original PARM. */
98 update_cloned_parm (tree parm, tree cloned_parm)
100 DECL_ABSTRACT_ORIGIN (cloned_parm) = parm;
102 /* We may have taken its address. */
103 TREE_ADDRESSABLE (cloned_parm) = TREE_ADDRESSABLE (parm);
105 /* The definition might have different constness. */
106 TREE_READONLY (cloned_parm) = TREE_READONLY (parm);
108 TREE_USED (cloned_parm) = TREE_USED (parm);
110 /* The name may have changed from the declaration. */
111 DECL_NAME (cloned_parm) = DECL_NAME (parm);
112 DECL_SOURCE_LOCATION (cloned_parm) = DECL_SOURCE_LOCATION (parm);
115 /* FN is a function that has a complete body. Clone the body as
116 necessary. Returns nonzero if there's no longer any need to
117 process the main body. */
120 maybe_clone_body (tree fn)
124 /* We only clone constructors and destructors. */
125 if (!DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
126 && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
129 /* Emit the DWARF1 abstract instance. */
130 (*debug_hooks->deferred_inline_function) (fn);
132 /* We know that any clones immediately follow FN in the TYPE_METHODS
134 for (clone = TREE_CHAIN (fn);
135 clone && DECL_CLONED_FUNCTION_P (clone);
136 clone = TREE_CHAIN (clone))
143 /* Update CLONE's source position information to match FN's. */
144 DECL_SOURCE_LOCATION (clone) = DECL_SOURCE_LOCATION (fn);
145 DECL_INLINE (clone) = DECL_INLINE (fn);
146 DECL_DECLARED_INLINE_P (clone) = DECL_DECLARED_INLINE_P (fn);
147 DECL_COMDAT (clone) = DECL_COMDAT (fn);
148 DECL_WEAK (clone) = DECL_WEAK (fn);
149 DECL_ONE_ONLY (clone) = DECL_ONE_ONLY (fn);
150 DECL_SECTION_NAME (clone) = DECL_SECTION_NAME (fn);
151 DECL_USE_TEMPLATE (clone) = DECL_USE_TEMPLATE (fn);
152 DECL_EXTERNAL (clone) = DECL_EXTERNAL (fn);
153 DECL_INTERFACE_KNOWN (clone) = DECL_INTERFACE_KNOWN (fn);
154 DECL_NOT_REALLY_EXTERN (clone) = DECL_NOT_REALLY_EXTERN (fn);
155 TREE_PUBLIC (clone) = TREE_PUBLIC (fn);
156 DECL_VISIBILITY (clone) = DECL_VISIBILITY (fn);
158 /* Adjust the parameter names and locations. */
159 parm = DECL_ARGUMENTS (fn);
160 clone_parm = DECL_ARGUMENTS (clone);
161 /* Update the `this' parameter, which is always first. */
162 update_cloned_parm (parm, clone_parm);
163 parm = TREE_CHAIN (parm);
164 clone_parm = TREE_CHAIN (clone_parm);
165 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
166 parm = TREE_CHAIN (parm);
167 if (DECL_HAS_VTT_PARM_P (fn))
168 parm = TREE_CHAIN (parm);
169 if (DECL_HAS_VTT_PARM_P (clone))
170 clone_parm = TREE_CHAIN (clone_parm);
172 parm = TREE_CHAIN (parm), clone_parm = TREE_CHAIN (clone_parm))
173 /* Update this parameter. */
174 update_cloned_parm (parm, clone_parm);
176 /* Start processing the function. */
177 push_to_top_level ();
178 start_function (NULL_TREE, clone, NULL_TREE, SF_PRE_PARSED);
180 /* Remap the parameters. */
181 decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
183 parm = DECL_ARGUMENTS (fn),
184 clone_parm = DECL_ARGUMENTS (clone);
187 parm = TREE_CHAIN (parm))
189 /* Map the in-charge parameter to an appropriate constant. */
190 if (DECL_HAS_IN_CHARGE_PARM_P (fn) && parmno == 1)
193 in_charge = in_charge_arg_for_name (DECL_NAME (clone));
194 splay_tree_insert (decl_map,
195 (splay_tree_key) parm,
196 (splay_tree_value) in_charge);
198 else if (DECL_ARTIFICIAL (parm)
199 && DECL_NAME (parm) == vtt_parm_identifier)
201 /* For a subobject constructor or destructor, the next
202 argument is the VTT parameter. Remap the VTT_PARM
203 from the CLONE to this parameter. */
204 if (DECL_HAS_VTT_PARM_P (clone))
206 DECL_ABSTRACT_ORIGIN (clone_parm) = parm;
207 splay_tree_insert (decl_map,
208 (splay_tree_key) parm,
209 (splay_tree_value) clone_parm);
210 clone_parm = TREE_CHAIN (clone_parm);
212 /* Otherwise, map the VTT parameter to `NULL'. */
215 splay_tree_insert (decl_map,
216 (splay_tree_key) parm,
217 (splay_tree_value) null_pointer_node);
220 /* Map other parameters to their equivalents in the cloned
224 splay_tree_insert (decl_map,
225 (splay_tree_key) parm,
226 (splay_tree_value) clone_parm);
227 clone_parm = TREE_CHAIN (clone_parm);
231 /* Clone the body. */
232 clone_body (clone, fn, decl_map);
234 /* There are as many statements in the clone as in the
236 DECL_ESTIMATED_INSNS (clone) = DECL_ESTIMATED_INSNS (fn);
239 splay_tree_delete (decl_map);
241 /* The clone can throw iff the original function can throw. */
242 cp_function_chain->can_throw = !TREE_NOTHROW (fn);
244 /* Now, expand this function into RTL, if appropriate. */
246 BLOCK_ABSTRACT_ORIGIN (DECL_INITIAL (clone)) = DECL_INITIAL (fn);
247 expand_or_defer_fn (clone);
248 pop_from_top_level ();
251 /* We don't need to process the original function any further. */
255 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
258 dump_function (enum tree_dump_index phase, tree fn)
263 stream = dump_begin (phase, &flags);
266 fprintf (stream, "\n;; Function %s",
267 decl_as_string (fn, TFF_DECL_SPECIFIERS));
268 fprintf (stream, " (%s)\n",
269 decl_as_string (DECL_ASSEMBLER_NAME (fn), 0));
270 fprintf (stream, ";; enabled by -fdump-%s\n", dump_flag_name (phase));
271 fprintf (stream, "\n");
273 dump_node (fn, TDF_SLIM | flags, stream);
274 dump_end (phase, stream);