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.

268 lines
8.4KB

  1. /* IPA predicates.
  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. /* Representation of inline parameters that do depend on context function is
  17. inlined into (i.e. known constant values of function parameters.
  18. Conditions that are interesting for function body are collected into CONDS
  19. vector. They are of simple as kind of a mathematical transformation on
  20. function parameter, T(function_param), in which the parameter occurs only
  21. once, and other operands are IPA invariant. The conditions are then
  22. referred by predicates. */
  23. /* A simplified representation of tree node, for unary, binary and ternary
  24. operation. Computations on parameter are decomposed to a series of this
  25. kind of structure. */
  26. struct GTY(()) expr_eval_op
  27. {
  28. /* Result type of expression. */
  29. tree type;
  30. /* Constant operands in expression, there are at most two. */
  31. tree val[2];
  32. /* Index of parameter operand in expression. */
  33. unsigned index : 2;
  34. /* Operation code of expression. */
  35. ENUM_BITFIELD(tree_code) code : 16;
  36. };
  37. typedef vec<expr_eval_op, va_gc> *expr_eval_ops;
  38. struct GTY(()) condition
  39. {
  40. /* If agg_contents is set, this is the offset from which the used data was
  41. loaded. */
  42. HOST_WIDE_INT offset;
  43. /* Type of the access reading the data (or the PARM_DECL SSA_NAME). */
  44. tree type;
  45. tree val;
  46. int operand_num;
  47. ENUM_BITFIELD(tree_code) code : 16;
  48. /* Set if the used data were loaded from an aggregate parameter or from
  49. data received by reference. */
  50. unsigned agg_contents : 1;
  51. /* If agg_contents is set, this differentiates between loads from data
  52. passed by reference and by value. */
  53. unsigned by_ref : 1;
  54. /* A set of sequential operations on the parameter, which can be seen as
  55. a mathematical function on the parameter. */
  56. expr_eval_ops param_ops;
  57. };
  58. /* Information kept about parameter of call site. */
  59. struct inline_param_summary
  60. {
  61. /* REG_BR_PROB_BASE based probability that parameter will change in between
  62. two invocation of the calls.
  63. I.e. loop invariant parameters
  64. REG_BR_PROB_BASE/estimated_iterations and regular
  65. parameters REG_BR_PROB_BASE.
  66. Value 0 is reserved for compile time invariants. */
  67. int change_prob;
  68. bool equal_to (const inline_param_summary &other) const
  69. {
  70. return change_prob == other.change_prob;
  71. }
  72. bool useless_p (void) const
  73. {
  74. return change_prob == REG_BR_PROB_BASE;
  75. }
  76. };
  77. typedef vec<condition, va_gc> *conditions;
  78. /* Predicates are used to represent function parameters (such as runtime)
  79. which depend on a context function is called in.
  80. Predicates are logical formulas in conjunctive-disjunctive form consisting
  81. of clauses which are bitmaps specifying a set of condition that must
  82. be true for a clause to be satisfied. Physically they are represented as
  83. array of clauses terminated by 0.
  84. In order to make predicate (possibly) true, all of its clauses must
  85. be (possibly) true. To make clause (possibly) true, one of conditions
  86. it mentions must be (possibly) true.
  87. There are fixed bounds on number of clauses and conditions and all the
  88. manipulation functions are conservative in positive direction. I.e. we
  89. may lose precision by thinking that predicate may be true even when it
  90. is not. */
  91. typedef uint32_t clause_t;
  92. class predicate
  93. {
  94. public:
  95. enum predicate_conditions
  96. {
  97. false_condition = 0,
  98. not_inlined_condition = 1,
  99. first_dynamic_condition = 2
  100. };
  101. /* Maximal number of conditions predicate can refer to. This is limited
  102. by using clause_t to be 32bit. */
  103. static const int num_conditions = 32;
  104. /* Special condition code we use to represent test that operand is compile
  105. time constant. */
  106. static const tree_code is_not_constant = ERROR_MARK;
  107. /* Special condition code we use to represent test that operand is not changed
  108. across invocation of the function. When operand IS_NOT_CONSTANT it is
  109. always CHANGED, however i.e. loop invariants can be NOT_CHANGED given
  110. percentage of executions even when they are not compile time constants. */
  111. static const tree_code changed = IDENTIFIER_NODE;
  112. /* Initialize predicate either to true of false depending on P. */
  113. inline predicate (bool p = true)
  114. {
  115. if (p)
  116. /* True predicate. */
  117. m_clause[0] = 0;
  118. else
  119. /* False predicate. */
  120. set_to_cond (false_condition);
  121. }
  122. /* Sanity check that we do not mix pointers to predicates with predicates. */
  123. inline predicate (predicate *)
  124. {
  125. gcc_unreachable ();
  126. }
  127. /* Return predicate testing condition I. */
  128. static inline predicate predicate_testing_cond (int i)
  129. {
  130. class predicate p;
  131. p.set_to_cond (i + first_dynamic_condition);
  132. return p;
  133. }
  134. /* Return predicate testing that function was not inlined. */
  135. static predicate not_inlined (void)
  136. {
  137. class predicate p;
  138. p.set_to_cond (not_inlined_condition);
  139. return p;
  140. }
  141. /* Compute logical and of predicates. */
  142. predicate & operator &= (const predicate &);
  143. inline predicate operator &(const predicate &p) const
  144. {
  145. predicate ret = *this;
  146. ret &= p;
  147. return ret;
  148. }
  149. /* Compute logical or of predicates. This is not operator because
  150. extra parameter CONDITIONS is needed */
  151. predicate or_with (conditions, const predicate &) const;
  152. /* Return true if predicates are known to be equal. */
  153. inline bool operator==(const predicate &p2) const
  154. {
  155. int i;
  156. for (i = 0; m_clause[i]; i++)
  157. {
  158. gcc_checking_assert (i < max_clauses);
  159. gcc_checking_assert (m_clause[i] > m_clause[i + 1]);
  160. gcc_checking_assert (!p2.m_clause[i]
  161. || p2.m_clause[i] > p2.m_clause[i + 1]);
  162. if (m_clause[i] != p2.m_clause[i])
  163. return false;
  164. }
  165. return !p2.m_clause[i];
  166. }
  167. /* Return true if predicates are known to be true or false depending
  168. on COND. */
  169. inline bool operator==(const bool cond) const
  170. {
  171. if (cond)
  172. return !m_clause[0];
  173. if (m_clause[0] == (1 << false_condition))
  174. {
  175. gcc_checking_assert (!m_clause[1]
  176. && m_clause[0] == 1
  177. << false_condition);
  178. return true;
  179. }
  180. return false;
  181. }
  182. inline bool operator!=(const predicate &p2) const
  183. {
  184. return !(*this == p2);
  185. }
  186. inline bool operator!=(const bool cond) const
  187. {
  188. return !(*this == cond);
  189. }
  190. /* Evaluate if predicate is known to be false given the clause of possible
  191. truths. */
  192. bool evaluate (clause_t) const;
  193. /* Estimate probability that predicate will be true in a given context. */
  194. int probability (conditions, clause_t, vec<inline_param_summary>) const;
  195. /* Dump predicate to F. Output newline if nl. */
  196. void dump (FILE *f, conditions, bool nl=true) const;
  197. void DEBUG_FUNCTION debug (conditions) const;
  198. /* Return predicate equal to THIS after duplication. */
  199. predicate remap_after_duplication (clause_t);
  200. /* Return predicate equal to THIS after inlining. */
  201. predicate remap_after_inlining (class ipa_fn_summary *,
  202. class ipa_node_params *params_summary,
  203. class ipa_fn_summary *,
  204. vec<int>, vec<int>, clause_t, const predicate &);
  205. void stream_in (class lto_input_block *);
  206. void stream_out (struct output_block *);
  207. private:
  208. static const int max_clauses = 8;
  209. clause_t m_clause[max_clauses + 1];
  210. /* Initialize predicate to one testing single condition number COND. */
  211. inline void set_to_cond (int cond)
  212. {
  213. m_clause[0] = 1 << cond;
  214. m_clause[1] = 0;
  215. }
  216. void add_clause (conditions conditions, clause_t);
  217. };
  218. void dump_condition (FILE *f, conditions conditions, int cond);
  219. predicate add_condition (class ipa_fn_summary *summary,
  220. class ipa_node_params *params_summary,
  221. int operand_num,
  222. tree type, struct agg_position_info *aggpos,
  223. enum tree_code code, tree val,
  224. expr_eval_ops param_ops = NULL);