OSDN Git Service

2007-07-06 H.J. Lu <hongjiu.lu@intel.com>
[pf3gnuchains/gcc-fork.git] / gcc / init-regs.c
1 /* Initialization of uninitialized regs. 
2    Copyright (C) 2007 Free Software Foundation,
3    Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "regs.h"
29 #include "expr.h"
30 #include "tree-pass.h"
31 #include "basic-block.h"
32 #include "flags.h"
33 #include "df.h"
34
35 /* Check all of the uses of pseudo variables.  If any use that is MUST
36    uninitialized, add a store of 0 immediately before it.  For
37    subregs, this makes combine happy.  For full word regs, this makes
38    other optimizations, like the register allocator and the reg-stack
39    happy as well as papers over some problems on the arm and other
40    processors where certain isa constraints cannot be handled by gcc.
41    These are of the form where two operands to an insn my not be the
42    same.  The ra will only make them the same if they do not
43    interfere, and this can only happen if one is not initialized.
44
45    There is also the unfortunate consequence that this may mask some
46    buggy programs where people forget to initialize stack variable.
47    Any programmer with half a brain would look at the uninitialized
48    variable warnings.  */
49
50 static void
51 initialize_uninitialized_regs (void)
52 {
53   basic_block bb;
54   bitmap already_genned = BITMAP_ALLOC (NULL);
55
56   if (optimize == 1)
57     {
58       df_live_add_problem ();
59       df_live_set_all_dirty ();
60     }
61
62   df_analyze ();
63
64   FOR_EACH_BB (bb)
65     {
66       rtx insn;
67       bitmap lr = DF_LR_IN (bb);
68       bitmap ur = DF_LIVE_IN (bb);
69       bitmap_clear (already_genned);
70
71       FOR_BB_INSNS (bb, insn)
72         {
73           unsigned int uid = INSN_UID (insn);
74           struct df_ref **use_rec;
75           if (!INSN_P (insn))
76             continue;
77
78           for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
79             {
80               struct df_ref *use = *use_rec;
81               unsigned int regno = DF_REF_REGNO (use);
82
83               /* Only do this for the pseudos.  */
84               if (regno < FIRST_PSEUDO_REGISTER)
85                 continue;
86
87               /* Do not generate multiple moves for the same regno.
88                  This is common for sequences of subreg operations.
89                  They would be deleted during combine but there is no
90                  reason to churn the system.  */
91               if (bitmap_bit_p (already_genned, regno))
92                 continue;
93
94               /* A use is MUST uninitialized if it reaches the top of
95                  the block from the inside of the block (the lr test)
96                  and no def for it reaches the top of the block from
97                  outside of the block (the ur test).  */
98               if (bitmap_bit_p (lr, regno)
99                   && (!bitmap_bit_p (ur, regno)))
100                 {
101                   rtx move_insn;
102                   rtx reg = DF_REF_REAL_REG (use);
103
104                   bitmap_set_bit (already_genned, regno); 
105
106                   start_sequence ();
107                   emit_move_insn (reg, CONST0_RTX (GET_MODE (reg)));
108                   move_insn = get_insns ();
109                   end_sequence ();
110                   add_insn_before (move_insn, insn, bb);
111                   if (dump_file)
112                     fprintf (dump_file, 
113                              "adding initialization in %s of reg %d at in block %d for insn %d.\n", 
114                              current_function_name (), regno, bb->index, uid);
115                 }
116             }
117         }
118     }
119
120   if (optimize == 1)
121     df_remove_problem (df_live);
122
123   BITMAP_FREE (already_genned);
124 }
125
126 static bool
127 gate_initialize_regs (void)
128 {
129   return optimize > 0;
130 }
131
132 static unsigned int
133 rest_of_handle_initialize_regs (void)
134 {
135   no_new_pseudos = 0;
136   initialize_uninitialized_regs ();
137   no_new_pseudos = 1;
138   return 0;
139 }
140
141 struct tree_opt_pass pass_initialize_regs =
142 {
143   "init-regs",                          /* name */
144   gate_initialize_regs,                 /* gate */
145   rest_of_handle_initialize_regs,       /* execute */
146   NULL,                                 /* sub */
147   NULL,                                 /* next */
148   0,                                    /* static_pass_number */
149   0,                                    /* tv_id */
150   0,                                    /* properties_required */
151   0,                                    /* properties_provided */
152   0,                                    /* properties_destroyed */
153   0,                                    /* todo_flags_start */
154   TODO_dump_func |
155   TODO_df_finish,                       /* todo_flags_finish */
156   0                                     /* letter */
157 };