1 /* Combine stack adjustments.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
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
8 Software Foundation; either version 3, or (at your option) any later
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
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
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.
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.
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.
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
43 #include "coretypes.h"
49 #include "insn-config.h"
65 #include "tree-pass.h"
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. */
75 HOST_WIDE_INT sp_offset
;
78 struct csa_reflist
*next
;
81 static int stack_memref_p (rtx
);
82 static rtx
single_set_for_csa (rtx_insn
*);
83 static void free_csa_reflist (struct csa_reflist
*);
84 static struct csa_reflist
*record_one_stack_ref (rtx_insn
*, rtx
*,
85 struct csa_reflist
*);
86 static int try_apply_stack_adjustment (rtx_insn
*, struct csa_reflist
*,
87 HOST_WIDE_INT
, HOST_WIDE_INT
);
88 static void combine_stack_adjustments_for_block (basic_block
);
91 /* Main entry point for stack adjustment combination. */
94 combine_stack_adjustments (void)
98 FOR_EACH_BB_FN (bb
, cfun
)
99 combine_stack_adjustments_for_block (bb
);
102 /* Recognize a MEM of the form (sp) or (plus sp const). */
105 stack_memref_p (rtx x
)
111 if (x
== stack_pointer_rtx
)
113 if (GET_CODE (x
) == PLUS
114 && XEXP (x
, 0) == stack_pointer_rtx
115 && CONST_INT_P (XEXP (x
, 1)))
121 /* Recognize either normal single_set or the hack in i386.md for
122 tying fp and sp adjustments. */
125 single_set_for_csa (rtx_insn
*insn
)
128 rtx tmp
= single_set (insn
);
132 if (!NONJUMP_INSN_P (insn
)
133 || GET_CODE (PATTERN (insn
)) != PARALLEL
)
136 tmp
= PATTERN (insn
);
137 if (GET_CODE (XVECEXP (tmp
, 0, 0)) != SET
)
140 for (i
= 1; i
< XVECLEN (tmp
, 0); ++i
)
142 rtx this_rtx
= XVECEXP (tmp
, 0, i
);
144 /* The special case is allowing a no-op set. */
145 if (GET_CODE (this_rtx
) == SET
146 && SET_SRC (this_rtx
) == SET_DEST (this_rtx
))
148 else if (GET_CODE (this_rtx
) != CLOBBER
149 && GET_CODE (this_rtx
) != USE
)
153 return XVECEXP (tmp
, 0, 0);
156 /* Free the list of csa_reflist nodes. */
159 free_csa_reflist (struct csa_reflist
*reflist
)
161 struct csa_reflist
*next
;
162 for (; reflist
; reflist
= next
)
164 next
= reflist
->next
;
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. */
173 static struct csa_reflist
*
174 record_one_stack_ref (rtx_insn
*insn
, rtx
*ref
, struct csa_reflist
*next_reflist
)
176 struct csa_reflist
*ml
;
178 ml
= XNEW (struct csa_reflist
);
180 if (REG_P (*ref
) || XEXP (*ref
, 0) == stack_pointer_rtx
)
183 ml
->sp_offset
= INTVAL (XEXP (XEXP (*ref
, 0), 1));
187 ml
->next
= next_reflist
;
192 /* We only know how to adjust the CFA; no other frame-related changes
193 may appear in any insn to be deleted. */
196 no_unhandled_cfa (rtx_insn
*insn
)
198 if (!RTX_FRAME_RELATED_P (insn
))
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;
206 for (rtx link
= REG_NOTES (insn
); link
; link
= XEXP (link
, 1))
207 switch (REG_NOTE_KIND (link
))
211 case REG_CFA_ADJUST_CFA
:
212 has_cfa_adjust
= true;
215 case REG_FRAME_RELATED_EXPR
:
216 case REG_CFA_DEF_CFA
:
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
:
227 return has_cfa_adjust
;
230 /* Attempt to apply ADJUST to the stack adjusting insn INSN, as well
231 as each of the memories and stack references in REFLIST. Return true
235 try_apply_stack_adjustment (rtx_insn
*insn
, struct csa_reflist
*reflist
,
236 HOST_WIDE_INT new_adjust
, HOST_WIDE_INT delta
)
238 struct csa_reflist
*ml
;
241 set
= single_set_for_csa (insn
);
242 if (MEM_P (SET_DEST (set
)))
243 validate_change (insn
, &SET_DEST (set
),
244 replace_equiv_address (SET_DEST (set
), stack_pointer_rtx
),
247 validate_change (insn
, &XEXP (SET_SRC (set
), 1), GEN_INT (new_adjust
), 1);
249 for (ml
= reflist
; ml
; ml
= ml
->next
)
251 rtx new_addr
= plus_constant (Pmode
, stack_pointer_rtx
,
252 ml
->sp_offset
- delta
);
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
))
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);
265 if (apply_change_group ())
267 /* Succeeded. Update our knowledge of the stack references. */
268 for (ml
= reflist
; ml
; ml
= ml
->next
)
269 ml
->sp_offset
-= delta
;
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. */
283 record_stack_refs (rtx_insn
*insn
, struct csa_reflist
**reflist
)
285 subrtx_ptr_iterator::array_type array
;
286 FOR_EACH_SUBRTX_PTR (iter
, array
, &PATTERN (insn
), NONCONST
)
290 switch (GET_CODE (x
))
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
))
299 *reflist
= record_one_stack_ref (insn
, loc
, *reflist
);
300 iter
.skip_subrtxes ();
302 /* Try harder for DEBUG_INSNs, handle e.g.
303 (mem (mem (sp + 16) + 4). */
304 else if (!DEBUG_INSN_P (insn
))
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.
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.
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
)
323 if (!DEBUG_INSN_P (insn
))
325 *reflist
= record_one_stack_ref (insn
, loc
, *reflist
);
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. */
340 maybe_move_args_size_note (rtx_insn
*last
, rtx_insn
*insn
, bool after
)
344 note
= find_reg_note (insn
, REG_ARGS_SIZE
, NULL_RTX
);
348 last_note
= find_reg_note (last
, REG_ARGS_SIZE
, NULL_RTX
);
351 /* The ARGS_SIZE notes are *not* cumulative. They represent an
352 absolute value, and the "most recent" note wins. */
354 XEXP (last_note
, 0) = XEXP (note
, 0);
357 add_reg_note (last
, REG_ARGS_SIZE
, XEXP (note
, 0));
360 /* Merge any REG_CFA_ADJUST_CFA note from SRC into DST.
361 AFTER is true iff DST follows SRC in the instruction stream. */
364 maybe_merge_cfa_adjust (rtx_insn
*dst
, rtx_insn
*src
, bool after
)
366 rtx snote
= NULL
, dnote
= NULL
;
370 if (RTX_FRAME_RELATED_P (src
))
371 snote
= find_reg_note (src
, REG_CFA_ADJUST_CFA
, NULL_RTX
);
374 sexp
= XEXP (snote
, 0);
376 if (RTX_FRAME_RELATED_P (dst
))
377 dnote
= find_reg_note (dst
, REG_CFA_ADJUST_CFA
, NULL_RTX
);
380 add_reg_note (dst
, REG_CFA_ADJUST_CFA
, sexp
);
383 dexp
= XEXP (dnote
, 0);
385 gcc_assert (GET_CODE (sexp
) == SET
);
386 gcc_assert (GET_CODE (dexp
) == SET
);
389 exp1
= dexp
, exp2
= sexp
;
391 exp1
= sexp
, exp2
= dexp
;
393 SET_SRC (exp1
) = simplify_replace_rtx (SET_SRC (exp1
), SET_DEST (exp2
),
395 XEXP (dnote
, 0) = exp1
;
398 /* Return the next (or previous) active insn within BB. */
401 prev_active_insn_bb (basic_block bb
, rtx_insn
*insn
)
403 for (insn
= PREV_INSN (insn
);
404 insn
!= PREV_INSN (BB_HEAD (bb
));
405 insn
= PREV_INSN (insn
))
406 if (active_insn_p (insn
))
412 next_active_insn_bb (basic_block bb
, rtx_insn
*insn
)
414 for (insn
= NEXT_INSN (insn
);
415 insn
!= NEXT_INSN (BB_END (bb
));
416 insn
= NEXT_INSN (insn
))
417 if (active_insn_p (insn
))
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. */
426 force_move_args_size_note (basic_block bb
, rtx_insn
*prev
, rtx_insn
*insn
)
429 rtx_insn
*test
, *next_candidate
, *prev_candidate
;
431 /* If PREV exists, tail-call to the logic in the other function. */
434 maybe_move_args_size_note (prev
, insn
, false);
438 /* First, make sure there's anything that needs doing. */
439 note
= find_reg_note (insn
, REG_ARGS_SIZE
, NULL_RTX
);
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
;
447 /* It is often the case that we have insns in the order:
449 add sp (previous deallocation)
450 sub sp (align for next arglist)
452 and the add/sub cancel. Therefore we begin by searching forward. */
455 while ((test
= next_active_insn_bb (bb
, test
)) != NULL
)
457 /* Found an existing note: nothing to do. */
458 if (find_reg_note (test
, REG_ARGS_SIZE
, NULL_RTX
))
460 /* Found something that affects unwinding. Stop searching. */
461 if (CALL_P (test
) || !insn_nothrow_p (test
))
463 if (next_candidate
== NULL
)
464 next_candidate
= test
;
468 while ((test
= prev_active_insn_bb (bb
, test
)) != NULL
)
471 /* Found a place that seems logical to adjust the stack. */
472 tnote
= find_reg_note (test
, REG_ARGS_SIZE
, NULL_RTX
);
475 XEXP (tnote
, 0) = XEXP (note
, 0);
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
))
486 test
= prev_candidate
;
487 else if (next_candidate
)
488 test
= next_candidate
;
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
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
);
498 add_reg_note (test
, REG_ARGS_SIZE
, XEXP (note
, 0));
501 /* Subroutine of combine_stack_adjustments, called for each basic block. */
504 combine_stack_adjustments_for_block (basic_block bb
)
506 HOST_WIDE_INT last_sp_adjust
= 0;
507 rtx_insn
*last_sp_set
= NULL
;
508 rtx_insn
*last2_sp_set
= NULL
;
509 struct csa_reflist
*reflist
= NULL
;
510 rtx_insn
*insn
, *next
;
512 bool end_of_block
= false;
514 for (insn
= BB_HEAD (bb
); !end_of_block
; insn
= next
)
516 end_of_block
= insn
== BB_END (bb
);
517 next
= NEXT_INSN (insn
);
522 set
= single_set_for_csa (insn
);
525 rtx dest
= SET_DEST (set
);
526 rtx src
= SET_SRC (set
);
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
532 && CONST_INT_P (XEXP (src
, 1)))
534 HOST_WIDE_INT this_adjust
= INTVAL (XEXP (src
, 1));
536 /* If we've not seen an adjustment previously, record
537 it now and continue. */
541 last_sp_adjust
= this_adjust
;
545 /* If not all recorded refs can be adjusted, or the
546 adjustment is now too large for a constant addition,
547 we cannot merge the two stack adjustments.
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.
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
563 Perhaps the best way to address this problem is to teach
564 gcc not to allocate stack for objects never used. */
566 /* Combine an allocation into the first instruction. */
567 if (STACK_GROWS_DOWNWARD
? this_adjust
<= 0 : this_adjust
>= 0)
569 if (no_unhandled_cfa (insn
)
570 && try_apply_stack_adjustment (last_sp_set
, reflist
,
576 maybe_move_args_size_note (last_sp_set
, insn
, false);
577 maybe_merge_cfa_adjust (last_sp_set
, insn
, false);
579 last_sp_adjust
+= this_adjust
;
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)
589 if (no_unhandled_cfa (last_sp_set
)
590 && try_apply_stack_adjustment (insn
, reflist
,
596 maybe_move_args_size_note (insn
, last_sp_set
, true);
597 maybe_merge_cfa_adjust (insn
, last_sp_set
, true);
598 delete_insn (last_sp_set
);
600 last_sp_adjust
+= this_adjust
;
601 free_csa_reflist (reflist
);
607 /* Combination failed. Restart processing from here. If
608 deallocation+allocation conspired to cancel, we can
609 delete the old deallocation insn. */
612 if (last_sp_adjust
== 0 && no_unhandled_cfa (last_sp_set
))
614 maybe_move_args_size_note (insn
, last_sp_set
, true);
615 maybe_merge_cfa_adjust (insn
, last_sp_set
, true);
616 delete_insn (last_sp_set
);
619 last2_sp_set
= last_sp_set
;
621 free_csa_reflist (reflist
);
624 last_sp_adjust
= this_adjust
;
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
633 && ((STACK_GROWS_DOWNWARD
634 ? (GET_CODE (XEXP (dest
, 0)) == PRE_DEC
636 == (HOST_WIDE_INT
) GET_MODE_SIZE (GET_MODE (dest
)))
637 : (GET_CODE (XEXP (dest
, 0)) == PRE_INC
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
643 && GET_CODE (XEXP (XEXP (dest
, 0), 1)) == PLUS
644 && XEXP (XEXP (XEXP (dest
, 0), 1), 0)
646 && GET_CODE (XEXP (XEXP (XEXP (dest
, 0), 1), 1))
648 && INTVAL (XEXP (XEXP (XEXP (dest
, 0), 1), 1))
650 && XEXP (XEXP (dest
, 0), 0) == stack_pointer_rtx
651 && !reg_mentioned_p (stack_pointer_rtx
, src
)
652 && memory_address_p (GET_MODE (dest
), stack_pointer_rtx
)
653 && try_apply_stack_adjustment (insn
, reflist
, 0,
657 maybe_move_args_size_note (last2_sp_set
, last_sp_set
, false);
659 maybe_move_args_size_note (insn
, last_sp_set
, true);
660 delete_insn (last_sp_set
);
661 free_csa_reflist (reflist
);
669 if (!CALL_P (insn
) && last_sp_set
670 && record_stack_refs (insn
, &reflist
))
673 /* Otherwise, we were not able to process the instruction.
674 Do not continue collecting data across such a one. */
677 || reg_mentioned_p (stack_pointer_rtx
, PATTERN (insn
))))
679 if (last_sp_set
&& last_sp_adjust
== 0)
681 force_move_args_size_note (bb
, last2_sp_set
, last_sp_set
);
682 delete_insn (last_sp_set
);
684 free_csa_reflist (reflist
);
692 if (last_sp_set
&& last_sp_adjust
== 0)
694 force_move_args_size_note (bb
, last2_sp_set
, last_sp_set
);
695 delete_insn (last_sp_set
);
699 free_csa_reflist (reflist
);
703 rest_of_handle_stack_adjustments (void)
705 df_note_add_problem ();
707 combine_stack_adjustments ();
713 const pass_data pass_data_stack_adjustments
=
717 OPTGROUP_NONE
, /* optinfo_flags */
718 TV_COMBINE_STACK_ADJUST
, /* tv_id */
719 0, /* properties_required */
720 0, /* properties_provided */
721 0, /* properties_destroyed */
722 0, /* todo_flags_start */
723 TODO_df_finish
, /* todo_flags_finish */
726 class pass_stack_adjustments
: public rtl_opt_pass
729 pass_stack_adjustments (gcc::context
*ctxt
)
730 : rtl_opt_pass (pass_data_stack_adjustments
, ctxt
)
733 /* opt_pass methods: */
734 virtual bool gate (function
*);
735 virtual unsigned int execute (function
*)
737 return rest_of_handle_stack_adjustments ();
740 }; // class pass_stack_adjustments
743 pass_stack_adjustments::gate (function
*)
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
)
753 return flag_combine_stack_adjustments
;
759 make_pass_stack_adjustments (gcc::context
*ctxt
)
761 return new pass_stack_adjustments (ctxt
);