1 /*
2  * Copyright 2011      INRIA Saclay
3  * Copyright 2012-2014 Ecole Normale Superieure
4  *
5  * Use of this software is governed by the MIT license
6  *
7  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9  * 91893 Orsay, France
10  * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
11  */
12 
13 #include <isl_ctx_private.h>
14 #include <isl/id.h>
15 #include <isl_map_private.h>
16 #include <isl_local_space_private.h>
17 #include <isl_space_private.h>
18 #include <isl_mat_private.h>
19 #include <isl_aff_private.h>
20 #include <isl_vec_private.h>
21 #include <isl_point_private.h>
22 #include <isl_seq.h>
23 #include <isl_local.h>
24 
isl_local_space_get_ctx(__isl_keep isl_local_space * ls)25 isl_ctx *isl_local_space_get_ctx(__isl_keep isl_local_space *ls)
26 {
27 	return ls ? ls->dim->ctx : NULL;
28 }
29 
30 /* Return a hash value that digests "ls".
31  */
isl_local_space_get_hash(__isl_keep isl_local_space * ls)32 uint32_t isl_local_space_get_hash(__isl_keep isl_local_space *ls)
33 {
34 	uint32_t hash, space_hash, div_hash;
35 
36 	if (!ls)
37 		return 0;
38 
39 	hash = isl_hash_init();
40 	space_hash = isl_space_get_hash(ls->dim);
41 	isl_hash_hash(hash, space_hash);
42 	div_hash = isl_mat_get_hash(ls->div);
43 	isl_hash_hash(hash, div_hash);
44 
45 	return hash;
46 }
47 
isl_local_space_alloc_div(__isl_take isl_space * space,__isl_take isl_mat * div)48 __isl_give isl_local_space *isl_local_space_alloc_div(
49 	__isl_take isl_space *space, __isl_take isl_mat *div)
50 {
51 	isl_ctx *ctx;
52 	isl_local_space *ls = NULL;
53 
54 	if (!space || !div)
55 		goto error;
56 
57 	ctx = isl_space_get_ctx(space);
58 	ls = isl_calloc_type(ctx, struct isl_local_space);
59 	if (!ls)
60 		goto error;
61 
62 	ls->ref = 1;
63 	ls->dim = space;
64 	ls->div = div;
65 
66 	return ls;
67 error:
68 	isl_mat_free(div);
69 	isl_space_free(space);
70 	isl_local_space_free(ls);
71 	return NULL;
72 }
73 
isl_local_space_alloc(__isl_take isl_space * space,unsigned n_div)74 __isl_give isl_local_space *isl_local_space_alloc(__isl_take isl_space *space,
75 	unsigned n_div)
76 {
77 	isl_ctx *ctx;
78 	isl_mat *div;
79 	isl_size total;
80 
81 	if (!space)
82 		return NULL;
83 
84 	total = isl_space_dim(space, isl_dim_all);
85 	if (total < 0)
86 		return isl_local_space_from_space(isl_space_free(space));
87 
88 	ctx = isl_space_get_ctx(space);
89 	div = isl_mat_alloc(ctx, n_div, 1 + 1 + total + n_div);
90 	return isl_local_space_alloc_div(space, div);
91 }
92 
isl_local_space_from_space(__isl_take isl_space * space)93 __isl_give isl_local_space *isl_local_space_from_space(
94 	__isl_take isl_space *space)
95 {
96 	return isl_local_space_alloc(space, 0);
97 }
98 
isl_local_space_copy(__isl_keep isl_local_space * ls)99 __isl_give isl_local_space *isl_local_space_copy(__isl_keep isl_local_space *ls)
100 {
101 	if (!ls)
102 		return NULL;
103 
104 	ls->ref++;
105 	return ls;
106 }
107 
isl_local_space_dup(__isl_keep isl_local_space * ls)108 __isl_give isl_local_space *isl_local_space_dup(__isl_keep isl_local_space *ls)
109 {
110 	if (!ls)
111 		return NULL;
112 
113 	return isl_local_space_alloc_div(isl_space_copy(ls->dim),
114 					 isl_mat_copy(ls->div));
115 
116 }
117 
isl_local_space_cow(__isl_take isl_local_space * ls)118 __isl_give isl_local_space *isl_local_space_cow(__isl_take isl_local_space *ls)
119 {
120 	if (!ls)
121 		return NULL;
122 
123 	if (ls->ref == 1)
124 		return ls;
125 	ls->ref--;
126 	return isl_local_space_dup(ls);
127 }
128 
isl_local_space_free(__isl_take isl_local_space * ls)129 __isl_null isl_local_space *isl_local_space_free(
130 	__isl_take isl_local_space *ls)
131 {
132 	if (!ls)
133 		return NULL;
134 
135 	if (--ls->ref > 0)
136 		return NULL;
137 
138 	isl_space_free(ls->dim);
139 	isl_mat_free(ls->div);
140 
141 	free(ls);
142 
143 	return NULL;
144 }
145 
146 /* Is the local space that of a parameter domain?
147  */
isl_local_space_is_params(__isl_keep isl_local_space * ls)148 isl_bool isl_local_space_is_params(__isl_keep isl_local_space *ls)
149 {
150 	if (!ls)
151 		return isl_bool_error;
152 	return isl_space_is_params(ls->dim);
153 }
154 
155 /* Is the local space that of a set?
156  */
isl_local_space_is_set(__isl_keep isl_local_space * ls)157 isl_bool isl_local_space_is_set(__isl_keep isl_local_space *ls)
158 {
159 	return ls ? isl_space_is_set(ls->dim) : isl_bool_error;
160 }
161 
162 #undef TYPE
163 #define TYPE	isl_local_space
164 
165 #include "isl_type_has_equal_space_bin_templ.c"
166 
167 /* Is the space of "ls" equal to "space"?
168  */
isl_local_space_has_space(__isl_keep isl_local_space * ls,__isl_keep isl_space * space)169 isl_bool isl_local_space_has_space(__isl_keep isl_local_space *ls,
170 	__isl_keep isl_space *space)
171 {
172 	return isl_space_is_equal(isl_local_space_peek_space(ls), space);
173 }
174 
175 /* Check that the space of "ls" is equal to "space".
176  */
isl_local_space_check_has_space(__isl_keep isl_local_space * ls,__isl_keep isl_space * space)177 static isl_stat isl_local_space_check_has_space(__isl_keep isl_local_space *ls,
178 	__isl_keep isl_space *space)
179 {
180 	isl_bool ok;
181 
182 	ok = isl_local_space_has_space(ls, space);
183 	if (ok < 0)
184 		return isl_stat_error;
185 	if (!ok)
186 		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
187 			"spaces don't match", return isl_stat_error);
188 	return isl_stat_ok;
189 }
190 
191 /* Return true if the two local spaces are identical, with identical
192  * expressions for the integer divisions.
193  */
isl_local_space_is_equal(__isl_keep isl_local_space * ls1,__isl_keep isl_local_space * ls2)194 isl_bool isl_local_space_is_equal(__isl_keep isl_local_space *ls1,
195 	__isl_keep isl_local_space *ls2)
196 {
197 	isl_bool equal;
198 
199 	equal = isl_local_space_has_equal_space(ls1, ls2);
200 	if (equal < 0 || !equal)
201 		return equal;
202 
203 	if (!isl_local_space_divs_known(ls1))
204 		return isl_bool_false;
205 	if (!isl_local_space_divs_known(ls2))
206 		return isl_bool_false;
207 
208 	return isl_mat_is_equal(ls1->div, ls2->div);
209 }
210 
211 /* Compare two isl_local_spaces.
212  *
213  * Return -1 if "ls1" is "smaller" than "ls2", 1 if "ls1" is "greater"
214  * than "ls2" and 0 if they are equal.
215  */
isl_local_space_cmp(__isl_keep isl_local_space * ls1,__isl_keep isl_local_space * ls2)216 int isl_local_space_cmp(__isl_keep isl_local_space *ls1,
217 	__isl_keep isl_local_space *ls2)
218 {
219 	int cmp;
220 
221 	if (ls1 == ls2)
222 		return 0;
223 	if (!ls1)
224 		return -1;
225 	if (!ls2)
226 		return 1;
227 
228 	cmp = isl_space_cmp(ls1->dim, ls2->dim);
229 	if (cmp != 0)
230 		return cmp;
231 
232 	return isl_local_cmp(ls1->div, ls2->div);
233 }
234 
isl_local_space_dim(__isl_keep isl_local_space * ls,enum isl_dim_type type)235 isl_size isl_local_space_dim(__isl_keep isl_local_space *ls,
236 	enum isl_dim_type type)
237 {
238 	if (!ls)
239 		return isl_size_error;
240 	if (type == isl_dim_div)
241 		return ls->div->n_row;
242 	if (type == isl_dim_all) {
243 		isl_size dim = isl_space_dim(ls->dim, isl_dim_all);
244 		if (dim < 0)
245 			return isl_size_error;
246 		return dim + ls->div->n_row;
247 	}
248 	return isl_space_dim(ls->dim, type);
249 }
250 
251 #undef TYPE
252 #define TYPE	isl_local_space
253 #include "check_type_range_templ.c"
254 
isl_local_space_offset(__isl_keep isl_local_space * ls,enum isl_dim_type type)255 unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
256 	enum isl_dim_type type)
257 {
258 	isl_space *space;
259 
260 	if (!ls)
261 		return 0;
262 
263 	space = ls->dim;
264 	switch (type) {
265 	case isl_dim_cst:	return 0;
266 	case isl_dim_param:	return 1;
267 	case isl_dim_in:	return 1 + space->nparam;
268 	case isl_dim_out:	return 1 + space->nparam + space->n_in;
269 	case isl_dim_div:
270 		return 1 + space->nparam + space->n_in + space->n_out;
271 	default:		return 0;
272 	}
273 }
274 
275 /* Return the position of the dimension of the given type and name
276  * in "ls".
277  * Return -1 if no such dimension can be found.
278  */
isl_local_space_find_dim_by_name(__isl_keep isl_local_space * ls,enum isl_dim_type type,const char * name)279 int isl_local_space_find_dim_by_name(__isl_keep isl_local_space *ls,
280 	enum isl_dim_type type, const char *name)
281 {
282 	if (!ls)
283 		return -1;
284 	if (type == isl_dim_div)
285 		return -1;
286 	return isl_space_find_dim_by_name(ls->dim, type, name);
287 }
288 
289 /* Does the given dimension have a name?
290  */
isl_local_space_has_dim_name(__isl_keep isl_local_space * ls,enum isl_dim_type type,unsigned pos)291 isl_bool isl_local_space_has_dim_name(__isl_keep isl_local_space *ls,
292 	enum isl_dim_type type, unsigned pos)
293 {
294 	return ls ? isl_space_has_dim_name(ls->dim, type, pos) : isl_bool_error;
295 }
296 
isl_local_space_get_dim_name(__isl_keep isl_local_space * ls,enum isl_dim_type type,unsigned pos)297 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space *ls,
298 	enum isl_dim_type type, unsigned pos)
299 {
300 	return ls ? isl_space_get_dim_name(ls->dim, type, pos) : NULL;
301 }
302 
isl_local_space_has_dim_id(__isl_keep isl_local_space * ls,enum isl_dim_type type,unsigned pos)303 isl_bool isl_local_space_has_dim_id(__isl_keep isl_local_space *ls,
304 	enum isl_dim_type type, unsigned pos)
305 {
306 	return ls ? isl_space_has_dim_id(ls->dim, type, pos) : isl_bool_error;
307 }
308 
isl_local_space_get_dim_id(__isl_keep isl_local_space * ls,enum isl_dim_type type,unsigned pos)309 __isl_give isl_id *isl_local_space_get_dim_id(__isl_keep isl_local_space *ls,
310 	enum isl_dim_type type, unsigned pos)
311 {
312 	return ls ? isl_space_get_dim_id(ls->dim, type, pos) : NULL;
313 }
314 
315 /* Return the argument of the integer division at position "pos" in "ls".
316  * All local variables in "ls" are known to have a (complete) explicit
317  * representation.
318  */
extract_div(__isl_keep isl_local_space * ls,int pos)319 static __isl_give isl_aff *extract_div(__isl_keep isl_local_space *ls, int pos)
320 {
321 	isl_aff *aff;
322 
323 	aff = isl_aff_alloc(isl_local_space_copy(ls));
324 	if (!aff)
325 		return NULL;
326 	isl_seq_cpy(aff->v->el, ls->div->row[pos], aff->v->size);
327 	return aff;
328 }
329 
330 /* Return the argument of the integer division at position "pos" in "ls".
331  * The integer division at that position is known to have a complete
332  * explicit representation, but some of the others do not.
333  * Remove them first because the domain of an isl_aff
334  * is not allowed to have unknown local variables.
335  */
drop_unknown_divs_and_extract_div(__isl_keep isl_local_space * ls,int pos)336 static __isl_give isl_aff *drop_unknown_divs_and_extract_div(
337 	__isl_keep isl_local_space *ls, int pos)
338 {
339 	int i;
340 	isl_size n;
341 	isl_bool unknown;
342 	isl_aff *aff;
343 
344 	n = isl_local_space_dim(ls, isl_dim_div);
345 	if (n < 0)
346 		return NULL;
347 	ls = isl_local_space_copy(ls);
348 	for (i = n - 1; i >= 0; --i) {
349 		unknown = isl_local_space_div_is_marked_unknown(ls, i);
350 		if (unknown < 0)
351 			ls = isl_local_space_free(ls);
352 		else if (!unknown)
353 			continue;
354 		ls = isl_local_space_drop_dims(ls, isl_dim_div, i, 1);
355 		if (pos > i)
356 			--pos;
357 	}
358 	aff = extract_div(ls, pos);
359 	isl_local_space_free(ls);
360 	return aff;
361 }
362 
363 /* Return the argument of the integer division at position "pos" in "ls".
364  * The integer division is assumed to have a complete explicit
365  * representation.  If some of the other integer divisions
366  * do not have an explicit representation, then they need
367  * to be removed first because the domain of an isl_aff
368  * is not allowed to have unknown local variables.
369  */
isl_local_space_get_div(__isl_keep isl_local_space * ls,int pos)370 __isl_give isl_aff *isl_local_space_get_div(__isl_keep isl_local_space *ls,
371 	int pos)
372 {
373 	isl_bool known;
374 
375 	if (!ls)
376 		return NULL;
377 
378 	if (pos < 0 || pos >= ls->div->n_row)
379 		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
380 			"index out of bounds", return NULL);
381 
382 	known = isl_local_space_div_is_known(ls, pos);
383 	if (known < 0)
384 		return NULL;
385 	if (!known)
386 		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
387 			"expression of div unknown", return NULL);
388 	if (!isl_local_space_is_set(ls))
389 		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
390 			"cannot represent divs of map spaces", return NULL);
391 
392 	known = isl_local_space_divs_known(ls);
393 	if (known < 0)
394 		return NULL;
395 	if (known)
396 		return extract_div(ls, pos);
397 	else
398 		return drop_unknown_divs_and_extract_div(ls, pos);
399 }
400 
401 /* Return the space of "ls".
402  */
isl_local_space_peek_space(__isl_keep isl_local_space * ls)403 __isl_keep isl_space *isl_local_space_peek_space(__isl_keep isl_local_space *ls)
404 {
405 	if (!ls)
406 		return NULL;
407 
408 	return ls->dim;
409 }
410 
isl_local_space_get_space(__isl_keep isl_local_space * ls)411 __isl_give isl_space *isl_local_space_get_space(__isl_keep isl_local_space *ls)
412 {
413 	return isl_space_copy(isl_local_space_peek_space(ls));
414 }
415 
416 /* Return the space of "ls".
417  * This may be either a copy or the space itself
418  * if there is only one reference to "ls".
419  * This allows the space to be modified inplace
420  * if both the local space and its space have only a single reference.
421  * The caller is not allowed to modify "ls" between this call and
422  * a subsequent call to isl_local_space_restore_space.
423  * The only exception is that isl_local_space_free can be called instead.
424  */
isl_local_space_take_space(__isl_keep isl_local_space * ls)425 __isl_give isl_space *isl_local_space_take_space(__isl_keep isl_local_space *ls)
426 {
427 	isl_space *space;
428 
429 	if (!ls)
430 		return NULL;
431 	if (ls->ref != 1)
432 		return isl_local_space_get_space(ls);
433 	space = ls->dim;
434 	ls->dim = NULL;
435 	return space;
436 }
437 
438 /* Set the space of "ls" to "space", where the space of "ls" may be missing
439  * due to a preceding call to isl_local_space_take_space.
440  * However, in this case, "ls" only has a single reference and
441  * then the call to isl_local_space_cow has no effect.
442  */
isl_local_space_restore_space(__isl_take isl_local_space * ls,__isl_take isl_space * space)443 __isl_give isl_local_space *isl_local_space_restore_space(
444 	__isl_take isl_local_space *ls, __isl_take isl_space *space)
445 {
446 	if (!ls || !space)
447 		goto error;
448 
449 	if (ls->dim == space) {
450 		isl_space_free(space);
451 		return ls;
452 	}
453 
454 	ls = isl_local_space_cow(ls);
455 	if (!ls)
456 		goto error;
457 	isl_space_free(ls->dim);
458 	ls->dim = space;
459 
460 	return ls;
461 error:
462 	isl_local_space_free(ls);
463 	isl_space_free(space);
464 	return NULL;
465 }
466 
467 /* Return the local variables of "ls".
468  */
isl_local_space_peek_local(__isl_keep isl_local_space * ls)469 __isl_keep isl_local *isl_local_space_peek_local(__isl_keep isl_local_space *ls)
470 {
471 	return ls ? ls->div : NULL;
472 }
473 
474 /* Replace the identifier of the tuple of type "type" by "id".
475  */
isl_local_space_set_tuple_id(__isl_take isl_local_space * ls,enum isl_dim_type type,__isl_take isl_id * id)476 __isl_give isl_local_space *isl_local_space_set_tuple_id(
477 	__isl_take isl_local_space *ls,
478 	enum isl_dim_type type, __isl_take isl_id *id)
479 {
480 	ls = isl_local_space_cow(ls);
481 	if (!ls)
482 		goto error;
483 	ls->dim = isl_space_set_tuple_id(ls->dim, type, id);
484 	if (!ls->dim)
485 		return isl_local_space_free(ls);
486 	return ls;
487 error:
488 	isl_id_free(id);
489 	return NULL;
490 }
491 
isl_local_space_set_dim_name(__isl_take isl_local_space * ls,enum isl_dim_type type,unsigned pos,const char * s)492 __isl_give isl_local_space *isl_local_space_set_dim_name(
493 	__isl_take isl_local_space *ls,
494 	enum isl_dim_type type, unsigned pos, const char *s)
495 {
496 	ls = isl_local_space_cow(ls);
497 	if (!ls)
498 		return NULL;
499 	ls->dim = isl_space_set_dim_name(ls->dim, type, pos, s);
500 	if (!ls->dim)
501 		return isl_local_space_free(ls);
502 
503 	return ls;
504 }
505 
isl_local_space_set_dim_id(__isl_take isl_local_space * ls,enum isl_dim_type type,unsigned pos,__isl_take isl_id * id)506 __isl_give isl_local_space *isl_local_space_set_dim_id(
507 	__isl_take isl_local_space *ls,
508 	enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
509 {
510 	ls = isl_local_space_cow(ls);
511 	if (!ls)
512 		goto error;
513 	ls->dim = isl_space_set_dim_id(ls->dim, type, pos, id);
514 	if (!ls->dim)
515 		return isl_local_space_free(ls);
516 
517 	return ls;
518 error:
519 	isl_id_free(id);
520 	return NULL;
521 }
522 
523 /* Construct a zero-dimensional local space with the given parameter domain.
524  */
isl_local_space_set_from_params(__isl_take isl_local_space * ls)525 __isl_give isl_local_space *isl_local_space_set_from_params(
526 	__isl_take isl_local_space *ls)
527 {
528 	isl_space *space;
529 
530 	space = isl_local_space_take_space(ls);
531 	space = isl_space_set_from_params(space);
532 	ls = isl_local_space_restore_space(ls, space);
533 
534 	return ls;
535 }
536 
isl_local_space_reset_space(__isl_take isl_local_space * ls,__isl_take isl_space * space)537 __isl_give isl_local_space *isl_local_space_reset_space(
538 	__isl_take isl_local_space *ls, __isl_take isl_space *space)
539 {
540 	ls = isl_local_space_cow(ls);
541 	if (!ls || !space)
542 		goto error;
543 
544 	isl_space_free(ls->dim);
545 	ls->dim = space;
546 
547 	return ls;
548 error:
549 	isl_local_space_free(ls);
550 	isl_space_free(space);
551 	return NULL;
552 }
553 
554 /* Reorder the dimensions of "ls" according to the given reordering.
555  * The reordering r is assumed to have been extended with the local
556  * variables, leaving them in the same order.
557  */
isl_local_space_realign(__isl_take isl_local_space * ls,__isl_take isl_reordering * r)558 __isl_give isl_local_space *isl_local_space_realign(
559 	__isl_take isl_local_space *ls, __isl_take isl_reordering *r)
560 {
561 	ls = isl_local_space_cow(ls);
562 	if (!ls || !r)
563 		goto error;
564 
565 	ls->div = isl_local_reorder(ls->div, isl_reordering_copy(r));
566 	if (!ls->div)
567 		goto error;
568 
569 	ls = isl_local_space_reset_space(ls, isl_reordering_get_space(r));
570 
571 	isl_reordering_free(r);
572 	return ls;
573 error:
574 	isl_local_space_free(ls);
575 	isl_reordering_free(r);
576 	return NULL;
577 }
578 
isl_local_space_add_div(__isl_take isl_local_space * ls,__isl_take isl_vec * div)579 __isl_give isl_local_space *isl_local_space_add_div(
580 	__isl_take isl_local_space *ls, __isl_take isl_vec *div)
581 {
582 	ls = isl_local_space_cow(ls);
583 	if (!ls || !div)
584 		goto error;
585 
586 	if (ls->div->n_col != div->size)
587 		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
588 			"incompatible dimensions", goto error);
589 
590 	ls->div = isl_mat_add_zero_cols(ls->div, 1);
591 	ls->div = isl_mat_add_rows(ls->div, 1);
592 	if (!ls->div)
593 		goto error;
594 
595 	isl_seq_cpy(ls->div->row[ls->div->n_row - 1], div->el, div->size);
596 	isl_int_set_si(ls->div->row[ls->div->n_row - 1][div->size], 0);
597 
598 	isl_vec_free(div);
599 	return ls;
600 error:
601 	isl_local_space_free(ls);
602 	isl_vec_free(div);
603 	return NULL;
604 }
605 
isl_local_space_replace_divs(__isl_take isl_local_space * ls,__isl_take isl_mat * div)606 __isl_give isl_local_space *isl_local_space_replace_divs(
607 	__isl_take isl_local_space *ls, __isl_take isl_mat *div)
608 {
609 	ls = isl_local_space_cow(ls);
610 
611 	if (!ls || !div)
612 		goto error;
613 
614 	isl_mat_free(ls->div);
615 	ls->div = div;
616 	return ls;
617 error:
618 	isl_mat_free(div);
619 	isl_local_space_free(ls);
620 	return NULL;
621 }
622 
623 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
624  * defined by "exp".
625  */
expand_row(__isl_keep isl_mat * dst,int d,__isl_keep isl_mat * src,int s,int * exp)626 static void expand_row(__isl_keep isl_mat *dst, int d,
627 	__isl_keep isl_mat *src, int s, int *exp)
628 {
629 	int i;
630 	unsigned c = src->n_col - src->n_row;
631 
632 	isl_seq_cpy(dst->row[d], src->row[s], c);
633 	isl_seq_clr(dst->row[d] + c, dst->n_col - c);
634 
635 	for (i = 0; i < s; ++i)
636 		isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
637 }
638 
639 /* Compare (known) divs.
640  * Return non-zero if at least one of the two divs is unknown.
641  * In particular, if both divs are unknown, we respect their
642  * current order.  Otherwise, we sort the known div after the unknown
643  * div only if the known div depends on the unknown div.
644  */
cmp_row(isl_int * row_i,isl_int * row_j,int i,int j,unsigned n_row,unsigned n_col)645 static int cmp_row(isl_int *row_i, isl_int *row_j, int i, int j,
646 	unsigned n_row, unsigned n_col)
647 {
648 	int li, lj;
649 	int unknown_i, unknown_j;
650 
651 	unknown_i = isl_int_is_zero(row_i[0]);
652 	unknown_j = isl_int_is_zero(row_j[0]);
653 
654 	if (unknown_i && unknown_j)
655 		return i - j;
656 
657 	if (unknown_i)
658 		li = n_col - n_row + i;
659 	else
660 		li = isl_seq_last_non_zero(row_i, n_col);
661 	if (unknown_j)
662 		lj = n_col - n_row + j;
663 	else
664 		lj = isl_seq_last_non_zero(row_j, n_col);
665 
666 	if (li != lj)
667 		return li - lj;
668 
669 	return isl_seq_cmp(row_i, row_j, n_col);
670 }
671 
672 /* Call cmp_row for divs in a matrix.
673  */
isl_mat_cmp_div(__isl_keep isl_mat * div,int i,int j)674 int isl_mat_cmp_div(__isl_keep isl_mat *div, int i, int j)
675 {
676 	return cmp_row(div->row[i], div->row[j], i, j, div->n_row, div->n_col);
677 }
678 
679 /* Call cmp_row for divs in a basic map.
680  */
bmap_cmp_row(__isl_keep isl_basic_map * bmap,int i,int j,unsigned total)681 static int bmap_cmp_row(__isl_keep isl_basic_map *bmap, int i, int j,
682 	unsigned total)
683 {
684 	return cmp_row(bmap->div[i], bmap->div[j], i, j, bmap->n_div, total);
685 }
686 
687 /* Sort the divs in "bmap".
688  *
689  * We first make sure divs are placed after divs on which they depend.
690  * Then we perform a simple insertion sort based on the same ordering
691  * that is used in isl_merge_divs.
692  */
isl_basic_map_sort_divs(__isl_take isl_basic_map * bmap)693 __isl_give isl_basic_map *isl_basic_map_sort_divs(
694 	__isl_take isl_basic_map *bmap)
695 {
696 	int i, j;
697 	isl_size total;
698 
699 	bmap = isl_basic_map_order_divs(bmap);
700 	if (!bmap)
701 		return NULL;
702 	if (bmap->n_div <= 1)
703 		return bmap;
704 
705 	total = isl_basic_map_dim(bmap, isl_dim_all);
706 	if (total < 0)
707 		return isl_basic_map_free(bmap);
708 	for (i = 1; i < bmap->n_div; ++i) {
709 		for (j = i - 1; j >= 0; --j) {
710 			if (bmap_cmp_row(bmap, j, j + 1, 2 + total) <= 0)
711 				break;
712 			bmap = isl_basic_map_swap_div(bmap, j, j + 1);
713 			if (!bmap)
714 				return NULL;
715 		}
716 	}
717 
718 	return bmap;
719 }
720 
721 /* Sort the divs in the basic maps of "map".
722  */
isl_map_sort_divs(__isl_take isl_map * map)723 __isl_give isl_map *isl_map_sort_divs(__isl_take isl_map *map)
724 {
725 	return isl_map_inline_foreach_basic_map(map, &isl_basic_map_sort_divs);
726 }
727 
728 /* Combine the two lists of divs into a single list.
729  * For each row i in div1, exp1[i] is set to the position of the corresponding
730  * row in the result.  Similarly for div2 and exp2.
731  * This function guarantees
732  *	exp1[i] >= i
733  *	exp1[i+1] > exp1[i]
734  * For optimal merging, the two input list should have been sorted.
735  */
isl_merge_divs(__isl_keep isl_mat * div1,__isl_keep isl_mat * div2,int * exp1,int * exp2)736 __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
737 	__isl_keep isl_mat *div2, int *exp1, int *exp2)
738 {
739 	int i, j, k;
740 	isl_mat *div = NULL;
741 	unsigned d;
742 
743 	if (!div1 || !div2)
744 		return NULL;
745 
746 	d = div1->n_col - div1->n_row;
747 	div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
748 				d + div1->n_row + div2->n_row);
749 	if (!div)
750 		return NULL;
751 
752 	for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
753 		int cmp;
754 
755 		expand_row(div, k, div1, i, exp1);
756 		expand_row(div, k + 1, div2, j, exp2);
757 
758 		cmp = isl_mat_cmp_div(div, k, k + 1);
759 		if (cmp == 0) {
760 			exp1[i++] = k;
761 			exp2[j++] = k;
762 		} else if (cmp < 0) {
763 			exp1[i++] = k;
764 		} else {
765 			exp2[j++] = k;
766 			isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
767 		}
768 	}
769 	for (; i < div1->n_row; ++i, ++k) {
770 		expand_row(div, k, div1, i, exp1);
771 		exp1[i] = k;
772 	}
773 	for (; j < div2->n_row; ++j, ++k) {
774 		expand_row(div, k, div2, j, exp2);
775 		exp2[j] = k;
776 	}
777 
778 	div->n_row = k;
779 	div->n_col = d + k;
780 
781 	return div;
782 }
783 
784 /* Swap divs "a" and "b" in "ls".
785  */
isl_local_space_swap_div(__isl_take isl_local_space * ls,int a,int b)786 __isl_give isl_local_space *isl_local_space_swap_div(
787 	__isl_take isl_local_space *ls, int a, int b)
788 {
789 	int offset;
790 
791 	ls = isl_local_space_cow(ls);
792 	if (!ls)
793 		return NULL;
794 	if (a < 0 || a >= ls->div->n_row || b < 0 || b >= ls->div->n_row)
795 		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
796 			"index out of bounds", return isl_local_space_free(ls));
797 	offset = ls->div->n_col - ls->div->n_row;
798 	ls->div = isl_mat_swap_cols(ls->div, offset + a, offset + b);
799 	ls->div = isl_mat_swap_rows(ls->div, a, b);
800 	if (!ls->div)
801 		return isl_local_space_free(ls);
802 	return ls;
803 }
804 
805 /* Construct a local space that contains all the divs in either
806  * "ls1" or "ls2".
807  */
isl_local_space_intersect(__isl_take isl_local_space * ls1,__isl_take isl_local_space * ls2)808 __isl_give isl_local_space *isl_local_space_intersect(
809 	__isl_take isl_local_space *ls1, __isl_take isl_local_space *ls2)
810 {
811 	isl_ctx *ctx;
812 	int *exp1 = NULL;
813 	int *exp2 = NULL;
814 	isl_mat *div = NULL;
815 	isl_bool equal;
816 
817 	if (!ls1 || !ls2)
818 		goto error;
819 
820 	ctx = isl_local_space_get_ctx(ls1);
821 	if (!isl_space_is_equal(ls1->dim, ls2->dim))
822 		isl_die(ctx, isl_error_invalid,
823 			"spaces should be identical", goto error);
824 
825 	if (ls2->div->n_row == 0) {
826 		isl_local_space_free(ls2);
827 		return ls1;
828 	}
829 
830 	if (ls1->div->n_row == 0) {
831 		isl_local_space_free(ls1);
832 		return ls2;
833 	}
834 
835 	exp1 = isl_alloc_array(ctx, int, ls1->div->n_row);
836 	exp2 = isl_alloc_array(ctx, int, ls2->div->n_row);
837 	if (!exp1 || !exp2)
838 		goto error;
839 
840 	div = isl_merge_divs(ls1->div, ls2->div, exp1, exp2);
841 	if (!div)
842 		goto error;
843 
844 	equal = isl_mat_is_equal(ls1->div, div);
845 	if (equal < 0)
846 		goto error;
847 	if (!equal)
848 		ls1 = isl_local_space_cow(ls1);
849 	if (!ls1)
850 		goto error;
851 
852 	free(exp1);
853 	free(exp2);
854 	isl_local_space_free(ls2);
855 	isl_mat_free(ls1->div);
856 	ls1->div = div;
857 
858 	return ls1;
859 error:
860 	free(exp1);
861 	free(exp2);
862 	isl_mat_free(div);
863 	isl_local_space_free(ls1);
864 	isl_local_space_free(ls2);
865 	return NULL;
866 }
867 
868 /* Is the local variable "div" of "ls" marked as not having
869  * an explicit representation?
870  * Note that even if this variable is not marked in this way and therefore
871  * does have an explicit representation, this representation may still
872  * depend (indirectly) on other local variables that do not
873  * have an explicit representation.
874  */
isl_local_space_div_is_marked_unknown(__isl_keep isl_local_space * ls,int div)875 isl_bool isl_local_space_div_is_marked_unknown(__isl_keep isl_local_space *ls,
876 	int div)
877 {
878 	if (!ls)
879 		return isl_bool_error;
880 	return isl_local_div_is_marked_unknown(ls->div, div);
881 }
882 
883 /* Does "ls" have a complete explicit representation for div "div"?
884  */
isl_local_space_div_is_known(__isl_keep isl_local_space * ls,int div)885 isl_bool isl_local_space_div_is_known(__isl_keep isl_local_space *ls, int div)
886 {
887 	if (!ls)
888 		return isl_bool_error;
889 	return isl_local_div_is_known(ls->div, div);
890 }
891 
892 /* Does "ls" have an explicit representation for all local variables?
893  */
isl_local_space_divs_known(__isl_keep isl_local_space * ls)894 isl_bool isl_local_space_divs_known(__isl_keep isl_local_space *ls)
895 {
896 	if (!ls)
897 		return isl_bool_error;
898 	return isl_local_divs_known(ls->div);
899 }
900 
isl_local_space_domain(__isl_take isl_local_space * ls)901 __isl_give isl_local_space *isl_local_space_domain(
902 	__isl_take isl_local_space *ls)
903 {
904 	isl_size n_out;
905 
906 	n_out = isl_local_space_dim(ls, isl_dim_out);
907 	if (n_out < 0)
908 		return isl_local_space_free(ls);
909 	ls = isl_local_space_drop_dims(ls, isl_dim_out, 0, n_out);
910 	ls = isl_local_space_cow(ls);
911 	if (!ls)
912 		return NULL;
913 	ls->dim = isl_space_domain(ls->dim);
914 	if (!ls->dim)
915 		return isl_local_space_free(ls);
916 	return ls;
917 }
918 
isl_local_space_range(__isl_take isl_local_space * ls)919 __isl_give isl_local_space *isl_local_space_range(
920 	__isl_take isl_local_space *ls)
921 {
922 	isl_size n_in;
923 
924 	n_in = isl_local_space_dim(ls, isl_dim_in);
925 	if (n_in < 0)
926 		return isl_local_space_free(ls);
927 	ls = isl_local_space_drop_dims(ls, isl_dim_in, 0, n_in);
928 	ls = isl_local_space_cow(ls);
929 	if (!ls)
930 		return NULL;
931 
932 	ls->dim = isl_space_range(ls->dim);
933 	if (!ls->dim)
934 		return isl_local_space_free(ls);
935 	return ls;
936 }
937 
938 /* Construct a local space for a map that has the given local
939  * space as domain and that has a zero-dimensional range.
940  */
isl_local_space_from_domain(__isl_take isl_local_space * ls)941 __isl_give isl_local_space *isl_local_space_from_domain(
942 	__isl_take isl_local_space *ls)
943 {
944 	ls = isl_local_space_cow(ls);
945 	if (!ls)
946 		return NULL;
947 	ls->dim = isl_space_from_domain(ls->dim);
948 	if (!ls->dim)
949 		return isl_local_space_free(ls);
950 	return ls;
951 }
952 
isl_local_space_add_dims(__isl_take isl_local_space * ls,enum isl_dim_type type,unsigned n)953 __isl_give isl_local_space *isl_local_space_add_dims(
954 	__isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
955 {
956 	isl_size pos;
957 
958 	pos = isl_local_space_dim(ls, type);
959 	if (pos < 0)
960 		return isl_local_space_free(ls);
961 	return isl_local_space_insert_dims(ls, type, pos, n);
962 }
963 
964 /* Lift the basic set "bset", living in the space of "ls"
965  * to live in a space with extra coordinates corresponding
966  * to the local variables of "ls".
967  */
isl_local_space_lift_basic_set(__isl_take isl_local_space * ls,__isl_take isl_basic_set * bset)968 __isl_give isl_basic_set *isl_local_space_lift_basic_set(
969 	__isl_take isl_local_space *ls, __isl_take isl_basic_set *bset)
970 {
971 	isl_size n_local;
972 	isl_space *space;
973 	isl_basic_set *ls_bset;
974 
975 	n_local = isl_local_space_dim(ls, isl_dim_div);
976 	space = isl_basic_set_peek_space(bset);
977 	if (n_local < 0 ||
978 	    isl_local_space_check_has_space(ls, space) < 0)
979 		goto error;
980 
981 	if (n_local == 0) {
982 		isl_local_space_free(ls);
983 		return bset;
984 	}
985 
986 	bset = isl_basic_set_add_dims(bset, isl_dim_set, n_local);
987 	ls_bset = isl_basic_set_from_local_space(ls);
988 	ls_bset = isl_basic_set_lift(ls_bset);
989 	ls_bset = isl_basic_set_flatten(ls_bset);
990 	bset = isl_basic_set_intersect(bset, ls_bset);
991 
992 	return bset;
993 error:
994 	isl_local_space_free(ls);
995 	isl_basic_set_free(bset);
996 	return NULL;
997 }
998 
999 /* Lift the set "set", living in the space of "ls"
1000  * to live in a space with extra coordinates corresponding
1001  * to the local variables of "ls".
1002  */
isl_local_space_lift_set(__isl_take isl_local_space * ls,__isl_take isl_set * set)1003 __isl_give isl_set *isl_local_space_lift_set(__isl_take isl_local_space *ls,
1004 	__isl_take isl_set *set)
1005 {
1006 	isl_size n_local;
1007 	isl_basic_set *bset;
1008 
1009 	n_local = isl_local_space_dim(ls, isl_dim_div);
1010 	if (n_local < 0 ||
1011 	    isl_local_space_check_has_space(ls, isl_set_peek_space(set)) < 0)
1012 		goto error;
1013 
1014 	if (n_local == 0) {
1015 		isl_local_space_free(ls);
1016 		return set;
1017 	}
1018 
1019 	set = isl_set_add_dims(set, isl_dim_set, n_local);
1020 	bset = isl_basic_set_from_local_space(ls);
1021 	bset = isl_basic_set_lift(bset);
1022 	bset = isl_basic_set_flatten(bset);
1023 	set = isl_set_intersect(set, isl_set_from_basic_set(bset));
1024 
1025 	return set;
1026 error:
1027 	isl_local_space_free(ls);
1028 	isl_set_free(set);
1029 	return NULL;
1030 }
1031 
1032 /* Remove common factor of non-constant terms and denominator.
1033  */
normalize_div(__isl_take isl_local_space * ls,int div)1034 static __isl_give isl_local_space *normalize_div(
1035 	__isl_take isl_local_space *ls, int div)
1036 {
1037 	isl_ctx *ctx = ls->div->ctx;
1038 	unsigned total = ls->div->n_col - 2;
1039 
1040 	isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
1041 	isl_int_gcd(ctx->normalize_gcd,
1042 		    ctx->normalize_gcd, ls->div->row[div][0]);
1043 	if (isl_int_is_one(ctx->normalize_gcd))
1044 		return ls;
1045 
1046 	isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
1047 			    ctx->normalize_gcd, total);
1048 	isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
1049 			    ctx->normalize_gcd);
1050 	isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
1051 			    ctx->normalize_gcd);
1052 
1053 	return ls;
1054 }
1055 
1056 /* Exploit the equalities in "eq" to simplify the expressions of
1057  * the integer divisions in "ls".
1058  * The integer divisions in "ls" are assumed to appear as regular
1059  * dimensions in "eq".
1060  */
isl_local_space_substitute_equalities(__isl_take isl_local_space * ls,__isl_take isl_basic_set * eq)1061 __isl_give isl_local_space *isl_local_space_substitute_equalities(
1062 	__isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
1063 {
1064 	int i, j, k;
1065 	isl_size total, dim;
1066 	unsigned n_div;
1067 
1068 	if (!ls || !eq)
1069 		goto error;
1070 
1071 	total = isl_space_dim(eq->dim, isl_dim_all);
1072 	dim = isl_local_space_dim(ls, isl_dim_all);
1073 	if (dim < 0 || total < 0)
1074 		goto error;
1075 	if (dim != total)
1076 		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1077 			"spaces don't match", goto error);
1078 	total++;
1079 	n_div = eq->n_div;
1080 	for (i = 0; i < eq->n_eq; ++i) {
1081 		j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1082 		if (j < 0 || j == 0 || j >= total)
1083 			continue;
1084 
1085 		for (k = 0; k < ls->div->n_row; ++k) {
1086 			if (isl_int_is_zero(ls->div->row[k][1 + j]))
1087 				continue;
1088 			ls = isl_local_space_cow(ls);
1089 			if (!ls)
1090 				goto error;
1091 			ls->div = isl_mat_cow(ls->div);
1092 			if (!ls->div)
1093 				goto error;
1094 			isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
1095 					&ls->div->row[k][0]);
1096 			ls = normalize_div(ls, k);
1097 			if (!ls)
1098 				goto error;
1099 		}
1100 	}
1101 
1102 	isl_basic_set_free(eq);
1103 	return ls;
1104 error:
1105 	isl_basic_set_free(eq);
1106 	isl_local_space_free(ls);
1107 	return NULL;
1108 }
1109 
1110 /* Plug in the affine expressions "subs" of length "subs_len" (including
1111  * the denominator and the constant term) into the variable at position "pos"
1112  * of the "n" div expressions starting at "first".
1113  *
1114  * Let i be the dimension to replace and let "subs" be of the form
1115  *
1116  *	f/d
1117  *
1118  * Any integer division starting at "first" with a non-zero coefficient for i,
1119  *
1120  *	floor((a i + g)/m)
1121  *
1122  * is replaced by
1123  *
1124  *	floor((a f + d g)/(m d))
1125  */
isl_local_space_substitute_seq(__isl_take isl_local_space * ls,enum isl_dim_type type,unsigned pos,isl_int * subs,int subs_len,int first,int n)1126 __isl_give isl_local_space *isl_local_space_substitute_seq(
1127 	__isl_take isl_local_space *ls,
1128 	enum isl_dim_type type, unsigned pos, isl_int *subs, int subs_len,
1129 	int first, int n)
1130 {
1131 	int i;
1132 	isl_int v;
1133 
1134 	if (n == 0)
1135 		return ls;
1136 	ls = isl_local_space_cow(ls);
1137 	if (!ls)
1138 		return NULL;
1139 	ls->div = isl_mat_cow(ls->div);
1140 	if (!ls->div)
1141 		return isl_local_space_free(ls);
1142 
1143 	if (first + n > ls->div->n_row)
1144 		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1145 			"index out of bounds", return isl_local_space_free(ls));
1146 
1147 	pos += isl_local_space_offset(ls, type);
1148 
1149 	isl_int_init(v);
1150 	for (i = first; i < first + n; ++i) {
1151 		if (isl_int_is_zero(ls->div->row[i][1 + pos]))
1152 			continue;
1153 		isl_seq_substitute(ls->div->row[i], pos, subs,
1154 			ls->div->n_col, subs_len, v);
1155 		ls = normalize_div(ls, i);
1156 		if (!ls)
1157 			break;
1158 	}
1159 	isl_int_clear(v);
1160 
1161 	return ls;
1162 }
1163 
1164 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
1165  * of "ls".
1166  *
1167  * Let i be the dimension to replace and let "subs" be of the form
1168  *
1169  *	f/d
1170  *
1171  * Any integer division with a non-zero coefficient for i,
1172  *
1173  *	floor((a i + g)/m)
1174  *
1175  * is replaced by
1176  *
1177  *	floor((a f + d g)/(m d))
1178  */
isl_local_space_substitute(__isl_take isl_local_space * ls,enum isl_dim_type type,unsigned pos,__isl_keep isl_aff * subs)1179 __isl_give isl_local_space *isl_local_space_substitute(
1180 	__isl_take isl_local_space *ls,
1181 	enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
1182 {
1183 	isl_size n_div;
1184 
1185 	ls = isl_local_space_cow(ls);
1186 	if (!ls || !subs)
1187 		return isl_local_space_free(ls);
1188 
1189 	if (!isl_space_is_equal(ls->dim, subs->ls->dim))
1190 		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1191 			"spaces don't match", return isl_local_space_free(ls));
1192 	n_div = isl_local_space_dim(subs->ls, isl_dim_div);
1193 	if (n_div < 0)
1194 		return isl_local_space_free(ls);
1195 	if (n_div != 0)
1196 		isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
1197 			"cannot handle divs yet",
1198 			return isl_local_space_free(ls));
1199 
1200 	return isl_local_space_substitute_seq(ls, type, pos, subs->v->el,
1201 					    subs->v->size, 0, ls->div->n_row);
1202 }
1203 
isl_local_space_is_named_or_nested(__isl_keep isl_local_space * ls,enum isl_dim_type type)1204 isl_bool isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
1205 	enum isl_dim_type type)
1206 {
1207 	if (!ls)
1208 		return isl_bool_error;
1209 	return isl_space_is_named_or_nested(ls->dim, type);
1210 }
1211 
isl_local_space_drop_dims(__isl_take isl_local_space * ls,enum isl_dim_type type,unsigned first,unsigned n)1212 __isl_give isl_local_space *isl_local_space_drop_dims(
1213 	__isl_take isl_local_space *ls,
1214 	enum isl_dim_type type, unsigned first, unsigned n)
1215 {
1216 	if (!ls)
1217 		return NULL;
1218 	if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
1219 		return ls;
1220 
1221 	if (isl_local_space_check_range(ls, type, first, n) < 0)
1222 		return isl_local_space_free(ls);
1223 
1224 	ls = isl_local_space_cow(ls);
1225 	if (!ls)
1226 		return NULL;
1227 
1228 	if (type == isl_dim_div) {
1229 		ls->div = isl_mat_drop_rows(ls->div, first, n);
1230 	} else {
1231 		ls->dim = isl_space_drop_dims(ls->dim, type, first, n);
1232 		if (!ls->dim)
1233 			return isl_local_space_free(ls);
1234 	}
1235 
1236 	first += 1 + isl_local_space_offset(ls, type);
1237 	ls->div = isl_mat_drop_cols(ls->div, first, n);
1238 	if (!ls->div)
1239 		return isl_local_space_free(ls);
1240 
1241 	return ls;
1242 }
1243 
isl_local_space_insert_dims(__isl_take isl_local_space * ls,enum isl_dim_type type,unsigned first,unsigned n)1244 __isl_give isl_local_space *isl_local_space_insert_dims(
1245 	__isl_take isl_local_space *ls,
1246 	enum isl_dim_type type, unsigned first, unsigned n)
1247 {
1248 	if (!ls)
1249 		return NULL;
1250 	if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
1251 		return ls;
1252 
1253 	if (isl_local_space_check_range(ls, type, first, 0) < 0)
1254 		return isl_local_space_free(ls);
1255 
1256 	ls = isl_local_space_cow(ls);
1257 	if (!ls)
1258 		return NULL;
1259 
1260 	if (type == isl_dim_div) {
1261 		ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
1262 	} else {
1263 		ls->dim = isl_space_insert_dims(ls->dim, type, first, n);
1264 		if (!ls->dim)
1265 			return isl_local_space_free(ls);
1266 	}
1267 
1268 	first += 1 + isl_local_space_offset(ls, type);
1269 	ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
1270 	if (!ls->div)
1271 		return isl_local_space_free(ls);
1272 
1273 	return ls;
1274 }
1275 
1276 /* Does the linear part of "constraint" correspond to
1277  * integer division "div" in "ls"?
1278  *
1279  * That is, given div = floor((c + f)/m), is the constraint of the form
1280  *
1281  *		f - m d + c' >= 0		[sign = 1]
1282  * or
1283  *		-f + m d + c'' >= 0		[sign = -1]
1284  * ?
1285  * If so, set *sign to the corresponding value.
1286  */
is_linear_div_constraint(__isl_keep isl_local_space * ls,isl_int * constraint,unsigned div,int * sign)1287 static isl_bool is_linear_div_constraint(__isl_keep isl_local_space *ls,
1288 	isl_int *constraint, unsigned div, int *sign)
1289 {
1290 	isl_bool unknown;
1291 	unsigned pos;
1292 
1293 	unknown = isl_local_space_div_is_marked_unknown(ls, div);
1294 	if (unknown < 0)
1295 		return isl_bool_error;
1296 	if (unknown)
1297 		return isl_bool_false;
1298 
1299 	pos = isl_local_space_offset(ls, isl_dim_div) + div;
1300 
1301 	if (isl_int_eq(constraint[pos], ls->div->row[div][0])) {
1302 		*sign = -1;
1303 		if (!isl_seq_is_neg(constraint + 1,
1304 				    ls->div->row[div] + 2, pos - 1))
1305 			return isl_bool_false;
1306 	} else if (isl_int_abs_eq(constraint[pos], ls->div->row[div][0])) {
1307 		*sign = 1;
1308 		if (!isl_seq_eq(constraint + 1, ls->div->row[div] + 2, pos - 1))
1309 			return isl_bool_false;
1310 	} else {
1311 		return isl_bool_false;
1312 	}
1313 	if (isl_seq_first_non_zero(constraint + pos + 1,
1314 				    ls->div->n_row - div - 1) != -1)
1315 		return isl_bool_false;
1316 	return isl_bool_true;
1317 }
1318 
1319 /* Check if the constraints pointed to by "constraint" is a div
1320  * constraint corresponding to div "div" in "ls".
1321  *
1322  * That is, if div = floor(f/m), then check if the constraint is
1323  *
1324  *		f - m d >= 0
1325  * or
1326  *		-(f-(m-1)) + m d >= 0
1327  *
1328  * First check if the linear part is of the right form and
1329  * then check the constant term.
1330  */
isl_local_space_is_div_constraint(__isl_keep isl_local_space * ls,isl_int * constraint,unsigned div)1331 isl_bool isl_local_space_is_div_constraint(__isl_keep isl_local_space *ls,
1332 	isl_int *constraint, unsigned div)
1333 {
1334 	int sign;
1335 	isl_bool linear;
1336 
1337 	linear = is_linear_div_constraint(ls, constraint, div, &sign);
1338 	if (linear < 0 || !linear)
1339 		return linear;
1340 
1341 	if (sign < 0) {
1342 		int neg;
1343 		isl_int_sub(ls->div->row[div][1],
1344 				ls->div->row[div][1], ls->div->row[div][0]);
1345 		isl_int_add_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
1346 		neg = isl_seq_is_neg(constraint, ls->div->row[div] + 1, 1);
1347 		isl_int_sub_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
1348 		isl_int_add(ls->div->row[div][1],
1349 				ls->div->row[div][1], ls->div->row[div][0]);
1350 		if (!neg)
1351 			return isl_bool_false;
1352 	} else {
1353 		if (!isl_int_eq(constraint[0], ls->div->row[div][1]))
1354 			return isl_bool_false;
1355 	}
1356 
1357 	return isl_bool_true;
1358 }
1359 
1360 /* Is the constraint pointed to by "constraint" one
1361  * of an equality that corresponds to integer division "div" in "ls"?
1362  *
1363  * That is, given an integer division of the form
1364  *
1365  *	a = floor((f + c)/m)
1366  *
1367  * is the equality of the form
1368  *
1369  *		-f + m d + c' = 0
1370  * ?
1371  * Note that the constant term is not checked explicitly, but given
1372  * that this is a valid equality constraint, the constant c' necessarily
1373  * has a value close to -c.
1374  */
isl_local_space_is_div_equality(__isl_keep isl_local_space * ls,isl_int * constraint,unsigned div)1375 isl_bool isl_local_space_is_div_equality(__isl_keep isl_local_space *ls,
1376 	isl_int *constraint, unsigned div)
1377 {
1378 	int sign;
1379 	isl_bool linear;
1380 
1381 	linear = is_linear_div_constraint(ls, constraint, div, &sign);
1382 	if (linear < 0 || !linear)
1383 		return linear;
1384 
1385 	return isl_bool_ok(sign < 0);
1386 }
1387 
1388 /*
1389  * Set active[i] to 1 if the dimension at position i is involved
1390  * in the linear expression l.
1391  */
isl_local_space_get_active(__isl_keep isl_local_space * ls,isl_int * l)1392 int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
1393 {
1394 	int i, j;
1395 	isl_ctx *ctx;
1396 	int *active = NULL;
1397 	isl_size total;
1398 	unsigned offset;
1399 
1400 	ctx = isl_local_space_get_ctx(ls);
1401 	total = isl_local_space_dim(ls, isl_dim_all);
1402 	if (total < 0)
1403 		return NULL;
1404 	active = isl_calloc_array(ctx, int, total);
1405 	if (total && !active)
1406 		return NULL;
1407 
1408 	for (i = 0; i < total; ++i)
1409 		active[i] = !isl_int_is_zero(l[i]);
1410 
1411 	offset = isl_local_space_offset(ls, isl_dim_div) - 1;
1412 	for (i = ls->div->n_row - 1; i >= 0; --i) {
1413 		if (!active[offset + i])
1414 			continue;
1415 		for (j = 0; j < total; ++j)
1416 			active[j] |= !isl_int_is_zero(ls->div->row[i][2 + j]);
1417 	}
1418 
1419 	return active;
1420 }
1421 
1422 /* Given a local space "ls" of a set, create a local space
1423  * for the lift of the set.  In particular, the result
1424  * is of the form [dim -> local[..]], with ls->div->n_row variables in the
1425  * range of the wrapped map.
1426  */
isl_local_space_lift(__isl_take isl_local_space * ls)1427 __isl_give isl_local_space *isl_local_space_lift(
1428 	__isl_take isl_local_space *ls)
1429 {
1430 	ls = isl_local_space_cow(ls);
1431 	if (!ls)
1432 		return NULL;
1433 
1434 	ls->dim = isl_space_lift(ls->dim, ls->div->n_row);
1435 	ls->div = isl_mat_drop_rows(ls->div, 0, ls->div->n_row);
1436 	if (!ls->dim || !ls->div)
1437 		return isl_local_space_free(ls);
1438 
1439 	return ls;
1440 }
1441 
1442 /* Construct a basic map that maps a set living in local space "ls"
1443  * to the corresponding lifted local space.
1444  */
isl_local_space_lifting(__isl_take isl_local_space * ls)1445 __isl_give isl_basic_map *isl_local_space_lifting(
1446 	__isl_take isl_local_space *ls)
1447 {
1448 	isl_basic_map *lifting;
1449 	isl_basic_set *bset;
1450 
1451 	if (!ls)
1452 		return NULL;
1453 	if (!isl_local_space_is_set(ls))
1454 		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1455 			"lifting only defined on set spaces", goto error);
1456 
1457 	bset = isl_basic_set_from_local_space(ls);
1458 	lifting = isl_basic_set_unwrap(isl_basic_set_lift(bset));
1459 	lifting = isl_basic_map_domain_map(lifting);
1460 	lifting = isl_basic_map_reverse(lifting);
1461 
1462 	return lifting;
1463 error:
1464 	isl_local_space_free(ls);
1465 	return NULL;
1466 }
1467 
1468 /* Compute the preimage of "ls" under the function represented by "ma".
1469  * In other words, plug in "ma" in "ls".  The result is a local space
1470  * that is part of the domain space of "ma".
1471  *
1472  * If the divs in "ls" are represented as
1473  *
1474  *	floor((a_i(p) + b_i x + c_i(divs))/n_i)
1475  *
1476  * and ma is represented by
1477  *
1478  *	x = D(p) + F(y) + G(divs')
1479  *
1480  * then the resulting divs are
1481  *
1482  *	floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
1483  *
1484  * We first copy over the divs from "ma" and then
1485  * we add the modified divs from "ls".
1486  */
isl_local_space_preimage_multi_aff(__isl_take isl_local_space * ls,__isl_take isl_multi_aff * ma)1487 __isl_give isl_local_space *isl_local_space_preimage_multi_aff(
1488 	__isl_take isl_local_space *ls, __isl_take isl_multi_aff *ma)
1489 {
1490 	int i;
1491 	isl_space *space;
1492 	isl_local_space *res = NULL;
1493 	isl_size n_div_ls, n_div_ma;
1494 	isl_int f, c1, c2, g;
1495 
1496 	ma = isl_multi_aff_align_divs(ma);
1497 	if (!ls || !ma)
1498 		goto error;
1499 	if (!isl_space_is_range_internal(ls->dim, ma->space))
1500 		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1501 			"spaces don't match", goto error);
1502 
1503 	n_div_ls = isl_local_space_dim(ls, isl_dim_div);
1504 	n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
1505 	if (n_div_ls < 0 || n_div_ma < 0)
1506 		goto error;
1507 
1508 	space = isl_space_domain(isl_multi_aff_get_space(ma));
1509 	res = isl_local_space_alloc(space, n_div_ma + n_div_ls);
1510 	if (!res)
1511 		goto error;
1512 
1513 	if (n_div_ma) {
1514 		isl_mat_free(res->div);
1515 		res->div = isl_mat_copy(ma->u.p[0]->ls->div);
1516 		res->div = isl_mat_add_zero_cols(res->div, n_div_ls);
1517 		res->div = isl_mat_add_rows(res->div, n_div_ls);
1518 		if (!res->div)
1519 			goto error;
1520 	}
1521 
1522 	isl_int_init(f);
1523 	isl_int_init(c1);
1524 	isl_int_init(c2);
1525 	isl_int_init(g);
1526 
1527 	for (i = 0; i < ls->div->n_row; ++i) {
1528 		if (isl_int_is_zero(ls->div->row[i][0])) {
1529 			isl_int_set_si(res->div->row[n_div_ma + i][0], 0);
1530 			continue;
1531 		}
1532 		if (isl_seq_preimage(res->div->row[n_div_ma + i],
1533 			    ls->div->row[i],
1534 			    ma, 0, 0, n_div_ma, n_div_ls, f, c1, c2, g, 1) < 0)
1535 			res = isl_local_space_free(res);
1536 		res = normalize_div(res, n_div_ma + i);
1537 		if (!res)
1538 			break;
1539 	}
1540 
1541 	isl_int_clear(f);
1542 	isl_int_clear(c1);
1543 	isl_int_clear(c2);
1544 	isl_int_clear(g);
1545 
1546 	isl_local_space_free(ls);
1547 	isl_multi_aff_free(ma);
1548 	return res;
1549 error:
1550 	isl_local_space_free(ls);
1551 	isl_multi_aff_free(ma);
1552 	isl_local_space_free(res);
1553 	return NULL;
1554 }
1555 
1556 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "ls"
1557  * to dimensions of "dst_type" at "dst_pos".
1558  *
1559  * Moving to/from local dimensions is not allowed.
1560  * We currently assume that the dimension type changes.
1561  */
isl_local_space_move_dims(__isl_take isl_local_space * ls,enum isl_dim_type dst_type,unsigned dst_pos,enum isl_dim_type src_type,unsigned src_pos,unsigned n)1562 __isl_give isl_local_space *isl_local_space_move_dims(
1563 	__isl_take isl_local_space *ls,
1564 	enum isl_dim_type dst_type, unsigned dst_pos,
1565 	enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1566 {
1567 	unsigned g_dst_pos;
1568 	unsigned g_src_pos;
1569 
1570 	if (!ls)
1571 		return NULL;
1572 	if (n == 0 &&
1573 	    !isl_local_space_is_named_or_nested(ls, src_type) &&
1574 	    !isl_local_space_is_named_or_nested(ls, dst_type))
1575 		return ls;
1576 
1577 	if (isl_local_space_check_range(ls, src_type, src_pos, n) < 0)
1578 		return isl_local_space_free(ls);
1579 	if (isl_local_space_check_range(ls, dst_type, dst_pos, 0) < 0)
1580 		return isl_local_space_free(ls);
1581 	if (src_type == isl_dim_div)
1582 		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1583 			"cannot move divs", return isl_local_space_free(ls));
1584 	if (dst_type == isl_dim_div)
1585 		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1586 			"cannot move to divs", return isl_local_space_free(ls));
1587 	if (dst_type == src_type && dst_pos == src_pos)
1588 		return ls;
1589 	if (dst_type == src_type)
1590 		isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
1591 			"moving dims within the same type not supported",
1592 			return isl_local_space_free(ls));
1593 
1594 	ls = isl_local_space_cow(ls);
1595 	if (!ls)
1596 		return NULL;
1597 
1598 	g_src_pos = 1 + isl_local_space_offset(ls, src_type) + src_pos;
1599 	g_dst_pos = 1 + isl_local_space_offset(ls, dst_type) + dst_pos;
1600 	if (dst_type > src_type)
1601 		g_dst_pos -= n;
1602 	ls->div = isl_mat_move_cols(ls->div, g_dst_pos, g_src_pos, n);
1603 	if (!ls->div)
1604 		return isl_local_space_free(ls);
1605 	ls->dim = isl_space_move_dims(ls->dim, dst_type, dst_pos,
1606 					src_type, src_pos, n);
1607 	if (!ls->dim)
1608 		return isl_local_space_free(ls);
1609 
1610 	return ls;
1611 }
1612 
1613 /* Remove any internal structure of the domain of "ls".
1614  * If there is any such internal structure in the input,
1615  * then the name of the corresponding space is also removed.
1616  */
isl_local_space_flatten_domain(__isl_take isl_local_space * ls)1617 __isl_give isl_local_space *isl_local_space_flatten_domain(
1618 	__isl_take isl_local_space *ls)
1619 {
1620 	if (!ls)
1621 		return NULL;
1622 
1623 	if (!ls->dim->nested[0])
1624 		return ls;
1625 
1626 	ls = isl_local_space_cow(ls);
1627 	if (!ls)
1628 		return NULL;
1629 
1630 	ls->dim = isl_space_flatten_domain(ls->dim);
1631 	if (!ls->dim)
1632 		return isl_local_space_free(ls);
1633 
1634 	return ls;
1635 }
1636 
1637 /* Remove any internal structure of the range of "ls".
1638  * If there is any such internal structure in the input,
1639  * then the name of the corresponding space is also removed.
1640  */
isl_local_space_flatten_range(__isl_take isl_local_space * ls)1641 __isl_give isl_local_space *isl_local_space_flatten_range(
1642 	__isl_take isl_local_space *ls)
1643 {
1644 	if (!ls)
1645 		return NULL;
1646 
1647 	if (!ls->dim->nested[1])
1648 		return ls;
1649 
1650 	ls = isl_local_space_cow(ls);
1651 	if (!ls)
1652 		return NULL;
1653 
1654 	ls->dim = isl_space_flatten_range(ls->dim);
1655 	if (!ls->dim)
1656 		return isl_local_space_free(ls);
1657 
1658 	return ls;
1659 }
1660 
1661 /* Given the local space "ls" of a map, return the local space of a set
1662  * that lives in a space that wraps the space of "ls" and that has
1663  * the same divs.
1664  */
isl_local_space_wrap(__isl_take isl_local_space * ls)1665 __isl_give isl_local_space *isl_local_space_wrap(__isl_take isl_local_space *ls)
1666 {
1667 	ls = isl_local_space_cow(ls);
1668 	if (!ls)
1669 		return NULL;
1670 
1671 	ls->dim = isl_space_wrap(ls->dim);
1672 	if (!ls->dim)
1673 		return isl_local_space_free(ls);
1674 
1675 	return ls;
1676 }
1677 
1678 /* Lift the point "pnt", living in the (set) space of "ls"
1679  * to live in a space with extra coordinates corresponding
1680  * to the local variables of "ls".
1681  */
isl_local_space_lift_point(__isl_take isl_local_space * ls,__isl_take isl_point * pnt)1682 __isl_give isl_point *isl_local_space_lift_point(__isl_take isl_local_space *ls,
1683 	__isl_take isl_point *pnt)
1684 {
1685 	isl_size n_local;
1686 	isl_space *space;
1687 	isl_local *local;
1688 	isl_vec *vec;
1689 
1690 	if (isl_local_space_check_has_space(ls, isl_point_peek_space(pnt)) < 0)
1691 		goto error;
1692 
1693 	local = isl_local_space_peek_local(ls);
1694 	n_local = isl_local_space_dim(ls, isl_dim_div);
1695 	if (n_local < 0)
1696 		goto error;
1697 
1698 	space = isl_point_take_space(pnt);
1699 	vec = isl_point_take_vec(pnt);
1700 
1701 	space = isl_space_lift(space, n_local);
1702 	vec = isl_local_extend_point_vec(local, vec);
1703 
1704 	pnt = isl_point_restore_vec(pnt, vec);
1705 	pnt = isl_point_restore_space(pnt, space);
1706 
1707 	isl_local_space_free(ls);
1708 
1709 	return pnt;
1710 error:
1711 	isl_local_space_free(ls);
1712 	isl_point_free(pnt);
1713 	return NULL;
1714 }
1715