1*e4b17023SJohn Marino /* Discover if the stack pointer is modified in a function.
2*e4b17023SJohn Marino Copyright (C) 2007, 2008, 2009
3*e4b17023SJohn Marino Free Software Foundation, Inc.
4*e4b17023SJohn Marino
5*e4b17023SJohn Marino This file is part of GCC.
6*e4b17023SJohn Marino
7*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
8*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
9*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
10*e4b17023SJohn Marino version.
11*e4b17023SJohn Marino
12*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15*e4b17023SJohn Marino for more details.
16*e4b17023SJohn Marino
17*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
18*e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see
19*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
20*e4b17023SJohn Marino
21*e4b17023SJohn Marino #include "config.h"
22*e4b17023SJohn Marino #include "system.h"
23*e4b17023SJohn Marino #include "coretypes.h"
24*e4b17023SJohn Marino #include "tm.h"
25*e4b17023SJohn Marino #include "tree.h"
26*e4b17023SJohn Marino #include "rtl.h"
27*e4b17023SJohn Marino #include "regs.h"
28*e4b17023SJohn Marino #include "expr.h"
29*e4b17023SJohn Marino #include "tree-pass.h"
30*e4b17023SJohn Marino #include "basic-block.h"
31*e4b17023SJohn Marino #include "flags.h"
32*e4b17023SJohn Marino #include "output.h"
33*e4b17023SJohn Marino #include "df.h"
34*e4b17023SJohn Marino
35*e4b17023SJohn Marino /* Determine if the stack pointer is constant over the life of the function.
36*e4b17023SJohn Marino Only useful before prologues have been emitted. */
37*e4b17023SJohn Marino
38*e4b17023SJohn Marino static void
notice_stack_pointer_modification_1(rtx x,const_rtx pat ATTRIBUTE_UNUSED,void * data ATTRIBUTE_UNUSED)39*e4b17023SJohn Marino notice_stack_pointer_modification_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
40*e4b17023SJohn Marino void *data ATTRIBUTE_UNUSED)
41*e4b17023SJohn Marino {
42*e4b17023SJohn Marino if (x == stack_pointer_rtx
43*e4b17023SJohn Marino /* The stack pointer is only modified indirectly as the result
44*e4b17023SJohn Marino of a push until later. See the comments in rtl.texi
45*e4b17023SJohn Marino regarding Embedded Side-Effects on Addresses. */
46*e4b17023SJohn Marino || (MEM_P (x)
47*e4b17023SJohn Marino && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_AUTOINC
48*e4b17023SJohn Marino && XEXP (XEXP (x, 0), 0) == stack_pointer_rtx))
49*e4b17023SJohn Marino current_function_sp_is_unchanging = 0;
50*e4b17023SJohn Marino }
51*e4b17023SJohn Marino
52*e4b17023SJohn Marino static void
notice_stack_pointer_modification(void)53*e4b17023SJohn Marino notice_stack_pointer_modification (void)
54*e4b17023SJohn Marino {
55*e4b17023SJohn Marino basic_block bb;
56*e4b17023SJohn Marino rtx insn;
57*e4b17023SJohn Marino
58*e4b17023SJohn Marino /* Assume that the stack pointer is unchanging if alloca hasn't
59*e4b17023SJohn Marino been used. */
60*e4b17023SJohn Marino current_function_sp_is_unchanging = !cfun->calls_alloca;
61*e4b17023SJohn Marino if (current_function_sp_is_unchanging)
62*e4b17023SJohn Marino FOR_EACH_BB (bb)
63*e4b17023SJohn Marino FOR_BB_INSNS (bb, insn)
64*e4b17023SJohn Marino {
65*e4b17023SJohn Marino if (INSN_P (insn))
66*e4b17023SJohn Marino {
67*e4b17023SJohn Marino /* Check if insn modifies the stack pointer. */
68*e4b17023SJohn Marino note_stores (PATTERN (insn),
69*e4b17023SJohn Marino notice_stack_pointer_modification_1,
70*e4b17023SJohn Marino NULL);
71*e4b17023SJohn Marino if (! current_function_sp_is_unchanging)
72*e4b17023SJohn Marino return;
73*e4b17023SJohn Marino }
74*e4b17023SJohn Marino }
75*e4b17023SJohn Marino
76*e4b17023SJohn Marino /* The value coming into this pass was 0, and the exit block uses
77*e4b17023SJohn Marino are based on this. If the value is now 1, we need to redo the
78*e4b17023SJohn Marino exit block uses. */
79*e4b17023SJohn Marino if (df && current_function_sp_is_unchanging)
80*e4b17023SJohn Marino df_update_exit_block_uses ();
81*e4b17023SJohn Marino }
82*e4b17023SJohn Marino
83*e4b17023SJohn Marino /* Some targets can emit simpler epilogues if they know that sp was
84*e4b17023SJohn Marino not ever modified during the function. After reload, of course,
85*e4b17023SJohn Marino we've already emitted the epilogue so there's no sense searching. */
86*e4b17023SJohn Marino
87*e4b17023SJohn Marino static unsigned int
rest_of_handle_stack_ptr_mod(void)88*e4b17023SJohn Marino rest_of_handle_stack_ptr_mod (void)
89*e4b17023SJohn Marino {
90*e4b17023SJohn Marino notice_stack_pointer_modification ();
91*e4b17023SJohn Marino return 0;
92*e4b17023SJohn Marino }
93*e4b17023SJohn Marino
94*e4b17023SJohn Marino struct rtl_opt_pass pass_stack_ptr_mod =
95*e4b17023SJohn Marino {
96*e4b17023SJohn Marino {
97*e4b17023SJohn Marino RTL_PASS,
98*e4b17023SJohn Marino "*stack_ptr_mod", /* name */
99*e4b17023SJohn Marino NULL, /* gate */
100*e4b17023SJohn Marino rest_of_handle_stack_ptr_mod, /* execute */
101*e4b17023SJohn Marino NULL, /* sub */
102*e4b17023SJohn Marino NULL, /* next */
103*e4b17023SJohn Marino 0, /* static_pass_number */
104*e4b17023SJohn Marino TV_NONE, /* tv_id */
105*e4b17023SJohn Marino 0, /* properties_required */
106*e4b17023SJohn Marino 0, /* properties_provided */
107*e4b17023SJohn Marino 0, /* properties_destroyed */
108*e4b17023SJohn Marino 0, /* todo_flags_start */
109*e4b17023SJohn Marino 0 /* todo_flags_finish */
110*e4b17023SJohn Marino }
111*e4b17023SJohn Marino };
112