OSDN Git Service

Merge tree-ssa-20020619-branch into mainline.
[pf3gnuchains/gcc-fork.git] / gcc / cp / optimize.c
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).
5
6 This file is part of GCC.
7
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.
12
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.
17
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.  */
22
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"
39 #include "flags.h"
40 #include "langhooks.h"
41 #include "diagnostic.h"
42 #include "tree-dump.h"
43 #include "tree-simple.h"
44
45 /* Prototypes.  */
46
47 static tree calls_setjmp_r (tree *, int *, void *);
48 static void update_cloned_parm (tree, tree);
49
50 /* Called from calls_setjmp_p via walk_tree.  */
51
52 static tree
53 calls_setjmp_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
54                 void *data ATTRIBUTE_UNUSED)
55 {
56   /* We're only interested in FUNCTION_DECLS.  */
57   if (TREE_CODE (*tp) != FUNCTION_DECL)
58     return NULL_TREE;
59
60   return setjmp_call_p (*tp) ? *tp : NULL_TREE;
61 }
62
63 /* Returns nonzero if FN calls `setjmp' or some other function that
64    can return more than once.  This function is conservative; it may
65    occasionally return a nonzero value even when FN does not actually
66    call `setjmp'.  */
67
68 bool
69 calls_setjmp_p (tree fn)
70 {
71   return walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
72                                        calls_setjmp_r,
73                                        NULL) != NULL_TREE;
74 }
75
76 /* CLONED_PARM is a copy of CLONE, generated for a cloned constructor
77    or destructor.  Update it to ensure that the source-position for
78    the cloned parameter matches that for the original, and that the
79    debugging generation code will be able to find the original PARM.  */
80
81 static void
82 update_cloned_parm (tree parm, tree cloned_parm)
83 {
84   DECL_ABSTRACT_ORIGIN (cloned_parm) = parm;
85
86   /* We may have taken its address.  */
87   TREE_ADDRESSABLE (cloned_parm) = TREE_ADDRESSABLE (parm);
88
89   /* The definition might have different constness.  */
90   TREE_READONLY (cloned_parm) = TREE_READONLY (parm);
91   
92   TREE_USED (cloned_parm) = TREE_USED (parm);
93   
94   /* The name may have changed from the declaration.  */
95   DECL_NAME (cloned_parm) = DECL_NAME (parm);
96   DECL_SOURCE_LOCATION (cloned_parm) = DECL_SOURCE_LOCATION (parm);
97 }
98
99 /* FN is a function that has a complete body.  Clone the body as
100    necessary.  Returns nonzero if there's no longer any need to
101    process the main body.  */
102
103 bool
104 maybe_clone_body (tree fn)
105 {
106   tree clone;
107
108   /* We only clone constructors and destructors.  */
109   if (!DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
110       && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
111     return 0;
112
113   /* Emit the DWARF1 abstract instance.  */
114   (*debug_hooks->deferred_inline_function) (fn);
115
116   /* We know that any clones immediately follow FN in the TYPE_METHODS
117      list.  */
118   for (clone = TREE_CHAIN (fn);
119        clone && DECL_CLONED_FUNCTION_P (clone);
120        clone = TREE_CHAIN (clone))
121     {
122       tree parm;
123       tree clone_parm;
124       int parmno;
125       splay_tree decl_map;
126
127       /* Update CLONE's source position information to match FN's.  */
128       DECL_SOURCE_LOCATION (clone) = DECL_SOURCE_LOCATION (fn);
129       DECL_INLINE (clone) = DECL_INLINE (fn);
130       DECL_DECLARED_INLINE_P (clone) = DECL_DECLARED_INLINE_P (fn);
131       DECL_COMDAT (clone) = DECL_COMDAT (fn);
132       DECL_WEAK (clone) = DECL_WEAK (fn);
133       DECL_ONE_ONLY (clone) = DECL_ONE_ONLY (fn);
134       DECL_SECTION_NAME (clone) = DECL_SECTION_NAME (fn);
135       DECL_USE_TEMPLATE (clone) = DECL_USE_TEMPLATE (fn);
136       DECL_EXTERNAL (clone) = DECL_EXTERNAL (fn);
137       DECL_INTERFACE_KNOWN (clone) = DECL_INTERFACE_KNOWN (fn);
138       DECL_NOT_REALLY_EXTERN (clone) = DECL_NOT_REALLY_EXTERN (fn);
139       TREE_PUBLIC (clone) = TREE_PUBLIC (fn);
140       DECL_VISIBILITY (clone) = DECL_VISIBILITY (fn);
141
142       /* Adjust the parameter names and locations.  */
143       parm = DECL_ARGUMENTS (fn);
144       clone_parm = DECL_ARGUMENTS (clone);
145       /* Update the `this' parameter, which is always first.  */
146       update_cloned_parm (parm, clone_parm);
147       parm = TREE_CHAIN (parm);
148       clone_parm = TREE_CHAIN (clone_parm);
149       if (DECL_HAS_IN_CHARGE_PARM_P (fn))
150         parm = TREE_CHAIN (parm);
151       if (DECL_HAS_VTT_PARM_P (fn))
152         parm = TREE_CHAIN (parm);
153       if (DECL_HAS_VTT_PARM_P (clone))
154         clone_parm = TREE_CHAIN (clone_parm);
155       for (; parm;
156            parm = TREE_CHAIN (parm), clone_parm = TREE_CHAIN (clone_parm))
157         /* Update this parameter.  */
158         update_cloned_parm (parm, clone_parm);
159
160       /* Start processing the function.  */
161       push_to_top_level ();
162       start_function (NULL_TREE, clone, NULL_TREE, SF_PRE_PARSED);
163
164       /* Remap the parameters.  */
165       decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
166       for (parmno = 0,
167              parm = DECL_ARGUMENTS (fn),
168              clone_parm = DECL_ARGUMENTS (clone);
169            parm;
170            ++parmno,
171              parm = TREE_CHAIN (parm))
172         {
173           /* Map the in-charge parameter to an appropriate constant.  */
174           if (DECL_HAS_IN_CHARGE_PARM_P (fn) && parmno == 1)
175             {
176               tree in_charge;
177               in_charge = in_charge_arg_for_name (DECL_NAME (clone));
178               splay_tree_insert (decl_map,
179                                  (splay_tree_key) parm,
180                                  (splay_tree_value) in_charge);
181             }
182           else if (DECL_ARTIFICIAL (parm)
183                    && DECL_NAME (parm) == vtt_parm_identifier)
184             {
185               /* For a subobject constructor or destructor, the next
186                  argument is the VTT parameter.  Remap the VTT_PARM
187                  from the CLONE to this parameter.  */
188               if (DECL_HAS_VTT_PARM_P (clone))
189                 {
190                   DECL_ABSTRACT_ORIGIN (clone_parm) = parm;
191                   splay_tree_insert (decl_map,
192                                      (splay_tree_key) parm,
193                                      (splay_tree_value) clone_parm);
194                   clone_parm = TREE_CHAIN (clone_parm);
195                 }
196               /* Otherwise, map the VTT parameter to `NULL'.  */
197               else
198                 {
199                   splay_tree_insert (decl_map,
200                                      (splay_tree_key) parm,
201                                      (splay_tree_value) null_pointer_node);
202                 }
203             }
204           /* Map other parameters to their equivalents in the cloned
205              function.  */
206           else
207             {
208               splay_tree_insert (decl_map,
209                                  (splay_tree_key) parm,
210                                  (splay_tree_value) clone_parm);
211               clone_parm = TREE_CHAIN (clone_parm);
212             }
213         }
214
215       /* Clone the body.  */
216       clone_body (clone, fn, decl_map);
217
218       /* Clean up.  */
219       splay_tree_delete (decl_map);
220
221       /* The clone can throw iff the original function can throw.  */
222       cp_function_chain->can_throw = !TREE_NOTHROW (fn);
223
224       /* Now, expand this function into RTL, if appropriate.  */
225       finish_function (0);
226       BLOCK_ABSTRACT_ORIGIN (DECL_INITIAL (clone)) = DECL_INITIAL (fn);
227       expand_or_defer_fn (clone);
228       pop_from_top_level ();
229     }
230
231   /* We don't need to process the original function any further.  */
232   return 1;
233 }