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