isl_multi_*: store pieces in a union
[isl.git] / isl_test.c
blobb7c70b72ef769b39b51c3e14dd2dbd5ca3eb0922
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <assert.h>
19 #include <stdio.h>
20 #include <limits.h>
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl_space_private.h>
25 #include <isl/set.h>
26 #include <isl/flow.h>
27 #include <isl_constraint_private.h>
28 #include <isl/polynomial.h>
29 #include <isl/union_set.h>
30 #include <isl/union_map.h>
31 #include <isl_factorization.h>
32 #include <isl/schedule.h>
33 #include <isl/schedule_node.h>
34 #include <isl_options_private.h>
35 #include <isl_vertices_private.h>
36 #include <isl/ast_build.h>
37 #include <isl/val.h>
38 #include <isl/ilp.h>
39 #include <isl_ast_build_expr.h>
40 #include <isl/options.h>
42 #include "isl_srcdir.c"
44 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
46 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
47 char *filename;
48 int length;
49 char *pattern = "%s/test_inputs/%s.%s";
51 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
52 + strlen(suffix) + 1;
53 filename = isl_alloc_array(ctx, char, length);
55 if (!filename)
56 return NULL;
58 sprintf(filename, pattern, srcdir, name, suffix);
60 return filename;
63 void test_parse_map(isl_ctx *ctx, const char *str)
65 isl_map *map;
67 map = isl_map_read_from_str(ctx, str);
68 assert(map);
69 isl_map_free(map);
72 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
74 isl_map *map, *map2;
75 int equal;
77 map = isl_map_read_from_str(ctx, str);
78 map2 = isl_map_read_from_str(ctx, str2);
79 equal = isl_map_is_equal(map, map2);
80 isl_map_free(map);
81 isl_map_free(map2);
83 if (equal < 0)
84 return -1;
85 if (!equal)
86 isl_die(ctx, isl_error_unknown, "maps not equal",
87 return -1);
89 return 0;
92 void test_parse_pwqp(isl_ctx *ctx, const char *str)
94 isl_pw_qpolynomial *pwqp;
96 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
97 assert(pwqp);
98 isl_pw_qpolynomial_free(pwqp);
101 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
103 isl_pw_aff *pwaff;
105 pwaff = isl_pw_aff_read_from_str(ctx, str);
106 assert(pwaff);
107 isl_pw_aff_free(pwaff);
110 /* Check that we can read an isl_multi_val from "str" without errors.
112 static int test_parse_multi_val(isl_ctx *ctx, const char *str)
114 isl_multi_val *mv;
116 mv = isl_multi_val_read_from_str(ctx, str);
117 isl_multi_val_free(mv);
119 return mv ? 0 : -1;
122 /* Pairs of binary relation representations that should represent
123 * the same binary relations.
125 struct {
126 const char *map1;
127 const char *map2;
128 } parse_map_equal_tests[] = {
129 { "{ [x,y] : [([x/2]+y)/3] >= 1 }",
130 "{ [x, y] : 2y >= 6 - x }" },
131 { "{ [x,y] : x <= min(y, 2*y+3) }",
132 "{ [x,y] : x <= y, 2*y + 3 }" },
133 { "{ [x,y] : x >= min(y, 2*y+3) }",
134 "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }" },
135 { "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }",
136 "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }" },
137 { "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }",
138 "{ [i,j] -> [min(i,j)] }" },
139 { "{ [i,j] : i != j }",
140 "{ [i,j] : i < j or i > j }" },
141 { "{ [i,j] : (i+1)*2 >= j }",
142 "{ [i, j] : j <= 2 + 2i }" },
143 { "{ [i] -> [i > 0 ? 4 : 5] }",
144 "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }" },
145 { "[N=2,M] -> { [i=[(M+N)/4]] }",
146 "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }" },
147 { "{ [x] : x >= 0 }",
148 "{ [x] : x-0 >= 0 }" },
149 { "{ [i] : ((i > 10)) }",
150 "{ [i] : i >= 11 }" },
151 { "{ [i] -> [0] }",
152 "{ [i] -> [0 * i] }" },
153 { "{ [a] -> [b] : (not false) }",
154 "{ [a] -> [b] : true }" },
155 { "{ [i] : i/2 <= 5 }",
156 "{ [i] : i <= 10 }" },
157 { "{Sym=[n] [i] : i <= n }",
158 "[n] -> { [i] : i <= n }" },
159 { "{ [*] }",
160 "{ [a] }" },
161 { "{ [i] : 2*floor(i/2) = i }",
162 "{ [i] : exists a : i = 2 a }" },
163 { "{ [a] -> [b] : a = 5 implies b = 5 }",
164 "{ [a] -> [b] : a != 5 or b = 5 }" },
165 { "{ [a] -> [a - 1 : a > 0] }",
166 "{ [a] -> [a - 1] : a > 0 }" },
167 { "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
168 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }" },
169 { "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
170 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
171 { "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
172 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
173 { "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
174 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
175 { "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
176 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
177 { "{ [a,b] -> [i,j] : a,b << i,j }",
178 "{ [a,b] -> [i,j] : a < i or (a = i and b < j) }" },
179 { "{ [a,b] -> [i,j] : a,b <<= i,j }",
180 "{ [a,b] -> [i,j] : a < i or (a = i and b <= j) }" },
181 { "{ [a,b] -> [i,j] : a,b >> i,j }",
182 "{ [a,b] -> [i,j] : a > i or (a = i and b > j) }" },
183 { "{ [a,b] -> [i,j] : a,b >>= i,j }",
184 "{ [a,b] -> [i,j] : a > i or (a = i and b >= j) }" },
185 { "{ [n] -> [i] : exists (a, b, c: 8b <= i - 32a and "
186 "8b >= -7 + i - 32 a and b >= 0 and b <= 3 and "
187 "8c < n - 32a and i < n and c >= 0 and "
188 "c <= 3 and c >= -4a) }",
189 "{ [n] -> [i] : 0 <= i < n }" },
190 { "{ [x] -> [] : exists (a, b: 0 <= a <= 1 and 0 <= b <= 3 and "
191 "2b <= x - 8a and 2b >= -1 + x - 8a) }",
192 "{ [x] -> [] : 0 <= x <= 15 }" },
193 { "{ [x] -> [x] : }",
194 "{ [x] -> [x] }" },
197 int test_parse(struct isl_ctx *ctx)
199 int i;
200 isl_map *map, *map2;
201 const char *str, *str2;
203 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
204 return -1;
205 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
206 return -1;
207 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
208 return -1;
210 str = "{ [i] -> [-i] }";
211 map = isl_map_read_from_str(ctx, str);
212 assert(map);
213 isl_map_free(map);
215 str = "{ A[i] -> L[([i/3])] }";
216 map = isl_map_read_from_str(ctx, str);
217 assert(map);
218 isl_map_free(map);
220 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
221 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
222 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
224 for (i = 0; i < ARRAY_SIZE(parse_map_equal_tests); ++i) {
225 str = parse_map_equal_tests[i].map1;
226 str2 = parse_map_equal_tests[i].map2;
227 if (test_parse_map_equal(ctx, str, str2) < 0)
228 return -1;
231 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
232 map = isl_map_read_from_str(ctx, str);
233 str = "{ [new, old] -> [o0, o1] : "
234 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
235 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
236 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
237 map2 = isl_map_read_from_str(ctx, str);
238 assert(isl_map_is_equal(map, map2));
239 isl_map_free(map);
240 isl_map_free(map2);
242 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
243 map = isl_map_read_from_str(ctx, str);
244 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
245 map2 = isl_map_read_from_str(ctx, str);
246 assert(isl_map_is_equal(map, map2));
247 isl_map_free(map);
248 isl_map_free(map2);
250 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
251 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
252 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
253 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
254 test_parse_pwaff(ctx, "{ [] -> [(100)] }");
256 return 0;
259 static int test_read(isl_ctx *ctx)
261 char *filename;
262 FILE *input;
263 isl_basic_set *bset1, *bset2;
264 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
265 int equal;
267 filename = get_filename(ctx, "set", "omega");
268 assert(filename);
269 input = fopen(filename, "r");
270 assert(input);
272 bset1 = isl_basic_set_read_from_file(ctx, input);
273 bset2 = isl_basic_set_read_from_str(ctx, str);
275 equal = isl_basic_set_is_equal(bset1, bset2);
277 isl_basic_set_free(bset1);
278 isl_basic_set_free(bset2);
279 free(filename);
281 fclose(input);
283 if (equal < 0)
284 return -1;
285 if (!equal)
286 isl_die(ctx, isl_error_unknown,
287 "read sets not equal", return -1);
289 return 0;
292 static int test_bounded(isl_ctx *ctx)
294 isl_set *set;
295 isl_bool bounded;
297 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
298 bounded = isl_set_is_bounded(set);
299 isl_set_free(set);
301 if (bounded < 0)
302 return -1;
303 if (!bounded)
304 isl_die(ctx, isl_error_unknown,
305 "set not considered bounded", return -1);
307 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
308 bounded = isl_set_is_bounded(set);
309 assert(!bounded);
310 isl_set_free(set);
312 if (bounded < 0)
313 return -1;
314 if (bounded)
315 isl_die(ctx, isl_error_unknown,
316 "set considered bounded", return -1);
318 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
319 bounded = isl_set_is_bounded(set);
320 isl_set_free(set);
322 if (bounded < 0)
323 return -1;
324 if (bounded)
325 isl_die(ctx, isl_error_unknown,
326 "set considered bounded", return -1);
328 return 0;
331 /* Construct the basic set { [i] : 5 <= i <= N } */
332 static int test_construction_1(isl_ctx *ctx)
334 isl_space *dim;
335 isl_local_space *ls;
336 isl_basic_set *bset;
337 isl_constraint *c;
339 dim = isl_space_set_alloc(ctx, 1, 1);
340 bset = isl_basic_set_universe(isl_space_copy(dim));
341 ls = isl_local_space_from_space(dim);
343 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
344 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
345 c = isl_constraint_set_coefficient_si(c, isl_dim_param, 0, 1);
346 bset = isl_basic_set_add_constraint(bset, c);
348 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
349 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
350 c = isl_constraint_set_constant_si(c, -5);
351 bset = isl_basic_set_add_constraint(bset, c);
353 isl_local_space_free(ls);
354 isl_basic_set_free(bset);
356 return 0;
359 /* Construct the basic set { [x] : -100 <= x <= 100 }
360 * using isl_basic_set_{lower,upper}_bound_val and
361 * check that it is equal the same basic set parsed from a string.
363 static int test_construction_2(isl_ctx *ctx)
365 isl_bool equal;
366 isl_val *v;
367 isl_space *space;
368 isl_basic_set *bset1, *bset2;
370 v = isl_val_int_from_si(ctx, 100);
371 space = isl_space_set_alloc(ctx, 0, 1);
372 bset1 = isl_basic_set_universe(space);
373 bset1 = isl_basic_set_upper_bound_val(bset1, isl_dim_set, 0,
374 isl_val_copy(v));
375 bset1 = isl_basic_set_lower_bound_val(bset1, isl_dim_set, 0,
376 isl_val_neg(v));
377 bset2 = isl_basic_set_read_from_str(ctx, "{ [x] : -100 <= x <= 100 }");
378 equal = isl_basic_set_is_equal(bset1, bset2);
379 isl_basic_set_free(bset1);
380 isl_basic_set_free(bset2);
382 if (equal < 0)
383 return -1;
384 if (!equal)
385 isl_die(ctx, isl_error_unknown,
386 "failed construction", return -1);
388 return 0;
391 /* Basic tests for constructing basic sets.
393 static int test_construction(isl_ctx *ctx)
395 if (test_construction_1(ctx) < 0)
396 return -1;
397 if (test_construction_2(ctx) < 0)
398 return -1;
399 return 0;
402 static int test_dim(isl_ctx *ctx)
404 const char *str;
405 isl_map *map1, *map2;
406 int equal;
408 map1 = isl_map_read_from_str(ctx,
409 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
410 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
411 map2 = isl_map_read_from_str(ctx,
412 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
413 equal = isl_map_is_equal(map1, map2);
414 isl_map_free(map2);
416 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
417 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
418 if (equal >= 0 && equal)
419 equal = isl_map_is_equal(map1, map2);
421 isl_map_free(map1);
422 isl_map_free(map2);
424 if (equal < 0)
425 return -1;
426 if (!equal)
427 isl_die(ctx, isl_error_unknown,
428 "unexpected result", return -1);
430 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
431 map1 = isl_map_read_from_str(ctx, str);
432 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
433 map2 = isl_map_read_from_str(ctx, str);
434 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
435 equal = isl_map_is_equal(map1, map2);
436 isl_map_free(map1);
437 isl_map_free(map2);
439 if (equal < 0)
440 return -1;
441 if (!equal)
442 isl_die(ctx, isl_error_unknown,
443 "unexpected result", return -1);
445 return 0;
448 struct {
449 __isl_give isl_val *(*op)(__isl_take isl_val *v);
450 const char *arg;
451 const char *res;
452 } val_un_tests[] = {
453 { &isl_val_neg, "0", "0" },
454 { &isl_val_abs, "0", "0" },
455 { &isl_val_2exp, "0", "1" },
456 { &isl_val_floor, "0", "0" },
457 { &isl_val_ceil, "0", "0" },
458 { &isl_val_neg, "1", "-1" },
459 { &isl_val_neg, "-1", "1" },
460 { &isl_val_neg, "1/2", "-1/2" },
461 { &isl_val_neg, "-1/2", "1/2" },
462 { &isl_val_neg, "infty", "-infty" },
463 { &isl_val_neg, "-infty", "infty" },
464 { &isl_val_neg, "NaN", "NaN" },
465 { &isl_val_abs, "1", "1" },
466 { &isl_val_abs, "-1", "1" },
467 { &isl_val_abs, "1/2", "1/2" },
468 { &isl_val_abs, "-1/2", "1/2" },
469 { &isl_val_abs, "infty", "infty" },
470 { &isl_val_abs, "-infty", "infty" },
471 { &isl_val_abs, "NaN", "NaN" },
472 { &isl_val_floor, "1", "1" },
473 { &isl_val_floor, "-1", "-1" },
474 { &isl_val_floor, "1/2", "0" },
475 { &isl_val_floor, "-1/2", "-1" },
476 { &isl_val_floor, "infty", "infty" },
477 { &isl_val_floor, "-infty", "-infty" },
478 { &isl_val_floor, "NaN", "NaN" },
479 { &isl_val_ceil, "1", "1" },
480 { &isl_val_ceil, "-1", "-1" },
481 { &isl_val_ceil, "1/2", "1" },
482 { &isl_val_ceil, "-1/2", "0" },
483 { &isl_val_ceil, "infty", "infty" },
484 { &isl_val_ceil, "-infty", "-infty" },
485 { &isl_val_ceil, "NaN", "NaN" },
486 { &isl_val_2exp, "-3", "1/8" },
487 { &isl_val_2exp, "-1", "1/2" },
488 { &isl_val_2exp, "1", "2" },
489 { &isl_val_2exp, "2", "4" },
490 { &isl_val_2exp, "3", "8" },
491 { &isl_val_inv, "1", "1" },
492 { &isl_val_inv, "2", "1/2" },
493 { &isl_val_inv, "1/2", "2" },
494 { &isl_val_inv, "-2", "-1/2" },
495 { &isl_val_inv, "-1/2", "-2" },
496 { &isl_val_inv, "0", "NaN" },
497 { &isl_val_inv, "NaN", "NaN" },
498 { &isl_val_inv, "infty", "0" },
499 { &isl_val_inv, "-infty", "0" },
502 /* Perform some basic tests of unary operations on isl_val objects.
504 static int test_un_val(isl_ctx *ctx)
506 int i;
507 isl_val *v, *res;
508 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
509 isl_bool ok, is_nan;
511 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
512 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
513 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
514 fn = val_un_tests[i].op;
515 v = fn(v);
516 is_nan = isl_val_is_nan(res);
517 if (is_nan < 0)
518 ok = isl_bool_error;
519 else if (is_nan)
520 ok = isl_val_is_nan(v);
521 else
522 ok = isl_val_eq(v, res);
523 isl_val_free(v);
524 isl_val_free(res);
525 if (ok < 0)
526 return -1;
527 if (!ok)
528 isl_die(ctx, isl_error_unknown,
529 "unexpected result", return -1);
532 return 0;
535 struct {
536 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
537 __isl_take isl_val *v2);
538 } val_bin_op[] = {
539 ['+'] = { &isl_val_add },
540 ['-'] = { &isl_val_sub },
541 ['*'] = { &isl_val_mul },
542 ['/'] = { &isl_val_div },
543 ['g'] = { &isl_val_gcd },
544 ['m'] = { &isl_val_min },
545 ['M'] = { &isl_val_max },
548 struct {
549 const char *arg1;
550 unsigned char op;
551 const char *arg2;
552 const char *res;
553 } val_bin_tests[] = {
554 { "0", '+', "0", "0" },
555 { "1", '+', "0", "1" },
556 { "1", '+', "1", "2" },
557 { "1", '-', "1", "0" },
558 { "1", '*', "1", "1" },
559 { "1", '/', "1", "1" },
560 { "2", '*', "3", "6" },
561 { "2", '*', "1/2", "1" },
562 { "2", '*', "1/3", "2/3" },
563 { "2/3", '*', "3/5", "2/5" },
564 { "2/3", '*', "7/5", "14/15" },
565 { "2", '/', "1/2", "4" },
566 { "-2", '/', "-1/2", "4" },
567 { "-2", '/', "1/2", "-4" },
568 { "2", '/', "-1/2", "-4" },
569 { "2", '/', "2", "1" },
570 { "2", '/', "3", "2/3" },
571 { "2/3", '/', "5/3", "2/5" },
572 { "2/3", '/', "5/7", "14/15" },
573 { "0", '/', "0", "NaN" },
574 { "42", '/', "0", "NaN" },
575 { "-42", '/', "0", "NaN" },
576 { "infty", '/', "0", "NaN" },
577 { "-infty", '/', "0", "NaN" },
578 { "NaN", '/', "0", "NaN" },
579 { "0", '/', "NaN", "NaN" },
580 { "42", '/', "NaN", "NaN" },
581 { "-42", '/', "NaN", "NaN" },
582 { "infty", '/', "NaN", "NaN" },
583 { "-infty", '/', "NaN", "NaN" },
584 { "NaN", '/', "NaN", "NaN" },
585 { "0", '/', "infty", "0" },
586 { "42", '/', "infty", "0" },
587 { "-42", '/', "infty", "0" },
588 { "infty", '/', "infty", "NaN" },
589 { "-infty", '/', "infty", "NaN" },
590 { "NaN", '/', "infty", "NaN" },
591 { "0", '/', "-infty", "0" },
592 { "42", '/', "-infty", "0" },
593 { "-42", '/', "-infty", "0" },
594 { "infty", '/', "-infty", "NaN" },
595 { "-infty", '/', "-infty", "NaN" },
596 { "NaN", '/', "-infty", "NaN" },
597 { "1", '-', "1/3", "2/3" },
598 { "1/3", '+', "1/2", "5/6" },
599 { "1/2", '+', "1/2", "1" },
600 { "3/4", '-', "1/4", "1/2" },
601 { "1/2", '-', "1/3", "1/6" },
602 { "infty", '+', "42", "infty" },
603 { "infty", '+', "infty", "infty" },
604 { "42", '+', "infty", "infty" },
605 { "infty", '-', "infty", "NaN" },
606 { "infty", '*', "infty", "infty" },
607 { "infty", '*', "-infty", "-infty" },
608 { "-infty", '*', "infty", "-infty" },
609 { "-infty", '*', "-infty", "infty" },
610 { "0", '*', "infty", "NaN" },
611 { "1", '*', "infty", "infty" },
612 { "infty", '*', "0", "NaN" },
613 { "infty", '*', "42", "infty" },
614 { "42", '-', "infty", "-infty" },
615 { "infty", '+', "-infty", "NaN" },
616 { "4", 'g', "6", "2" },
617 { "5", 'g', "6", "1" },
618 { "42", 'm', "3", "3" },
619 { "42", 'M', "3", "42" },
620 { "3", 'm', "42", "3" },
621 { "3", 'M', "42", "42" },
622 { "42", 'm', "infty", "42" },
623 { "42", 'M', "infty", "infty" },
624 { "42", 'm', "-infty", "-infty" },
625 { "42", 'M', "-infty", "42" },
626 { "42", 'm', "NaN", "NaN" },
627 { "42", 'M', "NaN", "NaN" },
628 { "infty", 'm', "-infty", "-infty" },
629 { "infty", 'M', "-infty", "infty" },
632 /* Perform some basic tests of binary operations on isl_val objects.
634 static int test_bin_val(isl_ctx *ctx)
636 int i;
637 isl_val *v1, *v2, *res;
638 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
639 __isl_take isl_val *v2);
640 int ok;
642 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
643 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
644 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
645 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
646 fn = val_bin_op[val_bin_tests[i].op].fn;
647 v1 = fn(v1, v2);
648 if (isl_val_is_nan(res))
649 ok = isl_val_is_nan(v1);
650 else
651 ok = isl_val_eq(v1, res);
652 isl_val_free(v1);
653 isl_val_free(res);
654 if (ok < 0)
655 return -1;
656 if (!ok)
657 isl_die(ctx, isl_error_unknown,
658 "unexpected result", return -1);
661 return 0;
664 /* Perform some basic tests on isl_val objects.
666 static int test_val(isl_ctx *ctx)
668 if (test_un_val(ctx) < 0)
669 return -1;
670 if (test_bin_val(ctx) < 0)
671 return -1;
672 return 0;
675 /* Sets described using existentially quantified variables that
676 * can also be described without.
678 static const char *elimination_tests[] = {
679 "{ [i,j] : 2 * [i/2] + 3 * [j/4] <= 10 and 2 i = j }",
680 "{ [m, w] : exists a : w - 2m - 5 <= 3a <= m - 2w }",
681 "{ [m, w] : exists a : w >= 0 and a < m and -1 + w <= a <= 2m - w }",
684 /* Check that redundant existentially quantified variables are
685 * getting removed.
687 static int test_elimination(isl_ctx *ctx)
689 int i;
690 unsigned n;
691 isl_basic_set *bset;
693 for (i = 0; i < ARRAY_SIZE(elimination_tests); ++i) {
694 bset = isl_basic_set_read_from_str(ctx, elimination_tests[i]);
695 n = isl_basic_set_dim(bset, isl_dim_div);
696 isl_basic_set_free(bset);
697 if (!bset)
698 return -1;
699 if (n != 0)
700 isl_die(ctx, isl_error_unknown,
701 "expecting no existentials", return -1);
704 return 0;
707 static int test_div(isl_ctx *ctx)
709 const char *str;
710 int empty;
711 isl_space *dim;
712 isl_set *set;
713 isl_local_space *ls;
714 struct isl_basic_set *bset;
715 struct isl_constraint *c;
717 /* test 1 */
718 dim = isl_space_set_alloc(ctx, 0, 3);
719 bset = isl_basic_set_universe(isl_space_copy(dim));
720 ls = isl_local_space_from_space(dim);
722 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
723 c = isl_constraint_set_constant_si(c, -1);
724 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
725 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
726 bset = isl_basic_set_add_constraint(bset, c);
728 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
729 c = isl_constraint_set_constant_si(c, 1);
730 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
731 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
732 bset = isl_basic_set_add_constraint(bset, c);
734 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
736 assert(bset && bset->n_div == 1);
737 isl_local_space_free(ls);
738 isl_basic_set_free(bset);
740 /* test 2 */
741 dim = isl_space_set_alloc(ctx, 0, 3);
742 bset = isl_basic_set_universe(isl_space_copy(dim));
743 ls = isl_local_space_from_space(dim);
745 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
746 c = isl_constraint_set_constant_si(c, 1);
747 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
748 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
749 bset = isl_basic_set_add_constraint(bset, c);
751 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
752 c = isl_constraint_set_constant_si(c, -1);
753 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
754 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
755 bset = isl_basic_set_add_constraint(bset, c);
757 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
759 assert(bset && bset->n_div == 1);
760 isl_local_space_free(ls);
761 isl_basic_set_free(bset);
763 /* test 3 */
764 dim = isl_space_set_alloc(ctx, 0, 3);
765 bset = isl_basic_set_universe(isl_space_copy(dim));
766 ls = isl_local_space_from_space(dim);
768 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
769 c = isl_constraint_set_constant_si(c, 1);
770 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
771 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
772 bset = isl_basic_set_add_constraint(bset, c);
774 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
775 c = isl_constraint_set_constant_si(c, -3);
776 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
777 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 4);
778 bset = isl_basic_set_add_constraint(bset, c);
780 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
782 assert(bset && bset->n_div == 1);
783 isl_local_space_free(ls);
784 isl_basic_set_free(bset);
786 /* test 4 */
787 dim = isl_space_set_alloc(ctx, 0, 3);
788 bset = isl_basic_set_universe(isl_space_copy(dim));
789 ls = isl_local_space_from_space(dim);
791 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
792 c = isl_constraint_set_constant_si(c, 2);
793 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
794 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
795 bset = isl_basic_set_add_constraint(bset, c);
797 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
798 c = isl_constraint_set_constant_si(c, -1);
799 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
800 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
801 bset = isl_basic_set_add_constraint(bset, c);
803 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
805 assert(isl_basic_set_is_empty(bset));
806 isl_local_space_free(ls);
807 isl_basic_set_free(bset);
809 /* test 5 */
810 dim = isl_space_set_alloc(ctx, 0, 3);
811 bset = isl_basic_set_universe(isl_space_copy(dim));
812 ls = isl_local_space_from_space(dim);
814 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
815 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
816 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
817 bset = isl_basic_set_add_constraint(bset, c);
819 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
820 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
821 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
822 bset = isl_basic_set_add_constraint(bset, c);
824 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
826 assert(bset && bset->n_div == 0);
827 isl_basic_set_free(bset);
828 isl_local_space_free(ls);
830 /* test 6 */
831 dim = isl_space_set_alloc(ctx, 0, 3);
832 bset = isl_basic_set_universe(isl_space_copy(dim));
833 ls = isl_local_space_from_space(dim);
835 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
836 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
837 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
838 bset = isl_basic_set_add_constraint(bset, c);
840 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
841 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
842 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
843 bset = isl_basic_set_add_constraint(bset, c);
845 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
847 assert(bset && bset->n_div == 1);
848 isl_basic_set_free(bset);
849 isl_local_space_free(ls);
851 /* test 7 */
852 /* This test is a bit tricky. We set up an equality
853 * a + 3b + 3c = 6 e0
854 * Normalization of divs creates _two_ divs
855 * a = 3 e0
856 * c - b - e0 = 2 e1
857 * Afterwards e0 is removed again because it has coefficient -1
858 * and we end up with the original equality and div again.
859 * Perhaps we can avoid the introduction of this temporary div.
861 dim = isl_space_set_alloc(ctx, 0, 4);
862 bset = isl_basic_set_universe(isl_space_copy(dim));
863 ls = isl_local_space_from_space(dim);
865 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
866 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
867 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
868 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -3);
869 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 6);
870 bset = isl_basic_set_add_constraint(bset, c);
872 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
874 /* Test disabled for now */
876 assert(bset && bset->n_div == 1);
878 isl_local_space_free(ls);
879 isl_basic_set_free(bset);
881 /* test 8 */
882 dim = isl_space_set_alloc(ctx, 0, 5);
883 bset = isl_basic_set_universe(isl_space_copy(dim));
884 ls = isl_local_space_from_space(dim);
886 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
887 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
888 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
889 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, -3);
890 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 4, 6);
891 bset = isl_basic_set_add_constraint(bset, c);
893 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
894 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
895 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 1);
896 c = isl_constraint_set_constant_si(c, 1);
897 bset = isl_basic_set_add_constraint(bset, c);
899 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
901 /* Test disabled for now */
903 assert(bset && bset->n_div == 1);
905 isl_local_space_free(ls);
906 isl_basic_set_free(bset);
908 /* test 9 */
909 dim = isl_space_set_alloc(ctx, 0, 4);
910 bset = isl_basic_set_universe(isl_space_copy(dim));
911 ls = isl_local_space_from_space(dim);
913 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
914 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
915 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -1);
916 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
917 bset = isl_basic_set_add_constraint(bset, c);
919 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
920 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
921 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 3);
922 c = isl_constraint_set_constant_si(c, 2);
923 bset = isl_basic_set_add_constraint(bset, c);
925 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
927 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
929 assert(!isl_basic_set_is_empty(bset));
931 isl_local_space_free(ls);
932 isl_basic_set_free(bset);
934 /* test 10 */
935 dim = isl_space_set_alloc(ctx, 0, 3);
936 bset = isl_basic_set_universe(isl_space_copy(dim));
937 ls = isl_local_space_from_space(dim);
939 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
940 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
941 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
942 bset = isl_basic_set_add_constraint(bset, c);
944 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
946 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
948 isl_local_space_free(ls);
949 isl_basic_set_free(bset);
951 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
952 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
953 set = isl_set_read_from_str(ctx, str);
954 set = isl_set_compute_divs(set);
955 isl_set_free(set);
956 if (!set)
957 return -1;
959 if (test_elimination(ctx) < 0)
960 return -1;
962 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
963 set = isl_set_read_from_str(ctx, str);
964 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
965 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
966 empty = isl_set_is_empty(set);
967 isl_set_free(set);
968 if (empty < 0)
969 return -1;
970 if (!empty)
971 isl_die(ctx, isl_error_unknown,
972 "result not as accurate as expected", return -1);
974 return 0;
977 void test_application_case(struct isl_ctx *ctx, const char *name)
979 char *filename;
980 FILE *input;
981 struct isl_basic_set *bset1, *bset2;
982 struct isl_basic_map *bmap;
984 filename = get_filename(ctx, name, "omega");
985 assert(filename);
986 input = fopen(filename, "r");
987 assert(input);
989 bset1 = isl_basic_set_read_from_file(ctx, input);
990 bmap = isl_basic_map_read_from_file(ctx, input);
992 bset1 = isl_basic_set_apply(bset1, bmap);
994 bset2 = isl_basic_set_read_from_file(ctx, input);
996 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
998 isl_basic_set_free(bset1);
999 isl_basic_set_free(bset2);
1000 free(filename);
1002 fclose(input);
1005 static int test_application(isl_ctx *ctx)
1007 test_application_case(ctx, "application");
1008 test_application_case(ctx, "application2");
1010 return 0;
1013 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1015 char *filename;
1016 FILE *input;
1017 struct isl_basic_set *bset1, *bset2;
1019 filename = get_filename(ctx, name, "polylib");
1020 assert(filename);
1021 input = fopen(filename, "r");
1022 assert(input);
1024 bset1 = isl_basic_set_read_from_file(ctx, input);
1025 bset2 = isl_basic_set_read_from_file(ctx, input);
1027 bset1 = isl_basic_set_affine_hull(bset1);
1029 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1031 isl_basic_set_free(bset1);
1032 isl_basic_set_free(bset2);
1033 free(filename);
1035 fclose(input);
1038 int test_affine_hull(struct isl_ctx *ctx)
1040 const char *str;
1041 isl_set *set;
1042 isl_basic_set *bset, *bset2;
1043 int n;
1044 int subset;
1046 test_affine_hull_case(ctx, "affine2");
1047 test_affine_hull_case(ctx, "affine");
1048 test_affine_hull_case(ctx, "affine3");
1050 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1051 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1052 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1053 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1054 set = isl_set_read_from_str(ctx, str);
1055 bset = isl_set_affine_hull(set);
1056 n = isl_basic_set_dim(bset, isl_dim_div);
1057 isl_basic_set_free(bset);
1058 if (n != 0)
1059 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1060 return -1);
1062 /* Check that isl_map_affine_hull is not confused by
1063 * the reordering of divs in isl_map_align_divs.
1065 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1066 "32e0 = b and 32e1 = c); "
1067 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1068 set = isl_set_read_from_str(ctx, str);
1069 bset = isl_set_affine_hull(set);
1070 isl_basic_set_free(bset);
1071 if (!bset)
1072 return -1;
1074 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1075 "32e2 = 31 + 31e0 }";
1076 set = isl_set_read_from_str(ctx, str);
1077 bset = isl_set_affine_hull(set);
1078 str = "{ [a] : exists e : a = 32 e }";
1079 bset2 = isl_basic_set_read_from_str(ctx, str);
1080 subset = isl_basic_set_is_subset(bset, bset2);
1081 isl_basic_set_free(bset);
1082 isl_basic_set_free(bset2);
1083 if (subset < 0)
1084 return -1;
1085 if (!subset)
1086 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1087 return -1);
1089 return 0;
1092 /* Pairs of maps and the corresponding expected results of
1093 * isl_map_plain_unshifted_simple_hull.
1095 struct {
1096 const char *map;
1097 const char *hull;
1098 } plain_unshifted_simple_hull_tests[] = {
1099 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1100 "{ [i] -> [j] : i >= 1 }" },
1101 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1102 "(j mod 4 = 2 and k mod 6 = n) }",
1103 "{ [n] -> [i,j,k] : j mod 4 = 2 }" },
1106 /* Basic tests for isl_map_plain_unshifted_simple_hull.
1108 static int test_plain_unshifted_simple_hull(isl_ctx *ctx)
1110 int i;
1111 isl_map *map;
1112 isl_basic_map *hull, *expected;
1113 isl_bool equal;
1115 for (i = 0; i < ARRAY_SIZE(plain_unshifted_simple_hull_tests); ++i) {
1116 const char *str;
1117 str = plain_unshifted_simple_hull_tests[i].map;
1118 map = isl_map_read_from_str(ctx, str);
1119 str = plain_unshifted_simple_hull_tests[i].hull;
1120 expected = isl_basic_map_read_from_str(ctx, str);
1121 hull = isl_map_plain_unshifted_simple_hull(map);
1122 equal = isl_basic_map_is_equal(hull, expected);
1123 isl_basic_map_free(hull);
1124 isl_basic_map_free(expected);
1125 if (equal < 0)
1126 return -1;
1127 if (!equal)
1128 isl_die(ctx, isl_error_unknown, "unexpected hull",
1129 return -1);
1132 return 0;
1135 /* Pairs of sets and the corresponding expected results of
1136 * isl_set_unshifted_simple_hull.
1138 struct {
1139 const char *set;
1140 const char *hull;
1141 } unshifted_simple_hull_tests[] = {
1142 { "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1143 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1146 /* Basic tests for isl_set_unshifted_simple_hull.
1148 static int test_unshifted_simple_hull(isl_ctx *ctx)
1150 int i;
1151 isl_set *set;
1152 isl_basic_set *hull, *expected;
1153 isl_bool equal;
1155 for (i = 0; i < ARRAY_SIZE(unshifted_simple_hull_tests); ++i) {
1156 const char *str;
1157 str = unshifted_simple_hull_tests[i].set;
1158 set = isl_set_read_from_str(ctx, str);
1159 str = unshifted_simple_hull_tests[i].hull;
1160 expected = isl_basic_set_read_from_str(ctx, str);
1161 hull = isl_set_unshifted_simple_hull(set);
1162 equal = isl_basic_set_is_equal(hull, expected);
1163 isl_basic_set_free(hull);
1164 isl_basic_set_free(expected);
1165 if (equal < 0)
1166 return -1;
1167 if (!equal)
1168 isl_die(ctx, isl_error_unknown, "unexpected hull",
1169 return -1);
1172 return 0;
1175 static int test_simple_hull(struct isl_ctx *ctx)
1177 const char *str;
1178 isl_set *set;
1179 isl_basic_set *bset;
1180 isl_bool is_empty;
1182 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1183 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1184 set = isl_set_read_from_str(ctx, str);
1185 bset = isl_set_simple_hull(set);
1186 is_empty = isl_basic_set_is_empty(bset);
1187 isl_basic_set_free(bset);
1189 if (is_empty == isl_bool_error)
1190 return -1;
1192 if (is_empty == isl_bool_false)
1193 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1194 return -1);
1196 if (test_plain_unshifted_simple_hull(ctx) < 0)
1197 return -1;
1198 if (test_unshifted_simple_hull(ctx) < 0)
1199 return -1;
1201 return 0;
1204 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1206 char *filename;
1207 FILE *input;
1208 struct isl_basic_set *bset1, *bset2;
1209 struct isl_set *set;
1211 filename = get_filename(ctx, name, "polylib");
1212 assert(filename);
1213 input = fopen(filename, "r");
1214 assert(input);
1216 bset1 = isl_basic_set_read_from_file(ctx, input);
1217 bset2 = isl_basic_set_read_from_file(ctx, input);
1219 set = isl_basic_set_union(bset1, bset2);
1220 bset1 = isl_set_convex_hull(set);
1222 bset2 = isl_basic_set_read_from_file(ctx, input);
1224 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1226 isl_basic_set_free(bset1);
1227 isl_basic_set_free(bset2);
1228 free(filename);
1230 fclose(input);
1233 struct {
1234 const char *set;
1235 const char *hull;
1236 } convex_hull_tests[] = {
1237 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1238 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1239 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1240 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1241 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1242 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1243 "i2 <= 5 and i2 >= 4; "
1244 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1245 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1246 "i2 <= 5 + i0 and i2 >= i0 }" },
1247 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1248 "{ [x, y] : 1 = 0 }" },
1249 { "{ [x, y, z] : 0 <= x, y, z <= 10; [x, y, 0] : x >= 0 and y > 0; "
1250 "[x, y, 0] : x >= 0 and y < 0 }",
1251 "{ [x, y, z] : x >= 0 and 0 <= z <= 10 }" },
1254 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1256 int i;
1257 int orig_convex = ctx->opt->convex;
1258 ctx->opt->convex = convex;
1260 test_convex_hull_case(ctx, "convex0");
1261 test_convex_hull_case(ctx, "convex1");
1262 test_convex_hull_case(ctx, "convex2");
1263 test_convex_hull_case(ctx, "convex3");
1264 test_convex_hull_case(ctx, "convex4");
1265 test_convex_hull_case(ctx, "convex5");
1266 test_convex_hull_case(ctx, "convex6");
1267 test_convex_hull_case(ctx, "convex7");
1268 test_convex_hull_case(ctx, "convex8");
1269 test_convex_hull_case(ctx, "convex9");
1270 test_convex_hull_case(ctx, "convex10");
1271 test_convex_hull_case(ctx, "convex11");
1272 test_convex_hull_case(ctx, "convex12");
1273 test_convex_hull_case(ctx, "convex13");
1274 test_convex_hull_case(ctx, "convex14");
1275 test_convex_hull_case(ctx, "convex15");
1277 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1278 isl_set *set1, *set2;
1279 int equal;
1281 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1282 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1283 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1284 equal = isl_set_is_equal(set1, set2);
1285 isl_set_free(set1);
1286 isl_set_free(set2);
1288 if (equal < 0)
1289 return -1;
1290 if (!equal)
1291 isl_die(ctx, isl_error_unknown,
1292 "unexpected convex hull", return -1);
1295 ctx->opt->convex = orig_convex;
1297 return 0;
1300 static int test_convex_hull(isl_ctx *ctx)
1302 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1303 return -1;
1304 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1305 return -1;
1306 return 0;
1309 void test_gist_case(struct isl_ctx *ctx, const char *name)
1311 char *filename;
1312 FILE *input;
1313 struct isl_basic_set *bset1, *bset2;
1315 filename = get_filename(ctx, name, "polylib");
1316 assert(filename);
1317 input = fopen(filename, "r");
1318 assert(input);
1320 bset1 = isl_basic_set_read_from_file(ctx, input);
1321 bset2 = isl_basic_set_read_from_file(ctx, input);
1323 bset1 = isl_basic_set_gist(bset1, bset2);
1325 bset2 = isl_basic_set_read_from_file(ctx, input);
1327 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1329 isl_basic_set_free(bset1);
1330 isl_basic_set_free(bset2);
1331 free(filename);
1333 fclose(input);
1336 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1338 struct {
1339 const char *map;
1340 const char *context;
1341 const char *gist;
1342 } plain_gist_tests[] = {
1343 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1344 "{ [i] -> [j] : i >= 1 }",
1345 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1346 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1347 "(j mod 4 = 2 and k mod 6 = n) }",
1348 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1349 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1350 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1351 "{ [i] -> [j] : i > j }",
1352 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1355 /* Basic tests for isl_map_plain_gist_basic_map.
1357 static int test_plain_gist(isl_ctx *ctx)
1359 int i;
1361 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1362 const char *str;
1363 int equal;
1364 isl_map *map, *gist;
1365 isl_basic_map *context;
1367 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1368 str = plain_gist_tests[i].context;
1369 context = isl_basic_map_read_from_str(ctx, str);
1370 map = isl_map_plain_gist_basic_map(map, context);
1371 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1372 equal = isl_map_is_equal(map, gist);
1373 isl_map_free(map);
1374 isl_map_free(gist);
1375 if (equal < 0)
1376 return -1;
1377 if (!equal)
1378 isl_die(ctx, isl_error_unknown,
1379 "incorrect gist result", return -1);
1382 return 0;
1385 struct {
1386 const char *set;
1387 const char *context;
1388 const char *gist;
1389 } gist_tests[] = {
1390 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1391 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1392 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1393 "{ [a, b, c] : a <= 15 }" },
1394 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1395 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1396 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1397 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1398 { "{ [m, n, a, b] : a <= 2147 + n }",
1399 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1400 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1401 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1402 "b >= 0) }",
1403 "{ [m, n, ku, kl] }" },
1404 { "{ [a, a, b] : a >= 10 }",
1405 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1406 "{ [a, a, b] : a >= 10 }" },
1407 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1408 "{ [0, j] : j >= 0 }" },
1409 /* Check that no constraints on i6 are introduced in the gist */
1410 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1411 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1412 "5e0 <= 381 - t1 and i4 <= 1) }",
1413 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1414 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1415 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1416 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1417 "20e0 >= 1511 - 4t1 - 5i4) }" },
1418 /* Check that no constraints on i6 are introduced in the gist */
1419 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1420 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1421 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1422 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1423 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1424 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1425 "20e1 <= 1530 - 4t1 - 5i4 and "
1426 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1427 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1428 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1429 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1430 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1431 "10e0 <= -91 + 5i4 + 4i6 and "
1432 "10e0 >= -105 + 5i4 + 4i6) }",
1433 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1434 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1435 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1436 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1437 "{ [a, b, q, p] : b >= 1 + a }",
1438 "{ [a, b, q, p] : false }" },
1439 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1440 "[n] -> { [x] : x mod 32 = 0 }",
1441 "[n] -> { [x = n] }" },
1442 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1443 "{ [x] : x mod 2 = 0 }" },
1444 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1445 "{ [x] : x mod 128 = 0 }" },
1446 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1447 "{ [x] : x mod 3200 = 0 }" },
1448 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1449 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1450 "{ [a, b, c = a] }" },
1451 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1452 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1453 "{ [a, b, c = a] : a mod 3 = 0 }" },
1454 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1455 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1456 "{ [x] }" },
1457 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1458 "{ [x,y] : 1 <= y <= 3 }",
1459 "{ [x,y] }" },
1462 /* Check that isl_set_gist behaves as expected.
1464 * For the test cases in gist_tests, besides checking that the result
1465 * is as expected, also check that applying the gist operation does
1466 * not modify the input set (an earlier version of isl would do that) and
1467 * that the test case is consistent, i.e., that the gist has the same
1468 * intersection with the context as the input set.
1470 static int test_gist(struct isl_ctx *ctx)
1472 int i;
1473 const char *str;
1474 isl_basic_set *bset1, *bset2;
1475 isl_map *map1, *map2;
1476 int equal;
1478 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1479 int equal_input, equal_intersection;
1480 isl_set *set1, *set2, *copy, *context;
1482 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1483 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1484 copy = isl_set_copy(set1);
1485 set1 = isl_set_gist(set1, isl_set_copy(context));
1486 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1487 equal = isl_set_is_equal(set1, set2);
1488 isl_set_free(set1);
1489 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1490 equal_input = isl_set_is_equal(set1, copy);
1491 isl_set_free(copy);
1492 set1 = isl_set_intersect(set1, isl_set_copy(context));
1493 set2 = isl_set_intersect(set2, context);
1494 equal_intersection = isl_set_is_equal(set1, set2);
1495 isl_set_free(set2);
1496 isl_set_free(set1);
1497 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1498 return -1;
1499 if (!equal)
1500 isl_die(ctx, isl_error_unknown,
1501 "incorrect gist result", return -1);
1502 if (!equal_input)
1503 isl_die(ctx, isl_error_unknown,
1504 "gist modified input", return -1);
1505 if (!equal_input)
1506 isl_die(ctx, isl_error_unknown,
1507 "inconsistent gist test case", return -1);
1510 test_gist_case(ctx, "gist1");
1512 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1513 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1514 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1515 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1516 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1517 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1518 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1519 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1520 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1521 bset1 = isl_basic_set_read_from_str(ctx, str);
1522 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1523 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1524 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1525 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1526 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1527 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1528 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1529 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1530 bset2 = isl_basic_set_read_from_str(ctx, str);
1531 bset1 = isl_basic_set_gist(bset1, bset2);
1532 assert(bset1 && bset1->n_div == 0);
1533 isl_basic_set_free(bset1);
1535 /* Check that the integer divisions of the second disjunct
1536 * do not spread to the first disjunct.
1538 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1539 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1540 "(exists (e0 = [(-1 + t1)/16], "
1541 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1542 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1543 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1544 "o0 <= 4294967295 and t1 <= -1)) }";
1545 map1 = isl_map_read_from_str(ctx, str);
1546 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1547 map2 = isl_map_read_from_str(ctx, str);
1548 map1 = isl_map_gist(map1, map2);
1549 if (!map1)
1550 return -1;
1551 if (map1->n != 1)
1552 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1553 isl_map_free(map1); return -1);
1554 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1555 isl_die(ctx, isl_error_unknown, "expecting single div",
1556 isl_map_free(map1); return -1);
1557 isl_map_free(map1);
1559 if (test_plain_gist(ctx) < 0)
1560 return -1;
1562 return 0;
1565 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1567 isl_set *set, *set2;
1568 int equal;
1569 int one;
1571 set = isl_set_read_from_str(ctx, str);
1572 set = isl_set_coalesce(set);
1573 set2 = isl_set_read_from_str(ctx, str);
1574 equal = isl_set_is_equal(set, set2);
1575 one = set && set->n == 1;
1576 isl_set_free(set);
1577 isl_set_free(set2);
1579 if (equal < 0)
1580 return -1;
1581 if (!equal)
1582 isl_die(ctx, isl_error_unknown,
1583 "coalesced set not equal to input", return -1);
1584 if (check_one && !one)
1585 isl_die(ctx, isl_error_unknown,
1586 "coalesced set should not be a union", return -1);
1588 return 0;
1591 /* Inputs for coalescing tests with unbounded wrapping.
1592 * "str" is a string representation of the input set.
1593 * "single_disjunct" is set if we expect the result to consist of
1594 * a single disjunct.
1596 struct {
1597 int single_disjunct;
1598 const char *str;
1599 } coalesce_unbounded_tests[] = {
1600 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1601 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1602 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1603 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1604 "-10 <= y <= 0}" },
1605 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1606 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1607 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1608 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1611 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1612 * option turned off.
1614 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1616 int i;
1617 int r = 0;
1618 int bounded;
1620 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1621 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1623 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1624 const char *str = coalesce_unbounded_tests[i].str;
1625 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1626 if (test_coalesce_set(ctx, str, check_one) >= 0)
1627 continue;
1628 r = -1;
1629 break;
1632 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1634 return r;
1637 /* Inputs for coalescing tests.
1638 * "str" is a string representation of the input set.
1639 * "single_disjunct" is set if we expect the result to consist of
1640 * a single disjunct.
1642 struct {
1643 int single_disjunct;
1644 const char *str;
1645 } coalesce_tests[] = {
1646 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1647 "y >= x & x >= 2 & 5 >= y }" },
1648 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1649 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1650 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1651 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1652 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1653 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1654 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1655 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1656 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1657 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1658 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1659 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1660 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1661 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1662 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1663 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1664 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1665 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1666 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1667 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1668 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1669 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1670 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1671 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1672 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1673 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1674 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1675 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1676 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1677 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1678 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1679 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1680 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1681 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1682 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1683 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1684 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1685 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1686 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1687 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1688 "[o0, o1, o2, o3, o4, o5, o6]] : "
1689 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1690 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1691 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1692 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1693 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1694 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1695 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1696 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1697 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1698 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1699 "o6 >= i3 + i6 - o3 and M >= 0 and "
1700 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1701 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1702 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1703 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1704 "(o0 = 0 and M >= 2 and N >= 3) or "
1705 "(M = 0 and o0 = 0 and N >= 3) }" },
1706 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1707 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1708 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1709 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1710 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1711 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1712 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1713 "(y = 3 and x = 1) }" },
1714 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1715 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1716 "i1 <= M and i3 <= M and i4 <= M) or "
1717 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1718 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1719 "i4 <= -1 + M) }" },
1720 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1721 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1722 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1723 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1724 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1725 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1726 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1727 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1728 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1729 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1730 { 0, "{ [a, b] : exists e : 2e = a and "
1731 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1732 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1733 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1734 "j >= 1 and j' <= i + j - i' and i >= 1; "
1735 "[1, 1, 1, 1] }" },
1736 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1737 "[i,j] : exists a : j = 3a }" },
1738 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1739 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1740 "a >= 3) or "
1741 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1742 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1743 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1744 "c <= 6 + 8a and a >= 3; "
1745 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1746 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1747 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1748 "[x,0] : 3 <= x <= 4 }" },
1749 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1750 "[x,0] : 4 <= x <= 5 }" },
1751 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1752 "[x,0] : 3 <= x <= 5 }" },
1753 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1754 "[x,0] : 3 <= x <= 4 }" },
1755 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1756 "i1 <= 0; "
1757 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1758 { 1, "{ [0,0]; [1,1] }" },
1759 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1760 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1761 "ii <= k;"
1762 "[k, 0, k] : k <= 6 and k >= 1 }" },
1763 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1764 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1765 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1766 { 1, "[n] -> { [1] : n >= 0;"
1767 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1768 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1769 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1770 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1771 "2e0 <= x and 2e0 <= n);"
1772 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1773 "n >= 0) }" },
1774 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1775 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1776 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1777 "t1 = 1 }" },
1778 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1779 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1780 "[0, 0] }" },
1781 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1782 "t1 >= 13 and t1 <= 16);"
1783 "[t1] : t1 <= 15 and t1 >= 12 }" },
1784 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
1785 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
1786 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
1787 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
1788 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
1789 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
1790 "i <= 4j + 2 }" },
1791 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
1792 "(exists (e0 : 3e0 = -2 + c0)) }" },
1793 { 0, "[n, b0, t0] -> "
1794 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1795 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1796 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1797 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1798 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
1799 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
1800 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
1801 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
1802 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1803 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1804 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1805 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
1806 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
1807 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
1808 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
1809 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
1810 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
1811 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
1812 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
1813 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
1814 { 0, "{ [i0, i1, i2] : "
1815 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
1816 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
1817 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
1818 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
1819 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
1820 "32e0 <= 31 + i0)) or "
1821 "i0 >= 0 }" },
1822 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
1823 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
1824 "2*floor((c)/2) = c and 0 <= a <= 192;"
1825 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
1827 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
1828 "(0 <= a <= b <= n) }" },
1829 { 1, "{ [a, b] : 0 <= a <= 2 and b >= 0 and "
1830 "((0 < b <= 13) or (2*floor((a + b)/2) >= -5 + a + 2b)) }" },
1831 { 1, "{ [a] : (2 <= a <= 5) or (a mod 2 = 1 and 1 <= a <= 5) }" },
1832 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
1833 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1834 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
1835 "b = 3 and 9e0 <= -19 + 2c)) }" },
1836 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
1837 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1838 "(a = 4 and b = 3 and "
1839 "9*floor((-16 + 2c)/9) <= -19 + 2c) }" },
1840 { 0, "{ [a, b, c] : (b <= 2 and b <= -2 + a) or "
1841 "(b = -1 + a and 0 < a <= 3 and "
1842 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1843 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
1844 "b = 3 and 9e0 <= -19 + 2c)) }" },
1845 { 1, "{ [y, x] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
1846 "[1, 0] }" },
1847 { 1, "{ [x, y] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
1848 "[0, 1] }" },
1849 { 1, "{ [1, y] : -1 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
1850 { 1, "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
1851 { 1, "{ [x, y] : 0 <= x <= 10 and x - 4*floor(x/4) <= 1 and y <= 0; "
1852 "[x, y] : 0 <= x <= 10 and x - 4*floor(x/4) > 1 and y <= 0; "
1853 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) <= 1 and 0 < y; "
1854 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) > 1 and 0 < y }" },
1855 { 1, "{ [x, 0] : 0 <= x <= 10 and x mod 2 = 0; "
1856 "[x, 0] : 0 <= x <= 10 and x mod 2 = 1; "
1857 "[x, y] : 0 <= x <= 10 and 1 <= y <= 10 }" },
1858 { 1, "{ [a] : a <= 8 and "
1859 "(a mod 10 = 7 or a mod 10 = 8 or a mod 10 = 9) }" },
1860 { 1, "{ [x, y] : 2y = -x and x <= 0 or "
1861 "x <= -1 and 2y <= -x - 1 and 2y >= x - 1 }" },
1862 { 0, "{ [x, y] : 2y = -x and x <= 0 or "
1863 "x <= -2 and 2y <= -x - 1 and 2y >= x - 1 }" },
1864 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
1865 "(a < 0 and 3*floor((a)/3) < a) }" },
1866 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
1867 "(a < -1 and 3*floor((a)/3) < a) }" },
1868 { 1, "{ [a, b] : a <= 1024 and b >= 0 and "
1869 "((-31 - a + b <= 32*floor((-1 - a)/32) <= -33 + b and "
1870 "32*floor((-1 - a)/32) <= -16 + b + 16*floor((-1 - a)/16))"
1871 "or (2 <= a <= 15 and b < a)) }" },
1872 { 1, "{ [a] : a > 0 and ((16*floor((a)/16) < a and "
1873 "32*floor((a)/32) < a) or a <= 15) }" },
1874 { 1, "{ [a, b, c, d] : (-a + d) mod 64 = 0 and a <= 8 and b <= 1 and "
1875 "10 - a <= c <= 3 and d >= 5 and 9 - 64b <= d <= 70;"
1876 "[a, b = 1, c, d] : (-a + d) mod 64 = 0 and a <= 8 and c >= 4 and "
1877 "10 - a <= c <= 5 and 5 <= d <= 73 - c }" },
1878 { 1, "[n, m] -> { S_0[i] : (-n + i) mod 3 = 0 and m >= 3 + n and "
1879 "i >= n and 3*floor((2 + n + 2m)/3) <= n + 3m - i; "
1880 "S_0[n] : n <= m <= 2 + n }" },
1881 { 1, "{ [a, b] : exists (e0: 0 <= a <= 1 and b >= 0 and "
1882 "2e0 >= -5 + a + 2b and 2e0 >= -1 + a + b and "
1883 "2e0 <= a + b); "
1884 "[a, b] : exists (e0: 0 <= a <= 1 and 2e0 >= -5 + a + 2b and "
1885 "2e0 >= -1 - a + b and 2e0 <= -a + b and "
1886 "2e0 < -a + 2b) }" },
1887 { 1, "{ [i, j, i - 8j] : 8 <= i <= 63 and -7 + i <= 8j <= i; "
1888 "[i, 0, i] : 0 <= i <= 7 }" },
1889 { 1, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [1, 1] }" },
1890 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [0, 2] }" },
1891 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [-1, 3] }" },
1892 { 1, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [1, 1] }" },
1893 { 0, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [2, 1] }" },
1896 /* A specialized coalescing test case that would result
1897 * in a segmentation fault or a failed assertion in earlier versions of isl.
1899 static int test_coalesce_special(struct isl_ctx *ctx)
1901 const char *str;
1902 isl_map *map1, *map2;
1904 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1905 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
1906 "(y = 201 and o1 <= 239 and o1 >= 212) or "
1907 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
1908 "o1 <= 239 and o1 >= 212)) or "
1909 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
1910 "o1 <= 241 and o1 >= 240));"
1911 "[S_L220_OUT[] -> T7[]] -> "
1912 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
1913 "(y = 2 and o1 <= 241 and o1 >= 212) or "
1914 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
1915 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
1916 map1 = isl_map_read_from_str(ctx, str);
1917 map1 = isl_map_align_divs_internal(map1);
1918 map1 = isl_map_coalesce(map1);
1919 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1920 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
1921 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
1922 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
1923 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
1924 map2 = isl_map_read_from_str(ctx, str);
1925 map2 = isl_map_union(map2, map1);
1926 map2 = isl_map_align_divs_internal(map2);
1927 map2 = isl_map_coalesce(map2);
1928 isl_map_free(map2);
1929 if (!map2)
1930 return -1;
1932 return 0;
1935 /* A specialized coalescing test case that would result in an assertion
1936 * in an earlier version of isl.
1937 * The explicit call to isl_basic_set_union prevents the implicit
1938 * equality constraints in the first basic map from being detected prior
1939 * to the call to isl_set_coalesce, at least at the point
1940 * where this test case was introduced.
1942 static int test_coalesce_special2(struct isl_ctx *ctx)
1944 const char *str;
1945 isl_basic_set *bset1, *bset2;
1946 isl_set *set;
1948 str = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
1949 bset1 = isl_basic_set_read_from_str(ctx, str);
1950 str = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }" ;
1951 bset2 = isl_basic_set_read_from_str(ctx, str);
1952 set = isl_basic_set_union(bset1, bset2);
1953 set = isl_set_coalesce(set);
1954 isl_set_free(set);
1956 if (!set)
1957 return -1;
1958 return 0;
1961 /* Check that calling isl_set_coalesce does not leave other sets
1962 * that may share some information with the input to isl_set_coalesce
1963 * in an inconsistent state.
1964 * In particular, older versions of isl would modify all copies
1965 * of the basic sets in the isl_set_coalesce input in a way
1966 * that could leave them in an inconsistent state.
1967 * The result of printing any other set containing one of these
1968 * basic sets would then result in an invalid set description.
1970 static int test_coalesce_special3(isl_ctx *ctx)
1972 const char *str;
1973 char *s;
1974 isl_set *set1, *set2;
1975 isl_printer *p;
1977 set1 = isl_set_read_from_str(ctx, "{ [0, 0, 0] }");
1978 str = "{ [a, b, a + b] : a >= 0 and b >= 0 and 0 < a + b }";
1979 set2 = isl_set_read_from_str(ctx, str);
1980 set1 = isl_set_union(set1, isl_set_copy(set2));
1981 set1 = isl_set_coalesce(set1);
1982 isl_set_free(set1);
1984 p = isl_printer_to_str(ctx);
1985 p = isl_printer_print_set(p, set2);
1986 isl_set_free(set2);
1987 s = isl_printer_get_str(p);
1988 isl_printer_free(p);
1989 set1 = isl_set_read_from_str(ctx, s);
1990 free(s);
1991 isl_set_free(set1);
1993 if (!set1)
1994 return -1;
1996 return 0;
1999 /* Test the functionality of isl_set_coalesce.
2000 * That is, check that the output is always equal to the input
2001 * and in some cases that the result consists of a single disjunct.
2003 static int test_coalesce(struct isl_ctx *ctx)
2005 int i;
2007 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
2008 const char *str = coalesce_tests[i].str;
2009 int check_one = coalesce_tests[i].single_disjunct;
2010 if (test_coalesce_set(ctx, str, check_one) < 0)
2011 return -1;
2014 if (test_coalesce_unbounded_wrapping(ctx) < 0)
2015 return -1;
2016 if (test_coalesce_special(ctx) < 0)
2017 return -1;
2018 if (test_coalesce_special2(ctx) < 0)
2019 return -1;
2020 if (test_coalesce_special3(ctx) < 0)
2021 return -1;
2023 return 0;
2026 /* Construct a representation of the graph on the right of Figure 1
2027 * in "Computing the Transitive Closure of a Union of
2028 * Affine Integer Tuple Relations".
2030 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2032 isl_set *dom;
2033 isl_map *up, *right;
2035 dom = isl_set_read_from_str(ctx,
2036 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2037 "2 x - 3 y + 3 >= 0 }");
2038 right = isl_map_read_from_str(ctx,
2039 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2040 up = isl_map_read_from_str(ctx,
2041 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2042 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2043 right = isl_map_intersect_range(right, isl_set_copy(dom));
2044 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2045 up = isl_map_intersect_range(up, dom);
2046 return isl_map_union(up, right);
2049 /* Construct a representation of the power of the graph
2050 * on the right of Figure 1 in "Computing the Transitive Closure of
2051 * a Union of Affine Integer Tuple Relations".
2053 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2055 return isl_map_read_from_str(ctx,
2056 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2057 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2058 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2061 /* Construct a representation of the transitive closure of the graph
2062 * on the right of Figure 1 in "Computing the Transitive Closure of
2063 * a Union of Affine Integer Tuple Relations".
2065 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2067 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2070 static int test_closure(isl_ctx *ctx)
2072 const char *str;
2073 isl_map *map, *map2;
2074 int exact, equal;
2076 /* COCOA example 1 */
2077 map = isl_map_read_from_str(ctx,
2078 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2079 "1 <= i and i < n and 1 <= j and j < n or "
2080 "i2 = i + 1 and j2 = j - 1 and "
2081 "1 <= i and i < n and 2 <= j and j <= n }");
2082 map = isl_map_power(map, &exact);
2083 assert(exact);
2084 isl_map_free(map);
2086 /* COCOA example 1 */
2087 map = isl_map_read_from_str(ctx,
2088 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2089 "1 <= i and i < n and 1 <= j and j < n or "
2090 "i2 = i + 1 and j2 = j - 1 and "
2091 "1 <= i and i < n and 2 <= j and j <= n }");
2092 map = isl_map_transitive_closure(map, &exact);
2093 assert(exact);
2094 map2 = isl_map_read_from_str(ctx,
2095 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2096 "1 <= i and i < n and 1 <= j and j <= n and "
2097 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2098 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2099 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2100 assert(isl_map_is_equal(map, map2));
2101 isl_map_free(map2);
2102 isl_map_free(map);
2104 map = isl_map_read_from_str(ctx,
2105 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2106 " 0 <= y and y <= n }");
2107 map = isl_map_transitive_closure(map, &exact);
2108 map2 = isl_map_read_from_str(ctx,
2109 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2110 " 0 <= y and y <= n }");
2111 assert(isl_map_is_equal(map, map2));
2112 isl_map_free(map2);
2113 isl_map_free(map);
2115 /* COCOA example 2 */
2116 map = isl_map_read_from_str(ctx,
2117 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2118 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2119 "i2 = i + 2 and j2 = j - 2 and "
2120 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2121 map = isl_map_transitive_closure(map, &exact);
2122 assert(exact);
2123 map2 = isl_map_read_from_str(ctx,
2124 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2125 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2126 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2127 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2128 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2129 assert(isl_map_is_equal(map, map2));
2130 isl_map_free(map);
2131 isl_map_free(map2);
2133 /* COCOA Fig.2 left */
2134 map = isl_map_read_from_str(ctx,
2135 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2136 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2137 "j <= n or "
2138 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2139 "j <= 2 i - 3 and j <= n - 2 or "
2140 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2141 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2142 map = isl_map_transitive_closure(map, &exact);
2143 assert(exact);
2144 isl_map_free(map);
2146 /* COCOA Fig.2 right */
2147 map = isl_map_read_from_str(ctx,
2148 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2149 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2150 "j <= n or "
2151 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2152 "j <= 2 i - 4 and j <= n - 3 or "
2153 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2154 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2155 map = isl_map_power(map, &exact);
2156 assert(exact);
2157 isl_map_free(map);
2159 /* COCOA Fig.2 right */
2160 map = isl_map_read_from_str(ctx,
2161 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2162 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2163 "j <= n or "
2164 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2165 "j <= 2 i - 4 and j <= n - 3 or "
2166 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2167 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2168 map = isl_map_transitive_closure(map, &exact);
2169 assert(exact);
2170 map2 = isl_map_read_from_str(ctx,
2171 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2172 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2173 "j <= n and 3 + i + 2 j <= 3 n and "
2174 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2175 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2176 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2177 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2178 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2179 assert(isl_map_is_equal(map, map2));
2180 isl_map_free(map2);
2181 isl_map_free(map);
2183 map = cocoa_fig_1_right_graph(ctx);
2184 map = isl_map_transitive_closure(map, &exact);
2185 assert(exact);
2186 map2 = cocoa_fig_1_right_tc(ctx);
2187 assert(isl_map_is_equal(map, map2));
2188 isl_map_free(map2);
2189 isl_map_free(map);
2191 map = cocoa_fig_1_right_graph(ctx);
2192 map = isl_map_power(map, &exact);
2193 map2 = cocoa_fig_1_right_power(ctx);
2194 equal = isl_map_is_equal(map, map2);
2195 isl_map_free(map2);
2196 isl_map_free(map);
2197 if (equal < 0)
2198 return -1;
2199 if (!exact)
2200 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2201 if (!equal)
2202 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2204 /* COCOA Theorem 1 counter example */
2205 map = isl_map_read_from_str(ctx,
2206 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2207 "i2 = 1 and j2 = j or "
2208 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2209 map = isl_map_transitive_closure(map, &exact);
2210 assert(exact);
2211 isl_map_free(map);
2213 map = isl_map_read_from_str(ctx,
2214 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2215 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2216 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2217 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2218 map = isl_map_transitive_closure(map, &exact);
2219 assert(exact);
2220 isl_map_free(map);
2222 /* Kelly et al 1996, fig 12 */
2223 map = isl_map_read_from_str(ctx,
2224 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2225 "1 <= i,j,j+1 <= n or "
2226 "j = n and j2 = 1 and i2 = i + 1 and "
2227 "1 <= i,i+1 <= n }");
2228 map = isl_map_transitive_closure(map, &exact);
2229 assert(exact);
2230 map2 = isl_map_read_from_str(ctx,
2231 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2232 "1 <= i <= n and i = i2 or "
2233 "1 <= i < i2 <= n and 1 <= j <= n and "
2234 "1 <= j2 <= n }");
2235 assert(isl_map_is_equal(map, map2));
2236 isl_map_free(map2);
2237 isl_map_free(map);
2239 /* Omega's closure4 */
2240 map = isl_map_read_from_str(ctx,
2241 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2242 "1 <= x,y <= 10 or "
2243 "x2 = x + 1 and y2 = y and "
2244 "1 <= x <= 20 && 5 <= y <= 15 }");
2245 map = isl_map_transitive_closure(map, &exact);
2246 assert(exact);
2247 isl_map_free(map);
2249 map = isl_map_read_from_str(ctx,
2250 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2251 map = isl_map_transitive_closure(map, &exact);
2252 assert(!exact);
2253 map2 = isl_map_read_from_str(ctx,
2254 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2255 assert(isl_map_is_equal(map, map2));
2256 isl_map_free(map);
2257 isl_map_free(map2);
2259 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2260 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2261 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2262 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2263 map = isl_map_read_from_str(ctx, str);
2264 map = isl_map_transitive_closure(map, &exact);
2265 assert(exact);
2266 map2 = isl_map_read_from_str(ctx, str);
2267 assert(isl_map_is_equal(map, map2));
2268 isl_map_free(map);
2269 isl_map_free(map2);
2271 str = "{[0] -> [1]; [2] -> [3]}";
2272 map = isl_map_read_from_str(ctx, str);
2273 map = isl_map_transitive_closure(map, &exact);
2274 assert(exact);
2275 map2 = isl_map_read_from_str(ctx, str);
2276 assert(isl_map_is_equal(map, map2));
2277 isl_map_free(map);
2278 isl_map_free(map2);
2280 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2281 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2282 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2283 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2284 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2285 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2286 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2287 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2288 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2289 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2290 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2291 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2292 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2293 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2294 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2295 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2296 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2297 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2298 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2299 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2300 map = isl_map_read_from_str(ctx, str);
2301 map = isl_map_transitive_closure(map, NULL);
2302 assert(map);
2303 isl_map_free(map);
2305 return 0;
2308 static int test_lex(struct isl_ctx *ctx)
2310 isl_space *dim;
2311 isl_map *map;
2312 int empty;
2314 dim = isl_space_set_alloc(ctx, 0, 0);
2315 map = isl_map_lex_le(dim);
2316 empty = isl_map_is_empty(map);
2317 isl_map_free(map);
2319 if (empty < 0)
2320 return -1;
2321 if (empty)
2322 isl_die(ctx, isl_error_unknown,
2323 "expecting non-empty result", return -1);
2325 return 0;
2328 /* Inputs for isl_map_lexmin tests.
2329 * "map" is the input and "lexmin" is the expected result.
2331 struct {
2332 const char *map;
2333 const char *lexmin;
2334 } lexmin_tests [] = {
2335 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
2336 "{ [x] -> [5] : 6 <= x <= 8; "
2337 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
2338 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
2339 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
2340 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
2341 "{ [x] -> [y] : (4y = x and x >= 0) or "
2342 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2343 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2344 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
2345 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
2346 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
2347 /* Check that empty pieces are properly combined. */
2348 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2349 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2350 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
2351 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2352 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2353 "x >= 4 }" },
2354 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
2355 "a <= 255 and c <= 255 and d <= 255 - j and "
2356 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
2357 "247d <= 247 + k - j and 247d <= 247 + k - b and "
2358 "247d <= 247 + i and 248 - b <= 248d <= c and "
2359 "254d >= i - a + b and 254d >= -a + b and "
2360 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
2361 "{ [i, k, j] -> "
2362 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
2363 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
2364 "247j >= 62738 - i and 509j <= 129795 + i and "
2365 "742j >= 188724 - i; "
2366 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
2367 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
2368 "16*floor((8 + b)/16) <= 7 + b; "
2369 "[a] -> [1] }",
2370 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
2371 "[a] -> [b = 0] : 0 < a <= 509 }" },
2372 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
2373 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
2376 static int test_lexmin(struct isl_ctx *ctx)
2378 int i;
2379 int equal;
2380 const char *str;
2381 isl_basic_map *bmap;
2382 isl_map *map, *map2;
2383 isl_set *set;
2384 isl_set *set2;
2385 isl_pw_multi_aff *pma;
2387 str = "[p0, p1] -> { [] -> [] : "
2388 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2389 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2390 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2391 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2392 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2393 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2394 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2395 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2396 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2397 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2398 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2399 map = isl_map_read_from_str(ctx, str);
2400 map = isl_map_lexmin(map);
2401 isl_map_free(map);
2403 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2404 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2405 set = isl_set_read_from_str(ctx, str);
2406 set = isl_set_lexmax(set);
2407 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2408 set2 = isl_set_read_from_str(ctx, str);
2409 set = isl_set_intersect(set, set2);
2410 assert(!isl_set_is_empty(set));
2411 isl_set_free(set);
2413 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
2414 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
2415 map = isl_map_lexmin(map);
2416 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
2417 equal = isl_map_is_equal(map, map2);
2418 isl_map_free(map);
2419 isl_map_free(map2);
2421 if (equal < 0)
2422 return -1;
2423 if (!equal)
2424 isl_die(ctx, isl_error_unknown,
2425 "unexpected result", return -1);
2428 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2429 " 8i' <= i and 8i' >= -7 + i }";
2430 bmap = isl_basic_map_read_from_str(ctx, str);
2431 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2432 map2 = isl_map_from_pw_multi_aff(pma);
2433 map = isl_map_from_basic_map(bmap);
2434 assert(isl_map_is_equal(map, map2));
2435 isl_map_free(map);
2436 isl_map_free(map2);
2438 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2439 " 8i' <= i and 8i' >= -7 + i }";
2440 set = isl_set_read_from_str(ctx, str);
2441 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2442 set2 = isl_set_from_pw_multi_aff(pma);
2443 equal = isl_set_is_equal(set, set2);
2444 isl_set_free(set);
2445 isl_set_free(set2);
2446 if (equal < 0)
2447 return -1;
2448 if (!equal)
2449 isl_die(ctx, isl_error_unknown,
2450 "unexpected difference between set and "
2451 "piecewise affine expression", return -1);
2453 return 0;
2456 /* A specialized isl_set_min_val test case that would return the wrong result
2457 * in earlier versions of isl.
2458 * The explicit call to isl_basic_set_union prevents the second basic set
2459 * from being determined to be empty prior to the call to isl_set_min_val,
2460 * at least at the point where this test case was introduced.
2462 static int test_min_special(isl_ctx *ctx)
2464 const char *str;
2465 isl_basic_set *bset1, *bset2;
2466 isl_set *set;
2467 isl_aff *obj;
2468 isl_val *res;
2469 int ok;
2471 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
2472 bset1 = isl_basic_set_read_from_str(ctx, str);
2473 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
2474 bset2 = isl_basic_set_read_from_str(ctx, str);
2475 set = isl_basic_set_union(bset1, bset2);
2476 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
2478 res = isl_set_min_val(set, obj);
2479 ok = isl_val_cmp_si(res, 5) == 0;
2481 isl_aff_free(obj);
2482 isl_set_free(set);
2483 isl_val_free(res);
2485 if (!res)
2486 return -1;
2487 if (!ok)
2488 isl_die(ctx, isl_error_unknown, "unexpected minimum",
2489 return -1);
2491 return 0;
2494 /* A specialized isl_set_min_val test case that would return an error
2495 * in earlier versions of isl.
2497 static int test_min_special2(isl_ctx *ctx)
2499 const char *str;
2500 isl_basic_set *bset;
2501 isl_aff *obj;
2502 isl_val *res;
2504 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
2505 bset = isl_basic_set_read_from_str(ctx, str);
2507 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
2509 res = isl_basic_set_max_val(bset, obj);
2511 isl_basic_set_free(bset);
2512 isl_aff_free(obj);
2513 isl_val_free(res);
2515 if (!res)
2516 return -1;
2518 return 0;
2521 struct {
2522 const char *set;
2523 const char *obj;
2524 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2525 __isl_keep isl_aff *obj);
2526 const char *res;
2527 } opt_tests[] = {
2528 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2529 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2530 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2531 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2532 &isl_set_max_val, "30" },
2536 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2537 * In particular, check the results on non-convex inputs.
2539 static int test_min(struct isl_ctx *ctx)
2541 int i;
2542 isl_set *set;
2543 isl_aff *obj;
2544 isl_val *val, *res;
2545 isl_bool ok;
2547 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2548 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2549 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2550 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2551 val = opt_tests[i].fn(set, obj);
2552 ok = isl_val_eq(res, val);
2553 isl_val_free(res);
2554 isl_val_free(val);
2555 isl_aff_free(obj);
2556 isl_set_free(set);
2558 if (ok < 0)
2559 return -1;
2560 if (!ok)
2561 isl_die(ctx, isl_error_unknown,
2562 "unexpected optimum", return -1);
2565 if (test_min_special(ctx) < 0)
2566 return -1;
2567 if (test_min_special2(ctx) < 0)
2568 return -1;
2570 return 0;
2573 struct must_may {
2574 isl_map *must;
2575 isl_map *may;
2578 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2579 void *dep_user, void *user)
2581 struct must_may *mm = (struct must_may *)user;
2583 if (must)
2584 mm->must = isl_map_union(mm->must, dep);
2585 else
2586 mm->may = isl_map_union(mm->may, dep);
2588 return isl_stat_ok;
2591 static int common_space(void *first, void *second)
2593 int depth = *(int *)first;
2594 return 2 * depth;
2597 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2599 isl_map *map2;
2600 int equal;
2602 if (!map)
2603 return -1;
2605 map2 = isl_map_read_from_str(map->ctx, str);
2606 equal = isl_map_is_equal(map, map2);
2607 isl_map_free(map2);
2609 return equal;
2612 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2614 int equal;
2616 equal = map_is_equal(map, str);
2617 if (equal < 0)
2618 return -1;
2619 if (!equal)
2620 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2621 "result not as expected", return -1);
2622 return 0;
2625 static int test_dep(struct isl_ctx *ctx)
2627 const char *str;
2628 isl_space *dim;
2629 isl_map *map;
2630 isl_access_info *ai;
2631 isl_flow *flow;
2632 int depth;
2633 struct must_may mm;
2635 depth = 3;
2637 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2638 map = isl_map_read_from_str(ctx, str);
2639 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2641 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2642 map = isl_map_read_from_str(ctx, str);
2643 ai = isl_access_info_add_source(ai, map, 1, &depth);
2645 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2646 map = isl_map_read_from_str(ctx, str);
2647 ai = isl_access_info_add_source(ai, map, 1, &depth);
2649 flow = isl_access_info_compute_flow(ai);
2650 dim = isl_space_alloc(ctx, 0, 3, 3);
2651 mm.must = isl_map_empty(isl_space_copy(dim));
2652 mm.may = isl_map_empty(dim);
2654 isl_flow_foreach(flow, collect_must_may, &mm);
2656 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2657 " [1,10,0] -> [2,5,0] }";
2658 assert(map_is_equal(mm.must, str));
2659 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2660 assert(map_is_equal(mm.may, str));
2662 isl_map_free(mm.must);
2663 isl_map_free(mm.may);
2664 isl_flow_free(flow);
2667 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2668 map = isl_map_read_from_str(ctx, str);
2669 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2671 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2672 map = isl_map_read_from_str(ctx, str);
2673 ai = isl_access_info_add_source(ai, map, 1, &depth);
2675 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2676 map = isl_map_read_from_str(ctx, str);
2677 ai = isl_access_info_add_source(ai, map, 0, &depth);
2679 flow = isl_access_info_compute_flow(ai);
2680 dim = isl_space_alloc(ctx, 0, 3, 3);
2681 mm.must = isl_map_empty(isl_space_copy(dim));
2682 mm.may = isl_map_empty(dim);
2684 isl_flow_foreach(flow, collect_must_may, &mm);
2686 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2687 assert(map_is_equal(mm.must, str));
2688 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2689 assert(map_is_equal(mm.may, str));
2691 isl_map_free(mm.must);
2692 isl_map_free(mm.may);
2693 isl_flow_free(flow);
2696 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2697 map = isl_map_read_from_str(ctx, str);
2698 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2700 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2701 map = isl_map_read_from_str(ctx, str);
2702 ai = isl_access_info_add_source(ai, map, 0, &depth);
2704 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2705 map = isl_map_read_from_str(ctx, str);
2706 ai = isl_access_info_add_source(ai, map, 0, &depth);
2708 flow = isl_access_info_compute_flow(ai);
2709 dim = isl_space_alloc(ctx, 0, 3, 3);
2710 mm.must = isl_map_empty(isl_space_copy(dim));
2711 mm.may = isl_map_empty(dim);
2713 isl_flow_foreach(flow, collect_must_may, &mm);
2715 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2716 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2717 assert(map_is_equal(mm.may, str));
2718 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2719 assert(map_is_equal(mm.must, str));
2721 isl_map_free(mm.must);
2722 isl_map_free(mm.may);
2723 isl_flow_free(flow);
2726 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2727 map = isl_map_read_from_str(ctx, str);
2728 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2730 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2731 map = isl_map_read_from_str(ctx, str);
2732 ai = isl_access_info_add_source(ai, map, 0, &depth);
2734 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2735 map = isl_map_read_from_str(ctx, str);
2736 ai = isl_access_info_add_source(ai, map, 0, &depth);
2738 flow = isl_access_info_compute_flow(ai);
2739 dim = isl_space_alloc(ctx, 0, 3, 3);
2740 mm.must = isl_map_empty(isl_space_copy(dim));
2741 mm.may = isl_map_empty(dim);
2743 isl_flow_foreach(flow, collect_must_may, &mm);
2745 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2746 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2747 assert(map_is_equal(mm.may, str));
2748 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2749 assert(map_is_equal(mm.must, str));
2751 isl_map_free(mm.must);
2752 isl_map_free(mm.may);
2753 isl_flow_free(flow);
2756 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2757 map = isl_map_read_from_str(ctx, str);
2758 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2760 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2761 map = isl_map_read_from_str(ctx, str);
2762 ai = isl_access_info_add_source(ai, map, 0, &depth);
2764 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2765 map = isl_map_read_from_str(ctx, str);
2766 ai = isl_access_info_add_source(ai, map, 0, &depth);
2768 flow = isl_access_info_compute_flow(ai);
2769 dim = isl_space_alloc(ctx, 0, 3, 3);
2770 mm.must = isl_map_empty(isl_space_copy(dim));
2771 mm.may = isl_map_empty(dim);
2773 isl_flow_foreach(flow, collect_must_may, &mm);
2775 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2776 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2777 assert(map_is_equal(mm.may, str));
2778 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2779 assert(map_is_equal(mm.must, str));
2781 isl_map_free(mm.must);
2782 isl_map_free(mm.may);
2783 isl_flow_free(flow);
2786 depth = 5;
2788 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2789 map = isl_map_read_from_str(ctx, str);
2790 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2792 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2793 map = isl_map_read_from_str(ctx, str);
2794 ai = isl_access_info_add_source(ai, map, 1, &depth);
2796 flow = isl_access_info_compute_flow(ai);
2797 dim = isl_space_alloc(ctx, 0, 5, 5);
2798 mm.must = isl_map_empty(isl_space_copy(dim));
2799 mm.may = isl_map_empty(dim);
2801 isl_flow_foreach(flow, collect_must_may, &mm);
2803 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2804 assert(map_is_equal(mm.must, str));
2805 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2806 assert(map_is_equal(mm.may, str));
2808 isl_map_free(mm.must);
2809 isl_map_free(mm.may);
2810 isl_flow_free(flow);
2812 return 0;
2815 /* Check that the dependence analysis proceeds without errors.
2816 * Earlier versions of isl would break down during the analysis
2817 * due to the use of the wrong spaces.
2819 static int test_flow(isl_ctx *ctx)
2821 const char *str;
2822 isl_union_map *access, *schedule;
2823 isl_union_map *must_dep, *may_dep;
2824 int r;
2826 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2827 access = isl_union_map_read_from_str(ctx, str);
2828 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2829 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2830 "S2[] -> [1,0,0,0]; "
2831 "S3[] -> [-1,0,0,0] }";
2832 schedule = isl_union_map_read_from_str(ctx, str);
2833 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2834 isl_union_map_copy(access), schedule,
2835 &must_dep, &may_dep, NULL, NULL);
2836 isl_union_map_free(may_dep);
2837 isl_union_map_free(must_dep);
2839 return r;
2842 struct {
2843 const char *map;
2844 int sv;
2845 } sv_tests[] = {
2846 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2847 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2848 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2849 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2850 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2851 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2852 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2853 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2854 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2857 int test_sv(isl_ctx *ctx)
2859 isl_union_map *umap;
2860 int i;
2861 int sv;
2863 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2864 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2865 sv = isl_union_map_is_single_valued(umap);
2866 isl_union_map_free(umap);
2867 if (sv < 0)
2868 return -1;
2869 if (sv_tests[i].sv && !sv)
2870 isl_die(ctx, isl_error_internal,
2871 "map not detected as single valued", return -1);
2872 if (!sv_tests[i].sv && sv)
2873 isl_die(ctx, isl_error_internal,
2874 "map detected as single valued", return -1);
2877 return 0;
2880 struct {
2881 const char *str;
2882 int bijective;
2883 } bijective_tests[] = {
2884 { "[N,M]->{[i,j] -> [i]}", 0 },
2885 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
2886 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
2887 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
2888 { "[N,M]->{[i,j] -> [j,i]}", 1 },
2889 { "[N,M]->{[i,j] -> [i+j]}", 0 },
2890 { "[N,M]->{[i,j] -> []}", 0 },
2891 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
2892 { "[N,M]->{[i,j] -> [2i]}", 0 },
2893 { "[N,M]->{[i,j] -> [i,i]}", 0 },
2894 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
2895 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
2896 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
2899 static int test_bijective(struct isl_ctx *ctx)
2901 isl_map *map;
2902 int i;
2903 int bijective;
2905 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
2906 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
2907 bijective = isl_map_is_bijective(map);
2908 isl_map_free(map);
2909 if (bijective < 0)
2910 return -1;
2911 if (bijective_tests[i].bijective && !bijective)
2912 isl_die(ctx, isl_error_internal,
2913 "map not detected as bijective", return -1);
2914 if (!bijective_tests[i].bijective && bijective)
2915 isl_die(ctx, isl_error_internal,
2916 "map detected as bijective", return -1);
2919 return 0;
2922 /* Inputs for isl_pw_qpolynomial_gist tests.
2923 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2925 struct {
2926 const char *pwqp;
2927 const char *set;
2928 const char *gist;
2929 } pwqp_gist_tests[] = {
2930 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2931 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2932 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2933 "{ [i] -> -1/2 + 1/2 * i }" },
2934 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2937 static int test_pwqp(struct isl_ctx *ctx)
2939 int i;
2940 const char *str;
2941 isl_set *set;
2942 isl_pw_qpolynomial *pwqp1, *pwqp2;
2943 int equal;
2945 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2946 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2948 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2949 isl_dim_in, 1, 1);
2951 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2952 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2954 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2956 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2958 isl_pw_qpolynomial_free(pwqp1);
2960 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2961 str = pwqp_gist_tests[i].pwqp;
2962 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2963 str = pwqp_gist_tests[i].set;
2964 set = isl_set_read_from_str(ctx, str);
2965 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2966 str = pwqp_gist_tests[i].gist;
2967 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2968 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2969 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2970 isl_pw_qpolynomial_free(pwqp1);
2972 if (equal < 0)
2973 return -1;
2974 if (!equal)
2975 isl_die(ctx, isl_error_unknown,
2976 "unexpected result", return -1);
2979 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2980 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2981 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2982 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2984 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2986 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2988 isl_pw_qpolynomial_free(pwqp1);
2990 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2991 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2992 str = "{ [x] -> x }";
2993 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2995 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2997 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2999 isl_pw_qpolynomial_free(pwqp1);
3001 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3002 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3003 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3004 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3005 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3006 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3007 isl_pw_qpolynomial_free(pwqp1);
3009 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3010 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3011 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3012 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3013 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3014 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3015 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3016 isl_pw_qpolynomial_free(pwqp1);
3017 isl_pw_qpolynomial_free(pwqp2);
3018 if (equal < 0)
3019 return -1;
3020 if (!equal)
3021 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3023 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3024 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3025 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3026 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3027 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3028 isl_val_one(ctx));
3029 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3030 isl_pw_qpolynomial_free(pwqp1);
3031 isl_pw_qpolynomial_free(pwqp2);
3032 if (equal < 0)
3033 return -1;
3034 if (!equal)
3035 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3037 return 0;
3040 static int test_split_periods(isl_ctx *ctx)
3042 const char *str;
3043 isl_pw_qpolynomial *pwqp;
3045 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3046 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3047 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3048 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3050 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3052 isl_pw_qpolynomial_free(pwqp);
3054 if (!pwqp)
3055 return -1;
3057 return 0;
3060 static int test_union(isl_ctx *ctx)
3062 const char *str;
3063 isl_union_set *uset1, *uset2;
3064 isl_union_map *umap1, *umap2;
3065 int equal;
3067 str = "{ [i] : 0 <= i <= 1 }";
3068 uset1 = isl_union_set_read_from_str(ctx, str);
3069 str = "{ [1] -> [0] }";
3070 umap1 = isl_union_map_read_from_str(ctx, str);
3072 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3073 equal = isl_union_map_is_equal(umap1, umap2);
3075 isl_union_map_free(umap1);
3076 isl_union_map_free(umap2);
3078 if (equal < 0)
3079 return -1;
3080 if (!equal)
3081 isl_die(ctx, isl_error_unknown, "union maps not equal",
3082 return -1);
3084 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3085 umap1 = isl_union_map_read_from_str(ctx, str);
3086 str = "{ A[i]; B[i] }";
3087 uset1 = isl_union_set_read_from_str(ctx, str);
3089 uset2 = isl_union_map_domain(umap1);
3091 equal = isl_union_set_is_equal(uset1, uset2);
3093 isl_union_set_free(uset1);
3094 isl_union_set_free(uset2);
3096 if (equal < 0)
3097 return -1;
3098 if (!equal)
3099 isl_die(ctx, isl_error_unknown, "union sets not equal",
3100 return -1);
3102 return 0;
3105 /* Check that computing a bound of a non-zero polynomial over an unbounded
3106 * domain does not produce a rational value.
3107 * In particular, check that the upper bound is infinity.
3109 static int test_bound_unbounded_domain(isl_ctx *ctx)
3111 const char *str;
3112 isl_pw_qpolynomial *pwqp;
3113 isl_pw_qpolynomial_fold *pwf, *pwf2;
3114 isl_bool equal;
3116 str = "{ [m,n] -> -m * n }";
3117 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3118 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3119 str = "{ infty }";
3120 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3121 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3122 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
3123 isl_pw_qpolynomial_fold_free(pwf);
3124 isl_pw_qpolynomial_fold_free(pwf2);
3126 if (equal < 0)
3127 return -1;
3128 if (!equal)
3129 isl_die(ctx, isl_error_unknown,
3130 "expecting infinite polynomial bound", return -1);
3132 return 0;
3135 static int test_bound(isl_ctx *ctx)
3137 const char *str;
3138 unsigned dim;
3139 isl_pw_qpolynomial *pwqp;
3140 isl_pw_qpolynomial_fold *pwf;
3142 if (test_bound_unbounded_domain(ctx) < 0)
3143 return -1;
3145 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
3146 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3147 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3148 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3149 isl_pw_qpolynomial_fold_free(pwf);
3150 if (dim != 4)
3151 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3152 return -1);
3154 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
3155 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3156 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3157 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3158 isl_pw_qpolynomial_fold_free(pwf);
3159 if (dim != 1)
3160 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3161 return -1);
3163 return 0;
3166 static int test_lift(isl_ctx *ctx)
3168 const char *str;
3169 isl_basic_map *bmap;
3170 isl_basic_set *bset;
3172 str = "{ [i0] : exists e0 : i0 = 4e0 }";
3173 bset = isl_basic_set_read_from_str(ctx, str);
3174 bset = isl_basic_set_lift(bset);
3175 bmap = isl_basic_map_from_range(bset);
3176 bset = isl_basic_map_domain(bmap);
3177 isl_basic_set_free(bset);
3179 return 0;
3182 struct {
3183 const char *set1;
3184 const char *set2;
3185 int subset;
3186 } subset_tests[] = {
3187 { "{ [112, 0] }",
3188 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
3189 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
3190 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
3191 { "{ [65] }",
3192 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
3193 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
3194 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
3195 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
3196 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
3197 "256e0 <= 255i and 256e0 >= -255 + 255i and "
3198 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
3199 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
3200 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
3201 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
3202 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
3203 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
3204 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
3205 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
3206 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
3207 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
3208 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
3209 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
3210 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
3211 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
3212 "4e0 <= -57 + i0 + i1)) or "
3213 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
3214 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
3215 "4e0 >= -61 + i0 + i1)) or "
3216 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
3217 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
3220 static int test_subset(isl_ctx *ctx)
3222 int i;
3223 isl_set *set1, *set2;
3224 int subset;
3226 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3227 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3228 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3229 subset = isl_set_is_subset(set1, set2);
3230 isl_set_free(set1);
3231 isl_set_free(set2);
3232 if (subset < 0)
3233 return -1;
3234 if (subset != subset_tests[i].subset)
3235 isl_die(ctx, isl_error_unknown,
3236 "incorrect subset result", return -1);
3239 return 0;
3242 struct {
3243 const char *minuend;
3244 const char *subtrahend;
3245 const char *difference;
3246 } subtract_domain_tests[] = {
3247 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3248 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3249 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3252 static int test_subtract(isl_ctx *ctx)
3254 int i;
3255 isl_union_map *umap1, *umap2;
3256 isl_union_pw_multi_aff *upma1, *upma2;
3257 isl_union_set *uset;
3258 int equal;
3260 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3261 umap1 = isl_union_map_read_from_str(ctx,
3262 subtract_domain_tests[i].minuend);
3263 uset = isl_union_set_read_from_str(ctx,
3264 subtract_domain_tests[i].subtrahend);
3265 umap2 = isl_union_map_read_from_str(ctx,
3266 subtract_domain_tests[i].difference);
3267 umap1 = isl_union_map_subtract_domain(umap1, uset);
3268 equal = isl_union_map_is_equal(umap1, umap2);
3269 isl_union_map_free(umap1);
3270 isl_union_map_free(umap2);
3271 if (equal < 0)
3272 return -1;
3273 if (!equal)
3274 isl_die(ctx, isl_error_unknown,
3275 "incorrect subtract domain result", return -1);
3278 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3279 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3280 subtract_domain_tests[i].minuend);
3281 uset = isl_union_set_read_from_str(ctx,
3282 subtract_domain_tests[i].subtrahend);
3283 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3284 subtract_domain_tests[i].difference);
3285 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3286 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3287 isl_union_pw_multi_aff_free(upma1);
3288 isl_union_pw_multi_aff_free(upma2);
3289 if (equal < 0)
3290 return -1;
3291 if (!equal)
3292 isl_die(ctx, isl_error_unknown,
3293 "incorrect subtract domain result", return -1);
3296 return 0;
3299 /* Check that intersecting the empty basic set with another basic set
3300 * does not increase the number of constraints. In particular,
3301 * the empty basic set should maintain its canonical representation.
3303 static int test_intersect(isl_ctx *ctx)
3305 int n1, n2;
3306 isl_basic_set *bset1, *bset2;
3308 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3309 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3310 n1 = isl_basic_set_n_constraint(bset1);
3311 bset1 = isl_basic_set_intersect(bset1, bset2);
3312 n2 = isl_basic_set_n_constraint(bset1);
3313 isl_basic_set_free(bset1);
3314 if (!bset1)
3315 return -1;
3316 if (n1 != n2)
3317 isl_die(ctx, isl_error_unknown,
3318 "number of constraints of empty set changed",
3319 return -1);
3321 return 0;
3324 int test_factorize(isl_ctx *ctx)
3326 const char *str;
3327 isl_basic_set *bset;
3328 isl_factorizer *f;
3330 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3331 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3332 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3333 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3334 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3335 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3336 "3i5 >= -2i0 - i2 + 3i4 }";
3337 bset = isl_basic_set_read_from_str(ctx, str);
3338 f = isl_basic_set_factorizer(bset);
3339 isl_basic_set_free(bset);
3340 isl_factorizer_free(f);
3341 if (!f)
3342 isl_die(ctx, isl_error_unknown,
3343 "failed to construct factorizer", return -1);
3345 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3346 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3347 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3348 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3349 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3350 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3351 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3352 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3353 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3354 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3355 bset = isl_basic_set_read_from_str(ctx, str);
3356 f = isl_basic_set_factorizer(bset);
3357 isl_basic_set_free(bset);
3358 isl_factorizer_free(f);
3359 if (!f)
3360 isl_die(ctx, isl_error_unknown,
3361 "failed to construct factorizer", return -1);
3363 return 0;
3366 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3368 int *injective = user;
3370 *injective = isl_map_is_injective(map);
3371 isl_map_free(map);
3373 if (*injective < 0 || !*injective)
3374 return isl_stat_error;
3376 return isl_stat_ok;
3379 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3380 const char *r, const char *s, int tilable, int parallel)
3382 int i;
3383 isl_union_set *D;
3384 isl_union_map *W, *R, *S;
3385 isl_union_map *empty;
3386 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3387 isl_union_map *validity, *proximity, *coincidence;
3388 isl_union_map *schedule;
3389 isl_union_map *test;
3390 isl_union_set *delta;
3391 isl_union_set *domain;
3392 isl_set *delta_set;
3393 isl_set *slice;
3394 isl_set *origin;
3395 isl_schedule_constraints *sc;
3396 isl_schedule *sched;
3397 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3399 D = isl_union_set_read_from_str(ctx, d);
3400 W = isl_union_map_read_from_str(ctx, w);
3401 R = isl_union_map_read_from_str(ctx, r);
3402 S = isl_union_map_read_from_str(ctx, s);
3404 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3405 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3407 empty = isl_union_map_empty(isl_union_map_get_space(S));
3408 isl_union_map_compute_flow(isl_union_map_copy(R),
3409 isl_union_map_copy(W), empty,
3410 isl_union_map_copy(S),
3411 &dep_raw, NULL, NULL, NULL);
3412 isl_union_map_compute_flow(isl_union_map_copy(W),
3413 isl_union_map_copy(W),
3414 isl_union_map_copy(R),
3415 isl_union_map_copy(S),
3416 &dep_waw, &dep_war, NULL, NULL);
3418 dep = isl_union_map_union(dep_waw, dep_war);
3419 dep = isl_union_map_union(dep, dep_raw);
3420 validity = isl_union_map_copy(dep);
3421 coincidence = isl_union_map_copy(dep);
3422 proximity = isl_union_map_copy(dep);
3424 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3425 sc = isl_schedule_constraints_set_validity(sc, validity);
3426 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3427 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3428 sched = isl_schedule_constraints_compute_schedule(sc);
3429 schedule = isl_schedule_get_map(sched);
3430 isl_schedule_free(sched);
3431 isl_union_map_free(W);
3432 isl_union_map_free(R);
3433 isl_union_map_free(S);
3435 is_injection = 1;
3436 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3438 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3439 is_complete = isl_union_set_is_subset(D, domain);
3440 isl_union_set_free(D);
3441 isl_union_set_free(domain);
3443 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3444 test = isl_union_map_apply_range(test, dep);
3445 test = isl_union_map_apply_range(test, schedule);
3447 delta = isl_union_map_deltas(test);
3448 if (isl_union_set_n_set(delta) == 0) {
3449 is_tilable = 1;
3450 is_parallel = 1;
3451 is_nonneg = 1;
3452 isl_union_set_free(delta);
3453 } else {
3454 delta_set = isl_set_from_union_set(delta);
3456 slice = isl_set_universe(isl_set_get_space(delta_set));
3457 for (i = 0; i < tilable; ++i)
3458 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3459 is_tilable = isl_set_is_subset(delta_set, slice);
3460 isl_set_free(slice);
3462 slice = isl_set_universe(isl_set_get_space(delta_set));
3463 for (i = 0; i < parallel; ++i)
3464 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3465 is_parallel = isl_set_is_subset(delta_set, slice);
3466 isl_set_free(slice);
3468 origin = isl_set_universe(isl_set_get_space(delta_set));
3469 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
3470 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3472 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3473 delta_set = isl_set_lexmin(delta_set);
3475 is_nonneg = isl_set_is_equal(delta_set, origin);
3477 isl_set_free(origin);
3478 isl_set_free(delta_set);
3481 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3482 is_injection < 0 || is_complete < 0)
3483 return -1;
3484 if (!is_complete)
3485 isl_die(ctx, isl_error_unknown,
3486 "generated schedule incomplete", return -1);
3487 if (!is_injection)
3488 isl_die(ctx, isl_error_unknown,
3489 "generated schedule not injective on each statement",
3490 return -1);
3491 if (!is_nonneg)
3492 isl_die(ctx, isl_error_unknown,
3493 "negative dependences in generated schedule",
3494 return -1);
3495 if (!is_tilable)
3496 isl_die(ctx, isl_error_unknown,
3497 "generated schedule not as tilable as expected",
3498 return -1);
3499 if (!is_parallel)
3500 isl_die(ctx, isl_error_unknown,
3501 "generated schedule not as parallel as expected",
3502 return -1);
3504 return 0;
3507 /* Compute a schedule for the given instance set, validity constraints,
3508 * proximity constraints and context and return a corresponding union map
3509 * representation.
3511 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3512 const char *domain, const char *validity, const char *proximity,
3513 const char *context)
3515 isl_set *con;
3516 isl_union_set *dom;
3517 isl_union_map *dep;
3518 isl_union_map *prox;
3519 isl_schedule_constraints *sc;
3520 isl_schedule *schedule;
3521 isl_union_map *sched;
3523 con = isl_set_read_from_str(ctx, context);
3524 dom = isl_union_set_read_from_str(ctx, domain);
3525 dep = isl_union_map_read_from_str(ctx, validity);
3526 prox = isl_union_map_read_from_str(ctx, proximity);
3527 sc = isl_schedule_constraints_on_domain(dom);
3528 sc = isl_schedule_constraints_set_context(sc, con);
3529 sc = isl_schedule_constraints_set_validity(sc, dep);
3530 sc = isl_schedule_constraints_set_proximity(sc, prox);
3531 schedule = isl_schedule_constraints_compute_schedule(sc);
3532 sched = isl_schedule_get_map(schedule);
3533 isl_schedule_free(schedule);
3535 return sched;
3538 /* Compute a schedule for the given instance set, validity constraints and
3539 * proximity constraints and return a corresponding union map representation.
3541 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
3542 const char *domain, const char *validity, const char *proximity)
3544 return compute_schedule_with_context(ctx, domain, validity, proximity,
3545 "{ : }");
3548 /* Check that a schedule can be constructed on the given domain
3549 * with the given validity and proximity constraints.
3551 static int test_has_schedule(isl_ctx *ctx, const char *domain,
3552 const char *validity, const char *proximity)
3554 isl_union_map *sched;
3556 sched = compute_schedule(ctx, domain, validity, proximity);
3557 if (!sched)
3558 return -1;
3560 isl_union_map_free(sched);
3561 return 0;
3564 int test_special_schedule(isl_ctx *ctx, const char *domain,
3565 const char *validity, const char *proximity, const char *expected_sched)
3567 isl_union_map *sched1, *sched2;
3568 int equal;
3570 sched1 = compute_schedule(ctx, domain, validity, proximity);
3571 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
3573 equal = isl_union_map_is_equal(sched1, sched2);
3574 isl_union_map_free(sched1);
3575 isl_union_map_free(sched2);
3577 if (equal < 0)
3578 return -1;
3579 if (!equal)
3580 isl_die(ctx, isl_error_unknown, "unexpected schedule",
3581 return -1);
3583 return 0;
3586 /* Check that the schedule map is properly padded, i.e., that the range
3587 * lives in a single space.
3589 static int test_padded_schedule(isl_ctx *ctx)
3591 const char *str;
3592 isl_union_set *D;
3593 isl_union_map *validity, *proximity;
3594 isl_schedule_constraints *sc;
3595 isl_schedule *sched;
3596 isl_union_map *umap;
3597 isl_union_set *range;
3598 isl_set *set;
3600 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
3601 D = isl_union_set_read_from_str(ctx, str);
3602 validity = isl_union_map_empty(isl_union_set_get_space(D));
3603 proximity = isl_union_map_copy(validity);
3604 sc = isl_schedule_constraints_on_domain(D);
3605 sc = isl_schedule_constraints_set_validity(sc, validity);
3606 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3607 sched = isl_schedule_constraints_compute_schedule(sc);
3608 umap = isl_schedule_get_map(sched);
3609 isl_schedule_free(sched);
3610 range = isl_union_map_range(umap);
3611 set = isl_set_from_union_set(range);
3612 isl_set_free(set);
3614 if (!set)
3615 return -1;
3617 return 0;
3620 /* Check that conditional validity constraints are also taken into
3621 * account across bands.
3622 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
3623 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
3624 * and then check that the adjacent order constraint C[2,1]->D[2,0]
3625 * is enforced by the rest of the schedule.
3627 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
3629 const char *str;
3630 isl_union_set *domain;
3631 isl_union_map *validity, *proximity, *condition;
3632 isl_union_map *sink, *source, *dep;
3633 isl_schedule_constraints *sc;
3634 isl_schedule *schedule;
3635 isl_union_access_info *access;
3636 isl_union_flow *flow;
3637 int empty;
3639 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3640 "A[k] : k >= 1 and k <= -1 + n; "
3641 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3642 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
3643 domain = isl_union_set_read_from_str(ctx, str);
3644 sc = isl_schedule_constraints_on_domain(domain);
3645 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
3646 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3647 "D[k, i] -> C[1 + k, i] : "
3648 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3649 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
3650 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
3651 validity = isl_union_map_read_from_str(ctx, str);
3652 sc = isl_schedule_constraints_set_validity(sc, validity);
3653 str = "[n] -> { C[k, i] -> D[k, i] : "
3654 "0 <= i <= -1 + k and k <= -1 + n }";
3655 proximity = isl_union_map_read_from_str(ctx, str);
3656 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3657 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
3658 "i <= -1 + k and i >= 1 and k <= -2 + n; "
3659 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
3660 "k <= -1 + n and i >= 0 and i <= -2 + k }";
3661 condition = isl_union_map_read_from_str(ctx, str);
3662 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
3663 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3664 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
3665 "i >= 0 and i <= -1 + k and k <= -1 + n and "
3666 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
3667 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
3668 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3669 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
3670 "k <= -1 + n and i >= 0 and i <= -1 + k and "
3671 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
3672 validity = isl_union_map_read_from_str(ctx, str);
3673 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3674 validity);
3675 schedule = isl_schedule_constraints_compute_schedule(sc);
3676 str = "{ D[2,0] -> [] }";
3677 sink = isl_union_map_read_from_str(ctx, str);
3678 access = isl_union_access_info_from_sink(sink);
3679 str = "{ C[2,1] -> [] }";
3680 source = isl_union_map_read_from_str(ctx, str);
3681 access = isl_union_access_info_set_must_source(access, source);
3682 access = isl_union_access_info_set_schedule(access, schedule);
3683 flow = isl_union_access_info_compute_flow(access);
3684 dep = isl_union_flow_get_must_dependence(flow);
3685 isl_union_flow_free(flow);
3686 empty = isl_union_map_is_empty(dep);
3687 isl_union_map_free(dep);
3689 if (empty < 0)
3690 return -1;
3691 if (empty)
3692 isl_die(ctx, isl_error_unknown,
3693 "conditional validity not respected", return -1);
3695 return 0;
3698 /* Check that the test for violated conditional validity constraints
3699 * is not confused by domain compression.
3700 * In particular, earlier versions of isl would apply
3701 * a schedule on the compressed domains to the original domains,
3702 * resulting in a failure to detect that the default schedule
3703 * violates the conditional validity constraints.
3705 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
3707 const char *str;
3708 isl_bool empty;
3709 isl_union_set *domain;
3710 isl_union_map *validity, *condition;
3711 isl_schedule_constraints *sc;
3712 isl_schedule *schedule;
3713 isl_union_map *umap;
3714 isl_map *map, *ge;
3716 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
3717 domain = isl_union_set_read_from_str(ctx, str);
3718 sc = isl_schedule_constraints_on_domain(domain);
3719 str = "{ B[1, i] -> A[0, i + 1] }";
3720 condition = isl_union_map_read_from_str(ctx, str);
3721 str = "{ A[0, i] -> B[1, i - 1] }";
3722 validity = isl_union_map_read_from_str(ctx, str);
3723 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3724 isl_union_map_copy(validity));
3725 schedule = isl_schedule_constraints_compute_schedule(sc);
3726 umap = isl_schedule_get_map(schedule);
3727 isl_schedule_free(schedule);
3728 validity = isl_union_map_apply_domain(validity,
3729 isl_union_map_copy(umap));
3730 validity = isl_union_map_apply_range(validity, umap);
3731 map = isl_map_from_union_map(validity);
3732 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3733 map = isl_map_intersect(map, ge);
3734 empty = isl_map_is_empty(map);
3735 isl_map_free(map);
3737 if (empty < 0)
3738 return -1;
3739 if (!empty)
3740 isl_die(ctx, isl_error_unknown,
3741 "conditional validity constraints not satisfied",
3742 return -1);
3744 return 0;
3747 /* Input for testing of schedule construction based on
3748 * conditional constraints.
3750 * domain is the iteration domain
3751 * flow are the flow dependences, which determine the validity and
3752 * proximity constraints
3753 * condition are the conditions on the conditional validity constraints
3754 * conditional_validity are the conditional validity constraints
3755 * outer_band_n is the expected number of members in the outer band
3757 struct {
3758 const char *domain;
3759 const char *flow;
3760 const char *condition;
3761 const char *conditional_validity;
3762 int outer_band_n;
3763 } live_range_tests[] = {
3764 /* Contrived example that illustrates that we need to keep
3765 * track of tagged condition dependences and
3766 * tagged conditional validity dependences
3767 * in isl_sched_edge separately.
3768 * In particular, the conditional validity constraints on A
3769 * cannot be satisfied,
3770 * but they can be ignored because there are no corresponding
3771 * condition constraints. However, we do have an additional
3772 * conditional validity constraint that maps to the same
3773 * dependence relation
3774 * as the condition constraint on B. If we did not make a distinction
3775 * between tagged condition and tagged conditional validity
3776 * dependences, then we
3777 * could end up treating this shared dependence as an condition
3778 * constraint on A, forcing a localization of the conditions,
3779 * which is impossible.
3781 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
3782 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
3783 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
3784 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3785 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3786 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
3789 /* TACO 2013 Fig. 7 */
3790 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3791 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3792 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3793 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
3794 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
3795 "0 <= i < n and 0 <= j < n - 1 }",
3796 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
3797 "0 <= i < n and 0 <= j < j' < n;"
3798 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
3799 "0 <= i < i' < n and 0 <= j,j' < n;"
3800 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
3801 "0 <= i,j,j' < n and j < j' }",
3804 /* TACO 2013 Fig. 7, without tags */
3805 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3806 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3807 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3808 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3809 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3810 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
3811 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
3812 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
3815 /* TACO 2013 Fig. 12 */
3816 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
3817 "S3[i,3] : 0 <= i <= 1 }",
3818 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
3819 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
3820 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
3821 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
3822 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3823 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
3824 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3825 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
3826 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
3827 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
3828 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
3833 /* Test schedule construction based on conditional constraints.
3834 * In particular, check the number of members in the outer band node
3835 * as an indication of whether tiling is possible or not.
3837 static int test_conditional_schedule_constraints(isl_ctx *ctx)
3839 int i;
3840 isl_union_set *domain;
3841 isl_union_map *condition;
3842 isl_union_map *flow;
3843 isl_union_map *validity;
3844 isl_schedule_constraints *sc;
3845 isl_schedule *schedule;
3846 isl_schedule_node *node;
3847 int n_member;
3849 if (test_special_conditional_schedule_constraints(ctx) < 0)
3850 return -1;
3851 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
3852 return -1;
3854 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
3855 domain = isl_union_set_read_from_str(ctx,
3856 live_range_tests[i].domain);
3857 flow = isl_union_map_read_from_str(ctx,
3858 live_range_tests[i].flow);
3859 condition = isl_union_map_read_from_str(ctx,
3860 live_range_tests[i].condition);
3861 validity = isl_union_map_read_from_str(ctx,
3862 live_range_tests[i].conditional_validity);
3863 sc = isl_schedule_constraints_on_domain(domain);
3864 sc = isl_schedule_constraints_set_validity(sc,
3865 isl_union_map_copy(flow));
3866 sc = isl_schedule_constraints_set_proximity(sc, flow);
3867 sc = isl_schedule_constraints_set_conditional_validity(sc,
3868 condition, validity);
3869 schedule = isl_schedule_constraints_compute_schedule(sc);
3870 node = isl_schedule_get_root(schedule);
3871 while (node &&
3872 isl_schedule_node_get_type(node) != isl_schedule_node_band)
3873 node = isl_schedule_node_first_child(node);
3874 n_member = isl_schedule_node_band_n_member(node);
3875 isl_schedule_node_free(node);
3876 isl_schedule_free(schedule);
3878 if (!schedule)
3879 return -1;
3880 if (n_member != live_range_tests[i].outer_band_n)
3881 isl_die(ctx, isl_error_unknown,
3882 "unexpected number of members in outer band",
3883 return -1);
3885 return 0;
3888 /* Check that the schedule computed for the given instance set and
3889 * dependence relation strongly satisfies the dependences.
3890 * In particular, check that no instance is scheduled before
3891 * or together with an instance on which it depends.
3892 * Earlier versions of isl would produce a schedule that
3893 * only weakly satisfies the dependences.
3895 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
3897 const char *domain, *dep;
3898 isl_union_map *D, *schedule;
3899 isl_map *map, *ge;
3900 int empty;
3902 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
3903 "A[i0] : 0 <= i0 <= 1 }";
3904 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
3905 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
3906 schedule = compute_schedule(ctx, domain, dep, dep);
3907 D = isl_union_map_read_from_str(ctx, dep);
3908 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
3909 D = isl_union_map_apply_range(D, schedule);
3910 map = isl_map_from_union_map(D);
3911 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3912 map = isl_map_intersect(map, ge);
3913 empty = isl_map_is_empty(map);
3914 isl_map_free(map);
3916 if (empty < 0)
3917 return -1;
3918 if (!empty)
3919 isl_die(ctx, isl_error_unknown,
3920 "dependences not strongly satisfied", return -1);
3922 return 0;
3925 /* Compute a schedule for input where the instance set constraints
3926 * conflict with the context constraints.
3927 * Earlier versions of isl did not properly handle this situation.
3929 static int test_conflicting_context_schedule(isl_ctx *ctx)
3931 isl_union_map *schedule;
3932 const char *domain, *context;
3934 domain = "[n] -> { A[] : n >= 0 }";
3935 context = "[n] -> { : n < 0 }";
3936 schedule = compute_schedule_with_context(ctx,
3937 domain, "{}", "{}", context);
3938 isl_union_map_free(schedule);
3940 if (!schedule)
3941 return -1;
3943 return 0;
3946 /* Check that the dependence carrying step is not confused by
3947 * a bound on the coefficient size.
3948 * In particular, force the scheduler to move to a dependence carrying
3949 * step by demanding outer coincidence and bound the size of
3950 * the coefficients. Earlier versions of isl would take this
3951 * bound into account while carrying dependences, breaking
3952 * fundamental assumptions.
3953 * On the other hand, the dependence carrying step now tries
3954 * to prevent loop coalescing by default, so check that indeed
3955 * no loop coalescing occurs by comparing the computed schedule
3956 * to the expected non-coalescing schedule.
3958 static int test_bounded_coefficients_schedule(isl_ctx *ctx)
3960 const char *domain, *dep;
3961 isl_union_set *I;
3962 isl_union_map *D;
3963 isl_schedule_constraints *sc;
3964 isl_schedule *schedule;
3965 isl_union_map *sched1, *sched2;
3966 isl_bool equal;
3968 domain = "{ C[i0, i1] : 2 <= i0 <= 3999 and 0 <= i1 <= -1 + i0 }";
3969 dep = "{ C[i0, i1] -> C[i0, 1 + i1] : i0 <= 3999 and i1 >= 0 and "
3970 "i1 <= -2 + i0; "
3971 "C[i0, -1 + i0] -> C[1 + i0, 0] : i0 <= 3998 and i0 >= 1 }";
3972 I = isl_union_set_read_from_str(ctx, domain);
3973 D = isl_union_map_read_from_str(ctx, dep);
3974 sc = isl_schedule_constraints_on_domain(I);
3975 sc = isl_schedule_constraints_set_validity(sc, isl_union_map_copy(D));
3976 sc = isl_schedule_constraints_set_coincidence(sc, D);
3977 isl_options_set_schedule_outer_coincidence(ctx, 1);
3978 isl_options_set_schedule_max_coefficient(ctx, 20);
3979 schedule = isl_schedule_constraints_compute_schedule(sc);
3980 isl_options_set_schedule_max_coefficient(ctx, -1);
3981 isl_options_set_schedule_outer_coincidence(ctx, 0);
3982 sched1 = isl_schedule_get_map(schedule);
3983 isl_schedule_free(schedule);
3985 sched2 = isl_union_map_read_from_str(ctx, "{ C[x,y] -> [x,y] }");
3986 equal = isl_union_map_is_equal(sched1, sched2);
3987 isl_union_map_free(sched1);
3988 isl_union_map_free(sched2);
3990 if (equal < 0)
3991 return -1;
3992 if (!equal)
3993 isl_die(ctx, isl_error_unknown,
3994 "unexpected schedule", return -1);
3996 return 0;
3999 /* Check that the bounds on the coefficients are respected.
4000 * This function checks for a particular output schedule,
4001 * but the exact output is not important, only that it does
4002 * not contain any coefficients greater than 4.
4003 * It is, however, easier to check for a particular output.
4004 * This test is only run for the whole component scheduler
4005 * because the incremental scheduler produces a slightly different schedule.
4007 static int test_bounded_coefficients_schedule_whole(isl_ctx *ctx)
4009 const char *domain, *dep, *str;
4010 isl_union_set *I;
4011 isl_union_map *D;
4012 isl_schedule_constraints *sc;
4013 isl_schedule *schedule;
4014 isl_union_map *sched1, *sched2;
4015 isl_bool equal;
4017 domain = "{ S_4[i, j, k] : 0 <= i < j <= 10 and 0 <= k <= 100; "
4018 "S_2[i, j] : 0 <= i < j <= 10; S_6[i, j] : 0 <= i < j <= 10 }";
4019 dep = "{ S_2[0, j] -> S_4[0, j, 0] : 0 < j <= 10; "
4020 "S_4[0, j, 100] -> S_6[0, j] : 0 < j <= 10 }";
4021 I = isl_union_set_read_from_str(ctx, domain);
4022 D = isl_union_map_read_from_str(ctx, dep);
4023 sc = isl_schedule_constraints_on_domain(I);
4024 sc = isl_schedule_constraints_set_validity(sc, D);
4025 isl_options_set_schedule_max_constant_term(ctx, 10);
4026 isl_options_set_schedule_max_coefficient(ctx, 4);
4027 schedule = isl_schedule_constraints_compute_schedule(sc);
4028 isl_options_set_schedule_max_coefficient(ctx, -1);
4029 isl_options_set_schedule_max_constant_term(ctx, -1);
4030 sched1 = isl_schedule_get_map(schedule);
4031 isl_schedule_free(schedule);
4033 str = "{ S_4[i, j, k] -> [i, j, 10 - k]; "
4034 "S_2[i, j] -> [0, i, j]; S_6[i, j] -> [0, 10 + i, j] }";
4035 sched2 = isl_union_map_read_from_str(ctx, str);
4036 equal = isl_union_map_is_equal(sched1, sched2);
4037 isl_union_map_free(sched1);
4038 isl_union_map_free(sched2);
4040 if (equal < 0)
4041 return -1;
4042 if (!equal)
4043 isl_die(ctx, isl_error_unknown,
4044 "unexpected schedule", return -1);
4046 return 0;
4049 /* Check that a set of schedule constraints that only allow for
4050 * a coalescing schedule still produces a schedule even if the user
4051 * request a non-coalescing schedule. Earlier versions of isl
4052 * would not handle this case correctly.
4054 static int test_coalescing_schedule(isl_ctx *ctx)
4056 const char *domain, *dep;
4057 isl_union_set *I;
4058 isl_union_map *D;
4059 isl_schedule_constraints *sc;
4060 isl_schedule *schedule;
4061 int treat_coalescing;
4063 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
4064 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
4065 I = isl_union_set_read_from_str(ctx, domain);
4066 D = isl_union_map_read_from_str(ctx, dep);
4067 sc = isl_schedule_constraints_on_domain(I);
4068 sc = isl_schedule_constraints_set_validity(sc, D);
4069 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4070 isl_options_set_schedule_treat_coalescing(ctx, 1);
4071 schedule = isl_schedule_constraints_compute_schedule(sc);
4072 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4073 isl_schedule_free(schedule);
4074 if (!schedule)
4075 return -1;
4076 return 0;
4079 /* Check that the scheduler does not perform any needless
4080 * compound skewing. Earlier versions of isl would compute
4081 * schedules in terms of transformed schedule coefficients and
4082 * would not accurately keep track of the sum of the original
4083 * schedule coefficients. It could then produce the schedule
4084 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
4085 * for the input below instead of the schedule below.
4087 static int test_skewing_schedule(isl_ctx *ctx)
4089 const char *D, *V, *P, *S;
4091 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
4092 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
4093 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
4094 "-1 <= a-i + b-j + c-k <= 1 }";
4095 P = "{ }";
4096 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
4098 return test_special_schedule(ctx, D, V, P, S);
4101 int test_schedule(isl_ctx *ctx)
4103 const char *D, *W, *R, *V, *P, *S;
4104 int max_coincidence;
4105 int treat_coalescing;
4107 /* Handle resulting schedule with zero bands. */
4108 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
4109 return -1;
4111 /* Jacobi */
4112 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
4113 W = "{ S1[t,i] -> a[t,i] }";
4114 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
4115 "S1[t,i] -> a[t-1,i+1] }";
4116 S = "{ S1[t,i] -> [t,i] }";
4117 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4118 return -1;
4120 /* Fig. 5 of CC2008 */
4121 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
4122 "j <= -1 + N }";
4123 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
4124 "j >= 2 and j <= -1 + N }";
4125 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
4126 "j >= 2 and j <= -1 + N; "
4127 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
4128 "j >= 2 and j <= -1 + N }";
4129 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4130 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4131 return -1;
4133 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
4134 W = "{ S1[i] -> a[i] }";
4135 R = "{ S2[i] -> a[i+1] }";
4136 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4137 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4138 return -1;
4140 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
4141 W = "{ S1[i] -> a[i] }";
4142 R = "{ S2[i] -> a[9-i] }";
4143 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4144 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4145 return -1;
4147 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
4148 W = "{ S1[i] -> a[i] }";
4149 R = "[N] -> { S2[i] -> a[N-1-i] }";
4150 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4151 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4152 return -1;
4154 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
4155 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
4156 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
4157 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
4158 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4159 return -1;
4161 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4162 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
4163 R = "{ S2[i,j] -> a[i-1,j] }";
4164 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4165 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4166 return -1;
4168 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4169 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
4170 R = "{ S2[i,j] -> a[i,j-1] }";
4171 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4172 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4173 return -1;
4175 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
4176 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
4177 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
4178 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
4179 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
4180 "S_0[] -> [0, 0, 0] }";
4181 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
4182 return -1;
4183 ctx->opt->schedule_parametric = 0;
4184 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4185 return -1;
4186 ctx->opt->schedule_parametric = 1;
4188 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
4189 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
4190 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
4191 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
4192 "S4[i] -> a[i,N] }";
4193 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
4194 "S4[i] -> [4,i,0] }";
4195 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
4196 isl_options_set_schedule_maximize_coincidence(ctx, 0);
4197 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4198 return -1;
4199 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
4201 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
4202 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4203 "j <= N }";
4204 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4205 "j <= N; "
4206 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
4207 "j <= N }";
4208 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4209 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4210 return -1;
4212 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
4213 " S_2[t] : t >= 0 and t <= -1 + N; "
4214 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
4215 "i <= -1 + N }";
4216 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
4217 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
4218 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
4219 "i >= 0 and i <= -1 + N }";
4220 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
4221 "i >= 0 and i <= -1 + N; "
4222 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
4223 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
4224 " S_0[t] -> [0, t, 0] }";
4226 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4227 return -1;
4228 ctx->opt->schedule_parametric = 0;
4229 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4230 return -1;
4231 ctx->opt->schedule_parametric = 1;
4233 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
4234 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
4235 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
4236 return -1;
4238 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
4239 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
4240 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
4241 "j >= 0 and j <= -1 + N; "
4242 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4243 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
4244 "j >= 0 and j <= -1 + N; "
4245 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4246 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
4247 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4248 return -1;
4250 D = "{ S_0[i] : i >= 0 }";
4251 W = "{ S_0[i] -> a[i] : i >= 0 }";
4252 R = "{ S_0[i] -> a[0] : i >= 0 }";
4253 S = "{ S_0[i] -> [0, i, 0] }";
4254 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4255 return -1;
4257 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
4258 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
4259 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
4260 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
4261 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4262 return -1;
4264 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
4265 "k <= -1 + n and k >= 0 }";
4266 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
4267 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
4268 "k <= -1 + n and k >= 0; "
4269 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
4270 "k <= -1 + n and k >= 0; "
4271 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
4272 "k <= -1 + n and k >= 0 }";
4273 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
4274 ctx->opt->schedule_outer_coincidence = 1;
4275 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4276 return -1;
4277 ctx->opt->schedule_outer_coincidence = 0;
4279 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
4280 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
4281 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
4282 "Stmt_for_body24[i0, i1, 1, 0]:"
4283 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
4284 "Stmt_for_body7[i0, i1, i2]:"
4285 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
4286 "i2 <= 7 }";
4288 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
4289 "Stmt_for_body24[1, i1, i2, i3]:"
4290 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
4291 "i2 >= 1;"
4292 "Stmt_for_body24[0, i1, i2, i3] -> "
4293 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
4294 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
4295 "i3 >= 0;"
4296 "Stmt_for_body24[0, i1, i2, i3] ->"
4297 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4298 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4299 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4300 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4301 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4302 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4303 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4304 "i1 <= 6 and i1 >= 0;"
4305 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4306 "Stmt_for_body7[i0, i1, i2] -> "
4307 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4308 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4309 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4310 "Stmt_for_body7[i0, i1, i2] -> "
4311 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4312 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4313 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4314 P = V;
4316 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4317 isl_options_set_schedule_treat_coalescing(ctx, 0);
4318 if (test_has_schedule(ctx, D, V, P) < 0)
4319 return -1;
4320 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4322 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4323 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4324 "j >= 1 and j <= 7;"
4325 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4326 "j >= 1 and j <= 8 }";
4327 P = "{ }";
4328 S = "{ S_0[i, j] -> [i + j, i] }";
4329 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4330 if (test_special_schedule(ctx, D, V, P, S) < 0)
4331 return -1;
4332 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4334 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4335 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4336 "j >= 0 and j <= -1 + i }";
4337 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4338 "i <= -1 + N and j >= 0;"
4339 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4340 "i <= -2 + N }";
4341 P = "{ }";
4342 S = "{ S_0[i, j] -> [i, j] }";
4343 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4344 if (test_special_schedule(ctx, D, V, P, S) < 0)
4345 return -1;
4346 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4348 /* Test both algorithms on a case with only proximity dependences. */
4349 D = "{ S[i,j] : 0 <= i <= 10 }";
4350 V = "{ }";
4351 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4352 S = "{ S[i, j] -> [j, i] }";
4353 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4354 if (test_special_schedule(ctx, D, V, P, S) < 0)
4355 return -1;
4356 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4357 if (test_special_schedule(ctx, D, V, P, S) < 0)
4358 return -1;
4360 D = "{ A[a]; B[] }";
4361 V = "{}";
4362 P = "{ A[a] -> B[] }";
4363 if (test_has_schedule(ctx, D, V, P) < 0)
4364 return -1;
4366 if (test_padded_schedule(ctx) < 0)
4367 return -1;
4369 /* Check that check for progress is not confused by rational
4370 * solution.
4372 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4373 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4374 "i0 <= -2 + N; "
4375 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4376 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4377 P = "{}";
4378 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4379 if (test_has_schedule(ctx, D, V, P) < 0)
4380 return -1;
4381 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4383 /* Check that we allow schedule rows that are only non-trivial
4384 * on some full-dimensional domains.
4386 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4387 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4388 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4389 P = "{}";
4390 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4391 if (test_has_schedule(ctx, D, V, P) < 0)
4392 return -1;
4393 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4395 if (test_conditional_schedule_constraints(ctx) < 0)
4396 return -1;
4398 if (test_strongly_satisfying_schedule(ctx) < 0)
4399 return -1;
4401 if (test_conflicting_context_schedule(ctx) < 0)
4402 return -1;
4404 if (test_bounded_coefficients_schedule(ctx) < 0)
4405 return -1;
4406 if (test_coalescing_schedule(ctx) < 0)
4407 return -1;
4408 if (test_skewing_schedule(ctx) < 0)
4409 return -1;
4411 return 0;
4414 /* Perform scheduling tests using the whole component scheduler.
4416 static int test_schedule_whole(isl_ctx *ctx)
4418 int whole;
4419 int r;
4421 whole = isl_options_get_schedule_whole_component(ctx);
4422 isl_options_set_schedule_whole_component(ctx, 1);
4423 r = test_schedule(ctx);
4424 if (r >= 0)
4425 r = test_bounded_coefficients_schedule_whole(ctx);
4426 isl_options_set_schedule_whole_component(ctx, whole);
4428 return r;
4431 /* Perform scheduling tests using the incremental scheduler.
4433 static int test_schedule_incremental(isl_ctx *ctx)
4435 int whole;
4436 int r;
4438 whole = isl_options_get_schedule_whole_component(ctx);
4439 isl_options_set_schedule_whole_component(ctx, 0);
4440 r = test_schedule(ctx);
4441 isl_options_set_schedule_whole_component(ctx, whole);
4443 return r;
4446 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4448 isl_union_map *umap;
4449 int test;
4451 umap = isl_union_map_read_from_str(ctx, str);
4452 test = isl_union_map_plain_is_injective(umap);
4453 isl_union_map_free(umap);
4454 if (test < 0)
4455 return -1;
4456 if (test == injective)
4457 return 0;
4458 if (injective)
4459 isl_die(ctx, isl_error_unknown,
4460 "map not detected as injective", return -1);
4461 else
4462 isl_die(ctx, isl_error_unknown,
4463 "map detected as injective", return -1);
4466 int test_injective(isl_ctx *ctx)
4468 const char *str;
4470 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4471 return -1;
4472 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4473 return -1;
4474 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4475 return -1;
4476 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4477 return -1;
4478 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4479 return -1;
4480 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4481 return -1;
4482 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4483 return -1;
4484 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4485 return -1;
4487 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4488 if (test_plain_injective(ctx, str, 1))
4489 return -1;
4490 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4491 if (test_plain_injective(ctx, str, 0))
4492 return -1;
4494 return 0;
4497 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4499 isl_aff *aff2;
4500 int equal;
4502 if (!aff)
4503 return -1;
4505 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4506 equal = isl_aff_plain_is_equal(aff, aff2);
4507 isl_aff_free(aff2);
4509 return equal;
4512 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4514 int equal;
4516 equal = aff_plain_is_equal(aff, str);
4517 if (equal < 0)
4518 return -1;
4519 if (!equal)
4520 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4521 "result not as expected", return -1);
4522 return 0;
4525 /* Is "upa" obviously equal to the isl_union_pw_aff represented by "str"?
4527 static isl_bool union_pw_aff_plain_is_equal(__isl_keep isl_union_pw_aff *upa,
4528 const char *str)
4530 isl_ctx *ctx;
4531 isl_union_pw_aff *upa2;
4532 isl_bool equal;
4534 if (!upa)
4535 return isl_bool_error;
4537 ctx = isl_union_pw_aff_get_ctx(upa);
4538 upa2 = isl_union_pw_aff_read_from_str(ctx, str);
4539 equal = isl_union_pw_aff_plain_is_equal(upa, upa2);
4540 isl_union_pw_aff_free(upa2);
4542 return equal;
4545 /* Check that "upa" is obviously equal to the isl_union_pw_aff
4546 * represented by "str".
4548 static isl_stat union_pw_aff_check_plain_equal(__isl_keep isl_union_pw_aff *upa,
4549 const char *str)
4551 isl_bool equal;
4553 equal = union_pw_aff_plain_is_equal(upa, str);
4554 if (equal < 0)
4555 return isl_stat_error;
4556 if (!equal)
4557 isl_die(isl_union_pw_aff_get_ctx(upa), isl_error_unknown,
4558 "result not as expected", return isl_stat_error);
4559 return isl_stat_ok;
4562 /* Basic tests on isl_union_pw_aff.
4564 * In particular, check that isl_union_pw_aff_aff_on_domain
4565 * aligns the parameters of the input objects and
4566 * that isl_union_pw_aff_param_on_domain_id properly
4567 * introduces the parameter.
4569 static int test_upa(isl_ctx *ctx)
4571 const char *str;
4572 isl_id *id;
4573 isl_aff *aff;
4574 isl_union_set *domain;
4575 isl_union_pw_aff *upa;
4576 isl_stat ok;
4578 aff = isl_aff_read_from_str(ctx, "[N] -> { [N] }");
4579 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
4580 domain = isl_union_set_read_from_str(ctx, str);
4581 upa = isl_union_pw_aff_aff_on_domain(domain, aff);
4582 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
4583 ok = union_pw_aff_check_plain_equal(upa, str);
4584 isl_union_pw_aff_free(upa);
4585 if (ok < 0)
4586 return -1;
4588 id = isl_id_alloc(ctx, "N", NULL);
4589 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
4590 domain = isl_union_set_read_from_str(ctx, str);
4591 upa = isl_union_pw_aff_param_on_domain_id(domain, id);
4592 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
4593 ok = union_pw_aff_check_plain_equal(upa, str);
4594 isl_union_pw_aff_free(upa);
4595 if (ok < 0)
4596 return -1;
4598 return 0;
4601 struct {
4602 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4603 __isl_take isl_aff *aff2);
4604 } aff_bin_op[] = {
4605 ['+'] = { &isl_aff_add },
4606 ['-'] = { &isl_aff_sub },
4607 ['*'] = { &isl_aff_mul },
4608 ['/'] = { &isl_aff_div },
4611 struct {
4612 const char *arg1;
4613 unsigned char op;
4614 const char *arg2;
4615 const char *res;
4616 } aff_bin_tests[] = {
4617 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
4618 "{ [i] -> [2i] }" },
4619 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
4620 "{ [i] -> [0] }" },
4621 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
4622 "{ [i] -> [2i] }" },
4623 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
4624 "{ [i] -> [2i] }" },
4625 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
4626 "{ [i] -> [i/2] }" },
4627 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
4628 "{ [i] -> [i] }" },
4629 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
4630 "{ [i] -> [NaN] }" },
4631 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
4632 "{ [i] -> [NaN] }" },
4633 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
4634 "{ [i] -> [NaN] }" },
4635 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
4636 "{ [i] -> [NaN] }" },
4637 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
4638 "{ [i] -> [NaN] }" },
4639 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
4640 "{ [i] -> [NaN] }" },
4641 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
4642 "{ [i] -> [NaN] }" },
4643 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
4644 "{ [i] -> [NaN] }" },
4645 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
4646 "{ [i] -> [NaN] }" },
4647 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
4648 "{ [i] -> [NaN] }" },
4649 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
4650 "{ [i] -> [NaN] }" },
4651 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
4652 "{ [i] -> [NaN] }" },
4655 /* Perform some basic tests of binary operations on isl_aff objects.
4657 static int test_bin_aff(isl_ctx *ctx)
4659 int i;
4660 isl_aff *aff1, *aff2, *res;
4661 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4662 __isl_take isl_aff *aff2);
4663 int ok;
4665 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
4666 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
4667 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
4668 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
4669 fn = aff_bin_op[aff_bin_tests[i].op].fn;
4670 aff1 = fn(aff1, aff2);
4671 if (isl_aff_is_nan(res))
4672 ok = isl_aff_is_nan(aff1);
4673 else
4674 ok = isl_aff_plain_is_equal(aff1, res);
4675 isl_aff_free(aff1);
4676 isl_aff_free(res);
4677 if (ok < 0)
4678 return -1;
4679 if (!ok)
4680 isl_die(ctx, isl_error_unknown,
4681 "unexpected result", return -1);
4684 return 0;
4687 struct {
4688 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
4689 __isl_take isl_pw_aff *pa2);
4690 } pw_aff_bin_op[] = {
4691 ['m'] = { &isl_pw_aff_min },
4692 ['M'] = { &isl_pw_aff_max },
4695 /* Inputs for binary isl_pw_aff operation tests.
4696 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
4697 * defined by pw_aff_bin_op, and "res" is the expected result.
4699 struct {
4700 const char *arg1;
4701 unsigned char op;
4702 const char *arg2;
4703 const char *res;
4704 } pw_aff_bin_tests[] = {
4705 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
4706 "{ [i] -> [i] }" },
4707 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
4708 "{ [i] -> [i] }" },
4709 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
4710 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
4711 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
4712 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
4713 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
4714 "{ [i] -> [NaN] }" },
4715 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
4716 "{ [i] -> [NaN] }" },
4719 /* Perform some basic tests of binary operations on isl_pw_aff objects.
4721 static int test_bin_pw_aff(isl_ctx *ctx)
4723 int i;
4724 isl_bool ok;
4725 isl_pw_aff *pa1, *pa2, *res;
4727 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
4728 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
4729 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
4730 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
4731 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
4732 if (isl_pw_aff_involves_nan(res))
4733 ok = isl_pw_aff_involves_nan(pa1);
4734 else
4735 ok = isl_pw_aff_plain_is_equal(pa1, res);
4736 isl_pw_aff_free(pa1);
4737 isl_pw_aff_free(res);
4738 if (ok < 0)
4739 return -1;
4740 if (!ok)
4741 isl_die(ctx, isl_error_unknown,
4742 "unexpected result", return -1);
4745 return 0;
4748 struct {
4749 __isl_give isl_union_pw_multi_aff *(*fn)(
4750 __isl_take isl_union_pw_multi_aff *upma1,
4751 __isl_take isl_union_pw_multi_aff *upma2);
4752 const char *arg1;
4753 const char *arg2;
4754 const char *res;
4755 } upma_bin_tests[] = {
4756 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
4757 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
4758 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
4759 "{ B[x] -> [2] : x >= 0 }",
4760 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
4761 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
4762 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
4763 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
4764 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
4765 "D[i] -> B[2] : i >= 5 }" },
4766 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4767 "{ B[x] -> C[2] : x > 0 }",
4768 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
4769 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4770 "{ B[x] -> A[2] : x >= 0 }",
4771 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
4774 /* Perform some basic tests of binary operations on
4775 * isl_union_pw_multi_aff objects.
4777 static int test_bin_upma(isl_ctx *ctx)
4779 int i;
4780 isl_union_pw_multi_aff *upma1, *upma2, *res;
4781 int ok;
4783 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
4784 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4785 upma_bin_tests[i].arg1);
4786 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4787 upma_bin_tests[i].arg2);
4788 res = isl_union_pw_multi_aff_read_from_str(ctx,
4789 upma_bin_tests[i].res);
4790 upma1 = upma_bin_tests[i].fn(upma1, upma2);
4791 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
4792 isl_union_pw_multi_aff_free(upma1);
4793 isl_union_pw_multi_aff_free(res);
4794 if (ok < 0)
4795 return -1;
4796 if (!ok)
4797 isl_die(ctx, isl_error_unknown,
4798 "unexpected result", return -1);
4801 return 0;
4804 struct {
4805 __isl_give isl_union_pw_multi_aff *(*fn)(
4806 __isl_take isl_union_pw_multi_aff *upma1,
4807 __isl_take isl_union_pw_multi_aff *upma2);
4808 const char *arg1;
4809 const char *arg2;
4810 } upma_bin_fail_tests[] = {
4811 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4812 "{ B[x] -> C[2] : x >= 0 }" },
4815 /* Perform some basic tests of binary operations on
4816 * isl_union_pw_multi_aff objects that are expected to fail.
4818 static int test_bin_upma_fail(isl_ctx *ctx)
4820 int i, n;
4821 isl_union_pw_multi_aff *upma1, *upma2;
4822 int on_error;
4824 on_error = isl_options_get_on_error(ctx);
4825 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
4826 n = ARRAY_SIZE(upma_bin_fail_tests);
4827 for (i = 0; i < n; ++i) {
4828 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4829 upma_bin_fail_tests[i].arg1);
4830 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4831 upma_bin_fail_tests[i].arg2);
4832 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
4833 isl_union_pw_multi_aff_free(upma1);
4834 if (upma1)
4835 break;
4837 isl_options_set_on_error(ctx, on_error);
4838 if (i < n)
4839 isl_die(ctx, isl_error_unknown,
4840 "operation not expected to succeed", return -1);
4842 return 0;
4845 /* Inputs for basic tests of unary operations on isl_multi_pw_aff objects.
4846 * "fn" is the function that is tested.
4847 * "arg" is a string description of the input.
4848 * "res" is a string description of the expected result.
4850 struct {
4851 __isl_give isl_multi_pw_aff *(*fn)(__isl_take isl_multi_pw_aff *mpa);
4852 const char *arg;
4853 const char *res;
4854 } mpa_un_tests[] = {
4855 { &isl_multi_pw_aff_range_factor_domain,
4856 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
4857 "{ A[x] -> B[(1 : x >= 5)] }" },
4858 { &isl_multi_pw_aff_range_factor_range,
4859 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
4860 "{ A[y] -> C[(2 : y <= 10)] }" },
4861 { &isl_multi_pw_aff_range_factor_domain,
4862 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
4863 "{ A[x] -> B[(1 : x >= 5)] }" },
4864 { &isl_multi_pw_aff_range_factor_range,
4865 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
4866 "{ A[y] -> C[] }" },
4867 { &isl_multi_pw_aff_range_factor_domain,
4868 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
4869 "{ A[x] -> B[] }" },
4870 { &isl_multi_pw_aff_range_factor_range,
4871 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
4872 "{ A[y] -> C[(2 : y <= 10)] }" },
4873 { &isl_multi_pw_aff_range_factor_domain,
4874 "{ A[x] -> [B[] -> C[]] }",
4875 "{ A[x] -> B[] }" },
4876 { &isl_multi_pw_aff_range_factor_range,
4877 "{ A[x] -> [B[] -> C[]] }",
4878 "{ A[y] -> C[] }" },
4879 { &isl_multi_pw_aff_factor_range,
4880 "{ [B[] -> C[]] }",
4881 "{ C[] }" },
4884 /* Perform some basic tests of unary operations on isl_multi_pw_aff objects.
4886 static int test_un_mpa(isl_ctx *ctx)
4888 int i;
4889 isl_bool ok;
4890 isl_multi_pw_aff *mpa, *res;
4892 for (i = 0; i < ARRAY_SIZE(mpa_un_tests); ++i) {
4893 mpa = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].arg);
4894 res = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].res);
4895 mpa = mpa_un_tests[i].fn(mpa);
4896 ok = isl_multi_pw_aff_plain_is_equal(mpa, res);
4897 isl_multi_pw_aff_free(mpa);
4898 isl_multi_pw_aff_free(res);
4899 if (ok < 0)
4900 return -1;
4901 if (!ok)
4902 isl_die(ctx, isl_error_unknown,
4903 "unexpected result", return -1);
4906 return 0;
4909 /* Inputs for basic tests of binary operations on isl_multi_pw_aff objects.
4910 * "fn" is the function that is tested.
4911 * "arg1" and "arg2" are string descriptions of the inputs.
4912 * "res" is a string description of the expected result.
4914 struct {
4915 __isl_give isl_multi_pw_aff *(*fn)(
4916 __isl_take isl_multi_pw_aff *mpa1,
4917 __isl_take isl_multi_pw_aff *mpa2);
4918 const char *arg1;
4919 const char *arg2;
4920 const char *res;
4921 } mpa_bin_tests[] = {
4922 { &isl_multi_pw_aff_add, "{ A[] -> [1] }", "{ A[] -> [2] }",
4923 "{ A[] -> [3] }" },
4924 { &isl_multi_pw_aff_add, "{ A[x] -> [(1 : x >= 5)] }",
4925 "{ A[x] -> [(x : x <= 10)] }",
4926 "{ A[x] -> [(1 + x : 5 <= x <= 10)] }" },
4927 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
4928 "{ A[y] -> C[(2 : y <= 10)] }",
4929 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
4930 { &isl_multi_pw_aff_range_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
4931 "{ A[] -> [B[1] -> C[2]] }" },
4932 { &isl_multi_pw_aff_range_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
4933 "{ A[] -> [B[] -> C[]] }" },
4934 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
4935 "{ A[y] -> C[(2 : y <= 10)] }",
4936 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[(2 : y <= 10)]] }" },
4937 { &isl_multi_pw_aff_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
4938 "{ [A[] -> A[]] -> [B[1] -> C[2]] }" },
4939 { &isl_multi_pw_aff_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
4940 "{ [A[] -> A[]] -> [B[] -> C[]] }" },
4941 { &isl_multi_pw_aff_pullback_multi_pw_aff,
4942 "{ B[i,j] -> C[i + 2j] }", "{ A[a,b] -> B[b,a] }",
4943 "{ A[a,b] -> C[b + 2a] }" },
4944 { &isl_multi_pw_aff_pullback_multi_pw_aff,
4945 "{ B[i,j] -> C[i + 2j] }",
4946 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
4947 "{ A[a,b] -> C[(b + 2a : b > a)] }" },
4948 { &isl_multi_pw_aff_pullback_multi_pw_aff,
4949 "{ B[i,j] -> C[(i + 2j : j > 4)] }",
4950 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
4951 "{ A[a,b] -> C[(b + 2a : b > a > 4)] }" },
4954 /* Perform some basic tests of binary operations on isl_multi_pw_aff objects.
4956 static int test_bin_mpa(isl_ctx *ctx)
4958 int i;
4959 isl_bool ok;
4960 isl_multi_pw_aff *mpa1, *mpa2, *res;
4962 for (i = 0; i < ARRAY_SIZE(mpa_bin_tests); ++i) {
4963 mpa1 = isl_multi_pw_aff_read_from_str(ctx,
4964 mpa_bin_tests[i].arg1);
4965 mpa2 = isl_multi_pw_aff_read_from_str(ctx,
4966 mpa_bin_tests[i].arg2);
4967 res = isl_multi_pw_aff_read_from_str(ctx,
4968 mpa_bin_tests[i].res);
4969 mpa1 = mpa_bin_tests[i].fn(mpa1, mpa2);
4970 ok = isl_multi_pw_aff_plain_is_equal(mpa1, res);
4971 isl_multi_pw_aff_free(mpa1);
4972 isl_multi_pw_aff_free(res);
4973 if (ok < 0)
4974 return -1;
4975 if (!ok)
4976 isl_die(ctx, isl_error_unknown,
4977 "unexpected result", return -1);
4980 return 0;
4983 /* Inputs for basic tests of unary operations on
4984 * isl_multi_union_pw_aff objects.
4985 * "fn" is the function that is tested.
4986 * "arg" is a string description of the input.
4987 * "res" is a string description of the expected result.
4989 struct {
4990 __isl_give isl_multi_union_pw_aff *(*fn)(
4991 __isl_take isl_multi_union_pw_aff *mupa);
4992 const char *arg;
4993 const char *res;
4994 } mupa_un_tests[] = {
4995 { &isl_multi_union_pw_aff_factor_range,
4996 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]",
4997 "C[{ A[] -> [2] }]" },
4998 { &isl_multi_union_pw_aff_factor_range,
4999 "[B[] -> C[{ A[] -> [2] }]]",
5000 "C[{ A[] -> [2] }]" },
5001 { &isl_multi_union_pw_aff_factor_range,
5002 "[B[{ A[] -> [1] }] -> C[]]",
5003 "C[]" },
5004 { &isl_multi_union_pw_aff_factor_range,
5005 "[B[] -> C[]]",
5006 "C[]" },
5009 /* Perform some basic tests of unary operations on
5010 * isl_multi_union_pw_aff objects.
5012 static int test_un_mupa(isl_ctx *ctx)
5014 int i;
5015 isl_bool ok;
5016 isl_multi_union_pw_aff *mupa, *res;
5018 for (i = 0; i < ARRAY_SIZE(mupa_un_tests); ++i) {
5019 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5020 mupa_un_tests[i].arg);
5021 res = isl_multi_union_pw_aff_read_from_str(ctx,
5022 mupa_un_tests[i].res);
5023 mupa = mupa_un_tests[i].fn(mupa);
5024 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5025 isl_multi_union_pw_aff_free(mupa);
5026 isl_multi_union_pw_aff_free(res);
5027 if (ok < 0)
5028 return -1;
5029 if (!ok)
5030 isl_die(ctx, isl_error_unknown,
5031 "unexpected result", return -1);
5034 return 0;
5037 /* Inputs for basic tests of binary operations on
5038 * isl_multi_union_pw_aff objects.
5039 * "fn" is the function that is tested.
5040 * "arg1" and "arg2" are string descriptions of the inputs.
5041 * "res" is a string description of the expected result.
5043 struct {
5044 __isl_give isl_multi_union_pw_aff *(*fn)(
5045 __isl_take isl_multi_union_pw_aff *mupa1,
5046 __isl_take isl_multi_union_pw_aff *mupa2);
5047 const char *arg1;
5048 const char *arg2;
5049 const char *res;
5050 } mupa_bin_tests[] = {
5051 { &isl_multi_union_pw_aff_add, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5052 "[{ A[] -> [3] }]" },
5053 { &isl_multi_union_pw_aff_sub, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5054 "[{ A[] -> [-1] }]" },
5055 { &isl_multi_union_pw_aff_add,
5056 "[{ A[] -> [1]; B[] -> [4] }]",
5057 "[{ A[] -> [2]; C[] -> [5] }]",
5058 "[{ A[] -> [3] }]" },
5059 { &isl_multi_union_pw_aff_union_add,
5060 "[{ A[] -> [1]; B[] -> [4] }]",
5061 "[{ A[] -> [2]; C[] -> [5] }]",
5062 "[{ A[] -> [3]; B[] -> [4]; C[] -> [5] }]" },
5063 { &isl_multi_union_pw_aff_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5064 "[{ A[x] -> [(x)] : x <= 10 }]",
5065 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10 }]" },
5066 { &isl_multi_union_pw_aff_union_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5067 "[{ A[x] -> [(x)] : x <= 10 }]",
5068 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10; "
5069 "A[x] -> [(1)] : x > 10; A[x] -> [(x)] : x < 5 }]" },
5070 { &isl_multi_union_pw_aff_range_product,
5071 "B[{ A[] -> [1] }]",
5072 "C[{ A[] -> [2] }]",
5073 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]" },
5074 { &isl_multi_union_pw_aff_range_product,
5075 "B[{ A[] -> [1]; D[] -> [3] }]",
5076 "C[{ A[] -> [2] }]",
5077 "[B[{ A[] -> [1]; D[] -> [3] }] -> C[{ A[] -> [2] }]]" },
5080 /* Perform some basic tests of binary operations on
5081 * isl_multi_union_pw_aff objects.
5083 static int test_bin_mupa(isl_ctx *ctx)
5085 int i;
5086 isl_bool ok;
5087 isl_multi_union_pw_aff *mupa1, *mupa2, *res;
5089 for (i = 0; i < ARRAY_SIZE(mupa_bin_tests); ++i) {
5090 mupa1 = isl_multi_union_pw_aff_read_from_str(ctx,
5091 mupa_bin_tests[i].arg1);
5092 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx,
5093 mupa_bin_tests[i].arg2);
5094 res = isl_multi_union_pw_aff_read_from_str(ctx,
5095 mupa_bin_tests[i].res);
5096 mupa1 = mupa_bin_tests[i].fn(mupa1, mupa2);
5097 ok = isl_multi_union_pw_aff_plain_is_equal(mupa1, res);
5098 isl_multi_union_pw_aff_free(mupa1);
5099 isl_multi_union_pw_aff_free(res);
5100 if (ok < 0)
5101 return -1;
5102 if (!ok)
5103 isl_die(ctx, isl_error_unknown,
5104 "unexpected result", return -1);
5107 return 0;
5110 /* Inputs for basic tests of binary operations on
5111 * pairs of isl_multi_union_pw_aff and isl_set objects.
5112 * "fn" is the function that is tested.
5113 * "arg1" and "arg2" are string descriptions of the inputs.
5114 * "res" is a string description of the expected result.
5116 struct {
5117 __isl_give isl_multi_union_pw_aff *(*fn)(
5118 __isl_take isl_multi_union_pw_aff *mupa,
5119 __isl_take isl_set *set);
5120 const char *arg1;
5121 const char *arg2;
5122 const char *res;
5123 } mupa_set_tests[] = {
5124 { &isl_multi_union_pw_aff_intersect_range,
5125 "C[{ B[i,j] -> [i + 2j] }]", "{ C[1] }",
5126 "C[{ B[i,j] -> [i + 2j] : i + 2j = 1 }]" },
5127 { &isl_multi_union_pw_aff_intersect_range,
5128 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[N] }",
5129 "[N] -> C[{ B[i,j] -> [i + 2j] : i + 2j = N }]" },
5130 { &isl_multi_union_pw_aff_intersect_range,
5131 "[N] -> C[{ B[i,j] -> [i + 2j + N] }]", "{ C[1] }",
5132 "[N] -> C[{ B[i,j] -> [i + 2j + N] : i + 2j + N = 1 }]" },
5133 { &isl_multi_union_pw_aff_intersect_range,
5134 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[x] : N >= 0 }",
5135 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0 }]" },
5136 { &isl_multi_union_pw_aff_intersect_params,
5137 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { : N >= 0 }",
5138 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0}]" },
5139 { &isl_multi_union_pw_aff_intersect_params,
5140 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "[N] -> { : N >= 0 }",
5141 "[N] -> C[{ B[i,j] -> [i + 2j] : 0 <= N <= 256 }]" },
5142 { &isl_multi_union_pw_aff_intersect_params,
5143 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "{ : }",
5144 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]" },
5147 /* Perform some basic tests of binary operations on
5148 * pairs of isl_multi_union_pw_aff and isl_set objects.
5150 static int test_mupa_set(isl_ctx *ctx)
5152 int i;
5153 isl_bool ok;
5154 isl_multi_union_pw_aff *mupa, *res;
5155 isl_set *set;
5157 for (i = 0; i < ARRAY_SIZE(mupa_set_tests); ++i) {
5158 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5159 mupa_set_tests[i].arg1);
5160 set = isl_set_read_from_str(ctx, mupa_set_tests[i].arg2);
5161 res = isl_multi_union_pw_aff_read_from_str(ctx,
5162 mupa_set_tests[i].res);
5163 mupa = mupa_set_tests[i].fn(mupa, set);
5164 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5165 isl_multi_union_pw_aff_free(mupa);
5166 isl_multi_union_pw_aff_free(res);
5167 if (ok < 0)
5168 return -1;
5169 if (!ok)
5170 isl_die(ctx, isl_error_unknown,
5171 "unexpected result", return -1);
5174 return 0;
5177 /* Inputs for basic tests of binary operations on
5178 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5179 * "fn" is the function that is tested.
5180 * "arg1" and "arg2" are string descriptions of the inputs.
5181 * "res" is a string description of the expected result.
5183 struct {
5184 __isl_give isl_multi_union_pw_aff *(*fn)(
5185 __isl_take isl_multi_union_pw_aff *mupa,
5186 __isl_take isl_union_set *uset);
5187 const char *arg1;
5188 const char *arg2;
5189 const char *res;
5190 } mupa_uset_tests[] = {
5191 { &isl_multi_union_pw_aff_intersect_domain,
5192 "C[{ B[i,j] -> [i + 2j] }]", "{ B[i,i] }",
5193 "C[{ B[i,i] -> [3i] }]" },
5196 /* Perform some basic tests of binary operations on
5197 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5199 static int test_mupa_uset(isl_ctx *ctx)
5201 int i;
5202 isl_bool ok;
5203 isl_multi_union_pw_aff *mupa, *res;
5204 isl_union_set *uset;
5206 for (i = 0; i < ARRAY_SIZE(mupa_uset_tests); ++i) {
5207 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5208 mupa_uset_tests[i].arg1);
5209 uset = isl_union_set_read_from_str(ctx,
5210 mupa_uset_tests[i].arg2);
5211 res = isl_multi_union_pw_aff_read_from_str(ctx,
5212 mupa_uset_tests[i].res);
5213 mupa = mupa_uset_tests[i].fn(mupa, uset);
5214 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5215 isl_multi_union_pw_aff_free(mupa);
5216 isl_multi_union_pw_aff_free(res);
5217 if (ok < 0)
5218 return -1;
5219 if (!ok)
5220 isl_die(ctx, isl_error_unknown,
5221 "unexpected result", return -1);
5224 return 0;
5227 /* Inputs for basic tests of binary operations on
5228 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5229 * "fn" is the function that is tested.
5230 * "arg1" and "arg2" are string descriptions of the inputs.
5231 * "res" is a string description of the expected result.
5233 struct {
5234 __isl_give isl_multi_union_pw_aff *(*fn)(
5235 __isl_take isl_multi_union_pw_aff *mupa,
5236 __isl_take isl_multi_aff *ma);
5237 const char *arg1;
5238 const char *arg2;
5239 const char *res;
5240 } mupa_ma_tests[] = {
5241 { &isl_multi_union_pw_aff_apply_multi_aff,
5242 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5243 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5244 "{ C[a,b] -> D[b,a] }",
5245 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
5246 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
5247 { &isl_multi_union_pw_aff_apply_multi_aff,
5248 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
5249 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5250 "{ C[a,b] -> D[b,a] }",
5251 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
5252 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
5253 { &isl_multi_union_pw_aff_apply_multi_aff,
5254 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5255 "[N] -> { C[a] -> D[a + N] }",
5256 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }] " },
5257 { &isl_multi_union_pw_aff_apply_multi_aff,
5258 "C[]",
5259 "{ C[] -> D[] }",
5260 "D[]" },
5263 /* Perform some basic tests of binary operations on
5264 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5266 static int test_mupa_ma(isl_ctx *ctx)
5268 int i;
5269 isl_bool ok;
5270 isl_multi_union_pw_aff *mupa, *res;
5271 isl_multi_aff *ma;
5273 for (i = 0; i < ARRAY_SIZE(mupa_ma_tests); ++i) {
5274 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5275 mupa_ma_tests[i].arg1);
5276 ma = isl_multi_aff_read_from_str(ctx, mupa_ma_tests[i].arg2);
5277 res = isl_multi_union_pw_aff_read_from_str(ctx,
5278 mupa_ma_tests[i].res);
5279 mupa = mupa_ma_tests[i].fn(mupa, ma);
5280 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5281 isl_multi_union_pw_aff_free(mupa);
5282 isl_multi_union_pw_aff_free(res);
5283 if (ok < 0)
5284 return -1;
5285 if (!ok)
5286 isl_die(ctx, isl_error_unknown,
5287 "unexpected result", return -1);
5290 return 0;
5293 /* Inputs for basic tests of binary operations on
5294 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
5295 * "fn" is the function that is tested.
5296 * "arg1" and "arg2" are string descriptions of the inputs.
5297 * "res" is a string description of the expected result.
5299 struct {
5300 __isl_give isl_union_pw_aff *(*fn)(
5301 __isl_take isl_multi_union_pw_aff *mupa,
5302 __isl_take isl_pw_aff *pa);
5303 const char *arg1;
5304 const char *arg2;
5305 const char *res;
5306 } mupa_pa_tests[] = {
5307 { &isl_multi_union_pw_aff_apply_pw_aff,
5308 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5309 "[N] -> { C[a] -> [a + N] }",
5310 "[N] -> { A[i,j] -> [i + N]; B[i,j] -> [j + N] }" },
5311 { &isl_multi_union_pw_aff_apply_pw_aff,
5312 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5313 "{ C[a] -> [a] : a >= 0; C[a] -> [-a] : a < 0 }",
5314 "{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
5315 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }" },
5318 /* Perform some basic tests of binary operations on
5319 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
5321 static int test_mupa_pa(isl_ctx *ctx)
5323 int i;
5324 isl_bool ok;
5325 isl_multi_union_pw_aff *mupa;
5326 isl_union_pw_aff *upa, *res;
5327 isl_pw_aff *pa;
5329 for (i = 0; i < ARRAY_SIZE(mupa_pa_tests); ++i) {
5330 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5331 mupa_pa_tests[i].arg1);
5332 pa = isl_pw_aff_read_from_str(ctx, mupa_pa_tests[i].arg2);
5333 res = isl_union_pw_aff_read_from_str(ctx,
5334 mupa_pa_tests[i].res);
5335 upa = mupa_pa_tests[i].fn(mupa, pa);
5336 ok = isl_union_pw_aff_plain_is_equal(upa, res);
5337 isl_union_pw_aff_free(upa);
5338 isl_union_pw_aff_free(res);
5339 if (ok < 0)
5340 return -1;
5341 if (!ok)
5342 isl_die(ctx, isl_error_unknown,
5343 "unexpected result", return -1);
5346 return 0;
5349 /* Inputs for basic tests of binary operations on
5350 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
5351 * "fn" is the function that is tested.
5352 * "arg1" and "arg2" are string descriptions of the inputs.
5353 * "res" is a string description of the expected result.
5355 struct {
5356 __isl_give isl_multi_union_pw_aff *(*fn)(
5357 __isl_take isl_multi_union_pw_aff *mupa,
5358 __isl_take isl_pw_multi_aff *pma);
5359 const char *arg1;
5360 const char *arg2;
5361 const char *res;
5362 } mupa_pma_tests[] = {
5363 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5364 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5365 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5366 "{ C[a,b] -> D[b,a] }",
5367 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
5368 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
5369 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5370 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
5371 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5372 "{ C[a,b] -> D[b,a] }",
5373 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
5374 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
5375 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5376 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5377 "[N] -> { C[a] -> D[a + N] }",
5378 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }]" },
5379 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5380 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5381 "{ C[a] -> D[a] : a >= 0; C[a] -> D[-a] : a < 0 }",
5382 "D[{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
5383 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }]" },
5384 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5385 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5386 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5387 "{ C[a,b] -> D[a,b] : a >= b; C[a,b] -> D[b,a] : a < b }",
5388 "D[{ A[i,j] -> [i] : i >= j; A[i,j] -> [j] : i < j; "
5389 "B[i,j] -> [j] : i <= j; B[i,j] -> [i] : i > j }, "
5390 "{ A[i,j] -> [j] : i >= j; A[i,j] -> [i] : i < j; "
5391 "B[i,j] -> [i] : i <= j; B[i,j] -> [j] : i > j }]" },
5392 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
5393 "C[]",
5394 "{ C[] -> D[] }",
5395 "D[]" },
5398 /* Perform some basic tests of binary operations on
5399 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
5401 static int test_mupa_pma(isl_ctx *ctx)
5403 int i;
5404 isl_bool ok;
5405 isl_multi_union_pw_aff *mupa, *res;
5406 isl_pw_multi_aff *pma;
5408 for (i = 0; i < ARRAY_SIZE(mupa_pma_tests); ++i) {
5409 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5410 mupa_pma_tests[i].arg1);
5411 pma = isl_pw_multi_aff_read_from_str(ctx,
5412 mupa_pma_tests[i].arg2);
5413 res = isl_multi_union_pw_aff_read_from_str(ctx,
5414 mupa_pma_tests[i].res);
5415 mupa = mupa_pma_tests[i].fn(mupa, pma);
5416 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5417 isl_multi_union_pw_aff_free(mupa);
5418 isl_multi_union_pw_aff_free(res);
5419 if (ok < 0)
5420 return -1;
5421 if (!ok)
5422 isl_die(ctx, isl_error_unknown,
5423 "unexpected result", return -1);
5426 return 0;
5429 /* Inputs for basic tests of binary operations on
5430 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
5431 * "fn" is the function that is tested.
5432 * "arg1" and "arg2" are string descriptions of the inputs.
5433 * "res" is a string description of the expected result.
5435 struct {
5436 __isl_give isl_multi_union_pw_aff *(*fn)(
5437 __isl_take isl_multi_union_pw_aff *mupa,
5438 __isl_take isl_union_pw_multi_aff *upma);
5439 const char *arg1;
5440 const char *arg2;
5441 const char *res;
5442 } mupa_upma_tests[] = {
5443 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
5444 "C[{ B[i,j] -> [i + 2j] }]", "{ A[a,b] -> B[b,a] }",
5445 "C[{ A[a,b] -> [b + 2a] }]" },
5446 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
5447 "C[{ B[i,j] -> [i + 2j] }]",
5448 "{ A[a,b] -> B[b,a] : b > a }",
5449 "C[{ A[a,b] -> [b + 2a] : b > a }]" },
5450 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
5451 "C[{ B[i,j] -> [i + 2j] : j > 4 }]",
5452 "{ A[a,b] -> B[b,a] : b > a }",
5453 "C[{ A[a,b] -> [b + 2a] : b > a > 4 }]" },
5454 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
5455 "C[{ B[i,j] -> [i + 2j] }]",
5456 "{ A[a,b] -> B[b,a] : a > b; A[a,b] -> B[a,b] : a <= b }",
5457 "C[{ A[a,b] -> [b + 2a] : a > b; A[a,b] -> [a + 2b] : a <= b }]" },
5460 /* Perform some basic tests of binary operations on
5461 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
5463 static int test_mupa_upma(isl_ctx *ctx)
5465 int i;
5466 isl_bool ok;
5467 isl_multi_union_pw_aff *mupa, *res;
5468 isl_union_pw_multi_aff *upma;
5470 for (i = 0; i < ARRAY_SIZE(mupa_upma_tests); ++i) {
5471 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5472 mupa_upma_tests[i].arg1);
5473 upma = isl_union_pw_multi_aff_read_from_str(ctx,
5474 mupa_upma_tests[i].arg2);
5475 res = isl_multi_union_pw_aff_read_from_str(ctx,
5476 mupa_upma_tests[i].res);
5477 mupa = mupa_upma_tests[i].fn(mupa, upma);
5478 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5479 isl_multi_union_pw_aff_free(mupa);
5480 isl_multi_union_pw_aff_free(res);
5481 if (ok < 0)
5482 return -1;
5483 if (!ok)
5484 isl_die(ctx, isl_error_unknown,
5485 "unexpected result", return -1);
5488 return 0;
5491 int test_aff(isl_ctx *ctx)
5493 const char *str;
5494 isl_set *set;
5495 isl_space *space;
5496 isl_local_space *ls;
5497 isl_aff *aff;
5498 int zero, equal;
5500 if (test_upa(ctx) < 0)
5501 return -1;
5502 if (test_bin_aff(ctx) < 0)
5503 return -1;
5504 if (test_bin_pw_aff(ctx) < 0)
5505 return -1;
5506 if (test_bin_upma(ctx) < 0)
5507 return -1;
5508 if (test_bin_upma_fail(ctx) < 0)
5509 return -1;
5510 if (test_un_mpa(ctx) < 0)
5511 return -1;
5512 if (test_bin_mpa(ctx) < 0)
5513 return -1;
5514 if (test_un_mupa(ctx) < 0)
5515 return -1;
5516 if (test_bin_mupa(ctx) < 0)
5517 return -1;
5518 if (test_mupa_set(ctx) < 0)
5519 return -1;
5520 if (test_mupa_uset(ctx) < 0)
5521 return -1;
5522 if (test_mupa_ma(ctx) < 0)
5523 return -1;
5524 if (test_mupa_pa(ctx) < 0)
5525 return -1;
5526 if (test_mupa_pma(ctx) < 0)
5527 return -1;
5528 if (test_mupa_upma(ctx) < 0)
5529 return -1;
5531 space = isl_space_set_alloc(ctx, 0, 1);
5532 ls = isl_local_space_from_space(space);
5533 aff = isl_aff_zero_on_domain(ls);
5535 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
5536 aff = isl_aff_scale_down_ui(aff, 3);
5537 aff = isl_aff_floor(aff);
5538 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
5539 aff = isl_aff_scale_down_ui(aff, 2);
5540 aff = isl_aff_floor(aff);
5541 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
5543 str = "{ [10] }";
5544 set = isl_set_read_from_str(ctx, str);
5545 aff = isl_aff_gist(aff, set);
5547 aff = isl_aff_add_constant_si(aff, -16);
5548 zero = isl_aff_plain_is_zero(aff);
5549 isl_aff_free(aff);
5551 if (zero < 0)
5552 return -1;
5553 if (!zero)
5554 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5556 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
5557 aff = isl_aff_scale_down_ui(aff, 64);
5558 aff = isl_aff_floor(aff);
5559 equal = aff_check_plain_equal(aff, "{ [-1] }");
5560 isl_aff_free(aff);
5561 if (equal < 0)
5562 return -1;
5564 return 0;
5567 /* Check that "pa" consists of a single expression.
5569 static int check_single_piece(isl_ctx *ctx, __isl_take isl_pw_aff *pa)
5571 int n;
5573 n = isl_pw_aff_n_piece(pa);
5574 isl_pw_aff_free(pa);
5576 if (!pa)
5577 return -1;
5578 if (n != 1)
5579 isl_die(ctx, isl_error_unknown, "expecting single expression",
5580 return -1);
5582 return 0;
5585 /* Check that the computation below results in a single expression.
5586 * One or two expressions may result depending on which constraint
5587 * ends up being considered as redundant with respect to the other
5588 * constraints after the projection that is performed internally
5589 * by isl_set_dim_min.
5591 static int test_dim_max_1(isl_ctx *ctx)
5593 const char *str;
5594 isl_set *set;
5595 isl_pw_aff *pa;
5597 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
5598 "-4a <= b <= 3 and b < n - 4a }";
5599 set = isl_set_read_from_str(ctx, str);
5600 pa = isl_set_dim_min(set, 0);
5601 return check_single_piece(ctx, pa);
5604 /* Check that the computation below results in a single expression.
5605 * The PIP problem corresponding to these constraints has a row
5606 * that causes a split of the solution domain. The solver should
5607 * first pick rows that split off empty parts such that the actual
5608 * solution domain does not get split.
5609 * Note that the description contains some redundant constraints.
5610 * If these constraints get removed first, then the row mentioned
5611 * above does not appear in the PIP problem.
5613 static int test_dim_max_2(isl_ctx *ctx)
5615 const char *str;
5616 isl_set *set;
5617 isl_pw_aff *pa;
5619 str = "[P, N] -> { [a] : a < N and a >= 0 and N > P and a <= P and "
5620 "N > 0 and P >= 0 }";
5621 set = isl_set_read_from_str(ctx, str);
5622 pa = isl_set_dim_max(set, 0);
5623 return check_single_piece(ctx, pa);
5626 int test_dim_max(isl_ctx *ctx)
5628 int equal;
5629 const char *str;
5630 isl_set *set1, *set2;
5631 isl_set *set;
5632 isl_map *map;
5633 isl_pw_aff *pwaff;
5635 if (test_dim_max_1(ctx) < 0)
5636 return -1;
5637 if (test_dim_max_2(ctx) < 0)
5638 return -1;
5640 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
5641 set = isl_set_read_from_str(ctx, str);
5642 pwaff = isl_set_dim_max(set, 0);
5643 set1 = isl_set_from_pw_aff(pwaff);
5644 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
5645 set2 = isl_set_read_from_str(ctx, str);
5646 equal = isl_set_is_equal(set1, set2);
5647 isl_set_free(set1);
5648 isl_set_free(set2);
5649 if (equal < 0)
5650 return -1;
5651 if (!equal)
5652 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5654 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
5655 set = isl_set_read_from_str(ctx, str);
5656 pwaff = isl_set_dim_max(set, 0);
5657 set1 = isl_set_from_pw_aff(pwaff);
5658 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
5659 set2 = isl_set_read_from_str(ctx, str);
5660 equal = isl_set_is_equal(set1, set2);
5661 isl_set_free(set1);
5662 isl_set_free(set2);
5663 if (equal < 0)
5664 return -1;
5665 if (!equal)
5666 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5668 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
5669 set = isl_set_read_from_str(ctx, str);
5670 pwaff = isl_set_dim_max(set, 0);
5671 set1 = isl_set_from_pw_aff(pwaff);
5672 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
5673 set2 = isl_set_read_from_str(ctx, str);
5674 equal = isl_set_is_equal(set1, set2);
5675 isl_set_free(set1);
5676 isl_set_free(set2);
5677 if (equal < 0)
5678 return -1;
5679 if (!equal)
5680 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5682 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
5683 "0 <= i < N and 0 <= j < M }";
5684 map = isl_map_read_from_str(ctx, str);
5685 set = isl_map_range(map);
5687 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
5688 set1 = isl_set_from_pw_aff(pwaff);
5689 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
5690 set2 = isl_set_read_from_str(ctx, str);
5691 equal = isl_set_is_equal(set1, set2);
5692 isl_set_free(set1);
5693 isl_set_free(set2);
5695 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
5696 set1 = isl_set_from_pw_aff(pwaff);
5697 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
5698 set2 = isl_set_read_from_str(ctx, str);
5699 if (equal >= 0 && equal)
5700 equal = isl_set_is_equal(set1, set2);
5701 isl_set_free(set1);
5702 isl_set_free(set2);
5704 isl_set_free(set);
5706 if (equal < 0)
5707 return -1;
5708 if (!equal)
5709 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5711 /* Check that solutions are properly merged. */
5712 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
5713 "c <= -1 + n - 4a - 2b and c >= -2b and "
5714 "4a >= -4 + n and c >= 0 }";
5715 set = isl_set_read_from_str(ctx, str);
5716 pwaff = isl_set_dim_min(set, 2);
5717 set1 = isl_set_from_pw_aff(pwaff);
5718 str = "[n] -> { [(0)] : n >= 1 }";
5719 set2 = isl_set_read_from_str(ctx, str);
5720 equal = isl_set_is_equal(set1, set2);
5721 isl_set_free(set1);
5722 isl_set_free(set2);
5724 if (equal < 0)
5725 return -1;
5726 if (!equal)
5727 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5729 /* Check that empty solution lie in the right space. */
5730 str = "[n] -> { [t,a] : 1 = 0 }";
5731 set = isl_set_read_from_str(ctx, str);
5732 pwaff = isl_set_dim_max(set, 0);
5733 set1 = isl_set_from_pw_aff(pwaff);
5734 str = "[n] -> { [t] : 1 = 0 }";
5735 set2 = isl_set_read_from_str(ctx, str);
5736 equal = isl_set_is_equal(set1, set2);
5737 isl_set_free(set1);
5738 isl_set_free(set2);
5740 if (equal < 0)
5741 return -1;
5742 if (!equal)
5743 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5745 return 0;
5748 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
5750 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
5751 const char *str)
5753 isl_ctx *ctx;
5754 isl_pw_multi_aff *pma2;
5755 int equal;
5757 if (!pma)
5758 return -1;
5760 ctx = isl_pw_multi_aff_get_ctx(pma);
5761 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5762 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
5763 isl_pw_multi_aff_free(pma2);
5765 return equal;
5768 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
5769 * represented by "str".
5771 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
5772 const char *str)
5774 int equal;
5776 equal = pw_multi_aff_plain_is_equal(pma, str);
5777 if (equal < 0)
5778 return -1;
5779 if (!equal)
5780 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
5781 "result not as expected", return -1);
5782 return 0;
5785 /* Basic test for isl_pw_multi_aff_product.
5787 * Check that multiple pieces are properly handled.
5789 static int test_product_pma(isl_ctx *ctx)
5791 int equal;
5792 const char *str;
5793 isl_pw_multi_aff *pma1, *pma2;
5795 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
5796 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
5797 str = "{ C[] -> D[] }";
5798 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5799 pma1 = isl_pw_multi_aff_product(pma1, pma2);
5800 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
5801 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
5802 equal = pw_multi_aff_check_plain_equal(pma1, str);
5803 isl_pw_multi_aff_free(pma1);
5804 if (equal < 0)
5805 return -1;
5807 return 0;
5810 int test_product(isl_ctx *ctx)
5812 const char *str;
5813 isl_set *set;
5814 isl_union_set *uset1, *uset2;
5815 int ok;
5817 str = "{ A[i] }";
5818 set = isl_set_read_from_str(ctx, str);
5819 set = isl_set_product(set, isl_set_copy(set));
5820 ok = isl_set_is_wrapping(set);
5821 isl_set_free(set);
5822 if (ok < 0)
5823 return -1;
5824 if (!ok)
5825 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5827 str = "{ [] }";
5828 uset1 = isl_union_set_read_from_str(ctx, str);
5829 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
5830 str = "{ [[] -> []] }";
5831 uset2 = isl_union_set_read_from_str(ctx, str);
5832 ok = isl_union_set_is_equal(uset1, uset2);
5833 isl_union_set_free(uset1);
5834 isl_union_set_free(uset2);
5835 if (ok < 0)
5836 return -1;
5837 if (!ok)
5838 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5840 if (test_product_pma(ctx) < 0)
5841 return -1;
5843 return 0;
5846 /* Check that two sets are not considered disjoint just because
5847 * they have a different set of (named) parameters.
5849 static int test_disjoint(isl_ctx *ctx)
5851 const char *str;
5852 isl_set *set, *set2;
5853 int disjoint;
5855 str = "[n] -> { [[]->[]] }";
5856 set = isl_set_read_from_str(ctx, str);
5857 str = "{ [[]->[]] }";
5858 set2 = isl_set_read_from_str(ctx, str);
5859 disjoint = isl_set_is_disjoint(set, set2);
5860 isl_set_free(set);
5861 isl_set_free(set2);
5862 if (disjoint < 0)
5863 return -1;
5864 if (disjoint)
5865 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5867 return 0;
5870 /* Inputs for isl_pw_multi_aff_is_equal tests.
5871 * "f1" and "f2" are the two function that need to be compared.
5872 * "equal" is the expected result.
5874 struct {
5875 int equal;
5876 const char *f1;
5877 const char *f2;
5878 } pma_equal_tests[] = {
5879 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
5880 "[N] -> { [0] : 0 <= N <= 1 }" },
5881 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
5882 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
5883 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
5884 "[N] -> { [0] : 0 <= N <= 1 }" },
5885 { 0, "{ [NaN] }", "{ [NaN] }" },
5888 int test_equal(isl_ctx *ctx)
5890 int i;
5891 const char *str;
5892 isl_set *set, *set2;
5893 int equal;
5895 str = "{ S_6[i] }";
5896 set = isl_set_read_from_str(ctx, str);
5897 str = "{ S_7[i] }";
5898 set2 = isl_set_read_from_str(ctx, str);
5899 equal = isl_set_is_equal(set, set2);
5900 isl_set_free(set);
5901 isl_set_free(set2);
5902 if (equal < 0)
5903 return -1;
5904 if (equal)
5905 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5907 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
5908 int expected = pma_equal_tests[i].equal;
5909 isl_pw_multi_aff *f1, *f2;
5911 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
5912 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
5913 equal = isl_pw_multi_aff_is_equal(f1, f2);
5914 isl_pw_multi_aff_free(f1);
5915 isl_pw_multi_aff_free(f2);
5916 if (equal < 0)
5917 return -1;
5918 if (equal != expected)
5919 isl_die(ctx, isl_error_unknown,
5920 "unexpected equality result", return -1);
5923 return 0;
5926 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
5927 enum isl_dim_type type, unsigned pos, int fixed)
5929 isl_bool test;
5931 test = isl_map_plain_is_fixed(map, type, pos, NULL);
5932 isl_map_free(map);
5933 if (test < 0)
5934 return -1;
5935 if (test == fixed)
5936 return 0;
5937 if (fixed)
5938 isl_die(ctx, isl_error_unknown,
5939 "map not detected as fixed", return -1);
5940 else
5941 isl_die(ctx, isl_error_unknown,
5942 "map detected as fixed", return -1);
5945 int test_fixed(isl_ctx *ctx)
5947 const char *str;
5948 isl_map *map;
5950 str = "{ [i] -> [i] }";
5951 map = isl_map_read_from_str(ctx, str);
5952 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
5953 return -1;
5954 str = "{ [i] -> [1] }";
5955 map = isl_map_read_from_str(ctx, str);
5956 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5957 return -1;
5958 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
5959 map = isl_map_read_from_str(ctx, str);
5960 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5961 return -1;
5962 map = isl_map_read_from_str(ctx, str);
5963 map = isl_map_neg(map);
5964 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5965 return -1;
5967 return 0;
5970 struct isl_vertices_test_data {
5971 const char *set;
5972 int n;
5973 const char *vertex[6];
5974 } vertices_tests[] = {
5975 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
5976 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
5977 { "{ A[t, i] : t = 14 and i = 1 }",
5978 1, { "{ A[14, 1] }" } },
5979 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
5980 "c <= m and m <= n and m > 0 }",
5981 6, {
5982 "[n, m] -> { [n, m, m] : 0 < m <= n }",
5983 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
5984 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
5985 "[n, m] -> { [m, m, m] : 0 < m <= n }",
5986 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
5987 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
5988 } },
5991 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
5993 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
5995 struct isl_vertices_test_data *data = user;
5996 isl_ctx *ctx;
5997 isl_multi_aff *ma;
5998 isl_basic_set *bset;
5999 isl_pw_multi_aff *pma;
6000 int i;
6001 isl_bool equal;
6003 ctx = isl_vertex_get_ctx(vertex);
6004 bset = isl_vertex_get_domain(vertex);
6005 ma = isl_vertex_get_expr(vertex);
6006 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
6008 for (i = 0; i < data->n; ++i) {
6009 isl_pw_multi_aff *pma_i;
6011 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
6012 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
6013 isl_pw_multi_aff_free(pma_i);
6015 if (equal < 0 || equal)
6016 break;
6019 isl_pw_multi_aff_free(pma);
6020 isl_vertex_free(vertex);
6022 if (equal < 0)
6023 return isl_stat_error;
6025 return equal ? isl_stat_ok : isl_stat_error;
6028 int test_vertices(isl_ctx *ctx)
6030 int i;
6032 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
6033 isl_basic_set *bset;
6034 isl_vertices *vertices;
6035 int ok = 1;
6036 int n;
6038 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
6039 vertices = isl_basic_set_compute_vertices(bset);
6040 n = isl_vertices_get_n_vertices(vertices);
6041 if (vertices_tests[i].n != n)
6042 ok = 0;
6043 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
6044 &vertices_tests[i]) < 0)
6045 ok = 0;
6046 isl_vertices_free(vertices);
6047 isl_basic_set_free(bset);
6049 if (!vertices)
6050 return -1;
6051 if (!ok)
6052 isl_die(ctx, isl_error_unknown, "unexpected vertices",
6053 return -1);
6056 return 0;
6059 int test_union_pw(isl_ctx *ctx)
6061 int equal;
6062 const char *str;
6063 isl_union_set *uset;
6064 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
6066 str = "{ [x] -> x^2 }";
6067 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
6068 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
6069 uset = isl_union_pw_qpolynomial_domain(upwqp1);
6070 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
6071 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
6072 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
6073 isl_union_pw_qpolynomial_free(upwqp1);
6074 isl_union_pw_qpolynomial_free(upwqp2);
6075 if (equal < 0)
6076 return -1;
6077 if (!equal)
6078 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6080 return 0;
6083 /* Inputs for basic tests of functions that select
6084 * subparts of the domain of an isl_multi_union_pw_aff.
6085 * "fn" is the function that is tested.
6086 * "arg" is a string description of the input.
6087 * "res" is a string description of the expected result.
6089 struct {
6090 __isl_give isl_union_set *(*fn)(
6091 __isl_take isl_multi_union_pw_aff *mupa);
6092 const char *arg;
6093 const char *res;
6094 } un_locus_tests[] = {
6095 { &isl_multi_union_pw_aff_zero_union_set,
6096 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6097 "{ A[0,j]; B[0,j] }" },
6098 { &isl_multi_union_pw_aff_zero_union_set,
6099 "F[{ A[i,j] -> [i-j]; B[i,j] -> [i-j] : i >= 0 }]",
6100 "{ A[i,i]; B[i,i] : i >= 0 }" },
6103 /* Perform some basic tests of functions that select
6104 * subparts of the domain of an isl_multi_union_pw_aff.
6106 static int test_un_locus(isl_ctx *ctx)
6108 int i;
6109 isl_bool ok;
6110 isl_union_set *uset, *res;
6111 isl_multi_union_pw_aff *mupa;
6113 for (i = 0; i < ARRAY_SIZE(un_locus_tests); ++i) {
6114 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6115 un_locus_tests[i].arg);
6116 res = isl_union_set_read_from_str(ctx, un_locus_tests[i].res);
6117 uset = un_locus_tests[i].fn(mupa);
6118 ok = isl_union_set_is_equal(uset, res);
6119 isl_union_set_free(uset);
6120 isl_union_set_free(res);
6121 if (ok < 0)
6122 return -1;
6123 if (!ok)
6124 isl_die(ctx, isl_error_unknown,
6125 "unexpected result", return -1);
6128 return 0;
6131 /* Inputs for basic tests of functions that select
6132 * subparts of an isl_union_map based on a relation
6133 * specified by an isl_multi_union_pw_aff.
6134 * "fn" is the function that is tested.
6135 * "arg1" and "arg2" are string descriptions of the inputs.
6136 * "res" is a string description of the expected result.
6138 struct {
6139 __isl_give isl_union_map *(*fn)(
6140 __isl_take isl_union_map *umap,
6141 __isl_take isl_multi_union_pw_aff *mupa);
6142 const char *arg1;
6143 const char *arg2;
6144 const char *res;
6145 } bin_locus_tests[] = {
6146 { &isl_union_map_eq_at_multi_union_pw_aff,
6147 "{ A[i,j] -> B[i',j'] }",
6148 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6149 "{ A[i,j] -> B[i,j'] }" },
6150 { &isl_union_map_eq_at_multi_union_pw_aff,
6151 "{ A[i,j] -> B[i',j'] }",
6152 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6153 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6154 "{ A[i,j] -> B[i,j] }" },
6155 { &isl_union_map_eq_at_multi_union_pw_aff,
6156 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6157 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6158 "{ A[i,j] -> B[i,j'] }" },
6159 { &isl_union_map_eq_at_multi_union_pw_aff,
6160 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6161 "F[{ A[i,j] -> [i]; B[i,j] -> [i]; C[i,j] -> [0] }]",
6162 "{ A[i,j] -> B[i,j']; A[0,j] -> C[i',j'] }" },
6163 { &isl_union_map_eq_at_multi_union_pw_aff,
6164 "{ A[i,j] -> B[i',j'] }",
6165 "F[{ A[i,j] -> [i] : i > j; B[i,j] -> [i] }]",
6166 "{ A[i,j] -> B[i,j'] : i > j }" },
6167 { &isl_union_map_lex_lt_at_multi_union_pw_aff,
6168 "{ A[i,j] -> B[i',j'] }",
6169 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6170 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6171 "{ A[i,j] -> B[i',j'] : i,j << i',j' }" },
6172 { &isl_union_map_lex_gt_at_multi_union_pw_aff,
6173 "{ A[i,j] -> B[i',j'] }",
6174 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6175 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6176 "{ A[i,j] -> B[i',j'] : i,j >> i',j' }" },
6179 /* Perform some basic tests of functions that select
6180 * subparts of an isl_union_map based on a relation
6181 * specified by an isl_multi_union_pw_aff.
6183 static int test_bin_locus(isl_ctx *ctx)
6185 int i;
6186 isl_bool ok;
6187 isl_union_map *umap, *res;
6188 isl_multi_union_pw_aff *mupa;
6190 for (i = 0; i < ARRAY_SIZE(bin_locus_tests); ++i) {
6191 umap = isl_union_map_read_from_str(ctx,
6192 bin_locus_tests[i].arg1);
6193 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6194 bin_locus_tests[i].arg2);
6195 res = isl_union_map_read_from_str(ctx, bin_locus_tests[i].res);
6196 umap = bin_locus_tests[i].fn(umap, mupa);
6197 ok = isl_union_map_is_equal(umap, res);
6198 isl_union_map_free(umap);
6199 isl_union_map_free(res);
6200 if (ok < 0)
6201 return -1;
6202 if (!ok)
6203 isl_die(ctx, isl_error_unknown,
6204 "unexpected result", return -1);
6207 return 0;
6210 /* Perform basic locus tests.
6212 static int test_locus(isl_ctx *ctx)
6214 if (test_un_locus(ctx) < 0)
6215 return -1;
6216 if (test_bin_locus(ctx) < 0)
6217 return -1;
6218 return 0;
6221 /* Test that isl_union_pw_qpolynomial_eval picks up the function
6222 * defined over the correct domain space.
6224 static int test_eval_1(isl_ctx *ctx)
6226 const char *str;
6227 isl_point *pnt;
6228 isl_set *set;
6229 isl_union_pw_qpolynomial *upwqp;
6230 isl_val *v;
6231 int cmp;
6233 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
6234 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
6235 str = "{ A[6] }";
6236 set = isl_set_read_from_str(ctx, str);
6237 pnt = isl_set_sample_point(set);
6238 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
6239 cmp = isl_val_cmp_si(v, 36);
6240 isl_val_free(v);
6242 if (!v)
6243 return -1;
6244 if (cmp != 0)
6245 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
6247 return 0;
6250 /* Check that isl_qpolynomial_eval handles getting called on a void point.
6252 static int test_eval_2(isl_ctx *ctx)
6254 const char *str;
6255 isl_point *pnt;
6256 isl_set *set;
6257 isl_qpolynomial *qp;
6258 isl_val *v;
6259 isl_bool ok;
6261 str = "{ A[x] -> [x] }";
6262 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
6263 str = "{ A[x] : false }";
6264 set = isl_set_read_from_str(ctx, str);
6265 pnt = isl_set_sample_point(set);
6266 v = isl_qpolynomial_eval(qp, pnt);
6267 ok = isl_val_is_nan(v);
6268 isl_val_free(v);
6270 if (ok < 0)
6271 return -1;
6272 if (!ok)
6273 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
6275 return 0;
6278 /* Perform basic polynomial evaluation tests.
6280 static int test_eval(isl_ctx *ctx)
6282 if (test_eval_1(ctx) < 0)
6283 return -1;
6284 if (test_eval_2(ctx) < 0)
6285 return -1;
6286 return 0;
6289 /* Descriptions of sets that are tested for reparsing after printing.
6291 const char *output_tests[] = {
6292 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
6293 "{ [x] : 1 = 0 }",
6294 "{ [x] : false }",
6295 "{ [x] : x mod 2 = 0 }",
6296 "{ [x] : x mod 2 = 1 }",
6297 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) < x }",
6298 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) < x }",
6299 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
6300 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
6301 "[n] -> { [y, x] : 2*((x + 2y) mod 3) = n }",
6302 "{ [x, y] : (2*floor(x/3) + 3*floor(y/4)) mod 5 = x }",
6305 /* Check that printing a set and reparsing a set from the printed output
6306 * results in the same set.
6308 static int test_output_set(isl_ctx *ctx)
6310 int i;
6311 char *str;
6312 isl_set *set1, *set2;
6313 isl_bool equal;
6315 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
6316 set1 = isl_set_read_from_str(ctx, output_tests[i]);
6317 str = isl_set_to_str(set1);
6318 set2 = isl_set_read_from_str(ctx, str);
6319 free(str);
6320 equal = isl_set_is_equal(set1, set2);
6321 isl_set_free(set1);
6322 isl_set_free(set2);
6323 if (equal < 0)
6324 return -1;
6325 if (!equal)
6326 isl_die(ctx, isl_error_unknown,
6327 "parsed output not the same", return -1);
6330 return 0;
6333 int test_output(isl_ctx *ctx)
6335 char *s;
6336 const char *str;
6337 isl_pw_aff *pa;
6338 isl_printer *p;
6339 int equal;
6341 if (test_output_set(ctx) < 0)
6342 return -1;
6344 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
6345 pa = isl_pw_aff_read_from_str(ctx, str);
6347 p = isl_printer_to_str(ctx);
6348 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
6349 p = isl_printer_print_pw_aff(p, pa);
6350 s = isl_printer_get_str(p);
6351 isl_printer_free(p);
6352 isl_pw_aff_free(pa);
6353 if (!s)
6354 equal = -1;
6355 else
6356 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
6357 free(s);
6358 if (equal < 0)
6359 return -1;
6360 if (!equal)
6361 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6363 return 0;
6366 int test_sample(isl_ctx *ctx)
6368 const char *str;
6369 isl_basic_set *bset1, *bset2;
6370 int empty, subset;
6372 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
6373 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
6374 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
6375 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
6376 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
6377 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
6378 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
6379 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
6380 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
6381 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
6382 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
6383 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
6384 "d - 1073741821e and "
6385 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
6386 "3j >= 1 - a + b + 2e and "
6387 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
6388 "3i <= 4 - a + 4b - e and "
6389 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
6390 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
6391 "c <= -1 + a and 3i >= -2 - a + 3e and "
6392 "1073741822e <= 1073741823 - a + 1073741822b + c and "
6393 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
6394 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
6395 "1073741823e >= 1 + 1073741823b - d and "
6396 "3i >= 1073741823b + c - 1073741823e - f and "
6397 "3i >= 1 + 2b + e + 3g }";
6398 bset1 = isl_basic_set_read_from_str(ctx, str);
6399 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
6400 empty = isl_basic_set_is_empty(bset2);
6401 subset = isl_basic_set_is_subset(bset2, bset1);
6402 isl_basic_set_free(bset1);
6403 isl_basic_set_free(bset2);
6404 if (empty < 0 || subset < 0)
6405 return -1;
6406 if (empty)
6407 isl_die(ctx, isl_error_unknown, "point not found", return -1);
6408 if (!subset)
6409 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
6411 return 0;
6414 int test_fixed_power(isl_ctx *ctx)
6416 const char *str;
6417 isl_map *map;
6418 isl_val *exp;
6419 int equal;
6421 str = "{ [i] -> [i + 1] }";
6422 map = isl_map_read_from_str(ctx, str);
6423 exp = isl_val_int_from_si(ctx, 23);
6424 map = isl_map_fixed_power_val(map, exp);
6425 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
6426 isl_map_free(map);
6427 if (equal < 0)
6428 return -1;
6430 return 0;
6433 int test_slice(isl_ctx *ctx)
6435 const char *str;
6436 isl_map *map;
6437 int equal;
6439 str = "{ [i] -> [j] }";
6440 map = isl_map_read_from_str(ctx, str);
6441 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
6442 equal = map_check_equal(map, "{ [i] -> [i] }");
6443 isl_map_free(map);
6444 if (equal < 0)
6445 return -1;
6447 str = "{ [i] -> [j] }";
6448 map = isl_map_read_from_str(ctx, str);
6449 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
6450 equal = map_check_equal(map, "{ [i] -> [j] }");
6451 isl_map_free(map);
6452 if (equal < 0)
6453 return -1;
6455 str = "{ [i] -> [j] }";
6456 map = isl_map_read_from_str(ctx, str);
6457 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
6458 equal = map_check_equal(map, "{ [i] -> [-i] }");
6459 isl_map_free(map);
6460 if (equal < 0)
6461 return -1;
6463 str = "{ [i] -> [j] }";
6464 map = isl_map_read_from_str(ctx, str);
6465 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
6466 equal = map_check_equal(map, "{ [0] -> [j] }");
6467 isl_map_free(map);
6468 if (equal < 0)
6469 return -1;
6471 str = "{ [i] -> [j] }";
6472 map = isl_map_read_from_str(ctx, str);
6473 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
6474 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
6475 isl_map_free(map);
6476 if (equal < 0)
6477 return -1;
6479 str = "{ [i] -> [j] }";
6480 map = isl_map_read_from_str(ctx, str);
6481 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
6482 equal = map_check_equal(map, "{ [i] -> [j] : false }");
6483 isl_map_free(map);
6484 if (equal < 0)
6485 return -1;
6487 return 0;
6490 int test_eliminate(isl_ctx *ctx)
6492 const char *str;
6493 isl_map *map;
6494 int equal;
6496 str = "{ [i] -> [j] : i = 2j }";
6497 map = isl_map_read_from_str(ctx, str);
6498 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
6499 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
6500 isl_map_free(map);
6501 if (equal < 0)
6502 return -1;
6504 return 0;
6507 /* Check that isl_set_dim_residue_class detects that the values of j
6508 * in the set below are all odd and that it does not detect any spurious
6509 * strides.
6511 static int test_residue_class(isl_ctx *ctx)
6513 const char *str;
6514 isl_set *set;
6515 isl_int m, r;
6516 isl_stat res;
6518 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
6519 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
6520 set = isl_set_read_from_str(ctx, str);
6521 isl_int_init(m);
6522 isl_int_init(r);
6523 res = isl_set_dim_residue_class(set, 1, &m, &r);
6524 if (res >= 0 &&
6525 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
6526 isl_die(ctx, isl_error_unknown, "incorrect residue class",
6527 res = isl_stat_error);
6528 isl_int_clear(r);
6529 isl_int_clear(m);
6530 isl_set_free(set);
6532 return res;
6535 int test_align_parameters(isl_ctx *ctx)
6537 const char *str;
6538 isl_space *space;
6539 isl_multi_aff *ma1, *ma2;
6540 int equal;
6542 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
6543 ma1 = isl_multi_aff_read_from_str(ctx, str);
6545 space = isl_space_params_alloc(ctx, 1);
6546 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
6547 ma1 = isl_multi_aff_align_params(ma1, space);
6549 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
6550 ma2 = isl_multi_aff_read_from_str(ctx, str);
6552 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
6554 isl_multi_aff_free(ma1);
6555 isl_multi_aff_free(ma2);
6557 if (equal < 0)
6558 return -1;
6559 if (!equal)
6560 isl_die(ctx, isl_error_unknown,
6561 "result not as expected", return -1);
6563 return 0;
6566 static int test_list(isl_ctx *ctx)
6568 isl_id *a, *b, *c, *d, *id;
6569 isl_id_list *list;
6570 int ok;
6572 a = isl_id_alloc(ctx, "a", NULL);
6573 b = isl_id_alloc(ctx, "b", NULL);
6574 c = isl_id_alloc(ctx, "c", NULL);
6575 d = isl_id_alloc(ctx, "d", NULL);
6577 list = isl_id_list_alloc(ctx, 4);
6578 list = isl_id_list_add(list, b);
6579 list = isl_id_list_insert(list, 0, a);
6580 list = isl_id_list_add(list, c);
6581 list = isl_id_list_add(list, d);
6582 list = isl_id_list_drop(list, 1, 1);
6584 if (!list)
6585 return -1;
6586 if (isl_id_list_n_id(list) != 3) {
6587 isl_id_list_free(list);
6588 isl_die(ctx, isl_error_unknown,
6589 "unexpected number of elements in list", return -1);
6592 id = isl_id_list_get_id(list, 0);
6593 ok = id == a;
6594 isl_id_free(id);
6595 id = isl_id_list_get_id(list, 1);
6596 ok = ok && id == c;
6597 isl_id_free(id);
6598 id = isl_id_list_get_id(list, 2);
6599 ok = ok && id == d;
6600 isl_id_free(id);
6602 isl_id_list_free(list);
6604 if (!ok)
6605 isl_die(ctx, isl_error_unknown,
6606 "unexpected elements in list", return -1);
6608 return 0;
6611 const char *set_conversion_tests[] = {
6612 "[N] -> { [i] : N - 1 <= 2 i <= N }",
6613 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
6614 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
6615 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
6616 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
6617 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
6618 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
6619 "-3 + c <= d <= 28 + c) }",
6622 /* Check that converting from isl_set to isl_pw_multi_aff and back
6623 * to isl_set produces the original isl_set.
6625 static int test_set_conversion(isl_ctx *ctx)
6627 int i;
6628 const char *str;
6629 isl_set *set1, *set2;
6630 isl_pw_multi_aff *pma;
6631 int equal;
6633 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
6634 str = set_conversion_tests[i];
6635 set1 = isl_set_read_from_str(ctx, str);
6636 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
6637 set2 = isl_set_from_pw_multi_aff(pma);
6638 equal = isl_set_is_equal(set1, set2);
6639 isl_set_free(set1);
6640 isl_set_free(set2);
6642 if (equal < 0)
6643 return -1;
6644 if (!equal)
6645 isl_die(ctx, isl_error_unknown, "bad conversion",
6646 return -1);
6649 return 0;
6652 const char *conversion_tests[] = {
6653 "{ [a, b, c, d] -> s0[a, b, e, f] : "
6654 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
6655 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
6656 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
6657 "9e <= -2 - 2a) }",
6658 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
6659 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
6660 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
6663 /* Check that converting from isl_map to isl_pw_multi_aff and back
6664 * to isl_map produces the original isl_map.
6666 static int test_map_conversion(isl_ctx *ctx)
6668 int i;
6669 isl_map *map1, *map2;
6670 isl_pw_multi_aff *pma;
6671 int equal;
6673 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
6674 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
6675 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
6676 map2 = isl_map_from_pw_multi_aff(pma);
6677 equal = isl_map_is_equal(map1, map2);
6678 isl_map_free(map1);
6679 isl_map_free(map2);
6681 if (equal < 0)
6682 return -1;
6683 if (!equal)
6684 isl_die(ctx, isl_error_unknown, "bad conversion",
6685 return -1);
6688 return 0;
6691 /* Descriptions of isl_pw_multi_aff objects for testing conversion
6692 * to isl_multi_pw_aff and back.
6694 const char *mpa_conversion_tests[] = {
6695 "{ [x] -> A[x] }",
6696 "{ [x] -> A[x] : x >= 0 }",
6697 "{ [x] -> A[x] : x >= 0; [x] -> A[-x] : x < 0 }",
6698 "{ [x] -> A[x, x + 1] }",
6699 "{ [x] -> A[] }",
6702 /* Check that conversion from isl_pw_multi_aff to isl_multi_pw_aff and
6703 * back to isl_pw_multi_aff preserves the original meaning.
6705 static int test_mpa_conversion(isl_ctx *ctx)
6707 int i;
6708 isl_pw_multi_aff *pma1, *pma2;
6709 isl_multi_pw_aff *mpa;
6710 int equal;
6712 for (i = 0; i < ARRAY_SIZE(mpa_conversion_tests); ++i) {
6713 const char *str;
6714 str = mpa_conversion_tests[i];
6715 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
6716 pma2 = isl_pw_multi_aff_copy(pma1);
6717 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma1);
6718 pma1 = isl_pw_multi_aff_from_multi_pw_aff(mpa);
6719 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6720 isl_pw_multi_aff_free(pma1);
6721 isl_pw_multi_aff_free(pma2);
6723 if (equal < 0)
6724 return -1;
6725 if (!equal)
6726 isl_die(ctx, isl_error_unknown, "bad conversion",
6727 return -1);
6730 return 0;
6733 /* Descriptions of union maps that should be convertible
6734 * to an isl_multi_union_pw_aff.
6736 const char *umap_mupa_conversion_tests[] = {
6737 "{ [a, b, c, d] -> s0[a, b, e, f] : "
6738 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
6739 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
6740 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
6741 "9e <= -2 - 2a) }",
6742 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
6743 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
6744 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
6745 "{ A[] -> B[0]; C[] -> B[1] }",
6748 /* Check that converting from isl_union_map to isl_multi_union_pw_aff and back
6749 * to isl_union_map produces the original isl_union_map.
6751 static int test_union_map_mupa_conversion(isl_ctx *ctx)
6753 int i;
6754 isl_union_map *umap1, *umap2;
6755 isl_multi_union_pw_aff *mupa;
6756 int equal;
6758 for (i = 0; i < ARRAY_SIZE(umap_mupa_conversion_tests); ++i) {
6759 const char *str;
6760 str = umap_mupa_conversion_tests[i];
6761 umap1 = isl_union_map_read_from_str(ctx, str);
6762 umap2 = isl_union_map_copy(umap1);
6763 mupa = isl_multi_union_pw_aff_from_union_map(umap2);
6764 umap2 = isl_union_map_from_multi_union_pw_aff(mupa);
6765 equal = isl_union_map_is_equal(umap1, umap2);
6766 isl_union_map_free(umap1);
6767 isl_union_map_free(umap2);
6769 if (equal < 0)
6770 return -1;
6771 if (!equal)
6772 isl_die(ctx, isl_error_unknown, "bad conversion",
6773 return -1);
6776 return 0;
6779 static int test_conversion(isl_ctx *ctx)
6781 if (test_set_conversion(ctx) < 0)
6782 return -1;
6783 if (test_map_conversion(ctx) < 0)
6784 return -1;
6785 if (test_mpa_conversion(ctx) < 0)
6786 return -1;
6787 if (test_union_map_mupa_conversion(ctx) < 0)
6788 return -1;
6789 return 0;
6792 /* Check that isl_basic_map_curry does not modify input.
6794 static int test_curry(isl_ctx *ctx)
6796 const char *str;
6797 isl_basic_map *bmap1, *bmap2;
6798 int equal;
6800 str = "{ [A[] -> B[]] -> C[] }";
6801 bmap1 = isl_basic_map_read_from_str(ctx, str);
6802 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
6803 equal = isl_basic_map_is_equal(bmap1, bmap2);
6804 isl_basic_map_free(bmap1);
6805 isl_basic_map_free(bmap2);
6807 if (equal < 0)
6808 return -1;
6809 if (equal)
6810 isl_die(ctx, isl_error_unknown,
6811 "curried map should not be equal to original",
6812 return -1);
6814 return 0;
6817 struct {
6818 const char *set;
6819 const char *ma;
6820 const char *res;
6821 } preimage_tests[] = {
6822 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
6823 "{ A[j,i] -> B[i,j] }",
6824 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
6825 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
6826 "{ A[a,b] -> B[a/2,b/6] }",
6827 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
6828 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
6829 "{ A[a,b] -> B[a/2,b/6] }",
6830 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
6831 "exists i,j : a = 2 i and b = 6 j }" },
6832 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
6833 "[n] -> { : 0 <= n <= 100 }" },
6834 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
6835 "{ A[a] -> B[2a] }",
6836 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
6837 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
6838 "{ A[a] -> B[([a/2])] }",
6839 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
6840 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
6841 "{ A[a] -> B[a,a,a/3] }",
6842 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
6843 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
6844 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
6847 static int test_preimage_basic_set(isl_ctx *ctx)
6849 int i;
6850 isl_basic_set *bset1, *bset2;
6851 isl_multi_aff *ma;
6852 int equal;
6854 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
6855 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
6856 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
6857 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
6858 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
6859 equal = isl_basic_set_is_equal(bset1, bset2);
6860 isl_basic_set_free(bset1);
6861 isl_basic_set_free(bset2);
6862 if (equal < 0)
6863 return -1;
6864 if (!equal)
6865 isl_die(ctx, isl_error_unknown, "bad preimage",
6866 return -1);
6869 return 0;
6872 struct {
6873 const char *map;
6874 const char *ma;
6875 const char *res;
6876 } preimage_domain_tests[] = {
6877 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
6878 "{ A[j,i] -> B[i,j] }",
6879 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
6880 { "{ B[i] -> C[i]; D[i] -> E[i] }",
6881 "{ A[i] -> B[i + 1] }",
6882 "{ A[i] -> C[i + 1] }" },
6883 { "{ B[i] -> C[i]; B[i] -> E[i] }",
6884 "{ A[i] -> B[i + 1] }",
6885 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
6886 { "{ B[i] -> C[([i/2])] }",
6887 "{ A[i] -> B[2i] }",
6888 "{ A[i] -> C[i] }" },
6889 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
6890 "{ A[i] -> B[([i/5]), ([i/7])] }",
6891 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
6892 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
6893 "[N] -> { A[] -> B[([N/5])] }",
6894 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
6895 { "{ B[i] -> C[i] : exists a : i = 5 a }",
6896 "{ A[i] -> B[2i] }",
6897 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
6898 { "{ B[i] -> C[i] : exists a : i = 2 a; "
6899 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
6900 "{ A[i] -> B[2i] }",
6901 "{ A[i] -> C[2i] }" },
6902 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
6903 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
6906 static int test_preimage_union_map(isl_ctx *ctx)
6908 int i;
6909 isl_union_map *umap1, *umap2;
6910 isl_multi_aff *ma;
6911 int equal;
6913 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
6914 umap1 = isl_union_map_read_from_str(ctx,
6915 preimage_domain_tests[i].map);
6916 ma = isl_multi_aff_read_from_str(ctx,
6917 preimage_domain_tests[i].ma);
6918 umap2 = isl_union_map_read_from_str(ctx,
6919 preimage_domain_tests[i].res);
6920 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
6921 equal = isl_union_map_is_equal(umap1, umap2);
6922 isl_union_map_free(umap1);
6923 isl_union_map_free(umap2);
6924 if (equal < 0)
6925 return -1;
6926 if (!equal)
6927 isl_die(ctx, isl_error_unknown, "bad preimage",
6928 return -1);
6931 return 0;
6934 static int test_preimage(isl_ctx *ctx)
6936 if (test_preimage_basic_set(ctx) < 0)
6937 return -1;
6938 if (test_preimage_union_map(ctx) < 0)
6939 return -1;
6941 return 0;
6944 struct {
6945 const char *ma1;
6946 const char *ma;
6947 const char *res;
6948 } pullback_tests[] = {
6949 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
6950 "{ A[a,b] -> C[b + 2a] }" },
6951 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
6952 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
6953 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
6954 "{ A[a] -> C[(a)/6] }" },
6955 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
6956 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
6957 "{ A[a] -> C[(2a)/3] }" },
6958 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
6959 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
6960 "{ A[i,j] -> C[i + j, i + j] }"},
6961 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
6962 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
6963 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
6964 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
6965 "{ [i, j] -> [floor((i)/3), j] }",
6966 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
6969 static int test_pullback(isl_ctx *ctx)
6971 int i;
6972 isl_multi_aff *ma1, *ma2;
6973 isl_multi_aff *ma;
6974 int equal;
6976 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
6977 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
6978 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
6979 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
6980 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
6981 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
6982 isl_multi_aff_free(ma1);
6983 isl_multi_aff_free(ma2);
6984 if (equal < 0)
6985 return -1;
6986 if (!equal)
6987 isl_die(ctx, isl_error_unknown, "bad pullback",
6988 return -1);
6991 return 0;
6994 /* Check that negation is printed correctly and that equal expressions
6995 * are correctly identified.
6997 static int test_ast(isl_ctx *ctx)
6999 isl_ast_expr *expr, *expr1, *expr2, *expr3;
7000 char *str;
7001 int ok, equal;
7003 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
7004 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
7005 expr = isl_ast_expr_add(expr1, expr2);
7006 expr2 = isl_ast_expr_copy(expr);
7007 expr = isl_ast_expr_neg(expr);
7008 expr2 = isl_ast_expr_neg(expr2);
7009 equal = isl_ast_expr_is_equal(expr, expr2);
7010 str = isl_ast_expr_to_C_str(expr);
7011 ok = str ? !strcmp(str, "-(A + B)") : -1;
7012 free(str);
7013 isl_ast_expr_free(expr);
7014 isl_ast_expr_free(expr2);
7016 if (ok < 0 || equal < 0)
7017 return -1;
7018 if (!equal)
7019 isl_die(ctx, isl_error_unknown,
7020 "equal expressions not considered equal", return -1);
7021 if (!ok)
7022 isl_die(ctx, isl_error_unknown,
7023 "isl_ast_expr printed incorrectly", return -1);
7025 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
7026 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
7027 expr = isl_ast_expr_add(expr1, expr2);
7028 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
7029 expr = isl_ast_expr_sub(expr3, expr);
7030 str = isl_ast_expr_to_C_str(expr);
7031 ok = str ? !strcmp(str, "C - (A + B)") : -1;
7032 free(str);
7033 isl_ast_expr_free(expr);
7035 if (ok < 0)
7036 return -1;
7037 if (!ok)
7038 isl_die(ctx, isl_error_unknown,
7039 "isl_ast_expr printed incorrectly", return -1);
7041 return 0;
7044 /* Check that isl_ast_build_expr_from_set returns a valid expression
7045 * for an empty set. Note that isl_ast_build_expr_from_set getting
7046 * called on an empty set probably indicates a bug in the caller.
7048 static int test_ast_build(isl_ctx *ctx)
7050 isl_set *set;
7051 isl_ast_build *build;
7052 isl_ast_expr *expr;
7054 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
7055 build = isl_ast_build_from_context(set);
7057 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
7058 expr = isl_ast_build_expr_from_set(build, set);
7060 isl_ast_expr_free(expr);
7061 isl_ast_build_free(build);
7063 if (!expr)
7064 return -1;
7066 return 0;
7069 /* Internal data structure for before_for and after_for callbacks.
7071 * depth is the current depth
7072 * before is the number of times before_for has been called
7073 * after is the number of times after_for has been called
7075 struct isl_test_codegen_data {
7076 int depth;
7077 int before;
7078 int after;
7081 /* This function is called before each for loop in the AST generated
7082 * from test_ast_gen1.
7084 * Increment the number of calls and the depth.
7085 * Check that the space returned by isl_ast_build_get_schedule_space
7086 * matches the target space of the schedule returned by
7087 * isl_ast_build_get_schedule.
7088 * Return an isl_id that is checked by the corresponding call
7089 * to after_for.
7091 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
7092 void *user)
7094 struct isl_test_codegen_data *data = user;
7095 isl_ctx *ctx;
7096 isl_space *space;
7097 isl_union_map *schedule;
7098 isl_union_set *uset;
7099 isl_set *set;
7100 int empty;
7101 char name[] = "d0";
7103 ctx = isl_ast_build_get_ctx(build);
7105 if (data->before >= 3)
7106 isl_die(ctx, isl_error_unknown,
7107 "unexpected number of for nodes", return NULL);
7108 if (data->depth >= 2)
7109 isl_die(ctx, isl_error_unknown,
7110 "unexpected depth", return NULL);
7112 snprintf(name, sizeof(name), "d%d", data->depth);
7113 data->before++;
7114 data->depth++;
7116 schedule = isl_ast_build_get_schedule(build);
7117 uset = isl_union_map_range(schedule);
7118 if (!uset)
7119 return NULL;
7120 if (isl_union_set_n_set(uset) != 1) {
7121 isl_union_set_free(uset);
7122 isl_die(ctx, isl_error_unknown,
7123 "expecting single range space", return NULL);
7126 space = isl_ast_build_get_schedule_space(build);
7127 set = isl_union_set_extract_set(uset, space);
7128 isl_union_set_free(uset);
7129 empty = isl_set_is_empty(set);
7130 isl_set_free(set);
7132 if (empty < 0)
7133 return NULL;
7134 if (empty)
7135 isl_die(ctx, isl_error_unknown,
7136 "spaces don't match", return NULL);
7138 return isl_id_alloc(ctx, name, NULL);
7141 /* This function is called after each for loop in the AST generated
7142 * from test_ast_gen1.
7144 * Increment the number of calls and decrement the depth.
7145 * Check that the annotation attached to the node matches
7146 * the isl_id returned by the corresponding call to before_for.
7148 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
7149 __isl_keep isl_ast_build *build, void *user)
7151 struct isl_test_codegen_data *data = user;
7152 isl_id *id;
7153 const char *name;
7154 int valid;
7156 data->after++;
7157 data->depth--;
7159 if (data->after > data->before)
7160 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
7161 "mismatch in number of for nodes",
7162 return isl_ast_node_free(node));
7164 id = isl_ast_node_get_annotation(node);
7165 if (!id)
7166 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
7167 "missing annotation", return isl_ast_node_free(node));
7169 name = isl_id_get_name(id);
7170 valid = name && atoi(name + 1) == data->depth;
7171 isl_id_free(id);
7173 if (!valid)
7174 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
7175 "wrong annotation", return isl_ast_node_free(node));
7177 return node;
7180 /* Check that the before_each_for and after_each_for callbacks
7181 * are called for each for loop in the generated code,
7182 * that they are called in the right order and that the isl_id
7183 * returned from the before_each_for callback is attached to
7184 * the isl_ast_node passed to the corresponding after_each_for call.
7186 static int test_ast_gen1(isl_ctx *ctx)
7188 const char *str;
7189 isl_set *set;
7190 isl_union_map *schedule;
7191 isl_ast_build *build;
7192 isl_ast_node *tree;
7193 struct isl_test_codegen_data data;
7195 str = "[N] -> { : N >= 10 }";
7196 set = isl_set_read_from_str(ctx, str);
7197 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
7198 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
7199 schedule = isl_union_map_read_from_str(ctx, str);
7201 data.before = 0;
7202 data.after = 0;
7203 data.depth = 0;
7204 build = isl_ast_build_from_context(set);
7205 build = isl_ast_build_set_before_each_for(build,
7206 &before_for, &data);
7207 build = isl_ast_build_set_after_each_for(build,
7208 &after_for, &data);
7209 tree = isl_ast_build_node_from_schedule_map(build, schedule);
7210 isl_ast_build_free(build);
7211 if (!tree)
7212 return -1;
7214 isl_ast_node_free(tree);
7216 if (data.before != 3 || data.after != 3)
7217 isl_die(ctx, isl_error_unknown,
7218 "unexpected number of for nodes", return -1);
7220 return 0;
7223 /* Check that the AST generator handles domains that are integrally disjoint
7224 * but not rationally disjoint.
7226 static int test_ast_gen2(isl_ctx *ctx)
7228 const char *str;
7229 isl_set *set;
7230 isl_union_map *schedule;
7231 isl_union_map *options;
7232 isl_ast_build *build;
7233 isl_ast_node *tree;
7235 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
7236 schedule = isl_union_map_read_from_str(ctx, str);
7237 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
7238 build = isl_ast_build_from_context(set);
7240 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
7241 options = isl_union_map_read_from_str(ctx, str);
7242 build = isl_ast_build_set_options(build, options);
7243 tree = isl_ast_build_node_from_schedule_map(build, schedule);
7244 isl_ast_build_free(build);
7245 if (!tree)
7246 return -1;
7247 isl_ast_node_free(tree);
7249 return 0;
7252 /* Increment *user on each call.
7254 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
7255 __isl_keep isl_ast_build *build, void *user)
7257 int *n = user;
7259 (*n)++;
7261 return node;
7264 /* Test that unrolling tries to minimize the number of instances.
7265 * In particular, for the schedule given below, make sure it generates
7266 * 3 nodes (rather than 101).
7268 static int test_ast_gen3(isl_ctx *ctx)
7270 const char *str;
7271 isl_set *set;
7272 isl_union_map *schedule;
7273 isl_union_map *options;
7274 isl_ast_build *build;
7275 isl_ast_node *tree;
7276 int n_domain = 0;
7278 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
7279 schedule = isl_union_map_read_from_str(ctx, str);
7280 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
7282 str = "{ [i] -> unroll[0] }";
7283 options = isl_union_map_read_from_str(ctx, str);
7285 build = isl_ast_build_from_context(set);
7286 build = isl_ast_build_set_options(build, options);
7287 build = isl_ast_build_set_at_each_domain(build,
7288 &count_domains, &n_domain);
7289 tree = isl_ast_build_node_from_schedule_map(build, schedule);
7290 isl_ast_build_free(build);
7291 if (!tree)
7292 return -1;
7294 isl_ast_node_free(tree);
7296 if (n_domain != 3)
7297 isl_die(ctx, isl_error_unknown,
7298 "unexpected number of for nodes", return -1);
7300 return 0;
7303 /* Check that if the ast_build_exploit_nested_bounds options is set,
7304 * we do not get an outer if node in the generated AST,
7305 * while we do get such an outer if node if the options is not set.
7307 static int test_ast_gen4(isl_ctx *ctx)
7309 const char *str;
7310 isl_set *set;
7311 isl_union_map *schedule;
7312 isl_ast_build *build;
7313 isl_ast_node *tree;
7314 enum isl_ast_node_type type;
7315 int enb;
7317 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
7318 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
7320 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
7322 schedule = isl_union_map_read_from_str(ctx, str);
7323 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
7324 build = isl_ast_build_from_context(set);
7325 tree = isl_ast_build_node_from_schedule_map(build, schedule);
7326 isl_ast_build_free(build);
7327 if (!tree)
7328 return -1;
7330 type = isl_ast_node_get_type(tree);
7331 isl_ast_node_free(tree);
7333 if (type == isl_ast_node_if)
7334 isl_die(ctx, isl_error_unknown,
7335 "not expecting if node", return -1);
7337 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
7339 schedule = isl_union_map_read_from_str(ctx, str);
7340 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
7341 build = isl_ast_build_from_context(set);
7342 tree = isl_ast_build_node_from_schedule_map(build, schedule);
7343 isl_ast_build_free(build);
7344 if (!tree)
7345 return -1;
7347 type = isl_ast_node_get_type(tree);
7348 isl_ast_node_free(tree);
7350 if (type != isl_ast_node_if)
7351 isl_die(ctx, isl_error_unknown,
7352 "expecting if node", return -1);
7354 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
7356 return 0;
7359 /* This function is called for each leaf in the AST generated
7360 * from test_ast_gen5.
7362 * We finalize the AST generation by extending the outer schedule
7363 * with a zero-dimensional schedule. If this results in any for loops,
7364 * then this means that we did not pass along enough information
7365 * about the outer schedule to the inner AST generation.
7367 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
7368 void *user)
7370 isl_union_map *schedule, *extra;
7371 isl_ast_node *tree;
7373 schedule = isl_ast_build_get_schedule(build);
7374 extra = isl_union_map_copy(schedule);
7375 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
7376 schedule = isl_union_map_range_product(schedule, extra);
7377 tree = isl_ast_build_node_from_schedule_map(build, schedule);
7378 isl_ast_build_free(build);
7380 if (!tree)
7381 return NULL;
7383 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
7384 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
7385 "code should not contain any for loop",
7386 return isl_ast_node_free(tree));
7388 return tree;
7391 /* Check that we do not lose any information when going back and
7392 * forth between internal and external schedule.
7394 * In particular, we create an AST where we unroll the only
7395 * non-constant dimension in the schedule. We therefore do
7396 * not expect any for loops in the AST. However, older versions
7397 * of isl would not pass along enough information about the outer
7398 * schedule when performing an inner code generation from a create_leaf
7399 * callback, resulting in the inner code generation producing a for loop.
7401 static int test_ast_gen5(isl_ctx *ctx)
7403 const char *str;
7404 isl_set *set;
7405 isl_union_map *schedule, *options;
7406 isl_ast_build *build;
7407 isl_ast_node *tree;
7409 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
7410 schedule = isl_union_map_read_from_str(ctx, str);
7412 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
7413 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
7414 options = isl_union_map_read_from_str(ctx, str);
7416 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
7417 build = isl_ast_build_from_context(set);
7418 build = isl_ast_build_set_options(build, options);
7419 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
7420 tree = isl_ast_build_node_from_schedule_map(build, schedule);
7421 isl_ast_build_free(build);
7422 isl_ast_node_free(tree);
7423 if (!tree)
7424 return -1;
7426 return 0;
7429 /* Check that the expression
7431 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
7433 * is not combined into
7435 * min(n/2, 0)
7437 * as this would result in n/2 being evaluated in parts of
7438 * the definition domain where n is not a multiple of 2.
7440 static int test_ast_expr(isl_ctx *ctx)
7442 const char *str;
7443 isl_pw_aff *pa;
7444 isl_ast_build *build;
7445 isl_ast_expr *expr;
7446 int min_max;
7447 int is_min;
7449 min_max = isl_options_get_ast_build_detect_min_max(ctx);
7450 isl_options_set_ast_build_detect_min_max(ctx, 1);
7452 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
7453 pa = isl_pw_aff_read_from_str(ctx, str);
7454 build = isl_ast_build_alloc(ctx);
7455 expr = isl_ast_build_expr_from_pw_aff(build, pa);
7456 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
7457 isl_ast_expr_get_op_type(expr) == isl_ast_op_min;
7458 isl_ast_build_free(build);
7459 isl_ast_expr_free(expr);
7461 isl_options_set_ast_build_detect_min_max(ctx, min_max);
7463 if (!expr)
7464 return -1;
7465 if (is_min)
7466 isl_die(ctx, isl_error_unknown,
7467 "expressions should not be combined", return -1);
7469 return 0;
7472 static int test_ast_gen(isl_ctx *ctx)
7474 if (test_ast_gen1(ctx) < 0)
7475 return -1;
7476 if (test_ast_gen2(ctx) < 0)
7477 return -1;
7478 if (test_ast_gen3(ctx) < 0)
7479 return -1;
7480 if (test_ast_gen4(ctx) < 0)
7481 return -1;
7482 if (test_ast_gen5(ctx) < 0)
7483 return -1;
7484 if (test_ast_expr(ctx) < 0)
7485 return -1;
7486 return 0;
7489 /* Check if dropping output dimensions from an isl_pw_multi_aff
7490 * works properly.
7492 static int test_pw_multi_aff(isl_ctx *ctx)
7494 const char *str;
7495 isl_pw_multi_aff *pma1, *pma2;
7496 int equal;
7498 str = "{ [i,j] -> [i+j, 4i-j] }";
7499 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
7500 str = "{ [i,j] -> [4i-j] }";
7501 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
7503 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
7505 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7507 isl_pw_multi_aff_free(pma1);
7508 isl_pw_multi_aff_free(pma2);
7509 if (equal < 0)
7510 return -1;
7511 if (!equal)
7512 isl_die(ctx, isl_error_unknown,
7513 "expressions not equal", return -1);
7515 return 0;
7518 /* Check that we can properly parse multi piecewise affine expressions
7519 * where the piecewise affine expressions have different domains.
7521 static int test_multi_pw_aff(isl_ctx *ctx)
7523 const char *str;
7524 isl_set *dom, *dom2;
7525 isl_multi_pw_aff *mpa1, *mpa2;
7526 isl_pw_aff *pa;
7527 int equal;
7528 int equal_domain;
7530 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
7531 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
7532 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
7533 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
7534 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
7535 str = "{ [i] -> [(i : i > 0), 2i] }";
7536 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
7538 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
7540 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
7541 dom = isl_pw_aff_domain(pa);
7542 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
7543 dom2 = isl_pw_aff_domain(pa);
7544 equal_domain = isl_set_is_equal(dom, dom2);
7546 isl_set_free(dom);
7547 isl_set_free(dom2);
7548 isl_multi_pw_aff_free(mpa1);
7549 isl_multi_pw_aff_free(mpa2);
7551 if (equal < 0)
7552 return -1;
7553 if (!equal)
7554 isl_die(ctx, isl_error_unknown,
7555 "expressions not equal", return -1);
7557 if (equal_domain < 0)
7558 return -1;
7559 if (equal_domain)
7560 isl_die(ctx, isl_error_unknown,
7561 "domains unexpectedly equal", return -1);
7563 return 0;
7566 /* This is a regression test for a bug where isl_basic_map_simplify
7567 * would end up in an infinite loop. In particular, we construct
7568 * an empty basic set that is not obviously empty.
7569 * isl_basic_set_is_empty marks the basic set as empty.
7570 * After projecting out i3, the variable can be dropped completely,
7571 * but isl_basic_map_simplify refrains from doing so if the basic set
7572 * is empty and would end up in an infinite loop if it didn't test
7573 * explicitly for empty basic maps in the outer loop.
7575 static int test_simplify_1(isl_ctx *ctx)
7577 const char *str;
7578 isl_basic_set *bset;
7579 int empty;
7581 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
7582 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
7583 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
7584 "i3 >= i2 }";
7585 bset = isl_basic_set_read_from_str(ctx, str);
7586 empty = isl_basic_set_is_empty(bset);
7587 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
7588 isl_basic_set_free(bset);
7589 if (!bset)
7590 return -1;
7591 if (!empty)
7592 isl_die(ctx, isl_error_unknown,
7593 "basic set should be empty", return -1);
7595 return 0;
7598 /* Check that the equality in the set description below
7599 * is simplified away.
7601 static int test_simplify_2(isl_ctx *ctx)
7603 const char *str;
7604 isl_basic_set *bset;
7605 isl_bool universe;
7607 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
7608 bset = isl_basic_set_read_from_str(ctx, str);
7609 universe = isl_basic_set_plain_is_universe(bset);
7610 isl_basic_set_free(bset);
7612 if (universe < 0)
7613 return -1;
7614 if (!universe)
7615 isl_die(ctx, isl_error_unknown,
7616 "equality not simplified away", return -1);
7617 return 0;
7620 /* Some simplification tests.
7622 static int test_simplify(isl_ctx *ctx)
7624 if (test_simplify_1(ctx) < 0)
7625 return -1;
7626 if (test_simplify_2(ctx) < 0)
7627 return -1;
7628 return 0;
7631 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
7632 * with gbr context would fail to disable the use of the shifted tableau
7633 * when transferring equalities for the input to the context, resulting
7634 * in invalid sample values.
7636 static int test_partial_lexmin(isl_ctx *ctx)
7638 const char *str;
7639 isl_basic_set *bset;
7640 isl_basic_map *bmap;
7641 isl_map *map;
7643 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
7644 bmap = isl_basic_map_read_from_str(ctx, str);
7645 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
7646 bset = isl_basic_set_read_from_str(ctx, str);
7647 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
7648 isl_map_free(map);
7650 if (!map)
7651 return -1;
7653 return 0;
7656 /* Check that the variable compression performed on the existentially
7657 * quantified variables inside isl_basic_set_compute_divs is not confused
7658 * by the implicit equalities among the parameters.
7660 static int test_compute_divs(isl_ctx *ctx)
7662 const char *str;
7663 isl_basic_set *bset;
7664 isl_set *set;
7666 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
7667 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
7668 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
7669 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
7670 bset = isl_basic_set_read_from_str(ctx, str);
7671 set = isl_basic_set_compute_divs(bset);
7672 isl_set_free(set);
7673 if (!set)
7674 return -1;
7676 return 0;
7679 /* Check that isl_schedule_get_map is not confused by a schedule tree
7680 * with divergent filter node parameters, as can result from a call
7681 * to isl_schedule_intersect_domain.
7683 static int test_schedule_tree(isl_ctx *ctx)
7685 const char *str;
7686 isl_union_set *uset;
7687 isl_schedule *sched1, *sched2;
7688 isl_union_map *umap;
7690 uset = isl_union_set_read_from_str(ctx, "{ A[i] }");
7691 sched1 = isl_schedule_from_domain(uset);
7692 uset = isl_union_set_read_from_str(ctx, "{ B[] }");
7693 sched2 = isl_schedule_from_domain(uset);
7695 sched1 = isl_schedule_sequence(sched1, sched2);
7696 str = "[n] -> { A[i] : 0 <= i < n; B[] }";
7697 uset = isl_union_set_read_from_str(ctx, str);
7698 sched1 = isl_schedule_intersect_domain(sched1, uset);
7699 umap = isl_schedule_get_map(sched1);
7700 isl_schedule_free(sched1);
7701 isl_union_map_free(umap);
7702 if (!umap)
7703 return -1;
7705 return 0;
7708 /* Check that the reaching domain elements and the prefix schedule
7709 * at a leaf node are the same before and after grouping.
7711 static int test_schedule_tree_group_1(isl_ctx *ctx)
7713 int equal;
7714 const char *str;
7715 isl_id *id;
7716 isl_union_set *uset;
7717 isl_multi_union_pw_aff *mupa;
7718 isl_union_pw_multi_aff *upma1, *upma2;
7719 isl_union_set *domain1, *domain2;
7720 isl_union_map *umap1, *umap2;
7721 isl_schedule_node *node;
7723 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
7724 uset = isl_union_set_read_from_str(ctx, str);
7725 node = isl_schedule_node_from_domain(uset);
7726 node = isl_schedule_node_child(node, 0);
7727 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
7728 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7729 node = isl_schedule_node_insert_partial_schedule(node, mupa);
7730 node = isl_schedule_node_child(node, 0);
7731 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
7732 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7733 node = isl_schedule_node_insert_partial_schedule(node, mupa);
7734 node = isl_schedule_node_child(node, 0);
7735 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
7736 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
7737 domain1 = isl_schedule_node_get_domain(node);
7738 id = isl_id_alloc(ctx, "group", NULL);
7739 node = isl_schedule_node_parent(node);
7740 node = isl_schedule_node_group(node, id);
7741 node = isl_schedule_node_child(node, 0);
7742 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
7743 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
7744 domain2 = isl_schedule_node_get_domain(node);
7745 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
7746 if (equal >= 0 && equal)
7747 equal = isl_union_set_is_equal(domain1, domain2);
7748 if (equal >= 0 && equal)
7749 equal = isl_union_map_is_equal(umap1, umap2);
7750 isl_union_map_free(umap1);
7751 isl_union_map_free(umap2);
7752 isl_union_set_free(domain1);
7753 isl_union_set_free(domain2);
7754 isl_union_pw_multi_aff_free(upma1);
7755 isl_union_pw_multi_aff_free(upma2);
7756 isl_schedule_node_free(node);
7758 if (equal < 0)
7759 return -1;
7760 if (!equal)
7761 isl_die(ctx, isl_error_unknown,
7762 "expressions not equal", return -1);
7764 return 0;
7767 /* Check that we can have nested groupings and that the union map
7768 * schedule representation is the same before and after the grouping.
7769 * Note that after the grouping, the union map representation contains
7770 * the domain constraints from the ranges of the expansion nodes,
7771 * while they are missing from the union map representation of
7772 * the tree without expansion nodes.
7774 * Also check that the global expansion is as expected.
7776 static int test_schedule_tree_group_2(isl_ctx *ctx)
7778 int equal, equal_expansion;
7779 const char *str;
7780 isl_id *id;
7781 isl_union_set *uset;
7782 isl_union_map *umap1, *umap2;
7783 isl_union_map *expansion1, *expansion2;
7784 isl_union_set_list *filters;
7785 isl_multi_union_pw_aff *mupa;
7786 isl_schedule *schedule;
7787 isl_schedule_node *node;
7789 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
7790 "S3[i,j] : 0 <= i,j < 10 }";
7791 uset = isl_union_set_read_from_str(ctx, str);
7792 node = isl_schedule_node_from_domain(uset);
7793 node = isl_schedule_node_child(node, 0);
7794 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
7795 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7796 node = isl_schedule_node_insert_partial_schedule(node, mupa);
7797 node = isl_schedule_node_child(node, 0);
7798 str = "{ S1[i,j] }";
7799 uset = isl_union_set_read_from_str(ctx, str);
7800 filters = isl_union_set_list_from_union_set(uset);
7801 str = "{ S2[i,j]; S3[i,j] }";
7802 uset = isl_union_set_read_from_str(ctx, str);
7803 filters = isl_union_set_list_add(filters, uset);
7804 node = isl_schedule_node_insert_sequence(node, filters);
7805 node = isl_schedule_node_child(node, 1);
7806 node = isl_schedule_node_child(node, 0);
7807 str = "{ S2[i,j] }";
7808 uset = isl_union_set_read_from_str(ctx, str);
7809 filters = isl_union_set_list_from_union_set(uset);
7810 str = "{ S3[i,j] }";
7811 uset = isl_union_set_read_from_str(ctx, str);
7812 filters = isl_union_set_list_add(filters, uset);
7813 node = isl_schedule_node_insert_sequence(node, filters);
7815 schedule = isl_schedule_node_get_schedule(node);
7816 umap1 = isl_schedule_get_map(schedule);
7817 uset = isl_schedule_get_domain(schedule);
7818 umap1 = isl_union_map_intersect_domain(umap1, uset);
7819 isl_schedule_free(schedule);
7821 node = isl_schedule_node_parent(node);
7822 node = isl_schedule_node_parent(node);
7823 id = isl_id_alloc(ctx, "group1", NULL);
7824 node = isl_schedule_node_group(node, id);
7825 node = isl_schedule_node_child(node, 1);
7826 node = isl_schedule_node_child(node, 0);
7827 id = isl_id_alloc(ctx, "group2", NULL);
7828 node = isl_schedule_node_group(node, id);
7830 schedule = isl_schedule_node_get_schedule(node);
7831 umap2 = isl_schedule_get_map(schedule);
7832 isl_schedule_free(schedule);
7834 node = isl_schedule_node_root(node);
7835 node = isl_schedule_node_child(node, 0);
7836 expansion1 = isl_schedule_node_get_subtree_expansion(node);
7837 isl_schedule_node_free(node);
7839 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
7840 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
7841 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
7843 expansion2 = isl_union_map_read_from_str(ctx, str);
7845 equal = isl_union_map_is_equal(umap1, umap2);
7846 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
7848 isl_union_map_free(umap1);
7849 isl_union_map_free(umap2);
7850 isl_union_map_free(expansion1);
7851 isl_union_map_free(expansion2);
7853 if (equal < 0 || equal_expansion < 0)
7854 return -1;
7855 if (!equal)
7856 isl_die(ctx, isl_error_unknown,
7857 "expressions not equal", return -1);
7858 if (!equal_expansion)
7859 isl_die(ctx, isl_error_unknown,
7860 "unexpected expansion", return -1);
7862 return 0;
7865 /* Some tests for the isl_schedule_node_group function.
7867 static int test_schedule_tree_group(isl_ctx *ctx)
7869 if (test_schedule_tree_group_1(ctx) < 0)
7870 return -1;
7871 if (test_schedule_tree_group_2(ctx) < 0)
7872 return -1;
7873 return 0;
7876 struct {
7877 const char *set;
7878 const char *dual;
7879 } coef_tests[] = {
7880 { "{ rat: [i] : 0 <= i <= 10 }",
7881 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
7882 { "{ rat: [i] : FALSE }",
7883 "{ rat: coefficients[[cst] -> [a]] }" },
7884 { "{ rat: [i] : }",
7885 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
7888 struct {
7889 const char *set;
7890 const char *dual;
7891 } sol_tests[] = {
7892 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
7893 "{ rat: [i] : 0 <= i <= 10 }" },
7894 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
7895 "{ rat: [i] }" },
7896 { "{ rat: coefficients[[cst] -> [a]] }",
7897 "{ rat: [i] : FALSE }" },
7900 /* Test the basic functionality of isl_basic_set_coefficients and
7901 * isl_basic_set_solutions.
7903 static int test_dual(isl_ctx *ctx)
7905 int i;
7907 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
7908 int equal;
7909 isl_basic_set *bset1, *bset2;
7911 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
7912 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
7913 bset1 = isl_basic_set_coefficients(bset1);
7914 equal = isl_basic_set_is_equal(bset1, bset2);
7915 isl_basic_set_free(bset1);
7916 isl_basic_set_free(bset2);
7917 if (equal < 0)
7918 return -1;
7919 if (!equal)
7920 isl_die(ctx, isl_error_unknown,
7921 "incorrect dual", return -1);
7924 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
7925 int equal;
7926 isl_basic_set *bset1, *bset2;
7928 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
7929 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
7930 bset1 = isl_basic_set_solutions(bset1);
7931 equal = isl_basic_set_is_equal(bset1, bset2);
7932 isl_basic_set_free(bset1);
7933 isl_basic_set_free(bset2);
7934 if (equal < 0)
7935 return -1;
7936 if (!equal)
7937 isl_die(ctx, isl_error_unknown,
7938 "incorrect dual", return -1);
7941 return 0;
7944 struct {
7945 int scale_tile;
7946 int shift_point;
7947 const char *domain;
7948 const char *schedule;
7949 const char *sizes;
7950 const char *tile;
7951 const char *point;
7952 } tile_tests[] = {
7953 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
7954 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7955 "{ [32,32] }",
7956 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
7957 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7959 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
7960 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7961 "{ [32,32] }",
7962 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
7963 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7965 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
7966 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7967 "{ [32,32] }",
7968 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
7969 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
7971 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
7972 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7973 "{ [32,32] }",
7974 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
7975 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
7979 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
7980 * tile the band and then check if the tile and point bands have the
7981 * expected partial schedule.
7983 static int test_tile(isl_ctx *ctx)
7985 int i;
7986 int scale;
7987 int shift;
7989 scale = isl_options_get_tile_scale_tile_loops(ctx);
7990 shift = isl_options_get_tile_shift_point_loops(ctx);
7992 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
7993 int opt;
7994 int equal;
7995 const char *str;
7996 isl_union_set *domain;
7997 isl_multi_union_pw_aff *mupa, *mupa2;
7998 isl_schedule_node *node;
7999 isl_multi_val *sizes;
8001 opt = tile_tests[i].scale_tile;
8002 isl_options_set_tile_scale_tile_loops(ctx, opt);
8003 opt = tile_tests[i].shift_point;
8004 isl_options_set_tile_shift_point_loops(ctx, opt);
8006 str = tile_tests[i].domain;
8007 domain = isl_union_set_read_from_str(ctx, str);
8008 node = isl_schedule_node_from_domain(domain);
8009 node = isl_schedule_node_child(node, 0);
8010 str = tile_tests[i].schedule;
8011 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8012 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8013 str = tile_tests[i].sizes;
8014 sizes = isl_multi_val_read_from_str(ctx, str);
8015 node = isl_schedule_node_band_tile(node, sizes);
8017 str = tile_tests[i].tile;
8018 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8019 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
8020 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
8021 isl_multi_union_pw_aff_free(mupa);
8022 isl_multi_union_pw_aff_free(mupa2);
8024 node = isl_schedule_node_child(node, 0);
8026 str = tile_tests[i].point;
8027 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8028 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
8029 if (equal >= 0 && equal)
8030 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
8031 mupa2);
8032 isl_multi_union_pw_aff_free(mupa);
8033 isl_multi_union_pw_aff_free(mupa2);
8035 isl_schedule_node_free(node);
8037 if (equal < 0)
8038 return -1;
8039 if (!equal)
8040 isl_die(ctx, isl_error_unknown,
8041 "unexpected result", return -1);
8044 isl_options_set_tile_scale_tile_loops(ctx, scale);
8045 isl_options_set_tile_shift_point_loops(ctx, shift);
8047 return 0;
8050 /* Check that the domain hash of a space is equal to the hash
8051 * of the domain of the space.
8053 static int test_domain_hash(isl_ctx *ctx)
8055 isl_map *map;
8056 isl_space *space;
8057 uint32_t hash1, hash2;
8059 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
8060 space = isl_map_get_space(map);
8061 isl_map_free(map);
8062 hash1 = isl_space_get_domain_hash(space);
8063 space = isl_space_domain(space);
8064 hash2 = isl_space_get_hash(space);
8065 isl_space_free(space);
8067 if (!space)
8068 return -1;
8069 if (hash1 != hash2)
8070 isl_die(ctx, isl_error_unknown,
8071 "domain hash not equal to hash of domain", return -1);
8073 return 0;
8076 /* Check that a universe basic set that is not obviously equal to the universe
8077 * is still recognized as being equal to the universe.
8079 static int test_universe(isl_ctx *ctx)
8081 const char *s;
8082 isl_basic_set *bset;
8083 isl_bool is_univ;
8085 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
8086 bset = isl_basic_set_read_from_str(ctx, s);
8087 is_univ = isl_basic_set_is_universe(bset);
8088 isl_basic_set_free(bset);
8090 if (is_univ < 0)
8091 return -1;
8092 if (!is_univ)
8093 isl_die(ctx, isl_error_unknown,
8094 "not recognized as universe set", return -1);
8096 return 0;
8099 /* Sets for which chambers are computed and checked.
8101 const char *chambers_tests[] = {
8102 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
8103 "z >= 0 and z <= C - y and z <= B - x - y }",
8106 /* Add the domain of "cell" to "cells".
8108 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
8110 isl_basic_set_list **cells = user;
8111 isl_basic_set *dom;
8113 dom = isl_cell_get_domain(cell);
8114 isl_cell_free(cell);
8115 *cells = isl_basic_set_list_add(*cells, dom);
8117 return *cells ? isl_stat_ok : isl_stat_error;
8120 /* Check that the elements of "list" are pairwise disjoint.
8122 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
8124 int i, j, n;
8126 if (!list)
8127 return isl_stat_error;
8129 n = isl_basic_set_list_n_basic_set(list);
8130 for (i = 0; i < n; ++i) {
8131 isl_basic_set *bset_i;
8133 bset_i = isl_basic_set_list_get_basic_set(list, i);
8134 for (j = i + 1; j < n; ++j) {
8135 isl_basic_set *bset_j;
8136 isl_bool disjoint;
8138 bset_j = isl_basic_set_list_get_basic_set(list, j);
8139 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
8140 isl_basic_set_free(bset_j);
8141 if (!disjoint)
8142 isl_die(isl_basic_set_list_get_ctx(list),
8143 isl_error_unknown, "not disjoint",
8144 break);
8145 if (disjoint < 0 || !disjoint)
8146 break;
8148 isl_basic_set_free(bset_i);
8149 if (j < n)
8150 return isl_stat_error;
8153 return isl_stat_ok;
8156 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
8157 * are pairwise disjoint.
8159 static int test_chambers(isl_ctx *ctx)
8161 int i;
8163 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
8164 isl_basic_set *bset;
8165 isl_vertices *vertices;
8166 isl_basic_set_list *cells;
8167 isl_stat ok;
8169 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
8170 vertices = isl_basic_set_compute_vertices(bset);
8171 cells = isl_basic_set_list_alloc(ctx, 0);
8172 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
8173 &cells) < 0)
8174 cells = isl_basic_set_list_free(cells);
8175 ok = check_pairwise_disjoint(cells);
8176 isl_basic_set_list_free(cells);
8177 isl_vertices_free(vertices);
8178 isl_basic_set_free(bset);
8180 if (ok < 0)
8181 return -1;
8184 return 0;
8187 struct {
8188 const char *name;
8189 int (*fn)(isl_ctx *ctx);
8190 } tests [] = {
8191 { "universe", &test_universe },
8192 { "domain hash", &test_domain_hash },
8193 { "dual", &test_dual },
8194 { "dependence analysis", &test_flow },
8195 { "val", &test_val },
8196 { "compute divs", &test_compute_divs },
8197 { "partial lexmin", &test_partial_lexmin },
8198 { "simplify", &test_simplify },
8199 { "curry", &test_curry },
8200 { "piecewise multi affine expressions", &test_pw_multi_aff },
8201 { "multi piecewise affine expressions", &test_multi_pw_aff },
8202 { "conversion", &test_conversion },
8203 { "list", &test_list },
8204 { "align parameters", &test_align_parameters },
8205 { "preimage", &test_preimage },
8206 { "pullback", &test_pullback },
8207 { "AST", &test_ast },
8208 { "AST build", &test_ast_build },
8209 { "AST generation", &test_ast_gen },
8210 { "eliminate", &test_eliminate },
8211 { "residue class", &test_residue_class },
8212 { "div", &test_div },
8213 { "slice", &test_slice },
8214 { "fixed power", &test_fixed_power },
8215 { "sample", &test_sample },
8216 { "output", &test_output },
8217 { "vertices", &test_vertices },
8218 { "chambers", &test_chambers },
8219 { "fixed", &test_fixed },
8220 { "equal", &test_equal },
8221 { "disjoint", &test_disjoint },
8222 { "product", &test_product },
8223 { "dim_max", &test_dim_max },
8224 { "affine", &test_aff },
8225 { "injective", &test_injective },
8226 { "schedule (whole component)", &test_schedule_whole },
8227 { "schedule (incremental)", &test_schedule_incremental },
8228 { "schedule tree", &test_schedule_tree },
8229 { "schedule tree grouping", &test_schedule_tree_group },
8230 { "tile", &test_tile },
8231 { "union_pw", &test_union_pw },
8232 { "locus", &test_locus },
8233 { "eval", &test_eval },
8234 { "parse", &test_parse },
8235 { "single-valued", &test_sv },
8236 { "affine hull", &test_affine_hull },
8237 { "simple_hull", &test_simple_hull },
8238 { "coalesce", &test_coalesce },
8239 { "factorize", &test_factorize },
8240 { "subset", &test_subset },
8241 { "subtract", &test_subtract },
8242 { "intersect", &test_intersect },
8243 { "lexmin", &test_lexmin },
8244 { "min", &test_min },
8245 { "gist", &test_gist },
8246 { "piecewise quasi-polynomials", &test_pwqp },
8247 { "lift", &test_lift },
8248 { "bound", &test_bound },
8249 { "union", &test_union },
8250 { "split periods", &test_split_periods },
8251 { "lexicographic order", &test_lex },
8252 { "bijectivity", &test_bijective },
8253 { "dataflow analysis", &test_dep },
8254 { "reading", &test_read },
8255 { "bounded", &test_bounded },
8256 { "construction", &test_construction },
8257 { "dimension manipulation", &test_dim },
8258 { "map application", &test_application },
8259 { "convex hull", &test_convex_hull },
8260 { "transitive closure", &test_closure },
8263 int main(int argc, char **argv)
8265 int i;
8266 struct isl_ctx *ctx;
8267 struct isl_options *options;
8269 options = isl_options_new_with_defaults();
8270 assert(options);
8271 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
8273 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
8274 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
8275 printf("%s\n", tests[i].name);
8276 if (tests[i].fn(ctx) < 0)
8277 goto error;
8279 isl_ctx_free(ctx);
8280 return 0;
8281 error:
8282 isl_ctx_free(ctx);
8283 return -1;