tree-chrec.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* Chains of recurrences.
  2. Copyright (C) 2003-2015 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);
  49. extern tree chrec_convert_rhs (tree, tree, gimple);
  50. extern tree chrec_convert_aggressive (tree, tree);
  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);
  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);
  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);
  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. return build3 (POLYNOMIAL_CHREC, TREE_TYPE (left),
  130. build_int_cst (NULL_TREE, loop_num), left, right);
  131. }
  132. /* Determines whether the expression CHREC is a constant. */
  133. static inline bool
  134. evolution_function_is_constant_p (const_tree chrec)
  135. {
  136. if (chrec == NULL_TREE)
  137. return false;
  138. switch (TREE_CODE (chrec))
  139. {
  140. case INTEGER_CST:
  141. case REAL_CST:
  142. return true;
  143. default:
  144. return false;
  145. }
  146. }
  147. /* Determine whether CHREC is an affine evolution function in LOOPNUM. */
  148. static inline bool
  149. evolution_function_is_affine_in_loop (const_tree chrec, int loopnum)
  150. {
  151. if (chrec == NULL_TREE)
  152. return false;
  153. switch (TREE_CODE (chrec))
  154. {
  155. case POLYNOMIAL_CHREC:
  156. if (evolution_function_is_invariant_p (CHREC_LEFT (chrec), loopnum)
  157. && evolution_function_is_invariant_p (CHREC_RIGHT (chrec), loopnum))
  158. return true;
  159. else
  160. return false;
  161. default:
  162. return false;
  163. }
  164. }
  165. /* Determine whether CHREC is an affine evolution function or not. */
  166. static inline bool
  167. evolution_function_is_affine_p (const_tree chrec)
  168. {
  169. return chrec
  170. && TREE_CODE (chrec) == POLYNOMIAL_CHREC
  171. && evolution_function_is_invariant_p (CHREC_RIGHT (chrec),
  172. CHREC_VARIABLE (chrec))
  173. && (TREE_CODE (CHREC_RIGHT (chrec)) != POLYNOMIAL_CHREC
  174. || evolution_function_is_affine_p (CHREC_RIGHT (chrec)));
  175. }
  176. /* Determines whether EXPR does not contains chrec expressions. */
  177. static inline bool
  178. tree_does_not_contain_chrecs (const_tree expr)
  179. {
  180. return !tree_contains_chrecs (expr, NULL);
  181. }
  182. /* Returns the type of the chrec. */
  183. static inline tree
  184. chrec_type (const_tree chrec)
  185. {
  186. if (automatically_generated_chrec_p (chrec))
  187. return NULL_TREE;
  188. return TREE_TYPE (chrec);
  189. }
  190. static inline tree
  191. chrec_fold_op (enum tree_code code, tree type, tree op0, tree op1)
  192. {
  193. switch (code)
  194. {
  195. case PLUS_EXPR:
  196. return chrec_fold_plus (type, op0, op1);
  197. case MINUS_EXPR:
  198. return chrec_fold_minus (type, op0, op1);
  199. case MULT_EXPR:
  200. return chrec_fold_multiply (type, op0, op1);
  201. default:
  202. gcc_unreachable ();
  203. }
  204. }
  205. #endif /* GCC_TREE_CHREC_H */