ipa-fnsummary.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* IPA function body analysis.
  2. Copyright (C) 2003-2019 Free Software Foundation, Inc.
  3. Contributed by Jan Hubicka
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef GCC_IPA_SUMMARY_H
  17. #define GCC_IPA_SUMMARY_H
  18. #include "sreal.h"
  19. #include "ipa-predicate.h"
  20. /* Hints are reasons why IPA heuristics should preffer specializing given
  21. function. They are represtented as bitmap of the following values. */
  22. enum ipa_hints_vals {
  23. /* When specialization turns indirect call into a direct call,
  24. it is good idea to do so. */
  25. INLINE_HINT_indirect_call = 1,
  26. /* Inlining may make loop iterations or loop stride known. It is good idea
  27. to do so because it enables loop optimizatoins. */
  28. INLINE_HINT_loop_iterations = 2,
  29. INLINE_HINT_loop_stride = 4,
  30. /* Inlining within same strongly connected component of callgraph is often
  31. a loss due to increased stack frame usage and prologue setup costs. */
  32. INLINE_HINT_same_scc = 8,
  33. /* Inlining functions in strongly connected component is not such a great
  34. win. */
  35. INLINE_HINT_in_scc = 16,
  36. /* If function is declared inline by user, it may be good idea to inline
  37. it. Set by simple_edge_hints in ipa-inline-analysis.c. */
  38. INLINE_HINT_declared_inline = 32,
  39. /* Programs are usually still organized for non-LTO compilation and thus
  40. if functions are in different modules, inlining may not be so important.
  41. Set by simple_edge_hints in ipa-inline-analysis.c. */
  42. INLINE_HINT_cross_module = 64,
  43. /* If array indexes of loads/stores become known there may be room for
  44. further optimization. */
  45. INLINE_HINT_array_index = 128,
  46. /* We know that the callee is hot by profile. */
  47. INLINE_HINT_known_hot = 256
  48. };
  49. typedef int ipa_hints;
  50. /* Simple description of whether a memory load or a condition refers to a load
  51. from an aggregate and if so, how and where from in the aggregate.
  52. Individual fields have the same meaning like fields with the same name in
  53. struct condition. */
  54. struct agg_position_info
  55. {
  56. HOST_WIDE_INT offset;
  57. bool agg_contents;
  58. bool by_ref;
  59. };
  60. /* Representation of function body size and time depending on the call
  61. context. We keep simple array of record, every containing of predicate
  62. and time/size to account. */
  63. struct GTY(()) size_time_entry
  64. {
  65. /* Predicate for code to be executed. */
  66. predicate exec_predicate;
  67. /* Predicate for value to be constant and optimized out in a specialized copy.
  68. When deciding on specialization this makes it possible to see how much
  69. the executed code paths will simplify. */
  70. predicate nonconst_predicate;
  71. int size;
  72. sreal GTY((skip)) time;
  73. };
  74. /* Function inlining information. */
  75. struct GTY(()) ipa_fn_summary
  76. {
  77. /* Keep all field empty so summary dumping works during its computation.
  78. This is useful for debugging. */
  79. ipa_fn_summary ()
  80. : estimated_self_stack_size (0), self_size (0), min_size (0),
  81. inlinable (false), single_caller (false),
  82. fp_expressions (false), estimated_stack_size (false),
  83. stack_frame_offset (false), time (0), size (0), conds (NULL),
  84. size_time_table (NULL), loop_iterations (NULL), loop_stride (NULL),
  85. array_index (NULL), growth (0), scc_no (0)
  86. {
  87. }
  88. /* Copy constructor. */
  89. ipa_fn_summary (const ipa_fn_summary &s)
  90. : estimated_self_stack_size (s.estimated_self_stack_size),
  91. self_size (s.self_size), min_size (s.min_size),
  92. inlinable (s.inlinable), single_caller (s.single_caller),
  93. fp_expressions (s.fp_expressions),
  94. estimated_stack_size (s.estimated_stack_size),
  95. stack_frame_offset (s.stack_frame_offset), time (s.time), size (s.size),
  96. conds (s.conds), size_time_table (s.size_time_table),
  97. loop_iterations (s.loop_iterations), loop_stride (s.loop_stride),
  98. array_index (s.array_index), growth (s.growth), scc_no (s.scc_no)
  99. {}
  100. /* Default constructor. */
  101. ~ipa_fn_summary ();
  102. /* Information about the function body itself. */
  103. /* Estimated stack frame consumption by the function. */
  104. HOST_WIDE_INT estimated_self_stack_size;
  105. /* Size of the function body. */
  106. int self_size;
  107. /* Minimal size increase after inlining. */
  108. int min_size;
  109. /* False when there something makes inlining impossible (such as va_arg). */
  110. unsigned inlinable : 1;
  111. /* True wen there is only one caller of the function before small function
  112. inlining. */
  113. unsigned int single_caller : 1;
  114. /* True if function contains any floating point expressions. */
  115. unsigned int fp_expressions : 1;
  116. /* Information about function that will result after applying all the
  117. inline decisions present in the callgraph. Generally kept up to
  118. date only for functions that are not inline clones. */
  119. /* Estimated stack frame consumption by the function. */
  120. HOST_WIDE_INT estimated_stack_size;
  121. /* Expected offset of the stack frame of function. */
  122. HOST_WIDE_INT stack_frame_offset;
  123. /* Estimated size of the function after inlining. */
  124. sreal GTY((skip)) time;
  125. int size;
  126. /* Conditional size/time information. The summaries are being
  127. merged during inlining. */
  128. conditions conds;
  129. vec<size_time_entry, va_gc> *size_time_table;
  130. /* Predicate on when some loop in the function becomes to have known
  131. bounds. */
  132. predicate * GTY((skip)) loop_iterations;
  133. /* Predicate on when some loop in the function becomes to have known
  134. stride. */
  135. predicate * GTY((skip)) loop_stride;
  136. /* Predicate on when some array indexes become constants. */
  137. predicate * GTY((skip)) array_index;
  138. /* Estimated growth for inlining all copies of the function before start
  139. of small functions inlining.
  140. This value will get out of date as the callers are duplicated, but
  141. using up-to-date value in the badness metric mean a lot of extra
  142. expenses. */
  143. int growth;
  144. /* Number of SCC on the beginning of inlining process. */
  145. int scc_no;
  146. /* Record time and size under given predicates. */
  147. void account_size_time (int, sreal, const predicate &, const predicate &);
  148. /* We keep values scaled up, so fractional sizes can be accounted. */
  149. static const int size_scale = 2;
  150. };
  151. class GTY((user)) ipa_fn_summary_t:
  152. public fast_function_summary <ipa_fn_summary *, va_gc>
  153. {
  154. public:
  155. ipa_fn_summary_t (symbol_table *symtab):
  156. fast_function_summary <ipa_fn_summary *, va_gc> (symtab) {}
  157. static ipa_fn_summary_t *create_ggc (symbol_table *symtab)
  158. {
  159. struct ipa_fn_summary_t *summary = new (ggc_alloc <ipa_fn_summary_t> ())
  160. ipa_fn_summary_t (symtab);
  161. summary->disable_insertion_hook ();
  162. return summary;
  163. }
  164. /* Remove ipa_fn_summary for all callees of NODE. */
  165. void remove_callees (cgraph_node *node);
  166. virtual void insert (cgraph_node *, ipa_fn_summary *);
  167. virtual void remove (cgraph_node *node, ipa_fn_summary *)
  168. {
  169. remove_callees (node);
  170. }
  171. virtual void duplicate (cgraph_node *src, cgraph_node *dst,
  172. ipa_fn_summary *src_data, ipa_fn_summary *dst_data);
  173. };
  174. extern GTY(()) fast_function_summary <ipa_fn_summary *, va_gc>
  175. *ipa_fn_summaries;
  176. /* Information kept about callgraph edges. */
  177. struct ipa_call_summary
  178. {
  179. /* Keep all field empty so summary dumping works during its computation.
  180. This is useful for debugging. */
  181. ipa_call_summary ()
  182. : predicate (NULL), param (vNULL), call_stmt_size (0), call_stmt_time (0),
  183. loop_depth (0), is_return_callee_uncaptured (false)
  184. {
  185. }
  186. /* Copy constructor. */
  187. ipa_call_summary (const ipa_call_summary &s):
  188. predicate (s.predicate), param (s.param), call_stmt_size (s.call_stmt_size),
  189. call_stmt_time (s.call_stmt_time), loop_depth (s.loop_depth),
  190. is_return_callee_uncaptured (s.is_return_callee_uncaptured)
  191. {
  192. }
  193. /* Default destructor. */
  194. ~ipa_call_summary ();
  195. class predicate *predicate;
  196. /* Vector indexed by parameters. */
  197. vec<inline_param_summary> param;
  198. /* Estimated size and time of the call statement. */
  199. int call_stmt_size;
  200. int call_stmt_time;
  201. /* Depth of loop nest, 0 means no nesting. */
  202. unsigned int loop_depth;
  203. /* Indicates whether the caller returns the value of it's callee. */
  204. bool is_return_callee_uncaptured;
  205. };
  206. class ipa_call_summary_t: public fast_call_summary <ipa_call_summary *, va_heap>
  207. {
  208. public:
  209. ipa_call_summary_t (symbol_table *symtab):
  210. fast_call_summary <ipa_call_summary *, va_heap> (symtab) {}
  211. /* Hook that is called by summary when an edge is duplicated. */
  212. virtual void duplicate (cgraph_edge *src, cgraph_edge *dst,
  213. ipa_call_summary *src_data,
  214. ipa_call_summary *dst_data);
  215. };
  216. extern fast_call_summary <ipa_call_summary *, va_heap> *ipa_call_summaries;
  217. /* In ipa-fnsummary.c */
  218. void ipa_debug_fn_summary (struct cgraph_node *);
  219. void ipa_dump_fn_summaries (FILE *f);
  220. void ipa_dump_fn_summary (FILE *f, struct cgraph_node *node);
  221. void ipa_dump_hints (FILE *f, ipa_hints);
  222. void ipa_free_fn_summary (void);
  223. void inline_analyze_function (struct cgraph_node *node);
  224. void estimate_ipcp_clone_size_and_time (struct cgraph_node *,
  225. vec<tree>,
  226. vec<ipa_polymorphic_call_context>,
  227. vec<ipa_agg_jump_function_p>,
  228. int *, sreal *, sreal *,
  229. ipa_hints *);
  230. void ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge);
  231. void ipa_update_overall_fn_summary (struct cgraph_node *node);
  232. void compute_fn_summary (struct cgraph_node *, bool);
  233. void evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
  234. clause_t *clause_ptr,
  235. clause_t *nonspec_clause_ptr,
  236. vec<tree> *known_vals_ptr,
  237. vec<ipa_polymorphic_call_context>
  238. *known_contexts_ptr,
  239. vec<ipa_agg_jump_function_p> *);
  240. void estimate_node_size_and_time (struct cgraph_node *node,
  241. clause_t possible_truths,
  242. clause_t nonspec_possible_truths,
  243. vec<tree> known_vals,
  244. vec<ipa_polymorphic_call_context>,
  245. vec<ipa_agg_jump_function_p> known_aggs,
  246. int *ret_size, int *ret_min_size,
  247. sreal *ret_time,
  248. sreal *ret_nonspecialized_time,
  249. ipa_hints *ret_hints,
  250. vec<inline_param_summary>
  251. inline_param_summary);
  252. void ipa_fnsummary_c_finalize (void);
  253. #endif /* GCC_IPA_FNSUMMARY_H */