2 * Copyright 2011 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 #include <isl_ctx_private.h>
12 #include <isl_map_private.h>
13 #include <isl_local_space_private.h>
14 #include <isl_dim_private.h>
15 #include <isl_mat_private.h>
18 isl_ctx
*isl_local_space_get_ctx(__isl_keep isl_local_space
*ls
)
20 return ls
? ls
->dim
->ctx
: NULL
;
23 __isl_give isl_local_space
*isl_local_space_alloc_div(__isl_take isl_dim
*dim
,
24 __isl_take isl_mat
*div
)
27 isl_local_space
*ls
= NULL
;
32 ctx
= isl_dim_get_ctx(dim
);
33 ls
= isl_calloc_type(ctx
, struct isl_local_space
);
44 isl_local_space_free(ls
);
48 __isl_give isl_local_space
*isl_local_space_alloc(__isl_take isl_dim
*dim
,
58 total
= isl_dim_total(dim
);
60 ctx
= isl_dim_get_ctx(dim
);
61 div
= isl_mat_alloc(ctx
, n_div
, 1 + 1 + total
+ n_div
);
62 return isl_local_space_alloc_div(dim
, div
);
65 __isl_give isl_local_space
*isl_local_space_from_dim(__isl_take isl_dim
*dim
)
67 return isl_local_space_alloc(dim
, 0);
70 __isl_give isl_local_space
*isl_local_space_copy(__isl_keep isl_local_space
*ls
)
79 __isl_give isl_local_space
*isl_local_space_dup(__isl_keep isl_local_space
*ls
)
84 return isl_local_space_alloc_div(isl_dim_copy(ls
->dim
),
85 isl_mat_copy(ls
->div
));
89 __isl_give isl_local_space
*isl_local_space_cow(__isl_take isl_local_space
*ls
)
97 return isl_local_space_dup(ls
);
100 void *isl_local_space_free(__isl_take isl_local_space
*ls
)
108 isl_dim_free(ls
->dim
);
109 isl_mat_free(ls
->div
);
116 /* Return true if the two local spaces are identical, with identical
117 * expressions for the integer divisions.
119 int isl_local_space_is_equal(__isl_keep isl_local_space
*ls1
,
120 __isl_keep isl_local_space
*ls2
)
127 equal
= isl_dim_equal(ls1
->dim
, ls2
->dim
);
128 if (equal
< 0 || !equal
)
131 if (!isl_local_space_divs_known(ls1
))
133 if (!isl_local_space_divs_known(ls2
))
136 return isl_mat_is_equal(ls1
->div
, ls2
->div
);
139 int isl_local_space_dim(__isl_keep isl_local_space
*ls
,
140 enum isl_dim_type type
)
144 if (type
== isl_dim_div
)
145 return ls
->div
->n_row
;
146 if (type
== isl_dim_all
)
147 return isl_dim_size(ls
->dim
, isl_dim_all
) + ls
->div
->n_row
;
148 return isl_dim_size(ls
->dim
, type
);
151 unsigned isl_local_space_offset(__isl_keep isl_local_space
*ls
,
152 enum isl_dim_type type
)
161 case isl_dim_cst
: return 0;
162 case isl_dim_param
: return 1;
163 case isl_dim_in
: return 1 + dim
->nparam
;
164 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
165 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
170 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space
*ls
,
171 enum isl_dim_type type
, unsigned pos
)
173 return ls
? isl_dim_get_name(ls
->dim
, type
, pos
) : NULL
;
176 __isl_give isl_div
*isl_local_space_get_div(__isl_keep isl_local_space
*ls
,
184 if (pos
< 0 || pos
>= ls
->div
->n_row
)
185 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
186 "index out of bounds", return NULL
);
188 if (isl_int_is_zero(ls
->div
->row
[pos
][0]))
189 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
190 "expression of div unknown", return NULL
);
192 bmap
= isl_basic_map_from_local_space(isl_local_space_copy(ls
));
193 return isl_basic_map_div(bmap
, pos
);
196 __isl_give isl_dim
*isl_local_space_get_dim(__isl_keep isl_local_space
*ls
)
201 return isl_dim_copy(ls
->dim
);
204 __isl_give isl_local_space
*isl_local_space_set_dim_name(
205 __isl_take isl_local_space
*ls
,
206 enum isl_dim_type type
, unsigned pos
, const char *s
)
208 ls
= isl_local_space_cow(ls
);
211 ls
->dim
= isl_dim_set_name(ls
->dim
, type
, pos
, s
);
213 return isl_local_space_free(ls
);
218 __isl_give isl_local_space
*isl_local_space_reset_dim(
219 __isl_take isl_local_space
*ls
, __isl_take isl_dim
*dim
)
221 ls
= isl_local_space_cow(ls
);
225 isl_dim_free(ls
->dim
);
230 isl_local_space_free(ls
);
235 /* Reorder the columns of the given div definitions according to the
237 * The order of the divs themselves is assumed not to change.
239 static __isl_give isl_mat
*reorder_divs(__isl_take isl_mat
*div
,
240 __isl_take isl_reordering
*r
)
249 extra
= isl_dim_total(r
->dim
) + div
->n_row
- r
->len
;
250 mat
= isl_mat_alloc(div
->ctx
, div
->n_row
, div
->n_col
+ extra
);
254 for (i
= 0; i
< div
->n_row
; ++i
) {
255 isl_seq_cpy(mat
->row
[i
], div
->row
[i
], 2);
256 isl_seq_clr(mat
->row
[i
] + 2, mat
->n_col
- 2);
257 for (j
= 0; j
< r
->len
; ++j
)
258 isl_int_set(mat
->row
[i
][2 + r
->pos
[j
]],
262 isl_reordering_free(r
);
266 isl_reordering_free(r
);
271 /* Reorder the dimensions of "ls" according to the given reordering.
272 * The reordering r is assumed to have been extended with the local
273 * variables, leaving them in the same order.
275 __isl_give isl_local_space
*isl_local_space_realign(
276 __isl_take isl_local_space
*ls
, __isl_take isl_reordering
*r
)
278 ls
= isl_local_space_cow(ls
);
282 ls
->div
= reorder_divs(ls
->div
, isl_reordering_copy(r
));
286 ls
= isl_local_space_reset_dim(ls
, isl_dim_copy(r
->dim
));
288 isl_reordering_free(r
);
291 isl_local_space_free(ls
);
292 isl_reordering_free(r
);
296 __isl_give isl_local_space
*isl_local_space_add_div(
297 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*div
)
299 ls
= isl_local_space_cow(ls
);
303 if (ls
->div
->n_col
!= div
->size
)
304 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
305 "incompatible dimensions", goto error
);
307 ls
->div
= isl_mat_add_zero_cols(ls
->div
, 1);
308 ls
->div
= isl_mat_add_rows(ls
->div
, 1);
312 isl_seq_cpy(ls
->div
->row
[ls
->div
->n_row
- 1], div
->el
, div
->size
);
313 isl_int_set_si(ls
->div
->row
[ls
->div
->n_row
- 1][div
->size
], 0);
318 isl_local_space_free(ls
);
323 __isl_give isl_local_space
*isl_local_space_replace_divs(
324 __isl_take isl_local_space
*ls
, __isl_take isl_mat
*div
)
326 ls
= isl_local_space_cow(ls
);
331 isl_mat_free(ls
->div
);
336 isl_local_space_free(ls
);
340 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
343 static void expand_row(__isl_keep isl_mat
*dst
, int d
,
344 __isl_keep isl_mat
*src
, int s
, int *exp
)
347 unsigned c
= src
->n_col
- src
->n_row
;
349 isl_seq_cpy(dst
->row
[d
], src
->row
[s
], c
);
350 isl_seq_clr(dst
->row
[d
] + c
, dst
->n_col
- c
);
352 for (i
= 0; i
< s
; ++i
)
353 isl_int_set(dst
->row
[d
][c
+ exp
[i
]], src
->row
[s
][c
+ i
]);
356 /* Compare (known) divs.
357 * Return non-zero if at least one of the two divs is unknown.
359 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
363 if (isl_int_is_zero(div
->row
[j
][0]))
365 if (isl_int_is_zero(div
->row
[i
][0]))
368 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
369 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
374 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
377 /* Combine the two lists of divs into a single list.
378 * For each row i in div1, exp1[i] is set to the position of the corresponding
379 * row in the result. Similarly for div2 and exp2.
380 * This function guarantees
382 * exp1[i+1] > exp1[i]
383 * For optimal merging, the two input list should have been sorted.
385 __isl_give isl_mat
*isl_merge_divs(__isl_keep isl_mat
*div1
,
386 __isl_keep isl_mat
*div2
, int *exp1
, int *exp2
)
390 unsigned d
= div1
->n_col
- div1
->n_row
;
392 div
= isl_mat_alloc(div1
->ctx
, 1 + div1
->n_row
+ div2
->n_row
,
393 d
+ div1
->n_row
+ div2
->n_row
);
397 for (i
= 0, j
= 0, k
= 0; i
< div1
->n_row
&& j
< div2
->n_row
; ++k
) {
400 expand_row(div
, k
, div1
, i
, exp1
);
401 expand_row(div
, k
+ 1, div2
, j
, exp2
);
403 cmp
= cmp_row(div
, k
, k
+ 1);
407 } else if (cmp
< 0) {
411 isl_seq_cpy(div
->row
[k
], div
->row
[k
+ 1], div
->n_col
);
414 for (; i
< div1
->n_row
; ++i
, ++k
) {
415 expand_row(div
, k
, div1
, i
, exp1
);
418 for (; j
< div2
->n_row
; ++j
, ++k
) {
419 expand_row(div
, k
, div2
, j
, exp2
);
429 int isl_local_space_divs_known(__isl_keep isl_local_space
*ls
)
436 for (i
= 0; i
< ls
->div
->n_row
; ++i
)
437 if (isl_int_is_zero(ls
->div
->row
[i
][0]))
443 /* Construct a local space for a map that has the given local
444 * space as domain and that has a zero-dimensional range.
446 __isl_give isl_local_space
*isl_local_space_from_domain(
447 __isl_take isl_local_space
*ls
)
449 ls
= isl_local_space_cow(ls
);
452 ls
->dim
= isl_dim_from_domain(ls
->dim
);
454 return isl_local_space_free(ls
);
458 __isl_give isl_local_space
*isl_local_space_add_dims(
459 __isl_take isl_local_space
*ls
, enum isl_dim_type type
, unsigned n
)
465 pos
= isl_local_space_dim(ls
, type
);
466 return isl_local_space_insert_dims(ls
, type
, pos
, n
);
469 /* Remove common factor of non-constant terms and denominator.
471 static void normalize_div(__isl_keep isl_local_space
*ls
, int div
)
473 isl_ctx
*ctx
= ls
->div
->ctx
;
474 unsigned total
= ls
->div
->n_col
- 2;
476 isl_seq_gcd(ls
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
477 isl_int_gcd(ctx
->normalize_gcd
,
478 ctx
->normalize_gcd
, ls
->div
->row
[div
][0]);
479 if (isl_int_is_one(ctx
->normalize_gcd
))
482 isl_seq_scale_down(ls
->div
->row
[div
] + 2, ls
->div
->row
[div
] + 2,
483 ctx
->normalize_gcd
, total
);
484 isl_int_divexact(ls
->div
->row
[div
][0], ls
->div
->row
[div
][0],
486 isl_int_fdiv_q(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1],
490 /* Exploit the equalities in "eq" to simplify the expressions of
491 * the integer divisions in "ls".
492 * The integer divisions in "ls" are assumed to appear as regular
493 * dimensions in "eq".
495 __isl_give isl_local_space
*isl_local_space_substitute_equalities(
496 __isl_take isl_local_space
*ls
, __isl_take isl_basic_set
*eq
)
502 ls
= isl_local_space_cow(ls
);
506 total
= isl_dim_total(eq
->dim
);
507 if (isl_local_space_dim(ls
, isl_dim_all
) != total
)
508 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
509 "dimensions don't match", goto error
);
512 for (i
= 0; i
< eq
->n_eq
; ++i
) {
513 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
514 if (j
< 0 || j
== 0 || j
>= total
)
517 for (k
= 0; k
< ls
->div
->n_row
; ++k
) {
518 if (isl_int_is_zero(ls
->div
->row
[k
][1 + j
]))
520 isl_seq_elim(ls
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
521 &ls
->div
->row
[k
][0]);
522 normalize_div(ls
, k
);
526 isl_basic_set_free(eq
);
529 isl_basic_set_free(eq
);
530 isl_local_space_free(ls
);
534 int isl_local_space_is_named_or_nested(__isl_keep isl_local_space
*ls
,
535 enum isl_dim_type type
)
539 return isl_dim_is_named_or_nested(ls
->dim
, type
);
542 __isl_give isl_local_space
*isl_local_space_drop_dims(
543 __isl_take isl_local_space
*ls
,
544 enum isl_dim_type type
, unsigned first
, unsigned n
)
550 if (n
== 0 && !isl_local_space_is_named_or_nested(ls
, type
))
553 ctx
= isl_local_space_get_ctx(ls
);
554 if (first
+ n
> isl_local_space_dim(ls
, type
))
555 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
556 return isl_local_space_free(ls
));
558 ls
= isl_local_space_cow(ls
);
562 if (type
== isl_dim_div
) {
563 ls
->div
= isl_mat_drop_rows(ls
->div
, first
, n
);
565 ls
->dim
= isl_dim_drop(ls
->dim
, type
, first
, n
);
567 return isl_local_space_free(ls
);
570 first
+= 1 + isl_local_space_offset(ls
, type
);
571 ls
->div
= isl_mat_drop_cols(ls
->div
, first
, n
);
573 return isl_local_space_free(ls
);
578 __isl_give isl_local_space
*isl_local_space_insert_dims(
579 __isl_take isl_local_space
*ls
,
580 enum isl_dim_type type
, unsigned first
, unsigned n
)
586 if (n
== 0 && !isl_local_space_is_named_or_nested(ls
, type
))
589 ctx
= isl_local_space_get_ctx(ls
);
590 if (first
> isl_local_space_dim(ls
, type
))
591 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
592 return isl_local_space_free(ls
));
594 ls
= isl_local_space_cow(ls
);
598 if (type
== isl_dim_div
) {
599 ls
->div
= isl_mat_insert_zero_rows(ls
->div
, first
, n
);
601 ls
->dim
= isl_dim_insert(ls
->dim
, type
, first
, n
);
603 return isl_local_space_free(ls
);
606 first
+= 1 + isl_local_space_offset(ls
, type
);
607 ls
->div
= isl_mat_insert_zero_cols(ls
->div
, first
, n
);
609 return isl_local_space_free(ls
);