2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
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_vec_private.h>
22 isl_ctx
*isl_local_space_get_ctx(__isl_keep isl_local_space
*ls
)
24 return ls
? ls
->dim
->ctx
: NULL
;
27 __isl_give isl_local_space
*isl_local_space_alloc_div(__isl_take isl_space
*dim
,
28 __isl_take isl_mat
*div
)
31 isl_local_space
*ls
= NULL
;
36 ctx
= isl_space_get_ctx(dim
);
37 ls
= isl_calloc_type(ctx
, struct isl_local_space
);
49 isl_local_space_free(ls
);
53 __isl_give isl_local_space
*isl_local_space_alloc(__isl_take isl_space
*dim
,
63 total
= isl_space_dim(dim
, isl_dim_all
);
65 ctx
= isl_space_get_ctx(dim
);
66 div
= isl_mat_alloc(ctx
, n_div
, 1 + 1 + total
+ n_div
);
67 return isl_local_space_alloc_div(dim
, div
);
70 __isl_give isl_local_space
*isl_local_space_from_space(__isl_take isl_space
*dim
)
72 return isl_local_space_alloc(dim
, 0);
75 __isl_give isl_local_space
*isl_local_space_copy(__isl_keep isl_local_space
*ls
)
84 __isl_give isl_local_space
*isl_local_space_dup(__isl_keep isl_local_space
*ls
)
89 return isl_local_space_alloc_div(isl_space_copy(ls
->dim
),
90 isl_mat_copy(ls
->div
));
94 __isl_give isl_local_space
*isl_local_space_cow(__isl_take isl_local_space
*ls
)
102 return isl_local_space_dup(ls
);
105 __isl_null isl_local_space
*isl_local_space_free(
106 __isl_take isl_local_space
*ls
)
114 isl_space_free(ls
->dim
);
115 isl_mat_free(ls
->div
);
122 /* Is the local space that of a parameter domain?
124 int isl_local_space_is_params(__isl_keep isl_local_space
*ls
)
128 return isl_space_is_params(ls
->dim
);
131 /* Is the local space that of a set?
133 int isl_local_space_is_set(__isl_keep isl_local_space
*ls
)
135 return ls
? isl_space_is_set(ls
->dim
) : -1;
138 /* Return true if the two local spaces are identical, with identical
139 * expressions for the integer divisions.
141 int isl_local_space_is_equal(__isl_keep isl_local_space
*ls1
,
142 __isl_keep isl_local_space
*ls2
)
149 equal
= isl_space_is_equal(ls1
->dim
, ls2
->dim
);
150 if (equal
< 0 || !equal
)
153 if (!isl_local_space_divs_known(ls1
))
155 if (!isl_local_space_divs_known(ls2
))
158 return isl_mat_is_equal(ls1
->div
, ls2
->div
);
161 /* Compare two isl_local_spaces.
163 * Return -1 if "ls1" is "smaller" than "ls2", 1 if "ls1" is "greater"
164 * than "ls2" and 0 if they are equal.
166 * The order is fairly arbitrary. We do "prefer" divs that only involve
167 * earlier dimensions in the sense that we consider local spaces where
168 * the first differing div involves earlier dimensions to be smaller.
170 int isl_local_space_cmp(__isl_keep isl_local_space
*ls1
,
171 __isl_keep isl_local_space
*ls2
)
186 cmp
= isl_space_cmp(ls1
->dim
, ls2
->dim
);
190 if (ls1
->div
->n_row
!= ls2
->div
->n_row
)
191 return ls1
->div
->n_row
- ls2
->div
->n_row
;
193 n_col
= isl_mat_cols(ls1
->div
);
194 for (i
= 0; i
< ls1
->div
->n_row
; ++i
) {
195 known1
= isl_local_space_div_is_known(ls1
, i
);
196 known2
= isl_local_space_div_is_known(ls2
, i
);
197 if (!known1
&& !known2
)
203 last1
= isl_seq_last_non_zero(ls1
->div
->row
[i
] + 1, n_col
- 1);
204 last2
= isl_seq_last_non_zero(ls2
->div
->row
[i
] + 1, n_col
- 1);
206 return last1
- last2
;
207 cmp
= isl_seq_cmp(ls1
->div
->row
[i
], ls2
->div
->row
[i
], n_col
);
215 int isl_local_space_dim(__isl_keep isl_local_space
*ls
,
216 enum isl_dim_type type
)
220 if (type
== isl_dim_div
)
221 return ls
->div
->n_row
;
222 if (type
== isl_dim_all
)
223 return isl_space_dim(ls
->dim
, isl_dim_all
) + ls
->div
->n_row
;
224 return isl_space_dim(ls
->dim
, type
);
227 unsigned isl_local_space_offset(__isl_keep isl_local_space
*ls
,
228 enum isl_dim_type type
)
237 case isl_dim_cst
: return 0;
238 case isl_dim_param
: return 1;
239 case isl_dim_in
: return 1 + dim
->nparam
;
240 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
241 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
246 /* Does the given dimension have a name?
248 int isl_local_space_has_dim_name(__isl_keep isl_local_space
*ls
,
249 enum isl_dim_type type
, unsigned pos
)
251 return ls
? isl_space_has_dim_name(ls
->dim
, type
, pos
) : -1;
254 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space
*ls
,
255 enum isl_dim_type type
, unsigned pos
)
257 return ls
? isl_space_get_dim_name(ls
->dim
, type
, pos
) : NULL
;
260 int isl_local_space_has_dim_id(__isl_keep isl_local_space
*ls
,
261 enum isl_dim_type type
, unsigned pos
)
263 return ls
? isl_space_has_dim_id(ls
->dim
, type
, pos
) : -1;
266 __isl_give isl_id
*isl_local_space_get_dim_id(__isl_keep isl_local_space
*ls
,
267 enum isl_dim_type type
, unsigned pos
)
269 return ls
? isl_space_get_dim_id(ls
->dim
, type
, pos
) : NULL
;
272 __isl_give isl_aff
*isl_local_space_get_div(__isl_keep isl_local_space
*ls
,
280 if (pos
< 0 || pos
>= ls
->div
->n_row
)
281 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
282 "index out of bounds", return NULL
);
284 if (isl_int_is_zero(ls
->div
->row
[pos
][0]))
285 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
286 "expression of div unknown", return NULL
);
287 if (!isl_local_space_is_set(ls
))
288 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
289 "cannot represent divs of map spaces", return NULL
);
291 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
294 isl_seq_cpy(aff
->v
->el
, ls
->div
->row
[pos
], aff
->v
->size
);
298 __isl_give isl_space
*isl_local_space_get_space(__isl_keep isl_local_space
*ls
)
303 return isl_space_copy(ls
->dim
);
306 /* Replace the identifier of the tuple of type "type" by "id".
308 __isl_give isl_local_space
*isl_local_space_set_tuple_id(
309 __isl_take isl_local_space
*ls
,
310 enum isl_dim_type type
, __isl_take isl_id
*id
)
312 ls
= isl_local_space_cow(ls
);
315 ls
->dim
= isl_space_set_tuple_id(ls
->dim
, type
, id
);
317 return isl_local_space_free(ls
);
324 __isl_give isl_local_space
*isl_local_space_set_dim_name(
325 __isl_take isl_local_space
*ls
,
326 enum isl_dim_type type
, unsigned pos
, const char *s
)
328 ls
= isl_local_space_cow(ls
);
331 ls
->dim
= isl_space_set_dim_name(ls
->dim
, type
, pos
, s
);
333 return isl_local_space_free(ls
);
338 __isl_give isl_local_space
*isl_local_space_set_dim_id(
339 __isl_take isl_local_space
*ls
,
340 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
342 ls
= isl_local_space_cow(ls
);
345 ls
->dim
= isl_space_set_dim_id(ls
->dim
, type
, pos
, id
);
347 return isl_local_space_free(ls
);
355 __isl_give isl_local_space
*isl_local_space_reset_space(
356 __isl_take isl_local_space
*ls
, __isl_take isl_space
*dim
)
358 ls
= isl_local_space_cow(ls
);
362 isl_space_free(ls
->dim
);
367 isl_local_space_free(ls
);
372 /* Reorder the columns of the given div definitions according to the
374 * The order of the divs themselves is assumed not to change.
376 static __isl_give isl_mat
*reorder_divs(__isl_take isl_mat
*div
,
377 __isl_take isl_reordering
*r
)
386 extra
= isl_space_dim(r
->dim
, isl_dim_all
) + div
->n_row
- r
->len
;
387 mat
= isl_mat_alloc(div
->ctx
, div
->n_row
, div
->n_col
+ extra
);
391 for (i
= 0; i
< div
->n_row
; ++i
) {
392 isl_seq_cpy(mat
->row
[i
], div
->row
[i
], 2);
393 isl_seq_clr(mat
->row
[i
] + 2, mat
->n_col
- 2);
394 for (j
= 0; j
< r
->len
; ++j
)
395 isl_int_set(mat
->row
[i
][2 + r
->pos
[j
]],
399 isl_reordering_free(r
);
403 isl_reordering_free(r
);
408 /* Reorder the dimensions of "ls" according to the given reordering.
409 * The reordering r is assumed to have been extended with the local
410 * variables, leaving them in the same order.
412 __isl_give isl_local_space
*isl_local_space_realign(
413 __isl_take isl_local_space
*ls
, __isl_take isl_reordering
*r
)
415 ls
= isl_local_space_cow(ls
);
419 ls
->div
= reorder_divs(ls
->div
, isl_reordering_copy(r
));
423 ls
= isl_local_space_reset_space(ls
, isl_space_copy(r
->dim
));
425 isl_reordering_free(r
);
428 isl_local_space_free(ls
);
429 isl_reordering_free(r
);
433 __isl_give isl_local_space
*isl_local_space_add_div(
434 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*div
)
436 ls
= isl_local_space_cow(ls
);
440 if (ls
->div
->n_col
!= div
->size
)
441 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
442 "incompatible dimensions", goto error
);
444 ls
->div
= isl_mat_add_zero_cols(ls
->div
, 1);
445 ls
->div
= isl_mat_add_rows(ls
->div
, 1);
449 isl_seq_cpy(ls
->div
->row
[ls
->div
->n_row
- 1], div
->el
, div
->size
);
450 isl_int_set_si(ls
->div
->row
[ls
->div
->n_row
- 1][div
->size
], 0);
455 isl_local_space_free(ls
);
460 __isl_give isl_local_space
*isl_local_space_replace_divs(
461 __isl_take isl_local_space
*ls
, __isl_take isl_mat
*div
)
463 ls
= isl_local_space_cow(ls
);
468 isl_mat_free(ls
->div
);
473 isl_local_space_free(ls
);
477 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
480 static void expand_row(__isl_keep isl_mat
*dst
, int d
,
481 __isl_keep isl_mat
*src
, int s
, int *exp
)
484 unsigned c
= src
->n_col
- src
->n_row
;
486 isl_seq_cpy(dst
->row
[d
], src
->row
[s
], c
);
487 isl_seq_clr(dst
->row
[d
] + c
, dst
->n_col
- c
);
489 for (i
= 0; i
< s
; ++i
)
490 isl_int_set(dst
->row
[d
][c
+ exp
[i
]], src
->row
[s
][c
+ i
]);
493 /* Compare (known) divs.
494 * Return non-zero if at least one of the two divs is unknown.
495 * In particular, if both divs are unknown, we respect their
496 * current order. Otherwise, we sort the known div after the unknown
497 * div only if the known div depends on the unknown div.
499 static int cmp_row(isl_int
*row_i
, isl_int
*row_j
, int i
, int j
,
500 unsigned n_row
, unsigned n_col
)
503 int unknown_i
, unknown_j
;
505 unknown_i
= isl_int_is_zero(row_i
[0]);
506 unknown_j
= isl_int_is_zero(row_j
[0]);
508 if (unknown_i
&& unknown_j
)
512 li
= n_col
- n_row
+ i
;
514 li
= isl_seq_last_non_zero(row_i
, n_col
);
516 lj
= n_col
- n_row
+ j
;
518 lj
= isl_seq_last_non_zero(row_j
, n_col
);
523 return isl_seq_cmp(row_i
, row_j
, n_col
);
526 /* Call cmp_row for divs in a matrix.
528 int isl_mat_cmp_div(__isl_keep isl_mat
*div
, int i
, int j
)
530 return cmp_row(div
->row
[i
], div
->row
[j
], i
, j
, div
->n_row
, div
->n_col
);
533 /* Call cmp_row for divs in a basic map.
535 static int bmap_cmp_row(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
538 return cmp_row(bmap
->div
[i
], bmap
->div
[j
], i
, j
, bmap
->n_div
, total
);
541 /* Sort the divs in "bmap".
543 * We first make sure divs are placed after divs on which they depend.
544 * Then we perform a simple insertion sort based on the same ordering
545 * that is used in isl_merge_divs.
547 __isl_give isl_basic_map
*isl_basic_map_sort_divs(
548 __isl_take isl_basic_map
*bmap
)
553 bmap
= isl_basic_map_order_divs(bmap
);
556 if (bmap
->n_div
<= 1)
559 total
= 2 + isl_basic_map_total_dim(bmap
);
560 for (i
= 1; i
< bmap
->n_div
; ++i
) {
561 for (j
= i
- 1; j
>= 0; --j
) {
562 if (bmap_cmp_row(bmap
, j
, j
+ 1, total
) <= 0)
564 isl_basic_map_swap_div(bmap
, j
, j
+ 1);
571 /* Sort the divs in the basic maps of "map".
573 __isl_give isl_map
*isl_map_sort_divs(__isl_take isl_map
*map
)
575 return isl_map_inline_foreach_basic_map(map
, &isl_basic_map_sort_divs
);
578 /* Combine the two lists of divs into a single list.
579 * For each row i in div1, exp1[i] is set to the position of the corresponding
580 * row in the result. Similarly for div2 and exp2.
581 * This function guarantees
583 * exp1[i+1] > exp1[i]
584 * For optimal merging, the two input list should have been sorted.
586 __isl_give isl_mat
*isl_merge_divs(__isl_keep isl_mat
*div1
,
587 __isl_keep isl_mat
*div2
, int *exp1
, int *exp2
)
596 d
= div1
->n_col
- div1
->n_row
;
597 div
= isl_mat_alloc(div1
->ctx
, 1 + div1
->n_row
+ div2
->n_row
,
598 d
+ div1
->n_row
+ div2
->n_row
);
602 for (i
= 0, j
= 0, k
= 0; i
< div1
->n_row
&& j
< div2
->n_row
; ++k
) {
605 expand_row(div
, k
, div1
, i
, exp1
);
606 expand_row(div
, k
+ 1, div2
, j
, exp2
);
608 cmp
= isl_mat_cmp_div(div
, k
, k
+ 1);
612 } else if (cmp
< 0) {
616 isl_seq_cpy(div
->row
[k
], div
->row
[k
+ 1], div
->n_col
);
619 for (; i
< div1
->n_row
; ++i
, ++k
) {
620 expand_row(div
, k
, div1
, i
, exp1
);
623 for (; j
< div2
->n_row
; ++j
, ++k
) {
624 expand_row(div
, k
, div2
, j
, exp2
);
634 /* Swap divs "a" and "b" in "ls".
636 __isl_give isl_local_space
*isl_local_space_swap_div(
637 __isl_take isl_local_space
*ls
, int a
, int b
)
641 ls
= isl_local_space_cow(ls
);
644 if (a
< 0 || a
>= ls
->div
->n_row
|| b
< 0 || b
>= ls
->div
->n_row
)
645 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
646 "index out of bounds", return isl_local_space_free(ls
));
647 offset
= ls
->div
->n_col
- ls
->div
->n_row
;
648 ls
->div
= isl_mat_swap_cols(ls
->div
, offset
+ a
, offset
+ b
);
649 ls
->div
= isl_mat_swap_rows(ls
->div
, a
, b
);
651 return isl_local_space_free(ls
);
655 /* Construct a local space that contains all the divs in either
658 __isl_give isl_local_space
*isl_local_space_intersect(
659 __isl_take isl_local_space
*ls1
, __isl_take isl_local_space
*ls2
)
670 ctx
= isl_local_space_get_ctx(ls1
);
671 if (!isl_space_is_equal(ls1
->dim
, ls2
->dim
))
672 isl_die(ctx
, isl_error_invalid
,
673 "spaces should be identical", goto error
);
675 if (ls2
->div
->n_row
== 0) {
676 isl_local_space_free(ls2
);
680 if (ls1
->div
->n_row
== 0) {
681 isl_local_space_free(ls1
);
685 exp1
= isl_alloc_array(ctx
, int, ls1
->div
->n_row
);
686 exp2
= isl_alloc_array(ctx
, int, ls2
->div
->n_row
);
690 div
= isl_merge_divs(ls1
->div
, ls2
->div
, exp1
, exp2
);
694 equal
= isl_mat_is_equal(ls1
->div
, div
);
698 ls1
= isl_local_space_cow(ls1
);
704 isl_local_space_free(ls2
);
705 isl_mat_free(ls1
->div
);
712 isl_local_space_free(ls1
);
713 isl_local_space_free(ls2
);
717 /* Does "ls" have an explicit representation for div "div"?
719 int isl_local_space_div_is_known(__isl_keep isl_local_space
*ls
, int div
)
723 if (div
< 0 || div
>= ls
->div
->n_row
)
724 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
725 "position out of bounds", return -1);
726 return !isl_int_is_zero(ls
->div
->row
[div
][0]);
729 int isl_local_space_divs_known(__isl_keep isl_local_space
*ls
)
736 for (i
= 0; i
< ls
->div
->n_row
; ++i
)
737 if (isl_int_is_zero(ls
->div
->row
[i
][0]))
743 __isl_give isl_local_space
*isl_local_space_domain(
744 __isl_take isl_local_space
*ls
)
746 ls
= isl_local_space_drop_dims(ls
, isl_dim_out
,
747 0, isl_local_space_dim(ls
, isl_dim_out
));
748 ls
= isl_local_space_cow(ls
);
751 ls
->dim
= isl_space_domain(ls
->dim
);
753 return isl_local_space_free(ls
);
757 __isl_give isl_local_space
*isl_local_space_range(
758 __isl_take isl_local_space
*ls
)
760 ls
= isl_local_space_drop_dims(ls
, isl_dim_in
,
761 0, isl_local_space_dim(ls
, isl_dim_in
));
762 ls
= isl_local_space_cow(ls
);
766 ls
->dim
= isl_space_range(ls
->dim
);
768 return isl_local_space_free(ls
);
772 /* Construct a local space for a map that has the given local
773 * space as domain and that has a zero-dimensional range.
775 __isl_give isl_local_space
*isl_local_space_from_domain(
776 __isl_take isl_local_space
*ls
)
778 ls
= isl_local_space_cow(ls
);
781 ls
->dim
= isl_space_from_domain(ls
->dim
);
783 return isl_local_space_free(ls
);
787 __isl_give isl_local_space
*isl_local_space_add_dims(
788 __isl_take isl_local_space
*ls
, enum isl_dim_type type
, unsigned n
)
794 pos
= isl_local_space_dim(ls
, type
);
795 return isl_local_space_insert_dims(ls
, type
, pos
, n
);
798 /* Remove common factor of non-constant terms and denominator.
800 static void normalize_div(__isl_keep isl_local_space
*ls
, int div
)
802 isl_ctx
*ctx
= ls
->div
->ctx
;
803 unsigned total
= ls
->div
->n_col
- 2;
805 isl_seq_gcd(ls
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
806 isl_int_gcd(ctx
->normalize_gcd
,
807 ctx
->normalize_gcd
, ls
->div
->row
[div
][0]);
808 if (isl_int_is_one(ctx
->normalize_gcd
))
811 isl_seq_scale_down(ls
->div
->row
[div
] + 2, ls
->div
->row
[div
] + 2,
812 ctx
->normalize_gcd
, total
);
813 isl_int_divexact(ls
->div
->row
[div
][0], ls
->div
->row
[div
][0],
815 isl_int_fdiv_q(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1],
819 /* Exploit the equalities in "eq" to simplify the expressions of
820 * the integer divisions in "ls".
821 * The integer divisions in "ls" are assumed to appear as regular
822 * dimensions in "eq".
824 __isl_give isl_local_space
*isl_local_space_substitute_equalities(
825 __isl_take isl_local_space
*ls
, __isl_take isl_basic_set
*eq
)
834 total
= isl_space_dim(eq
->dim
, isl_dim_all
);
835 if (isl_local_space_dim(ls
, isl_dim_all
) != total
)
836 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
837 "spaces don't match", goto error
);
840 for (i
= 0; i
< eq
->n_eq
; ++i
) {
841 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
842 if (j
< 0 || j
== 0 || j
>= total
)
845 for (k
= 0; k
< ls
->div
->n_row
; ++k
) {
846 if (isl_int_is_zero(ls
->div
->row
[k
][1 + j
]))
848 ls
= isl_local_space_cow(ls
);
851 ls
->div
= isl_mat_cow(ls
->div
);
854 isl_seq_elim(ls
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
855 &ls
->div
->row
[k
][0]);
856 normalize_div(ls
, k
);
860 isl_basic_set_free(eq
);
863 isl_basic_set_free(eq
);
864 isl_local_space_free(ls
);
868 /* Plug in the affine expressions "subs" of length "subs_len" (including
869 * the denominator and the constant term) into the variable at position "pos"
870 * of the "n" div expressions starting at "first".
872 * Let i be the dimension to replace and let "subs" be of the form
876 * Any integer division starting at "first" with a non-zero coefficient for i,
882 * floor((a f + d g)/(m d))
884 __isl_give isl_local_space
*isl_local_space_substitute_seq(
885 __isl_take isl_local_space
*ls
,
886 enum isl_dim_type type
, unsigned pos
, isl_int
*subs
, int subs_len
,
894 ls
= isl_local_space_cow(ls
);
897 ls
->div
= isl_mat_cow(ls
->div
);
899 return isl_local_space_free(ls
);
901 if (first
+ n
> ls
->div
->n_row
)
902 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
903 "index out of bounds", return isl_local_space_free(ls
));
905 pos
+= isl_local_space_offset(ls
, type
);
908 for (i
= first
; i
< ls
->div
->n_row
; ++i
) {
909 if (isl_int_is_zero(ls
->div
->row
[i
][1 + pos
]))
911 isl_seq_substitute(ls
->div
->row
[i
], pos
, subs
,
912 ls
->div
->n_col
, subs_len
, v
);
913 normalize_div(ls
, i
);
920 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
923 * Let i be the dimension to replace and let "subs" be of the form
927 * Any integer division with a non-zero coefficient for i,
933 * floor((a f + d g)/(m d))
935 __isl_give isl_local_space
*isl_local_space_substitute(
936 __isl_take isl_local_space
*ls
,
937 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
939 ls
= isl_local_space_cow(ls
);
941 return isl_local_space_free(ls
);
943 if (!isl_space_is_equal(ls
->dim
, subs
->ls
->dim
))
944 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
945 "spaces don't match", return isl_local_space_free(ls
));
946 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
947 isl_die(isl_local_space_get_ctx(ls
), isl_error_unsupported
,
948 "cannot handle divs yet",
949 return isl_local_space_free(ls
));
951 return isl_local_space_substitute_seq(ls
, type
, pos
, subs
->v
->el
,
952 subs
->v
->size
, 0, ls
->div
->n_row
);
955 int isl_local_space_is_named_or_nested(__isl_keep isl_local_space
*ls
,
956 enum isl_dim_type type
)
960 return isl_space_is_named_or_nested(ls
->dim
, type
);
963 __isl_give isl_local_space
*isl_local_space_drop_dims(
964 __isl_take isl_local_space
*ls
,
965 enum isl_dim_type type
, unsigned first
, unsigned n
)
971 if (n
== 0 && !isl_local_space_is_named_or_nested(ls
, type
))
974 ctx
= isl_local_space_get_ctx(ls
);
975 if (first
+ n
> isl_local_space_dim(ls
, type
))
976 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
977 return isl_local_space_free(ls
));
979 ls
= isl_local_space_cow(ls
);
983 if (type
== isl_dim_div
) {
984 ls
->div
= isl_mat_drop_rows(ls
->div
, first
, n
);
986 ls
->dim
= isl_space_drop_dims(ls
->dim
, type
, first
, n
);
988 return isl_local_space_free(ls
);
991 first
+= 1 + isl_local_space_offset(ls
, type
);
992 ls
->div
= isl_mat_drop_cols(ls
->div
, first
, n
);
994 return isl_local_space_free(ls
);
999 __isl_give isl_local_space
*isl_local_space_insert_dims(
1000 __isl_take isl_local_space
*ls
,
1001 enum isl_dim_type type
, unsigned first
, unsigned n
)
1007 if (n
== 0 && !isl_local_space_is_named_or_nested(ls
, type
))
1010 ctx
= isl_local_space_get_ctx(ls
);
1011 if (first
> isl_local_space_dim(ls
, type
))
1012 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
1013 return isl_local_space_free(ls
));
1015 ls
= isl_local_space_cow(ls
);
1019 if (type
== isl_dim_div
) {
1020 ls
->div
= isl_mat_insert_zero_rows(ls
->div
, first
, n
);
1022 ls
->dim
= isl_space_insert_dims(ls
->dim
, type
, first
, n
);
1024 return isl_local_space_free(ls
);
1027 first
+= 1 + isl_local_space_offset(ls
, type
);
1028 ls
->div
= isl_mat_insert_zero_cols(ls
->div
, first
, n
);
1030 return isl_local_space_free(ls
);
1035 /* Check if the constraints pointed to by "constraint" is a div
1036 * constraint corresponding to div "div" in "ls".
1038 * That is, if div = floor(f/m), then check if the constraint is
1042 * -(f-(m-1)) + m d >= 0
1044 int isl_local_space_is_div_constraint(__isl_keep isl_local_space
*ls
,
1045 isl_int
*constraint
, unsigned div
)
1052 if (isl_int_is_zero(ls
->div
->row
[div
][0]))
1055 pos
= isl_local_space_offset(ls
, isl_dim_div
) + div
;
1057 if (isl_int_eq(constraint
[pos
], ls
->div
->row
[div
][0])) {
1059 isl_int_sub(ls
->div
->row
[div
][1],
1060 ls
->div
->row
[div
][1], ls
->div
->row
[div
][0]);
1061 isl_int_add_ui(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1], 1);
1062 neg
= isl_seq_is_neg(constraint
, ls
->div
->row
[div
]+1, pos
);
1063 isl_int_sub_ui(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1], 1);
1064 isl_int_add(ls
->div
->row
[div
][1],
1065 ls
->div
->row
[div
][1], ls
->div
->row
[div
][0]);
1068 if (isl_seq_first_non_zero(constraint
+pos
+1,
1069 ls
->div
->n_row
-div
-1) != -1)
1071 } else if (isl_int_abs_eq(constraint
[pos
], ls
->div
->row
[div
][0])) {
1072 if (!isl_seq_eq(constraint
, ls
->div
->row
[div
]+1, pos
))
1074 if (isl_seq_first_non_zero(constraint
+pos
+1,
1075 ls
->div
->n_row
-div
-1) != -1)
1084 * Set active[i] to 1 if the dimension at position i is involved
1085 * in the linear expression l.
1087 int *isl_local_space_get_active(__isl_keep isl_local_space
*ls
, isl_int
*l
)
1095 ctx
= isl_local_space_get_ctx(ls
);
1096 total
= isl_local_space_dim(ls
, isl_dim_all
);
1097 active
= isl_calloc_array(ctx
, int, total
);
1098 if (total
&& !active
)
1101 for (i
= 0; i
< total
; ++i
)
1102 active
[i
] = !isl_int_is_zero(l
[i
]);
1104 offset
= isl_local_space_offset(ls
, isl_dim_div
) - 1;
1105 for (i
= ls
->div
->n_row
- 1; i
>= 0; --i
) {
1106 if (!active
[offset
+ i
])
1108 for (j
= 0; j
< total
; ++j
)
1109 active
[j
] |= !isl_int_is_zero(ls
->div
->row
[i
][2 + j
]);
1115 /* Given a local space "ls" of a set, create a local space
1116 * for the lift of the set. In particular, the result
1117 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
1118 * range of the wrapped map.
1120 __isl_give isl_local_space
*isl_local_space_lift(
1121 __isl_take isl_local_space
*ls
)
1123 ls
= isl_local_space_cow(ls
);
1127 ls
->dim
= isl_space_lift(ls
->dim
, ls
->div
->n_row
);
1128 ls
->div
= isl_mat_drop_rows(ls
->div
, 0, ls
->div
->n_row
);
1129 if (!ls
->dim
|| !ls
->div
)
1130 return isl_local_space_free(ls
);
1135 /* Construct a basic map that maps a set living in local space "ls"
1136 * to the corresponding lifted local space.
1138 __isl_give isl_basic_map
*isl_local_space_lifting(
1139 __isl_take isl_local_space
*ls
)
1141 isl_basic_map
*lifting
;
1142 isl_basic_set
*bset
;
1146 if (!isl_local_space_is_set(ls
))
1147 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1148 "lifting only defined on set spaces", goto error
);
1150 bset
= isl_basic_set_from_local_space(ls
);
1151 lifting
= isl_basic_set_unwrap(isl_basic_set_lift(bset
));
1152 lifting
= isl_basic_map_domain_map(lifting
);
1153 lifting
= isl_basic_map_reverse(lifting
);
1157 isl_local_space_free(ls
);
1161 /* Compute the preimage of "ls" under the function represented by "ma".
1162 * In other words, plug in "ma" in "ls". The result is a local space
1163 * that is part of the domain space of "ma".
1165 * If the divs in "ls" are represented as
1167 * floor((a_i(p) + b_i x + c_i(divs))/n_i)
1169 * and ma is represented by
1171 * x = D(p) + F(y) + G(divs')
1173 * then the resulting divs are
1175 * floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
1177 * We first copy over the divs from "ma" and then
1178 * we add the modified divs from "ls".
1180 __isl_give isl_local_space
*isl_local_space_preimage_multi_aff(
1181 __isl_take isl_local_space
*ls
, __isl_take isl_multi_aff
*ma
)
1185 isl_local_space
*res
= NULL
;
1186 int n_div_ls
, n_div_ma
;
1187 isl_int f
, c1
, c2
, g
;
1189 ma
= isl_multi_aff_align_divs(ma
);
1192 if (!isl_space_is_range_internal(ls
->dim
, ma
->space
))
1193 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1194 "spaces don't match", goto error
);
1196 n_div_ls
= isl_local_space_dim(ls
, isl_dim_div
);
1197 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
1199 space
= isl_space_domain(isl_multi_aff_get_space(ma
));
1200 res
= isl_local_space_alloc(space
, n_div_ma
+ n_div_ls
);
1205 isl_mat_free(res
->div
);
1206 res
->div
= isl_mat_copy(ma
->p
[0]->ls
->div
);
1207 res
->div
= isl_mat_add_zero_cols(res
->div
, n_div_ls
);
1208 res
->div
= isl_mat_add_rows(res
->div
, n_div_ls
);
1218 for (i
= 0; i
< ls
->div
->n_row
; ++i
) {
1219 if (isl_int_is_zero(ls
->div
->row
[i
][0])) {
1220 isl_int_set_si(res
->div
->row
[n_div_ma
+ i
][0], 0);
1223 isl_seq_preimage(res
->div
->row
[n_div_ma
+ i
], ls
->div
->row
[i
],
1224 ma
, 0, 0, n_div_ma
, n_div_ls
, f
, c1
, c2
, g
, 1);
1225 normalize_div(res
, n_div_ma
+ i
);
1233 isl_local_space_free(ls
);
1234 isl_multi_aff_free(ma
);
1237 isl_local_space_free(ls
);
1238 isl_multi_aff_free(ma
);
1239 isl_local_space_free(res
);
1243 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "ls"
1244 * to dimensions of "dst_type" at "dst_pos".
1246 * Moving to/from local dimensions is not allowed.
1247 * We currently assume that the dimension type changes.
1249 __isl_give isl_local_space
*isl_local_space_move_dims(
1250 __isl_take isl_local_space
*ls
,
1251 enum isl_dim_type dst_type
, unsigned dst_pos
,
1252 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1260 !isl_local_space_is_named_or_nested(ls
, src_type
) &&
1261 !isl_local_space_is_named_or_nested(ls
, dst_type
))
1264 if (src_pos
+ n
> isl_local_space_dim(ls
, src_type
))
1265 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1266 "range out of bounds", return isl_local_space_free(ls
));
1267 if (dst_pos
> isl_local_space_dim(ls
, dst_type
))
1268 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1269 "position out of bounds",
1270 return isl_local_space_free(ls
));
1271 if (src_type
== isl_dim_div
)
1272 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1273 "cannot move divs", return isl_local_space_free(ls
));
1274 if (dst_type
== isl_dim_div
)
1275 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1276 "cannot move to divs", return isl_local_space_free(ls
));
1277 if (dst_type
== src_type
&& dst_pos
== src_pos
)
1279 if (dst_type
== src_type
)
1280 isl_die(isl_local_space_get_ctx(ls
), isl_error_unsupported
,
1281 "moving dims within the same type not supported",
1282 return isl_local_space_free(ls
));
1284 ls
= isl_local_space_cow(ls
);
1288 g_src_pos
= 1 + isl_local_space_offset(ls
, src_type
) + src_pos
;
1289 g_dst_pos
= 1 + isl_local_space_offset(ls
, dst_type
) + dst_pos
;
1290 if (dst_type
> src_type
)
1292 ls
->div
= isl_mat_move_cols(ls
->div
, g_dst_pos
, g_src_pos
, n
);
1294 return isl_local_space_free(ls
);
1295 ls
->dim
= isl_space_move_dims(ls
->dim
, dst_type
, dst_pos
,
1296 src_type
, src_pos
, n
);
1298 return isl_local_space_free(ls
);
1303 /* Remove any internal structure of the domain of "ls".
1304 * If there is any such internal structure in the input,
1305 * then the name of the corresponding space is also removed.
1307 __isl_give isl_local_space
*isl_local_space_flatten_domain(
1308 __isl_take isl_local_space
*ls
)
1313 if (!ls
->dim
->nested
[0])
1316 ls
= isl_local_space_cow(ls
);
1320 ls
->dim
= isl_space_flatten_domain(ls
->dim
);
1322 return isl_local_space_free(ls
);
1327 /* Remove any internal structure of the range of "ls".
1328 * If there is any such internal structure in the input,
1329 * then the name of the corresponding space is also removed.
1331 __isl_give isl_local_space
*isl_local_space_flatten_range(
1332 __isl_take isl_local_space
*ls
)
1337 if (!ls
->dim
->nested
[1])
1340 ls
= isl_local_space_cow(ls
);
1344 ls
->dim
= isl_space_flatten_range(ls
->dim
);
1346 return isl_local_space_free(ls
);