2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2013 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 void *isl_local_space_free(__isl_take isl_local_space
*ls
)
113 isl_space_free(ls
->dim
);
114 isl_mat_free(ls
->div
);
121 /* Is the local space that of a set?
123 int isl_local_space_is_set(__isl_keep isl_local_space
*ls
)
125 return ls
? isl_space_is_set(ls
->dim
) : -1;
128 /* Return true if the two local spaces are identical, with identical
129 * expressions for the integer divisions.
131 int isl_local_space_is_equal(__isl_keep isl_local_space
*ls1
,
132 __isl_keep isl_local_space
*ls2
)
139 equal
= isl_space_is_equal(ls1
->dim
, ls2
->dim
);
140 if (equal
< 0 || !equal
)
143 if (!isl_local_space_divs_known(ls1
))
145 if (!isl_local_space_divs_known(ls2
))
148 return isl_mat_is_equal(ls1
->div
, ls2
->div
);
151 int isl_local_space_dim(__isl_keep isl_local_space
*ls
,
152 enum isl_dim_type type
)
156 if (type
== isl_dim_div
)
157 return ls
->div
->n_row
;
158 if (type
== isl_dim_all
)
159 return isl_space_dim(ls
->dim
, isl_dim_all
) + ls
->div
->n_row
;
160 return isl_space_dim(ls
->dim
, type
);
163 unsigned isl_local_space_offset(__isl_keep isl_local_space
*ls
,
164 enum isl_dim_type type
)
173 case isl_dim_cst
: return 0;
174 case isl_dim_param
: return 1;
175 case isl_dim_in
: return 1 + dim
->nparam
;
176 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
177 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
182 /* Does the given dimension have a name?
184 int isl_local_space_has_dim_name(__isl_keep isl_local_space
*ls
,
185 enum isl_dim_type type
, unsigned pos
)
187 return ls
? isl_space_has_dim_name(ls
->dim
, type
, pos
) : -1;
190 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space
*ls
,
191 enum isl_dim_type type
, unsigned pos
)
193 return ls
? isl_space_get_dim_name(ls
->dim
, type
, pos
) : NULL
;
196 int isl_local_space_has_dim_id(__isl_keep isl_local_space
*ls
,
197 enum isl_dim_type type
, unsigned pos
)
199 return ls
? isl_space_has_dim_id(ls
->dim
, type
, pos
) : -1;
202 __isl_give isl_id
*isl_local_space_get_dim_id(__isl_keep isl_local_space
*ls
,
203 enum isl_dim_type type
, unsigned pos
)
205 return ls
? isl_space_get_dim_id(ls
->dim
, type
, pos
) : NULL
;
208 __isl_give isl_aff
*isl_local_space_get_div(__isl_keep isl_local_space
*ls
,
216 if (pos
< 0 || pos
>= ls
->div
->n_row
)
217 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
218 "index out of bounds", return NULL
);
220 if (isl_int_is_zero(ls
->div
->row
[pos
][0]))
221 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
222 "expression of div unknown", return NULL
);
223 if (!isl_local_space_is_set(ls
))
224 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
225 "cannot represent divs of map spaces", return NULL
);
227 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
230 isl_seq_cpy(aff
->v
->el
, ls
->div
->row
[pos
], aff
->v
->size
);
234 __isl_give isl_space
*isl_local_space_get_space(__isl_keep isl_local_space
*ls
)
239 return isl_space_copy(ls
->dim
);
242 /* Replace the identifier of the tuple of type "type" by "id".
244 __isl_give isl_local_space
*isl_local_space_set_tuple_id(
245 __isl_take isl_local_space
*ls
,
246 enum isl_dim_type type
, __isl_take isl_id
*id
)
248 ls
= isl_local_space_cow(ls
);
250 return isl_id_free(id
);
251 ls
->dim
= isl_space_set_tuple_id(ls
->dim
, type
, id
);
253 return isl_local_space_free(ls
);
257 __isl_give isl_local_space
*isl_local_space_set_dim_name(
258 __isl_take isl_local_space
*ls
,
259 enum isl_dim_type type
, unsigned pos
, const char *s
)
261 ls
= isl_local_space_cow(ls
);
264 ls
->dim
= isl_space_set_dim_name(ls
->dim
, type
, pos
, s
);
266 return isl_local_space_free(ls
);
271 __isl_give isl_local_space
*isl_local_space_set_dim_id(
272 __isl_take isl_local_space
*ls
,
273 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
275 ls
= isl_local_space_cow(ls
);
277 return isl_id_free(id
);
278 ls
->dim
= isl_space_set_dim_id(ls
->dim
, type
, pos
, id
);
280 return isl_local_space_free(ls
);
285 __isl_give isl_local_space
*isl_local_space_reset_space(
286 __isl_take isl_local_space
*ls
, __isl_take isl_space
*dim
)
288 ls
= isl_local_space_cow(ls
);
292 isl_space_free(ls
->dim
);
297 isl_local_space_free(ls
);
302 /* Reorder the columns of the given div definitions according to the
304 * The order of the divs themselves is assumed not to change.
306 static __isl_give isl_mat
*reorder_divs(__isl_take isl_mat
*div
,
307 __isl_take isl_reordering
*r
)
316 extra
= isl_space_dim(r
->dim
, isl_dim_all
) + div
->n_row
- r
->len
;
317 mat
= isl_mat_alloc(div
->ctx
, div
->n_row
, div
->n_col
+ extra
);
321 for (i
= 0; i
< div
->n_row
; ++i
) {
322 isl_seq_cpy(mat
->row
[i
], div
->row
[i
], 2);
323 isl_seq_clr(mat
->row
[i
] + 2, mat
->n_col
- 2);
324 for (j
= 0; j
< r
->len
; ++j
)
325 isl_int_set(mat
->row
[i
][2 + r
->pos
[j
]],
329 isl_reordering_free(r
);
333 isl_reordering_free(r
);
338 /* Reorder the dimensions of "ls" according to the given reordering.
339 * The reordering r is assumed to have been extended with the local
340 * variables, leaving them in the same order.
342 __isl_give isl_local_space
*isl_local_space_realign(
343 __isl_take isl_local_space
*ls
, __isl_take isl_reordering
*r
)
345 ls
= isl_local_space_cow(ls
);
349 ls
->div
= reorder_divs(ls
->div
, isl_reordering_copy(r
));
353 ls
= isl_local_space_reset_space(ls
, isl_space_copy(r
->dim
));
355 isl_reordering_free(r
);
358 isl_local_space_free(ls
);
359 isl_reordering_free(r
);
363 __isl_give isl_local_space
*isl_local_space_add_div(
364 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*div
)
366 ls
= isl_local_space_cow(ls
);
370 if (ls
->div
->n_col
!= div
->size
)
371 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
372 "incompatible dimensions", goto error
);
374 ls
->div
= isl_mat_add_zero_cols(ls
->div
, 1);
375 ls
->div
= isl_mat_add_rows(ls
->div
, 1);
379 isl_seq_cpy(ls
->div
->row
[ls
->div
->n_row
- 1], div
->el
, div
->size
);
380 isl_int_set_si(ls
->div
->row
[ls
->div
->n_row
- 1][div
->size
], 0);
385 isl_local_space_free(ls
);
390 __isl_give isl_local_space
*isl_local_space_replace_divs(
391 __isl_take isl_local_space
*ls
, __isl_take isl_mat
*div
)
393 ls
= isl_local_space_cow(ls
);
398 isl_mat_free(ls
->div
);
403 isl_local_space_free(ls
);
407 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
410 static void expand_row(__isl_keep isl_mat
*dst
, int d
,
411 __isl_keep isl_mat
*src
, int s
, int *exp
)
414 unsigned c
= src
->n_col
- src
->n_row
;
416 isl_seq_cpy(dst
->row
[d
], src
->row
[s
], c
);
417 isl_seq_clr(dst
->row
[d
] + c
, dst
->n_col
- c
);
419 for (i
= 0; i
< s
; ++i
)
420 isl_int_set(dst
->row
[d
][c
+ exp
[i
]], src
->row
[s
][c
+ i
]);
423 /* Compare (known) divs.
424 * Return non-zero if at least one of the two divs is unknown.
425 * In particular, if both divs are unknown, we respect their
426 * current order. Otherwise, we sort the known div after the unknown
427 * div only if the known div depends on the unknown div.
429 static int cmp_row(isl_int
*row_i
, isl_int
*row_j
, int i
, int j
,
430 unsigned n_row
, unsigned n_col
)
433 int unknown_i
, unknown_j
;
435 unknown_i
= isl_int_is_zero(row_i
[0]);
436 unknown_j
= isl_int_is_zero(row_j
[0]);
438 if (unknown_i
&& unknown_j
)
442 li
= n_col
- n_row
+ i
;
444 li
= isl_seq_last_non_zero(row_i
, n_col
);
446 lj
= n_col
- n_row
+ j
;
448 lj
= isl_seq_last_non_zero(row_j
, n_col
);
453 return isl_seq_cmp(row_i
, row_j
, n_col
);
456 /* Call cmp_row for divs in a matrix.
458 int isl_mat_cmp_div(__isl_keep isl_mat
*div
, int i
, int j
)
460 return cmp_row(div
->row
[i
], div
->row
[j
], i
, j
, div
->n_row
, div
->n_col
);
463 /* Call cmp_row for divs in a basic map.
465 static int bmap_cmp_row(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
468 return cmp_row(bmap
->div
[i
], bmap
->div
[j
], i
, j
, bmap
->n_div
, total
);
471 /* Sort the divs in "bmap".
473 * We first make sure divs are placed after divs on which they depend.
474 * Then we perform a simple insertion sort based on the same ordering
475 * that is used in isl_merge_divs.
477 __isl_give isl_basic_map
*isl_basic_map_sort_divs(
478 __isl_take isl_basic_map
*bmap
)
483 bmap
= isl_basic_map_order_divs(bmap
);
486 if (bmap
->n_div
<= 1)
489 total
= 2 + isl_basic_map_total_dim(bmap
);
490 for (i
= 1; i
< bmap
->n_div
; ++i
) {
491 for (j
= i
- 1; j
>= 0; --j
) {
492 if (bmap_cmp_row(bmap
, j
, j
+ 1, total
) <= 0)
494 isl_basic_map_swap_div(bmap
, j
, j
+ 1);
501 /* Sort the divs in the basic maps of "map".
503 __isl_give isl_map
*isl_map_sort_divs(__isl_take isl_map
*map
)
505 return isl_map_inline_foreach_basic_map(map
, &isl_basic_map_sort_divs
);
508 /* Combine the two lists of divs into a single list.
509 * For each row i in div1, exp1[i] is set to the position of the corresponding
510 * row in the result. Similarly for div2 and exp2.
511 * This function guarantees
513 * exp1[i+1] > exp1[i]
514 * For optimal merging, the two input list should have been sorted.
516 __isl_give isl_mat
*isl_merge_divs(__isl_keep isl_mat
*div1
,
517 __isl_keep isl_mat
*div2
, int *exp1
, int *exp2
)
526 d
= div1
->n_col
- div1
->n_row
;
527 div
= isl_mat_alloc(div1
->ctx
, 1 + div1
->n_row
+ div2
->n_row
,
528 d
+ div1
->n_row
+ div2
->n_row
);
532 for (i
= 0, j
= 0, k
= 0; i
< div1
->n_row
&& j
< div2
->n_row
; ++k
) {
535 expand_row(div
, k
, div1
, i
, exp1
);
536 expand_row(div
, k
+ 1, div2
, j
, exp2
);
538 cmp
= isl_mat_cmp_div(div
, k
, k
+ 1);
542 } else if (cmp
< 0) {
546 isl_seq_cpy(div
->row
[k
], div
->row
[k
+ 1], div
->n_col
);
549 for (; i
< div1
->n_row
; ++i
, ++k
) {
550 expand_row(div
, k
, div1
, i
, exp1
);
553 for (; j
< div2
->n_row
; ++j
, ++k
) {
554 expand_row(div
, k
, div2
, j
, exp2
);
564 /* Swap divs "a" and "b" in "ls".
566 __isl_give isl_local_space
*isl_local_space_swap_div(
567 __isl_take isl_local_space
*ls
, int a
, int b
)
571 ls
= isl_local_space_cow(ls
);
574 if (a
< 0 || a
>= ls
->div
->n_row
|| b
< 0 || b
>= ls
->div
->n_row
)
575 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
576 "index out of bounds", return isl_local_space_free(ls
));
577 offset
= ls
->div
->n_col
- ls
->div
->n_row
;
578 ls
->div
= isl_mat_swap_cols(ls
->div
, offset
+ a
, offset
+ b
);
579 ls
->div
= isl_mat_swap_rows(ls
->div
, a
, b
);
581 return isl_local_space_free(ls
);
585 /* Construct a local space that contains all the divs in either
588 __isl_give isl_local_space
*isl_local_space_intersect(
589 __isl_take isl_local_space
*ls1
, __isl_take isl_local_space
*ls2
)
599 ctx
= isl_local_space_get_ctx(ls1
);
600 if (!isl_space_is_equal(ls1
->dim
, ls2
->dim
))
601 isl_die(ctx
, isl_error_invalid
,
602 "spaces should be identical", goto error
);
604 if (ls2
->div
->n_row
== 0) {
605 isl_local_space_free(ls2
);
609 if (ls1
->div
->n_row
== 0) {
610 isl_local_space_free(ls1
);
614 exp1
= isl_alloc_array(ctx
, int, ls1
->div
->n_row
);
615 exp2
= isl_alloc_array(ctx
, int, ls2
->div
->n_row
);
619 div
= isl_merge_divs(ls1
->div
, ls2
->div
, exp1
, exp2
);
625 isl_local_space_free(ls2
);
626 isl_mat_free(ls1
->div
);
633 isl_local_space_free(ls1
);
634 isl_local_space_free(ls2
);
638 int isl_local_space_divs_known(__isl_keep isl_local_space
*ls
)
645 for (i
= 0; i
< ls
->div
->n_row
; ++i
)
646 if (isl_int_is_zero(ls
->div
->row
[i
][0]))
652 __isl_give isl_local_space
*isl_local_space_domain(
653 __isl_take isl_local_space
*ls
)
655 ls
= isl_local_space_drop_dims(ls
, isl_dim_out
,
656 0, isl_local_space_dim(ls
, isl_dim_out
));
657 ls
= isl_local_space_cow(ls
);
660 ls
->dim
= isl_space_domain(ls
->dim
);
662 return isl_local_space_free(ls
);
666 __isl_give isl_local_space
*isl_local_space_range(
667 __isl_take isl_local_space
*ls
)
669 ls
= isl_local_space_drop_dims(ls
, isl_dim_in
,
670 0, isl_local_space_dim(ls
, isl_dim_in
));
671 ls
= isl_local_space_cow(ls
);
675 ls
->dim
= isl_space_range(ls
->dim
);
677 return isl_local_space_free(ls
);
681 /* Construct a local space for a map that has the given local
682 * space as domain and that has a zero-dimensional range.
684 __isl_give isl_local_space
*isl_local_space_from_domain(
685 __isl_take isl_local_space
*ls
)
687 ls
= isl_local_space_cow(ls
);
690 ls
->dim
= isl_space_from_domain(ls
->dim
);
692 return isl_local_space_free(ls
);
696 __isl_give isl_local_space
*isl_local_space_add_dims(
697 __isl_take isl_local_space
*ls
, enum isl_dim_type type
, unsigned n
)
703 pos
= isl_local_space_dim(ls
, type
);
704 return isl_local_space_insert_dims(ls
, type
, pos
, n
);
707 /* Remove common factor of non-constant terms and denominator.
709 static void normalize_div(__isl_keep isl_local_space
*ls
, int div
)
711 isl_ctx
*ctx
= ls
->div
->ctx
;
712 unsigned total
= ls
->div
->n_col
- 2;
714 isl_seq_gcd(ls
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
715 isl_int_gcd(ctx
->normalize_gcd
,
716 ctx
->normalize_gcd
, ls
->div
->row
[div
][0]);
717 if (isl_int_is_one(ctx
->normalize_gcd
))
720 isl_seq_scale_down(ls
->div
->row
[div
] + 2, ls
->div
->row
[div
] + 2,
721 ctx
->normalize_gcd
, total
);
722 isl_int_divexact(ls
->div
->row
[div
][0], ls
->div
->row
[div
][0],
724 isl_int_fdiv_q(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1],
728 /* Exploit the equalities in "eq" to simplify the expressions of
729 * the integer divisions in "ls".
730 * The integer divisions in "ls" are assumed to appear as regular
731 * dimensions in "eq".
733 __isl_give isl_local_space
*isl_local_space_substitute_equalities(
734 __isl_take isl_local_space
*ls
, __isl_take isl_basic_set
*eq
)
743 total
= isl_space_dim(eq
->dim
, isl_dim_all
);
744 if (isl_local_space_dim(ls
, isl_dim_all
) != total
)
745 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
746 "spaces don't match", goto error
);
749 for (i
= 0; i
< eq
->n_eq
; ++i
) {
750 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
751 if (j
< 0 || j
== 0 || j
>= total
)
754 for (k
= 0; k
< ls
->div
->n_row
; ++k
) {
755 if (isl_int_is_zero(ls
->div
->row
[k
][1 + j
]))
757 ls
= isl_local_space_cow(ls
);
760 ls
->div
= isl_mat_cow(ls
->div
);
763 isl_seq_elim(ls
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
764 &ls
->div
->row
[k
][0]);
765 normalize_div(ls
, k
);
769 isl_basic_set_free(eq
);
772 isl_basic_set_free(eq
);
773 isl_local_space_free(ls
);
777 /* Plug in the affine expressions "subs" of length "subs_len" (including
778 * the denominator and the constant term) into the variable at position "pos"
779 * of the "n" div expressions starting at "first".
781 * Let i be the dimension to replace and let "subs" be of the form
785 * Any integer division starting at "first" with a non-zero coefficient for i,
791 * floor((a f + d g)/(m d))
793 __isl_give isl_local_space
*isl_local_space_substitute_seq(
794 __isl_take isl_local_space
*ls
,
795 enum isl_dim_type type
, unsigned pos
, isl_int
*subs
, int subs_len
,
803 ls
= isl_local_space_cow(ls
);
806 ls
->div
= isl_mat_cow(ls
->div
);
808 return isl_local_space_free(ls
);
810 if (first
+ n
> ls
->div
->n_row
)
811 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
812 "index out of bounds", return isl_local_space_free(ls
));
814 pos
+= isl_local_space_offset(ls
, type
);
817 for (i
= first
; i
< ls
->div
->n_row
; ++i
) {
818 if (isl_int_is_zero(ls
->div
->row
[i
][1 + pos
]))
820 isl_seq_substitute(ls
->div
->row
[i
], pos
, subs
,
821 ls
->div
->n_col
, subs_len
, v
);
822 normalize_div(ls
, i
);
829 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
832 * Let i be the dimension to replace and let "subs" be of the form
836 * Any integer division with a non-zero coefficient for i,
842 * floor((a f + d g)/(m d))
844 __isl_give isl_local_space
*isl_local_space_substitute(
845 __isl_take isl_local_space
*ls
,
846 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
848 ls
= isl_local_space_cow(ls
);
850 return isl_local_space_free(ls
);
852 if (!isl_space_is_equal(ls
->dim
, subs
->ls
->dim
))
853 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
854 "spaces don't match", return isl_local_space_free(ls
));
855 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
856 isl_die(isl_local_space_get_ctx(ls
), isl_error_unsupported
,
857 "cannot handle divs yet",
858 return isl_local_space_free(ls
));
860 return isl_local_space_substitute_seq(ls
, type
, pos
, subs
->v
->el
,
861 subs
->v
->size
, 0, ls
->div
->n_row
);
864 int isl_local_space_is_named_or_nested(__isl_keep isl_local_space
*ls
,
865 enum isl_dim_type type
)
869 return isl_space_is_named_or_nested(ls
->dim
, type
);
872 __isl_give isl_local_space
*isl_local_space_drop_dims(
873 __isl_take isl_local_space
*ls
,
874 enum isl_dim_type type
, unsigned first
, unsigned n
)
880 if (n
== 0 && !isl_local_space_is_named_or_nested(ls
, type
))
883 ctx
= isl_local_space_get_ctx(ls
);
884 if (first
+ n
> isl_local_space_dim(ls
, type
))
885 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
886 return isl_local_space_free(ls
));
888 ls
= isl_local_space_cow(ls
);
892 if (type
== isl_dim_div
) {
893 ls
->div
= isl_mat_drop_rows(ls
->div
, first
, n
);
895 ls
->dim
= isl_space_drop_dims(ls
->dim
, type
, first
, n
);
897 return isl_local_space_free(ls
);
900 first
+= 1 + isl_local_space_offset(ls
, type
);
901 ls
->div
= isl_mat_drop_cols(ls
->div
, first
, n
);
903 return isl_local_space_free(ls
);
908 __isl_give isl_local_space
*isl_local_space_insert_dims(
909 __isl_take isl_local_space
*ls
,
910 enum isl_dim_type type
, unsigned first
, unsigned n
)
916 if (n
== 0 && !isl_local_space_is_named_or_nested(ls
, type
))
919 ctx
= isl_local_space_get_ctx(ls
);
920 if (first
> isl_local_space_dim(ls
, type
))
921 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
922 return isl_local_space_free(ls
));
924 ls
= isl_local_space_cow(ls
);
928 if (type
== isl_dim_div
) {
929 ls
->div
= isl_mat_insert_zero_rows(ls
->div
, first
, n
);
931 ls
->dim
= isl_space_insert_dims(ls
->dim
, type
, first
, n
);
933 return isl_local_space_free(ls
);
936 first
+= 1 + isl_local_space_offset(ls
, type
);
937 ls
->div
= isl_mat_insert_zero_cols(ls
->div
, first
, n
);
939 return isl_local_space_free(ls
);
944 /* Check if the constraints pointed to by "constraint" is a div
945 * constraint corresponding to div "div" in "ls".
947 * That is, if div = floor(f/m), then check if the constraint is
951 * -(f-(m-1)) + m d >= 0
953 int isl_local_space_is_div_constraint(__isl_keep isl_local_space
*ls
,
954 isl_int
*constraint
, unsigned div
)
961 if (isl_int_is_zero(ls
->div
->row
[div
][0]))
964 pos
= isl_local_space_offset(ls
, isl_dim_div
) + div
;
966 if (isl_int_eq(constraint
[pos
], ls
->div
->row
[div
][0])) {
968 isl_int_sub(ls
->div
->row
[div
][1],
969 ls
->div
->row
[div
][1], ls
->div
->row
[div
][0]);
970 isl_int_add_ui(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1], 1);
971 neg
= isl_seq_is_neg(constraint
, ls
->div
->row
[div
]+1, pos
);
972 isl_int_sub_ui(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1], 1);
973 isl_int_add(ls
->div
->row
[div
][1],
974 ls
->div
->row
[div
][1], ls
->div
->row
[div
][0]);
977 if (isl_seq_first_non_zero(constraint
+pos
+1,
978 ls
->div
->n_row
-div
-1) != -1)
980 } else if (isl_int_abs_eq(constraint
[pos
], ls
->div
->row
[div
][0])) {
981 if (!isl_seq_eq(constraint
, ls
->div
->row
[div
]+1, pos
))
983 if (isl_seq_first_non_zero(constraint
+pos
+1,
984 ls
->div
->n_row
-div
-1) != -1)
993 * Set active[i] to 1 if the dimension at position i is involved
994 * in the linear expression l.
996 int *isl_local_space_get_active(__isl_keep isl_local_space
*ls
, isl_int
*l
)
1004 ctx
= isl_local_space_get_ctx(ls
);
1005 total
= isl_local_space_dim(ls
, isl_dim_all
);
1006 active
= isl_calloc_array(ctx
, int, total
);
1007 if (total
&& !active
)
1010 for (i
= 0; i
< total
; ++i
)
1011 active
[i
] = !isl_int_is_zero(l
[i
]);
1013 offset
= isl_local_space_offset(ls
, isl_dim_div
) - 1;
1014 for (i
= ls
->div
->n_row
- 1; i
>= 0; --i
) {
1015 if (!active
[offset
+ i
])
1017 for (j
= 0; j
< total
; ++j
)
1018 active
[j
] |= !isl_int_is_zero(ls
->div
->row
[i
][2 + j
]);
1024 /* Given a local space "ls" of a set, create a local space
1025 * for the lift of the set. In particular, the result
1026 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
1027 * range of the wrapped map.
1029 __isl_give isl_local_space
*isl_local_space_lift(
1030 __isl_take isl_local_space
*ls
)
1032 ls
= isl_local_space_cow(ls
);
1036 ls
->dim
= isl_space_lift(ls
->dim
, ls
->div
->n_row
);
1037 ls
->div
= isl_mat_drop_rows(ls
->div
, 0, ls
->div
->n_row
);
1038 if (!ls
->dim
|| !ls
->div
)
1039 return isl_local_space_free(ls
);
1044 /* Construct a basic map that maps a set living in local space "ls"
1045 * to the corresponding lifted local space.
1047 __isl_give isl_basic_map
*isl_local_space_lifting(
1048 __isl_take isl_local_space
*ls
)
1050 isl_basic_map
*lifting
;
1051 isl_basic_set
*bset
;
1055 if (!isl_local_space_is_set(ls
))
1056 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1057 "lifting only defined on set spaces",
1058 return isl_local_space_free(ls
));
1060 bset
= isl_basic_set_from_local_space(ls
);
1061 lifting
= isl_basic_set_unwrap(isl_basic_set_lift(bset
));
1062 lifting
= isl_basic_map_domain_map(lifting
);
1063 lifting
= isl_basic_map_reverse(lifting
);
1068 /* Compute the preimage of "ls" under the function represented by "ma".
1069 * In other words, plug in "ma" in "ls". The result is a local space
1070 * that is part of the domain space of "ma".
1072 * If the divs in "ls" are represented as
1074 * floor((a_i(p) + b_i x + c_i(divs))/n_i)
1076 * and ma is represented by
1078 * x = D(p) + F(y) + G(divs')
1080 * then the resulting divs are
1082 * floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
1084 * We first copy over the divs from "ma" and then
1085 * we add the modified divs from "ls".
1087 __isl_give isl_local_space
*isl_local_space_preimage_multi_aff(
1088 __isl_take isl_local_space
*ls
, __isl_take isl_multi_aff
*ma
)
1092 isl_local_space
*res
= NULL
;
1093 int n_div_ls
, n_div_ma
;
1094 isl_int f
, c1
, c2
, g
;
1096 ma
= isl_multi_aff_align_divs(ma
);
1099 if (!isl_space_is_range_internal(ls
->dim
, ma
->space
))
1100 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1101 "spaces don't match", goto error
);
1103 n_div_ls
= isl_local_space_dim(ls
, isl_dim_div
);
1104 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
1106 space
= isl_space_domain(isl_multi_aff_get_space(ma
));
1107 res
= isl_local_space_alloc(space
, n_div_ma
+ n_div_ls
);
1112 isl_mat_free(res
->div
);
1113 res
->div
= isl_mat_copy(ma
->p
[0]->ls
->div
);
1114 res
->div
= isl_mat_add_zero_cols(res
->div
, n_div_ls
);
1115 res
->div
= isl_mat_add_rows(res
->div
, n_div_ls
);
1125 for (i
= 0; i
< ls
->div
->n_row
; ++i
) {
1126 if (isl_int_is_zero(ls
->div
->row
[i
][0])) {
1127 isl_int_set_si(res
->div
->row
[n_div_ma
+ i
][0], 0);
1130 isl_seq_preimage(res
->div
->row
[n_div_ma
+ i
], ls
->div
->row
[i
],
1131 ma
, 0, 0, n_div_ma
, n_div_ls
, f
, c1
, c2
, g
, 1);
1132 normalize_div(res
, n_div_ma
+ i
);
1140 isl_local_space_free(ls
);
1141 isl_multi_aff_free(ma
);
1144 isl_local_space_free(ls
);
1145 isl_multi_aff_free(ma
);
1146 isl_local_space_free(res
);
1150 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "ls"
1151 * to dimensions of "dst_type" at "dst_pos".
1153 * Moving to/from local dimensions is not allowed.
1154 * We currently assume that the dimension type changes.
1156 __isl_give isl_local_space
*isl_local_space_move_dims(
1157 __isl_take isl_local_space
*ls
,
1158 enum isl_dim_type dst_type
, unsigned dst_pos
,
1159 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1167 !isl_local_space_is_named_or_nested(ls
, src_type
) &&
1168 !isl_local_space_is_named_or_nested(ls
, dst_type
))
1171 if (src_pos
+ n
> isl_local_space_dim(ls
, src_type
))
1172 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1173 "range out of bounds", return isl_local_space_free(ls
));
1174 if (dst_pos
> isl_local_space_dim(ls
, dst_type
))
1175 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1176 "position out of bounds",
1177 return isl_local_space_free(ls
));
1178 if (src_type
== isl_dim_div
)
1179 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1180 "cannot move divs", return isl_local_space_free(ls
));
1181 if (dst_type
== isl_dim_div
)
1182 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1183 "cannot move to divs", return isl_local_space_free(ls
));
1184 if (dst_type
== src_type
&& dst_pos
== src_pos
)
1186 if (dst_type
== src_type
)
1187 isl_die(isl_local_space_get_ctx(ls
), isl_error_unsupported
,
1188 "moving dims within the same type not supported",
1189 return isl_local_space_free(ls
));
1191 ls
= isl_local_space_cow(ls
);
1195 g_src_pos
= 1 + isl_local_space_offset(ls
, src_type
) + src_pos
;
1196 g_dst_pos
= 1 + isl_local_space_offset(ls
, dst_type
) + dst_pos
;
1197 if (dst_type
> src_type
)
1199 ls
->div
= isl_mat_move_cols(ls
->div
, g_dst_pos
, g_src_pos
, n
);
1201 return isl_local_space_free(ls
);
1202 ls
->dim
= isl_space_move_dims(ls
->dim
, dst_type
, dst_pos
,
1203 src_type
, src_pos
, n
);
1205 return isl_local_space_free(ls
);