remove PetScan::try_extract_affine_condition
[pet.git] / aff.c
blobb019174c0a5040dfd385b3fb321ecb46d7a7ee00
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
32 * Leiden University.
35 #include "aff.h"
37 /* If the isl_pw_aff on which isl_pw_aff_foreach_piece is called
38 * has a constant expression on its only domain, then replace
39 * the isl_val in *user by this constant.
40 * The caller is assumed to have checked that this function will
41 * be called exactly once.
43 static int extract_cst(__isl_take isl_set *set, __isl_take isl_aff *aff,
44 void *user)
46 isl_val **inc = (isl_val **)user;
48 if (isl_aff_is_cst(aff)) {
49 isl_val_free(*inc);
50 *inc = isl_aff_get_constant_val(aff);
53 isl_set_free(set);
54 isl_aff_free(aff);
56 return 0;
59 /* If "pa" represents a constant value over a single domain,
60 * then return this constant.
61 * Otherwise return NaN.
63 __isl_give isl_val *pet_extract_cst(__isl_keep isl_pw_aff *pa)
65 isl_val *v;
67 if (!pa)
68 return NULL;
69 v = isl_val_nan(isl_pw_aff_get_ctx(pa));
70 if (isl_pw_aff_n_piece(pa) != 1)
71 return v;
72 if (isl_pw_aff_foreach_piece(pa, &extract_cst, &v) < 0)
73 v = isl_val_free(v);
74 return v;
77 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
79 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
80 __isl_take isl_set *dom)
82 isl_pw_aff *pa;
83 pa = isl_set_indicator_function(set);
84 pa = isl_pw_aff_intersect_domain(pa, isl_set_coalesce(dom));
85 return pa;
88 /* Return "lhs && rhs", defined on the shared definition domain.
90 __isl_give isl_pw_aff *pet_and(__isl_take isl_pw_aff *lhs,
91 __isl_take isl_pw_aff *rhs)
93 isl_set *cond;
94 isl_set *dom;
96 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
97 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
98 cond = isl_set_intersect(isl_pw_aff_non_zero_set(lhs),
99 isl_pw_aff_non_zero_set(rhs));
100 return indicator_function(cond, dom);
103 /* Return "!pa", defined on the domain of "pa".
105 * If "pa" involves any NaN, then return NaN.
107 __isl_give isl_pw_aff *pet_not(__isl_take isl_pw_aff *pa)
109 isl_set *cond, *dom;
111 if (!pa)
112 return NULL;
113 if (isl_pw_aff_involves_nan(pa)) {
114 isl_space *space = isl_pw_aff_get_domain_space(pa);
115 isl_local_space *ls = isl_local_space_from_space(space);
116 isl_pw_aff_free(pa);
117 return isl_pw_aff_nan_on_domain(ls);
120 dom = isl_pw_aff_domain(isl_pw_aff_copy(pa));
121 cond = isl_pw_aff_zero_set(pa);
122 pa = indicator_function(cond, dom);
124 return pa;
127 /* Return "!!pa", i.e., a function that is equal to 1 when "pa"
128 * is non-zero and equal to 0 when "pa" is equal to zero,
129 * on the domain of "pa".
131 * If "pa" involves any NaN, then return NaN.
133 __isl_give isl_pw_aff *pet_to_bool(__isl_take isl_pw_aff *pa)
135 isl_set *cond, *dom;
137 if (!pa)
138 return NULL;
139 if (isl_pw_aff_involves_nan(pa)) {
140 isl_space *space = isl_pw_aff_get_domain_space(pa);
141 isl_local_space *ls = isl_local_space_from_space(space);
142 isl_pw_aff_free(pa);
143 return isl_pw_aff_nan_on_domain(ls);
146 dom = isl_pw_aff_domain(isl_pw_aff_copy(pa));
147 cond = isl_pw_aff_non_zero_set(pa);
148 pa = indicator_function(cond, dom);
150 return pa;
153 /* Return the result of applying the comparison operator "type"
154 * to "pa1" and "pa2".
156 * In particular, construct an isl_pw_aff that is equal to 1
157 * on the subset of the shared domain of "pa1" and "pa2" where
158 * the comparison holds and 0 on the other part of the shared domain.
160 * If "pa1" or "pa2" involve any NaN, then return NaN.
162 __isl_give isl_pw_aff *pet_comparison(enum pet_op_type type,
163 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2)
165 isl_set *dom;
166 isl_set *cond;
167 isl_pw_aff *res;
169 if (!pa1 || !pa2)
170 goto error;
171 if (isl_pw_aff_involves_nan(pa1) || isl_pw_aff_involves_nan(pa2)) {
172 isl_space *space = isl_pw_aff_get_domain_space(pa1);
173 isl_local_space *ls = isl_local_space_from_space(space);
174 isl_pw_aff_free(pa1);
175 isl_pw_aff_free(pa2);
176 return isl_pw_aff_nan_on_domain(ls);
179 dom = isl_pw_aff_domain(isl_pw_aff_copy(pa1));
180 dom = isl_set_intersect(dom, isl_pw_aff_domain(isl_pw_aff_copy(pa2)));
182 switch (type) {
183 case pet_op_lt:
184 cond = isl_pw_aff_lt_set(pa1, pa2);
185 break;
186 case pet_op_le:
187 cond = isl_pw_aff_le_set(pa1, pa2);
188 break;
189 case pet_op_gt:
190 cond = isl_pw_aff_gt_set(pa1, pa2);
191 break;
192 case pet_op_ge:
193 cond = isl_pw_aff_ge_set(pa1, pa2);
194 break;
195 case pet_op_eq:
196 cond = isl_pw_aff_eq_set(pa1, pa2);
197 break;
198 case pet_op_ne:
199 cond = isl_pw_aff_ne_set(pa1, pa2);
200 break;
201 default:
202 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_internal,
203 "not a comparison operator", cond = NULL);
204 isl_pw_aff_free(pa1);
205 isl_pw_aff_free(pa2);
208 cond = isl_set_coalesce(cond);
209 res = indicator_function(cond, dom);
211 return res;
212 error:
213 isl_pw_aff_free(pa1);
214 isl_pw_aff_free(pa2);
215 return NULL;
218 /* Return "lhs && rhs", with shortcut semantics.
219 * That is, if lhs is false, then the result is defined even if rhs is not.
220 * In practice, we compute lhs ? rhs : lhs.
222 static __isl_give isl_pw_aff *pw_aff_and_then(__isl_take isl_pw_aff *lhs,
223 __isl_take isl_pw_aff *rhs)
225 return isl_pw_aff_cond(isl_pw_aff_copy(lhs), rhs, lhs);
228 /* Return "lhs || rhs", with shortcut semantics.
229 * That is, if lhs is true, then the result is defined even if rhs is not.
230 * In practice, we compute lhs ? lhs : rhs.
232 static __isl_give isl_pw_aff *pw_aff_or_else(__isl_take isl_pw_aff *lhs,
233 __isl_take isl_pw_aff *rhs)
235 return isl_pw_aff_cond(isl_pw_aff_copy(lhs), lhs, rhs);
238 /* Return the result of applying the boolean operator "type"
239 * to "pa1" and "pa2".
241 __isl_give isl_pw_aff *pet_boolean(enum pet_op_type type,
242 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2)
244 isl_ctx *ctx;
246 switch (type) {
247 case pet_op_land:
248 return pw_aff_and_then(pa1, pa2);
249 case pet_op_lor:
250 return pw_aff_or_else(pa1, pa2);
251 default:
252 ctx = isl_pw_aff_get_ctx(pa1);
253 isl_pw_aff_free(pa1);
254 isl_pw_aff_free(pa2);
255 isl_die(ctx, isl_error_internal,
256 "not a boolean operator", return NULL);