2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 #include <isl_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl_aff_private.h>
15 #include <isl_space_private.h>
16 #include <isl_local_space_private.h>
17 #include <isl_mat_private.h>
18 #include <isl_list_private.h>
19 #include <isl/constraint.h>
22 #include <isl_config.h>
24 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
25 __isl_take isl_vec
*v
)
32 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
42 isl_local_space_free(ls
);
47 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
56 ctx
= isl_local_space_get_ctx(ls
);
57 if (!isl_local_space_divs_known(ls
))
58 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
60 if (!isl_local_space_is_set(ls
))
61 isl_die(ctx
, isl_error_invalid
,
62 "domain of affine expression should be a set",
65 total
= isl_local_space_dim(ls
, isl_dim_all
);
66 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
67 return isl_aff_alloc_vec(ls
, v
);
69 isl_local_space_free(ls
);
73 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
77 aff
= isl_aff_alloc(ls
);
81 isl_int_set_si(aff
->v
->el
[0], 1);
82 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
87 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
96 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
101 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
102 isl_vec_copy(aff
->v
));
105 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
113 return isl_aff_dup(aff
);
116 void *isl_aff_free(__isl_take isl_aff
*aff
)
124 isl_local_space_free(aff
->ls
);
125 isl_vec_free(aff
->v
);
132 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
134 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
137 /* Externally, an isl_aff has a map space, but internally, the
138 * ls field corresponds to the domain of that space.
140 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
144 if (type
== isl_dim_out
)
146 if (type
== isl_dim_in
)
148 return isl_local_space_dim(aff
->ls
, type
);
151 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
153 return aff
? isl_local_space_get_space(aff
->ls
) : NULL
;
156 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
161 space
= isl_local_space_get_space(aff
->ls
);
162 space
= isl_space_from_domain(space
);
163 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
167 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
168 __isl_keep isl_aff
*aff
)
170 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
173 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
178 ls
= isl_local_space_copy(aff
->ls
);
179 ls
= isl_local_space_from_domain(ls
);
180 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
184 /* Externally, an isl_aff has a map space, but internally, the
185 * ls field corresponds to the domain of that space.
187 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
188 enum isl_dim_type type
, unsigned pos
)
192 if (type
== isl_dim_out
)
194 if (type
== isl_dim_in
)
196 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
199 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
200 __isl_take isl_space
*dim
)
202 aff
= isl_aff_cow(aff
);
206 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
208 return isl_aff_free(aff
);
217 /* Reset the space of "aff". This function is called from isl_pw_templ.c
218 * and doesn't know if the space of an element object is represented
219 * directly or through its domain. It therefore passes along both.
221 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
222 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
224 isl_space_free(space
);
225 return isl_aff_reset_domain_space(aff
, domain
);
228 /* Reorder the coefficients of the affine expression based
229 * on the given reodering.
230 * The reordering r is assumed to have been extended with the local
233 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
234 __isl_take isl_reordering
*r
, int n_div
)
242 res
= isl_vec_alloc(vec
->ctx
,
243 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
244 isl_seq_cpy(res
->el
, vec
->el
, 2);
245 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
246 for (i
= 0; i
< r
->len
; ++i
)
247 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
249 isl_reordering_free(r
);
254 isl_reordering_free(r
);
258 /* Reorder the dimensions of the domain of "aff" according
259 * to the given reordering.
261 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
262 __isl_take isl_reordering
*r
)
264 aff
= isl_aff_cow(aff
);
268 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
269 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
270 aff
->ls
->div
->n_row
);
271 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
273 if (!aff
->v
|| !aff
->ls
)
274 return isl_aff_free(aff
);
279 isl_reordering_free(r
);
283 int isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
288 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
291 int isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
298 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
299 if (equal
< 0 || !equal
)
302 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
305 int isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
309 isl_int_set(*v
, aff
->v
->el
[0]);
313 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
317 isl_int_set(*v
, aff
->v
->el
[1]);
321 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
322 enum isl_dim_type type
, int pos
, isl_int
*v
)
327 if (type
== isl_dim_out
)
328 isl_die(aff
->v
->ctx
, isl_error_invalid
,
329 "output/set dimension does not have a coefficient",
331 if (type
== isl_dim_in
)
334 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
335 isl_die(aff
->v
->ctx
, isl_error_invalid
,
336 "position out of bounds", return -1);
338 pos
+= isl_local_space_offset(aff
->ls
, type
);
339 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
344 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
346 aff
= isl_aff_cow(aff
);
350 aff
->v
= isl_vec_cow(aff
->v
);
352 return isl_aff_free(aff
);
354 isl_int_set(aff
->v
->el
[0], v
);
359 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
361 aff
= isl_aff_cow(aff
);
365 aff
->v
= isl_vec_cow(aff
->v
);
367 return isl_aff_free(aff
);
369 isl_int_set(aff
->v
->el
[1], v
);
374 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
376 if (isl_int_is_zero(v
))
379 aff
= isl_aff_cow(aff
);
383 aff
->v
= isl_vec_cow(aff
->v
);
385 return isl_aff_free(aff
);
387 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
392 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
397 isl_int_set_si(t
, v
);
398 aff
= isl_aff_add_constant(aff
, t
);
404 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
406 aff
= isl_aff_cow(aff
);
410 aff
->v
= isl_vec_cow(aff
->v
);
412 return isl_aff_free(aff
);
414 isl_int_set_si(aff
->v
->el
[1], v
);
419 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
420 enum isl_dim_type type
, int pos
, isl_int v
)
425 if (type
== isl_dim_out
)
426 isl_die(aff
->v
->ctx
, isl_error_invalid
,
427 "output/set dimension does not have a coefficient",
428 return isl_aff_free(aff
));
429 if (type
== isl_dim_in
)
432 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
433 isl_die(aff
->v
->ctx
, isl_error_invalid
,
434 "position out of bounds", return isl_aff_free(aff
));
436 aff
= isl_aff_cow(aff
);
440 aff
->v
= isl_vec_cow(aff
->v
);
442 return isl_aff_free(aff
);
444 pos
+= isl_local_space_offset(aff
->ls
, type
);
445 isl_int_set(aff
->v
->el
[1 + pos
], v
);
450 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
451 enum isl_dim_type type
, int pos
, int v
)
456 if (type
== isl_dim_out
)
457 isl_die(aff
->v
->ctx
, isl_error_invalid
,
458 "output/set dimension does not have a coefficient",
459 return isl_aff_free(aff
));
460 if (type
== isl_dim_in
)
463 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
464 isl_die(aff
->v
->ctx
, isl_error_invalid
,
465 "position out of bounds", return isl_aff_free(aff
));
467 aff
= isl_aff_cow(aff
);
471 aff
->v
= isl_vec_cow(aff
->v
);
473 return isl_aff_free(aff
);
475 pos
+= isl_local_space_offset(aff
->ls
, type
);
476 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
481 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
482 enum isl_dim_type type
, int pos
, isl_int v
)
487 if (type
== isl_dim_out
)
488 isl_die(aff
->v
->ctx
, isl_error_invalid
,
489 "output/set dimension does not have a coefficient",
490 return isl_aff_free(aff
));
491 if (type
== isl_dim_in
)
494 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
495 isl_die(aff
->v
->ctx
, isl_error_invalid
,
496 "position out of bounds", return isl_aff_free(aff
));
498 aff
= isl_aff_cow(aff
);
502 aff
->v
= isl_vec_cow(aff
->v
);
504 return isl_aff_free(aff
);
506 pos
+= isl_local_space_offset(aff
->ls
, type
);
507 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
512 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
513 enum isl_dim_type type
, int pos
, int v
)
518 isl_int_set_si(t
, v
);
519 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
525 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
530 return isl_local_space_get_div(aff
->ls
, pos
);
533 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
535 aff
= isl_aff_cow(aff
);
538 aff
->v
= isl_vec_cow(aff
->v
);
540 return isl_aff_free(aff
);
542 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
547 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
551 aff
->v
= isl_vec_normalize(aff
->v
);
553 return isl_aff_free(aff
);
557 /* Given f, return floor(f).
558 * If f is an integer expression, then just return f.
559 * Otherwise, if f = g/m, write g = q m + r,
560 * create a new div d = [r/m] and return the expression q + d.
561 * The coefficients in r are taken to lie between -m/2 and m/2.
563 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
573 if (isl_int_is_one(aff
->v
->el
[0]))
576 aff
= isl_aff_cow(aff
);
580 aff
->v
= isl_vec_cow(aff
->v
);
581 div
= isl_vec_copy(aff
->v
);
582 div
= isl_vec_cow(div
);
584 return isl_aff_free(aff
);
586 ctx
= isl_aff_get_ctx(aff
);
587 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
588 for (i
= 1; i
< aff
->v
->size
; ++i
) {
589 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
590 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
591 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
592 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
593 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
597 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
599 return isl_aff_free(aff
);
602 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
604 return isl_aff_free(aff
);
605 isl_int_set_si(aff
->v
->el
[0], 1);
606 isl_int_set_si(aff
->v
->el
[size
], 1);
613 * aff mod m = aff - m * floor(aff/m)
615 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
619 res
= isl_aff_copy(aff
);
620 aff
= isl_aff_scale_down(aff
, m
);
621 aff
= isl_aff_floor(aff
);
622 aff
= isl_aff_scale(aff
, m
);
623 res
= isl_aff_sub(res
, aff
);
630 * pwaff mod m = pwaff - m * floor(pwaff/m)
632 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
636 res
= isl_pw_aff_copy(pwaff
);
637 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
638 pwaff
= isl_pw_aff_floor(pwaff
);
639 pwaff
= isl_pw_aff_scale(pwaff
, m
);
640 res
= isl_pw_aff_sub(res
, pwaff
);
645 /* Given f, return ceil(f).
646 * If f is an integer expression, then just return f.
647 * Otherwise, create a new div d = [-f] and return the expression -d.
649 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
654 if (isl_int_is_one(aff
->v
->el
[0]))
657 aff
= isl_aff_neg(aff
);
658 aff
= isl_aff_floor(aff
);
659 aff
= isl_aff_neg(aff
);
664 /* Apply the expansion computed by isl_merge_divs.
665 * The expansion itself is given by "exp" while the resulting
666 * list of divs is given by "div".
668 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
669 __isl_take isl_mat
*div
, int *exp
)
676 aff
= isl_aff_cow(aff
);
680 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
681 new_n_div
= isl_mat_rows(div
);
682 if (new_n_div
< old_n_div
)
683 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
684 "not an expansion", goto error
);
686 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
690 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
692 for (i
= new_n_div
- 1; i
>= 0; --i
) {
693 if (j
>= 0 && exp
[j
] == i
) {
695 isl_int_swap(aff
->v
->el
[offset
+ i
],
696 aff
->v
->el
[offset
+ j
]);
699 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
702 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
713 /* Add two affine expressions that live in the same local space.
715 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
716 __isl_take isl_aff
*aff2
)
720 aff1
= isl_aff_cow(aff1
);
724 aff1
->v
= isl_vec_cow(aff1
->v
);
730 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
731 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
732 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
733 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
734 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
735 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
736 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
748 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
749 __isl_take isl_aff
*aff2
)
759 ctx
= isl_aff_get_ctx(aff1
);
760 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
761 isl_die(ctx
, isl_error_invalid
,
762 "spaces don't match", goto error
);
764 if (aff1
->ls
->div
->n_row
== 0 && aff2
->ls
->div
->n_row
== 0)
765 return add_expanded(aff1
, aff2
);
767 exp1
= isl_alloc_array(ctx
, int, aff1
->ls
->div
->n_row
);
768 exp2
= isl_alloc_array(ctx
, int, aff2
->ls
->div
->n_row
);
772 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
773 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
774 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
778 return add_expanded(aff1
, aff2
);
787 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
788 __isl_take isl_aff
*aff2
)
790 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
793 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
797 if (isl_int_is_one(f
))
800 aff
= isl_aff_cow(aff
);
803 aff
->v
= isl_vec_cow(aff
->v
);
805 return isl_aff_free(aff
);
808 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
809 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
810 isl_int_divexact(gcd
, f
, gcd
);
811 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
817 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
821 if (isl_int_is_one(f
))
824 aff
= isl_aff_cow(aff
);
827 aff
->v
= isl_vec_cow(aff
->v
);
829 return isl_aff_free(aff
);
832 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
833 isl_int_gcd(gcd
, gcd
, f
);
834 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
835 isl_int_divexact(gcd
, f
, gcd
);
836 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
842 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
850 isl_int_set_ui(v
, f
);
851 aff
= isl_aff_scale_down(aff
, v
);
857 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
858 enum isl_dim_type type
, unsigned pos
, const char *s
)
860 aff
= isl_aff_cow(aff
);
863 if (type
== isl_dim_out
)
864 isl_die(aff
->v
->ctx
, isl_error_invalid
,
865 "cannot set name of output/set dimension",
866 return isl_aff_free(aff
));
867 if (type
== isl_dim_in
)
869 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
871 return isl_aff_free(aff
);
876 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
877 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
879 aff
= isl_aff_cow(aff
);
881 return isl_id_free(id
);
882 if (type
== isl_dim_out
)
883 isl_die(aff
->v
->ctx
, isl_error_invalid
,
884 "cannot set name of output/set dimension",
886 if (type
== isl_dim_in
)
888 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
890 return isl_aff_free(aff
);
899 /* Exploit the equalities in "eq" to simplify the affine expression
900 * and the expressions of the integer divisions in the local space.
901 * The integer divisions in this local space are assumed to appear
902 * as regular dimensions in "eq".
904 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
905 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
914 isl_basic_set_free(eq
);
918 aff
= isl_aff_cow(aff
);
922 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
923 isl_basic_set_copy(eq
));
927 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
929 for (i
= 0; i
< eq
->n_eq
; ++i
) {
930 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
931 if (j
< 0 || j
== 0 || j
>= total
)
934 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
938 isl_basic_set_free(eq
);
941 isl_basic_set_free(eq
);
946 /* Exploit the equalities in "eq" to simplify the affine expression
947 * and the expressions of the integer divisions in the local space.
949 static __isl_give isl_aff
*isl_aff_substitute_equalities(
950 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
956 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
958 eq
= isl_basic_set_add(eq
, isl_dim_set
, n_div
);
959 return isl_aff_substitute_equalities_lifted(aff
, eq
);
961 isl_basic_set_free(eq
);
966 /* Look for equalities among the variables shared by context and aff
967 * and the integer divisions of aff, if any.
968 * The equalities are then used to eliminate coefficients and/or integer
969 * divisions from aff.
971 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
972 __isl_take isl_set
*context
)
979 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
983 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
984 ls
= isl_aff_get_domain_local_space(aff
);
985 bset
= isl_basic_set_from_local_space(ls
);
986 bset
= isl_basic_set_lift(bset
);
987 bset
= isl_basic_set_flatten(bset
);
988 context
= isl_set_intersect(context
,
989 isl_set_from_basic_set(bset
));
992 hull
= isl_set_affine_hull(context
);
993 return isl_aff_substitute_equalities_lifted(aff
, hull
);
996 isl_set_free(context
);
1000 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
1001 __isl_take isl_set
*context
)
1003 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
1004 dom_context
= isl_set_intersect_params(dom_context
, context
);
1005 return isl_aff_gist(aff
, dom_context
);
1008 /* Return a basic set containing those elements in the space
1009 * of aff where it is non-negative.
1011 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
1013 isl_constraint
*ineq
;
1015 ineq
= isl_inequality_from_aff(aff
);
1017 return isl_basic_set_from_constraint(ineq
);
1020 /* Return a basic set containing those elements in the space
1021 * of aff where it is zero.
1023 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
1025 isl_constraint
*ineq
;
1027 ineq
= isl_equality_from_aff(aff
);
1029 return isl_basic_set_from_constraint(ineq
);
1032 /* Return a basic set containing those elements in the shared space
1033 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1035 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
1036 __isl_take isl_aff
*aff2
)
1038 aff1
= isl_aff_sub(aff1
, aff2
);
1040 return isl_aff_nonneg_basic_set(aff1
);
1043 /* Return a basic set containing those elements in the shared space
1044 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1046 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
1047 __isl_take isl_aff
*aff2
)
1049 return isl_aff_ge_basic_set(aff2
, aff1
);
1052 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
1053 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
1055 aff1
= isl_aff_add(aff1
, aff2
);
1056 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
1060 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
1068 /* Check whether the given affine expression has non-zero coefficient
1069 * for any dimension in the given range or if any of these dimensions
1070 * appear with non-zero coefficients in any of the integer divisions
1071 * involved in the affine expression.
1073 int isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
1074 enum isl_dim_type type
, unsigned first
, unsigned n
)
1086 ctx
= isl_aff_get_ctx(aff
);
1087 if (first
+ n
> isl_aff_dim(aff
, type
))
1088 isl_die(ctx
, isl_error_invalid
,
1089 "range out of bounds", return -1);
1091 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
1095 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
1096 for (i
= 0; i
< n
; ++i
)
1097 if (active
[first
+ i
]) {
1110 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
1111 enum isl_dim_type type
, unsigned first
, unsigned n
)
1117 if (type
== isl_dim_out
)
1118 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1119 "cannot drop output/set dimension",
1120 return isl_aff_free(aff
));
1121 if (type
== isl_dim_in
)
1123 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1126 ctx
= isl_aff_get_ctx(aff
);
1127 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
1128 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
1129 return isl_aff_free(aff
));
1131 aff
= isl_aff_cow(aff
);
1135 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
1137 return isl_aff_free(aff
);
1139 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1140 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
1142 return isl_aff_free(aff
);
1147 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
1148 enum isl_dim_type type
, unsigned first
, unsigned n
)
1154 if (type
== isl_dim_out
)
1155 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1156 "cannot insert output/set dimensions",
1157 return isl_aff_free(aff
));
1158 if (type
== isl_dim_in
)
1160 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1163 ctx
= isl_aff_get_ctx(aff
);
1164 if (first
> isl_local_space_dim(aff
->ls
, type
))
1165 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
1166 return isl_aff_free(aff
));
1168 aff
= isl_aff_cow(aff
);
1172 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
1174 return isl_aff_free(aff
);
1176 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1177 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
1179 return isl_aff_free(aff
);
1184 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
1185 enum isl_dim_type type
, unsigned n
)
1189 pos
= isl_aff_dim(aff
, type
);
1191 return isl_aff_insert_dims(aff
, type
, pos
, n
);
1194 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
1195 enum isl_dim_type type
, unsigned n
)
1199 pos
= isl_pw_aff_dim(pwaff
, type
);
1201 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
1204 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
1206 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
1207 return isl_pw_aff_alloc(dom
, aff
);
1211 #define PW isl_pw_aff
1215 #define EL_IS_ZERO is_empty
1219 #define IS_ZERO is_empty
1225 #define NO_MOVE_DIMS
1229 #include <isl_pw_templ.c>
1231 static __isl_give isl_set
*align_params_pw_pw_set_and(
1232 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
1233 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1234 __isl_take isl_pw_aff
*pwaff2
))
1236 if (!pwaff1
|| !pwaff2
)
1238 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
1239 pwaff2
->dim
, isl_dim_param
))
1240 return fn(pwaff1
, pwaff2
);
1241 if (!isl_space_has_named_params(pwaff1
->dim
) ||
1242 !isl_space_has_named_params(pwaff2
->dim
))
1243 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
1244 "unaligned unnamed parameters", goto error
);
1245 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
1246 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
1247 return fn(pwaff1
, pwaff2
);
1249 isl_pw_aff_free(pwaff1
);
1250 isl_pw_aff_free(pwaff2
);
1254 /* Compute a piecewise quasi-affine expression with a domain that
1255 * is the union of those of pwaff1 and pwaff2 and such that on each
1256 * cell, the quasi-affine expression is the better (according to cmp)
1257 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1258 * is defined on a given cell, then the associated expression
1259 * is the defined one.
1261 static __isl_give isl_pw_aff
*pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1262 __isl_take isl_pw_aff
*pwaff2
,
1263 __isl_give isl_basic_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
1264 __isl_take isl_aff
*aff2
))
1271 if (!pwaff1
|| !pwaff2
)
1274 ctx
= isl_space_get_ctx(pwaff1
->dim
);
1275 if (!isl_space_is_equal(pwaff1
->dim
, pwaff2
->dim
))
1276 isl_die(ctx
, isl_error_invalid
,
1277 "arguments should live in same space", goto error
);
1279 if (isl_pw_aff_is_empty(pwaff1
)) {
1280 isl_pw_aff_free(pwaff1
);
1284 if (isl_pw_aff_is_empty(pwaff2
)) {
1285 isl_pw_aff_free(pwaff2
);
1289 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
1290 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
1292 for (i
= 0; i
< pwaff1
->n
; ++i
) {
1293 set
= isl_set_copy(pwaff1
->p
[i
].set
);
1294 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1295 struct isl_set
*common
;
1298 common
= isl_set_intersect(
1299 isl_set_copy(pwaff1
->p
[i
].set
),
1300 isl_set_copy(pwaff2
->p
[j
].set
));
1301 better
= isl_set_from_basic_set(cmp(
1302 isl_aff_copy(pwaff2
->p
[j
].aff
),
1303 isl_aff_copy(pwaff1
->p
[i
].aff
)));
1304 better
= isl_set_intersect(common
, better
);
1305 if (isl_set_plain_is_empty(better
)) {
1306 isl_set_free(better
);
1309 set
= isl_set_subtract(set
, isl_set_copy(better
));
1311 res
= isl_pw_aff_add_piece(res
, better
,
1312 isl_aff_copy(pwaff2
->p
[j
].aff
));
1314 res
= isl_pw_aff_add_piece(res
, set
,
1315 isl_aff_copy(pwaff1
->p
[i
].aff
));
1318 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1319 set
= isl_set_copy(pwaff2
->p
[j
].set
);
1320 for (i
= 0; i
< pwaff1
->n
; ++i
)
1321 set
= isl_set_subtract(set
,
1322 isl_set_copy(pwaff1
->p
[i
].set
));
1323 res
= isl_pw_aff_add_piece(res
, set
,
1324 isl_aff_copy(pwaff2
->p
[j
].aff
));
1327 isl_pw_aff_free(pwaff1
);
1328 isl_pw_aff_free(pwaff2
);
1332 isl_pw_aff_free(pwaff1
);
1333 isl_pw_aff_free(pwaff2
);
1337 /* Compute a piecewise quasi-affine expression with a domain that
1338 * is the union of those of pwaff1 and pwaff2 and such that on each
1339 * cell, the quasi-affine expression is the maximum of those of pwaff1
1340 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1341 * cell, then the associated expression is the defined one.
1343 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1344 __isl_take isl_pw_aff
*pwaff2
)
1346 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_ge_basic_set
);
1349 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1350 __isl_take isl_pw_aff
*pwaff2
)
1352 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
1356 /* Compute a piecewise quasi-affine expression with a domain that
1357 * is the union of those of pwaff1 and pwaff2 and such that on each
1358 * cell, the quasi-affine expression is the minimum of those of pwaff1
1359 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1360 * cell, then the associated expression is the defined one.
1362 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1363 __isl_take isl_pw_aff
*pwaff2
)
1365 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_le_basic_set
);
1368 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1369 __isl_take isl_pw_aff
*pwaff2
)
1371 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
1375 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1376 __isl_take isl_pw_aff
*pwaff2
, int max
)
1379 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
1381 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
1384 /* Construct a map with as domain the domain of pwaff and
1385 * one-dimensional range corresponding to the affine expressions.
1387 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1396 dim
= isl_pw_aff_get_space(pwaff
);
1397 map
= isl_map_empty(dim
);
1399 for (i
= 0; i
< pwaff
->n
; ++i
) {
1400 isl_basic_map
*bmap
;
1403 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
1404 map_i
= isl_map_from_basic_map(bmap
);
1405 map_i
= isl_map_intersect_domain(map_i
,
1406 isl_set_copy(pwaff
->p
[i
].set
));
1407 map
= isl_map_union_disjoint(map
, map_i
);
1410 isl_pw_aff_free(pwaff
);
1415 /* Construct a map with as domain the domain of pwaff and
1416 * one-dimensional range corresponding to the affine expressions.
1418 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1420 if (isl_space_is_set(pwaff
->dim
))
1421 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1422 "space of input is not a map",
1423 return isl_pw_aff_free(pwaff
));
1424 return map_from_pw_aff(pwaff
);
1427 /* Construct a one-dimensional set with as parameter domain
1428 * the domain of pwaff and the single set dimension
1429 * corresponding to the affine expressions.
1431 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1433 if (!isl_space_is_set(pwaff
->dim
))
1434 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1435 "space of input is not a set",
1436 return isl_pw_aff_free(pwaff
));
1437 return map_from_pw_aff(pwaff
);
1440 /* Return a set containing those elements in the domain
1441 * of pwaff where it is non-negative.
1443 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
1451 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
1453 for (i
= 0; i
< pwaff
->n
; ++i
) {
1454 isl_basic_set
*bset
;
1457 bset
= isl_aff_nonneg_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1458 set_i
= isl_set_from_basic_set(bset
);
1459 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
1460 set
= isl_set_union_disjoint(set
, set_i
);
1463 isl_pw_aff_free(pwaff
);
1468 /* Return a set containing those elements in the domain
1469 * of pwaff where it is zero.
1471 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
1479 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
1481 for (i
= 0; i
< pwaff
->n
; ++i
) {
1482 isl_basic_set
*bset
;
1485 bset
= isl_aff_zero_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1486 set_i
= isl_set_from_basic_set(bset
);
1487 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
1488 set
= isl_set_union_disjoint(set
, set_i
);
1491 isl_pw_aff_free(pwaff
);
1496 /* Return a set containing those elements in the domain
1497 * of pwaff where it is not zero.
1499 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
1501 return isl_set_complement(isl_pw_aff_zero_set(pwaff
));
1504 /* Return a set containing those elements in the shared domain
1505 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1507 * We compute the difference on the shared domain and then construct
1508 * the set of values where this difference is non-negative.
1509 * If strict is set, we first subtract 1 from the difference.
1510 * If equal is set, we only return the elements where pwaff1 and pwaff2
1513 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
1514 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
1516 isl_set
*set1
, *set2
;
1518 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
1519 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
1520 set1
= isl_set_intersect(set1
, set2
);
1521 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
1522 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
1523 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
1526 isl_space
*dim
= isl_set_get_space(set1
);
1528 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1529 aff
= isl_aff_add_constant_si(aff
, -1);
1530 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
1535 return isl_pw_aff_zero_set(pwaff1
);
1536 return isl_pw_aff_nonneg_set(pwaff1
);
1539 /* Return a set containing those elements in the shared domain
1540 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1542 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1543 __isl_take isl_pw_aff
*pwaff2
)
1545 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
1548 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1549 __isl_take isl_pw_aff
*pwaff2
)
1551 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
1554 /* Return a set containing those elements in the shared domain
1555 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1557 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1558 __isl_take isl_pw_aff
*pwaff2
)
1560 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
1563 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1564 __isl_take isl_pw_aff
*pwaff2
)
1566 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
1569 /* Return a set containing those elements in the shared domain
1570 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1572 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1573 __isl_take isl_pw_aff
*pwaff2
)
1575 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
1578 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1579 __isl_take isl_pw_aff
*pwaff2
)
1581 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
1584 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
1585 __isl_take isl_pw_aff
*pwaff2
)
1587 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
1590 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
1591 __isl_take isl_pw_aff
*pwaff2
)
1593 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
1596 /* Return a set containing those elements in the shared domain
1597 * of the elements of list1 and list2 where each element in list1
1598 * has the relation specified by "fn" with each element in list2.
1600 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
1601 __isl_take isl_pw_aff_list
*list2
,
1602 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1603 __isl_take isl_pw_aff
*pwaff2
))
1609 if (!list1
|| !list2
)
1612 ctx
= isl_pw_aff_list_get_ctx(list1
);
1613 if (list1
->n
< 1 || list2
->n
< 1)
1614 isl_die(ctx
, isl_error_invalid
,
1615 "list should contain at least one element", goto error
);
1617 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
1618 for (i
= 0; i
< list1
->n
; ++i
)
1619 for (j
= 0; j
< list2
->n
; ++j
) {
1622 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
1623 isl_pw_aff_copy(list2
->p
[j
]));
1624 set
= isl_set_intersect(set
, set_ij
);
1627 isl_pw_aff_list_free(list1
);
1628 isl_pw_aff_list_free(list2
);
1631 isl_pw_aff_list_free(list1
);
1632 isl_pw_aff_list_free(list2
);
1636 /* Return a set containing those elements in the shared domain
1637 * of the elements of list1 and list2 where each element in list1
1638 * is equal to each element in list2.
1640 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
1641 __isl_take isl_pw_aff_list
*list2
)
1643 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
1646 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
1647 __isl_take isl_pw_aff_list
*list2
)
1649 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
1652 /* Return a set containing those elements in the shared domain
1653 * of the elements of list1 and list2 where each element in list1
1654 * is less than or equal to each element in list2.
1656 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
1657 __isl_take isl_pw_aff_list
*list2
)
1659 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
1662 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
1663 __isl_take isl_pw_aff_list
*list2
)
1665 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
1668 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
1669 __isl_take isl_pw_aff_list
*list2
)
1671 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
1674 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
1675 __isl_take isl_pw_aff_list
*list2
)
1677 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
1681 /* Return a set containing those elements in the shared domain
1682 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1684 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
1685 __isl_take isl_pw_aff
*pwaff2
)
1687 isl_set
*set_lt
, *set_gt
;
1689 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
1690 isl_pw_aff_copy(pwaff2
));
1691 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
1692 return isl_set_union_disjoint(set_lt
, set_gt
);
1695 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
1696 __isl_take isl_pw_aff
*pwaff2
)
1698 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
1701 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
1706 if (isl_int_is_one(v
))
1708 if (!isl_int_is_pos(v
))
1709 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1710 "factor needs to be positive",
1711 return isl_pw_aff_free(pwaff
));
1712 pwaff
= isl_pw_aff_cow(pwaff
);
1718 for (i
= 0; i
< pwaff
->n
; ++i
) {
1719 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
1720 if (!pwaff
->p
[i
].aff
)
1721 return isl_pw_aff_free(pwaff
);
1727 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
1731 pwaff
= isl_pw_aff_cow(pwaff
);
1737 for (i
= 0; i
< pwaff
->n
; ++i
) {
1738 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
1739 if (!pwaff
->p
[i
].aff
)
1740 return isl_pw_aff_free(pwaff
);
1746 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
1750 pwaff
= isl_pw_aff_cow(pwaff
);
1756 for (i
= 0; i
< pwaff
->n
; ++i
) {
1757 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
1758 if (!pwaff
->p
[i
].aff
)
1759 return isl_pw_aff_free(pwaff
);
1765 /* Return an affine expression that is equal to pwaff_true for elements
1766 * in "cond" and to pwaff_false for elements not in "cond".
1767 * That is, return cond ? pwaff_true : pwaff_false;
1769 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_set
*cond
,
1770 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
1774 comp
= isl_set_complement(isl_set_copy(cond
));
1775 pwaff_true
= isl_pw_aff_intersect_domain(pwaff_true
, cond
);
1776 pwaff_false
= isl_pw_aff_intersect_domain(pwaff_false
, comp
);
1778 return isl_pw_aff_add_disjoint(pwaff_true
, pwaff_false
);
1781 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
1786 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
1789 /* Check whether pwaff is a piecewise constant.
1791 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
1798 for (i
= 0; i
< pwaff
->n
; ++i
) {
1799 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
1800 if (is_cst
< 0 || !is_cst
)
1807 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
1808 __isl_take isl_aff
*aff2
)
1810 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
1811 return isl_aff_mul(aff2
, aff1
);
1813 if (!isl_aff_is_cst(aff2
))
1814 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
1815 "at least one affine expression should be constant",
1818 aff1
= isl_aff_cow(aff1
);
1822 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
1823 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
1833 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
1834 __isl_take isl_pw_aff
*pwaff2
)
1836 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
1839 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
1840 __isl_take isl_pw_aff
*pwaff2
)
1842 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
1845 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
1846 __isl_take isl_pw_aff
*pwaff2
)
1848 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
1851 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
1852 __isl_take isl_pw_aff
*pwaff2
)
1854 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
1857 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
1858 __isl_take isl_pw_aff
*pwaff2
)
1860 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
1863 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
1864 __isl_take isl_pw_aff
*pwaff2
)
1868 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
1869 isl_pw_aff_copy(pwaff2
));
1870 return isl_pw_aff_cond(le
, pwaff1
, pwaff2
);
1873 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
1874 __isl_take isl_pw_aff
*pwaff2
)
1876 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
1879 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
1880 __isl_take isl_pw_aff
*pwaff2
)
1884 le
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
1885 isl_pw_aff_copy(pwaff2
));
1886 return isl_pw_aff_cond(le
, pwaff1
, pwaff2
);
1889 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
1890 __isl_take isl_pw_aff
*pwaff2
)
1892 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
1895 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
1896 __isl_take isl_pw_aff_list
*list
,
1897 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1898 __isl_take isl_pw_aff
*pwaff2
))
1907 ctx
= isl_pw_aff_list_get_ctx(list
);
1909 isl_die(ctx
, isl_error_invalid
,
1910 "list should contain at least one element",
1911 return isl_pw_aff_list_free(list
));
1913 res
= isl_pw_aff_copy(list
->p
[0]);
1914 for (i
= 1; i
< list
->n
; ++i
)
1915 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
1917 isl_pw_aff_list_free(list
);
1921 /* Return an isl_pw_aff that maps each element in the intersection of the
1922 * domains of the elements of list to the minimal corresponding affine
1925 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
1927 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
1930 /* Return an isl_pw_aff that maps each element in the intersection of the
1931 * domains of the elements of list to the maximal corresponding affine
1934 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
1936 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
1942 #include <isl_multi_templ.c>
1944 __isl_give isl_multi_aff
*isl_multi_aff_add(__isl_take isl_multi_aff
*maff1
,
1945 __isl_take isl_multi_aff
*maff2
)
1950 maff1
= isl_multi_aff_cow(maff1
);
1951 if (!maff1
|| !maff2
)
1954 ctx
= isl_multi_aff_get_ctx(maff1
);
1955 if (!isl_space_is_equal(maff1
->space
, maff2
->space
))
1956 isl_die(ctx
, isl_error_invalid
,
1957 "spaces don't match", goto error
);
1959 for (i
= 0; i
< maff1
->n
; ++i
) {
1960 maff1
->p
[i
] = isl_aff_add(maff1
->p
[i
],
1961 isl_aff_copy(maff2
->p
[i
]));
1966 isl_multi_aff_free(maff2
);
1969 isl_multi_aff_free(maff1
);
1970 isl_multi_aff_free(maff2
);
1974 /* Exploit the equalities in "eq" to simplify the affine expressions.
1976 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
1977 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
1981 maff
= isl_multi_aff_cow(maff
);
1985 for (i
= 0; i
< maff
->n
; ++i
) {
1986 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
1987 isl_basic_set_copy(eq
));
1992 isl_basic_set_free(eq
);
1995 isl_basic_set_free(eq
);
1996 isl_multi_aff_free(maff
);
2000 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
2005 maff
= isl_multi_aff_cow(maff
);
2009 for (i
= 0; i
< maff
->n
; ++i
) {
2010 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
2012 return isl_multi_aff_free(maff
);
2018 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
2019 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
2021 maff1
= isl_multi_aff_add(maff1
, maff2
);
2022 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
2026 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
2034 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff
*maff1
,
2035 __isl_keep isl_multi_aff
*maff2
)
2040 if (!maff1
|| !maff2
)
2042 if (maff1
->n
!= maff2
->n
)
2044 equal
= isl_space_is_equal(maff1
->space
, maff2
->space
);
2045 if (equal
< 0 || !equal
)
2048 for (i
= 0; i
< maff1
->n
; ++i
) {
2049 equal
= isl_aff_plain_is_equal(maff1
->p
[i
], maff2
->p
[i
]);
2050 if (equal
< 0 || !equal
)
2057 __isl_give isl_multi_aff
*isl_multi_aff_set_dim_name(
2058 __isl_take isl_multi_aff
*maff
,
2059 enum isl_dim_type type
, unsigned pos
, const char *s
)
2063 maff
= isl_multi_aff_cow(maff
);
2067 maff
->space
= isl_space_set_dim_name(maff
->space
, type
, pos
, s
);
2069 return isl_multi_aff_free(maff
);
2070 for (i
= 0; i
< maff
->n
; ++i
) {
2071 maff
->p
[i
] = isl_aff_set_dim_name(maff
->p
[i
], type
, pos
, s
);
2073 return isl_multi_aff_free(maff
);
2079 __isl_give isl_multi_aff
*isl_multi_aff_drop_dims(__isl_take isl_multi_aff
*maff
,
2080 enum isl_dim_type type
, unsigned first
, unsigned n
)
2084 maff
= isl_multi_aff_cow(maff
);
2088 maff
->space
= isl_space_drop_dims(maff
->space
, type
, first
, n
);
2090 return isl_multi_aff_free(maff
);
2091 for (i
= 0; i
< maff
->n
; ++i
) {
2092 maff
->p
[i
] = isl_aff_drop_dims(maff
->p
[i
], type
, first
, n
);
2094 return isl_multi_aff_free(maff
);
2101 #define PW isl_pw_multi_aff
2103 #define EL isl_multi_aff
2105 #define EL_IS_ZERO is_empty
2109 #define IS_ZERO is_empty
2116 #define NO_INVOLVES_DIMS
2117 #define NO_MOVE_DIMS
2118 #define NO_INSERT_DIMS
2122 #include <isl_pw_templ.c>
2124 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
2125 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2127 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
2128 &isl_multi_aff_add
);
2131 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
2132 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2134 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
2138 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
2139 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2141 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
2144 /* Construct a map mapping the domain the piecewise multi-affine expression
2145 * to its range, with each dimension in the range equated to the
2146 * corresponding affine expression on its cell.
2148 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
2156 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
2158 for (i
= 0; i
< pma
->n
; ++i
) {
2159 isl_multi_aff
*maff
;
2160 isl_basic_map
*bmap
;
2163 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
2164 bmap
= isl_basic_map_from_multi_aff(maff
);
2165 map_i
= isl_map_from_basic_map(bmap
);
2166 map_i
= isl_map_intersect_domain(map_i
,
2167 isl_set_copy(pma
->p
[i
].set
));
2168 map
= isl_map_union_disjoint(map
, map_i
);
2171 isl_pw_multi_aff_free(pma
);
2175 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
2177 if (!isl_space_is_set(pma
->dim
))
2178 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
2179 "isl_pw_multi_aff cannot be converted into an isl_set",
2180 return isl_pw_multi_aff_free(pma
));
2182 return isl_map_from_pw_multi_aff(pma
);
2185 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2186 * This obivously only works if the input "map" is single-valued.
2187 * If so, we compute the lexicographic minimum of the image in the form
2188 * of an isl_pw_multi_aff. Since the image is unique, it is equal
2189 * to its lexicographic minimum.
2190 * If the input is not single-valued, we produce an error.
2192 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
2196 isl_pw_multi_aff
*pma
;
2201 sv
= isl_map_is_single_valued(map
);
2205 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2206 "map is not single-valued", goto error
);
2207 map
= isl_map_make_disjoint(map
);
2211 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
2213 for (i
= 0; i
< map
->n
; ++i
) {
2214 isl_pw_multi_aff
*pma_i
;
2215 isl_basic_map
*bmap
;
2216 bmap
= isl_basic_map_copy(map
->p
[i
]);
2217 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
2218 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
2228 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
2230 return isl_pw_multi_aff_from_map(set
);
2233 /* Plug in "subs" for dimension "type", "pos" of "aff".
2235 * Let i be the dimension to replace and let "subs" be of the form
2239 * and "aff" of the form
2245 * floor((a f + d g')/(m d))
2247 * where g' is the result of plugging in "subs" in each of the integer
2250 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
2251 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
2256 aff
= isl_aff_cow(aff
);
2258 return isl_aff_free(aff
);
2260 ctx
= isl_aff_get_ctx(aff
);
2261 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
2262 isl_die(ctx
, isl_error_invalid
,
2263 "spaces don't match", return isl_aff_free(aff
));
2264 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
2265 isl_die(ctx
, isl_error_unsupported
,
2266 "cannot handle divs yet", return isl_aff_free(aff
));
2268 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
2270 return isl_aff_free(aff
);
2272 aff
->v
= isl_vec_cow(aff
->v
);
2274 return isl_aff_free(aff
);
2276 pos
+= isl_local_space_offset(aff
->ls
, type
);
2279 isl_int_set(v
, aff
->v
->el
[1 + pos
]);
2280 isl_int_set_si(aff
->v
->el
[1 + pos
], 0);
2281 isl_seq_combine(aff
->v
->el
+ 1, subs
->v
->el
[0], aff
->v
->el
+ 1,
2282 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
2283 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], subs
->v
->el
[0]);
2289 /* Plug in "subs" for dimension "type", "pos" in each of the affine
2290 * expressions in "maff".
2292 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
2293 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
2294 __isl_keep isl_aff
*subs
)
2298 maff
= isl_multi_aff_cow(maff
);
2300 return isl_multi_aff_free(maff
);
2302 if (type
== isl_dim_in
)
2305 for (i
= 0; i
< maff
->n
; ++i
) {
2306 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
2308 return isl_multi_aff_free(maff
);
2314 /* Plug in "subs" for dimension "type", "pos" of "pma".
2316 * pma is of the form
2320 * while subs is of the form
2322 * v' = B_j(v) -> S_j
2324 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
2325 * has a contribution in the result, in particular
2327 * C_ij(S_j) -> M_i(S_j)
2329 * Note that plugging in S_j in C_ij may also result in an empty set
2330 * and this contribution should simply be discarded.
2332 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
2333 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
2334 __isl_keep isl_pw_aff
*subs
)
2337 isl_pw_multi_aff
*res
;
2340 return isl_pw_multi_aff_free(pma
);
2342 n
= pma
->n
* subs
->n
;
2343 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
2345 for (i
= 0; i
< pma
->n
; ++i
) {
2346 for (j
= 0; j
< subs
->n
; ++j
) {
2348 isl_multi_aff
*res_ij
;
2349 common
= isl_set_intersect(
2350 isl_set_copy(pma
->p
[i
].set
),
2351 isl_set_copy(subs
->p
[j
].set
));
2352 common
= isl_set_substitute(common
,
2353 type
, pos
, subs
->p
[j
].aff
);
2354 if (isl_set_plain_is_empty(common
)) {
2355 isl_set_free(common
);
2359 res_ij
= isl_multi_aff_substitute(
2360 isl_multi_aff_copy(pma
->p
[i
].maff
),
2361 type
, pos
, subs
->p
[j
].aff
);
2363 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
2367 isl_pw_multi_aff_free(pma
);
2371 /* Extend the local space of "dst" to include the divs
2372 * in the local space of "src".
2374 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
2375 __isl_keep isl_aff
*src
)
2383 return isl_aff_free(dst
);
2385 ctx
= isl_aff_get_ctx(src
);
2386 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
2387 isl_die(ctx
, isl_error_invalid
,
2388 "spaces don't match", goto error
);
2390 if (src
->ls
->div
->n_row
== 0)
2393 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
2394 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
2398 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
2399 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
2407 return isl_aff_free(dst
);
2410 /* Adjust the local spaces of the affine expressions in "maff"
2411 * such that they all have the save divs.
2413 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
2414 __isl_take isl_multi_aff
*maff
)
2422 maff
= isl_multi_aff_cow(maff
);
2426 for (i
= 1; i
< maff
->n
; ++i
)
2427 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
2428 for (i
= 1; i
< maff
->n
; ++i
) {
2429 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
2431 return isl_multi_aff_free(maff
);
2437 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
2439 aff
= isl_aff_cow(aff
);
2443 aff
->ls
= isl_local_space_lift(aff
->ls
);
2445 return isl_aff_free(aff
);
2450 /* Lift "maff" to a space with extra dimensions such that the result
2451 * has no more existentially quantified variables.
2452 * If "ls" is not NULL, then *ls is assigned the local space that lies
2453 * at the basis of the lifting applied to "maff".
2455 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
2456 __isl_give isl_local_space
**ls
)
2470 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
2471 *ls
= isl_local_space_from_space(space
);
2473 return isl_multi_aff_free(maff
);
2478 maff
= isl_multi_aff_cow(maff
);
2479 maff
= isl_multi_aff_align_divs(maff
);
2483 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
2484 space
= isl_multi_aff_get_space(maff
);
2485 space
= isl_space_lift(isl_space_domain(space
), n_div
);
2486 space
= isl_space_extend_domain_with_range(space
,
2487 isl_multi_aff_get_space(maff
));
2489 return isl_multi_aff_free(maff
);
2490 isl_space_free(maff
->space
);
2491 maff
->space
= space
;
2494 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
2496 return isl_multi_aff_free(maff
);
2499 for (i
= 0; i
< maff
->n
; ++i
) {
2500 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
2508 isl_local_space_free(*ls
);
2509 return isl_multi_aff_free(maff
);