gcc/
[official-gcc.git] / gcc / init-regs.c
blob5d7d4cc2e02f9a38088a3fe44cd1ab1a389ea078
1 /* Initialization of uninitialized regs.
2 Copyright (C) 2007-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "alias.h"
25 #include "symtab.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "function.h"
31 #include "flags.h"
32 #include "insn-config.h"
33 #include "expmed.h"
34 #include "dojump.h"
35 #include "explow.h"
36 #include "calls.h"
37 #include "emit-rtl.h"
38 #include "varasm.h"
39 #include "stmt.h"
40 #include "expr.h"
41 #include "tree-pass.h"
42 #include "predict.h"
43 #include "dominance.h"
44 #include "cfg.h"
45 #include "basic-block.h"
46 #include "df.h"
48 /* Check all of the uses of pseudo variables. If any use that is MUST
49 uninitialized, add a store of 0 immediately before it. For
50 subregs, this makes combine happy. For full word regs, this makes
51 other optimizations, like the register allocator and the reg-stack
52 happy as well as papers over some problems on the arm and other
53 processors where certain isa constraints cannot be handled by gcc.
54 These are of the form where two operands to an insn my not be the
55 same. The ra will only make them the same if they do not
56 interfere, and this can only happen if one is not initialized.
58 There is also the unfortunate consequence that this may mask some
59 buggy programs where people forget to initialize stack variable.
60 Any programmer with half a brain would look at the uninitialized
61 variable warnings. */
63 static void
64 initialize_uninitialized_regs (void)
66 basic_block bb;
67 bitmap already_genned = BITMAP_ALLOC (NULL);
69 if (optimize == 1)
71 df_live_add_problem ();
72 df_live_set_all_dirty ();
75 df_analyze ();
77 FOR_EACH_BB_FN (bb, cfun)
79 rtx_insn *insn;
80 bitmap lr = DF_LR_IN (bb);
81 bitmap ur = DF_LIVE_IN (bb);
82 bitmap_clear (already_genned);
84 FOR_BB_INSNS (bb, insn)
86 df_ref use;
87 if (!NONDEBUG_INSN_P (insn))
88 continue;
90 FOR_EACH_INSN_USE (use, insn)
92 unsigned int regno = DF_REF_REGNO (use);
94 /* Only do this for the pseudos. */
95 if (regno < FIRST_PSEUDO_REGISTER)
96 continue;
98 /* Ignore pseudo PIC register. */
99 if (pic_offset_table_rtx
100 && regno == REGNO (pic_offset_table_rtx))
101 continue;
103 /* Do not generate multiple moves for the same regno.
104 This is common for sequences of subreg operations.
105 They would be deleted during combine but there is no
106 reason to churn the system. */
107 if (bitmap_bit_p (already_genned, regno))
108 continue;
110 /* A use is MUST uninitialized if it reaches the top of
111 the block from the inside of the block (the lr test)
112 and no def for it reaches the top of the block from
113 outside of the block (the ur test). */
114 if (bitmap_bit_p (lr, regno)
115 && (!bitmap_bit_p (ur, regno)))
117 rtx_insn *move_insn;
118 rtx reg = DF_REF_REAL_REG (use);
120 bitmap_set_bit (already_genned, regno);
122 start_sequence ();
123 emit_move_insn (reg, CONST0_RTX (GET_MODE (reg)));
124 move_insn = get_insns ();
125 end_sequence ();
126 emit_insn_before (move_insn, insn);
127 if (dump_file)
128 fprintf (dump_file,
129 "adding initialization in %s of reg %d at in block %d for insn %d.\n",
130 current_function_name (), regno, bb->index,
131 INSN_UID (insn));
137 if (optimize == 1)
139 if (dump_file)
140 df_dump (dump_file);
141 df_remove_problem (df_live);
144 BITMAP_FREE (already_genned);
147 namespace {
149 const pass_data pass_data_initialize_regs =
151 RTL_PASS, /* type */
152 "init-regs", /* name */
153 OPTGROUP_NONE, /* optinfo_flags */
154 TV_NONE, /* tv_id */
155 0, /* properties_required */
156 0, /* properties_provided */
157 0, /* properties_destroyed */
158 0, /* todo_flags_start */
159 TODO_df_finish, /* todo_flags_finish */
162 class pass_initialize_regs : public rtl_opt_pass
164 public:
165 pass_initialize_regs (gcc::context *ctxt)
166 : rtl_opt_pass (pass_data_initialize_regs, ctxt)
169 /* opt_pass methods: */
170 virtual bool gate (function *) { return optimize > 0; }
171 virtual unsigned int execute (function *)
173 initialize_uninitialized_regs ();
174 return 0;
177 }; // class pass_initialize_regs
179 } // anon namespace
181 rtl_opt_pass *
182 make_pass_initialize_regs (gcc::context *ctxt)
184 return new pass_initialize_regs (ctxt);