stack-ptr-mod.c 3.45 KB
Newer Older
H.J. Lu committed
1
/* Discover if the stack pointer is modified in a function.
2
   Copyright (C) 2007-2014 Free Software Foundation, Inc.
3 4 5 6 7

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
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 10 11 12 13 14 15 16
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
17 18
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "rtl.h"
#include "regs.h"
#include "expr.h"
#include "tree-pass.h"
#include "basic-block.h"
#include "flags.h"
#include "output.h"
#include "df.h"

/* Determine if the stack pointer is constant over the life of the function.
   Only useful before prologues have been emitted.  */

static void
38
notice_stack_pointer_modification_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
39 40 41 42 43 44 45 46 47
				     void *data ATTRIBUTE_UNUSED)
{
  if (x == stack_pointer_rtx
      /* The stack pointer is only modified indirectly as the result
	 of a push until later.  See the comments in rtl.texi
	 regarding Embedded Side-Effects on Addresses.  */
      || (MEM_P (x)
	  && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_AUTOINC
	  && XEXP (XEXP (x, 0), 0) == stack_pointer_rtx))
48
    crtl->sp_is_unchanging = 0;
49 50 51 52 53 54 55 56 57 58
}

static void
notice_stack_pointer_modification (void)
{
  basic_block bb;
  rtx insn;

  /* Assume that the stack pointer is unchanging if alloca hasn't
     been used.  */
59 60
  crtl->sp_is_unchanging = !cfun->calls_alloca;
  if (crtl->sp_is_unchanging)
61
    FOR_EACH_BB_FN (bb, cfun)
62 63 64 65 66 67 68 69
      FOR_BB_INSNS (bb, insn)
        {
	  if (INSN_P (insn))
	    {
	      /* Check if insn modifies the stack pointer.  */
	      note_stores (PATTERN (insn),
			   notice_stack_pointer_modification_1,
			   NULL);
70
	      if (! crtl->sp_is_unchanging)
71 72 73 74 75 76 77
		return;
	    }
	}

  /* The value coming into this pass was 0, and the exit block uses
     are based on this.  If the value is now 1, we need to redo the
     exit block uses.  */
78
  if (df && crtl->sp_is_unchanging)
79 80 81 82 83 84 85 86 87 88 89 90 91 92
    df_update_exit_block_uses ();
}

  /* Some targets can emit simpler epilogues if they know that sp was
     not ever modified during the function.  After reload, of course,
     we've already emitted the epilogue so there's no sense searching.  */

static unsigned int
rest_of_handle_stack_ptr_mod (void)
{
  notice_stack_pointer_modification ();
  return 0;
}

93 94 95
namespace {

const pass_data pass_data_stack_ptr_mod =
96
{
97 98 99 100 101 102 103 104 105 106 107
  RTL_PASS, /* type */
  "*stack_ptr_mod", /* name */
  OPTGROUP_NONE, /* optinfo_flags */
  false, /* has_gate */
  true, /* has_execute */
  TV_NONE, /* tv_id */
  0, /* properties_required */
  0, /* properties_provided */
  0, /* properties_destroyed */
  0, /* todo_flags_start */
  0, /* todo_flags_finish */
108
};
109 110 111 112

class pass_stack_ptr_mod : public rtl_opt_pass
{
public:
113 114
  pass_stack_ptr_mod (gcc::context *ctxt)
    : rtl_opt_pass (pass_data_stack_ptr_mod, ctxt)
115 116 117 118 119 120 121 122 123 124 125 126 127 128
  {}

  /* opt_pass methods: */
  unsigned int execute () { return rest_of_handle_stack_ptr_mod (); }

}; // class pass_stack_ptr_mod

} // anon namespace

rtl_opt_pass *
make_pass_stack_ptr_mod (gcc::context *ctxt)
{
  return new pass_stack_ptr_mod (ctxt);
}