Update Copyright years for files modified in 2011 and/or 2012.
[platform/upstream/gcc.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    2010, 2011, 2012 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 "input.h"
29 #include "params.h"
30 #include "hashtab.h"
31 #include "target.h"
32 #include "debug.h"
33 #include "tree-inline.h"
34 #include "flags.h"
35 #include "langhooks.h"
36 #include "diagnostic-core.h"
37 #include "dumpfile.h"
38 #include "gimple.h"
39 #include "tree-iterator.h"
40 #include "cgraph.h"
41
42 /* Prototypes.  */
43
44 static void update_cloned_parm (tree, tree, bool);
45
46 /* CLONED_PARM is a copy of CLONE, generated for a cloned constructor
47    or destructor.  Update it to ensure that the source-position for
48    the cloned parameter matches that for the original, and that the
49    debugging generation code will be able to find the original PARM.  */
50
51 static void
52 update_cloned_parm (tree parm, tree cloned_parm, bool first)
53 {
54   DECL_ABSTRACT_ORIGIN (cloned_parm) = parm;
55
56   /* We may have taken its address.  */
57   TREE_ADDRESSABLE (cloned_parm) = TREE_ADDRESSABLE (parm);
58
59   /* The definition might have different constness.  */
60   TREE_READONLY (cloned_parm) = TREE_READONLY (parm);
61
62   TREE_USED (cloned_parm) = !first || TREE_USED (parm);
63
64   /* The name may have changed from the declaration.  */
65   DECL_NAME (cloned_parm) = DECL_NAME (parm);
66   DECL_SOURCE_LOCATION (cloned_parm) = DECL_SOURCE_LOCATION (parm);
67   TREE_TYPE (cloned_parm) = TREE_TYPE (parm);
68
69   DECL_GIMPLE_REG_P (cloned_parm) = DECL_GIMPLE_REG_P (parm);
70 }
71
72
73 /* FN is a function in High GIMPLE form that has a complete body and no
74    CFG.  CLONE is a function whose body is to be set to a copy of FN,
75    mapping argument declarations according to the ARG_MAP splay_tree.  */
76
77 static void
78 clone_body (tree clone, tree fn, void *arg_map)
79 {
80   copy_body_data id;
81   tree stmts;
82
83   /* Clone the body, as if we were making an inline call.  But, remap
84      the parameters in the callee to the parameters of caller.  */
85   memset (&id, 0, sizeof (id));
86   id.src_fn = fn;
87   id.dst_fn = clone;
88   id.src_cfun = DECL_STRUCT_FUNCTION (fn);
89   id.decl_map = (struct pointer_map_t *) arg_map;
90
91   id.copy_decl = copy_decl_no_change;
92   id.transform_call_graph_edges = CB_CGE_DUPLICATE;
93   id.transform_new_cfg = true;
94   id.transform_return_to_modify = false;
95   id.transform_lang_insert_block = NULL;
96
97   /* We're not inside any EH region.  */
98   id.eh_lp_nr = 0;
99
100   stmts = DECL_SAVED_TREE (fn);
101   walk_tree (&stmts, copy_tree_body_r, &id, NULL);
102
103   /* Also remap the initializer of any static variables so that they (in
104      particular, any label addresses) correspond to the base variant rather
105      than the abstract one.  */
106   if (DECL_NAME (clone) == base_dtor_identifier
107       || DECL_NAME (clone) == base_ctor_identifier)
108     {
109       unsigned ix;
110       tree decl;
111
112       FOR_EACH_LOCAL_DECL (DECL_STRUCT_FUNCTION (fn), ix, decl)
113         walk_tree (&DECL_INITIAL (decl), copy_tree_body_r, &id, NULL);
114     }
115
116   append_to_statement_list_force (stmts, &DECL_SAVED_TREE (clone));
117 }
118
119 /* DELETE_DTOR is a delete destructor whose body will be built.
120    COMPLETE_DTOR is the corresponding complete destructor.  */
121
122 static void
123 build_delete_destructor_body (tree delete_dtor, tree complete_dtor)
124 {
125   tree call_dtor, call_delete;
126   tree parm = DECL_ARGUMENTS (delete_dtor);
127   tree virtual_size = cxx_sizeof (current_class_type);
128
129   /* Call the corresponding complete destructor.  */
130   gcc_assert (complete_dtor);
131   call_dtor = build_cxx_call (complete_dtor, 1, &parm,
132                               tf_warning_or_error);
133   add_stmt (call_dtor);
134
135   add_stmt (build_stmt (0, LABEL_EXPR, cdtor_label));
136
137   /* Call the delete function.  */
138   call_delete = build_op_delete_call (DELETE_EXPR, current_class_ptr,
139                                       virtual_size,
140                                       /*global_p=*/false,
141                                       /*placement=*/NULL_TREE,
142                                       /*alloc_fn=*/NULL_TREE,
143                                       tf_warning_or_error);
144   add_stmt (call_delete);
145
146   /* Return the address of the object.  */
147   if (targetm.cxx.cdtor_returns_this ())
148     {
149       tree val = DECL_ARGUMENTS (delete_dtor);
150       val = build2 (MODIFY_EXPR, TREE_TYPE (val),
151                     DECL_RESULT (delete_dtor), val);
152       add_stmt (build_stmt (0, RETURN_EXPR, val));
153     }
154 }
155
156 /* Return name of comdat group for complete and base ctor (or dtor)
157    that have the same body.  If dtor is virtual, deleting dtor goes
158    into this comdat group as well.  */
159
160 static tree
161 cdtor_comdat_group (tree complete, tree base)
162 {
163   tree complete_name = DECL_COMDAT_GROUP (complete);
164   tree base_name = DECL_COMDAT_GROUP (base);
165   char *grp_name;
166   const char *p, *q;
167   bool diff_seen = false;
168   size_t idx;
169   if (complete_name == NULL)
170     complete_name = cxx_comdat_group (complete);
171   if (base_name == NULL)
172     base_name = cxx_comdat_group (base);
173   gcc_assert (IDENTIFIER_LENGTH (complete_name)
174               == IDENTIFIER_LENGTH (base_name));
175   grp_name = XALLOCAVEC (char, IDENTIFIER_LENGTH (complete_name) + 1);
176   p = IDENTIFIER_POINTER (complete_name);
177   q = IDENTIFIER_POINTER (base_name);
178   for (idx = 0; idx < IDENTIFIER_LENGTH (complete_name); idx++)
179     if (p[idx] == q[idx])
180       grp_name[idx] = p[idx];
181     else
182       {
183         gcc_assert (!diff_seen
184                     && idx > 0
185                     && (p[idx - 1] == 'C' || p[idx - 1] == 'D')
186                     && p[idx] == '1'
187                     && q[idx] == '2');
188         grp_name[idx] = '5';
189         diff_seen = true;
190       }
191   grp_name[idx] = '\0';
192   gcc_assert (diff_seen);
193   return get_identifier (grp_name);
194 }
195
196 /* FN is a function that has a complete body.  Clone the body as
197    necessary.  Returns nonzero if there's no longer any need to
198    process the main body.  */
199
200 bool
201 maybe_clone_body (tree fn)
202 {
203   tree comdat_group = NULL_TREE;
204   tree clone;
205   tree fns[3];
206   bool first = true;
207   bool in_charge_parm_used;
208   int idx;
209   bool need_alias = false;
210
211   /* We only clone constructors and destructors.  */
212   if (!DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
213       && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
214     return 0;
215
216   /* Emit the DWARF1 abstract instance.  */
217   (*debug_hooks->deferred_inline_function) (fn);
218
219   in_charge_parm_used = CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)) != NULL;
220   fns[0] = NULL_TREE;
221   fns[1] = NULL_TREE;
222   fns[2] = NULL_TREE;
223
224   /* Look for the complete destructor which may be used to build the
225      delete destructor.  */
226   FOR_EACH_CLONE (clone, fn)
227     if (DECL_NAME (clone) == complete_dtor_identifier
228         || DECL_NAME (clone) == complete_ctor_identifier)
229       fns[1] = clone;
230     else if (DECL_NAME (clone) == base_dtor_identifier
231              || DECL_NAME (clone) == base_ctor_identifier)
232       fns[0] = clone;
233     else if (DECL_NAME (clone) == deleting_dtor_identifier)
234       fns[2] = clone;
235     else
236       gcc_unreachable ();
237
238   /* Remember if we can't have multiple clones for some reason.  We need to
239      check this before we remap local static initializers in clone_body.  */
240   if (!tree_versionable_function_p (fn))
241     need_alias = true;
242
243   /* We know that any clones immediately follow FN in the TYPE_METHODS
244      list.  */
245   push_to_top_level ();
246   for (idx = 0; idx < 3; idx++)
247     {
248       tree parm;
249       tree clone_parm;
250       int parmno;
251       bool alias = false;
252       struct pointer_map_t *decl_map;
253
254       clone = fns[idx];
255       if (!clone)
256         continue;      
257
258       /* Update CLONE's source position information to match FN's.  */
259       DECL_SOURCE_LOCATION (clone) = DECL_SOURCE_LOCATION (fn);
260       DECL_DECLARED_INLINE_P (clone) = DECL_DECLARED_INLINE_P (fn);
261       DECL_DECLARED_CONSTEXPR_P (clone) = DECL_DECLARED_CONSTEXPR_P (fn);
262       DECL_COMDAT (clone) = DECL_COMDAT (fn);
263       DECL_WEAK (clone) = DECL_WEAK (fn);
264
265       /* We don't copy the comdat group from fn to clone because the assembler
266          name of fn was corrupted by write_mangled_name by adding *INTERNAL*
267          to it. By doing so, it also corrupted the comdat group. */
268       if (DECL_ONE_ONLY (fn))
269         DECL_COMDAT_GROUP (clone) = cxx_comdat_group (clone);
270       DECL_SECTION_NAME (clone) = DECL_SECTION_NAME (fn);
271       DECL_USE_TEMPLATE (clone) = DECL_USE_TEMPLATE (fn);
272       DECL_EXTERNAL (clone) = DECL_EXTERNAL (fn);
273       DECL_INTERFACE_KNOWN (clone) = DECL_INTERFACE_KNOWN (fn);
274       DECL_NOT_REALLY_EXTERN (clone) = DECL_NOT_REALLY_EXTERN (fn);
275       TREE_PUBLIC (clone) = TREE_PUBLIC (fn);
276       DECL_VISIBILITY (clone) = DECL_VISIBILITY (fn);
277       DECL_VISIBILITY_SPECIFIED (clone) = DECL_VISIBILITY_SPECIFIED (fn);
278       DECL_DLLIMPORT_P (clone) = DECL_DLLIMPORT_P (fn);
279       DECL_ATTRIBUTES (clone) = copy_list (DECL_ATTRIBUTES (fn));
280       DECL_DISREGARD_INLINE_LIMITS (clone) = DECL_DISREGARD_INLINE_LIMITS (fn);
281
282       /* Adjust the parameter names and locations.  */
283       parm = DECL_ARGUMENTS (fn);
284       clone_parm = DECL_ARGUMENTS (clone);
285       /* Update the `this' parameter, which is always first.  */
286       update_cloned_parm (parm, clone_parm, first);
287       parm = DECL_CHAIN (parm);
288       clone_parm = DECL_CHAIN (clone_parm);
289       if (DECL_HAS_IN_CHARGE_PARM_P (fn))
290         parm = DECL_CHAIN (parm);
291       if (DECL_HAS_VTT_PARM_P (fn))
292         parm = DECL_CHAIN (parm);
293       if (DECL_HAS_VTT_PARM_P (clone))
294         clone_parm = DECL_CHAIN (clone_parm);
295       for (; parm;
296            parm = DECL_CHAIN (parm), clone_parm = DECL_CHAIN (clone_parm))
297         /* Update this parameter.  */
298         update_cloned_parm (parm, clone_parm, first);
299
300       /* Start processing the function.  */
301       start_preparsed_function (clone, NULL_TREE, SF_PRE_PARSED);
302
303       /* Tell cgraph if both ctors or both dtors are known to have
304          the same body.  */
305       if (!in_charge_parm_used
306           && fns[0]
307           && idx == 1
308           && !flag_use_repository
309           && DECL_INTERFACE_KNOWN (fns[0])
310           && (SUPPORTS_ONE_ONLY || !DECL_WEAK (fns[0]))
311           && (!DECL_ONE_ONLY (fns[0])
312               || (HAVE_COMDAT_GROUP
313                   && DECL_WEAK (fns[0])))
314           && !flag_syntax_only
315           /* Set linkage flags appropriately before
316              cgraph_create_function_alias looks at them.  */
317           && expand_or_defer_fn_1 (clone)
318           && cgraph_same_body_alias (cgraph_get_node (fns[0]),
319                                      clone, fns[0]))
320         {
321           alias = true;
322           if (DECL_ONE_ONLY (fns[0]))
323             {
324               /* For comdat base and complete cdtors put them
325                  into the same, *[CD]5* comdat group instead of
326                  *[CD][12]*.  */
327               comdat_group = cdtor_comdat_group (fns[1], fns[0]);
328               DECL_COMDAT_GROUP (fns[0]) = comdat_group;
329               symtab_add_to_same_comdat_group (symtab_get_node (clone),
330                                                symtab_get_node (fns[0]));
331             }
332         }
333
334       /* Build the delete destructor by calling complete destructor
335          and delete function.  */
336       if (idx == 2)
337         {
338           build_delete_destructor_body (clone, fns[1]);
339           /* If *[CD][12]* dtors go into the *[CD]5* comdat group and dtor is
340              virtual, it goes into the same comdat group as well.  */
341           if (comdat_group)
342             symtab_add_to_same_comdat_group
343                ((symtab_node) cgraph_get_create_node (clone),
344                 symtab_get_node (fns[0]));
345         }
346       else if (alias)
347         /* No need to populate body.  */ ;
348       else
349         {
350           /* If we can't have multiple copies of FN (say, because there's a
351              static local initialized with the address of a label), we need
352              to use an alias for the complete variant.  */
353           if (idx == 1 && need_alias)
354             {
355               if (DECL_STRUCT_FUNCTION (fn)->cannot_be_copied_set)
356                 sorry (DECL_STRUCT_FUNCTION (fn)->cannot_be_copied_reason, fn);
357               else
358                 sorry ("making multiple clones of %qD", fn);
359             }
360
361           /* Remap the parameters.  */
362           decl_map = pointer_map_create ();
363           for (parmno = 0,
364                 parm = DECL_ARGUMENTS (fn),
365                 clone_parm = DECL_ARGUMENTS (clone);
366               parm;
367               ++parmno,
368                 parm = DECL_CHAIN (parm))
369             {
370               /* Map the in-charge parameter to an appropriate constant.  */
371               if (DECL_HAS_IN_CHARGE_PARM_P (fn) && parmno == 1)
372                 {
373                   tree in_charge;
374                   in_charge = in_charge_arg_for_name (DECL_NAME (clone));
375                   *pointer_map_insert (decl_map, parm) = in_charge;
376                 }
377               else if (DECL_ARTIFICIAL (parm)
378                        && DECL_NAME (parm) == vtt_parm_identifier)
379                 {
380                   /* For a subobject constructor or destructor, the next
381                      argument is the VTT parameter.  Remap the VTT_PARM
382                      from the CLONE to this parameter.  */
383                   if (DECL_HAS_VTT_PARM_P (clone))
384                     {
385                       DECL_ABSTRACT_ORIGIN (clone_parm) = parm;
386                       *pointer_map_insert (decl_map, parm) = clone_parm;
387                       clone_parm = DECL_CHAIN (clone_parm);
388                     }
389                   /* Otherwise, map the VTT parameter to `NULL'.  */
390                   else
391                     *pointer_map_insert (decl_map, parm)
392                        = fold_convert (TREE_TYPE (parm), null_pointer_node);
393                 }
394               /* Map other parameters to their equivalents in the cloned
395                  function.  */
396               else
397                 {
398                   *pointer_map_insert (decl_map, parm) = clone_parm;
399                   clone_parm = DECL_CHAIN (clone_parm);
400                 }
401             }
402
403           if (targetm.cxx.cdtor_returns_this ())
404             {
405               parm = DECL_RESULT (fn);
406               clone_parm = DECL_RESULT (clone);
407               *pointer_map_insert (decl_map, parm) = clone_parm;
408             }
409
410           /* Clone the body.  */
411           clone_body (clone, fn, decl_map);
412
413           /* Clean up.  */
414           pointer_map_destroy (decl_map);
415         }
416
417       /* The clone can throw iff the original function can throw.  */
418       cp_function_chain->can_throw = !TREE_NOTHROW (fn);
419
420       /* Now, expand this function into RTL, if appropriate.  */
421       finish_function (0);
422       BLOCK_ABSTRACT_ORIGIN (DECL_INITIAL (clone)) = DECL_INITIAL (fn);
423       if (alias)
424         {
425           if (expand_or_defer_fn_1 (clone))
426             emit_associated_thunks (clone);
427         }
428       else
429         expand_or_defer_fn (clone);
430       first = false;
431     }
432   pop_from_top_level ();
433
434   /* We don't need to process the original function any further.  */
435   return 1;
436 }