• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  * Copyright 2010      INRIA Saclay
4  * Copyright 2016-2017 Sven Verdoolaege
5  *
6  * Use of this software is governed by the MIT license
7  *
8  * Written by Sven Verdoolaege, K.U.Leuven, Departement
9  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10  * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12  */
13 
14 #include <isl_ctx_private.h>
15 #include "isl_map_private.h"
16 #include <isl_seq.h>
17 #include "isl_tab.h"
18 #include "isl_sample.h"
19 #include <isl_mat_private.h>
20 #include <isl_vec_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_constraint_private.h>
23 #include <isl_options_private.h>
24 #include <isl_config.h>
25 
26 #include <bset_to_bmap.c>
27 
28 /*
29  * The implementation of parametric integer linear programming in this file
30  * was inspired by the paper "Parametric Integer Programming" and the
31  * report "Solving systems of affine (in)equalities" by Paul Feautrier
32  * (and others).
33  *
34  * The strategy used for obtaining a feasible solution is different
35  * from the one used in isl_tab.c.  In particular, in isl_tab.c,
36  * upon finding a constraint that is not yet satisfied, we pivot
37  * in a row that increases the constant term of the row holding the
38  * constraint, making sure the sample solution remains feasible
39  * for all the constraints it already satisfied.
40  * Here, we always pivot in the row holding the constraint,
41  * choosing a column that induces the lexicographically smallest
42  * increment to the sample solution.
43  *
44  * By starting out from a sample value that is lexicographically
45  * smaller than any integer point in the problem space, the first
46  * feasible integer sample point we find will also be the lexicographically
47  * smallest.  If all variables can be assumed to be non-negative,
48  * then the initial sample value may be chosen equal to zero.
49  * However, we will not make this assumption.  Instead, we apply
50  * the "big parameter" trick.  Any variable x is then not directly
51  * used in the tableau, but instead it is represented by another
52  * variable x' = M + x, where M is an arbitrarily large (positive)
53  * value.  x' is therefore always non-negative, whatever the value of x.
54  * Taking as initial sample value x' = 0 corresponds to x = -M,
55  * which is always smaller than any possible value of x.
56  *
57  * The big parameter trick is used in the main tableau and
58  * also in the context tableau if isl_context_lex is used.
59  * In this case, each tableaus has its own big parameter.
60  * Before doing any real work, we check if all the parameters
61  * happen to be non-negative.  If so, we drop the column corresponding
62  * to M from the initial context tableau.
63  * If isl_context_gbr is used, then the big parameter trick is only
64  * used in the main tableau.
65  */
66 
67 struct isl_context;
68 struct isl_context_op {
69 	/* detect nonnegative parameters in context and mark them in tab */
70 	struct isl_tab *(*detect_nonnegative_parameters)(
71 			struct isl_context *context, struct isl_tab *tab);
72 	/* return temporary reference to basic set representation of context */
73 	struct isl_basic_set *(*peek_basic_set)(struct isl_context *context);
74 	/* return temporary reference to tableau representation of context */
75 	struct isl_tab *(*peek_tab)(struct isl_context *context);
76 	/* add equality; check is 1 if eq may not be valid;
77 	 * update is 1 if we may want to call ineq_sign on context later.
78 	 */
79 	void (*add_eq)(struct isl_context *context, isl_int *eq,
80 			int check, int update);
81 	/* add inequality; check is 1 if ineq may not be valid;
82 	 * update is 1 if we may want to call ineq_sign on context later.
83 	 */
84 	void (*add_ineq)(struct isl_context *context, isl_int *ineq,
85 			int check, int update);
86 	/* check sign of ineq based on previous information.
87 	 * strict is 1 if saturation should be treated as a positive sign.
88 	 */
89 	enum isl_tab_row_sign (*ineq_sign)(struct isl_context *context,
90 			isl_int *ineq, int strict);
91 	/* check if inequality maintains feasibility */
92 	int (*test_ineq)(struct isl_context *context, isl_int *ineq);
93 	/* return index of a div that corresponds to "div" */
94 	int (*get_div)(struct isl_context *context, struct isl_tab *tab,
95 			struct isl_vec *div);
96 	/* insert div "div" to context at "pos" and return non-negativity */
97 	isl_bool (*insert_div)(struct isl_context *context, int pos,
98 		__isl_keep isl_vec *div);
99 	int (*detect_equalities)(struct isl_context *context,
100 			struct isl_tab *tab);
101 	/* return row index of "best" split */
102 	int (*best_split)(struct isl_context *context, struct isl_tab *tab);
103 	/* check if context has already been determined to be empty */
104 	int (*is_empty)(struct isl_context *context);
105 	/* check if context is still usable */
106 	int (*is_ok)(struct isl_context *context);
107 	/* save a copy/snapshot of context */
108 	void *(*save)(struct isl_context *context);
109 	/* restore saved context */
110 	void (*restore)(struct isl_context *context, void *);
111 	/* discard saved context */
112 	void (*discard)(void *);
113 	/* invalidate context */
114 	void (*invalidate)(struct isl_context *context);
115 	/* free context */
116 	__isl_null struct isl_context *(*free)(struct isl_context *context);
117 };
118 
119 /* Shared parts of context representation.
120  *
121  * "n_unknown" is the number of final unknown integer divisions
122  * in the input domain.
123  */
124 struct isl_context {
125 	struct isl_context_op *op;
126 	int n_unknown;
127 };
128 
129 struct isl_context_lex {
130 	struct isl_context context;
131 	struct isl_tab *tab;
132 };
133 
134 /* A stack (linked list) of solutions of subtrees of the search space.
135  *
136  * "ma" describes the solution as a function of "dom".
137  * In particular, the domain space of "ma" is equal to the space of "dom".
138  *
139  * If "ma" is NULL, then there is no solution on "dom".
140  */
141 struct isl_partial_sol {
142 	int level;
143 	struct isl_basic_set *dom;
144 	isl_multi_aff *ma;
145 
146 	struct isl_partial_sol *next;
147 };
148 
149 struct isl_sol;
150 struct isl_sol_callback {
151 	struct isl_tab_callback callback;
152 	struct isl_sol *sol;
153 };
154 
155 /* isl_sol is an interface for constructing a solution to
156  * a parametric integer linear programming problem.
157  * Every time the algorithm reaches a state where a solution
158  * can be read off from the tableau, the function "add" is called
159  * on the isl_sol passed to find_solutions_main.  In a state where
160  * the tableau is empty, "add_empty" is called instead.
161  * "free" is called to free the implementation specific fields, if any.
162  *
163  * "error" is set if some error has occurred.  This flag invalidates
164  * the remainder of the data structure.
165  * If "rational" is set, then a rational optimization is being performed.
166  * "level" is the current level in the tree with nodes for each
167  * split in the context.
168  * If "max" is set, then a maximization problem is being solved, rather than
169  * a minimization problem, which means that the variables in the
170  * tableau have value "M - x" rather than "M + x".
171  * "n_out" is the number of output dimensions in the input.
172  * "space" is the space in which the solution (and also the input) lives.
173  *
174  * The context tableau is owned by isl_sol and is updated incrementally.
175  *
176  * There are currently two implementations of this interface,
177  * isl_sol_map, which simply collects the solutions in an isl_map
178  * and (optionally) the parts of the context where there is no solution
179  * in an isl_set, and
180  * isl_sol_pma, which collects an isl_pw_multi_aff instead.
181  */
182 struct isl_sol {
183 	int error;
184 	int rational;
185 	int level;
186 	int max;
187 	isl_size n_out;
188 	isl_space *space;
189 	struct isl_context *context;
190 	struct isl_partial_sol *partial;
191 	void (*add)(struct isl_sol *sol,
192 		__isl_take isl_basic_set *dom, __isl_take isl_multi_aff *ma);
193 	void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
194 	void (*free)(struct isl_sol *sol);
195 	struct isl_sol_callback	dec_level;
196 };
197 
sol_free(struct isl_sol * sol)198 static void sol_free(struct isl_sol *sol)
199 {
200 	struct isl_partial_sol *partial, *next;
201 	if (!sol)
202 		return;
203 	for (partial = sol->partial; partial; partial = next) {
204 		next = partial->next;
205 		isl_basic_set_free(partial->dom);
206 		isl_multi_aff_free(partial->ma);
207 		free(partial);
208 	}
209 	isl_space_free(sol->space);
210 	if (sol->context)
211 		sol->context->op->free(sol->context);
212 	sol->free(sol);
213 	free(sol);
214 }
215 
216 /* Push a partial solution represented by a domain and function "ma"
217  * onto the stack of partial solutions.
218  * If "ma" is NULL, then "dom" represents a part of the domain
219  * with no solution.
220  */
sol_push_sol(struct isl_sol * sol,__isl_take isl_basic_set * dom,__isl_take isl_multi_aff * ma)221 static void sol_push_sol(struct isl_sol *sol,
222 	__isl_take isl_basic_set *dom, __isl_take isl_multi_aff *ma)
223 {
224 	struct isl_partial_sol *partial;
225 
226 	if (sol->error || !dom)
227 		goto error;
228 
229 	partial = isl_alloc_type(dom->ctx, struct isl_partial_sol);
230 	if (!partial)
231 		goto error;
232 
233 	partial->level = sol->level;
234 	partial->dom = dom;
235 	partial->ma = ma;
236 	partial->next = sol->partial;
237 
238 	sol->partial = partial;
239 
240 	return;
241 error:
242 	isl_basic_set_free(dom);
243 	isl_multi_aff_free(ma);
244 	sol->error = 1;
245 }
246 
247 /* Check that the final columns of "M", starting at "first", are zero.
248  */
check_final_columns_are_zero(__isl_keep isl_mat * M,unsigned first)249 static isl_stat check_final_columns_are_zero(__isl_keep isl_mat *M,
250 	unsigned first)
251 {
252 	int i;
253 	isl_size rows, cols;
254 	unsigned n;
255 
256 	rows = isl_mat_rows(M);
257 	cols = isl_mat_cols(M);
258 	if (rows < 0 || cols < 0)
259 		return isl_stat_error;
260 	n = cols - first;
261 	for (i = 0; i < rows; ++i)
262 		if (isl_seq_first_non_zero(M->row[i] + first, n) != -1)
263 			isl_die(isl_mat_get_ctx(M), isl_error_internal,
264 				"final columns should be zero",
265 				return isl_stat_error);
266 	return isl_stat_ok;
267 }
268 
269 /* Set the affine expressions in "ma" according to the rows in "M", which
270  * are defined over the local space "ls".
271  * The matrix "M" may have extra (zero) columns beyond the number
272  * of variables in "ls".
273  */
set_from_affine_matrix(__isl_take isl_multi_aff * ma,__isl_take isl_local_space * ls,__isl_take isl_mat * M)274 static __isl_give isl_multi_aff *set_from_affine_matrix(
275 	__isl_take isl_multi_aff *ma, __isl_take isl_local_space *ls,
276 	__isl_take isl_mat *M)
277 {
278 	int i;
279 	isl_size dim;
280 	isl_aff *aff;
281 
282 	dim = isl_local_space_dim(ls, isl_dim_all);
283 	if (!ma || dim < 0 || !M)
284 		goto error;
285 
286 	if (check_final_columns_are_zero(M, 1 + dim) < 0)
287 		goto error;
288 	for (i = 1; i < M->n_row; ++i) {
289 		aff = isl_aff_alloc(isl_local_space_copy(ls));
290 		if (aff) {
291 			isl_int_set(aff->v->el[0], M->row[0][0]);
292 			isl_seq_cpy(aff->v->el + 1, M->row[i], 1 + dim);
293 		}
294 		aff = isl_aff_normalize(aff);
295 		ma = isl_multi_aff_set_aff(ma, i - 1, aff);
296 	}
297 	isl_local_space_free(ls);
298 	isl_mat_free(M);
299 
300 	return ma;
301 error:
302 	isl_local_space_free(ls);
303 	isl_mat_free(M);
304 	isl_multi_aff_free(ma);
305 	return NULL;
306 }
307 
308 /* Push a partial solution represented by a domain and mapping M
309  * onto the stack of partial solutions.
310  *
311  * The affine matrix "M" maps the dimensions of the context
312  * to the output variables.  Convert it into an isl_multi_aff and
313  * then call sol_push_sol.
314  *
315  * Note that the description of the initial context may have involved
316  * existentially quantified variables, in which case they also appear
317  * in "dom".  These need to be removed before creating the affine
318  * expression because an affine expression cannot be defined in terms
319  * of existentially quantified variables without a known representation.
320  * Since newly added integer divisions are inserted before these
321  * existentially quantified variables, they are still in the final
322  * positions and the corresponding final columns of "M" are zero
323  * because align_context_divs adds the existentially quantified
324  * variables of the context to the main tableau without any constraints and
325  * any equality constraints that are added later on can only serve
326  * to eliminate these existentially quantified variables.
327  */
sol_push_sol_mat(struct isl_sol * sol,__isl_take isl_basic_set * dom,__isl_take isl_mat * M)328 static void sol_push_sol_mat(struct isl_sol *sol,
329 	__isl_take isl_basic_set *dom, __isl_take isl_mat *M)
330 {
331 	isl_local_space *ls;
332 	isl_multi_aff *ma;
333 	isl_size n_div;
334 	int n_known;
335 
336 	n_div = isl_basic_set_dim(dom, isl_dim_div);
337 	if (n_div < 0)
338 		goto error;
339 	n_known = n_div - sol->context->n_unknown;
340 
341 	ma = isl_multi_aff_alloc(isl_space_copy(sol->space));
342 	ls = isl_basic_set_get_local_space(dom);
343 	ls = isl_local_space_drop_dims(ls, isl_dim_div,
344 					n_known, n_div - n_known);
345 	ma = set_from_affine_matrix(ma, ls, M);
346 
347 	if (!ma)
348 		dom = isl_basic_set_free(dom);
349 	sol_push_sol(sol, dom, ma);
350 	return;
351 error:
352 	isl_basic_set_free(dom);
353 	isl_mat_free(M);
354 	sol_push_sol(sol, NULL, NULL);
355 }
356 
357 /* Pop one partial solution from the partial solution stack and
358  * pass it on to sol->add or sol->add_empty.
359  */
sol_pop_one(struct isl_sol * sol)360 static void sol_pop_one(struct isl_sol *sol)
361 {
362 	struct isl_partial_sol *partial;
363 
364 	partial = sol->partial;
365 	sol->partial = partial->next;
366 
367 	if (partial->ma)
368 		sol->add(sol, partial->dom, partial->ma);
369 	else
370 		sol->add_empty(sol, partial->dom);
371 	free(partial);
372 }
373 
374 /* Return a fresh copy of the domain represented by the context tableau.
375  */
sol_domain(struct isl_sol * sol)376 static struct isl_basic_set *sol_domain(struct isl_sol *sol)
377 {
378 	struct isl_basic_set *bset;
379 
380 	if (sol->error)
381 		return NULL;
382 
383 	bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
384 	bset = isl_basic_set_update_from_tab(bset,
385 			sol->context->op->peek_tab(sol->context));
386 
387 	return bset;
388 }
389 
390 /* Check whether two partial solutions have the same affine expressions.
391  */
same_solution(struct isl_partial_sol * s1,struct isl_partial_sol * s2)392 static isl_bool same_solution(struct isl_partial_sol *s1,
393 	struct isl_partial_sol *s2)
394 {
395 	if (!s1->ma != !s2->ma)
396 		return isl_bool_false;
397 	if (!s1->ma)
398 		return isl_bool_true;
399 
400 	return isl_multi_aff_plain_is_equal(s1->ma, s2->ma);
401 }
402 
403 /* Swap the initial two partial solutions in "sol".
404  *
405  * That is, go from
406  *
407  *	sol->partial = p1; p1->next = p2; p2->next = p3
408  *
409  * to
410  *
411  *	sol->partial = p2; p2->next = p1; p1->next = p3
412  */
swap_initial(struct isl_sol * sol)413 static void swap_initial(struct isl_sol *sol)
414 {
415 	struct isl_partial_sol *partial;
416 
417 	partial = sol->partial;
418 	sol->partial = partial->next;
419 	partial->next = partial->next->next;
420 	sol->partial->next = partial;
421 }
422 
423 /* Combine the initial two partial solution of "sol" into
424  * a partial solution with the current context domain of "sol" and
425  * the function description of the second partial solution in the list.
426  * The level of the new partial solution is set to the current level.
427  *
428  * That is, the first two partial solutions (D1,M1) and (D2,M2) are
429  * replaced by (D,M2), where D is the domain of "sol", which is assumed
430  * to be the union of D1 and D2, while M1 is assumed to be equal to M2
431  * (at least on D1).
432  */
combine_initial_into_second(struct isl_sol * sol)433 static isl_stat combine_initial_into_second(struct isl_sol *sol)
434 {
435 	struct isl_partial_sol *partial;
436 	isl_basic_set *bset;
437 
438 	partial = sol->partial;
439 
440 	bset = sol_domain(sol);
441 	isl_basic_set_free(partial->next->dom);
442 	partial->next->dom = bset;
443 	partial->next->level = sol->level;
444 
445 	if (!bset)
446 		return isl_stat_error;
447 
448 	sol->partial = partial->next;
449 	isl_basic_set_free(partial->dom);
450 	isl_multi_aff_free(partial->ma);
451 	free(partial);
452 
453 	return isl_stat_ok;
454 }
455 
456 /* Are "ma1" and "ma2" equal to each other on "dom"?
457  *
458  * Combine "ma1" and "ma2" with "dom" and check if the results are the same.
459  * "dom" may have existentially quantified variables.  Eliminate them first
460  * as otherwise they would have to be eliminated twice, in a more complicated
461  * context.
462  */
equal_on_domain(__isl_keep isl_multi_aff * ma1,__isl_keep isl_multi_aff * ma2,__isl_keep isl_basic_set * dom)463 static isl_bool equal_on_domain(__isl_keep isl_multi_aff *ma1,
464 	__isl_keep isl_multi_aff *ma2, __isl_keep isl_basic_set *dom)
465 {
466 	isl_set *set;
467 	isl_pw_multi_aff *pma1, *pma2;
468 	isl_bool equal;
469 
470 	set = isl_basic_set_compute_divs(isl_basic_set_copy(dom));
471 	pma1 = isl_pw_multi_aff_alloc(isl_set_copy(set),
472 					isl_multi_aff_copy(ma1));
473 	pma2 = isl_pw_multi_aff_alloc(set, isl_multi_aff_copy(ma2));
474 	equal = isl_pw_multi_aff_is_equal(pma1, pma2);
475 	isl_pw_multi_aff_free(pma1);
476 	isl_pw_multi_aff_free(pma2);
477 
478 	return equal;
479 }
480 
481 /* The initial two partial solutions of "sol" are known to be at
482  * the same level.
483  * If they represent the same solution (on different parts of the domain),
484  * then combine them into a single solution at the current level.
485  * Otherwise, pop them both.
486  *
487  * Even if the two partial solution are not obviously the same,
488  * one may still be a simplification of the other over its own domain.
489  * Also check if the two sets of affine functions are equal when
490  * restricted to one of the domains.  If so, combine the two
491  * using the set of affine functions on the other domain.
492  * That is, for two partial solutions (D1,M1) and (D2,M2),
493  * if M1 = M2 on D1, then the pair of partial solutions can
494  * be replaced by (D1+D2,M2) and similarly when M1 = M2 on D2.
495  */
combine_initial_if_equal(struct isl_sol * sol)496 static isl_stat combine_initial_if_equal(struct isl_sol *sol)
497 {
498 	struct isl_partial_sol *partial;
499 	isl_bool same;
500 
501 	partial = sol->partial;
502 
503 	same = same_solution(partial, partial->next);
504 	if (same < 0)
505 		return isl_stat_error;
506 	if (same)
507 		return combine_initial_into_second(sol);
508 	if (partial->ma && partial->next->ma) {
509 		same = equal_on_domain(partial->ma, partial->next->ma,
510 					partial->dom);
511 		if (same < 0)
512 			return isl_stat_error;
513 		if (same)
514 			return combine_initial_into_second(sol);
515 		same = equal_on_domain(partial->ma, partial->next->ma,
516 					partial->next->dom);
517 		if (same) {
518 			swap_initial(sol);
519 			return combine_initial_into_second(sol);
520 		}
521 	}
522 
523 	sol_pop_one(sol);
524 	sol_pop_one(sol);
525 
526 	return isl_stat_ok;
527 }
528 
529 /* Pop all solutions from the partial solution stack that were pushed onto
530  * the stack at levels that are deeper than the current level.
531  * If the two topmost elements on the stack have the same level
532  * and represent the same solution, then their domains are combined.
533  * This combined domain is the same as the current context domain
534  * as sol_pop is called each time we move back to a higher level.
535  * If the outer level (0) has been reached, then all partial solutions
536  * at the current level are also popped off.
537  */
sol_pop(struct isl_sol * sol)538 static void sol_pop(struct isl_sol *sol)
539 {
540 	struct isl_partial_sol *partial;
541 
542 	if (sol->error)
543 		return;
544 
545 	partial = sol->partial;
546 	if (!partial)
547 		return;
548 
549 	if (partial->level == 0 && sol->level == 0) {
550 		for (partial = sol->partial; partial; partial = sol->partial)
551 			sol_pop_one(sol);
552 		return;
553 	}
554 
555 	if (partial->level <= sol->level)
556 		return;
557 
558 	if (partial->next && partial->next->level == partial->level) {
559 		if (combine_initial_if_equal(sol) < 0)
560 			goto error;
561 	} else
562 		sol_pop_one(sol);
563 
564 	if (sol->level == 0) {
565 		for (partial = sol->partial; partial; partial = sol->partial)
566 			sol_pop_one(sol);
567 		return;
568 	}
569 
570 	if (0)
571 error:		sol->error = 1;
572 }
573 
sol_dec_level(struct isl_sol * sol)574 static void sol_dec_level(struct isl_sol *sol)
575 {
576 	if (sol->error)
577 		return;
578 
579 	sol->level--;
580 
581 	sol_pop(sol);
582 }
583 
sol_dec_level_wrap(struct isl_tab_callback * cb)584 static isl_stat sol_dec_level_wrap(struct isl_tab_callback *cb)
585 {
586 	struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
587 
588 	sol_dec_level(callback->sol);
589 
590 	return callback->sol->error ? isl_stat_error : isl_stat_ok;
591 }
592 
593 /* Move down to next level and push callback onto context tableau
594  * to decrease the level again when it gets rolled back across
595  * the current state.  That is, dec_level will be called with
596  * the context tableau in the same state as it is when inc_level
597  * is called.
598  */
sol_inc_level(struct isl_sol * sol)599 static void sol_inc_level(struct isl_sol *sol)
600 {
601 	struct isl_tab *tab;
602 
603 	if (sol->error)
604 		return;
605 
606 	sol->level++;
607 	tab = sol->context->op->peek_tab(sol->context);
608 	if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
609 		sol->error = 1;
610 }
611 
scale_rows(struct isl_mat * mat,isl_int m,int n_row)612 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
613 {
614 	int i;
615 
616 	if (isl_int_is_one(m))
617 		return;
618 
619 	for (i = 0; i < n_row; ++i)
620 		isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
621 }
622 
623 /* Add the solution identified by the tableau and the context tableau.
624  *
625  * The layout of the variables is as follows.
626  *	tab->n_var is equal to the total number of variables in the input
627  *			map (including divs that were copied from the context)
628  *			+ the number of extra divs constructed
629  *      Of these, the first tab->n_param and the last tab->n_div variables
630  *	correspond to the variables in the context, i.e.,
631  *		tab->n_param + tab->n_div = context_tab->n_var
632  *	tab->n_param is equal to the number of parameters and input
633  *			dimensions in the input map
634  *	tab->n_div is equal to the number of divs in the context
635  *
636  * If there is no solution, then call add_empty with a basic set
637  * that corresponds to the context tableau.  (If add_empty is NULL,
638  * then do nothing).
639  *
640  * If there is a solution, then first construct a matrix that maps
641  * all dimensions of the context to the output variables, i.e.,
642  * the output dimensions in the input map.
643  * The divs in the input map (if any) that do not correspond to any
644  * div in the context do not appear in the solution.
645  * The algorithm will make sure that they have an integer value,
646  * but these values themselves are of no interest.
647  * We have to be careful not to drop or rearrange any divs in the
648  * context because that would change the meaning of the matrix.
649  *
650  * To extract the value of the output variables, it should be noted
651  * that we always use a big parameter M in the main tableau and so
652  * the variable stored in this tableau is not an output variable x itself, but
653  *	x' = M + x (in case of minimization)
654  * or
655  *	x' = M - x (in case of maximization)
656  * If x' appears in a column, then its optimal value is zero,
657  * which means that the optimal value of x is an unbounded number
658  * (-M for minimization and M for maximization).
659  * We currently assume that the output dimensions in the original map
660  * are bounded, so this cannot occur.
661  * Similarly, when x' appears in a row, then the coefficient of M in that
662  * row is necessarily 1.
663  * If the row in the tableau represents
664  *	d x' = c + d M + e(y)
665  * then, in case of minimization, the corresponding row in the matrix
666  * will be
667  *	a c + a e(y)
668  * with a d = m, the (updated) common denominator of the matrix.
669  * In case of maximization, the row will be
670  *	-a c - a e(y)
671  */
sol_add(struct isl_sol * sol,struct isl_tab * tab)672 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
673 {
674 	struct isl_basic_set *bset = NULL;
675 	struct isl_mat *mat = NULL;
676 	unsigned off;
677 	int row;
678 	isl_int m;
679 
680 	if (sol->error || !tab)
681 		goto error;
682 
683 	if (tab->empty && !sol->add_empty)
684 		return;
685 	if (sol->context->op->is_empty(sol->context))
686 		return;
687 
688 	bset = sol_domain(sol);
689 
690 	if (tab->empty) {
691 		sol_push_sol(sol, bset, NULL);
692 		return;
693 	}
694 
695 	off = 2 + tab->M;
696 
697 	mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
698 					    1 + tab->n_param + tab->n_div);
699 	if (!mat)
700 		goto error;
701 
702 	isl_int_init(m);
703 
704 	isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
705 	isl_int_set_si(mat->row[0][0], 1);
706 	for (row = 0; row < sol->n_out; ++row) {
707 		int i = tab->n_param + row;
708 		int r, j;
709 
710 		isl_seq_clr(mat->row[1 + row], mat->n_col);
711 		if (!tab->var[i].is_row) {
712 			if (tab->M)
713 				isl_die(mat->ctx, isl_error_invalid,
714 					"unbounded optimum", goto error2);
715 			continue;
716 		}
717 
718 		r = tab->var[i].index;
719 		if (tab->M &&
720 		    isl_int_ne(tab->mat->row[r][2], tab->mat->row[r][0]))
721 			isl_die(mat->ctx, isl_error_invalid,
722 				"unbounded optimum", goto error2);
723 		isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
724 		isl_int_divexact(m, tab->mat->row[r][0], m);
725 		scale_rows(mat, m, 1 + row);
726 		isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
727 		isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
728 		for (j = 0; j < tab->n_param; ++j) {
729 			int col;
730 			if (tab->var[j].is_row)
731 				continue;
732 			col = tab->var[j].index;
733 			isl_int_mul(mat->row[1 + row][1 + j], m,
734 				    tab->mat->row[r][off + col]);
735 		}
736 		for (j = 0; j < tab->n_div; ++j) {
737 			int col;
738 			if (tab->var[tab->n_var - tab->n_div+j].is_row)
739 				continue;
740 			col = tab->var[tab->n_var - tab->n_div+j].index;
741 			isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
742 				    tab->mat->row[r][off + col]);
743 		}
744 		if (sol->max)
745 			isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
746 				    mat->n_col);
747 	}
748 
749 	isl_int_clear(m);
750 
751 	sol_push_sol_mat(sol, bset, mat);
752 	return;
753 error2:
754 	isl_int_clear(m);
755 error:
756 	isl_basic_set_free(bset);
757 	isl_mat_free(mat);
758 	sol->error = 1;
759 }
760 
761 struct isl_sol_map {
762 	struct isl_sol	sol;
763 	struct isl_map	*map;
764 	struct isl_set	*empty;
765 };
766 
sol_map_free(struct isl_sol * sol)767 static void sol_map_free(struct isl_sol *sol)
768 {
769 	struct isl_sol_map *sol_map = (struct isl_sol_map *) sol;
770 	isl_map_free(sol_map->map);
771 	isl_set_free(sol_map->empty);
772 }
773 
774 /* This function is called for parts of the context where there is
775  * no solution, with "bset" corresponding to the context tableau.
776  * Simply add the basic set to the set "empty".
777  */
sol_map_add_empty(struct isl_sol_map * sol,struct isl_basic_set * bset)778 static void sol_map_add_empty(struct isl_sol_map *sol,
779 	struct isl_basic_set *bset)
780 {
781 	if (!bset || !sol->empty)
782 		goto error;
783 
784 	sol->empty = isl_set_grow(sol->empty, 1);
785 	bset = isl_basic_set_simplify(bset);
786 	bset = isl_basic_set_finalize(bset);
787 	sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
788 	if (!sol->empty)
789 		goto error;
790 	isl_basic_set_free(bset);
791 	return;
792 error:
793 	isl_basic_set_free(bset);
794 	sol->sol.error = 1;
795 }
796 
sol_map_add_empty_wrap(struct isl_sol * sol,struct isl_basic_set * bset)797 static void sol_map_add_empty_wrap(struct isl_sol *sol,
798 	struct isl_basic_set *bset)
799 {
800 	sol_map_add_empty((struct isl_sol_map *)sol, bset);
801 }
802 
803 /* Given a basic set "dom" that represents the context and a tuple of
804  * affine expressions "ma" defined over this domain, construct a basic map
805  * that expresses this function on the domain.
806  */
sol_map_add(struct isl_sol_map * sol,__isl_take isl_basic_set * dom,__isl_take isl_multi_aff * ma)807 static void sol_map_add(struct isl_sol_map *sol,
808 	__isl_take isl_basic_set *dom, __isl_take isl_multi_aff *ma)
809 {
810 	isl_basic_map *bmap;
811 
812 	if (sol->sol.error || !dom || !ma)
813 		goto error;
814 
815 	bmap = isl_basic_map_from_multi_aff2(ma, sol->sol.rational);
816 	bmap = isl_basic_map_intersect_domain(bmap, dom);
817 	sol->map = isl_map_grow(sol->map, 1);
818 	sol->map = isl_map_add_basic_map(sol->map, bmap);
819 	if (!sol->map)
820 		sol->sol.error = 1;
821 	return;
822 error:
823 	isl_basic_set_free(dom);
824 	isl_multi_aff_free(ma);
825 	sol->sol.error = 1;
826 }
827 
sol_map_add_wrap(struct isl_sol * sol,__isl_take isl_basic_set * dom,__isl_take isl_multi_aff * ma)828 static void sol_map_add_wrap(struct isl_sol *sol,
829 	__isl_take isl_basic_set *dom, __isl_take isl_multi_aff *ma)
830 {
831 	sol_map_add((struct isl_sol_map *)sol, dom, ma);
832 }
833 
834 
835 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
836  * i.e., the constant term and the coefficients of all variables that
837  * appear in the context tableau.
838  * Note that the coefficient of the big parameter M is NOT copied.
839  * The context tableau may not have a big parameter and even when it
840  * does, it is a different big parameter.
841  */
get_row_parameter_line(struct isl_tab * tab,int row,isl_int * line)842 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
843 {
844 	int i;
845 	unsigned off = 2 + tab->M;
846 
847 	isl_int_set(line[0], tab->mat->row[row][1]);
848 	for (i = 0; i < tab->n_param; ++i) {
849 		if (tab->var[i].is_row)
850 			isl_int_set_si(line[1 + i], 0);
851 		else {
852 			int col = tab->var[i].index;
853 			isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
854 		}
855 	}
856 	for (i = 0; i < tab->n_div; ++i) {
857 		if (tab->var[tab->n_var - tab->n_div + i].is_row)
858 			isl_int_set_si(line[1 + tab->n_param + i], 0);
859 		else {
860 			int col = tab->var[tab->n_var - tab->n_div + i].index;
861 			isl_int_set(line[1 + tab->n_param + i],
862 				    tab->mat->row[row][off + col]);
863 		}
864 	}
865 }
866 
867 /* Check if rows "row1" and "row2" have identical "parametric constants",
868  * as explained above.
869  * In this case, we also insist that the coefficients of the big parameter
870  * be the same as the values of the constants will only be the same
871  * if these coefficients are also the same.
872  */
identical_parameter_line(struct isl_tab * tab,int row1,int row2)873 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
874 {
875 	int i;
876 	unsigned off = 2 + tab->M;
877 
878 	if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
879 		return 0;
880 
881 	if (tab->M && isl_int_ne(tab->mat->row[row1][2],
882 				 tab->mat->row[row2][2]))
883 		return 0;
884 
885 	for (i = 0; i < tab->n_param + tab->n_div; ++i) {
886 		int pos = i < tab->n_param ? i :
887 			tab->n_var - tab->n_div + i - tab->n_param;
888 		int col;
889 
890 		if (tab->var[pos].is_row)
891 			continue;
892 		col = tab->var[pos].index;
893 		if (isl_int_ne(tab->mat->row[row1][off + col],
894 			       tab->mat->row[row2][off + col]))
895 			return 0;
896 	}
897 	return 1;
898 }
899 
900 /* Return an inequality that expresses that the "parametric constant"
901  * should be non-negative.
902  * This function is only called when the coefficient of the big parameter
903  * is equal to zero.
904  */
get_row_parameter_ineq(struct isl_tab * tab,int row)905 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
906 {
907 	struct isl_vec *ineq;
908 
909 	ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
910 	if (!ineq)
911 		return NULL;
912 
913 	get_row_parameter_line(tab, row, ineq->el);
914 	if (ineq)
915 		ineq = isl_vec_normalize(ineq);
916 
917 	return ineq;
918 }
919 
920 /* Normalize a div expression of the form
921  *
922  *	[(g*f(x) + c)/(g * m)]
923  *
924  * with c the constant term and f(x) the remaining coefficients, to
925  *
926  *	[(f(x) + [c/g])/m]
927  */
normalize_div(__isl_keep isl_vec * div)928 static void normalize_div(__isl_keep isl_vec *div)
929 {
930 	isl_ctx *ctx = isl_vec_get_ctx(div);
931 	int len = div->size - 2;
932 
933 	isl_seq_gcd(div->el + 2, len, &ctx->normalize_gcd);
934 	isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, div->el[0]);
935 
936 	if (isl_int_is_one(ctx->normalize_gcd))
937 		return;
938 
939 	isl_int_divexact(div->el[0], div->el[0], ctx->normalize_gcd);
940 	isl_int_fdiv_q(div->el[1], div->el[1], ctx->normalize_gcd);
941 	isl_seq_scale_down(div->el + 2, div->el + 2, ctx->normalize_gcd, len);
942 }
943 
944 /* Return an integer division for use in a parametric cut based
945  * on the given row.
946  * In particular, let the parametric constant of the row be
947  *
948  *		\sum_i a_i y_i
949  *
950  * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
951  * The div returned is equal to
952  *
953  *		floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
954  */
get_row_parameter_div(struct isl_tab * tab,int row)955 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
956 {
957 	struct isl_vec *div;
958 
959 	div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
960 	if (!div)
961 		return NULL;
962 
963 	isl_int_set(div->el[0], tab->mat->row[row][0]);
964 	get_row_parameter_line(tab, row, div->el + 1);
965 	isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
966 	normalize_div(div);
967 	isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
968 
969 	return div;
970 }
971 
972 /* Return an integer division for use in transferring an integrality constraint
973  * to the context.
974  * In particular, let the parametric constant of the row be
975  *
976  *		\sum_i a_i y_i
977  *
978  * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
979  * The the returned div is equal to
980  *
981  *		floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
982  */
get_row_split_div(struct isl_tab * tab,int row)983 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
984 {
985 	struct isl_vec *div;
986 
987 	div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
988 	if (!div)
989 		return NULL;
990 
991 	isl_int_set(div->el[0], tab->mat->row[row][0]);
992 	get_row_parameter_line(tab, row, div->el + 1);
993 	normalize_div(div);
994 	isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
995 
996 	return div;
997 }
998 
999 /* Construct and return an inequality that expresses an upper bound
1000  * on the given div.
1001  * In particular, if the div is given by
1002  *
1003  *	d = floor(e/m)
1004  *
1005  * then the inequality expresses
1006  *
1007  *	m d <= e
1008  */
ineq_for_div(__isl_keep isl_basic_set * bset,unsigned div)1009 static __isl_give isl_vec *ineq_for_div(__isl_keep isl_basic_set *bset,
1010 	unsigned div)
1011 {
1012 	isl_size total;
1013 	unsigned div_pos;
1014 	struct isl_vec *ineq;
1015 
1016 	total = isl_basic_set_dim(bset, isl_dim_all);
1017 	if (total < 0)
1018 		return NULL;
1019 
1020 	div_pos = 1 + total - bset->n_div + div;
1021 
1022 	ineq = isl_vec_alloc(bset->ctx, 1 + total);
1023 	if (!ineq)
1024 		return NULL;
1025 
1026 	isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
1027 	isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
1028 	return ineq;
1029 }
1030 
1031 /* Given a row in the tableau and a div that was created
1032  * using get_row_split_div and that has been constrained to equality, i.e.,
1033  *
1034  *		d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
1035  *
1036  * replace the expression "\sum_i {a_i} y_i" in the row by d,
1037  * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
1038  * The coefficients of the non-parameters in the tableau have been
1039  * verified to be integral.  We can therefore simply replace coefficient b
1040  * by floor(b).  For the coefficients of the parameters we have
1041  * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
1042  * floor(b) = b.
1043  */
set_row_cst_to_div(struct isl_tab * tab,int row,int div)1044 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
1045 {
1046 	isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
1047 			tab->mat->row[row][0], 1 + tab->M + tab->n_col);
1048 
1049 	isl_int_set_si(tab->mat->row[row][0], 1);
1050 
1051 	if (tab->var[tab->n_var - tab->n_div + div].is_row) {
1052 		int drow = tab->var[tab->n_var - tab->n_div + div].index;
1053 
1054 		isl_assert(tab->mat->ctx,
1055 			isl_int_is_one(tab->mat->row[drow][0]), goto error);
1056 		isl_seq_combine(tab->mat->row[row] + 1,
1057 			tab->mat->ctx->one, tab->mat->row[row] + 1,
1058 			tab->mat->ctx->one, tab->mat->row[drow] + 1,
1059 			1 + tab->M + tab->n_col);
1060 	} else {
1061 		int dcol = tab->var[tab->n_var - tab->n_div + div].index;
1062 
1063 		isl_int_add_ui(tab->mat->row[row][2 + tab->M + dcol],
1064 				tab->mat->row[row][2 + tab->M + dcol], 1);
1065 	}
1066 
1067 	return tab;
1068 error:
1069 	isl_tab_free(tab);
1070 	return NULL;
1071 }
1072 
1073 /* Check if the (parametric) constant of the given row is obviously
1074  * negative, meaning that we don't need to consult the context tableau.
1075  * If there is a big parameter and its coefficient is non-zero,
1076  * then this coefficient determines the outcome.
1077  * Otherwise, we check whether the constant is negative and
1078  * all non-zero coefficients of parameters are negative and
1079  * belong to non-negative parameters.
1080  */
is_obviously_neg(struct isl_tab * tab,int row)1081 static int is_obviously_neg(struct isl_tab *tab, int row)
1082 {
1083 	int i;
1084 	int col;
1085 	unsigned off = 2 + tab->M;
1086 
1087 	if (tab->M) {
1088 		if (isl_int_is_pos(tab->mat->row[row][2]))
1089 			return 0;
1090 		if (isl_int_is_neg(tab->mat->row[row][2]))
1091 			return 1;
1092 	}
1093 
1094 	if (isl_int_is_nonneg(tab->mat->row[row][1]))
1095 		return 0;
1096 	for (i = 0; i < tab->n_param; ++i) {
1097 		/* Eliminated parameter */
1098 		if (tab->var[i].is_row)
1099 			continue;
1100 		col = tab->var[i].index;
1101 		if (isl_int_is_zero(tab->mat->row[row][off + col]))
1102 			continue;
1103 		if (!tab->var[i].is_nonneg)
1104 			return 0;
1105 		if (isl_int_is_pos(tab->mat->row[row][off + col]))
1106 			return 0;
1107 	}
1108 	for (i = 0; i < tab->n_div; ++i) {
1109 		if (tab->var[tab->n_var - tab->n_div + i].is_row)
1110 			continue;
1111 		col = tab->var[tab->n_var - tab->n_div + i].index;
1112 		if (isl_int_is_zero(tab->mat->row[row][off + col]))
1113 			continue;
1114 		if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
1115 			return 0;
1116 		if (isl_int_is_pos(tab->mat->row[row][off + col]))
1117 			return 0;
1118 	}
1119 	return 1;
1120 }
1121 
1122 /* Check if the (parametric) constant of the given row is obviously
1123  * non-negative, meaning that we don't need to consult the context tableau.
1124  * If there is a big parameter and its coefficient is non-zero,
1125  * then this coefficient determines the outcome.
1126  * Otherwise, we check whether the constant is non-negative and
1127  * all non-zero coefficients of parameters are positive and
1128  * belong to non-negative parameters.
1129  */
is_obviously_nonneg(struct isl_tab * tab,int row)1130 static int is_obviously_nonneg(struct isl_tab *tab, int row)
1131 {
1132 	int i;
1133 	int col;
1134 	unsigned off = 2 + tab->M;
1135 
1136 	if (tab->M) {
1137 		if (isl_int_is_pos(tab->mat->row[row][2]))
1138 			return 1;
1139 		if (isl_int_is_neg(tab->mat->row[row][2]))
1140 			return 0;
1141 	}
1142 
1143 	if (isl_int_is_neg(tab->mat->row[row][1]))
1144 		return 0;
1145 	for (i = 0; i < tab->n_param; ++i) {
1146 		/* Eliminated parameter */
1147 		if (tab->var[i].is_row)
1148 			continue;
1149 		col = tab->var[i].index;
1150 		if (isl_int_is_zero(tab->mat->row[row][off + col]))
1151 			continue;
1152 		if (!tab->var[i].is_nonneg)
1153 			return 0;
1154 		if (isl_int_is_neg(tab->mat->row[row][off + col]))
1155 			return 0;
1156 	}
1157 	for (i = 0; i < tab->n_div; ++i) {
1158 		if (tab->var[tab->n_var - tab->n_div + i].is_row)
1159 			continue;
1160 		col = tab->var[tab->n_var - tab->n_div + i].index;
1161 		if (isl_int_is_zero(tab->mat->row[row][off + col]))
1162 			continue;
1163 		if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
1164 			return 0;
1165 		if (isl_int_is_neg(tab->mat->row[row][off + col]))
1166 			return 0;
1167 	}
1168 	return 1;
1169 }
1170 
1171 /* Given a row r and two columns, return the column that would
1172  * lead to the lexicographically smallest increment in the sample
1173  * solution when leaving the basis in favor of the row.
1174  * Pivoting with column c will increment the sample value by a non-negative
1175  * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1176  * corresponding to the non-parametric variables.
1177  * If variable v appears in a column c_v, then a_{v,c} = 1 iff c = c_v,
1178  * with all other entries in this virtual row equal to zero.
1179  * If variable v appears in a row, then a_{v,c} is the element in column c
1180  * of that row.
1181  *
1182  * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1183  * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1184  * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1185  * increment.  Otherwise, it's c2.
1186  */
lexmin_col_pair(struct isl_tab * tab,int row,int col1,int col2,isl_int tmp)1187 static int lexmin_col_pair(struct isl_tab *tab,
1188 	int row, int col1, int col2, isl_int tmp)
1189 {
1190 	int i;
1191 	isl_int *tr;
1192 
1193 	tr = tab->mat->row[row] + 2 + tab->M;
1194 
1195 	for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1196 		int s1, s2;
1197 		isl_int *r;
1198 
1199 		if (!tab->var[i].is_row) {
1200 			if (tab->var[i].index == col1)
1201 				return col2;
1202 			if (tab->var[i].index == col2)
1203 				return col1;
1204 			continue;
1205 		}
1206 
1207 		if (tab->var[i].index == row)
1208 			continue;
1209 
1210 		r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
1211 		s1 = isl_int_sgn(r[col1]);
1212 		s2 = isl_int_sgn(r[col2]);
1213 		if (s1 == 0 && s2 == 0)
1214 			continue;
1215 		if (s1 < s2)
1216 			return col1;
1217 		if (s2 < s1)
1218 			return col2;
1219 
1220 		isl_int_mul(tmp, r[col2], tr[col1]);
1221 		isl_int_submul(tmp, r[col1], tr[col2]);
1222 		if (isl_int_is_pos(tmp))
1223 			return col1;
1224 		if (isl_int_is_neg(tmp))
1225 			return col2;
1226 	}
1227 	return -1;
1228 }
1229 
1230 /* Does the index into the tab->var or tab->con array "index"
1231  * correspond to a variable in the context tableau?
1232  * In particular, it needs to be an index into the tab->var array and
1233  * it needs to refer to either one of the first tab->n_param variables or
1234  * one of the last tab->n_div variables.
1235  */
is_parameter_var(struct isl_tab * tab,int index)1236 static int is_parameter_var(struct isl_tab *tab, int index)
1237 {
1238 	if (index < 0)
1239 		return 0;
1240 	if (index < tab->n_param)
1241 		return 1;
1242 	if (index >= tab->n_var - tab->n_div)
1243 		return 1;
1244 	return 0;
1245 }
1246 
1247 /* Does column "col" of "tab" refer to a variable in the context tableau?
1248  */
col_is_parameter_var(struct isl_tab * tab,int col)1249 static int col_is_parameter_var(struct isl_tab *tab, int col)
1250 {
1251 	return is_parameter_var(tab, tab->col_var[col]);
1252 }
1253 
1254 /* Does row "row" of "tab" refer to a variable in the context tableau?
1255  */
row_is_parameter_var(struct isl_tab * tab,int row)1256 static int row_is_parameter_var(struct isl_tab *tab, int row)
1257 {
1258 	return is_parameter_var(tab, tab->row_var[row]);
1259 }
1260 
1261 /* Given a row in the tableau, find and return the column that would
1262  * result in the lexicographically smallest, but positive, increment
1263  * in the sample point.
1264  * If there is no such column, then return tab->n_col.
1265  * If anything goes wrong, return -1.
1266  */
lexmin_pivot_col(struct isl_tab * tab,int row)1267 static int lexmin_pivot_col(struct isl_tab *tab, int row)
1268 {
1269 	int j;
1270 	int col = tab->n_col;
1271 	isl_int *tr;
1272 	isl_int tmp;
1273 
1274 	tr = tab->mat->row[row] + 2 + tab->M;
1275 
1276 	isl_int_init(tmp);
1277 
1278 	for (j = tab->n_dead; j < tab->n_col; ++j) {
1279 		if (col_is_parameter_var(tab, j))
1280 			continue;
1281 
1282 		if (!isl_int_is_pos(tr[j]))
1283 			continue;
1284 
1285 		if (col == tab->n_col)
1286 			col = j;
1287 		else
1288 			col = lexmin_col_pair(tab, row, col, j, tmp);
1289 		isl_assert(tab->mat->ctx, col >= 0, goto error);
1290 	}
1291 
1292 	isl_int_clear(tmp);
1293 	return col;
1294 error:
1295 	isl_int_clear(tmp);
1296 	return -1;
1297 }
1298 
1299 /* Return the first known violated constraint, i.e., a non-negative
1300  * constraint that currently has an either obviously negative value
1301  * or a previously determined to be negative value.
1302  *
1303  * If any constraint has a negative coefficient for the big parameter,
1304  * if any, then we return one of these first.
1305  */
first_neg(struct isl_tab * tab)1306 static int first_neg(struct isl_tab *tab)
1307 {
1308 	int row;
1309 
1310 	if (tab->M)
1311 		for (row = tab->n_redundant; row < tab->n_row; ++row) {
1312 			if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1313 				continue;
1314 			if (!isl_int_is_neg(tab->mat->row[row][2]))
1315 				continue;
1316 			if (tab->row_sign)
1317 				tab->row_sign[row] = isl_tab_row_neg;
1318 			return row;
1319 		}
1320 	for (row = tab->n_redundant; row < tab->n_row; ++row) {
1321 		if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1322 			continue;
1323 		if (tab->row_sign) {
1324 			if (tab->row_sign[row] == 0 &&
1325 			    is_obviously_neg(tab, row))
1326 				tab->row_sign[row] = isl_tab_row_neg;
1327 			if (tab->row_sign[row] != isl_tab_row_neg)
1328 				continue;
1329 		} else if (!is_obviously_neg(tab, row))
1330 			continue;
1331 		return row;
1332 	}
1333 	return -1;
1334 }
1335 
1336 /* Check whether the invariant that all columns are lexico-positive
1337  * is satisfied.  This function is not called from the current code
1338  * but is useful during debugging.
1339  */
1340 static void check_lexpos(struct isl_tab *tab) __attribute__ ((unused));
check_lexpos(struct isl_tab * tab)1341 static void check_lexpos(struct isl_tab *tab)
1342 {
1343 	unsigned off = 2 + tab->M;
1344 	int col;
1345 	int var;
1346 	int row;
1347 
1348 	for (col = tab->n_dead; col < tab->n_col; ++col) {
1349 		if (col_is_parameter_var(tab, col))
1350 			continue;
1351 		for (var = tab->n_param; var < tab->n_var - tab->n_div; ++var) {
1352 			if (!tab->var[var].is_row) {
1353 				if (tab->var[var].index == col)
1354 					break;
1355 				else
1356 					continue;
1357 			}
1358 			row = tab->var[var].index;
1359 			if (isl_int_is_zero(tab->mat->row[row][off + col]))
1360 				continue;
1361 			if (isl_int_is_pos(tab->mat->row[row][off + col]))
1362 				break;
1363 			fprintf(stderr, "lexneg column %d (row %d)\n",
1364 				col, row);
1365 		}
1366 		if (var >= tab->n_var - tab->n_div)
1367 			fprintf(stderr, "zero column %d\n", col);
1368 	}
1369 }
1370 
1371 /* Report to the caller that the given constraint is part of an encountered
1372  * conflict.
1373  */
report_conflicting_constraint(struct isl_tab * tab,int con)1374 static int report_conflicting_constraint(struct isl_tab *tab, int con)
1375 {
1376 	return tab->conflict(con, tab->conflict_user);
1377 }
1378 
1379 /* Given a conflicting row in the tableau, report all constraints
1380  * involved in the row to the caller.  That is, the row itself
1381  * (if it represents a constraint) and all constraint columns with
1382  * non-zero (and therefore negative) coefficients.
1383  */
report_conflict(struct isl_tab * tab,int row)1384 static int report_conflict(struct isl_tab *tab, int row)
1385 {
1386 	int j;
1387 	isl_int *tr;
1388 
1389 	if (!tab->conflict)
1390 		return 0;
1391 
1392 	if (tab->row_var[row] < 0 &&
1393 	    report_conflicting_constraint(tab, ~tab->row_var[row]) < 0)
1394 		return -1;
1395 
1396 	tr = tab->mat->row[row] + 2 + tab->M;
1397 
1398 	for (j = tab->n_dead; j < tab->n_col; ++j) {
1399 		if (col_is_parameter_var(tab, j))
1400 			continue;
1401 
1402 		if (!isl_int_is_neg(tr[j]))
1403 			continue;
1404 
1405 		if (tab->col_var[j] < 0 &&
1406 		    report_conflicting_constraint(tab, ~tab->col_var[j]) < 0)
1407 			return -1;
1408 	}
1409 
1410 	return 0;
1411 }
1412 
1413 /* Resolve all known or obviously violated constraints through pivoting.
1414  * In particular, as long as we can find any violated constraint, we
1415  * look for a pivoting column that would result in the lexicographically
1416  * smallest increment in the sample point.  If there is no such column
1417  * then the tableau is infeasible.
1418  */
1419 static int restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
restore_lexmin(struct isl_tab * tab)1420 static int restore_lexmin(struct isl_tab *tab)
1421 {
1422 	int row, col;
1423 
1424 	if (!tab)
1425 		return -1;
1426 	if (tab->empty)
1427 		return 0;
1428 	while ((row = first_neg(tab)) != -1) {
1429 		col = lexmin_pivot_col(tab, row);
1430 		if (col >= tab->n_col) {
1431 			if (report_conflict(tab, row) < 0)
1432 				return -1;
1433 			if (isl_tab_mark_empty(tab) < 0)
1434 				return -1;
1435 			return 0;
1436 		}
1437 		if (col < 0)
1438 			return -1;
1439 		if (isl_tab_pivot(tab, row, col) < 0)
1440 			return -1;
1441 	}
1442 	return 0;
1443 }
1444 
1445 /* Given a row that represents an equality, look for an appropriate
1446  * pivoting column.
1447  * In particular, if there are any non-zero coefficients among
1448  * the non-parameter variables, then we take the last of these
1449  * variables.  Eliminating this variable in terms of the other
1450  * variables and/or parameters does not influence the property
1451  * that all column in the initial tableau are lexicographically
1452  * positive.  The row corresponding to the eliminated variable
1453  * will only have non-zero entries below the diagonal of the
1454  * initial tableau.  That is, we transform
1455  *
1456  *		I				I
1457  *		  1		into		a
1458  *		    I				  I
1459  *
1460  * If there is no such non-parameter variable, then we are dealing with
1461  * pure parameter equality and we pick any parameter with coefficient 1 or -1
1462  * for elimination.  This will ensure that the eliminated parameter
1463  * always has an integer value whenever all the other parameters are integral.
1464  * If there is no such parameter then we return -1.
1465  */
last_var_col_or_int_par_col(struct isl_tab * tab,int row)1466 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
1467 {
1468 	unsigned off = 2 + tab->M;
1469 	int i;
1470 
1471 	for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
1472 		int col;
1473 		if (tab->var[i].is_row)
1474 			continue;
1475 		col = tab->var[i].index;
1476 		if (col <= tab->n_dead)
1477 			continue;
1478 		if (!isl_int_is_zero(tab->mat->row[row][off + col]))
1479 			return col;
1480 	}
1481 	for (i = tab->n_dead; i < tab->n_col; ++i) {
1482 		if (isl_int_is_one(tab->mat->row[row][off + i]))
1483 			return i;
1484 		if (isl_int_is_negone(tab->mat->row[row][off + i]))
1485 			return i;
1486 	}
1487 	return -1;
1488 }
1489 
1490 /* Add an equality that is known to be valid to the tableau.
1491  * We first check if we can eliminate a variable or a parameter.
1492  * If not, we add the equality as two inequalities.
1493  * In this case, the equality was a pure parameter equality and there
1494  * is no need to resolve any constraint violations.
1495  *
1496  * This function assumes that at least two more rows and at least
1497  * two more elements in the constraint array are available in the tableau.
1498  */
add_lexmin_valid_eq(struct isl_tab * tab,isl_int * eq)1499 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1500 {
1501 	int i;
1502 	int r;
1503 
1504 	if (!tab)
1505 		return NULL;
1506 	r = isl_tab_add_row(tab, eq);
1507 	if (r < 0)
1508 		goto error;
1509 
1510 	r = tab->con[r].index;
1511 	i = last_var_col_or_int_par_col(tab, r);
1512 	if (i < 0) {
1513 		tab->con[r].is_nonneg = 1;
1514 		if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1515 			goto error;
1516 		isl_seq_neg(eq, eq, 1 + tab->n_var);
1517 		r = isl_tab_add_row(tab, eq);
1518 		if (r < 0)
1519 			goto error;
1520 		tab->con[r].is_nonneg = 1;
1521 		if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1522 			goto error;
1523 	} else {
1524 		if (isl_tab_pivot(tab, r, i) < 0)
1525 			goto error;
1526 		if (isl_tab_kill_col(tab, i) < 0)
1527 			goto error;
1528 		tab->n_eq++;
1529 	}
1530 
1531 	return tab;
1532 error:
1533 	isl_tab_free(tab);
1534 	return NULL;
1535 }
1536 
1537 /* Check if the given row is a pure constant.
1538  */
is_constant(struct isl_tab * tab,int row)1539 static int is_constant(struct isl_tab *tab, int row)
1540 {
1541 	unsigned off = 2 + tab->M;
1542 
1543 	return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1544 					tab->n_col - tab->n_dead) == -1;
1545 }
1546 
1547 /* Is the given row a parametric constant?
1548  * That is, does it only involve variables that also appear in the context?
1549  */
is_parametric_constant(struct isl_tab * tab,int row)1550 static int is_parametric_constant(struct isl_tab *tab, int row)
1551 {
1552 	unsigned off = 2 + tab->M;
1553 	int col;
1554 
1555 	for (col = tab->n_dead; col < tab->n_col; ++col) {
1556 		if (col_is_parameter_var(tab, col))
1557 			continue;
1558 		if (isl_int_is_zero(tab->mat->row[row][off + col]))
1559 			continue;
1560 		return 0;
1561 	}
1562 
1563 	return 1;
1564 }
1565 
1566 /* Add an equality that may or may not be valid to the tableau.
1567  * If the resulting row is a pure constant, then it must be zero.
1568  * Otherwise, the resulting tableau is empty.
1569  *
1570  * If the row is not a pure constant, then we add two inequalities,
1571  * each time checking that they can be satisfied.
1572  * In the end we try to use one of the two constraints to eliminate
1573  * a column.
1574  *
1575  * This function assumes that at least two more rows and at least
1576  * two more elements in the constraint array are available in the tableau.
1577  */
1578 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
add_lexmin_eq(struct isl_tab * tab,isl_int * eq)1579 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1580 {
1581 	int r1, r2;
1582 	int row;
1583 	struct isl_tab_undo *snap;
1584 
1585 	if (!tab)
1586 		return -1;
1587 	snap = isl_tab_snap(tab);
1588 	r1 = isl_tab_add_row(tab, eq);
1589 	if (r1 < 0)
1590 		return -1;
1591 	tab->con[r1].is_nonneg = 1;
1592 	if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1593 		return -1;
1594 
1595 	row = tab->con[r1].index;
1596 	if (is_constant(tab, row)) {
1597 		if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1598 		    (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1599 			if (isl_tab_mark_empty(tab) < 0)
1600 				return -1;
1601 			return 0;
1602 		}
1603 		if (isl_tab_rollback(tab, snap) < 0)
1604 			return -1;
1605 		return 0;
1606 	}
1607 
1608 	if (restore_lexmin(tab) < 0)
1609 		return -1;
1610 	if (tab->empty)
1611 		return 0;
1612 
1613 	isl_seq_neg(eq, eq, 1 + tab->n_var);
1614 
1615 	r2 = isl_tab_add_row(tab, eq);
1616 	if (r2 < 0)
1617 		return -1;
1618 	tab->con[r2].is_nonneg = 1;
1619 	if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1620 		return -1;
1621 
1622 	if (restore_lexmin(tab) < 0)
1623 		return -1;
1624 	if (tab->empty)
1625 		return 0;
1626 
1627 	if (!tab->con[r1].is_row) {
1628 		if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1629 			return -1;
1630 	} else if (!tab->con[r2].is_row) {
1631 		if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1632 			return -1;
1633 	}
1634 
1635 	if (tab->bmap) {
1636 		tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1637 		if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1638 			return -1;
1639 		isl_seq_neg(eq, eq, 1 + tab->n_var);
1640 		tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1641 		isl_seq_neg(eq, eq, 1 + tab->n_var);
1642 		if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1643 			return -1;
1644 		if (!tab->bmap)
1645 			return -1;
1646 	}
1647 
1648 	return 0;
1649 }
1650 
1651 /* Add an inequality to the tableau, resolving violations using
1652  * restore_lexmin.
1653  *
1654  * This function assumes that at least one more row and at least
1655  * one more element in the constraint array are available in the tableau.
1656  */
add_lexmin_ineq(struct isl_tab * tab,isl_int * ineq)1657 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1658 {
1659 	int r;
1660 
1661 	if (!tab)
1662 		return NULL;
1663 	if (tab->bmap) {
1664 		tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1665 		if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1666 			goto error;
1667 		if (!tab->bmap)
1668 			goto error;
1669 	}
1670 	r = isl_tab_add_row(tab, ineq);
1671 	if (r < 0)
1672 		goto error;
1673 	tab->con[r].is_nonneg = 1;
1674 	if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1675 		goto error;
1676 	if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1677 		if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1678 			goto error;
1679 		return tab;
1680 	}
1681 
1682 	if (restore_lexmin(tab) < 0)
1683 		goto error;
1684 	if (!tab->empty && tab->con[r].is_row &&
1685 		 isl_tab_row_is_redundant(tab, tab->con[r].index))
1686 		if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1687 			goto error;
1688 	return tab;
1689 error:
1690 	isl_tab_free(tab);
1691 	return NULL;
1692 }
1693 
1694 /* Check if the coefficients of the parameters are all integral.
1695  */
integer_parameter(struct isl_tab * tab,int row)1696 static int integer_parameter(struct isl_tab *tab, int row)
1697 {
1698 	int i;
1699 	int col;
1700 	unsigned off = 2 + tab->M;
1701 
1702 	for (i = 0; i < tab->n_param; ++i) {
1703 		/* Eliminated parameter */
1704 		if (tab->var[i].is_row)
1705 			continue;
1706 		col = tab->var[i].index;
1707 		if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1708 						tab->mat->row[row][0]))
1709 			return 0;
1710 	}
1711 	for (i = 0; i < tab->n_div; ++i) {
1712 		if (tab->var[tab->n_var - tab->n_div + i].is_row)
1713 			continue;
1714 		col = tab->var[tab->n_var - tab->n_div + i].index;
1715 		if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1716 						tab->mat->row[row][0]))
1717 			return 0;
1718 	}
1719 	return 1;
1720 }
1721 
1722 /* Check if the coefficients of the non-parameter variables are all integral.
1723  */
integer_variable(struct isl_tab * tab,int row)1724 static int integer_variable(struct isl_tab *tab, int row)
1725 {
1726 	int i;
1727 	unsigned off = 2 + tab->M;
1728 
1729 	for (i = tab->n_dead; i < tab->n_col; ++i) {
1730 		if (col_is_parameter_var(tab, i))
1731 			continue;
1732 		if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1733 						tab->mat->row[row][0]))
1734 			return 0;
1735 	}
1736 	return 1;
1737 }
1738 
1739 /* Check if the constant term is integral.
1740  */
integer_constant(struct isl_tab * tab,int row)1741 static int integer_constant(struct isl_tab *tab, int row)
1742 {
1743 	return isl_int_is_divisible_by(tab->mat->row[row][1],
1744 					tab->mat->row[row][0]);
1745 }
1746 
1747 #define I_CST	1 << 0
1748 #define I_PAR	1 << 1
1749 #define I_VAR	1 << 2
1750 
1751 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1752  * that is non-integer and therefore requires a cut and return
1753  * the index of the variable.
1754  * For parametric tableaus, there are three parts in a row,
1755  * the constant, the coefficients of the parameters and the rest.
1756  * For each part, we check whether the coefficients in that part
1757  * are all integral and if so, set the corresponding flag in *f.
1758  * If the constant and the parameter part are integral, then the
1759  * current sample value is integral and no cut is required
1760  * (irrespective of whether the variable part is integral).
1761  */
next_non_integer_var(struct isl_tab * tab,int var,int * f)1762 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1763 {
1764 	var = var < 0 ? tab->n_param : var + 1;
1765 
1766 	for (; var < tab->n_var - tab->n_div; ++var) {
1767 		int flags = 0;
1768 		int row;
1769 		if (!tab->var[var].is_row)
1770 			continue;
1771 		row = tab->var[var].index;
1772 		if (integer_constant(tab, row))
1773 			ISL_FL_SET(flags, I_CST);
1774 		if (integer_parameter(tab, row))
1775 			ISL_FL_SET(flags, I_PAR);
1776 		if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1777 			continue;
1778 		if (integer_variable(tab, row))
1779 			ISL_FL_SET(flags, I_VAR);
1780 		*f = flags;
1781 		return var;
1782 	}
1783 	return -1;
1784 }
1785 
1786 /* Check for first (non-parameter) variable that is non-integer and
1787  * therefore requires a cut and return the corresponding row.
1788  * For parametric tableaus, there are three parts in a row,
1789  * the constant, the coefficients of the parameters and the rest.
1790  * For each part, we check whether the coefficients in that part
1791  * are all integral and if so, set the corresponding flag in *f.
1792  * If the constant and the parameter part are integral, then the
1793  * current sample value is integral and no cut is required
1794  * (irrespective of whether the variable part is integral).
1795  */
first_non_integer_row(struct isl_tab * tab,int * f)1796 static int first_non_integer_row(struct isl_tab *tab, int *f)
1797 {
1798 	int var = next_non_integer_var(tab, -1, f);
1799 
1800 	return var < 0 ? -1 : tab->var[var].index;
1801 }
1802 
1803 /* Add a (non-parametric) cut to cut away the non-integral sample
1804  * value of the given row.
1805  *
1806  * If the row is given by
1807  *
1808  *	m r = f + \sum_i a_i y_i
1809  *
1810  * then the cut is
1811  *
1812  *	c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1813  *
1814  * The big parameter, if any, is ignored, since it is assumed to be big
1815  * enough to be divisible by any integer.
1816  * If the tableau is actually a parametric tableau, then this function
1817  * is only called when all coefficients of the parameters are integral.
1818  * The cut therefore has zero coefficients for the parameters.
1819  *
1820  * The current value is known to be negative, so row_sign, if it
1821  * exists, is set accordingly.
1822  *
1823  * Return the row of the cut or -1.
1824  */
add_cut(struct isl_tab * tab,int row)1825 static int add_cut(struct isl_tab *tab, int row)
1826 {
1827 	int i;
1828 	int r;
1829 	isl_int *r_row;
1830 	unsigned off = 2 + tab->M;
1831 
1832 	if (isl_tab_extend_cons(tab, 1) < 0)
1833 		return -1;
1834 	r = isl_tab_allocate_con(tab);
1835 	if (r < 0)
1836 		return -1;
1837 
1838 	r_row = tab->mat->row[tab->con[r].index];
1839 	isl_int_set(r_row[0], tab->mat->row[row][0]);
1840 	isl_int_neg(r_row[1], tab->mat->row[row][1]);
1841 	isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1842 	isl_int_neg(r_row[1], r_row[1]);
1843 	if (tab->M)
1844 		isl_int_set_si(r_row[2], 0);
1845 	for (i = 0; i < tab->n_col; ++i)
1846 		isl_int_fdiv_r(r_row[off + i],
1847 			tab->mat->row[row][off + i], tab->mat->row[row][0]);
1848 
1849 	tab->con[r].is_nonneg = 1;
1850 	if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1851 		return -1;
1852 	if (tab->row_sign)
1853 		tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1854 
1855 	return tab->con[r].index;
1856 }
1857 
1858 #define CUT_ALL 1
1859 #define CUT_ONE 0
1860 
1861 /* Given a non-parametric tableau, add cuts until an integer
1862  * sample point is obtained or until the tableau is determined
1863  * to be integer infeasible.
1864  * As long as there is any non-integer value in the sample point,
1865  * we add appropriate cuts, if possible, for each of these
1866  * non-integer values and then resolve the violated
1867  * cut constraints using restore_lexmin.
1868  * If one of the corresponding rows is equal to an integral
1869  * combination of variables/constraints plus a non-integral constant,
1870  * then there is no way to obtain an integer point and we return
1871  * a tableau that is marked empty.
1872  * The parameter cutting_strategy controls the strategy used when adding cuts
1873  * to remove non-integer points. CUT_ALL adds all possible cuts
1874  * before continuing the search. CUT_ONE adds only one cut at a time.
1875  */
cut_to_integer_lexmin(struct isl_tab * tab,int cutting_strategy)1876 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab,
1877 	int cutting_strategy)
1878 {
1879 	int var;
1880 	int row;
1881 	int flags;
1882 
1883 	if (!tab)
1884 		return NULL;
1885 	if (tab->empty)
1886 		return tab;
1887 
1888 	while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1889 		do {
1890 			if (ISL_FL_ISSET(flags, I_VAR)) {
1891 				if (isl_tab_mark_empty(tab) < 0)
1892 					goto error;
1893 				return tab;
1894 			}
1895 			row = tab->var[var].index;
1896 			row = add_cut(tab, row);
1897 			if (row < 0)
1898 				goto error;
1899 			if (cutting_strategy == CUT_ONE)
1900 				break;
1901 		} while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1902 		if (restore_lexmin(tab) < 0)
1903 			goto error;
1904 		if (tab->empty)
1905 			break;
1906 	}
1907 	return tab;
1908 error:
1909 	isl_tab_free(tab);
1910 	return NULL;
1911 }
1912 
1913 /* Check whether all the currently active samples also satisfy the inequality
1914  * "ineq" (treated as an equality if eq is set).
1915  * Remove those samples that do not.
1916  */
check_samples(struct isl_tab * tab,isl_int * ineq,int eq)1917 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1918 {
1919 	int i;
1920 	isl_int v;
1921 
1922 	if (!tab)
1923 		return NULL;
1924 
1925 	isl_assert(tab->mat->ctx, tab->bmap, goto error);
1926 	isl_assert(tab->mat->ctx, tab->samples, goto error);
1927 	isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1928 
1929 	isl_int_init(v);
1930 	for (i = tab->n_outside; i < tab->n_sample; ++i) {
1931 		int sgn;
1932 		isl_seq_inner_product(ineq, tab->samples->row[i],
1933 					1 + tab->n_var, &v);
1934 		sgn = isl_int_sgn(v);
1935 		if (eq ? (sgn == 0) : (sgn >= 0))
1936 			continue;
1937 		tab = isl_tab_drop_sample(tab, i);
1938 		if (!tab)
1939 			break;
1940 	}
1941 	isl_int_clear(v);
1942 
1943 	return tab;
1944 error:
1945 	isl_tab_free(tab);
1946 	return NULL;
1947 }
1948 
1949 /* Check whether the sample value of the tableau is finite,
1950  * i.e., either the tableau does not use a big parameter, or
1951  * all values of the variables are equal to the big parameter plus
1952  * some constant.  This constant is the actual sample value.
1953  */
sample_is_finite(struct isl_tab * tab)1954 static int sample_is_finite(struct isl_tab *tab)
1955 {
1956 	int i;
1957 
1958 	if (!tab->M)
1959 		return 1;
1960 
1961 	for (i = 0; i < tab->n_var; ++i) {
1962 		int row;
1963 		if (!tab->var[i].is_row)
1964 			return 0;
1965 		row = tab->var[i].index;
1966 		if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1967 			return 0;
1968 	}
1969 	return 1;
1970 }
1971 
1972 /* Check if the context tableau of sol has any integer points.
1973  * Leave tab in empty state if no integer point can be found.
1974  * If an integer point can be found and if moreover it is finite,
1975  * then it is added to the list of sample values.
1976  *
1977  * This function is only called when none of the currently active sample
1978  * values satisfies the most recently added constraint.
1979  */
check_integer_feasible(struct isl_tab * tab)1980 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1981 {
1982 	struct isl_tab_undo *snap;
1983 
1984 	if (!tab)
1985 		return NULL;
1986 
1987 	snap = isl_tab_snap(tab);
1988 	if (isl_tab_push_basis(tab) < 0)
1989 		goto error;
1990 
1991 	tab = cut_to_integer_lexmin(tab, CUT_ALL);
1992 	if (!tab)
1993 		goto error;
1994 
1995 	if (!tab->empty && sample_is_finite(tab)) {
1996 		struct isl_vec *sample;
1997 
1998 		sample = isl_tab_get_sample_value(tab);
1999 
2000 		if (isl_tab_add_sample(tab, sample) < 0)
2001 			goto error;
2002 	}
2003 
2004 	if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
2005 		goto error;
2006 
2007 	return tab;
2008 error:
2009 	isl_tab_free(tab);
2010 	return NULL;
2011 }
2012 
2013 /* Check if any of the currently active sample values satisfies
2014  * the inequality "ineq" (an equality if eq is set).
2015  */
tab_has_valid_sample(struct isl_tab * tab,isl_int * ineq,int eq)2016 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
2017 {
2018 	int i;
2019 	isl_int v;
2020 
2021 	if (!tab)
2022 		return -1;
2023 
2024 	isl_assert(tab->mat->ctx, tab->bmap, return -1);
2025 	isl_assert(tab->mat->ctx, tab->samples, return -1);
2026 	isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
2027 
2028 	isl_int_init(v);
2029 	for (i = tab->n_outside; i < tab->n_sample; ++i) {
2030 		int sgn;
2031 		isl_seq_inner_product(ineq, tab->samples->row[i],
2032 					1 + tab->n_var, &v);
2033 		sgn = isl_int_sgn(v);
2034 		if (eq ? (sgn == 0) : (sgn >= 0))
2035 			break;
2036 	}
2037 	isl_int_clear(v);
2038 
2039 	return i < tab->n_sample;
2040 }
2041 
2042 /* Insert a div specified by "div" to the tableau "tab" at position "pos" and
2043  * return isl_bool_true if the div is obviously non-negative.
2044  */
context_tab_insert_div(struct isl_tab * tab,int pos,__isl_keep isl_vec * div,isl_stat (* add_ineq)(void * user,isl_int *),void * user)2045 static isl_bool context_tab_insert_div(struct isl_tab *tab, int pos,
2046 	__isl_keep isl_vec *div,
2047 	isl_stat (*add_ineq)(void *user, isl_int *), void *user)
2048 {
2049 	int i;
2050 	int r;
2051 	struct isl_mat *samples;
2052 	int nonneg;
2053 
2054 	r = isl_tab_insert_div(tab, pos, div, add_ineq, user);
2055 	if (r < 0)
2056 		return isl_bool_error;
2057 	nonneg = tab->var[r].is_nonneg;
2058 	tab->var[r].frozen = 1;
2059 
2060 	samples = isl_mat_extend(tab->samples,
2061 			tab->n_sample, 1 + tab->n_var);
2062 	tab->samples = samples;
2063 	if (!samples)
2064 		return isl_bool_error;
2065 	for (i = tab->n_outside; i < samples->n_row; ++i) {
2066 		isl_seq_inner_product(div->el + 1, samples->row[i],
2067 			div->size - 1, &samples->row[i][samples->n_col - 1]);
2068 		isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
2069 			       samples->row[i][samples->n_col - 1], div->el[0]);
2070 	}
2071 	tab->samples = isl_mat_move_cols(tab->samples, 1 + pos,
2072 					1 + tab->n_var - 1, 1);
2073 	if (!tab->samples)
2074 		return isl_bool_error;
2075 
2076 	return isl_bool_ok(nonneg);
2077 }
2078 
2079 /* Add a div specified by "div" to both the main tableau and
2080  * the context tableau.  In case of the main tableau, we only
2081  * need to add an extra div.  In the context tableau, we also
2082  * need to express the meaning of the div.
2083  * Return the index of the div or -1 if anything went wrong.
2084  *
2085  * The new integer division is added before any unknown integer
2086  * divisions in the context to ensure that it does not get
2087  * equated to some linear combination involving unknown integer
2088  * divisions.
2089  */
add_div(struct isl_tab * tab,struct isl_context * context,__isl_keep isl_vec * div)2090 static int add_div(struct isl_tab *tab, struct isl_context *context,
2091 	__isl_keep isl_vec *div)
2092 {
2093 	int r;
2094 	int pos;
2095 	isl_bool nonneg;
2096 	struct isl_tab *context_tab = context->op->peek_tab(context);
2097 
2098 	if (!tab || !context_tab)
2099 		goto error;
2100 
2101 	pos = context_tab->n_var - context->n_unknown;
2102 	if ((nonneg = context->op->insert_div(context, pos, div)) < 0)
2103 		goto error;
2104 
2105 	if (!context->op->is_ok(context))
2106 		goto error;
2107 
2108 	pos = tab->n_var - context->n_unknown;
2109 	if (isl_tab_extend_vars(tab, 1) < 0)
2110 		goto error;
2111 	r = isl_tab_insert_var(tab, pos);
2112 	if (r < 0)
2113 		goto error;
2114 	if (nonneg)
2115 		tab->var[r].is_nonneg = 1;
2116 	tab->var[r].frozen = 1;
2117 	tab->n_div++;
2118 
2119 	return tab->n_div - 1 - context->n_unknown;
2120 error:
2121 	context->op->invalidate(context);
2122 	return -1;
2123 }
2124 
2125 /* Return the position of the integer division that is equal to div/denom
2126  * if there is one.  Otherwise, return a position beyond the integer divisions.
2127  */
find_div(struct isl_tab * tab,isl_int * div,isl_int denom)2128 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
2129 {
2130 	int i;
2131 	isl_size total = isl_basic_map_dim(tab->bmap, isl_dim_all);
2132 	isl_size n_div;
2133 
2134 	n_div = isl_basic_map_dim(tab->bmap, isl_dim_div);
2135 	if (total < 0 || n_div < 0)
2136 		return -1;
2137 	for (i = 0; i < n_div; ++i) {
2138 		if (isl_int_ne(tab->bmap->div[i][0], denom))
2139 			continue;
2140 		if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
2141 			continue;
2142 		return i;
2143 	}
2144 	return n_div;
2145 }
2146 
2147 /* Return the index of a div that corresponds to "div".
2148  * We first check if we already have such a div and if not, we create one.
2149  */
get_div(struct isl_tab * tab,struct isl_context * context,struct isl_vec * div)2150 static int get_div(struct isl_tab *tab, struct isl_context *context,
2151 	struct isl_vec *div)
2152 {
2153 	int d;
2154 	struct isl_tab *context_tab = context->op->peek_tab(context);
2155 	unsigned n_div;
2156 
2157 	if (!context_tab)
2158 		return -1;
2159 
2160 	n_div = isl_basic_map_dim(context_tab->bmap, isl_dim_div);
2161 	d = find_div(context_tab, div->el + 1, div->el[0]);
2162 	if (d < 0)
2163 		return -1;
2164 	if (d < n_div)
2165 		return d;
2166 
2167 	return add_div(tab, context, div);
2168 }
2169 
2170 /* Add a parametric cut to cut away the non-integral sample value
2171  * of the given row.
2172  * Let a_i be the coefficients of the constant term and the parameters
2173  * and let b_i be the coefficients of the variables or constraints
2174  * in basis of the tableau.
2175  * Let q be the div q = floor(\sum_i {-a_i} y_i).
2176  *
2177  * The cut is expressed as
2178  *
2179  *	c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
2180  *
2181  * If q did not already exist in the context tableau, then it is added first.
2182  * If q is in a column of the main tableau then the "+ q" can be accomplished
2183  * by setting the corresponding entry to the denominator of the constraint.
2184  * If q happens to be in a row of the main tableau, then the corresponding
2185  * row needs to be added instead (taking care of the denominators).
2186  * Note that this is very unlikely, but perhaps not entirely impossible.
2187  *
2188  * The current value of the cut is known to be negative (or at least
2189  * non-positive), so row_sign is set accordingly.
2190  *
2191  * Return the row of the cut or -1.
2192  */
add_parametric_cut(struct isl_tab * tab,int row,struct isl_context * context)2193 static int add_parametric_cut(struct isl_tab *tab, int row,
2194 	struct isl_context *context)
2195 {
2196 	struct isl_vec *div;
2197 	int d;
2198 	int i;
2199 	int r;
2200 	isl_int *r_row;
2201 	int col;
2202 	int n;
2203 	unsigned off = 2 + tab->M;
2204 
2205 	if (!context)
2206 		return -1;
2207 
2208 	div = get_row_parameter_div(tab, row);
2209 	if (!div)
2210 		return -1;
2211 
2212 	n = tab->n_div - context->n_unknown;
2213 	d = context->op->get_div(context, tab, div);
2214 	isl_vec_free(div);
2215 	if (d < 0)
2216 		return -1;
2217 
2218 	if (isl_tab_extend_cons(tab, 1) < 0)
2219 		return -1;
2220 	r = isl_tab_allocate_con(tab);
2221 	if (r < 0)
2222 		return -1;
2223 
2224 	r_row = tab->mat->row[tab->con[r].index];
2225 	isl_int_set(r_row[0], tab->mat->row[row][0]);
2226 	isl_int_neg(r_row[1], tab->mat->row[row][1]);
2227 	isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
2228 	isl_int_neg(r_row[1], r_row[1]);
2229 	if (tab->M)
2230 		isl_int_set_si(r_row[2], 0);
2231 	for (i = 0; i < tab->n_param; ++i) {
2232 		if (tab->var[i].is_row)
2233 			continue;
2234 		col = tab->var[i].index;
2235 		isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2236 		isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2237 				tab->mat->row[row][0]);
2238 		isl_int_neg(r_row[off + col], r_row[off + col]);
2239 	}
2240 	for (i = 0; i < tab->n_div; ++i) {
2241 		if (tab->var[tab->n_var - tab->n_div + i].is_row)
2242 			continue;
2243 		col = tab->var[tab->n_var - tab->n_div + i].index;
2244 		isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2245 		isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2246 				tab->mat->row[row][0]);
2247 		isl_int_neg(r_row[off + col], r_row[off + col]);
2248 	}
2249 	for (i = 0; i < tab->n_col; ++i) {
2250 		if (tab->col_var[i] >= 0 &&
2251 		    (tab->col_var[i] < tab->n_param ||
2252 		     tab->col_var[i] >= tab->n_var - tab->n_div))
2253 			continue;
2254 		isl_int_fdiv_r(r_row[off + i],
2255 			tab->mat->row[row][off + i], tab->mat->row[row][0]);
2256 	}
2257 	if (tab->var[tab->n_var - tab->n_div + d].is_row) {
2258 		isl_int gcd;
2259 		int d_row = tab->var[tab->n_var - tab->n_div + d].index;
2260 		isl_int_init(gcd);
2261 		isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
2262 		isl_int_divexact(r_row[0], r_row[0], gcd);
2263 		isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
2264 		isl_seq_combine(r_row + 1, gcd, r_row + 1,
2265 				r_row[0], tab->mat->row[d_row] + 1,
2266 				off - 1 + tab->n_col);
2267 		isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
2268 		isl_int_clear(gcd);
2269 	} else {
2270 		col = tab->var[tab->n_var - tab->n_div + d].index;
2271 		isl_int_set(r_row[off + col], tab->mat->row[row][0]);
2272 	}
2273 
2274 	tab->con[r].is_nonneg = 1;
2275 	if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2276 		return -1;
2277 	if (tab->row_sign)
2278 		tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
2279 
2280 	row = tab->con[r].index;
2281 
2282 	if (d >= n && context->op->detect_equalities(context, tab) < 0)
2283 		return -1;
2284 
2285 	return row;
2286 }
2287 
2288 /* Construct a tableau for bmap that can be used for computing
2289  * the lexicographic minimum (or maximum) of bmap.
2290  * If not NULL, then dom is the domain where the minimum
2291  * should be computed.  In this case, we set up a parametric
2292  * tableau with row signs (initialized to "unknown").
2293  * If M is set, then the tableau will use a big parameter.
2294  * If max is set, then a maximum should be computed instead of a minimum.
2295  * This means that for each variable x, the tableau will contain the variable
2296  * x' = M - x, rather than x' = M + x.  This in turn means that the coefficient
2297  * of the variables in all constraints are negated prior to adding them
2298  * to the tableau.
2299  */
tab_for_lexmin(__isl_keep isl_basic_map * bmap,__isl_keep isl_basic_set * dom,unsigned M,int max)2300 static __isl_give struct isl_tab *tab_for_lexmin(__isl_keep isl_basic_map *bmap,
2301 	__isl_keep isl_basic_set *dom, unsigned M, int max)
2302 {
2303 	int i;
2304 	struct isl_tab *tab;
2305 	unsigned n_var;
2306 	unsigned o_var;
2307 	isl_size total;
2308 
2309 	total = isl_basic_map_dim(bmap, isl_dim_all);
2310 	if (total < 0)
2311 		return NULL;
2312 	tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
2313 			    total, M);
2314 	if (!tab)
2315 		return NULL;
2316 
2317 	tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2318 	if (dom) {
2319 		isl_size dom_total;
2320 		dom_total = isl_basic_set_dim(dom, isl_dim_all);
2321 		if (dom_total < 0)
2322 			goto error;
2323 		tab->n_param = dom_total - dom->n_div;
2324 		tab->n_div = dom->n_div;
2325 		tab->row_sign = isl_calloc_array(bmap->ctx,
2326 					enum isl_tab_row_sign, tab->mat->n_row);
2327 		if (tab->mat->n_row && !tab->row_sign)
2328 			goto error;
2329 	}
2330 	if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2331 		if (isl_tab_mark_empty(tab) < 0)
2332 			goto error;
2333 		return tab;
2334 	}
2335 
2336 	for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
2337 		tab->var[i].is_nonneg = 1;
2338 		tab->var[i].frozen = 1;
2339 	}
2340 	o_var = 1 + tab->n_param;
2341 	n_var = tab->n_var - tab->n_param - tab->n_div;
2342 	for (i = 0; i < bmap->n_eq; ++i) {
2343 		if (max)
2344 			isl_seq_neg(bmap->eq[i] + o_var,
2345 				    bmap->eq[i] + o_var, n_var);
2346 		tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
2347 		if (max)
2348 			isl_seq_neg(bmap->eq[i] + o_var,
2349 				    bmap->eq[i] + o_var, n_var);
2350 		if (!tab || tab->empty)
2351 			return tab;
2352 	}
2353 	if (bmap->n_eq && restore_lexmin(tab) < 0)
2354 		goto error;
2355 	for (i = 0; i < bmap->n_ineq; ++i) {
2356 		if (max)
2357 			isl_seq_neg(bmap->ineq[i] + o_var,
2358 				    bmap->ineq[i] + o_var, n_var);
2359 		tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2360 		if (max)
2361 			isl_seq_neg(bmap->ineq[i] + o_var,
2362 				    bmap->ineq[i] + o_var, n_var);
2363 		if (!tab || tab->empty)
2364 			return tab;
2365 	}
2366 	return tab;
2367 error:
2368 	isl_tab_free(tab);
2369 	return NULL;
2370 }
2371 
2372 /* Given a main tableau where more than one row requires a split,
2373  * determine and return the "best" row to split on.
2374  *
2375  * If any of the rows requiring a split only involves
2376  * variables that also appear in the context tableau,
2377  * then the negative part is guaranteed not to have a solution.
2378  * It is therefore best to split on any of these rows first.
2379  *
2380  * Otherwise,
2381  * given two rows in the main tableau, if the inequality corresponding
2382  * to the first row is redundant with respect to that of the second row
2383  * in the current tableau, then it is better to split on the second row,
2384  * since in the positive part, both rows will be positive.
2385  * (In the negative part a pivot will have to be performed and just about
2386  * anything can happen to the sign of the other row.)
2387  *
2388  * As a simple heuristic, we therefore select the row that makes the most
2389  * of the other rows redundant.
2390  *
2391  * Perhaps it would also be useful to look at the number of constraints
2392  * that conflict with any given constraint.
2393  *
2394  * best is the best row so far (-1 when we have not found any row yet).
2395  * best_r is the number of other rows made redundant by row best.
2396  * When best is still -1, bset_r is meaningless, but it is initialized
2397  * to some arbitrary value (0) anyway.  Without this redundant initialization
2398  * valgrind may warn about uninitialized memory accesses when isl
2399  * is compiled with some versions of gcc.
2400  */
best_split(struct isl_tab * tab,struct isl_tab * context_tab)2401 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2402 {
2403 	struct isl_tab_undo *snap;
2404 	int split;
2405 	int row;
2406 	int best = -1;
2407 	int best_r = 0;
2408 
2409 	if (isl_tab_extend_cons(context_tab, 2) < 0)
2410 		return -1;
2411 
2412 	snap = isl_tab_snap(context_tab);
2413 
2414 	for (split = tab->n_redundant; split < tab->n_row; ++split) {
2415 		struct isl_tab_undo *snap2;
2416 		struct isl_vec *ineq = NULL;
2417 		int r = 0;
2418 		int ok;
2419 
2420 		if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2421 			continue;
2422 		if (tab->row_sign[split] != isl_tab_row_any)
2423 			continue;
2424 
2425 		if (is_parametric_constant(tab, split))
2426 			return split;
2427 
2428 		ineq = get_row_parameter_ineq(tab, split);
2429 		if (!ineq)
2430 			return -1;
2431 		ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2432 		isl_vec_free(ineq);
2433 		if (!ok)
2434 			return -1;
2435 
2436 		snap2 = isl_tab_snap(context_tab);
2437 
2438 		for (row = tab->n_redundant; row < tab->n_row; ++row) {
2439 			struct isl_tab_var *var;
2440 
2441 			if (row == split)
2442 				continue;
2443 			if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2444 				continue;
2445 			if (tab->row_sign[row] != isl_tab_row_any)
2446 				continue;
2447 
2448 			ineq = get_row_parameter_ineq(tab, row);
2449 			if (!ineq)
2450 				return -1;
2451 			ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2452 			isl_vec_free(ineq);
2453 			if (!ok)
2454 				return -1;
2455 			var = &context_tab->con[context_tab->n_con - 1];
2456 			if (!context_tab->empty &&
2457 			    !isl_tab_min_at_most_neg_one(context_tab, var))
2458 				r++;
2459 			if (isl_tab_rollback(context_tab, snap2) < 0)
2460 				return -1;
2461 		}
2462 		if (best == -1 || r > best_r) {
2463 			best = split;
2464 			best_r = r;
2465 		}
2466 		if (isl_tab_rollback(context_tab, snap) < 0)
2467 			return -1;
2468 	}
2469 
2470 	return best;
2471 }
2472 
context_lex_peek_basic_set(struct isl_context * context)2473 static struct isl_basic_set *context_lex_peek_basic_set(
2474 	struct isl_context *context)
2475 {
2476 	struct isl_context_lex *clex = (struct isl_context_lex *)context;
2477 	if (!clex->tab)
2478 		return NULL;
2479 	return isl_tab_peek_bset(clex->tab);
2480 }
2481 
context_lex_peek_tab(struct isl_context * context)2482 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2483 {
2484 	struct isl_context_lex *clex = (struct isl_context_lex *)context;
2485 	return clex->tab;
2486 }
2487 
context_lex_add_eq(struct isl_context * context,isl_int * eq,int check,int update)2488 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2489 		int check, int update)
2490 {
2491 	struct isl_context_lex *clex = (struct isl_context_lex *)context;
2492 	if (isl_tab_extend_cons(clex->tab, 2) < 0)
2493 		goto error;
2494 	if (add_lexmin_eq(clex->tab, eq) < 0)
2495 		goto error;
2496 	if (check) {
2497 		int v = tab_has_valid_sample(clex->tab, eq, 1);
2498 		if (v < 0)
2499 			goto error;
2500 		if (!v)
2501 			clex->tab = check_integer_feasible(clex->tab);
2502 	}
2503 	if (update)
2504 		clex->tab = check_samples(clex->tab, eq, 1);
2505 	return;
2506 error:
2507 	isl_tab_free(clex->tab);
2508 	clex->tab = NULL;
2509 }
2510 
context_lex_add_ineq(struct isl_context * context,isl_int * ineq,int check,int update)2511 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2512 		int check, int update)
2513 {
2514 	struct isl_context_lex *clex = (struct isl_context_lex *)context;
2515 	if (isl_tab_extend_cons(clex->tab, 1) < 0)
2516 		goto error;
2517 	clex->tab = add_lexmin_ineq(clex->tab, ineq);
2518 	if (check) {
2519 		int v = tab_has_valid_sample(clex->tab, ineq, 0);
2520 		if (v < 0)
2521 			goto error;
2522 		if (!v)
2523 			clex->tab = check_integer_feasible(clex->tab);
2524 	}
2525 	if (update)
2526 		clex->tab = check_samples(clex->tab, ineq, 0);
2527 	return;
2528 error:
2529 	isl_tab_free(clex->tab);
2530 	clex->tab = NULL;
2531 }
2532 
context_lex_add_ineq_wrap(void * user,isl_int * ineq)2533 static isl_stat context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2534 {
2535 	struct isl_context *context = (struct isl_context *)user;
2536 	context_lex_add_ineq(context, ineq, 0, 0);
2537 	return context->op->is_ok(context) ? isl_stat_ok : isl_stat_error;
2538 }
2539 
2540 /* Check which signs can be obtained by "ineq" on all the currently
2541  * active sample values.  See row_sign for more information.
2542  */
tab_ineq_sign(struct isl_tab * tab,isl_int * ineq,int strict)2543 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2544 	int strict)
2545 {
2546 	int i;
2547 	int sgn;
2548 	isl_int tmp;
2549 	enum isl_tab_row_sign res = isl_tab_row_unknown;
2550 
2551 	isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2552 	isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2553 			return isl_tab_row_unknown);
2554 
2555 	isl_int_init(tmp);
2556 	for (i = tab->n_outside; i < tab->n_sample; ++i) {
2557 		isl_seq_inner_product(tab->samples->row[i], ineq,
2558 					1 + tab->n_var, &tmp);
2559 		sgn = isl_int_sgn(tmp);
2560 		if (sgn > 0 || (sgn == 0 && strict)) {
2561 			if (res == isl_tab_row_unknown)
2562 				res = isl_tab_row_pos;
2563 			if (res == isl_tab_row_neg)
2564 				res = isl_tab_row_any;
2565 		}
2566 		if (sgn < 0) {
2567 			if (res == isl_tab_row_unknown)
2568 				res = isl_tab_row_neg;
2569 			if (res == isl_tab_row_pos)
2570 				res = isl_tab_row_any;
2571 		}
2572 		if (res == isl_tab_row_any)
2573 			break;
2574 	}
2575 	isl_int_clear(tmp);
2576 
2577 	return res;
2578 }
2579 
context_lex_ineq_sign(struct isl_context * context,isl_int * ineq,int strict)2580 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2581 			isl_int *ineq, int strict)
2582 {
2583 	struct isl_context_lex *clex = (struct isl_context_lex *)context;
2584 	return tab_ineq_sign(clex->tab, ineq, strict);
2585 }
2586 
2587 /* Check whether "ineq" can be added to the tableau without rendering
2588  * it infeasible.
2589  */
context_lex_test_ineq(struct isl_context * context,isl_int * ineq)2590 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2591 {
2592 	struct isl_context_lex *clex = (struct isl_context_lex *)context;
2593 	struct isl_tab_undo *snap;
2594 	int feasible;
2595 
2596 	if (!clex->tab)
2597 		return -1;
2598 
2599 	if (isl_tab_extend_cons(clex->tab, 1) < 0)
2600 		return -1;
2601 
2602 	snap = isl_tab_snap(clex->tab);
2603 	if (isl_tab_push_basis(clex->tab) < 0)
2604 		return -1;
2605 	clex->tab = add_lexmin_ineq(clex->tab, ineq);
2606 	clex->tab = check_integer_feasible(clex->tab);
2607 	if (!clex->tab)
2608 		return -1;
2609 	feasible = !clex->tab->empty;
2610 	if (isl_tab_rollback(clex->tab, snap) < 0)
2611 		return -1;
2612 
2613 	return feasible;
2614 }
2615 
context_lex_get_div(struct isl_context * context,struct isl_tab * tab,struct isl_vec * div)2616 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2617 		struct isl_vec *div)
2618 {
2619 	return get_div(tab, context, div);
2620 }
2621 
2622 /* Insert a div specified by "div" to the context tableau at position "pos" and
2623  * return isl_bool_true if the div is obviously non-negative.
2624  * context_tab_add_div will always return isl_bool_true, because all variables
2625  * in a isl_context_lex tableau are non-negative.
2626  * However, if we are using a big parameter in the context, then this only
2627  * reflects the non-negativity of the variable used to _encode_ the
2628  * div, i.e., div' = M + div, so we can't draw any conclusions.
2629  */
context_lex_insert_div(struct isl_context * context,int pos,__isl_keep isl_vec * div)2630 static isl_bool context_lex_insert_div(struct isl_context *context, int pos,
2631 	__isl_keep isl_vec *div)
2632 {
2633 	struct isl_context_lex *clex = (struct isl_context_lex *)context;
2634 	isl_bool nonneg;
2635 	nonneg = context_tab_insert_div(clex->tab, pos, div,
2636 					context_lex_add_ineq_wrap, context);
2637 	if (nonneg < 0)
2638 		return isl_bool_error;
2639 	if (clex->tab->M)
2640 		return isl_bool_false;
2641 	return nonneg;
2642 }
2643 
context_lex_detect_equalities(struct isl_context * context,struct isl_tab * tab)2644 static int context_lex_detect_equalities(struct isl_context *context,
2645 		struct isl_tab *tab)
2646 {
2647 	return 0;
2648 }
2649 
context_lex_best_split(struct isl_context * context,struct isl_tab * tab)2650 static int context_lex_best_split(struct isl_context *context,
2651 		struct isl_tab *tab)
2652 {
2653 	struct isl_context_lex *clex = (struct isl_context_lex *)context;
2654 	struct isl_tab_undo *snap;
2655 	int r;
2656 
2657 	snap = isl_tab_snap(clex->tab);
2658 	if (isl_tab_push_basis(clex->tab) < 0)
2659 		return -1;
2660 	r = best_split(tab, clex->tab);
2661 
2662 	if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2663 		return -1;
2664 
2665 	return r;
2666 }
2667 
context_lex_is_empty(struct isl_context * context)2668 static int context_lex_is_empty(struct isl_context *context)
2669 {
2670 	struct isl_context_lex *clex = (struct isl_context_lex *)context;
2671 	if (!clex->tab)
2672 		return -1;
2673 	return clex->tab->empty;
2674 }
2675 
context_lex_save(struct isl_context * context)2676 static void *context_lex_save(struct isl_context *context)
2677 {
2678 	struct isl_context_lex *clex = (struct isl_context_lex *)context;
2679 	struct isl_tab_undo *snap;
2680 
2681 	snap = isl_tab_snap(clex->tab);
2682 	if (isl_tab_push_basis(clex->tab) < 0)
2683 		return NULL;
2684 	if (isl_tab_save_samples(clex->tab) < 0)
2685 		return NULL;
2686 
2687 	return snap;
2688 }
2689 
context_lex_restore(struct isl_context * context,void * save)2690 static void context_lex_restore(struct isl_context *context, void *save)
2691 {
2692 	struct isl_context_lex *clex = (struct isl_context_lex *)context;
2693 	if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2694 		isl_tab_free(clex->tab);
2695 		clex->tab = NULL;
2696 	}
2697 }
2698 
context_lex_discard(void * save)2699 static void context_lex_discard(void *save)
2700 {
2701 }
2702 
context_lex_is_ok(struct isl_context * context)2703 static int context_lex_is_ok(struct isl_context *context)
2704 {
2705 	struct isl_context_lex *clex = (struct isl_context_lex *)context;
2706 	return !!clex->tab;
2707 }
2708 
2709 /* For each variable in the context tableau, check if the variable can
2710  * only attain non-negative values.  If so, mark the parameter as non-negative
2711  * in the main tableau.  This allows for a more direct identification of some
2712  * cases of violated constraints.
2713  */
tab_detect_nonnegative_parameters(struct isl_tab * tab,struct isl_tab * context_tab)2714 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2715 	struct isl_tab *context_tab)
2716 {
2717 	int i;
2718 	struct isl_tab_undo *snap;
2719 	struct isl_vec *ineq = NULL;
2720 	struct isl_tab_var *var;
2721 	int n;
2722 
2723 	if (context_tab->n_var == 0)
2724 		return tab;
2725 
2726 	ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2727 	if (!ineq)
2728 		goto error;
2729 
2730 	if (isl_tab_extend_cons(context_tab, 1) < 0)
2731 		goto error;
2732 
2733 	snap = isl_tab_snap(context_tab);
2734 
2735 	n = 0;
2736 	isl_seq_clr(ineq->el, ineq->size);
2737 	for (i = 0; i < context_tab->n_var; ++i) {
2738 		isl_int_set_si(ineq->el[1 + i], 1);
2739 		if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2740 			goto error;
2741 		var = &context_tab->con[context_tab->n_con - 1];
2742 		if (!context_tab->empty &&
2743 		    !isl_tab_min_at_most_neg_one(context_tab, var)) {
2744 			int j = i;
2745 			if (i >= tab->n_param)
2746 				j = i - tab->n_param + tab->n_var - tab->n_div;
2747 			tab->var[j].is_nonneg = 1;
2748 			n++;
2749 		}
2750 		isl_int_set_si(ineq->el[1 + i], 0);
2751 		if (isl_tab_rollback(context_tab, snap) < 0)
2752 			goto error;
2753 	}
2754 
2755 	if (context_tab->M && n == context_tab->n_var) {
2756 		context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2757 		context_tab->M = 0;
2758 	}
2759 
2760 	isl_vec_free(ineq);
2761 	return tab;
2762 error:
2763 	isl_vec_free(ineq);
2764 	isl_tab_free(tab);
2765 	return NULL;
2766 }
2767 
context_lex_detect_nonnegative_parameters(struct isl_context * context,struct isl_tab * tab)2768 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2769 	struct isl_context *context, struct isl_tab *tab)
2770 {
2771 	struct isl_context_lex *clex = (struct isl_context_lex *)context;
2772 	struct isl_tab_undo *snap;
2773 
2774 	if (!tab)
2775 		return NULL;
2776 
2777 	snap = isl_tab_snap(clex->tab);
2778 	if (isl_tab_push_basis(clex->tab) < 0)
2779 		goto error;
2780 
2781 	tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2782 
2783 	if (isl_tab_rollback(clex->tab, snap) < 0)
2784 		goto error;
2785 
2786 	return tab;
2787 error:
2788 	isl_tab_free(tab);
2789 	return NULL;
2790 }
2791 
context_lex_invalidate(struct isl_context * context)2792 static void context_lex_invalidate(struct isl_context *context)
2793 {
2794 	struct isl_context_lex *clex = (struct isl_context_lex *)context;
2795 	isl_tab_free(clex->tab);
2796 	clex->tab = NULL;
2797 }
2798 
context_lex_free(struct isl_context * context)2799 static __isl_null struct isl_context *context_lex_free(
2800 	struct isl_context *context)
2801 {
2802 	struct isl_context_lex *clex = (struct isl_context_lex *)context;
2803 	isl_tab_free(clex->tab);
2804 	free(clex);
2805 
2806 	return NULL;
2807 }
2808 
2809 struct isl_context_op isl_context_lex_op = {
2810 	context_lex_detect_nonnegative_parameters,
2811 	context_lex_peek_basic_set,
2812 	context_lex_peek_tab,
2813 	context_lex_add_eq,
2814 	context_lex_add_ineq,
2815 	context_lex_ineq_sign,
2816 	context_lex_test_ineq,
2817 	context_lex_get_div,
2818 	context_lex_insert_div,
2819 	context_lex_detect_equalities,
2820 	context_lex_best_split,
2821 	context_lex_is_empty,
2822 	context_lex_is_ok,
2823 	context_lex_save,
2824 	context_lex_restore,
2825 	context_lex_discard,
2826 	context_lex_invalidate,
2827 	context_lex_free,
2828 };
2829 
context_tab_for_lexmin(__isl_take isl_basic_set * bset)2830 static struct isl_tab *context_tab_for_lexmin(__isl_take isl_basic_set *bset)
2831 {
2832 	struct isl_tab *tab;
2833 
2834 	if (!bset)
2835 		return NULL;
2836 	tab = tab_for_lexmin(bset_to_bmap(bset), NULL, 1, 0);
2837 	if (isl_tab_track_bset(tab, bset) < 0)
2838 		goto error;
2839 	tab = isl_tab_init_samples(tab);
2840 	return tab;
2841 error:
2842 	isl_tab_free(tab);
2843 	return NULL;
2844 }
2845 
isl_context_lex_alloc(struct isl_basic_set * dom)2846 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2847 {
2848 	struct isl_context_lex *clex;
2849 
2850 	if (!dom)
2851 		return NULL;
2852 
2853 	clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2854 	if (!clex)
2855 		return NULL;
2856 
2857 	clex->context.op = &isl_context_lex_op;
2858 
2859 	clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2860 	if (restore_lexmin(clex->tab) < 0)
2861 		goto error;
2862 	clex->tab = check_integer_feasible(clex->tab);
2863 	if (!clex->tab)
2864 		goto error;
2865 
2866 	return &clex->context;
2867 error:
2868 	clex->context.op->free(&clex->context);
2869 	return NULL;
2870 }
2871 
2872 /* Representation of the context when using generalized basis reduction.
2873  *
2874  * "shifted" contains the offsets of the unit hypercubes that lie inside the
2875  * context.  Any rational point in "shifted" can therefore be rounded
2876  * up to an integer point in the context.
2877  * If the context is constrained by any equality, then "shifted" is not used
2878  * as it would be empty.
2879  */
2880 struct isl_context_gbr {
2881 	struct isl_context context;
2882 	struct isl_tab *tab;
2883 	struct isl_tab *shifted;
2884 	struct isl_tab *cone;
2885 };
2886 
context_gbr_detect_nonnegative_parameters(struct isl_context * context,struct isl_tab * tab)2887 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2888 	struct isl_context *context, struct isl_tab *tab)
2889 {
2890 	struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2891 	if (!tab)
2892 		return NULL;
2893 	return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2894 }
2895 
context_gbr_peek_basic_set(struct isl_context * context)2896 static struct isl_basic_set *context_gbr_peek_basic_set(
2897 	struct isl_context *context)
2898 {
2899 	struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2900 	if (!cgbr->tab)
2901 		return NULL;
2902 	return isl_tab_peek_bset(cgbr->tab);
2903 }
2904 
context_gbr_peek_tab(struct isl_context * context)2905 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2906 {
2907 	struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2908 	return cgbr->tab;
2909 }
2910 
2911 /* Initialize the "shifted" tableau of the context, which
2912  * contains the constraints of the original tableau shifted
2913  * by the sum of all negative coefficients.  This ensures
2914  * that any rational point in the shifted tableau can
2915  * be rounded up to yield an integer point in the original tableau.
2916  */
gbr_init_shifted(struct isl_context_gbr * cgbr)2917 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2918 {
2919 	int i, j;
2920 	struct isl_vec *cst;
2921 	struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2922 	isl_size dim = isl_basic_set_dim(bset, isl_dim_all);
2923 
2924 	if (dim < 0)
2925 		return;
2926 	cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2927 	if (!cst)
2928 		return;
2929 
2930 	for (i = 0; i < bset->n_ineq; ++i) {
2931 		isl_int_set(cst->el[i], bset->ineq[i][0]);
2932 		for (j = 0; j < dim; ++j) {
2933 			if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2934 				continue;
2935 			isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2936 				    bset->ineq[i][1 + j]);
2937 		}
2938 	}
2939 
2940 	cgbr->shifted = isl_tab_from_basic_set(bset, 0);
2941 
2942 	for (i = 0; i < bset->n_ineq; ++i)
2943 		isl_int_set(bset->ineq[i][0], cst->el[i]);
2944 
2945 	isl_vec_free(cst);
2946 }
2947 
2948 /* Check if the shifted tableau is non-empty, and if so
2949  * use the sample point to construct an integer point
2950  * of the context tableau.
2951  */
gbr_get_shifted_sample(struct isl_context_gbr * cgbr)2952 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2953 {
2954 	struct isl_vec *sample;
2955 
2956 	if (!cgbr->shifted)
2957 		gbr_init_shifted(cgbr);
2958 	if (!cgbr->shifted)
2959 		return NULL;
2960 	if (cgbr->shifted->empty)
2961 		return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2962 
2963 	sample = isl_tab_get_sample_value(cgbr->shifted);
2964 	sample = isl_vec_ceil(sample);
2965 
2966 	return sample;
2967 }
2968 
drop_constant_terms(__isl_take isl_basic_set * bset)2969 static __isl_give isl_basic_set *drop_constant_terms(
2970 	__isl_take isl_basic_set *bset)
2971 {
2972 	int i;
2973 
2974 	if (!bset)
2975 		return NULL;
2976 
2977 	for (i = 0; i < bset->n_eq; ++i)
2978 		isl_int_set_si(bset->eq[i][0], 0);
2979 
2980 	for (i = 0; i < bset->n_ineq; ++i)
2981 		isl_int_set_si(bset->ineq[i][0], 0);
2982 
2983 	return bset;
2984 }
2985 
use_shifted(struct isl_context_gbr * cgbr)2986 static int use_shifted(struct isl_context_gbr *cgbr)
2987 {
2988 	if (!cgbr->tab)
2989 		return 0;
2990 	return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2991 }
2992 
gbr_get_sample(struct isl_context_gbr * cgbr)2993 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2994 {
2995 	struct isl_basic_set *bset;
2996 	struct isl_basic_set *cone;
2997 
2998 	if (isl_tab_sample_is_integer(cgbr->tab))
2999 		return isl_tab_get_sample_value(cgbr->tab);
3000 
3001 	if (use_shifted(cgbr)) {
3002 		struct isl_vec *sample;
3003 
3004 		sample = gbr_get_shifted_sample(cgbr);
3005 		if (!sample || sample->size > 0)
3006 			return sample;
3007 
3008 		isl_vec_free(sample);
3009 	}
3010 
3011 	if (!cgbr->cone) {
3012 		bset = isl_tab_peek_bset(cgbr->tab);
3013 		cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3014 		if (!cgbr->cone)
3015 			return NULL;
3016 		if (isl_tab_track_bset(cgbr->cone,
3017 					isl_basic_set_copy(bset)) < 0)
3018 			return NULL;
3019 	}
3020 	if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3021 		return NULL;
3022 
3023 	if (cgbr->cone->n_dead == cgbr->cone->n_col) {
3024 		struct isl_vec *sample;
3025 		struct isl_tab_undo *snap;
3026 
3027 		if (cgbr->tab->basis) {
3028 			if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
3029 				isl_mat_free(cgbr->tab->basis);
3030 				cgbr->tab->basis = NULL;
3031 			}
3032 			cgbr->tab->n_zero = 0;
3033 			cgbr->tab->n_unbounded = 0;
3034 		}
3035 
3036 		snap = isl_tab_snap(cgbr->tab);
3037 
3038 		sample = isl_tab_sample(cgbr->tab);
3039 
3040 		if (!sample || isl_tab_rollback(cgbr->tab, snap) < 0) {
3041 			isl_vec_free(sample);
3042 			return NULL;
3043 		}
3044 
3045 		return sample;
3046 	}
3047 
3048 	cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
3049 	cone = drop_constant_terms(cone);
3050 	cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
3051 	cone = isl_basic_set_underlying_set(cone);
3052 	cone = isl_basic_set_gauss(cone, NULL);
3053 
3054 	bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
3055 	bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
3056 	bset = isl_basic_set_underlying_set(bset);
3057 	bset = isl_basic_set_gauss(bset, NULL);
3058 
3059 	return isl_basic_set_sample_with_cone(bset, cone);
3060 }
3061 
check_gbr_integer_feasible(struct isl_context_gbr * cgbr)3062 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
3063 {
3064 	struct isl_vec *sample;
3065 
3066 	if (!cgbr->tab)
3067 		return;
3068 
3069 	if (cgbr->tab->empty)
3070 		return;
3071 
3072 	sample = gbr_get_sample(cgbr);
3073 	if (!sample)
3074 		goto error;
3075 
3076 	if (sample->size == 0) {
3077 		isl_vec_free(sample);
3078 		if (isl_tab_mark_empty(cgbr->tab) < 0)
3079 			goto error;
3080 		return;
3081 	}
3082 
3083 	if (isl_tab_add_sample(cgbr->tab, sample) < 0)
3084 		goto error;
3085 
3086 	return;
3087 error:
3088 	isl_tab_free(cgbr->tab);
3089 	cgbr->tab = NULL;
3090 }
3091 
add_gbr_eq(struct isl_tab * tab,isl_int * eq)3092 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
3093 {
3094 	if (!tab)
3095 		return NULL;
3096 
3097 	if (isl_tab_extend_cons(tab, 2) < 0)
3098 		goto error;
3099 
3100 	if (isl_tab_add_eq(tab, eq) < 0)
3101 		goto error;
3102 
3103 	return tab;
3104 error:
3105 	isl_tab_free(tab);
3106 	return NULL;
3107 }
3108 
3109 /* Add the equality described by "eq" to the context.
3110  * If "check" is set, then we check if the context is empty after
3111  * adding the equality.
3112  * If "update" is set, then we check if the samples are still valid.
3113  *
3114  * We do not explicitly add shifted copies of the equality to
3115  * cgbr->shifted since they would conflict with each other.
3116  * Instead, we directly mark cgbr->shifted empty.
3117  */
context_gbr_add_eq(struct isl_context * context,isl_int * eq,int check,int update)3118 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
3119 		int check, int update)
3120 {
3121 	struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3122 
3123 	cgbr->tab = add_gbr_eq(cgbr->tab, eq);
3124 
3125 	if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
3126 		if (isl_tab_mark_empty(cgbr->shifted) < 0)
3127 			goto error;
3128 	}
3129 
3130 	if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
3131 		if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
3132 			goto error;
3133 		if (isl_tab_add_eq(cgbr->cone, eq) < 0)
3134 			goto error;
3135 	}
3136 
3137 	if (check) {
3138 		int v = tab_has_valid_sample(cgbr->tab, eq, 1);
3139 		if (v < 0)
3140 			goto error;
3141 		if (!v)
3142 			check_gbr_integer_feasible(cgbr);
3143 	}
3144 	if (update)
3145 		cgbr->tab = check_samples(cgbr->tab, eq, 1);
3146 	return;
3147 error:
3148 	isl_tab_free(cgbr->tab);
3149 	cgbr->tab = NULL;
3150 }
3151 
add_gbr_ineq(struct isl_context_gbr * cgbr,isl_int * ineq)3152 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
3153 {
3154 	if (!cgbr->tab)
3155 		return;
3156 
3157 	if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
3158 		goto error;
3159 
3160 	if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
3161 		goto error;
3162 
3163 	if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
3164 		int i;
3165 		isl_size dim;
3166 		dim = isl_basic_map_dim(cgbr->tab->bmap, isl_dim_all);
3167 		if (dim < 0)
3168 			goto error;
3169 
3170 		if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
3171 			goto error;
3172 
3173 		for (i = 0; i < dim; ++i) {
3174 			if (!isl_int_is_neg(ineq[1 + i]))
3175 				continue;
3176 			isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
3177 		}
3178 
3179 		if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
3180 			goto error;
3181 
3182 		for (i = 0; i < dim; ++i) {
3183 			if (!isl_int_is_neg(ineq[1 + i]))
3184 				continue;
3185 			isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
3186 		}
3187 	}
3188 
3189 	if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
3190 		if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
3191 			goto error;
3192 		if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
3193 			goto error;
3194 	}
3195 
3196 	return;
3197 error:
3198 	isl_tab_free(cgbr->tab);
3199 	cgbr->tab = NULL;
3200 }
3201 
context_gbr_add_ineq(struct isl_context * context,isl_int * ineq,int check,int update)3202 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
3203 		int check, int update)
3204 {
3205 	struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3206 
3207 	add_gbr_ineq(cgbr, ineq);
3208 	if (!cgbr->tab)
3209 		return;
3210 
3211 	if (check) {
3212 		int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
3213 		if (v < 0)
3214 			goto error;
3215 		if (!v)
3216 			check_gbr_integer_feasible(cgbr);
3217 	}
3218 	if (update)
3219 		cgbr->tab = check_samples(cgbr->tab, ineq, 0);
3220 	return;
3221 error:
3222 	isl_tab_free(cgbr->tab);
3223 	cgbr->tab = NULL;
3224 }
3225 
context_gbr_add_ineq_wrap(void * user,isl_int * ineq)3226 static isl_stat context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
3227 {
3228 	struct isl_context *context = (struct isl_context *)user;
3229 	context_gbr_add_ineq(context, ineq, 0, 0);
3230 	return context->op->is_ok(context) ? isl_stat_ok : isl_stat_error;
3231 }
3232 
context_gbr_ineq_sign(struct isl_context * context,isl_int * ineq,int strict)3233 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
3234 			isl_int *ineq, int strict)
3235 {
3236 	struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3237 	return tab_ineq_sign(cgbr->tab, ineq, strict);
3238 }
3239 
3240 /* Check whether "ineq" can be added to the tableau without rendering
3241  * it infeasible.
3242  */
context_gbr_test_ineq(struct isl_context * context,isl_int * ineq)3243 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
3244 {
3245 	struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3246 	struct isl_tab_undo *snap;
3247 	struct isl_tab_undo *shifted_snap = NULL;
3248 	struct isl_tab_undo *cone_snap = NULL;
3249 	int feasible;
3250 
3251 	if (!cgbr->tab)
3252 		return -1;
3253 
3254 	if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
3255 		return -1;
3256 
3257 	snap = isl_tab_snap(cgbr->tab);
3258 	if (cgbr->shifted)
3259 		shifted_snap = isl_tab_snap(cgbr->shifted);
3260 	if (cgbr->cone)
3261 		cone_snap = isl_tab_snap(cgbr->cone);
3262 	add_gbr_ineq(cgbr, ineq);
3263 	check_gbr_integer_feasible(cgbr);
3264 	if (!cgbr->tab)
3265 		return -1;
3266 	feasible = !cgbr->tab->empty;
3267 	if (isl_tab_rollback(cgbr->tab, snap) < 0)
3268 		return -1;
3269 	if (shifted_snap) {
3270 		if (isl_tab_rollback(cgbr->shifted, shifted_snap))
3271 			return -1;
3272 	} else if (cgbr->shifted) {
3273 		isl_tab_free(cgbr->shifted);
3274 		cgbr->shifted = NULL;
3275 	}
3276 	if (cone_snap) {
3277 		if (isl_tab_rollback(cgbr->cone, cone_snap))
3278 			return -1;
3279 	} else if (cgbr->cone) {
3280 		isl_tab_free(cgbr->cone);
3281 		cgbr->cone = NULL;
3282 	}
3283 
3284 	return feasible;
3285 }
3286 
3287 /* Return the column of the last of the variables associated to
3288  * a column that has a non-zero coefficient.
3289  * This function is called in a context where only coefficients
3290  * of parameters or divs can be non-zero.
3291  */
last_non_zero_var_col(struct isl_tab * tab,isl_int * p)3292 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
3293 {
3294 	int i;
3295 	int col;
3296 
3297 	if (tab->n_var == 0)
3298 		return -1;
3299 
3300 	for (i = tab->n_var - 1; i >= 0; --i) {
3301 		if (i >= tab->n_param && i < tab->n_var - tab->n_div)
3302 			continue;
3303 		if (tab->var[i].is_row)
3304 			continue;
3305 		col = tab->var[i].index;
3306 		if (!isl_int_is_zero(p[col]))
3307 			return col;
3308 	}
3309 
3310 	return -1;
3311 }
3312 
3313 /* Look through all the recently added equalities in the context
3314  * to see if we can propagate any of them to the main tableau.
3315  *
3316  * The newly added equalities in the context are encoded as pairs
3317  * of inequalities starting at inequality "first".
3318  *
3319  * We tentatively add each of these equalities to the main tableau
3320  * and if this happens to result in a row with a final coefficient
3321  * that is one or negative one, we use it to kill a column
3322  * in the main tableau.  Otherwise, we discard the tentatively
3323  * added row.
3324  * This tentative addition of equality constraints turns
3325  * on the undo facility of the tableau.  Turn it off again
3326  * at the end, assuming it was turned off to begin with.
3327  *
3328  * Return 0 on success and -1 on failure.
3329  */
propagate_equalities(struct isl_context_gbr * cgbr,struct isl_tab * tab,unsigned first)3330 static int propagate_equalities(struct isl_context_gbr *cgbr,
3331 	struct isl_tab *tab, unsigned first)
3332 {
3333 	int i;
3334 	struct isl_vec *eq = NULL;
3335 	isl_bool needs_undo;
3336 
3337 	needs_undo = isl_tab_need_undo(tab);
3338 	if (needs_undo < 0)
3339 		goto error;
3340 	eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
3341 	if (!eq)
3342 		goto error;
3343 
3344 	if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
3345 		goto error;
3346 
3347 	isl_seq_clr(eq->el + 1 + tab->n_param,
3348 		    tab->n_var - tab->n_param - tab->n_div);
3349 	for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3350 		int j;
3351 		int r;
3352 		struct isl_tab_undo *snap;
3353 		snap = isl_tab_snap(tab);
3354 
3355 		isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3356 		isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3357 			    cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3358 			    tab->n_div);
3359 
3360 		r = isl_tab_add_row(tab, eq->el);
3361 		if (r < 0)
3362 			goto error;
3363 		r = tab->con[r].index;
3364 		j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3365 		if (j < 0 || j < tab->n_dead ||
3366 		    !isl_int_is_one(tab->mat->row[r][0]) ||
3367 		    (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3368 		     !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3369 			if (isl_tab_rollback(tab, snap) < 0)
3370 				goto error;
3371 			continue;
3372 		}
3373 		if (isl_tab_pivot(tab, r, j) < 0)
3374 			goto error;
3375 		if (isl_tab_kill_col(tab, j) < 0)
3376 			goto error;
3377 
3378 		if (restore_lexmin(tab) < 0)
3379 			goto error;
3380 	}
3381 
3382 	if (!needs_undo)
3383 		isl_tab_clear_undo(tab);
3384 	isl_vec_free(eq);
3385 
3386 	return 0;
3387 error:
3388 	isl_vec_free(eq);
3389 	isl_tab_free(cgbr->tab);
3390 	cgbr->tab = NULL;
3391 	return -1;
3392 }
3393 
context_gbr_detect_equalities(struct isl_context * context,struct isl_tab * tab)3394 static int context_gbr_detect_equalities(struct isl_context *context,
3395 	struct isl_tab *tab)
3396 {
3397 	struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3398 	unsigned n_ineq;
3399 
3400 	if (!cgbr->cone) {
3401 		struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3402 		cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3403 		if (!cgbr->cone)
3404 			goto error;
3405 		if (isl_tab_track_bset(cgbr->cone,
3406 					isl_basic_set_copy(bset)) < 0)
3407 			goto error;
3408 	}
3409 	if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3410 		goto error;
3411 
3412 	n_ineq = cgbr->tab->bmap->n_ineq;
3413 	cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3414 	if (!cgbr->tab)
3415 		return -1;
3416 	if (cgbr->tab->bmap->n_ineq > n_ineq &&
3417 	    propagate_equalities(cgbr, tab, n_ineq) < 0)
3418 		return -1;
3419 
3420 	return 0;
3421 error:
3422 	isl_tab_free(cgbr->tab);
3423 	cgbr->tab = NULL;
3424 	return -1;
3425 }
3426 
context_gbr_get_div(struct isl_context * context,struct isl_tab * tab,struct isl_vec * div)3427 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3428 		struct isl_vec *div)
3429 {
3430 	return get_div(tab, context, div);
3431 }
3432 
context_gbr_insert_div(struct isl_context * context,int pos,__isl_keep isl_vec * div)3433 static isl_bool context_gbr_insert_div(struct isl_context *context, int pos,
3434 	__isl_keep isl_vec *div)
3435 {
3436 	struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3437 	if (cgbr->cone) {
3438 		int r, o_div;
3439 		isl_size n_div;
3440 
3441 		n_div = isl_basic_map_dim(cgbr->cone->bmap, isl_dim_div);
3442 		if (n_div < 0)
3443 			return isl_bool_error;
3444 		o_div = cgbr->cone->n_var - n_div;
3445 
3446 		if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3447 			return isl_bool_error;
3448 		if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3449 			return isl_bool_error;
3450 		if ((r = isl_tab_insert_var(cgbr->cone, pos)) <0)
3451 			return isl_bool_error;
3452 
3453 		cgbr->cone->bmap = isl_basic_map_insert_div(cgbr->cone->bmap,
3454 						    r - o_div, div);
3455 		if (!cgbr->cone->bmap)
3456 			return isl_bool_error;
3457 		if (isl_tab_push_var(cgbr->cone, isl_tab_undo_bmap_div,
3458 				    &cgbr->cone->var[r]) < 0)
3459 			return isl_bool_error;
3460 	}
3461 	return context_tab_insert_div(cgbr->tab, pos, div,
3462 					context_gbr_add_ineq_wrap, context);
3463 }
3464 
context_gbr_best_split(struct isl_context * context,struct isl_tab * tab)3465 static int context_gbr_best_split(struct isl_context *context,
3466 		struct isl_tab *tab)
3467 {
3468 	struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3469 	struct isl_tab_undo *snap;
3470 	int r;
3471 
3472 	snap = isl_tab_snap(cgbr->tab);
3473 	r = best_split(tab, cgbr->tab);
3474 
3475 	if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3476 		return -1;
3477 
3478 	return r;
3479 }
3480 
context_gbr_is_empty(struct isl_context * context)3481 static int context_gbr_is_empty(struct isl_context *context)
3482 {
3483 	struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3484 	if (!cgbr->tab)
3485 		return -1;
3486 	return cgbr->tab->empty;
3487 }
3488 
3489 struct isl_gbr_tab_undo {
3490 	struct isl_tab_undo *tab_snap;
3491 	struct isl_tab_undo *shifted_snap;
3492 	struct isl_tab_undo *cone_snap;
3493 };
3494 
context_gbr_save(struct isl_context * context)3495 static void *context_gbr_save(struct isl_context *context)
3496 {
3497 	struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3498 	struct isl_gbr_tab_undo *snap;
3499 
3500 	if (!cgbr->tab)
3501 		return NULL;
3502 
3503 	snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3504 	if (!snap)
3505 		return NULL;
3506 
3507 	snap->tab_snap = isl_tab_snap(cgbr->tab);
3508 	if (isl_tab_save_samples(cgbr->tab) < 0)
3509 		goto error;
3510 
3511 	if (cgbr->shifted)
3512 		snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3513 	else
3514 		snap->shifted_snap = NULL;
3515 
3516 	if (cgbr->cone)
3517 		snap->cone_snap = isl_tab_snap(cgbr->cone);
3518 	else
3519 		snap->cone_snap = NULL;
3520 
3521 	return snap;
3522 error:
3523 	free(snap);
3524 	return NULL;
3525 }
3526 
context_gbr_restore(struct isl_context * context,void * save)3527 static void context_gbr_restore(struct isl_context *context, void *save)
3528 {
3529 	struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3530 	struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3531 	if (!snap)
3532 		goto error;
3533 	if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0)
3534 		goto error;
3535 
3536 	if (snap->shifted_snap) {
3537 		if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3538 			goto error;
3539 	} else if (cgbr->shifted) {
3540 		isl_tab_free(cgbr->shifted);
3541 		cgbr->shifted = NULL;
3542 	}
3543 
3544 	if (snap->cone_snap) {
3545 		if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3546 			goto error;
3547 	} else if (cgbr->cone) {
3548 		isl_tab_free(cgbr->cone);
3549 		cgbr->cone = NULL;
3550 	}
3551 
3552 	free(snap);
3553 
3554 	return;
3555 error:
3556 	free(snap);
3557 	isl_tab_free(cgbr->tab);
3558 	cgbr->tab = NULL;
3559 }
3560 
context_gbr_discard(void * save)3561 static void context_gbr_discard(void *save)
3562 {
3563 	struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3564 	free(snap);
3565 }
3566 
context_gbr_is_ok(struct isl_context * context)3567 static int context_gbr_is_ok(struct isl_context *context)
3568 {
3569 	struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3570 	return !!cgbr->tab;
3571 }
3572 
context_gbr_invalidate(struct isl_context * context)3573 static void context_gbr_invalidate(struct isl_context *context)
3574 {
3575 	struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3576 	isl_tab_free(cgbr->tab);
3577 	cgbr->tab = NULL;
3578 }
3579 
context_gbr_free(struct isl_context * context)3580 static __isl_null struct isl_context *context_gbr_free(
3581 	struct isl_context *context)
3582 {
3583 	struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3584 	isl_tab_free(cgbr->tab);
3585 	isl_tab_free(cgbr->shifted);
3586 	isl_tab_free(cgbr->cone);
3587 	free(cgbr);
3588 
3589 	return NULL;
3590 }
3591 
3592 struct isl_context_op isl_context_gbr_op = {
3593 	context_gbr_detect_nonnegative_parameters,
3594 	context_gbr_peek_basic_set,
3595 	context_gbr_peek_tab,
3596 	context_gbr_add_eq,
3597 	context_gbr_add_ineq,
3598 	context_gbr_ineq_sign,
3599 	context_gbr_test_ineq,
3600 	context_gbr_get_div,
3601 	context_gbr_insert_div,
3602 	context_gbr_detect_equalities,
3603 	context_gbr_best_split,
3604 	context_gbr_is_empty,
3605 	context_gbr_is_ok,
3606 	context_gbr_save,
3607 	context_gbr_restore,
3608 	context_gbr_discard,
3609 	context_gbr_invalidate,
3610 	context_gbr_free,
3611 };
3612 
isl_context_gbr_alloc(__isl_keep isl_basic_set * dom)3613 static struct isl_context *isl_context_gbr_alloc(__isl_keep isl_basic_set *dom)
3614 {
3615 	struct isl_context_gbr *cgbr;
3616 
3617 	if (!dom)
3618 		return NULL;
3619 
3620 	cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3621 	if (!cgbr)
3622 		return NULL;
3623 
3624 	cgbr->context.op = &isl_context_gbr_op;
3625 
3626 	cgbr->shifted = NULL;
3627 	cgbr->cone = NULL;
3628 	cgbr->tab = isl_tab_from_basic_set(dom, 1);
3629 	cgbr->tab = isl_tab_init_samples(cgbr->tab);
3630 	if (!cgbr->tab)
3631 		goto error;
3632 	check_gbr_integer_feasible(cgbr);
3633 
3634 	return &cgbr->context;
3635 error:
3636 	cgbr->context.op->free(&cgbr->context);
3637 	return NULL;
3638 }
3639 
3640 /* Allocate a context corresponding to "dom".
3641  * The representation specific fields are initialized by
3642  * isl_context_lex_alloc or isl_context_gbr_alloc.
3643  * The shared "n_unknown" field is initialized to the number
3644  * of final unknown integer divisions in "dom".
3645  */
isl_context_alloc(__isl_keep isl_basic_set * dom)3646 static struct isl_context *isl_context_alloc(__isl_keep isl_basic_set *dom)
3647 {
3648 	struct isl_context *context;
3649 	int first;
3650 	isl_size n_div;
3651 
3652 	if (!dom)
3653 		return NULL;
3654 
3655 	if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3656 		context = isl_context_lex_alloc(dom);
3657 	else
3658 		context = isl_context_gbr_alloc(dom);
3659 
3660 	if (!context)
3661 		return NULL;
3662 
3663 	first = isl_basic_set_first_unknown_div(dom);
3664 	n_div = isl_basic_set_dim(dom, isl_dim_div);
3665 	if (first < 0 || n_div < 0)
3666 		return context->op->free(context);
3667 	context->n_unknown = n_div - first;
3668 
3669 	return context;
3670 }
3671 
3672 /* Initialize some common fields of "sol", which keeps track
3673  * of the solution of an optimization problem on "bmap" over
3674  * the domain "dom".
3675  * If "max" is set, then a maximization problem is being solved, rather than
3676  * a minimization problem, which means that the variables in the
3677  * tableau have value "M - x" rather than "M + x".
3678  */
sol_init(struct isl_sol * sol,__isl_keep isl_basic_map * bmap,__isl_keep isl_basic_set * dom,int max)3679 static isl_stat sol_init(struct isl_sol *sol, __isl_keep isl_basic_map *bmap,
3680 	__isl_keep isl_basic_set *dom, int max)
3681 {
3682 	sol->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3683 	sol->dec_level.callback.run = &sol_dec_level_wrap;
3684 	sol->dec_level.sol = sol;
3685 	sol->max = max;
3686 	sol->n_out = isl_basic_map_dim(bmap, isl_dim_out);
3687 	sol->space = isl_basic_map_get_space(bmap);
3688 
3689 	sol->context = isl_context_alloc(dom);
3690 	if (sol->n_out < 0 || !sol->space || !sol->context)
3691 		return isl_stat_error;
3692 
3693 	return isl_stat_ok;
3694 }
3695 
3696 /* Construct an isl_sol_map structure for accumulating the solution.
3697  * If track_empty is set, then we also keep track of the parts
3698  * of the context where there is no solution.
3699  * If max is set, then we are solving a maximization, rather than
3700  * a minimization problem, which means that the variables in the
3701  * tableau have value "M - x" rather than "M + x".
3702  */
sol_map_init(__isl_keep isl_basic_map * bmap,__isl_take isl_basic_set * dom,int track_empty,int max)3703 static struct isl_sol *sol_map_init(__isl_keep isl_basic_map *bmap,
3704 	__isl_take isl_basic_set *dom, int track_empty, int max)
3705 {
3706 	struct isl_sol_map *sol_map = NULL;
3707 	isl_space *space;
3708 
3709 	if (!bmap)
3710 		goto error;
3711 
3712 	sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3713 	if (!sol_map)
3714 		goto error;
3715 
3716 	sol_map->sol.free = &sol_map_free;
3717 	if (sol_init(&sol_map->sol, bmap, dom, max) < 0)
3718 		goto error;
3719 	sol_map->sol.add = &sol_map_add_wrap;
3720 	sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3721 	space = isl_space_copy(sol_map->sol.space);
3722 	sol_map->map = isl_map_alloc_space(space, 1, ISL_MAP_DISJOINT);
3723 	if (!sol_map->map)
3724 		goto error;
3725 
3726 	if (track_empty) {
3727 		sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
3728 							1, ISL_SET_DISJOINT);
3729 		if (!sol_map->empty)
3730 			goto error;
3731 	}
3732 
3733 	isl_basic_set_free(dom);
3734 	return &sol_map->sol;
3735 error:
3736 	isl_basic_set_free(dom);
3737 	sol_free(&sol_map->sol);
3738 	return NULL;
3739 }
3740 
3741 /* Check whether all coefficients of (non-parameter) variables
3742  * are non-positive, meaning that no pivots can be performed on the row.
3743  */
is_critical(struct isl_tab * tab,int row)3744 static int is_critical(struct isl_tab *tab, int row)
3745 {
3746 	int j;
3747 	unsigned off = 2 + tab->M;
3748 
3749 	for (j = tab->n_dead; j < tab->n_col; ++j) {
3750 		if (col_is_parameter_var(tab, j))
3751 			continue;
3752 
3753 		if (isl_int_is_pos(tab->mat->row[row][off + j]))
3754 			return 0;
3755 	}
3756 
3757 	return 1;
3758 }
3759 
3760 /* Check whether the inequality represented by vec is strict over the integers,
3761  * i.e., there are no integer values satisfying the constraint with
3762  * equality.  This happens if the gcd of the coefficients is not a divisor
3763  * of the constant term.  If so, scale the constraint down by the gcd
3764  * of the coefficients.
3765  */
is_strict(struct isl_vec * vec)3766 static int is_strict(struct isl_vec *vec)
3767 {
3768 	isl_int gcd;
3769 	int strict = 0;
3770 
3771 	isl_int_init(gcd);
3772 	isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3773 	if (!isl_int_is_one(gcd)) {
3774 		strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3775 		isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3776 		isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3777 	}
3778 	isl_int_clear(gcd);
3779 
3780 	return strict;
3781 }
3782 
3783 /* Determine the sign of the given row of the main tableau.
3784  * The result is one of
3785  *	isl_tab_row_pos: always non-negative; no pivot needed
3786  *	isl_tab_row_neg: always non-positive; pivot
3787  *	isl_tab_row_any: can be both positive and negative; split
3788  *
3789  * We first handle some simple cases
3790  *	- the row sign may be known already
3791  *	- the row may be obviously non-negative
3792  *	- the parametric constant may be equal to that of another row
3793  *	  for which we know the sign.  This sign will be either "pos" or
3794  *	  "any".  If it had been "neg" then we would have pivoted before.
3795  *
3796  * If none of these cases hold, we check the value of the row for each
3797  * of the currently active samples.  Based on the signs of these values
3798  * we make an initial determination of the sign of the row.
3799  *
3800  *	all zero			->	unk(nown)
3801  *	all non-negative		->	pos
3802  *	all non-positive		->	neg
3803  *	both negative and positive	->	all
3804  *
3805  * If we end up with "all", we are done.
3806  * Otherwise, we perform a check for positive and/or negative
3807  * values as follows.
3808  *
3809  *	samples	       neg	       unk	       pos
3810  *	<0 ?			    Y        N	    Y        N
3811  *					    pos    any      pos
3812  *	>0 ?	     Y      N	 Y     N
3813  *		    any    neg  any   neg
3814  *
3815  * There is no special sign for "zero", because we can usually treat zero
3816  * as either non-negative or non-positive, whatever works out best.
3817  * However, if the row is "critical", meaning that pivoting is impossible
3818  * then we don't want to limp zero with the non-positive case, because
3819  * then we we would lose the solution for those values of the parameters
3820  * where the value of the row is zero.  Instead, we treat 0 as non-negative
3821  * ensuring a split if the row can attain both zero and negative values.
3822  * The same happens when the original constraint was one that could not
3823  * be satisfied with equality by any integer values of the parameters.
3824  * In this case, we normalize the constraint, but then a value of zero
3825  * for the normalized constraint is actually a positive value for the
3826  * original constraint, so again we need to treat zero as non-negative.
3827  * In both these cases, we have the following decision tree instead:
3828  *
3829  *	all non-negative		->	pos
3830  *	all negative			->	neg
3831  *	both negative and non-negative	->	all
3832  *
3833  *	samples	       neg	          	       pos
3834  *	<0 ?			             	    Y        N
3835  *					           any      pos
3836  *	>=0 ?	     Y      N
3837  *		    any    neg
3838  */
row_sign(struct isl_tab * tab,struct isl_sol * sol,int row)3839 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3840 	struct isl_sol *sol, int row)
3841 {
3842 	struct isl_vec *ineq = NULL;
3843 	enum isl_tab_row_sign res = isl_tab_row_unknown;
3844 	int critical;
3845 	int strict;
3846 	int row2;
3847 
3848 	if (tab->row_sign[row] != isl_tab_row_unknown)
3849 		return tab->row_sign[row];
3850 	if (is_obviously_nonneg(tab, row))
3851 		return isl_tab_row_pos;
3852 	for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3853 		if (tab->row_sign[row2] == isl_tab_row_unknown)
3854 			continue;
3855 		if (identical_parameter_line(tab, row, row2))
3856 			return tab->row_sign[row2];
3857 	}
3858 
3859 	critical = is_critical(tab, row);
3860 
3861 	ineq = get_row_parameter_ineq(tab, row);
3862 	if (!ineq)
3863 		goto error;
3864 
3865 	strict = is_strict(ineq);
3866 
3867 	res = sol->context->op->ineq_sign(sol->context, ineq->el,
3868 					  critical || strict);
3869 
3870 	if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3871 		/* test for negative values */
3872 		int feasible;
3873 		isl_seq_neg(ineq->el, ineq->el, ineq->size);
3874 		isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3875 
3876 		feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3877 		if (feasible < 0)
3878 			goto error;
3879 		if (!feasible)
3880 			res = isl_tab_row_pos;
3881 		else
3882 			res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3883 							   : isl_tab_row_any;
3884 		if (res == isl_tab_row_neg) {
3885 			isl_seq_neg(ineq->el, ineq->el, ineq->size);
3886 			isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3887 		}
3888 	}
3889 
3890 	if (res == isl_tab_row_neg) {
3891 		/* test for positive values */
3892 		int feasible;
3893 		if (!critical && !strict)
3894 			isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3895 
3896 		feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3897 		if (feasible < 0)
3898 			goto error;
3899 		if (feasible)
3900 			res = isl_tab_row_any;
3901 	}
3902 
3903 	isl_vec_free(ineq);
3904 	return res;
3905 error:
3906 	isl_vec_free(ineq);
3907 	return isl_tab_row_unknown;
3908 }
3909 
3910 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3911 
3912 /* Find solutions for values of the parameters that satisfy the given
3913  * inequality.
3914  *
3915  * We currently take a snapshot of the context tableau that is reset
3916  * when we return from this function, while we make a copy of the main
3917  * tableau, leaving the original main tableau untouched.
3918  * These are fairly arbitrary choices.  Making a copy also of the context
3919  * tableau would obviate the need to undo any changes made to it later,
3920  * while taking a snapshot of the main tableau could reduce memory usage.
3921  * If we were to switch to taking a snapshot of the main tableau,
3922  * we would have to keep in mind that we need to save the row signs
3923  * and that we need to do this before saving the current basis
3924  * such that the basis has been restore before we restore the row signs.
3925  */
find_in_pos(struct isl_sol * sol,struct isl_tab * tab,isl_int * ineq)3926 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3927 {
3928 	void *saved;
3929 
3930 	if (!sol->context)
3931 		goto error;
3932 	saved = sol->context->op->save(sol->context);
3933 
3934 	tab = isl_tab_dup(tab);
3935 	if (!tab)
3936 		goto error;
3937 
3938 	sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3939 
3940 	find_solutions(sol, tab);
3941 
3942 	if (!sol->error)
3943 		sol->context->op->restore(sol->context, saved);
3944 	else
3945 		sol->context->op->discard(saved);
3946 	return;
3947 error:
3948 	sol->error = 1;
3949 }
3950 
3951 /* Record the absence of solutions for those values of the parameters
3952  * that do not satisfy the given inequality with equality.
3953  */
no_sol_in_strict(struct isl_sol * sol,struct isl_tab * tab,struct isl_vec * ineq)3954 static void no_sol_in_strict(struct isl_sol *sol,
3955 	struct isl_tab *tab, struct isl_vec *ineq)
3956 {
3957 	int empty;
3958 	void *saved;
3959 
3960 	if (!sol->context || sol->error)
3961 		goto error;
3962 	saved = sol->context->op->save(sol->context);
3963 
3964 	isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3965 
3966 	sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3967 	if (!sol->context)
3968 		goto error;
3969 
3970 	empty = tab->empty;
3971 	tab->empty = 1;
3972 	sol_add(sol, tab);
3973 	tab->empty = empty;
3974 
3975 	isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3976 
3977 	sol->context->op->restore(sol->context, saved);
3978 	return;
3979 error:
3980 	sol->error = 1;
3981 }
3982 
3983 /* Reset all row variables that are marked to have a sign that may
3984  * be both positive and negative to have an unknown sign.
3985  */
reset_any_to_unknown(struct isl_tab * tab)3986 static void reset_any_to_unknown(struct isl_tab *tab)
3987 {
3988 	int row;
3989 
3990 	for (row = tab->n_redundant; row < tab->n_row; ++row) {
3991 		if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3992 			continue;
3993 		if (tab->row_sign[row] == isl_tab_row_any)
3994 			tab->row_sign[row] = isl_tab_row_unknown;
3995 	}
3996 }
3997 
3998 /* Compute the lexicographic minimum of the set represented by the main
3999  * tableau "tab" within the context "sol->context_tab".
4000  * On entry the sample value of the main tableau is lexicographically
4001  * less than or equal to this lexicographic minimum.
4002  * Pivots are performed until a feasible point is found, which is then
4003  * necessarily equal to the minimum, or until the tableau is found to
4004  * be infeasible.  Some pivots may need to be performed for only some
4005  * feasible values of the context tableau.  If so, the context tableau
4006  * is split into a part where the pivot is needed and a part where it is not.
4007  *
4008  * Whenever we enter the main loop, the main tableau is such that no
4009  * "obvious" pivots need to be performed on it, where "obvious" means
4010  * that the given row can be seen to be negative without looking at
4011  * the context tableau.  In particular, for non-parametric problems,
4012  * no pivots need to be performed on the main tableau.
4013  * The caller of find_solutions is responsible for making this property
4014  * hold prior to the first iteration of the loop, while restore_lexmin
4015  * is called before every other iteration.
4016  *
4017  * Inside the main loop, we first examine the signs of the rows of
4018  * the main tableau within the context of the context tableau.
4019  * If we find a row that is always non-positive for all values of
4020  * the parameters satisfying the context tableau and negative for at
4021  * least one value of the parameters, we perform the appropriate pivot
4022  * and start over.  An exception is the case where no pivot can be
4023  * performed on the row.  In this case, we require that the sign of
4024  * the row is negative for all values of the parameters (rather than just
4025  * non-positive).  This special case is handled inside row_sign, which
4026  * will say that the row can have any sign if it determines that it can
4027  * attain both negative and zero values.
4028  *
4029  * If we can't find a row that always requires a pivot, but we can find
4030  * one or more rows that require a pivot for some values of the parameters
4031  * (i.e., the row can attain both positive and negative signs), then we split
4032  * the context tableau into two parts, one where we force the sign to be
4033  * non-negative and one where we force is to be negative.
4034  * The non-negative part is handled by a recursive call (through find_in_pos).
4035  * Upon returning from this call, we continue with the negative part and
4036  * perform the required pivot.
4037  *
4038  * If no such rows can be found, all rows are non-negative and we have
4039  * found a (rational) feasible point.  If we only wanted a rational point
4040  * then we are done.
4041  * Otherwise, we check if all values of the sample point of the tableau
4042  * are integral for the variables.  If so, we have found the minimal
4043  * integral point and we are done.
4044  * If the sample point is not integral, then we need to make a distinction
4045  * based on whether the constant term is non-integral or the coefficients
4046  * of the parameters.  Furthermore, in order to decide how to handle
4047  * the non-integrality, we also need to know whether the coefficients
4048  * of the other columns in the tableau are integral.  This leads
4049  * to the following table.  The first two rows do not correspond
4050  * to a non-integral sample point and are only mentioned for completeness.
4051  *
4052  *	constant	parameters	other
4053  *
4054  *	int		int		int	|
4055  *	int		int		rat	| -> no problem
4056  *
4057  *	rat		int		int	  -> fail
4058  *
4059  *	rat		int		rat	  -> cut
4060  *
4061  *	int		rat		rat	|
4062  *	rat		rat		rat	| -> parametric cut
4063  *
4064  *	int		rat		int	|
4065  *	rat		rat		int	| -> split context
4066  *
4067  * If the parametric constant is completely integral, then there is nothing
4068  * to be done.  If the constant term is non-integral, but all the other
4069  * coefficient are integral, then there is nothing that can be done
4070  * and the tableau has no integral solution.
4071  * If, on the other hand, one or more of the other columns have rational
4072  * coefficients, but the parameter coefficients are all integral, then
4073  * we can perform a regular (non-parametric) cut.
4074  * Finally, if there is any parameter coefficient that is non-integral,
4075  * then we need to involve the context tableau.  There are two cases here.
4076  * If at least one other column has a rational coefficient, then we
4077  * can perform a parametric cut in the main tableau by adding a new
4078  * integer division in the context tableau.
4079  * If all other columns have integral coefficients, then we need to
4080  * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
4081  * is always integral.  We do this by introducing an integer division
4082  * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
4083  * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
4084  * Since q is expressed in the tableau as
4085  *	c + \sum a_i y_i - m q >= 0
4086  *	-c - \sum a_i y_i + m q + m - 1 >= 0
4087  * it is sufficient to add the inequality
4088  *	-c - \sum a_i y_i + m q >= 0
4089  * In the part of the context where this inequality does not hold, the
4090  * main tableau is marked as being empty.
4091  */
find_solutions(struct isl_sol * sol,struct isl_tab * tab)4092 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
4093 {
4094 	struct isl_context *context;
4095 	int r;
4096 
4097 	if (!tab || sol->error)
4098 		goto error;
4099 
4100 	context = sol->context;
4101 
4102 	if (tab->empty)
4103 		goto done;
4104 	if (context->op->is_empty(context))
4105 		goto done;
4106 
4107 	for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
4108 		int flags;
4109 		int row;
4110 		enum isl_tab_row_sign sgn;
4111 		int split = -1;
4112 		int n_split = 0;
4113 
4114 		for (row = tab->n_redundant; row < tab->n_row; ++row) {
4115 			if (!isl_tab_var_from_row(tab, row)->is_nonneg)
4116 				continue;
4117 			sgn = row_sign(tab, sol, row);
4118 			if (!sgn)
4119 				goto error;
4120 			tab->row_sign[row] = sgn;
4121 			if (sgn == isl_tab_row_any)
4122 				n_split++;
4123 			if (sgn == isl_tab_row_any && split == -1)
4124 				split = row;
4125 			if (sgn == isl_tab_row_neg)
4126 				break;
4127 		}
4128 		if (row < tab->n_row)
4129 			continue;
4130 		if (split != -1) {
4131 			struct isl_vec *ineq;
4132 			if (n_split != 1)
4133 				split = context->op->best_split(context, tab);
4134 			if (split < 0)
4135 				goto error;
4136 			ineq = get_row_parameter_ineq(tab, split);
4137 			if (!ineq)
4138 				goto error;
4139 			is_strict(ineq);
4140 			reset_any_to_unknown(tab);
4141 			tab->row_sign[split] = isl_tab_row_pos;
4142 			sol_inc_level(sol);
4143 			find_in_pos(sol, tab, ineq->el);
4144 			tab->row_sign[split] = isl_tab_row_neg;
4145 			isl_seq_neg(ineq->el, ineq->el, ineq->size);
4146 			isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
4147 			if (!sol->error)
4148 				context->op->add_ineq(context, ineq->el, 0, 1);
4149 			isl_vec_free(ineq);
4150 			if (sol->error)
4151 				goto error;
4152 			continue;
4153 		}
4154 		if (tab->rational)
4155 			break;
4156 		row = first_non_integer_row(tab, &flags);
4157 		if (row < 0)
4158 			break;
4159 		if (ISL_FL_ISSET(flags, I_PAR)) {
4160 			if (ISL_FL_ISSET(flags, I_VAR)) {
4161 				if (isl_tab_mark_empty(tab) < 0)
4162 					goto error;
4163 				break;
4164 			}
4165 			row = add_cut(tab, row);
4166 		} else if (ISL_FL_ISSET(flags, I_VAR)) {
4167 			struct isl_vec *div;
4168 			struct isl_vec *ineq;
4169 			int d;
4170 			div = get_row_split_div(tab, row);
4171 			if (!div)
4172 				goto error;
4173 			d = context->op->get_div(context, tab, div);
4174 			isl_vec_free(div);
4175 			if (d < 0)
4176 				goto error;
4177 			ineq = ineq_for_div(context->op->peek_basic_set(context), d);
4178 			if (!ineq)
4179 				goto error;
4180 			sol_inc_level(sol);
4181 			no_sol_in_strict(sol, tab, ineq);
4182 			isl_seq_neg(ineq->el, ineq->el, ineq->size);
4183 			context->op->add_ineq(context, ineq->el, 1, 1);
4184 			isl_vec_free(ineq);
4185 			if (sol->error || !context->op->is_ok(context))
4186 				goto error;
4187 			tab = set_row_cst_to_div(tab, row, d);
4188 			if (context->op->is_empty(context))
4189 				break;
4190 		} else
4191 			row = add_parametric_cut(tab, row, context);
4192 		if (row < 0)
4193 			goto error;
4194 	}
4195 	if (r < 0)
4196 		goto error;
4197 done:
4198 	sol_add(sol, tab);
4199 	isl_tab_free(tab);
4200 	return;
4201 error:
4202 	isl_tab_free(tab);
4203 	sol->error = 1;
4204 }
4205 
4206 /* Does "sol" contain a pair of partial solutions that could potentially
4207  * be merged?
4208  *
4209  * We currently only check that "sol" is not in an error state
4210  * and that there are at least two partial solutions of which the final two
4211  * are defined at the same level.
4212  */
sol_has_mergeable_solutions(struct isl_sol * sol)4213 static int sol_has_mergeable_solutions(struct isl_sol *sol)
4214 {
4215 	if (sol->error)
4216 		return 0;
4217 	if (!sol->partial)
4218 		return 0;
4219 	if (!sol->partial->next)
4220 		return 0;
4221 	return sol->partial->level == sol->partial->next->level;
4222 }
4223 
4224 /* Compute the lexicographic minimum of the set represented by the main
4225  * tableau "tab" within the context "sol->context_tab".
4226  *
4227  * As a preprocessing step, we first transfer all the purely parametric
4228  * equalities from the main tableau to the context tableau, i.e.,
4229  * parameters that have been pivoted to a row.
4230  * These equalities are ignored by the main algorithm, because the
4231  * corresponding rows may not be marked as being non-negative.
4232  * In parts of the context where the added equality does not hold,
4233  * the main tableau is marked as being empty.
4234  *
4235  * Before we embark on the actual computation, we save a copy
4236  * of the context.  When we return, we check if there are any
4237  * partial solutions that can potentially be merged.  If so,
4238  * we perform a rollback to the initial state of the context.
4239  * The merging of partial solutions happens inside calls to
4240  * sol_dec_level that are pushed onto the undo stack of the context.
4241  * If there are no partial solutions that can potentially be merged
4242  * then the rollback is skipped as it would just be wasted effort.
4243  */
find_solutions_main(struct isl_sol * sol,struct isl_tab * tab)4244 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
4245 {
4246 	int row;
4247 	void *saved;
4248 
4249 	if (!tab)
4250 		goto error;
4251 
4252 	sol->level = 0;
4253 
4254 	for (row = tab->n_redundant; row < tab->n_row; ++row) {
4255 		int p;
4256 		struct isl_vec *eq;
4257 
4258 		if (!row_is_parameter_var(tab, row))
4259 			continue;
4260 		if (tab->row_var[row] < tab->n_param)
4261 			p = tab->row_var[row];
4262 		else
4263 			p = tab->row_var[row]
4264 				+ tab->n_param - (tab->n_var - tab->n_div);
4265 
4266 		eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
4267 		if (!eq)
4268 			goto error;
4269 		get_row_parameter_line(tab, row, eq->el);
4270 		isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
4271 		eq = isl_vec_normalize(eq);
4272 
4273 		sol_inc_level(sol);
4274 		no_sol_in_strict(sol, tab, eq);
4275 
4276 		isl_seq_neg(eq->el, eq->el, eq->size);
4277 		sol_inc_level(sol);
4278 		no_sol_in_strict(sol, tab, eq);
4279 		isl_seq_neg(eq->el, eq->el, eq->size);
4280 
4281 		sol->context->op->add_eq(sol->context, eq->el, 1, 1);
4282 
4283 		isl_vec_free(eq);
4284 
4285 		if (isl_tab_mark_redundant(tab, row) < 0)
4286 			goto error;
4287 
4288 		if (sol->context->op->is_empty(sol->context))
4289 			break;
4290 
4291 		row = tab->n_redundant - 1;
4292 	}
4293 
4294 	saved = sol->context->op->save(sol->context);
4295 
4296 	find_solutions(sol, tab);
4297 
4298 	if (sol_has_mergeable_solutions(sol))
4299 		sol->context->op->restore(sol->context, saved);
4300 	else
4301 		sol->context->op->discard(saved);
4302 
4303 	sol->level = 0;
4304 	sol_pop(sol);
4305 
4306 	return;
4307 error:
4308 	isl_tab_free(tab);
4309 	sol->error = 1;
4310 }
4311 
4312 /* Check if integer division "div" of "dom" also occurs in "bmap".
4313  * If so, return its position within the divs.
4314  * Otherwise, return a position beyond the integer divisions.
4315  */
find_context_div(__isl_keep isl_basic_map * bmap,__isl_keep isl_basic_set * dom,unsigned div)4316 static int find_context_div(__isl_keep isl_basic_map *bmap,
4317 	__isl_keep isl_basic_set *dom, unsigned div)
4318 {
4319 	int i;
4320 	isl_size b_v_div, d_v_div;
4321 	isl_size n_div;
4322 
4323 	b_v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4324 	d_v_div = isl_basic_set_var_offset(dom, isl_dim_div);
4325 	n_div = isl_basic_map_dim(bmap, isl_dim_div);
4326 	if (b_v_div < 0 || d_v_div < 0 || n_div < 0)
4327 		return -1;
4328 
4329 	if (isl_int_is_zero(dom->div[div][0]))
4330 		return n_div;
4331 	if (isl_seq_first_non_zero(dom->div[div] + 2 + d_v_div,
4332 				    dom->n_div) != -1)
4333 		return n_div;
4334 
4335 	for (i = 0; i < n_div; ++i) {
4336 		if (isl_int_is_zero(bmap->div[i][0]))
4337 			continue;
4338 		if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_v_div,
4339 					   (b_v_div - d_v_div) + n_div) != -1)
4340 			continue;
4341 		if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_v_div))
4342 			return i;
4343 	}
4344 	return n_div;
4345 }
4346 
4347 /* The correspondence between the variables in the main tableau,
4348  * the context tableau, and the input map and domain is as follows.
4349  * The first n_param and the last n_div variables of the main tableau
4350  * form the variables of the context tableau.
4351  * In the basic map, these n_param variables correspond to the
4352  * parameters and the input dimensions.  In the domain, they correspond
4353  * to the parameters and the set dimensions.
4354  * The n_div variables correspond to the integer divisions in the domain.
4355  * To ensure that everything lines up, we may need to copy some of the
4356  * integer divisions of the domain to the map.  These have to be placed
4357  * in the same order as those in the context and they have to be placed
4358  * after any other integer divisions that the map may have.
4359  * This function performs the required reordering.
4360  */
align_context_divs(__isl_take isl_basic_map * bmap,__isl_keep isl_basic_set * dom)4361 static __isl_give isl_basic_map *align_context_divs(
4362 	__isl_take isl_basic_map *bmap, __isl_keep isl_basic_set *dom)
4363 {
4364 	int i;
4365 	int common = 0;
4366 	int other;
4367 	unsigned bmap_n_div;
4368 
4369 	bmap_n_div = isl_basic_map_dim(bmap, isl_dim_div);
4370 
4371 	for (i = 0; i < dom->n_div; ++i) {
4372 		int pos;
4373 
4374 		pos = find_context_div(bmap, dom, i);
4375 		if (pos < 0)
4376 			return isl_basic_map_free(bmap);
4377 		if (pos < bmap_n_div)
4378 			common++;
4379 	}
4380 	other = bmap_n_div - common;
4381 	if (dom->n_div - common > 0) {
4382 		bmap = isl_basic_map_extend(bmap, dom->n_div - common, 0, 0);
4383 		if (!bmap)
4384 			return NULL;
4385 	}
4386 	for (i = 0; i < dom->n_div; ++i) {
4387 		int pos = find_context_div(bmap, dom, i);
4388 		if (pos < 0)
4389 			bmap = isl_basic_map_free(bmap);
4390 		if (pos >= bmap_n_div) {
4391 			pos = isl_basic_map_alloc_div(bmap);
4392 			if (pos < 0)
4393 				goto error;
4394 			isl_int_set_si(bmap->div[pos][0], 0);
4395 			bmap_n_div++;
4396 		}
4397 		if (pos != other + i)
4398 			bmap = isl_basic_map_swap_div(bmap, pos, other + i);
4399 	}
4400 	return bmap;
4401 error:
4402 	isl_basic_map_free(bmap);
4403 	return NULL;
4404 }
4405 
4406 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4407  * some obvious symmetries.
4408  *
4409  * We make sure the divs in the domain are properly ordered,
4410  * because they will be added one by one in the given order
4411  * during the construction of the solution map.
4412  * Furthermore, make sure that the known integer divisions
4413  * appear before any unknown integer division because the solution
4414  * may depend on the known integer divisions, while anything that
4415  * depends on any variable starting from the first unknown integer
4416  * division is ignored in sol_pma_add.
4417  */
basic_map_partial_lexopt_base_sol(__isl_take isl_basic_map * bmap,__isl_take isl_basic_set * dom,__isl_give isl_set ** empty,int max,struct isl_sol * (* init)(__isl_keep isl_basic_map * bmap,__isl_take isl_basic_set * dom,int track_empty,int max))4418 static struct isl_sol *basic_map_partial_lexopt_base_sol(
4419 	__isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4420 	__isl_give isl_set **empty, int max,
4421 	struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
4422 		    __isl_take isl_basic_set *dom, int track_empty, int max))
4423 {
4424 	struct isl_tab *tab;
4425 	struct isl_sol *sol = NULL;
4426 	struct isl_context *context;
4427 
4428 	if (dom->n_div) {
4429 		dom = isl_basic_set_sort_divs(dom);
4430 		bmap = align_context_divs(bmap, dom);
4431 	}
4432 	sol = init(bmap, dom, !!empty, max);
4433 	if (!sol)
4434 		goto error;
4435 
4436 	context = sol->context;
4437 	if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
4438 		/* nothing */;
4439 	else if (isl_basic_map_plain_is_empty(bmap)) {
4440 		if (sol->add_empty)
4441 			sol->add_empty(sol,
4442 		    isl_basic_set_copy(context->op->peek_basic_set(context)));
4443 	} else {
4444 		tab = tab_for_lexmin(bmap,
4445 				    context->op->peek_basic_set(context), 1, max);
4446 		tab = context->op->detect_nonnegative_parameters(context, tab);
4447 		find_solutions_main(sol, tab);
4448 	}
4449 	if (sol->error)
4450 		goto error;
4451 
4452 	isl_basic_map_free(bmap);
4453 	return sol;
4454 error:
4455 	sol_free(sol);
4456 	isl_basic_map_free(bmap);
4457 	return NULL;
4458 }
4459 
4460 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4461  * some obvious symmetries.
4462  *
4463  * We call basic_map_partial_lexopt_base_sol and extract the results.
4464  */
basic_map_partial_lexopt_base(__isl_take isl_basic_map * bmap,__isl_take isl_basic_set * dom,__isl_give isl_set ** empty,int max)4465 static __isl_give isl_map *basic_map_partial_lexopt_base(
4466 	__isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4467 	__isl_give isl_set **empty, int max)
4468 {
4469 	isl_map *result = NULL;
4470 	struct isl_sol *sol;
4471 	struct isl_sol_map *sol_map;
4472 
4473 	sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
4474 						&sol_map_init);
4475 	if (!sol)
4476 		return NULL;
4477 	sol_map = (struct isl_sol_map *) sol;
4478 
4479 	result = isl_map_copy(sol_map->map);
4480 	if (empty)
4481 		*empty = isl_set_copy(sol_map->empty);
4482 	sol_free(&sol_map->sol);
4483 	return result;
4484 }
4485 
4486 /* Return a count of the number of occurrences of the "n" first
4487  * variables in the inequality constraints of "bmap".
4488  */
count_occurrences(__isl_keep isl_basic_map * bmap,int n)4489 static __isl_give int *count_occurrences(__isl_keep isl_basic_map *bmap,
4490 	int n)
4491 {
4492 	int i, j;
4493 	isl_ctx *ctx;
4494 	int *occurrences;
4495 
4496 	if (!bmap)
4497 		return NULL;
4498 	ctx = isl_basic_map_get_ctx(bmap);
4499 	occurrences = isl_calloc_array(ctx, int, n);
4500 	if (!occurrences)
4501 		return NULL;
4502 
4503 	for (i = 0; i < bmap->n_ineq; ++i) {
4504 		for (j = 0; j < n; ++j) {
4505 			if (!isl_int_is_zero(bmap->ineq[i][1 + j]))
4506 				occurrences[j]++;
4507 		}
4508 	}
4509 
4510 	return occurrences;
4511 }
4512 
4513 /* Do all of the "n" variables with non-zero coefficients in "c"
4514  * occur in exactly a single constraint.
4515  * "occurrences" is an array of length "n" containing the number
4516  * of occurrences of each of the variables in the inequality constraints.
4517  */
single_occurrence(int n,isl_int * c,int * occurrences)4518 static int single_occurrence(int n, isl_int *c, int *occurrences)
4519 {
4520 	int i;
4521 
4522 	for (i = 0; i < n; ++i) {
4523 		if (isl_int_is_zero(c[i]))
4524 			continue;
4525 		if (occurrences[i] != 1)
4526 			return 0;
4527 	}
4528 
4529 	return 1;
4530 }
4531 
4532 /* Do all of the "n" initial variables that occur in inequality constraint
4533  * "ineq" of "bmap" only occur in that constraint?
4534  */
all_single_occurrence(__isl_keep isl_basic_map * bmap,int ineq,int n)4535 static int all_single_occurrence(__isl_keep isl_basic_map *bmap, int ineq,
4536 	int n)
4537 {
4538 	int i, j;
4539 
4540 	for (i = 0; i < n; ++i) {
4541 		if (isl_int_is_zero(bmap->ineq[ineq][1 + i]))
4542 			continue;
4543 		for (j = 0; j < bmap->n_ineq; ++j) {
4544 			if (j == ineq)
4545 				continue;
4546 			if (!isl_int_is_zero(bmap->ineq[j][1 + i]))
4547 				return 0;
4548 		}
4549 	}
4550 
4551 	return 1;
4552 }
4553 
4554 /* Structure used during detection of parallel constraints.
4555  * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4556  * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4557  * val: the coefficients of the output variables
4558  */
4559 struct isl_constraint_equal_info {
4560 	unsigned n_in;
4561 	unsigned n_out;
4562 	isl_int *val;
4563 };
4564 
4565 /* Check whether the coefficients of the output variables
4566  * of the constraint in "entry" are equal to info->val.
4567  */
constraint_equal(const void * entry,const void * val)4568 static isl_bool constraint_equal(const void *entry, const void *val)
4569 {
4570 	isl_int **row = (isl_int **)entry;
4571 	const struct isl_constraint_equal_info *info = val;
4572 	int eq;
4573 
4574 	eq = isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4575 	return isl_bool_ok(eq);
4576 }
4577 
4578 /* Check whether "bmap" has a pair of constraints that have
4579  * the same coefficients for the output variables.
4580  * Note that the coefficients of the existentially quantified
4581  * variables need to be zero since the existentially quantified
4582  * of the result are usually not the same as those of the input.
4583  * Furthermore, check that each of the input variables that occur
4584  * in those constraints does not occur in any other constraint.
4585  * If so, return true and return the row indices of the two constraints
4586  * in *first and *second.
4587  */
parallel_constraints(__isl_keep isl_basic_map * bmap,int * first,int * second)4588 static isl_bool parallel_constraints(__isl_keep isl_basic_map *bmap,
4589 	int *first, int *second)
4590 {
4591 	int i;
4592 	isl_ctx *ctx;
4593 	int *occurrences = NULL;
4594 	struct isl_hash_table *table = NULL;
4595 	struct isl_hash_table_entry *entry;
4596 	struct isl_constraint_equal_info info;
4597 	isl_size nparam, n_in, n_out, n_div;
4598 
4599 	ctx = isl_basic_map_get_ctx(bmap);
4600 	table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4601 	if (!table)
4602 		goto error;
4603 
4604 	nparam = isl_basic_map_dim(bmap, isl_dim_param);
4605 	n_in = isl_basic_map_dim(bmap, isl_dim_in);
4606 	n_out = isl_basic_map_dim(bmap, isl_dim_out);
4607 	n_div = isl_basic_map_dim(bmap, isl_dim_div);
4608 	if (nparam < 0 || n_in < 0 || n_out < 0 || n_div < 0)
4609 		goto error;
4610 	info.n_in = nparam + n_in;
4611 	occurrences = count_occurrences(bmap, info.n_in);
4612 	if (info.n_in && !occurrences)
4613 		goto error;
4614 	info.n_out = n_out + n_div;
4615 	for (i = 0; i < bmap->n_ineq; ++i) {
4616 		uint32_t hash;
4617 
4618 		info.val = bmap->ineq[i] + 1 + info.n_in;
4619 		if (isl_seq_first_non_zero(info.val, n_out) < 0)
4620 			continue;
4621 		if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4622 			continue;
4623 		if (!single_occurrence(info.n_in, bmap->ineq[i] + 1,
4624 					occurrences))
4625 			continue;
4626 		hash = isl_seq_get_hash(info.val, info.n_out);
4627 		entry = isl_hash_table_find(ctx, table, hash,
4628 					    constraint_equal, &info, 1);
4629 		if (!entry)
4630 			goto error;
4631 		if (entry->data)
4632 			break;
4633 		entry->data = &bmap->ineq[i];
4634 	}
4635 
4636 	if (i < bmap->n_ineq) {
4637 		*first = ((isl_int **)entry->data) - bmap->ineq;
4638 		*second = i;
4639 	}
4640 
4641 	isl_hash_table_free(ctx, table);
4642 	free(occurrences);
4643 
4644 	return isl_bool_ok(i < bmap->n_ineq);
4645 error:
4646 	isl_hash_table_free(ctx, table);
4647 	free(occurrences);
4648 	return isl_bool_error;
4649 }
4650 
4651 /* Given a set of upper bounds in "var", add constraints to "bset"
4652  * that make the i-th bound smallest.
4653  *
4654  * In particular, if there are n bounds b_i, then add the constraints
4655  *
4656  *	b_i <= b_j	for j > i
4657  *	b_i <  b_j	for j < i
4658  */
select_minimum(__isl_take isl_basic_set * bset,__isl_keep isl_mat * var,int i)4659 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4660 	__isl_keep isl_mat *var, int i)
4661 {
4662 	isl_ctx *ctx;
4663 	int j, k;
4664 
4665 	ctx = isl_mat_get_ctx(var);
4666 
4667 	for (j = 0; j < var->n_row; ++j) {
4668 		if (j == i)
4669 			continue;
4670 		k = isl_basic_set_alloc_inequality(bset);
4671 		if (k < 0)
4672 			goto error;
4673 		isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4674 				ctx->negone, var->row[i], var->n_col);
4675 		isl_int_set_si(bset->ineq[k][var->n_col], 0);
4676 		if (j < i)
4677 			isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4678 	}
4679 
4680 	bset = isl_basic_set_finalize(bset);
4681 
4682 	return bset;
4683 error:
4684 	isl_basic_set_free(bset);
4685 	return NULL;
4686 }
4687 
4688 /* Given a set of upper bounds on the last "input" variable m,
4689  * construct a set that assigns the minimal upper bound to m, i.e.,
4690  * construct a set that divides the space into cells where one
4691  * of the upper bounds is smaller than all the others and assign
4692  * this upper bound to m.
4693  *
4694  * In particular, if there are n bounds b_i, then the result
4695  * consists of n basic sets, each one of the form
4696  *
4697  *	m = b_i
4698  *	b_i <= b_j	for j > i
4699  *	b_i <  b_j	for j < i
4700  */
set_minimum(__isl_take isl_space * space,__isl_take isl_mat * var)4701 static __isl_give isl_set *set_minimum(__isl_take isl_space *space,
4702 	__isl_take isl_mat *var)
4703 {
4704 	int i, k;
4705 	isl_basic_set *bset = NULL;
4706 	isl_set *set = NULL;
4707 
4708 	if (!space || !var)
4709 		goto error;
4710 
4711 	set = isl_set_alloc_space(isl_space_copy(space),
4712 				var->n_row, ISL_SET_DISJOINT);
4713 
4714 	for (i = 0; i < var->n_row; ++i) {
4715 		bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
4716 					       1, var->n_row - 1);
4717 		k = isl_basic_set_alloc_equality(bset);
4718 		if (k < 0)
4719 			goto error;
4720 		isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4721 		isl_int_set_si(bset->eq[k][var->n_col], -1);
4722 		bset = select_minimum(bset, var, i);
4723 		set = isl_set_add_basic_set(set, bset);
4724 	}
4725 
4726 	isl_space_free(space);
4727 	isl_mat_free(var);
4728 	return set;
4729 error:
4730 	isl_basic_set_free(bset);
4731 	isl_set_free(set);
4732 	isl_space_free(space);
4733 	isl_mat_free(var);
4734 	return NULL;
4735 }
4736 
4737 /* Given that the last input variable of "bmap" represents the minimum
4738  * of the bounds in "cst", check whether we need to split the domain
4739  * based on which bound attains the minimum.
4740  *
4741  * A split is needed when the minimum appears in an integer division
4742  * or in an equality.  Otherwise, it is only needed if it appears in
4743  * an upper bound that is different from the upper bounds on which it
4744  * is defined.
4745  */
need_split_basic_map(__isl_keep isl_basic_map * bmap,__isl_keep isl_mat * cst)4746 static isl_bool need_split_basic_map(__isl_keep isl_basic_map *bmap,
4747 	__isl_keep isl_mat *cst)
4748 {
4749 	int i, j;
4750 	isl_size total;
4751 	unsigned pos;
4752 
4753 	pos = cst->n_col - 1;
4754 	total = isl_basic_map_dim(bmap, isl_dim_all);
4755 	if (total < 0)
4756 		return isl_bool_error;
4757 
4758 	for (i = 0; i < bmap->n_div; ++i)
4759 		if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4760 			return isl_bool_true;
4761 
4762 	for (i = 0; i < bmap->n_eq; ++i)
4763 		if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4764 			return isl_bool_true;
4765 
4766 	for (i = 0; i < bmap->n_ineq; ++i) {
4767 		if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4768 			continue;
4769 		if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4770 			return isl_bool_true;
4771 		if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4772 					   total - pos - 1) >= 0)
4773 			return isl_bool_true;
4774 
4775 		for (j = 0; j < cst->n_row; ++j)
4776 			if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4777 				break;
4778 		if (j >= cst->n_row)
4779 			return isl_bool_true;
4780 	}
4781 
4782 	return isl_bool_false;
4783 }
4784 
4785 /* Given that the last set variable of "bset" represents the minimum
4786  * of the bounds in "cst", check whether we need to split the domain
4787  * based on which bound attains the minimum.
4788  *
4789  * We simply call need_split_basic_map here.  This is safe because
4790  * the position of the minimum is computed from "cst" and not
4791  * from "bmap".
4792  */
need_split_basic_set(__isl_keep isl_basic_set * bset,__isl_keep isl_mat * cst)4793 static isl_bool need_split_basic_set(__isl_keep isl_basic_set *bset,
4794 	__isl_keep isl_mat *cst)
4795 {
4796 	return need_split_basic_map(bset_to_bmap(bset), cst);
4797 }
4798 
4799 /* Given that the last set variable of "set" represents the minimum
4800  * of the bounds in "cst", check whether we need to split the domain
4801  * based on which bound attains the minimum.
4802  */
need_split_set(__isl_keep isl_set * set,__isl_keep isl_mat * cst)4803 static isl_bool need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4804 {
4805 	int i;
4806 
4807 	for (i = 0; i < set->n; ++i) {
4808 		isl_bool split;
4809 
4810 		split = need_split_basic_set(set->p[i], cst);
4811 		if (split < 0 || split)
4812 			return split;
4813 	}
4814 
4815 	return isl_bool_false;
4816 }
4817 
4818 /* Given a map of which the last input variable is the minimum
4819  * of the bounds in "cst", split each basic set in the set
4820  * in pieces where one of the bounds is (strictly) smaller than the others.
4821  * This subdivision is given in "min_expr".
4822  * The variable is subsequently projected out.
4823  *
4824  * We only do the split when it is needed.
4825  * For example if the last input variable m = min(a,b) and the only
4826  * constraints in the given basic set are lower bounds on m,
4827  * i.e., l <= m = min(a,b), then we can simply project out m
4828  * to obtain l <= a and l <= b, without having to split on whether
4829  * m is equal to a or b.
4830  */
split_domain(__isl_take isl_map * opt,__isl_take isl_set * min_expr,__isl_take isl_mat * cst)4831 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4832 	__isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4833 {
4834 	isl_size n_in;
4835 	int i;
4836 	isl_space *space;
4837 	isl_map *res;
4838 
4839 	n_in = isl_map_dim(opt, isl_dim_in);
4840 	if (n_in < 0 || !min_expr || !cst)
4841 		goto error;
4842 
4843 	space = isl_map_get_space(opt);
4844 	space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
4845 	res = isl_map_empty(space);
4846 
4847 	for (i = 0; i < opt->n; ++i) {
4848 		isl_map *map;
4849 		isl_bool split;
4850 
4851 		map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4852 		split = need_split_basic_map(opt->p[i], cst);
4853 		if (split < 0)
4854 			map = isl_map_free(map);
4855 		else if (split)
4856 			map = isl_map_intersect_domain(map,
4857 						       isl_set_copy(min_expr));
4858 		map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4859 
4860 		res = isl_map_union_disjoint(res, map);
4861 	}
4862 
4863 	isl_map_free(opt);
4864 	isl_set_free(min_expr);
4865 	isl_mat_free(cst);
4866 	return res;
4867 error:
4868 	isl_map_free(opt);
4869 	isl_set_free(min_expr);
4870 	isl_mat_free(cst);
4871 	return NULL;
4872 }
4873 
4874 /* Given a set of which the last set variable is the minimum
4875  * of the bounds in "cst", split each basic set in the set
4876  * in pieces where one of the bounds is (strictly) smaller than the others.
4877  * This subdivision is given in "min_expr".
4878  * The variable is subsequently projected out.
4879  */
split(__isl_take isl_set * empty,__isl_take isl_set * min_expr,__isl_take isl_mat * cst)4880 static __isl_give isl_set *split(__isl_take isl_set *empty,
4881 	__isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4882 {
4883 	isl_map *map;
4884 
4885 	map = isl_map_from_domain(empty);
4886 	map = split_domain(map, min_expr, cst);
4887 	empty = isl_map_domain(map);
4888 
4889 	return empty;
4890 }
4891 
4892 static __isl_give isl_map *basic_map_partial_lexopt(
4893 	__isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4894 	__isl_give isl_set **empty, int max);
4895 
4896 /* This function is called from basic_map_partial_lexopt_symm.
4897  * The last variable of "bmap" and "dom" corresponds to the minimum
4898  * of the bounds in "cst".  "map_space" is the space of the original
4899  * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4900  * is the space of the original domain.
4901  *
4902  * We recursively call basic_map_partial_lexopt and then plug in
4903  * the definition of the minimum in the result.
4904  */
basic_map_partial_lexopt_symm_core(__isl_take isl_basic_map * bmap,__isl_take isl_basic_set * dom,__isl_give isl_set ** empty,int max,__isl_take isl_mat * cst,__isl_take isl_space * map_space,__isl_take isl_space * set_space)4905 static __isl_give isl_map *basic_map_partial_lexopt_symm_core(
4906 	__isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4907 	__isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4908 	__isl_take isl_space *map_space, __isl_take isl_space *set_space)
4909 {
4910 	isl_map *opt;
4911 	isl_set *min_expr;
4912 
4913 	min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4914 
4915 	opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4916 
4917 	if (empty) {
4918 		*empty = split(*empty,
4919 			       isl_set_copy(min_expr), isl_mat_copy(cst));
4920 		*empty = isl_set_reset_space(*empty, set_space);
4921 	}
4922 
4923 	opt = split_domain(opt, min_expr, cst);
4924 	opt = isl_map_reset_space(opt, map_space);
4925 
4926 	return opt;
4927 }
4928 
4929 /* Extract a domain from "bmap" for the purpose of computing
4930  * a lexicographic optimum.
4931  *
4932  * This function is only called when the caller wants to compute a full
4933  * lexicographic optimum, i.e., without specifying a domain.  In this case,
4934  * the caller is not interested in the part of the domain space where
4935  * there is no solution and the domain can be initialized to those constraints
4936  * of "bmap" that only involve the parameters and the input dimensions.
4937  * This relieves the parametric programming engine from detecting those
4938  * inequalities and transferring them to the context.  More importantly,
4939  * it ensures that those inequalities are transferred first and not
4940  * intermixed with inequalities that actually split the domain.
4941  *
4942  * If the caller does not require the absence of existentially quantified
4943  * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4944  * then the actual domain of "bmap" can be used.  This ensures that
4945  * the domain does not need to be split at all just to separate out
4946  * pieces of the domain that do not have a solution from piece that do.
4947  * This domain cannot be used in general because it may involve
4948  * (unknown) existentially quantified variables which will then also
4949  * appear in the solution.
4950  */
extract_domain(__isl_keep isl_basic_map * bmap,unsigned flags)4951 static __isl_give isl_basic_set *extract_domain(__isl_keep isl_basic_map *bmap,
4952 	unsigned flags)
4953 {
4954 	isl_size n_div;
4955 	isl_size n_out;
4956 
4957 	n_div = isl_basic_map_dim(bmap, isl_dim_div);
4958 	n_out = isl_basic_map_dim(bmap, isl_dim_out);
4959 	if (n_div < 0 || n_out < 0)
4960 		return NULL;
4961 	bmap = isl_basic_map_copy(bmap);
4962 	if (ISL_FL_ISSET(flags, ISL_OPT_QE)) {
4963 		bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4964 							isl_dim_div, 0, n_div);
4965 		bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
4966 							isl_dim_out, 0, n_out);
4967 	}
4968 	return isl_basic_map_domain(bmap);
4969 }
4970 
4971 #undef TYPE
4972 #define TYPE	isl_map
4973 #undef SUFFIX
4974 #define SUFFIX
4975 #include "isl_tab_lexopt_templ.c"
4976 
4977 /* Extract the subsequence of the sample value of "tab"
4978  * starting at "pos" and of length "len".
4979  */
extract_sample_sequence(struct isl_tab * tab,int pos,int len)4980 static __isl_give isl_vec *extract_sample_sequence(struct isl_tab *tab,
4981 	int pos, int len)
4982 {
4983 	int i;
4984 	isl_ctx *ctx;
4985 	isl_vec *v;
4986 
4987 	ctx = isl_tab_get_ctx(tab);
4988 	v = isl_vec_alloc(ctx, len);
4989 	if (!v)
4990 		return NULL;
4991 	for (i = 0; i < len; ++i) {
4992 		if (!tab->var[pos + i].is_row) {
4993 			isl_int_set_si(v->el[i], 0);
4994 		} else {
4995 			int row;
4996 
4997 			row = tab->var[pos + i].index;
4998 			isl_int_divexact(v->el[i], tab->mat->row[row][1],
4999 					tab->mat->row[row][0]);
5000 		}
5001 	}
5002 
5003 	return v;
5004 }
5005 
5006 /* Check if the sequence of variables starting at "pos"
5007  * represents a trivial solution according to "trivial".
5008  * That is, is the result of applying "trivial" to this sequence
5009  * equal to the zero vector?
5010  */
region_is_trivial(struct isl_tab * tab,int pos,__isl_keep isl_mat * trivial)5011 static isl_bool region_is_trivial(struct isl_tab *tab, int pos,
5012 	__isl_keep isl_mat *trivial)
5013 {
5014 	isl_size n, len;
5015 	isl_vec *v;
5016 	isl_bool is_trivial;
5017 
5018 	n = isl_mat_rows(trivial);
5019 	if (n < 0)
5020 		return isl_bool_error;
5021 
5022 	if (n == 0)
5023 		return isl_bool_false;
5024 
5025 	len = isl_mat_cols(trivial);
5026 	if (len < 0)
5027 		return isl_bool_error;
5028 	v = extract_sample_sequence(tab, pos, len);
5029 	v = isl_mat_vec_product(isl_mat_copy(trivial), v);
5030 	is_trivial = isl_vec_is_zero(v);
5031 	isl_vec_free(v);
5032 
5033 	return is_trivial;
5034 }
5035 
5036 /* Global internal data for isl_tab_basic_set_non_trivial_lexmin.
5037  *
5038  * "n_op" is the number of initial coordinates to optimize,
5039  * as passed to isl_tab_basic_set_non_trivial_lexmin.
5040  * "region" is the "n_region"-sized array of regions passed
5041  * to isl_tab_basic_set_non_trivial_lexmin.
5042  *
5043  * "tab" is the tableau that corresponds to the ILP problem.
5044  * "local" is an array of local data structure, one for each
5045  * (potential) level of the backtracking procedure of
5046  * isl_tab_basic_set_non_trivial_lexmin.
5047  * "v" is a pre-allocated vector that can be used for adding
5048  * constraints to the tableau.
5049  *
5050  * "sol" contains the best solution found so far.
5051  * It is initialized to a vector of size zero.
5052  */
5053 struct isl_lexmin_data {
5054 	int n_op;
5055 	int n_region;
5056 	struct isl_trivial_region *region;
5057 
5058 	struct isl_tab *tab;
5059 	struct isl_local_region *local;
5060 	isl_vec *v;
5061 
5062 	isl_vec *sol;
5063 };
5064 
5065 /* Return the index of the first trivial region, "n_region" if all regions
5066  * are non-trivial or -1 in case of error.
5067  */
first_trivial_region(struct isl_lexmin_data * data)5068 static int first_trivial_region(struct isl_lexmin_data *data)
5069 {
5070 	int i;
5071 
5072 	for (i = 0; i < data->n_region; ++i) {
5073 		isl_bool trivial;
5074 		trivial = region_is_trivial(data->tab, data->region[i].pos,
5075 					data->region[i].trivial);
5076 		if (trivial < 0)
5077 			return -1;
5078 		if (trivial)
5079 			return i;
5080 	}
5081 
5082 	return data->n_region;
5083 }
5084 
5085 /* Check if the solution is optimal, i.e., whether the first
5086  * n_op entries are zero.
5087  */
is_optimal(__isl_keep isl_vec * sol,int n_op)5088 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
5089 {
5090 	int i;
5091 
5092 	for (i = 0; i < n_op; ++i)
5093 		if (!isl_int_is_zero(sol->el[1 + i]))
5094 			return 0;
5095 	return 1;
5096 }
5097 
5098 /* Add constraints to "tab" that ensure that any solution is significantly
5099  * better than that represented by "sol".  That is, find the first
5100  * relevant (within first n_op) non-zero coefficient and force it (along
5101  * with all previous coefficients) to be zero.
5102  * If the solution is already optimal (all relevant coefficients are zero),
5103  * then just mark the table as empty.
5104  * "n_zero" is the number of coefficients that have been forced zero
5105  * by previous calls to this function at the same level.
5106  * Return the updated number of forced zero coefficients or -1 on error.
5107  *
5108  * This function assumes that at least 2 * (n_op - n_zero) more rows and
5109  * at least 2 * (n_op - n_zero) more elements in the constraint array
5110  * are available in the tableau.
5111  */
force_better_solution(struct isl_tab * tab,__isl_keep isl_vec * sol,int n_op,int n_zero)5112 static int force_better_solution(struct isl_tab *tab,
5113 	__isl_keep isl_vec *sol, int n_op, int n_zero)
5114 {
5115 	int i, n;
5116 	isl_ctx *ctx;
5117 	isl_vec *v = NULL;
5118 
5119 	if (!sol)
5120 		return -1;
5121 
5122 	for (i = n_zero; i < n_op; ++i)
5123 		if (!isl_int_is_zero(sol->el[1 + i]))
5124 			break;
5125 
5126 	if (i == n_op) {
5127 		if (isl_tab_mark_empty(tab) < 0)
5128 			return -1;
5129 		return n_op;
5130 	}
5131 
5132 	ctx = isl_vec_get_ctx(sol);
5133 	v = isl_vec_alloc(ctx, 1 + tab->n_var);
5134 	if (!v)
5135 		return -1;
5136 
5137 	n = i + 1;
5138 	for (; i >= n_zero; --i) {
5139 		v = isl_vec_clr(v);
5140 		isl_int_set_si(v->el[1 + i], -1);
5141 		if (add_lexmin_eq(tab, v->el) < 0)
5142 			goto error;
5143 	}
5144 
5145 	isl_vec_free(v);
5146 	return n;
5147 error:
5148 	isl_vec_free(v);
5149 	return -1;
5150 }
5151 
5152 /* Fix triviality direction "dir" of the given region to zero.
5153  *
5154  * This function assumes that at least two more rows and at least
5155  * two more elements in the constraint array are available in the tableau.
5156  */
fix_zero(struct isl_tab * tab,struct isl_trivial_region * region,int dir,struct isl_lexmin_data * data)5157 static isl_stat fix_zero(struct isl_tab *tab, struct isl_trivial_region *region,
5158 	int dir, struct isl_lexmin_data *data)
5159 {
5160 	isl_size len;
5161 
5162 	data->v = isl_vec_clr(data->v);
5163 	if (!data->v)
5164 		return isl_stat_error;
5165 	len = isl_mat_cols(region->trivial);
5166 	if (len < 0)
5167 		return isl_stat_error;
5168 	isl_seq_cpy(data->v->el + 1 + region->pos, region->trivial->row[dir],
5169 		    len);
5170 	if (add_lexmin_eq(tab, data->v->el) < 0)
5171 		return isl_stat_error;
5172 
5173 	return isl_stat_ok;
5174 }
5175 
5176 /* This function selects case "side" for non-triviality region "region",
5177  * assuming all the equality constraints have been imposed already.
5178  * In particular, the triviality direction side/2 is made positive
5179  * if side is even and made negative if side is odd.
5180  *
5181  * This function assumes that at least one more row and at least
5182  * one more element in the constraint array are available in the tableau.
5183  */
pos_neg(struct isl_tab * tab,struct isl_trivial_region * region,int side,struct isl_lexmin_data * data)5184 static struct isl_tab *pos_neg(struct isl_tab *tab,
5185 	struct isl_trivial_region *region,
5186 	int side, struct isl_lexmin_data *data)
5187 {
5188 	isl_size len;
5189 
5190 	data->v = isl_vec_clr(data->v);
5191 	if (!data->v)
5192 		goto error;
5193 	isl_int_set_si(data->v->el[0], -1);
5194 	len = isl_mat_cols(region->trivial);
5195 	if (len < 0)
5196 		goto error;
5197 	if (side % 2 == 0)
5198 		isl_seq_cpy(data->v->el + 1 + region->pos,
5199 			    region->trivial->row[side / 2], len);
5200 	else
5201 		isl_seq_neg(data->v->el + 1 + region->pos,
5202 			    region->trivial->row[side / 2], len);
5203 	return add_lexmin_ineq(tab, data->v->el);
5204 error:
5205 	isl_tab_free(tab);
5206 	return NULL;
5207 }
5208 
5209 /* Local data at each level of the backtracking procedure of
5210  * isl_tab_basic_set_non_trivial_lexmin.
5211  *
5212  * "update" is set if a solution has been found in the current case
5213  * of this level, such that a better solution needs to be enforced
5214  * in the next case.
5215  * "n_zero" is the number of initial coordinates that have already
5216  * been forced to be zero at this level.
5217  * "region" is the non-triviality region considered at this level.
5218  * "side" is the index of the current case at this level.
5219  * "n" is the number of triviality directions.
5220  * "snap" is a snapshot of the tableau holding a state that needs
5221  * to be satisfied by all subsequent cases.
5222  */
5223 struct isl_local_region {
5224 	int update;
5225 	int n_zero;
5226 	int region;
5227 	int side;
5228 	int n;
5229 	struct isl_tab_undo *snap;
5230 };
5231 
5232 /* Initialize the global data structure "data" used while solving
5233  * the ILP problem "bset".
5234  */
init_lexmin_data(struct isl_lexmin_data * data,__isl_keep isl_basic_set * bset)5235 static isl_stat init_lexmin_data(struct isl_lexmin_data *data,
5236 	__isl_keep isl_basic_set *bset)
5237 {
5238 	isl_ctx *ctx;
5239 
5240 	ctx = isl_basic_set_get_ctx(bset);
5241 
5242 	data->tab = tab_for_lexmin(bset, NULL, 0, 0);
5243 	if (!data->tab)
5244 		return isl_stat_error;
5245 
5246 	data->v = isl_vec_alloc(ctx, 1 + data->tab->n_var);
5247 	if (!data->v)
5248 		return isl_stat_error;
5249 	data->local = isl_calloc_array(ctx, struct isl_local_region,
5250 					data->n_region);
5251 	if (data->n_region && !data->local)
5252 		return isl_stat_error;
5253 
5254 	data->sol = isl_vec_alloc(ctx, 0);
5255 
5256 	return isl_stat_ok;
5257 }
5258 
5259 /* Mark all outer levels as requiring a better solution
5260  * in the next cases.
5261  */
update_outer_levels(struct isl_lexmin_data * data,int level)5262 static void update_outer_levels(struct isl_lexmin_data *data, int level)
5263 {
5264 	int i;
5265 
5266 	for (i = 0; i < level; ++i)
5267 		data->local[i].update = 1;
5268 }
5269 
5270 /* Initialize "local" to refer to region "region" and
5271  * to initiate processing at this level.
5272  */
init_local_region(struct isl_local_region * local,int region,struct isl_lexmin_data * data)5273 static isl_stat init_local_region(struct isl_local_region *local, int region,
5274 	struct isl_lexmin_data *data)
5275 {
5276 	isl_size n = isl_mat_rows(data->region[region].trivial);
5277 
5278 	if (n < 0)
5279 		return isl_stat_error;
5280 	local->n = n;
5281 	local->region = region;
5282 	local->side = 0;
5283 	local->update = 0;
5284 	local->n_zero = 0;
5285 
5286 	return isl_stat_ok;
5287 }
5288 
5289 /* What to do next after entering a level of the backtracking procedure.
5290  *
5291  * error: some error has occurred; abort
5292  * done: an optimal solution has been found; stop search
5293  * backtrack: backtrack to the previous level
5294  * handle: add the constraints for the current level and
5295  * 	move to the next level
5296  */
5297 enum isl_next {
5298 	isl_next_error = -1,
5299 	isl_next_done,
5300 	isl_next_backtrack,
5301 	isl_next_handle,
5302 };
5303 
5304 /* Have all cases of the current region been considered?
5305  * If there are n directions, then there are 2n cases.
5306  *
5307  * The constraints in the current tableau are imposed
5308  * in all subsequent cases.  This means that if the current
5309  * tableau is empty, then none of those cases should be considered
5310  * anymore and all cases have effectively been considered.
5311  */
finished_all_cases(struct isl_local_region * local,struct isl_lexmin_data * data)5312 static int finished_all_cases(struct isl_local_region *local,
5313 	struct isl_lexmin_data *data)
5314 {
5315 	if (data->tab->empty)
5316 		return 1;
5317 	return local->side >= 2 * local->n;
5318 }
5319 
5320 /* Enter level "level" of the backtracking search and figure out
5321  * what to do next.  "init" is set if the level was entered
5322  * from a higher level and needs to be initialized.
5323  * Otherwise, the level is entered as a result of backtracking and
5324  * the tableau needs to be restored to a position that can
5325  * be used for the next case at this level.
5326  * The snapshot is assumed to have been saved in the previous case,
5327  * before the constraints specific to that case were added.
5328  *
5329  * In the initialization case, the local region is initialized
5330  * to point to the first violated region.
5331  * If the constraints of all regions are satisfied by the current
5332  * sample of the tableau, then tell the caller to continue looking
5333  * for a better solution or to stop searching if an optimal solution
5334  * has been found.
5335  *
5336  * If the tableau is empty or if all cases at the current level
5337  * have been considered, then the caller needs to backtrack as well.
5338  */
enter_level(int level,int init,struct isl_lexmin_data * data)5339 static enum isl_next enter_level(int level, int init,
5340 	struct isl_lexmin_data *data)
5341 {
5342 	struct isl_local_region *local = &data->local[level];
5343 
5344 	if (init) {
5345 		int r;
5346 
5347 		data->tab = cut_to_integer_lexmin(data->tab, CUT_ONE);
5348 		if (!data->tab)
5349 			return isl_next_error;
5350 		if (data->tab->empty)
5351 			return isl_next_backtrack;
5352 		r = first_trivial_region(data);
5353 		if (r < 0)
5354 			return isl_next_error;
5355 		if (r == data->n_region) {
5356 			update_outer_levels(data, level);
5357 			isl_vec_free(data->sol);
5358 			data->sol = isl_tab_get_sample_value(data->tab);
5359 			if (!data->sol)
5360 				return isl_next_error;
5361 			if (is_optimal(data->sol, data->n_op))
5362 				return isl_next_done;
5363 			return isl_next_backtrack;
5364 		}
5365 		if (level >= data->n_region)
5366 			isl_die(isl_vec_get_ctx(data->v), isl_error_internal,
5367 				"nesting level too deep",
5368 				return isl_next_error);
5369 		if (init_local_region(local, r, data) < 0)
5370 			return isl_next_error;
5371 		if (isl_tab_extend_cons(data->tab,
5372 				    2 * local->n + 2 * data->n_op) < 0)
5373 			return isl_next_error;
5374 	} else {
5375 		if (isl_tab_rollback(data->tab, local->snap) < 0)
5376 			return isl_next_error;
5377 	}
5378 
5379 	if (finished_all_cases(local, data))
5380 		return isl_next_backtrack;
5381 	return isl_next_handle;
5382 }
5383 
5384 /* If a solution has been found in the previous case at this level
5385  * (marked by local->update being set), then add constraints
5386  * that enforce a better solution in the present and all following cases.
5387  * The constraints only need to be imposed once because they are
5388  * included in the snapshot (taken in pick_side) that will be used in
5389  * subsequent cases.
5390  */
better_next_side(struct isl_local_region * local,struct isl_lexmin_data * data)5391 static isl_stat better_next_side(struct isl_local_region *local,
5392 	struct isl_lexmin_data *data)
5393 {
5394 	if (!local->update)
5395 		return isl_stat_ok;
5396 
5397 	local->n_zero = force_better_solution(data->tab,
5398 				data->sol, data->n_op, local->n_zero);
5399 	if (local->n_zero < 0)
5400 		return isl_stat_error;
5401 
5402 	local->update = 0;
5403 
5404 	return isl_stat_ok;
5405 }
5406 
5407 /* Add constraints to data->tab that select the current case (local->side)
5408  * at the current level.
5409  *
5410  * If the linear combinations v should not be zero, then the cases are
5411  *	v_0 >= 1
5412  *	v_0 <= -1
5413  *	v_0 = 0 and v_1 >= 1
5414  *	v_0 = 0 and v_1 <= -1
5415  *	v_0 = 0 and v_1 = 0 and v_2 >= 1
5416  *	v_0 = 0 and v_1 = 0 and v_2 <= -1
5417  *	...
5418  * in this order.
5419  *
5420  * A snapshot is taken after the equality constraint (if any) has been added
5421  * such that the next case can start off from this position.
5422  * The rollback to this position is performed in enter_level.
5423  */
pick_side(struct isl_local_region * local,struct isl_lexmin_data * data)5424 static isl_stat pick_side(struct isl_local_region *local,
5425 	struct isl_lexmin_data *data)
5426 {
5427 	struct isl_trivial_region *region;
5428 	int side, base;
5429 
5430 	region = &data->region[local->region];
5431 	side = local->side;
5432 	base = 2 * (side/2);
5433 
5434 	if (side == base && base >= 2 &&
5435 	    fix_zero(data->tab, region, base / 2 - 1, data) < 0)
5436 		return isl_stat_error;
5437 
5438 	local->snap = isl_tab_snap(data->tab);
5439 	if (isl_tab_push_basis(data->tab) < 0)
5440 		return isl_stat_error;
5441 
5442 	data->tab = pos_neg(data->tab, region, side, data);
5443 	if (!data->tab)
5444 		return isl_stat_error;
5445 	return isl_stat_ok;
5446 }
5447 
5448 /* Free the memory associated to "data".
5449  */
clear_lexmin_data(struct isl_lexmin_data * data)5450 static void clear_lexmin_data(struct isl_lexmin_data *data)
5451 {
5452 	free(data->local);
5453 	isl_vec_free(data->v);
5454 	isl_tab_free(data->tab);
5455 }
5456 
5457 /* Return the lexicographically smallest non-trivial solution of the
5458  * given ILP problem.
5459  *
5460  * All variables are assumed to be non-negative.
5461  *
5462  * n_op is the number of initial coordinates to optimize.
5463  * That is, once a solution has been found, we will only continue looking
5464  * for solutions that result in significantly better values for those
5465  * initial coordinates.  That is, we only continue looking for solutions
5466  * that increase the number of initial zeros in this sequence.
5467  *
5468  * A solution is non-trivial, if it is non-trivial on each of the
5469  * specified regions.  Each region represents a sequence of
5470  * triviality directions on a sequence of variables that starts
5471  * at a given position.  A solution is non-trivial on such a region if
5472  * at least one of the triviality directions is non-zero
5473  * on that sequence of variables.
5474  *
5475  * Whenever a conflict is encountered, all constraints involved are
5476  * reported to the caller through a call to "conflict".
5477  *
5478  * We perform a simple branch-and-bound backtracking search.
5479  * Each level in the search represents an initially trivial region
5480  * that is forced to be non-trivial.
5481  * At each level we consider 2 * n cases, where n
5482  * is the number of triviality directions.
5483  * In terms of those n directions v_i, we consider the cases
5484  *	v_0 >= 1
5485  *	v_0 <= -1
5486  *	v_0 = 0 and v_1 >= 1
5487  *	v_0 = 0 and v_1 <= -1
5488  *	v_0 = 0 and v_1 = 0 and v_2 >= 1
5489  *	v_0 = 0 and v_1 = 0 and v_2 <= -1
5490  *	...
5491  * in this order.
5492  */
isl_tab_basic_set_non_trivial_lexmin(__isl_take isl_basic_set * bset,int n_op,int n_region,struct isl_trivial_region * region,int (* conflict)(int con,void * user),void * user)5493 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
5494 	__isl_take isl_basic_set *bset, int n_op, int n_region,
5495 	struct isl_trivial_region *region,
5496 	int (*conflict)(int con, void *user), void *user)
5497 {
5498 	struct isl_lexmin_data data = { n_op, n_region, region };
5499 	int level, init;
5500 
5501 	if (!bset)
5502 		return NULL;
5503 
5504 	if (init_lexmin_data(&data, bset) < 0)
5505 		goto error;
5506 	data.tab->conflict = conflict;
5507 	data.tab->conflict_user = user;
5508 
5509 	level = 0;
5510 	init = 1;
5511 
5512 	while (level >= 0) {
5513 		enum isl_next next;
5514 		struct isl_local_region *local = &data.local[level];
5515 
5516 		next = enter_level(level, init, &data);
5517 		if (next < 0)
5518 			goto error;
5519 		if (next == isl_next_done)
5520 			break;
5521 		if (next == isl_next_backtrack) {
5522 			level--;
5523 			init = 0;
5524 			continue;
5525 		}
5526 
5527 		if (better_next_side(local, &data) < 0)
5528 			goto error;
5529 		if (pick_side(local, &data) < 0)
5530 			goto error;
5531 
5532 		local->side++;
5533 		level++;
5534 		init = 1;
5535 	}
5536 
5537 	clear_lexmin_data(&data);
5538 	isl_basic_set_free(bset);
5539 
5540 	return data.sol;
5541 error:
5542 	clear_lexmin_data(&data);
5543 	isl_basic_set_free(bset);
5544 	isl_vec_free(data.sol);
5545 	return NULL;
5546 }
5547 
5548 /* Wrapper for a tableau that is used for computing
5549  * the lexicographically smallest rational point of a non-negative set.
5550  * This point is represented by the sample value of "tab",
5551  * unless "tab" is empty.
5552  */
5553 struct isl_tab_lexmin {
5554 	isl_ctx *ctx;
5555 	struct isl_tab *tab;
5556 };
5557 
5558 /* Free "tl" and return NULL.
5559  */
isl_tab_lexmin_free(__isl_take isl_tab_lexmin * tl)5560 __isl_null isl_tab_lexmin *isl_tab_lexmin_free(__isl_take isl_tab_lexmin *tl)
5561 {
5562 	if (!tl)
5563 		return NULL;
5564 	isl_ctx_deref(tl->ctx);
5565 	isl_tab_free(tl->tab);
5566 	free(tl);
5567 
5568 	return NULL;
5569 }
5570 
5571 /* Construct an isl_tab_lexmin for computing
5572  * the lexicographically smallest rational point in "bset",
5573  * assuming that all variables are non-negative.
5574  */
isl_tab_lexmin_from_basic_set(__isl_take isl_basic_set * bset)5575 __isl_give isl_tab_lexmin *isl_tab_lexmin_from_basic_set(
5576 	__isl_take isl_basic_set *bset)
5577 {
5578 	isl_ctx *ctx;
5579 	isl_tab_lexmin *tl;
5580 
5581 	if (!bset)
5582 		return NULL;
5583 
5584 	ctx = isl_basic_set_get_ctx(bset);
5585 	tl = isl_calloc_type(ctx, struct isl_tab_lexmin);
5586 	if (!tl)
5587 		goto error;
5588 	tl->ctx = ctx;
5589 	isl_ctx_ref(ctx);
5590 	tl->tab = tab_for_lexmin(bset, NULL, 0, 0);
5591 	isl_basic_set_free(bset);
5592 	if (!tl->tab)
5593 		return isl_tab_lexmin_free(tl);
5594 	return tl;
5595 error:
5596 	isl_basic_set_free(bset);
5597 	isl_tab_lexmin_free(tl);
5598 	return NULL;
5599 }
5600 
5601 /* Return the dimension of the set represented by "tl".
5602  */
isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin * tl)5603 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin *tl)
5604 {
5605 	return tl ? tl->tab->n_var : -1;
5606 }
5607 
5608 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5609  * solution if needed.
5610  * The equality is added as two opposite inequality constraints.
5611  */
isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin * tl,isl_int * eq)5612 __isl_give isl_tab_lexmin *isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin *tl,
5613 	isl_int *eq)
5614 {
5615 	unsigned n_var;
5616 
5617 	if (!tl || !eq)
5618 		return isl_tab_lexmin_free(tl);
5619 
5620 	if (isl_tab_extend_cons(tl->tab, 2) < 0)
5621 		return isl_tab_lexmin_free(tl);
5622 	n_var = tl->tab->n_var;
5623 	isl_seq_neg(eq, eq, 1 + n_var);
5624 	tl->tab = add_lexmin_ineq(tl->tab, eq);
5625 	isl_seq_neg(eq, eq, 1 + n_var);
5626 	tl->tab = add_lexmin_ineq(tl->tab, eq);
5627 
5628 	if (!tl->tab)
5629 		return isl_tab_lexmin_free(tl);
5630 
5631 	return tl;
5632 }
5633 
5634 /* Add cuts to "tl" until the sample value reaches an integer value or
5635  * until the result becomes empty.
5636  */
isl_tab_lexmin_cut_to_integer(__isl_take isl_tab_lexmin * tl)5637 __isl_give isl_tab_lexmin *isl_tab_lexmin_cut_to_integer(
5638 	__isl_take isl_tab_lexmin *tl)
5639 {
5640 	if (!tl)
5641 		return NULL;
5642 	tl->tab = cut_to_integer_lexmin(tl->tab, CUT_ONE);
5643 	if (!tl->tab)
5644 		return isl_tab_lexmin_free(tl);
5645 	return tl;
5646 }
5647 
5648 /* Return the lexicographically smallest rational point in the basic set
5649  * from which "tl" was constructed.
5650  * If the original input was empty, then return a zero-length vector.
5651  */
isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin * tl)5652 __isl_give isl_vec *isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin *tl)
5653 {
5654 	if (!tl)
5655 		return NULL;
5656 	if (tl->tab->empty)
5657 		return isl_vec_alloc(tl->ctx, 0);
5658 	else
5659 		return isl_tab_get_sample_value(tl->tab);
5660 }
5661 
5662 struct isl_sol_pma {
5663 	struct isl_sol	sol;
5664 	isl_pw_multi_aff *pma;
5665 	isl_set *empty;
5666 };
5667 
sol_pma_free(struct isl_sol * sol)5668 static void sol_pma_free(struct isl_sol *sol)
5669 {
5670 	struct isl_sol_pma *sol_pma = (struct isl_sol_pma *) sol;
5671 	isl_pw_multi_aff_free(sol_pma->pma);
5672 	isl_set_free(sol_pma->empty);
5673 }
5674 
5675 /* This function is called for parts of the context where there is
5676  * no solution, with "bset" corresponding to the context tableau.
5677  * Simply add the basic set to the set "empty".
5678  */
sol_pma_add_empty(struct isl_sol_pma * sol,__isl_take isl_basic_set * bset)5679 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5680 	__isl_take isl_basic_set *bset)
5681 {
5682 	if (!bset || !sol->empty)
5683 		goto error;
5684 
5685 	sol->empty = isl_set_grow(sol->empty, 1);
5686 	bset = isl_basic_set_simplify(bset);
5687 	bset = isl_basic_set_finalize(bset);
5688 	sol->empty = isl_set_add_basic_set(sol->empty, bset);
5689 	if (!sol->empty)
5690 		sol->sol.error = 1;
5691 	return;
5692 error:
5693 	isl_basic_set_free(bset);
5694 	sol->sol.error = 1;
5695 }
5696 
5697 /* Given a basic set "dom" that represents the context and a tuple of
5698  * affine expressions "maff" defined over this domain, construct
5699  * an isl_pw_multi_aff with a single cell corresponding to "dom" and
5700  * the affine expressions in "maff".
5701  */
sol_pma_add(struct isl_sol_pma * sol,__isl_take isl_basic_set * dom,__isl_take isl_multi_aff * maff)5702 static void sol_pma_add(struct isl_sol_pma *sol,
5703 	__isl_take isl_basic_set *dom, __isl_take isl_multi_aff *maff)
5704 {
5705 	isl_pw_multi_aff *pma;
5706 
5707 	dom = isl_basic_set_simplify(dom);
5708 	dom = isl_basic_set_finalize(dom);
5709 	pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5710 	sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5711 	if (!sol->pma)
5712 		sol->sol.error = 1;
5713 }
5714 
sol_pma_add_empty_wrap(struct isl_sol * sol,__isl_take isl_basic_set * bset)5715 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5716 	__isl_take isl_basic_set *bset)
5717 {
5718 	sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5719 }
5720 
sol_pma_add_wrap(struct isl_sol * sol,__isl_take isl_basic_set * dom,__isl_take isl_multi_aff * ma)5721 static void sol_pma_add_wrap(struct isl_sol *sol,
5722 	__isl_take isl_basic_set *dom, __isl_take isl_multi_aff *ma)
5723 {
5724 	sol_pma_add((struct isl_sol_pma *)sol, dom, ma);
5725 }
5726 
5727 /* Construct an isl_sol_pma structure for accumulating the solution.
5728  * If track_empty is set, then we also keep track of the parts
5729  * of the context where there is no solution.
5730  * If max is set, then we are solving a maximization, rather than
5731  * a minimization problem, which means that the variables in the
5732  * tableau have value "M - x" rather than "M + x".
5733  */
sol_pma_init(__isl_keep isl_basic_map * bmap,__isl_take isl_basic_set * dom,int track_empty,int max)5734 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5735 	__isl_take isl_basic_set *dom, int track_empty, int max)
5736 {
5737 	struct isl_sol_pma *sol_pma = NULL;
5738 	isl_space *space;
5739 
5740 	if (!bmap)
5741 		goto error;
5742 
5743 	sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5744 	if (!sol_pma)
5745 		goto error;
5746 
5747 	sol_pma->sol.free = &sol_pma_free;
5748 	if (sol_init(&sol_pma->sol, bmap, dom, max) < 0)
5749 		goto error;
5750 	sol_pma->sol.add = &sol_pma_add_wrap;
5751 	sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5752 	space = isl_space_copy(sol_pma->sol.space);
5753 	sol_pma->pma = isl_pw_multi_aff_empty(space);
5754 	if (!sol_pma->pma)
5755 		goto error;
5756 
5757 	if (track_empty) {
5758 		sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5759 							1, ISL_SET_DISJOINT);
5760 		if (!sol_pma->empty)
5761 			goto error;
5762 	}
5763 
5764 	isl_basic_set_free(dom);
5765 	return &sol_pma->sol;
5766 error:
5767 	isl_basic_set_free(dom);
5768 	sol_free(&sol_pma->sol);
5769 	return NULL;
5770 }
5771 
5772 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5773  * some obvious symmetries.
5774  *
5775  * We call basic_map_partial_lexopt_base_sol and extract the results.
5776  */
basic_map_partial_lexopt_base_pw_multi_aff(__isl_take isl_basic_map * bmap,__isl_take isl_basic_set * dom,__isl_give isl_set ** empty,int max)5777 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pw_multi_aff(
5778 	__isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5779 	__isl_give isl_set **empty, int max)
5780 {
5781 	isl_pw_multi_aff *result = NULL;
5782 	struct isl_sol *sol;
5783 	struct isl_sol_pma *sol_pma;
5784 
5785 	sol = basic_map_partial_lexopt_base_sol(bmap, dom, empty, max,
5786 						&sol_pma_init);
5787 	if (!sol)
5788 		return NULL;
5789 	sol_pma = (struct isl_sol_pma *) sol;
5790 
5791 	result = isl_pw_multi_aff_copy(sol_pma->pma);
5792 	if (empty)
5793 		*empty = isl_set_copy(sol_pma->empty);
5794 	sol_free(&sol_pma->sol);
5795 	return result;
5796 }
5797 
5798 /* Given that the last input variable of "maff" represents the minimum
5799  * of some bounds, check whether we need to plug in the expression
5800  * of the minimum.
5801  *
5802  * In particular, check if the last input variable appears in any
5803  * of the expressions in "maff".
5804  */
need_substitution(__isl_keep isl_multi_aff * maff)5805 static isl_bool need_substitution(__isl_keep isl_multi_aff *maff)
5806 {
5807 	int i;
5808 	isl_size n_in;
5809 	unsigned pos;
5810 
5811 	n_in = isl_multi_aff_dim(maff, isl_dim_in);
5812 	if (n_in < 0)
5813 		return isl_bool_error;
5814 	pos = n_in - 1;
5815 
5816 	for (i = 0; i < maff->n; ++i) {
5817 		isl_bool involves;
5818 
5819 		involves = isl_aff_involves_dims(maff->u.p[i],
5820 						isl_dim_in, pos, 1);
5821 		if (involves < 0 || involves)
5822 			return involves;
5823 	}
5824 
5825 	return isl_bool_false;
5826 }
5827 
5828 /* Given a set of upper bounds on the last "input" variable m,
5829  * construct a piecewise affine expression that selects
5830  * the minimal upper bound to m, i.e.,
5831  * divide the space into cells where one
5832  * of the upper bounds is smaller than all the others and select
5833  * this upper bound on that cell.
5834  *
5835  * In particular, if there are n bounds b_i, then the result
5836  * consists of n cell, each one of the form
5837  *
5838  *	b_i <= b_j	for j > i
5839  *	b_i <  b_j	for j < i
5840  *
5841  * The affine expression on this cell is
5842  *
5843  *	b_i
5844  */
set_minimum_pa(__isl_take isl_space * space,__isl_take isl_mat * var)5845 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5846 	__isl_take isl_mat *var)
5847 {
5848 	int i;
5849 	isl_aff *aff = NULL;
5850 	isl_basic_set *bset = NULL;
5851 	isl_pw_aff *paff = NULL;
5852 	isl_space *pw_space;
5853 	isl_local_space *ls = NULL;
5854 
5855 	if (!space || !var)
5856 		goto error;
5857 
5858 	ls = isl_local_space_from_space(isl_space_copy(space));
5859 	pw_space = isl_space_copy(space);
5860 	pw_space = isl_space_from_domain(pw_space);
5861 	pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5862 	paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5863 
5864 	for (i = 0; i < var->n_row; ++i) {
5865 		isl_pw_aff *paff_i;
5866 
5867 		aff = isl_aff_alloc(isl_local_space_copy(ls));
5868 		bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5869 					       0, var->n_row - 1);
5870 		if (!aff || !bset)
5871 			goto error;
5872 		isl_int_set_si(aff->v->el[0], 1);
5873 		isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5874 		isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5875 		bset = select_minimum(bset, var, i);
5876 		paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5877 		paff = isl_pw_aff_add_disjoint(paff, paff_i);
5878 	}
5879 
5880 	isl_local_space_free(ls);
5881 	isl_space_free(space);
5882 	isl_mat_free(var);
5883 	return paff;
5884 error:
5885 	isl_aff_free(aff);
5886 	isl_basic_set_free(bset);
5887 	isl_pw_aff_free(paff);
5888 	isl_local_space_free(ls);
5889 	isl_space_free(space);
5890 	isl_mat_free(var);
5891 	return NULL;
5892 }
5893 
5894 /* Given a piecewise multi-affine expression of which the last input variable
5895  * is the minimum of the bounds in "cst", plug in the value of the minimum.
5896  * This minimum expression is given in "min_expr_pa".
5897  * The set "min_expr" contains the same information, but in the form of a set.
5898  * The variable is subsequently projected out.
5899  *
5900  * The implementation is similar to those of "split" and "split_domain".
5901  * If the variable appears in a given expression, then minimum expression
5902  * is plugged in.  Otherwise, if the variable appears in the constraints
5903  * and a split is required, then the domain is split.  Otherwise, no split
5904  * is performed.
5905  */
split_domain_pma(__isl_take isl_pw_multi_aff * opt,__isl_take isl_pw_aff * min_expr_pa,__isl_take isl_set * min_expr,__isl_take isl_mat * cst)5906 static __isl_give isl_pw_multi_aff *split_domain_pma(
5907 	__isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5908 	__isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5909 {
5910 	isl_size n_in;
5911 	int i;
5912 	isl_space *space;
5913 	isl_pw_multi_aff *res;
5914 
5915 	if (!opt || !min_expr || !cst)
5916 		goto error;
5917 
5918 	n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5919 	if (n_in < 0)
5920 		goto error;
5921 	space = isl_pw_multi_aff_get_space(opt);
5922 	space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5923 	res = isl_pw_multi_aff_empty(space);
5924 
5925 	for (i = 0; i < opt->n; ++i) {
5926 		isl_bool subs;
5927 		isl_pw_multi_aff *pma;
5928 
5929 		pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5930 					 isl_multi_aff_copy(opt->p[i].maff));
5931 		subs = need_substitution(opt->p[i].maff);
5932 		if (subs < 0) {
5933 			pma = isl_pw_multi_aff_free(pma);
5934 		} else if (subs) {
5935 			pma = isl_pw_multi_aff_substitute(pma,
5936 					isl_dim_in, n_in - 1, min_expr_pa);
5937 		} else {
5938 			isl_bool split;
5939 			split = need_split_set(opt->p[i].set, cst);
5940 			if (split < 0)
5941 				pma = isl_pw_multi_aff_free(pma);
5942 			else if (split)
5943 				pma = isl_pw_multi_aff_intersect_domain(pma,
5944 						       isl_set_copy(min_expr));
5945 		}
5946 		pma = isl_pw_multi_aff_project_out(pma,
5947 						    isl_dim_in, n_in - 1, 1);
5948 
5949 		res = isl_pw_multi_aff_add_disjoint(res, pma);
5950 	}
5951 
5952 	isl_pw_multi_aff_free(opt);
5953 	isl_pw_aff_free(min_expr_pa);
5954 	isl_set_free(min_expr);
5955 	isl_mat_free(cst);
5956 	return res;
5957 error:
5958 	isl_pw_multi_aff_free(opt);
5959 	isl_pw_aff_free(min_expr_pa);
5960 	isl_set_free(min_expr);
5961 	isl_mat_free(cst);
5962 	return NULL;
5963 }
5964 
5965 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pw_multi_aff(
5966 	__isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5967 	__isl_give isl_set **empty, int max);
5968 
5969 /* This function is called from basic_map_partial_lexopt_symm.
5970  * The last variable of "bmap" and "dom" corresponds to the minimum
5971  * of the bounds in "cst".  "map_space" is the space of the original
5972  * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5973  * is the space of the original domain.
5974  *
5975  * We recursively call basic_map_partial_lexopt and then plug in
5976  * the definition of the minimum in the result.
5977  */
5978 static __isl_give isl_pw_multi_aff *
basic_map_partial_lexopt_symm_core_pw_multi_aff(__isl_take isl_basic_map * bmap,__isl_take isl_basic_set * dom,__isl_give isl_set ** empty,int max,__isl_take isl_mat * cst,__isl_take isl_space * map_space,__isl_take isl_space * set_space)5979 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5980 	__isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5981 	__isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5982 	__isl_take isl_space *map_space, __isl_take isl_space *set_space)
5983 {
5984 	isl_pw_multi_aff *opt;
5985 	isl_pw_aff *min_expr_pa;
5986 	isl_set *min_expr;
5987 
5988 	min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5989 	min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5990 					isl_mat_copy(cst));
5991 
5992 	opt = basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, max);
5993 
5994 	if (empty) {
5995 		*empty = split(*empty,
5996 			       isl_set_copy(min_expr), isl_mat_copy(cst));
5997 		*empty = isl_set_reset_space(*empty, set_space);
5998 	}
5999 
6000 	opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
6001 	opt = isl_pw_multi_aff_reset_space(opt, map_space);
6002 
6003 	return opt;
6004 }
6005 
6006 #undef TYPE
6007 #define TYPE	isl_pw_multi_aff
6008 #undef SUFFIX
6009 #define SUFFIX	_pw_multi_aff
6010 #include "isl_tab_lexopt_templ.c"
6011