1 /*
2  * Copyright 2011      Sven Verdoolaege
3  * Copyright 2012-2013 Ecole Normale Superieure
4  *
5  * Use of this software is governed by the MIT license
6  *
7  * Written by Sven Verdoolaege,
8  * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
9  */
10 
11 #include <isl_multi_macro.h>
12 
13 /* Does the space of "domain" correspond to that of the domain of "multi"?
14  * The parameters do not need to be aligned.
15  */
FN(MULTI (BASE),compatible_domain)16 static isl_bool FN(MULTI(BASE),compatible_domain)(
17 	__isl_keep MULTI(BASE) *multi, __isl_keep DOM *domain)
18 {
19 	isl_bool ok;
20 	isl_space *space, *domain_space;
21 
22 	domain_space = FN(DOM,get_space)(domain);
23 	space = FN(MULTI(BASE),get_space)(multi);
24 	ok = isl_space_has_domain_tuples(domain_space, space);
25 	isl_space_free(space);
26 	isl_space_free(domain_space);
27 
28 	return ok;
29 }
30 
31 /* Check that the space of "domain" corresponds to
32  * that of the domain of "multi", ignoring parameters.
33  */
FN(MULTI (BASE),check_compatible_domain)34 static isl_stat FN(MULTI(BASE),check_compatible_domain)(
35 	__isl_keep MULTI(BASE) *multi, __isl_keep DOM *domain)
36 {
37 	isl_bool ok;
38 
39 	ok = FN(MULTI(BASE),compatible_domain)(multi, domain);
40 	if (ok < 0)
41 		return isl_stat_error;
42 	if (!ok)
43 		isl_die(FN(DOM,get_ctx)(domain), isl_error_invalid,
44 			"incompatible spaces", return isl_stat_error);
45 
46 	return isl_stat_ok;
47 }
48 
49 /* Intersect the explicit domain of "multi" with "domain".
50  *
51  * The parameters of "multi" and "domain" are assumed to have been aligned.
52  *
53  * In the case of an isl_multi_union_pw_aff object, the explicit domain
54  * is allowed to have only constraints on the parameters, while
55  * "domain" contains actual domain elements.  In this case,
56  * "domain" is intersected with those parameter constraints and
57  * then used as the explicit domain of "multi".
58  */
MULTI(BASE)59 static __isl_give MULTI(BASE) *FN(MULTI(BASE),domain_intersect_aligned)(
60 	__isl_take MULTI(BASE) *multi, __isl_take DOM *domain)
61 {
62 	isl_bool is_params;
63 	DOM *multi_dom;
64 
65 	if (FN(MULTI(BASE),check_compatible_domain)(multi, domain) < 0)
66 		goto error;
67 	if (FN(MULTI(BASE),check_has_explicit_domain)(multi) < 0)
68 		goto error;
69 	is_params = FN(DOM,is_params)(multi->u.dom);
70 	if (is_params < 0)
71 		goto error;
72 	multi_dom = FN(MULTI(BASE),get_explicit_domain)(multi);
73 	if (!is_params) {
74 		domain = FN(DOM,intersect)(multi_dom, domain);
75 	} else {
76 		isl_set *params;
77 
78 		params = FN(DOM,params)(multi_dom);
79 		domain = FN(DOM,intersect_params)(domain, params);
80 	}
81 	multi = FN(MULTI(BASE),set_explicit_domain)(multi, domain);
82 	return multi;
83 error:
84 	FN(MULTI(BASE),free)(multi);
85 	FN(DOM,free)(domain);
86 	return NULL;
87 }
88 
89 /* Intersect the explicit domain of "multi" with "domain".
90  * First align the parameters, if needed.
91  */
MULTI(BASE)92 static __isl_give MULTI(BASE) *FN(MULTI(BASE),domain_intersect)(
93 	__isl_take MULTI(BASE) *multi, __isl_take DOM *domain)
94 {
95 	return FN(FN(MULTI(BASE),align_params),DOMBASE)(multi, domain,
96 				    FN(MULTI(BASE),domain_intersect_aligned));
97 }
98 
99 /* Intersect the domain of "multi" with "domain".
100  *
101  * If "multi" has an explicit domain, then only this domain
102  * needs to be intersected.
103  */
MULTI(BASE)104 __isl_give MULTI(BASE) *FN(MULTI(BASE),intersect_domain)(
105 	__isl_take MULTI(BASE) *multi, __isl_take DOM *domain)
106 {
107 	if (FN(MULTI(BASE),has_explicit_domain)(multi))
108 		return FN(MULTI(BASE),domain_intersect)(multi, domain);
109 	return FN(FN(MULTI(BASE),apply),DOMBASE)(multi, domain,
110 					&FN(EL,intersect_domain));
111 }
112 
113 /* Intersect the parameter domain of the explicit domain of "multi"
114  * with "domain".
115  */
MULTI(BASE)116 static __isl_give MULTI(BASE) *FN(MULTI(BASE),domain_intersect_params_aligned)(
117 	__isl_take MULTI(BASE) *multi, __isl_take isl_set *domain)
118 {
119 	DOM *multi_dom;
120 
121 	multi_dom = FN(MULTI(BASE),get_explicit_domain)(multi);
122 	multi_dom = FN(DOM,intersect_params)(multi_dom, domain);
123 	multi = FN(MULTI(BASE),set_explicit_domain)(multi, multi_dom);
124 
125 	return multi;
126 }
127 
128 /* Intersect the parameter domain of the explicit domain of "multi"
129  * with "domain".
130  * First align the parameters, if needed.
131  */
MULTI(BASE)132 static __isl_give MULTI(BASE) *FN(MULTI(BASE),domain_intersect_params)(
133 	__isl_take MULTI(BASE) *multi, __isl_take isl_set *domain)
134 {
135 	return FN(FN(MULTI(BASE),align_params),set)(multi, domain,
136 			    FN(MULTI(BASE),domain_intersect_params_aligned));
137 }
138 
139 /* Intersect the parameter domain of "multi" with "domain".
140  *
141  * If "multi" has an explicit domain, then only this domain
142  * needs to be intersected.
143  */
MULTI(BASE)144 __isl_give MULTI(BASE) *FN(MULTI(BASE),intersect_params)(
145 	__isl_take MULTI(BASE) *multi, __isl_take isl_set *domain)
146 {
147 	if (FN(MULTI(BASE),has_explicit_domain)(multi))
148 		return FN(MULTI(BASE),domain_intersect_params)(multi, domain);
149 	return FN(MULTI(BASE),apply_set)(multi, domain,
150 					&FN(EL,intersect_params));
151 }
152