combine-stack-adj.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /* Combine stack adjustments.
  2. Copyright (C) 1987-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. /* Track stack adjustments and stack memory references. Attempt to
  16. reduce the number of stack adjustments by back-propagating across
  17. the memory references.
  18. This is intended primarily for use with targets that do not define
  19. ACCUMULATE_OUTGOING_ARGS. It is of significantly more value to
  20. targets that define PREFERRED_STACK_BOUNDARY more aligned than
  21. STACK_BOUNDARY (e.g. x86), or if not all registers can be pushed
  22. (e.g. x86 fp regs) which would ordinarily have to be implemented
  23. as a sub/mov pair due to restrictions in calls.c.
  24. Propagation stops when any of the insns that need adjusting are
  25. (a) no longer valid because we've exceeded their range, (b) a
  26. non-trivial push instruction, or (c) a call instruction.
  27. Restriction B is based on the assumption that push instructions
  28. are smaller or faster. If a port really wants to remove all
  29. pushes, it should have defined ACCUMULATE_OUTGOING_ARGS. The
  30. one exception that is made is for an add immediately followed
  31. by a push. */
  32. #include "config.h"
  33. #include "system.h"
  34. #include "coretypes.h"
  35. #include "tm.h"
  36. #include "rtl.h"
  37. #include "tm_p.h"
  38. #include "insn-config.h"
  39. #include "recog.h"
  40. #include "regs.h"
  41. #include "hard-reg-set.h"
  42. #include "flags.h"
  43. #include "hashtab.h"
  44. #include "hash-set.h"
  45. #include "vec.h"
  46. #include "machmode.h"
  47. #include "input.h"
  48. #include "function.h"
  49. #include "symtab.h"
  50. #include "statistics.h"
  51. #include "double-int.h"
  52. #include "real.h"
  53. #include "fixed-value.h"
  54. #include "alias.h"
  55. #include "wide-int.h"
  56. #include "inchash.h"
  57. #include "tree.h"
  58. #include "expmed.h"
  59. #include "dojump.h"
  60. #include "explow.h"
  61. #include "calls.h"
  62. #include "emit-rtl.h"
  63. #include "varasm.h"
  64. #include "stmt.h"
  65. #include "expr.h"
  66. #include "predict.h"
  67. #include "dominance.h"
  68. #include "cfg.h"
  69. #include "cfgrtl.h"
  70. #include "basic-block.h"
  71. #include "df.h"
  72. #include "except.h"
  73. #include "reload.h"
  74. #include "tree-pass.h"
  75. #include "rtl-iter.h"
  76. /* Turn STACK_GROWS_DOWNWARD into a boolean. */
  77. #ifdef STACK_GROWS_DOWNWARD
  78. #undef STACK_GROWS_DOWNWARD
  79. #define STACK_GROWS_DOWNWARD 1
  80. #else
  81. #define STACK_GROWS_DOWNWARD 0
  82. #endif
  83. /* This structure records two kinds of stack references between stack
  84. adjusting instructions: stack references in memory addresses for
  85. regular insns and all stack references for debug insns. */
  86. struct csa_reflist
  87. {
  88. HOST_WIDE_INT sp_offset;
  89. rtx_insn *insn;
  90. rtx *ref;
  91. struct csa_reflist *next;
  92. };
  93. static int stack_memref_p (rtx);
  94. static rtx single_set_for_csa (rtx_insn *);
  95. static void free_csa_reflist (struct csa_reflist *);
  96. static struct csa_reflist *record_one_stack_ref (rtx_insn *, rtx *,
  97. struct csa_reflist *);
  98. static int try_apply_stack_adjustment (rtx_insn *, struct csa_reflist *,
  99. HOST_WIDE_INT, HOST_WIDE_INT);
  100. static void combine_stack_adjustments_for_block (basic_block);
  101. /* Main entry point for stack adjustment combination. */
  102. static void
  103. combine_stack_adjustments (void)
  104. {
  105. basic_block bb;
  106. FOR_EACH_BB_FN (bb, cfun)
  107. combine_stack_adjustments_for_block (bb);
  108. }
  109. /* Recognize a MEM of the form (sp) or (plus sp const). */
  110. static int
  111. stack_memref_p (rtx x)
  112. {
  113. if (!MEM_P (x))
  114. return 0;
  115. x = XEXP (x, 0);
  116. if (x == stack_pointer_rtx)
  117. return 1;
  118. if (GET_CODE (x) == PLUS
  119. && XEXP (x, 0) == stack_pointer_rtx
  120. && CONST_INT_P (XEXP (x, 1)))
  121. return 1;
  122. return 0;
  123. }
  124. /* Recognize either normal single_set or the hack in i386.md for
  125. tying fp and sp adjustments. */
  126. static rtx
  127. single_set_for_csa (rtx_insn *insn)
  128. {
  129. int i;
  130. rtx tmp = single_set (insn);
  131. if (tmp)
  132. return tmp;
  133. if (!NONJUMP_INSN_P (insn)
  134. || GET_CODE (PATTERN (insn)) != PARALLEL)
  135. return NULL_RTX;
  136. tmp = PATTERN (insn);
  137. if (GET_CODE (XVECEXP (tmp, 0, 0)) != SET)
  138. return NULL_RTX;
  139. for (i = 1; i < XVECLEN (tmp, 0); ++i)
  140. {
  141. rtx this_rtx = XVECEXP (tmp, 0, i);
  142. /* The special case is allowing a no-op set. */
  143. if (GET_CODE (this_rtx) == SET
  144. && SET_SRC (this_rtx) == SET_DEST (this_rtx))
  145. ;
  146. else if (GET_CODE (this_rtx) != CLOBBER
  147. && GET_CODE (this_rtx) != USE)
  148. return NULL_RTX;
  149. }
  150. return XVECEXP (tmp, 0, 0);
  151. }
  152. /* Free the list of csa_reflist nodes. */
  153. static void
  154. free_csa_reflist (struct csa_reflist *reflist)
  155. {
  156. struct csa_reflist *next;
  157. for (; reflist ; reflist = next)
  158. {
  159. next = reflist->next;
  160. free (reflist);
  161. }
  162. }
  163. /* Create a new csa_reflist node from the given stack reference.
  164. It is already known that the reference is either a MEM satisfying the
  165. predicate stack_memref_p or a REG representing the stack pointer. */
  166. static struct csa_reflist *
  167. record_one_stack_ref (rtx_insn *insn, rtx *ref, struct csa_reflist *next_reflist)
  168. {
  169. struct csa_reflist *ml;
  170. ml = XNEW (struct csa_reflist);
  171. if (REG_P (*ref) || XEXP (*ref, 0) == stack_pointer_rtx)
  172. ml->sp_offset = 0;
  173. else
  174. ml->sp_offset = INTVAL (XEXP (XEXP (*ref, 0), 1));
  175. ml->insn = insn;
  176. ml->ref = ref;
  177. ml->next = next_reflist;
  178. return ml;
  179. }
  180. /* We only know how to adjust the CFA; no other frame-related changes
  181. may appear in any insn to be deleted. */
  182. static bool
  183. no_unhandled_cfa (rtx_insn *insn)
  184. {
  185. if (!RTX_FRAME_RELATED_P (insn))
  186. return true;
  187. /* No CFA notes at all is a legacy interpretation like
  188. FRAME_RELATED_EXPR, and is context sensitive within
  189. the prologue state machine. We can't handle that here. */
  190. bool has_cfa_adjust = false;
  191. for (rtx link = REG_NOTES (insn); link; link = XEXP (link, 1))
  192. switch (REG_NOTE_KIND (link))
  193. {
  194. default:
  195. break;
  196. case REG_CFA_ADJUST_CFA:
  197. has_cfa_adjust = true;
  198. break;
  199. case REG_FRAME_RELATED_EXPR:
  200. case REG_CFA_DEF_CFA:
  201. case REG_CFA_OFFSET:
  202. case REG_CFA_REGISTER:
  203. case REG_CFA_EXPRESSION:
  204. case REG_CFA_RESTORE:
  205. case REG_CFA_SET_VDRAP:
  206. case REG_CFA_WINDOW_SAVE:
  207. case REG_CFA_FLUSH_QUEUE:
  208. return false;
  209. }
  210. return has_cfa_adjust;
  211. }
  212. /* Attempt to apply ADJUST to the stack adjusting insn INSN, as well
  213. as each of the memories and stack references in REFLIST. Return true
  214. on success. */
  215. static int
  216. try_apply_stack_adjustment (rtx_insn *insn, struct csa_reflist *reflist,
  217. HOST_WIDE_INT new_adjust, HOST_WIDE_INT delta)
  218. {
  219. struct csa_reflist *ml;
  220. rtx set;
  221. set = single_set_for_csa (insn);
  222. if (MEM_P (SET_DEST (set)))
  223. validate_change (insn, &SET_DEST (set),
  224. replace_equiv_address (SET_DEST (set), stack_pointer_rtx),
  225. 1);
  226. else
  227. validate_change (insn, &XEXP (SET_SRC (set), 1), GEN_INT (new_adjust), 1);
  228. for (ml = reflist; ml ; ml = ml->next)
  229. {
  230. rtx new_addr = plus_constant (Pmode, stack_pointer_rtx,
  231. ml->sp_offset - delta);
  232. rtx new_val;
  233. if (MEM_P (*ml->ref))
  234. new_val = replace_equiv_address_nv (*ml->ref, new_addr);
  235. else if (GET_MODE (*ml->ref) == GET_MODE (stack_pointer_rtx))
  236. new_val = new_addr;
  237. else
  238. new_val = lowpart_subreg (GET_MODE (*ml->ref), new_addr,
  239. GET_MODE (new_addr));
  240. validate_change (ml->insn, ml->ref, new_val, 1);
  241. }
  242. if (apply_change_group ())
  243. {
  244. /* Succeeded. Update our knowledge of the stack references. */
  245. for (ml = reflist; ml ; ml = ml->next)
  246. ml->sp_offset -= delta;
  247. return 1;
  248. }
  249. else
  250. return 0;
  251. }
  252. /* For non-debug insns, record all stack memory references in INSN
  253. and return true if there were no other (unrecorded) references to the
  254. stack pointer. For debug insns, record all stack references regardless
  255. of context and unconditionally return true. */
  256. static bool
  257. record_stack_refs (rtx_insn *insn, struct csa_reflist **reflist)
  258. {
  259. subrtx_ptr_iterator::array_type array;
  260. FOR_EACH_SUBRTX_PTR (iter, array, &PATTERN (insn), NONCONST)
  261. {
  262. rtx *loc = *iter;
  263. rtx x = *loc;
  264. switch (GET_CODE (x))
  265. {
  266. case MEM:
  267. if (!reg_mentioned_p (stack_pointer_rtx, x))
  268. iter.skip_subrtxes ();
  269. /* We are not able to handle correctly all possible memrefs
  270. containing stack pointer, so this check is necessary. */
  271. else if (stack_memref_p (x))
  272. {
  273. *reflist = record_one_stack_ref (insn, loc, *reflist);
  274. iter.skip_subrtxes ();
  275. }
  276. /* Try harder for DEBUG_INSNs, handle e.g.
  277. (mem (mem (sp + 16) + 4). */
  278. else if (!DEBUG_INSN_P (insn))
  279. return false;
  280. break;
  281. case REG:
  282. /* ??? We want be able to handle non-memory stack pointer
  283. references later. For now just discard all insns referring to
  284. stack pointer outside mem expressions. We would probably
  285. want to teach validate_replace to simplify expressions first.
  286. We can't just compare with STACK_POINTER_RTX because the
  287. reference to the stack pointer might be in some other mode.
  288. In particular, an explicit clobber in an asm statement will
  289. result in a QImode clobber.
  290. In DEBUG_INSNs, we want to replace all occurrences, otherwise
  291. they will cause -fcompare-debug failures. */
  292. if (REGNO (x) == STACK_POINTER_REGNUM)
  293. {
  294. if (!DEBUG_INSN_P (insn))
  295. return false;
  296. *reflist = record_one_stack_ref (insn, loc, *reflist);
  297. }
  298. break;
  299. default:
  300. break;
  301. }
  302. }
  303. return true;
  304. }
  305. /* If INSN has a REG_ARGS_SIZE note, move it to LAST.
  306. AFTER is true iff LAST follows INSN in the instruction stream. */
  307. static void
  308. maybe_move_args_size_note (rtx_insn *last, rtx_insn *insn, bool after)
  309. {
  310. rtx note, last_note;
  311. note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
  312. if (note == NULL)
  313. return;
  314. last_note = find_reg_note (last, REG_ARGS_SIZE, NULL_RTX);
  315. if (last_note)
  316. {
  317. /* The ARGS_SIZE notes are *not* cumulative. They represent an
  318. absolute value, and the "most recent" note wins. */
  319. if (!after)
  320. XEXP (last_note, 0) = XEXP (note, 0);
  321. }
  322. else
  323. add_reg_note (last, REG_ARGS_SIZE, XEXP (note, 0));
  324. }
  325. /* Merge any REG_CFA_ADJUST_CFA note from SRC into DST.
  326. AFTER is true iff DST follows SRC in the instruction stream. */
  327. static void
  328. maybe_merge_cfa_adjust (rtx_insn *dst, rtx_insn *src, bool after)
  329. {
  330. rtx snote = NULL, dnote = NULL;
  331. rtx sexp, dexp;
  332. rtx exp1, exp2;
  333. if (RTX_FRAME_RELATED_P (src))
  334. snote = find_reg_note (src, REG_CFA_ADJUST_CFA, NULL_RTX);
  335. if (snote == NULL)
  336. return;
  337. sexp = XEXP (snote, 0);
  338. if (RTX_FRAME_RELATED_P (dst))
  339. dnote = find_reg_note (dst, REG_CFA_ADJUST_CFA, NULL_RTX);
  340. if (dnote == NULL)
  341. {
  342. add_reg_note (dst, REG_CFA_ADJUST_CFA, sexp);
  343. return;
  344. }
  345. dexp = XEXP (dnote, 0);
  346. gcc_assert (GET_CODE (sexp) == SET);
  347. gcc_assert (GET_CODE (dexp) == SET);
  348. if (after)
  349. exp1 = dexp, exp2 = sexp;
  350. else
  351. exp1 = sexp, exp2 = dexp;
  352. SET_SRC (exp1) = simplify_replace_rtx (SET_SRC (exp1), SET_DEST (exp2),
  353. SET_SRC (exp2));
  354. XEXP (dnote, 0) = exp1;
  355. }
  356. /* Return the next (or previous) active insn within BB. */
  357. static rtx_insn *
  358. prev_active_insn_bb (basic_block bb, rtx_insn *insn)
  359. {
  360. for (insn = PREV_INSN (insn);
  361. insn != PREV_INSN (BB_HEAD (bb));
  362. insn = PREV_INSN (insn))
  363. if (active_insn_p (insn))
  364. return insn;
  365. return NULL;
  366. }
  367. static rtx_insn *
  368. next_active_insn_bb (basic_block bb, rtx_insn *insn)
  369. {
  370. for (insn = NEXT_INSN (insn);
  371. insn != NEXT_INSN (BB_END (bb));
  372. insn = NEXT_INSN (insn))
  373. if (active_insn_p (insn))
  374. return insn;
  375. return NULL;
  376. }
  377. /* If INSN has a REG_ARGS_SIZE note, if possible move it to PREV. Otherwise
  378. search for a nearby candidate within BB where we can stick the note. */
  379. static void
  380. force_move_args_size_note (basic_block bb, rtx_insn *prev, rtx_insn *insn)
  381. {
  382. rtx note;
  383. rtx_insn *test, *next_candidate, *prev_candidate;
  384. /* If PREV exists, tail-call to the logic in the other function. */
  385. if (prev)
  386. {
  387. maybe_move_args_size_note (prev, insn, false);
  388. return;
  389. }
  390. /* First, make sure there's anything that needs doing. */
  391. note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
  392. if (note == NULL)
  393. return;
  394. /* We need to find a spot between the previous and next exception points
  395. where we can place the note and "properly" deallocate the arguments. */
  396. next_candidate = prev_candidate = NULL;
  397. /* It is often the case that we have insns in the order:
  398. call
  399. add sp (previous deallocation)
  400. sub sp (align for next arglist)
  401. push arg
  402. and the add/sub cancel. Therefore we begin by searching forward. */
  403. test = insn;
  404. while ((test = next_active_insn_bb (bb, test)) != NULL)
  405. {
  406. /* Found an existing note: nothing to do. */
  407. if (find_reg_note (test, REG_ARGS_SIZE, NULL_RTX))
  408. return;
  409. /* Found something that affects unwinding. Stop searching. */
  410. if (CALL_P (test) || !insn_nothrow_p (test))
  411. break;
  412. if (next_candidate == NULL)
  413. next_candidate = test;
  414. }
  415. test = insn;
  416. while ((test = prev_active_insn_bb (bb, test)) != NULL)
  417. {
  418. rtx tnote;
  419. /* Found a place that seems logical to adjust the stack. */
  420. tnote = find_reg_note (test, REG_ARGS_SIZE, NULL_RTX);
  421. if (tnote)
  422. {
  423. XEXP (tnote, 0) = XEXP (note, 0);
  424. return;
  425. }
  426. if (prev_candidate == NULL)
  427. prev_candidate = test;
  428. /* Found something that affects unwinding. Stop searching. */
  429. if (CALL_P (test) || !insn_nothrow_p (test))
  430. break;
  431. }
  432. if (prev_candidate)
  433. test = prev_candidate;
  434. else if (next_candidate)
  435. test = next_candidate;
  436. else
  437. {
  438. /* ??? We *must* have a place, lest we ICE on the lost adjustment.
  439. Options are: dummy clobber insn, nop, or prevent the removal of
  440. the sp += 0 insn. */
  441. /* TODO: Find another way to indicate to the dwarf2 code that we
  442. have not in fact lost an adjustment. */
  443. test = emit_insn_before (gen_rtx_CLOBBER (VOIDmode, const0_rtx), insn);
  444. }
  445. add_reg_note (test, REG_ARGS_SIZE, XEXP (note, 0));
  446. }
  447. /* Subroutine of combine_stack_adjustments, called for each basic block. */
  448. static void
  449. combine_stack_adjustments_for_block (basic_block bb)
  450. {
  451. HOST_WIDE_INT last_sp_adjust = 0;
  452. rtx_insn *last_sp_set = NULL;
  453. rtx_insn *last2_sp_set = NULL;
  454. struct csa_reflist *reflist = NULL;
  455. rtx_insn *insn, *next;
  456. rtx set;
  457. bool end_of_block = false;
  458. for (insn = BB_HEAD (bb); !end_of_block ; insn = next)
  459. {
  460. end_of_block = insn == BB_END (bb);
  461. next = NEXT_INSN (insn);
  462. if (! INSN_P (insn))
  463. continue;
  464. set = single_set_for_csa (insn);
  465. if (set)
  466. {
  467. rtx dest = SET_DEST (set);
  468. rtx src = SET_SRC (set);
  469. /* Find constant additions to the stack pointer. */
  470. if (dest == stack_pointer_rtx
  471. && GET_CODE (src) == PLUS
  472. && XEXP (src, 0) == stack_pointer_rtx
  473. && CONST_INT_P (XEXP (src, 1)))
  474. {
  475. HOST_WIDE_INT this_adjust = INTVAL (XEXP (src, 1));
  476. /* If we've not seen an adjustment previously, record
  477. it now and continue. */
  478. if (! last_sp_set)
  479. {
  480. last_sp_set = insn;
  481. last_sp_adjust = this_adjust;
  482. continue;
  483. }
  484. /* If not all recorded refs can be adjusted, or the
  485. adjustment is now too large for a constant addition,
  486. we cannot merge the two stack adjustments.
  487. Also we need to be careful to not move stack pointer
  488. such that we create stack accesses outside the allocated
  489. area. We can combine an allocation into the first insn,
  490. or a deallocation into the second insn. We can not
  491. combine an allocation followed by a deallocation.
  492. The only somewhat frequent occurrence of the later is when
  493. a function allocates a stack frame but does not use it.
  494. For this case, we would need to analyze rtl stream to be
  495. sure that allocated area is really unused. This means not
  496. only checking the memory references, but also all registers
  497. or global memory references possibly containing a stack
  498. frame address.
  499. Perhaps the best way to address this problem is to teach
  500. gcc not to allocate stack for objects never used. */
  501. /* Combine an allocation into the first instruction. */
  502. if (STACK_GROWS_DOWNWARD ? this_adjust <= 0 : this_adjust >= 0)
  503. {
  504. if (no_unhandled_cfa (insn)
  505. && try_apply_stack_adjustment (last_sp_set, reflist,
  506. last_sp_adjust
  507. + this_adjust,
  508. this_adjust))
  509. {
  510. /* It worked! */
  511. maybe_move_args_size_note (last_sp_set, insn, false);
  512. maybe_merge_cfa_adjust (last_sp_set, insn, false);
  513. delete_insn (insn);
  514. last_sp_adjust += this_adjust;
  515. continue;
  516. }
  517. }
  518. /* Otherwise we have a deallocation. Do not combine with
  519. a previous allocation. Combine into the second insn. */
  520. else if (STACK_GROWS_DOWNWARD
  521. ? last_sp_adjust >= 0 : last_sp_adjust <= 0)
  522. {
  523. if (no_unhandled_cfa (last_sp_set)
  524. && try_apply_stack_adjustment (insn, reflist,
  525. last_sp_adjust
  526. + this_adjust,
  527. -last_sp_adjust))
  528. {
  529. /* It worked! */
  530. maybe_move_args_size_note (insn, last_sp_set, true);
  531. maybe_merge_cfa_adjust (insn, last_sp_set, true);
  532. delete_insn (last_sp_set);
  533. last_sp_set = insn;
  534. last_sp_adjust += this_adjust;
  535. free_csa_reflist (reflist);
  536. reflist = NULL;
  537. continue;
  538. }
  539. }
  540. /* Combination failed. Restart processing from here. If
  541. deallocation+allocation conspired to cancel, we can
  542. delete the old deallocation insn. */
  543. if (last_sp_set)
  544. {
  545. if (last_sp_adjust == 0 && no_unhandled_cfa (last_sp_set))
  546. {
  547. maybe_move_args_size_note (insn, last_sp_set, true);
  548. maybe_merge_cfa_adjust (insn, last_sp_set, true);
  549. delete_insn (last_sp_set);
  550. }
  551. else
  552. last2_sp_set = last_sp_set;
  553. }
  554. free_csa_reflist (reflist);
  555. reflist = NULL;
  556. last_sp_set = insn;
  557. last_sp_adjust = this_adjust;
  558. continue;
  559. }
  560. /* Find a store with pre-(dec|inc)rement or pre-modify of exactly
  561. the previous adjustment and turn it into a simple store. This
  562. is equivalent to anticipating the stack adjustment so this must
  563. be an allocation. */
  564. if (MEM_P (dest)
  565. && ((STACK_GROWS_DOWNWARD
  566. ? (GET_CODE (XEXP (dest, 0)) == PRE_DEC
  567. && last_sp_adjust
  568. == (HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (dest)))
  569. : (GET_CODE (XEXP (dest, 0)) == PRE_INC
  570. && last_sp_adjust
  571. == -(HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (dest))))
  572. || ((STACK_GROWS_DOWNWARD
  573. ? last_sp_adjust >= 0 : last_sp_adjust <= 0)
  574. && GET_CODE (XEXP (dest, 0)) == PRE_MODIFY
  575. && GET_CODE (XEXP (XEXP (dest, 0), 1)) == PLUS
  576. && XEXP (XEXP (XEXP (dest, 0), 1), 0)
  577. == stack_pointer_rtx
  578. && GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
  579. == CONST_INT
  580. && INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1))
  581. == -last_sp_adjust))
  582. && XEXP (XEXP (dest, 0), 0) == stack_pointer_rtx
  583. && !reg_mentioned_p (stack_pointer_rtx, src)
  584. && memory_address_p (GET_MODE (dest), stack_pointer_rtx)
  585. && try_apply_stack_adjustment (insn, reflist, 0,
  586. -last_sp_adjust))
  587. {
  588. if (last2_sp_set)
  589. maybe_move_args_size_note (last2_sp_set, last_sp_set, false);
  590. else
  591. maybe_move_args_size_note (insn, last_sp_set, true);
  592. delete_insn (last_sp_set);
  593. free_csa_reflist (reflist);
  594. reflist = NULL;
  595. last_sp_set = NULL;
  596. last_sp_adjust = 0;
  597. continue;
  598. }
  599. }
  600. if (!CALL_P (insn) && last_sp_set
  601. && record_stack_refs (insn, &reflist))
  602. continue;
  603. /* Otherwise, we were not able to process the instruction.
  604. Do not continue collecting data across such a one. */
  605. if (last_sp_set
  606. && (CALL_P (insn)
  607. || reg_mentioned_p (stack_pointer_rtx, PATTERN (insn))))
  608. {
  609. if (last_sp_set && last_sp_adjust == 0)
  610. {
  611. force_move_args_size_note (bb, last2_sp_set, last_sp_set);
  612. delete_insn (last_sp_set);
  613. }
  614. free_csa_reflist (reflist);
  615. reflist = NULL;
  616. last2_sp_set = NULL;
  617. last_sp_set = NULL;
  618. last_sp_adjust = 0;
  619. }
  620. }
  621. if (last_sp_set && last_sp_adjust == 0)
  622. {
  623. force_move_args_size_note (bb, last2_sp_set, last_sp_set);
  624. delete_insn (last_sp_set);
  625. }
  626. if (reflist)
  627. free_csa_reflist (reflist);
  628. }
  629. static unsigned int
  630. rest_of_handle_stack_adjustments (void)
  631. {
  632. df_note_add_problem ();
  633. df_analyze ();
  634. combine_stack_adjustments ();
  635. return 0;
  636. }
  637. namespace {
  638. const pass_data pass_data_stack_adjustments =
  639. {
  640. RTL_PASS, /* type */
  641. "csa", /* name */
  642. OPTGROUP_NONE, /* optinfo_flags */
  643. TV_COMBINE_STACK_ADJUST, /* tv_id */
  644. 0, /* properties_required */
  645. 0, /* properties_provided */
  646. 0, /* properties_destroyed */
  647. 0, /* todo_flags_start */
  648. TODO_df_finish, /* todo_flags_finish */
  649. };
  650. class pass_stack_adjustments : public rtl_opt_pass
  651. {
  652. public:
  653. pass_stack_adjustments (gcc::context *ctxt)
  654. : rtl_opt_pass (pass_data_stack_adjustments, ctxt)
  655. {}
  656. /* opt_pass methods: */
  657. virtual bool gate (function *);
  658. virtual unsigned int execute (function *)
  659. {
  660. return rest_of_handle_stack_adjustments ();
  661. }
  662. }; // class pass_stack_adjustments
  663. bool
  664. pass_stack_adjustments::gate (function *)
  665. {
  666. /* This is kind of a heuristic. We need to run combine_stack_adjustments
  667. even for machines with possibly nonzero TARGET_RETURN_POPS_ARGS
  668. and ACCUMULATE_OUTGOING_ARGS. We expect that only ports having
  669. push instructions will have popping returns. */
  670. #ifndef PUSH_ROUNDING
  671. if (ACCUMULATE_OUTGOING_ARGS)
  672. return false;
  673. #endif
  674. return flag_combine_stack_adjustments;
  675. }
  676. } // anon namespace
  677. rtl_opt_pass *
  678. make_pass_stack_adjustments (gcc::context *ctxt)
  679. {
  680. return new pass_stack_adjustments (ctxt);
  681. }