2013-01-08 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / stack-ptr-mod.c
blob0790d6adaf27823af29e6143eee0fbf544d09ae3
1 /* Discover if the stack pointer is modified in a function.
2 Copyright (C) 2007, 2008, 2009, 2012
3 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 3, or (at your option) any later
10 version.
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
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "regs.h"
28 #include "expr.h"
29 #include "tree-pass.h"
30 #include "basic-block.h"
31 #include "flags.h"
32 #include "output.h"
33 #include "df.h"
35 /* Determine if the stack pointer is constant over the life of the function.
36 Only useful before prologues have been emitted. */
38 static void
39 notice_stack_pointer_modification_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
40 void *data ATTRIBUTE_UNUSED)
42 if (x == stack_pointer_rtx
43 /* The stack pointer is only modified indirectly as the result
44 of a push until later. See the comments in rtl.texi
45 regarding Embedded Side-Effects on Addresses. */
46 || (MEM_P (x)
47 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_AUTOINC
48 && XEXP (XEXP (x, 0), 0) == stack_pointer_rtx))
49 crtl->sp_is_unchanging = 0;
52 static void
53 notice_stack_pointer_modification (void)
55 basic_block bb;
56 rtx insn;
58 /* Assume that the stack pointer is unchanging if alloca hasn't
59 been used. */
60 crtl->sp_is_unchanging = !cfun->calls_alloca;
61 if (crtl->sp_is_unchanging)
62 FOR_EACH_BB (bb)
63 FOR_BB_INSNS (bb, insn)
65 if (INSN_P (insn))
67 /* Check if insn modifies the stack pointer. */
68 note_stores (PATTERN (insn),
69 notice_stack_pointer_modification_1,
70 NULL);
71 if (! crtl->sp_is_unchanging)
72 return;
76 /* The value coming into this pass was 0, and the exit block uses
77 are based on this. If the value is now 1, we need to redo the
78 exit block uses. */
79 if (df && crtl->sp_is_unchanging)
80 df_update_exit_block_uses ();
83 /* Some targets can emit simpler epilogues if they know that sp was
84 not ever modified during the function. After reload, of course,
85 we've already emitted the epilogue so there's no sense searching. */
87 static unsigned int
88 rest_of_handle_stack_ptr_mod (void)
90 notice_stack_pointer_modification ();
91 return 0;
94 struct rtl_opt_pass pass_stack_ptr_mod =
97 RTL_PASS,
98 "*stack_ptr_mod", /* name */
99 OPTGROUP_NONE, /* optinfo_flags */
100 NULL, /* gate */
101 rest_of_handle_stack_ptr_mod, /* execute */
102 NULL, /* sub */
103 NULL, /* next */
104 0, /* static_pass_number */
105 TV_NONE, /* tv_id */
106 0, /* properties_required */
107 0, /* properties_provided */
108 0, /* properties_destroyed */
109 0, /* todo_flags_start */
110 0 /* todo_flags_finish */