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>
21 #include <isl_local.h>
23 isl_ctx
*isl_local_space_get_ctx(__isl_keep isl_local_space
*ls
)
25 return ls
? ls
->dim
->ctx
: NULL
;
28 /* Return a hash value that digests "ls".
30 uint32_t isl_local_space_get_hash(__isl_keep isl_local_space
*ls
)
32 uint32_t hash
, space_hash
, div_hash
;
37 hash
= isl_hash_init();
38 space_hash
= isl_space_get_hash(ls
->dim
);
39 isl_hash_hash(hash
, space_hash
);
40 div_hash
= isl_mat_get_hash(ls
->div
);
41 isl_hash_hash(hash
, div_hash
);
46 __isl_give isl_local_space
*isl_local_space_alloc_div(__isl_take isl_space
*dim
,
47 __isl_take isl_mat
*div
)
50 isl_local_space
*ls
= NULL
;
55 ctx
= isl_space_get_ctx(dim
);
56 ls
= isl_calloc_type(ctx
, struct isl_local_space
);
68 isl_local_space_free(ls
);
72 __isl_give isl_local_space
*isl_local_space_alloc(__isl_take isl_space
*dim
,
82 total
= isl_space_dim(dim
, isl_dim_all
);
84 ctx
= isl_space_get_ctx(dim
);
85 div
= isl_mat_alloc(ctx
, n_div
, 1 + 1 + total
+ n_div
);
86 return isl_local_space_alloc_div(dim
, div
);
89 __isl_give isl_local_space
*isl_local_space_from_space(__isl_take isl_space
*dim
)
91 return isl_local_space_alloc(dim
, 0);
94 __isl_give isl_local_space
*isl_local_space_copy(__isl_keep isl_local_space
*ls
)
103 __isl_give isl_local_space
*isl_local_space_dup(__isl_keep isl_local_space
*ls
)
108 return isl_local_space_alloc_div(isl_space_copy(ls
->dim
),
109 isl_mat_copy(ls
->div
));
113 __isl_give isl_local_space
*isl_local_space_cow(__isl_take isl_local_space
*ls
)
121 return isl_local_space_dup(ls
);
124 __isl_null isl_local_space
*isl_local_space_free(
125 __isl_take isl_local_space
*ls
)
133 isl_space_free(ls
->dim
);
134 isl_mat_free(ls
->div
);
141 /* Is the local space that of a parameter domain?
143 isl_bool
isl_local_space_is_params(__isl_keep isl_local_space
*ls
)
146 return isl_bool_error
;
147 return isl_space_is_params(ls
->dim
);
150 /* Is the local space that of a set?
152 isl_bool
isl_local_space_is_set(__isl_keep isl_local_space
*ls
)
154 return ls
? isl_space_is_set(ls
->dim
) : isl_bool_error
;
157 /* Do "ls1" and "ls2" have the same space?
159 isl_bool
isl_local_space_has_equal_space(__isl_keep isl_local_space
*ls1
,
160 __isl_keep isl_local_space
*ls2
)
163 return isl_bool_error
;
165 return isl_space_is_equal(ls1
->dim
, ls2
->dim
);
168 /* Return true if the two local spaces are identical, with identical
169 * expressions for the integer divisions.
171 isl_bool
isl_local_space_is_equal(__isl_keep isl_local_space
*ls1
,
172 __isl_keep isl_local_space
*ls2
)
176 equal
= isl_local_space_has_equal_space(ls1
, ls2
);
177 if (equal
< 0 || !equal
)
180 if (!isl_local_space_divs_known(ls1
))
181 return isl_bool_false
;
182 if (!isl_local_space_divs_known(ls2
))
183 return isl_bool_false
;
185 return isl_mat_is_equal(ls1
->div
, ls2
->div
);
188 /* Compare two isl_local_spaces.
190 * Return -1 if "ls1" is "smaller" than "ls2", 1 if "ls1" is "greater"
191 * than "ls2" and 0 if they are equal.
193 int isl_local_space_cmp(__isl_keep isl_local_space
*ls1
,
194 __isl_keep isl_local_space
*ls2
)
205 cmp
= isl_space_cmp(ls1
->dim
, ls2
->dim
);
209 return isl_local_cmp(ls1
->div
, ls2
->div
);
212 int isl_local_space_dim(__isl_keep isl_local_space
*ls
,
213 enum isl_dim_type type
)
217 if (type
== isl_dim_div
)
218 return ls
->div
->n_row
;
219 if (type
== isl_dim_all
)
220 return isl_space_dim(ls
->dim
, isl_dim_all
) + ls
->div
->n_row
;
221 return isl_space_dim(ls
->dim
, type
);
224 unsigned isl_local_space_offset(__isl_keep isl_local_space
*ls
,
225 enum isl_dim_type type
)
234 case isl_dim_cst
: return 0;
235 case isl_dim_param
: return 1;
236 case isl_dim_in
: return 1 + dim
->nparam
;
237 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
238 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
243 /* Return the position of the dimension of the given type and name
245 * Return -1 if no such dimension can be found.
247 int isl_local_space_find_dim_by_name(__isl_keep isl_local_space
*ls
,
248 enum isl_dim_type type
, const char *name
)
252 if (type
== isl_dim_div
)
254 return isl_space_find_dim_by_name(ls
->dim
, type
, name
);
257 /* Does the given dimension have a name?
259 isl_bool
isl_local_space_has_dim_name(__isl_keep isl_local_space
*ls
,
260 enum isl_dim_type type
, unsigned pos
)
262 return ls
? isl_space_has_dim_name(ls
->dim
, type
, pos
) : isl_bool_error
;
265 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space
*ls
,
266 enum isl_dim_type type
, unsigned pos
)
268 return ls
? isl_space_get_dim_name(ls
->dim
, type
, pos
) : NULL
;
271 isl_bool
isl_local_space_has_dim_id(__isl_keep isl_local_space
*ls
,
272 enum isl_dim_type type
, unsigned pos
)
274 return ls
? isl_space_has_dim_id(ls
->dim
, type
, pos
) : isl_bool_error
;
277 __isl_give isl_id
*isl_local_space_get_dim_id(__isl_keep isl_local_space
*ls
,
278 enum isl_dim_type type
, unsigned pos
)
280 return ls
? isl_space_get_dim_id(ls
->dim
, type
, pos
) : NULL
;
283 __isl_give isl_aff
*isl_local_space_get_div(__isl_keep isl_local_space
*ls
,
291 if (pos
< 0 || pos
>= ls
->div
->n_row
)
292 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
293 "index out of bounds", return NULL
);
295 if (isl_int_is_zero(ls
->div
->row
[pos
][0]))
296 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
297 "expression of div unknown", return NULL
);
298 if (!isl_local_space_is_set(ls
))
299 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
300 "cannot represent divs of map spaces", return NULL
);
302 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
305 isl_seq_cpy(aff
->v
->el
, ls
->div
->row
[pos
], aff
->v
->size
);
309 __isl_give isl_space
*isl_local_space_get_space(__isl_keep isl_local_space
*ls
)
314 return isl_space_copy(ls
->dim
);
317 /* Replace the identifier of the tuple of type "type" by "id".
319 __isl_give isl_local_space
*isl_local_space_set_tuple_id(
320 __isl_take isl_local_space
*ls
,
321 enum isl_dim_type type
, __isl_take isl_id
*id
)
323 ls
= isl_local_space_cow(ls
);
326 ls
->dim
= isl_space_set_tuple_id(ls
->dim
, type
, id
);
328 return isl_local_space_free(ls
);
335 __isl_give isl_local_space
*isl_local_space_set_dim_name(
336 __isl_take isl_local_space
*ls
,
337 enum isl_dim_type type
, unsigned pos
, const char *s
)
339 ls
= isl_local_space_cow(ls
);
342 ls
->dim
= isl_space_set_dim_name(ls
->dim
, type
, pos
, s
);
344 return isl_local_space_free(ls
);
349 __isl_give isl_local_space
*isl_local_space_set_dim_id(
350 __isl_take isl_local_space
*ls
,
351 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
353 ls
= isl_local_space_cow(ls
);
356 ls
->dim
= isl_space_set_dim_id(ls
->dim
, type
, pos
, id
);
358 return isl_local_space_free(ls
);
366 __isl_give isl_local_space
*isl_local_space_reset_space(
367 __isl_take isl_local_space
*ls
, __isl_take isl_space
*dim
)
369 ls
= isl_local_space_cow(ls
);
373 isl_space_free(ls
->dim
);
378 isl_local_space_free(ls
);
383 /* Reorder the columns of the given div definitions according to the
385 * The order of the divs themselves is assumed not to change.
387 static __isl_give isl_mat
*reorder_divs(__isl_take isl_mat
*div
,
388 __isl_take isl_reordering
*r
)
397 extra
= isl_space_dim(r
->dim
, isl_dim_all
) + div
->n_row
- r
->len
;
398 mat
= isl_mat_alloc(div
->ctx
, div
->n_row
, div
->n_col
+ extra
);
402 for (i
= 0; i
< div
->n_row
; ++i
) {
403 isl_seq_cpy(mat
->row
[i
], div
->row
[i
], 2);
404 isl_seq_clr(mat
->row
[i
] + 2, mat
->n_col
- 2);
405 for (j
= 0; j
< r
->len
; ++j
)
406 isl_int_set(mat
->row
[i
][2 + r
->pos
[j
]],
410 isl_reordering_free(r
);
414 isl_reordering_free(r
);
419 /* Reorder the dimensions of "ls" according to the given reordering.
420 * The reordering r is assumed to have been extended with the local
421 * variables, leaving them in the same order.
423 __isl_give isl_local_space
*isl_local_space_realign(
424 __isl_take isl_local_space
*ls
, __isl_take isl_reordering
*r
)
426 ls
= isl_local_space_cow(ls
);
430 ls
->div
= reorder_divs(ls
->div
, isl_reordering_copy(r
));
434 ls
= isl_local_space_reset_space(ls
, isl_space_copy(r
->dim
));
436 isl_reordering_free(r
);
439 isl_local_space_free(ls
);
440 isl_reordering_free(r
);
444 __isl_give isl_local_space
*isl_local_space_add_div(
445 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*div
)
447 ls
= isl_local_space_cow(ls
);
451 if (ls
->div
->n_col
!= div
->size
)
452 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
453 "incompatible dimensions", goto error
);
455 ls
->div
= isl_mat_add_zero_cols(ls
->div
, 1);
456 ls
->div
= isl_mat_add_rows(ls
->div
, 1);
460 isl_seq_cpy(ls
->div
->row
[ls
->div
->n_row
- 1], div
->el
, div
->size
);
461 isl_int_set_si(ls
->div
->row
[ls
->div
->n_row
- 1][div
->size
], 0);
466 isl_local_space_free(ls
);
471 __isl_give isl_local_space
*isl_local_space_replace_divs(
472 __isl_take isl_local_space
*ls
, __isl_take isl_mat
*div
)
474 ls
= isl_local_space_cow(ls
);
479 isl_mat_free(ls
->div
);
484 isl_local_space_free(ls
);
488 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
491 static void expand_row(__isl_keep isl_mat
*dst
, int d
,
492 __isl_keep isl_mat
*src
, int s
, int *exp
)
495 unsigned c
= src
->n_col
- src
->n_row
;
497 isl_seq_cpy(dst
->row
[d
], src
->row
[s
], c
);
498 isl_seq_clr(dst
->row
[d
] + c
, dst
->n_col
- c
);
500 for (i
= 0; i
< s
; ++i
)
501 isl_int_set(dst
->row
[d
][c
+ exp
[i
]], src
->row
[s
][c
+ i
]);
504 /* Compare (known) divs.
505 * Return non-zero if at least one of the two divs is unknown.
506 * In particular, if both divs are unknown, we respect their
507 * current order. Otherwise, we sort the known div after the unknown
508 * div only if the known div depends on the unknown div.
510 static int cmp_row(isl_int
*row_i
, isl_int
*row_j
, int i
, int j
,
511 unsigned n_row
, unsigned n_col
)
514 int unknown_i
, unknown_j
;
516 unknown_i
= isl_int_is_zero(row_i
[0]);
517 unknown_j
= isl_int_is_zero(row_j
[0]);
519 if (unknown_i
&& unknown_j
)
523 li
= n_col
- n_row
+ i
;
525 li
= isl_seq_last_non_zero(row_i
, n_col
);
527 lj
= n_col
- n_row
+ j
;
529 lj
= isl_seq_last_non_zero(row_j
, n_col
);
534 return isl_seq_cmp(row_i
, row_j
, n_col
);
537 /* Call cmp_row for divs in a matrix.
539 int isl_mat_cmp_div(__isl_keep isl_mat
*div
, int i
, int j
)
541 return cmp_row(div
->row
[i
], div
->row
[j
], i
, j
, div
->n_row
, div
->n_col
);
544 /* Call cmp_row for divs in a basic map.
546 static int bmap_cmp_row(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
549 return cmp_row(bmap
->div
[i
], bmap
->div
[j
], i
, j
, bmap
->n_div
, total
);
552 /* Sort the divs in "bmap".
554 * We first make sure divs are placed after divs on which they depend.
555 * Then we perform a simple insertion sort based on the same ordering
556 * that is used in isl_merge_divs.
558 __isl_give isl_basic_map
*isl_basic_map_sort_divs(
559 __isl_take isl_basic_map
*bmap
)
564 bmap
= isl_basic_map_order_divs(bmap
);
567 if (bmap
->n_div
<= 1)
570 total
= 2 + isl_basic_map_total_dim(bmap
);
571 for (i
= 1; i
< bmap
->n_div
; ++i
) {
572 for (j
= i
- 1; j
>= 0; --j
) {
573 if (bmap_cmp_row(bmap
, j
, j
+ 1, total
) <= 0)
575 isl_basic_map_swap_div(bmap
, j
, j
+ 1);
582 /* Sort the divs in the basic maps of "map".
584 __isl_give isl_map
*isl_map_sort_divs(__isl_take isl_map
*map
)
586 return isl_map_inline_foreach_basic_map(map
, &isl_basic_map_sort_divs
);
589 /* Combine the two lists of divs into a single list.
590 * For each row i in div1, exp1[i] is set to the position of the corresponding
591 * row in the result. Similarly for div2 and exp2.
592 * This function guarantees
594 * exp1[i+1] > exp1[i]
595 * For optimal merging, the two input list should have been sorted.
597 __isl_give isl_mat
*isl_merge_divs(__isl_keep isl_mat
*div1
,
598 __isl_keep isl_mat
*div2
, int *exp1
, int *exp2
)
607 d
= div1
->n_col
- div1
->n_row
;
608 div
= isl_mat_alloc(div1
->ctx
, 1 + div1
->n_row
+ div2
->n_row
,
609 d
+ div1
->n_row
+ div2
->n_row
);
613 for (i
= 0, j
= 0, k
= 0; i
< div1
->n_row
&& j
< div2
->n_row
; ++k
) {
616 expand_row(div
, k
, div1
, i
, exp1
);
617 expand_row(div
, k
+ 1, div2
, j
, exp2
);
619 cmp
= isl_mat_cmp_div(div
, k
, k
+ 1);
623 } else if (cmp
< 0) {
627 isl_seq_cpy(div
->row
[k
], div
->row
[k
+ 1], div
->n_col
);
630 for (; i
< div1
->n_row
; ++i
, ++k
) {
631 expand_row(div
, k
, div1
, i
, exp1
);
634 for (; j
< div2
->n_row
; ++j
, ++k
) {
635 expand_row(div
, k
, div2
, j
, exp2
);
645 /* Swap divs "a" and "b" in "ls".
647 __isl_give isl_local_space
*isl_local_space_swap_div(
648 __isl_take isl_local_space
*ls
, int a
, int b
)
652 ls
= isl_local_space_cow(ls
);
655 if (a
< 0 || a
>= ls
->div
->n_row
|| b
< 0 || b
>= ls
->div
->n_row
)
656 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
657 "index out of bounds", return isl_local_space_free(ls
));
658 offset
= ls
->div
->n_col
- ls
->div
->n_row
;
659 ls
->div
= isl_mat_swap_cols(ls
->div
, offset
+ a
, offset
+ b
);
660 ls
->div
= isl_mat_swap_rows(ls
->div
, a
, b
);
662 return isl_local_space_free(ls
);
666 /* Construct a local space that contains all the divs in either
669 __isl_give isl_local_space
*isl_local_space_intersect(
670 __isl_take isl_local_space
*ls1
, __isl_take isl_local_space
*ls2
)
681 ctx
= isl_local_space_get_ctx(ls1
);
682 if (!isl_space_is_equal(ls1
->dim
, ls2
->dim
))
683 isl_die(ctx
, isl_error_invalid
,
684 "spaces should be identical", goto error
);
686 if (ls2
->div
->n_row
== 0) {
687 isl_local_space_free(ls2
);
691 if (ls1
->div
->n_row
== 0) {
692 isl_local_space_free(ls1
);
696 exp1
= isl_alloc_array(ctx
, int, ls1
->div
->n_row
);
697 exp2
= isl_alloc_array(ctx
, int, ls2
->div
->n_row
);
701 div
= isl_merge_divs(ls1
->div
, ls2
->div
, exp1
, exp2
);
705 equal
= isl_mat_is_equal(ls1
->div
, div
);
709 ls1
= isl_local_space_cow(ls1
);
715 isl_local_space_free(ls2
);
716 isl_mat_free(ls1
->div
);
723 isl_local_space_free(ls1
);
724 isl_local_space_free(ls2
);
728 /* Does "ls" have an explicit representation for div "div"?
730 isl_bool
isl_local_space_div_is_known(__isl_keep isl_local_space
*ls
, int div
)
733 return isl_bool_error
;
734 return isl_local_div_is_known(ls
->div
, div
);
737 /* Does "ls" have an explicit representation for all local variables?
739 isl_bool
isl_local_space_divs_known(__isl_keep isl_local_space
*ls
)
744 return isl_bool_error
;
746 for (i
= 0; i
< ls
->div
->n_row
; ++i
) {
747 isl_bool known
= isl_local_space_div_is_known(ls
, i
);
748 if (known
< 0 || !known
)
752 return isl_bool_true
;
755 __isl_give isl_local_space
*isl_local_space_domain(
756 __isl_take isl_local_space
*ls
)
758 ls
= isl_local_space_drop_dims(ls
, isl_dim_out
,
759 0, isl_local_space_dim(ls
, isl_dim_out
));
760 ls
= isl_local_space_cow(ls
);
763 ls
->dim
= isl_space_domain(ls
->dim
);
765 return isl_local_space_free(ls
);
769 __isl_give isl_local_space
*isl_local_space_range(
770 __isl_take isl_local_space
*ls
)
772 ls
= isl_local_space_drop_dims(ls
, isl_dim_in
,
773 0, isl_local_space_dim(ls
, isl_dim_in
));
774 ls
= isl_local_space_cow(ls
);
778 ls
->dim
= isl_space_range(ls
->dim
);
780 return isl_local_space_free(ls
);
784 /* Construct a local space for a map that has the given local
785 * space as domain and that has a zero-dimensional range.
787 __isl_give isl_local_space
*isl_local_space_from_domain(
788 __isl_take isl_local_space
*ls
)
790 ls
= isl_local_space_cow(ls
);
793 ls
->dim
= isl_space_from_domain(ls
->dim
);
795 return isl_local_space_free(ls
);
799 __isl_give isl_local_space
*isl_local_space_add_dims(
800 __isl_take isl_local_space
*ls
, enum isl_dim_type type
, unsigned n
)
806 pos
= isl_local_space_dim(ls
, type
);
807 return isl_local_space_insert_dims(ls
, type
, pos
, n
);
810 /* Remove common factor of non-constant terms and denominator.
812 static void normalize_div(__isl_keep isl_local_space
*ls
, int div
)
814 isl_ctx
*ctx
= ls
->div
->ctx
;
815 unsigned total
= ls
->div
->n_col
- 2;
817 isl_seq_gcd(ls
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
818 isl_int_gcd(ctx
->normalize_gcd
,
819 ctx
->normalize_gcd
, ls
->div
->row
[div
][0]);
820 if (isl_int_is_one(ctx
->normalize_gcd
))
823 isl_seq_scale_down(ls
->div
->row
[div
] + 2, ls
->div
->row
[div
] + 2,
824 ctx
->normalize_gcd
, total
);
825 isl_int_divexact(ls
->div
->row
[div
][0], ls
->div
->row
[div
][0],
827 isl_int_fdiv_q(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1],
831 /* Exploit the equalities in "eq" to simplify the expressions of
832 * the integer divisions in "ls".
833 * The integer divisions in "ls" are assumed to appear as regular
834 * dimensions in "eq".
836 __isl_give isl_local_space
*isl_local_space_substitute_equalities(
837 __isl_take isl_local_space
*ls
, __isl_take isl_basic_set
*eq
)
846 total
= isl_space_dim(eq
->dim
, isl_dim_all
);
847 if (isl_local_space_dim(ls
, isl_dim_all
) != total
)
848 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
849 "spaces don't match", goto error
);
852 for (i
= 0; i
< eq
->n_eq
; ++i
) {
853 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
854 if (j
< 0 || j
== 0 || j
>= total
)
857 for (k
= 0; k
< ls
->div
->n_row
; ++k
) {
858 if (isl_int_is_zero(ls
->div
->row
[k
][1 + j
]))
860 ls
= isl_local_space_cow(ls
);
863 ls
->div
= isl_mat_cow(ls
->div
);
866 isl_seq_elim(ls
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
867 &ls
->div
->row
[k
][0]);
868 normalize_div(ls
, k
);
872 isl_basic_set_free(eq
);
875 isl_basic_set_free(eq
);
876 isl_local_space_free(ls
);
880 /* Plug in the affine expressions "subs" of length "subs_len" (including
881 * the denominator and the constant term) into the variable at position "pos"
882 * of the "n" div expressions starting at "first".
884 * Let i be the dimension to replace and let "subs" be of the form
888 * Any integer division starting at "first" with a non-zero coefficient for i,
894 * floor((a f + d g)/(m d))
896 __isl_give isl_local_space
*isl_local_space_substitute_seq(
897 __isl_take isl_local_space
*ls
,
898 enum isl_dim_type type
, unsigned pos
, isl_int
*subs
, int subs_len
,
906 ls
= isl_local_space_cow(ls
);
909 ls
->div
= isl_mat_cow(ls
->div
);
911 return isl_local_space_free(ls
);
913 if (first
+ n
> ls
->div
->n_row
)
914 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
915 "index out of bounds", return isl_local_space_free(ls
));
917 pos
+= isl_local_space_offset(ls
, type
);
920 for (i
= first
; i
< first
+ n
; ++i
) {
921 if (isl_int_is_zero(ls
->div
->row
[i
][1 + pos
]))
923 isl_seq_substitute(ls
->div
->row
[i
], pos
, subs
,
924 ls
->div
->n_col
, subs_len
, v
);
925 normalize_div(ls
, i
);
932 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
935 * Let i be the dimension to replace and let "subs" be of the form
939 * Any integer division with a non-zero coefficient for i,
945 * floor((a f + d g)/(m d))
947 __isl_give isl_local_space
*isl_local_space_substitute(
948 __isl_take isl_local_space
*ls
,
949 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
951 ls
= isl_local_space_cow(ls
);
953 return isl_local_space_free(ls
);
955 if (!isl_space_is_equal(ls
->dim
, subs
->ls
->dim
))
956 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
957 "spaces don't match", return isl_local_space_free(ls
));
958 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
959 isl_die(isl_local_space_get_ctx(ls
), isl_error_unsupported
,
960 "cannot handle divs yet",
961 return isl_local_space_free(ls
));
963 return isl_local_space_substitute_seq(ls
, type
, pos
, subs
->v
->el
,
964 subs
->v
->size
, 0, ls
->div
->n_row
);
967 int isl_local_space_is_named_or_nested(__isl_keep isl_local_space
*ls
,
968 enum isl_dim_type type
)
972 return isl_space_is_named_or_nested(ls
->dim
, type
);
975 __isl_give isl_local_space
*isl_local_space_drop_dims(
976 __isl_take isl_local_space
*ls
,
977 enum isl_dim_type type
, unsigned first
, unsigned n
)
983 if (n
== 0 && !isl_local_space_is_named_or_nested(ls
, type
))
986 ctx
= isl_local_space_get_ctx(ls
);
987 if (first
+ n
> isl_local_space_dim(ls
, type
))
988 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
989 return isl_local_space_free(ls
));
991 ls
= isl_local_space_cow(ls
);
995 if (type
== isl_dim_div
) {
996 ls
->div
= isl_mat_drop_rows(ls
->div
, first
, n
);
998 ls
->dim
= isl_space_drop_dims(ls
->dim
, type
, first
, n
);
1000 return isl_local_space_free(ls
);
1003 first
+= 1 + isl_local_space_offset(ls
, type
);
1004 ls
->div
= isl_mat_drop_cols(ls
->div
, first
, n
);
1006 return isl_local_space_free(ls
);
1011 __isl_give isl_local_space
*isl_local_space_insert_dims(
1012 __isl_take isl_local_space
*ls
,
1013 enum isl_dim_type type
, unsigned first
, unsigned n
)
1019 if (n
== 0 && !isl_local_space_is_named_or_nested(ls
, type
))
1022 ctx
= isl_local_space_get_ctx(ls
);
1023 if (first
> isl_local_space_dim(ls
, type
))
1024 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
1025 return isl_local_space_free(ls
));
1027 ls
= isl_local_space_cow(ls
);
1031 if (type
== isl_dim_div
) {
1032 ls
->div
= isl_mat_insert_zero_rows(ls
->div
, first
, n
);
1034 ls
->dim
= isl_space_insert_dims(ls
->dim
, type
, first
, n
);
1036 return isl_local_space_free(ls
);
1039 first
+= 1 + isl_local_space_offset(ls
, type
);
1040 ls
->div
= isl_mat_insert_zero_cols(ls
->div
, first
, n
);
1042 return isl_local_space_free(ls
);
1047 /* Check if the constraints pointed to by "constraint" is a div
1048 * constraint corresponding to div "div" in "ls".
1050 * That is, if div = floor(f/m), then check if the constraint is
1054 * -(f-(m-1)) + m d >= 0
1056 int isl_local_space_is_div_constraint(__isl_keep isl_local_space
*ls
,
1057 isl_int
*constraint
, unsigned div
)
1064 if (isl_int_is_zero(ls
->div
->row
[div
][0]))
1067 pos
= isl_local_space_offset(ls
, isl_dim_div
) + div
;
1069 if (isl_int_eq(constraint
[pos
], ls
->div
->row
[div
][0])) {
1071 isl_int_sub(ls
->div
->row
[div
][1],
1072 ls
->div
->row
[div
][1], ls
->div
->row
[div
][0]);
1073 isl_int_add_ui(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1], 1);
1074 neg
= isl_seq_is_neg(constraint
, ls
->div
->row
[div
]+1, pos
);
1075 isl_int_sub_ui(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1], 1);
1076 isl_int_add(ls
->div
->row
[div
][1],
1077 ls
->div
->row
[div
][1], ls
->div
->row
[div
][0]);
1080 if (isl_seq_first_non_zero(constraint
+pos
+1,
1081 ls
->div
->n_row
-div
-1) != -1)
1083 } else if (isl_int_abs_eq(constraint
[pos
], ls
->div
->row
[div
][0])) {
1084 if (!isl_seq_eq(constraint
, ls
->div
->row
[div
]+1, pos
))
1086 if (isl_seq_first_non_zero(constraint
+pos
+1,
1087 ls
->div
->n_row
-div
-1) != -1)
1096 * Set active[i] to 1 if the dimension at position i is involved
1097 * in the linear expression l.
1099 int *isl_local_space_get_active(__isl_keep isl_local_space
*ls
, isl_int
*l
)
1107 ctx
= isl_local_space_get_ctx(ls
);
1108 total
= isl_local_space_dim(ls
, isl_dim_all
);
1109 active
= isl_calloc_array(ctx
, int, total
);
1110 if (total
&& !active
)
1113 for (i
= 0; i
< total
; ++i
)
1114 active
[i
] = !isl_int_is_zero(l
[i
]);
1116 offset
= isl_local_space_offset(ls
, isl_dim_div
) - 1;
1117 for (i
= ls
->div
->n_row
- 1; i
>= 0; --i
) {
1118 if (!active
[offset
+ i
])
1120 for (j
= 0; j
< total
; ++j
)
1121 active
[j
] |= !isl_int_is_zero(ls
->div
->row
[i
][2 + j
]);
1127 /* Given a local space "ls" of a set, create a local space
1128 * for the lift of the set. In particular, the result
1129 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
1130 * range of the wrapped map.
1132 __isl_give isl_local_space
*isl_local_space_lift(
1133 __isl_take isl_local_space
*ls
)
1135 ls
= isl_local_space_cow(ls
);
1139 ls
->dim
= isl_space_lift(ls
->dim
, ls
->div
->n_row
);
1140 ls
->div
= isl_mat_drop_rows(ls
->div
, 0, ls
->div
->n_row
);
1141 if (!ls
->dim
|| !ls
->div
)
1142 return isl_local_space_free(ls
);
1147 /* Construct a basic map that maps a set living in local space "ls"
1148 * to the corresponding lifted local space.
1150 __isl_give isl_basic_map
*isl_local_space_lifting(
1151 __isl_take isl_local_space
*ls
)
1153 isl_basic_map
*lifting
;
1154 isl_basic_set
*bset
;
1158 if (!isl_local_space_is_set(ls
))
1159 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1160 "lifting only defined on set spaces", goto error
);
1162 bset
= isl_basic_set_from_local_space(ls
);
1163 lifting
= isl_basic_set_unwrap(isl_basic_set_lift(bset
));
1164 lifting
= isl_basic_map_domain_map(lifting
);
1165 lifting
= isl_basic_map_reverse(lifting
);
1169 isl_local_space_free(ls
);
1173 /* Compute the preimage of "ls" under the function represented by "ma".
1174 * In other words, plug in "ma" in "ls". The result is a local space
1175 * that is part of the domain space of "ma".
1177 * If the divs in "ls" are represented as
1179 * floor((a_i(p) + b_i x + c_i(divs))/n_i)
1181 * and ma is represented by
1183 * x = D(p) + F(y) + G(divs')
1185 * then the resulting divs are
1187 * floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
1189 * We first copy over the divs from "ma" and then
1190 * we add the modified divs from "ls".
1192 __isl_give isl_local_space
*isl_local_space_preimage_multi_aff(
1193 __isl_take isl_local_space
*ls
, __isl_take isl_multi_aff
*ma
)
1197 isl_local_space
*res
= NULL
;
1198 int n_div_ls
, n_div_ma
;
1199 isl_int f
, c1
, c2
, g
;
1201 ma
= isl_multi_aff_align_divs(ma
);
1204 if (!isl_space_is_range_internal(ls
->dim
, ma
->space
))
1205 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1206 "spaces don't match", goto error
);
1208 n_div_ls
= isl_local_space_dim(ls
, isl_dim_div
);
1209 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
1211 space
= isl_space_domain(isl_multi_aff_get_space(ma
));
1212 res
= isl_local_space_alloc(space
, n_div_ma
+ n_div_ls
);
1217 isl_mat_free(res
->div
);
1218 res
->div
= isl_mat_copy(ma
->p
[0]->ls
->div
);
1219 res
->div
= isl_mat_add_zero_cols(res
->div
, n_div_ls
);
1220 res
->div
= isl_mat_add_rows(res
->div
, n_div_ls
);
1230 for (i
= 0; i
< ls
->div
->n_row
; ++i
) {
1231 if (isl_int_is_zero(ls
->div
->row
[i
][0])) {
1232 isl_int_set_si(res
->div
->row
[n_div_ma
+ i
][0], 0);
1235 isl_seq_preimage(res
->div
->row
[n_div_ma
+ i
], ls
->div
->row
[i
],
1236 ma
, 0, 0, n_div_ma
, n_div_ls
, f
, c1
, c2
, g
, 1);
1237 normalize_div(res
, n_div_ma
+ i
);
1245 isl_local_space_free(ls
);
1246 isl_multi_aff_free(ma
);
1249 isl_local_space_free(ls
);
1250 isl_multi_aff_free(ma
);
1251 isl_local_space_free(res
);
1255 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "ls"
1256 * to dimensions of "dst_type" at "dst_pos".
1258 * Moving to/from local dimensions is not allowed.
1259 * We currently assume that the dimension type changes.
1261 __isl_give isl_local_space
*isl_local_space_move_dims(
1262 __isl_take isl_local_space
*ls
,
1263 enum isl_dim_type dst_type
, unsigned dst_pos
,
1264 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1272 !isl_local_space_is_named_or_nested(ls
, src_type
) &&
1273 !isl_local_space_is_named_or_nested(ls
, dst_type
))
1276 if (src_pos
+ n
> isl_local_space_dim(ls
, src_type
))
1277 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1278 "range out of bounds", return isl_local_space_free(ls
));
1279 if (dst_pos
> isl_local_space_dim(ls
, dst_type
))
1280 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1281 "position out of bounds",
1282 return isl_local_space_free(ls
));
1283 if (src_type
== isl_dim_div
)
1284 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1285 "cannot move divs", return isl_local_space_free(ls
));
1286 if (dst_type
== isl_dim_div
)
1287 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1288 "cannot move to divs", return isl_local_space_free(ls
));
1289 if (dst_type
== src_type
&& dst_pos
== src_pos
)
1291 if (dst_type
== src_type
)
1292 isl_die(isl_local_space_get_ctx(ls
), isl_error_unsupported
,
1293 "moving dims within the same type not supported",
1294 return isl_local_space_free(ls
));
1296 ls
= isl_local_space_cow(ls
);
1300 g_src_pos
= 1 + isl_local_space_offset(ls
, src_type
) + src_pos
;
1301 g_dst_pos
= 1 + isl_local_space_offset(ls
, dst_type
) + dst_pos
;
1302 if (dst_type
> src_type
)
1304 ls
->div
= isl_mat_move_cols(ls
->div
, g_dst_pos
, g_src_pos
, n
);
1306 return isl_local_space_free(ls
);
1307 ls
->dim
= isl_space_move_dims(ls
->dim
, dst_type
, dst_pos
,
1308 src_type
, src_pos
, n
);
1310 return isl_local_space_free(ls
);
1315 /* Remove any internal structure of the domain of "ls".
1316 * If there is any such internal structure in the input,
1317 * then the name of the corresponding space is also removed.
1319 __isl_give isl_local_space
*isl_local_space_flatten_domain(
1320 __isl_take isl_local_space
*ls
)
1325 if (!ls
->dim
->nested
[0])
1328 ls
= isl_local_space_cow(ls
);
1332 ls
->dim
= isl_space_flatten_domain(ls
->dim
);
1334 return isl_local_space_free(ls
);
1339 /* Remove any internal structure of the range of "ls".
1340 * If there is any such internal structure in the input,
1341 * then the name of the corresponding space is also removed.
1343 __isl_give isl_local_space
*isl_local_space_flatten_range(
1344 __isl_take isl_local_space
*ls
)
1349 if (!ls
->dim
->nested
[1])
1352 ls
= isl_local_space_cow(ls
);
1356 ls
->dim
= isl_space_flatten_range(ls
->dim
);
1358 return isl_local_space_free(ls
);
1363 /* Given the local space "ls" of a map, return the local space of a set
1364 * that lives in a space that wraps the space of "ls" and that has
1367 __isl_give isl_local_space
*isl_local_space_wrap(__isl_take isl_local_space
*ls
)
1369 ls
= isl_local_space_cow(ls
);
1373 ls
->dim
= isl_space_wrap(ls
->dim
);
1375 return isl_local_space_free(ls
);