First version committed to git
[zpugcc/jano.git] / toolchain / gcc / gcc / cp / optimize.c
blob5ada1312e59d1a55e0952b9cfdf60ae13ac40b77
1 /* Perform optimizations on tree structure.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004
3 Free Software Foundation, Inc.
4 Written by Mark Michell (mark@codesourcery.com).
6 This file is part of GCC.
8 GCC 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)
11 any later version.
13 GCC 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 GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "rtl.h"
30 #include "insn-config.h"
31 #include "input.h"
32 #include "integrate.h"
33 #include "toplev.h"
34 #include "varray.h"
35 #include "params.h"
36 #include "hashtab.h"
37 #include "debug.h"
38 #include "tree-inline.h"
40 /* Prototypes. */
42 static tree calls_setjmp_r (tree *, int *, void *);
43 static void update_cloned_parm (tree, tree);
44 static void dump_function (enum tree_dump_index, tree);
46 /* Optimize the body of FN. */
48 void
49 optimize_function (tree fn)
51 dump_function (TDI_original, fn);
53 if (flag_inline_trees
54 /* We do not inline thunks, as (a) the backend tries to optimize
55 the call to the thunkee, (b) tree based inlining breaks that
56 optimization, (c) virtual functions are rarely inlineable,
57 and (d) TARGET_ASM_OUTPUT_MI_THUNK is there to DTRT anyway. */
58 && !DECL_THUNK_P (fn))
60 optimize_inline_calls (fn);
61 dump_function (TDI_inlined, fn);
64 dump_function (TDI_optimized, fn);
67 /* Called from calls_setjmp_p via walk_tree. */
69 static tree
70 calls_setjmp_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
71 void *data ATTRIBUTE_UNUSED)
73 /* We're only interested in FUNCTION_DECLS. */
74 if (TREE_CODE (*tp) != FUNCTION_DECL)
75 return NULL_TREE;
77 return setjmp_call_p (*tp) ? *tp : NULL_TREE;
80 /* Returns nonzero if FN calls `setjmp' or some other function that
81 can return more than once. This function is conservative; it may
82 occasionally return a nonzero value even when FN does not actually
83 call `setjmp'. */
85 bool
86 calls_setjmp_p (tree fn)
88 return walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
89 calls_setjmp_r,
90 NULL) != NULL_TREE;
93 /* CLONED_PARM is a copy of CLONE, generated for a cloned constructor
94 or destructor. Update it to ensure that the source-position for
95 the cloned parameter matches that for the original, and that the
96 debugging generation code will be able to find the original PARM. */
98 static void
99 update_cloned_parm (tree parm, tree cloned_parm)
101 DECL_ABSTRACT_ORIGIN (cloned_parm) = parm;
103 /* We may have taken its address. */
104 TREE_ADDRESSABLE (cloned_parm) = TREE_ADDRESSABLE (parm);
106 /* The definition might have different constness. */
107 TREE_READONLY (cloned_parm) = TREE_READONLY (parm);
109 TREE_USED (cloned_parm) = TREE_USED (parm);
111 /* The name may have changed from the declaration. */
112 DECL_NAME (cloned_parm) = DECL_NAME (parm);
113 DECL_SOURCE_LOCATION (cloned_parm) = DECL_SOURCE_LOCATION (parm);
116 /* FN is a function that has a complete body. Clone the body as
117 necessary. Returns nonzero if there's no longer any need to
118 process the main body. */
120 bool
121 maybe_clone_body (tree fn)
123 tree clone;
125 /* We only clone constructors and destructors. */
126 if (!DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
127 && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
128 return 0;
130 /* Emit the DWARF1 abstract instance. */
131 (*debug_hooks->deferred_inline_function) (fn);
133 /* We know that any clones immediately follow FN in the TYPE_METHODS
134 list. */
135 for (clone = TREE_CHAIN (fn);
136 clone && DECL_CLONED_FUNCTION_P (clone);
137 clone = TREE_CHAIN (clone))
139 tree parm;
140 tree clone_parm;
141 int parmno;
142 splay_tree decl_map;
144 /* Update CLONE's source position information to match FN's. */
145 DECL_SOURCE_LOCATION (clone) = DECL_SOURCE_LOCATION (fn);
146 DECL_INLINE (clone) = DECL_INLINE (fn);
147 DECL_DECLARED_INLINE_P (clone) = DECL_DECLARED_INLINE_P (fn);
148 DECL_COMDAT (clone) = DECL_COMDAT (fn);
149 DECL_WEAK (clone) = DECL_WEAK (fn);
150 DECL_ONE_ONLY (clone) = DECL_ONE_ONLY (fn);
151 DECL_SECTION_NAME (clone) = DECL_SECTION_NAME (fn);
152 DECL_USE_TEMPLATE (clone) = DECL_USE_TEMPLATE (fn);
153 DECL_EXTERNAL (clone) = DECL_EXTERNAL (fn);
154 DECL_INTERFACE_KNOWN (clone) = DECL_INTERFACE_KNOWN (fn);
155 DECL_NOT_REALLY_EXTERN (clone) = DECL_NOT_REALLY_EXTERN (fn);
156 TREE_PUBLIC (clone) = TREE_PUBLIC (fn);
157 DECL_VISIBILITY (clone) = DECL_VISIBILITY (fn);
159 /* Adjust the parameter names and locations. */
160 parm = DECL_ARGUMENTS (fn);
161 clone_parm = DECL_ARGUMENTS (clone);
162 /* Update the `this' parameter, which is always first. */
163 update_cloned_parm (parm, clone_parm);
164 parm = TREE_CHAIN (parm);
165 clone_parm = TREE_CHAIN (clone_parm);
166 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
167 parm = TREE_CHAIN (parm);
168 if (DECL_HAS_VTT_PARM_P (fn))
169 parm = TREE_CHAIN (parm);
170 if (DECL_HAS_VTT_PARM_P (clone))
171 clone_parm = TREE_CHAIN (clone_parm);
172 for (; parm;
173 parm = TREE_CHAIN (parm), clone_parm = TREE_CHAIN (clone_parm))
174 /* Update this parameter. */
175 update_cloned_parm (parm, clone_parm);
177 /* Start processing the function. */
178 push_to_top_level ();
179 start_function (NULL_TREE, clone, NULL_TREE, SF_PRE_PARSED);
181 /* Remap the parameters. */
182 decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
183 for (parmno = 0,
184 parm = DECL_ARGUMENTS (fn),
185 clone_parm = DECL_ARGUMENTS (clone);
186 parm;
187 ++parmno,
188 parm = TREE_CHAIN (parm))
190 /* Map the in-charge parameter to an appropriate constant. */
191 if (DECL_HAS_IN_CHARGE_PARM_P (fn) && parmno == 1)
193 tree in_charge;
194 in_charge = in_charge_arg_for_name (DECL_NAME (clone));
195 splay_tree_insert (decl_map,
196 (splay_tree_key) parm,
197 (splay_tree_value) in_charge);
199 else if (DECL_ARTIFICIAL (parm)
200 && DECL_NAME (parm) == vtt_parm_identifier)
202 /* For a subobject constructor or destructor, the next
203 argument is the VTT parameter. Remap the VTT_PARM
204 from the CLONE to this parameter. */
205 if (DECL_HAS_VTT_PARM_P (clone))
207 DECL_ABSTRACT_ORIGIN (clone_parm) = parm;
208 splay_tree_insert (decl_map,
209 (splay_tree_key) parm,
210 (splay_tree_value) clone_parm);
211 clone_parm = TREE_CHAIN (clone_parm);
213 /* Otherwise, map the VTT parameter to `NULL'. */
214 else
216 splay_tree_insert (decl_map,
217 (splay_tree_key) parm,
218 (splay_tree_value) null_pointer_node);
221 /* Map other parameters to their equivalents in the cloned
222 function. */
223 else
225 splay_tree_insert (decl_map,
226 (splay_tree_key) parm,
227 (splay_tree_value) clone_parm);
228 clone_parm = TREE_CHAIN (clone_parm);
232 /* Clone the body. */
233 clone_body (clone, fn, decl_map);
235 /* Clean up. */
236 splay_tree_delete (decl_map);
238 /* The clone can throw iff the original function can throw. */
239 cp_function_chain->can_throw = !TREE_NOTHROW (fn);
241 /* Now, expand this function into RTL, if appropriate. */
242 finish_function (0);
243 BLOCK_ABSTRACT_ORIGIN (DECL_INITIAL (clone)) = DECL_INITIAL (fn);
244 expand_or_defer_fn (clone);
245 pop_from_top_level ();
248 /* We don't need to process the original function any further. */
249 return 1;
252 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
254 static void
255 dump_function (enum tree_dump_index phase, tree fn)
257 FILE *stream;
258 int flags;
260 stream = dump_begin (phase, &flags);
261 if (stream)
263 fprintf (stream, "\n;; Function %s",
264 decl_as_string (fn, TFF_DECL_SPECIFIERS));
265 fprintf (stream, " (%s)\n",
266 decl_as_string (DECL_ASSEMBLER_NAME (fn), 0));
267 fprintf (stream, ";; enabled by -fdump-%s\n", dump_flag_name (phase));
268 fprintf (stream, "\n");
270 dump_node (fn, TDF_SLIM | flags, stream);
271 dump_end (phase, stream);