isl_test.c: test_gist: check internal consistency
[isl.git] / isl_test.c
blob0746d622b0f8213f4525ecee9b29e345f93c3d9a
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.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 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
44 static char *srcdir;
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 }" },
195 int test_parse(struct isl_ctx *ctx)
197 int i;
198 isl_map *map, *map2;
199 const char *str, *str2;
201 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
202 return -1;
203 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
204 return -1;
205 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
206 return -1;
208 str = "{ [i] -> [-i] }";
209 map = isl_map_read_from_str(ctx, str);
210 assert(map);
211 isl_map_free(map);
213 str = "{ A[i] -> L[([i/3])] }";
214 map = isl_map_read_from_str(ctx, str);
215 assert(map);
216 isl_map_free(map);
218 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
219 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
220 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
222 for (i = 0; i < ARRAY_SIZE(parse_map_equal_tests); ++i) {
223 str = parse_map_equal_tests[i].map1;
224 str2 = parse_map_equal_tests[i].map2;
225 if (test_parse_map_equal(ctx, str, str2) < 0)
226 return -1;
229 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
230 map = isl_map_read_from_str(ctx, str);
231 str = "{ [new, old] -> [o0, o1] : "
232 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
233 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
234 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
235 map2 = isl_map_read_from_str(ctx, str);
236 assert(isl_map_is_equal(map, map2));
237 isl_map_free(map);
238 isl_map_free(map2);
240 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
241 map = isl_map_read_from_str(ctx, str);
242 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
243 map2 = isl_map_read_from_str(ctx, str);
244 assert(isl_map_is_equal(map, map2));
245 isl_map_free(map);
246 isl_map_free(map2);
248 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
249 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
250 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
251 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
253 return 0;
256 static int test_read(isl_ctx *ctx)
258 char *filename;
259 FILE *input;
260 isl_basic_set *bset1, *bset2;
261 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
262 int equal;
264 filename = get_filename(ctx, "set", "omega");
265 assert(filename);
266 input = fopen(filename, "r");
267 assert(input);
269 bset1 = isl_basic_set_read_from_file(ctx, input);
270 bset2 = isl_basic_set_read_from_str(ctx, str);
272 equal = isl_basic_set_is_equal(bset1, bset2);
274 isl_basic_set_free(bset1);
275 isl_basic_set_free(bset2);
276 free(filename);
278 fclose(input);
280 if (equal < 0)
281 return -1;
282 if (!equal)
283 isl_die(ctx, isl_error_unknown,
284 "read sets not equal", return -1);
286 return 0;
289 static int test_bounded(isl_ctx *ctx)
291 isl_set *set;
292 int bounded;
294 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
295 bounded = isl_set_is_bounded(set);
296 isl_set_free(set);
298 if (bounded < 0)
299 return -1;
300 if (!bounded)
301 isl_die(ctx, isl_error_unknown,
302 "set not considered bounded", return -1);
304 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
305 bounded = isl_set_is_bounded(set);
306 assert(!bounded);
307 isl_set_free(set);
309 if (bounded < 0)
310 return -1;
311 if (bounded)
312 isl_die(ctx, isl_error_unknown,
313 "set considered bounded", return -1);
315 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
316 bounded = isl_set_is_bounded(set);
317 isl_set_free(set);
319 if (bounded < 0)
320 return -1;
321 if (bounded)
322 isl_die(ctx, isl_error_unknown,
323 "set considered bounded", return -1);
325 return 0;
328 /* Construct the basic set { [i] : 5 <= i <= N } */
329 static int test_construction(isl_ctx *ctx)
331 isl_int v;
332 isl_space *dim;
333 isl_local_space *ls;
334 isl_basic_set *bset;
335 isl_constraint *c;
337 isl_int_init(v);
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 isl_int_set_si(v, -1);
345 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
346 isl_int_set_si(v, 1);
347 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
348 bset = isl_basic_set_add_constraint(bset, c);
350 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
351 isl_int_set_si(v, 1);
352 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
353 isl_int_set_si(v, -5);
354 isl_constraint_set_constant(c, v);
355 bset = isl_basic_set_add_constraint(bset, c);
357 isl_local_space_free(ls);
358 isl_basic_set_free(bset);
360 isl_int_clear(v);
362 return 0;
365 static int test_dim(isl_ctx *ctx)
367 const char *str;
368 isl_map *map1, *map2;
369 int equal;
371 map1 = isl_map_read_from_str(ctx,
372 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
373 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
374 map2 = isl_map_read_from_str(ctx,
375 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
376 equal = isl_map_is_equal(map1, map2);
377 isl_map_free(map2);
379 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
380 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
381 if (equal >= 0 && equal)
382 equal = isl_map_is_equal(map1, map2);
384 isl_map_free(map1);
385 isl_map_free(map2);
387 if (equal < 0)
388 return -1;
389 if (!equal)
390 isl_die(ctx, isl_error_unknown,
391 "unexpected result", return -1);
393 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
394 map1 = isl_map_read_from_str(ctx, str);
395 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
396 map2 = isl_map_read_from_str(ctx, str);
397 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
398 equal = isl_map_is_equal(map1, map2);
399 isl_map_free(map1);
400 isl_map_free(map2);
402 if (equal < 0)
403 return -1;
404 if (!equal)
405 isl_die(ctx, isl_error_unknown,
406 "unexpected result", return -1);
408 return 0;
411 struct {
412 __isl_give isl_val *(*op)(__isl_take isl_val *v);
413 const char *arg;
414 const char *res;
415 } val_un_tests[] = {
416 { &isl_val_neg, "0", "0" },
417 { &isl_val_abs, "0", "0" },
418 { &isl_val_2exp, "0", "1" },
419 { &isl_val_floor, "0", "0" },
420 { &isl_val_ceil, "0", "0" },
421 { &isl_val_neg, "1", "-1" },
422 { &isl_val_neg, "-1", "1" },
423 { &isl_val_neg, "1/2", "-1/2" },
424 { &isl_val_neg, "-1/2", "1/2" },
425 { &isl_val_neg, "infty", "-infty" },
426 { &isl_val_neg, "-infty", "infty" },
427 { &isl_val_neg, "NaN", "NaN" },
428 { &isl_val_abs, "1", "1" },
429 { &isl_val_abs, "-1", "1" },
430 { &isl_val_abs, "1/2", "1/2" },
431 { &isl_val_abs, "-1/2", "1/2" },
432 { &isl_val_abs, "infty", "infty" },
433 { &isl_val_abs, "-infty", "infty" },
434 { &isl_val_abs, "NaN", "NaN" },
435 { &isl_val_floor, "1", "1" },
436 { &isl_val_floor, "-1", "-1" },
437 { &isl_val_floor, "1/2", "0" },
438 { &isl_val_floor, "-1/2", "-1" },
439 { &isl_val_floor, "infty", "infty" },
440 { &isl_val_floor, "-infty", "-infty" },
441 { &isl_val_floor, "NaN", "NaN" },
442 { &isl_val_ceil, "1", "1" },
443 { &isl_val_ceil, "-1", "-1" },
444 { &isl_val_ceil, "1/2", "1" },
445 { &isl_val_ceil, "-1/2", "0" },
446 { &isl_val_ceil, "infty", "infty" },
447 { &isl_val_ceil, "-infty", "-infty" },
448 { &isl_val_ceil, "NaN", "NaN" },
449 { &isl_val_2exp, "-3", "1/8" },
450 { &isl_val_2exp, "-1", "1/2" },
451 { &isl_val_2exp, "1", "2" },
452 { &isl_val_2exp, "2", "4" },
453 { &isl_val_2exp, "3", "8" },
454 { &isl_val_inv, "1", "1" },
455 { &isl_val_inv, "2", "1/2" },
456 { &isl_val_inv, "1/2", "2" },
457 { &isl_val_inv, "-2", "-1/2" },
458 { &isl_val_inv, "-1/2", "-2" },
459 { &isl_val_inv, "0", "NaN" },
460 { &isl_val_inv, "NaN", "NaN" },
461 { &isl_val_inv, "infty", "0" },
462 { &isl_val_inv, "-infty", "0" },
465 /* Perform some basic tests of unary operations on isl_val objects.
467 static int test_un_val(isl_ctx *ctx)
469 int i;
470 isl_val *v, *res;
471 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
472 int ok;
474 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
475 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
476 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
477 fn = val_un_tests[i].op;
478 v = fn(v);
479 if (isl_val_is_nan(res))
480 ok = isl_val_is_nan(v);
481 else
482 ok = isl_val_eq(v, res);
483 isl_val_free(v);
484 isl_val_free(res);
485 if (ok < 0)
486 return -1;
487 if (!ok)
488 isl_die(ctx, isl_error_unknown,
489 "unexpected result", return -1);
492 return 0;
495 struct {
496 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
497 __isl_take isl_val *v2);
498 } val_bin_op[] = {
499 ['+'] = { &isl_val_add },
500 ['-'] = { &isl_val_sub },
501 ['*'] = { &isl_val_mul },
502 ['/'] = { &isl_val_div },
503 ['g'] = { &isl_val_gcd },
504 ['m'] = { &isl_val_min },
505 ['M'] = { &isl_val_max },
508 struct {
509 const char *arg1;
510 unsigned char op;
511 const char *arg2;
512 const char *res;
513 } val_bin_tests[] = {
514 { "0", '+', "0", "0" },
515 { "1", '+', "0", "1" },
516 { "1", '+', "1", "2" },
517 { "1", '-', "1", "0" },
518 { "1", '*', "1", "1" },
519 { "1", '/', "1", "1" },
520 { "2", '*', "3", "6" },
521 { "2", '*', "1/2", "1" },
522 { "2", '*', "1/3", "2/3" },
523 { "2/3", '*', "3/5", "2/5" },
524 { "2/3", '*', "7/5", "14/15" },
525 { "2", '/', "1/2", "4" },
526 { "-2", '/', "-1/2", "4" },
527 { "-2", '/', "1/2", "-4" },
528 { "2", '/', "-1/2", "-4" },
529 { "2", '/', "2", "1" },
530 { "2", '/', "3", "2/3" },
531 { "2/3", '/', "5/3", "2/5" },
532 { "2/3", '/', "5/7", "14/15" },
533 { "0", '/', "0", "NaN" },
534 { "42", '/', "0", "NaN" },
535 { "-42", '/', "0", "NaN" },
536 { "infty", '/', "0", "NaN" },
537 { "-infty", '/', "0", "NaN" },
538 { "NaN", '/', "0", "NaN" },
539 { "0", '/', "NaN", "NaN" },
540 { "42", '/', "NaN", "NaN" },
541 { "-42", '/', "NaN", "NaN" },
542 { "infty", '/', "NaN", "NaN" },
543 { "-infty", '/', "NaN", "NaN" },
544 { "NaN", '/', "NaN", "NaN" },
545 { "0", '/', "infty", "0" },
546 { "42", '/', "infty", "0" },
547 { "-42", '/', "infty", "0" },
548 { "infty", '/', "infty", "NaN" },
549 { "-infty", '/', "infty", "NaN" },
550 { "NaN", '/', "infty", "NaN" },
551 { "0", '/', "-infty", "0" },
552 { "42", '/', "-infty", "0" },
553 { "-42", '/', "-infty", "0" },
554 { "infty", '/', "-infty", "NaN" },
555 { "-infty", '/', "-infty", "NaN" },
556 { "NaN", '/', "-infty", "NaN" },
557 { "1", '-', "1/3", "2/3" },
558 { "1/3", '+', "1/2", "5/6" },
559 { "1/2", '+', "1/2", "1" },
560 { "3/4", '-', "1/4", "1/2" },
561 { "1/2", '-', "1/3", "1/6" },
562 { "infty", '+', "42", "infty" },
563 { "infty", '+', "infty", "infty" },
564 { "42", '+', "infty", "infty" },
565 { "infty", '-', "infty", "NaN" },
566 { "infty", '*', "infty", "infty" },
567 { "infty", '*', "-infty", "-infty" },
568 { "-infty", '*', "infty", "-infty" },
569 { "-infty", '*', "-infty", "infty" },
570 { "0", '*', "infty", "NaN" },
571 { "1", '*', "infty", "infty" },
572 { "infty", '*', "0", "NaN" },
573 { "infty", '*', "42", "infty" },
574 { "42", '-', "infty", "-infty" },
575 { "infty", '+', "-infty", "NaN" },
576 { "4", 'g', "6", "2" },
577 { "5", 'g', "6", "1" },
578 { "42", 'm', "3", "3" },
579 { "42", 'M', "3", "42" },
580 { "3", 'm', "42", "3" },
581 { "3", 'M', "42", "42" },
582 { "42", 'm', "infty", "42" },
583 { "42", 'M', "infty", "infty" },
584 { "42", 'm', "-infty", "-infty" },
585 { "42", 'M', "-infty", "42" },
586 { "42", 'm', "NaN", "NaN" },
587 { "42", 'M', "NaN", "NaN" },
588 { "infty", 'm', "-infty", "-infty" },
589 { "infty", 'M', "-infty", "infty" },
592 /* Perform some basic tests of binary operations on isl_val objects.
594 static int test_bin_val(isl_ctx *ctx)
596 int i;
597 isl_val *v1, *v2, *res;
598 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
599 __isl_take isl_val *v2);
600 int ok;
602 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
603 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
604 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
605 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
606 fn = val_bin_op[val_bin_tests[i].op].fn;
607 v1 = fn(v1, v2);
608 if (isl_val_is_nan(res))
609 ok = isl_val_is_nan(v1);
610 else
611 ok = isl_val_eq(v1, res);
612 isl_val_free(v1);
613 isl_val_free(res);
614 if (ok < 0)
615 return -1;
616 if (!ok)
617 isl_die(ctx, isl_error_unknown,
618 "unexpected result", return -1);
621 return 0;
624 /* Perform some basic tests on isl_val objects.
626 static int test_val(isl_ctx *ctx)
628 if (test_un_val(ctx) < 0)
629 return -1;
630 if (test_bin_val(ctx) < 0)
631 return -1;
632 return 0;
635 static int test_div(isl_ctx *ctx)
637 unsigned n;
638 const char *str;
639 int empty;
640 isl_int v;
641 isl_space *dim;
642 isl_set *set;
643 isl_local_space *ls;
644 struct isl_basic_set *bset;
645 struct isl_constraint *c;
647 isl_int_init(v);
649 /* test 1 */
650 dim = isl_space_set_alloc(ctx, 0, 3);
651 bset = isl_basic_set_universe(isl_space_copy(dim));
652 ls = isl_local_space_from_space(dim);
654 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
655 isl_int_set_si(v, -1);
656 isl_constraint_set_constant(c, v);
657 isl_int_set_si(v, 1);
658 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
659 isl_int_set_si(v, 3);
660 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
661 bset = isl_basic_set_add_constraint(bset, c);
663 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
664 isl_int_set_si(v, 1);
665 isl_constraint_set_constant(c, v);
666 isl_int_set_si(v, -1);
667 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
668 isl_int_set_si(v, 3);
669 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
670 bset = isl_basic_set_add_constraint(bset, c);
672 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
674 assert(bset && bset->n_div == 1);
675 isl_local_space_free(ls);
676 isl_basic_set_free(bset);
678 /* test 2 */
679 dim = isl_space_set_alloc(ctx, 0, 3);
680 bset = isl_basic_set_universe(isl_space_copy(dim));
681 ls = isl_local_space_from_space(dim);
683 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
684 isl_int_set_si(v, 1);
685 isl_constraint_set_constant(c, v);
686 isl_int_set_si(v, -1);
687 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
688 isl_int_set_si(v, 3);
689 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
690 bset = isl_basic_set_add_constraint(bset, c);
692 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
693 isl_int_set_si(v, -1);
694 isl_constraint_set_constant(c, v);
695 isl_int_set_si(v, 1);
696 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
697 isl_int_set_si(v, 3);
698 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
699 bset = isl_basic_set_add_constraint(bset, c);
701 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
703 assert(bset && bset->n_div == 1);
704 isl_local_space_free(ls);
705 isl_basic_set_free(bset);
707 /* test 3 */
708 dim = isl_space_set_alloc(ctx, 0, 3);
709 bset = isl_basic_set_universe(isl_space_copy(dim));
710 ls = isl_local_space_from_space(dim);
712 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
713 isl_int_set_si(v, 1);
714 isl_constraint_set_constant(c, v);
715 isl_int_set_si(v, -1);
716 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
717 isl_int_set_si(v, 3);
718 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
719 bset = isl_basic_set_add_constraint(bset, c);
721 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
722 isl_int_set_si(v, -3);
723 isl_constraint_set_constant(c, v);
724 isl_int_set_si(v, 1);
725 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
726 isl_int_set_si(v, 4);
727 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
728 bset = isl_basic_set_add_constraint(bset, c);
730 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
732 assert(bset && bset->n_div == 1);
733 isl_local_space_free(ls);
734 isl_basic_set_free(bset);
736 /* test 4 */
737 dim = isl_space_set_alloc(ctx, 0, 3);
738 bset = isl_basic_set_universe(isl_space_copy(dim));
739 ls = isl_local_space_from_space(dim);
741 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
742 isl_int_set_si(v, 2);
743 isl_constraint_set_constant(c, v);
744 isl_int_set_si(v, -1);
745 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
746 isl_int_set_si(v, 3);
747 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
748 bset = isl_basic_set_add_constraint(bset, c);
750 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
751 isl_int_set_si(v, -1);
752 isl_constraint_set_constant(c, v);
753 isl_int_set_si(v, 1);
754 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
755 isl_int_set_si(v, 6);
756 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
757 bset = isl_basic_set_add_constraint(bset, c);
759 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
761 assert(isl_basic_set_is_empty(bset));
762 isl_local_space_free(ls);
763 isl_basic_set_free(bset);
765 /* test 5 */
766 dim = isl_space_set_alloc(ctx, 0, 3);
767 bset = isl_basic_set_universe(isl_space_copy(dim));
768 ls = isl_local_space_from_space(dim);
770 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
771 isl_int_set_si(v, -1);
772 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
773 isl_int_set_si(v, 3);
774 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
775 bset = isl_basic_set_add_constraint(bset, c);
777 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
778 isl_int_set_si(v, 1);
779 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
780 isl_int_set_si(v, -3);
781 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
782 bset = isl_basic_set_add_constraint(bset, c);
784 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
786 assert(bset && bset->n_div == 0);
787 isl_basic_set_free(bset);
788 isl_local_space_free(ls);
790 /* test 6 */
791 dim = isl_space_set_alloc(ctx, 0, 3);
792 bset = isl_basic_set_universe(isl_space_copy(dim));
793 ls = isl_local_space_from_space(dim);
795 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
796 isl_int_set_si(v, -1);
797 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
798 isl_int_set_si(v, 6);
799 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
800 bset = isl_basic_set_add_constraint(bset, c);
802 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
803 isl_int_set_si(v, 1);
804 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
805 isl_int_set_si(v, -3);
806 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
807 bset = isl_basic_set_add_constraint(bset, c);
809 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
811 assert(bset && bset->n_div == 1);
812 isl_basic_set_free(bset);
813 isl_local_space_free(ls);
815 /* test 7 */
816 /* This test is a bit tricky. We set up an equality
817 * a + 3b + 3c = 6 e0
818 * Normalization of divs creates _two_ divs
819 * a = 3 e0
820 * c - b - e0 = 2 e1
821 * Afterwards e0 is removed again because it has coefficient -1
822 * and we end up with the original equality and div again.
823 * Perhaps we can avoid the introduction of this temporary div.
825 dim = isl_space_set_alloc(ctx, 0, 4);
826 bset = isl_basic_set_universe(isl_space_copy(dim));
827 ls = isl_local_space_from_space(dim);
829 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
830 isl_int_set_si(v, -1);
831 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
832 isl_int_set_si(v, -3);
833 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
834 isl_int_set_si(v, -3);
835 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
836 isl_int_set_si(v, 6);
837 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
838 bset = isl_basic_set_add_constraint(bset, c);
840 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
842 /* Test disabled for now */
844 assert(bset && bset->n_div == 1);
846 isl_local_space_free(ls);
847 isl_basic_set_free(bset);
849 /* test 8 */
850 dim = isl_space_set_alloc(ctx, 0, 5);
851 bset = isl_basic_set_universe(isl_space_copy(dim));
852 ls = isl_local_space_from_space(dim);
854 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
855 isl_int_set_si(v, -1);
856 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
857 isl_int_set_si(v, -3);
858 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
859 isl_int_set_si(v, -3);
860 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
861 isl_int_set_si(v, 6);
862 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
863 bset = isl_basic_set_add_constraint(bset, c);
865 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
866 isl_int_set_si(v, -1);
867 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
868 isl_int_set_si(v, 1);
869 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
870 isl_int_set_si(v, 1);
871 isl_constraint_set_constant(c, v);
872 bset = isl_basic_set_add_constraint(bset, c);
874 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
876 /* Test disabled for now */
878 assert(bset && bset->n_div == 1);
880 isl_local_space_free(ls);
881 isl_basic_set_free(bset);
883 /* test 9 */
884 dim = isl_space_set_alloc(ctx, 0, 4);
885 bset = isl_basic_set_universe(isl_space_copy(dim));
886 ls = isl_local_space_from_space(dim);
888 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
889 isl_int_set_si(v, 1);
890 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
891 isl_int_set_si(v, -1);
892 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
893 isl_int_set_si(v, -2);
894 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
895 bset = isl_basic_set_add_constraint(bset, c);
897 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
898 isl_int_set_si(v, -1);
899 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
900 isl_int_set_si(v, 3);
901 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
902 isl_int_set_si(v, 2);
903 isl_constraint_set_constant(c, v);
904 bset = isl_basic_set_add_constraint(bset, c);
906 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
908 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
910 assert(!isl_basic_set_is_empty(bset));
912 isl_local_space_free(ls);
913 isl_basic_set_free(bset);
915 /* test 10 */
916 dim = isl_space_set_alloc(ctx, 0, 3);
917 bset = isl_basic_set_universe(isl_space_copy(dim));
918 ls = isl_local_space_from_space(dim);
920 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
921 isl_int_set_si(v, 1);
922 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
923 isl_int_set_si(v, -2);
924 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
925 bset = isl_basic_set_add_constraint(bset, c);
927 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
929 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
931 isl_local_space_free(ls);
932 isl_basic_set_free(bset);
934 isl_int_clear(v);
936 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
937 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
938 set = isl_set_read_from_str(ctx, str);
939 set = isl_set_compute_divs(set);
940 isl_set_free(set);
941 if (!set)
942 return -1;
944 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
945 bset = isl_basic_set_read_from_str(ctx, str);
946 n = isl_basic_set_dim(bset, isl_dim_div);
947 isl_basic_set_free(bset);
948 if (!bset)
949 return -1;
950 if (n != 0)
951 isl_die(ctx, isl_error_unknown,
952 "expecting no existentials", return -1);
954 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
955 set = isl_set_read_from_str(ctx, str);
956 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
957 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
958 empty = isl_set_is_empty(set);
959 isl_set_free(set);
960 if (empty < 0)
961 return -1;
962 if (!empty)
963 isl_die(ctx, isl_error_unknown,
964 "result not as accurate as expected", return -1);
966 return 0;
969 void test_application_case(struct isl_ctx *ctx, const char *name)
971 char *filename;
972 FILE *input;
973 struct isl_basic_set *bset1, *bset2;
974 struct isl_basic_map *bmap;
976 filename = get_filename(ctx, name, "omega");
977 assert(filename);
978 input = fopen(filename, "r");
979 assert(input);
981 bset1 = isl_basic_set_read_from_file(ctx, input);
982 bmap = isl_basic_map_read_from_file(ctx, input);
984 bset1 = isl_basic_set_apply(bset1, bmap);
986 bset2 = isl_basic_set_read_from_file(ctx, input);
988 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
990 isl_basic_set_free(bset1);
991 isl_basic_set_free(bset2);
992 free(filename);
994 fclose(input);
997 static int test_application(isl_ctx *ctx)
999 test_application_case(ctx, "application");
1000 test_application_case(ctx, "application2");
1002 return 0;
1005 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1007 char *filename;
1008 FILE *input;
1009 struct isl_basic_set *bset1, *bset2;
1011 filename = get_filename(ctx, name, "polylib");
1012 assert(filename);
1013 input = fopen(filename, "r");
1014 assert(input);
1016 bset1 = isl_basic_set_read_from_file(ctx, input);
1017 bset2 = isl_basic_set_read_from_file(ctx, input);
1019 bset1 = isl_basic_set_affine_hull(bset1);
1021 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1023 isl_basic_set_free(bset1);
1024 isl_basic_set_free(bset2);
1025 free(filename);
1027 fclose(input);
1030 int test_affine_hull(struct isl_ctx *ctx)
1032 const char *str;
1033 isl_set *set;
1034 isl_basic_set *bset, *bset2;
1035 int n;
1036 int subset;
1038 test_affine_hull_case(ctx, "affine2");
1039 test_affine_hull_case(ctx, "affine");
1040 test_affine_hull_case(ctx, "affine3");
1042 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1043 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1044 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1045 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1046 set = isl_set_read_from_str(ctx, str);
1047 bset = isl_set_affine_hull(set);
1048 n = isl_basic_set_dim(bset, isl_dim_div);
1049 isl_basic_set_free(bset);
1050 if (n != 0)
1051 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1052 return -1);
1054 /* Check that isl_map_affine_hull is not confused by
1055 * the reordering of divs in isl_map_align_divs.
1057 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1058 "32e0 = b and 32e1 = c); "
1059 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1060 set = isl_set_read_from_str(ctx, str);
1061 bset = isl_set_affine_hull(set);
1062 isl_basic_set_free(bset);
1063 if (!bset)
1064 return -1;
1066 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1067 "32e2 = 31 + 31e0 }";
1068 set = isl_set_read_from_str(ctx, str);
1069 bset = isl_set_affine_hull(set);
1070 str = "{ [a] : exists e : a = 32 e }";
1071 bset2 = isl_basic_set_read_from_str(ctx, str);
1072 subset = isl_basic_set_is_subset(bset, bset2);
1073 isl_basic_set_free(bset);
1074 isl_basic_set_free(bset2);
1075 if (subset < 0)
1076 return -1;
1077 if (!subset)
1078 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1079 return -1);
1081 return 0;
1084 /* Pairs of maps and the corresponding expected results of
1085 * isl_map_plain_unshifted_simple_hull.
1087 struct {
1088 const char *map;
1089 const char *hull;
1090 } plain_unshifted_simple_hull_tests[] = {
1091 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1092 "{ [i] -> [j] : i >= 1 }" },
1093 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1094 "(j mod 4 = 2 and k mod 6 = n) }",
1095 "{ [n] -> [i,j,k] : j mod 4 = 2 }" },
1098 /* Basic tests for isl_map_plain_unshifted_simple_hull.
1100 static int test_plain_unshifted_simple_hull(isl_ctx *ctx)
1102 int i;
1103 isl_map *map;
1104 isl_basic_map *hull, *expected;
1105 isl_bool equal;
1107 for (i = 0; i < ARRAY_SIZE(plain_unshifted_simple_hull_tests); ++i) {
1108 const char *str;
1109 str = plain_unshifted_simple_hull_tests[i].map;
1110 map = isl_map_read_from_str(ctx, str);
1111 str = plain_unshifted_simple_hull_tests[i].hull;
1112 expected = isl_basic_map_read_from_str(ctx, str);
1113 hull = isl_map_plain_unshifted_simple_hull(map);
1114 equal = isl_basic_map_is_equal(hull, expected);
1115 isl_basic_map_free(hull);
1116 isl_basic_map_free(expected);
1117 if (equal < 0)
1118 return -1;
1119 if (!equal)
1120 isl_die(ctx, isl_error_unknown, "unexpected hull",
1121 return -1);
1124 return 0;
1127 static int test_simple_hull(struct isl_ctx *ctx)
1129 const char *str;
1130 isl_set *set;
1131 isl_basic_set *bset;
1132 isl_bool is_empty;
1134 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1135 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1136 set = isl_set_read_from_str(ctx, str);
1137 bset = isl_set_simple_hull(set);
1138 is_empty = isl_basic_set_is_empty(bset);
1139 isl_basic_set_free(bset);
1141 if (is_empty == isl_bool_error)
1142 return -1;
1144 if (is_empty == isl_bool_false)
1145 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1146 return -1);
1148 if (test_plain_unshifted_simple_hull(ctx) < 0)
1149 return -1;
1151 return 0;
1154 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1156 char *filename;
1157 FILE *input;
1158 struct isl_basic_set *bset1, *bset2;
1159 struct isl_set *set;
1161 filename = get_filename(ctx, name, "polylib");
1162 assert(filename);
1163 input = fopen(filename, "r");
1164 assert(input);
1166 bset1 = isl_basic_set_read_from_file(ctx, input);
1167 bset2 = isl_basic_set_read_from_file(ctx, input);
1169 set = isl_basic_set_union(bset1, bset2);
1170 bset1 = isl_set_convex_hull(set);
1172 bset2 = isl_basic_set_read_from_file(ctx, input);
1174 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1176 isl_basic_set_free(bset1);
1177 isl_basic_set_free(bset2);
1178 free(filename);
1180 fclose(input);
1183 struct {
1184 const char *set;
1185 const char *hull;
1186 } convex_hull_tests[] = {
1187 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1188 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1189 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1190 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1191 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1192 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1193 "i2 <= 5 and i2 >= 4; "
1194 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1195 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1196 "i2 <= 5 + i0 and i2 >= i0 }" },
1197 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1198 "{ [x, y] : 1 = 0 }" },
1201 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1203 int i;
1204 int orig_convex = ctx->opt->convex;
1205 ctx->opt->convex = convex;
1207 test_convex_hull_case(ctx, "convex0");
1208 test_convex_hull_case(ctx, "convex1");
1209 test_convex_hull_case(ctx, "convex2");
1210 test_convex_hull_case(ctx, "convex3");
1211 test_convex_hull_case(ctx, "convex4");
1212 test_convex_hull_case(ctx, "convex5");
1213 test_convex_hull_case(ctx, "convex6");
1214 test_convex_hull_case(ctx, "convex7");
1215 test_convex_hull_case(ctx, "convex8");
1216 test_convex_hull_case(ctx, "convex9");
1217 test_convex_hull_case(ctx, "convex10");
1218 test_convex_hull_case(ctx, "convex11");
1219 test_convex_hull_case(ctx, "convex12");
1220 test_convex_hull_case(ctx, "convex13");
1221 test_convex_hull_case(ctx, "convex14");
1222 test_convex_hull_case(ctx, "convex15");
1224 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1225 isl_set *set1, *set2;
1226 int equal;
1228 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1229 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1230 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1231 equal = isl_set_is_equal(set1, set2);
1232 isl_set_free(set1);
1233 isl_set_free(set2);
1235 if (equal < 0)
1236 return -1;
1237 if (!equal)
1238 isl_die(ctx, isl_error_unknown,
1239 "unexpected convex hull", return -1);
1242 ctx->opt->convex = orig_convex;
1244 return 0;
1247 static int test_convex_hull(isl_ctx *ctx)
1249 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1250 return -1;
1251 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1252 return -1;
1253 return 0;
1256 void test_gist_case(struct isl_ctx *ctx, const char *name)
1258 char *filename;
1259 FILE *input;
1260 struct isl_basic_set *bset1, *bset2;
1262 filename = get_filename(ctx, name, "polylib");
1263 assert(filename);
1264 input = fopen(filename, "r");
1265 assert(input);
1267 bset1 = isl_basic_set_read_from_file(ctx, input);
1268 bset2 = isl_basic_set_read_from_file(ctx, input);
1270 bset1 = isl_basic_set_gist(bset1, bset2);
1272 bset2 = isl_basic_set_read_from_file(ctx, input);
1274 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1276 isl_basic_set_free(bset1);
1277 isl_basic_set_free(bset2);
1278 free(filename);
1280 fclose(input);
1283 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1285 struct {
1286 const char *map;
1287 const char *context;
1288 const char *gist;
1289 } plain_gist_tests[] = {
1290 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1291 "{ [i] -> [j] : i >= 1 }",
1292 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1293 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1294 "(j mod 4 = 2 and k mod 6 = n) }",
1295 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1296 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1297 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1298 "{ [i] -> [j] : i > j }",
1299 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1302 /* Basic tests for isl_map_plain_gist_basic_map.
1304 static int test_plain_gist(isl_ctx *ctx)
1306 int i;
1308 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1309 const char *str;
1310 int equal;
1311 isl_map *map, *gist;
1312 isl_basic_map *context;
1314 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1315 str = plain_gist_tests[i].context;
1316 context = isl_basic_map_read_from_str(ctx, str);
1317 map = isl_map_plain_gist_basic_map(map, context);
1318 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1319 equal = isl_map_is_equal(map, gist);
1320 isl_map_free(map);
1321 isl_map_free(gist);
1322 if (equal < 0)
1323 return -1;
1324 if (!equal)
1325 isl_die(ctx, isl_error_unknown,
1326 "incorrect gist result", return -1);
1329 return 0;
1332 struct {
1333 const char *set;
1334 const char *context;
1335 const char *gist;
1336 } gist_tests[] = {
1337 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1338 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1339 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1340 "{ [a, b, c] : a <= 15 }" },
1341 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1342 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1343 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1344 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1345 { "{ [m, n, a, b] : a <= 2147 + n }",
1346 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1347 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1348 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1349 "b >= 0) }",
1350 "{ [m, n, ku, kl] }" },
1351 { "{ [a, a, b] : a >= 10 }",
1352 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1353 "{ [a, a, b] : a >= 10 }" },
1354 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1355 "{ [0, j] : j >= 0 }" },
1356 /* Check that no constraints on i6 are introduced in the gist */
1357 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1358 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1359 "5e0 <= 381 - t1 and i4 <= 1) }",
1360 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1361 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1362 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1363 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1364 "20e0 >= 1511 - 4t1 - 5i4) }" },
1365 /* Check that no constraints on i6 are introduced in the gist */
1366 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1367 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1368 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1369 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1370 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1371 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1372 "20e1 <= 1530 - 4t1 - 5i4 and "
1373 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1374 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1375 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1376 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1377 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1378 "10e0 <= -91 + 5i4 + 4i6 and "
1379 "10e0 >= -105 + 5i4 + 4i6) }",
1380 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1381 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1382 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1383 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1384 "{ [a, b, q, p] : b >= 1 + a }",
1385 "{ [a, b, q, p] : false }" },
1388 /* Check that isl_set_gist behaves as expected.
1390 * For the test cases in gist_tests, besides checking that the result
1391 * is as expected, also check that applying the gist operation does
1392 * not modify the input set (an earlier version of isl would do that) and
1393 * that the test case is consistent, i.e., that the gist has the same
1394 * intersection with the context as the input set.
1396 static int test_gist(struct isl_ctx *ctx)
1398 int i;
1399 const char *str;
1400 isl_basic_set *bset1, *bset2;
1401 isl_map *map1, *map2;
1402 int equal;
1404 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1405 int equal_input, equal_intersection;
1406 isl_set *set1, *set2, *copy, *context;
1408 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1409 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1410 copy = isl_set_copy(set1);
1411 set1 = isl_set_gist(set1, isl_set_copy(context));
1412 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1413 equal = isl_set_is_equal(set1, set2);
1414 isl_set_free(set1);
1415 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1416 equal_input = isl_set_is_equal(set1, copy);
1417 isl_set_free(copy);
1418 set1 = isl_set_intersect(set1, isl_set_copy(context));
1419 set2 = isl_set_intersect(set2, context);
1420 equal_intersection = isl_set_is_equal(set1, set2);
1421 isl_set_free(set2);
1422 isl_set_free(set1);
1423 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1424 return -1;
1425 if (!equal)
1426 isl_die(ctx, isl_error_unknown,
1427 "incorrect gist result", return -1);
1428 if (!equal_input)
1429 isl_die(ctx, isl_error_unknown,
1430 "gist modified input", return -1);
1431 if (!equal_input)
1432 isl_die(ctx, isl_error_unknown,
1433 "inconsistent gist test case", return -1);
1436 test_gist_case(ctx, "gist1");
1438 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1439 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1440 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1441 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1442 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1443 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1444 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1445 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1446 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1447 bset1 = isl_basic_set_read_from_str(ctx, str);
1448 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1449 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1450 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1451 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1452 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1453 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1454 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1455 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1456 bset2 = isl_basic_set_read_from_str(ctx, str);
1457 bset1 = isl_basic_set_gist(bset1, bset2);
1458 assert(bset1 && bset1->n_div == 0);
1459 isl_basic_set_free(bset1);
1461 /* Check that the integer divisions of the second disjunct
1462 * do not spread to the first disjunct.
1464 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1465 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1466 "(exists (e0 = [(-1 + t1)/16], "
1467 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1468 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1469 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1470 "o0 <= 4294967295 and t1 <= -1)) }";
1471 map1 = isl_map_read_from_str(ctx, str);
1472 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1473 map2 = isl_map_read_from_str(ctx, str);
1474 map1 = isl_map_gist(map1, map2);
1475 if (!map1)
1476 return -1;
1477 if (map1->n != 1)
1478 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1479 isl_map_free(map1); return -1);
1480 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1481 isl_die(ctx, isl_error_unknown, "expecting single div",
1482 isl_map_free(map1); return -1);
1483 isl_map_free(map1);
1485 if (test_plain_gist(ctx) < 0)
1486 return -1;
1488 return 0;
1491 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1493 isl_set *set, *set2;
1494 int equal;
1495 int one;
1497 set = isl_set_read_from_str(ctx, str);
1498 set = isl_set_coalesce(set);
1499 set2 = isl_set_read_from_str(ctx, str);
1500 equal = isl_set_is_equal(set, set2);
1501 one = set && set->n == 1;
1502 isl_set_free(set);
1503 isl_set_free(set2);
1505 if (equal < 0)
1506 return -1;
1507 if (!equal)
1508 isl_die(ctx, isl_error_unknown,
1509 "coalesced set not equal to input", return -1);
1510 if (check_one && !one)
1511 isl_die(ctx, isl_error_unknown,
1512 "coalesced set should not be a union", return -1);
1514 return 0;
1517 /* Inputs for coalescing tests with unbounded wrapping.
1518 * "str" is a string representation of the input set.
1519 * "single_disjunct" is set if we expect the result to consist of
1520 * a single disjunct.
1522 struct {
1523 int single_disjunct;
1524 const char *str;
1525 } coalesce_unbounded_tests[] = {
1526 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1527 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1528 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1529 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1530 "-10 <= y <= 0}" },
1531 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1532 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1533 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1534 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1537 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1538 * option turned off.
1540 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1542 int i;
1543 int r = 0;
1544 int bounded;
1546 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1547 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1549 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1550 const char *str = coalesce_unbounded_tests[i].str;
1551 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1552 if (test_coalesce_set(ctx, str, check_one) >= 0)
1553 continue;
1554 r = -1;
1555 break;
1558 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1560 return r;
1563 /* Inputs for coalescing tests.
1564 * "str" is a string representation of the input set.
1565 * "single_disjunct" is set if we expect the result to consist of
1566 * a single disjunct.
1568 struct {
1569 int single_disjunct;
1570 const char *str;
1571 } coalesce_tests[] = {
1572 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1573 "y >= x & x >= 2 & 5 >= y }" },
1574 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1575 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1576 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1577 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1578 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1579 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1580 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1581 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1582 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1583 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1584 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1585 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1586 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1587 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1588 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1589 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1590 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1591 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1592 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1593 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1594 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1595 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1596 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1597 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1598 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1599 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1600 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1601 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1602 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1603 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1604 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1605 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1606 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1607 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1608 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1609 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1610 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1611 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1612 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1613 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1614 "[o0, o1, o2, o3, o4, o5, o6]] : "
1615 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1616 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1617 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1618 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1619 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1620 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1621 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1622 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1623 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1624 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1625 "o6 >= i3 + i6 - o3 and M >= 0 and "
1626 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1627 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1628 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1629 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1630 "(o0 = 0 and M >= 2 and N >= 3) or "
1631 "(M = 0 and o0 = 0 and N >= 3) }" },
1632 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1633 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1634 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1635 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1636 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1637 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1638 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1639 "(y = 3 and x = 1) }" },
1640 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1641 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1642 "i1 <= M and i3 <= M and i4 <= M) or "
1643 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1644 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1645 "i4 <= -1 + M) }" },
1646 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1647 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1648 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1649 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1650 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1651 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1652 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1653 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1654 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1655 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1656 { 0, "{ [a, b] : exists e : 2e = a and "
1657 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1658 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1659 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1660 "j >= 1 and j' <= i + j - i' and i >= 1; "
1661 "[1, 1, 1, 1] }" },
1662 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1663 "[i,j] : exists a : j = 3a }" },
1664 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1665 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1666 "a >= 3) or "
1667 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1668 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1669 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1670 "c <= 6 + 8a and a >= 3; "
1671 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1672 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1673 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1674 "[x,0] : 3 <= x <= 4 }" },
1675 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1676 "[x,0] : 4 <= x <= 5 }" },
1677 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1678 "[x,0] : 3 <= x <= 5 }" },
1679 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1680 "[x,0] : 3 <= x <= 4 }" },
1681 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1682 "i1 <= 0; "
1683 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1684 { 1, "{ [0,0]; [1,1] }" },
1685 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1686 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1687 "ii <= k;"
1688 "[k, 0, k] : k <= 6 and k >= 1 }" },
1689 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1690 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1691 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1692 { 1, "[n] -> { [1] : n >= 0;"
1693 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1694 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1695 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1696 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1697 "2e0 <= x and 2e0 <= n);"
1698 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1699 "n >= 0) }" },
1700 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1701 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1702 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1703 "t1 = 1 }" },
1704 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1705 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1706 "[0, 0] }" },
1707 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1708 "t1 >= 13 and t1 <= 16);"
1709 "[t1] : t1 <= 15 and t1 >= 12 }" },
1710 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
1711 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
1712 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
1713 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
1714 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
1715 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
1716 "i <= 4j + 2 }" },
1717 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
1718 "(exists (e0 : 3e0 = -2 + c0)) }" },
1719 { 0, "[n, b0, t0] -> "
1720 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1721 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1722 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1723 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1724 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
1725 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
1726 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
1727 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
1728 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1729 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1730 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1731 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
1732 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
1733 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
1734 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
1735 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
1736 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
1737 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
1738 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
1739 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
1740 { 0, "{ [i0, i1, i2] : "
1741 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
1742 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
1743 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
1744 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
1745 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
1746 "32e0 <= 31 + i0)) or "
1747 "i0 >= 0 }" },
1748 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
1749 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
1750 "2*floor((c)/2) = c and 0 <= a <= 192;"
1751 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
1755 /* A specialized coalescing test case that would result
1756 * in a segmentation fault or a failed assertion in earlier versions of isl.
1758 static int test_coalesce_special(struct isl_ctx *ctx)
1760 const char *str;
1761 isl_map *map1, *map2;
1763 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1764 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
1765 "(y = 201 and o1 <= 239 and o1 >= 212) or "
1766 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
1767 "o1 <= 239 and o1 >= 212)) or "
1768 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
1769 "o1 <= 241 and o1 >= 240));"
1770 "[S_L220_OUT[] -> T7[]] -> "
1771 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
1772 "(y = 2 and o1 <= 241 and o1 >= 212) or "
1773 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
1774 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
1775 map1 = isl_map_read_from_str(ctx, str);
1776 map1 = isl_map_align_divs(map1);
1777 map1 = isl_map_coalesce(map1);
1778 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1779 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
1780 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
1781 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
1782 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
1783 map2 = isl_map_read_from_str(ctx, str);
1784 map2 = isl_map_union(map2, map1);
1785 map2 = isl_map_align_divs(map2);
1786 map2 = isl_map_coalesce(map2);
1787 isl_map_free(map2);
1788 if (!map2)
1789 return -1;
1791 return 0;
1794 /* Test the functionality of isl_set_coalesce.
1795 * That is, check that the output is always equal to the input
1796 * and in some cases that the result consists of a single disjunct.
1798 static int test_coalesce(struct isl_ctx *ctx)
1800 int i;
1802 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1803 const char *str = coalesce_tests[i].str;
1804 int check_one = coalesce_tests[i].single_disjunct;
1805 if (test_coalesce_set(ctx, str, check_one) < 0)
1806 return -1;
1809 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1810 return -1;
1811 if (test_coalesce_special(ctx) < 0)
1812 return -1;
1814 return 0;
1817 /* Construct a representation of the graph on the right of Figure 1
1818 * in "Computing the Transitive Closure of a Union of
1819 * Affine Integer Tuple Relations".
1821 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
1823 isl_set *dom;
1824 isl_map *up, *right;
1826 dom = isl_set_read_from_str(ctx,
1827 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1828 "2 x - 3 y + 3 >= 0 }");
1829 right = isl_map_read_from_str(ctx,
1830 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1831 up = isl_map_read_from_str(ctx,
1832 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1833 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1834 right = isl_map_intersect_range(right, isl_set_copy(dom));
1835 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1836 up = isl_map_intersect_range(up, dom);
1837 return isl_map_union(up, right);
1840 /* Construct a representation of the power of the graph
1841 * on the right of Figure 1 in "Computing the Transitive Closure of
1842 * a Union of Affine Integer Tuple Relations".
1844 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
1846 return isl_map_read_from_str(ctx,
1847 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
1848 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
1849 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
1852 /* Construct a representation of the transitive closure of the graph
1853 * on the right of Figure 1 in "Computing the Transitive Closure of
1854 * a Union of Affine Integer Tuple Relations".
1856 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
1858 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
1861 static int test_closure(isl_ctx *ctx)
1863 const char *str;
1864 isl_map *map, *map2;
1865 int exact, equal;
1867 /* COCOA example 1 */
1868 map = isl_map_read_from_str(ctx,
1869 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1870 "1 <= i and i < n and 1 <= j and j < n or "
1871 "i2 = i + 1 and j2 = j - 1 and "
1872 "1 <= i and i < n and 2 <= j and j <= n }");
1873 map = isl_map_power(map, &exact);
1874 assert(exact);
1875 isl_map_free(map);
1877 /* COCOA example 1 */
1878 map = isl_map_read_from_str(ctx,
1879 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1880 "1 <= i and i < n and 1 <= j and j < n or "
1881 "i2 = i + 1 and j2 = j - 1 and "
1882 "1 <= i and i < n and 2 <= j and j <= n }");
1883 map = isl_map_transitive_closure(map, &exact);
1884 assert(exact);
1885 map2 = isl_map_read_from_str(ctx,
1886 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1887 "1 <= i and i < n and 1 <= j and j <= n and "
1888 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1889 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1890 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1891 assert(isl_map_is_equal(map, map2));
1892 isl_map_free(map2);
1893 isl_map_free(map);
1895 map = isl_map_read_from_str(ctx,
1896 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1897 " 0 <= y and y <= n }");
1898 map = isl_map_transitive_closure(map, &exact);
1899 map2 = isl_map_read_from_str(ctx,
1900 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1901 " 0 <= y and y <= n }");
1902 assert(isl_map_is_equal(map, map2));
1903 isl_map_free(map2);
1904 isl_map_free(map);
1906 /* COCOA example 2 */
1907 map = isl_map_read_from_str(ctx,
1908 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1909 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1910 "i2 = i + 2 and j2 = j - 2 and "
1911 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1912 map = isl_map_transitive_closure(map, &exact);
1913 assert(exact);
1914 map2 = isl_map_read_from_str(ctx,
1915 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1916 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1917 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1918 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1919 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1920 assert(isl_map_is_equal(map, map2));
1921 isl_map_free(map);
1922 isl_map_free(map2);
1924 /* COCOA Fig.2 left */
1925 map = isl_map_read_from_str(ctx,
1926 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1927 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1928 "j <= n or "
1929 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1930 "j <= 2 i - 3 and j <= n - 2 or "
1931 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1932 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1933 map = isl_map_transitive_closure(map, &exact);
1934 assert(exact);
1935 isl_map_free(map);
1937 /* COCOA Fig.2 right */
1938 map = isl_map_read_from_str(ctx,
1939 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1940 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1941 "j <= n or "
1942 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1943 "j <= 2 i - 4 and j <= n - 3 or "
1944 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1945 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1946 map = isl_map_power(map, &exact);
1947 assert(exact);
1948 isl_map_free(map);
1950 /* COCOA Fig.2 right */
1951 map = isl_map_read_from_str(ctx,
1952 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1953 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1954 "j <= n or "
1955 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1956 "j <= 2 i - 4 and j <= n - 3 or "
1957 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1958 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1959 map = isl_map_transitive_closure(map, &exact);
1960 assert(exact);
1961 map2 = isl_map_read_from_str(ctx,
1962 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1963 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1964 "j <= n and 3 + i + 2 j <= 3 n and "
1965 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1966 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1967 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1968 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1969 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1970 assert(isl_map_is_equal(map, map2));
1971 isl_map_free(map2);
1972 isl_map_free(map);
1974 map = cocoa_fig_1_right_graph(ctx);
1975 map = isl_map_transitive_closure(map, &exact);
1976 assert(exact);
1977 map2 = cocoa_fig_1_right_tc(ctx);
1978 assert(isl_map_is_equal(map, map2));
1979 isl_map_free(map2);
1980 isl_map_free(map);
1982 map = cocoa_fig_1_right_graph(ctx);
1983 map = isl_map_power(map, &exact);
1984 map2 = cocoa_fig_1_right_power(ctx);
1985 equal = isl_map_is_equal(map, map2);
1986 isl_map_free(map2);
1987 isl_map_free(map);
1988 if (equal < 0)
1989 return -1;
1990 if (!exact)
1991 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
1992 if (!equal)
1993 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
1995 /* COCOA Theorem 1 counter example */
1996 map = isl_map_read_from_str(ctx,
1997 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1998 "i2 = 1 and j2 = j or "
1999 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2000 map = isl_map_transitive_closure(map, &exact);
2001 assert(exact);
2002 isl_map_free(map);
2004 map = isl_map_read_from_str(ctx,
2005 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2006 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2007 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2008 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2009 map = isl_map_transitive_closure(map, &exact);
2010 assert(exact);
2011 isl_map_free(map);
2013 /* Kelly et al 1996, fig 12 */
2014 map = isl_map_read_from_str(ctx,
2015 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2016 "1 <= i,j,j+1 <= n or "
2017 "j = n and j2 = 1 and i2 = i + 1 and "
2018 "1 <= i,i+1 <= n }");
2019 map = isl_map_transitive_closure(map, &exact);
2020 assert(exact);
2021 map2 = isl_map_read_from_str(ctx,
2022 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2023 "1 <= i <= n and i = i2 or "
2024 "1 <= i < i2 <= n and 1 <= j <= n and "
2025 "1 <= j2 <= n }");
2026 assert(isl_map_is_equal(map, map2));
2027 isl_map_free(map2);
2028 isl_map_free(map);
2030 /* Omega's closure4 */
2031 map = isl_map_read_from_str(ctx,
2032 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2033 "1 <= x,y <= 10 or "
2034 "x2 = x + 1 and y2 = y and "
2035 "1 <= x <= 20 && 5 <= y <= 15 }");
2036 map = isl_map_transitive_closure(map, &exact);
2037 assert(exact);
2038 isl_map_free(map);
2040 map = isl_map_read_from_str(ctx,
2041 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2042 map = isl_map_transitive_closure(map, &exact);
2043 assert(!exact);
2044 map2 = isl_map_read_from_str(ctx,
2045 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2046 assert(isl_map_is_equal(map, map2));
2047 isl_map_free(map);
2048 isl_map_free(map2);
2050 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2051 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2052 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2053 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2054 map = isl_map_read_from_str(ctx, str);
2055 map = isl_map_transitive_closure(map, &exact);
2056 assert(exact);
2057 map2 = isl_map_read_from_str(ctx, str);
2058 assert(isl_map_is_equal(map, map2));
2059 isl_map_free(map);
2060 isl_map_free(map2);
2062 str = "{[0] -> [1]; [2] -> [3]}";
2063 map = isl_map_read_from_str(ctx, str);
2064 map = isl_map_transitive_closure(map, &exact);
2065 assert(exact);
2066 map2 = isl_map_read_from_str(ctx, str);
2067 assert(isl_map_is_equal(map, map2));
2068 isl_map_free(map);
2069 isl_map_free(map2);
2071 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2072 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2073 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2074 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2075 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2076 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2077 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2078 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2079 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2080 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2081 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2082 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2083 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2084 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2085 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2086 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2087 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2088 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2089 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2090 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2091 map = isl_map_read_from_str(ctx, str);
2092 map = isl_map_transitive_closure(map, NULL);
2093 assert(map);
2094 isl_map_free(map);
2096 return 0;
2099 static int test_lex(struct isl_ctx *ctx)
2101 isl_space *dim;
2102 isl_map *map;
2103 int empty;
2105 dim = isl_space_set_alloc(ctx, 0, 0);
2106 map = isl_map_lex_le(dim);
2107 empty = isl_map_is_empty(map);
2108 isl_map_free(map);
2110 if (empty < 0)
2111 return -1;
2112 if (empty)
2113 isl_die(ctx, isl_error_unknown,
2114 "expecting non-empty result", return -1);
2116 return 0;
2119 static int test_lexmin(struct isl_ctx *ctx)
2121 int equal;
2122 const char *str;
2123 isl_basic_map *bmap;
2124 isl_map *map, *map2;
2125 isl_set *set;
2126 isl_set *set2;
2127 isl_pw_multi_aff *pma;
2129 str = "[p0, p1] -> { [] -> [] : "
2130 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2131 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2132 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2133 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2134 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2135 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2136 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2137 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2138 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2139 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2140 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2141 map = isl_map_read_from_str(ctx, str);
2142 map = isl_map_lexmin(map);
2143 isl_map_free(map);
2145 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2146 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2147 set = isl_set_read_from_str(ctx, str);
2148 set = isl_set_lexmax(set);
2149 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2150 set2 = isl_set_read_from_str(ctx, str);
2151 set = isl_set_intersect(set, set2);
2152 assert(!isl_set_is_empty(set));
2153 isl_set_free(set);
2155 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
2156 map = isl_map_read_from_str(ctx, str);
2157 map = isl_map_lexmin(map);
2158 str = "{ [x] -> [5] : 6 <= x <= 8; "
2159 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
2160 map2 = isl_map_read_from_str(ctx, str);
2161 assert(isl_map_is_equal(map, map2));
2162 isl_map_free(map);
2163 isl_map_free(map2);
2165 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
2166 map = isl_map_read_from_str(ctx, str);
2167 map2 = isl_map_copy(map);
2168 map = isl_map_lexmin(map);
2169 assert(isl_map_is_equal(map, map2));
2170 isl_map_free(map);
2171 isl_map_free(map2);
2173 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
2174 map = isl_map_read_from_str(ctx, str);
2175 map = isl_map_lexmin(map);
2176 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
2177 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2178 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2179 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
2180 map2 = isl_map_read_from_str(ctx, str);
2181 assert(isl_map_is_equal(map, map2));
2182 isl_map_free(map);
2183 isl_map_free(map2);
2185 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2186 " 8i' <= i and 8i' >= -7 + i }";
2187 bmap = isl_basic_map_read_from_str(ctx, str);
2188 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2189 map2 = isl_map_from_pw_multi_aff(pma);
2190 map = isl_map_from_basic_map(bmap);
2191 assert(isl_map_is_equal(map, map2));
2192 isl_map_free(map);
2193 isl_map_free(map2);
2195 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
2196 map = isl_map_read_from_str(ctx, str);
2197 map = isl_map_lexmin(map);
2198 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
2199 map2 = isl_map_read_from_str(ctx, str);
2200 assert(isl_map_is_equal(map, map2));
2201 isl_map_free(map);
2202 isl_map_free(map2);
2204 /* Check that empty pieces are properly combined. */
2205 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2206 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2207 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
2208 map = isl_map_read_from_str(ctx, str);
2209 map = isl_map_lexmin(map);
2210 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2211 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2212 "x >= 4 }";
2213 map2 = isl_map_read_from_str(ctx, str);
2214 assert(isl_map_is_equal(map, map2));
2215 isl_map_free(map);
2216 isl_map_free(map2);
2218 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2219 " 8i' <= i and 8i' >= -7 + i }";
2220 set = isl_set_read_from_str(ctx, str);
2221 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2222 set2 = isl_set_from_pw_multi_aff(pma);
2223 equal = isl_set_is_equal(set, set2);
2224 isl_set_free(set);
2225 isl_set_free(set2);
2226 if (equal < 0)
2227 return -1;
2228 if (!equal)
2229 isl_die(ctx, isl_error_unknown,
2230 "unexpected difference between set and "
2231 "piecewise affine expression", return -1);
2233 return 0;
2236 struct {
2237 const char *set;
2238 const char *obj;
2239 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2240 __isl_keep isl_aff *obj);
2241 const char *res;
2242 } opt_tests[] = {
2243 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2244 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2245 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2246 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2247 &isl_set_max_val, "30" },
2251 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2252 * In particular, check the results on non-convex inputs.
2254 static int test_min(struct isl_ctx *ctx)
2256 int i;
2257 isl_set *set;
2258 isl_aff *obj;
2259 isl_val *val, *res;
2260 isl_bool ok;
2262 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2263 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2264 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2265 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2266 val = opt_tests[i].fn(set, obj);
2267 ok = isl_val_eq(res, val);
2268 isl_val_free(res);
2269 isl_val_free(val);
2270 isl_aff_free(obj);
2271 isl_set_free(set);
2273 if (ok < 0)
2274 return -1;
2275 if (!ok)
2276 isl_die(ctx, isl_error_unknown,
2277 "unexpected optimum", return -1);
2280 return 0;
2283 struct must_may {
2284 isl_map *must;
2285 isl_map *may;
2288 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2289 void *dep_user, void *user)
2291 struct must_may *mm = (struct must_may *)user;
2293 if (must)
2294 mm->must = isl_map_union(mm->must, dep);
2295 else
2296 mm->may = isl_map_union(mm->may, dep);
2298 return isl_stat_ok;
2301 static int common_space(void *first, void *second)
2303 int depth = *(int *)first;
2304 return 2 * depth;
2307 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2309 isl_map *map2;
2310 int equal;
2312 if (!map)
2313 return -1;
2315 map2 = isl_map_read_from_str(map->ctx, str);
2316 equal = isl_map_is_equal(map, map2);
2317 isl_map_free(map2);
2319 return equal;
2322 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2324 int equal;
2326 equal = map_is_equal(map, str);
2327 if (equal < 0)
2328 return -1;
2329 if (!equal)
2330 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2331 "result not as expected", return -1);
2332 return 0;
2335 static int test_dep(struct isl_ctx *ctx)
2337 const char *str;
2338 isl_space *dim;
2339 isl_map *map;
2340 isl_access_info *ai;
2341 isl_flow *flow;
2342 int depth;
2343 struct must_may mm;
2345 depth = 3;
2347 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2348 map = isl_map_read_from_str(ctx, str);
2349 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2351 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2352 map = isl_map_read_from_str(ctx, str);
2353 ai = isl_access_info_add_source(ai, map, 1, &depth);
2355 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2356 map = isl_map_read_from_str(ctx, str);
2357 ai = isl_access_info_add_source(ai, map, 1, &depth);
2359 flow = isl_access_info_compute_flow(ai);
2360 dim = isl_space_alloc(ctx, 0, 3, 3);
2361 mm.must = isl_map_empty(isl_space_copy(dim));
2362 mm.may = isl_map_empty(dim);
2364 isl_flow_foreach(flow, collect_must_may, &mm);
2366 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2367 " [1,10,0] -> [2,5,0] }";
2368 assert(map_is_equal(mm.must, str));
2369 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2370 assert(map_is_equal(mm.may, str));
2372 isl_map_free(mm.must);
2373 isl_map_free(mm.may);
2374 isl_flow_free(flow);
2377 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2378 map = isl_map_read_from_str(ctx, str);
2379 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2381 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2382 map = isl_map_read_from_str(ctx, str);
2383 ai = isl_access_info_add_source(ai, map, 1, &depth);
2385 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2386 map = isl_map_read_from_str(ctx, str);
2387 ai = isl_access_info_add_source(ai, map, 0, &depth);
2389 flow = isl_access_info_compute_flow(ai);
2390 dim = isl_space_alloc(ctx, 0, 3, 3);
2391 mm.must = isl_map_empty(isl_space_copy(dim));
2392 mm.may = isl_map_empty(dim);
2394 isl_flow_foreach(flow, collect_must_may, &mm);
2396 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2397 assert(map_is_equal(mm.must, str));
2398 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2399 assert(map_is_equal(mm.may, str));
2401 isl_map_free(mm.must);
2402 isl_map_free(mm.may);
2403 isl_flow_free(flow);
2406 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2407 map = isl_map_read_from_str(ctx, str);
2408 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2410 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2411 map = isl_map_read_from_str(ctx, str);
2412 ai = isl_access_info_add_source(ai, map, 0, &depth);
2414 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2415 map = isl_map_read_from_str(ctx, str);
2416 ai = isl_access_info_add_source(ai, map, 0, &depth);
2418 flow = isl_access_info_compute_flow(ai);
2419 dim = isl_space_alloc(ctx, 0, 3, 3);
2420 mm.must = isl_map_empty(isl_space_copy(dim));
2421 mm.may = isl_map_empty(dim);
2423 isl_flow_foreach(flow, collect_must_may, &mm);
2425 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2426 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2427 assert(map_is_equal(mm.may, str));
2428 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2429 assert(map_is_equal(mm.must, str));
2431 isl_map_free(mm.must);
2432 isl_map_free(mm.may);
2433 isl_flow_free(flow);
2436 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2437 map = isl_map_read_from_str(ctx, str);
2438 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2440 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2441 map = isl_map_read_from_str(ctx, str);
2442 ai = isl_access_info_add_source(ai, map, 0, &depth);
2444 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2445 map = isl_map_read_from_str(ctx, str);
2446 ai = isl_access_info_add_source(ai, map, 0, &depth);
2448 flow = isl_access_info_compute_flow(ai);
2449 dim = isl_space_alloc(ctx, 0, 3, 3);
2450 mm.must = isl_map_empty(isl_space_copy(dim));
2451 mm.may = isl_map_empty(dim);
2453 isl_flow_foreach(flow, collect_must_may, &mm);
2455 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2456 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2457 assert(map_is_equal(mm.may, str));
2458 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2459 assert(map_is_equal(mm.must, str));
2461 isl_map_free(mm.must);
2462 isl_map_free(mm.may);
2463 isl_flow_free(flow);
2466 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2467 map = isl_map_read_from_str(ctx, str);
2468 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2470 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2471 map = isl_map_read_from_str(ctx, str);
2472 ai = isl_access_info_add_source(ai, map, 0, &depth);
2474 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2475 map = isl_map_read_from_str(ctx, str);
2476 ai = isl_access_info_add_source(ai, map, 0, &depth);
2478 flow = isl_access_info_compute_flow(ai);
2479 dim = isl_space_alloc(ctx, 0, 3, 3);
2480 mm.must = isl_map_empty(isl_space_copy(dim));
2481 mm.may = isl_map_empty(dim);
2483 isl_flow_foreach(flow, collect_must_may, &mm);
2485 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2486 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2487 assert(map_is_equal(mm.may, str));
2488 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2489 assert(map_is_equal(mm.must, str));
2491 isl_map_free(mm.must);
2492 isl_map_free(mm.may);
2493 isl_flow_free(flow);
2496 depth = 5;
2498 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2499 map = isl_map_read_from_str(ctx, str);
2500 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2502 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2503 map = isl_map_read_from_str(ctx, str);
2504 ai = isl_access_info_add_source(ai, map, 1, &depth);
2506 flow = isl_access_info_compute_flow(ai);
2507 dim = isl_space_alloc(ctx, 0, 5, 5);
2508 mm.must = isl_map_empty(isl_space_copy(dim));
2509 mm.may = isl_map_empty(dim);
2511 isl_flow_foreach(flow, collect_must_may, &mm);
2513 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2514 assert(map_is_equal(mm.must, str));
2515 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2516 assert(map_is_equal(mm.may, str));
2518 isl_map_free(mm.must);
2519 isl_map_free(mm.may);
2520 isl_flow_free(flow);
2522 return 0;
2525 /* Check that the dependence analysis proceeds without errors.
2526 * Earlier versions of isl would break down during the analysis
2527 * due to the use of the wrong spaces.
2529 static int test_flow(isl_ctx *ctx)
2531 const char *str;
2532 isl_union_map *access, *schedule;
2533 isl_union_map *must_dep, *may_dep;
2534 int r;
2536 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2537 access = isl_union_map_read_from_str(ctx, str);
2538 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2539 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2540 "S2[] -> [1,0,0,0]; "
2541 "S3[] -> [-1,0,0,0] }";
2542 schedule = isl_union_map_read_from_str(ctx, str);
2543 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2544 isl_union_map_copy(access), schedule,
2545 &must_dep, &may_dep, NULL, NULL);
2546 isl_union_map_free(may_dep);
2547 isl_union_map_free(must_dep);
2549 return r;
2552 struct {
2553 const char *map;
2554 int sv;
2555 } sv_tests[] = {
2556 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2557 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2558 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2559 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2560 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2561 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2562 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2563 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2564 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2567 int test_sv(isl_ctx *ctx)
2569 isl_union_map *umap;
2570 int i;
2571 int sv;
2573 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2574 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2575 sv = isl_union_map_is_single_valued(umap);
2576 isl_union_map_free(umap);
2577 if (sv < 0)
2578 return -1;
2579 if (sv_tests[i].sv && !sv)
2580 isl_die(ctx, isl_error_internal,
2581 "map not detected as single valued", return -1);
2582 if (!sv_tests[i].sv && sv)
2583 isl_die(ctx, isl_error_internal,
2584 "map detected as single valued", return -1);
2587 return 0;
2590 struct {
2591 const char *str;
2592 int bijective;
2593 } bijective_tests[] = {
2594 { "[N,M]->{[i,j] -> [i]}", 0 },
2595 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
2596 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
2597 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
2598 { "[N,M]->{[i,j] -> [j,i]}", 1 },
2599 { "[N,M]->{[i,j] -> [i+j]}", 0 },
2600 { "[N,M]->{[i,j] -> []}", 0 },
2601 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
2602 { "[N,M]->{[i,j] -> [2i]}", 0 },
2603 { "[N,M]->{[i,j] -> [i,i]}", 0 },
2604 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
2605 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
2606 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
2609 static int test_bijective(struct isl_ctx *ctx)
2611 isl_map *map;
2612 int i;
2613 int bijective;
2615 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
2616 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
2617 bijective = isl_map_is_bijective(map);
2618 isl_map_free(map);
2619 if (bijective < 0)
2620 return -1;
2621 if (bijective_tests[i].bijective && !bijective)
2622 isl_die(ctx, isl_error_internal,
2623 "map not detected as bijective", return -1);
2624 if (!bijective_tests[i].bijective && bijective)
2625 isl_die(ctx, isl_error_internal,
2626 "map detected as bijective", return -1);
2629 return 0;
2632 /* Inputs for isl_pw_qpolynomial_gist tests.
2633 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2635 struct {
2636 const char *pwqp;
2637 const char *set;
2638 const char *gist;
2639 } pwqp_gist_tests[] = {
2640 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2641 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2642 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2643 "{ [i] -> -1/2 + 1/2 * i }" },
2644 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2647 static int test_pwqp(struct isl_ctx *ctx)
2649 int i;
2650 const char *str;
2651 isl_set *set;
2652 isl_pw_qpolynomial *pwqp1, *pwqp2;
2653 int equal;
2655 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2656 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2658 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2659 isl_dim_in, 1, 1);
2661 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2662 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2664 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2666 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2668 isl_pw_qpolynomial_free(pwqp1);
2670 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2671 str = pwqp_gist_tests[i].pwqp;
2672 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2673 str = pwqp_gist_tests[i].set;
2674 set = isl_set_read_from_str(ctx, str);
2675 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2676 str = pwqp_gist_tests[i].gist;
2677 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2678 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2679 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2680 isl_pw_qpolynomial_free(pwqp1);
2682 if (equal < 0)
2683 return -1;
2684 if (!equal)
2685 isl_die(ctx, isl_error_unknown,
2686 "unexpected result", return -1);
2689 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2690 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2691 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2692 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2694 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2696 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2698 isl_pw_qpolynomial_free(pwqp1);
2700 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2701 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2702 str = "{ [x] -> x }";
2703 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2705 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2707 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2709 isl_pw_qpolynomial_free(pwqp1);
2711 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2712 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2713 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2714 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2715 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2716 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2717 isl_pw_qpolynomial_free(pwqp1);
2719 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2720 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2721 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2722 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2723 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2724 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2725 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2726 isl_pw_qpolynomial_free(pwqp1);
2727 isl_pw_qpolynomial_free(pwqp2);
2728 if (equal < 0)
2729 return -1;
2730 if (!equal)
2731 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2733 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2734 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2735 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2736 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2737 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2738 isl_val_one(ctx));
2739 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2740 isl_pw_qpolynomial_free(pwqp1);
2741 isl_pw_qpolynomial_free(pwqp2);
2742 if (equal < 0)
2743 return -1;
2744 if (!equal)
2745 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2747 return 0;
2750 static int test_split_periods(isl_ctx *ctx)
2752 const char *str;
2753 isl_pw_qpolynomial *pwqp;
2755 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2756 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2757 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2758 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2760 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2762 isl_pw_qpolynomial_free(pwqp);
2764 if (!pwqp)
2765 return -1;
2767 return 0;
2770 static int test_union(isl_ctx *ctx)
2772 const char *str;
2773 isl_union_set *uset1, *uset2;
2774 isl_union_map *umap1, *umap2;
2775 int equal;
2777 str = "{ [i] : 0 <= i <= 1 }";
2778 uset1 = isl_union_set_read_from_str(ctx, str);
2779 str = "{ [1] -> [0] }";
2780 umap1 = isl_union_map_read_from_str(ctx, str);
2782 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2783 equal = isl_union_map_is_equal(umap1, umap2);
2785 isl_union_map_free(umap1);
2786 isl_union_map_free(umap2);
2788 if (equal < 0)
2789 return -1;
2790 if (!equal)
2791 isl_die(ctx, isl_error_unknown, "union maps not equal",
2792 return -1);
2794 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2795 umap1 = isl_union_map_read_from_str(ctx, str);
2796 str = "{ A[i]; B[i] }";
2797 uset1 = isl_union_set_read_from_str(ctx, str);
2799 uset2 = isl_union_map_domain(umap1);
2801 equal = isl_union_set_is_equal(uset1, uset2);
2803 isl_union_set_free(uset1);
2804 isl_union_set_free(uset2);
2806 if (equal < 0)
2807 return -1;
2808 if (!equal)
2809 isl_die(ctx, isl_error_unknown, "union sets not equal",
2810 return -1);
2812 return 0;
2815 /* Check that computing a bound of a non-zero polynomial over an unbounded
2816 * domain does not produce a rational value.
2817 * In particular, check that the upper bound is infinity.
2819 static int test_bound_unbounded_domain(isl_ctx *ctx)
2821 const char *str;
2822 isl_pw_qpolynomial *pwqp;
2823 isl_pw_qpolynomial_fold *pwf, *pwf2;
2824 isl_bool equal;
2826 str = "{ [m,n] -> -m * n }";
2827 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2828 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2829 str = "{ infty }";
2830 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2831 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2832 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
2833 isl_pw_qpolynomial_fold_free(pwf);
2834 isl_pw_qpolynomial_fold_free(pwf2);
2836 if (equal < 0)
2837 return -1;
2838 if (!equal)
2839 isl_die(ctx, isl_error_unknown,
2840 "expecting infinite polynomial bound", return -1);
2842 return 0;
2845 static int test_bound(isl_ctx *ctx)
2847 const char *str;
2848 unsigned dim;
2849 isl_pw_qpolynomial *pwqp;
2850 isl_pw_qpolynomial_fold *pwf;
2852 if (test_bound_unbounded_domain(ctx) < 0)
2853 return -1;
2855 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2856 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2857 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2858 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
2859 isl_pw_qpolynomial_fold_free(pwf);
2860 if (dim != 4)
2861 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
2862 return -1);
2864 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2865 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2866 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2867 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
2868 isl_pw_qpolynomial_fold_free(pwf);
2869 if (dim != 1)
2870 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
2871 return -1);
2873 return 0;
2876 static int test_lift(isl_ctx *ctx)
2878 const char *str;
2879 isl_basic_map *bmap;
2880 isl_basic_set *bset;
2882 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2883 bset = isl_basic_set_read_from_str(ctx, str);
2884 bset = isl_basic_set_lift(bset);
2885 bmap = isl_basic_map_from_range(bset);
2886 bset = isl_basic_map_domain(bmap);
2887 isl_basic_set_free(bset);
2889 return 0;
2892 struct {
2893 const char *set1;
2894 const char *set2;
2895 int subset;
2896 } subset_tests[] = {
2897 { "{ [112, 0] }",
2898 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2899 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2900 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2901 { "{ [65] }",
2902 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2903 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2904 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2905 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2906 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2907 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2908 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2909 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2910 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2911 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2912 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2913 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2914 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2915 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
2916 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
2917 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
2918 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
2919 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
2920 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
2921 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
2922 "4e0 <= -57 + i0 + i1)) or "
2923 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
2924 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
2925 "4e0 >= -61 + i0 + i1)) or "
2926 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
2927 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
2930 static int test_subset(isl_ctx *ctx)
2932 int i;
2933 isl_set *set1, *set2;
2934 int subset;
2936 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2937 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2938 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2939 subset = isl_set_is_subset(set1, set2);
2940 isl_set_free(set1);
2941 isl_set_free(set2);
2942 if (subset < 0)
2943 return -1;
2944 if (subset != subset_tests[i].subset)
2945 isl_die(ctx, isl_error_unknown,
2946 "incorrect subset result", return -1);
2949 return 0;
2952 struct {
2953 const char *minuend;
2954 const char *subtrahend;
2955 const char *difference;
2956 } subtract_domain_tests[] = {
2957 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2958 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2959 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2962 static int test_subtract(isl_ctx *ctx)
2964 int i;
2965 isl_union_map *umap1, *umap2;
2966 isl_union_pw_multi_aff *upma1, *upma2;
2967 isl_union_set *uset;
2968 int equal;
2970 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2971 umap1 = isl_union_map_read_from_str(ctx,
2972 subtract_domain_tests[i].minuend);
2973 uset = isl_union_set_read_from_str(ctx,
2974 subtract_domain_tests[i].subtrahend);
2975 umap2 = isl_union_map_read_from_str(ctx,
2976 subtract_domain_tests[i].difference);
2977 umap1 = isl_union_map_subtract_domain(umap1, uset);
2978 equal = isl_union_map_is_equal(umap1, umap2);
2979 isl_union_map_free(umap1);
2980 isl_union_map_free(umap2);
2981 if (equal < 0)
2982 return -1;
2983 if (!equal)
2984 isl_die(ctx, isl_error_unknown,
2985 "incorrect subtract domain result", return -1);
2988 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2989 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
2990 subtract_domain_tests[i].minuend);
2991 uset = isl_union_set_read_from_str(ctx,
2992 subtract_domain_tests[i].subtrahend);
2993 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
2994 subtract_domain_tests[i].difference);
2995 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
2996 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
2997 isl_union_pw_multi_aff_free(upma1);
2998 isl_union_pw_multi_aff_free(upma2);
2999 if (equal < 0)
3000 return -1;
3001 if (!equal)
3002 isl_die(ctx, isl_error_unknown,
3003 "incorrect subtract domain result", return -1);
3006 return 0;
3009 /* Check that intersecting the empty basic set with another basic set
3010 * does not increase the number of constraints. In particular,
3011 * the empty basic set should maintain its canonical representation.
3013 static int test_intersect(isl_ctx *ctx)
3015 int n1, n2;
3016 isl_basic_set *bset1, *bset2;
3018 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3019 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3020 n1 = isl_basic_set_n_constraint(bset1);
3021 bset1 = isl_basic_set_intersect(bset1, bset2);
3022 n2 = isl_basic_set_n_constraint(bset1);
3023 isl_basic_set_free(bset1);
3024 if (!bset1)
3025 return -1;
3026 if (n1 != n2)
3027 isl_die(ctx, isl_error_unknown,
3028 "number of constraints of empty set changed",
3029 return -1);
3031 return 0;
3034 int test_factorize(isl_ctx *ctx)
3036 const char *str;
3037 isl_basic_set *bset;
3038 isl_factorizer *f;
3040 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3041 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3042 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3043 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3044 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3045 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3046 "3i5 >= -2i0 - i2 + 3i4 }";
3047 bset = isl_basic_set_read_from_str(ctx, str);
3048 f = isl_basic_set_factorizer(bset);
3049 isl_basic_set_free(bset);
3050 isl_factorizer_free(f);
3051 if (!f)
3052 isl_die(ctx, isl_error_unknown,
3053 "failed to construct factorizer", return -1);
3055 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3056 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3057 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3058 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3059 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3060 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3061 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3062 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3063 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3064 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3065 bset = isl_basic_set_read_from_str(ctx, str);
3066 f = isl_basic_set_factorizer(bset);
3067 isl_basic_set_free(bset);
3068 isl_factorizer_free(f);
3069 if (!f)
3070 isl_die(ctx, isl_error_unknown,
3071 "failed to construct factorizer", return -1);
3073 return 0;
3076 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3078 int *injective = user;
3080 *injective = isl_map_is_injective(map);
3081 isl_map_free(map);
3083 if (*injective < 0 || !*injective)
3084 return isl_stat_error;
3086 return isl_stat_ok;
3089 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3090 const char *r, const char *s, int tilable, int parallel)
3092 int i;
3093 isl_union_set *D;
3094 isl_union_map *W, *R, *S;
3095 isl_union_map *empty;
3096 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3097 isl_union_map *validity, *proximity, *coincidence;
3098 isl_union_map *schedule;
3099 isl_union_map *test;
3100 isl_union_set *delta;
3101 isl_union_set *domain;
3102 isl_set *delta_set;
3103 isl_set *slice;
3104 isl_set *origin;
3105 isl_schedule_constraints *sc;
3106 isl_schedule *sched;
3107 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3109 D = isl_union_set_read_from_str(ctx, d);
3110 W = isl_union_map_read_from_str(ctx, w);
3111 R = isl_union_map_read_from_str(ctx, r);
3112 S = isl_union_map_read_from_str(ctx, s);
3114 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3115 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3117 empty = isl_union_map_empty(isl_union_map_get_space(S));
3118 isl_union_map_compute_flow(isl_union_map_copy(R),
3119 isl_union_map_copy(W), empty,
3120 isl_union_map_copy(S),
3121 &dep_raw, NULL, NULL, NULL);
3122 isl_union_map_compute_flow(isl_union_map_copy(W),
3123 isl_union_map_copy(W),
3124 isl_union_map_copy(R),
3125 isl_union_map_copy(S),
3126 &dep_waw, &dep_war, NULL, NULL);
3128 dep = isl_union_map_union(dep_waw, dep_war);
3129 dep = isl_union_map_union(dep, dep_raw);
3130 validity = isl_union_map_copy(dep);
3131 coincidence = isl_union_map_copy(dep);
3132 proximity = isl_union_map_copy(dep);
3134 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3135 sc = isl_schedule_constraints_set_validity(sc, validity);
3136 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3137 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3138 sched = isl_schedule_constraints_compute_schedule(sc);
3139 schedule = isl_schedule_get_map(sched);
3140 isl_schedule_free(sched);
3141 isl_union_map_free(W);
3142 isl_union_map_free(R);
3143 isl_union_map_free(S);
3145 is_injection = 1;
3146 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3148 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3149 is_complete = isl_union_set_is_subset(D, domain);
3150 isl_union_set_free(D);
3151 isl_union_set_free(domain);
3153 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3154 test = isl_union_map_apply_range(test, dep);
3155 test = isl_union_map_apply_range(test, schedule);
3157 delta = isl_union_map_deltas(test);
3158 if (isl_union_set_n_set(delta) == 0) {
3159 is_tilable = 1;
3160 is_parallel = 1;
3161 is_nonneg = 1;
3162 isl_union_set_free(delta);
3163 } else {
3164 delta_set = isl_set_from_union_set(delta);
3166 slice = isl_set_universe(isl_set_get_space(delta_set));
3167 for (i = 0; i < tilable; ++i)
3168 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3169 is_tilable = isl_set_is_subset(delta_set, slice);
3170 isl_set_free(slice);
3172 slice = isl_set_universe(isl_set_get_space(delta_set));
3173 for (i = 0; i < parallel; ++i)
3174 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3175 is_parallel = isl_set_is_subset(delta_set, slice);
3176 isl_set_free(slice);
3178 origin = isl_set_universe(isl_set_get_space(delta_set));
3179 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
3180 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3182 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3183 delta_set = isl_set_lexmin(delta_set);
3185 is_nonneg = isl_set_is_equal(delta_set, origin);
3187 isl_set_free(origin);
3188 isl_set_free(delta_set);
3191 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3192 is_injection < 0 || is_complete < 0)
3193 return -1;
3194 if (!is_complete)
3195 isl_die(ctx, isl_error_unknown,
3196 "generated schedule incomplete", return -1);
3197 if (!is_injection)
3198 isl_die(ctx, isl_error_unknown,
3199 "generated schedule not injective on each statement",
3200 return -1);
3201 if (!is_nonneg)
3202 isl_die(ctx, isl_error_unknown,
3203 "negative dependences in generated schedule",
3204 return -1);
3205 if (!is_tilable)
3206 isl_die(ctx, isl_error_unknown,
3207 "generated schedule not as tilable as expected",
3208 return -1);
3209 if (!is_parallel)
3210 isl_die(ctx, isl_error_unknown,
3211 "generated schedule not as parallel as expected",
3212 return -1);
3214 return 0;
3217 /* Compute a schedule for the given instance set, validity constraints,
3218 * proximity constraints and context and return a corresponding union map
3219 * representation.
3221 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3222 const char *domain, const char *validity, const char *proximity,
3223 const char *context)
3225 isl_set *con;
3226 isl_union_set *dom;
3227 isl_union_map *dep;
3228 isl_union_map *prox;
3229 isl_schedule_constraints *sc;
3230 isl_schedule *schedule;
3231 isl_union_map *sched;
3233 con = isl_set_read_from_str(ctx, context);
3234 dom = isl_union_set_read_from_str(ctx, domain);
3235 dep = isl_union_map_read_from_str(ctx, validity);
3236 prox = isl_union_map_read_from_str(ctx, proximity);
3237 sc = isl_schedule_constraints_on_domain(dom);
3238 sc = isl_schedule_constraints_set_context(sc, con);
3239 sc = isl_schedule_constraints_set_validity(sc, dep);
3240 sc = isl_schedule_constraints_set_proximity(sc, prox);
3241 schedule = isl_schedule_constraints_compute_schedule(sc);
3242 sched = isl_schedule_get_map(schedule);
3243 isl_schedule_free(schedule);
3245 return sched;
3248 /* Compute a schedule for the given instance set, validity constraints and
3249 * proximity constraints and return a corresponding union map representation.
3251 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
3252 const char *domain, const char *validity, const char *proximity)
3254 return compute_schedule_with_context(ctx, domain, validity, proximity,
3255 "{ : }");
3258 /* Check that a schedule can be constructed on the given domain
3259 * with the given validity and proximity constraints.
3261 static int test_has_schedule(isl_ctx *ctx, const char *domain,
3262 const char *validity, const char *proximity)
3264 isl_union_map *sched;
3266 sched = compute_schedule(ctx, domain, validity, proximity);
3267 if (!sched)
3268 return -1;
3270 isl_union_map_free(sched);
3271 return 0;
3274 int test_special_schedule(isl_ctx *ctx, const char *domain,
3275 const char *validity, const char *proximity, const char *expected_sched)
3277 isl_union_map *sched1, *sched2;
3278 int equal;
3280 sched1 = compute_schedule(ctx, domain, validity, proximity);
3281 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
3283 equal = isl_union_map_is_equal(sched1, sched2);
3284 isl_union_map_free(sched1);
3285 isl_union_map_free(sched2);
3287 if (equal < 0)
3288 return -1;
3289 if (!equal)
3290 isl_die(ctx, isl_error_unknown, "unexpected schedule",
3291 return -1);
3293 return 0;
3296 /* Check that the schedule map is properly padded, even after being
3297 * reconstructed from the band forest.
3299 static int test_padded_schedule(isl_ctx *ctx)
3301 const char *str;
3302 isl_union_set *D;
3303 isl_union_map *validity, *proximity;
3304 isl_schedule_constraints *sc;
3305 isl_schedule *sched;
3306 isl_union_map *map1, *map2;
3307 isl_band_list *list;
3308 int equal;
3310 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
3311 D = isl_union_set_read_from_str(ctx, str);
3312 validity = isl_union_map_empty(isl_union_set_get_space(D));
3313 proximity = isl_union_map_copy(validity);
3314 sc = isl_schedule_constraints_on_domain(D);
3315 sc = isl_schedule_constraints_set_validity(sc, validity);
3316 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3317 sched = isl_schedule_constraints_compute_schedule(sc);
3318 map1 = isl_schedule_get_map(sched);
3319 list = isl_schedule_get_band_forest(sched);
3320 isl_band_list_free(list);
3321 map2 = isl_schedule_get_map(sched);
3322 isl_schedule_free(sched);
3323 equal = isl_union_map_is_equal(map1, map2);
3324 isl_union_map_free(map1);
3325 isl_union_map_free(map2);
3327 if (equal < 0)
3328 return -1;
3329 if (!equal)
3330 isl_die(ctx, isl_error_unknown,
3331 "reconstructed schedule map not the same as original",
3332 return -1);
3334 return 0;
3337 /* Check that conditional validity constraints are also taken into
3338 * account across bands.
3339 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
3340 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
3341 * and then check that the adjacent order constraint C[2,1]->D[2,0]
3342 * is enforced by the rest of the schedule.
3344 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
3346 const char *str;
3347 isl_union_set *domain;
3348 isl_union_map *validity, *proximity, *condition;
3349 isl_union_map *sink, *source, *dep;
3350 isl_schedule_constraints *sc;
3351 isl_schedule *schedule;
3352 isl_union_access_info *access;
3353 isl_union_flow *flow;
3354 int empty;
3356 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3357 "A[k] : k >= 1 and k <= -1 + n; "
3358 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3359 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
3360 domain = isl_union_set_read_from_str(ctx, str);
3361 sc = isl_schedule_constraints_on_domain(domain);
3362 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
3363 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3364 "D[k, i] -> C[1 + k, i] : "
3365 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3366 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
3367 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
3368 validity = isl_union_map_read_from_str(ctx, str);
3369 sc = isl_schedule_constraints_set_validity(sc, validity);
3370 str = "[n] -> { C[k, i] -> D[k, i] : "
3371 "0 <= i <= -1 + k and k <= -1 + n }";
3372 proximity = isl_union_map_read_from_str(ctx, str);
3373 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3374 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
3375 "i <= -1 + k and i >= 1 and k <= -2 + n; "
3376 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
3377 "k <= -1 + n and i >= 0 and i <= -2 + k }";
3378 condition = isl_union_map_read_from_str(ctx, str);
3379 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
3380 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3381 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
3382 "i >= 0 and i <= -1 + k and k <= -1 + n and "
3383 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
3384 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
3385 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3386 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
3387 "k <= -1 + n and i >= 0 and i <= -1 + k and "
3388 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
3389 validity = isl_union_map_read_from_str(ctx, str);
3390 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3391 validity);
3392 schedule = isl_schedule_constraints_compute_schedule(sc);
3393 str = "{ D[2,0] -> [] }";
3394 sink = isl_union_map_read_from_str(ctx, str);
3395 access = isl_union_access_info_from_sink(sink);
3396 str = "{ C[2,1] -> [] }";
3397 source = isl_union_map_read_from_str(ctx, str);
3398 access = isl_union_access_info_set_must_source(access, source);
3399 access = isl_union_access_info_set_schedule(access, schedule);
3400 flow = isl_union_access_info_compute_flow(access);
3401 dep = isl_union_flow_get_must_dependence(flow);
3402 isl_union_flow_free(flow);
3403 empty = isl_union_map_is_empty(dep);
3404 isl_union_map_free(dep);
3406 if (empty < 0)
3407 return -1;
3408 if (empty)
3409 isl_die(ctx, isl_error_unknown,
3410 "conditional validity not respected", return -1);
3412 return 0;
3415 /* Input for testing of schedule construction based on
3416 * conditional constraints.
3418 * domain is the iteration domain
3419 * flow are the flow dependences, which determine the validity and
3420 * proximity constraints
3421 * condition are the conditions on the conditional validity constraints
3422 * conditional_validity are the conditional validity constraints
3423 * outer_band_n is the expected number of members in the outer band
3425 struct {
3426 const char *domain;
3427 const char *flow;
3428 const char *condition;
3429 const char *conditional_validity;
3430 int outer_band_n;
3431 } live_range_tests[] = {
3432 /* Contrived example that illustrates that we need to keep
3433 * track of tagged condition dependences and
3434 * tagged conditional validity dependences
3435 * in isl_sched_edge separately.
3436 * In particular, the conditional validity constraints on A
3437 * cannot be satisfied,
3438 * but they can be ignored because there are no corresponding
3439 * condition constraints. However, we do have an additional
3440 * conditional validity constraint that maps to the same
3441 * dependence relation
3442 * as the condition constraint on B. If we did not make a distinction
3443 * between tagged condition and tagged conditional validity
3444 * dependences, then we
3445 * could end up treating this shared dependence as an condition
3446 * constraint on A, forcing a localization of the conditions,
3447 * which is impossible.
3449 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
3450 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
3451 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
3452 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3453 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3454 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
3457 /* TACO 2013 Fig. 7 */
3458 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3459 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3460 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3461 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
3462 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
3463 "0 <= i < n and 0 <= j < n - 1 }",
3464 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
3465 "0 <= i < n and 0 <= j < j' < n;"
3466 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
3467 "0 <= i < i' < n and 0 <= j,j' < n;"
3468 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
3469 "0 <= i,j,j' < n and j < j' }",
3472 /* TACO 2013 Fig. 7, without tags */
3473 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3474 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3475 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3476 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3477 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3478 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
3479 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
3480 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
3483 /* TACO 2013 Fig. 12 */
3484 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
3485 "S3[i,3] : 0 <= i <= 1 }",
3486 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
3487 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
3488 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
3489 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
3490 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3491 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
3492 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3493 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
3494 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
3495 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
3496 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
3501 /* Test schedule construction based on conditional constraints.
3502 * In particular, check the number of members in the outer band node
3503 * as an indication of whether tiling is possible or not.
3505 static int test_conditional_schedule_constraints(isl_ctx *ctx)
3507 int i;
3508 isl_union_set *domain;
3509 isl_union_map *condition;
3510 isl_union_map *flow;
3511 isl_union_map *validity;
3512 isl_schedule_constraints *sc;
3513 isl_schedule *schedule;
3514 isl_schedule_node *node;
3515 int n_member;
3517 if (test_special_conditional_schedule_constraints(ctx) < 0)
3518 return -1;
3520 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
3521 domain = isl_union_set_read_from_str(ctx,
3522 live_range_tests[i].domain);
3523 flow = isl_union_map_read_from_str(ctx,
3524 live_range_tests[i].flow);
3525 condition = isl_union_map_read_from_str(ctx,
3526 live_range_tests[i].condition);
3527 validity = isl_union_map_read_from_str(ctx,
3528 live_range_tests[i].conditional_validity);
3529 sc = isl_schedule_constraints_on_domain(domain);
3530 sc = isl_schedule_constraints_set_validity(sc,
3531 isl_union_map_copy(flow));
3532 sc = isl_schedule_constraints_set_proximity(sc, flow);
3533 sc = isl_schedule_constraints_set_conditional_validity(sc,
3534 condition, validity);
3535 schedule = isl_schedule_constraints_compute_schedule(sc);
3536 node = isl_schedule_get_root(schedule);
3537 while (node &&
3538 isl_schedule_node_get_type(node) != isl_schedule_node_band)
3539 node = isl_schedule_node_first_child(node);
3540 n_member = isl_schedule_node_band_n_member(node);
3541 isl_schedule_node_free(node);
3542 isl_schedule_free(schedule);
3544 if (!schedule)
3545 return -1;
3546 if (n_member != live_range_tests[i].outer_band_n)
3547 isl_die(ctx, isl_error_unknown,
3548 "unexpected number of members in outer band",
3549 return -1);
3551 return 0;
3554 /* Check that the schedule computed for the given instance set and
3555 * dependence relation strongly satisfies the dependences.
3556 * In particular, check that no instance is scheduled before
3557 * or together with an instance on which it depends.
3558 * Earlier versions of isl would produce a schedule that
3559 * only weakly satisfies the dependences.
3561 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
3563 const char *domain, *dep;
3564 isl_union_map *D, *schedule;
3565 isl_map *map, *ge;
3566 int empty;
3568 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
3569 "A[i0] : 0 <= i0 <= 1 }";
3570 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
3571 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
3572 schedule = compute_schedule(ctx, domain, dep, dep);
3573 D = isl_union_map_read_from_str(ctx, dep);
3574 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
3575 D = isl_union_map_apply_range(D, schedule);
3576 map = isl_map_from_union_map(D);
3577 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3578 map = isl_map_intersect(map, ge);
3579 empty = isl_map_is_empty(map);
3580 isl_map_free(map);
3582 if (empty < 0)
3583 return -1;
3584 if (!empty)
3585 isl_die(ctx, isl_error_unknown,
3586 "dependences not strongly satisfied", return -1);
3588 return 0;
3591 /* Compute a schedule for input where the instance set constraints
3592 * conflict with the context constraints.
3593 * Earlier versions of isl did not properly handle this situation.
3595 static int test_conflicting_context_schedule(isl_ctx *ctx)
3597 isl_union_map *schedule;
3598 const char *domain, *context;
3600 domain = "[n] -> { A[] : n >= 0 }";
3601 context = "[n] -> { : n < 0 }";
3602 schedule = compute_schedule_with_context(ctx,
3603 domain, "{}", "{}", context);
3604 isl_union_map_free(schedule);
3606 if (!schedule)
3607 return -1;
3609 return 0;
3612 /* Check that the dependence carrying step is not confused by
3613 * a bound on the coefficient size.
3614 * In particular, force the scheduler to move to a dependence carrying
3615 * step by demanding outer coincidence and bound the size of
3616 * the coefficients. Earlier versions of isl would take this
3617 * bound into account while carrying dependences, breaking
3618 * fundamental assumptions.
3620 static int test_bounded_coefficients_schedule(isl_ctx *ctx)
3622 const char *domain, *dep;
3623 isl_union_set *I;
3624 isl_union_map *D;
3625 isl_schedule_constraints *sc;
3626 isl_schedule *schedule;
3628 domain = "{ C[i0, i1] : 2 <= i0 <= 3999 and 0 <= i1 <= -1 + i0 }";
3629 dep = "{ C[i0, i1] -> C[i0, 1 + i1] : i0 <= 3999 and i1 >= 0 and "
3630 "i1 <= -2 + i0; "
3631 "C[i0, -1 + i0] -> C[1 + i0, 0] : i0 <= 3998 and i0 >= 1 }";
3632 I = isl_union_set_read_from_str(ctx, domain);
3633 D = isl_union_map_read_from_str(ctx, dep);
3634 sc = isl_schedule_constraints_on_domain(I);
3635 sc = isl_schedule_constraints_set_validity(sc, isl_union_map_copy(D));
3636 sc = isl_schedule_constraints_set_coincidence(sc, D);
3637 isl_options_set_schedule_outer_coincidence(ctx, 1);
3638 isl_options_set_schedule_max_coefficient(ctx, 20);
3639 schedule = isl_schedule_constraints_compute_schedule(sc);
3640 isl_options_set_schedule_max_coefficient(ctx, -1);
3641 isl_options_set_schedule_outer_coincidence(ctx, 0);
3642 isl_schedule_free(schedule);
3644 if (!schedule)
3645 return -1;
3647 return 0;
3650 int test_schedule(isl_ctx *ctx)
3652 const char *D, *W, *R, *V, *P, *S;
3653 int max_coincidence;
3655 /* Handle resulting schedule with zero bands. */
3656 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
3657 return -1;
3659 /* Jacobi */
3660 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
3661 W = "{ S1[t,i] -> a[t,i] }";
3662 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
3663 "S1[t,i] -> a[t-1,i+1] }";
3664 S = "{ S1[t,i] -> [t,i] }";
3665 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3666 return -1;
3668 /* Fig. 5 of CC2008 */
3669 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
3670 "j <= -1 + N }";
3671 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
3672 "j >= 2 and j <= -1 + N }";
3673 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
3674 "j >= 2 and j <= -1 + N; "
3675 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
3676 "j >= 2 and j <= -1 + N }";
3677 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3678 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3679 return -1;
3681 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
3682 W = "{ S1[i] -> a[i] }";
3683 R = "{ S2[i] -> a[i+1] }";
3684 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3685 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3686 return -1;
3688 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
3689 W = "{ S1[i] -> a[i] }";
3690 R = "{ S2[i] -> a[9-i] }";
3691 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3692 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3693 return -1;
3695 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
3696 W = "{ S1[i] -> a[i] }";
3697 R = "[N] -> { S2[i] -> a[N-1-i] }";
3698 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3699 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3700 return -1;
3702 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
3703 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
3704 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
3705 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
3706 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3707 return -1;
3709 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3710 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
3711 R = "{ S2[i,j] -> a[i-1,j] }";
3712 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3713 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3714 return -1;
3716 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3717 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
3718 R = "{ S2[i,j] -> a[i,j-1] }";
3719 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3720 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3721 return -1;
3723 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
3724 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
3725 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
3726 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
3727 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
3728 "S_0[] -> [0, 0, 0] }";
3729 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
3730 return -1;
3731 ctx->opt->schedule_parametric = 0;
3732 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3733 return -1;
3734 ctx->opt->schedule_parametric = 1;
3736 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
3737 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
3738 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
3739 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
3740 "S4[i] -> a[i,N] }";
3741 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
3742 "S4[i] -> [4,i,0] }";
3743 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
3744 isl_options_set_schedule_maximize_coincidence(ctx, 0);
3745 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3746 return -1;
3747 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
3749 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
3750 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3751 "j <= N }";
3752 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3753 "j <= N; "
3754 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
3755 "j <= N }";
3756 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3757 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3758 return -1;
3760 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
3761 " S_2[t] : t >= 0 and t <= -1 + N; "
3762 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
3763 "i <= -1 + N }";
3764 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
3765 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
3766 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
3767 "i >= 0 and i <= -1 + N }";
3768 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
3769 "i >= 0 and i <= -1 + N; "
3770 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
3771 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
3772 " S_0[t] -> [0, t, 0] }";
3774 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3775 return -1;
3776 ctx->opt->schedule_parametric = 0;
3777 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3778 return -1;
3779 ctx->opt->schedule_parametric = 1;
3781 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
3782 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
3783 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
3784 return -1;
3786 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
3787 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
3788 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
3789 "j >= 0 and j <= -1 + N; "
3790 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3791 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
3792 "j >= 0 and j <= -1 + N; "
3793 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3794 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
3795 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3796 return -1;
3798 D = "{ S_0[i] : i >= 0 }";
3799 W = "{ S_0[i] -> a[i] : i >= 0 }";
3800 R = "{ S_0[i] -> a[0] : i >= 0 }";
3801 S = "{ S_0[i] -> [0, i, 0] }";
3802 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3803 return -1;
3805 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
3806 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
3807 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
3808 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
3809 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3810 return -1;
3812 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
3813 "k <= -1 + n and k >= 0 }";
3814 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
3815 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
3816 "k <= -1 + n and k >= 0; "
3817 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
3818 "k <= -1 + n and k >= 0; "
3819 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
3820 "k <= -1 + n and k >= 0 }";
3821 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
3822 ctx->opt->schedule_outer_coincidence = 1;
3823 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3824 return -1;
3825 ctx->opt->schedule_outer_coincidence = 0;
3827 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3828 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3829 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3830 "Stmt_for_body24[i0, i1, 1, 0]:"
3831 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3832 "Stmt_for_body7[i0, i1, i2]:"
3833 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3834 "i2 <= 7 }";
3836 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3837 "Stmt_for_body24[1, i1, i2, i3]:"
3838 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3839 "i2 >= 1;"
3840 "Stmt_for_body24[0, i1, i2, i3] -> "
3841 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3842 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3843 "i3 >= 0;"
3844 "Stmt_for_body24[0, i1, i2, i3] ->"
3845 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3846 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3847 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3848 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3849 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3850 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3851 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3852 "i1 <= 6 and i1 >= 0;"
3853 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3854 "Stmt_for_body7[i0, i1, i2] -> "
3855 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3856 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3857 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3858 "Stmt_for_body7[i0, i1, i2] -> "
3859 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3860 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3861 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3862 P = V;
3863 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3864 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3865 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3867 if (test_special_schedule(ctx, D, V, P, S) < 0)
3868 return -1;
3870 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3871 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3872 "j >= 1 and j <= 7;"
3873 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3874 "j >= 1 and j <= 8 }";
3875 P = "{ }";
3876 S = "{ S_0[i, j] -> [i + j, j] }";
3877 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3878 if (test_special_schedule(ctx, D, V, P, S) < 0)
3879 return -1;
3880 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3882 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3883 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3884 "j >= 0 and j <= -1 + i }";
3885 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3886 "i <= -1 + N and j >= 0;"
3887 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3888 "i <= -2 + N }";
3889 P = "{ }";
3890 S = "{ S_0[i, j] -> [i, j] }";
3891 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3892 if (test_special_schedule(ctx, D, V, P, S) < 0)
3893 return -1;
3894 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3896 /* Test both algorithms on a case with only proximity dependences. */
3897 D = "{ S[i,j] : 0 <= i <= 10 }";
3898 V = "{ }";
3899 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3900 S = "{ S[i, j] -> [j, i] }";
3901 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3902 if (test_special_schedule(ctx, D, V, P, S) < 0)
3903 return -1;
3904 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3905 if (test_special_schedule(ctx, D, V, P, S) < 0)
3906 return -1;
3908 D = "{ A[a]; B[] }";
3909 V = "{}";
3910 P = "{ A[a] -> B[] }";
3911 if (test_has_schedule(ctx, D, V, P) < 0)
3912 return -1;
3914 if (test_padded_schedule(ctx) < 0)
3915 return -1;
3917 /* Check that check for progress is not confused by rational
3918 * solution.
3920 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3921 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3922 "i0 <= -2 + N; "
3923 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3924 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3925 P = "{}";
3926 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3927 if (test_has_schedule(ctx, D, V, P) < 0)
3928 return -1;
3929 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3931 /* Check that we allow schedule rows that are only non-trivial
3932 * on some full-dimensional domains.
3934 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
3935 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
3936 "S1[j] -> S2[1] : 0 <= j <= 1 }";
3937 P = "{}";
3938 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3939 if (test_has_schedule(ctx, D, V, P) < 0)
3940 return -1;
3941 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3943 if (test_conditional_schedule_constraints(ctx) < 0)
3944 return -1;
3946 if (test_strongly_satisfying_schedule(ctx) < 0)
3947 return -1;
3949 if (test_conflicting_context_schedule(ctx) < 0)
3950 return -1;
3952 if (test_bounded_coefficients_schedule(ctx) < 0)
3953 return -1;
3955 return 0;
3958 /* Perform scheduling tests using the whole component scheduler.
3960 static int test_schedule_whole(isl_ctx *ctx)
3962 int whole;
3963 int r;
3965 whole = isl_options_get_schedule_whole_component(ctx);
3966 isl_options_set_schedule_whole_component(ctx, 1);
3967 r = test_schedule(ctx);
3968 isl_options_set_schedule_whole_component(ctx, whole);
3970 return r;
3973 /* Perform scheduling tests using the incremental scheduler.
3975 static int test_schedule_incremental(isl_ctx *ctx)
3977 int whole;
3978 int r;
3980 whole = isl_options_get_schedule_whole_component(ctx);
3981 isl_options_set_schedule_whole_component(ctx, 0);
3982 r = test_schedule(ctx);
3983 isl_options_set_schedule_whole_component(ctx, whole);
3985 return r;
3988 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3990 isl_union_map *umap;
3991 int test;
3993 umap = isl_union_map_read_from_str(ctx, str);
3994 test = isl_union_map_plain_is_injective(umap);
3995 isl_union_map_free(umap);
3996 if (test < 0)
3997 return -1;
3998 if (test == injective)
3999 return 0;
4000 if (injective)
4001 isl_die(ctx, isl_error_unknown,
4002 "map not detected as injective", return -1);
4003 else
4004 isl_die(ctx, isl_error_unknown,
4005 "map detected as injective", return -1);
4008 int test_injective(isl_ctx *ctx)
4010 const char *str;
4012 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4013 return -1;
4014 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4015 return -1;
4016 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4017 return -1;
4018 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4019 return -1;
4020 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4021 return -1;
4022 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4023 return -1;
4024 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4025 return -1;
4026 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4027 return -1;
4029 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4030 if (test_plain_injective(ctx, str, 1))
4031 return -1;
4032 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4033 if (test_plain_injective(ctx, str, 0))
4034 return -1;
4036 return 0;
4039 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4041 isl_aff *aff2;
4042 int equal;
4044 if (!aff)
4045 return -1;
4047 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4048 equal = isl_aff_plain_is_equal(aff, aff2);
4049 isl_aff_free(aff2);
4051 return equal;
4054 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4056 int equal;
4058 equal = aff_plain_is_equal(aff, str);
4059 if (equal < 0)
4060 return -1;
4061 if (!equal)
4062 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4063 "result not as expected", return -1);
4064 return 0;
4067 struct {
4068 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4069 __isl_take isl_aff *aff2);
4070 } aff_bin_op[] = {
4071 ['+'] = { &isl_aff_add },
4072 ['-'] = { &isl_aff_sub },
4073 ['*'] = { &isl_aff_mul },
4074 ['/'] = { &isl_aff_div },
4077 struct {
4078 const char *arg1;
4079 unsigned char op;
4080 const char *arg2;
4081 const char *res;
4082 } aff_bin_tests[] = {
4083 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
4084 "{ [i] -> [2i] }" },
4085 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
4086 "{ [i] -> [0] }" },
4087 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
4088 "{ [i] -> [2i] }" },
4089 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
4090 "{ [i] -> [2i] }" },
4091 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
4092 "{ [i] -> [i/2] }" },
4093 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
4094 "{ [i] -> [i] }" },
4095 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
4096 "{ [i] -> [NaN] }" },
4097 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
4098 "{ [i] -> [NaN] }" },
4099 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
4100 "{ [i] -> [NaN] }" },
4101 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
4102 "{ [i] -> [NaN] }" },
4103 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
4104 "{ [i] -> [NaN] }" },
4105 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
4106 "{ [i] -> [NaN] }" },
4107 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
4108 "{ [i] -> [NaN] }" },
4109 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
4110 "{ [i] -> [NaN] }" },
4111 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
4112 "{ [i] -> [NaN] }" },
4113 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
4114 "{ [i] -> [NaN] }" },
4115 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
4116 "{ [i] -> [NaN] }" },
4117 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
4118 "{ [i] -> [NaN] }" },
4121 /* Perform some basic tests of binary operations on isl_aff objects.
4123 static int test_bin_aff(isl_ctx *ctx)
4125 int i;
4126 isl_aff *aff1, *aff2, *res;
4127 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4128 __isl_take isl_aff *aff2);
4129 int ok;
4131 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
4132 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
4133 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
4134 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
4135 fn = aff_bin_op[aff_bin_tests[i].op].fn;
4136 aff1 = fn(aff1, aff2);
4137 if (isl_aff_is_nan(res))
4138 ok = isl_aff_is_nan(aff1);
4139 else
4140 ok = isl_aff_plain_is_equal(aff1, res);
4141 isl_aff_free(aff1);
4142 isl_aff_free(res);
4143 if (ok < 0)
4144 return -1;
4145 if (!ok)
4146 isl_die(ctx, isl_error_unknown,
4147 "unexpected result", return -1);
4150 return 0;
4153 struct {
4154 __isl_give isl_union_pw_multi_aff *(*fn)(
4155 __isl_take isl_union_pw_multi_aff *upma1,
4156 __isl_take isl_union_pw_multi_aff *upma2);
4157 const char *arg1;
4158 const char *arg2;
4159 const char *res;
4160 } upma_bin_tests[] = {
4161 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
4162 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
4163 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
4164 "{ B[x] -> [2] : x >= 0 }",
4165 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
4166 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
4167 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
4168 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
4169 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
4170 "D[i] -> B[2] : i >= 5 }" },
4171 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4172 "{ B[x] -> C[2] : x > 0 }",
4173 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
4174 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4175 "{ B[x] -> A[2] : x >= 0 }",
4176 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
4179 /* Perform some basic tests of binary operations on
4180 * isl_union_pw_multi_aff objects.
4182 static int test_bin_upma(isl_ctx *ctx)
4184 int i;
4185 isl_union_pw_multi_aff *upma1, *upma2, *res;
4186 int ok;
4188 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
4189 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4190 upma_bin_tests[i].arg1);
4191 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4192 upma_bin_tests[i].arg2);
4193 res = isl_union_pw_multi_aff_read_from_str(ctx,
4194 upma_bin_tests[i].res);
4195 upma1 = upma_bin_tests[i].fn(upma1, upma2);
4196 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
4197 isl_union_pw_multi_aff_free(upma1);
4198 isl_union_pw_multi_aff_free(res);
4199 if (ok < 0)
4200 return -1;
4201 if (!ok)
4202 isl_die(ctx, isl_error_unknown,
4203 "unexpected result", return -1);
4206 return 0;
4209 struct {
4210 __isl_give isl_union_pw_multi_aff *(*fn)(
4211 __isl_take isl_union_pw_multi_aff *upma1,
4212 __isl_take isl_union_pw_multi_aff *upma2);
4213 const char *arg1;
4214 const char *arg2;
4215 } upma_bin_fail_tests[] = {
4216 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4217 "{ B[x] -> C[2] : x >= 0 }" },
4220 /* Perform some basic tests of binary operations on
4221 * isl_union_pw_multi_aff objects that are expected to fail.
4223 static int test_bin_upma_fail(isl_ctx *ctx)
4225 int i, n;
4226 isl_union_pw_multi_aff *upma1, *upma2;
4227 int on_error;
4229 on_error = isl_options_get_on_error(ctx);
4230 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
4231 n = ARRAY_SIZE(upma_bin_fail_tests);
4232 for (i = 0; i < n; ++i) {
4233 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4234 upma_bin_fail_tests[i].arg1);
4235 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4236 upma_bin_fail_tests[i].arg2);
4237 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
4238 isl_union_pw_multi_aff_free(upma1);
4239 if (upma1)
4240 break;
4242 isl_options_set_on_error(ctx, on_error);
4243 if (i < n)
4244 isl_die(ctx, isl_error_unknown,
4245 "operation not expected to succeed", return -1);
4247 return 0;
4250 int test_aff(isl_ctx *ctx)
4252 const char *str;
4253 isl_set *set;
4254 isl_space *space;
4255 isl_local_space *ls;
4256 isl_aff *aff;
4257 int zero, equal;
4259 if (test_bin_aff(ctx) < 0)
4260 return -1;
4261 if (test_bin_upma(ctx) < 0)
4262 return -1;
4263 if (test_bin_upma_fail(ctx) < 0)
4264 return -1;
4266 space = isl_space_set_alloc(ctx, 0, 1);
4267 ls = isl_local_space_from_space(space);
4268 aff = isl_aff_zero_on_domain(ls);
4270 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4271 aff = isl_aff_scale_down_ui(aff, 3);
4272 aff = isl_aff_floor(aff);
4273 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4274 aff = isl_aff_scale_down_ui(aff, 2);
4275 aff = isl_aff_floor(aff);
4276 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4278 str = "{ [10] }";
4279 set = isl_set_read_from_str(ctx, str);
4280 aff = isl_aff_gist(aff, set);
4282 aff = isl_aff_add_constant_si(aff, -16);
4283 zero = isl_aff_plain_is_zero(aff);
4284 isl_aff_free(aff);
4286 if (zero < 0)
4287 return -1;
4288 if (!zero)
4289 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4291 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
4292 aff = isl_aff_scale_down_ui(aff, 64);
4293 aff = isl_aff_floor(aff);
4294 equal = aff_check_plain_equal(aff, "{ [-1] }");
4295 isl_aff_free(aff);
4296 if (equal < 0)
4297 return -1;
4299 return 0;
4302 int test_dim_max(isl_ctx *ctx)
4304 int equal;
4305 const char *str;
4306 isl_set *set1, *set2;
4307 isl_set *set;
4308 isl_map *map;
4309 isl_pw_aff *pwaff;
4311 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
4312 set = isl_set_read_from_str(ctx, str);
4313 pwaff = isl_set_dim_max(set, 0);
4314 set1 = isl_set_from_pw_aff(pwaff);
4315 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
4316 set2 = isl_set_read_from_str(ctx, str);
4317 equal = isl_set_is_equal(set1, set2);
4318 isl_set_free(set1);
4319 isl_set_free(set2);
4320 if (equal < 0)
4321 return -1;
4322 if (!equal)
4323 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4325 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
4326 set = isl_set_read_from_str(ctx, str);
4327 pwaff = isl_set_dim_max(set, 0);
4328 set1 = isl_set_from_pw_aff(pwaff);
4329 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4330 set2 = isl_set_read_from_str(ctx, str);
4331 equal = isl_set_is_equal(set1, set2);
4332 isl_set_free(set1);
4333 isl_set_free(set2);
4334 if (equal < 0)
4335 return -1;
4336 if (!equal)
4337 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4339 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
4340 set = isl_set_read_from_str(ctx, str);
4341 pwaff = isl_set_dim_max(set, 0);
4342 set1 = isl_set_from_pw_aff(pwaff);
4343 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4344 set2 = isl_set_read_from_str(ctx, str);
4345 equal = isl_set_is_equal(set1, set2);
4346 isl_set_free(set1);
4347 isl_set_free(set2);
4348 if (equal < 0)
4349 return -1;
4350 if (!equal)
4351 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4353 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
4354 "0 <= i < N and 0 <= j < M }";
4355 map = isl_map_read_from_str(ctx, str);
4356 set = isl_map_range(map);
4358 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
4359 set1 = isl_set_from_pw_aff(pwaff);
4360 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
4361 set2 = isl_set_read_from_str(ctx, str);
4362 equal = isl_set_is_equal(set1, set2);
4363 isl_set_free(set1);
4364 isl_set_free(set2);
4366 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
4367 set1 = isl_set_from_pw_aff(pwaff);
4368 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
4369 set2 = isl_set_read_from_str(ctx, str);
4370 if (equal >= 0 && equal)
4371 equal = isl_set_is_equal(set1, set2);
4372 isl_set_free(set1);
4373 isl_set_free(set2);
4375 isl_set_free(set);
4377 if (equal < 0)
4378 return -1;
4379 if (!equal)
4380 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4382 /* Check that solutions are properly merged. */
4383 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
4384 "c <= -1 + n - 4a - 2b and c >= -2b and "
4385 "4a >= -4 + n and c >= 0 }";
4386 set = isl_set_read_from_str(ctx, str);
4387 pwaff = isl_set_dim_min(set, 2);
4388 set1 = isl_set_from_pw_aff(pwaff);
4389 str = "[n] -> { [(0)] : n >= 1 }";
4390 set2 = isl_set_read_from_str(ctx, str);
4391 equal = isl_set_is_equal(set1, set2);
4392 isl_set_free(set1);
4393 isl_set_free(set2);
4395 if (equal < 0)
4396 return -1;
4397 if (!equal)
4398 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4400 /* Check that empty solution lie in the right space. */
4401 str = "[n] -> { [t,a] : 1 = 0 }";
4402 set = isl_set_read_from_str(ctx, str);
4403 pwaff = isl_set_dim_max(set, 0);
4404 set1 = isl_set_from_pw_aff(pwaff);
4405 str = "[n] -> { [t] : 1 = 0 }";
4406 set2 = isl_set_read_from_str(ctx, str);
4407 equal = isl_set_is_equal(set1, set2);
4408 isl_set_free(set1);
4409 isl_set_free(set2);
4411 if (equal < 0)
4412 return -1;
4413 if (!equal)
4414 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4416 return 0;
4419 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
4421 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
4422 const char *str)
4424 isl_ctx *ctx;
4425 isl_pw_multi_aff *pma2;
4426 int equal;
4428 if (!pma)
4429 return -1;
4431 ctx = isl_pw_multi_aff_get_ctx(pma);
4432 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4433 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
4434 isl_pw_multi_aff_free(pma2);
4436 return equal;
4439 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
4440 * represented by "str".
4442 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
4443 const char *str)
4445 int equal;
4447 equal = pw_multi_aff_plain_is_equal(pma, str);
4448 if (equal < 0)
4449 return -1;
4450 if (!equal)
4451 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
4452 "result not as expected", return -1);
4453 return 0;
4456 /* Basic test for isl_pw_multi_aff_product.
4458 * Check that multiple pieces are properly handled.
4460 static int test_product_pma(isl_ctx *ctx)
4462 int equal;
4463 const char *str;
4464 isl_pw_multi_aff *pma1, *pma2;
4466 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
4467 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4468 str = "{ C[] -> D[] }";
4469 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4470 pma1 = isl_pw_multi_aff_product(pma1, pma2);
4471 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
4472 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
4473 equal = pw_multi_aff_check_plain_equal(pma1, str);
4474 isl_pw_multi_aff_free(pma1);
4475 if (equal < 0)
4476 return -1;
4478 return 0;
4481 int test_product(isl_ctx *ctx)
4483 const char *str;
4484 isl_set *set;
4485 isl_union_set *uset1, *uset2;
4486 int ok;
4488 str = "{ A[i] }";
4489 set = isl_set_read_from_str(ctx, str);
4490 set = isl_set_product(set, isl_set_copy(set));
4491 ok = isl_set_is_wrapping(set);
4492 isl_set_free(set);
4493 if (ok < 0)
4494 return -1;
4495 if (!ok)
4496 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4498 str = "{ [] }";
4499 uset1 = isl_union_set_read_from_str(ctx, str);
4500 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
4501 str = "{ [[] -> []] }";
4502 uset2 = isl_union_set_read_from_str(ctx, str);
4503 ok = isl_union_set_is_equal(uset1, uset2);
4504 isl_union_set_free(uset1);
4505 isl_union_set_free(uset2);
4506 if (ok < 0)
4507 return -1;
4508 if (!ok)
4509 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4511 if (test_product_pma(ctx) < 0)
4512 return -1;
4514 return 0;
4517 /* Check that two sets are not considered disjoint just because
4518 * they have a different set of (named) parameters.
4520 static int test_disjoint(isl_ctx *ctx)
4522 const char *str;
4523 isl_set *set, *set2;
4524 int disjoint;
4526 str = "[n] -> { [[]->[]] }";
4527 set = isl_set_read_from_str(ctx, str);
4528 str = "{ [[]->[]] }";
4529 set2 = isl_set_read_from_str(ctx, str);
4530 disjoint = isl_set_is_disjoint(set, set2);
4531 isl_set_free(set);
4532 isl_set_free(set2);
4533 if (disjoint < 0)
4534 return -1;
4535 if (disjoint)
4536 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4538 return 0;
4541 int test_equal(isl_ctx *ctx)
4543 const char *str;
4544 isl_set *set, *set2;
4545 int equal;
4547 str = "{ S_6[i] }";
4548 set = isl_set_read_from_str(ctx, str);
4549 str = "{ S_7[i] }";
4550 set2 = isl_set_read_from_str(ctx, str);
4551 equal = isl_set_is_equal(set, set2);
4552 isl_set_free(set);
4553 isl_set_free(set2);
4554 if (equal < 0)
4555 return -1;
4556 if (equal)
4557 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4559 return 0;
4562 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
4563 enum isl_dim_type type, unsigned pos, int fixed)
4565 int test;
4567 test = isl_map_plain_is_fixed(map, type, pos, NULL);
4568 isl_map_free(map);
4569 if (test < 0)
4570 return -1;
4571 if (test == fixed)
4572 return 0;
4573 if (fixed)
4574 isl_die(ctx, isl_error_unknown,
4575 "map not detected as fixed", return -1);
4576 else
4577 isl_die(ctx, isl_error_unknown,
4578 "map detected as fixed", return -1);
4581 int test_fixed(isl_ctx *ctx)
4583 const char *str;
4584 isl_map *map;
4586 str = "{ [i] -> [i] }";
4587 map = isl_map_read_from_str(ctx, str);
4588 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
4589 return -1;
4590 str = "{ [i] -> [1] }";
4591 map = isl_map_read_from_str(ctx, str);
4592 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4593 return -1;
4594 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
4595 map = isl_map_read_from_str(ctx, str);
4596 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4597 return -1;
4598 map = isl_map_read_from_str(ctx, str);
4599 map = isl_map_neg(map);
4600 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4601 return -1;
4603 return 0;
4606 struct isl_vertices_test_data {
4607 const char *set;
4608 int n;
4609 const char *vertex[2];
4610 } vertices_tests[] = {
4611 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
4612 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
4613 { "{ A[t, i] : t = 14 and i = 1 }",
4614 1, { "{ A[14, 1] }" } },
4617 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
4619 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
4621 struct isl_vertices_test_data *data = user;
4622 isl_ctx *ctx;
4623 isl_multi_aff *ma;
4624 isl_basic_set *bset;
4625 isl_pw_multi_aff *pma;
4626 int i;
4627 isl_bool equal;
4629 ctx = isl_vertex_get_ctx(vertex);
4630 bset = isl_vertex_get_domain(vertex);
4631 ma = isl_vertex_get_expr(vertex);
4632 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
4634 for (i = 0; i < data->n; ++i) {
4635 isl_pw_multi_aff *pma_i;
4637 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
4638 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
4639 isl_pw_multi_aff_free(pma_i);
4641 if (equal < 0 || equal)
4642 break;
4645 isl_pw_multi_aff_free(pma);
4646 isl_vertex_free(vertex);
4648 if (equal < 0)
4649 return isl_stat_error;
4651 return equal ? isl_stat_ok : isl_stat_error;
4654 int test_vertices(isl_ctx *ctx)
4656 int i;
4658 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
4659 isl_basic_set *bset;
4660 isl_vertices *vertices;
4661 int ok = 1;
4662 int n;
4664 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
4665 vertices = isl_basic_set_compute_vertices(bset);
4666 n = isl_vertices_get_n_vertices(vertices);
4667 if (vertices_tests[i].n != n)
4668 ok = 0;
4669 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
4670 &vertices_tests[i]) < 0)
4671 ok = 0;
4672 isl_vertices_free(vertices);
4673 isl_basic_set_free(bset);
4675 if (!vertices)
4676 return -1;
4677 if (!ok)
4678 isl_die(ctx, isl_error_unknown, "unexpected vertices",
4679 return -1);
4682 return 0;
4685 int test_union_pw(isl_ctx *ctx)
4687 int equal;
4688 const char *str;
4689 isl_union_set *uset;
4690 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
4692 str = "{ [x] -> x^2 }";
4693 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
4694 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
4695 uset = isl_union_pw_qpolynomial_domain(upwqp1);
4696 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
4697 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
4698 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
4699 isl_union_pw_qpolynomial_free(upwqp1);
4700 isl_union_pw_qpolynomial_free(upwqp2);
4701 if (equal < 0)
4702 return -1;
4703 if (!equal)
4704 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4706 return 0;
4709 /* Test that isl_union_pw_qpolynomial_eval picks up the function
4710 * defined over the correct domain space.
4712 static int test_eval(isl_ctx *ctx)
4714 const char *str;
4715 isl_point *pnt;
4716 isl_set *set;
4717 isl_union_pw_qpolynomial *upwqp;
4718 isl_val *v;
4719 int cmp;
4721 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
4722 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
4723 str = "{ A[6] }";
4724 set = isl_set_read_from_str(ctx, str);
4725 pnt = isl_set_sample_point(set);
4726 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
4727 cmp = isl_val_cmp_si(v, 36);
4728 isl_val_free(v);
4730 if (!v)
4731 return -1;
4732 if (cmp != 0)
4733 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
4735 return 0;
4738 int test_output(isl_ctx *ctx)
4740 char *s;
4741 const char *str;
4742 isl_pw_aff *pa;
4743 isl_printer *p;
4744 int equal;
4746 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
4747 pa = isl_pw_aff_read_from_str(ctx, str);
4749 p = isl_printer_to_str(ctx);
4750 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
4751 p = isl_printer_print_pw_aff(p, pa);
4752 s = isl_printer_get_str(p);
4753 isl_printer_free(p);
4754 isl_pw_aff_free(pa);
4755 if (!s)
4756 equal = -1;
4757 else
4758 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
4759 free(s);
4760 if (equal < 0)
4761 return -1;
4762 if (!equal)
4763 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4765 return 0;
4768 int test_sample(isl_ctx *ctx)
4770 const char *str;
4771 isl_basic_set *bset1, *bset2;
4772 int empty, subset;
4774 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
4775 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
4776 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
4777 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
4778 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
4779 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
4780 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
4781 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
4782 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
4783 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
4784 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
4785 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
4786 "d - 1073741821e and "
4787 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
4788 "3j >= 1 - a + b + 2e and "
4789 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
4790 "3i <= 4 - a + 4b - e and "
4791 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
4792 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
4793 "c <= -1 + a and 3i >= -2 - a + 3e and "
4794 "1073741822e <= 1073741823 - a + 1073741822b + c and "
4795 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
4796 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
4797 "1073741823e >= 1 + 1073741823b - d and "
4798 "3i >= 1073741823b + c - 1073741823e - f and "
4799 "3i >= 1 + 2b + e + 3g }";
4800 bset1 = isl_basic_set_read_from_str(ctx, str);
4801 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
4802 empty = isl_basic_set_is_empty(bset2);
4803 subset = isl_basic_set_is_subset(bset2, bset1);
4804 isl_basic_set_free(bset1);
4805 isl_basic_set_free(bset2);
4806 if (empty < 0 || subset < 0)
4807 return -1;
4808 if (empty)
4809 isl_die(ctx, isl_error_unknown, "point not found", return -1);
4810 if (!subset)
4811 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
4813 return 0;
4816 int test_fixed_power(isl_ctx *ctx)
4818 const char *str;
4819 isl_map *map;
4820 isl_int exp;
4821 int equal;
4823 isl_int_init(exp);
4824 str = "{ [i] -> [i + 1] }";
4825 map = isl_map_read_from_str(ctx, str);
4826 isl_int_set_si(exp, 23);
4827 map = isl_map_fixed_power(map, exp);
4828 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
4829 isl_int_clear(exp);
4830 isl_map_free(map);
4831 if (equal < 0)
4832 return -1;
4834 return 0;
4837 int test_slice(isl_ctx *ctx)
4839 const char *str;
4840 isl_map *map;
4841 int equal;
4843 str = "{ [i] -> [j] }";
4844 map = isl_map_read_from_str(ctx, str);
4845 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
4846 equal = map_check_equal(map, "{ [i] -> [i] }");
4847 isl_map_free(map);
4848 if (equal < 0)
4849 return -1;
4851 str = "{ [i] -> [j] }";
4852 map = isl_map_read_from_str(ctx, str);
4853 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
4854 equal = map_check_equal(map, "{ [i] -> [j] }");
4855 isl_map_free(map);
4856 if (equal < 0)
4857 return -1;
4859 str = "{ [i] -> [j] }";
4860 map = isl_map_read_from_str(ctx, str);
4861 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
4862 equal = map_check_equal(map, "{ [i] -> [-i] }");
4863 isl_map_free(map);
4864 if (equal < 0)
4865 return -1;
4867 str = "{ [i] -> [j] }";
4868 map = isl_map_read_from_str(ctx, str);
4869 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
4870 equal = map_check_equal(map, "{ [0] -> [j] }");
4871 isl_map_free(map);
4872 if (equal < 0)
4873 return -1;
4875 str = "{ [i] -> [j] }";
4876 map = isl_map_read_from_str(ctx, str);
4877 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
4878 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
4879 isl_map_free(map);
4880 if (equal < 0)
4881 return -1;
4883 str = "{ [i] -> [j] }";
4884 map = isl_map_read_from_str(ctx, str);
4885 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
4886 equal = map_check_equal(map, "{ [i] -> [j] : false }");
4887 isl_map_free(map);
4888 if (equal < 0)
4889 return -1;
4891 return 0;
4894 int test_eliminate(isl_ctx *ctx)
4896 const char *str;
4897 isl_map *map;
4898 int equal;
4900 str = "{ [i] -> [j] : i = 2j }";
4901 map = isl_map_read_from_str(ctx, str);
4902 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
4903 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
4904 isl_map_free(map);
4905 if (equal < 0)
4906 return -1;
4908 return 0;
4911 /* Check that isl_set_dim_residue_class detects that the values of j
4912 * in the set below are all odd and that it does not detect any spurious
4913 * strides.
4915 static int test_residue_class(isl_ctx *ctx)
4917 const char *str;
4918 isl_set *set;
4919 isl_int m, r;
4920 int res;
4922 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
4923 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
4924 set = isl_set_read_from_str(ctx, str);
4925 isl_int_init(m);
4926 isl_int_init(r);
4927 res = isl_set_dim_residue_class(set, 1, &m, &r);
4928 if (res >= 0 &&
4929 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
4930 isl_die(ctx, isl_error_unknown, "incorrect residue class",
4931 res = -1);
4932 isl_int_clear(r);
4933 isl_int_clear(m);
4934 isl_set_free(set);
4936 return res;
4939 int test_align_parameters(isl_ctx *ctx)
4941 const char *str;
4942 isl_space *space;
4943 isl_multi_aff *ma1, *ma2;
4944 int equal;
4946 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
4947 ma1 = isl_multi_aff_read_from_str(ctx, str);
4949 space = isl_space_params_alloc(ctx, 1);
4950 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
4951 ma1 = isl_multi_aff_align_params(ma1, space);
4953 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
4954 ma2 = isl_multi_aff_read_from_str(ctx, str);
4956 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4958 isl_multi_aff_free(ma1);
4959 isl_multi_aff_free(ma2);
4961 if (equal < 0)
4962 return -1;
4963 if (!equal)
4964 isl_die(ctx, isl_error_unknown,
4965 "result not as expected", return -1);
4967 return 0;
4970 static int test_list(isl_ctx *ctx)
4972 isl_id *a, *b, *c, *d, *id;
4973 isl_id_list *list;
4974 int ok;
4976 a = isl_id_alloc(ctx, "a", NULL);
4977 b = isl_id_alloc(ctx, "b", NULL);
4978 c = isl_id_alloc(ctx, "c", NULL);
4979 d = isl_id_alloc(ctx, "d", NULL);
4981 list = isl_id_list_alloc(ctx, 4);
4982 list = isl_id_list_add(list, a);
4983 list = isl_id_list_add(list, b);
4984 list = isl_id_list_add(list, c);
4985 list = isl_id_list_add(list, d);
4986 list = isl_id_list_drop(list, 1, 1);
4988 if (isl_id_list_n_id(list) != 3) {
4989 isl_id_list_free(list);
4990 isl_die(ctx, isl_error_unknown,
4991 "unexpected number of elements in list", return -1);
4994 id = isl_id_list_get_id(list, 0);
4995 ok = id == a;
4996 isl_id_free(id);
4997 id = isl_id_list_get_id(list, 1);
4998 ok = ok && id == c;
4999 isl_id_free(id);
5000 id = isl_id_list_get_id(list, 2);
5001 ok = ok && id == d;
5002 isl_id_free(id);
5004 isl_id_list_free(list);
5006 if (!ok)
5007 isl_die(ctx, isl_error_unknown,
5008 "unexpected elements in list", return -1);
5010 return 0;
5013 const char *set_conversion_tests[] = {
5014 "[N] -> { [i] : N - 1 <= 2 i <= N }",
5015 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
5016 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5017 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5018 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
5019 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
5020 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
5021 "-3 + c <= d <= 28 + c) }",
5024 /* Check that converting from isl_set to isl_pw_multi_aff and back
5025 * to isl_set produces the original isl_set.
5027 static int test_set_conversion(isl_ctx *ctx)
5029 int i;
5030 const char *str;
5031 isl_set *set1, *set2;
5032 isl_pw_multi_aff *pma;
5033 int equal;
5035 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
5036 str = set_conversion_tests[i];
5037 set1 = isl_set_read_from_str(ctx, str);
5038 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
5039 set2 = isl_set_from_pw_multi_aff(pma);
5040 equal = isl_set_is_equal(set1, set2);
5041 isl_set_free(set1);
5042 isl_set_free(set2);
5044 if (equal < 0)
5045 return -1;
5046 if (!equal)
5047 isl_die(ctx, isl_error_unknown, "bad conversion",
5048 return -1);
5051 return 0;
5054 const char *conversion_tests[] = {
5055 "{ [a, b, c, d] -> s0[a, b, e, f] : "
5056 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
5057 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
5058 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
5059 "9e <= -2 - 2a) }",
5060 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
5061 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
5062 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
5065 /* Check that converting from isl_map to isl_pw_multi_aff and back
5066 * to isl_map produces the original isl_map.
5068 static int test_map_conversion(isl_ctx *ctx)
5070 int i;
5071 isl_map *map1, *map2;
5072 isl_pw_multi_aff *pma;
5073 int equal;
5075 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
5076 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
5077 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
5078 map2 = isl_map_from_pw_multi_aff(pma);
5079 equal = isl_map_is_equal(map1, map2);
5080 isl_map_free(map1);
5081 isl_map_free(map2);
5083 if (equal < 0)
5084 return -1;
5085 if (!equal)
5086 isl_die(ctx, isl_error_unknown, "bad conversion",
5087 return -1);
5090 return 0;
5093 static int test_conversion(isl_ctx *ctx)
5095 if (test_set_conversion(ctx) < 0)
5096 return -1;
5097 if (test_map_conversion(ctx) < 0)
5098 return -1;
5099 return 0;
5102 /* Check that isl_basic_map_curry does not modify input.
5104 static int test_curry(isl_ctx *ctx)
5106 const char *str;
5107 isl_basic_map *bmap1, *bmap2;
5108 int equal;
5110 str = "{ [A[] -> B[]] -> C[] }";
5111 bmap1 = isl_basic_map_read_from_str(ctx, str);
5112 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
5113 equal = isl_basic_map_is_equal(bmap1, bmap2);
5114 isl_basic_map_free(bmap1);
5115 isl_basic_map_free(bmap2);
5117 if (equal < 0)
5118 return -1;
5119 if (equal)
5120 isl_die(ctx, isl_error_unknown,
5121 "curried map should not be equal to original",
5122 return -1);
5124 return 0;
5127 struct {
5128 const char *set;
5129 const char *ma;
5130 const char *res;
5131 } preimage_tests[] = {
5132 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
5133 "{ A[j,i] -> B[i,j] }",
5134 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
5135 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5136 "{ A[a,b] -> B[a/2,b/6] }",
5137 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
5138 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5139 "{ A[a,b] -> B[a/2,b/6] }",
5140 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
5141 "exists i,j : a = 2 i and b = 6 j }" },
5142 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
5143 "[n] -> { : 0 <= n <= 100 }" },
5144 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5145 "{ A[a] -> B[2a] }",
5146 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
5147 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5148 "{ A[a] -> B[([a/2])] }",
5149 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
5150 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
5151 "{ A[a] -> B[a,a,a/3] }",
5152 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
5153 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
5154 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
5157 static int test_preimage_basic_set(isl_ctx *ctx)
5159 int i;
5160 isl_basic_set *bset1, *bset2;
5161 isl_multi_aff *ma;
5162 int equal;
5164 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
5165 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
5166 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
5167 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
5168 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
5169 equal = isl_basic_set_is_equal(bset1, bset2);
5170 isl_basic_set_free(bset1);
5171 isl_basic_set_free(bset2);
5172 if (equal < 0)
5173 return -1;
5174 if (!equal)
5175 isl_die(ctx, isl_error_unknown, "bad preimage",
5176 return -1);
5179 return 0;
5182 struct {
5183 const char *map;
5184 const char *ma;
5185 const char *res;
5186 } preimage_domain_tests[] = {
5187 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
5188 "{ A[j,i] -> B[i,j] }",
5189 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
5190 { "{ B[i] -> C[i]; D[i] -> E[i] }",
5191 "{ A[i] -> B[i + 1] }",
5192 "{ A[i] -> C[i + 1] }" },
5193 { "{ B[i] -> C[i]; B[i] -> E[i] }",
5194 "{ A[i] -> B[i + 1] }",
5195 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
5196 { "{ B[i] -> C[([i/2])] }",
5197 "{ A[i] -> B[2i] }",
5198 "{ A[i] -> C[i] }" },
5199 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
5200 "{ A[i] -> B[([i/5]), ([i/7])] }",
5201 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
5202 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
5203 "[N] -> { A[] -> B[([N/5])] }",
5204 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
5205 { "{ B[i] -> C[i] : exists a : i = 5 a }",
5206 "{ A[i] -> B[2i] }",
5207 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
5208 { "{ B[i] -> C[i] : exists a : i = 2 a; "
5209 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
5210 "{ A[i] -> B[2i] }",
5211 "{ A[i] -> C[2i] }" },
5212 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
5213 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
5216 static int test_preimage_union_map(isl_ctx *ctx)
5218 int i;
5219 isl_union_map *umap1, *umap2;
5220 isl_multi_aff *ma;
5221 int equal;
5223 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
5224 umap1 = isl_union_map_read_from_str(ctx,
5225 preimage_domain_tests[i].map);
5226 ma = isl_multi_aff_read_from_str(ctx,
5227 preimage_domain_tests[i].ma);
5228 umap2 = isl_union_map_read_from_str(ctx,
5229 preimage_domain_tests[i].res);
5230 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
5231 equal = isl_union_map_is_equal(umap1, umap2);
5232 isl_union_map_free(umap1);
5233 isl_union_map_free(umap2);
5234 if (equal < 0)
5235 return -1;
5236 if (!equal)
5237 isl_die(ctx, isl_error_unknown, "bad preimage",
5238 return -1);
5241 return 0;
5244 static int test_preimage(isl_ctx *ctx)
5246 if (test_preimage_basic_set(ctx) < 0)
5247 return -1;
5248 if (test_preimage_union_map(ctx) < 0)
5249 return -1;
5251 return 0;
5254 struct {
5255 const char *ma1;
5256 const char *ma;
5257 const char *res;
5258 } pullback_tests[] = {
5259 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
5260 "{ A[a,b] -> C[b + 2a] }" },
5261 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
5262 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
5263 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
5264 "{ A[a] -> C[(a)/6] }" },
5265 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
5266 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
5267 "{ A[a] -> C[(2a)/3] }" },
5268 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
5269 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
5270 "{ A[i,j] -> C[i + j, i + j] }"},
5271 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
5272 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
5273 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
5274 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
5275 "{ [i, j] -> [floor((i)/3), j] }",
5276 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
5279 static int test_pullback(isl_ctx *ctx)
5281 int i;
5282 isl_multi_aff *ma1, *ma2;
5283 isl_multi_aff *ma;
5284 int equal;
5286 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
5287 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
5288 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
5289 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
5290 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
5291 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5292 isl_multi_aff_free(ma1);
5293 isl_multi_aff_free(ma2);
5294 if (equal < 0)
5295 return -1;
5296 if (!equal)
5297 isl_die(ctx, isl_error_unknown, "bad pullback",
5298 return -1);
5301 return 0;
5304 /* Check that negation is printed correctly and that equal expressions
5305 * are correctly identified.
5307 static int test_ast(isl_ctx *ctx)
5309 isl_ast_expr *expr, *expr1, *expr2, *expr3;
5310 char *str;
5311 int ok, equal;
5313 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5314 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5315 expr = isl_ast_expr_add(expr1, expr2);
5316 expr2 = isl_ast_expr_copy(expr);
5317 expr = isl_ast_expr_neg(expr);
5318 expr2 = isl_ast_expr_neg(expr2);
5319 equal = isl_ast_expr_is_equal(expr, expr2);
5320 str = isl_ast_expr_to_str(expr);
5321 ok = str ? !strcmp(str, "-(A + B)") : -1;
5322 free(str);
5323 isl_ast_expr_free(expr);
5324 isl_ast_expr_free(expr2);
5326 if (ok < 0 || equal < 0)
5327 return -1;
5328 if (!equal)
5329 isl_die(ctx, isl_error_unknown,
5330 "equal expressions not considered equal", return -1);
5331 if (!ok)
5332 isl_die(ctx, isl_error_unknown,
5333 "isl_ast_expr printed incorrectly", return -1);
5335 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5336 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5337 expr = isl_ast_expr_add(expr1, expr2);
5338 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
5339 expr = isl_ast_expr_sub(expr3, expr);
5340 str = isl_ast_expr_to_str(expr);
5341 ok = str ? !strcmp(str, "C - (A + B)") : -1;
5342 free(str);
5343 isl_ast_expr_free(expr);
5345 if (ok < 0)
5346 return -1;
5347 if (!ok)
5348 isl_die(ctx, isl_error_unknown,
5349 "isl_ast_expr printed incorrectly", return -1);
5351 return 0;
5354 /* Check that isl_ast_build_expr_from_set returns a valid expression
5355 * for an empty set. Note that isl_ast_build_expr_from_set getting
5356 * called on an empty set probably indicates a bug in the caller.
5358 static int test_ast_build(isl_ctx *ctx)
5360 isl_set *set;
5361 isl_ast_build *build;
5362 isl_ast_expr *expr;
5364 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5365 build = isl_ast_build_from_context(set);
5367 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
5368 expr = isl_ast_build_expr_from_set(build, set);
5370 isl_ast_expr_free(expr);
5371 isl_ast_build_free(build);
5373 if (!expr)
5374 return -1;
5376 return 0;
5379 /* Internal data structure for before_for and after_for callbacks.
5381 * depth is the current depth
5382 * before is the number of times before_for has been called
5383 * after is the number of times after_for has been called
5385 struct isl_test_codegen_data {
5386 int depth;
5387 int before;
5388 int after;
5391 /* This function is called before each for loop in the AST generated
5392 * from test_ast_gen1.
5394 * Increment the number of calls and the depth.
5395 * Check that the space returned by isl_ast_build_get_schedule_space
5396 * matches the target space of the schedule returned by
5397 * isl_ast_build_get_schedule.
5398 * Return an isl_id that is checked by the corresponding call
5399 * to after_for.
5401 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
5402 void *user)
5404 struct isl_test_codegen_data *data = user;
5405 isl_ctx *ctx;
5406 isl_space *space;
5407 isl_union_map *schedule;
5408 isl_union_set *uset;
5409 isl_set *set;
5410 int empty;
5411 char name[] = "d0";
5413 ctx = isl_ast_build_get_ctx(build);
5415 if (data->before >= 3)
5416 isl_die(ctx, isl_error_unknown,
5417 "unexpected number of for nodes", return NULL);
5418 if (data->depth >= 2)
5419 isl_die(ctx, isl_error_unknown,
5420 "unexpected depth", return NULL);
5422 snprintf(name, sizeof(name), "d%d", data->depth);
5423 data->before++;
5424 data->depth++;
5426 schedule = isl_ast_build_get_schedule(build);
5427 uset = isl_union_map_range(schedule);
5428 if (!uset)
5429 return NULL;
5430 if (isl_union_set_n_set(uset) != 1) {
5431 isl_union_set_free(uset);
5432 isl_die(ctx, isl_error_unknown,
5433 "expecting single range space", return NULL);
5436 space = isl_ast_build_get_schedule_space(build);
5437 set = isl_union_set_extract_set(uset, space);
5438 isl_union_set_free(uset);
5439 empty = isl_set_is_empty(set);
5440 isl_set_free(set);
5442 if (empty < 0)
5443 return NULL;
5444 if (empty)
5445 isl_die(ctx, isl_error_unknown,
5446 "spaces don't match", return NULL);
5448 return isl_id_alloc(ctx, name, NULL);
5451 /* This function is called after each for loop in the AST generated
5452 * from test_ast_gen1.
5454 * Increment the number of calls and decrement the depth.
5455 * Check that the annotation attached to the node matches
5456 * the isl_id returned by the corresponding call to before_for.
5458 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
5459 __isl_keep isl_ast_build *build, void *user)
5461 struct isl_test_codegen_data *data = user;
5462 isl_id *id;
5463 const char *name;
5464 int valid;
5466 data->after++;
5467 data->depth--;
5469 if (data->after > data->before)
5470 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5471 "mismatch in number of for nodes",
5472 return isl_ast_node_free(node));
5474 id = isl_ast_node_get_annotation(node);
5475 if (!id)
5476 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5477 "missing annotation", return isl_ast_node_free(node));
5479 name = isl_id_get_name(id);
5480 valid = name && atoi(name + 1) == data->depth;
5481 isl_id_free(id);
5483 if (!valid)
5484 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5485 "wrong annotation", return isl_ast_node_free(node));
5487 return node;
5490 /* Check that the before_each_for and after_each_for callbacks
5491 * are called for each for loop in the generated code,
5492 * that they are called in the right order and that the isl_id
5493 * returned from the before_each_for callback is attached to
5494 * the isl_ast_node passed to the corresponding after_each_for call.
5496 static int test_ast_gen1(isl_ctx *ctx)
5498 const char *str;
5499 isl_set *set;
5500 isl_union_map *schedule;
5501 isl_ast_build *build;
5502 isl_ast_node *tree;
5503 struct isl_test_codegen_data data;
5505 str = "[N] -> { : N >= 10 }";
5506 set = isl_set_read_from_str(ctx, str);
5507 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
5508 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
5509 schedule = isl_union_map_read_from_str(ctx, str);
5511 data.before = 0;
5512 data.after = 0;
5513 data.depth = 0;
5514 build = isl_ast_build_from_context(set);
5515 build = isl_ast_build_set_before_each_for(build,
5516 &before_for, &data);
5517 build = isl_ast_build_set_after_each_for(build,
5518 &after_for, &data);
5519 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5520 isl_ast_build_free(build);
5521 if (!tree)
5522 return -1;
5524 isl_ast_node_free(tree);
5526 if (data.before != 3 || data.after != 3)
5527 isl_die(ctx, isl_error_unknown,
5528 "unexpected number of for nodes", return -1);
5530 return 0;
5533 /* Check that the AST generator handles domains that are integrally disjoint
5534 * but not rationally disjoint.
5536 static int test_ast_gen2(isl_ctx *ctx)
5538 const char *str;
5539 isl_set *set;
5540 isl_union_map *schedule;
5541 isl_union_map *options;
5542 isl_ast_build *build;
5543 isl_ast_node *tree;
5545 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
5546 schedule = isl_union_map_read_from_str(ctx, str);
5547 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5548 build = isl_ast_build_from_context(set);
5550 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
5551 options = isl_union_map_read_from_str(ctx, str);
5552 build = isl_ast_build_set_options(build, options);
5553 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5554 isl_ast_build_free(build);
5555 if (!tree)
5556 return -1;
5557 isl_ast_node_free(tree);
5559 return 0;
5562 /* Increment *user on each call.
5564 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
5565 __isl_keep isl_ast_build *build, void *user)
5567 int *n = user;
5569 (*n)++;
5571 return node;
5574 /* Test that unrolling tries to minimize the number of instances.
5575 * In particular, for the schedule given below, make sure it generates
5576 * 3 nodes (rather than 101).
5578 static int test_ast_gen3(isl_ctx *ctx)
5580 const char *str;
5581 isl_set *set;
5582 isl_union_map *schedule;
5583 isl_union_map *options;
5584 isl_ast_build *build;
5585 isl_ast_node *tree;
5586 int n_domain = 0;
5588 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
5589 schedule = isl_union_map_read_from_str(ctx, str);
5590 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5592 str = "{ [i] -> unroll[0] }";
5593 options = isl_union_map_read_from_str(ctx, str);
5595 build = isl_ast_build_from_context(set);
5596 build = isl_ast_build_set_options(build, options);
5597 build = isl_ast_build_set_at_each_domain(build,
5598 &count_domains, &n_domain);
5599 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5600 isl_ast_build_free(build);
5601 if (!tree)
5602 return -1;
5604 isl_ast_node_free(tree);
5606 if (n_domain != 3)
5607 isl_die(ctx, isl_error_unknown,
5608 "unexpected number of for nodes", return -1);
5610 return 0;
5613 /* Check that if the ast_build_exploit_nested_bounds options is set,
5614 * we do not get an outer if node in the generated AST,
5615 * while we do get such an outer if node if the options is not set.
5617 static int test_ast_gen4(isl_ctx *ctx)
5619 const char *str;
5620 isl_set *set;
5621 isl_union_map *schedule;
5622 isl_ast_build *build;
5623 isl_ast_node *tree;
5624 enum isl_ast_node_type type;
5625 int enb;
5627 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
5628 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
5630 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
5632 schedule = isl_union_map_read_from_str(ctx, str);
5633 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5634 build = isl_ast_build_from_context(set);
5635 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5636 isl_ast_build_free(build);
5637 if (!tree)
5638 return -1;
5640 type = isl_ast_node_get_type(tree);
5641 isl_ast_node_free(tree);
5643 if (type == isl_ast_node_if)
5644 isl_die(ctx, isl_error_unknown,
5645 "not expecting if node", return -1);
5647 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
5649 schedule = isl_union_map_read_from_str(ctx, str);
5650 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5651 build = isl_ast_build_from_context(set);
5652 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5653 isl_ast_build_free(build);
5654 if (!tree)
5655 return -1;
5657 type = isl_ast_node_get_type(tree);
5658 isl_ast_node_free(tree);
5660 if (type != isl_ast_node_if)
5661 isl_die(ctx, isl_error_unknown,
5662 "expecting if node", return -1);
5664 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
5666 return 0;
5669 /* This function is called for each leaf in the AST generated
5670 * from test_ast_gen5.
5672 * We finalize the AST generation by extending the outer schedule
5673 * with a zero-dimensional schedule. If this results in any for loops,
5674 * then this means that we did not pass along enough information
5675 * about the outer schedule to the inner AST generation.
5677 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
5678 void *user)
5680 isl_union_map *schedule, *extra;
5681 isl_ast_node *tree;
5683 schedule = isl_ast_build_get_schedule(build);
5684 extra = isl_union_map_copy(schedule);
5685 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
5686 schedule = isl_union_map_range_product(schedule, extra);
5687 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5688 isl_ast_build_free(build);
5690 if (!tree)
5691 return NULL;
5693 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
5694 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
5695 "code should not contain any for loop",
5696 return isl_ast_node_free(tree));
5698 return tree;
5701 /* Check that we do not lose any information when going back and
5702 * forth between internal and external schedule.
5704 * In particular, we create an AST where we unroll the only
5705 * non-constant dimension in the schedule. We therefore do
5706 * not expect any for loops in the AST. However, older versions
5707 * of isl would not pass along enough information about the outer
5708 * schedule when performing an inner code generation from a create_leaf
5709 * callback, resulting in the inner code generation producing a for loop.
5711 static int test_ast_gen5(isl_ctx *ctx)
5713 const char *str;
5714 isl_set *set;
5715 isl_union_map *schedule, *options;
5716 isl_ast_build *build;
5717 isl_ast_node *tree;
5719 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
5720 schedule = isl_union_map_read_from_str(ctx, str);
5722 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
5723 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
5724 options = isl_union_map_read_from_str(ctx, str);
5726 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5727 build = isl_ast_build_from_context(set);
5728 build = isl_ast_build_set_options(build, options);
5729 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
5730 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5731 isl_ast_build_free(build);
5732 isl_ast_node_free(tree);
5733 if (!tree)
5734 return -1;
5736 return 0;
5739 static int test_ast_gen(isl_ctx *ctx)
5741 if (test_ast_gen1(ctx) < 0)
5742 return -1;
5743 if (test_ast_gen2(ctx) < 0)
5744 return -1;
5745 if (test_ast_gen3(ctx) < 0)
5746 return -1;
5747 if (test_ast_gen4(ctx) < 0)
5748 return -1;
5749 if (test_ast_gen5(ctx) < 0)
5750 return -1;
5751 return 0;
5754 /* Check if dropping output dimensions from an isl_pw_multi_aff
5755 * works properly.
5757 static int test_pw_multi_aff(isl_ctx *ctx)
5759 const char *str;
5760 isl_pw_multi_aff *pma1, *pma2;
5761 int equal;
5763 str = "{ [i,j] -> [i+j, 4i-j] }";
5764 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
5765 str = "{ [i,j] -> [4i-j] }";
5766 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5768 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
5770 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
5772 isl_pw_multi_aff_free(pma1);
5773 isl_pw_multi_aff_free(pma2);
5774 if (equal < 0)
5775 return -1;
5776 if (!equal)
5777 isl_die(ctx, isl_error_unknown,
5778 "expressions not equal", return -1);
5780 return 0;
5783 /* Check that we can properly parse multi piecewise affine expressions
5784 * where the piecewise affine expressions have different domains.
5786 static int test_multi_pw_aff(isl_ctx *ctx)
5788 const char *str;
5789 isl_set *dom, *dom2;
5790 isl_multi_pw_aff *mpa1, *mpa2;
5791 isl_pw_aff *pa;
5792 int equal;
5793 int equal_domain;
5795 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
5796 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
5797 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
5798 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
5799 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
5800 str = "{ [i] -> [(i : i > 0), 2i] }";
5801 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
5803 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
5805 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
5806 dom = isl_pw_aff_domain(pa);
5807 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
5808 dom2 = isl_pw_aff_domain(pa);
5809 equal_domain = isl_set_is_equal(dom, dom2);
5811 isl_set_free(dom);
5812 isl_set_free(dom2);
5813 isl_multi_pw_aff_free(mpa1);
5814 isl_multi_pw_aff_free(mpa2);
5816 if (equal < 0)
5817 return -1;
5818 if (!equal)
5819 isl_die(ctx, isl_error_unknown,
5820 "expressions not equal", return -1);
5822 if (equal_domain < 0)
5823 return -1;
5824 if (equal_domain)
5825 isl_die(ctx, isl_error_unknown,
5826 "domains unexpectedly equal", return -1);
5828 return 0;
5831 /* This is a regression test for a bug where isl_basic_map_simplify
5832 * would end up in an infinite loop. In particular, we construct
5833 * an empty basic set that is not obviously empty.
5834 * isl_basic_set_is_empty marks the basic set as empty.
5835 * After projecting out i3, the variable can be dropped completely,
5836 * but isl_basic_map_simplify refrains from doing so if the basic set
5837 * is empty and would end up in an infinite loop if it didn't test
5838 * explicitly for empty basic maps in the outer loop.
5840 static int test_simplify_1(isl_ctx *ctx)
5842 const char *str;
5843 isl_basic_set *bset;
5844 int empty;
5846 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
5847 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
5848 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
5849 "i3 >= i2 }";
5850 bset = isl_basic_set_read_from_str(ctx, str);
5851 empty = isl_basic_set_is_empty(bset);
5852 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
5853 isl_basic_set_free(bset);
5854 if (!bset)
5855 return -1;
5856 if (!empty)
5857 isl_die(ctx, isl_error_unknown,
5858 "basic set should be empty", return -1);
5860 return 0;
5863 /* Check that the equality in the set description below
5864 * is simplified away.
5866 static int test_simplify_2(isl_ctx *ctx)
5868 const char *str;
5869 isl_basic_set *bset;
5870 isl_bool universe;
5872 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
5873 bset = isl_basic_set_read_from_str(ctx, str);
5874 universe = isl_basic_set_plain_is_universe(bset);
5875 isl_basic_set_free(bset);
5877 if (universe < 0)
5878 return -1;
5879 if (!universe)
5880 isl_die(ctx, isl_error_unknown,
5881 "equality not simplified away", return -1);
5882 return 0;
5885 /* Some simplification tests.
5887 static int test_simplify(isl_ctx *ctx)
5889 if (test_simplify_1(ctx) < 0)
5890 return -1;
5891 if (test_simplify_2(ctx) < 0)
5892 return -1;
5893 return 0;
5896 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
5897 * with gbr context would fail to disable the use of the shifted tableau
5898 * when transferring equalities for the input to the context, resulting
5899 * in invalid sample values.
5901 static int test_partial_lexmin(isl_ctx *ctx)
5903 const char *str;
5904 isl_basic_set *bset;
5905 isl_basic_map *bmap;
5906 isl_map *map;
5908 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
5909 bmap = isl_basic_map_read_from_str(ctx, str);
5910 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
5911 bset = isl_basic_set_read_from_str(ctx, str);
5912 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
5913 isl_map_free(map);
5915 if (!map)
5916 return -1;
5918 return 0;
5921 /* Check that the variable compression performed on the existentially
5922 * quantified variables inside isl_basic_set_compute_divs is not confused
5923 * by the implicit equalities among the parameters.
5925 static int test_compute_divs(isl_ctx *ctx)
5927 const char *str;
5928 isl_basic_set *bset;
5929 isl_set *set;
5931 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
5932 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
5933 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
5934 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
5935 bset = isl_basic_set_read_from_str(ctx, str);
5936 set = isl_basic_set_compute_divs(bset);
5937 isl_set_free(set);
5938 if (!set)
5939 return -1;
5941 return 0;
5944 /* Check that the reaching domain elements and the prefix schedule
5945 * at a leaf node are the same before and after grouping.
5947 static int test_schedule_tree_group_1(isl_ctx *ctx)
5949 int equal;
5950 const char *str;
5951 isl_id *id;
5952 isl_union_set *uset;
5953 isl_multi_union_pw_aff *mupa;
5954 isl_union_pw_multi_aff *upma1, *upma2;
5955 isl_union_set *domain1, *domain2;
5956 isl_union_map *umap1, *umap2;
5957 isl_schedule_node *node;
5959 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
5960 uset = isl_union_set_read_from_str(ctx, str);
5961 node = isl_schedule_node_from_domain(uset);
5962 node = isl_schedule_node_child(node, 0);
5963 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
5964 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5965 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5966 node = isl_schedule_node_child(node, 0);
5967 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
5968 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5969 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5970 node = isl_schedule_node_child(node, 0);
5971 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
5972 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
5973 domain1 = isl_schedule_node_get_domain(node);
5974 id = isl_id_alloc(ctx, "group", NULL);
5975 node = isl_schedule_node_parent(node);
5976 node = isl_schedule_node_group(node, id);
5977 node = isl_schedule_node_child(node, 0);
5978 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
5979 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
5980 domain2 = isl_schedule_node_get_domain(node);
5981 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
5982 if (equal >= 0 && equal)
5983 equal = isl_union_set_is_equal(domain1, domain2);
5984 if (equal >= 0 && equal)
5985 equal = isl_union_map_is_equal(umap1, umap2);
5986 isl_union_map_free(umap1);
5987 isl_union_map_free(umap2);
5988 isl_union_set_free(domain1);
5989 isl_union_set_free(domain2);
5990 isl_union_pw_multi_aff_free(upma1);
5991 isl_union_pw_multi_aff_free(upma2);
5992 isl_schedule_node_free(node);
5994 if (equal < 0)
5995 return -1;
5996 if (!equal)
5997 isl_die(ctx, isl_error_unknown,
5998 "expressions not equal", return -1);
6000 return 0;
6003 /* Check that we can have nested groupings and that the union map
6004 * schedule representation is the same before and after the grouping.
6005 * Note that after the grouping, the union map representation contains
6006 * the domain constraints from the ranges of the expansion nodes,
6007 * while they are missing from the union map representation of
6008 * the tree without expansion nodes.
6010 * Also check that the global expansion is as expected.
6012 static int test_schedule_tree_group_2(isl_ctx *ctx)
6014 int equal, equal_expansion;
6015 const char *str;
6016 isl_id *id;
6017 isl_union_set *uset;
6018 isl_union_map *umap1, *umap2;
6019 isl_union_map *expansion1, *expansion2;
6020 isl_union_set_list *filters;
6021 isl_multi_union_pw_aff *mupa;
6022 isl_schedule *schedule;
6023 isl_schedule_node *node;
6025 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
6026 "S3[i,j] : 0 <= i,j < 10 }";
6027 uset = isl_union_set_read_from_str(ctx, str);
6028 node = isl_schedule_node_from_domain(uset);
6029 node = isl_schedule_node_child(node, 0);
6030 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
6031 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6032 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6033 node = isl_schedule_node_child(node, 0);
6034 str = "{ S1[i,j] }";
6035 uset = isl_union_set_read_from_str(ctx, str);
6036 filters = isl_union_set_list_from_union_set(uset);
6037 str = "{ S2[i,j]; S3[i,j] }";
6038 uset = isl_union_set_read_from_str(ctx, str);
6039 filters = isl_union_set_list_add(filters, uset);
6040 node = isl_schedule_node_insert_sequence(node, filters);
6041 node = isl_schedule_node_child(node, 1);
6042 node = isl_schedule_node_child(node, 0);
6043 str = "{ S2[i,j] }";
6044 uset = isl_union_set_read_from_str(ctx, str);
6045 filters = isl_union_set_list_from_union_set(uset);
6046 str = "{ S3[i,j] }";
6047 uset = isl_union_set_read_from_str(ctx, str);
6048 filters = isl_union_set_list_add(filters, uset);
6049 node = isl_schedule_node_insert_sequence(node, filters);
6051 schedule = isl_schedule_node_get_schedule(node);
6052 umap1 = isl_schedule_get_map(schedule);
6053 uset = isl_schedule_get_domain(schedule);
6054 umap1 = isl_union_map_intersect_domain(umap1, uset);
6055 isl_schedule_free(schedule);
6057 node = isl_schedule_node_parent(node);
6058 node = isl_schedule_node_parent(node);
6059 id = isl_id_alloc(ctx, "group1", NULL);
6060 node = isl_schedule_node_group(node, id);
6061 node = isl_schedule_node_child(node, 1);
6062 node = isl_schedule_node_child(node, 0);
6063 id = isl_id_alloc(ctx, "group2", NULL);
6064 node = isl_schedule_node_group(node, id);
6066 schedule = isl_schedule_node_get_schedule(node);
6067 umap2 = isl_schedule_get_map(schedule);
6068 isl_schedule_free(schedule);
6070 node = isl_schedule_node_root(node);
6071 node = isl_schedule_node_child(node, 0);
6072 expansion1 = isl_schedule_node_get_subtree_expansion(node);
6073 isl_schedule_node_free(node);
6075 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
6076 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
6077 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
6079 expansion2 = isl_union_map_read_from_str(ctx, str);
6081 equal = isl_union_map_is_equal(umap1, umap2);
6082 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
6084 isl_union_map_free(umap1);
6085 isl_union_map_free(umap2);
6086 isl_union_map_free(expansion1);
6087 isl_union_map_free(expansion2);
6089 if (equal < 0 || equal_expansion < 0)
6090 return -1;
6091 if (!equal)
6092 isl_die(ctx, isl_error_unknown,
6093 "expressions not equal", return -1);
6094 if (!equal_expansion)
6095 isl_die(ctx, isl_error_unknown,
6096 "unexpected expansion", return -1);
6098 return 0;
6101 /* Some tests for the isl_schedule_node_group function.
6103 static int test_schedule_tree_group(isl_ctx *ctx)
6105 if (test_schedule_tree_group_1(ctx) < 0)
6106 return -1;
6107 if (test_schedule_tree_group_2(ctx) < 0)
6108 return -1;
6109 return 0;
6112 struct {
6113 const char *set;
6114 const char *dual;
6115 } coef_tests[] = {
6116 { "{ rat: [i] : 0 <= i <= 10 }",
6117 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
6118 { "{ rat: [i] : FALSE }",
6119 "{ rat: coefficients[[cst] -> [a]] }" },
6120 { "{ rat: [i] : }",
6121 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
6124 struct {
6125 const char *set;
6126 const char *dual;
6127 } sol_tests[] = {
6128 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
6129 "{ rat: [i] : 0 <= i <= 10 }" },
6130 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
6131 "{ rat: [i] }" },
6132 { "{ rat: coefficients[[cst] -> [a]] }",
6133 "{ rat: [i] : FALSE }" },
6136 /* Test the basic functionality of isl_basic_set_coefficients and
6137 * isl_basic_set_solutions.
6139 static int test_dual(isl_ctx *ctx)
6141 int i;
6143 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
6144 int equal;
6145 isl_basic_set *bset1, *bset2;
6147 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
6148 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
6149 bset1 = isl_basic_set_coefficients(bset1);
6150 equal = isl_basic_set_is_equal(bset1, bset2);
6151 isl_basic_set_free(bset1);
6152 isl_basic_set_free(bset2);
6153 if (equal < 0)
6154 return -1;
6155 if (!equal)
6156 isl_die(ctx, isl_error_unknown,
6157 "incorrect dual", return -1);
6160 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
6161 int equal;
6162 isl_basic_set *bset1, *bset2;
6164 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
6165 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
6166 bset1 = isl_basic_set_solutions(bset1);
6167 equal = isl_basic_set_is_equal(bset1, bset2);
6168 isl_basic_set_free(bset1);
6169 isl_basic_set_free(bset2);
6170 if (equal < 0)
6171 return -1;
6172 if (!equal)
6173 isl_die(ctx, isl_error_unknown,
6174 "incorrect dual", return -1);
6177 return 0;
6180 struct {
6181 int scale_tile;
6182 int shift_point;
6183 const char *domain;
6184 const char *schedule;
6185 const char *sizes;
6186 const char *tile;
6187 const char *point;
6188 } tile_tests[] = {
6189 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6190 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6191 "{ [32,32] }",
6192 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6193 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6195 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6196 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6197 "{ [32,32] }",
6198 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6199 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6201 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6202 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6203 "{ [32,32] }",
6204 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6205 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6207 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6208 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6209 "{ [32,32] }",
6210 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6211 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6215 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
6216 * tile the band and then check if the tile and point bands have the
6217 * expected partial schedule.
6219 static int test_tile(isl_ctx *ctx)
6221 int i;
6222 int scale;
6223 int shift;
6225 scale = isl_options_get_tile_scale_tile_loops(ctx);
6226 shift = isl_options_get_tile_shift_point_loops(ctx);
6228 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
6229 int opt;
6230 int equal;
6231 const char *str;
6232 isl_union_set *domain;
6233 isl_multi_union_pw_aff *mupa, *mupa2;
6234 isl_schedule_node *node;
6235 isl_multi_val *sizes;
6237 opt = tile_tests[i].scale_tile;
6238 isl_options_set_tile_scale_tile_loops(ctx, opt);
6239 opt = tile_tests[i].shift_point;
6240 isl_options_set_tile_shift_point_loops(ctx, opt);
6242 str = tile_tests[i].domain;
6243 domain = isl_union_set_read_from_str(ctx, str);
6244 node = isl_schedule_node_from_domain(domain);
6245 node = isl_schedule_node_child(node, 0);
6246 str = tile_tests[i].schedule;
6247 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6248 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6249 str = tile_tests[i].sizes;
6250 sizes = isl_multi_val_read_from_str(ctx, str);
6251 node = isl_schedule_node_band_tile(node, sizes);
6253 str = tile_tests[i].tile;
6254 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6255 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6256 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
6257 isl_multi_union_pw_aff_free(mupa);
6258 isl_multi_union_pw_aff_free(mupa2);
6260 node = isl_schedule_node_child(node, 0);
6262 str = tile_tests[i].point;
6263 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6264 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6265 if (equal >= 0 && equal)
6266 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
6267 mupa2);
6268 isl_multi_union_pw_aff_free(mupa);
6269 isl_multi_union_pw_aff_free(mupa2);
6271 isl_schedule_node_free(node);
6273 if (equal < 0)
6274 return -1;
6275 if (!equal)
6276 isl_die(ctx, isl_error_unknown,
6277 "unexpected result", return -1);
6280 isl_options_set_tile_scale_tile_loops(ctx, scale);
6281 isl_options_set_tile_shift_point_loops(ctx, shift);
6283 return 0;
6286 /* Check that the domain hash of a space is equal to the hash
6287 * of the domain of the space.
6289 static int test_domain_hash(isl_ctx *ctx)
6291 isl_map *map;
6292 isl_space *space;
6293 uint32_t hash1, hash2;
6295 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
6296 space = isl_map_get_space(map);
6297 isl_map_free(map);
6298 hash1 = isl_space_get_domain_hash(space);
6299 space = isl_space_domain(space);
6300 hash2 = isl_space_get_hash(space);
6301 isl_space_free(space);
6303 if (!space)
6304 return -1;
6305 if (hash1 != hash2)
6306 isl_die(ctx, isl_error_unknown,
6307 "domain hash not equal to hash of domain", return -1);
6309 return 0;
6312 /* Check that a universe basic set that is not obviously equal to the universe
6313 * is still recognized as being equal to the universe.
6315 static int test_universe(isl_ctx *ctx)
6317 const char *s;
6318 isl_basic_set *bset;
6319 isl_bool is_univ;
6321 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
6322 bset = isl_basic_set_read_from_str(ctx, s);
6323 is_univ = isl_basic_set_is_universe(bset);
6324 isl_basic_set_free(bset);
6326 if (is_univ < 0)
6327 return -1;
6328 if (!is_univ)
6329 isl_die(ctx, isl_error_unknown,
6330 "not recognized as universe set", return -1);
6332 return 0;
6335 struct {
6336 const char *name;
6337 int (*fn)(isl_ctx *ctx);
6338 } tests [] = {
6339 { "universe", &test_universe },
6340 { "domain hash", &test_domain_hash },
6341 { "dual", &test_dual },
6342 { "dependence analysis", &test_flow },
6343 { "val", &test_val },
6344 { "compute divs", &test_compute_divs },
6345 { "partial lexmin", &test_partial_lexmin },
6346 { "simplify", &test_simplify },
6347 { "curry", &test_curry },
6348 { "piecewise multi affine expressions", &test_pw_multi_aff },
6349 { "multi piecewise affine expressions", &test_multi_pw_aff },
6350 { "conversion", &test_conversion },
6351 { "list", &test_list },
6352 { "align parameters", &test_align_parameters },
6353 { "preimage", &test_preimage },
6354 { "pullback", &test_pullback },
6355 { "AST", &test_ast },
6356 { "AST build", &test_ast_build },
6357 { "AST generation", &test_ast_gen },
6358 { "eliminate", &test_eliminate },
6359 { "residue class", &test_residue_class },
6360 { "div", &test_div },
6361 { "slice", &test_slice },
6362 { "fixed power", &test_fixed_power },
6363 { "sample", &test_sample },
6364 { "output", &test_output },
6365 { "vertices", &test_vertices },
6366 { "fixed", &test_fixed },
6367 { "equal", &test_equal },
6368 { "disjoint", &test_disjoint },
6369 { "product", &test_product },
6370 { "dim_max", &test_dim_max },
6371 { "affine", &test_aff },
6372 { "injective", &test_injective },
6373 { "schedule (whole component)", &test_schedule_whole },
6374 { "schedule (incremental)", &test_schedule_incremental },
6375 { "schedule tree grouping", &test_schedule_tree_group },
6376 { "tile", &test_tile },
6377 { "union_pw", &test_union_pw },
6378 { "eval", &test_eval },
6379 { "parse", &test_parse },
6380 { "single-valued", &test_sv },
6381 { "affine hull", &test_affine_hull },
6382 { "simple_hull", &test_simple_hull },
6383 { "coalesce", &test_coalesce },
6384 { "factorize", &test_factorize },
6385 { "subset", &test_subset },
6386 { "subtract", &test_subtract },
6387 { "intersect", &test_intersect },
6388 { "lexmin", &test_lexmin },
6389 { "min", &test_min },
6390 { "gist", &test_gist },
6391 { "piecewise quasi-polynomials", &test_pwqp },
6392 { "lift", &test_lift },
6393 { "bound", &test_bound },
6394 { "union", &test_union },
6395 { "split periods", &test_split_periods },
6396 { "lexicographic order", &test_lex },
6397 { "bijectivity", &test_bijective },
6398 { "dataflow analysis", &test_dep },
6399 { "reading", &test_read },
6400 { "bounded", &test_bounded },
6401 { "construction", &test_construction },
6402 { "dimension manipulation", &test_dim },
6403 { "map application", &test_application },
6404 { "convex hull", &test_convex_hull },
6405 { "transitive closure", &test_closure },
6408 int main(int argc, char **argv)
6410 int i;
6411 struct isl_ctx *ctx;
6412 struct isl_options *options;
6414 srcdir = getenv("srcdir");
6415 assert(srcdir);
6417 options = isl_options_new_with_defaults();
6418 assert(options);
6419 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
6421 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
6422 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
6423 printf("%s\n", tests[i].name);
6424 if (tests[i].fn(ctx) < 0)
6425 goto error;
6427 isl_ctx_free(ctx);
6428 return 0;
6429 error:
6430 isl_ctx_free(ctx);
6431 return -1;