tree-chrec.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* Chains of recurrences.
  2. Copyright (C) 2003-2019 Free Software Foundation, Inc.
  3. Contributed by Sebastian Pop <pop@cri.ensmp.fr>
  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_TREE_CHREC_H
  17. #define GCC_TREE_CHREC_H
  18. /* The following trees are unique elements. Thus the comparison of another
  19. element to these elements should be done on the pointer to these trees,
  20. and not on their value. */
  21. extern tree chrec_not_analyzed_yet;
  22. extern GTY(()) tree chrec_dont_know;
  23. extern GTY(()) tree chrec_known;
  24. /* After having added an automatically generated element, please
  25. include it in the following function. */
  26. static inline bool
  27. automatically_generated_chrec_p (const_tree chrec)
  28. {
  29. return (chrec == chrec_dont_know
  30. || chrec == chrec_known);
  31. }
  32. /* The tree nodes aka. CHRECs. */
  33. static inline bool
  34. tree_is_chrec (const_tree expr)
  35. {
  36. if (TREE_CODE (expr) == POLYNOMIAL_CHREC
  37. || automatically_generated_chrec_p (expr))
  38. return true;
  39. else
  40. return false;
  41. }
  42. enum ev_direction {EV_DIR_GROWS, EV_DIR_DECREASES, EV_DIR_UNKNOWN};
  43. enum ev_direction scev_direction (const_tree);
  44. /* Chrec folding functions. */
  45. extern tree chrec_fold_plus (tree, tree, tree);
  46. extern tree chrec_fold_minus (tree, tree, tree);
  47. extern tree chrec_fold_multiply (tree, tree, tree);
  48. extern tree chrec_convert (tree, tree, gimple *, bool = true, tree = NULL);
  49. extern tree chrec_convert_rhs (tree, tree, gimple *);
  50. extern tree chrec_convert_aggressive (tree, tree, bool *);
  51. /* Operations. */
  52. extern tree chrec_apply (unsigned, tree, tree);
  53. extern tree chrec_apply_map (tree, vec<tree> );
  54. extern tree chrec_replace_initial_condition (tree, tree);
  55. extern tree initial_condition (tree);
  56. extern tree initial_condition_in_loop_num (tree, unsigned);
  57. extern tree evolution_part_in_loop_num (tree, unsigned);
  58. extern tree hide_evolution_in_other_loops_than_loop (tree, unsigned);
  59. extern tree reset_evolution_in_loop (unsigned, tree, tree);
  60. extern tree chrec_merge (tree, tree);
  61. extern void for_each_scev_op (tree *, bool (*) (tree *, void *), void *);
  62. extern bool convert_affine_scev (struct loop *, tree, tree *, tree *, gimple *,
  63. bool, tree = NULL);
  64. /* Observers. */
  65. extern bool eq_evolutions_p (const_tree, const_tree);
  66. extern bool is_multivariate_chrec (const_tree);
  67. extern bool chrec_contains_symbols (const_tree, struct loop * = NULL);
  68. extern bool chrec_contains_symbols_defined_in_loop (const_tree, unsigned);
  69. extern bool chrec_contains_undetermined (const_tree);
  70. extern bool tree_contains_chrecs (const_tree, int *);
  71. extern bool evolution_function_is_affine_multivariate_p (const_tree, int);
  72. extern bool evolution_function_is_univariate_p (const_tree, int = 0);
  73. extern unsigned nb_vars_in_chrec (tree);
  74. extern bool evolution_function_is_invariant_p (tree, int);
  75. extern bool scev_is_linear_expression (tree);
  76. extern bool evolution_function_right_is_integer_cst (const_tree);
  77. /* Determines whether CHREC is equal to zero. */
  78. static inline bool
  79. chrec_zerop (const_tree chrec)
  80. {
  81. if (chrec == NULL_TREE)
  82. return false;
  83. if (TREE_CODE (chrec) == INTEGER_CST)
  84. return integer_zerop (chrec);
  85. return false;
  86. }
  87. /* Determines whether CHREC is a loop invariant with respect to LOOP_NUM.
  88. Set the result in RES and return true when the property can be computed. */
  89. static inline bool
  90. no_evolution_in_loop_p (tree chrec, unsigned loop_num, bool *res)
  91. {
  92. tree scev;
  93. if (chrec == chrec_not_analyzed_yet
  94. || chrec == chrec_dont_know
  95. || chrec_contains_symbols_defined_in_loop (chrec, loop_num))
  96. return false;
  97. STRIP_NOPS (chrec);
  98. scev = hide_evolution_in_other_loops_than_loop (chrec, loop_num);
  99. *res = !tree_contains_chrecs (scev, NULL);
  100. return true;
  101. }
  102. /* Build a polynomial chain of recurrence. */
  103. static inline tree
  104. build_polynomial_chrec (unsigned loop_num,
  105. tree left,
  106. tree right)
  107. {
  108. bool val;
  109. if (left == chrec_dont_know
  110. || right == chrec_dont_know)
  111. return chrec_dont_know;
  112. if (!no_evolution_in_loop_p (left, loop_num, &val)
  113. || !val)
  114. return chrec_dont_know;
  115. /* Types of left and right sides of a chrec should be compatible, but
  116. pointer CHRECs are special in that the evolution is of ptroff type. */
  117. if (POINTER_TYPE_P (TREE_TYPE (left)))
  118. gcc_checking_assert (ptrofftype_p (TREE_TYPE (right)));
  119. else
  120. {
  121. /* Pointer types should occur only on the left hand side, i.e. in
  122. the base of the chrec, and not in the step. */
  123. gcc_checking_assert (!POINTER_TYPE_P (TREE_TYPE (right))
  124. && types_compatible_p (TREE_TYPE (left),
  125. TREE_TYPE (right)));
  126. }
  127. if (chrec_zerop (right))
  128. return left;
  129. tree chrec = build2 (POLYNOMIAL_CHREC, TREE_TYPE (left), left, right);
  130. CHREC_VARIABLE (chrec) = loop_num;
  131. return chrec;
  132. }
  133. /* Determines whether the expression CHREC is a constant. */
  134. static inline bool
  135. evolution_function_is_constant_p (const_tree chrec)
  136. {
  137. if (chrec == NULL_TREE)
  138. return false;
  139. return is_gimple_min_invariant (chrec);
  140. }
  141. /* Determine whether CHREC is an affine evolution function in LOOPNUM. */
  142. static inline bool
  143. evolution_function_is_affine_in_loop (const_tree chrec, int loopnum)
  144. {
  145. if (chrec == NULL_TREE)
  146. return false;
  147. switch (TREE_CODE (chrec))
  148. {
  149. case POLYNOMIAL_CHREC:
  150. if (evolution_function_is_invariant_p (CHREC_LEFT (chrec), loopnum)
  151. && evolution_function_is_invariant_p (CHREC_RIGHT (chrec), loopnum))
  152. return true;
  153. else
  154. return false;
  155. default:
  156. return false;
  157. }
  158. }
  159. /* Determine whether CHREC is an affine evolution function or not. */
  160. static inline bool
  161. evolution_function_is_affine_p (const_tree chrec)
  162. {
  163. return chrec
  164. && TREE_CODE (chrec) == POLYNOMIAL_CHREC
  165. && evolution_function_is_invariant_p (CHREC_RIGHT (chrec),
  166. CHREC_VARIABLE (chrec))
  167. && (TREE_CODE (CHREC_RIGHT (chrec)) != POLYNOMIAL_CHREC
  168. || evolution_function_is_affine_p (CHREC_RIGHT (chrec)));
  169. }
  170. /* Determines whether EXPR does not contains chrec expressions. */
  171. static inline bool
  172. tree_does_not_contain_chrecs (const_tree expr)
  173. {
  174. return !tree_contains_chrecs (expr, NULL);
  175. }
  176. /* Returns the type of the chrec. */
  177. static inline tree
  178. chrec_type (const_tree chrec)
  179. {
  180. if (automatically_generated_chrec_p (chrec))
  181. return NULL_TREE;
  182. return TREE_TYPE (chrec);
  183. }
  184. static inline tree
  185. chrec_fold_op (enum tree_code code, tree type, tree op0, tree op1)
  186. {
  187. switch (code)
  188. {
  189. case PLUS_EXPR:
  190. return chrec_fold_plus (type, op0, op1);
  191. case MINUS_EXPR:
  192. return chrec_fold_minus (type, op0, op1);
  193. case MULT_EXPR:
  194. return chrec_fold_multiply (type, op0, op1);
  195. default:
  196. gcc_unreachable ();
  197. }
  198. }
  199. #endif /* GCC_TREE_CHREC_H */