compare-elim.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /* Post-reload compare elimination.
  2. Copyright (C) 2010-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. /* There is a set of targets whose general-purpose move or addition
  16. instructions clobber the flags. These targets cannot split their
  17. CBRANCH/CSTORE etc patterns before reload is complete, lest reload
  18. itself insert these instructions in between the flags setter and user.
  19. Because these targets cannot split the compare from the use, they
  20. cannot make use of the comparison elimination offered by the combine pass.
  21. This is a small pass intended to provide comparison elimination similar to
  22. what is available via NOTICE_UPDATE_CC for cc0 targets. This should help
  23. encourage cc0 targets to convert to an explicit post-reload representation
  24. of the flags.
  25. This pass assumes:
  26. (0) CBRANCH/CSTORE etc have been split in pass_split_after_reload.
  27. (1) All comparison patterns are represented as
  28. [(set (reg:CC) (compare:CC (reg) (reg_or_immediate)))]
  29. (2) All insn patterns that modify the flags are represented as
  30. [(set (reg) (operation)
  31. (clobber (reg:CC))]
  32. (3) If an insn of form (2) can usefully set the flags, there is
  33. another pattern of the form
  34. [(set (reg) (operation)
  35. (set (reg:CCM) (compare:CCM (operation) (immediate)))]
  36. The mode CCM will be chosen as if by SELECT_CC_MODE.
  37. Note that unlike NOTICE_UPDATE_CC, we do not handle memory operands.
  38. This could be handled as a future enhancement.
  39. */
  40. #include "config.h"
  41. #include "system.h"
  42. #include "coretypes.h"
  43. #include "tm.h"
  44. #include "rtl.h"
  45. #include "tm_p.h"
  46. #include "insn-config.h"
  47. #include "recog.h"
  48. #include "flags.h"
  49. #include "predict.h"
  50. #include "vec.h"
  51. #include "hashtab.h"
  52. #include "hash-set.h"
  53. #include "machmode.h"
  54. #include "hard-reg-set.h"
  55. #include "input.h"
  56. #include "function.h"
  57. #include "dominance.h"
  58. #include "cfg.h"
  59. #include "cfgrtl.h"
  60. #include "basic-block.h"
  61. #include "tree-pass.h"
  62. #include "target.h"
  63. #include "df.h"
  64. #include "domwalk.h"
  65. /* These structures describe a comparison and how it is used. */
  66. /* The choice of maximum 3 uses comes from wanting to eliminate the two
  67. duplicate compares from a three-way branch on the sign of a value.
  68. This is also sufficient to eliminate the duplicate compare against the
  69. high-part of a double-word comparison. */
  70. #define MAX_CMP_USE 3
  71. struct comparison_use
  72. {
  73. /* The instruction in which the result of the compare is used. */
  74. rtx_insn *insn;
  75. /* The location of the flags register within the use. */
  76. rtx *loc;
  77. /* The comparison code applied against the flags register. */
  78. enum rtx_code code;
  79. };
  80. struct comparison
  81. {
  82. /* The comparison instruction. */
  83. rtx_insn *insn;
  84. /* The insn prior to the comparison insn that clobbers the flags. */
  85. rtx_insn *prev_clobber;
  86. /* The two values being compared. These will be either REGs or
  87. constants. */
  88. rtx in_a, in_b;
  89. /* The REG_EH_REGION of the comparison. */
  90. rtx eh_note;
  91. /* Information about how this comparison is used. */
  92. struct comparison_use uses[MAX_CMP_USE];
  93. /* The original CC_MODE for this comparison. */
  94. machine_mode orig_mode;
  95. /* The number of uses identified for this comparison. */
  96. unsigned short n_uses;
  97. /* True if not all uses of this comparison have been identified.
  98. This can happen either for overflowing the array above, or if
  99. the flags register is used in some unusual context. */
  100. bool missing_uses;
  101. /* True if its inputs are still valid at the end of the block. */
  102. bool inputs_valid;
  103. };
  104. typedef struct comparison *comparison_struct_p;
  105. static vec<comparison_struct_p> all_compares;
  106. /* Look for a "conforming" comparison, as defined above. If valid, return
  107. the rtx for the COMPARE itself. */
  108. static rtx
  109. conforming_compare (rtx_insn *insn)
  110. {
  111. rtx set, src, dest;
  112. set = single_set (insn);
  113. if (set == NULL)
  114. return NULL;
  115. src = SET_SRC (set);
  116. if (GET_CODE (src) != COMPARE)
  117. return NULL;
  118. dest = SET_DEST (set);
  119. if (!REG_P (dest) || REGNO (dest) != targetm.flags_regnum)
  120. return NULL;
  121. if (REG_P (XEXP (src, 0))
  122. && (REG_P (XEXP (src, 1)) || CONSTANT_P (XEXP (src, 1))))
  123. return src;
  124. return NULL;
  125. }
  126. /* Look for a pattern of the "correct" form for an insn with a flags clobber
  127. for which we may be able to eliminate a compare later. We're not looking
  128. to validate any inputs at this time, merely see that the basic shape is
  129. correct. The term "arithmetic" may be somewhat misleading... */
  130. static bool
  131. arithmetic_flags_clobber_p (rtx_insn *insn)
  132. {
  133. rtx pat, x;
  134. if (!NONJUMP_INSN_P (insn))
  135. return false;
  136. pat = PATTERN (insn);
  137. if (extract_asm_operands (pat))
  138. return false;
  139. if (GET_CODE (pat) == PARALLEL && XVECLEN (pat, 0) == 2)
  140. {
  141. x = XVECEXP (pat, 0, 0);
  142. if (GET_CODE (x) != SET)
  143. return false;
  144. x = SET_DEST (x);
  145. if (!REG_P (x))
  146. return false;
  147. x = XVECEXP (pat, 0, 1);
  148. if (GET_CODE (x) == CLOBBER)
  149. {
  150. x = XEXP (x, 0);
  151. if (REG_P (x) && REGNO (x) == targetm.flags_regnum)
  152. return true;
  153. }
  154. }
  155. return false;
  156. }
  157. /* Look for uses of FLAGS in INSN. If we find one we can analyze, record
  158. it in CMP; otherwise indicate that we've missed a use. */
  159. static void
  160. find_flags_uses_in_insn (struct comparison *cmp, rtx_insn *insn)
  161. {
  162. df_ref use;
  163. /* If we've already lost track of uses, don't bother collecting more. */
  164. if (cmp->missing_uses)
  165. return;
  166. /* Find a USE of the flags register. */
  167. FOR_EACH_INSN_USE (use, insn)
  168. if (DF_REF_REGNO (use) == targetm.flags_regnum)
  169. {
  170. rtx x, *loc;
  171. /* If this is an unusual use, quit. */
  172. if (DF_REF_TYPE (use) != DF_REF_REG_USE)
  173. goto fail;
  174. /* If we've run out of slots to record uses, quit. */
  175. if (cmp->n_uses == MAX_CMP_USE)
  176. goto fail;
  177. /* Unfortunately the location of the flags register, while present
  178. in the reference structure, doesn't help. We need to find the
  179. comparison code that is outer to the actual flags use. */
  180. loc = DF_REF_LOC (use);
  181. x = PATTERN (insn);
  182. if (GET_CODE (x) == PARALLEL)
  183. x = XVECEXP (x, 0, 0);
  184. x = SET_SRC (x);
  185. if (GET_CODE (x) == IF_THEN_ELSE)
  186. x = XEXP (x, 0);
  187. if (COMPARISON_P (x)
  188. && loc == &XEXP (x, 0)
  189. && XEXP (x, 1) == const0_rtx)
  190. {
  191. /* We've found a use of the flags that we understand. */
  192. struct comparison_use *cuse = &cmp->uses[cmp->n_uses++];
  193. cuse->insn = insn;
  194. cuse->loc = loc;
  195. cuse->code = GET_CODE (x);
  196. }
  197. else
  198. goto fail;
  199. }
  200. return;
  201. fail:
  202. /* We failed to recognize this use of the flags register. */
  203. cmp->missing_uses = true;
  204. }
  205. class find_comparison_dom_walker : public dom_walker
  206. {
  207. public:
  208. find_comparison_dom_walker (cdi_direction direction)
  209. : dom_walker (direction) {}
  210. virtual void before_dom_children (basic_block);
  211. };
  212. /* Return true if conforming COMPARE with EH_NOTE is redundant with comparison
  213. CMP and can thus be eliminated. */
  214. static bool
  215. can_eliminate_compare (rtx compare, rtx eh_note, struct comparison *cmp)
  216. {
  217. /* Take care that it's in the same EH region. */
  218. if (cfun->can_throw_non_call_exceptions
  219. && !rtx_equal_p (eh_note, cmp->eh_note))
  220. return false;
  221. /* Make sure the compare is redundant with the previous. */
  222. if (!rtx_equal_p (XEXP (compare, 0), cmp->in_a)
  223. || !rtx_equal_p (XEXP (compare, 1), cmp->in_b))
  224. return false;
  225. /* New mode must be compatible with the previous compare mode. */
  226. enum machine_mode new_mode
  227. = targetm.cc_modes_compatible (GET_MODE (compare), cmp->orig_mode);
  228. if (new_mode == VOIDmode)
  229. return false;
  230. if (cmp->orig_mode != new_mode)
  231. {
  232. /* Generate new comparison for substitution. */
  233. rtx flags = gen_rtx_REG (new_mode, targetm.flags_regnum);
  234. rtx x = gen_rtx_COMPARE (new_mode, cmp->in_a, cmp->in_b);
  235. x = gen_rtx_SET (VOIDmode, flags, x);
  236. if (!validate_change (cmp->insn, &PATTERN (cmp->insn), x, false))
  237. return false;
  238. cmp->orig_mode = new_mode;
  239. }
  240. return true;
  241. }
  242. /* Identify comparison instructions within BB. If the flags from the last
  243. compare in the BB is live at the end of the block, install the compare
  244. in BB->AUX. Called via dom_walker.walk (). */
  245. void
  246. find_comparison_dom_walker::before_dom_children (basic_block bb)
  247. {
  248. struct comparison *last_cmp;
  249. rtx_insn *insn, *next, *last_clobber;
  250. bool last_cmp_valid;
  251. bool need_purge = false;
  252. bitmap killed;
  253. killed = BITMAP_ALLOC (NULL);
  254. /* The last comparison that was made. Will be reset to NULL
  255. once the flags are clobbered. */
  256. last_cmp = NULL;
  257. /* True iff the last comparison has not been clobbered, nor
  258. have its inputs. Used to eliminate duplicate compares. */
  259. last_cmp_valid = false;
  260. /* The last insn that clobbered the flags, if that insn is of
  261. a form that may be valid for eliminating a following compare.
  262. To be reset to NULL once the flags are set otherwise. */
  263. last_clobber = NULL;
  264. /* Propagate the last live comparison throughout the extended basic block. */
  265. if (single_pred_p (bb))
  266. {
  267. last_cmp = (struct comparison *) single_pred (bb)->aux;
  268. if (last_cmp)
  269. last_cmp_valid = last_cmp->inputs_valid;
  270. }
  271. for (insn = BB_HEAD (bb); insn; insn = next)
  272. {
  273. rtx src;
  274. next = (insn == BB_END (bb) ? NULL : NEXT_INSN (insn));
  275. if (!NONDEBUG_INSN_P (insn))
  276. continue;
  277. /* Compute the set of registers modified by this instruction. */
  278. bitmap_clear (killed);
  279. df_simulate_find_defs (insn, killed);
  280. src = conforming_compare (insn);
  281. if (src)
  282. {
  283. rtx eh_note = NULL;
  284. if (cfun->can_throw_non_call_exceptions)
  285. eh_note = find_reg_note (insn, REG_EH_REGION, NULL);
  286. if (last_cmp_valid && can_eliminate_compare (src, eh_note, last_cmp))
  287. {
  288. if (eh_note)
  289. need_purge = true;
  290. delete_insn (insn);
  291. continue;
  292. }
  293. last_cmp = XCNEW (struct comparison);
  294. last_cmp->insn = insn;
  295. last_cmp->prev_clobber = last_clobber;
  296. last_cmp->in_a = XEXP (src, 0);
  297. last_cmp->in_b = XEXP (src, 1);
  298. last_cmp->eh_note = eh_note;
  299. last_cmp->orig_mode = GET_MODE (src);
  300. all_compares.safe_push (last_cmp);
  301. /* It's unusual, but be prepared for comparison patterns that
  302. also clobber an input, or perhaps a scratch. */
  303. last_clobber = NULL;
  304. last_cmp_valid = true;
  305. }
  306. /* Notice if this instruction kills the flags register. */
  307. else if (bitmap_bit_p (killed, targetm.flags_regnum))
  308. {
  309. /* See if this insn could be the "clobber" that eliminates
  310. a future comparison. */
  311. last_clobber = (arithmetic_flags_clobber_p (insn) ? insn : NULL);
  312. /* In either case, the previous compare is no longer valid. */
  313. last_cmp = NULL;
  314. last_cmp_valid = false;
  315. }
  316. /* Notice if this instruction uses the flags register. */
  317. else if (last_cmp)
  318. find_flags_uses_in_insn (last_cmp, insn);
  319. /* Notice if any of the inputs to the comparison have changed. */
  320. if (last_cmp_valid
  321. && (bitmap_bit_p (killed, REGNO (last_cmp->in_a))
  322. || (REG_P (last_cmp->in_b)
  323. && bitmap_bit_p (killed, REGNO (last_cmp->in_b)))))
  324. last_cmp_valid = false;
  325. }
  326. BITMAP_FREE (killed);
  327. /* Remember the live comparison for subsequent members of
  328. the extended basic block. */
  329. if (last_cmp)
  330. {
  331. bb->aux = last_cmp;
  332. last_cmp->inputs_valid = last_cmp_valid;
  333. /* Look to see if the flags register is live outgoing here, and
  334. incoming to any successor not part of the extended basic block. */
  335. if (bitmap_bit_p (df_get_live_out (bb), targetm.flags_regnum))
  336. {
  337. edge e;
  338. edge_iterator ei;
  339. FOR_EACH_EDGE (e, ei, bb->succs)
  340. {
  341. basic_block dest = e->dest;
  342. if (bitmap_bit_p (df_get_live_in (bb), targetm.flags_regnum)
  343. && !single_pred_p (dest))
  344. {
  345. last_cmp->missing_uses = true;
  346. break;
  347. }
  348. }
  349. }
  350. }
  351. /* If we deleted a compare with a REG_EH_REGION note, we may need to
  352. remove EH edges. */
  353. if (need_purge)
  354. purge_dead_edges (bb);
  355. }
  356. /* Find all comparisons in the function. */
  357. static void
  358. find_comparisons (void)
  359. {
  360. calculate_dominance_info (CDI_DOMINATORS);
  361. find_comparison_dom_walker (CDI_DOMINATORS)
  362. .walk (cfun->cfg->x_entry_block_ptr);
  363. clear_aux_for_blocks ();
  364. free_dominance_info (CDI_DOMINATORS);
  365. }
  366. /* Select an alternate CC_MODE for a comparison insn comparing A and B.
  367. Note that inputs are almost certainly different than the IN_A and IN_B
  368. stored in CMP -- we're called while attempting to eliminate the compare
  369. after all. Return the new FLAGS rtx if successful, else return NULL.
  370. Note that this function may start a change group. */
  371. static rtx
  372. maybe_select_cc_mode (struct comparison *cmp, rtx a ATTRIBUTE_UNUSED,
  373. rtx b ATTRIBUTE_UNUSED)
  374. {
  375. machine_mode sel_mode;
  376. const int n = cmp->n_uses;
  377. rtx flags = NULL;
  378. #ifndef SELECT_CC_MODE
  379. /* Minimize code differences when this target macro is undefined. */
  380. return NULL;
  381. #define SELECT_CC_MODE(A,B,C) (gcc_unreachable (), VOIDmode)
  382. #endif
  383. /* If we don't have access to all of the uses, we can't validate. */
  384. if (cmp->missing_uses || n == 0)
  385. return NULL;
  386. /* Find a new mode that works for all of the uses. Special case the
  387. common case of exactly one use. */
  388. if (n == 1)
  389. {
  390. sel_mode = SELECT_CC_MODE (cmp->uses[0].code, a, b);
  391. if (sel_mode != cmp->orig_mode)
  392. {
  393. flags = gen_rtx_REG (sel_mode, targetm.flags_regnum);
  394. validate_change (cmp->uses[0].insn, cmp->uses[0].loc, flags, true);
  395. }
  396. }
  397. else
  398. {
  399. int i;
  400. sel_mode = SELECT_CC_MODE (cmp->uses[0].code, a, b);
  401. for (i = 1; i < n; ++i)
  402. {
  403. machine_mode new_mode = SELECT_CC_MODE (cmp->uses[i].code, a, b);
  404. if (new_mode != sel_mode)
  405. {
  406. sel_mode = targetm.cc_modes_compatible (sel_mode, new_mode);
  407. if (sel_mode == VOIDmode)
  408. return NULL;
  409. }
  410. }
  411. if (sel_mode != cmp->orig_mode)
  412. {
  413. flags = gen_rtx_REG (sel_mode, targetm.flags_regnum);
  414. for (i = 0; i < n; ++i)
  415. validate_change (cmp->uses[i].insn, cmp->uses[i].loc, flags, true);
  416. }
  417. }
  418. return flags;
  419. }
  420. /* Attempt to replace a comparison with a prior arithmetic insn that can
  421. compute the same flags value as the comparison itself. Return true if
  422. successful, having made all rtl modifications necessary. */
  423. static bool
  424. try_eliminate_compare (struct comparison *cmp)
  425. {
  426. rtx_insn *insn, *bb_head;
  427. rtx x, flags, in_a, cmp_src;
  428. /* We must have found an interesting "clobber" preceding the compare. */
  429. if (cmp->prev_clobber == NULL)
  430. return false;
  431. /* ??? For the moment we don't handle comparisons for which IN_B
  432. is a register. We accepted these during initial comparison
  433. recognition in order to eliminate duplicate compares.
  434. An improvement here would be to handle x = a - b; if (a cmp b). */
  435. if (!CONSTANT_P (cmp->in_b))
  436. return false;
  437. /* Verify that IN_A is not clobbered in between CMP and PREV_CLOBBER.
  438. Given that this target requires this pass, we can assume that most
  439. insns do clobber the flags, and so the distance between the compare
  440. and the clobber is likely to be small. */
  441. /* ??? This is one point at which one could argue that DF_REF_CHAIN would
  442. be useful, but it is thought to be too heavy-weight a solution here. */
  443. in_a = cmp->in_a;
  444. insn = cmp->insn;
  445. bb_head = BB_HEAD (BLOCK_FOR_INSN (insn));
  446. for (insn = PREV_INSN (insn);
  447. insn != cmp->prev_clobber;
  448. insn = PREV_INSN (insn))
  449. {
  450. const int abnormal_flags
  451. = (DF_REF_CONDITIONAL | DF_REF_PARTIAL | DF_REF_MAY_CLOBBER
  452. | DF_REF_MUST_CLOBBER | DF_REF_SIGN_EXTRACT
  453. | DF_REF_ZERO_EXTRACT | DF_REF_STRICT_LOW_PART
  454. | DF_REF_PRE_POST_MODIFY);
  455. df_ref def;
  456. /* Note that the BB_HEAD is always either a note or a label, but in
  457. any case it means that IN_A is defined outside the block. */
  458. if (insn == bb_head)
  459. return false;
  460. if (NOTE_P (insn) || DEBUG_INSN_P (insn))
  461. continue;
  462. /* Find a possible def of IN_A in INSN. */
  463. FOR_EACH_INSN_DEF (def, insn)
  464. if (DF_REF_REGNO (def) == REGNO (in_a))
  465. break;
  466. /* No definitions of IN_A; continue searching. */
  467. if (def == NULL)
  468. continue;
  469. /* Bail if this is not a totally normal set of IN_A. */
  470. if (DF_REF_IS_ARTIFICIAL (def))
  471. return false;
  472. if (DF_REF_FLAGS (def) & abnormal_flags)
  473. return false;
  474. /* We've found an insn between the compare and the clobber that sets
  475. IN_A. Given that pass_cprop_hardreg has not yet run, we still find
  476. situations in which we can usefully look through a copy insn. */
  477. x = single_set (insn);
  478. if (x == NULL)
  479. return false;
  480. in_a = SET_SRC (x);
  481. if (!REG_P (in_a))
  482. return false;
  483. }
  484. /* We've reached PREV_CLOBBER without finding a modification of IN_A.
  485. Validate that PREV_CLOBBER itself does in fact refer to IN_A. Do
  486. recall that we've already validated the shape of PREV_CLOBBER. */
  487. x = XVECEXP (PATTERN (insn), 0, 0);
  488. if (rtx_equal_p (SET_DEST (x), in_a))
  489. cmp_src = SET_SRC (x);
  490. /* Also check operations with implicit extensions, e.g.:
  491. [(set (reg:DI)
  492. (zero_extend:DI (plus:SI (reg:SI)(reg:SI))))
  493. (set (reg:CCZ flags)
  494. (compare:CCZ
  495. (plus:SI (reg:SI)(reg:SI))
  496. (const_int 0)))] */
  497. else if (REG_P (SET_DEST (x))
  498. && REG_P (in_a)
  499. && REGNO (SET_DEST (x)) == REGNO (in_a)
  500. && (GET_CODE (SET_SRC (x)) == ZERO_EXTEND
  501. || GET_CODE (SET_SRC (x)) == SIGN_EXTEND)
  502. && GET_MODE (XEXP (SET_SRC (x), 0)) == GET_MODE (in_a))
  503. cmp_src = XEXP (SET_SRC (x), 0);
  504. else
  505. return false;
  506. /* Determine if we ought to use a different CC_MODE here. */
  507. flags = maybe_select_cc_mode (cmp, cmp_src, cmp->in_b);
  508. if (flags == NULL)
  509. flags = gen_rtx_REG (cmp->orig_mode, targetm.flags_regnum);
  510. /* Generate a new comparison for installation in the setter. */
  511. x = copy_rtx (cmp_src);
  512. x = gen_rtx_COMPARE (GET_MODE (flags), x, cmp->in_b);
  513. x = gen_rtx_SET (VOIDmode, flags, x);
  514. /* Succeed if the new instruction is valid. Note that we may have started
  515. a change group within maybe_select_cc_mode, therefore we must continue. */
  516. validate_change (insn, &XVECEXP (PATTERN (insn), 0, 1), x, true);
  517. if (!apply_change_group ())
  518. return false;
  519. /* Success. Delete the compare insn... */
  520. delete_insn (cmp->insn);
  521. /* ... and any notes that are now invalid due to multiple sets. */
  522. x = find_regno_note (insn, REG_UNUSED, targetm.flags_regnum);
  523. if (x)
  524. remove_note (insn, x);
  525. x = find_reg_note (insn, REG_EQUAL, NULL);
  526. if (x)
  527. remove_note (insn, x);
  528. x = find_reg_note (insn, REG_EQUIV, NULL);
  529. if (x)
  530. remove_note (insn, x);
  531. return true;
  532. }
  533. /* Main entry point to the pass. */
  534. static unsigned int
  535. execute_compare_elim_after_reload (void)
  536. {
  537. df_analyze ();
  538. gcc_checking_assert (!all_compares.exists ());
  539. /* Locate all comparisons and their uses, and eliminate duplicates. */
  540. find_comparisons ();
  541. if (all_compares.exists ())
  542. {
  543. struct comparison *cmp;
  544. size_t i;
  545. /* Eliminate comparisons that are redundant with flags computation. */
  546. FOR_EACH_VEC_ELT (all_compares, i, cmp)
  547. {
  548. try_eliminate_compare (cmp);
  549. XDELETE (cmp);
  550. }
  551. all_compares.release ();
  552. }
  553. return 0;
  554. }
  555. namespace {
  556. const pass_data pass_data_compare_elim_after_reload =
  557. {
  558. RTL_PASS, /* type */
  559. "cmpelim", /* name */
  560. OPTGROUP_NONE, /* optinfo_flags */
  561. TV_NONE, /* tv_id */
  562. 0, /* properties_required */
  563. 0, /* properties_provided */
  564. 0, /* properties_destroyed */
  565. 0, /* todo_flags_start */
  566. ( TODO_df_finish | TODO_df_verify ), /* todo_flags_finish */
  567. };
  568. class pass_compare_elim_after_reload : public rtl_opt_pass
  569. {
  570. public:
  571. pass_compare_elim_after_reload (gcc::context *ctxt)
  572. : rtl_opt_pass (pass_data_compare_elim_after_reload, ctxt)
  573. {}
  574. /* opt_pass methods: */
  575. virtual bool gate (function *)
  576. {
  577. /* Setting this target hook value is how a backend indicates the need. */
  578. if (targetm.flags_regnum == INVALID_REGNUM)
  579. return false;
  580. return flag_compare_elim_after_reload;
  581. }
  582. virtual unsigned int execute (function *)
  583. {
  584. return execute_compare_elim_after_reload ();
  585. }
  586. }; // class pass_compare_elim_after_reload
  587. } // anon namespace
  588. rtl_opt_pass *
  589. make_pass_compare_elim_after_reload (gcc::context *ctxt)
  590. {
  591. return new pass_compare_elim_after_reload (ctxt);
  592. }