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 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
284 __isl_take isl_space
*model
)
289 if (!isl_space_match(aff
->ls
->dim
, isl_dim_param
,
290 model
, isl_dim_param
)) {
293 model
= isl_space_drop_dims(model
, isl_dim_in
,
294 0, isl_space_dim(model
, isl_dim_in
));
295 model
= isl_space_drop_dims(model
, isl_dim_out
,
296 0, isl_space_dim(model
, isl_dim_out
));
297 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
298 exp
= isl_reordering_extend_space(exp
,
299 isl_aff_get_domain_space(aff
));
300 aff
= isl_aff_realign_domain(aff
, exp
);
303 isl_space_free(model
);
306 isl_space_free(model
);
311 int isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
316 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
319 int isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
326 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
327 if (equal
< 0 || !equal
)
330 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
333 int isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
337 isl_int_set(*v
, aff
->v
->el
[0]);
341 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
345 isl_int_set(*v
, aff
->v
->el
[1]);
349 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
350 enum isl_dim_type type
, int pos
, isl_int
*v
)
355 if (type
== isl_dim_out
)
356 isl_die(aff
->v
->ctx
, isl_error_invalid
,
357 "output/set dimension does not have a coefficient",
359 if (type
== isl_dim_in
)
362 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
363 isl_die(aff
->v
->ctx
, isl_error_invalid
,
364 "position out of bounds", return -1);
366 pos
+= isl_local_space_offset(aff
->ls
, type
);
367 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
372 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
374 aff
= isl_aff_cow(aff
);
378 aff
->v
= isl_vec_cow(aff
->v
);
380 return isl_aff_free(aff
);
382 isl_int_set(aff
->v
->el
[0], v
);
387 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
389 aff
= isl_aff_cow(aff
);
393 aff
->v
= isl_vec_cow(aff
->v
);
395 return isl_aff_free(aff
);
397 isl_int_set(aff
->v
->el
[1], v
);
402 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
404 if (isl_int_is_zero(v
))
407 aff
= isl_aff_cow(aff
);
411 aff
->v
= isl_vec_cow(aff
->v
);
413 return isl_aff_free(aff
);
415 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
420 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
425 isl_int_set_si(t
, v
);
426 aff
= isl_aff_add_constant(aff
, t
);
432 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
434 aff
= isl_aff_cow(aff
);
438 aff
->v
= isl_vec_cow(aff
->v
);
440 return isl_aff_free(aff
);
442 isl_int_set_si(aff
->v
->el
[1], v
);
447 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
448 enum isl_dim_type type
, int pos
, isl_int v
)
453 if (type
== isl_dim_out
)
454 isl_die(aff
->v
->ctx
, isl_error_invalid
,
455 "output/set dimension does not have a coefficient",
456 return isl_aff_free(aff
));
457 if (type
== isl_dim_in
)
460 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
461 isl_die(aff
->v
->ctx
, isl_error_invalid
,
462 "position out of bounds", return isl_aff_free(aff
));
464 aff
= isl_aff_cow(aff
);
468 aff
->v
= isl_vec_cow(aff
->v
);
470 return isl_aff_free(aff
);
472 pos
+= isl_local_space_offset(aff
->ls
, type
);
473 isl_int_set(aff
->v
->el
[1 + pos
], v
);
478 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
479 enum isl_dim_type type
, int pos
, int v
)
484 if (type
== isl_dim_out
)
485 isl_die(aff
->v
->ctx
, isl_error_invalid
,
486 "output/set dimension does not have a coefficient",
487 return isl_aff_free(aff
));
488 if (type
== isl_dim_in
)
491 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
492 isl_die(aff
->v
->ctx
, isl_error_invalid
,
493 "position out of bounds", return isl_aff_free(aff
));
495 aff
= isl_aff_cow(aff
);
499 aff
->v
= isl_vec_cow(aff
->v
);
501 return isl_aff_free(aff
);
503 pos
+= isl_local_space_offset(aff
->ls
, type
);
504 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
509 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
510 enum isl_dim_type type
, int pos
, isl_int v
)
515 if (type
== isl_dim_out
)
516 isl_die(aff
->v
->ctx
, isl_error_invalid
,
517 "output/set dimension does not have a coefficient",
518 return isl_aff_free(aff
));
519 if (type
== isl_dim_in
)
522 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
523 isl_die(aff
->v
->ctx
, isl_error_invalid
,
524 "position out of bounds", return isl_aff_free(aff
));
526 aff
= isl_aff_cow(aff
);
530 aff
->v
= isl_vec_cow(aff
->v
);
532 return isl_aff_free(aff
);
534 pos
+= isl_local_space_offset(aff
->ls
, type
);
535 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
540 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
541 enum isl_dim_type type
, int pos
, int v
)
546 isl_int_set_si(t
, v
);
547 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
553 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
558 return isl_local_space_get_div(aff
->ls
, pos
);
561 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
563 aff
= isl_aff_cow(aff
);
566 aff
->v
= isl_vec_cow(aff
->v
);
568 return isl_aff_free(aff
);
570 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
575 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
579 aff
->v
= isl_vec_normalize(aff
->v
);
581 return isl_aff_free(aff
);
585 /* Given f, return floor(f).
586 * If f is an integer expression, then just return f.
587 * Otherwise, if f = g/m, write g = q m + r,
588 * create a new div d = [r/m] and return the expression q + d.
589 * The coefficients in r are taken to lie between -m/2 and m/2.
591 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
601 if (isl_int_is_one(aff
->v
->el
[0]))
604 aff
= isl_aff_cow(aff
);
608 aff
->v
= isl_vec_cow(aff
->v
);
609 div
= isl_vec_copy(aff
->v
);
610 div
= isl_vec_cow(div
);
612 return isl_aff_free(aff
);
614 ctx
= isl_aff_get_ctx(aff
);
615 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
616 for (i
= 1; i
< aff
->v
->size
; ++i
) {
617 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
618 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
619 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
620 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
621 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
625 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
627 return isl_aff_free(aff
);
630 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
632 return isl_aff_free(aff
);
633 isl_int_set_si(aff
->v
->el
[0], 1);
634 isl_int_set_si(aff
->v
->el
[size
], 1);
641 * aff mod m = aff - m * floor(aff/m)
643 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
647 res
= isl_aff_copy(aff
);
648 aff
= isl_aff_scale_down(aff
, m
);
649 aff
= isl_aff_floor(aff
);
650 aff
= isl_aff_scale(aff
, m
);
651 res
= isl_aff_sub(res
, aff
);
658 * pwaff mod m = pwaff - m * floor(pwaff/m)
660 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
664 res
= isl_pw_aff_copy(pwaff
);
665 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
666 pwaff
= isl_pw_aff_floor(pwaff
);
667 pwaff
= isl_pw_aff_scale(pwaff
, m
);
668 res
= isl_pw_aff_sub(res
, pwaff
);
673 /* Given f, return ceil(f).
674 * If f is an integer expression, then just return f.
675 * Otherwise, create a new div d = [-f] and return the expression -d.
677 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
682 if (isl_int_is_one(aff
->v
->el
[0]))
685 aff
= isl_aff_neg(aff
);
686 aff
= isl_aff_floor(aff
);
687 aff
= isl_aff_neg(aff
);
692 /* Apply the expansion computed by isl_merge_divs.
693 * The expansion itself is given by "exp" while the resulting
694 * list of divs is given by "div".
696 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
697 __isl_take isl_mat
*div
, int *exp
)
704 aff
= isl_aff_cow(aff
);
708 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
709 new_n_div
= isl_mat_rows(div
);
710 if (new_n_div
< old_n_div
)
711 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
712 "not an expansion", goto error
);
714 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
718 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
720 for (i
= new_n_div
- 1; i
>= 0; --i
) {
721 if (j
>= 0 && exp
[j
] == i
) {
723 isl_int_swap(aff
->v
->el
[offset
+ i
],
724 aff
->v
->el
[offset
+ j
]);
727 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
730 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
741 /* Add two affine expressions that live in the same local space.
743 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
744 __isl_take isl_aff
*aff2
)
748 aff1
= isl_aff_cow(aff1
);
752 aff1
->v
= isl_vec_cow(aff1
->v
);
758 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
759 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
760 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
761 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
762 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
763 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
764 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
776 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
777 __isl_take isl_aff
*aff2
)
787 ctx
= isl_aff_get_ctx(aff1
);
788 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
789 isl_die(ctx
, isl_error_invalid
,
790 "spaces don't match", goto error
);
792 if (aff1
->ls
->div
->n_row
== 0 && aff2
->ls
->div
->n_row
== 0)
793 return add_expanded(aff1
, aff2
);
795 exp1
= isl_alloc_array(ctx
, int, aff1
->ls
->div
->n_row
);
796 exp2
= isl_alloc_array(ctx
, int, aff2
->ls
->div
->n_row
);
800 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
801 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
802 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
806 return add_expanded(aff1
, aff2
);
815 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
816 __isl_take isl_aff
*aff2
)
818 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
821 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
825 if (isl_int_is_one(f
))
828 aff
= isl_aff_cow(aff
);
831 aff
->v
= isl_vec_cow(aff
->v
);
833 return isl_aff_free(aff
);
836 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
837 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
838 isl_int_divexact(gcd
, f
, gcd
);
839 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
845 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
849 if (isl_int_is_one(f
))
852 aff
= isl_aff_cow(aff
);
855 aff
->v
= isl_vec_cow(aff
->v
);
857 return isl_aff_free(aff
);
860 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
861 isl_int_gcd(gcd
, gcd
, f
);
862 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
863 isl_int_divexact(gcd
, f
, gcd
);
864 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
870 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
878 isl_int_set_ui(v
, f
);
879 aff
= isl_aff_scale_down(aff
, v
);
885 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
886 enum isl_dim_type type
, unsigned pos
, const char *s
)
888 aff
= isl_aff_cow(aff
);
891 if (type
== isl_dim_out
)
892 isl_die(aff
->v
->ctx
, isl_error_invalid
,
893 "cannot set name of output/set dimension",
894 return isl_aff_free(aff
));
895 if (type
== isl_dim_in
)
897 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
899 return isl_aff_free(aff
);
904 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
905 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
907 aff
= isl_aff_cow(aff
);
909 return isl_id_free(id
);
910 if (type
== isl_dim_out
)
911 isl_die(aff
->v
->ctx
, isl_error_invalid
,
912 "cannot set name of output/set dimension",
914 if (type
== isl_dim_in
)
916 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
918 return isl_aff_free(aff
);
927 /* Exploit the equalities in "eq" to simplify the affine expression
928 * and the expressions of the integer divisions in the local space.
929 * The integer divisions in this local space are assumed to appear
930 * as regular dimensions in "eq".
932 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
933 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
942 isl_basic_set_free(eq
);
946 aff
= isl_aff_cow(aff
);
950 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
951 isl_basic_set_copy(eq
));
955 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
957 for (i
= 0; i
< eq
->n_eq
; ++i
) {
958 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
959 if (j
< 0 || j
== 0 || j
>= total
)
962 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
966 isl_basic_set_free(eq
);
967 aff
= isl_aff_normalize(aff
);
970 isl_basic_set_free(eq
);
975 /* Exploit the equalities in "eq" to simplify the affine expression
976 * and the expressions of the integer divisions in the local space.
978 static __isl_give isl_aff
*isl_aff_substitute_equalities(
979 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
985 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
987 eq
= isl_basic_set_add(eq
, isl_dim_set
, n_div
);
988 return isl_aff_substitute_equalities_lifted(aff
, eq
);
990 isl_basic_set_free(eq
);
995 /* Look for equalities among the variables shared by context and aff
996 * and the integer divisions of aff, if any.
997 * The equalities are then used to eliminate coefficients and/or integer
998 * divisions from aff.
1000 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
1001 __isl_take isl_set
*context
)
1003 isl_basic_set
*hull
;
1008 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1010 isl_basic_set
*bset
;
1011 isl_local_space
*ls
;
1012 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
1013 ls
= isl_aff_get_domain_local_space(aff
);
1014 bset
= isl_basic_set_from_local_space(ls
);
1015 bset
= isl_basic_set_lift(bset
);
1016 bset
= isl_basic_set_flatten(bset
);
1017 context
= isl_set_intersect(context
,
1018 isl_set_from_basic_set(bset
));
1021 hull
= isl_set_affine_hull(context
);
1022 return isl_aff_substitute_equalities_lifted(aff
, hull
);
1025 isl_set_free(context
);
1029 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
1030 __isl_take isl_set
*context
)
1032 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
1033 dom_context
= isl_set_intersect_params(dom_context
, context
);
1034 return isl_aff_gist(aff
, dom_context
);
1037 /* Return a basic set containing those elements in the space
1038 * of aff where it is non-negative.
1040 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
1042 isl_constraint
*ineq
;
1044 ineq
= isl_inequality_from_aff(aff
);
1046 return isl_basic_set_from_constraint(ineq
);
1049 /* Return a basic set containing those elements in the space
1050 * of aff where it is zero.
1052 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
1054 isl_constraint
*ineq
;
1056 ineq
= isl_equality_from_aff(aff
);
1058 return isl_basic_set_from_constraint(ineq
);
1061 /* Return a basic set containing those elements in the shared space
1062 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1064 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
1065 __isl_take isl_aff
*aff2
)
1067 aff1
= isl_aff_sub(aff1
, aff2
);
1069 return isl_aff_nonneg_basic_set(aff1
);
1072 /* Return a basic set containing those elements in the shared space
1073 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1075 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
1076 __isl_take isl_aff
*aff2
)
1078 return isl_aff_ge_basic_set(aff2
, aff1
);
1081 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
1082 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
1084 aff1
= isl_aff_add(aff1
, aff2
);
1085 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
1089 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
1097 /* Check whether the given affine expression has non-zero coefficient
1098 * for any dimension in the given range or if any of these dimensions
1099 * appear with non-zero coefficients in any of the integer divisions
1100 * involved in the affine expression.
1102 int isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
1103 enum isl_dim_type type
, unsigned first
, unsigned n
)
1115 ctx
= isl_aff_get_ctx(aff
);
1116 if (first
+ n
> isl_aff_dim(aff
, type
))
1117 isl_die(ctx
, isl_error_invalid
,
1118 "range out of bounds", return -1);
1120 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
1124 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
1125 for (i
= 0; i
< n
; ++i
)
1126 if (active
[first
+ i
]) {
1139 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
1140 enum isl_dim_type type
, unsigned first
, unsigned n
)
1146 if (type
== isl_dim_out
)
1147 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1148 "cannot drop output/set dimension",
1149 return isl_aff_free(aff
));
1150 if (type
== isl_dim_in
)
1152 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1155 ctx
= isl_aff_get_ctx(aff
);
1156 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
1157 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
1158 return isl_aff_free(aff
));
1160 aff
= isl_aff_cow(aff
);
1164 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
1166 return isl_aff_free(aff
);
1168 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1169 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
1171 return isl_aff_free(aff
);
1176 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
1177 enum isl_dim_type type
, unsigned first
, unsigned n
)
1183 if (type
== isl_dim_out
)
1184 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1185 "cannot insert output/set dimensions",
1186 return isl_aff_free(aff
));
1187 if (type
== isl_dim_in
)
1189 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1192 ctx
= isl_aff_get_ctx(aff
);
1193 if (first
> isl_local_space_dim(aff
->ls
, type
))
1194 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
1195 return isl_aff_free(aff
));
1197 aff
= isl_aff_cow(aff
);
1201 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
1203 return isl_aff_free(aff
);
1205 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1206 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
1208 return isl_aff_free(aff
);
1213 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
1214 enum isl_dim_type type
, unsigned n
)
1218 pos
= isl_aff_dim(aff
, type
);
1220 return isl_aff_insert_dims(aff
, type
, pos
, n
);
1223 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
1224 enum isl_dim_type type
, unsigned n
)
1228 pos
= isl_pw_aff_dim(pwaff
, type
);
1230 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
1233 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
1235 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
1236 return isl_pw_aff_alloc(dom
, aff
);
1240 #define PW isl_pw_aff
1244 #define EL_IS_ZERO is_empty
1248 #define IS_ZERO is_empty
1251 #undef DEFAULT_IS_ZERO
1252 #define DEFAULT_IS_ZERO 0
1256 #define NO_MOVE_DIMS
1260 #include <isl_pw_templ.c>
1262 static __isl_give isl_set
*align_params_pw_pw_set_and(
1263 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
1264 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1265 __isl_take isl_pw_aff
*pwaff2
))
1267 if (!pwaff1
|| !pwaff2
)
1269 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
1270 pwaff2
->dim
, isl_dim_param
))
1271 return fn(pwaff1
, pwaff2
);
1272 if (!isl_space_has_named_params(pwaff1
->dim
) ||
1273 !isl_space_has_named_params(pwaff2
->dim
))
1274 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
1275 "unaligned unnamed parameters", goto error
);
1276 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
1277 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
1278 return fn(pwaff1
, pwaff2
);
1280 isl_pw_aff_free(pwaff1
);
1281 isl_pw_aff_free(pwaff2
);
1285 /* Compute a piecewise quasi-affine expression with a domain that
1286 * is the union of those of pwaff1 and pwaff2 and such that on each
1287 * cell, the quasi-affine expression is the better (according to cmp)
1288 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1289 * is defined on a given cell, then the associated expression
1290 * is the defined one.
1292 static __isl_give isl_pw_aff
*pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1293 __isl_take isl_pw_aff
*pwaff2
,
1294 __isl_give isl_basic_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
1295 __isl_take isl_aff
*aff2
))
1302 if (!pwaff1
|| !pwaff2
)
1305 ctx
= isl_space_get_ctx(pwaff1
->dim
);
1306 if (!isl_space_is_equal(pwaff1
->dim
, pwaff2
->dim
))
1307 isl_die(ctx
, isl_error_invalid
,
1308 "arguments should live in same space", goto error
);
1310 if (isl_pw_aff_is_empty(pwaff1
)) {
1311 isl_pw_aff_free(pwaff1
);
1315 if (isl_pw_aff_is_empty(pwaff2
)) {
1316 isl_pw_aff_free(pwaff2
);
1320 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
1321 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
1323 for (i
= 0; i
< pwaff1
->n
; ++i
) {
1324 set
= isl_set_copy(pwaff1
->p
[i
].set
);
1325 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1326 struct isl_set
*common
;
1329 common
= isl_set_intersect(
1330 isl_set_copy(pwaff1
->p
[i
].set
),
1331 isl_set_copy(pwaff2
->p
[j
].set
));
1332 better
= isl_set_from_basic_set(cmp(
1333 isl_aff_copy(pwaff2
->p
[j
].aff
),
1334 isl_aff_copy(pwaff1
->p
[i
].aff
)));
1335 better
= isl_set_intersect(common
, better
);
1336 if (isl_set_plain_is_empty(better
)) {
1337 isl_set_free(better
);
1340 set
= isl_set_subtract(set
, isl_set_copy(better
));
1342 res
= isl_pw_aff_add_piece(res
, better
,
1343 isl_aff_copy(pwaff2
->p
[j
].aff
));
1345 res
= isl_pw_aff_add_piece(res
, set
,
1346 isl_aff_copy(pwaff1
->p
[i
].aff
));
1349 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1350 set
= isl_set_copy(pwaff2
->p
[j
].set
);
1351 for (i
= 0; i
< pwaff1
->n
; ++i
)
1352 set
= isl_set_subtract(set
,
1353 isl_set_copy(pwaff1
->p
[i
].set
));
1354 res
= isl_pw_aff_add_piece(res
, set
,
1355 isl_aff_copy(pwaff2
->p
[j
].aff
));
1358 isl_pw_aff_free(pwaff1
);
1359 isl_pw_aff_free(pwaff2
);
1363 isl_pw_aff_free(pwaff1
);
1364 isl_pw_aff_free(pwaff2
);
1368 /* Compute a piecewise quasi-affine expression with a domain that
1369 * is the union of those of pwaff1 and pwaff2 and such that on each
1370 * cell, the quasi-affine expression is the maximum of those of pwaff1
1371 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1372 * cell, then the associated expression is the defined one.
1374 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1375 __isl_take isl_pw_aff
*pwaff2
)
1377 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_ge_basic_set
);
1380 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1381 __isl_take isl_pw_aff
*pwaff2
)
1383 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
1387 /* Compute a piecewise quasi-affine expression with a domain that
1388 * is the union of those of pwaff1 and pwaff2 and such that on each
1389 * cell, the quasi-affine expression is the minimum of those of pwaff1
1390 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1391 * cell, then the associated expression is the defined one.
1393 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1394 __isl_take isl_pw_aff
*pwaff2
)
1396 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_le_basic_set
);
1399 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1400 __isl_take isl_pw_aff
*pwaff2
)
1402 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
1406 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1407 __isl_take isl_pw_aff
*pwaff2
, int max
)
1410 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
1412 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
1415 /* Construct a map with as domain the domain of pwaff and
1416 * one-dimensional range corresponding to the affine expressions.
1418 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1427 dim
= isl_pw_aff_get_space(pwaff
);
1428 map
= isl_map_empty(dim
);
1430 for (i
= 0; i
< pwaff
->n
; ++i
) {
1431 isl_basic_map
*bmap
;
1434 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
1435 map_i
= isl_map_from_basic_map(bmap
);
1436 map_i
= isl_map_intersect_domain(map_i
,
1437 isl_set_copy(pwaff
->p
[i
].set
));
1438 map
= isl_map_union_disjoint(map
, map_i
);
1441 isl_pw_aff_free(pwaff
);
1446 /* Construct a map with as domain the domain of pwaff and
1447 * one-dimensional range corresponding to the affine expressions.
1449 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1453 if (isl_space_is_set(pwaff
->dim
))
1454 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1455 "space of input is not a map",
1456 return isl_pw_aff_free(pwaff
));
1457 return map_from_pw_aff(pwaff
);
1460 /* Construct a one-dimensional set with as parameter domain
1461 * the domain of pwaff and the single set dimension
1462 * corresponding to the affine expressions.
1464 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1468 if (!isl_space_is_set(pwaff
->dim
))
1469 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1470 "space of input is not a set",
1471 return isl_pw_aff_free(pwaff
));
1472 return map_from_pw_aff(pwaff
);
1475 /* Return a set containing those elements in the domain
1476 * of pwaff where it is non-negative.
1478 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
1486 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
1488 for (i
= 0; i
< pwaff
->n
; ++i
) {
1489 isl_basic_set
*bset
;
1492 bset
= isl_aff_nonneg_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1493 set_i
= isl_set_from_basic_set(bset
);
1494 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
1495 set
= isl_set_union_disjoint(set
, set_i
);
1498 isl_pw_aff_free(pwaff
);
1503 /* Return a set containing those elements in the domain
1504 * of pwaff where it is zero.
1506 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
1514 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
1516 for (i
= 0; i
< pwaff
->n
; ++i
) {
1517 isl_basic_set
*bset
;
1520 bset
= isl_aff_zero_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1521 set_i
= isl_set_from_basic_set(bset
);
1522 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
1523 set
= isl_set_union_disjoint(set
, set_i
);
1526 isl_pw_aff_free(pwaff
);
1531 /* Return a set containing those elements in the domain
1532 * of pwaff where it is not zero.
1534 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
1536 return isl_set_complement(isl_pw_aff_zero_set(pwaff
));
1539 /* Return a set containing those elements in the shared domain
1540 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1542 * We compute the difference on the shared domain and then construct
1543 * the set of values where this difference is non-negative.
1544 * If strict is set, we first subtract 1 from the difference.
1545 * If equal is set, we only return the elements where pwaff1 and pwaff2
1548 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
1549 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
1551 isl_set
*set1
, *set2
;
1553 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
1554 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
1555 set1
= isl_set_intersect(set1
, set2
);
1556 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
1557 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
1558 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
1561 isl_space
*dim
= isl_set_get_space(set1
);
1563 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1564 aff
= isl_aff_add_constant_si(aff
, -1);
1565 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
1570 return isl_pw_aff_zero_set(pwaff1
);
1571 return isl_pw_aff_nonneg_set(pwaff1
);
1574 /* Return a set containing those elements in the shared domain
1575 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1577 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1578 __isl_take isl_pw_aff
*pwaff2
)
1580 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
1583 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1584 __isl_take isl_pw_aff
*pwaff2
)
1586 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
1589 /* Return a set containing those elements in the shared domain
1590 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1592 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1593 __isl_take isl_pw_aff
*pwaff2
)
1595 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
1598 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1599 __isl_take isl_pw_aff
*pwaff2
)
1601 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
1604 /* Return a set containing those elements in the shared domain
1605 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1607 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1608 __isl_take isl_pw_aff
*pwaff2
)
1610 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
1613 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1614 __isl_take isl_pw_aff
*pwaff2
)
1616 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
1619 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
1620 __isl_take isl_pw_aff
*pwaff2
)
1622 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
1625 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
1626 __isl_take isl_pw_aff
*pwaff2
)
1628 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
1631 /* Return a set containing those elements in the shared domain
1632 * of the elements of list1 and list2 where each element in list1
1633 * has the relation specified by "fn" with each element in list2.
1635 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
1636 __isl_take isl_pw_aff_list
*list2
,
1637 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1638 __isl_take isl_pw_aff
*pwaff2
))
1644 if (!list1
|| !list2
)
1647 ctx
= isl_pw_aff_list_get_ctx(list1
);
1648 if (list1
->n
< 1 || list2
->n
< 1)
1649 isl_die(ctx
, isl_error_invalid
,
1650 "list should contain at least one element", goto error
);
1652 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
1653 for (i
= 0; i
< list1
->n
; ++i
)
1654 for (j
= 0; j
< list2
->n
; ++j
) {
1657 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
1658 isl_pw_aff_copy(list2
->p
[j
]));
1659 set
= isl_set_intersect(set
, set_ij
);
1662 isl_pw_aff_list_free(list1
);
1663 isl_pw_aff_list_free(list2
);
1666 isl_pw_aff_list_free(list1
);
1667 isl_pw_aff_list_free(list2
);
1671 /* Return a set containing those elements in the shared domain
1672 * of the elements of list1 and list2 where each element in list1
1673 * is equal to each element in list2.
1675 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
1676 __isl_take isl_pw_aff_list
*list2
)
1678 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
1681 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
1682 __isl_take isl_pw_aff_list
*list2
)
1684 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
1687 /* Return a set containing those elements in the shared domain
1688 * of the elements of list1 and list2 where each element in list1
1689 * is less than or equal to each element in list2.
1691 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
1692 __isl_take isl_pw_aff_list
*list2
)
1694 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
1697 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
1698 __isl_take isl_pw_aff_list
*list2
)
1700 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
1703 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
1704 __isl_take isl_pw_aff_list
*list2
)
1706 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
1709 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
1710 __isl_take isl_pw_aff_list
*list2
)
1712 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
1716 /* Return a set containing those elements in the shared domain
1717 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1719 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
1720 __isl_take isl_pw_aff
*pwaff2
)
1722 isl_set
*set_lt
, *set_gt
;
1724 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
1725 isl_pw_aff_copy(pwaff2
));
1726 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
1727 return isl_set_union_disjoint(set_lt
, set_gt
);
1730 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
1731 __isl_take isl_pw_aff
*pwaff2
)
1733 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
1736 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
1741 if (isl_int_is_one(v
))
1743 if (!isl_int_is_pos(v
))
1744 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1745 "factor needs to be positive",
1746 return isl_pw_aff_free(pwaff
));
1747 pwaff
= isl_pw_aff_cow(pwaff
);
1753 for (i
= 0; i
< pwaff
->n
; ++i
) {
1754 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
1755 if (!pwaff
->p
[i
].aff
)
1756 return isl_pw_aff_free(pwaff
);
1762 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
1766 pwaff
= isl_pw_aff_cow(pwaff
);
1772 for (i
= 0; i
< pwaff
->n
; ++i
) {
1773 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
1774 if (!pwaff
->p
[i
].aff
)
1775 return isl_pw_aff_free(pwaff
);
1781 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
1785 pwaff
= isl_pw_aff_cow(pwaff
);
1791 for (i
= 0; i
< pwaff
->n
; ++i
) {
1792 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
1793 if (!pwaff
->p
[i
].aff
)
1794 return isl_pw_aff_free(pwaff
);
1800 /* Return an affine expression that is equal to pwaff_true for elements
1801 * in "cond" and to pwaff_false for elements not in "cond".
1802 * That is, return cond ? pwaff_true : pwaff_false;
1804 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_set
*cond
,
1805 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
1809 comp
= isl_set_complement(isl_set_copy(cond
));
1810 pwaff_true
= isl_pw_aff_intersect_domain(pwaff_true
, cond
);
1811 pwaff_false
= isl_pw_aff_intersect_domain(pwaff_false
, comp
);
1813 return isl_pw_aff_add_disjoint(pwaff_true
, pwaff_false
);
1816 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
1821 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
1824 /* Check whether pwaff is a piecewise constant.
1826 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
1833 for (i
= 0; i
< pwaff
->n
; ++i
) {
1834 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
1835 if (is_cst
< 0 || !is_cst
)
1842 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
1843 __isl_take isl_aff
*aff2
)
1845 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
1846 return isl_aff_mul(aff2
, aff1
);
1848 if (!isl_aff_is_cst(aff2
))
1849 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
1850 "at least one affine expression should be constant",
1853 aff1
= isl_aff_cow(aff1
);
1857 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
1858 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
1868 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
1869 __isl_take isl_pw_aff
*pwaff2
)
1871 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
1874 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
1875 __isl_take isl_pw_aff
*pwaff2
)
1877 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
1880 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
1881 __isl_take isl_pw_aff
*pwaff2
)
1883 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
1886 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
1887 __isl_take isl_pw_aff
*pwaff2
)
1889 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
1892 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
1893 __isl_take isl_pw_aff
*pwaff2
)
1895 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
1898 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
1899 __isl_take isl_pw_aff
*pwaff2
)
1903 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
1904 isl_pw_aff_copy(pwaff2
));
1905 return isl_pw_aff_cond(le
, pwaff1
, pwaff2
);
1908 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
1909 __isl_take isl_pw_aff
*pwaff2
)
1911 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
1914 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
1915 __isl_take isl_pw_aff
*pwaff2
)
1919 le
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
1920 isl_pw_aff_copy(pwaff2
));
1921 return isl_pw_aff_cond(le
, pwaff1
, pwaff2
);
1924 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
1925 __isl_take isl_pw_aff
*pwaff2
)
1927 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
1930 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
1931 __isl_take isl_pw_aff_list
*list
,
1932 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1933 __isl_take isl_pw_aff
*pwaff2
))
1942 ctx
= isl_pw_aff_list_get_ctx(list
);
1944 isl_die(ctx
, isl_error_invalid
,
1945 "list should contain at least one element",
1946 return isl_pw_aff_list_free(list
));
1948 res
= isl_pw_aff_copy(list
->p
[0]);
1949 for (i
= 1; i
< list
->n
; ++i
)
1950 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
1952 isl_pw_aff_list_free(list
);
1956 /* Return an isl_pw_aff that maps each element in the intersection of the
1957 * domains of the elements of list to the minimal corresponding affine
1960 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
1962 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
1965 /* Return an isl_pw_aff that maps each element in the intersection of the
1966 * domains of the elements of list to the maximal corresponding affine
1969 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
1971 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
1977 #include <isl_multi_templ.c>
1979 __isl_give isl_multi_aff
*isl_multi_aff_add(__isl_take isl_multi_aff
*maff1
,
1980 __isl_take isl_multi_aff
*maff2
)
1985 maff1
= isl_multi_aff_cow(maff1
);
1986 if (!maff1
|| !maff2
)
1989 ctx
= isl_multi_aff_get_ctx(maff1
);
1990 if (!isl_space_is_equal(maff1
->space
, maff2
->space
))
1991 isl_die(ctx
, isl_error_invalid
,
1992 "spaces don't match", goto error
);
1994 for (i
= 0; i
< maff1
->n
; ++i
) {
1995 maff1
->p
[i
] = isl_aff_add(maff1
->p
[i
],
1996 isl_aff_copy(maff2
->p
[i
]));
2001 isl_multi_aff_free(maff2
);
2004 isl_multi_aff_free(maff1
);
2005 isl_multi_aff_free(maff2
);
2009 /* Exploit the equalities in "eq" to simplify the affine expressions.
2011 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
2012 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
2016 maff
= isl_multi_aff_cow(maff
);
2020 for (i
= 0; i
< maff
->n
; ++i
) {
2021 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
2022 isl_basic_set_copy(eq
));
2027 isl_basic_set_free(eq
);
2030 isl_basic_set_free(eq
);
2031 isl_multi_aff_free(maff
);
2035 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
2040 maff
= isl_multi_aff_cow(maff
);
2044 for (i
= 0; i
< maff
->n
; ++i
) {
2045 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
2047 return isl_multi_aff_free(maff
);
2053 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
2054 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
2056 maff1
= isl_multi_aff_add(maff1
, maff2
);
2057 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
2061 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
2069 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff
*maff1
,
2070 __isl_keep isl_multi_aff
*maff2
)
2075 if (!maff1
|| !maff2
)
2077 if (maff1
->n
!= maff2
->n
)
2079 equal
= isl_space_is_equal(maff1
->space
, maff2
->space
);
2080 if (equal
< 0 || !equal
)
2083 for (i
= 0; i
< maff1
->n
; ++i
) {
2084 equal
= isl_aff_plain_is_equal(maff1
->p
[i
], maff2
->p
[i
]);
2085 if (equal
< 0 || !equal
)
2092 __isl_give isl_multi_aff
*isl_multi_aff_set_dim_name(
2093 __isl_take isl_multi_aff
*maff
,
2094 enum isl_dim_type type
, unsigned pos
, const char *s
)
2098 maff
= isl_multi_aff_cow(maff
);
2102 maff
->space
= isl_space_set_dim_name(maff
->space
, type
, pos
, s
);
2104 return isl_multi_aff_free(maff
);
2105 for (i
= 0; i
< maff
->n
; ++i
) {
2106 maff
->p
[i
] = isl_aff_set_dim_name(maff
->p
[i
], type
, pos
, s
);
2108 return isl_multi_aff_free(maff
);
2114 __isl_give isl_multi_aff
*isl_multi_aff_drop_dims(__isl_take isl_multi_aff
*maff
,
2115 enum isl_dim_type type
, unsigned first
, unsigned n
)
2119 maff
= isl_multi_aff_cow(maff
);
2123 maff
->space
= isl_space_drop_dims(maff
->space
, type
, first
, n
);
2125 return isl_multi_aff_free(maff
);
2126 for (i
= 0; i
< maff
->n
; ++i
) {
2127 maff
->p
[i
] = isl_aff_drop_dims(maff
->p
[i
], type
, first
, n
);
2129 return isl_multi_aff_free(maff
);
2136 #define PW isl_pw_multi_aff
2138 #define EL isl_multi_aff
2140 #define EL_IS_ZERO is_empty
2144 #define IS_ZERO is_empty
2147 #undef DEFAULT_IS_ZERO
2148 #define DEFAULT_IS_ZERO 0
2153 #define NO_INVOLVES_DIMS
2154 #define NO_MOVE_DIMS
2155 #define NO_INSERT_DIMS
2159 #include <isl_pw_templ.c>
2161 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
2162 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2164 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
2165 &isl_multi_aff_add
);
2168 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
2169 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2171 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
2175 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
2176 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2178 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
2181 /* Construct a map mapping the domain the piecewise multi-affine expression
2182 * to its range, with each dimension in the range equated to the
2183 * corresponding affine expression on its cell.
2185 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
2193 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
2195 for (i
= 0; i
< pma
->n
; ++i
) {
2196 isl_multi_aff
*maff
;
2197 isl_basic_map
*bmap
;
2200 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
2201 bmap
= isl_basic_map_from_multi_aff(maff
);
2202 map_i
= isl_map_from_basic_map(bmap
);
2203 map_i
= isl_map_intersect_domain(map_i
,
2204 isl_set_copy(pma
->p
[i
].set
));
2205 map
= isl_map_union_disjoint(map
, map_i
);
2208 isl_pw_multi_aff_free(pma
);
2212 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
2214 if (!isl_space_is_set(pma
->dim
))
2215 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
2216 "isl_pw_multi_aff cannot be converted into an isl_set",
2217 return isl_pw_multi_aff_free(pma
));
2219 return isl_map_from_pw_multi_aff(pma
);
2222 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2223 * This obivously only works if the input "map" is single-valued.
2224 * If so, we compute the lexicographic minimum of the image in the form
2225 * of an isl_pw_multi_aff. Since the image is unique, it is equal
2226 * to its lexicographic minimum.
2227 * If the input is not single-valued, we produce an error.
2229 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
2233 isl_pw_multi_aff
*pma
;
2238 sv
= isl_map_is_single_valued(map
);
2242 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2243 "map is not single-valued", goto error
);
2244 map
= isl_map_make_disjoint(map
);
2248 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
2250 for (i
= 0; i
< map
->n
; ++i
) {
2251 isl_pw_multi_aff
*pma_i
;
2252 isl_basic_map
*bmap
;
2253 bmap
= isl_basic_map_copy(map
->p
[i
]);
2254 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
2255 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
2265 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
2267 return isl_pw_multi_aff_from_map(set
);
2270 /* Plug in "subs" for dimension "type", "pos" of "aff".
2272 * Let i be the dimension to replace and let "subs" be of the form
2276 * and "aff" of the form
2282 * floor((a f + d g')/(m d))
2284 * where g' is the result of plugging in "subs" in each of the integer
2287 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
2288 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
2293 aff
= isl_aff_cow(aff
);
2295 return isl_aff_free(aff
);
2297 ctx
= isl_aff_get_ctx(aff
);
2298 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
2299 isl_die(ctx
, isl_error_invalid
,
2300 "spaces don't match", return isl_aff_free(aff
));
2301 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
2302 isl_die(ctx
, isl_error_unsupported
,
2303 "cannot handle divs yet", return isl_aff_free(aff
));
2305 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
2307 return isl_aff_free(aff
);
2309 aff
->v
= isl_vec_cow(aff
->v
);
2311 return isl_aff_free(aff
);
2313 pos
+= isl_local_space_offset(aff
->ls
, type
);
2316 isl_int_set(v
, aff
->v
->el
[1 + pos
]);
2317 isl_int_set_si(aff
->v
->el
[1 + pos
], 0);
2318 isl_seq_combine(aff
->v
->el
+ 1, subs
->v
->el
[0], aff
->v
->el
+ 1,
2319 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
2320 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], subs
->v
->el
[0]);
2326 /* Plug in "subs" for dimension "type", "pos" in each of the affine
2327 * expressions in "maff".
2329 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
2330 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
2331 __isl_keep isl_aff
*subs
)
2335 maff
= isl_multi_aff_cow(maff
);
2337 return isl_multi_aff_free(maff
);
2339 if (type
== isl_dim_in
)
2342 for (i
= 0; i
< maff
->n
; ++i
) {
2343 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
2345 return isl_multi_aff_free(maff
);
2351 /* Plug in "subs" for dimension "type", "pos" of "pma".
2353 * pma is of the form
2357 * while subs is of the form
2359 * v' = B_j(v) -> S_j
2361 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
2362 * has a contribution in the result, in particular
2364 * C_ij(S_j) -> M_i(S_j)
2366 * Note that plugging in S_j in C_ij may also result in an empty set
2367 * and this contribution should simply be discarded.
2369 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
2370 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
2371 __isl_keep isl_pw_aff
*subs
)
2374 isl_pw_multi_aff
*res
;
2377 return isl_pw_multi_aff_free(pma
);
2379 n
= pma
->n
* subs
->n
;
2380 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
2382 for (i
= 0; i
< pma
->n
; ++i
) {
2383 for (j
= 0; j
< subs
->n
; ++j
) {
2385 isl_multi_aff
*res_ij
;
2386 common
= isl_set_intersect(
2387 isl_set_copy(pma
->p
[i
].set
),
2388 isl_set_copy(subs
->p
[j
].set
));
2389 common
= isl_set_substitute(common
,
2390 type
, pos
, subs
->p
[j
].aff
);
2391 if (isl_set_plain_is_empty(common
)) {
2392 isl_set_free(common
);
2396 res_ij
= isl_multi_aff_substitute(
2397 isl_multi_aff_copy(pma
->p
[i
].maff
),
2398 type
, pos
, subs
->p
[j
].aff
);
2400 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
2404 isl_pw_multi_aff_free(pma
);
2408 /* Extend the local space of "dst" to include the divs
2409 * in the local space of "src".
2411 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
2412 __isl_keep isl_aff
*src
)
2420 return isl_aff_free(dst
);
2422 ctx
= isl_aff_get_ctx(src
);
2423 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
2424 isl_die(ctx
, isl_error_invalid
,
2425 "spaces don't match", goto error
);
2427 if (src
->ls
->div
->n_row
== 0)
2430 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
2431 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
2435 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
2436 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
2444 return isl_aff_free(dst
);
2447 /* Adjust the local spaces of the affine expressions in "maff"
2448 * such that they all have the save divs.
2450 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
2451 __isl_take isl_multi_aff
*maff
)
2459 maff
= isl_multi_aff_cow(maff
);
2463 for (i
= 1; i
< maff
->n
; ++i
)
2464 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
2465 for (i
= 1; i
< maff
->n
; ++i
) {
2466 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
2468 return isl_multi_aff_free(maff
);
2474 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
2476 aff
= isl_aff_cow(aff
);
2480 aff
->ls
= isl_local_space_lift(aff
->ls
);
2482 return isl_aff_free(aff
);
2487 /* Lift "maff" to a space with extra dimensions such that the result
2488 * has no more existentially quantified variables.
2489 * If "ls" is not NULL, then *ls is assigned the local space that lies
2490 * at the basis of the lifting applied to "maff".
2492 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
2493 __isl_give isl_local_space
**ls
)
2507 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
2508 *ls
= isl_local_space_from_space(space
);
2510 return isl_multi_aff_free(maff
);
2515 maff
= isl_multi_aff_cow(maff
);
2516 maff
= isl_multi_aff_align_divs(maff
);
2520 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
2521 space
= isl_multi_aff_get_space(maff
);
2522 space
= isl_space_lift(isl_space_domain(space
), n_div
);
2523 space
= isl_space_extend_domain_with_range(space
,
2524 isl_multi_aff_get_space(maff
));
2526 return isl_multi_aff_free(maff
);
2527 isl_space_free(maff
->space
);
2528 maff
->space
= space
;
2531 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
2533 return isl_multi_aff_free(maff
);
2536 for (i
= 0; i
< maff
->n
; ++i
) {
2537 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
2545 isl_local_space_free(*ls
);
2546 return isl_multi_aff_free(maff
);
2550 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
2552 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
2553 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
2563 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
2564 if (pos
< 0 || pos
>= n_out
)
2565 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
2566 "index out of bounds", return NULL
);
2568 space
= isl_pw_multi_aff_get_space(pma
);
2569 space
= isl_space_drop_dims(space
, isl_dim_out
,
2570 pos
+ 1, n_out
- pos
- 1);
2571 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
2573 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
2574 for (i
= 0; i
< pma
->n
; ++i
) {
2576 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
2577 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);