1/*
2 * Copyright 2011      INRIA Saclay
3 * Copyright 2012      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_map_private.h>
15#include <isl_local_space_private.h>
16#include <isl_space_private.h>
17#include <isl_mat_private.h>
18#include <isl_aff_private.h>
19#include <isl/seq.h>
20
21isl_ctx *isl_local_space_get_ctx(__isl_keep isl_local_space *ls)
22{
23	return ls ? ls->dim->ctx : NULL;
24}
25
26__isl_give isl_local_space *isl_local_space_alloc_div(__isl_take isl_space *dim,
27	__isl_take isl_mat *div)
28{
29	isl_ctx *ctx;
30	isl_local_space *ls = NULL;
31
32	if (!dim || !div)
33		goto error;
34
35	ctx = isl_space_get_ctx(dim);
36	ls = isl_calloc_type(ctx, struct isl_local_space);
37	if (!ls)
38		goto error;
39
40	ls->ref = 1;
41	ls->dim = dim;
42	ls->div = div;
43
44	return ls;
45error:
46	isl_mat_free(div);
47	isl_space_free(dim);
48	isl_local_space_free(ls);
49	return NULL;
50}
51
52__isl_give isl_local_space *isl_local_space_alloc(__isl_take isl_space *dim,
53	unsigned n_div)
54{
55	isl_ctx *ctx;
56	isl_mat *div;
57	unsigned total;
58
59	if (!dim)
60		return NULL;
61
62	total = isl_space_dim(dim, isl_dim_all);
63
64	ctx = isl_space_get_ctx(dim);
65	div = isl_mat_alloc(ctx, n_div, 1 + 1 + total + n_div);
66	return isl_local_space_alloc_div(dim, div);
67}
68
69__isl_give isl_local_space *isl_local_space_from_space(__isl_take isl_space *dim)
70{
71	return isl_local_space_alloc(dim, 0);
72}
73
74__isl_give isl_local_space *isl_local_space_copy(__isl_keep isl_local_space *ls)
75{
76	if (!ls)
77		return NULL;
78
79	ls->ref++;
80	return ls;
81}
82
83__isl_give isl_local_space *isl_local_space_dup(__isl_keep isl_local_space *ls)
84{
85	if (!ls)
86		return NULL;
87
88	return isl_local_space_alloc_div(isl_space_copy(ls->dim),
89					 isl_mat_copy(ls->div));
90
91}
92
93__isl_give isl_local_space *isl_local_space_cow(__isl_take isl_local_space *ls)
94{
95	if (!ls)
96		return NULL;
97
98	if (ls->ref == 1)
99		return ls;
100	ls->ref--;
101	return isl_local_space_dup(ls);
102}
103
104void *isl_local_space_free(__isl_take isl_local_space *ls)
105{
106	if (!ls)
107		return NULL;
108
109	if (--ls->ref > 0)
110		return NULL;
111
112	isl_space_free(ls->dim);
113	isl_mat_free(ls->div);
114
115	free(ls);
116
117	return NULL;
118}
119
120/* Is the local space that of a set?
121 */
122int isl_local_space_is_set(__isl_keep isl_local_space *ls)
123{
124	return ls ? isl_space_is_set(ls->dim) : -1;
125}
126
127/* Return true if the two local spaces are identical, with identical
128 * expressions for the integer divisions.
129 */
130int isl_local_space_is_equal(__isl_keep isl_local_space *ls1,
131	__isl_keep isl_local_space *ls2)
132{
133	int equal;
134
135	if (!ls1 || !ls2)
136		return -1;
137
138	equal = isl_space_is_equal(ls1->dim, ls2->dim);
139	if (equal < 0 || !equal)
140		return equal;
141
142	if (!isl_local_space_divs_known(ls1))
143		return 0;
144	if (!isl_local_space_divs_known(ls2))
145		return 0;
146
147	return isl_mat_is_equal(ls1->div, ls2->div);
148}
149
150int isl_local_space_dim(__isl_keep isl_local_space *ls,
151	enum isl_dim_type type)
152{
153	if (!ls)
154		return 0;
155	if (type == isl_dim_div)
156		return ls->div->n_row;
157	if (type == isl_dim_all)
158		return isl_space_dim(ls->dim, isl_dim_all) + ls->div->n_row;
159	return isl_space_dim(ls->dim, type);
160}
161
162unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
163	enum isl_dim_type type)
164{
165	isl_space *dim;
166
167	if (!ls)
168		return 0;
169
170	dim = ls->dim;
171	switch (type) {
172	case isl_dim_cst:	return 0;
173	case isl_dim_param:	return 1;
174	case isl_dim_in:	return 1 + dim->nparam;
175	case isl_dim_out:	return 1 + dim->nparam + dim->n_in;
176	case isl_dim_div:	return 1 + dim->nparam + dim->n_in + dim->n_out;
177	default:		return 0;
178	}
179}
180
181/* Does the given dimension have a name?
182 */
183int isl_local_space_has_dim_name(__isl_keep isl_local_space *ls,
184	enum isl_dim_type type, unsigned pos)
185{
186	return ls ? isl_space_has_dim_name(ls->dim, type, pos) : -1;
187}
188
189const char *isl_local_space_get_dim_name(__isl_keep isl_local_space *ls,
190	enum isl_dim_type type, unsigned pos)
191{
192	return ls ? isl_space_get_dim_name(ls->dim, type, pos) : NULL;
193}
194
195int isl_local_space_has_dim_id(__isl_keep isl_local_space *ls,
196	enum isl_dim_type type, unsigned pos)
197{
198	return ls ? isl_space_has_dim_id(ls->dim, type, pos) : -1;
199}
200
201__isl_give isl_id *isl_local_space_get_dim_id(__isl_keep isl_local_space *ls,
202	enum isl_dim_type type, unsigned pos)
203{
204	return ls ? isl_space_get_dim_id(ls->dim, type, pos) : NULL;
205}
206
207__isl_give isl_aff *isl_local_space_get_div(__isl_keep isl_local_space *ls,
208	int pos)
209{
210	isl_aff *aff;
211
212	if (!ls)
213		return NULL;
214
215	if (pos < 0 || pos >= ls->div->n_row)
216		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
217			"index out of bounds", return NULL);
218
219	if (isl_int_is_zero(ls->div->row[pos][0]))
220		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
221			"expression of div unknown", return NULL);
222	if (!isl_local_space_is_set(ls))
223		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
224			"cannot represent divs of map spaces", return NULL);
225
226	aff = isl_aff_alloc(isl_local_space_copy(ls));
227	if (!aff)
228		return NULL;
229	isl_seq_cpy(aff->v->el, ls->div->row[pos], aff->v->size);
230	return aff;
231}
232
233__isl_give isl_space *isl_local_space_get_space(__isl_keep isl_local_space *ls)
234{
235	if (!ls)
236		return NULL;
237
238	return isl_space_copy(ls->dim);
239}
240
241__isl_give isl_local_space *isl_local_space_set_dim_name(
242	__isl_take isl_local_space *ls,
243	enum isl_dim_type type, unsigned pos, const char *s)
244{
245	ls = isl_local_space_cow(ls);
246	if (!ls)
247		return NULL;
248	ls->dim = isl_space_set_dim_name(ls->dim, type, pos, s);
249	if (!ls->dim)
250		return isl_local_space_free(ls);
251
252	return ls;
253}
254
255__isl_give isl_local_space *isl_local_space_set_dim_id(
256	__isl_take isl_local_space *ls,
257	enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
258{
259	ls = isl_local_space_cow(ls);
260	if (!ls)
261		return isl_id_free(id);
262	ls->dim = isl_space_set_dim_id(ls->dim, type, pos, id);
263	if (!ls->dim)
264		return isl_local_space_free(ls);
265
266	return ls;
267}
268
269__isl_give isl_local_space *isl_local_space_reset_space(
270	__isl_take isl_local_space *ls, __isl_take isl_space *dim)
271{
272	ls = isl_local_space_cow(ls);
273	if (!ls || !dim)
274		goto error;
275
276	isl_space_free(ls->dim);
277	ls->dim = dim;
278
279	return ls;
280error:
281	isl_local_space_free(ls);
282	isl_space_free(dim);
283	return NULL;
284}
285
286/* Reorder the columns of the given div definitions according to the
287 * given reordering.
288 * The order of the divs themselves is assumed not to change.
289 */
290static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
291	__isl_take isl_reordering *r)
292{
293	int i, j;
294	isl_mat *mat;
295	int extra;
296
297	if (!div || !r)
298		goto error;
299
300	extra = isl_space_dim(r->dim, isl_dim_all) + div->n_row - r->len;
301	mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
302	if (!mat)
303		goto error;
304
305	for (i = 0; i < div->n_row; ++i) {
306		isl_seq_cpy(mat->row[i], div->row[i], 2);
307		isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
308		for (j = 0; j < r->len; ++j)
309			isl_int_set(mat->row[i][2 + r->pos[j]],
310				    div->row[i][2 + j]);
311	}
312
313	isl_reordering_free(r);
314	isl_mat_free(div);
315	return mat;
316error:
317	isl_reordering_free(r);
318	isl_mat_free(div);
319	return NULL;
320}
321
322/* Reorder the dimensions of "ls" according to the given reordering.
323 * The reordering r is assumed to have been extended with the local
324 * variables, leaving them in the same order.
325 */
326__isl_give isl_local_space *isl_local_space_realign(
327	__isl_take isl_local_space *ls, __isl_take isl_reordering *r)
328{
329	ls = isl_local_space_cow(ls);
330	if (!ls || !r)
331		goto error;
332
333	ls->div = reorder_divs(ls->div, isl_reordering_copy(r));
334	if (!ls->div)
335		goto error;
336
337	ls = isl_local_space_reset_space(ls, isl_space_copy(r->dim));
338
339	isl_reordering_free(r);
340	return ls;
341error:
342	isl_local_space_free(ls);
343	isl_reordering_free(r);
344	return NULL;
345}
346
347__isl_give isl_local_space *isl_local_space_add_div(
348	__isl_take isl_local_space *ls, __isl_take isl_vec *div)
349{
350	ls = isl_local_space_cow(ls);
351	if (!ls || !div)
352		goto error;
353
354	if (ls->div->n_col != div->size)
355		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
356			"incompatible dimensions", goto error);
357
358	ls->div = isl_mat_add_zero_cols(ls->div, 1);
359	ls->div = isl_mat_add_rows(ls->div, 1);
360	if (!ls->div)
361		goto error;
362
363	isl_seq_cpy(ls->div->row[ls->div->n_row - 1], div->el, div->size);
364	isl_int_set_si(ls->div->row[ls->div->n_row - 1][div->size], 0);
365
366	isl_vec_free(div);
367	return ls;
368error:
369	isl_local_space_free(ls);
370	isl_vec_free(div);
371	return NULL;
372}
373
374__isl_give isl_local_space *isl_local_space_replace_divs(
375	__isl_take isl_local_space *ls, __isl_take isl_mat *div)
376{
377	ls = isl_local_space_cow(ls);
378
379	if (!ls || !div)
380		goto error;
381
382	isl_mat_free(ls->div);
383	ls->div = div;
384	return ls;
385error:
386	isl_mat_free(div);
387	isl_local_space_free(ls);
388	return NULL;
389}
390
391/* Copy row "s" of "src" to row "d" of "dst", applying the expansion
392 * defined by "exp".
393 */
394static void expand_row(__isl_keep isl_mat *dst, int d,
395	__isl_keep isl_mat *src, int s, int *exp)
396{
397	int i;
398	unsigned c = src->n_col - src->n_row;
399
400	isl_seq_cpy(dst->row[d], src->row[s], c);
401	isl_seq_clr(dst->row[d] + c, dst->n_col - c);
402
403	for (i = 0; i < s; ++i)
404		isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
405}
406
407/* Compare (known) divs.
408 * Return non-zero if at least one of the two divs is unknown.
409 * In particular, if both divs are unknown, we respect their
410 * current order.  Otherwise, we sort the known div after the unknown
411 * div only if the known div depends on the unknown div.
412 */
413static int cmp_row(isl_int *row_i, isl_int *row_j, int i, int j,
414	unsigned n_row, unsigned n_col)
415{
416	int li, lj;
417	int unknown_i, unknown_j;
418
419	unknown_i = isl_int_is_zero(row_i[0]);
420	unknown_j = isl_int_is_zero(row_j[0]);
421
422	if (unknown_i && unknown_j)
423		return i - j;
424
425	if (unknown_i)
426		li = n_col - n_row + i;
427	else
428		li = isl_seq_last_non_zero(row_i, n_col);
429	if (unknown_j)
430		lj = n_col - n_row + j;
431	else
432		lj = isl_seq_last_non_zero(row_j, n_col);
433
434	if (li != lj)
435		return li - lj;
436
437	return isl_seq_cmp(row_i, row_j, n_col);
438}
439
440/* Call cmp_row for divs in a matrix.
441 */
442int isl_mat_cmp_div(__isl_keep isl_mat *div, int i, int j)
443{
444	return cmp_row(div->row[i], div->row[j], i, j, div->n_row, div->n_col);
445}
446
447/* Call cmp_row for divs in a basic map.
448 */
449static int bmap_cmp_row(__isl_keep isl_basic_map *bmap, int i, int j,
450	unsigned total)
451{
452	return cmp_row(bmap->div[i], bmap->div[j], i, j, bmap->n_div, total);
453}
454
455/* Sort the divs in "bmap".
456 *
457 * We first make sure divs are placed after divs on which they depend.
458 * Then we perform a simple insertion sort based on the same ordering
459 * that is used in isl_merge_divs.
460 */
461__isl_give isl_basic_map *isl_basic_map_sort_divs(
462	__isl_take isl_basic_map *bmap)
463{
464	int i, j;
465	unsigned total;
466
467	bmap = isl_basic_map_order_divs(bmap);
468	if (!bmap)
469		return NULL;
470	if (bmap->n_div <= 1)
471		return bmap;
472
473	total = 2 + isl_basic_map_total_dim(bmap);
474	for (i = 1; i < bmap->n_div; ++i) {
475		for (j = i - 1; j >= 0; --j) {
476			if (bmap_cmp_row(bmap, j, j + 1, total) <= 0)
477				break;
478			isl_basic_map_swap_div(bmap, j, j + 1);
479		}
480	}
481
482	return bmap;
483}
484
485/* Sort the divs in the basic maps of "map".
486 */
487__isl_give isl_map *isl_map_sort_divs(__isl_take isl_map *map)
488{
489	return isl_map_inline_foreach_basic_map(map, &isl_basic_map_sort_divs);
490}
491
492/* Combine the two lists of divs into a single list.
493 * For each row i in div1, exp1[i] is set to the position of the corresponding
494 * row in the result.  Similarly for div2 and exp2.
495 * This function guarantees
496 *	exp1[i] >= i
497 *	exp1[i+1] > exp1[i]
498 * For optimal merging, the two input list should have been sorted.
499 */
500__isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
501	__isl_keep isl_mat *div2, int *exp1, int *exp2)
502{
503	int i, j, k;
504	isl_mat *div = NULL;
505	unsigned d;
506
507	if (!div1 || !div2)
508		return NULL;
509
510	d = div1->n_col - div1->n_row;
511	div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
512				d + div1->n_row + div2->n_row);
513	if (!div)
514		return NULL;
515
516	for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
517		int cmp;
518
519		expand_row(div, k, div1, i, exp1);
520		expand_row(div, k + 1, div2, j, exp2);
521
522		cmp = isl_mat_cmp_div(div, k, k + 1);
523		if (cmp == 0) {
524			exp1[i++] = k;
525			exp2[j++] = k;
526		} else if (cmp < 0) {
527			exp1[i++] = k;
528		} else {
529			exp2[j++] = k;
530			isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
531		}
532	}
533	for (; i < div1->n_row; ++i, ++k) {
534		expand_row(div, k, div1, i, exp1);
535		exp1[i] = k;
536	}
537	for (; j < div2->n_row; ++j, ++k) {
538		expand_row(div, k, div2, j, exp2);
539		exp2[j] = k;
540	}
541
542	div->n_row = k;
543	div->n_col = d + k;
544
545	return div;
546}
547
548/* Swap divs "a" and "b" in "ls".
549 */
550__isl_give isl_local_space *isl_local_space_swap_div(
551	__isl_take isl_local_space *ls, int a, int b)
552{
553	int offset;
554
555	ls = isl_local_space_cow(ls);
556	if (!ls)
557		return NULL;
558	if (a < 0 || a >= ls->div->n_row || b < 0 || b >= ls->div->n_row)
559		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
560			"index out of bounds", return isl_local_space_free(ls));
561	offset = ls->div->n_col - ls->div->n_row;
562	ls->div = isl_mat_swap_cols(ls->div, offset + a, offset + b);
563	ls->div = isl_mat_swap_rows(ls->div, a, b);
564	if (!ls->div)
565		return isl_local_space_free(ls);
566	return ls;
567}
568
569/* Construct a local space that contains all the divs in either
570 * "ls1" or "ls2".
571 */
572__isl_give isl_local_space *isl_local_space_intersect(
573	__isl_take isl_local_space *ls1, __isl_take isl_local_space *ls2)
574{
575	isl_ctx *ctx;
576	int *exp1 = NULL;
577	int *exp2 = NULL;
578	isl_mat *div;
579
580	if (!ls1 || !ls2)
581		goto error;
582
583	ctx = isl_local_space_get_ctx(ls1);
584	if (!isl_space_is_equal(ls1->dim, ls2->dim))
585		isl_die(ctx, isl_error_invalid,
586			"spaces should be identical", goto error);
587
588	if (ls2->div->n_row == 0) {
589		isl_local_space_free(ls2);
590		return ls1;
591	}
592
593	if (ls1->div->n_row == 0) {
594		isl_local_space_free(ls1);
595		return ls2;
596	}
597
598	exp1 = isl_alloc_array(ctx, int, ls1->div->n_row);
599	exp2 = isl_alloc_array(ctx, int, ls2->div->n_row);
600	if (!exp1 || !exp2)
601		goto error;
602
603	div = isl_merge_divs(ls1->div, ls2->div, exp1, exp2);
604	if (!div)
605		goto error;
606
607	free(exp1);
608	free(exp2);
609	isl_local_space_free(ls2);
610	isl_mat_free(ls1->div);
611	ls1->div = div;
612
613	return ls1;
614error:
615	free(exp1);
616	free(exp2);
617	isl_local_space_free(ls1);
618	isl_local_space_free(ls2);
619	return NULL;
620}
621
622int isl_local_space_divs_known(__isl_keep isl_local_space *ls)
623{
624	int i;
625
626	if (!ls)
627		return -1;
628
629	for (i = 0; i < ls->div->n_row; ++i)
630		if (isl_int_is_zero(ls->div->row[i][0]))
631			return 0;
632
633	return 1;
634}
635
636__isl_give isl_local_space *isl_local_space_domain(
637	__isl_take isl_local_space *ls)
638{
639	ls = isl_local_space_drop_dims(ls, isl_dim_out,
640					0, isl_local_space_dim(ls, isl_dim_out));
641	ls = isl_local_space_cow(ls);
642	if (!ls)
643		return NULL;
644	ls->dim = isl_space_domain(ls->dim);
645	if (!ls->dim)
646		return isl_local_space_free(ls);
647	return ls;
648}
649
650__isl_give isl_local_space *isl_local_space_range(
651	__isl_take isl_local_space *ls)
652{
653	ls = isl_local_space_drop_dims(ls, isl_dim_in,
654					0, isl_local_space_dim(ls, isl_dim_in));
655	ls = isl_local_space_cow(ls);
656	if (!ls)
657		return NULL;
658
659	ls->dim = isl_space_range(ls->dim);
660	if (!ls->dim)
661		return isl_local_space_free(ls);
662	return ls;
663}
664
665/* Construct a local space for a map that has the given local
666 * space as domain and that has a zero-dimensional range.
667 */
668__isl_give isl_local_space *isl_local_space_from_domain(
669	__isl_take isl_local_space *ls)
670{
671	ls = isl_local_space_cow(ls);
672	if (!ls)
673		return NULL;
674	ls->dim = isl_space_from_domain(ls->dim);
675	if (!ls->dim)
676		return isl_local_space_free(ls);
677	return ls;
678}
679
680__isl_give isl_local_space *isl_local_space_add_dims(
681	__isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
682{
683	int pos;
684
685	if (!ls)
686		return NULL;
687	pos = isl_local_space_dim(ls, type);
688	return isl_local_space_insert_dims(ls, type, pos, n);
689}
690
691/* Remove common factor of non-constant terms and denominator.
692 */
693static void normalize_div(__isl_keep isl_local_space *ls, int div)
694{
695	isl_ctx *ctx = ls->div->ctx;
696	unsigned total = ls->div->n_col - 2;
697
698	isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
699	isl_int_gcd(ctx->normalize_gcd,
700		    ctx->normalize_gcd, ls->div->row[div][0]);
701	if (isl_int_is_one(ctx->normalize_gcd))
702		return;
703
704	isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
705			    ctx->normalize_gcd, total);
706	isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
707			    ctx->normalize_gcd);
708	isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
709			    ctx->normalize_gcd);
710}
711
712/* Exploit the equalities in "eq" to simplify the expressions of
713 * the integer divisions in "ls".
714 * The integer divisions in "ls" are assumed to appear as regular
715 * dimensions in "eq".
716 */
717__isl_give isl_local_space *isl_local_space_substitute_equalities(
718	__isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
719{
720	int i, j, k;
721	unsigned total;
722	unsigned n_div;
723
724	if (!ls || !eq)
725		goto error;
726
727	total = isl_space_dim(eq->dim, isl_dim_all);
728	if (isl_local_space_dim(ls, isl_dim_all) != total)
729		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
730			"spaces don't match", goto error);
731	total++;
732	n_div = eq->n_div;
733	for (i = 0; i < eq->n_eq; ++i) {
734		j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
735		if (j < 0 || j == 0 || j >= total)
736			continue;
737
738		for (k = 0; k < ls->div->n_row; ++k) {
739			if (isl_int_is_zero(ls->div->row[k][1 + j]))
740				continue;
741			ls = isl_local_space_cow(ls);
742			if (!ls)
743				goto error;
744			ls->div = isl_mat_cow(ls->div);
745			if (!ls->div)
746				goto error;
747			isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
748					&ls->div->row[k][0]);
749			normalize_div(ls, k);
750		}
751	}
752
753	isl_basic_set_free(eq);
754	return ls;
755error:
756	isl_basic_set_free(eq);
757	isl_local_space_free(ls);
758	return NULL;
759}
760
761/* Plug in the affine expressions "subs" of length "subs_len" (including
762 * the denominator and the constant term) into the variable at position "pos"
763 * of the "n" div expressions starting at "first".
764 *
765 * Let i be the dimension to replace and let "subs" be of the form
766 *
767 *	f/d
768 *
769 * Any integer division starting at "first" with a non-zero coefficient for i,
770 *
771 *	floor((a i + g)/m)
772 *
773 * is replaced by
774 *
775 *	floor((a f + d g)/(m d))
776 */
777__isl_give isl_local_space *isl_local_space_substitute_seq(
778	__isl_take isl_local_space *ls,
779	enum isl_dim_type type, unsigned pos, isl_int *subs, int subs_len,
780	int first, int n)
781{
782	int i;
783	isl_int v;
784
785	if (n == 0)
786		return ls;
787	ls = isl_local_space_cow(ls);
788	if (!ls)
789		return NULL;
790	ls->div = isl_mat_cow(ls->div);
791	if (!ls->div)
792		return isl_local_space_free(ls);
793
794	if (first + n > 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
798	pos += isl_local_space_offset(ls, type);
799
800	isl_int_init(v);
801	for (i = first; i < ls->div->n_row; ++i) {
802		if (isl_int_is_zero(ls->div->row[i][1 + pos]))
803			continue;
804		isl_seq_substitute(ls->div->row[i], pos, subs,
805			ls->div->n_col, subs_len, v);
806		normalize_div(ls, i);
807	}
808	isl_int_clear(v);
809
810	return ls;
811}
812
813/* Plug in "subs" for dimension "type", "pos" in the integer divisions
814 * of "ls".
815 *
816 * Let i be the dimension to replace and let "subs" be of the form
817 *
818 *	f/d
819 *
820 * Any integer division with a non-zero coefficient for i,
821 *
822 *	floor((a i + g)/m)
823 *
824 * is replaced by
825 *
826 *	floor((a f + d g)/(m d))
827 */
828__isl_give isl_local_space *isl_local_space_substitute(
829	__isl_take isl_local_space *ls,
830	enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
831{
832	ls = isl_local_space_cow(ls);
833	if (!ls || !subs)
834		return isl_local_space_free(ls);
835
836	if (!isl_space_is_equal(ls->dim, subs->ls->dim))
837		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
838			"spaces don't match", return isl_local_space_free(ls));
839	if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
840		isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
841			"cannot handle divs yet",
842			return isl_local_space_free(ls));
843
844	return isl_local_space_substitute_seq(ls, type, pos, subs->v->el,
845					    subs->v->size, 0, ls->div->n_row);
846}
847
848int isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
849	enum isl_dim_type type)
850{
851	if (!ls)
852		return -1;
853	return isl_space_is_named_or_nested(ls->dim, type);
854}
855
856__isl_give isl_local_space *isl_local_space_drop_dims(
857	__isl_take isl_local_space *ls,
858	enum isl_dim_type type, unsigned first, unsigned n)
859{
860	isl_ctx *ctx;
861
862	if (!ls)
863		return NULL;
864	if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
865		return ls;
866
867	ctx = isl_local_space_get_ctx(ls);
868	if (first + n > isl_local_space_dim(ls, type))
869		isl_die(ctx, isl_error_invalid, "range out of bounds",
870			return isl_local_space_free(ls));
871
872	ls = isl_local_space_cow(ls);
873	if (!ls)
874		return NULL;
875
876	if (type == isl_dim_div) {
877		ls->div = isl_mat_drop_rows(ls->div, first, n);
878	} else {
879		ls->dim = isl_space_drop_dims(ls->dim, type, first, n);
880		if (!ls->dim)
881			return isl_local_space_free(ls);
882	}
883
884	first += 1 + isl_local_space_offset(ls, type);
885	ls->div = isl_mat_drop_cols(ls->div, first, n);
886	if (!ls->div)
887		return isl_local_space_free(ls);
888
889	return ls;
890}
891
892__isl_give isl_local_space *isl_local_space_insert_dims(
893	__isl_take isl_local_space *ls,
894	enum isl_dim_type type, unsigned first, unsigned n)
895{
896	isl_ctx *ctx;
897
898	if (!ls)
899		return NULL;
900	if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
901		return ls;
902
903	ctx = isl_local_space_get_ctx(ls);
904	if (first > isl_local_space_dim(ls, type))
905		isl_die(ctx, isl_error_invalid, "position out of bounds",
906			return isl_local_space_free(ls));
907
908	ls = isl_local_space_cow(ls);
909	if (!ls)
910		return NULL;
911
912	if (type == isl_dim_div) {
913		ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
914	} else {
915		ls->dim = isl_space_insert_dims(ls->dim, type, first, n);
916		if (!ls->dim)
917			return isl_local_space_free(ls);
918	}
919
920	first += 1 + isl_local_space_offset(ls, type);
921	ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
922	if (!ls->div)
923		return isl_local_space_free(ls);
924
925	return ls;
926}
927
928/* Check if the constraints pointed to by "constraint" is a div
929 * constraint corresponding to div "div" in "ls".
930 *
931 * That is, if div = floor(f/m), then check if the constraint is
932 *
933 *		f - m d >= 0
934 * or
935 *		-(f-(m-1)) + m d >= 0
936 */
937int isl_local_space_is_div_constraint(__isl_keep isl_local_space *ls,
938	isl_int *constraint, unsigned div)
939{
940	unsigned pos;
941
942	if (!ls)
943		return -1;
944
945	if (isl_int_is_zero(ls->div->row[div][0]))
946		return 0;
947
948	pos = isl_local_space_offset(ls, isl_dim_div) + div;
949
950	if (isl_int_eq(constraint[pos], ls->div->row[div][0])) {
951		int neg;
952		isl_int_sub(ls->div->row[div][1],
953				ls->div->row[div][1], ls->div->row[div][0]);
954		isl_int_add_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
955		neg = isl_seq_is_neg(constraint, ls->div->row[div]+1, pos);
956		isl_int_sub_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
957		isl_int_add(ls->div->row[div][1],
958				ls->div->row[div][1], ls->div->row[div][0]);
959		if (!neg)
960			return 0;
961		if (isl_seq_first_non_zero(constraint+pos+1,
962					    ls->div->n_row-div-1) != -1)
963			return 0;
964	} else if (isl_int_abs_eq(constraint[pos], ls->div->row[div][0])) {
965		if (!isl_seq_eq(constraint, ls->div->row[div]+1, pos))
966			return 0;
967		if (isl_seq_first_non_zero(constraint+pos+1,
968					    ls->div->n_row-div-1) != -1)
969			return 0;
970	} else
971		return 0;
972
973	return 1;
974}
975
976/*
977 * Set active[i] to 1 if the dimension at position i is involved
978 * in the linear expression l.
979 */
980int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
981{
982	int i, j;
983	isl_ctx *ctx;
984	int *active = NULL;
985	unsigned total;
986	unsigned offset;
987
988	ctx = isl_local_space_get_ctx(ls);
989	total = isl_local_space_dim(ls, isl_dim_all);
990	active = isl_calloc_array(ctx, int, total);
991	if (total && !active)
992		return NULL;
993
994	for (i = 0; i < total; ++i)
995		active[i] = !isl_int_is_zero(l[i]);
996
997	offset = isl_local_space_offset(ls, isl_dim_div) - 1;
998	for (i = ls->div->n_row - 1; i >= 0; --i) {
999		if (!active[offset + i])
1000			continue;
1001		for (j = 0; j < total; ++j)
1002			active[j] |= !isl_int_is_zero(ls->div->row[i][2 + j]);
1003	}
1004
1005	return active;
1006}
1007
1008/* Given a local space "ls" of a set, create a local space
1009 * for the lift of the set.  In particular, the result
1010 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
1011 * range of the wrapped map.
1012 */
1013__isl_give isl_local_space *isl_local_space_lift(
1014	__isl_take isl_local_space *ls)
1015{
1016	ls = isl_local_space_cow(ls);
1017	if (!ls)
1018		return NULL;
1019
1020	ls->dim = isl_space_lift(ls->dim, ls->div->n_row);
1021	ls->div = isl_mat_drop_rows(ls->div, 0, ls->div->n_row);
1022	if (!ls->dim || !ls->div)
1023		return isl_local_space_free(ls);
1024
1025	return ls;
1026}
1027
1028/* Construct a basic map that maps a set living in local space "ls"
1029 * to the corresponding lifted local space.
1030 */
1031__isl_give isl_basic_map *isl_local_space_lifting(
1032	__isl_take isl_local_space *ls)
1033{
1034	isl_basic_map *lifting;
1035	isl_basic_set *bset;
1036
1037	if (!ls)
1038		return NULL;
1039	if (!isl_local_space_is_set(ls))
1040		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1041			"lifting only defined on set spaces",
1042			return isl_local_space_free(ls));
1043
1044	bset = isl_basic_set_from_local_space(ls);
1045	lifting = isl_basic_set_unwrap(isl_basic_set_lift(bset));
1046	lifting = isl_basic_map_domain_map(lifting);
1047	lifting = isl_basic_map_reverse(lifting);
1048
1049	return lifting;
1050}
1051
1052/* Compute the preimage of "ls" under the function represented by "ma".
1053 * In other words, plug in "ma" in "ls".  The result is a local space
1054 * that is part of the domain space of "ma".
1055 *
1056 * If the divs in "ls" are represented as
1057 *
1058 *	floor((a_i(p) + b_i x + c_i(divs))/n_i)
1059 *
1060 * and ma is represented by
1061 *
1062 *	x = D(p) + F(y) + G(divs')
1063 *
1064 * then the resulting divs are
1065 *
1066 *	floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
1067 *
1068 * We first copy over the divs from "ma" and then
1069 * we add the modified divs from "ls".
1070 */
1071__isl_give isl_local_space *isl_local_space_preimage_multi_aff(
1072	__isl_take isl_local_space *ls, __isl_take isl_multi_aff *ma)
1073{
1074	int i;
1075	isl_space *space;
1076	isl_local_space *res = NULL;
1077	int n_div_ls, n_div_ma;
1078	isl_int f, c1, c2, g;
1079
1080	ma = isl_multi_aff_align_divs(ma);
1081	if (!ls || !ma)
1082		goto error;
1083	if (!isl_space_is_range_internal(ls->dim, ma->space))
1084		isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1085			"spaces don't match", goto error);
1086
1087	n_div_ls = isl_local_space_dim(ls, isl_dim_div);
1088	n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
1089
1090	space = isl_space_domain(isl_multi_aff_get_space(ma));
1091	res = isl_local_space_alloc(space, n_div_ma + n_div_ls);
1092	if (!res)
1093		goto error;
1094
1095	if (n_div_ma) {
1096		isl_mat_free(res->div);
1097		res->div = isl_mat_copy(ma->p[0]->ls->div);
1098		res->div = isl_mat_add_zero_cols(res->div, n_div_ls);
1099		res->div = isl_mat_add_rows(res->div, n_div_ls);
1100		if (!res->div)
1101			goto error;
1102	}
1103
1104	isl_int_init(f);
1105	isl_int_init(c1);
1106	isl_int_init(c2);
1107	isl_int_init(g);
1108
1109	for (i = 0; i < ls->div->n_row; ++i) {
1110		if (isl_int_is_zero(ls->div->row[i][0])) {
1111			isl_int_set_si(res->div->row[n_div_ma + i][0], 0);
1112			continue;
1113		}
1114		isl_seq_preimage(res->div->row[n_div_ma + i], ls->div->row[i],
1115				ma, 0, 0, n_div_ma, n_div_ls, f, c1, c2, g, 1);
1116		normalize_div(res, n_div_ma + i);
1117	}
1118
1119	isl_int_clear(f);
1120	isl_int_clear(c1);
1121	isl_int_clear(c2);
1122	isl_int_clear(g);
1123
1124	isl_local_space_free(ls);
1125	isl_multi_aff_free(ma);
1126	return res;
1127error:
1128	isl_local_space_free(ls);
1129	isl_multi_aff_free(ma);
1130	isl_local_space_free(res);
1131	return NULL;
1132}
1133