PR target/4198
[official-gcc.git] / gcc / cp / optimize.c
blob7ccfaffbf0db38ab8ed0a1e73e422bc3207f39a8
1 /* Perform optimizations on tree structure.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004
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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "rtl.h"
30 #include "insn-config.h"
31 #include "input.h"
32 #include "integrate.h"
33 #include "toplev.h"
34 #include "varray.h"
35 #include "params.h"
36 #include "hashtab.h"
37 #include "target.h"
38 #include "debug.h"
39 #include "tree-inline.h"
40 #include "flags.h"
41 #include "langhooks.h"
42 #include "diagnostic.h"
43 #include "tree-dump.h"
44 #include "tree-gimple.h"
46 /* Prototypes. */
48 static void update_cloned_parm (tree, tree);
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. */
55 static void
56 update_cloned_parm (tree parm, tree cloned_parm)
58 DECL_ABSTRACT_ORIGIN (cloned_parm) = parm;
60 /* We may have taken its address. */
61 TREE_ADDRESSABLE (cloned_parm) = TREE_ADDRESSABLE (parm);
63 /* The definition might have different constness. */
64 TREE_READONLY (cloned_parm) = TREE_READONLY (parm);
66 TREE_USED (cloned_parm) = TREE_USED (parm);
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);
73 /* FN is a function that has a complete body. Clone the body as
74 necessary. Returns nonzero if there's no longer any need to
75 process the main body. */
77 bool
78 maybe_clone_body (tree fn)
80 tree clone;
82 /* We only clone constructors and destructors. */
83 if (!DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
84 && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
85 return 0;
87 /* Emit the DWARF1 abstract instance. */
88 (*debug_hooks->deferred_inline_function) (fn);
90 /* We know that any clones immediately follow FN in the TYPE_METHODS
91 list. */
92 push_to_top_level ();
93 FOR_EACH_CLONE (clone, fn)
95 tree parm;
96 tree clone_parm;
97 int parmno;
98 splay_tree decl_map;
100 /* Update CLONE's source position information to match FN's. */
101 DECL_SOURCE_LOCATION (clone) = DECL_SOURCE_LOCATION (fn);
102 DECL_INLINE (clone) = DECL_INLINE (fn);
103 DECL_DECLARED_INLINE_P (clone) = DECL_DECLARED_INLINE_P (fn);
104 DECL_COMDAT (clone) = DECL_COMDAT (fn);
105 DECL_WEAK (clone) = DECL_WEAK (fn);
106 DECL_ONE_ONLY (clone) = DECL_ONE_ONLY (fn);
107 DECL_SECTION_NAME (clone) = DECL_SECTION_NAME (fn);
108 DECL_USE_TEMPLATE (clone) = DECL_USE_TEMPLATE (fn);
109 DECL_EXTERNAL (clone) = DECL_EXTERNAL (fn);
110 DECL_INTERFACE_KNOWN (clone) = DECL_INTERFACE_KNOWN (fn);
111 DECL_NOT_REALLY_EXTERN (clone) = DECL_NOT_REALLY_EXTERN (fn);
112 TREE_PUBLIC (clone) = TREE_PUBLIC (fn);
113 DECL_VISIBILITY (clone) = DECL_VISIBILITY (fn);
114 DECL_VISIBILITY_SPECIFIED (clone) = DECL_VISIBILITY_SPECIFIED (fn);
116 /* Adjust the parameter names and locations. */
117 parm = DECL_ARGUMENTS (fn);
118 clone_parm = DECL_ARGUMENTS (clone);
119 /* Update the `this' parameter, which is always first. */
120 update_cloned_parm (parm, clone_parm);
121 parm = TREE_CHAIN (parm);
122 clone_parm = TREE_CHAIN (clone_parm);
123 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
124 parm = TREE_CHAIN (parm);
125 if (DECL_HAS_VTT_PARM_P (fn))
126 parm = TREE_CHAIN (parm);
127 if (DECL_HAS_VTT_PARM_P (clone))
128 clone_parm = TREE_CHAIN (clone_parm);
129 for (; parm;
130 parm = TREE_CHAIN (parm), clone_parm = TREE_CHAIN (clone_parm))
131 /* Update this parameter. */
132 update_cloned_parm (parm, clone_parm);
134 /* Start processing the function. */
135 start_preparsed_function (clone, NULL_TREE, SF_PRE_PARSED);
137 /* Remap the parameters. */
138 decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
139 for (parmno = 0,
140 parm = DECL_ARGUMENTS (fn),
141 clone_parm = DECL_ARGUMENTS (clone);
142 parm;
143 ++parmno,
144 parm = TREE_CHAIN (parm))
146 /* Map the in-charge parameter to an appropriate constant. */
147 if (DECL_HAS_IN_CHARGE_PARM_P (fn) && parmno == 1)
149 tree in_charge;
150 in_charge = in_charge_arg_for_name (DECL_NAME (clone));
151 splay_tree_insert (decl_map,
152 (splay_tree_key) parm,
153 (splay_tree_value) in_charge);
155 else if (DECL_ARTIFICIAL (parm)
156 && DECL_NAME (parm) == vtt_parm_identifier)
158 /* For a subobject constructor or destructor, the next
159 argument is the VTT parameter. Remap the VTT_PARM
160 from the CLONE to this parameter. */
161 if (DECL_HAS_VTT_PARM_P (clone))
163 DECL_ABSTRACT_ORIGIN (clone_parm) = parm;
164 splay_tree_insert (decl_map,
165 (splay_tree_key) parm,
166 (splay_tree_value) clone_parm);
167 clone_parm = TREE_CHAIN (clone_parm);
169 /* Otherwise, map the VTT parameter to `NULL'. */
170 else
172 splay_tree_insert (decl_map,
173 (splay_tree_key) parm,
174 (splay_tree_value) null_pointer_node);
177 /* Map other parameters to their equivalents in the cloned
178 function. */
179 else
181 splay_tree_insert (decl_map,
182 (splay_tree_key) parm,
183 (splay_tree_value) clone_parm);
184 clone_parm = TREE_CHAIN (clone_parm);
188 if (targetm.cxx.cdtor_returns_this ())
190 parm = DECL_RESULT (fn);
191 clone_parm = DECL_RESULT (clone);
192 splay_tree_insert (decl_map, (splay_tree_key) parm,
193 (splay_tree_value) clone_parm);
195 /* Clone the body. */
196 clone_body (clone, fn, decl_map);
198 /* Clean up. */
199 splay_tree_delete (decl_map);
201 /* The clone can throw iff the original function can throw. */
202 cp_function_chain->can_throw = !TREE_NOTHROW (fn);
204 /* Now, expand this function into RTL, if appropriate. */
205 finish_function (0);
206 BLOCK_ABSTRACT_ORIGIN (DECL_INITIAL (clone)) = DECL_INITIAL (fn);
207 expand_or_defer_fn (clone);
209 pop_from_top_level ();
211 /* We don't need to process the original function any further. */
212 return 1;