resolve-sw-modes.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* Mode switching cleanup pass for the EPIPHANY cpu.
  2. Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3. Contributed by Embecosm on behalf of Adapteva, Inc.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "machmode.h"
  20. #include "tm.h"
  21. #include "hard-reg-set.h"
  22. #include "tm_p.h"
  23. #include "vec.h"
  24. #include "sbitmap.h"
  25. #include "predict.h"
  26. #include "hashtab.h"
  27. #include "hash-set.h"
  28. #include "input.h"
  29. #include "function.h"
  30. #include "dominance.h"
  31. #include "cfg.h"
  32. #include "cfgrtl.h"
  33. #include "cfganal.h"
  34. #include "lcm.h"
  35. #include "cfgbuild.h"
  36. #include "cfgcleanup.h"
  37. #include "basic-block.h"
  38. #include "df.h"
  39. #include "rtl.h"
  40. #include "insn-config.h"
  41. #include "insn-codes.h"
  42. #include "emit-rtl.h"
  43. #include "recog.h"
  44. #include "insn-attr-common.h"
  45. #include "tree-pass.h"
  46. namespace {
  47. const pass_data pass_data_resolve_sw_modes =
  48. {
  49. RTL_PASS, /* type */
  50. "resolve_sw_modes", /* name */
  51. OPTGROUP_NONE, /* optinfo_flags */
  52. TV_MODE_SWITCH, /* tv_id */
  53. 0, /* properties_required */
  54. 0, /* properties_provided */
  55. 0, /* properties_destroyed */
  56. 0, /* todo_flags_start */
  57. TODO_df_finish, /* todo_flags_finish */
  58. };
  59. class pass_resolve_sw_modes : public rtl_opt_pass
  60. {
  61. public:
  62. pass_resolve_sw_modes(gcc::context *ctxt)
  63. : rtl_opt_pass(pass_data_resolve_sw_modes, ctxt)
  64. {}
  65. /* opt_pass methods: */
  66. virtual bool gate (function *) { return optimize; }
  67. virtual unsigned int execute (function *);
  68. }; // class pass_resolve_sw_modes
  69. /* Clean-up after mode switching:
  70. Check for mode setting insns that have FP_MODE_ROUND_UNKNOWN.
  71. If only one rounding mode is required, select that one.
  72. Else we have to choose one to use in this mode setting insn and
  73. insert new mode setting insns on the edges where the other mode
  74. becomes unambigous. */
  75. unsigned
  76. pass_resolve_sw_modes::execute (function *fun)
  77. {
  78. basic_block bb;
  79. rtx_insn *insn;
  80. rtx src;
  81. vec<basic_block> todo;
  82. sbitmap pushed;
  83. bool need_commit = false;
  84. bool finalize_fp_sets = (MACHINE_FUNCTION (cfun)->unknown_mode_sets == 0);
  85. todo.create (last_basic_block_for_fn (fun));
  86. pushed = sbitmap_alloc (last_basic_block_for_fn (fun));
  87. bitmap_clear (pushed);
  88. if (!finalize_fp_sets)
  89. {
  90. df_note_add_problem ();
  91. df_analyze ();
  92. }
  93. FOR_EACH_BB_FN (bb, fun)
  94. FOR_BB_INSNS (bb, insn)
  95. {
  96. enum attr_fp_mode selected_mode;
  97. if (!NONJUMP_INSN_P (insn)
  98. || recog_memoized (insn) != CODE_FOR_set_fp_mode)
  99. continue;
  100. src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
  101. if (finalize_fp_sets)
  102. {
  103. SET_SRC (XVECEXP (PATTERN (insn), 0, 2)) = copy_rtx (src);
  104. if (REG_P (src))
  105. df_insn_rescan (insn);
  106. continue;
  107. }
  108. if (REG_P (src)
  109. || XINT (XVECEXP (XEXP (src, 0), 0, 0), 0) != FP_MODE_ROUND_UNKNOWN)
  110. continue;
  111. if (find_regno_note (insn, REG_UNUSED, FP_TRUNCATE_REGNUM))
  112. selected_mode = FP_MODE_ROUND_NEAREST;
  113. else if (find_regno_note (insn, REG_UNUSED, FP_NEAREST_REGNUM))
  114. selected_mode = FP_MODE_ROUND_TRUNC;
  115. else
  116. {
  117. /* We could get more fancy in the selection of the mode by
  118. checking the total frequency of the affected edges. */
  119. selected_mode = (enum attr_fp_mode) epiphany_normal_fp_rounding;
  120. todo.quick_push (bb);
  121. bitmap_set_bit (pushed, bb->index);
  122. }
  123. XVECEXP (XEXP (src, 0), 0, 0) = GEN_INT (selected_mode);
  124. SET_SRC (XVECEXP (PATTERN (insn), 0, 1)) = copy_rtx (src);
  125. SET_SRC (XVECEXP (PATTERN (insn), 0, 2)) = copy_rtx (src);
  126. df_insn_rescan (insn);
  127. }
  128. while (todo.length ())
  129. {
  130. basic_block bb = todo.pop ();
  131. int selected_reg, jilted_reg;
  132. enum attr_fp_mode jilted_mode;
  133. edge e;
  134. edge_iterator ei;
  135. bitmap_set_bit (pushed, bb->index);
  136. bitmap_set_bit (pushed, bb->index);
  137. if (epiphany_normal_fp_rounding == FP_MODE_ROUND_NEAREST)
  138. {
  139. selected_reg = FP_NEAREST_REGNUM;
  140. jilted_reg = FP_TRUNCATE_REGNUM;
  141. jilted_mode = FP_MODE_ROUND_TRUNC;
  142. }
  143. else
  144. {
  145. selected_reg = FP_TRUNCATE_REGNUM;
  146. jilted_reg = FP_NEAREST_REGNUM;
  147. jilted_mode = FP_MODE_ROUND_NEAREST;
  148. }
  149. FOR_EACH_EDGE (e, ei, bb->succs)
  150. {
  151. basic_block succ = e->dest;
  152. rtx_insn *seq;
  153. if (!REGNO_REG_SET_P (DF_LIVE_IN (succ), jilted_reg))
  154. continue;
  155. if (REGNO_REG_SET_P (DF_LIVE_IN (succ), selected_reg))
  156. {
  157. if (bitmap_bit_p (pushed, succ->index))
  158. continue;
  159. todo.quick_push (succ);
  160. bitmap_set_bit (pushed, bb->index);
  161. continue;
  162. }
  163. start_sequence ();
  164. emit_set_fp_mode (EPIPHANY_MSW_ENTITY_ROUND_UNKNOWN,
  165. jilted_mode, FP_MODE_NONE, NULL);
  166. seq = get_insns ();
  167. end_sequence ();
  168. need_commit = true;
  169. insert_insn_on_edge (seq, e);
  170. }
  171. }
  172. todo.release ();
  173. sbitmap_free (pushed);
  174. if (need_commit)
  175. commit_edge_insertions ();
  176. return 0;
  177. }
  178. } // anon namespace
  179. rtl_opt_pass *
  180. make_pass_resolve_sw_modes (gcc::context *ctxt)
  181. {
  182. return new pass_resolve_sw_modes (ctxt);
  183. }