2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012 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>
21 isl_ctx
*isl_local_space_get_ctx(__isl_keep isl_local_space
*ls
)
23 return ls
? ls
->dim
->ctx
: NULL
;
26 __isl_give isl_local_space
*isl_local_space_alloc_div(__isl_take isl_space
*dim
,
27 __isl_take isl_mat
*div
)
30 isl_local_space
*ls
= NULL
;
35 ctx
= isl_space_get_ctx(dim
);
36 ls
= isl_calloc_type(ctx
, struct isl_local_space
);
48 isl_local_space_free(ls
);
52 __isl_give isl_local_space
*isl_local_space_alloc(__isl_take isl_space
*dim
,
62 total
= isl_space_dim(dim
, isl_dim_all
);
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
);
69 __isl_give isl_local_space
*isl_local_space_from_space(__isl_take isl_space
*dim
)
71 return isl_local_space_alloc(dim
, 0);
74 __isl_give isl_local_space
*isl_local_space_copy(__isl_keep isl_local_space
*ls
)
83 __isl_give isl_local_space
*isl_local_space_dup(__isl_keep isl_local_space
*ls
)
88 return isl_local_space_alloc_div(isl_space_copy(ls
->dim
),
89 isl_mat_copy(ls
->div
));
93 __isl_give isl_local_space
*isl_local_space_cow(__isl_take isl_local_space
*ls
)
101 return isl_local_space_dup(ls
);
104 void *isl_local_space_free(__isl_take isl_local_space
*ls
)
112 isl_space_free(ls
->dim
);
113 isl_mat_free(ls
->div
);
120 /* Is the local space that of a set?
122 int isl_local_space_is_set(__isl_keep isl_local_space
*ls
)
124 return ls
? isl_space_is_set(ls
->dim
) : -1;
127 /* Return true if the two local spaces are identical, with identical
128 * expressions for the integer divisions.
130 int isl_local_space_is_equal(__isl_keep isl_local_space
*ls1
,
131 __isl_keep isl_local_space
*ls2
)
138 equal
= isl_space_is_equal(ls1
->dim
, ls2
->dim
);
139 if (equal
< 0 || !equal
)
142 if (!isl_local_space_divs_known(ls1
))
144 if (!isl_local_space_divs_known(ls2
))
147 return isl_mat_is_equal(ls1
->div
, ls2
->div
);
150 int isl_local_space_dim(__isl_keep isl_local_space
*ls
,
151 enum isl_dim_type type
)
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
);
162 unsigned isl_local_space_offset(__isl_keep isl_local_space
*ls
,
163 enum isl_dim_type 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
;
181 /* Does the given dimension have a name?
183 int isl_local_space_has_dim_name(__isl_keep isl_local_space
*ls
,
184 enum isl_dim_type type
, unsigned pos
)
186 return ls
? isl_space_has_dim_name(ls
->dim
, type
, pos
) : -1;
189 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space
*ls
,
190 enum isl_dim_type type
, unsigned pos
)
192 return ls
? isl_space_get_dim_name(ls
->dim
, type
, pos
) : NULL
;
195 int isl_local_space_has_dim_id(__isl_keep isl_local_space
*ls
,
196 enum isl_dim_type type
, unsigned pos
)
198 return ls
? isl_space_has_dim_id(ls
->dim
, type
, pos
) : -1;
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
)
204 return ls
? isl_space_get_dim_id(ls
->dim
, type
, pos
) : NULL
;
207 __isl_give isl_aff
*isl_local_space_get_div(__isl_keep isl_local_space
*ls
,
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
);
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
);
226 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
229 isl_seq_cpy(aff
->v
->el
, ls
->div
->row
[pos
], aff
->v
->size
);
233 __isl_give isl_space
*isl_local_space_get_space(__isl_keep isl_local_space
*ls
)
238 return isl_space_copy(ls
->dim
);
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
)
245 ls
= isl_local_space_cow(ls
);
248 ls
->dim
= isl_space_set_dim_name(ls
->dim
, type
, pos
, s
);
250 return isl_local_space_free(ls
);
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
)
259 ls
= isl_local_space_cow(ls
);
261 return isl_id_free(id
);
262 ls
->dim
= isl_space_set_dim_id(ls
->dim
, type
, pos
, id
);
264 return isl_local_space_free(ls
);
269 __isl_give isl_local_space
*isl_local_space_reset_space(
270 __isl_take isl_local_space
*ls
, __isl_take isl_space
*dim
)
272 ls
= isl_local_space_cow(ls
);
276 isl_space_free(ls
->dim
);
281 isl_local_space_free(ls
);
286 /* Reorder the columns of the given div definitions according to the
288 * The order of the divs themselves is assumed not to change.
290 static __isl_give isl_mat
*reorder_divs(__isl_take isl_mat
*div
,
291 __isl_take isl_reordering
*r
)
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
);
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
]],
313 isl_reordering_free(r
);
317 isl_reordering_free(r
);
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.
326 __isl_give isl_local_space
*isl_local_space_realign(
327 __isl_take isl_local_space
*ls
, __isl_take isl_reordering
*r
)
329 ls
= isl_local_space_cow(ls
);
333 ls
->div
= reorder_divs(ls
->div
, isl_reordering_copy(r
));
337 ls
= isl_local_space_reset_space(ls
, isl_space_copy(r
->dim
));
339 isl_reordering_free(r
);
342 isl_local_space_free(ls
);
343 isl_reordering_free(r
);
347 __isl_give isl_local_space
*isl_local_space_add_div(
348 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*div
)
350 ls
= isl_local_space_cow(ls
);
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
);
358 ls
->div
= isl_mat_add_zero_cols(ls
->div
, 1);
359 ls
->div
= isl_mat_add_rows(ls
->div
, 1);
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);
369 isl_local_space_free(ls
);
374 __isl_give isl_local_space
*isl_local_space_replace_divs(
375 __isl_take isl_local_space
*ls
, __isl_take isl_mat
*div
)
377 ls
= isl_local_space_cow(ls
);
382 isl_mat_free(ls
->div
);
387 isl_local_space_free(ls
);
391 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
394 static void expand_row(__isl_keep isl_mat
*dst
, int d
,
395 __isl_keep isl_mat
*src
, int s
, int *exp
)
398 unsigned c
= src
->n_col
- src
->n_row
;
400 isl_seq_cpy(dst
->row
[d
], src
->row
[s
], c
);
401 isl_seq_clr(dst
->row
[d
] + c
, dst
->n_col
- c
);
403 for (i
= 0; i
< s
; ++i
)
404 isl_int_set(dst
->row
[d
][c
+ exp
[i
]], src
->row
[s
][c
+ i
]);
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.
413 static int cmp_row(isl_int
*row_i
, isl_int
*row_j
, int i
, int j
,
414 unsigned n_row
, unsigned n_col
)
417 int unknown_i
, unknown_j
;
419 unknown_i
= isl_int_is_zero(row_i
[0]);
420 unknown_j
= isl_int_is_zero(row_j
[0]);
422 if (unknown_i
&& unknown_j
)
426 li
= n_col
- n_row
+ i
;
428 li
= isl_seq_last_non_zero(row_i
, n_col
);
430 lj
= n_col
- n_row
+ j
;
432 lj
= isl_seq_last_non_zero(row_j
, n_col
);
437 return isl_seq_cmp(row_i
, row_j
, n_col
);
440 /* Call cmp_row for divs in a matrix.
442 int isl_mat_cmp_div(__isl_keep isl_mat
*div
, int i
, int j
)
444 return cmp_row(div
->row
[i
], div
->row
[j
], i
, j
, div
->n_row
, div
->n_col
);
447 /* Call cmp_row for divs in a basic map.
449 static int bmap_cmp_row(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
452 return cmp_row(bmap
->div
[i
], bmap
->div
[j
], i
, j
, bmap
->n_div
, total
);
455 /* Sort the divs in "bmap".
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.
461 __isl_give isl_basic_map
*isl_basic_map_sort_divs(
462 __isl_take isl_basic_map
*bmap
)
467 bmap
= isl_basic_map_order_divs(bmap
);
470 if (bmap
->n_div
<= 1)
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)
478 isl_basic_map_swap_div(bmap
, j
, j
+ 1);
485 /* Sort the divs in the basic maps of "map".
487 __isl_give isl_map
*isl_map_sort_divs(__isl_take isl_map
*map
)
489 return isl_map_inline_foreach_basic_map(map
, &isl_basic_map_sort_divs
);
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
497 * exp1[i+1] > exp1[i]
498 * For optimal merging, the two input list should have been sorted.
500 __isl_give isl_mat
*isl_merge_divs(__isl_keep isl_mat
*div1
,
501 __isl_keep isl_mat
*div2
, int *exp1
, int *exp2
)
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
);
516 for (i
= 0, j
= 0, k
= 0; i
< div1
->n_row
&& j
< div2
->n_row
; ++k
) {
519 expand_row(div
, k
, div1
, i
, exp1
);
520 expand_row(div
, k
+ 1, div2
, j
, exp2
);
522 cmp
= isl_mat_cmp_div(div
, k
, k
+ 1);
526 } else if (cmp
< 0) {
530 isl_seq_cpy(div
->row
[k
], div
->row
[k
+ 1], div
->n_col
);
533 for (; i
< div1
->n_row
; ++i
, ++k
) {
534 expand_row(div
, k
, div1
, i
, exp1
);
537 for (; j
< div2
->n_row
; ++j
, ++k
) {
538 expand_row(div
, k
, div2
, j
, exp2
);
548 /* Swap divs "a" and "b" in "ls".
550 __isl_give isl_local_space
*isl_local_space_swap_div(
551 __isl_take isl_local_space
*ls
, int a
, int b
)
555 ls
= isl_local_space_cow(ls
);
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
);
565 return isl_local_space_free(ls
);
569 /* Construct a local space that contains all the divs in either
572 __isl_give isl_local_space
*isl_local_space_intersect(
573 __isl_take isl_local_space
*ls1
, __isl_take isl_local_space
*ls2
)
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
);
588 if (ls2
->div
->n_row
== 0) {
589 isl_local_space_free(ls2
);
593 if (ls1
->div
->n_row
== 0) {
594 isl_local_space_free(ls1
);
598 exp1
= isl_alloc_array(ctx
, int, ls1
->div
->n_row
);
599 exp2
= isl_alloc_array(ctx
, int, ls2
->div
->n_row
);
603 div
= isl_merge_divs(ls1
->div
, ls2
->div
, exp1
, exp2
);
609 isl_local_space_free(ls2
);
610 isl_mat_free(ls1
->div
);
617 isl_local_space_free(ls1
);
618 isl_local_space_free(ls2
);
622 int isl_local_space_divs_known(__isl_keep isl_local_space
*ls
)
629 for (i
= 0; i
< ls
->div
->n_row
; ++i
)
630 if (isl_int_is_zero(ls
->div
->row
[i
][0]))
636 __isl_give isl_local_space
*isl_local_space_domain(
637 __isl_take isl_local_space
*ls
)
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
);
644 ls
->dim
= isl_space_domain(ls
->dim
);
646 return isl_local_space_free(ls
);
650 __isl_give isl_local_space
*isl_local_space_range(
651 __isl_take isl_local_space
*ls
)
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
);
659 ls
->dim
= isl_space_range(ls
->dim
);
661 return isl_local_space_free(ls
);
665 /* Construct a local space for a map that has the given local
666 * space as domain and that has a zero-dimensional range.
668 __isl_give isl_local_space
*isl_local_space_from_domain(
669 __isl_take isl_local_space
*ls
)
671 ls
= isl_local_space_cow(ls
);
674 ls
->dim
= isl_space_from_domain(ls
->dim
);
676 return isl_local_space_free(ls
);
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
)
687 pos
= isl_local_space_dim(ls
, type
);
688 return isl_local_space_insert_dims(ls
, type
, pos
, n
);
691 /* Remove common factor of non-constant terms and denominator.
693 static void normalize_div(__isl_keep isl_local_space
*ls
, int div
)
695 isl_ctx
*ctx
= ls
->div
->ctx
;
696 unsigned total
= ls
->div
->n_col
- 2;
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
))
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],
708 isl_int_fdiv_q(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1],
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".
717 __isl_give isl_local_space
*isl_local_space_substitute_equalities(
718 __isl_take isl_local_space
*ls
, __isl_take isl_basic_set
*eq
)
724 ls
= isl_local_space_cow(ls
);
728 total
= isl_space_dim(eq
->dim
, isl_dim_all
);
729 if (isl_local_space_dim(ls
, isl_dim_all
) != total
)
730 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
731 "dimensions don't match", goto error
);
734 for (i
= 0; i
< eq
->n_eq
; ++i
) {
735 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
736 if (j
< 0 || j
== 0 || j
>= total
)
739 for (k
= 0; k
< ls
->div
->n_row
; ++k
) {
740 if (isl_int_is_zero(ls
->div
->row
[k
][1 + j
]))
742 isl_seq_elim(ls
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
743 &ls
->div
->row
[k
][0]);
744 normalize_div(ls
, k
);
748 isl_basic_set_free(eq
);
751 isl_basic_set_free(eq
);
752 isl_local_space_free(ls
);
756 /* Plug in the affine expressions "subs" of length "subs_len" (including
757 * the denominator and the constant term) into the variable at position "pos"
758 * of the "n" div expressions starting at "first".
760 * Let i be the dimension to replace and let "subs" be of the form
764 * Any integer division starting at "first" with a non-zero coefficient for i,
770 * floor((a f + d g)/(m d))
772 __isl_give isl_local_space
*isl_local_space_substitute_seq(
773 __isl_take isl_local_space
*ls
,
774 enum isl_dim_type type
, unsigned pos
, isl_int
*subs
, int subs_len
,
782 ls
= isl_local_space_cow(ls
);
785 ls
->div
= isl_mat_cow(ls
->div
);
787 return isl_local_space_free(ls
);
789 if (first
+ n
> ls
->div
->n_row
)
790 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
791 "index out of bounds", return isl_local_space_free(ls
));
793 pos
+= isl_local_space_offset(ls
, type
);
796 for (i
= first
; i
< ls
->div
->n_row
; ++i
) {
797 if (isl_int_is_zero(ls
->div
->row
[i
][1 + pos
]))
799 isl_seq_substitute(ls
->div
->row
[i
], pos
, subs
,
800 ls
->div
->n_col
, subs_len
, v
);
801 normalize_div(ls
, i
);
808 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
811 * Let i be the dimension to replace and let "subs" be of the form
815 * Any integer division with a non-zero coefficient for i,
821 * floor((a f + d g)/(m d))
823 __isl_give isl_local_space
*isl_local_space_substitute(
824 __isl_take isl_local_space
*ls
,
825 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
827 ls
= isl_local_space_cow(ls
);
829 return isl_local_space_free(ls
);
831 if (!isl_space_is_equal(ls
->dim
, subs
->ls
->dim
))
832 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
833 "spaces don't match", return isl_local_space_free(ls
));
834 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
835 isl_die(isl_local_space_get_ctx(ls
), isl_error_unsupported
,
836 "cannot handle divs yet",
837 return isl_local_space_free(ls
));
839 return isl_local_space_substitute_seq(ls
, type
, pos
, subs
->v
->el
,
840 subs
->v
->size
, 0, ls
->div
->n_row
);
843 int isl_local_space_is_named_or_nested(__isl_keep isl_local_space
*ls
,
844 enum isl_dim_type type
)
848 return isl_space_is_named_or_nested(ls
->dim
, type
);
851 __isl_give isl_local_space
*isl_local_space_drop_dims(
852 __isl_take isl_local_space
*ls
,
853 enum isl_dim_type type
, unsigned first
, unsigned n
)
859 if (n
== 0 && !isl_local_space_is_named_or_nested(ls
, type
))
862 ctx
= isl_local_space_get_ctx(ls
);
863 if (first
+ n
> isl_local_space_dim(ls
, type
))
864 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
865 return isl_local_space_free(ls
));
867 ls
= isl_local_space_cow(ls
);
871 if (type
== isl_dim_div
) {
872 ls
->div
= isl_mat_drop_rows(ls
->div
, first
, n
);
874 ls
->dim
= isl_space_drop_dims(ls
->dim
, type
, first
, n
);
876 return isl_local_space_free(ls
);
879 first
+= 1 + isl_local_space_offset(ls
, type
);
880 ls
->div
= isl_mat_drop_cols(ls
->div
, first
, n
);
882 return isl_local_space_free(ls
);
887 __isl_give isl_local_space
*isl_local_space_insert_dims(
888 __isl_take isl_local_space
*ls
,
889 enum isl_dim_type type
, unsigned first
, unsigned n
)
895 if (n
== 0 && !isl_local_space_is_named_or_nested(ls
, type
))
898 ctx
= isl_local_space_get_ctx(ls
);
899 if (first
> isl_local_space_dim(ls
, type
))
900 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
901 return isl_local_space_free(ls
));
903 ls
= isl_local_space_cow(ls
);
907 if (type
== isl_dim_div
) {
908 ls
->div
= isl_mat_insert_zero_rows(ls
->div
, first
, n
);
910 ls
->dim
= isl_space_insert_dims(ls
->dim
, type
, first
, n
);
912 return isl_local_space_free(ls
);
915 first
+= 1 + isl_local_space_offset(ls
, type
);
916 ls
->div
= isl_mat_insert_zero_cols(ls
->div
, first
, n
);
918 return isl_local_space_free(ls
);
923 /* Check if the constraints pointed to by "constraint" is a div
924 * constraint corresponding to div "div" in "ls".
926 * That is, if div = floor(f/m), then check if the constraint is
930 * -(f-(m-1)) + m d >= 0
932 int isl_local_space_is_div_constraint(__isl_keep isl_local_space
*ls
,
933 isl_int
*constraint
, unsigned div
)
940 if (isl_int_is_zero(ls
->div
->row
[div
][0]))
943 pos
= isl_local_space_offset(ls
, isl_dim_div
) + div
;
945 if (isl_int_eq(constraint
[pos
], ls
->div
->row
[div
][0])) {
947 isl_int_sub(ls
->div
->row
[div
][1],
948 ls
->div
->row
[div
][1], ls
->div
->row
[div
][0]);
949 isl_int_add_ui(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1], 1);
950 neg
= isl_seq_is_neg(constraint
, ls
->div
->row
[div
]+1, pos
);
951 isl_int_sub_ui(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1], 1);
952 isl_int_add(ls
->div
->row
[div
][1],
953 ls
->div
->row
[div
][1], ls
->div
->row
[div
][0]);
956 if (isl_seq_first_non_zero(constraint
+pos
+1,
957 ls
->div
->n_row
-div
-1) != -1)
959 } else if (isl_int_abs_eq(constraint
[pos
], ls
->div
->row
[div
][0])) {
960 if (!isl_seq_eq(constraint
, ls
->div
->row
[div
]+1, pos
))
962 if (isl_seq_first_non_zero(constraint
+pos
+1,
963 ls
->div
->n_row
-div
-1) != -1)
972 * Set active[i] to 1 if the dimension at position i is involved
973 * in the linear expression l.
975 int *isl_local_space_get_active(__isl_keep isl_local_space
*ls
, isl_int
*l
)
983 ctx
= isl_local_space_get_ctx(ls
);
984 total
= isl_local_space_dim(ls
, isl_dim_all
);
985 active
= isl_calloc_array(ctx
, int, total
);
989 for (i
= 0; i
< total
; ++i
)
990 active
[i
] = !isl_int_is_zero(l
[i
]);
992 offset
= isl_local_space_offset(ls
, isl_dim_div
) - 1;
993 for (i
= ls
->div
->n_row
- 1; i
>= 0; --i
) {
994 if (!active
[offset
+ i
])
996 for (j
= 0; j
< total
; ++j
)
997 active
[j
] |= !isl_int_is_zero(ls
->div
->row
[i
][2 + j
]);
1003 /* Given a local space "ls" of a set, create a local space
1004 * for the lift of the set. In particular, the result
1005 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
1006 * range of the wrapped map.
1008 __isl_give isl_local_space
*isl_local_space_lift(
1009 __isl_take isl_local_space
*ls
)
1011 ls
= isl_local_space_cow(ls
);
1015 ls
->dim
= isl_space_lift(ls
->dim
, ls
->div
->n_row
);
1016 ls
->div
= isl_mat_drop_rows(ls
->div
, 0, ls
->div
->n_row
);
1017 if (!ls
->dim
|| !ls
->div
)
1018 return isl_local_space_free(ls
);
1023 /* Construct a basic map that maps a set living in local space "ls"
1024 * to the corresponding lifted local space.
1026 __isl_give isl_basic_map
*isl_local_space_lifting(
1027 __isl_take isl_local_space
*ls
)
1029 isl_basic_map
*lifting
;
1030 isl_basic_set
*bset
;
1034 if (!isl_local_space_is_set(ls
))
1035 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1036 "lifting only defined on set spaces",
1037 return isl_local_space_free(ls
));
1039 bset
= isl_basic_set_from_local_space(ls
);
1040 lifting
= isl_basic_set_unwrap(isl_basic_set_lift(bset
));
1041 lifting
= isl_basic_map_domain_map(lifting
);
1042 lifting
= isl_basic_map_reverse(lifting
);
1047 /* Compute the preimage of "ls" under the function represented by "ma".
1048 * In other words, plug in "ma" in "ls". The result is a local space
1049 * that is part of the domain space of "ma".
1051 * If the divs in "ls" are represented as
1053 * floor((a_i(p) + b_i x + c_i(divs))/n_i)
1055 * and ma is represented by
1057 * x = D(p) + F(y) + G(divs')
1059 * then the resulting divs are
1061 * floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
1063 * We first copy over the divs from "ma" and then
1064 * we add the modified divs from "ls".
1066 __isl_give isl_local_space
*isl_local_space_preimage_multi_aff(
1067 __isl_take isl_local_space
*ls
, __isl_take isl_multi_aff
*ma
)
1071 isl_local_space
*res
= NULL
;
1072 int n_div_ls
, n_div_ma
;
1073 isl_int f
, c1
, c2
, g
;
1075 ma
= isl_multi_aff_align_divs(ma
);
1078 if (!isl_space_is_range_internal(ls
->dim
, ma
->space
))
1079 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1080 "spaces don't match", goto error
);
1082 n_div_ls
= isl_local_space_dim(ls
, isl_dim_div
);
1083 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
1085 space
= isl_space_domain(isl_multi_aff_get_space(ma
));
1086 res
= isl_local_space_alloc(space
, n_div_ma
+ n_div_ls
);
1091 isl_mat_free(res
->div
);
1092 res
->div
= isl_mat_copy(ma
->p
[0]->ls
->div
);
1093 res
->div
= isl_mat_add_zero_cols(res
->div
, n_div_ls
);
1094 res
->div
= isl_mat_add_rows(res
->div
, n_div_ls
);
1104 for (i
= 0; i
< ls
->div
->n_row
; ++i
) {
1105 if (isl_int_is_zero(ls
->div
->row
[i
][0])) {
1106 isl_int_set_si(res
->div
->row
[n_div_ma
+ i
][0], 0);
1109 isl_seq_preimage(res
->div
->row
[n_div_ma
+ i
], ls
->div
->row
[i
],
1110 ma
, n_div_ma
, n_div_ls
, f
, c1
, c2
, g
, 1);
1111 normalize_div(res
, n_div_ma
+ i
);
1119 isl_local_space_free(ls
);
1120 isl_multi_aff_free(ma
);
1123 isl_local_space_free(ls
);
1124 isl_multi_aff_free(ma
);
1125 isl_local_space_free(res
);