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