stack-ptr-mod.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Discover if the stack pointer is modified in a function.
  2. Copyright (C) 2007-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "hash-set.h"
  20. #include "machmode.h"
  21. #include "vec.h"
  22. #include "double-int.h"
  23. #include "input.h"
  24. #include "alias.h"
  25. #include "symtab.h"
  26. #include "wide-int.h"
  27. #include "inchash.h"
  28. #include "tree.h"
  29. #include "rtl.h"
  30. #include "regs.h"
  31. #include "hashtab.h"
  32. #include "hard-reg-set.h"
  33. #include "function.h"
  34. #include "flags.h"
  35. #include "statistics.h"
  36. #include "real.h"
  37. #include "fixed-value.h"
  38. #include "insn-config.h"
  39. #include "expmed.h"
  40. #include "dojump.h"
  41. #include "explow.h"
  42. #include "calls.h"
  43. #include "emit-rtl.h"
  44. #include "varasm.h"
  45. #include "stmt.h"
  46. #include "expr.h"
  47. #include "tree-pass.h"
  48. #include "predict.h"
  49. #include "dominance.h"
  50. #include "cfg.h"
  51. #include "basic-block.h"
  52. #include "output.h"
  53. #include "df.h"
  54. /* Determine if the stack pointer is constant over the life of the function.
  55. Only useful before prologues have been emitted. */
  56. static void
  57. notice_stack_pointer_modification_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
  58. void *data ATTRIBUTE_UNUSED)
  59. {
  60. if (x == stack_pointer_rtx
  61. /* The stack pointer is only modified indirectly as the result
  62. of a push until later. See the comments in rtl.texi
  63. regarding Embedded Side-Effects on Addresses. */
  64. || (MEM_P (x)
  65. && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_AUTOINC
  66. && XEXP (XEXP (x, 0), 0) == stack_pointer_rtx))
  67. crtl->sp_is_unchanging = 0;
  68. }
  69. /* Some targets can emit simpler epilogues if they know that sp was
  70. not ever modified during the function. After reload, of course,
  71. we've already emitted the epilogue so there's no sense searching. */
  72. namespace {
  73. const pass_data pass_data_stack_ptr_mod =
  74. {
  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 */
  84. };
  85. class pass_stack_ptr_mod : public rtl_opt_pass
  86. {
  87. public:
  88. pass_stack_ptr_mod (gcc::context *ctxt)
  89. : rtl_opt_pass (pass_data_stack_ptr_mod, ctxt)
  90. {}
  91. /* opt_pass methods: */
  92. virtual unsigned int execute (function *);
  93. }; // class pass_stack_ptr_mod
  94. unsigned int
  95. pass_stack_ptr_mod::execute (function *fun)
  96. {
  97. basic_block bb;
  98. rtx_insn *insn;
  99. /* Assume that the stack pointer is unchanging if alloca hasn't
  100. been used. */
  101. crtl->sp_is_unchanging = !fun->calls_alloca;
  102. if (crtl->sp_is_unchanging)
  103. FOR_EACH_BB_FN (bb, fun)
  104. FOR_BB_INSNS (bb, insn)
  105. {
  106. if (INSN_P (insn))
  107. {
  108. /* Check if insn modifies the stack pointer. */
  109. note_stores (PATTERN (insn),
  110. notice_stack_pointer_modification_1,
  111. NULL);
  112. if (! crtl->sp_is_unchanging)
  113. return 0;
  114. }
  115. }
  116. /* The value coming into this pass was 0, and the exit block uses
  117. are based on this. If the value is now 1, we need to redo the
  118. exit block uses. */
  119. if (df && crtl->sp_is_unchanging)
  120. df_update_exit_block_uses ();
  121. return 0;
  122. }
  123. } // anon namespace
  124. rtl_opt_pass *
  125. make_pass_stack_ptr_mod (gcc::context *ctxt)
  126. {
  127. return new pass_stack_ptr_mod (ctxt);
  128. }