mode-switching.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /* CPU mode switching
  2. Copyright (C) 1998-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 "target.h"
  20. #include "rtl.h"
  21. #include "regs.h"
  22. #include "hard-reg-set.h"
  23. #include "flags.h"
  24. #include "insn-config.h"
  25. #include "recog.h"
  26. #include "predict.h"
  27. #include "vec.h"
  28. #include "hashtab.h"
  29. #include "hash-set.h"
  30. #include "machmode.h"
  31. #include "input.h"
  32. #include "function.h"
  33. #include "dominance.h"
  34. #include "cfg.h"
  35. #include "cfgrtl.h"
  36. #include "cfganal.h"
  37. #include "lcm.h"
  38. #include "cfgcleanup.h"
  39. #include "basic-block.h"
  40. #include "tm_p.h"
  41. #include "tree-pass.h"
  42. #include "df.h"
  43. #include "emit-rtl.h"
  44. /* We want target macros for the mode switching code to be able to refer
  45. to instruction attribute values. */
  46. #include "insn-attr.h"
  47. #ifdef OPTIMIZE_MODE_SWITCHING
  48. /* The algorithm for setting the modes consists of scanning the insn list
  49. and finding all the insns which require a specific mode. Each insn gets
  50. a unique struct seginfo element. These structures are inserted into a list
  51. for each basic block. For each entity, there is an array of bb_info over
  52. the flow graph basic blocks (local var 'bb_info'), which contains a list
  53. of all insns within that basic block, in the order they are encountered.
  54. For each entity, any basic block WITHOUT any insns requiring a specific
  55. mode are given a single entry without a mode (each basic block in the
  56. flow graph must have at least one entry in the segment table).
  57. The LCM algorithm is then run over the flow graph to determine where to
  58. place the sets to the highest-priority mode with respect to the first
  59. insn in any one block. Any adjustments required to the transparency
  60. vectors are made, then the next iteration starts for the next-lower
  61. priority mode, till for each entity all modes are exhausted.
  62. More details can be found in the code of optimize_mode_switching. */
  63. /* This structure contains the information for each insn which requires
  64. either single or double mode to be set.
  65. MODE is the mode this insn must be executed in.
  66. INSN_PTR is the insn to be executed (may be the note that marks the
  67. beginning of a basic block).
  68. BBNUM is the flow graph basic block this insn occurs in.
  69. NEXT is the next insn in the same basic block. */
  70. struct seginfo
  71. {
  72. int mode;
  73. rtx_insn *insn_ptr;
  74. int bbnum;
  75. struct seginfo *next;
  76. HARD_REG_SET regs_live;
  77. };
  78. struct bb_info
  79. {
  80. struct seginfo *seginfo;
  81. int computing;
  82. int mode_out;
  83. int mode_in;
  84. };
  85. static struct seginfo * new_seginfo (int, rtx_insn *, int, HARD_REG_SET);
  86. static void add_seginfo (struct bb_info *, struct seginfo *);
  87. static void reg_dies (rtx, HARD_REG_SET *);
  88. static void reg_becomes_live (rtx, const_rtx, void *);
  89. /* Clear ode I from entity J in bitmap B. */
  90. #define clear_mode_bit(b, j, i) \
  91. bitmap_clear_bit (b, (j * max_num_modes) + i)
  92. /* Test mode I from entity J in bitmap B. */
  93. #define mode_bit_p(b, j, i) \
  94. bitmap_bit_p (b, (j * max_num_modes) + i)
  95. /* Set mode I from entity J in bitmal B. */
  96. #define set_mode_bit(b, j, i) \
  97. bitmap_set_bit (b, (j * max_num_modes) + i)
  98. /* Emit modes segments from EDGE_LIST associated with entity E.
  99. INFO gives mode availability for each mode. */
  100. static bool
  101. commit_mode_sets (struct edge_list *edge_list, int e, struct bb_info *info)
  102. {
  103. bool need_commit = false;
  104. for (int ed = NUM_EDGES (edge_list) - 1; ed >= 0; ed--)
  105. {
  106. edge eg = INDEX_EDGE (edge_list, ed);
  107. int mode;
  108. if ((mode = (int)(intptr_t)(eg->aux)) != -1)
  109. {
  110. HARD_REG_SET live_at_edge;
  111. basic_block src_bb = eg->src;
  112. int cur_mode = info[src_bb->index].mode_out;
  113. rtx mode_set;
  114. REG_SET_TO_HARD_REG_SET (live_at_edge, df_get_live_out (src_bb));
  115. rtl_profile_for_edge (eg);
  116. start_sequence ();
  117. targetm.mode_switching.emit (e, mode, cur_mode, live_at_edge);
  118. mode_set = get_insns ();
  119. end_sequence ();
  120. default_rtl_profile ();
  121. /* Do not bother to insert empty sequence. */
  122. if (mode_set == NULL_RTX)
  123. continue;
  124. /* We should not get an abnormal edge here. */
  125. gcc_assert (! (eg->flags & EDGE_ABNORMAL));
  126. need_commit = true;
  127. insert_insn_on_edge (mode_set, eg);
  128. }
  129. }
  130. return need_commit;
  131. }
  132. /* Allocate a new BBINFO structure, initialized with the MODE, INSN,
  133. and basic block BB parameters.
  134. INSN may not be a NOTE_INSN_BASIC_BLOCK, unless it is an empty
  135. basic block; that allows us later to insert instructions in a FIFO-like
  136. manner. */
  137. static struct seginfo *
  138. new_seginfo (int mode, rtx_insn *insn, int bb, HARD_REG_SET regs_live)
  139. {
  140. struct seginfo *ptr;
  141. gcc_assert (!NOTE_INSN_BASIC_BLOCK_P (insn)
  142. || insn == BB_END (NOTE_BASIC_BLOCK (insn)));
  143. ptr = XNEW (struct seginfo);
  144. ptr->mode = mode;
  145. ptr->insn_ptr = insn;
  146. ptr->bbnum = bb;
  147. ptr->next = NULL;
  148. COPY_HARD_REG_SET (ptr->regs_live, regs_live);
  149. return ptr;
  150. }
  151. /* Add a seginfo element to the end of a list.
  152. HEAD is a pointer to the list beginning.
  153. INFO is the structure to be linked in. */
  154. static void
  155. add_seginfo (struct bb_info *head, struct seginfo *info)
  156. {
  157. struct seginfo *ptr;
  158. if (head->seginfo == NULL)
  159. head->seginfo = info;
  160. else
  161. {
  162. ptr = head->seginfo;
  163. while (ptr->next != NULL)
  164. ptr = ptr->next;
  165. ptr->next = info;
  166. }
  167. }
  168. /* Record in LIVE that register REG died. */
  169. static void
  170. reg_dies (rtx reg, HARD_REG_SET *live)
  171. {
  172. int regno;
  173. if (!REG_P (reg))
  174. return;
  175. regno = REGNO (reg);
  176. if (regno < FIRST_PSEUDO_REGISTER)
  177. remove_from_hard_reg_set (live, GET_MODE (reg), regno);
  178. }
  179. /* Record in LIVE that register REG became live.
  180. This is called via note_stores. */
  181. static void
  182. reg_becomes_live (rtx reg, const_rtx setter ATTRIBUTE_UNUSED, void *live)
  183. {
  184. int regno;
  185. if (GET_CODE (reg) == SUBREG)
  186. reg = SUBREG_REG (reg);
  187. if (!REG_P (reg))
  188. return;
  189. regno = REGNO (reg);
  190. if (regno < FIRST_PSEUDO_REGISTER)
  191. add_to_hard_reg_set ((HARD_REG_SET *) live, GET_MODE (reg), regno);
  192. }
  193. /* Split the fallthrough edge to the exit block, so that we can note
  194. that there NORMAL_MODE is required. Return the new block if it's
  195. inserted before the exit block. Otherwise return null. */
  196. static basic_block
  197. create_pre_exit (int n_entities, int *entity_map, const int *num_modes)
  198. {
  199. edge eg;
  200. edge_iterator ei;
  201. basic_block pre_exit;
  202. /* The only non-call predecessor at this stage is a block with a
  203. fallthrough edge; there can be at most one, but there could be
  204. none at all, e.g. when exit is called. */
  205. pre_exit = 0;
  206. FOR_EACH_EDGE (eg, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
  207. if (eg->flags & EDGE_FALLTHRU)
  208. {
  209. basic_block src_bb = eg->src;
  210. rtx_insn *last_insn;
  211. rtx ret_reg;
  212. gcc_assert (!pre_exit);
  213. /* If this function returns a value at the end, we have to
  214. insert the final mode switch before the return value copy
  215. to its hard register. */
  216. if (EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds) == 1
  217. && NONJUMP_INSN_P ((last_insn = BB_END (src_bb)))
  218. && GET_CODE (PATTERN (last_insn)) == USE
  219. && GET_CODE ((ret_reg = XEXP (PATTERN (last_insn), 0))) == REG)
  220. {
  221. int ret_start = REGNO (ret_reg);
  222. int nregs = hard_regno_nregs[ret_start][GET_MODE (ret_reg)];
  223. int ret_end = ret_start + nregs;
  224. bool short_block = false;
  225. bool multi_reg_return = false;
  226. bool forced_late_switch = false;
  227. rtx_insn *before_return_copy;
  228. do
  229. {
  230. rtx_insn *return_copy = PREV_INSN (last_insn);
  231. rtx return_copy_pat, copy_reg;
  232. int copy_start, copy_num;
  233. int j;
  234. if (NONDEBUG_INSN_P (return_copy))
  235. {
  236. /* When using SJLJ exceptions, the call to the
  237. unregister function is inserted between the
  238. clobber of the return value and the copy.
  239. We do not want to split the block before this
  240. or any other call; if we have not found the
  241. copy yet, the copy must have been deleted. */
  242. if (CALL_P (return_copy))
  243. {
  244. short_block = true;
  245. break;
  246. }
  247. return_copy_pat = PATTERN (return_copy);
  248. switch (GET_CODE (return_copy_pat))
  249. {
  250. case USE:
  251. /* Skip USEs of multiple return registers.
  252. __builtin_apply pattern is also handled here. */
  253. if (GET_CODE (XEXP (return_copy_pat, 0)) == REG
  254. && (targetm.calls.function_value_regno_p
  255. (REGNO (XEXP (return_copy_pat, 0)))))
  256. {
  257. multi_reg_return = true;
  258. last_insn = return_copy;
  259. continue;
  260. }
  261. break;
  262. case ASM_OPERANDS:
  263. /* Skip barrier insns. */
  264. if (!MEM_VOLATILE_P (return_copy_pat))
  265. break;
  266. /* Fall through. */
  267. case ASM_INPUT:
  268. case UNSPEC_VOLATILE:
  269. last_insn = return_copy;
  270. continue;
  271. default:
  272. break;
  273. }
  274. /* If the return register is not (in its entirety)
  275. likely spilled, the return copy might be
  276. partially or completely optimized away. */
  277. return_copy_pat = single_set (return_copy);
  278. if (!return_copy_pat)
  279. {
  280. return_copy_pat = PATTERN (return_copy);
  281. if (GET_CODE (return_copy_pat) != CLOBBER)
  282. break;
  283. else if (!optimize)
  284. {
  285. /* This might be (clobber (reg [<result>]))
  286. when not optimizing. Then check if
  287. the previous insn is the clobber for
  288. the return register. */
  289. copy_reg = SET_DEST (return_copy_pat);
  290. if (GET_CODE (copy_reg) == REG
  291. && !HARD_REGISTER_NUM_P (REGNO (copy_reg)))
  292. {
  293. if (INSN_P (PREV_INSN (return_copy)))
  294. {
  295. return_copy = PREV_INSN (return_copy);
  296. return_copy_pat = PATTERN (return_copy);
  297. if (GET_CODE (return_copy_pat) != CLOBBER)
  298. break;
  299. }
  300. }
  301. }
  302. }
  303. copy_reg = SET_DEST (return_copy_pat);
  304. if (GET_CODE (copy_reg) == REG)
  305. copy_start = REGNO (copy_reg);
  306. else if (GET_CODE (copy_reg) == SUBREG
  307. && GET_CODE (SUBREG_REG (copy_reg)) == REG)
  308. copy_start = REGNO (SUBREG_REG (copy_reg));
  309. else
  310. {
  311. /* When control reaches end of non-void function,
  312. there are no return copy insns at all. This
  313. avoids an ice on that invalid function. */
  314. if (ret_start + nregs == ret_end)
  315. short_block = true;
  316. break;
  317. }
  318. if (!targetm.calls.function_value_regno_p (copy_start))
  319. copy_num = 0;
  320. else
  321. copy_num
  322. = hard_regno_nregs[copy_start][GET_MODE (copy_reg)];
  323. /* If the return register is not likely spilled, - as is
  324. the case for floating point on SH4 - then it might
  325. be set by an arithmetic operation that needs a
  326. different mode than the exit block. */
  327. for (j = n_entities - 1; j >= 0; j--)
  328. {
  329. int e = entity_map[j];
  330. int mode =
  331. targetm.mode_switching.needed (e, return_copy);
  332. if (mode != num_modes[e]
  333. && mode != targetm.mode_switching.exit (e))
  334. break;
  335. }
  336. if (j >= 0)
  337. {
  338. /* __builtin_return emits a sequence of loads to all
  339. return registers. One of them might require
  340. another mode than MODE_EXIT, even if it is
  341. unrelated to the return value, so we want to put
  342. the final mode switch after it. */
  343. if (multi_reg_return
  344. && targetm.calls.function_value_regno_p
  345. (copy_start))
  346. forced_late_switch = true;
  347. /* For the SH4, floating point loads depend on fpscr,
  348. thus we might need to put the final mode switch
  349. after the return value copy. That is still OK,
  350. because a floating point return value does not
  351. conflict with address reloads. */
  352. if (copy_start >= ret_start
  353. && copy_start + copy_num <= ret_end
  354. && OBJECT_P (SET_SRC (return_copy_pat)))
  355. forced_late_switch = true;
  356. break;
  357. }
  358. if (copy_num == 0)
  359. {
  360. last_insn = return_copy;
  361. continue;
  362. }
  363. if (copy_start >= ret_start
  364. && copy_start + copy_num <= ret_end)
  365. nregs -= copy_num;
  366. else if (!multi_reg_return
  367. || !targetm.calls.function_value_regno_p
  368. (copy_start))
  369. break;
  370. last_insn = return_copy;
  371. }
  372. /* ??? Exception handling can lead to the return value
  373. copy being already separated from the return value use,
  374. as in unwind-dw2.c .
  375. Similarly, conditionally returning without a value,
  376. and conditionally using builtin_return can lead to an
  377. isolated use. */
  378. if (return_copy == BB_HEAD (src_bb))
  379. {
  380. short_block = true;
  381. break;
  382. }
  383. last_insn = return_copy;
  384. }
  385. while (nregs);
  386. /* If we didn't see a full return value copy, verify that there
  387. is a plausible reason for this. If some, but not all of the
  388. return register is likely spilled, we can expect that there
  389. is a copy for the likely spilled part. */
  390. gcc_assert (!nregs
  391. || forced_late_switch
  392. || short_block
  393. || !(targetm.class_likely_spilled_p
  394. (REGNO_REG_CLASS (ret_start)))
  395. || (nregs
  396. != hard_regno_nregs[ret_start][GET_MODE (ret_reg)])
  397. /* For multi-hard-register floating point
  398. values, sometimes the likely-spilled part
  399. is ordinarily copied first, then the other
  400. part is set with an arithmetic operation.
  401. This doesn't actually cause reload
  402. failures, so let it pass. */
  403. || (GET_MODE_CLASS (GET_MODE (ret_reg)) != MODE_INT
  404. && nregs != 1));
  405. if (!NOTE_INSN_BASIC_BLOCK_P (last_insn))
  406. {
  407. before_return_copy
  408. = emit_note_before (NOTE_INSN_DELETED, last_insn);
  409. /* Instructions preceding LAST_INSN in the same block might
  410. require a different mode than MODE_EXIT, so if we might
  411. have such instructions, keep them in a separate block
  412. from pre_exit. */
  413. src_bb = split_block (src_bb,
  414. PREV_INSN (before_return_copy))->dest;
  415. }
  416. else
  417. before_return_copy = last_insn;
  418. pre_exit = split_block (src_bb, before_return_copy)->src;
  419. }
  420. else
  421. {
  422. pre_exit = split_edge (eg);
  423. }
  424. }
  425. return pre_exit;
  426. }
  427. /* Find all insns that need a particular mode setting, and insert the
  428. necessary mode switches. Return true if we did work. */
  429. static int
  430. optimize_mode_switching (void)
  431. {
  432. int e;
  433. basic_block bb;
  434. bool need_commit = false;
  435. static const int num_modes[] = NUM_MODES_FOR_MODE_SWITCHING;
  436. #define N_ENTITIES ARRAY_SIZE (num_modes)
  437. int entity_map[N_ENTITIES];
  438. struct bb_info *bb_info[N_ENTITIES];
  439. int i, j;
  440. int n_entities = 0;
  441. int max_num_modes = 0;
  442. bool emitted ATTRIBUTE_UNUSED = false;
  443. basic_block post_entry = 0;
  444. basic_block pre_exit = 0;
  445. struct edge_list *edge_list = 0;
  446. /* These bitmaps are used for the LCM algorithm. */
  447. sbitmap *kill, *del, *insert, *antic, *transp, *comp;
  448. sbitmap *avin, *avout;
  449. for (e = N_ENTITIES - 1; e >= 0; e--)
  450. if (OPTIMIZE_MODE_SWITCHING (e))
  451. {
  452. int entry_exit_extra = 0;
  453. /* Create the list of segments within each basic block.
  454. If NORMAL_MODE is defined, allow for two extra
  455. blocks split from the entry and exit block. */
  456. if (targetm.mode_switching.entry && targetm.mode_switching.exit)
  457. entry_exit_extra = 3;
  458. bb_info[n_entities]
  459. = XCNEWVEC (struct bb_info,
  460. last_basic_block_for_fn (cfun) + entry_exit_extra);
  461. entity_map[n_entities++] = e;
  462. if (num_modes[e] > max_num_modes)
  463. max_num_modes = num_modes[e];
  464. }
  465. if (! n_entities)
  466. return 0;
  467. /* Make sure if MODE_ENTRY is defined MODE_EXIT is defined. */
  468. gcc_assert ((targetm.mode_switching.entry && targetm.mode_switching.exit)
  469. || (!targetm.mode_switching.entry
  470. && !targetm.mode_switching.exit));
  471. if (targetm.mode_switching.entry && targetm.mode_switching.exit)
  472. {
  473. /* Split the edge from the entry block, so that we can note that
  474. there NORMAL_MODE is supplied. */
  475. post_entry = split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
  476. pre_exit = create_pre_exit (n_entities, entity_map, num_modes);
  477. }
  478. df_analyze ();
  479. /* Create the bitmap vectors. */
  480. antic = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
  481. n_entities * max_num_modes);
  482. transp = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
  483. n_entities * max_num_modes);
  484. comp = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
  485. n_entities * max_num_modes);
  486. avin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
  487. n_entities * max_num_modes);
  488. avout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
  489. n_entities * max_num_modes);
  490. kill = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
  491. n_entities * max_num_modes);
  492. bitmap_vector_ones (transp, last_basic_block_for_fn (cfun));
  493. bitmap_vector_clear (antic, last_basic_block_for_fn (cfun));
  494. bitmap_vector_clear (comp, last_basic_block_for_fn (cfun));
  495. for (j = n_entities - 1; j >= 0; j--)
  496. {
  497. int e = entity_map[j];
  498. int no_mode = num_modes[e];
  499. struct bb_info *info = bb_info[j];
  500. rtx_insn *insn;
  501. /* Determine what the first use (if any) need for a mode of entity E is.
  502. This will be the mode that is anticipatable for this block.
  503. Also compute the initial transparency settings. */
  504. FOR_EACH_BB_FN (bb, cfun)
  505. {
  506. struct seginfo *ptr;
  507. int last_mode = no_mode;
  508. bool any_set_required = false;
  509. HARD_REG_SET live_now;
  510. info[bb->index].mode_out = info[bb->index].mode_in = no_mode;
  511. REG_SET_TO_HARD_REG_SET (live_now, df_get_live_in (bb));
  512. /* Pretend the mode is clobbered across abnormal edges. */
  513. {
  514. edge_iterator ei;
  515. edge eg;
  516. FOR_EACH_EDGE (eg, ei, bb->preds)
  517. if (eg->flags & EDGE_COMPLEX)
  518. break;
  519. if (eg)
  520. {
  521. rtx_insn *ins_pos = BB_HEAD (bb);
  522. if (LABEL_P (ins_pos))
  523. ins_pos = NEXT_INSN (ins_pos);
  524. gcc_assert (NOTE_INSN_BASIC_BLOCK_P (ins_pos));
  525. if (ins_pos != BB_END (bb))
  526. ins_pos = NEXT_INSN (ins_pos);
  527. ptr = new_seginfo (no_mode, ins_pos, bb->index, live_now);
  528. add_seginfo (info + bb->index, ptr);
  529. for (i = 0; i < no_mode; i++)
  530. clear_mode_bit (transp[bb->index], j, i);
  531. }
  532. }
  533. FOR_BB_INSNS (bb, insn)
  534. {
  535. if (INSN_P (insn))
  536. {
  537. int mode = targetm.mode_switching.needed (e, insn);
  538. rtx link;
  539. if (mode != no_mode && mode != last_mode)
  540. {
  541. any_set_required = true;
  542. last_mode = mode;
  543. ptr = new_seginfo (mode, insn, bb->index, live_now);
  544. add_seginfo (info + bb->index, ptr);
  545. for (i = 0; i < no_mode; i++)
  546. clear_mode_bit (transp[bb->index], j, i);
  547. }
  548. if (targetm.mode_switching.after)
  549. last_mode = targetm.mode_switching.after (e, last_mode,
  550. insn);
  551. /* Update LIVE_NOW. */
  552. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  553. if (REG_NOTE_KIND (link) == REG_DEAD)
  554. reg_dies (XEXP (link, 0), &live_now);
  555. note_stores (PATTERN (insn), reg_becomes_live, &live_now);
  556. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  557. if (REG_NOTE_KIND (link) == REG_UNUSED)
  558. reg_dies (XEXP (link, 0), &live_now);
  559. }
  560. }
  561. info[bb->index].computing = last_mode;
  562. /* Check for blocks without ANY mode requirements.
  563. N.B. because of MODE_AFTER, last_mode might still
  564. be different from no_mode, in which case we need to
  565. mark the block as nontransparent. */
  566. if (!any_set_required)
  567. {
  568. ptr = new_seginfo (no_mode, BB_END (bb), bb->index, live_now);
  569. add_seginfo (info + bb->index, ptr);
  570. if (last_mode != no_mode)
  571. for (i = 0; i < no_mode; i++)
  572. clear_mode_bit (transp[bb->index], j, i);
  573. }
  574. }
  575. if (targetm.mode_switching.entry && targetm.mode_switching.exit)
  576. {
  577. int mode = targetm.mode_switching.entry (e);
  578. info[post_entry->index].mode_out =
  579. info[post_entry->index].mode_in = no_mode;
  580. if (pre_exit)
  581. {
  582. info[pre_exit->index].mode_out =
  583. info[pre_exit->index].mode_in = no_mode;
  584. }
  585. if (mode != no_mode)
  586. {
  587. bb = post_entry;
  588. /* By always making this nontransparent, we save
  589. an extra check in make_preds_opaque. We also
  590. need this to avoid confusing pre_edge_lcm when
  591. antic is cleared but transp and comp are set. */
  592. for (i = 0; i < no_mode; i++)
  593. clear_mode_bit (transp[bb->index], j, i);
  594. /* Insert a fake computing definition of MODE into entry
  595. blocks which compute no mode. This represents the mode on
  596. entry. */
  597. info[bb->index].computing = mode;
  598. if (pre_exit)
  599. info[pre_exit->index].seginfo->mode =
  600. targetm.mode_switching.exit (e);
  601. }
  602. }
  603. /* Set the anticipatable and computing arrays. */
  604. for (i = 0; i < no_mode; i++)
  605. {
  606. int m = targetm.mode_switching.priority (entity_map[j], i);
  607. FOR_EACH_BB_FN (bb, cfun)
  608. {
  609. if (info[bb->index].seginfo->mode == m)
  610. set_mode_bit (antic[bb->index], j, m);
  611. if (info[bb->index].computing == m)
  612. set_mode_bit (comp[bb->index], j, m);
  613. }
  614. }
  615. }
  616. /* Calculate the optimal locations for the
  617. placement mode switches to modes with priority I. */
  618. FOR_EACH_BB_FN (bb, cfun)
  619. bitmap_not (kill[bb->index], transp[bb->index]);
  620. edge_list = pre_edge_lcm_avs (n_entities * max_num_modes, transp, comp, antic,
  621. kill, avin, avout, &insert, &del);
  622. for (j = n_entities - 1; j >= 0; j--)
  623. {
  624. int no_mode = num_modes[entity_map[j]];
  625. /* Insert all mode sets that have been inserted by lcm. */
  626. for (int ed = NUM_EDGES (edge_list) - 1; ed >= 0; ed--)
  627. {
  628. edge eg = INDEX_EDGE (edge_list, ed);
  629. eg->aux = (void *)(intptr_t)-1;
  630. for (i = 0; i < no_mode; i++)
  631. {
  632. int m = targetm.mode_switching.priority (entity_map[j], i);
  633. if (mode_bit_p (insert[ed], j, m))
  634. {
  635. eg->aux = (void *)(intptr_t)m;
  636. break;
  637. }
  638. }
  639. }
  640. FOR_EACH_BB_FN (bb, cfun)
  641. {
  642. struct bb_info *info = bb_info[j];
  643. int last_mode = no_mode;
  644. /* intialize mode in availability for bb. */
  645. for (i = 0; i < no_mode; i++)
  646. if (mode_bit_p (avout[bb->index], j, i))
  647. {
  648. if (last_mode == no_mode)
  649. last_mode = i;
  650. if (last_mode != i)
  651. {
  652. last_mode = no_mode;
  653. break;
  654. }
  655. }
  656. info[bb->index].mode_out = last_mode;
  657. /* intialize mode out availability for bb. */
  658. last_mode = no_mode;
  659. for (i = 0; i < no_mode; i++)
  660. if (mode_bit_p (avin[bb->index], j, i))
  661. {
  662. if (last_mode == no_mode)
  663. last_mode = i;
  664. if (last_mode != i)
  665. {
  666. last_mode = no_mode;
  667. break;
  668. }
  669. }
  670. info[bb->index].mode_in = last_mode;
  671. for (i = 0; i < no_mode; i++)
  672. if (mode_bit_p (del[bb->index], j, i))
  673. info[bb->index].seginfo->mode = no_mode;
  674. }
  675. /* Now output the remaining mode sets in all the segments. */
  676. /* In case there was no mode inserted. the mode information on the edge
  677. might not be complete.
  678. Update mode info on edges and commit pending mode sets. */
  679. need_commit |= commit_mode_sets (edge_list, entity_map[j], bb_info[j]);
  680. /* Reset modes for next entity. */
  681. clear_aux_for_edges ();
  682. FOR_EACH_BB_FN (bb, cfun)
  683. {
  684. struct seginfo *ptr, *next;
  685. int cur_mode = bb_info[j][bb->index].mode_in;
  686. for (ptr = bb_info[j][bb->index].seginfo; ptr; ptr = next)
  687. {
  688. next = ptr->next;
  689. if (ptr->mode != no_mode)
  690. {
  691. rtx_insn *mode_set;
  692. rtl_profile_for_bb (bb);
  693. start_sequence ();
  694. targetm.mode_switching.emit (entity_map[j], ptr->mode,
  695. cur_mode, ptr->regs_live);
  696. mode_set = get_insns ();
  697. end_sequence ();
  698. /* modes kill each other inside a basic block. */
  699. cur_mode = ptr->mode;
  700. /* Insert MODE_SET only if it is nonempty. */
  701. if (mode_set != NULL_RTX)
  702. {
  703. emitted = true;
  704. if (NOTE_INSN_BASIC_BLOCK_P (ptr->insn_ptr))
  705. /* We need to emit the insns in a FIFO-like manner,
  706. i.e. the first to be emitted at our insertion
  707. point ends up first in the instruction steam.
  708. Because we made sure that NOTE_INSN_BASIC_BLOCK is
  709. only used for initially empty basic blocks, we
  710. can achieve this by appending at the end of
  711. the block. */
  712. emit_insn_after
  713. (mode_set, BB_END (NOTE_BASIC_BLOCK (ptr->insn_ptr)));
  714. else
  715. emit_insn_before (mode_set, ptr->insn_ptr);
  716. }
  717. default_rtl_profile ();
  718. }
  719. free (ptr);
  720. }
  721. }
  722. free (bb_info[j]);
  723. }
  724. free_edge_list (edge_list);
  725. /* Finished. Free up all the things we've allocated. */
  726. sbitmap_vector_free (del);
  727. sbitmap_vector_free (insert);
  728. sbitmap_vector_free (kill);
  729. sbitmap_vector_free (antic);
  730. sbitmap_vector_free (transp);
  731. sbitmap_vector_free (comp);
  732. sbitmap_vector_free (avin);
  733. sbitmap_vector_free (avout);
  734. if (need_commit)
  735. commit_edge_insertions ();
  736. if (targetm.mode_switching.entry && targetm.mode_switching.exit)
  737. cleanup_cfg (CLEANUP_NO_INSN_DEL);
  738. else if (!need_commit && !emitted)
  739. return 0;
  740. return 1;
  741. }
  742. #endif /* OPTIMIZE_MODE_SWITCHING */
  743. namespace {
  744. const pass_data pass_data_mode_switching =
  745. {
  746. RTL_PASS, /* type */
  747. "mode_sw", /* name */
  748. OPTGROUP_NONE, /* optinfo_flags */
  749. TV_MODE_SWITCH, /* tv_id */
  750. 0, /* properties_required */
  751. 0, /* properties_provided */
  752. 0, /* properties_destroyed */
  753. 0, /* todo_flags_start */
  754. TODO_df_finish, /* todo_flags_finish */
  755. };
  756. class pass_mode_switching : public rtl_opt_pass
  757. {
  758. public:
  759. pass_mode_switching (gcc::context *ctxt)
  760. : rtl_opt_pass (pass_data_mode_switching, ctxt)
  761. {}
  762. /* opt_pass methods: */
  763. /* The epiphany backend creates a second instance of this pass, so we need
  764. a clone method. */
  765. opt_pass * clone () { return new pass_mode_switching (m_ctxt); }
  766. virtual bool gate (function *)
  767. {
  768. #ifdef OPTIMIZE_MODE_SWITCHING
  769. return true;
  770. #else
  771. return false;
  772. #endif
  773. }
  774. virtual unsigned int execute (function *)
  775. {
  776. #ifdef OPTIMIZE_MODE_SWITCHING
  777. optimize_mode_switching ();
  778. #endif /* OPTIMIZE_MODE_SWITCHING */
  779. return 0;
  780. }
  781. }; // class pass_mode_switching
  782. } // anon namespace
  783. rtl_opt_pass *
  784. make_pass_mode_switching (gcc::context *ctxt)
  785. {
  786. return new pass_mode_switching (ctxt);
  787. }