You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

393 line
13KB

  1. /* IPA function body analysis.
  2. Copyright (C) 2003-2020 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 prefer specializing given
  21. function. They are represented 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 optimizations. */
  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. /* We know that the callee is hot by profile. */
  44. INLINE_HINT_known_hot = 128
  45. };
  46. typedef int ipa_hints;
  47. /* Simple description of whether a memory load or a condition refers to a load
  48. from an aggregate and if so, how and where from in the aggregate.
  49. Individual fields have the same meaning like fields with the same name in
  50. struct condition. */
  51. struct agg_position_info
  52. {
  53. HOST_WIDE_INT offset;
  54. bool agg_contents;
  55. bool by_ref;
  56. };
  57. /* Representation of function body size and time depending on the call
  58. context. We keep simple array of record, every containing of predicate
  59. and time/size to account. */
  60. class GTY(()) size_time_entry
  61. {
  62. public:
  63. /* Predicate for code to be executed. */
  64. predicate exec_predicate;
  65. /* Predicate for value to be constant and optimized out in a specialized copy.
  66. When deciding on specialization this makes it possible to see how much
  67. the executed code paths will simplify. */
  68. predicate nonconst_predicate;
  69. int size;
  70. sreal GTY((skip)) time;
  71. };
  72. /* Summary about function and stack frame sizes. We keep this info
  73. for inline clones and also for WPA streaming. For this reason this is not
  74. part of ipa_fn_summary which exists only for offline functions. */
  75. class ipa_size_summary
  76. {
  77. public:
  78. /* Estimated stack frame consumption by the function. */
  79. HOST_WIDE_INT estimated_self_stack_size;
  80. /* Size of the function body. */
  81. int self_size;
  82. /* Estimated size of the function after inlining. */
  83. int size;
  84. ipa_size_summary ()
  85. : estimated_self_stack_size (0), self_size (0), size (0)
  86. {
  87. }
  88. };
  89. /* Function inlining information. */
  90. class GTY(()) ipa_fn_summary
  91. {
  92. public:
  93. /* Keep all field empty so summary dumping works during its computation.
  94. This is useful for debugging. */
  95. ipa_fn_summary ()
  96. : min_size (0),
  97. inlinable (false), single_caller (false),
  98. fp_expressions (false), estimated_stack_size (false),
  99. time (0), conds (NULL),
  100. size_time_table (NULL), call_size_time_table (NULL), loop_iterations (NULL),
  101. loop_stride (NULL), growth (0), scc_no (0)
  102. {
  103. }
  104. /* Copy constructor. */
  105. ipa_fn_summary (const ipa_fn_summary &s)
  106. : min_size (s.min_size),
  107. inlinable (s.inlinable), single_caller (s.single_caller),
  108. fp_expressions (s.fp_expressions),
  109. estimated_stack_size (s.estimated_stack_size),
  110. time (s.time), conds (s.conds), size_time_table (s.size_time_table),
  111. call_size_time_table (NULL),
  112. loop_iterations (s.loop_iterations), loop_stride (s.loop_stride),
  113. growth (s.growth), scc_no (s.scc_no)
  114. {}
  115. /* Default constructor. */
  116. ~ipa_fn_summary ();
  117. /* Information about the function body itself. */
  118. /* Minimal size increase after inlining. */
  119. int min_size;
  120. /* False when there something makes inlining impossible (such as va_arg). */
  121. unsigned inlinable : 1;
  122. /* True wen there is only one caller of the function before small function
  123. inlining. */
  124. unsigned int single_caller : 1;
  125. /* True if function contains any floating point expressions. */
  126. unsigned int fp_expressions : 1;
  127. /* Information about function that will result after applying all the
  128. inline decisions present in the callgraph. Generally kept up to
  129. date only for functions that are not inline clones. */
  130. /* Estimated stack frame consumption by the function. */
  131. HOST_WIDE_INT estimated_stack_size;
  132. /* Estimated runtime of function after inlining. */
  133. sreal GTY((skip)) time;
  134. /* Conditional size/time information. The summaries are being
  135. merged during inlining. */
  136. conditions conds;
  137. /* Normal code is accounted in size_time_table, while calls are
  138. accounted in call_size_time_table. This is because calls
  139. are often adjusted by IPA optimizations and thus this summary
  140. is generated from call summary information when needed. */
  141. vec<size_time_entry, va_gc> *size_time_table;
  142. vec<size_time_entry, va_gc> *call_size_time_table;
  143. /* Predicate on when some loop in the function becomes to have known
  144. bounds. */
  145. predicate * GTY((skip)) loop_iterations;
  146. /* Predicate on when some loop in the function becomes to have known
  147. stride. */
  148. predicate * GTY((skip)) loop_stride;
  149. /* Estimated growth for inlining all copies of the function before start
  150. of small functions inlining.
  151. This value will get out of date as the callers are duplicated, but
  152. using up-to-date value in the badness metric mean a lot of extra
  153. expenses. */
  154. int growth;
  155. /* Number of SCC on the beginning of inlining process. */
  156. int scc_no;
  157. /* Record time and size under given predicates. */
  158. void account_size_time (int, sreal, const predicate &, const predicate &,
  159. bool call = false);
  160. /* We keep values scaled up, so fractional sizes can be accounted. */
  161. static const int size_scale = 2;
  162. /* Maximal size of size_time_table before we start to be conservative. */
  163. static const int max_size_time_table_size = 256;
  164. };
  165. class GTY((user)) ipa_fn_summary_t:
  166. public fast_function_summary <ipa_fn_summary *, va_gc>
  167. {
  168. public:
  169. ipa_fn_summary_t (symbol_table *symtab):
  170. fast_function_summary <ipa_fn_summary *, va_gc> (symtab) {}
  171. static ipa_fn_summary_t *create_ggc (symbol_table *symtab)
  172. {
  173. class ipa_fn_summary_t *summary
  174. = new (ggc_alloc_no_dtor<ipa_fn_summary_t> ()) ipa_fn_summary_t (symtab);
  175. summary->disable_insertion_hook ();
  176. return summary;
  177. }
  178. /* Remove ipa_fn_summary for all callees of NODE. */
  179. void remove_callees (cgraph_node *node);
  180. virtual void insert (cgraph_node *, ipa_fn_summary *);
  181. virtual void remove (cgraph_node *node, ipa_fn_summary *)
  182. {
  183. remove_callees (node);
  184. }
  185. virtual void duplicate (cgraph_node *src, cgraph_node *dst,
  186. ipa_fn_summary *src_data, ipa_fn_summary *dst_data);
  187. };
  188. extern GTY(()) fast_function_summary <ipa_fn_summary *, va_gc>
  189. *ipa_fn_summaries;
  190. class ipa_size_summary_t:
  191. public fast_function_summary <ipa_size_summary *, va_heap>
  192. {
  193. public:
  194. ipa_size_summary_t (symbol_table *symtab):
  195. fast_function_summary <ipa_size_summary *, va_heap> (symtab)
  196. {
  197. disable_insertion_hook ();
  198. }
  199. virtual void duplicate (cgraph_node *, cgraph_node *,
  200. ipa_size_summary *src_data,
  201. ipa_size_summary *dst_data)
  202. {
  203. *dst_data = *src_data;
  204. }
  205. };
  206. extern fast_function_summary <ipa_size_summary *, va_heap>
  207. *ipa_size_summaries;
  208. /* Information kept about callgraph edges. */
  209. class ipa_call_summary
  210. {
  211. public:
  212. /* Keep all field empty so summary dumping works during its computation.
  213. This is useful for debugging. */
  214. ipa_call_summary ()
  215. : predicate (NULL), param (vNULL), call_stmt_size (0), call_stmt_time (0),
  216. loop_depth (0), is_return_callee_uncaptured (false)
  217. {
  218. }
  219. /* Copy constructor. */
  220. ipa_call_summary (const ipa_call_summary &s):
  221. predicate (s.predicate), param (s.param), call_stmt_size (s.call_stmt_size),
  222. call_stmt_time (s.call_stmt_time), loop_depth (s.loop_depth),
  223. is_return_callee_uncaptured (s.is_return_callee_uncaptured)
  224. {
  225. }
  226. /* Default destructor. */
  227. ~ipa_call_summary ();
  228. class predicate *predicate;
  229. /* Vector indexed by parameters. */
  230. vec<inline_param_summary> param;
  231. /* Estimated size and time of the call statement. */
  232. int call_stmt_size;
  233. int call_stmt_time;
  234. /* Depth of loop nest, 0 means no nesting. */
  235. unsigned int loop_depth;
  236. /* Indicates whether the caller returns the value of it's callee. */
  237. bool is_return_callee_uncaptured;
  238. };
  239. class ipa_call_summary_t: public fast_call_summary <ipa_call_summary *, va_heap>
  240. {
  241. public:
  242. ipa_call_summary_t (symbol_table *symtab):
  243. fast_call_summary <ipa_call_summary *, va_heap> (symtab) {}
  244. /* Hook that is called by summary when an edge is duplicated. */
  245. virtual void duplicate (cgraph_edge *src, cgraph_edge *dst,
  246. ipa_call_summary *src_data,
  247. ipa_call_summary *dst_data);
  248. };
  249. /* This object describe a context of call. That is a summary of known
  250. information about its parameters. Main purpose of this context is
  251. to give more realistic estimations of function runtime, size and
  252. inline hints. */
  253. class ipa_call_context
  254. {
  255. public:
  256. ipa_call_context (cgraph_node *node,
  257. clause_t possible_truths,
  258. clause_t nonspec_possible_truths,
  259. vec<tree> known_vals,
  260. vec<ipa_polymorphic_call_context> known_contexts,
  261. vec<ipa_agg_value_set> known_aggs,
  262. vec<inline_param_summary> m_inline_param_summary);
  263. ipa_call_context ()
  264. : m_node(NULL)
  265. {
  266. }
  267. void estimate_size_and_time (int *ret_size, int *ret_min_size,
  268. sreal *ret_time,
  269. sreal *ret_nonspecialized_time,
  270. ipa_hints *ret_hints);
  271. void duplicate_from (const ipa_call_context &ctx);
  272. void release (bool all = false);
  273. bool equal_to (const ipa_call_context &);
  274. bool exists_p ()
  275. {
  276. return m_node != NULL;
  277. }
  278. private:
  279. /* Called function. */
  280. cgraph_node *m_node;
  281. /* Clause describing what predicate conditionals can be satisfied
  282. in this context if function is inlined/specialized. */
  283. clause_t m_possible_truths;
  284. /* Clause describing what predicate conditionals can be satisfied
  285. in this context if function is kept offline. */
  286. clause_t m_nonspec_possible_truths;
  287. /* Inline summary maintains info about change probabilities. */
  288. vec<inline_param_summary> m_inline_param_summary;
  289. /* The following is used only to resolve indirect calls. */
  290. /* Vector describing known values of parameters. */
  291. vec<tree> m_known_vals;
  292. /* Vector describing known polymorphic call contexts. */
  293. vec<ipa_polymorphic_call_context> m_known_contexts;
  294. /* Vector describing known aggregate values. */
  295. vec<ipa_agg_value_set> m_known_aggs;
  296. };
  297. extern fast_call_summary <ipa_call_summary *, va_heap> *ipa_call_summaries;
  298. /* In ipa-fnsummary.c */
  299. void ipa_debug_fn_summary (struct cgraph_node *);
  300. void ipa_dump_fn_summaries (FILE *f);
  301. void ipa_dump_fn_summary (FILE *f, struct cgraph_node *node);
  302. void ipa_dump_hints (FILE *f, ipa_hints);
  303. void ipa_free_fn_summary (void);
  304. void ipa_free_size_summary (void);
  305. void inline_analyze_function (struct cgraph_node *node);
  306. void estimate_ipcp_clone_size_and_time (struct cgraph_node *,
  307. vec<tree>,
  308. vec<ipa_polymorphic_call_context>,
  309. vec<ipa_agg_value_set>,
  310. int *, sreal *, sreal *,
  311. ipa_hints *);
  312. void ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge);
  313. void ipa_update_overall_fn_summary (struct cgraph_node *node, bool reset = true);
  314. void compute_fn_summary (struct cgraph_node *, bool);
  315. void evaluate_properties_for_edge (struct cgraph_edge *e,
  316. bool inline_p,
  317. clause_t *clause_ptr,
  318. clause_t *nonspec_clause_ptr,
  319. vec<tree> *known_vals_ptr,
  320. vec<ipa_polymorphic_call_context>
  321. *known_contexts_ptr,
  322. vec<ipa_agg_value_set> *);
  323. void ipa_fnsummary_c_finalize (void);
  324. HOST_WIDE_INT ipa_get_stack_frame_offset (struct cgraph_node *node);
  325. void ipa_remove_from_growth_caches (struct cgraph_edge *edge);
  326. /* Return true if EDGE is a cross module call. */
  327. static inline bool
  328. cross_module_call_p (struct cgraph_edge *edge)
  329. {
  330. /* Here we do not want to walk to alias target becuase ICF may create
  331. cross-unit aliases. */
  332. if (edge->caller->unit_id == edge->callee->unit_id)
  333. return false;
  334. /* If the call is to a (former) comdat function or s symbol with mutiple
  335. extern inline definitions then treat is as in-module call. */
  336. if (edge->callee->merged_extern_inline || edge->callee->merged_comdat
  337. || DECL_COMDAT (edge->callee->decl))
  338. return false;
  339. return true;
  340. }
  341. #endif /* GCC_IPA_FNSUMMARY_H */