Merged with mainline at revision 128810.
[official-gcc.git] / gcc / cp / optimize.c
blob0f5017365e37bb11789617833db81560f4c86896
1 /* Perform optimizations on tree structure.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2007
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 "tree-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);
75 /* FN is a function that has a complete body. Clone the body as
76 necessary. Returns nonzero if there's no longer any need to
77 process the main body. */
79 bool
80 maybe_clone_body (tree fn)
82 tree clone;
83 bool first = true;
85 /* We only clone constructors and destructors. */
86 if (!DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
87 && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
88 return 0;
90 /* Emit the DWARF1 abstract instance. */
91 (*debug_hooks->deferred_inline_function) (fn);
93 /* We know that any clones immediately follow FN in the TYPE_METHODS
94 list. */
95 push_to_top_level ();
96 FOR_EACH_CLONE (clone, fn)
98 tree parm;
99 tree clone_parm;
100 int parmno;
101 struct pointer_map_t *decl_map;
103 /* Update CLONE's source position information to match FN's. */
104 DECL_SOURCE_LOCATION (clone) = DECL_SOURCE_LOCATION (fn);
105 DECL_INLINE (clone) = DECL_INLINE (fn);
106 DECL_DECLARED_INLINE_P (clone) = DECL_DECLARED_INLINE_P (fn);
107 DECL_COMDAT (clone) = DECL_COMDAT (fn);
108 DECL_WEAK (clone) = DECL_WEAK (fn);
109 DECL_ONE_ONLY (clone) = DECL_ONE_ONLY (fn);
110 DECL_SECTION_NAME (clone) = DECL_SECTION_NAME (fn);
111 DECL_USE_TEMPLATE (clone) = DECL_USE_TEMPLATE (fn);
112 DECL_EXTERNAL (clone) = DECL_EXTERNAL (fn);
113 DECL_INTERFACE_KNOWN (clone) = DECL_INTERFACE_KNOWN (fn);
114 DECL_NOT_REALLY_EXTERN (clone) = DECL_NOT_REALLY_EXTERN (fn);
115 TREE_PUBLIC (clone) = TREE_PUBLIC (fn);
116 DECL_VISIBILITY (clone) = DECL_VISIBILITY (fn);
117 DECL_VISIBILITY_SPECIFIED (clone) = DECL_VISIBILITY_SPECIFIED (fn);
119 /* Adjust the parameter names and locations. */
120 parm = DECL_ARGUMENTS (fn);
121 clone_parm = DECL_ARGUMENTS (clone);
122 /* Update the `this' parameter, which is always first. */
123 update_cloned_parm (parm, clone_parm, first);
124 parm = TREE_CHAIN (parm);
125 clone_parm = TREE_CHAIN (clone_parm);
126 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
127 parm = TREE_CHAIN (parm);
128 if (DECL_HAS_VTT_PARM_P (fn))
129 parm = TREE_CHAIN (parm);
130 if (DECL_HAS_VTT_PARM_P (clone))
131 clone_parm = TREE_CHAIN (clone_parm);
132 for (; parm;
133 parm = TREE_CHAIN (parm), clone_parm = TREE_CHAIN (clone_parm))
134 /* Update this parameter. */
135 update_cloned_parm (parm, clone_parm, first);
137 /* Start processing the function. */
138 start_preparsed_function (clone, NULL_TREE, SF_PRE_PARSED);
140 /* Remap the parameters. */
141 decl_map = pointer_map_create ();
142 for (parmno = 0,
143 parm = DECL_ARGUMENTS (fn),
144 clone_parm = DECL_ARGUMENTS (clone);
145 parm;
146 ++parmno,
147 parm = TREE_CHAIN (parm))
149 /* Map the in-charge parameter to an appropriate constant. */
150 if (DECL_HAS_IN_CHARGE_PARM_P (fn) && parmno == 1)
152 tree in_charge;
153 in_charge = in_charge_arg_for_name (DECL_NAME (clone));
154 *pointer_map_insert (decl_map, parm) = in_charge;
156 else if (DECL_ARTIFICIAL (parm)
157 && DECL_NAME (parm) == vtt_parm_identifier)
159 /* For a subobject constructor or destructor, the next
160 argument is the VTT parameter. Remap the VTT_PARM
161 from the CLONE to this parameter. */
162 if (DECL_HAS_VTT_PARM_P (clone))
164 DECL_ABSTRACT_ORIGIN (clone_parm) = parm;
165 *pointer_map_insert (decl_map, parm) = clone_parm;
166 clone_parm = TREE_CHAIN (clone_parm);
168 /* Otherwise, map the VTT parameter to `NULL'. */
169 else
170 *pointer_map_insert (decl_map, parm) = null_pointer_node;
172 /* Map other parameters to their equivalents in the cloned
173 function. */
174 else
176 *pointer_map_insert (decl_map, parm) = clone_parm;
177 clone_parm = TREE_CHAIN (clone_parm);
181 if (targetm.cxx.cdtor_returns_this ())
183 parm = DECL_RESULT (fn);
184 clone_parm = DECL_RESULT (clone);
185 *pointer_map_insert (decl_map, parm) = clone_parm;
187 /* Clone the body. */
188 clone_body (clone, fn, decl_map);
190 /* Clean up. */
191 pointer_map_destroy (decl_map);
193 /* The clone can throw iff the original function can throw. */
194 cp_function_chain->can_throw = !TREE_NOTHROW (fn);
196 /* Now, expand this function into RTL, if appropriate. */
197 finish_function (0);
198 BLOCK_ABSTRACT_ORIGIN (DECL_INITIAL (clone)) = DECL_INITIAL (fn);
199 expand_or_defer_fn (clone);
200 first = false;
202 pop_from_top_level ();
204 /* We don't need to process the original function any further. */
205 return 1;