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