1 /* Combine stack adjustments.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
22 /* Track stack adjustments and stack memory references. Attempt to
23 reduce the number of stack adjustments by back-propagating across
24 the memory references.
26 This is intended primarily for use with targets that do not define
27 ACCUMULATE_OUTGOING_ARGS. It is of significantly more value to
28 targets that define PREFERRED_STACK_BOUNDARY more aligned than
29 STACK_BOUNDARY (e.g. x86), or if not all registers can be pushed
30 (e.g. x86 fp regs) which would ordinarily have to be implemented
31 as a sub/mov pair due to restrictions in calls.c.
33 Propagation stops when any of the insns that need adjusting are
34 (a) no longer valid because we've exceeded their range, (b) a
35 non-trivial push instruction, or (c) a call instruction.
37 Restriction B is based on the assumption that push instructions
38 are smaller or faster. If a port really wants to remove all
39 pushes, it should have defined ACCUMULATE_OUTGOING_ARGS. The
40 one exception that is made is for an add immediately followed
45 #include "coretypes.h"
49 #include "insn-config.h"
53 #include "hard-reg-set.h"
57 #include "basic-block.h"
62 #include "tree-pass.h"
65 /* Turn STACK_GROWS_DOWNWARD into a boolean. */
66 #ifdef STACK_GROWS_DOWNWARD
67 #undef STACK_GROWS_DOWNWARD
68 #define STACK_GROWS_DOWNWARD 1
70 #define STACK_GROWS_DOWNWARD 0
73 /* This structure records stack memory references between stack adjusting
78 HOST_WIDE_INT sp_offset
;
80 struct csa_memlist
*next
;
83 static int stack_memref_p (rtx
);
84 static rtx
single_set_for_csa (rtx
);
85 static void free_csa_memlist (struct csa_memlist
*);
86 static struct csa_memlist
*record_one_stack_memref (rtx
, rtx
*,
87 struct csa_memlist
*);
88 static int try_apply_stack_adjustment (rtx
, struct csa_memlist
*,
89 HOST_WIDE_INT
, HOST_WIDE_INT
);
90 static void combine_stack_adjustments_for_block (basic_block
);
91 static int record_stack_memrefs (rtx
*, void *);
94 /* Main entry point for stack adjustment combination. */
97 combine_stack_adjustments (void)
102 combine_stack_adjustments_for_block (bb
);
105 /* Recognize a MEM of the form (sp) or (plus sp const). */
108 stack_memref_p (rtx x
)
114 if (x
== stack_pointer_rtx
)
116 if (GET_CODE (x
) == PLUS
117 && XEXP (x
, 0) == stack_pointer_rtx
118 && GET_CODE (XEXP (x
, 1)) == CONST_INT
)
124 /* Recognize either normal single_set or the hack in i386.md for
125 tying fp and sp adjustments. */
128 single_set_for_csa (rtx insn
)
131 rtx tmp
= single_set (insn
);
135 if (!NONJUMP_INSN_P (insn
)
136 || GET_CODE (PATTERN (insn
)) != PARALLEL
)
139 tmp
= PATTERN (insn
);
140 if (GET_CODE (XVECEXP (tmp
, 0, 0)) != SET
)
143 for (i
= 1; i
< XVECLEN (tmp
, 0); ++i
)
145 rtx
this = XVECEXP (tmp
, 0, i
);
147 /* The special case is allowing a no-op set. */
148 if (GET_CODE (this) == SET
149 && SET_SRC (this) == SET_DEST (this))
151 else if (GET_CODE (this) != CLOBBER
152 && GET_CODE (this) != USE
)
156 return XVECEXP (tmp
, 0, 0);
159 /* Free the list of csa_memlist nodes. */
162 free_csa_memlist (struct csa_memlist
*memlist
)
164 struct csa_memlist
*next
;
165 for (; memlist
; memlist
= next
)
167 next
= memlist
->next
;
172 /* Create a new csa_memlist node from the given memory reference.
173 It is already known that the memory is stack_memref_p. */
175 static struct csa_memlist
*
176 record_one_stack_memref (rtx insn
, rtx
*mem
, struct csa_memlist
*next_memlist
)
178 struct csa_memlist
*ml
;
180 ml
= XNEW (struct csa_memlist
);
182 if (XEXP (*mem
, 0) == stack_pointer_rtx
)
185 ml
->sp_offset
= INTVAL (XEXP (XEXP (*mem
, 0), 1));
189 ml
->next
= next_memlist
;
194 /* Attempt to apply ADJUST to the stack adjusting insn INSN, as well
195 as each of the memories in MEMLIST. Return true on success. */
198 try_apply_stack_adjustment (rtx insn
, struct csa_memlist
*memlist
, HOST_WIDE_INT new_adjust
,
201 struct csa_memlist
*ml
;
204 set
= single_set_for_csa (insn
);
205 validate_change (insn
, &XEXP (SET_SRC (set
), 1), GEN_INT (new_adjust
), 1);
207 for (ml
= memlist
; ml
; ml
= ml
->next
)
210 replace_equiv_address_nv (*ml
->mem
,
211 plus_constant (stack_pointer_rtx
,
212 ml
->sp_offset
- delta
)), 1);
214 if (apply_change_group ())
216 /* Succeeded. Update our knowledge of the memory references. */
217 for (ml
= memlist
; ml
; ml
= ml
->next
)
218 ml
->sp_offset
-= delta
;
226 /* Called via for_each_rtx and used to record all stack memory references in
227 the insn and discard all other stack pointer references. */
228 struct record_stack_memrefs_data
231 struct csa_memlist
*memlist
;
235 record_stack_memrefs (rtx
*xp
, void *data
)
238 struct record_stack_memrefs_data
*d
=
239 (struct record_stack_memrefs_data
*) data
;
242 switch (GET_CODE (x
))
245 if (!reg_mentioned_p (stack_pointer_rtx
, x
))
247 /* We are not able to handle correctly all possible memrefs containing
248 stack pointer, so this check is necessary. */
249 if (stack_memref_p (x
))
251 d
->memlist
= record_one_stack_memref (d
->insn
, xp
, d
->memlist
);
256 /* ??? We want be able to handle non-memory stack pointer
257 references later. For now just discard all insns referring to
258 stack pointer outside mem expressions. We would probably
259 want to teach validate_replace to simplify expressions first.
261 We can't just compare with STACK_POINTER_RTX because the
262 reference to the stack pointer might be in some other mode.
263 In particular, an explicit clobber in an asm statement will
264 result in a QImode clobber. */
265 if (REGNO (x
) == STACK_POINTER_REGNUM
)
274 /* Subroutine of combine_stack_adjustments, called for each basic block. */
277 combine_stack_adjustments_for_block (basic_block bb
)
279 HOST_WIDE_INT last_sp_adjust
= 0;
280 rtx last_sp_set
= NULL_RTX
;
281 struct csa_memlist
*memlist
= NULL
;
283 struct record_stack_memrefs_data data
;
284 bool end_of_block
= false;
286 for (insn
= BB_HEAD (bb
); !end_of_block
; insn
= next
)
288 end_of_block
= insn
== BB_END (bb
);
289 next
= NEXT_INSN (insn
);
294 set
= single_set_for_csa (insn
);
297 rtx dest
= SET_DEST (set
);
298 rtx src
= SET_SRC (set
);
300 /* Find constant additions to the stack pointer. */
301 if (dest
== stack_pointer_rtx
302 && GET_CODE (src
) == PLUS
303 && XEXP (src
, 0) == stack_pointer_rtx
304 && GET_CODE (XEXP (src
, 1)) == CONST_INT
)
306 HOST_WIDE_INT this_adjust
= INTVAL (XEXP (src
, 1));
308 /* If we've not seen an adjustment previously, record
309 it now and continue. */
313 last_sp_adjust
= this_adjust
;
317 /* If not all recorded memrefs can be adjusted, or the
318 adjustment is now too large for a constant addition,
319 we cannot merge the two stack adjustments.
321 Also we need to be careful to not move stack pointer
322 such that we create stack accesses outside the allocated
323 area. We can combine an allocation into the first insn,
324 or a deallocation into the second insn. We can not
325 combine an allocation followed by a deallocation.
327 The only somewhat frequent occurrence of the later is when
328 a function allocates a stack frame but does not use it.
329 For this case, we would need to analyze rtl stream to be
330 sure that allocated area is really unused. This means not
331 only checking the memory references, but also all registers
332 or global memory references possibly containing a stack
335 Perhaps the best way to address this problem is to teach
336 gcc not to allocate stack for objects never used. */
338 /* Combine an allocation into the first instruction. */
339 if (STACK_GROWS_DOWNWARD
? this_adjust
<= 0 : this_adjust
>= 0)
341 if (try_apply_stack_adjustment (last_sp_set
, memlist
,
342 last_sp_adjust
+ this_adjust
,
347 last_sp_adjust
+= this_adjust
;
352 /* Otherwise we have a deallocation. Do not combine with
353 a previous allocation. Combine into the second insn. */
354 else if (STACK_GROWS_DOWNWARD
355 ? last_sp_adjust
>= 0 : last_sp_adjust
<= 0)
357 if (try_apply_stack_adjustment (insn
, memlist
,
358 last_sp_adjust
+ this_adjust
,
362 delete_insn (last_sp_set
);
364 last_sp_adjust
+= this_adjust
;
365 free_csa_memlist (memlist
);
371 /* Combination failed. Restart processing from here. If
372 deallocation+allocation conspired to cancel, we can
373 delete the old deallocation insn. */
374 if (last_sp_set
&& last_sp_adjust
== 0)
376 free_csa_memlist (memlist
);
379 last_sp_adjust
= this_adjust
;
383 /* Find a predecrement of exactly the previous adjustment and
384 turn it into a direct store. Obviously we can't do this if
385 there were any intervening uses of the stack pointer. */
388 && ((GET_CODE (XEXP (dest
, 0)) == PRE_DEC
390 == (HOST_WIDE_INT
) GET_MODE_SIZE (GET_MODE (dest
))))
391 || (GET_CODE (XEXP (dest
, 0)) == PRE_MODIFY
392 && GET_CODE (XEXP (XEXP (dest
, 0), 1)) == PLUS
393 && XEXP (XEXP (XEXP (dest
, 0), 1), 0) == stack_pointer_rtx
394 && (GET_CODE (XEXP (XEXP (XEXP (dest
, 0), 1), 1))
396 && (INTVAL (XEXP (XEXP (XEXP (dest
, 0), 1), 1))
397 == -last_sp_adjust
)))
398 && XEXP (XEXP (dest
, 0), 0) == stack_pointer_rtx
399 && ! reg_mentioned_p (stack_pointer_rtx
, src
)
400 && memory_address_p (GET_MODE (dest
), stack_pointer_rtx
)
401 && validate_change (insn
, &SET_DEST (set
),
402 replace_equiv_address (dest
,
406 delete_insn (last_sp_set
);
407 free_csa_memlist (memlist
);
409 last_sp_set
= NULL_RTX
;
416 data
.memlist
= memlist
;
417 if (!CALL_P (insn
) && last_sp_set
418 && !for_each_rtx (&PATTERN (insn
), record_stack_memrefs
, &data
))
420 memlist
= data
.memlist
;
423 memlist
= data
.memlist
;
425 /* Otherwise, we were not able to process the instruction.
426 Do not continue collecting data across such a one. */
429 || reg_mentioned_p (stack_pointer_rtx
, PATTERN (insn
))))
431 if (last_sp_set
&& last_sp_adjust
== 0)
432 delete_insn (last_sp_set
);
433 free_csa_memlist (memlist
);
435 last_sp_set
= NULL_RTX
;
440 if (last_sp_set
&& last_sp_adjust
== 0)
441 delete_insn (last_sp_set
);
444 free_csa_memlist (memlist
);
449 gate_handle_stack_adjustments (void)
451 return (optimize
> 0);
455 rest_of_handle_stack_adjustments (void)
457 life_analysis (PROP_POSTRELOAD
);
458 cleanup_cfg (CLEANUP_EXPENSIVE
| CLEANUP_UPDATE_LIFE
459 | (flag_crossjumping
? CLEANUP_CROSSJUMP
: 0));
461 /* This is kind of a heuristic. We need to run combine_stack_adjustments
462 even for machines with possibly nonzero RETURN_POPS_ARGS
463 and ACCUMULATE_OUTGOING_ARGS. We expect that only ports having
464 push instructions will have popping returns. */
465 #ifndef PUSH_ROUNDING
466 if (!ACCUMULATE_OUTGOING_ARGS
)
468 combine_stack_adjustments ();
472 struct tree_opt_pass pass_stack_adjustments
=
475 gate_handle_stack_adjustments
, /* gate */
476 rest_of_handle_stack_adjustments
, /* execute */
479 0, /* static_pass_number */
481 0, /* properties_required */
482 0, /* properties_provided */
483 0, /* properties_destroyed */
484 0, /* todo_flags_start */
486 TODO_ggc_collect
, /* todo_flags_finish */