OSDN Git Service

2009-11-06 Andrew Pinski <andrew_pinski@playstation.sony.com>
[pf3gnuchains/gcc-fork.git] / gcc / cp / optimize.c
1 /* Perform optimizations on tree structure.
2    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2007, 2008, 2009
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 3, 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 COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
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 "target.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 "gimple.h"
44 #include "tree-iterator.h"
45
46 /* Prototypes.  */
47
48 static void update_cloned_parm (tree, tree, bool);
49
50 /* CLONED_PARM is a copy of CLONE, generated for a cloned constructor
51    or destructor.  Update it to ensure that the source-position for
52    the cloned parameter matches that for the original, and that the
53    debugging generation code will be able to find the original PARM.  */
54
55 static void
56 update_cloned_parm (tree parm, tree cloned_parm, bool first)
57 {
58   DECL_ABSTRACT_ORIGIN (cloned_parm) = parm;
59
60   /* We may have taken its address.  */
61   TREE_ADDRESSABLE (cloned_parm) = TREE_ADDRESSABLE (parm);
62
63   /* The definition might have different constness.  */
64   TREE_READONLY (cloned_parm) = TREE_READONLY (parm);
65
66   TREE_USED (cloned_parm) = !first || TREE_USED (parm);
67
68   /* The name may have changed from the declaration.  */
69   DECL_NAME (cloned_parm) = DECL_NAME (parm);
70   DECL_SOURCE_LOCATION (cloned_parm) = DECL_SOURCE_LOCATION (parm);
71   TREE_TYPE (cloned_parm) = TREE_TYPE (parm);
72
73   DECL_GIMPLE_REG_P (cloned_parm) = DECL_GIMPLE_REG_P (parm);
74 }
75
76
77 /* FN is a function in High GIMPLE form that has a complete body and no
78    CFG.  CLONE is a function whose body is to be set to a copy of FN,
79    mapping argument declarations according to the ARG_MAP splay_tree.  */
80
81 static void
82 clone_body (tree clone, tree fn, void *arg_map)
83 {
84   copy_body_data id;
85   tree stmts;
86
87   /* Clone the body, as if we were making an inline call.  But, remap
88      the parameters in the callee to the parameters of caller.  */
89   memset (&id, 0, sizeof (id));
90   id.src_fn = fn;
91   id.dst_fn = clone;
92   id.src_cfun = DECL_STRUCT_FUNCTION (fn);
93   id.decl_map = (struct pointer_map_t *) arg_map;
94
95   id.copy_decl = copy_decl_no_change;
96   id.transform_call_graph_edges = CB_CGE_DUPLICATE;
97   id.transform_new_cfg = true;
98   id.transform_return_to_modify = false;
99   id.transform_lang_insert_block = NULL;
100
101   /* We're not inside any EH region.  */
102   id.eh_lp_nr = 0;
103
104   stmts = DECL_SAVED_TREE (fn);
105   walk_tree (&stmts, copy_tree_body_r, &id, NULL);
106   append_to_statement_list_force (stmts, &DECL_SAVED_TREE (clone));
107 }
108
109 /* DELETE_DTOR is a delete destructor whose body will be built.
110    COMPLETE_DTOR is the corresponding complete destructor.  */
111
112 static void
113 build_delete_destructor_body (tree delete_dtor, tree complete_dtor)
114 {
115   tree call_dtor, call_delete;
116   tree parm = DECL_ARGUMENTS (delete_dtor);
117   tree virtual_size = cxx_sizeof (current_class_type);
118
119   /* Call the corresponding complete destructor.  */
120   gcc_assert (complete_dtor);
121   call_dtor = build_cxx_call (complete_dtor, 1, &parm);
122   add_stmt (call_dtor);
123
124   add_stmt (build_stmt (0, LABEL_EXPR, cdtor_label));
125
126   /* Call the delete function.  */
127   call_delete = build_op_delete_call (DELETE_EXPR, current_class_ptr,
128                                       virtual_size,
129                                       /*global_p=*/false,
130                                       /*placement=*/NULL_TREE,
131                                       /*alloc_fn=*/NULL_TREE);
132   add_stmt (call_delete);
133
134   /* Return the address of the object.  */
135   if (targetm.cxx.cdtor_returns_this ())
136     {
137       tree val = DECL_ARGUMENTS (delete_dtor);
138       val = build2 (MODIFY_EXPR, TREE_TYPE (val),
139                     DECL_RESULT (delete_dtor), val);
140       add_stmt (build_stmt (0, RETURN_EXPR, val));
141     }
142 }
143
144 /* FN is a function that has a complete body.  Clone the body as
145    necessary.  Returns nonzero if there's no longer any need to
146    process the main body.  */
147
148 bool
149 maybe_clone_body (tree fn)
150 {
151   tree clone;
152   tree complete_dtor = NULL_TREE;
153   bool first = true;
154
155   /* We only clone constructors and destructors.  */
156   if (!DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
157       && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
158     return 0;
159
160   /* Emit the DWARF1 abstract instance.  */
161   (*debug_hooks->deferred_inline_function) (fn);
162
163   /* Look for the complete destructor which may be used to build the
164      delete destructor.  */
165   FOR_EACH_CLONE (clone, fn)
166     if (DECL_NAME (clone) == complete_dtor_identifier)
167       {
168         complete_dtor = clone;
169         break;
170       }
171
172   /* We know that any clones immediately follow FN in the TYPE_METHODS
173      list.  */
174   push_to_top_level ();
175   FOR_EACH_CLONE (clone, fn)
176     {
177       tree parm;
178       tree clone_parm;
179       int parmno;
180       struct pointer_map_t *decl_map;
181
182       /* Update CLONE's source position information to match FN's.  */
183       DECL_SOURCE_LOCATION (clone) = DECL_SOURCE_LOCATION (fn);
184       DECL_DECLARED_INLINE_P (clone) = DECL_DECLARED_INLINE_P (fn);
185       DECL_COMDAT (clone) = DECL_COMDAT (fn);
186       DECL_WEAK (clone) = DECL_WEAK (fn);
187
188       /* We don't copy the comdat group from fn to clone because the assembler
189          name of fn was corrupted by write_mangled_name by adding *INTERNAL*
190          to it. By doing so, it also corrupted the comdat group. */
191       if (DECL_ONE_ONLY (fn))
192         DECL_COMDAT_GROUP (clone) = cxx_comdat_group (clone);
193       DECL_SECTION_NAME (clone) = DECL_SECTION_NAME (fn);
194       DECL_USE_TEMPLATE (clone) = DECL_USE_TEMPLATE (fn);
195       DECL_EXTERNAL (clone) = DECL_EXTERNAL (fn);
196       DECL_INTERFACE_KNOWN (clone) = DECL_INTERFACE_KNOWN (fn);
197       DECL_NOT_REALLY_EXTERN (clone) = DECL_NOT_REALLY_EXTERN (fn);
198       TREE_PUBLIC (clone) = TREE_PUBLIC (fn);
199       DECL_VISIBILITY (clone) = DECL_VISIBILITY (fn);
200       DECL_VISIBILITY_SPECIFIED (clone) = DECL_VISIBILITY_SPECIFIED (fn);
201       DECL_DLLIMPORT_P (clone) = DECL_DLLIMPORT_P (fn);
202       DECL_ATTRIBUTES (clone) = copy_list (DECL_ATTRIBUTES (fn));
203       DECL_DISREGARD_INLINE_LIMITS (clone) = DECL_DISREGARD_INLINE_LIMITS (fn);
204
205       /* Adjust the parameter names and locations.  */
206       parm = DECL_ARGUMENTS (fn);
207       clone_parm = DECL_ARGUMENTS (clone);
208       /* Update the `this' parameter, which is always first.  */
209       update_cloned_parm (parm, clone_parm, first);
210       parm = TREE_CHAIN (parm);
211       clone_parm = TREE_CHAIN (clone_parm);
212       if (DECL_HAS_IN_CHARGE_PARM_P (fn))
213         parm = TREE_CHAIN (parm);
214       if (DECL_HAS_VTT_PARM_P (fn))
215         parm = TREE_CHAIN (parm);
216       if (DECL_HAS_VTT_PARM_P (clone))
217         clone_parm = TREE_CHAIN (clone_parm);
218       for (; parm;
219            parm = TREE_CHAIN (parm), clone_parm = TREE_CHAIN (clone_parm))
220         /* Update this parameter.  */
221         update_cloned_parm (parm, clone_parm, first);
222
223       /* Start processing the function.  */
224       start_preparsed_function (clone, NULL_TREE, SF_PRE_PARSED);
225
226       /* Build the delete destructor by calling complete destructor
227          and delete function.  */
228       if (DECL_NAME (clone) == deleting_dtor_identifier)
229         build_delete_destructor_body (clone, complete_dtor);
230       else
231         {
232           /* Remap the parameters.  */
233           decl_map = pointer_map_create ();
234           for (parmno = 0,
235                 parm = DECL_ARGUMENTS (fn),
236                 clone_parm = DECL_ARGUMENTS (clone);
237               parm;
238               ++parmno,
239                 parm = TREE_CHAIN (parm))
240             {
241               /* Map the in-charge parameter to an appropriate constant.  */
242               if (DECL_HAS_IN_CHARGE_PARM_P (fn) && parmno == 1)
243                 {
244                   tree in_charge;
245                   in_charge = in_charge_arg_for_name (DECL_NAME (clone));
246                   *pointer_map_insert (decl_map, parm) = in_charge;
247                 }
248               else if (DECL_ARTIFICIAL (parm)
249                        && DECL_NAME (parm) == vtt_parm_identifier)
250                 {
251                   /* For a subobject constructor or destructor, the next
252                      argument is the VTT parameter.  Remap the VTT_PARM
253                      from the CLONE to this parameter.  */
254                   if (DECL_HAS_VTT_PARM_P (clone))
255                     {
256                       DECL_ABSTRACT_ORIGIN (clone_parm) = parm;
257                       *pointer_map_insert (decl_map, parm) = clone_parm;
258                       clone_parm = TREE_CHAIN (clone_parm);
259                     }
260                   /* Otherwise, map the VTT parameter to `NULL'.  */
261                   else
262                     *pointer_map_insert (decl_map, parm)
263                        = fold_convert (TREE_TYPE (parm), null_pointer_node);
264                 }
265               /* Map other parameters to their equivalents in the cloned
266                  function.  */
267               else
268                 {
269                   *pointer_map_insert (decl_map, parm) = clone_parm;
270                   clone_parm = TREE_CHAIN (clone_parm);
271                 }
272             }
273
274           if (targetm.cxx.cdtor_returns_this ())
275             {
276               parm = DECL_RESULT (fn);
277               clone_parm = DECL_RESULT (clone);
278               *pointer_map_insert (decl_map, parm) = clone_parm;
279             }
280
281           /* Clone the body.  */
282           clone_body (clone, fn, decl_map);
283
284           /* Clean up.  */
285           pointer_map_destroy (decl_map);
286         }
287
288       /* The clone can throw iff the original function can throw.  */
289       cp_function_chain->can_throw = !TREE_NOTHROW (fn);
290
291       /* Now, expand this function into RTL, if appropriate.  */
292       finish_function (0);
293       BLOCK_ABSTRACT_ORIGIN (DECL_INITIAL (clone)) = DECL_INITIAL (fn);
294       expand_or_defer_fn (clone);
295       first = false;
296     }
297   pop_from_top_level ();
298
299   /* We don't need to process the original function any further.  */
300   return 1;
301 }