2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <isl_val_private.h>
17 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
18 #define FN(TYPE,NAME) xFN(TYPE,NAME)
19 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
20 #define S(TYPE,NAME) xS(TYPE,NAME)
23 __isl_give PW
*FN(PW
,alloc_size
)(__isl_take isl_space
*dim
,
24 enum isl_fold type
, int n
)
26 __isl_give PW
*FN(PW
,alloc_size
)(__isl_take isl_space
*dim
, int n
)
34 ctx
= isl_space_get_ctx(dim
);
35 isl_assert(ctx
, n
>= 0, goto error
);
36 pw
= isl_alloc(ctx
, struct PW
,
37 sizeof(struct PW
) + (n
- 1) * sizeof(S(PW
,piece
)));
55 __isl_give PW
*FN(PW
,ZERO
)(__isl_take isl_space
*dim
, enum isl_fold type
)
57 return FN(PW
,alloc_size
)(dim
, type
, 0);
60 __isl_give PW
*FN(PW
,ZERO
)(__isl_take isl_space
*dim
)
62 return FN(PW
,alloc_size
)(dim
, 0);
66 __isl_give PW
*FN(PW
,add_piece
)(__isl_take PW
*pw
,
67 __isl_take isl_set
*set
, __isl_take EL
*el
)
70 isl_space
*el_dim
= NULL
;
72 if (!pw
|| !set
|| !el
)
75 if (isl_set_plain_is_empty(set
) || FN(EL
,EL_IS_ZERO
)(el
)) {
81 ctx
= isl_set_get_ctx(set
);
83 if (pw
->type
!= el
->type
)
84 isl_die(ctx
, isl_error_invalid
, "fold types don't match",
87 el_dim
= FN(EL
,get_space(el
));
88 isl_assert(ctx
, isl_space_is_equal(pw
->dim
, el_dim
), goto error
);
89 isl_assert(ctx
, pw
->n
< pw
->size
, goto error
);
91 pw
->p
[pw
->n
].set
= set
;
92 pw
->p
[pw
->n
].FIELD
= el
;
95 isl_space_free(el_dim
);
98 isl_space_free(el_dim
);
106 __isl_give PW
*FN(PW
,alloc
)(enum isl_fold type
,
107 __isl_take isl_set
*set
, __isl_take EL
*el
)
109 __isl_give PW
*FN(PW
,alloc
)(__isl_take isl_set
*set
, __isl_take EL
*el
)
118 pw
= FN(PW
,alloc_size
)(FN(EL
,get_space
)(el
), type
, 1);
120 pw
= FN(PW
,alloc_size
)(FN(EL
,get_space
)(el
), 1);
123 return FN(PW
,add_piece
)(pw
, set
, el
);
130 __isl_give PW
*FN(PW
,dup
)(__isl_keep PW
*pw
)
139 dup
= FN(PW
,alloc_size
)(isl_space_copy(pw
->dim
), pw
->type
, pw
->n
);
141 dup
= FN(PW
,alloc_size
)(isl_space_copy(pw
->dim
), pw
->n
);
146 for (i
= 0; i
< pw
->n
; ++i
)
147 dup
= FN(PW
,add_piece
)(dup
, isl_set_copy(pw
->p
[i
].set
),
148 FN(EL
,copy
)(pw
->p
[i
].FIELD
));
153 __isl_give PW
*FN(PW
,cow
)(__isl_take PW
*pw
)
161 return FN(PW
,dup
)(pw
);
164 __isl_give PW
*FN(PW
,copy
)(__isl_keep PW
*pw
)
173 __isl_null PW
*FN(PW
,free
)(__isl_take PW
*pw
)
182 for (i
= 0; i
< pw
->n
; ++i
) {
183 isl_set_free(pw
->p
[i
].set
);
184 FN(EL
,free
)(pw
->p
[i
].FIELD
);
186 isl_space_free(pw
->dim
);
192 const char *FN(PW
,get_dim_name
)(__isl_keep PW
*pw
, enum isl_dim_type type
,
195 return pw
? isl_space_get_dim_name(pw
->dim
, type
, pos
) : NULL
;
198 int FN(PW
,has_dim_id
)(__isl_keep PW
*pw
, enum isl_dim_type type
, unsigned pos
)
200 return pw
? isl_space_has_dim_id(pw
->dim
, type
, pos
) : -1;
203 __isl_give isl_id
*FN(PW
,get_dim_id
)(__isl_keep PW
*pw
, enum isl_dim_type type
,
206 return pw
? isl_space_get_dim_id(pw
->dim
, type
, pos
) : NULL
;
209 int FN(PW
,has_tuple_name
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
211 return pw
? isl_space_has_tuple_name(pw
->dim
, type
) : -1;
214 const char *FN(PW
,get_tuple_name
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
216 return pw
? isl_space_get_tuple_name(pw
->dim
, type
) : NULL
;
219 int FN(PW
,has_tuple_id
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
221 return pw
? isl_space_has_tuple_id(pw
->dim
, type
) : -1;
224 __isl_give isl_id
*FN(PW
,get_tuple_id
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
226 return pw
? isl_space_get_tuple_id(pw
->dim
, type
) : NULL
;
229 int FN(PW
,IS_ZERO
)(__isl_keep PW
*pw
)
238 __isl_give PW
*FN(PW
,realign_domain
)(__isl_take PW
*pw
,
239 __isl_take isl_reordering
*exp
)
247 for (i
= 0; i
< pw
->n
; ++i
) {
248 pw
->p
[i
].set
= isl_set_realign(pw
->p
[i
].set
,
249 isl_reordering_copy(exp
));
252 pw
->p
[i
].FIELD
= FN(EL
,realign_domain
)(pw
->p
[i
].FIELD
,
253 isl_reordering_copy(exp
));
258 pw
= FN(PW
,reset_domain_space
)(pw
, isl_space_copy(exp
->dim
));
260 isl_reordering_free(exp
);
263 isl_reordering_free(exp
);
268 /* Align the parameters of "pw" to those of "model".
270 __isl_give PW
*FN(PW
,align_params
)(__isl_take PW
*pw
, __isl_take isl_space
*model
)
277 ctx
= isl_space_get_ctx(model
);
278 if (!isl_space_has_named_params(model
))
279 isl_die(ctx
, isl_error_invalid
,
280 "model has unnamed parameters", goto error
);
281 if (!isl_space_has_named_params(pw
->dim
))
282 isl_die(ctx
, isl_error_invalid
,
283 "input has unnamed parameters", goto error
);
284 if (!isl_space_match(pw
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
287 model
= isl_space_drop_dims(model
, isl_dim_in
,
288 0, isl_space_dim(model
, isl_dim_in
));
289 model
= isl_space_drop_dims(model
, isl_dim_out
,
290 0, isl_space_dim(model
, isl_dim_out
));
291 exp
= isl_parameter_alignment_reordering(pw
->dim
, model
);
292 exp
= isl_reordering_extend_space(exp
,
293 FN(PW
,get_domain_space
)(pw
));
294 pw
= FN(PW
,realign_domain
)(pw
, exp
);
297 isl_space_free(model
);
300 isl_space_free(model
);
305 static __isl_give PW
*FN(PW
,align_params_pw_pw_and
)(__isl_take PW
*pw1
,
307 __isl_give PW
*(*fn
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
))
313 if (isl_space_match(pw1
->dim
, isl_dim_param
, pw2
->dim
, isl_dim_param
))
315 ctx
= FN(PW
,get_ctx
)(pw1
);
316 if (!isl_space_has_named_params(pw1
->dim
) ||
317 !isl_space_has_named_params(pw2
->dim
))
318 isl_die(ctx
, isl_error_invalid
,
319 "unaligned unnamed parameters", goto error
);
320 pw1
= FN(PW
,align_params
)(pw1
, FN(PW
,get_space
)(pw2
));
321 pw2
= FN(PW
,align_params
)(pw2
, FN(PW
,get_space
)(pw1
));
329 static __isl_give PW
*FN(PW
,align_params_pw_set_and
)(__isl_take PW
*pw
,
330 __isl_take isl_set
*set
,
331 __isl_give PW
*(*fn
)(__isl_take PW
*pw
, __isl_take isl_set
*set
))
337 if (isl_space_match(pw
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
))
339 ctx
= FN(PW
,get_ctx
)(pw
);
340 if (!isl_space_has_named_params(pw
->dim
) ||
341 !isl_space_has_named_params(set
->dim
))
342 isl_die(ctx
, isl_error_invalid
,
343 "unaligned unnamed parameters", goto error
);
344 pw
= FN(PW
,align_params
)(pw
, isl_set_get_space(set
));
345 set
= isl_set_align_params(set
, FN(PW
,get_space
)(pw
));
354 static __isl_give PW
*FN(PW
,union_add_aligned
)(__isl_take PW
*pw1
,
365 ctx
= isl_space_get_ctx(pw1
->dim
);
367 if (pw1
->type
!= pw2
->type
)
368 isl_die(ctx
, isl_error_invalid
,
369 "fold types don't match", goto error
);
371 isl_assert(ctx
, isl_space_is_equal(pw1
->dim
, pw2
->dim
), goto error
);
373 if (FN(PW
,IS_ZERO
)(pw1
)) {
378 if (FN(PW
,IS_ZERO
)(pw2
)) {
383 n
= (pw1
->n
+ 1) * (pw2
->n
+ 1);
385 res
= FN(PW
,alloc_size
)(isl_space_copy(pw1
->dim
), pw1
->type
, n
);
387 res
= FN(PW
,alloc_size
)(isl_space_copy(pw1
->dim
), n
);
390 for (i
= 0; i
< pw1
->n
; ++i
) {
391 set
= isl_set_copy(pw1
->p
[i
].set
);
392 for (j
= 0; j
< pw2
->n
; ++j
) {
393 struct isl_set
*common
;
395 common
= isl_set_intersect(isl_set_copy(pw1
->p
[i
].set
),
396 isl_set_copy(pw2
->p
[j
].set
));
397 if (isl_set_plain_is_empty(common
)) {
398 isl_set_free(common
);
401 set
= isl_set_subtract(set
,
402 isl_set_copy(pw2
->p
[j
].set
));
404 sum
= FN(EL
,add_on_domain
)(common
,
405 FN(EL
,copy
)(pw1
->p
[i
].FIELD
),
406 FN(EL
,copy
)(pw2
->p
[j
].FIELD
));
408 res
= FN(PW
,add_piece
)(res
, common
, sum
);
410 res
= FN(PW
,add_piece
)(res
, set
, FN(EL
,copy
)(pw1
->p
[i
].FIELD
));
413 for (j
= 0; j
< pw2
->n
; ++j
) {
414 set
= isl_set_copy(pw2
->p
[j
].set
);
415 for (i
= 0; i
< pw1
->n
; ++i
)
416 set
= isl_set_subtract(set
,
417 isl_set_copy(pw1
->p
[i
].set
));
418 res
= FN(PW
,add_piece
)(res
, set
, FN(EL
,copy
)(pw2
->p
[j
].FIELD
));
431 /* Private version of "union_add". For isl_pw_qpolynomial and
432 * isl_pw_qpolynomial_fold, we prefer to simply call it "add".
434 static __isl_give PW
*FN(PW
,union_add_
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
)
436 return FN(PW
,align_params_pw_pw_and
)(pw1
, pw2
,
437 &FN(PW
,union_add_aligned
));
440 /* Make sure "pw" has room for at least "n" more pieces.
442 * If there is only one reference to pw, we extend it in place.
443 * Otherwise, we create a new PW and copy the pieces.
445 static __isl_give PW
*FN(PW
,grow
)(__isl_take PW
*pw
, int n
)
453 if (pw
->n
+ n
<= pw
->size
)
455 ctx
= FN(PW
,get_ctx
)(pw
);
458 res
= isl_realloc(ctx
, pw
, struct PW
,
459 sizeof(struct PW
) + (n
- 1) * sizeof(S(PW
,piece
)));
461 return FN(PW
,free
)(pw
);
466 res
= FN(PW
,alloc_size
)(isl_space_copy(pw
->dim
), pw
->type
, n
);
468 res
= FN(PW
,alloc_size
)(isl_space_copy(pw
->dim
), n
);
471 return FN(PW
,free
)(pw
);
472 for (i
= 0; i
< pw
->n
; ++i
)
473 res
= FN(PW
,add_piece
)(res
, isl_set_copy(pw
->p
[i
].set
),
474 FN(EL
,copy
)(pw
->p
[i
].FIELD
));
479 static __isl_give PW
*FN(PW
,add_disjoint_aligned
)(__isl_take PW
*pw1
,
488 if (pw1
->size
< pw1
->n
+ pw2
->n
&& pw1
->n
< pw2
->n
)
489 return FN(PW
,add_disjoint_aligned
)(pw2
, pw1
);
491 ctx
= isl_space_get_ctx(pw1
->dim
);
493 if (pw1
->type
!= pw2
->type
)
494 isl_die(ctx
, isl_error_invalid
,
495 "fold types don't match", goto error
);
497 isl_assert(ctx
, isl_space_is_equal(pw1
->dim
, pw2
->dim
), goto error
);
499 if (FN(PW
,IS_ZERO
)(pw1
)) {
504 if (FN(PW
,IS_ZERO
)(pw2
)) {
509 pw1
= FN(PW
,grow
)(pw1
, pw2
->n
);
513 for (i
= 0; i
< pw2
->n
; ++i
)
514 pw1
= FN(PW
,add_piece
)(pw1
,
515 isl_set_copy(pw2
->p
[i
].set
),
516 FN(EL
,copy
)(pw2
->p
[i
].FIELD
));
527 __isl_give PW
*FN(PW
,add_disjoint
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
)
529 return FN(PW
,align_params_pw_pw_and
)(pw1
, pw2
,
530 &FN(PW
,add_disjoint_aligned
));
533 /* This function is currently only used from isl_aff.c
535 static __isl_give PW
*FN(PW
,on_shared_domain_in
)(__isl_take PW
*pw1
,
536 __isl_take PW
*pw2
, __isl_take isl_space
*space
,
537 __isl_give EL
*(*fn
)(__isl_take EL
*el1
, __isl_take EL
*el2
))
538 __attribute__ ((unused
));
540 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
541 * The result of "fn" (and therefore also of this function) lives in "space".
543 static __isl_give PW
*FN(PW
,on_shared_domain_in
)(__isl_take PW
*pw1
,
544 __isl_take PW
*pw2
, __isl_take isl_space
*space
,
545 __isl_give EL
*(*fn
)(__isl_take EL
*el1
, __isl_take EL
*el2
))
555 res
= FN(PW
,alloc_size
)(isl_space_copy(space
), pw1
->type
, n
);
557 res
= FN(PW
,alloc_size
)(isl_space_copy(space
), n
);
560 for (i
= 0; i
< pw1
->n
; ++i
) {
561 for (j
= 0; j
< pw2
->n
; ++j
) {
566 common
= isl_set_intersect(
567 isl_set_copy(pw1
->p
[i
].set
),
568 isl_set_copy(pw2
->p
[j
].set
));
569 empty
= isl_set_plain_is_empty(common
);
570 if (empty
< 0 || empty
) {
571 isl_set_free(common
);
577 res_ij
= fn(FN(EL
,copy
)(pw1
->p
[i
].FIELD
),
578 FN(EL
,copy
)(pw2
->p
[j
].FIELD
));
579 res_ij
= FN(EL
,gist
)(res_ij
, isl_set_copy(common
));
581 res
= FN(PW
,add_piece
)(res
, common
, res_ij
);
585 isl_space_free(space
);
590 isl_space_free(space
);
597 /* This function is currently only used from isl_aff.c
599 static __isl_give PW
*FN(PW
,on_shared_domain
)(__isl_take PW
*pw1
,
601 __isl_give EL
*(*fn
)(__isl_take EL
*el1
, __isl_take EL
*el2
))
602 __attribute__ ((unused
));
604 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
605 * The result of "fn" is assumed to live in the same space as "pw1" and "pw2".
607 static __isl_give PW
*FN(PW
,on_shared_domain
)(__isl_take PW
*pw1
,
609 __isl_give EL
*(*fn
)(__isl_take EL
*el1
, __isl_take EL
*el2
))
616 space
= isl_space_copy(pw1
->dim
);
617 return FN(PW
,on_shared_domain_in
)(pw1
, pw2
, space
, fn
);
625 __isl_give PW
*FN(PW
,neg
)(__isl_take PW
*pw
)
632 if (FN(PW
,IS_ZERO
)(pw
))
639 for (i
= 0; i
< pw
->n
; ++i
) {
640 pw
->p
[i
].FIELD
= FN(EL
,neg
)(pw
->p
[i
].FIELD
);
642 return FN(PW
,free
)(pw
);
648 __isl_give PW
*FN(PW
,sub
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
)
650 return FN(PW
,add
)(pw1
, FN(PW
,neg
)(pw2
));
655 __isl_give isl_val
*FN(PW
,eval
)(__isl_take PW
*pw
, __isl_take isl_point
*pnt
)
660 isl_space
*pnt_dim
= NULL
;
665 ctx
= isl_point_get_ctx(pnt
);
666 pnt_dim
= isl_point_get_space(pnt
);
667 isl_assert(ctx
, isl_space_is_domain_internal(pnt_dim
, pw
->dim
),
670 for (i
= 0; i
< pw
->n
; ++i
) {
671 found
= isl_set_contains_point(pw
->p
[i
].set
, pnt
);
678 v
= FN(EL
,eval
)(FN(EL
,copy
)(pw
->p
[i
].FIELD
),
679 isl_point_copy(pnt
));
681 v
= isl_val_zero(ctx
);
683 isl_space_free(pnt_dim
);
688 isl_space_free(pnt_dim
);
694 /* Return the parameter domain of "pw".
696 __isl_give isl_set
*FN(PW
,params
)(__isl_take PW
*pw
)
698 return isl_set_params(FN(PW
,domain
)(pw
));
701 __isl_give isl_set
*FN(PW
,domain
)(__isl_take PW
*pw
)
709 dom
= isl_set_empty(FN(PW
,get_domain_space
)(pw
));
710 for (i
= 0; i
< pw
->n
; ++i
)
711 dom
= isl_set_union_disjoint(dom
, isl_set_copy(pw
->p
[i
].set
));
718 /* Exploit the equalities in the domain of piece "i" of "pw"
719 * to simplify the associated function.
720 * If the domain of piece "i" is empty, then remove it entirely,
721 * replacing it with the final piece.
723 static int FN(PW
,exploit_equalities_and_remove_if_empty
)(__isl_keep PW
*pw
,
727 int empty
= isl_set_plain_is_empty(pw
->p
[i
].set
);
732 isl_set_free(pw
->p
[i
].set
);
733 FN(EL
,free
)(pw
->p
[i
].FIELD
);
735 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
741 aff
= isl_set_affine_hull(isl_set_copy(pw
->p
[i
].set
));
742 pw
->p
[i
].FIELD
= FN(EL
,substitute_equalities
)(pw
->p
[i
].FIELD
, aff
);
749 /* Convert a piecewise expression defined over a parameter domain
750 * into one that is defined over a zero-dimensional set.
752 __isl_give PW
*FN(PW
,from_range
)(__isl_take PW
*pw
)
758 if (!isl_space_is_set(pw
->dim
))
759 isl_die(FN(PW
,get_ctx
)(pw
), isl_error_invalid
,
760 "not living in a set space", return FN(PW
,free
)(pw
));
762 space
= FN(PW
,get_space
)(pw
);
763 space
= isl_space_from_range(space
);
764 pw
= FN(PW
,reset_space
)(pw
, space
);
769 /* Fix the value of the given parameter or domain dimension of "pw"
770 * to be equal to "value".
772 __isl_give PW
*FN(PW
,fix_si
)(__isl_take PW
*pw
, enum isl_dim_type type
,
773 unsigned pos
, int value
)
780 if (type
== isl_dim_out
)
781 isl_die(FN(PW
,get_ctx
)(pw
), isl_error_invalid
,
782 "cannot fix output dimension", return FN(PW
,free
)(pw
));
787 if (type
== isl_dim_in
)
792 return FN(PW
,free
)(pw
);
794 for (i
= pw
->n
- 1; i
>= 0; --i
) {
795 pw
->p
[i
].set
= isl_set_fix_si(pw
->p
[i
].set
, type
, pos
, value
);
796 if (FN(PW
,exploit_equalities_and_remove_if_empty
)(pw
, i
) < 0)
797 return FN(PW
,free
)(pw
);
803 /* Restrict the domain of "pw" by combining each cell
804 * with "set" through a call to "fn", where "fn" may be
805 * isl_set_intersect or isl_set_intersect_params.
807 static __isl_give PW
*FN(PW
,intersect_aligned
)(__isl_take PW
*pw
,
808 __isl_take isl_set
*set
,
809 __isl_give isl_set
*(*fn
)(__isl_take isl_set
*set1
,
810 __isl_take isl_set
*set2
))
826 for (i
= pw
->n
- 1; i
>= 0; --i
) {
827 pw
->p
[i
].set
= fn(pw
->p
[i
].set
, isl_set_copy(set
));
828 if (FN(PW
,exploit_equalities_and_remove_if_empty
)(pw
, i
) < 0)
840 static __isl_give PW
*FN(PW
,intersect_domain_aligned
)(__isl_take PW
*pw
,
841 __isl_take isl_set
*set
)
843 return FN(PW
,intersect_aligned
)(pw
, set
, &isl_set_intersect
);
846 __isl_give PW
*FN(PW
,intersect_domain
)(__isl_take PW
*pw
,
847 __isl_take isl_set
*context
)
849 return FN(PW
,align_params_pw_set_and
)(pw
, context
,
850 &FN(PW
,intersect_domain_aligned
));
853 static __isl_give PW
*FN(PW
,intersect_params_aligned
)(__isl_take PW
*pw
,
854 __isl_take isl_set
*set
)
856 return FN(PW
,intersect_aligned
)(pw
, set
, &isl_set_intersect_params
);
859 /* Intersect the domain of "pw" with the parameter domain "context".
861 __isl_give PW
*FN(PW
,intersect_params
)(__isl_take PW
*pw
,
862 __isl_take isl_set
*context
)
864 return FN(PW
,align_params_pw_set_and
)(pw
, context
,
865 &FN(PW
,intersect_params_aligned
));
868 /* Compute the gist of "pw" with respect to the domain constraints
869 * of "context" for the case where the domain of the last element
870 * of "pw" is equal to "context".
871 * Call "fn_el" to compute the gist of this element, replace
872 * its domain by the universe and drop all other elements
873 * as their domains are necessarily disjoint from "context".
875 static __isl_give PW
*FN(PW
,gist_last
)(__isl_take PW
*pw
,
876 __isl_take isl_set
*context
,
877 __isl_give EL
*(*fn_el
)(__isl_take EL
*el
, __isl_take isl_set
*set
))
882 for (i
= 0; i
< pw
->n
- 1; ++i
) {
883 isl_set_free(pw
->p
[i
].set
);
884 FN(EL
,free
)(pw
->p
[i
].FIELD
);
886 pw
->p
[0].FIELD
= pw
->p
[pw
->n
- 1].FIELD
;
887 pw
->p
[0].set
= pw
->p
[pw
->n
- 1].set
;
890 space
= isl_set_get_space(context
);
891 pw
->p
[0].FIELD
= fn_el(pw
->p
[0].FIELD
, context
);
892 context
= isl_set_universe(space
);
893 isl_set_free(pw
->p
[0].set
);
894 pw
->p
[0].set
= context
;
896 if (!pw
->p
[0].FIELD
|| !pw
->p
[0].set
)
897 return FN(PW
,free
)(pw
);
902 /* Compute the gist of "pw" with respect to the domain constraints
903 * of "context". Call "fn_el" to compute the gist of the elements
904 * and "fn_dom" to compute the gist of the domains.
906 * If the piecewise expression is empty or the context is the universe,
907 * then nothing can be simplified.
909 static __isl_give PW
*FN(PW
,gist_aligned
)(__isl_take PW
*pw
,
910 __isl_take isl_set
*context
,
911 __isl_give EL
*(*fn_el
)(__isl_take EL
*el
,
912 __isl_take isl_set
*set
),
913 __isl_give isl_set
*(*fn_dom
)(__isl_take isl_set
*set
,
914 __isl_take isl_basic_set
*bset
))
918 isl_basic_set
*hull
= NULL
;
924 isl_set_free(context
);
928 is_universe
= isl_set_plain_is_universe(context
);
932 isl_set_free(context
);
936 if (!isl_space_match(pw
->dim
, isl_dim_param
,
937 context
->dim
, isl_dim_param
)) {
938 pw
= FN(PW
,align_params
)(pw
, isl_set_get_space(context
));
939 context
= isl_set_align_params(context
, FN(PW
,get_space
)(pw
));
949 equal
= isl_set_plain_is_equal(pw
->p
[0].set
, context
);
953 return FN(PW
,gist_last
)(pw
, context
, fn_el
);
956 context
= isl_set_compute_divs(context
);
957 hull
= isl_set_simple_hull(isl_set_copy(context
));
959 for (i
= pw
->n
- 1; i
>= 0; --i
) {
963 if (i
== pw
->n
- 1) {
965 equal
= isl_set_plain_is_equal(pw
->p
[i
].set
, context
);
969 isl_basic_set_free(hull
);
970 return FN(PW
,gist_last
)(pw
, context
, fn_el
);
973 set_i
= isl_set_intersect(isl_set_copy(pw
->p
[i
].set
),
974 isl_set_copy(context
));
975 empty
= isl_set_plain_is_empty(set_i
);
976 pw
->p
[i
].FIELD
= fn_el(pw
->p
[i
].FIELD
, set_i
);
977 pw
->p
[i
].set
= fn_dom(pw
->p
[i
].set
, isl_basic_set_copy(hull
));
978 if (empty
< 0 || !pw
->p
[i
].FIELD
|| !pw
->p
[i
].set
)
981 isl_set_free(pw
->p
[i
].set
);
982 FN(EL
,free
)(pw
->p
[i
].FIELD
);
984 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
989 isl_basic_set_free(hull
);
990 isl_set_free(context
);
995 isl_basic_set_free(hull
);
996 isl_set_free(context
);
1000 static __isl_give PW
*FN(PW
,gist_domain_aligned
)(__isl_take PW
*pw
,
1001 __isl_take isl_set
*set
)
1003 return FN(PW
,gist_aligned
)(pw
, set
, &FN(EL
,gist
),
1004 &isl_set_gist_basic_set
);
1007 __isl_give PW
*FN(PW
,gist
)(__isl_take PW
*pw
, __isl_take isl_set
*context
)
1009 return FN(PW
,align_params_pw_set_and
)(pw
, context
,
1010 &FN(PW
,gist_domain_aligned
));
1013 static __isl_give PW
*FN(PW
,gist_params_aligned
)(__isl_take PW
*pw
,
1014 __isl_take isl_set
*set
)
1016 return FN(PW
,gist_aligned
)(pw
, set
, &FN(EL
,gist_params
),
1017 &isl_set_gist_params_basic_set
);
1020 __isl_give PW
*FN(PW
,gist_params
)(__isl_take PW
*pw
,
1021 __isl_take isl_set
*context
)
1023 return FN(PW
,align_params_pw_set_and
)(pw
, context
,
1024 &FN(PW
,gist_params_aligned
));
1027 __isl_give PW
*FN(PW
,coalesce
)(__isl_take PW
*pw
)
1036 for (i
= pw
->n
- 1; i
>= 0; --i
) {
1037 for (j
= i
- 1; j
>= 0; --j
) {
1038 if (!FN(EL
,plain_is_equal
)(pw
->p
[i
].FIELD
,
1041 pw
->p
[j
].set
= isl_set_union(pw
->p
[j
].set
,
1043 FN(EL
,free
)(pw
->p
[i
].FIELD
);
1045 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
1051 pw
->p
[i
].set
= isl_set_coalesce(pw
->p
[i
].set
);
1062 isl_ctx
*FN(PW
,get_ctx
)(__isl_keep PW
*pw
)
1064 return pw
? isl_space_get_ctx(pw
->dim
) : NULL
;
1067 #ifndef NO_INVOLVES_DIMS
1068 int FN(PW
,involves_dims
)(__isl_keep PW
*pw
, enum isl_dim_type type
,
1069 unsigned first
, unsigned n
)
1072 enum isl_dim_type set_type
;
1076 if (pw
->n
== 0 || n
== 0)
1079 set_type
= type
== isl_dim_in
? isl_dim_set
: type
;
1081 for (i
= 0; i
< pw
->n
; ++i
) {
1082 int involves
= FN(EL
,involves_dims
)(pw
->p
[i
].FIELD
,
1084 if (involves
< 0 || involves
)
1086 involves
= isl_set_involves_dims(pw
->p
[i
].set
,
1087 set_type
, first
, n
);
1088 if (involves
< 0 || involves
)
1095 __isl_give PW
*FN(PW
,set_dim_name
)(__isl_take PW
*pw
,
1096 enum isl_dim_type type
, unsigned pos
, const char *s
)
1099 enum isl_dim_type set_type
;
1101 pw
= FN(PW
,cow
)(pw
);
1105 set_type
= type
== isl_dim_in
? isl_dim_set
: type
;
1107 pw
->dim
= isl_space_set_dim_name(pw
->dim
, type
, pos
, s
);
1111 for (i
= 0; i
< pw
->n
; ++i
) {
1112 pw
->p
[i
].set
= isl_set_set_dim_name(pw
->p
[i
].set
,
1116 pw
->p
[i
].FIELD
= FN(EL
,set_dim_name
)(pw
->p
[i
].FIELD
, type
, pos
, s
);
1117 if (!pw
->p
[i
].FIELD
)
1127 #ifndef NO_DROP_DIMS
1128 __isl_give PW
*FN(PW
,drop_dims
)(__isl_take PW
*pw
,
1129 enum isl_dim_type type
, unsigned first
, unsigned n
)
1132 enum isl_dim_type set_type
;
1136 if (n
== 0 && !isl_space_get_tuple_name(pw
->dim
, type
))
1139 set_type
= type
== isl_dim_in
? isl_dim_set
: type
;
1141 pw
= FN(PW
,cow
)(pw
);
1144 pw
->dim
= isl_space_drop_dims(pw
->dim
, type
, first
, n
);
1147 for (i
= 0; i
< pw
->n
; ++i
) {
1148 pw
->p
[i
].FIELD
= FN(EL
,drop_dims
)(pw
->p
[i
].FIELD
, type
, first
, n
);
1149 if (!pw
->p
[i
].FIELD
)
1151 if (type
== isl_dim_out
)
1153 pw
->p
[i
].set
= isl_set_drop(pw
->p
[i
].set
, set_type
, first
, n
);
1164 /* This function is very similar to drop_dims.
1165 * The only difference is that the cells may still involve
1166 * the specified dimensions. They are removed using
1167 * isl_set_project_out instead of isl_set_drop.
1169 __isl_give PW
*FN(PW
,project_out
)(__isl_take PW
*pw
,
1170 enum isl_dim_type type
, unsigned first
, unsigned n
)
1173 enum isl_dim_type set_type
;
1177 if (n
== 0 && !isl_space_get_tuple_name(pw
->dim
, type
))
1180 set_type
= type
== isl_dim_in
? isl_dim_set
: type
;
1182 pw
= FN(PW
,cow
)(pw
);
1185 pw
->dim
= isl_space_drop_dims(pw
->dim
, type
, first
, n
);
1188 for (i
= 0; i
< pw
->n
; ++i
) {
1189 pw
->p
[i
].set
= isl_set_project_out(pw
->p
[i
].set
,
1190 set_type
, first
, n
);
1193 pw
->p
[i
].FIELD
= FN(EL
,drop_dims
)(pw
->p
[i
].FIELD
, type
, first
, n
);
1194 if (!pw
->p
[i
].FIELD
)
1204 /* Project the domain of pw onto its parameter space.
1206 __isl_give PW
*FN(PW
,project_domain_on_params
)(__isl_take PW
*pw
)
1211 n
= FN(PW
,dim
)(pw
, isl_dim_in
);
1212 pw
= FN(PW
,project_out
)(pw
, isl_dim_in
, 0, n
);
1213 space
= FN(PW
,get_domain_space
)(pw
);
1214 space
= isl_space_params(space
);
1215 pw
= FN(PW
,reset_domain_space
)(pw
, space
);
1220 #ifndef NO_INSERT_DIMS
1221 __isl_give PW
*FN(PW
,insert_dims
)(__isl_take PW
*pw
, enum isl_dim_type type
,
1222 unsigned first
, unsigned n
)
1225 enum isl_dim_type set_type
;
1229 if (n
== 0 && !isl_space_is_named_or_nested(pw
->dim
, type
))
1232 set_type
= type
== isl_dim_in
? isl_dim_set
: type
;
1234 pw
= FN(PW
,cow
)(pw
);
1238 pw
->dim
= isl_space_insert_dims(pw
->dim
, type
, first
, n
);
1242 for (i
= 0; i
< pw
->n
; ++i
) {
1243 pw
->p
[i
].set
= isl_set_insert_dims(pw
->p
[i
].set
,
1244 set_type
, first
, n
);
1247 pw
->p
[i
].FIELD
= FN(EL
,insert_dims
)(pw
->p
[i
].FIELD
,
1249 if (!pw
->p
[i
].FIELD
)
1260 __isl_give PW
*FN(PW
,fix_dim
)(__isl_take PW
*pw
,
1261 enum isl_dim_type type
, unsigned pos
, isl_int v
)
1268 if (type
== isl_dim_in
)
1271 pw
= FN(PW
,cow
)(pw
);
1274 for (i
= 0; i
< pw
->n
; ++i
) {
1275 pw
->p
[i
].set
= isl_set_fix(pw
->p
[i
].set
, type
, pos
, v
);
1276 if (FN(PW
,exploit_equalities_and_remove_if_empty
)(pw
, i
) < 0)
1277 return FN(PW
,free
)(pw
);
1283 /* Fix the value of the variable at position "pos" of type "type" of "pw"
1284 * to be equal to "v".
1286 __isl_give PW
*FN(PW
,fix_val
)(__isl_take PW
*pw
,
1287 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
1290 return FN(PW
,free
)(pw
);
1291 if (!isl_val_is_int(v
))
1292 isl_die(FN(PW
,get_ctx
)(pw
), isl_error_invalid
,
1293 "expecting integer value", goto error
);
1295 pw
= FN(PW
,fix_dim
)(pw
, type
, pos
, v
->n
);
1301 return FN(PW
,free
)(pw
);
1304 unsigned FN(PW
,dim
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
1306 return pw
? isl_space_dim(pw
->dim
, type
) : 0;
1309 __isl_give PW
*FN(PW
,split_dims
)(__isl_take PW
*pw
,
1310 enum isl_dim_type type
, unsigned first
, unsigned n
)
1319 if (type
== isl_dim_in
)
1322 pw
= FN(PW
,cow
)(pw
);
1327 for (i
= 0; i
< pw
->n
; ++i
) {
1328 pw
->p
[i
].set
= isl_set_split_dims(pw
->p
[i
].set
, type
, first
, n
);
1340 /* Compute the maximal value attained by the piecewise quasipolynomial
1341 * on its domain or zero if the domain is empty.
1342 * In the worst case, the domain is scanned completely,
1343 * so the domain is assumed to be bounded.
1345 __isl_give isl_val
*FN(PW
,opt
)(__isl_take PW
*pw
, int max
)
1354 opt
= isl_val_zero(FN(PW
,get_ctx
)(pw
));
1359 opt
= FN(EL
,opt_on_domain
)(FN(EL
,copy
)(pw
->p
[0].FIELD
),
1360 isl_set_copy(pw
->p
[0].set
), max
);
1361 for (i
= 1; i
< pw
->n
; ++i
) {
1363 opt_i
= FN(EL
,opt_on_domain
)(FN(EL
,copy
)(pw
->p
[i
].FIELD
),
1364 isl_set_copy(pw
->p
[i
].set
), max
);
1366 opt
= isl_val_max(opt
, opt_i
);
1368 opt
= isl_val_min(opt
, opt_i
);
1375 __isl_give isl_val
*FN(PW
,max
)(__isl_take PW
*pw
)
1377 return FN(PW
,opt
)(pw
, 1);
1380 __isl_give isl_val
*FN(PW
,min
)(__isl_take PW
*pw
)
1382 return FN(PW
,opt
)(pw
, 0);
1386 __isl_give isl_space
*FN(PW
,get_space
)(__isl_keep PW
*pw
)
1388 return pw
? isl_space_copy(pw
->dim
) : NULL
;
1391 __isl_give isl_space
*FN(PW
,get_domain_space
)(__isl_keep PW
*pw
)
1393 return pw
? isl_space_domain(isl_space_copy(pw
->dim
)) : NULL
;
1396 /* Return the position of the dimension of the given type and name
1398 * Return -1 if no such dimension can be found.
1400 int FN(PW
,find_dim_by_name
)(__isl_keep PW
*pw
,
1401 enum isl_dim_type type
, const char *name
)
1405 return isl_space_find_dim_by_name(pw
->dim
, type
, name
);
1408 #ifndef NO_RESET_DIM
1409 /* Reset the space of "pw". Since we don't know if the elements
1410 * represent the spaces themselves or their domains, we pass along
1411 * both when we call their reset_space_and_domain.
1413 static __isl_give PW
*FN(PW
,reset_space_and_domain
)(__isl_take PW
*pw
,
1414 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
1418 pw
= FN(PW
,cow
)(pw
);
1419 if (!pw
|| !space
|| !domain
)
1422 for (i
= 0; i
< pw
->n
; ++i
) {
1423 pw
->p
[i
].set
= isl_set_reset_space(pw
->p
[i
].set
,
1424 isl_space_copy(domain
));
1427 pw
->p
[i
].FIELD
= FN(EL
,reset_space_and_domain
)(pw
->p
[i
].FIELD
,
1428 isl_space_copy(space
), isl_space_copy(domain
));
1429 if (!pw
->p
[i
].FIELD
)
1433 isl_space_free(domain
);
1435 isl_space_free(pw
->dim
);
1440 isl_space_free(domain
);
1441 isl_space_free(space
);
1446 __isl_give PW
*FN(PW
,reset_domain_space
)(__isl_take PW
*pw
,
1447 __isl_take isl_space
*domain
)
1451 space
= isl_space_extend_domain_with_range(isl_space_copy(domain
),
1452 FN(PW
,get_space
)(pw
));
1453 return FN(PW
,reset_space_and_domain
)(pw
, space
, domain
);
1456 __isl_give PW
*FN(PW
,reset_space
)(__isl_take PW
*pw
, __isl_take isl_space
*dim
)
1460 domain
= isl_space_domain(isl_space_copy(dim
));
1461 return FN(PW
,reset_space_and_domain
)(pw
, dim
, domain
);
1464 __isl_give PW
*FN(PW
,set_tuple_id
)(__isl_take PW
*pw
, enum isl_dim_type type
,
1465 __isl_take isl_id
*id
)
1469 pw
= FN(PW
,cow
)(pw
);
1473 space
= FN(PW
,get_space
)(pw
);
1474 space
= isl_space_set_tuple_id(space
, type
, id
);
1476 return FN(PW
,reset_space
)(pw
, space
);
1479 return FN(PW
,free
)(pw
);
1482 /* Drop the id on the specified tuple.
1484 __isl_give PW
*FN(PW
,reset_tuple_id
)(__isl_take PW
*pw
, enum isl_dim_type type
)
1490 if (!FN(PW
,has_tuple_id
)(pw
, type
))
1493 pw
= FN(PW
,cow
)(pw
);
1497 space
= FN(PW
,get_space
)(pw
);
1498 space
= isl_space_reset_tuple_id(space
, type
);
1500 return FN(PW
,reset_space
)(pw
, space
);
1503 __isl_give PW
*FN(PW
,set_dim_id
)(__isl_take PW
*pw
,
1504 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1506 pw
= FN(PW
,cow
)(pw
);
1509 pw
->dim
= isl_space_set_dim_id(pw
->dim
, type
, pos
, id
);
1510 return FN(PW
,reset_space
)(pw
, isl_space_copy(pw
->dim
));
1513 return FN(PW
,free
)(pw
);
1517 int FN(PW
,has_equal_space
)(__isl_keep PW
*pw1
, __isl_keep PW
*pw2
)
1522 return isl_space_is_equal(pw1
->dim
, pw2
->dim
);
1526 __isl_give PW
*FN(PW
,morph_domain
)(__isl_take PW
*pw
,
1527 __isl_take isl_morph
*morph
)
1535 ctx
= isl_space_get_ctx(pw
->dim
);
1536 isl_assert(ctx
, isl_space_is_domain_internal(morph
->dom
->dim
, pw
->dim
),
1539 pw
= FN(PW
,cow
)(pw
);
1542 pw
->dim
= isl_space_extend_domain_with_range(
1543 isl_space_copy(morph
->ran
->dim
), pw
->dim
);
1547 for (i
= 0; i
< pw
->n
; ++i
) {
1548 pw
->p
[i
].set
= isl_morph_set(isl_morph_copy(morph
), pw
->p
[i
].set
);
1551 pw
->p
[i
].FIELD
= FN(EL
,morph_domain
)(pw
->p
[i
].FIELD
,
1552 isl_morph_copy(morph
));
1553 if (!pw
->p
[i
].FIELD
)
1557 isl_morph_free(morph
);
1562 isl_morph_free(morph
);
1567 int FN(PW
,n_piece
)(__isl_keep PW
*pw
)
1569 return pw
? pw
->n
: 0;
1572 int FN(PW
,foreach_piece
)(__isl_keep PW
*pw
,
1573 int (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
, void *user
),
1581 for (i
= 0; i
< pw
->n
; ++i
)
1582 if (fn(isl_set_copy(pw
->p
[i
].set
),
1583 FN(EL
,copy
)(pw
->p
[i
].FIELD
), user
) < 0)
1590 static int any_divs(__isl_keep isl_set
*set
)
1597 for (i
= 0; i
< set
->n
; ++i
)
1598 if (set
->p
[i
]->n_div
> 0)
1604 static int foreach_lifted_subset(__isl_take isl_set
*set
, __isl_take EL
*el
,
1605 int (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
,
1606 void *user
), void *user
)
1613 for (i
= 0; i
< set
->n
; ++i
) {
1617 lift
= isl_set_from_basic_set(isl_basic_set_copy(set
->p
[i
]));
1618 lift
= isl_set_lift(lift
);
1620 copy
= FN(EL
,copy
)(el
);
1621 copy
= FN(EL
,lift
)(copy
, isl_set_get_space(lift
));
1623 if (fn(lift
, copy
, user
) < 0)
1637 int FN(PW
,foreach_lifted_piece
)(__isl_keep PW
*pw
,
1638 int (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
,
1639 void *user
), void *user
)
1646 for (i
= 0; i
< pw
->n
; ++i
) {
1650 set
= isl_set_copy(pw
->p
[i
].set
);
1651 el
= FN(EL
,copy
)(pw
->p
[i
].FIELD
);
1652 if (!any_divs(set
)) {
1653 if (fn(set
, el
, user
) < 0)
1657 if (foreach_lifted_subset(set
, el
, fn
, user
) < 0)
1665 #ifndef NO_MOVE_DIMS
1666 __isl_give PW
*FN(PW
,move_dims
)(__isl_take PW
*pw
,
1667 enum isl_dim_type dst_type
, unsigned dst_pos
,
1668 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1672 pw
= FN(PW
,cow
)(pw
);
1676 pw
->dim
= isl_space_move_dims(pw
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
1680 for (i
= 0; i
< pw
->n
; ++i
) {
1681 pw
->p
[i
].FIELD
= FN(EL
,move_dims
)(pw
->p
[i
].FIELD
,
1682 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1683 if (!pw
->p
[i
].FIELD
)
1687 if (dst_type
== isl_dim_in
)
1688 dst_type
= isl_dim_set
;
1689 if (src_type
== isl_dim_in
)
1690 src_type
= isl_dim_set
;
1692 for (i
= 0; i
< pw
->n
; ++i
) {
1693 pw
->p
[i
].set
= isl_set_move_dims(pw
->p
[i
].set
,
1695 src_type
, src_pos
, n
);
1707 __isl_give PW
*FN(PW
,mul_isl_int
)(__isl_take PW
*pw
, isl_int v
)
1711 if (isl_int_is_one(v
))
1713 if (pw
&& DEFAULT_IS_ZERO
&& isl_int_is_zero(v
)) {
1715 isl_space
*dim
= FN(PW
,get_space
)(pw
);
1717 zero
= FN(PW
,ZERO
)(dim
, pw
->type
);
1719 zero
= FN(PW
,ZERO
)(dim
);
1724 pw
= FN(PW
,cow
)(pw
);
1731 if (isl_int_is_neg(v
))
1732 pw
->type
= isl_fold_type_negate(pw
->type
);
1734 for (i
= 0; i
< pw
->n
; ++i
) {
1735 pw
->p
[i
].FIELD
= FN(EL
,scale
)(pw
->p
[i
].FIELD
, v
);
1736 if (!pw
->p
[i
].FIELD
)
1746 /* Multiply the pieces of "pw" by "v" and return the result.
1748 __isl_give PW
*FN(PW
,scale_val
)(__isl_take PW
*pw
, __isl_take isl_val
*v
)
1755 if (isl_val_is_one(v
)) {
1759 if (pw
&& DEFAULT_IS_ZERO
&& isl_val_is_zero(v
)) {
1761 isl_space
*space
= FN(PW
,get_space
)(pw
);
1763 zero
= FN(PW
,ZERO
)(space
, pw
->type
);
1765 zero
= FN(PW
,ZERO
)(space
);
1775 pw
= FN(PW
,cow
)(pw
);
1780 if (isl_val_is_neg(v
))
1781 pw
->type
= isl_fold_type_negate(pw
->type
);
1783 for (i
= 0; i
< pw
->n
; ++i
) {
1784 pw
->p
[i
].FIELD
= FN(EL
,scale_val
)(pw
->p
[i
].FIELD
,
1786 if (!pw
->p
[i
].FIELD
)
1798 /* Divide the pieces of "pw" by "v" and return the result.
1800 __isl_give PW
*FN(PW
,scale_down_val
)(__isl_take PW
*pw
, __isl_take isl_val
*v
)
1807 if (isl_val_is_one(v
)) {
1812 if (!isl_val_is_rat(v
))
1813 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1814 "expecting rational factor", goto error
);
1815 if (isl_val_is_zero(v
))
1816 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1817 "cannot scale down by zero", goto error
);
1823 pw
= FN(PW
,cow
)(pw
);
1828 if (isl_val_is_neg(v
))
1829 pw
->type
= isl_fold_type_negate(pw
->type
);
1831 for (i
= 0; i
< pw
->n
; ++i
) {
1832 pw
->p
[i
].FIELD
= FN(EL
,scale_down_val
)(pw
->p
[i
].FIELD
,
1834 if (!pw
->p
[i
].FIELD
)
1846 __isl_give PW
*FN(PW
,scale
)(__isl_take PW
*pw
, isl_int v
)
1848 return FN(PW
,mul_isl_int
)(pw
, v
);
1851 static int FN(PW
,qsort_set_cmp
)(const void *p1
, const void *p2
)
1853 isl_set
*set1
= *(isl_set
* const *)p1
;
1854 isl_set
*set2
= *(isl_set
* const *)p2
;
1856 return isl_set_plain_cmp(set1
, set2
);
1859 /* We normalize in place, but if anything goes wrong we need
1860 * to return NULL, so we need to make sure we don't change the
1861 * meaning of any possible other copies of map.
1863 __isl_give PW
*FN(PW
,normalize
)(__isl_take PW
*pw
)
1870 for (i
= 0; i
< pw
->n
; ++i
) {
1871 set
= isl_set_normalize(isl_set_copy(pw
->p
[i
].set
));
1873 return FN(PW
,free
)(pw
);
1874 isl_set_free(pw
->p
[i
].set
);
1877 qsort(pw
->p
, pw
->n
, sizeof(pw
->p
[0]), &FN(PW
,qsort_set_cmp
));
1878 for (i
= pw
->n
- 1; i
>= 1; --i
) {
1879 if (!isl_set_plain_is_equal(pw
->p
[i
- 1].set
, pw
->p
[i
].set
))
1881 if (!FN(EL
,plain_is_equal
)(pw
->p
[i
- 1].FIELD
, pw
->p
[i
].FIELD
))
1883 set
= isl_set_union(isl_set_copy(pw
->p
[i
- 1].set
),
1884 isl_set_copy(pw
->p
[i
].set
));
1886 return FN(PW
,free
)(pw
);
1887 isl_set_free(pw
->p
[i
].set
);
1888 FN(EL
,free
)(pw
->p
[i
].FIELD
);
1889 isl_set_free(pw
->p
[i
- 1].set
);
1890 pw
->p
[i
- 1].set
= set
;
1891 for (j
= i
+ 1; j
< pw
->n
; ++j
)
1892 pw
->p
[j
- 1] = pw
->p
[j
];
1899 /* Is pw1 obviously equal to pw2?
1900 * That is, do they have obviously identical cells and obviously identical
1901 * elements on each cell?
1903 int FN(PW
,plain_is_equal
)(__isl_keep PW
*pw1
, __isl_keep PW
*pw2
)
1913 if (!isl_space_is_equal(pw1
->dim
, pw2
->dim
))
1916 pw1
= FN(PW
,copy
)(pw1
);
1917 pw2
= FN(PW
,copy
)(pw2
);
1918 pw1
= FN(PW
,normalize
)(pw1
);
1919 pw2
= FN(PW
,normalize
)(pw2
);
1923 equal
= pw1
->n
== pw2
->n
;
1924 for (i
= 0; equal
&& i
< pw1
->n
; ++i
) {
1925 equal
= isl_set_plain_is_equal(pw1
->p
[i
].set
, pw2
->p
[i
].set
);
1930 equal
= FN(EL
,plain_is_equal
)(pw1
->p
[i
].FIELD
, pw2
->p
[i
].FIELD
);
1945 static __isl_give PW
*FN(PW
,align_params_pw_multi_aff_and
)(__isl_take PW
*pw
,
1946 __isl_take isl_multi_aff
*ma
,
1947 __isl_give PW
*(*fn
)(__isl_take PW
*pw
, __isl_take isl_multi_aff
*ma
))
1950 isl_space
*ma_space
;
1952 ma_space
= isl_multi_aff_get_space(ma
);
1953 if (!pw
|| !ma
|| !ma_space
)
1955 if (isl_space_match(pw
->dim
, isl_dim_param
, ma_space
, isl_dim_param
)) {
1956 isl_space_free(ma_space
);
1959 ctx
= FN(PW
,get_ctx
)(pw
);
1960 if (!isl_space_has_named_params(pw
->dim
) ||
1961 !isl_space_has_named_params(ma_space
))
1962 isl_die(ctx
, isl_error_invalid
,
1963 "unaligned unnamed parameters", goto error
);
1964 pw
= FN(PW
,align_params
)(pw
, ma_space
);
1965 ma
= isl_multi_aff_align_params(ma
, FN(PW
,get_space
)(pw
));
1968 isl_space_free(ma_space
);
1970 isl_multi_aff_free(ma
);
1974 static __isl_give PW
*FN(PW
,align_params_pw_pw_multi_aff_and
)(__isl_take PW
*pw
,
1975 __isl_take isl_pw_multi_aff
*pma
,
1976 __isl_give PW
*(*fn
)(__isl_take PW
*pw
,
1977 __isl_take isl_pw_multi_aff
*ma
))
1980 isl_space
*pma_space
;
1982 pma_space
= isl_pw_multi_aff_get_space(pma
);
1983 if (!pw
|| !pma
|| !pma_space
)
1985 if (isl_space_match(pw
->dim
, isl_dim_param
, pma_space
, isl_dim_param
)) {
1986 isl_space_free(pma_space
);
1989 ctx
= FN(PW
,get_ctx
)(pw
);
1990 if (!isl_space_has_named_params(pw
->dim
) ||
1991 !isl_space_has_named_params(pma_space
))
1992 isl_die(ctx
, isl_error_invalid
,
1993 "unaligned unnamed parameters", goto error
);
1994 pw
= FN(PW
,align_params
)(pw
, pma_space
);
1995 pma
= isl_pw_multi_aff_align_params(pma
, FN(PW
,get_space
)(pw
));
1998 isl_space_free(pma_space
);
2000 isl_pw_multi_aff_free(pma
);
2004 /* Compute the pullback of "pw" by the function represented by "ma".
2005 * In other words, plug in "ma" in "pw".
2007 static __isl_give PW
*FN(PW
,pullback_multi_aff_aligned
)(__isl_take PW
*pw
,
2008 __isl_take isl_multi_aff
*ma
)
2011 isl_space
*space
= NULL
;
2013 ma
= isl_multi_aff_align_divs(ma
);
2014 pw
= FN(PW
,cow
)(pw
);
2018 space
= isl_space_join(isl_multi_aff_get_space(ma
),
2019 FN(PW
,get_space
)(pw
));
2021 for (i
= 0; i
< pw
->n
; ++i
) {
2022 pw
->p
[i
].set
= isl_set_preimage_multi_aff(pw
->p
[i
].set
,
2023 isl_multi_aff_copy(ma
));
2026 pw
->p
[i
].FIELD
= FN(EL
,pullback_multi_aff
)(pw
->p
[i
].FIELD
,
2027 isl_multi_aff_copy(ma
));
2028 if (!pw
->p
[i
].FIELD
)
2032 pw
= FN(PW
,reset_space
)(pw
, space
);
2033 isl_multi_aff_free(ma
);
2036 isl_space_free(space
);
2037 isl_multi_aff_free(ma
);
2042 __isl_give PW
*FN(PW
,pullback_multi_aff
)(__isl_take PW
*pw
,
2043 __isl_take isl_multi_aff
*ma
)
2045 return FN(PW
,align_params_pw_multi_aff_and
)(pw
, ma
,
2046 &FN(PW
,pullback_multi_aff_aligned
));
2049 /* Compute the pullback of "pw" by the function represented by "pma".
2050 * In other words, plug in "pma" in "pw".
2052 static __isl_give PW
*FN(PW
,pullback_pw_multi_aff_aligned
)(__isl_take PW
*pw
,
2053 __isl_take isl_pw_multi_aff
*pma
)
2063 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
2064 FN(PW
,get_space
)(pw
));
2065 isl_pw_multi_aff_free(pma
);
2066 res
= FN(PW
,empty
)(space
);
2071 res
= FN(PW
,pullback_multi_aff
)(FN(PW
,copy
)(pw
),
2072 isl_multi_aff_copy(pma
->p
[0].maff
));
2073 res
= FN(PW
,intersect_domain
)(res
, isl_set_copy(pma
->p
[0].set
));
2075 for (i
= 1; i
< pma
->n
; ++i
) {
2078 res_i
= FN(PW
,pullback_multi_aff
)(FN(PW
,copy
)(pw
),
2079 isl_multi_aff_copy(pma
->p
[i
].maff
));
2080 res_i
= FN(PW
,intersect_domain
)(res_i
,
2081 isl_set_copy(pma
->p
[i
].set
));
2082 res
= FN(PW
,add_disjoint
)(res
, res_i
);
2085 isl_pw_multi_aff_free(pma
);
2089 isl_pw_multi_aff_free(pma
);
2094 __isl_give PW
*FN(PW
,pullback_pw_multi_aff
)(__isl_take PW
*pw
,
2095 __isl_take isl_pw_multi_aff
*pma
)
2097 return FN(PW
,align_params_pw_pw_multi_aff_and
)(pw
, pma
,
2098 &FN(PW
,pullback_pw_multi_aff_aligned
));