isl_local.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright 2011 INRIA Saclay
  3. * Copyright 2014 Ecole Normale Superieure
  4. *
  5. * Use of this software is governed by the MIT license
  6. *
  7. * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
  8. * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
  9. * 91893 Orsay, France
  10. * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
  11. */
  12. #include <isl/space.h>
  13. #include <isl_vec_private.h>
  14. #include <isl_mat_private.h>
  15. #include <isl_reordering.h>
  16. #include <isl_seq.h>
  17. #include <isl_local_private.h>
  18. /* Return the isl_ctx to which "local" belongs.
  19. */
  20. isl_ctx *isl_local_get_ctx(__isl_keep isl_local *local)
  21. {
  22. if (!local)
  23. return NULL;
  24. return isl_mat_get_ctx(local);
  25. }
  26. /* Create an isl_local object from a matrix describing
  27. * integer divisions.
  28. *
  29. * An isl_local object is current defined as exactly such a matrix,
  30. * so simply return the input.
  31. */
  32. __isl_give isl_local *isl_local_alloc_from_mat(__isl_take isl_mat *mat)
  33. {
  34. return mat;
  35. }
  36. /* Free "local" and return NULL.
  37. */
  38. __isl_null isl_local *isl_local_free(__isl_take isl_local *local)
  39. {
  40. isl_mat_free(local);
  41. return NULL;
  42. }
  43. /* Return the number of local variables (isl_dim_div),
  44. * the number of other variables (isl_dim_set) or
  45. * the total number of variables (isl_dim_all) in "local".
  46. *
  47. * Other types do not have any meaning for an isl_local object.
  48. */
  49. isl_size isl_local_dim(__isl_keep isl_local *local, enum isl_dim_type type)
  50. {
  51. isl_mat *mat = local;
  52. if (!local)
  53. return isl_size_error;
  54. if (type == isl_dim_div)
  55. return isl_mat_rows(mat);
  56. if (type == isl_dim_all) {
  57. isl_size cols = isl_mat_cols(mat);
  58. if (cols < 0)
  59. return isl_size_error;
  60. return cols - 2;
  61. }
  62. if (type == isl_dim_set) {
  63. isl_size total, n_div;
  64. total = isl_local_dim(local, isl_dim_all);
  65. n_div = isl_local_dim(local, isl_dim_div);
  66. if (total < 0 || n_div < 0)
  67. return isl_size_error;
  68. return total - n_div;
  69. }
  70. isl_die(isl_local_get_ctx(local), isl_error_unsupported,
  71. "unsupported dimension type", return isl_size_error);
  72. }
  73. #undef TYPE
  74. #define TYPE isl_local
  75. static
  76. #include "check_type_range_templ.c"
  77. /* Check that "pos" is a valid position for a variable in "local".
  78. */
  79. static isl_stat isl_local_check_pos(__isl_keep isl_local *local, int pos)
  80. {
  81. return isl_local_check_range(local, isl_dim_div, pos, 1);
  82. }
  83. /* Given local variables "local",
  84. * is the variable at position "pos" marked as not having
  85. * an explicit representation?
  86. * Note that even if this variable is not marked in this way and therefore
  87. * does have an explicit representation, this representation may still
  88. * depend (indirectly) on other local variables that do not
  89. * have an explicit representation.
  90. */
  91. isl_bool isl_local_div_is_marked_unknown(__isl_keep isl_local *local, int pos)
  92. {
  93. isl_mat *mat = local;
  94. if (isl_local_check_pos(local, pos) < 0)
  95. return isl_bool_error;
  96. return isl_bool_ok(isl_int_is_zero(mat->row[pos][0]));
  97. }
  98. /* Given local variables "local",
  99. * does the variable at position "pos" have a complete explicit representation?
  100. * Having a complete explicit representation requires not only
  101. * an explicit representation, but also that all local variables
  102. * that appear in this explicit representation in turn have
  103. * a complete explicit representation.
  104. */
  105. isl_bool isl_local_div_is_known(__isl_keep isl_local *local, int pos)
  106. {
  107. isl_bool marked;
  108. int i, off;
  109. isl_size n, cols;
  110. isl_mat *mat = local;
  111. if (isl_local_check_pos(local, pos) < 0)
  112. return isl_bool_error;
  113. marked = isl_local_div_is_marked_unknown(local, pos);
  114. if (marked < 0 || marked)
  115. return isl_bool_not(marked);
  116. n = isl_local_dim(local, isl_dim_div);
  117. cols = isl_mat_cols(mat);
  118. if (n < 0 || cols < 0)
  119. return isl_bool_error;
  120. off = cols - n;
  121. for (i = n - 1; i >= 0; --i) {
  122. isl_bool known;
  123. if (isl_int_is_zero(mat->row[pos][off + i]))
  124. continue;
  125. known = isl_local_div_is_known(local, i);
  126. if (known < 0 || !known)
  127. return known;
  128. }
  129. return isl_bool_true;
  130. }
  131. /* Does "local" have an explicit representation for all local variables?
  132. */
  133. isl_bool isl_local_divs_known(__isl_keep isl_local *local)
  134. {
  135. int i;
  136. isl_size n;
  137. n = isl_local_dim(local, isl_dim_div);
  138. if (n < 0)
  139. return isl_bool_error;
  140. for (i = 0; i < n; ++i) {
  141. isl_bool unknown = isl_local_div_is_marked_unknown(local, i);
  142. if (unknown < 0 || unknown)
  143. return isl_bool_not(unknown);
  144. }
  145. return isl_bool_true;
  146. }
  147. /* Compare two sets of local variables, defined over
  148. * the same space.
  149. *
  150. * Return -1 if "local1" is "smaller" than "local2", 1 if "local1" is "greater"
  151. * than "local2" and 0 if they are equal.
  152. *
  153. * The order is fairly arbitrary. We do "prefer" divs that only involve
  154. * earlier dimensions in the sense that we consider matrices where
  155. * the first differing div involves earlier dimensions to be smaller.
  156. */
  157. int isl_local_cmp(__isl_keep isl_local *local1, __isl_keep isl_local *local2)
  158. {
  159. int i;
  160. int cmp;
  161. isl_bool unknown1, unknown2;
  162. int last1, last2;
  163. isl_size n_col;
  164. isl_mat *mat1 = local1;
  165. isl_mat *mat2 = local2;
  166. if (local1 == local2)
  167. return 0;
  168. if (!local1)
  169. return -1;
  170. if (!local2)
  171. return 1;
  172. if (mat1->n_row != mat2->n_row)
  173. return mat1->n_row - mat2->n_row;
  174. n_col = isl_mat_cols(mat1);
  175. if (n_col < 0)
  176. return -1;
  177. for (i = 0; i < mat1->n_row; ++i) {
  178. unknown1 = isl_local_div_is_marked_unknown(local1, i);
  179. unknown2 = isl_local_div_is_marked_unknown(local2, i);
  180. if (unknown1 && unknown2)
  181. continue;
  182. if (unknown1)
  183. return 1;
  184. if (unknown2)
  185. return -1;
  186. last1 = isl_seq_last_non_zero(mat1->row[i] + 1, n_col - 1);
  187. last2 = isl_seq_last_non_zero(mat2->row[i] + 1, n_col - 1);
  188. if (last1 != last2)
  189. return last1 - last2;
  190. cmp = isl_seq_cmp(mat1->row[i], mat2->row[i], n_col);
  191. if (cmp != 0)
  192. return cmp;
  193. }
  194. return 0;
  195. }
  196. /* Reorder the columns of the given local variables according to the
  197. * given reordering.
  198. * The order of the local variables themselves is assumed not to change.
  199. */
  200. __isl_give isl_local *isl_local_reorder(__isl_take isl_local *local,
  201. __isl_take isl_reordering *r)
  202. {
  203. isl_mat *div = local;
  204. int i, j;
  205. isl_size dim;
  206. isl_space *space;
  207. isl_mat *mat;
  208. int extra;
  209. if (!local || !r)
  210. goto error;
  211. space = isl_reordering_peek_space(r);
  212. dim = isl_space_dim(space, isl_dim_all);
  213. if (dim < 0)
  214. goto error;
  215. extra = dim + div->n_row - r->len;
  216. mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
  217. if (!mat)
  218. goto error;
  219. for (i = 0; i < div->n_row; ++i) {
  220. isl_seq_cpy(mat->row[i], div->row[i], 2);
  221. isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
  222. for (j = 0; j < r->len; ++j)
  223. isl_int_set(mat->row[i][2 + r->pos[j]],
  224. div->row[i][2 + j]);
  225. }
  226. isl_reordering_free(r);
  227. isl_local_free(local);
  228. return isl_local_alloc_from_mat(mat);
  229. error:
  230. isl_reordering_free(r);
  231. isl_local_free(local);
  232. return NULL;
  233. }
  234. /* Extend a vector "v" representing an integer point
  235. * in the domain space of "local"
  236. * to one that also includes values for the local variables.
  237. * All local variables are required to have an explicit representation.
  238. * If there are no local variables, then the point is not required
  239. * to be integral.
  240. */
  241. __isl_give isl_vec *isl_local_extend_point_vec(__isl_keep isl_local *local,
  242. __isl_take isl_vec *v)
  243. {
  244. isl_size dim, n_div, size;
  245. isl_bool known;
  246. isl_mat *mat = local;
  247. if (!local || !v)
  248. return isl_vec_free(v);
  249. known = isl_local_divs_known(local);
  250. if (known < 0)
  251. return isl_vec_free(v);
  252. if (!known)
  253. isl_die(isl_local_get_ctx(local), isl_error_invalid,
  254. "unknown local variables", return isl_vec_free(v));
  255. dim = isl_local_dim(local, isl_dim_set);
  256. n_div = isl_local_dim(local, isl_dim_div);
  257. size = isl_vec_size(v);
  258. if (dim < 0 || n_div < 0 || size < 0)
  259. return isl_vec_free(v);
  260. if (size != 1 + dim)
  261. isl_die(isl_local_get_ctx(local), isl_error_invalid,
  262. "incorrect size", return isl_vec_free(v));
  263. if (n_div == 0)
  264. return v;
  265. if (!isl_int_is_one(v->el[0]))
  266. isl_die(isl_local_get_ctx(local), isl_error_invalid,
  267. "expecting integer point", return isl_vec_free(v));
  268. {
  269. int i;
  270. v = isl_vec_add_els(v, n_div);
  271. if (!v)
  272. return NULL;
  273. for (i = 0; i < n_div; ++i) {
  274. isl_seq_inner_product(mat->row[i] + 1, v->el,
  275. 1 + dim + i, &v->el[1+dim+i]);
  276. isl_int_fdiv_q(v->el[1+dim+i], v->el[1+dim+i],
  277. mat->row[i][0]);
  278. }
  279. }
  280. return v;
  281. }