OSDN Git Service

* class.c (layout_class_type): For non-POD class types, also copy
[pf3gnuchains/gcc-fork.git] / gcc / cp / optimize.c
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).
4
5 This file is part of GCC.
6
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)
10 any later version.
11
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.
16
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
20 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "cp-tree.h"
28 #include "rtl.h"
29 #include "insn-config.h"
30 #include "input.h"
31 #include "integrate.h"
32 #include "toplev.h"
33 #include "varray.h"
34 #include "params.h"
35 #include "hashtab.h"
36 #include "debug.h"
37 #include "tree-inline.h"
38
39 /* Prototypes.  */
40
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);
44
45 /* Optimize the body of FN.  */
46
47 void
48 optimize_function (tree fn)
49 {
50   dump_function (TDI_original, fn);
51
52   if (flag_inline_trees
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))
58     {
59       optimize_inline_calls (fn);
60       dump_function (TDI_inlined, fn);
61     }
62   
63   dump_function (TDI_optimized, fn);
64 }
65
66 /* Called from calls_setjmp_p via walk_tree.  */
67
68 static tree
69 calls_setjmp_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
70                 void *data ATTRIBUTE_UNUSED)
71 {
72   /* We're only interested in FUNCTION_DECLS.  */
73   if (TREE_CODE (*tp) != FUNCTION_DECL)
74     return NULL_TREE;
75
76   return setjmp_call_p (*tp) ? *tp : NULL_TREE;
77 }
78
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
82    call `setjmp'.  */
83
84 bool
85 calls_setjmp_p (tree fn)
86 {
87   return walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
88                                        calls_setjmp_r,
89                                        NULL) != NULL_TREE;
90 }
91
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.  */
96
97 static void
98 update_cloned_parm (tree parm, tree cloned_parm)
99 {
100   DECL_ABSTRACT_ORIGIN (cloned_parm) = parm;
101
102   /* We may have taken its address.  */
103   TREE_ADDRESSABLE (cloned_parm) = TREE_ADDRESSABLE (parm);
104
105   /* The definition might have different constness.  */
106   TREE_READONLY (cloned_parm) = TREE_READONLY (parm);
107   
108   TREE_USED (cloned_parm) = TREE_USED (parm);
109   
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);
113 }
114
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.  */
118
119 bool
120 maybe_clone_body (tree fn)
121 {
122   tree clone;
123
124   /* We only clone constructors and destructors.  */
125   if (!DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
126       && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
127     return 0;
128
129   /* Emit the DWARF1 abstract instance.  */
130   (*debug_hooks->deferred_inline_function) (fn);
131
132   /* Our caller does not expect collection to happen, which it might if
133      we decide to compile the function to rtl now.  Arrange for a new
134      gc context to be created if so.  */
135   function_depth++;
136
137   /* We know that any clones immediately follow FN in the TYPE_METHODS
138      list.  */
139   for (clone = TREE_CHAIN (fn);
140        clone && DECL_CLONED_FUNCTION_P (clone);
141        clone = TREE_CHAIN (clone))
142     {
143       tree parm;
144       tree clone_parm;
145       int parmno;
146       splay_tree decl_map;
147
148       /* Update CLONE's source position information to match FN's.  */
149       DECL_SOURCE_LOCATION (clone) = DECL_SOURCE_LOCATION (fn);
150       DECL_INLINE (clone) = DECL_INLINE (fn);
151       DECL_DECLARED_INLINE_P (clone) = DECL_DECLARED_INLINE_P (fn);
152       DECL_COMDAT (clone) = DECL_COMDAT (fn);
153       DECL_WEAK (clone) = DECL_WEAK (fn);
154       DECL_ONE_ONLY (clone) = DECL_ONE_ONLY (fn);
155       DECL_SECTION_NAME (clone) = DECL_SECTION_NAME (fn);
156       DECL_USE_TEMPLATE (clone) = DECL_USE_TEMPLATE (fn);
157       DECL_EXTERNAL (clone) = DECL_EXTERNAL (fn);
158       DECL_INTERFACE_KNOWN (clone) = DECL_INTERFACE_KNOWN (fn);
159       DECL_NOT_REALLY_EXTERN (clone) = DECL_NOT_REALLY_EXTERN (fn);
160       TREE_PUBLIC (clone) = TREE_PUBLIC (fn);
161       DECL_VISIBILITY (clone) = DECL_VISIBILITY (fn);
162
163       /* Adjust the parameter names and locations.  */
164       parm = DECL_ARGUMENTS (fn);
165       clone_parm = DECL_ARGUMENTS (clone);
166       /* Update the `this' parameter, which is always first.  */
167       update_cloned_parm (parm, clone_parm);
168       parm = TREE_CHAIN (parm);
169       clone_parm = TREE_CHAIN (clone_parm);
170       if (DECL_HAS_IN_CHARGE_PARM_P (fn))
171         parm = TREE_CHAIN (parm);
172       if (DECL_HAS_VTT_PARM_P (fn))
173         parm = TREE_CHAIN (parm);
174       if (DECL_HAS_VTT_PARM_P (clone))
175         clone_parm = TREE_CHAIN (clone_parm);
176       for (; parm;
177            parm = TREE_CHAIN (parm), clone_parm = TREE_CHAIN (clone_parm))
178         /* Update this parameter.  */
179         update_cloned_parm (parm, clone_parm);
180
181       /* Start processing the function.  */
182       push_to_top_level ();
183       start_function (NULL_TREE, clone, NULL_TREE, SF_PRE_PARSED);
184
185       /* Remap the parameters.  */
186       decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
187       for (parmno = 0,
188              parm = DECL_ARGUMENTS (fn),
189              clone_parm = DECL_ARGUMENTS (clone);
190            parm;
191            ++parmno,
192              parm = TREE_CHAIN (parm))
193         {
194           /* Map the in-charge parameter to an appropriate constant.  */
195           if (DECL_HAS_IN_CHARGE_PARM_P (fn) && parmno == 1)
196             {
197               tree in_charge;
198               in_charge = in_charge_arg_for_name (DECL_NAME (clone));
199               splay_tree_insert (decl_map,
200                                  (splay_tree_key) parm,
201                                  (splay_tree_value) in_charge);
202             }
203           else if (DECL_ARTIFICIAL (parm)
204                    && DECL_NAME (parm) == vtt_parm_identifier)
205             {
206               /* For a subobject constructor or destructor, the next
207                  argument is the VTT parameter.  Remap the VTT_PARM
208                  from the CLONE to this parameter.  */
209               if (DECL_HAS_VTT_PARM_P (clone))
210                 {
211                   DECL_ABSTRACT_ORIGIN (clone_parm) = parm;
212                   splay_tree_insert (decl_map,
213                                      (splay_tree_key) parm,
214                                      (splay_tree_value) clone_parm);
215                   clone_parm = TREE_CHAIN (clone_parm);
216                 }
217               /* Otherwise, map the VTT parameter to `NULL'.  */
218               else
219                 {
220                   splay_tree_insert (decl_map,
221                                      (splay_tree_key) parm,
222                                      (splay_tree_value) null_pointer_node);
223                 }
224             }
225           /* Map other parameters to their equivalents in the cloned
226              function.  */
227           else
228             {
229               splay_tree_insert (decl_map,
230                                  (splay_tree_key) parm,
231                                  (splay_tree_value) clone_parm);
232               clone_parm = TREE_CHAIN (clone_parm);
233             }
234         }
235
236       /* Clone the body.  */
237       clone_body (clone, fn, decl_map);
238
239       /* There are as many statements in the clone as in the
240          original.  */
241       DECL_ESTIMATED_INSNS (clone) = DECL_ESTIMATED_INSNS (fn);
242
243       /* Clean up.  */
244       splay_tree_delete (decl_map);
245
246       /* The clone can throw iff the original function can throw.  */
247       cp_function_chain->can_throw = !TREE_NOTHROW (fn);
248
249       /* Now, expand this function into RTL, if appropriate.  */
250       finish_function (0);
251       BLOCK_ABSTRACT_ORIGIN (DECL_INITIAL (clone)) = DECL_INITIAL (fn);
252       expand_or_defer_fn (clone);
253       pop_from_top_level ();
254     }
255
256   function_depth--;
257
258   /* We don't need to process the original function any further.  */
259   return 1;
260 }
261
262 /* Dump FUNCTION_DECL FN as tree dump PHASE.  */
263
264 static void
265 dump_function (enum tree_dump_index phase, tree fn)
266 {
267   FILE *stream;
268   int flags;
269
270   stream = dump_begin (phase, &flags);
271   if (stream)
272     {
273       fprintf (stream, "\n;; Function %s",
274                decl_as_string (fn, TFF_DECL_SPECIFIERS));
275       fprintf (stream, " (%s)\n",
276                decl_as_string (DECL_ASSEMBLER_NAME (fn), 0));
277       fprintf (stream, ";; enabled by -fdump-%s\n", dump_flag_name (phase));
278       fprintf (stream, "\n");
279       
280       dump_node (fn, TDF_SLIM | flags, stream);
281       dump_end (phase, stream);
282     }
283 }