omega.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* Source code for an implementation of the Omega test, an integer
  2. programming algorithm for dependence analysis, by William Pugh,
  3. appeared in Supercomputing '91 and CACM Aug 92.
  4. This code has no license restrictions, and is considered public
  5. domain.
  6. Changes copyright (C) 2005-2015 Free Software Foundation, Inc.
  7. Contributed by Sebastian Pop <sebastian.pop@inria.fr>
  8. This file is part of GCC.
  9. GCC is free software; you can redistribute it and/or modify it under
  10. the terms of the GNU General Public License as published by the Free
  11. Software Foundation; either version 3, or (at your option) any later
  12. version.
  13. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  14. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with GCC; see the file COPYING3. If not see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "config.h"
  21. #include "params.h"
  22. #ifndef GCC_OMEGA_H
  23. #define GCC_OMEGA_H
  24. #define OMEGA_MAX_VARS PARAM_VALUE (PARAM_OMEGA_MAX_VARS)
  25. #define OMEGA_MAX_GEQS PARAM_VALUE (PARAM_OMEGA_MAX_GEQS)
  26. #define OMEGA_MAX_EQS PARAM_VALUE (PARAM_OMEGA_MAX_EQS)
  27. #define pos_infinity (0x7ffffff)
  28. #define neg_infinity (-0x7ffffff)
  29. /* Results of the Omega solver. */
  30. enum omega_result {
  31. omega_false = 0,
  32. omega_true = 1,
  33. /* Value returned when the solver is unable to determine an
  34. answer. */
  35. omega_unknown = 2,
  36. /* Value used for asking the solver to simplify the system. */
  37. omega_simplify = 3
  38. };
  39. /* Values used for labeling equations. Private (not used outside the
  40. solver). */
  41. enum omega_eqn_color {
  42. omega_black = 0,
  43. omega_red = 1
  44. };
  45. /* Structure for equations. */
  46. typedef struct eqn_d
  47. {
  48. int key;
  49. int touched;
  50. enum omega_eqn_color color;
  51. /* Array of coefficients for the equation. The layout of the data
  52. is as follows: coef[0] is the constant, coef[i] for 1 <= i <=
  53. OMEGA_MAX_VARS, are the coefficients for each dimension. Examples:
  54. the equation 0 = 9 + x + 0y + 5z is encoded as [9 1 0 5], the
  55. inequality 0 <= -8 + x + 2y + 3z is encoded as [-8 1 2 3]. */
  56. int *coef;
  57. } *eqn;
  58. typedef struct omega_pb_d
  59. {
  60. /* The number of variables in the system of equations. */
  61. int num_vars;
  62. /* Safe variables are not eliminated during the Fourier-Motzkin
  63. simplification of the system. Safe variables are all those
  64. variables that are placed at the beginning of the array of
  65. variables: PB->var[1, ..., SAFE_VARS]. PB->var[0] is not used,
  66. as PB->eqs[x]->coef[0] represents the constant of the equation. */
  67. int safe_vars;
  68. /* Number of elements in eqs[]. */
  69. int num_eqs;
  70. /* Number of elements in geqs[]. */
  71. int num_geqs;
  72. /* Number of elements in subs[]. */
  73. int num_subs;
  74. int hash_version;
  75. bool variables_initialized;
  76. bool variables_freed;
  77. /* Index or name of variables. Negative integers are reserved for
  78. wildcard variables. Maps the index of variables in the original
  79. problem to the new index of the variable. The index for a
  80. variable in the coef array of an equation can change as some
  81. variables are eliminated. */
  82. int *var;
  83. int *forwarding_address;
  84. /* Inequalities in the system of constraints. */
  85. eqn geqs;
  86. /* Equations in the system of constraints. */
  87. eqn eqs;
  88. /* A map of substituted variables. */
  89. eqn subs;
  90. } *omega_pb;
  91. extern void omega_initialize (void);
  92. extern omega_pb omega_alloc_problem (int, int);
  93. extern enum omega_result omega_solve_problem (omega_pb, enum omega_result);
  94. extern enum omega_result omega_simplify_problem (omega_pb);
  95. extern enum omega_result omega_simplify_approximate (omega_pb);
  96. extern enum omega_result omega_constrain_variable_sign (omega_pb,
  97. enum omega_eqn_color,
  98. int, int);
  99. extern void debug (omega_pb_d &ref);
  100. extern void debug (omega_pb_d *ptr);
  101. extern void debug_omega_problem (omega_pb);
  102. extern void omega_print_problem (FILE *, omega_pb);
  103. extern void omega_print_red_equations (FILE *, omega_pb);
  104. extern int omega_count_red_equations (omega_pb);
  105. extern void omega_pretty_print_problem (FILE *, omega_pb);
  106. extern void omega_unprotect_variable (omega_pb, int var);
  107. extern void omega_negate_geq (omega_pb, int);
  108. extern void omega_convert_eq_to_geqs (omega_pb, int eq);
  109. extern void omega_print_eqn (FILE *, omega_pb, eqn, bool, int);
  110. extern bool omega_problem_has_red_equations (omega_pb);
  111. extern enum omega_result omega_eliminate_redundant (omega_pb, bool);
  112. extern void omega_eliminate_red (omega_pb, bool);
  113. extern void omega_constrain_variable_value (omega_pb, enum omega_eqn_color,
  114. int, int);
  115. extern bool omega_query_variable (omega_pb, int, int *, int *);
  116. extern int omega_query_variable_signs (omega_pb, int, int, int, int,
  117. int, int, bool *, int *);
  118. extern bool omega_query_variable_bounds (omega_pb, int, int *, int *);
  119. extern void (*omega_when_reduced) (omega_pb);
  120. extern void omega_no_procedure (omega_pb);
  121. /* Return true when variable I in problem PB is a wildcard. */
  122. static inline bool
  123. omega_wildcard_p (omega_pb pb, int i)
  124. {
  125. return (pb->var[i] < 0);
  126. }
  127. /* Return true when variable I in problem PB is a safe variable. */
  128. static inline bool
  129. omega_safe_var_p (omega_pb pb, int i)
  130. {
  131. /* The constant of an equation is not a variable. */
  132. gcc_assert (0 < i);
  133. return (i <= pb->safe_vars);
  134. }
  135. /* Print to FILE equality E from PB. */
  136. static inline void
  137. omega_print_eq (FILE *file, omega_pb pb, eqn e)
  138. {
  139. omega_print_eqn (file, pb, e, false, 0);
  140. }
  141. /* Print to FILE inequality E from PB. */
  142. static inline void
  143. omega_print_geq (FILE *file, omega_pb pb, eqn e)
  144. {
  145. omega_print_eqn (file, pb, e, true, 0);
  146. }
  147. /* Print to FILE inequality E from PB. */
  148. static inline void
  149. omega_print_geq_extra (FILE *file, omega_pb pb, eqn e)
  150. {
  151. omega_print_eqn (file, pb, e, true, 1);
  152. }
  153. /* E1 = E2, make a copy of E2 into E1. Equations contain S variables. */
  154. static inline void
  155. omega_copy_eqn (eqn e1, eqn e2, int s)
  156. {
  157. e1->key = e2->key;
  158. e1->touched = e2->touched;
  159. e1->color = e2->color;
  160. memcpy (e1->coef, e2->coef, (s + 1) * sizeof (int));
  161. }
  162. /* Initialize E = 0. Equation E contains S variables. */
  163. static inline void
  164. omega_init_eqn_zero (eqn e, int s)
  165. {
  166. e->key = 0;
  167. e->touched = 0;
  168. e->color = omega_black;
  169. memset (e->coef, 0, (s + 1) * sizeof (int));
  170. }
  171. /* Allocate N equations with S variables. */
  172. static inline eqn
  173. omega_alloc_eqns (int s, int n)
  174. {
  175. int i;
  176. eqn res = (eqn) (xcalloc (n, sizeof (struct eqn_d)));
  177. for (i = n - 1; i >= 0; i--)
  178. {
  179. res[i].coef = (int *) (xcalloc (OMEGA_MAX_VARS + 1, sizeof (int)));
  180. omega_init_eqn_zero (&res[i], s);
  181. }
  182. return res;
  183. }
  184. /* Free N equations from array EQ. */
  185. static inline void
  186. omega_free_eqns (eqn eq, int n)
  187. {
  188. int i;
  189. for (i = n - 1; i >= 0; i--)
  190. free (eq[i].coef);
  191. free (eq);
  192. }
  193. /* Returns true when E is an inequality with a single variable. */
  194. static inline bool
  195. single_var_geq (eqn e, int nv ATTRIBUTE_UNUSED)
  196. {
  197. return (e->key != 0
  198. && -OMEGA_MAX_VARS <= e->key && e->key <= OMEGA_MAX_VARS);
  199. }
  200. /* Allocate a new equality with all coefficients 0, and tagged with
  201. COLOR. Return the index of this equality in problem PB. */
  202. static inline int
  203. omega_add_zero_eq (omega_pb pb, enum omega_eqn_color color)
  204. {
  205. int idx = pb->num_eqs++;
  206. gcc_assert (pb->num_eqs <= OMEGA_MAX_EQS);
  207. omega_init_eqn_zero (&pb->eqs[idx], pb->num_vars);
  208. pb->eqs[idx].color = color;
  209. return idx;
  210. }
  211. /* Allocate a new inequality with all coefficients 0, and tagged with
  212. COLOR. Return the index of this inequality in problem PB. */
  213. static inline int
  214. omega_add_zero_geq (omega_pb pb, enum omega_eqn_color color)
  215. {
  216. int idx = pb->num_geqs;
  217. pb->num_geqs++;
  218. gcc_assert (pb->num_geqs <= OMEGA_MAX_GEQS);
  219. omega_init_eqn_zero (&pb->geqs[idx], pb->num_vars);
  220. pb->geqs[idx].touched = 1;
  221. pb->geqs[idx].color = color;
  222. return idx;
  223. }
  224. /* Initialize variables for problem PB. */
  225. static inline void
  226. omega_initialize_variables (omega_pb pb)
  227. {
  228. int i;
  229. for (i = pb->num_vars; i >= 0; i--)
  230. pb->forwarding_address[i] = pb->var[i] = i;
  231. pb->variables_initialized = true;
  232. }
  233. /* Free problem PB. */
  234. static inline void
  235. omega_free_problem (omega_pb pb)
  236. {
  237. free (pb->var);
  238. free (pb->forwarding_address);
  239. omega_free_eqns (pb->geqs, OMEGA_MAX_GEQS);
  240. omega_free_eqns (pb->eqs, OMEGA_MAX_EQS);
  241. omega_free_eqns (pb->subs, OMEGA_MAX_VARS + 1);
  242. free (pb);
  243. }
  244. /* Copy omega problems: P1 = P2. */
  245. static inline void
  246. omega_copy_problem (omega_pb p1, omega_pb p2)
  247. {
  248. int e, i;
  249. p1->num_vars = p2->num_vars;
  250. p1->hash_version = p2->hash_version;
  251. p1->variables_initialized = p2->variables_initialized;
  252. p1->variables_freed = p2->variables_freed;
  253. p1->safe_vars = p2->safe_vars;
  254. p1->num_eqs = p2->num_eqs;
  255. p1->num_subs = p2->num_subs;
  256. p1->num_geqs = p2->num_geqs;
  257. for (e = p2->num_eqs - 1; e >= 0; e--)
  258. omega_copy_eqn (&(p1->eqs[e]), &(p2->eqs[e]), p2->num_vars);
  259. for (e = p2->num_geqs - 1; e >= 0; e--)
  260. omega_copy_eqn (&(p1->geqs[e]), &(p2->geqs[e]), p2->num_vars);
  261. for (e = p2->num_subs - 1; e >= 0; e--)
  262. omega_copy_eqn (&(p1->subs[e]), &(p2->subs[e]), p2->num_vars);
  263. for (i = p2->num_vars; i >= 0; i--)
  264. p1->var[i] = p2->var[i];
  265. for (i = OMEGA_MAX_VARS; i >= 0; i--)
  266. p1->forwarding_address[i] = p2->forwarding_address[i];
  267. }
  268. #endif /* GCC_OMEGA_H */