2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / stack-ptr-mod.c
blob9b5df397461597c27a408e1922c0b55164689092
1 /* Discover if the stack pointer is modified in a function.
2 Copyright (C) 2007-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
9 version.
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
14 for more details.
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 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "input.h"
25 #include "alias.h"
26 #include "symtab.h"
27 #include "tree.h"
28 #include "rtl.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "function.h"
32 #include "flags.h"
33 #include "insn-config.h"
34 #include "expmed.h"
35 #include "dojump.h"
36 #include "explow.h"
37 #include "calls.h"
38 #include "emit-rtl.h"
39 #include "varasm.h"
40 #include "stmt.h"
41 #include "expr.h"
42 #include "tree-pass.h"
43 #include "predict.h"
44 #include "dominance.h"
45 #include "cfg.h"
46 #include "basic-block.h"
47 #include "output.h"
48 #include "df.h"
50 /* Determine if the stack pointer is constant over the life of the function.
51 Only useful before prologues have been emitted. */
53 static void
54 notice_stack_pointer_modification_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
55 void *data ATTRIBUTE_UNUSED)
57 if (x == stack_pointer_rtx
58 /* The stack pointer is only modified indirectly as the result
59 of a push until later. See the comments in rtl.texi
60 regarding Embedded Side-Effects on Addresses. */
61 || (MEM_P (x)
62 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_AUTOINC
63 && XEXP (XEXP (x, 0), 0) == stack_pointer_rtx))
64 crtl->sp_is_unchanging = 0;
67 /* Some targets can emit simpler epilogues if they know that sp was
68 not ever modified during the function. After reload, of course,
69 we've already emitted the epilogue so there's no sense searching. */
71 namespace {
73 const pass_data pass_data_stack_ptr_mod =
75 RTL_PASS, /* type */
76 "*stack_ptr_mod", /* name */
77 OPTGROUP_NONE, /* optinfo_flags */
78 TV_NONE, /* tv_id */
79 0, /* properties_required */
80 0, /* properties_provided */
81 0, /* properties_destroyed */
82 0, /* todo_flags_start */
83 0, /* todo_flags_finish */
86 class pass_stack_ptr_mod : public rtl_opt_pass
88 public:
89 pass_stack_ptr_mod (gcc::context *ctxt)
90 : rtl_opt_pass (pass_data_stack_ptr_mod, ctxt)
93 /* opt_pass methods: */
94 virtual unsigned int execute (function *);
96 }; // class pass_stack_ptr_mod
98 unsigned int
99 pass_stack_ptr_mod::execute (function *fun)
101 basic_block bb;
102 rtx_insn *insn;
104 /* Assume that the stack pointer is unchanging if alloca hasn't
105 been used. */
106 crtl->sp_is_unchanging = !fun->calls_alloca;
107 if (crtl->sp_is_unchanging)
108 FOR_EACH_BB_FN (bb, fun)
109 FOR_BB_INSNS (bb, insn)
111 if (INSN_P (insn))
113 /* Check if insn modifies the stack pointer. */
114 note_stores (PATTERN (insn),
115 notice_stack_pointer_modification_1,
116 NULL);
117 if (! crtl->sp_is_unchanging)
118 return 0;
122 /* The value coming into this pass was 0, and the exit block uses
123 are based on this. If the value is now 1, we need to redo the
124 exit block uses. */
125 if (df && crtl->sp_is_unchanging)
126 df_update_exit_block_uses ();
128 return 0;
131 } // anon namespace
133 rtl_opt_pass *
134 make_pass_stack_ptr_mod (gcc::context *ctxt)
136 return new pass_stack_ptr_mod (ctxt);