ipa-inline.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /* Inlining decision heuristics.
  2. Copyright (C) 2003-2015 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_INLINE_H
  17. #define GCC_IPA_INLINE_H
  18. /* Representation of inline parameters that do depend on context function is
  19. inlined into (i.e. known constant values of function parameters.
  20. Conditions that are interesting for function body are collected into CONDS
  21. vector. They are of simple for function_param OP VAL, where VAL is
  22. IPA invariant. The conditions are then referred by predicates. */
  23. struct GTY(()) condition
  24. {
  25. /* If agg_contents is set, this is the offset from which the used data was
  26. loaded. */
  27. HOST_WIDE_INT offset;
  28. tree val;
  29. int operand_num;
  30. ENUM_BITFIELD(tree_code) code : 16;
  31. /* Set if the used data were loaded from an aggregate parameter or from
  32. data received by reference. */
  33. unsigned agg_contents : 1;
  34. /* If agg_contents is set, this differentiates between loads from data
  35. passed by reference and by value. */
  36. unsigned by_ref : 1;
  37. };
  38. /* Inline hints are reasons why inline heuristics should preffer inlining given
  39. function. They are represtented as bitmap of the following values. */
  40. enum inline_hints_vals {
  41. /* When inlining turns indirect call into a direct call,
  42. it is good idea to do so. */
  43. INLINE_HINT_indirect_call = 1,
  44. /* Inlining may make loop iterations or loop stride known. It is good idea
  45. to do so because it enables loop optimizatoins. */
  46. INLINE_HINT_loop_iterations = 2,
  47. INLINE_HINT_loop_stride = 4,
  48. /* Inlining within same strongly connected component of callgraph is often
  49. a loss due to increased stack frame usage and prologue setup costs. */
  50. INLINE_HINT_same_scc = 8,
  51. /* Inlining functions in strongly connected component is not such a great
  52. win. */
  53. INLINE_HINT_in_scc = 16,
  54. /* If function is declared inline by user, it may be good idea to inline
  55. it. */
  56. INLINE_HINT_declared_inline = 32,
  57. /* Programs are usually still organized for non-LTO compilation and thus
  58. if functions are in different modules, inlining may not be so important.
  59. */
  60. INLINE_HINT_cross_module = 64,
  61. /* If array indexes of loads/stores become known there may be room for
  62. further optimization. */
  63. INLINE_HINT_array_index = 128,
  64. /* We know that the callee is hot by profile. */
  65. INLINE_HINT_known_hot = 256
  66. };
  67. typedef int inline_hints;
  68. typedef vec<condition, va_gc> *conditions;
  69. /* Representation of predicates i.e. formulas using conditions defined
  70. above. Predicates are simple logical formulas in conjunctive-disjunctive
  71. form.
  72. Predicate is array of clauses terminated by 0. Every clause must be true
  73. in order to make predicate true.
  74. Clauses are represented as bitmaps of conditions. One of conditions
  75. must be true in order for clause to be true. */
  76. #define MAX_CLAUSES 8
  77. typedef unsigned int clause_t;
  78. struct GTY(()) predicate
  79. {
  80. clause_t clause[MAX_CLAUSES + 1];
  81. };
  82. /* Represnetation of function body size and time depending on the inline
  83. context. We keep simple array of record, every containing of predicate
  84. and time/size to account.
  85. We keep values scaled up, so fractional sizes and times can be
  86. accounted. */
  87. #define INLINE_SIZE_SCALE 2
  88. #define INLINE_TIME_SCALE (CGRAPH_FREQ_BASE * 2)
  89. struct GTY(()) size_time_entry
  90. {
  91. struct predicate predicate;
  92. int size;
  93. int time;
  94. };
  95. /* Function inlining information. */
  96. struct GTY(()) inline_summary
  97. {
  98. /* Information about the function body itself. */
  99. /* Estimated stack frame consumption by the function. */
  100. HOST_WIDE_INT estimated_self_stack_size;
  101. /* Size of the function body. */
  102. int self_size;
  103. /* Time of the function body. */
  104. int self_time;
  105. /* Minimal size increase after inlining. */
  106. int min_size;
  107. /* False when there something makes inlining impossible (such as va_arg). */
  108. unsigned inlinable : 1;
  109. /* True when function contains cilk spawn (and thus we can not inline
  110. into it). */
  111. unsigned contains_cilk_spawn : 1;
  112. /* True wen there is only one caller of the function before small function
  113. inlining. */
  114. unsigned int single_caller : 1;
  115. /* Information about function that will result after applying all the
  116. inline decisions present in the callgraph. Generally kept up to
  117. date only for functions that are not inline clones. */
  118. /* Estimated stack frame consumption by the function. */
  119. HOST_WIDE_INT estimated_stack_size;
  120. /* Expected offset of the stack frame of inlined function. */
  121. HOST_WIDE_INT stack_frame_offset;
  122. /* Estimated size of the function after inlining. */
  123. int time;
  124. int size;
  125. /* Conditional size/time information. The summaries are being
  126. merged during inlining. */
  127. conditions conds;
  128. vec<size_time_entry, va_gc> *entry;
  129. /* Predicate on when some loop in the function becomes to have known
  130. bounds. */
  131. struct predicate * GTY((skip)) loop_iterations;
  132. /* Predicate on when some loop in the function becomes to have known
  133. stride. */
  134. struct predicate * GTY((skip)) loop_stride;
  135. /* Predicate on when some array indexes become constants. */
  136. struct predicate * GTY((skip)) array_index;
  137. /* Estimated growth for inlining all copies of the function before start
  138. of small functions inlining.
  139. This value will get out of date as the callers are duplicated, but
  140. using up-to-date value in the badness metric mean a lot of extra
  141. expenses. */
  142. int growth;
  143. /* Number of SCC on the beginning of inlining process. */
  144. int scc_no;
  145. };
  146. class GTY((user)) inline_summary_t: public function_summary <inline_summary *>
  147. {
  148. public:
  149. inline_summary_t (symbol_table *symtab, bool ggc):
  150. function_summary <inline_summary *> (symtab, ggc) {}
  151. static inline_summary_t *create_ggc (symbol_table *symtab)
  152. {
  153. struct inline_summary_t *summary = new (ggc_cleared_alloc <inline_summary_t> ())
  154. inline_summary_t(symtab, true);
  155. summary->disable_insertion_hook ();
  156. return summary;
  157. }
  158. virtual void insert (cgraph_node *, inline_summary *);
  159. virtual void remove (cgraph_node *node, inline_summary *);
  160. virtual void duplicate (cgraph_node *src, cgraph_node *dst,
  161. inline_summary *src_data, inline_summary *dst_data);
  162. };
  163. extern GTY(()) function_summary <inline_summary *> *inline_summaries;
  164. /* Information kept about parameter of call site. */
  165. struct inline_param_summary
  166. {
  167. /* REG_BR_PROB_BASE based probability that parameter will change in between
  168. two invocation of the calls.
  169. I.e. loop invariant parameters
  170. REG_BR_PROB_BASE/estimated_iterations and regular
  171. parameters REG_BR_PROB_BASE.
  172. Value 0 is reserved for compile time invariants. */
  173. int change_prob;
  174. };
  175. /* Information kept about callgraph edges. */
  176. struct inline_edge_summary
  177. {
  178. /* Estimated size and time of the call statement. */
  179. int call_stmt_size;
  180. int call_stmt_time;
  181. /* Depth of loop nest, 0 means no nesting. */
  182. unsigned short int loop_depth;
  183. struct predicate *predicate;
  184. /* Array indexed by parameters.
  185. 0 means that parameter change all the time, REG_BR_PROB_BASE means
  186. that parameter is constant. */
  187. vec<inline_param_summary> param;
  188. };
  189. /* Need a typedef for inline_edge_summary because of inline function
  190. 'inline_edge_summary' below. */
  191. typedef struct inline_edge_summary inline_edge_summary_t;
  192. extern vec<inline_edge_summary_t> inline_edge_summary_vec;
  193. struct edge_growth_cache_entry
  194. {
  195. int time, size;
  196. inline_hints hints;
  197. };
  198. extern vec<edge_growth_cache_entry> edge_growth_cache;
  199. /* In ipa-inline-analysis.c */
  200. void debug_inline_summary (struct cgraph_node *);
  201. void dump_inline_summaries (FILE *f);
  202. void dump_inline_summary (FILE *f, struct cgraph_node *node);
  203. void dump_inline_hints (FILE *f, inline_hints);
  204. void inline_generate_summary (void);
  205. void inline_read_summary (void);
  206. void inline_write_summary (void);
  207. void inline_free_summary (void);
  208. void inline_analyze_function (struct cgraph_node *node);
  209. void initialize_inline_failed (struct cgraph_edge *);
  210. int estimate_time_after_inlining (struct cgraph_node *, struct cgraph_edge *);
  211. int estimate_size_after_inlining (struct cgraph_node *, struct cgraph_edge *);
  212. void estimate_ipcp_clone_size_and_time (struct cgraph_node *,
  213. vec<tree>,
  214. vec<ipa_polymorphic_call_context>,
  215. vec<ipa_agg_jump_function_p>,
  216. int *, int *, inline_hints *);
  217. int estimate_growth (struct cgraph_node *);
  218. bool growth_likely_positive (struct cgraph_node *, int);
  219. void inline_merge_summary (struct cgraph_edge *edge);
  220. void inline_update_overall_summary (struct cgraph_node *node);
  221. int do_estimate_edge_size (struct cgraph_edge *edge);
  222. int do_estimate_edge_time (struct cgraph_edge *edge);
  223. inline_hints do_estimate_edge_hints (struct cgraph_edge *edge);
  224. void initialize_growth_caches (void);
  225. void free_growth_caches (void);
  226. void compute_inline_parameters (struct cgraph_node *, bool);
  227. bool speculation_useful_p (struct cgraph_edge *e, bool anticipate_inlining);
  228. unsigned int early_inliner (function *fun);
  229. bool inline_account_function_p (struct cgraph_node *node);
  230. /* In ipa-inline-transform.c */
  231. bool inline_call (struct cgraph_edge *, bool, vec<cgraph_edge *> *, int *, bool,
  232. bool *callee_removed = NULL);
  233. unsigned int inline_transform (struct cgraph_node *);
  234. void clone_inlined_nodes (struct cgraph_edge *e, bool, bool, int *,
  235. int freq_scale);
  236. extern int ncalls_inlined;
  237. extern int nfunctions_inlined;
  238. static inline struct inline_edge_summary *
  239. inline_edge_summary (struct cgraph_edge *edge)
  240. {
  241. return &inline_edge_summary_vec[edge->uid];
  242. }
  243. /* Return estimated size of the inline sequence of EDGE. */
  244. static inline int
  245. estimate_edge_size (struct cgraph_edge *edge)
  246. {
  247. int ret;
  248. if ((int)edge_growth_cache.length () <= edge->uid
  249. || !(ret = edge_growth_cache[edge->uid].size))
  250. return do_estimate_edge_size (edge);
  251. return ret - (ret > 0);
  252. }
  253. /* Return estimated callee growth after inlining EDGE. */
  254. static inline int
  255. estimate_edge_growth (struct cgraph_edge *edge)
  256. {
  257. #ifdef ENABLE_CHECKING
  258. gcc_checking_assert (inline_edge_summary (edge)->call_stmt_size
  259. || !edge->callee->analyzed);
  260. #endif
  261. return (estimate_edge_size (edge)
  262. - inline_edge_summary (edge)->call_stmt_size);
  263. }
  264. /* Return estimated callee runtime increase after inlning
  265. EDGE. */
  266. static inline int
  267. estimate_edge_time (struct cgraph_edge *edge)
  268. {
  269. int ret;
  270. if ((int)edge_growth_cache.length () <= edge->uid
  271. || !(ret = edge_growth_cache[edge->uid].time))
  272. return do_estimate_edge_time (edge);
  273. return ret - (ret > 0);
  274. }
  275. /* Return estimated callee runtime increase after inlning
  276. EDGE. */
  277. static inline inline_hints
  278. estimate_edge_hints (struct cgraph_edge *edge)
  279. {
  280. inline_hints ret;
  281. if ((int)edge_growth_cache.length () <= edge->uid
  282. || !(ret = edge_growth_cache[edge->uid].hints))
  283. return do_estimate_edge_hints (edge);
  284. return ret - 1;
  285. }
  286. /* Reset cached value for EDGE. */
  287. static inline void
  288. reset_edge_growth_cache (struct cgraph_edge *edge)
  289. {
  290. if ((int)edge_growth_cache.length () > edge->uid)
  291. {
  292. struct edge_growth_cache_entry zero = {0, 0, 0};
  293. edge_growth_cache[edge->uid] = zero;
  294. }
  295. }
  296. #endif /* GCC_IPA_INLINE_H */