init-regs.c 5.1 KB

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