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"
47 #include "insn-config.h"
50 #include "hard-reg-set.h"
59 #include "statistics.h"
60 #include "double-int.h"
62 #include "fixed-value.h"
76 #include "dominance.h"
79 #include "basic-block.h"
83 #include "tree-pass.h"
87 /* Turn STACK_GROWS_DOWNWARD into a boolean. */
88 #ifdef STACK_GROWS_DOWNWARD
89 #undef STACK_GROWS_DOWNWARD
90 #define STACK_GROWS_DOWNWARD 1
92 #define STACK_GROWS_DOWNWARD 0
95 /* This structure records two kinds of stack references between stack
96 adjusting instructions: stack references in memory addresses for
97 regular insns and all stack references for debug insns. */
101 HOST_WIDE_INT sp_offset
;
104 struct csa_reflist
*next
;
107 static int stack_memref_p (rtx
);
108 static rtx
single_set_for_csa (rtx_insn
*);
109 static void free_csa_reflist (struct csa_reflist
*);
110 static struct csa_reflist
*record_one_stack_ref (rtx_insn
*, rtx
*,
111 struct csa_reflist
*);
112 static int try_apply_stack_adjustment (rtx_insn
*, struct csa_reflist
*,
113 HOST_WIDE_INT
, HOST_WIDE_INT
);
114 static void combine_stack_adjustments_for_block (basic_block
);
117 /* Main entry point for stack adjustment combination. */
120 combine_stack_adjustments (void)
124 FOR_EACH_BB_FN (bb
, cfun
)
125 combine_stack_adjustments_for_block (bb
);
128 /* Recognize a MEM of the form (sp) or (plus sp const). */
131 stack_memref_p (rtx x
)
137 if (x
== stack_pointer_rtx
)
139 if (GET_CODE (x
) == PLUS
140 && XEXP (x
, 0) == stack_pointer_rtx
141 && CONST_INT_P (XEXP (x
, 1)))
147 /* Recognize either normal single_set or the hack in i386.md for
148 tying fp and sp adjustments. */
151 single_set_for_csa (rtx_insn
*insn
)
154 rtx tmp
= single_set (insn
);
158 if (!NONJUMP_INSN_P (insn
)
159 || GET_CODE (PATTERN (insn
)) != PARALLEL
)
162 tmp
= PATTERN (insn
);
163 if (GET_CODE (XVECEXP (tmp
, 0, 0)) != SET
)
166 for (i
= 1; i
< XVECLEN (tmp
, 0); ++i
)
168 rtx this_rtx
= XVECEXP (tmp
, 0, i
);
170 /* The special case is allowing a no-op set. */
171 if (GET_CODE (this_rtx
) == SET
172 && SET_SRC (this_rtx
) == SET_DEST (this_rtx
))
174 else if (GET_CODE (this_rtx
) != CLOBBER
175 && GET_CODE (this_rtx
) != USE
)
179 return XVECEXP (tmp
, 0, 0);
182 /* Free the list of csa_reflist nodes. */
185 free_csa_reflist (struct csa_reflist
*reflist
)
187 struct csa_reflist
*next
;
188 for (; reflist
; reflist
= next
)
190 next
= reflist
->next
;
195 /* Create a new csa_reflist node from the given stack reference.
196 It is already known that the reference is either a MEM satisfying the
197 predicate stack_memref_p or a REG representing the stack pointer. */
199 static struct csa_reflist
*
200 record_one_stack_ref (rtx_insn
*insn
, rtx
*ref
, struct csa_reflist
*next_reflist
)
202 struct csa_reflist
*ml
;
204 ml
= XNEW (struct csa_reflist
);
206 if (REG_P (*ref
) || XEXP (*ref
, 0) == stack_pointer_rtx
)
209 ml
->sp_offset
= INTVAL (XEXP (XEXP (*ref
, 0), 1));
213 ml
->next
= next_reflist
;
218 /* We only know how to adjust the CFA; no other frame-related changes
219 may appear in any insn to be deleted. */
222 no_unhandled_cfa (rtx_insn
*insn
)
224 if (!RTX_FRAME_RELATED_P (insn
))
227 /* No CFA notes at all is a legacy interpretation like
228 FRAME_RELATED_EXPR, and is context sensitive within
229 the prologue state machine. We can't handle that here. */
230 bool has_cfa_adjust
= false;
232 for (rtx link
= REG_NOTES (insn
); link
; link
= XEXP (link
, 1))
233 switch (REG_NOTE_KIND (link
))
237 case REG_CFA_ADJUST_CFA
:
238 has_cfa_adjust
= true;
241 case REG_FRAME_RELATED_EXPR
:
242 case REG_CFA_DEF_CFA
:
244 case REG_CFA_REGISTER
:
245 case REG_CFA_EXPRESSION
:
246 case REG_CFA_RESTORE
:
247 case REG_CFA_SET_VDRAP
:
248 case REG_CFA_WINDOW_SAVE
:
249 case REG_CFA_FLUSH_QUEUE
:
253 return has_cfa_adjust
;
256 /* Attempt to apply ADJUST to the stack adjusting insn INSN, as well
257 as each of the memories and stack references in REFLIST. Return true
261 try_apply_stack_adjustment (rtx_insn
*insn
, struct csa_reflist
*reflist
,
262 HOST_WIDE_INT new_adjust
, HOST_WIDE_INT delta
)
264 struct csa_reflist
*ml
;
267 set
= single_set_for_csa (insn
);
268 if (MEM_P (SET_DEST (set
)))
269 validate_change (insn
, &SET_DEST (set
),
270 replace_equiv_address (SET_DEST (set
), stack_pointer_rtx
),
273 validate_change (insn
, &XEXP (SET_SRC (set
), 1), GEN_INT (new_adjust
), 1);
275 for (ml
= reflist
; ml
; ml
= ml
->next
)
277 rtx new_addr
= plus_constant (Pmode
, stack_pointer_rtx
,
278 ml
->sp_offset
- delta
);
281 if (MEM_P (*ml
->ref
))
282 new_val
= replace_equiv_address_nv (*ml
->ref
, new_addr
);
283 else if (GET_MODE (*ml
->ref
) == GET_MODE (stack_pointer_rtx
))
286 new_val
= lowpart_subreg (GET_MODE (*ml
->ref
), new_addr
,
287 GET_MODE (new_addr
));
288 validate_change (ml
->insn
, ml
->ref
, new_val
, 1);
291 if (apply_change_group ())
293 /* Succeeded. Update our knowledge of the stack references. */
294 for (ml
= reflist
; ml
; ml
= ml
->next
)
295 ml
->sp_offset
-= delta
;
303 /* For non-debug insns, record all stack memory references in INSN
304 and return true if there were no other (unrecorded) references to the
305 stack pointer. For debug insns, record all stack references regardless
306 of context and unconditionally return true. */
309 record_stack_refs (rtx_insn
*insn
, struct csa_reflist
**reflist
)
311 subrtx_ptr_iterator::array_type array
;
312 FOR_EACH_SUBRTX_PTR (iter
, array
, &PATTERN (insn
), NONCONST
)
316 switch (GET_CODE (x
))
319 if (!reg_mentioned_p (stack_pointer_rtx
, x
))
320 iter
.skip_subrtxes ();
321 /* We are not able to handle correctly all possible memrefs
322 containing stack pointer, so this check is necessary. */
323 else if (stack_memref_p (x
))
325 *reflist
= record_one_stack_ref (insn
, loc
, *reflist
);
326 iter
.skip_subrtxes ();
328 /* Try harder for DEBUG_INSNs, handle e.g.
329 (mem (mem (sp + 16) + 4). */
330 else if (!DEBUG_INSN_P (insn
))
335 /* ??? We want be able to handle non-memory stack pointer
336 references later. For now just discard all insns referring to
337 stack pointer outside mem expressions. We would probably
338 want to teach validate_replace to simplify expressions first.
340 We can't just compare with STACK_POINTER_RTX because the
341 reference to the stack pointer might be in some other mode.
342 In particular, an explicit clobber in an asm statement will
343 result in a QImode clobber.
345 In DEBUG_INSNs, we want to replace all occurrences, otherwise
346 they will cause -fcompare-debug failures. */
347 if (REGNO (x
) == STACK_POINTER_REGNUM
)
349 if (!DEBUG_INSN_P (insn
))
351 *reflist
= record_one_stack_ref (insn
, loc
, *reflist
);
362 /* If INSN has a REG_ARGS_SIZE note, move it to LAST.
363 AFTER is true iff LAST follows INSN in the instruction stream. */
366 maybe_move_args_size_note (rtx_insn
*last
, rtx_insn
*insn
, bool after
)
370 note
= find_reg_note (insn
, REG_ARGS_SIZE
, NULL_RTX
);
374 last_note
= find_reg_note (last
, REG_ARGS_SIZE
, NULL_RTX
);
377 /* The ARGS_SIZE notes are *not* cumulative. They represent an
378 absolute value, and the "most recent" note wins. */
380 XEXP (last_note
, 0) = XEXP (note
, 0);
383 add_reg_note (last
, REG_ARGS_SIZE
, XEXP (note
, 0));
386 /* Merge any REG_CFA_ADJUST_CFA note from SRC into DST.
387 AFTER is true iff DST follows SRC in the instruction stream. */
390 maybe_merge_cfa_adjust (rtx_insn
*dst
, rtx_insn
*src
, bool after
)
392 rtx snote
= NULL
, dnote
= NULL
;
396 if (RTX_FRAME_RELATED_P (src
))
397 snote
= find_reg_note (src
, REG_CFA_ADJUST_CFA
, NULL_RTX
);
400 sexp
= XEXP (snote
, 0);
402 if (RTX_FRAME_RELATED_P (dst
))
403 dnote
= find_reg_note (dst
, REG_CFA_ADJUST_CFA
, NULL_RTX
);
406 add_reg_note (dst
, REG_CFA_ADJUST_CFA
, sexp
);
409 dexp
= XEXP (dnote
, 0);
411 gcc_assert (GET_CODE (sexp
) == SET
);
412 gcc_assert (GET_CODE (dexp
) == SET
);
415 exp1
= dexp
, exp2
= sexp
;
417 exp1
= sexp
, exp2
= dexp
;
419 SET_SRC (exp1
) = simplify_replace_rtx (SET_SRC (exp1
), SET_DEST (exp2
),
421 XEXP (dnote
, 0) = exp1
;
424 /* Return the next (or previous) active insn within BB. */
427 prev_active_insn_bb (basic_block bb
, rtx_insn
*insn
)
429 for (insn
= PREV_INSN (insn
);
430 insn
!= PREV_INSN (BB_HEAD (bb
));
431 insn
= PREV_INSN (insn
))
432 if (active_insn_p (insn
))
438 next_active_insn_bb (basic_block bb
, rtx_insn
*insn
)
440 for (insn
= NEXT_INSN (insn
);
441 insn
!= NEXT_INSN (BB_END (bb
));
442 insn
= NEXT_INSN (insn
))
443 if (active_insn_p (insn
))
448 /* If INSN has a REG_ARGS_SIZE note, if possible move it to PREV. Otherwise
449 search for a nearby candidate within BB where we can stick the note. */
452 force_move_args_size_note (basic_block bb
, rtx_insn
*prev
, rtx_insn
*insn
)
455 rtx_insn
*test
, *next_candidate
, *prev_candidate
;
457 /* If PREV exists, tail-call to the logic in the other function. */
460 maybe_move_args_size_note (prev
, insn
, false);
464 /* First, make sure there's anything that needs doing. */
465 note
= find_reg_note (insn
, REG_ARGS_SIZE
, NULL_RTX
);
469 /* We need to find a spot between the previous and next exception points
470 where we can place the note and "properly" deallocate the arguments. */
471 next_candidate
= prev_candidate
= NULL
;
473 /* It is often the case that we have insns in the order:
475 add sp (previous deallocation)
476 sub sp (align for next arglist)
478 and the add/sub cancel. Therefore we begin by searching forward. */
481 while ((test
= next_active_insn_bb (bb
, test
)) != NULL
)
483 /* Found an existing note: nothing to do. */
484 if (find_reg_note (test
, REG_ARGS_SIZE
, NULL_RTX
))
486 /* Found something that affects unwinding. Stop searching. */
487 if (CALL_P (test
) || !insn_nothrow_p (test
))
489 if (next_candidate
== NULL
)
490 next_candidate
= test
;
494 while ((test
= prev_active_insn_bb (bb
, test
)) != NULL
)
497 /* Found a place that seems logical to adjust the stack. */
498 tnote
= find_reg_note (test
, REG_ARGS_SIZE
, NULL_RTX
);
501 XEXP (tnote
, 0) = XEXP (note
, 0);
504 if (prev_candidate
== NULL
)
505 prev_candidate
= test
;
506 /* Found something that affects unwinding. Stop searching. */
507 if (CALL_P (test
) || !insn_nothrow_p (test
))
512 test
= prev_candidate
;
513 else if (next_candidate
)
514 test
= next_candidate
;
517 /* ??? We *must* have a place, lest we ICE on the lost adjustment.
518 Options are: dummy clobber insn, nop, or prevent the removal of
520 /* TODO: Find another way to indicate to the dwarf2 code that we
521 have not in fact lost an adjustment. */
522 test
= emit_insn_before (gen_rtx_CLOBBER (VOIDmode
, const0_rtx
), insn
);
524 add_reg_note (test
, REG_ARGS_SIZE
, XEXP (note
, 0));
527 /* Subroutine of combine_stack_adjustments, called for each basic block. */
530 combine_stack_adjustments_for_block (basic_block bb
)
532 HOST_WIDE_INT last_sp_adjust
= 0;
533 rtx_insn
*last_sp_set
= NULL
;
534 rtx_insn
*last2_sp_set
= NULL
;
535 struct csa_reflist
*reflist
= NULL
;
536 rtx_insn
*insn
, *next
;
538 bool end_of_block
= false;
540 for (insn
= BB_HEAD (bb
); !end_of_block
; insn
= next
)
542 end_of_block
= insn
== BB_END (bb
);
543 next
= NEXT_INSN (insn
);
548 set
= single_set_for_csa (insn
);
551 rtx dest
= SET_DEST (set
);
552 rtx src
= SET_SRC (set
);
554 /* Find constant additions to the stack pointer. */
555 if (dest
== stack_pointer_rtx
556 && GET_CODE (src
) == PLUS
557 && XEXP (src
, 0) == stack_pointer_rtx
558 && CONST_INT_P (XEXP (src
, 1)))
560 HOST_WIDE_INT this_adjust
= INTVAL (XEXP (src
, 1));
562 /* If we've not seen an adjustment previously, record
563 it now and continue. */
567 last_sp_adjust
= this_adjust
;
571 /* If not all recorded refs can be adjusted, or the
572 adjustment is now too large for a constant addition,
573 we cannot merge the two stack adjustments.
575 Also we need to be careful to not move stack pointer
576 such that we create stack accesses outside the allocated
577 area. We can combine an allocation into the first insn,
578 or a deallocation into the second insn. We can not
579 combine an allocation followed by a deallocation.
581 The only somewhat frequent occurrence of the later is when
582 a function allocates a stack frame but does not use it.
583 For this case, we would need to analyze rtl stream to be
584 sure that allocated area is really unused. This means not
585 only checking the memory references, but also all registers
586 or global memory references possibly containing a stack
589 Perhaps the best way to address this problem is to teach
590 gcc not to allocate stack for objects never used. */
592 /* Combine an allocation into the first instruction. */
593 if (STACK_GROWS_DOWNWARD
? this_adjust
<= 0 : this_adjust
>= 0)
595 if (no_unhandled_cfa (insn
)
596 && try_apply_stack_adjustment (last_sp_set
, reflist
,
602 maybe_move_args_size_note (last_sp_set
, insn
, false);
603 maybe_merge_cfa_adjust (last_sp_set
, insn
, false);
605 last_sp_adjust
+= this_adjust
;
610 /* Otherwise we have a deallocation. Do not combine with
611 a previous allocation. Combine into the second insn. */
612 else if (STACK_GROWS_DOWNWARD
613 ? last_sp_adjust
>= 0 : last_sp_adjust
<= 0)
615 if (no_unhandled_cfa (last_sp_set
)
616 && try_apply_stack_adjustment (insn
, reflist
,
622 maybe_move_args_size_note (insn
, last_sp_set
, true);
623 maybe_merge_cfa_adjust (insn
, last_sp_set
, true);
624 delete_insn (last_sp_set
);
626 last_sp_adjust
+= this_adjust
;
627 free_csa_reflist (reflist
);
633 /* Combination failed. Restart processing from here. If
634 deallocation+allocation conspired to cancel, we can
635 delete the old deallocation insn. */
638 if (last_sp_adjust
== 0 && no_unhandled_cfa (last_sp_set
))
640 maybe_move_args_size_note (insn
, last_sp_set
, true);
641 maybe_merge_cfa_adjust (insn
, last_sp_set
, true);
642 delete_insn (last_sp_set
);
645 last2_sp_set
= last_sp_set
;
647 free_csa_reflist (reflist
);
650 last_sp_adjust
= this_adjust
;
654 /* Find a store with pre-(dec|inc)rement or pre-modify of exactly
655 the previous adjustment and turn it into a simple store. This
656 is equivalent to anticipating the stack adjustment so this must
659 && ((STACK_GROWS_DOWNWARD
660 ? (GET_CODE (XEXP (dest
, 0)) == PRE_DEC
662 == (HOST_WIDE_INT
) GET_MODE_SIZE (GET_MODE (dest
)))
663 : (GET_CODE (XEXP (dest
, 0)) == PRE_INC
665 == -(HOST_WIDE_INT
) GET_MODE_SIZE (GET_MODE (dest
))))
666 || ((STACK_GROWS_DOWNWARD
667 ? last_sp_adjust
>= 0 : last_sp_adjust
<= 0)
668 && GET_CODE (XEXP (dest
, 0)) == PRE_MODIFY
669 && GET_CODE (XEXP (XEXP (dest
, 0), 1)) == PLUS
670 && XEXP (XEXP (XEXP (dest
, 0), 1), 0)
672 && GET_CODE (XEXP (XEXP (XEXP (dest
, 0), 1), 1))
674 && INTVAL (XEXP (XEXP (XEXP (dest
, 0), 1), 1))
676 && XEXP (XEXP (dest
, 0), 0) == stack_pointer_rtx
677 && !reg_mentioned_p (stack_pointer_rtx
, src
)
678 && memory_address_p (GET_MODE (dest
), stack_pointer_rtx
)
679 && try_apply_stack_adjustment (insn
, reflist
, 0,
683 maybe_move_args_size_note (last2_sp_set
, last_sp_set
, false);
685 maybe_move_args_size_note (insn
, last_sp_set
, true);
686 delete_insn (last_sp_set
);
687 free_csa_reflist (reflist
);
695 if (!CALL_P (insn
) && last_sp_set
696 && record_stack_refs (insn
, &reflist
))
699 /* Otherwise, we were not able to process the instruction.
700 Do not continue collecting data across such a one. */
703 || reg_mentioned_p (stack_pointer_rtx
, PATTERN (insn
))))
705 if (last_sp_set
&& last_sp_adjust
== 0)
707 force_move_args_size_note (bb
, last2_sp_set
, last_sp_set
);
708 delete_insn (last_sp_set
);
710 free_csa_reflist (reflist
);
718 if (last_sp_set
&& last_sp_adjust
== 0)
720 force_move_args_size_note (bb
, last2_sp_set
, last_sp_set
);
721 delete_insn (last_sp_set
);
725 free_csa_reflist (reflist
);
729 rest_of_handle_stack_adjustments (void)
731 df_note_add_problem ();
733 combine_stack_adjustments ();
739 const pass_data pass_data_stack_adjustments
=
743 OPTGROUP_NONE
, /* optinfo_flags */
744 TV_COMBINE_STACK_ADJUST
, /* tv_id */
745 0, /* properties_required */
746 0, /* properties_provided */
747 0, /* properties_destroyed */
748 0, /* todo_flags_start */
749 TODO_df_finish
, /* todo_flags_finish */
752 class pass_stack_adjustments
: public rtl_opt_pass
755 pass_stack_adjustments (gcc::context
*ctxt
)
756 : rtl_opt_pass (pass_data_stack_adjustments
, ctxt
)
759 /* opt_pass methods: */
760 virtual bool gate (function
*);
761 virtual unsigned int execute (function
*)
763 return rest_of_handle_stack_adjustments ();
766 }; // class pass_stack_adjustments
769 pass_stack_adjustments::gate (function
*)
771 /* This is kind of a heuristic. We need to run combine_stack_adjustments
772 even for machines with possibly nonzero TARGET_RETURN_POPS_ARGS
773 and ACCUMULATE_OUTGOING_ARGS. We expect that only ports having
774 push instructions will have popping returns. */
775 #ifndef PUSH_ROUNDING
776 if (ACCUMULATE_OUTGOING_ARGS
)
779 return flag_combine_stack_adjustments
;
785 make_pass_stack_adjustments (gcc::context
*ctxt
)
787 return new pass_stack_adjustments (ctxt
);