isl_tab.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright 2008-2009 Katholieke Universiteit Leuven
  3. *
  4. * Use of this software is governed by the MIT license
  5. *
  6. * Written by Sven Verdoolaege, K.U.Leuven, Departement
  7. * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
  8. */
  9. #ifndef ISL_TAB_H
  10. #define ISL_TAB_H
  11. #include <isl/lp.h>
  12. #include <isl/map.h>
  13. #include <isl/mat.h>
  14. #include <isl/set.h>
  15. #include <isl_config.h>
  16. struct isl_tab_var {
  17. int index;
  18. unsigned is_row : 1;
  19. unsigned is_nonneg : 1;
  20. unsigned is_zero : 1;
  21. unsigned is_redundant : 1;
  22. unsigned marked : 1;
  23. unsigned frozen : 1;
  24. unsigned negated : 1;
  25. };
  26. enum isl_tab_undo_type {
  27. isl_tab_undo_bottom,
  28. isl_tab_undo_rational,
  29. isl_tab_undo_empty,
  30. isl_tab_undo_nonneg,
  31. isl_tab_undo_redundant,
  32. isl_tab_undo_freeze,
  33. isl_tab_undo_zero,
  34. isl_tab_undo_allocate,
  35. isl_tab_undo_relax,
  36. isl_tab_undo_unrestrict,
  37. isl_tab_undo_bmap_ineq,
  38. isl_tab_undo_bmap_eq,
  39. isl_tab_undo_bmap_div,
  40. isl_tab_undo_saved_basis,
  41. isl_tab_undo_drop_sample,
  42. isl_tab_undo_saved_samples,
  43. isl_tab_undo_callback,
  44. };
  45. struct isl_tab_callback {
  46. isl_stat (*run)(struct isl_tab_callback *cb);
  47. };
  48. union isl_tab_undo_val {
  49. int var_index;
  50. int *col_var;
  51. int n;
  52. struct isl_tab_callback *callback;
  53. };
  54. struct isl_tab_undo {
  55. enum isl_tab_undo_type type;
  56. union isl_tab_undo_val u;
  57. struct isl_tab_undo *next;
  58. };
  59. /* The tableau maintains equality relations.
  60. * Each column and each row is associated to a variable or a constraint.
  61. * The "value" of an inequality constraint is the value of the corresponding
  62. * slack variable.
  63. * The "row_var" and "col_var" arrays map column and row indices
  64. * to indices in the "var" and "con" arrays. The elements of these
  65. * arrays maintain extra information about the variables and the constraints.
  66. * Each row expresses the corresponding row variable as an affine expression
  67. * of the column variables.
  68. * The first two columns in the matrix contain the common denominator of
  69. * the row and the numerator of the constant term.
  70. * If "M" is set, then the third column represents the "big parameter".
  71. * The third (M = 0) or fourth (M = 1) column
  72. * in the matrix is called column 0 with respect to the col_var array.
  73. * The sample value of the tableau is the value that assigns zero
  74. * to all the column variables and the constant term of each affine
  75. * expression to the corresponding row variable.
  76. * The operations on the tableau maintain the property that the sample
  77. * value satisfies the non-negativity constraints (usually on the slack
  78. * variables).
  79. *
  80. * The big parameter represents an arbitrarily big (and divisible)
  81. * positive number. If present, then the sign of a row is determined
  82. * lexicographically, with the sign of the big parameter coefficient
  83. * considered first. The big parameter is only used while
  84. * solving PILP problems.
  85. *
  86. * The first n_dead column variables have their values fixed to zero.
  87. * The corresponding tab_vars are flagged "is_zero".
  88. * Some of the rows that have have zero coefficients in all but
  89. * the dead columns are also flagged "is_zero".
  90. *
  91. * The first n_redundant rows correspond to inequality constraints
  92. * that are always satisfied for any value satisfying the non-redundant
  93. * rows. The corresponding tab_vars are flagged "is_redundant".
  94. * A row variable that is flagged "is_zero" is also flagged "is_redundant"
  95. * since the constraint has been reduced to 0 = 0 and is therefore always
  96. * satisfied.
  97. *
  98. * There are "n_var" variables in total. The first "n_param" of these
  99. * are called parameters and the last "n_div" of these are called divs.
  100. * The basic tableau operations makes no distinction between different
  101. * kinds of variables. These special variables are only used while
  102. * solving PILP problems.
  103. *
  104. * Dead columns and redundant rows are detected on the fly.
  105. * However, the basic operations do not ensure that all dead columns
  106. * or all redundant rows are detected.
  107. * isl_tab_detect_implicit_equalities and isl_tab_detect_redundant can be used
  108. * to perform an exhaustive search for dead columns and redundant rows.
  109. *
  110. * The samples matrix contains "n_sample" integer points that have at some
  111. * point been elements satisfying the tableau. The first "n_outside"
  112. * of them no longer satisfy the tableau. They are kept because they
  113. * can be reinstated during rollback when the constraint that cut them
  114. * out is removed. These samples are only maintained for the context
  115. * tableau while solving PILP problems.
  116. *
  117. * If "preserve" is set, then we want to keep all constraints in the
  118. * tableau, even if they turn out to be redundant.
  119. */
  120. enum isl_tab_row_sign {
  121. isl_tab_row_unknown = 0,
  122. isl_tab_row_pos,
  123. isl_tab_row_neg,
  124. isl_tab_row_any,
  125. };
  126. struct isl_tab {
  127. struct isl_mat *mat;
  128. unsigned n_row;
  129. unsigned n_col;
  130. unsigned n_dead;
  131. unsigned n_redundant;
  132. unsigned n_var;
  133. unsigned n_param;
  134. unsigned n_div;
  135. unsigned max_var;
  136. unsigned n_con;
  137. unsigned n_eq;
  138. unsigned max_con;
  139. struct isl_tab_var *var;
  140. struct isl_tab_var *con;
  141. int *row_var; /* v >= 0 -> var v; v < 0 -> con ~v */
  142. int *col_var; /* v >= 0 -> var v; v < 0 -> con ~v */
  143. enum isl_tab_row_sign *row_sign;
  144. struct isl_tab_undo bottom;
  145. struct isl_tab_undo *top;
  146. struct isl_vec *dual;
  147. struct isl_basic_map *bmap;
  148. unsigned n_sample;
  149. unsigned n_outside;
  150. int *sample_index;
  151. struct isl_mat *samples;
  152. int n_zero;
  153. int n_unbounded;
  154. struct isl_mat *basis;
  155. int (*conflict)(int con, void *user);
  156. void *conflict_user;
  157. unsigned strict_redundant : 1;
  158. unsigned need_undo : 1;
  159. unsigned preserve : 1;
  160. unsigned rational : 1;
  161. unsigned empty : 1;
  162. unsigned in_undo : 1;
  163. unsigned M : 1;
  164. unsigned cone : 1;
  165. };
  166. struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
  167. unsigned n_row, unsigned n_var, unsigned M);
  168. void isl_tab_free(struct isl_tab *tab);
  169. isl_ctx *isl_tab_get_ctx(struct isl_tab *tab);
  170. __isl_give struct isl_tab *isl_tab_from_basic_map(
  171. __isl_keep isl_basic_map *bmap, int track);
  172. __isl_give struct isl_tab *isl_tab_from_basic_set(
  173. __isl_keep isl_basic_set *bset, int track);
  174. struct isl_tab *isl_tab_from_recession_cone(__isl_keep isl_basic_set *bset,
  175. int parametric);
  176. isl_bool isl_tab_cone_is_bounded(struct isl_tab *tab);
  177. __isl_give isl_basic_map *isl_basic_map_update_from_tab(
  178. __isl_take isl_basic_map *bmap, struct isl_tab *tab);
  179. __isl_give isl_basic_set *isl_basic_set_update_from_tab(
  180. __isl_take isl_basic_set *bset, struct isl_tab *tab);
  181. int isl_tab_detect_implicit_equalities(struct isl_tab *tab) WARN_UNUSED;
  182. __isl_give isl_basic_map *isl_tab_make_equalities_explicit(struct isl_tab *tab,
  183. __isl_take isl_basic_map *bmap);
  184. int isl_tab_detect_redundant(struct isl_tab *tab) WARN_UNUSED;
  185. isl_stat isl_tab_restore_redundant(struct isl_tab *tab);
  186. #define ISL_TAB_SAVE_DUAL (1 << 0)
  187. enum isl_lp_result isl_tab_min(struct isl_tab *tab,
  188. isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
  189. unsigned flags) WARN_UNUSED;
  190. isl_stat isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq) WARN_UNUSED;
  191. isl_stat isl_tab_add_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
  192. int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
  193. int isl_tab_freeze_constraint(struct isl_tab *tab, int con) WARN_UNUSED;
  194. isl_stat isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
  195. WARN_UNUSED;
  196. isl_stat isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
  197. WARN_UNUSED;
  198. __isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab);
  199. int isl_tab_is_equality(struct isl_tab *tab, int con);
  200. int isl_tab_is_redundant(struct isl_tab *tab, int con);
  201. int isl_tab_sample_is_integer(struct isl_tab *tab);
  202. __isl_give isl_vec *isl_tab_get_sample_value(struct isl_tab *tab);
  203. enum isl_ineq_type {
  204. isl_ineq_error = -1,
  205. isl_ineq_redundant,
  206. isl_ineq_separate,
  207. isl_ineq_cut,
  208. isl_ineq_adj_eq,
  209. isl_ineq_adj_ineq,
  210. };
  211. enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq);
  212. struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab);
  213. isl_stat isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap) WARN_UNUSED;
  214. isl_bool isl_tab_need_undo(struct isl_tab *tab);
  215. void isl_tab_clear_undo(struct isl_tab *tab);
  216. int isl_tab_relax(struct isl_tab *tab, int con) WARN_UNUSED;
  217. int isl_tab_select_facet(struct isl_tab *tab, int con) WARN_UNUSED;
  218. int isl_tab_unrestrict(struct isl_tab *tab, int con) WARN_UNUSED;
  219. void isl_tab_dump(__isl_keep struct isl_tab *tab);
  220. /* Compute maximum instead of minimum. */
  221. #define ISL_OPT_MAX (1 << 0)
  222. /* Compute full instead of partial optimum; also, domain argument is NULL. */
  223. #define ISL_OPT_FULL (1 << 1)
  224. /* Result should be free of (unknown) quantified variables. */
  225. #define ISL_OPT_QE (1 << 2)
  226. __isl_give isl_map *isl_tab_basic_map_partial_lexopt(
  227. __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
  228. __isl_give isl_set **empty, unsigned flags);
  229. __isl_give isl_pw_multi_aff *isl_tab_basic_map_partial_lexopt_pw_multi_aff(
  230. __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
  231. __isl_give isl_set **empty, unsigned flags);
  232. /* An isl_trivial_region represents a non-triviality region.
  233. * The region is trivial if applying "trivial" to a given sequence
  234. * of variables results in a zero vector.
  235. * pos is the location (starting at 0) of the first variable in the sequence.
  236. */
  237. struct isl_trivial_region {
  238. int pos;
  239. isl_mat *trivial;
  240. };
  241. __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
  242. __isl_take isl_basic_set *bset, int n_op, int n_region,
  243. struct isl_trivial_region *region,
  244. int (*conflict)(int con, void *user), void *user);
  245. struct isl_tab_lexmin;
  246. typedef struct isl_tab_lexmin isl_tab_lexmin;
  247. __isl_give isl_tab_lexmin *isl_tab_lexmin_from_basic_set(
  248. __isl_take isl_basic_set *bset);
  249. int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin *tl);
  250. __isl_give isl_tab_lexmin *isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin *tl,
  251. isl_int *eq);
  252. __isl_give isl_tab_lexmin *isl_tab_lexmin_cut_to_integer(
  253. __isl_take isl_tab_lexmin *tl);
  254. __isl_give isl_vec *isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin *tl);
  255. __isl_null isl_tab_lexmin *isl_tab_lexmin_free(__isl_take isl_tab_lexmin *tl);
  256. /* private */
  257. struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i);
  258. int isl_tab_mark_redundant(struct isl_tab *tab, int row) WARN_UNUSED;
  259. int isl_tab_mark_rational(struct isl_tab *tab) WARN_UNUSED;
  260. isl_stat isl_tab_mark_empty(struct isl_tab *tab) WARN_UNUSED;
  261. struct isl_tab *isl_tab_dup(struct isl_tab *tab);
  262. struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2);
  263. int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new) WARN_UNUSED;
  264. int isl_tab_allocate_con(struct isl_tab *tab) WARN_UNUSED;
  265. int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new) WARN_UNUSED;
  266. int isl_tab_insert_var(struct isl_tab *tab, int pos) WARN_UNUSED;
  267. int isl_tab_pivot(struct isl_tab *tab, int row, int col) WARN_UNUSED;
  268. int isl_tab_add_row(struct isl_tab *tab, isl_int *line) WARN_UNUSED;
  269. int isl_tab_row_is_redundant(struct isl_tab *tab, int row);
  270. int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var);
  271. int isl_tab_sign_of_max(struct isl_tab *tab, int con);
  272. int isl_tab_kill_col(struct isl_tab *tab, int col) WARN_UNUSED;
  273. isl_stat isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
  274. WARN_UNUSED;
  275. isl_stat isl_tab_push_var(struct isl_tab *tab,
  276. enum isl_tab_undo_type type, struct isl_tab_var *var) WARN_UNUSED;
  277. isl_stat isl_tab_push_basis(struct isl_tab *tab) WARN_UNUSED;
  278. struct isl_tab *isl_tab_init_samples(struct isl_tab *tab) WARN_UNUSED;
  279. int isl_tab_add_sample(struct isl_tab *tab,
  280. __isl_take isl_vec *sample) WARN_UNUSED;
  281. struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s);
  282. isl_stat isl_tab_save_samples(struct isl_tab *tab) WARN_UNUSED;
  283. struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
  284. struct isl_tab *tab_cone) WARN_UNUSED;
  285. isl_bool isl_tab_is_constant(struct isl_tab *tab, int var, isl_int *value);
  286. isl_stat isl_tab_detect_constants(struct isl_tab *tab);
  287. isl_stat isl_tab_push_callback(struct isl_tab *tab,
  288. struct isl_tab_callback *callback) WARN_UNUSED;
  289. int isl_tab_insert_div(struct isl_tab *tab, int pos, __isl_keep isl_vec *div,
  290. isl_stat (*add_ineq)(void *user, isl_int *), void *user);
  291. int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div);
  292. int isl_tab_shift_var(struct isl_tab *tab, int pos, isl_int shift) WARN_UNUSED;
  293. isl_stat isl_tab_swap_constraints(struct isl_tab *tab, int con1, int con2);
  294. #endif