isl_range.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. #include <isl_ctx_private.h>
  2. #include <isl/val.h>
  3. #include <isl_constraint_private.h>
  4. #include <isl/set.h>
  5. #include <isl_polynomial_private.h>
  6. #include <isl_morph.h>
  7. #include <isl_range.h>
  8. struct range_data {
  9. struct isl_bound *bound;
  10. int *signs;
  11. int sign;
  12. int test_monotonicity;
  13. int monotonicity;
  14. int tight;
  15. isl_qpolynomial *poly;
  16. isl_pw_qpolynomial_fold *pwf;
  17. isl_pw_qpolynomial_fold *pwf_tight;
  18. };
  19. static isl_stat propagate_on_domain(__isl_take isl_basic_set *bset,
  20. __isl_take isl_qpolynomial *poly, struct range_data *data);
  21. /* Check whether the polynomial "poly" has sign "sign" over "bset",
  22. * i.e., if sign == 1, check that the lower bound on the polynomial
  23. * is non-negative and if sign == -1, check that the upper bound on
  24. * the polynomial is non-positive.
  25. */
  26. static isl_bool has_sign(__isl_keep isl_basic_set *bset,
  27. __isl_keep isl_qpolynomial *poly, int sign, int *signs)
  28. {
  29. struct range_data data_m;
  30. isl_size nparam;
  31. isl_space *space;
  32. isl_val *opt;
  33. isl_bool r;
  34. enum isl_fold type;
  35. nparam = isl_basic_set_dim(bset, isl_dim_param);
  36. if (nparam < 0)
  37. return isl_bool_error;
  38. bset = isl_basic_set_copy(bset);
  39. poly = isl_qpolynomial_copy(poly);
  40. bset = isl_basic_set_move_dims(bset, isl_dim_set, 0,
  41. isl_dim_param, 0, nparam);
  42. poly = isl_qpolynomial_move_dims(poly, isl_dim_in, 0,
  43. isl_dim_param, 0, nparam);
  44. space = isl_qpolynomial_get_space(poly);
  45. space = isl_space_params(space);
  46. space = isl_space_from_domain(space);
  47. space = isl_space_add_dims(space, isl_dim_out, 1);
  48. data_m.test_monotonicity = 0;
  49. data_m.signs = signs;
  50. data_m.sign = -sign;
  51. type = data_m.sign < 0 ? isl_fold_min : isl_fold_max;
  52. data_m.pwf = isl_pw_qpolynomial_fold_zero(space, type);
  53. data_m.tight = 0;
  54. data_m.pwf_tight = NULL;
  55. if (propagate_on_domain(bset, poly, &data_m) < 0)
  56. goto error;
  57. if (sign > 0)
  58. opt = isl_pw_qpolynomial_fold_min(data_m.pwf);
  59. else
  60. opt = isl_pw_qpolynomial_fold_max(data_m.pwf);
  61. if (!opt)
  62. r = isl_bool_error;
  63. else if (isl_val_is_nan(opt) ||
  64. isl_val_is_infty(opt) ||
  65. isl_val_is_neginfty(opt))
  66. r = isl_bool_false;
  67. else
  68. r = isl_bool_ok(sign * isl_val_sgn(opt) >= 0);
  69. isl_val_free(opt);
  70. return r;
  71. error:
  72. isl_pw_qpolynomial_fold_free(data_m.pwf);
  73. return isl_bool_error;
  74. }
  75. /* Return 1 if poly is monotonically increasing in the last set variable,
  76. * -1 if poly is monotonically decreasing in the last set variable,
  77. * 0 if no conclusion,
  78. * -2 on error.
  79. *
  80. * We simply check the sign of p(x+1)-p(x)
  81. */
  82. static int monotonicity(__isl_keep isl_basic_set *bset,
  83. __isl_keep isl_qpolynomial *poly, struct range_data *data)
  84. {
  85. isl_ctx *ctx;
  86. isl_space *space;
  87. isl_qpolynomial *sub = NULL;
  88. isl_qpolynomial *diff = NULL;
  89. int result = 0;
  90. isl_bool s;
  91. isl_size nvar;
  92. nvar = isl_basic_set_dim(bset, isl_dim_set);
  93. if (nvar < 0)
  94. return -2;
  95. ctx = isl_qpolynomial_get_ctx(poly);
  96. space = isl_qpolynomial_get_domain_space(poly);
  97. sub = isl_qpolynomial_var_on_domain(isl_space_copy(space),
  98. isl_dim_set, nvar - 1);
  99. sub = isl_qpolynomial_add(sub,
  100. isl_qpolynomial_rat_cst_on_domain(space, ctx->one, ctx->one));
  101. diff = isl_qpolynomial_substitute(isl_qpolynomial_copy(poly),
  102. isl_dim_in, nvar - 1, 1, &sub);
  103. diff = isl_qpolynomial_sub(diff, isl_qpolynomial_copy(poly));
  104. s = has_sign(bset, diff, 1, data->signs);
  105. if (s < 0)
  106. goto error;
  107. if (s)
  108. result = 1;
  109. else {
  110. s = has_sign(bset, diff, -1, data->signs);
  111. if (s < 0)
  112. goto error;
  113. if (s)
  114. result = -1;
  115. }
  116. isl_qpolynomial_free(diff);
  117. isl_qpolynomial_free(sub);
  118. return result;
  119. error:
  120. isl_qpolynomial_free(diff);
  121. isl_qpolynomial_free(sub);
  122. return -2;
  123. }
  124. /* Return a positive ("sign" > 0) or negative ("sign" < 0) infinite polynomial
  125. * with domain space "space".
  126. */
  127. static __isl_give isl_qpolynomial *signed_infty(__isl_take isl_space *space,
  128. int sign)
  129. {
  130. if (sign > 0)
  131. return isl_qpolynomial_infty_on_domain(space);
  132. else
  133. return isl_qpolynomial_neginfty_on_domain(space);
  134. }
  135. static __isl_give isl_qpolynomial *bound2poly(__isl_take isl_constraint *bound,
  136. __isl_take isl_space *space, unsigned pos, int sign)
  137. {
  138. if (!bound)
  139. return signed_infty(space, sign);
  140. isl_space_free(space);
  141. return isl_qpolynomial_from_constraint(bound, isl_dim_set, pos);
  142. }
  143. static int bound_is_integer(__isl_keep isl_constraint *bound, unsigned pos)
  144. {
  145. isl_int c;
  146. int is_int;
  147. if (!bound)
  148. return 1;
  149. isl_int_init(c);
  150. isl_constraint_get_coefficient(bound, isl_dim_set, pos, &c);
  151. is_int = isl_int_is_one(c) || isl_int_is_negone(c);
  152. isl_int_clear(c);
  153. return is_int;
  154. }
  155. struct isl_fixed_sign_data {
  156. int *signs;
  157. int sign;
  158. isl_qpolynomial *poly;
  159. };
  160. /* Add term "term" to data->poly if it has sign data->sign.
  161. * The sign is determined based on the signs of the parameters
  162. * and variables in data->signs. The integer divisions, if
  163. * any, are assumed to be non-negative.
  164. */
  165. static isl_stat collect_fixed_sign_terms(__isl_take isl_term *term, void *user)
  166. {
  167. struct isl_fixed_sign_data *data = (struct isl_fixed_sign_data *)user;
  168. isl_int n;
  169. int i;
  170. int sign;
  171. isl_size nparam;
  172. isl_size nvar;
  173. isl_size exp;
  174. nparam = isl_term_dim(term, isl_dim_param);
  175. nvar = isl_term_dim(term, isl_dim_set);
  176. if (nparam < 0 || nvar < 0)
  177. return isl_stat_error;
  178. isl_int_init(n);
  179. isl_term_get_num(term, &n);
  180. sign = isl_int_sgn(n);
  181. isl_int_clear(n);
  182. for (i = 0; i < nparam; ++i) {
  183. if (data->signs[i] > 0)
  184. continue;
  185. exp = isl_term_get_exp(term, isl_dim_param, i);
  186. if (exp < 0)
  187. return isl_stat_error;
  188. if (exp % 2)
  189. sign = -sign;
  190. }
  191. for (i = 0; i < nvar; ++i) {
  192. if (data->signs[nparam + i] > 0)
  193. continue;
  194. exp = isl_term_get_exp(term, isl_dim_set, i);
  195. if (exp < 0)
  196. return isl_stat_error;
  197. if (exp % 2)
  198. sign = -sign;
  199. }
  200. if (sign == data->sign) {
  201. isl_qpolynomial *t = isl_qpolynomial_from_term(term);
  202. data->poly = isl_qpolynomial_add(data->poly, t);
  203. } else
  204. isl_term_free(term);
  205. return isl_stat_ok;
  206. }
  207. /* Construct and return a polynomial that consists of the terms
  208. * in "poly" that have sign "sign". The integer divisions, if
  209. * any, are assumed to be non-negative.
  210. */
  211. __isl_give isl_qpolynomial *isl_qpolynomial_terms_of_sign(
  212. __isl_keep isl_qpolynomial *poly, int *signs, int sign)
  213. {
  214. isl_space *space;
  215. struct isl_fixed_sign_data data = { signs, sign };
  216. space = isl_qpolynomial_get_domain_space(poly);
  217. data.poly = isl_qpolynomial_zero_on_domain(space);
  218. if (isl_qpolynomial_foreach_term(poly, collect_fixed_sign_terms, &data) < 0)
  219. goto error;
  220. return data.poly;
  221. error:
  222. isl_qpolynomial_free(data.poly);
  223. return NULL;
  224. }
  225. /* Helper function to add a guarded polynomial to either pwf_tight or pwf,
  226. * depending on whether the result has been determined to be tight.
  227. */
  228. static isl_stat add_guarded_poly(__isl_take isl_basic_set *bset,
  229. __isl_take isl_qpolynomial *poly, struct range_data *data)
  230. {
  231. enum isl_fold type = data->sign < 0 ? isl_fold_min : isl_fold_max;
  232. isl_set *set;
  233. isl_qpolynomial_fold *fold;
  234. isl_pw_qpolynomial_fold *pwf;
  235. bset = isl_basic_set_params(bset);
  236. poly = isl_qpolynomial_project_domain_on_params(poly);
  237. fold = isl_qpolynomial_fold_alloc(type, poly);
  238. set = isl_set_from_basic_set(bset);
  239. pwf = isl_pw_qpolynomial_fold_alloc(type, set, fold);
  240. if (data->tight)
  241. data->pwf_tight = isl_pw_qpolynomial_fold_fold(
  242. data->pwf_tight, pwf);
  243. else
  244. data->pwf = isl_pw_qpolynomial_fold_fold(data->pwf, pwf);
  245. return isl_stat_ok;
  246. }
  247. /* Plug in "sub" for the variable at position "pos" in "poly".
  248. *
  249. * If "sub" is an infinite polynomial and if the variable actually
  250. * appears in "poly", then calling isl_qpolynomial_substitute
  251. * to perform the substitution may result in a NaN result.
  252. * In such cases, return positive or negative infinity instead,
  253. * depending on whether an upper bound or a lower bound is being computed,
  254. * and mark the result as not being tight.
  255. */
  256. static __isl_give isl_qpolynomial *plug_in_at_pos(
  257. __isl_take isl_qpolynomial *poly, int pos,
  258. __isl_take isl_qpolynomial *sub, struct range_data *data)
  259. {
  260. isl_bool involves, infty;
  261. involves = isl_qpolynomial_involves_dims(poly, isl_dim_in, pos, 1);
  262. if (involves < 0)
  263. goto error;
  264. if (!involves) {
  265. isl_qpolynomial_free(sub);
  266. return poly;
  267. }
  268. infty = isl_qpolynomial_is_infty(sub);
  269. if (infty >= 0 && !infty)
  270. infty = isl_qpolynomial_is_neginfty(sub);
  271. if (infty < 0)
  272. goto error;
  273. if (infty) {
  274. isl_space *space = isl_qpolynomial_get_domain_space(poly);
  275. data->tight = 0;
  276. isl_qpolynomial_free(poly);
  277. isl_qpolynomial_free(sub);
  278. return signed_infty(space, data->sign);
  279. }
  280. poly = isl_qpolynomial_substitute(poly, isl_dim_in, pos, 1, &sub);
  281. isl_qpolynomial_free(sub);
  282. return poly;
  283. error:
  284. isl_qpolynomial_free(poly);
  285. isl_qpolynomial_free(sub);
  286. return NULL;
  287. }
  288. /* Given a lower and upper bound on the final variable and constraints
  289. * on the remaining variables where these bounds are active,
  290. * eliminate the variable from data->poly based on these bounds.
  291. * If the polynomial has been determined to be monotonic
  292. * in the variable, then simply plug in the appropriate bound.
  293. * If the current polynomial is tight and if this bound is integer,
  294. * then the result is still tight. In all other cases, the results
  295. * may not be tight.
  296. * Otherwise, plug in the largest bound (in absolute value) in
  297. * the positive terms (if an upper bound is wanted) or the negative terms
  298. * (if a lower bounded is wanted) and the other bound in the other terms.
  299. *
  300. * If all variables have been eliminated, then record the result.
  301. * Ohterwise, recurse on the next variable.
  302. */
  303. static isl_stat propagate_on_bound_pair(__isl_take isl_constraint *lower,
  304. __isl_take isl_constraint *upper, __isl_take isl_basic_set *bset,
  305. void *user)
  306. {
  307. struct range_data *data = (struct range_data *)user;
  308. int save_tight = data->tight;
  309. isl_qpolynomial *poly;
  310. isl_stat r;
  311. isl_size nvar, nparam;
  312. nvar = isl_basic_set_dim(bset, isl_dim_set);
  313. nparam = isl_basic_set_dim(bset, isl_dim_param);
  314. if (nvar < 0 || nparam < 0)
  315. goto error;
  316. if (data->monotonicity) {
  317. isl_qpolynomial *sub;
  318. isl_space *space = isl_qpolynomial_get_domain_space(data->poly);
  319. if (data->monotonicity * data->sign > 0) {
  320. if (data->tight)
  321. data->tight = bound_is_integer(upper, nvar);
  322. sub = bound2poly(upper, space, nvar, 1);
  323. isl_constraint_free(lower);
  324. } else {
  325. if (data->tight)
  326. data->tight = bound_is_integer(lower, nvar);
  327. sub = bound2poly(lower, space, nvar, -1);
  328. isl_constraint_free(upper);
  329. }
  330. poly = isl_qpolynomial_copy(data->poly);
  331. poly = plug_in_at_pos(poly, nvar, sub, data);
  332. poly = isl_qpolynomial_drop_dims(poly, isl_dim_in, nvar, 1);
  333. } else {
  334. isl_qpolynomial *l, *u;
  335. isl_qpolynomial *pos, *neg;
  336. isl_space *space = isl_qpolynomial_get_domain_space(data->poly);
  337. int sign = data->sign * data->signs[nparam + nvar];
  338. data->tight = 0;
  339. u = bound2poly(upper, isl_space_copy(space), nvar, 1);
  340. l = bound2poly(lower, space, nvar, -1);
  341. pos = isl_qpolynomial_terms_of_sign(data->poly, data->signs, sign);
  342. neg = isl_qpolynomial_terms_of_sign(data->poly, data->signs, -sign);
  343. pos = plug_in_at_pos(pos, nvar, u, data);
  344. neg = plug_in_at_pos(neg, nvar, l, data);
  345. poly = isl_qpolynomial_add(pos, neg);
  346. poly = isl_qpolynomial_drop_dims(poly, isl_dim_in, nvar, 1);
  347. }
  348. if (nvar == 0)
  349. r = add_guarded_poly(bset, poly, data);
  350. else
  351. r = propagate_on_domain(bset, poly, data);
  352. data->tight = save_tight;
  353. return r;
  354. error:
  355. isl_constraint_free(lower);
  356. isl_constraint_free(upper);
  357. isl_basic_set_free(bset);
  358. return isl_stat_error;
  359. }
  360. /* Recursively perform range propagation on the polynomial "poly"
  361. * defined over the basic set "bset" and collect the results in "data".
  362. */
  363. static isl_stat propagate_on_domain(__isl_take isl_basic_set *bset,
  364. __isl_take isl_qpolynomial *poly, struct range_data *data)
  365. {
  366. isl_bool is_cst;
  367. isl_ctx *ctx;
  368. isl_qpolynomial *save_poly = data->poly;
  369. int save_monotonicity = data->monotonicity;
  370. isl_size d;
  371. d = isl_basic_set_dim(bset, isl_dim_set);
  372. is_cst = isl_qpolynomial_is_cst(poly, NULL, NULL);
  373. if (d < 0 || is_cst < 0)
  374. goto error;
  375. ctx = isl_basic_set_get_ctx(bset);
  376. isl_assert(ctx, d >= 1, goto error);
  377. if (is_cst) {
  378. bset = isl_basic_set_project_out(bset, isl_dim_set, 0, d);
  379. poly = isl_qpolynomial_drop_dims(poly, isl_dim_in, 0, d);
  380. return add_guarded_poly(bset, poly, data);
  381. }
  382. if (data->test_monotonicity)
  383. data->monotonicity = monotonicity(bset, poly, data);
  384. else
  385. data->monotonicity = 0;
  386. if (data->monotonicity < -1)
  387. goto error;
  388. data->poly = poly;
  389. if (isl_basic_set_foreach_bound_pair(bset, isl_dim_set, d - 1,
  390. &propagate_on_bound_pair, data) < 0)
  391. goto error;
  392. isl_basic_set_free(bset);
  393. isl_qpolynomial_free(poly);
  394. data->monotonicity = save_monotonicity;
  395. data->poly = save_poly;
  396. return isl_stat_ok;
  397. error:
  398. isl_basic_set_free(bset);
  399. isl_qpolynomial_free(poly);
  400. data->monotonicity = save_monotonicity;
  401. data->poly = save_poly;
  402. return isl_stat_error;
  403. }
  404. static isl_stat basic_guarded_poly_bound(__isl_take isl_basic_set *bset,
  405. void *user)
  406. {
  407. struct range_data *data = (struct range_data *)user;
  408. isl_ctx *ctx;
  409. isl_size nparam = isl_basic_set_dim(bset, isl_dim_param);
  410. isl_size dim = isl_basic_set_dim(bset, isl_dim_set);
  411. isl_size total = isl_basic_set_dim(bset, isl_dim_all);
  412. isl_stat r;
  413. data->signs = NULL;
  414. if (nparam < 0 || dim < 0 || total < 0)
  415. goto error;
  416. ctx = isl_basic_set_get_ctx(bset);
  417. data->signs = isl_alloc_array(ctx, int, total);
  418. if (isl_basic_set_dims_get_sign(bset, isl_dim_set, 0, dim,
  419. data->signs + nparam) < 0)
  420. goto error;
  421. if (isl_basic_set_dims_get_sign(bset, isl_dim_param, 0, nparam,
  422. data->signs) < 0)
  423. goto error;
  424. r = propagate_on_domain(bset, isl_qpolynomial_copy(data->poly), data);
  425. free(data->signs);
  426. return r;
  427. error:
  428. free(data->signs);
  429. isl_basic_set_free(bset);
  430. return isl_stat_error;
  431. }
  432. static isl_stat qpolynomial_bound_on_domain_range(
  433. __isl_take isl_basic_set *bset, __isl_take isl_qpolynomial *poly,
  434. struct range_data *data)
  435. {
  436. isl_size nparam = isl_basic_set_dim(bset, isl_dim_param);
  437. isl_size nvar = isl_basic_set_dim(bset, isl_dim_set);
  438. isl_set *set = NULL;
  439. if (nparam < 0 || nvar < 0)
  440. goto error;
  441. if (nvar == 0)
  442. return add_guarded_poly(bset, poly, data);
  443. set = isl_set_from_basic_set(bset);
  444. set = isl_set_split_dims(set, isl_dim_param, 0, nparam);
  445. set = isl_set_split_dims(set, isl_dim_set, 0, nvar);
  446. data->poly = poly;
  447. data->test_monotonicity = 1;
  448. if (isl_set_foreach_basic_set(set, &basic_guarded_poly_bound, data) < 0)
  449. goto error;
  450. isl_set_free(set);
  451. isl_qpolynomial_free(poly);
  452. return isl_stat_ok;
  453. error:
  454. isl_set_free(set);
  455. isl_qpolynomial_free(poly);
  456. return isl_stat_error;
  457. }
  458. isl_stat isl_qpolynomial_bound_on_domain_range(__isl_take isl_basic_set *bset,
  459. __isl_take isl_qpolynomial *poly, struct isl_bound *bound)
  460. {
  461. struct range_data data;
  462. isl_stat r;
  463. data.pwf = bound->pwf;
  464. data.pwf_tight = bound->pwf_tight;
  465. data.tight = bound->check_tight;
  466. if (bound->type == isl_fold_min)
  467. data.sign = -1;
  468. else
  469. data.sign = 1;
  470. r = qpolynomial_bound_on_domain_range(bset, poly, &data);
  471. bound->pwf = data.pwf;
  472. bound->pwf_tight = data.pwf_tight;
  473. return r;
  474. }