OSDN Git Service

* c-decl.c (c_init_decl_processing): Clear input_file_name
[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   /* While in this function, we may choose to go off and compile
53      another function.  For example, we might instantiate a function
54      in the hopes of inlining it.  Normally, that wouldn't trigger any
55      actual RTL code-generation -- but it will if the template is
56      actually needed.  (For example, if it's address is taken, or if
57      some other function already refers to the template.)  If
58      code-generation occurs, then garbage collection will occur, so we
59      must protect ourselves, just as we do while building up the body
60      of the function.  */
61   ++function_depth;
62
63   if (flag_inline_trees
64       /* We do not inline thunks, as (a) the backend tries to optimize
65          the call to the thunkee, (b) tree based inlining breaks that
66          optimization, (c) virtual functions are rarely inlineable,
67          and (d) TARGET_ASM_OUTPUT_MI_THUNK is there to DTRT anyway.  */
68       && !DECL_THUNK_P (fn))
69     {
70       optimize_inline_calls (fn);
71
72       dump_function (TDI_inlined, fn);
73     }
74   
75   /* Undo the call to ggc_push_context above.  */
76   --function_depth;
77   
78   dump_function (TDI_optimized, fn);
79 }
80
81 /* Called from calls_setjmp_p via walk_tree.  */
82
83 static tree
84 calls_setjmp_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
85                 void *data ATTRIBUTE_UNUSED)
86 {
87   /* We're only interested in FUNCTION_DECLS.  */
88   if (TREE_CODE (*tp) != FUNCTION_DECL)
89     return NULL_TREE;
90
91   return setjmp_call_p (*tp) ? *tp : NULL_TREE;
92 }
93
94 /* Returns nonzero if FN calls `setjmp' or some other function that
95    can return more than once.  This function is conservative; it may
96    occasionally return a nonzero value even when FN does not actually
97    call `setjmp'.  */
98
99 bool
100 calls_setjmp_p (tree fn)
101 {
102   return walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
103                                        calls_setjmp_r,
104                                        NULL) != NULL_TREE;
105 }
106
107 /* CLONED_PARM is a copy of CLONE, generated for a cloned constructor
108    or destructor.  Update it to ensure that the source-position for
109    the cloned parameter matches that for the original, and that the
110    debugging generation code will be able to find the original PARM.  */
111
112 static void
113 update_cloned_parm (tree parm, tree cloned_parm)
114 {
115   DECL_ABSTRACT_ORIGIN (cloned_parm) = parm;
116
117   /* We may have taken its address.  */
118   TREE_ADDRESSABLE (cloned_parm) = TREE_ADDRESSABLE (parm);
119
120   /* The definition might have different constness.  */
121   TREE_READONLY (cloned_parm) = TREE_READONLY (parm);
122   
123   TREE_USED (cloned_parm) = TREE_USED (parm);
124   
125   /* The name may have changed from the declaration.  */
126   DECL_NAME (cloned_parm) = DECL_NAME (parm);
127   DECL_SOURCE_LOCATION (cloned_parm) = DECL_SOURCE_LOCATION (parm);
128 }
129
130 /* FN is a function that has a complete body.  Clone the body as
131    necessary.  Returns nonzero if there's no longer any need to
132    process the main body.  */
133
134 bool
135 maybe_clone_body (tree fn)
136 {
137   tree clone;
138   bool first = true;
139
140   /* We only clone constructors and destructors.  */
141   if (!DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
142       && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
143     return 0;
144
145   /* Emit the DWARF1 abstract instance.  */
146   (*debug_hooks->deferred_inline_function) (fn);
147
148   /* We know that any clones immediately follow FN in the TYPE_METHODS
149      list.  */
150   for (clone = TREE_CHAIN (fn);
151        clone && DECL_CLONED_FUNCTION_P (clone);
152        clone = TREE_CHAIN (clone), first = false)
153     {
154       tree parm;
155       tree clone_parm;
156       int parmno;
157       splay_tree decl_map;
158
159       /* Update CLONE's source position information to match FN's.  */
160       DECL_SOURCE_LOCATION (clone) = DECL_SOURCE_LOCATION (fn);
161       DECL_INLINE (clone) = DECL_INLINE (fn);
162       DID_INLINE_FUNC (clone) = DID_INLINE_FUNC (fn);
163       DECL_DECLARED_INLINE_P (clone) = DECL_DECLARED_INLINE_P (fn);
164       DECL_COMDAT (clone) = DECL_COMDAT (fn);
165       DECL_WEAK (clone) = DECL_WEAK (fn);
166       DECL_ONE_ONLY (clone) = DECL_ONE_ONLY (fn);
167       DECL_SECTION_NAME (clone) = DECL_SECTION_NAME (fn);
168       DECL_USE_TEMPLATE (clone) = DECL_USE_TEMPLATE (fn);
169       DECL_EXTERNAL (clone) = DECL_EXTERNAL (fn);
170       DECL_INTERFACE_KNOWN (clone) = DECL_INTERFACE_KNOWN (fn);
171       DECL_NOT_REALLY_EXTERN (clone) = DECL_NOT_REALLY_EXTERN (fn);
172       TREE_PUBLIC (clone) = TREE_PUBLIC (fn);
173
174       /* Adjust the parameter names and locations.  */
175       parm = DECL_ARGUMENTS (fn);
176       clone_parm = DECL_ARGUMENTS (clone);
177       /* Update the `this' parameter, which is always first.  */
178       update_cloned_parm (parm, clone_parm);
179       parm = TREE_CHAIN (parm);
180       clone_parm = TREE_CHAIN (clone_parm);
181       if (DECL_HAS_IN_CHARGE_PARM_P (fn))
182         parm = TREE_CHAIN (parm);
183       if (DECL_HAS_VTT_PARM_P (fn))
184         parm = TREE_CHAIN (parm);
185       if (DECL_HAS_VTT_PARM_P (clone))
186         clone_parm = TREE_CHAIN (clone_parm);
187       for (; parm;
188            parm = TREE_CHAIN (parm), clone_parm = TREE_CHAIN (clone_parm))
189         {
190           /* Update this parameter.  */
191           update_cloned_parm (parm, clone_parm);
192           /* We should only give unused information for one clone.  */
193           if (!first)
194             TREE_USED (clone_parm) = 1;
195         }
196
197       /* Start processing the function.  */
198       push_to_top_level ();
199       start_function (NULL_TREE, clone, NULL_TREE, SF_PRE_PARSED);
200
201       /* Remap the parameters.  */
202       decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
203       for (parmno = 0,
204              parm = DECL_ARGUMENTS (fn),
205              clone_parm = DECL_ARGUMENTS (clone);
206            parm;
207            ++parmno,
208              parm = TREE_CHAIN (parm))
209         {
210           /* Map the in-charge parameter to an appropriate constant.  */
211           if (DECL_HAS_IN_CHARGE_PARM_P (fn) && parmno == 1)
212             {
213               tree in_charge;
214               in_charge = in_charge_arg_for_name (DECL_NAME (clone));
215               splay_tree_insert (decl_map,
216                                  (splay_tree_key) parm,
217                                  (splay_tree_value) in_charge);
218             }
219           else if (DECL_ARTIFICIAL (parm)
220                    && DECL_NAME (parm) == vtt_parm_identifier)
221             {
222               /* For a subobject constructor or destructor, the next
223                  argument is the VTT parameter.  Remap the VTT_PARM
224                  from the CLONE to this parameter.  */
225               if (DECL_HAS_VTT_PARM_P (clone))
226                 {
227                   DECL_ABSTRACT_ORIGIN (clone_parm) = parm;
228                   splay_tree_insert (decl_map,
229                                      (splay_tree_key) parm,
230                                      (splay_tree_value) clone_parm);
231                   clone_parm = TREE_CHAIN (clone_parm);
232                 }
233               /* Otherwise, map the VTT parameter to `NULL'.  */
234               else
235                 {
236                   splay_tree_insert (decl_map,
237                                      (splay_tree_key) parm,
238                                      (splay_tree_value) null_pointer_node);
239                 }
240             }
241           /* Map other parameters to their equivalents in the cloned
242              function.  */
243           else
244             {
245               splay_tree_insert (decl_map,
246                                  (splay_tree_key) parm,
247                                  (splay_tree_value) clone_parm);
248               clone_parm = TREE_CHAIN (clone_parm);
249             }
250         }
251
252       /* Clone the body.  */
253       clone_body (clone, fn, decl_map);
254
255       /* There are as many statements in the clone as in the
256          original.  */
257       DECL_NUM_STMTS (clone) = DECL_NUM_STMTS (fn);
258
259       /* Clean up.  */
260       splay_tree_delete (decl_map);
261
262       /* The clone can throw iff the original function can throw.  */
263       cp_function_chain->can_throw = !TREE_NOTHROW (fn);
264
265       /* Now, expand this function into RTL, if appropriate.  */
266       finish_function (0);
267       BLOCK_ABSTRACT_ORIGIN (DECL_INITIAL (clone)) = DECL_INITIAL (fn);
268       expand_body (clone);
269       pop_from_top_level ();
270     }
271
272   /* We don't need to process the original function any further.  */
273   return 1;
274 }
275
276 /* Dump FUNCTION_DECL FN as tree dump PHASE.  */
277
278 static void
279 dump_function (enum tree_dump_index phase, tree fn)
280 {
281   FILE *stream;
282   int flags;
283
284   stream = dump_begin (phase, &flags);
285   if (stream)
286     {
287       fprintf (stream, "\n;; Function %s",
288                decl_as_string (fn, TFF_DECL_SPECIFIERS));
289       fprintf (stream, " (%s)\n",
290                decl_as_string (DECL_ASSEMBLER_NAME (fn), 0));
291       fprintf (stream, ";; enabled by -%s\n", dump_flag_name (phase));
292       fprintf (stream, "\n");
293       
294       dump_node (fn, TDF_SLIM | flags, stream);
295       dump_end (phase, stream);
296     }
297 }