OSDN Git Service

Give credit, where credit is due.
[pf3gnuchains/gcc-fork.git] / gcc / integrate.c
1 /* Procedure integration for GCC.
2    Copyright (C) 1988, 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4    Contributed by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
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.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27
28 #include "rtl.h"
29 #include "tree.h"
30 #include "tm_p.h"
31 #include "regs.h"
32 #include "flags.h"
33 #include "debug.h"
34 #include "insn-config.h"
35 #include "expr.h"
36 #include "output.h"
37 #include "recog.h"
38 #include "integrate.h"
39 #include "real.h"
40 #include "except.h"
41 #include "function.h"
42 #include "toplev.h"
43 #include "intl.h"
44 #include "params.h"
45 #include "ggc.h"
46 #include "target.h"
47 #include "langhooks.h"
48
49 /* Round to the next highest integer that meets the alignment.  */
50 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
51 \f
52
53 /* Private type used by {get/has}_hard_reg_initial_val.  */
54 typedef struct initial_value_pair GTY(()) {
55   rtx hard_reg;
56   rtx pseudo;
57 } initial_value_pair;
58 typedef struct initial_value_struct GTY(()) {
59   int num_entries;
60   int max_entries;
61   initial_value_pair * GTY ((length ("%h.num_entries"))) entries;
62 } initial_value_struct;
63
64 static void set_block_origin_self (tree);
65 static void set_block_abstract_flags (tree, int);
66 \f
67
68 /* Return false if the function FNDECL cannot be inlined on account of its
69    attributes, true otherwise.  */
70 bool
71 function_attribute_inlinable_p (tree fndecl)
72 {
73   if (targetm.attribute_table)
74     {
75       tree a;
76
77       for (a = DECL_ATTRIBUTES (fndecl); a; a = TREE_CHAIN (a))
78         {
79           tree name = TREE_PURPOSE (a);
80           int i;
81
82           for (i = 0; targetm.attribute_table[i].name != NULL; i++)
83             if (is_attribute_p (targetm.attribute_table[i].name, name))
84               return targetm.function_attribute_inlinable_p (fndecl);
85         }
86     }
87
88   return true;
89 }
90 \f
91 /* Copy NODE (which must be a DECL).  The DECL originally was in the FROM_FN,
92    but now it will be in the TO_FN.  */
93
94 tree
95 copy_decl_for_inlining (tree decl, tree from_fn, tree to_fn)
96 {
97   tree copy;
98
99   /* Copy the declaration.  */
100   if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
101     {
102       tree type = TREE_TYPE (decl);
103
104       /* For a parameter or result, we must make an equivalent VAR_DECL, not a
105          new PARM_DECL.  */
106       copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
107       TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
108       TREE_READONLY (copy) = TREE_READONLY (decl);
109       TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
110     }
111   else
112     {
113       copy = copy_node (decl);
114       /* The COPY is not abstract; it will be generated in TO_FN.  */
115       DECL_ABSTRACT (copy) = 0;
116       lang_hooks.dup_lang_specific_decl (copy);
117
118       /* TREE_ADDRESSABLE isn't used to indicate that a label's
119          address has been taken; it's for internal bookkeeping in
120          expand_goto_internal.  */
121       if (TREE_CODE (copy) == LABEL_DECL)
122         {
123           TREE_ADDRESSABLE (copy) = 0;
124           LABEL_DECL_UID (copy) = -1;
125         }
126     }
127
128   /* Don't generate debug information for the copy if we wouldn't have
129      generated it for the copy either.  */
130   DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
131   DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
132
133   /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
134      declaration inspired this copy.  */
135   DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
136
137   /* The new variable/label has no RTL, yet.  */
138   if (!TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
139     SET_DECL_RTL (copy, NULL_RTX);
140
141   /* These args would always appear unused, if not for this.  */
142   TREE_USED (copy) = 1;
143
144   /* Set the context for the new declaration.  */
145   if (!DECL_CONTEXT (decl))
146     /* Globals stay global.  */
147     ;
148   else if (DECL_CONTEXT (decl) != from_fn)
149     /* Things that weren't in the scope of the function we're inlining
150        from aren't in the scope we're inlining to, either.  */
151     ;
152   else if (TREE_STATIC (decl))
153     /* Function-scoped static variables should stay in the original
154        function.  */
155     ;
156   else
157     /* Ordinary automatic local variables are now in the scope of the
158        new function.  */
159     DECL_CONTEXT (copy) = to_fn;
160
161   return copy;
162 }
163
164 \f
165 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
166    given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
167    that it points to the node itself, thus indicating that the node is its
168    own (abstract) origin.  Additionally, if the BLOCK_ABSTRACT_ORIGIN for
169    the given node is NULL, recursively descend the decl/block tree which
170    it is the root of, and for each other ..._DECL or BLOCK node contained
171    therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
172    still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
173    values to point to themselves.  */
174
175 static void
176 set_block_origin_self (tree stmt)
177 {
178   if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
179     {
180       BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
181
182       {
183         tree local_decl;
184
185         for (local_decl = BLOCK_VARS (stmt);
186              local_decl != NULL_TREE;
187              local_decl = TREE_CHAIN (local_decl))
188           set_decl_origin_self (local_decl);    /* Potential recursion.  */
189       }
190
191       {
192         tree subblock;
193
194         for (subblock = BLOCK_SUBBLOCKS (stmt);
195              subblock != NULL_TREE;
196              subblock = BLOCK_CHAIN (subblock))
197           set_block_origin_self (subblock);     /* Recurse.  */
198       }
199     }
200 }
201
202 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
203    the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
204    node to so that it points to the node itself, thus indicating that the
205    node represents its own (abstract) origin.  Additionally, if the
206    DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
207    the decl/block tree of which the given node is the root of, and for
208    each other ..._DECL or BLOCK node contained therein whose
209    DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
210    set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
211    point to themselves.  */
212
213 void
214 set_decl_origin_self (tree decl)
215 {
216   if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
217     {
218       DECL_ABSTRACT_ORIGIN (decl) = decl;
219       if (TREE_CODE (decl) == FUNCTION_DECL)
220         {
221           tree arg;
222
223           for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
224             DECL_ABSTRACT_ORIGIN (arg) = arg;
225           if (DECL_INITIAL (decl) != NULL_TREE
226               && DECL_INITIAL (decl) != error_mark_node)
227             set_block_origin_self (DECL_INITIAL (decl));
228         }
229     }
230 }
231 \f
232 /* Given a pointer to some BLOCK node, and a boolean value to set the
233    "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
234    the given block, and for all local decls and all local sub-blocks
235    (recursively) which are contained therein.  */
236
237 static void
238 set_block_abstract_flags (tree stmt, int setting)
239 {
240   tree local_decl;
241   tree subblock;
242
243   BLOCK_ABSTRACT (stmt) = setting;
244
245   for (local_decl = BLOCK_VARS (stmt);
246        local_decl != NULL_TREE;
247        local_decl = TREE_CHAIN (local_decl))
248     set_decl_abstract_flags (local_decl, setting);
249
250   for (subblock = BLOCK_SUBBLOCKS (stmt);
251        subblock != NULL_TREE;
252        subblock = BLOCK_CHAIN (subblock))
253     set_block_abstract_flags (subblock, setting);
254 }
255
256 /* Given a pointer to some ..._DECL node, and a boolean value to set the
257    "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
258    given decl, and (in the case where the decl is a FUNCTION_DECL) also
259    set the abstract flags for all of the parameters, local vars, local
260    blocks and sub-blocks (recursively) to the same setting.  */
261
262 void
263 set_decl_abstract_flags (tree decl, int setting)
264 {
265   DECL_ABSTRACT (decl) = setting;
266   if (TREE_CODE (decl) == FUNCTION_DECL)
267     {
268       tree arg;
269
270       for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
271         DECL_ABSTRACT (arg) = setting;
272       if (DECL_INITIAL (decl) != NULL_TREE
273           && DECL_INITIAL (decl) != error_mark_node)
274         set_block_abstract_flags (DECL_INITIAL (decl), setting);
275     }
276 }
277 \f
278 /* Functions to keep track of the values hard regs had at the start of
279    the function.  */
280
281 rtx
282 get_hard_reg_initial_reg (struct function *fun, rtx reg)
283 {
284   struct initial_value_struct *ivs = fun->hard_reg_initial_vals;
285   int i;
286
287   if (ivs == 0)
288     return NULL_RTX;
289
290   for (i = 0; i < ivs->num_entries; i++)
291     if (rtx_equal_p (ivs->entries[i].pseudo, reg))
292       return ivs->entries[i].hard_reg;
293
294   return NULL_RTX;
295 }
296
297 /* Make sure that there's a pseudo register of mode MODE that stores the
298    initial value of hard register REGNO.  Return an rtx for such a pseudo.  */
299
300 rtx
301 get_hard_reg_initial_val (enum machine_mode mode, unsigned int regno)
302 {
303   struct initial_value_struct *ivs;
304   rtx rv;
305
306   rv = has_hard_reg_initial_val (mode, regno);
307   if (rv)
308     return rv;
309
310   ivs = cfun->hard_reg_initial_vals;
311   if (ivs == 0)
312     {
313       ivs = ggc_alloc (sizeof (initial_value_struct));
314       ivs->num_entries = 0;
315       ivs->max_entries = 5;
316       ivs->entries = ggc_alloc (5 * sizeof (initial_value_pair));
317       cfun->hard_reg_initial_vals = ivs;
318     }
319
320   if (ivs->num_entries >= ivs->max_entries)
321     {
322       ivs->max_entries += 5;
323       ivs->entries = ggc_realloc (ivs->entries,
324                                   ivs->max_entries
325                                   * sizeof (initial_value_pair));
326     }
327
328   ivs->entries[ivs->num_entries].hard_reg = gen_rtx_REG (mode, regno);
329   ivs->entries[ivs->num_entries].pseudo = gen_reg_rtx (mode);
330
331   return ivs->entries[ivs->num_entries++].pseudo;
332 }
333
334 /* See if get_hard_reg_initial_val has been used to create a pseudo
335    for the initial value of hard register REGNO in mode MODE.  Return
336    the associated pseudo if so, otherwise return NULL.  */
337
338 rtx
339 has_hard_reg_initial_val (enum machine_mode mode, unsigned int regno)
340 {
341   struct initial_value_struct *ivs;
342   int i;
343
344   ivs = cfun->hard_reg_initial_vals;
345   if (ivs != 0)
346     for (i = 0; i < ivs->num_entries; i++)
347       if (GET_MODE (ivs->entries[i].hard_reg) == mode
348           && REGNO (ivs->entries[i].hard_reg) == regno)
349         return ivs->entries[i].pseudo;
350
351   return NULL_RTX;
352 }
353
354 void
355 emit_initial_value_sets (void)
356 {
357   struct initial_value_struct *ivs = cfun->hard_reg_initial_vals;
358   int i;
359   rtx seq;
360
361   if (ivs == 0)
362     return;
363
364   start_sequence ();
365   for (i = 0; i < ivs->num_entries; i++)
366     emit_move_insn (ivs->entries[i].pseudo, ivs->entries[i].hard_reg);
367   seq = get_insns ();
368   end_sequence ();
369
370   emit_insn_after (seq, entry_of_function ());
371 }
372
373 /* If the backend knows where to allocate pseudos for hard
374    register initial values, register these allocations now.  */
375 void
376 allocate_initial_values (rtx *reg_equiv_memory_loc ATTRIBUTE_UNUSED)
377 {
378 #ifdef ALLOCATE_INITIAL_VALUE
379   struct initial_value_struct *ivs = cfun->hard_reg_initial_vals;
380   int i;
381
382   if (ivs == 0)
383     return;
384
385   for (i = 0; i < ivs->num_entries; i++)
386     {
387       int regno = REGNO (ivs->entries[i].pseudo);
388       rtx x = ALLOCATE_INITIAL_VALUE (ivs->entries[i].hard_reg);
389
390       if (x && REG_N_SETS (REGNO (ivs->entries[i].pseudo)) <= 1)
391         {
392           if (MEM_P (x))
393             reg_equiv_memory_loc[regno] = x;
394           else
395             {
396               basic_block bb;
397               int new_regno;
398
399               gcc_assert (REG_P (x));
400               new_regno = REGNO (x);
401               reg_renumber[regno] = new_regno;
402               /* Poke the regno right into regno_reg_rtx so that even
403                  fixed regs are accepted.  */
404               REGNO (ivs->entries[i].pseudo) = new_regno;
405               /* Update global register liveness information.  */
406               FOR_EACH_BB (bb)
407                 {
408                   if (REGNO_REG_SET_P(bb->global_live_at_start, regno))
409                     SET_REGNO_REG_SET (bb->global_live_at_start, new_regno);
410                   if (REGNO_REG_SET_P(bb->global_live_at_end, regno))
411                     SET_REGNO_REG_SET (bb->global_live_at_end, new_regno);
412                 }
413             }
414         }
415     }
416 #endif
417 }
418
419 #include "gt-integrate.h"