gcc/
[official-gcc.git] / gcc / cp / optimize.c
blob8c7b9e82ccb5cbcc700dfe3d42651b953a09a11b
1 /* Perform optimizations on tree structure.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2007, 2008
3 Free Software Foundation, Inc.
4 Written by Mark Michell (mark@codesourcery.com).
6 This file is part of GCC.
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.
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.
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/>. */
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"
45 /* Prototypes. */
47 static void update_cloned_parm (tree, tree, bool);
49 /* CLONED_PARM is a copy of CLONE, generated for a cloned constructor
50 or destructor. Update it to ensure that the source-position for
51 the cloned parameter matches that for the original, and that the
52 debugging generation code will be able to find the original PARM. */
54 static void
55 update_cloned_parm (tree parm, tree cloned_parm, bool first)
57 DECL_ABSTRACT_ORIGIN (cloned_parm) = parm;
59 /* We may have taken its address. */
60 TREE_ADDRESSABLE (cloned_parm) = TREE_ADDRESSABLE (parm);
62 /* The definition might have different constness. */
63 TREE_READONLY (cloned_parm) = TREE_READONLY (parm);
65 TREE_USED (cloned_parm) = !first || TREE_USED (parm);
67 /* The name may have changed from the declaration. */
68 DECL_NAME (cloned_parm) = DECL_NAME (parm);
69 DECL_SOURCE_LOCATION (cloned_parm) = DECL_SOURCE_LOCATION (parm);
70 TREE_TYPE (cloned_parm) = TREE_TYPE (parm);
72 DECL_GIMPLE_REG_P (cloned_parm) = DECL_GIMPLE_REG_P (parm);
76 /* FN is a function in High GIMPLE form that has a complete body and no
77 CFG. CLONE is a function whose body is to be set to a copy of FN,
78 mapping argument declarations according to the ARG_MAP splay_tree. */
80 static void
81 clone_body (tree clone, tree fn, void *arg_map)
83 copy_body_data id;
84 gimple_seq new_body;
86 /* FN must already be in GIMPLE form. */
87 gcc_assert (gimple_body (fn));
89 /* Clone the body, as if we were making an inline call. But, remap
90 the parameters in the callee to the parameters of caller. */
91 memset (&id, 0, sizeof (id));
92 id.src_fn = fn;
93 id.dst_fn = clone;
94 id.src_cfun = DECL_STRUCT_FUNCTION (fn);
95 id.decl_map = (struct pointer_map_t *) arg_map;
97 id.copy_decl = copy_decl_no_change;
98 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
99 id.transform_new_cfg = true;
100 id.transform_return_to_modify = false;
101 id.transform_lang_insert_block = NULL;
103 /* We're not inside any EH region. */
104 id.eh_region = -1;
106 /* Actually copy the body. */
107 new_body = remap_gimple_seq (gimple_body (fn), &id);
108 gimple_set_body (clone, new_body);
111 /* FN is a function that has a complete body. Clone the body as
112 necessary. Returns nonzero if there's no longer any need to
113 process the main body. */
115 bool
116 maybe_clone_body (tree fn)
118 tree clone;
119 bool first = true;
121 /* We only clone constructors and destructors. */
122 if (!DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
123 && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
124 return 0;
126 /* Emit the DWARF1 abstract instance. */
127 (*debug_hooks->deferred_inline_function) (fn);
129 /* We know that any clones immediately follow FN in the TYPE_METHODS
130 list. */
131 push_to_top_level ();
132 FOR_EACH_CLONE (clone, fn)
134 tree parm;
135 tree clone_parm;
136 int parmno;
137 struct pointer_map_t *decl_map;
139 /* Update CLONE's source position information to match FN's. */
140 DECL_SOURCE_LOCATION (clone) = DECL_SOURCE_LOCATION (fn);
141 DECL_DECLARED_INLINE_P (clone) = DECL_DECLARED_INLINE_P (fn);
142 DECL_COMDAT (clone) = DECL_COMDAT (fn);
143 DECL_WEAK (clone) = DECL_WEAK (fn);
144 DECL_ONE_ONLY (clone) = DECL_ONE_ONLY (fn);
145 DECL_SECTION_NAME (clone) = DECL_SECTION_NAME (fn);
146 DECL_USE_TEMPLATE (clone) = DECL_USE_TEMPLATE (fn);
147 DECL_EXTERNAL (clone) = DECL_EXTERNAL (fn);
148 DECL_INTERFACE_KNOWN (clone) = DECL_INTERFACE_KNOWN (fn);
149 DECL_NOT_REALLY_EXTERN (clone) = DECL_NOT_REALLY_EXTERN (fn);
150 TREE_PUBLIC (clone) = TREE_PUBLIC (fn);
151 DECL_VISIBILITY (clone) = DECL_VISIBILITY (fn);
152 DECL_VISIBILITY_SPECIFIED (clone) = DECL_VISIBILITY_SPECIFIED (fn);
153 DECL_DLLIMPORT_P (clone) = DECL_DLLIMPORT_P (fn);
155 /* Adjust the parameter names and locations. */
156 parm = DECL_ARGUMENTS (fn);
157 clone_parm = DECL_ARGUMENTS (clone);
158 /* Update the `this' parameter, which is always first. */
159 update_cloned_parm (parm, clone_parm, first);
160 parm = TREE_CHAIN (parm);
161 clone_parm = TREE_CHAIN (clone_parm);
162 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
163 parm = TREE_CHAIN (parm);
164 if (DECL_HAS_VTT_PARM_P (fn))
165 parm = TREE_CHAIN (parm);
166 if (DECL_HAS_VTT_PARM_P (clone))
167 clone_parm = TREE_CHAIN (clone_parm);
168 for (; parm;
169 parm = TREE_CHAIN (parm), clone_parm = TREE_CHAIN (clone_parm))
170 /* Update this parameter. */
171 update_cloned_parm (parm, clone_parm, first);
173 /* Start processing the function. */
174 start_preparsed_function (clone, NULL_TREE, SF_PRE_PARSED);
176 /* Remap the parameters. */
177 decl_map = pointer_map_create ();
178 for (parmno = 0,
179 parm = DECL_ARGUMENTS (fn),
180 clone_parm = DECL_ARGUMENTS (clone);
181 parm;
182 ++parmno,
183 parm = TREE_CHAIN (parm))
185 /* Map the in-charge parameter to an appropriate constant. */
186 if (DECL_HAS_IN_CHARGE_PARM_P (fn) && parmno == 1)
188 tree in_charge;
189 in_charge = in_charge_arg_for_name (DECL_NAME (clone));
190 *pointer_map_insert (decl_map, parm) = in_charge;
192 else if (DECL_ARTIFICIAL (parm)
193 && DECL_NAME (parm) == vtt_parm_identifier)
195 /* For a subobject constructor or destructor, the next
196 argument is the VTT parameter. Remap the VTT_PARM
197 from the CLONE to this parameter. */
198 if (DECL_HAS_VTT_PARM_P (clone))
200 DECL_ABSTRACT_ORIGIN (clone_parm) = parm;
201 *pointer_map_insert (decl_map, parm) = clone_parm;
202 clone_parm = TREE_CHAIN (clone_parm);
204 /* Otherwise, map the VTT parameter to `NULL'. */
205 else
206 *pointer_map_insert (decl_map, parm) = null_pointer_node;
208 /* Map other parameters to their equivalents in the cloned
209 function. */
210 else
212 *pointer_map_insert (decl_map, parm) = clone_parm;
213 clone_parm = TREE_CHAIN (clone_parm);
217 if (targetm.cxx.cdtor_returns_this ())
219 parm = DECL_RESULT (fn);
220 clone_parm = DECL_RESULT (clone);
221 *pointer_map_insert (decl_map, parm) = clone_parm;
223 /* Clone the body. */
224 clone_body (clone, fn, decl_map);
226 /* Clean up. */
227 pointer_map_destroy (decl_map);
229 /* The clone can throw iff the original function can throw. */
230 cp_function_chain->can_throw = !TREE_NOTHROW (fn);
232 /* Now, expand this function into RTL, if appropriate. */
233 finish_function (0);
234 BLOCK_ABSTRACT_ORIGIN (DECL_INITIAL (clone)) = DECL_INITIAL (fn);
235 DECL_SAVED_TREE (clone) = NULL;
236 expand_or_defer_fn (clone);
237 first = false;
239 pop_from_top_level ();
241 /* We don't need to process the original function any further. */
242 return 1;