isl_map_simplify.c: fix typo in comment
[isl.git] / isl_test.c
blob2c4810dbc4fb3558d530cb7fd1415c64946c5794
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 static int test_gist(struct isl_ctx *ctx)
1390 int i;
1391 const char *str;
1392 isl_basic_set *bset1, *bset2;
1393 isl_map *map1, *map2;
1394 int equal;
1396 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1397 int equal_input;
1398 isl_set *set1, *set2, *copy;
1400 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1401 set2 = isl_set_read_from_str(ctx, gist_tests[i].context);
1402 copy = isl_set_copy(set1);
1403 set1 = isl_set_gist(set1, set2);
1404 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1405 equal = isl_set_is_equal(set1, set2);
1406 isl_set_free(set1);
1407 isl_set_free(set2);
1408 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1409 equal_input = isl_set_is_equal(set1, copy);
1410 isl_set_free(set1);
1411 isl_set_free(copy);
1412 if (equal < 0 || equal_input < 0)
1413 return -1;
1414 if (!equal)
1415 isl_die(ctx, isl_error_unknown,
1416 "incorrect gist result", return -1);
1417 if (!equal_input)
1418 isl_die(ctx, isl_error_unknown,
1419 "gist modified input", return -1);
1422 test_gist_case(ctx, "gist1");
1424 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1425 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1426 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1427 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1428 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1429 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1430 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1431 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1432 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1433 bset1 = isl_basic_set_read_from_str(ctx, str);
1434 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1435 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1436 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1437 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1438 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1439 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1440 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1441 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1442 bset2 = isl_basic_set_read_from_str(ctx, str);
1443 bset1 = isl_basic_set_gist(bset1, bset2);
1444 assert(bset1 && bset1->n_div == 0);
1445 isl_basic_set_free(bset1);
1447 /* Check that the integer divisions of the second disjunct
1448 * do not spread to the first disjunct.
1450 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1451 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1452 "(exists (e0 = [(-1 + t1)/16], "
1453 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1454 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1455 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1456 "o0 <= 4294967295 and t1 <= -1)) }";
1457 map1 = isl_map_read_from_str(ctx, str);
1458 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1459 map2 = isl_map_read_from_str(ctx, str);
1460 map1 = isl_map_gist(map1, map2);
1461 if (!map1)
1462 return -1;
1463 if (map1->n != 1)
1464 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1465 isl_map_free(map1); return -1);
1466 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1467 isl_die(ctx, isl_error_unknown, "expecting single div",
1468 isl_map_free(map1); return -1);
1469 isl_map_free(map1);
1471 if (test_plain_gist(ctx) < 0)
1472 return -1;
1474 return 0;
1477 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1479 isl_set *set, *set2;
1480 int equal;
1481 int one;
1483 set = isl_set_read_from_str(ctx, str);
1484 set = isl_set_coalesce(set);
1485 set2 = isl_set_read_from_str(ctx, str);
1486 equal = isl_set_is_equal(set, set2);
1487 one = set && set->n == 1;
1488 isl_set_free(set);
1489 isl_set_free(set2);
1491 if (equal < 0)
1492 return -1;
1493 if (!equal)
1494 isl_die(ctx, isl_error_unknown,
1495 "coalesced set not equal to input", return -1);
1496 if (check_one && !one)
1497 isl_die(ctx, isl_error_unknown,
1498 "coalesced set should not be a union", return -1);
1500 return 0;
1503 /* Inputs for coalescing tests with unbounded wrapping.
1504 * "str" is a string representation of the input set.
1505 * "single_disjunct" is set if we expect the result to consist of
1506 * a single disjunct.
1508 struct {
1509 int single_disjunct;
1510 const char *str;
1511 } coalesce_unbounded_tests[] = {
1512 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1513 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1514 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1515 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1516 "-10 <= y <= 0}" },
1517 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1518 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1519 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1520 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1523 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1524 * option turned off.
1526 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1528 int i;
1529 int r = 0;
1530 int bounded;
1532 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1533 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1535 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1536 const char *str = coalesce_unbounded_tests[i].str;
1537 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1538 if (test_coalesce_set(ctx, str, check_one) >= 0)
1539 continue;
1540 r = -1;
1541 break;
1544 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1546 return r;
1549 /* Inputs for coalescing tests.
1550 * "str" is a string representation of the input set.
1551 * "single_disjunct" is set if we expect the result to consist of
1552 * a single disjunct.
1554 struct {
1555 int single_disjunct;
1556 const char *str;
1557 } coalesce_tests[] = {
1558 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1559 "y >= x & x >= 2 & 5 >= y }" },
1560 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1561 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1562 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1563 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1564 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1565 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1566 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1567 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1568 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1569 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1570 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1571 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1572 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1573 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1574 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1575 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1576 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1577 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1578 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1579 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1580 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1581 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1582 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1583 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1584 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1585 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1586 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1587 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1588 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1589 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1590 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1591 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1592 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1593 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1594 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1595 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1596 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1597 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1598 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1599 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1600 "[o0, o1, o2, o3, o4, o5, o6]] : "
1601 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1602 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1603 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1604 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1605 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1606 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1607 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1608 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1609 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1610 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1611 "o6 >= i3 + i6 - o3 and M >= 0 and "
1612 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1613 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1614 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1615 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1616 "(o0 = 0 and M >= 2 and N >= 3) or "
1617 "(M = 0 and o0 = 0 and N >= 3) }" },
1618 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1619 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1620 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1621 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1622 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1623 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1624 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1625 "(y = 3 and x = 1) }" },
1626 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1627 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1628 "i1 <= M and i3 <= M and i4 <= M) or "
1629 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1630 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1631 "i4 <= -1 + M) }" },
1632 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1633 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1634 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1635 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1636 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1637 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1638 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1639 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1640 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1641 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1642 { 0, "{ [a, b] : exists e : 2e = a and "
1643 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1644 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1645 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1646 "j >= 1 and j' <= i + j - i' and i >= 1; "
1647 "[1, 1, 1, 1] }" },
1648 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1649 "[i,j] : exists a : j = 3a }" },
1650 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1651 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1652 "a >= 3) or "
1653 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1654 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1655 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1656 "c <= 6 + 8a and a >= 3; "
1657 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1658 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1659 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1660 "[x,0] : 3 <= x <= 4 }" },
1661 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1662 "[x,0] : 4 <= x <= 5 }" },
1663 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1664 "[x,0] : 3 <= x <= 5 }" },
1665 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1666 "[x,0] : 3 <= x <= 4 }" },
1667 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1668 "i1 <= 0; "
1669 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1670 { 1, "{ [0,0]; [1,1] }" },
1671 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1672 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1673 "ii <= k;"
1674 "[k, 0, k] : k <= 6 and k >= 1 }" },
1675 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1676 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1677 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1678 { 1, "[n] -> { [1] : n >= 0;"
1679 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1680 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1681 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1682 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1683 "2e0 <= x and 2e0 <= n);"
1684 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1685 "n >= 0) }" },
1686 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1687 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1688 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1689 "t1 = 1 }" },
1690 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1691 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1692 "[0, 0] }" },
1693 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1694 "t1 >= 13 and t1 <= 16);"
1695 "[t1] : t1 <= 15 and t1 >= 12 }" },
1696 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
1697 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
1698 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
1699 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
1700 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
1701 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
1702 "i <= 4j + 2 }" },
1703 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
1704 "(exists (e0 : 3e0 = -2 + c0)) }" },
1705 { 0, "[n, b0, t0] -> "
1706 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1707 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1708 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1709 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1710 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
1711 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
1712 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
1713 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
1714 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1715 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1716 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1717 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
1718 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
1719 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
1720 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
1721 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
1722 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
1723 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
1724 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
1725 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
1726 { 0, "{ [i0, i1, i2] : "
1727 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
1728 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
1729 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
1730 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
1731 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
1732 "32e0 <= 31 + i0)) or "
1733 "i0 >= 0 }" },
1734 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
1735 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
1736 "2*floor((c)/2) = c and 0 <= a <= 192;"
1737 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
1741 /* A specialized coalescing test case that would result
1742 * in a segmentation fault or a failed assertion in earlier versions of isl.
1744 static int test_coalesce_special(struct isl_ctx *ctx)
1746 const char *str;
1747 isl_map *map1, *map2;
1749 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1750 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
1751 "(y = 201 and o1 <= 239 and o1 >= 212) or "
1752 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
1753 "o1 <= 239 and o1 >= 212)) or "
1754 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
1755 "o1 <= 241 and o1 >= 240));"
1756 "[S_L220_OUT[] -> T7[]] -> "
1757 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
1758 "(y = 2 and o1 <= 241 and o1 >= 212) or "
1759 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
1760 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
1761 map1 = isl_map_read_from_str(ctx, str);
1762 map1 = isl_map_align_divs(map1);
1763 map1 = isl_map_coalesce(map1);
1764 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1765 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
1766 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
1767 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
1768 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
1769 map2 = isl_map_read_from_str(ctx, str);
1770 map2 = isl_map_union(map2, map1);
1771 map2 = isl_map_align_divs(map2);
1772 map2 = isl_map_coalesce(map2);
1773 isl_map_free(map2);
1774 if (!map2)
1775 return -1;
1777 return 0;
1780 /* Test the functionality of isl_set_coalesce.
1781 * That is, check that the output is always equal to the input
1782 * and in some cases that the result consists of a single disjunct.
1784 static int test_coalesce(struct isl_ctx *ctx)
1786 int i;
1788 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1789 const char *str = coalesce_tests[i].str;
1790 int check_one = coalesce_tests[i].single_disjunct;
1791 if (test_coalesce_set(ctx, str, check_one) < 0)
1792 return -1;
1795 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1796 return -1;
1797 if (test_coalesce_special(ctx) < 0)
1798 return -1;
1800 return 0;
1803 /* Construct a representation of the graph on the right of Figure 1
1804 * in "Computing the Transitive Closure of a Union of
1805 * Affine Integer Tuple Relations".
1807 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
1809 isl_set *dom;
1810 isl_map *up, *right;
1812 dom = isl_set_read_from_str(ctx,
1813 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1814 "2 x - 3 y + 3 >= 0 }");
1815 right = isl_map_read_from_str(ctx,
1816 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1817 up = isl_map_read_from_str(ctx,
1818 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1819 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1820 right = isl_map_intersect_range(right, isl_set_copy(dom));
1821 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1822 up = isl_map_intersect_range(up, dom);
1823 return isl_map_union(up, right);
1826 /* Construct a representation of the power of the graph
1827 * on the right of Figure 1 in "Computing the Transitive Closure of
1828 * a Union of Affine Integer Tuple Relations".
1830 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
1832 return isl_map_read_from_str(ctx,
1833 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
1834 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
1835 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
1838 /* Construct a representation of the transitive closure of the graph
1839 * on the right of Figure 1 in "Computing the Transitive Closure of
1840 * a Union of Affine Integer Tuple Relations".
1842 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
1844 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
1847 static int test_closure(isl_ctx *ctx)
1849 const char *str;
1850 isl_map *map, *map2;
1851 int exact, equal;
1853 /* COCOA example 1 */
1854 map = isl_map_read_from_str(ctx,
1855 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1856 "1 <= i and i < n and 1 <= j and j < n or "
1857 "i2 = i + 1 and j2 = j - 1 and "
1858 "1 <= i and i < n and 2 <= j and j <= n }");
1859 map = isl_map_power(map, &exact);
1860 assert(exact);
1861 isl_map_free(map);
1863 /* COCOA example 1 */
1864 map = isl_map_read_from_str(ctx,
1865 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1866 "1 <= i and i < n and 1 <= j and j < n or "
1867 "i2 = i + 1 and j2 = j - 1 and "
1868 "1 <= i and i < n and 2 <= j and j <= n }");
1869 map = isl_map_transitive_closure(map, &exact);
1870 assert(exact);
1871 map2 = isl_map_read_from_str(ctx,
1872 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1873 "1 <= i and i < n and 1 <= j and j <= n and "
1874 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1875 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1876 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1877 assert(isl_map_is_equal(map, map2));
1878 isl_map_free(map2);
1879 isl_map_free(map);
1881 map = isl_map_read_from_str(ctx,
1882 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1883 " 0 <= y and y <= n }");
1884 map = isl_map_transitive_closure(map, &exact);
1885 map2 = isl_map_read_from_str(ctx,
1886 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1887 " 0 <= y and y <= n }");
1888 assert(isl_map_is_equal(map, map2));
1889 isl_map_free(map2);
1890 isl_map_free(map);
1892 /* COCOA example 2 */
1893 map = isl_map_read_from_str(ctx,
1894 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1895 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1896 "i2 = i + 2 and j2 = j - 2 and "
1897 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1898 map = isl_map_transitive_closure(map, &exact);
1899 assert(exact);
1900 map2 = isl_map_read_from_str(ctx,
1901 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1902 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1903 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1904 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1905 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1906 assert(isl_map_is_equal(map, map2));
1907 isl_map_free(map);
1908 isl_map_free(map2);
1910 /* COCOA Fig.2 left */
1911 map = isl_map_read_from_str(ctx,
1912 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1913 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1914 "j <= n or "
1915 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1916 "j <= 2 i - 3 and j <= n - 2 or "
1917 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1918 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1919 map = isl_map_transitive_closure(map, &exact);
1920 assert(exact);
1921 isl_map_free(map);
1923 /* COCOA Fig.2 right */
1924 map = isl_map_read_from_str(ctx,
1925 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1926 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1927 "j <= n or "
1928 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1929 "j <= 2 i - 4 and j <= n - 3 or "
1930 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1931 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1932 map = isl_map_power(map, &exact);
1933 assert(exact);
1934 isl_map_free(map);
1936 /* COCOA Fig.2 right */
1937 map = isl_map_read_from_str(ctx,
1938 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1939 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1940 "j <= n or "
1941 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1942 "j <= 2 i - 4 and j <= n - 3 or "
1943 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1944 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1945 map = isl_map_transitive_closure(map, &exact);
1946 assert(exact);
1947 map2 = isl_map_read_from_str(ctx,
1948 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1949 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1950 "j <= n and 3 + i + 2 j <= 3 n and "
1951 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1952 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1953 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1954 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1955 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1956 assert(isl_map_is_equal(map, map2));
1957 isl_map_free(map2);
1958 isl_map_free(map);
1960 map = cocoa_fig_1_right_graph(ctx);
1961 map = isl_map_transitive_closure(map, &exact);
1962 assert(exact);
1963 map2 = cocoa_fig_1_right_tc(ctx);
1964 assert(isl_map_is_equal(map, map2));
1965 isl_map_free(map2);
1966 isl_map_free(map);
1968 map = cocoa_fig_1_right_graph(ctx);
1969 map = isl_map_power(map, &exact);
1970 map2 = cocoa_fig_1_right_power(ctx);
1971 equal = isl_map_is_equal(map, map2);
1972 isl_map_free(map2);
1973 isl_map_free(map);
1974 if (equal < 0)
1975 return -1;
1976 if (!exact)
1977 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
1978 if (!equal)
1979 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
1981 /* COCOA Theorem 1 counter example */
1982 map = isl_map_read_from_str(ctx,
1983 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1984 "i2 = 1 and j2 = j or "
1985 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1986 map = isl_map_transitive_closure(map, &exact);
1987 assert(exact);
1988 isl_map_free(map);
1990 map = isl_map_read_from_str(ctx,
1991 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1992 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1993 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1994 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1995 map = isl_map_transitive_closure(map, &exact);
1996 assert(exact);
1997 isl_map_free(map);
1999 /* Kelly et al 1996, fig 12 */
2000 map = isl_map_read_from_str(ctx,
2001 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2002 "1 <= i,j,j+1 <= n or "
2003 "j = n and j2 = 1 and i2 = i + 1 and "
2004 "1 <= i,i+1 <= n }");
2005 map = isl_map_transitive_closure(map, &exact);
2006 assert(exact);
2007 map2 = isl_map_read_from_str(ctx,
2008 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2009 "1 <= i <= n and i = i2 or "
2010 "1 <= i < i2 <= n and 1 <= j <= n and "
2011 "1 <= j2 <= n }");
2012 assert(isl_map_is_equal(map, map2));
2013 isl_map_free(map2);
2014 isl_map_free(map);
2016 /* Omega's closure4 */
2017 map = isl_map_read_from_str(ctx,
2018 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2019 "1 <= x,y <= 10 or "
2020 "x2 = x + 1 and y2 = y and "
2021 "1 <= x <= 20 && 5 <= y <= 15 }");
2022 map = isl_map_transitive_closure(map, &exact);
2023 assert(exact);
2024 isl_map_free(map);
2026 map = isl_map_read_from_str(ctx,
2027 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2028 map = isl_map_transitive_closure(map, &exact);
2029 assert(!exact);
2030 map2 = isl_map_read_from_str(ctx,
2031 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2032 assert(isl_map_is_equal(map, map2));
2033 isl_map_free(map);
2034 isl_map_free(map2);
2036 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2037 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2038 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2039 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2040 map = isl_map_read_from_str(ctx, str);
2041 map = isl_map_transitive_closure(map, &exact);
2042 assert(exact);
2043 map2 = isl_map_read_from_str(ctx, str);
2044 assert(isl_map_is_equal(map, map2));
2045 isl_map_free(map);
2046 isl_map_free(map2);
2048 str = "{[0] -> [1]; [2] -> [3]}";
2049 map = isl_map_read_from_str(ctx, str);
2050 map = isl_map_transitive_closure(map, &exact);
2051 assert(exact);
2052 map2 = isl_map_read_from_str(ctx, str);
2053 assert(isl_map_is_equal(map, map2));
2054 isl_map_free(map);
2055 isl_map_free(map2);
2057 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2058 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2059 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2060 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2061 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2062 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2063 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2064 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2065 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2066 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2067 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2068 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2069 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2070 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2071 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2072 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2073 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2074 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2075 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2076 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2077 map = isl_map_read_from_str(ctx, str);
2078 map = isl_map_transitive_closure(map, NULL);
2079 assert(map);
2080 isl_map_free(map);
2082 return 0;
2085 static int test_lex(struct isl_ctx *ctx)
2087 isl_space *dim;
2088 isl_map *map;
2089 int empty;
2091 dim = isl_space_set_alloc(ctx, 0, 0);
2092 map = isl_map_lex_le(dim);
2093 empty = isl_map_is_empty(map);
2094 isl_map_free(map);
2096 if (empty < 0)
2097 return -1;
2098 if (empty)
2099 isl_die(ctx, isl_error_unknown,
2100 "expecting non-empty result", return -1);
2102 return 0;
2105 static int test_lexmin(struct isl_ctx *ctx)
2107 int equal;
2108 const char *str;
2109 isl_basic_map *bmap;
2110 isl_map *map, *map2;
2111 isl_set *set;
2112 isl_set *set2;
2113 isl_pw_multi_aff *pma;
2115 str = "[p0, p1] -> { [] -> [] : "
2116 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2117 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2118 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2119 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2120 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2121 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2122 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2123 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2124 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2125 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2126 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2127 map = isl_map_read_from_str(ctx, str);
2128 map = isl_map_lexmin(map);
2129 isl_map_free(map);
2131 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2132 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2133 set = isl_set_read_from_str(ctx, str);
2134 set = isl_set_lexmax(set);
2135 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2136 set2 = isl_set_read_from_str(ctx, str);
2137 set = isl_set_intersect(set, set2);
2138 assert(!isl_set_is_empty(set));
2139 isl_set_free(set);
2141 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
2142 map = isl_map_read_from_str(ctx, str);
2143 map = isl_map_lexmin(map);
2144 str = "{ [x] -> [5] : 6 <= x <= 8; "
2145 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
2146 map2 = isl_map_read_from_str(ctx, str);
2147 assert(isl_map_is_equal(map, map2));
2148 isl_map_free(map);
2149 isl_map_free(map2);
2151 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
2152 map = isl_map_read_from_str(ctx, str);
2153 map2 = isl_map_copy(map);
2154 map = isl_map_lexmin(map);
2155 assert(isl_map_is_equal(map, map2));
2156 isl_map_free(map);
2157 isl_map_free(map2);
2159 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
2160 map = isl_map_read_from_str(ctx, str);
2161 map = isl_map_lexmin(map);
2162 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
2163 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2164 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2165 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
2166 map2 = isl_map_read_from_str(ctx, str);
2167 assert(isl_map_is_equal(map, map2));
2168 isl_map_free(map);
2169 isl_map_free(map2);
2171 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2172 " 8i' <= i and 8i' >= -7 + i }";
2173 bmap = isl_basic_map_read_from_str(ctx, str);
2174 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2175 map2 = isl_map_from_pw_multi_aff(pma);
2176 map = isl_map_from_basic_map(bmap);
2177 assert(isl_map_is_equal(map, map2));
2178 isl_map_free(map);
2179 isl_map_free(map2);
2181 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
2182 map = isl_map_read_from_str(ctx, str);
2183 map = isl_map_lexmin(map);
2184 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
2185 map2 = isl_map_read_from_str(ctx, str);
2186 assert(isl_map_is_equal(map, map2));
2187 isl_map_free(map);
2188 isl_map_free(map2);
2190 /* Check that empty pieces are properly combined. */
2191 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2192 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2193 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
2194 map = isl_map_read_from_str(ctx, str);
2195 map = isl_map_lexmin(map);
2196 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2197 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2198 "x >= 4 }";
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 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2205 " 8i' <= i and 8i' >= -7 + i }";
2206 set = isl_set_read_from_str(ctx, str);
2207 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2208 set2 = isl_set_from_pw_multi_aff(pma);
2209 equal = isl_set_is_equal(set, set2);
2210 isl_set_free(set);
2211 isl_set_free(set2);
2212 if (equal < 0)
2213 return -1;
2214 if (!equal)
2215 isl_die(ctx, isl_error_unknown,
2216 "unexpected difference between set and "
2217 "piecewise affine expression", return -1);
2219 return 0;
2222 struct {
2223 const char *set;
2224 const char *obj;
2225 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2226 __isl_keep isl_aff *obj);
2227 const char *res;
2228 } opt_tests[] = {
2229 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2230 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2231 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2232 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2233 &isl_set_max_val, "30" },
2237 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2238 * In particular, check the results on non-convex inputs.
2240 static int test_min(struct isl_ctx *ctx)
2242 int i;
2243 isl_set *set;
2244 isl_aff *obj;
2245 isl_val *val, *res;
2246 isl_bool ok;
2248 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2249 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2250 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2251 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2252 val = opt_tests[i].fn(set, obj);
2253 ok = isl_val_eq(res, val);
2254 isl_val_free(res);
2255 isl_val_free(val);
2256 isl_aff_free(obj);
2257 isl_set_free(set);
2259 if (ok < 0)
2260 return -1;
2261 if (!ok)
2262 isl_die(ctx, isl_error_unknown,
2263 "unexpected optimum", return -1);
2266 return 0;
2269 struct must_may {
2270 isl_map *must;
2271 isl_map *may;
2274 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2275 void *dep_user, void *user)
2277 struct must_may *mm = (struct must_may *)user;
2279 if (must)
2280 mm->must = isl_map_union(mm->must, dep);
2281 else
2282 mm->may = isl_map_union(mm->may, dep);
2284 return isl_stat_ok;
2287 static int common_space(void *first, void *second)
2289 int depth = *(int *)first;
2290 return 2 * depth;
2293 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2295 isl_map *map2;
2296 int equal;
2298 if (!map)
2299 return -1;
2301 map2 = isl_map_read_from_str(map->ctx, str);
2302 equal = isl_map_is_equal(map, map2);
2303 isl_map_free(map2);
2305 return equal;
2308 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2310 int equal;
2312 equal = map_is_equal(map, str);
2313 if (equal < 0)
2314 return -1;
2315 if (!equal)
2316 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2317 "result not as expected", return -1);
2318 return 0;
2321 static int test_dep(struct isl_ctx *ctx)
2323 const char *str;
2324 isl_space *dim;
2325 isl_map *map;
2326 isl_access_info *ai;
2327 isl_flow *flow;
2328 int depth;
2329 struct must_may mm;
2331 depth = 3;
2333 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2334 map = isl_map_read_from_str(ctx, str);
2335 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2337 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2338 map = isl_map_read_from_str(ctx, str);
2339 ai = isl_access_info_add_source(ai, map, 1, &depth);
2341 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2342 map = isl_map_read_from_str(ctx, str);
2343 ai = isl_access_info_add_source(ai, map, 1, &depth);
2345 flow = isl_access_info_compute_flow(ai);
2346 dim = isl_space_alloc(ctx, 0, 3, 3);
2347 mm.must = isl_map_empty(isl_space_copy(dim));
2348 mm.may = isl_map_empty(dim);
2350 isl_flow_foreach(flow, collect_must_may, &mm);
2352 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2353 " [1,10,0] -> [2,5,0] }";
2354 assert(map_is_equal(mm.must, str));
2355 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2356 assert(map_is_equal(mm.may, str));
2358 isl_map_free(mm.must);
2359 isl_map_free(mm.may);
2360 isl_flow_free(flow);
2363 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2364 map = isl_map_read_from_str(ctx, str);
2365 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2367 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2368 map = isl_map_read_from_str(ctx, str);
2369 ai = isl_access_info_add_source(ai, map, 1, &depth);
2371 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2372 map = isl_map_read_from_str(ctx, str);
2373 ai = isl_access_info_add_source(ai, map, 0, &depth);
2375 flow = isl_access_info_compute_flow(ai);
2376 dim = isl_space_alloc(ctx, 0, 3, 3);
2377 mm.must = isl_map_empty(isl_space_copy(dim));
2378 mm.may = isl_map_empty(dim);
2380 isl_flow_foreach(flow, collect_must_may, &mm);
2382 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2383 assert(map_is_equal(mm.must, str));
2384 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2385 assert(map_is_equal(mm.may, str));
2387 isl_map_free(mm.must);
2388 isl_map_free(mm.may);
2389 isl_flow_free(flow);
2392 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2393 map = isl_map_read_from_str(ctx, str);
2394 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2396 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2397 map = isl_map_read_from_str(ctx, str);
2398 ai = isl_access_info_add_source(ai, map, 0, &depth);
2400 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2401 map = isl_map_read_from_str(ctx, str);
2402 ai = isl_access_info_add_source(ai, map, 0, &depth);
2404 flow = isl_access_info_compute_flow(ai);
2405 dim = isl_space_alloc(ctx, 0, 3, 3);
2406 mm.must = isl_map_empty(isl_space_copy(dim));
2407 mm.may = isl_map_empty(dim);
2409 isl_flow_foreach(flow, collect_must_may, &mm);
2411 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2412 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2413 assert(map_is_equal(mm.may, str));
2414 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2415 assert(map_is_equal(mm.must, str));
2417 isl_map_free(mm.must);
2418 isl_map_free(mm.may);
2419 isl_flow_free(flow);
2422 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2423 map = isl_map_read_from_str(ctx, str);
2424 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2426 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2427 map = isl_map_read_from_str(ctx, str);
2428 ai = isl_access_info_add_source(ai, map, 0, &depth);
2430 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2431 map = isl_map_read_from_str(ctx, str);
2432 ai = isl_access_info_add_source(ai, map, 0, &depth);
2434 flow = isl_access_info_compute_flow(ai);
2435 dim = isl_space_alloc(ctx, 0, 3, 3);
2436 mm.must = isl_map_empty(isl_space_copy(dim));
2437 mm.may = isl_map_empty(dim);
2439 isl_flow_foreach(flow, collect_must_may, &mm);
2441 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2442 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2443 assert(map_is_equal(mm.may, str));
2444 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2445 assert(map_is_equal(mm.must, str));
2447 isl_map_free(mm.must);
2448 isl_map_free(mm.may);
2449 isl_flow_free(flow);
2452 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2453 map = isl_map_read_from_str(ctx, str);
2454 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2456 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2457 map = isl_map_read_from_str(ctx, str);
2458 ai = isl_access_info_add_source(ai, map, 0, &depth);
2460 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2461 map = isl_map_read_from_str(ctx, str);
2462 ai = isl_access_info_add_source(ai, map, 0, &depth);
2464 flow = isl_access_info_compute_flow(ai);
2465 dim = isl_space_alloc(ctx, 0, 3, 3);
2466 mm.must = isl_map_empty(isl_space_copy(dim));
2467 mm.may = isl_map_empty(dim);
2469 isl_flow_foreach(flow, collect_must_may, &mm);
2471 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2472 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2473 assert(map_is_equal(mm.may, str));
2474 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2475 assert(map_is_equal(mm.must, str));
2477 isl_map_free(mm.must);
2478 isl_map_free(mm.may);
2479 isl_flow_free(flow);
2482 depth = 5;
2484 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2485 map = isl_map_read_from_str(ctx, str);
2486 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2488 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2489 map = isl_map_read_from_str(ctx, str);
2490 ai = isl_access_info_add_source(ai, map, 1, &depth);
2492 flow = isl_access_info_compute_flow(ai);
2493 dim = isl_space_alloc(ctx, 0, 5, 5);
2494 mm.must = isl_map_empty(isl_space_copy(dim));
2495 mm.may = isl_map_empty(dim);
2497 isl_flow_foreach(flow, collect_must_may, &mm);
2499 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2500 assert(map_is_equal(mm.must, str));
2501 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2502 assert(map_is_equal(mm.may, str));
2504 isl_map_free(mm.must);
2505 isl_map_free(mm.may);
2506 isl_flow_free(flow);
2508 return 0;
2511 /* Check that the dependence analysis proceeds without errors.
2512 * Earlier versions of isl would break down during the analysis
2513 * due to the use of the wrong spaces.
2515 static int test_flow(isl_ctx *ctx)
2517 const char *str;
2518 isl_union_map *access, *schedule;
2519 isl_union_map *must_dep, *may_dep;
2520 int r;
2522 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2523 access = isl_union_map_read_from_str(ctx, str);
2524 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2525 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2526 "S2[] -> [1,0,0,0]; "
2527 "S3[] -> [-1,0,0,0] }";
2528 schedule = isl_union_map_read_from_str(ctx, str);
2529 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2530 isl_union_map_copy(access), schedule,
2531 &must_dep, &may_dep, NULL, NULL);
2532 isl_union_map_free(may_dep);
2533 isl_union_map_free(must_dep);
2535 return r;
2538 struct {
2539 const char *map;
2540 int sv;
2541 } sv_tests[] = {
2542 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2543 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2544 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2545 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2546 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2547 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2548 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2549 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2550 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2553 int test_sv(isl_ctx *ctx)
2555 isl_union_map *umap;
2556 int i;
2557 int sv;
2559 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2560 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2561 sv = isl_union_map_is_single_valued(umap);
2562 isl_union_map_free(umap);
2563 if (sv < 0)
2564 return -1;
2565 if (sv_tests[i].sv && !sv)
2566 isl_die(ctx, isl_error_internal,
2567 "map not detected as single valued", return -1);
2568 if (!sv_tests[i].sv && sv)
2569 isl_die(ctx, isl_error_internal,
2570 "map detected as single valued", return -1);
2573 return 0;
2576 struct {
2577 const char *str;
2578 int bijective;
2579 } bijective_tests[] = {
2580 { "[N,M]->{[i,j] -> [i]}", 0 },
2581 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
2582 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
2583 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
2584 { "[N,M]->{[i,j] -> [j,i]}", 1 },
2585 { "[N,M]->{[i,j] -> [i+j]}", 0 },
2586 { "[N,M]->{[i,j] -> []}", 0 },
2587 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
2588 { "[N,M]->{[i,j] -> [2i]}", 0 },
2589 { "[N,M]->{[i,j] -> [i,i]}", 0 },
2590 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
2591 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
2592 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
2595 static int test_bijective(struct isl_ctx *ctx)
2597 isl_map *map;
2598 int i;
2599 int bijective;
2601 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
2602 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
2603 bijective = isl_map_is_bijective(map);
2604 isl_map_free(map);
2605 if (bijective < 0)
2606 return -1;
2607 if (bijective_tests[i].bijective && !bijective)
2608 isl_die(ctx, isl_error_internal,
2609 "map not detected as bijective", return -1);
2610 if (!bijective_tests[i].bijective && bijective)
2611 isl_die(ctx, isl_error_internal,
2612 "map detected as bijective", return -1);
2615 return 0;
2618 /* Inputs for isl_pw_qpolynomial_gist tests.
2619 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2621 struct {
2622 const char *pwqp;
2623 const char *set;
2624 const char *gist;
2625 } pwqp_gist_tests[] = {
2626 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2627 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2628 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2629 "{ [i] -> -1/2 + 1/2 * i }" },
2630 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2633 static int test_pwqp(struct isl_ctx *ctx)
2635 int i;
2636 const char *str;
2637 isl_set *set;
2638 isl_pw_qpolynomial *pwqp1, *pwqp2;
2639 int equal;
2641 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2642 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2644 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2645 isl_dim_in, 1, 1);
2647 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2648 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2650 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2652 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2654 isl_pw_qpolynomial_free(pwqp1);
2656 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2657 str = pwqp_gist_tests[i].pwqp;
2658 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2659 str = pwqp_gist_tests[i].set;
2660 set = isl_set_read_from_str(ctx, str);
2661 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2662 str = pwqp_gist_tests[i].gist;
2663 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2664 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2665 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2666 isl_pw_qpolynomial_free(pwqp1);
2668 if (equal < 0)
2669 return -1;
2670 if (!equal)
2671 isl_die(ctx, isl_error_unknown,
2672 "unexpected result", return -1);
2675 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2676 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2677 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2678 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2680 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2682 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2684 isl_pw_qpolynomial_free(pwqp1);
2686 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2687 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2688 str = "{ [x] -> x }";
2689 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2691 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2693 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2695 isl_pw_qpolynomial_free(pwqp1);
2697 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2698 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2699 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2700 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2701 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2702 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2703 isl_pw_qpolynomial_free(pwqp1);
2705 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2706 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2707 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2708 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2709 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2710 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2711 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2712 isl_pw_qpolynomial_free(pwqp1);
2713 isl_pw_qpolynomial_free(pwqp2);
2714 if (equal < 0)
2715 return -1;
2716 if (!equal)
2717 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2719 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
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 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2724 isl_val_one(ctx));
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 return 0;
2736 static int test_split_periods(isl_ctx *ctx)
2738 const char *str;
2739 isl_pw_qpolynomial *pwqp;
2741 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2742 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2743 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2744 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2746 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2748 isl_pw_qpolynomial_free(pwqp);
2750 if (!pwqp)
2751 return -1;
2753 return 0;
2756 static int test_union(isl_ctx *ctx)
2758 const char *str;
2759 isl_union_set *uset1, *uset2;
2760 isl_union_map *umap1, *umap2;
2761 int equal;
2763 str = "{ [i] : 0 <= i <= 1 }";
2764 uset1 = isl_union_set_read_from_str(ctx, str);
2765 str = "{ [1] -> [0] }";
2766 umap1 = isl_union_map_read_from_str(ctx, str);
2768 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2769 equal = isl_union_map_is_equal(umap1, umap2);
2771 isl_union_map_free(umap1);
2772 isl_union_map_free(umap2);
2774 if (equal < 0)
2775 return -1;
2776 if (!equal)
2777 isl_die(ctx, isl_error_unknown, "union maps not equal",
2778 return -1);
2780 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2781 umap1 = isl_union_map_read_from_str(ctx, str);
2782 str = "{ A[i]; B[i] }";
2783 uset1 = isl_union_set_read_from_str(ctx, str);
2785 uset2 = isl_union_map_domain(umap1);
2787 equal = isl_union_set_is_equal(uset1, uset2);
2789 isl_union_set_free(uset1);
2790 isl_union_set_free(uset2);
2792 if (equal < 0)
2793 return -1;
2794 if (!equal)
2795 isl_die(ctx, isl_error_unknown, "union sets not equal",
2796 return -1);
2798 return 0;
2801 /* Check that computing a bound of a non-zero polynomial over an unbounded
2802 * domain does not produce a rational value.
2803 * In particular, check that the upper bound is infinity.
2805 static int test_bound_unbounded_domain(isl_ctx *ctx)
2807 const char *str;
2808 isl_pw_qpolynomial *pwqp;
2809 isl_pw_qpolynomial_fold *pwf, *pwf2;
2810 isl_bool equal;
2812 str = "{ [m,n] -> -m * n }";
2813 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2814 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2815 str = "{ infty }";
2816 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2817 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2818 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
2819 isl_pw_qpolynomial_fold_free(pwf);
2820 isl_pw_qpolynomial_fold_free(pwf2);
2822 if (equal < 0)
2823 return -1;
2824 if (!equal)
2825 isl_die(ctx, isl_error_unknown,
2826 "expecting infinite polynomial bound", return -1);
2828 return 0;
2831 static int test_bound(isl_ctx *ctx)
2833 const char *str;
2834 unsigned dim;
2835 isl_pw_qpolynomial *pwqp;
2836 isl_pw_qpolynomial_fold *pwf;
2838 if (test_bound_unbounded_domain(ctx) < 0)
2839 return -1;
2841 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2842 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2843 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2844 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
2845 isl_pw_qpolynomial_fold_free(pwf);
2846 if (dim != 4)
2847 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
2848 return -1);
2850 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2851 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2852 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2853 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
2854 isl_pw_qpolynomial_fold_free(pwf);
2855 if (dim != 1)
2856 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
2857 return -1);
2859 return 0;
2862 static int test_lift(isl_ctx *ctx)
2864 const char *str;
2865 isl_basic_map *bmap;
2866 isl_basic_set *bset;
2868 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2869 bset = isl_basic_set_read_from_str(ctx, str);
2870 bset = isl_basic_set_lift(bset);
2871 bmap = isl_basic_map_from_range(bset);
2872 bset = isl_basic_map_domain(bmap);
2873 isl_basic_set_free(bset);
2875 return 0;
2878 struct {
2879 const char *set1;
2880 const char *set2;
2881 int subset;
2882 } subset_tests[] = {
2883 { "{ [112, 0] }",
2884 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2885 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2886 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2887 { "{ [65] }",
2888 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2889 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2890 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2891 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2892 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2893 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2894 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2895 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2896 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2897 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2898 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2899 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2900 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2901 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
2902 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
2903 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
2904 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
2905 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
2906 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
2907 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
2908 "4e0 <= -57 + i0 + i1)) or "
2909 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
2910 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
2911 "4e0 >= -61 + i0 + i1)) or "
2912 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
2913 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
2916 static int test_subset(isl_ctx *ctx)
2918 int i;
2919 isl_set *set1, *set2;
2920 int subset;
2922 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2923 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2924 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2925 subset = isl_set_is_subset(set1, set2);
2926 isl_set_free(set1);
2927 isl_set_free(set2);
2928 if (subset < 0)
2929 return -1;
2930 if (subset != subset_tests[i].subset)
2931 isl_die(ctx, isl_error_unknown,
2932 "incorrect subset result", return -1);
2935 return 0;
2938 struct {
2939 const char *minuend;
2940 const char *subtrahend;
2941 const char *difference;
2942 } subtract_domain_tests[] = {
2943 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2944 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2945 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2948 static int test_subtract(isl_ctx *ctx)
2950 int i;
2951 isl_union_map *umap1, *umap2;
2952 isl_union_pw_multi_aff *upma1, *upma2;
2953 isl_union_set *uset;
2954 int equal;
2956 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2957 umap1 = isl_union_map_read_from_str(ctx,
2958 subtract_domain_tests[i].minuend);
2959 uset = isl_union_set_read_from_str(ctx,
2960 subtract_domain_tests[i].subtrahend);
2961 umap2 = isl_union_map_read_from_str(ctx,
2962 subtract_domain_tests[i].difference);
2963 umap1 = isl_union_map_subtract_domain(umap1, uset);
2964 equal = isl_union_map_is_equal(umap1, umap2);
2965 isl_union_map_free(umap1);
2966 isl_union_map_free(umap2);
2967 if (equal < 0)
2968 return -1;
2969 if (!equal)
2970 isl_die(ctx, isl_error_unknown,
2971 "incorrect subtract domain result", return -1);
2974 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2975 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
2976 subtract_domain_tests[i].minuend);
2977 uset = isl_union_set_read_from_str(ctx,
2978 subtract_domain_tests[i].subtrahend);
2979 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
2980 subtract_domain_tests[i].difference);
2981 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
2982 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
2983 isl_union_pw_multi_aff_free(upma1);
2984 isl_union_pw_multi_aff_free(upma2);
2985 if (equal < 0)
2986 return -1;
2987 if (!equal)
2988 isl_die(ctx, isl_error_unknown,
2989 "incorrect subtract domain result", return -1);
2992 return 0;
2995 /* Check that intersecting the empty basic set with another basic set
2996 * does not increase the number of constraints. In particular,
2997 * the empty basic set should maintain its canonical representation.
2999 static int test_intersect(isl_ctx *ctx)
3001 int n1, n2;
3002 isl_basic_set *bset1, *bset2;
3004 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3005 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3006 n1 = isl_basic_set_n_constraint(bset1);
3007 bset1 = isl_basic_set_intersect(bset1, bset2);
3008 n2 = isl_basic_set_n_constraint(bset1);
3009 isl_basic_set_free(bset1);
3010 if (!bset1)
3011 return -1;
3012 if (n1 != n2)
3013 isl_die(ctx, isl_error_unknown,
3014 "number of constraints of empty set changed",
3015 return -1);
3017 return 0;
3020 int test_factorize(isl_ctx *ctx)
3022 const char *str;
3023 isl_basic_set *bset;
3024 isl_factorizer *f;
3026 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3027 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3028 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3029 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3030 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3031 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3032 "3i5 >= -2i0 - i2 + 3i4 }";
3033 bset = isl_basic_set_read_from_str(ctx, str);
3034 f = isl_basic_set_factorizer(bset);
3035 isl_basic_set_free(bset);
3036 isl_factorizer_free(f);
3037 if (!f)
3038 isl_die(ctx, isl_error_unknown,
3039 "failed to construct factorizer", return -1);
3041 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3042 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3043 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3044 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3045 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3046 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3047 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3048 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3049 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3050 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3051 bset = isl_basic_set_read_from_str(ctx, str);
3052 f = isl_basic_set_factorizer(bset);
3053 isl_basic_set_free(bset);
3054 isl_factorizer_free(f);
3055 if (!f)
3056 isl_die(ctx, isl_error_unknown,
3057 "failed to construct factorizer", return -1);
3059 return 0;
3062 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3064 int *injective = user;
3066 *injective = isl_map_is_injective(map);
3067 isl_map_free(map);
3069 if (*injective < 0 || !*injective)
3070 return isl_stat_error;
3072 return isl_stat_ok;
3075 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3076 const char *r, const char *s, int tilable, int parallel)
3078 int i;
3079 isl_union_set *D;
3080 isl_union_map *W, *R, *S;
3081 isl_union_map *empty;
3082 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3083 isl_union_map *validity, *proximity, *coincidence;
3084 isl_union_map *schedule;
3085 isl_union_map *test;
3086 isl_union_set *delta;
3087 isl_union_set *domain;
3088 isl_set *delta_set;
3089 isl_set *slice;
3090 isl_set *origin;
3091 isl_schedule_constraints *sc;
3092 isl_schedule *sched;
3093 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3095 D = isl_union_set_read_from_str(ctx, d);
3096 W = isl_union_map_read_from_str(ctx, w);
3097 R = isl_union_map_read_from_str(ctx, r);
3098 S = isl_union_map_read_from_str(ctx, s);
3100 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3101 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3103 empty = isl_union_map_empty(isl_union_map_get_space(S));
3104 isl_union_map_compute_flow(isl_union_map_copy(R),
3105 isl_union_map_copy(W), empty,
3106 isl_union_map_copy(S),
3107 &dep_raw, NULL, NULL, NULL);
3108 isl_union_map_compute_flow(isl_union_map_copy(W),
3109 isl_union_map_copy(W),
3110 isl_union_map_copy(R),
3111 isl_union_map_copy(S),
3112 &dep_waw, &dep_war, NULL, NULL);
3114 dep = isl_union_map_union(dep_waw, dep_war);
3115 dep = isl_union_map_union(dep, dep_raw);
3116 validity = isl_union_map_copy(dep);
3117 coincidence = isl_union_map_copy(dep);
3118 proximity = isl_union_map_copy(dep);
3120 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3121 sc = isl_schedule_constraints_set_validity(sc, validity);
3122 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3123 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3124 sched = isl_schedule_constraints_compute_schedule(sc);
3125 schedule = isl_schedule_get_map(sched);
3126 isl_schedule_free(sched);
3127 isl_union_map_free(W);
3128 isl_union_map_free(R);
3129 isl_union_map_free(S);
3131 is_injection = 1;
3132 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3134 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3135 is_complete = isl_union_set_is_subset(D, domain);
3136 isl_union_set_free(D);
3137 isl_union_set_free(domain);
3139 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3140 test = isl_union_map_apply_range(test, dep);
3141 test = isl_union_map_apply_range(test, schedule);
3143 delta = isl_union_map_deltas(test);
3144 if (isl_union_set_n_set(delta) == 0) {
3145 is_tilable = 1;
3146 is_parallel = 1;
3147 is_nonneg = 1;
3148 isl_union_set_free(delta);
3149 } else {
3150 delta_set = isl_set_from_union_set(delta);
3152 slice = isl_set_universe(isl_set_get_space(delta_set));
3153 for (i = 0; i < tilable; ++i)
3154 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3155 is_tilable = isl_set_is_subset(delta_set, slice);
3156 isl_set_free(slice);
3158 slice = isl_set_universe(isl_set_get_space(delta_set));
3159 for (i = 0; i < parallel; ++i)
3160 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3161 is_parallel = isl_set_is_subset(delta_set, slice);
3162 isl_set_free(slice);
3164 origin = isl_set_universe(isl_set_get_space(delta_set));
3165 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
3166 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3168 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3169 delta_set = isl_set_lexmin(delta_set);
3171 is_nonneg = isl_set_is_equal(delta_set, origin);
3173 isl_set_free(origin);
3174 isl_set_free(delta_set);
3177 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3178 is_injection < 0 || is_complete < 0)
3179 return -1;
3180 if (!is_complete)
3181 isl_die(ctx, isl_error_unknown,
3182 "generated schedule incomplete", return -1);
3183 if (!is_injection)
3184 isl_die(ctx, isl_error_unknown,
3185 "generated schedule not injective on each statement",
3186 return -1);
3187 if (!is_nonneg)
3188 isl_die(ctx, isl_error_unknown,
3189 "negative dependences in generated schedule",
3190 return -1);
3191 if (!is_tilable)
3192 isl_die(ctx, isl_error_unknown,
3193 "generated schedule not as tilable as expected",
3194 return -1);
3195 if (!is_parallel)
3196 isl_die(ctx, isl_error_unknown,
3197 "generated schedule not as parallel as expected",
3198 return -1);
3200 return 0;
3203 /* Compute a schedule for the given instance set, validity constraints,
3204 * proximity constraints and context and return a corresponding union map
3205 * representation.
3207 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3208 const char *domain, const char *validity, const char *proximity,
3209 const char *context)
3211 isl_set *con;
3212 isl_union_set *dom;
3213 isl_union_map *dep;
3214 isl_union_map *prox;
3215 isl_schedule_constraints *sc;
3216 isl_schedule *schedule;
3217 isl_union_map *sched;
3219 con = isl_set_read_from_str(ctx, context);
3220 dom = isl_union_set_read_from_str(ctx, domain);
3221 dep = isl_union_map_read_from_str(ctx, validity);
3222 prox = isl_union_map_read_from_str(ctx, proximity);
3223 sc = isl_schedule_constraints_on_domain(dom);
3224 sc = isl_schedule_constraints_set_context(sc, con);
3225 sc = isl_schedule_constraints_set_validity(sc, dep);
3226 sc = isl_schedule_constraints_set_proximity(sc, prox);
3227 schedule = isl_schedule_constraints_compute_schedule(sc);
3228 sched = isl_schedule_get_map(schedule);
3229 isl_schedule_free(schedule);
3231 return sched;
3234 /* Compute a schedule for the given instance set, validity constraints and
3235 * proximity constraints and return a corresponding union map representation.
3237 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
3238 const char *domain, const char *validity, const char *proximity)
3240 return compute_schedule_with_context(ctx, domain, validity, proximity,
3241 "{ : }");
3244 /* Check that a schedule can be constructed on the given domain
3245 * with the given validity and proximity constraints.
3247 static int test_has_schedule(isl_ctx *ctx, const char *domain,
3248 const char *validity, const char *proximity)
3250 isl_union_map *sched;
3252 sched = compute_schedule(ctx, domain, validity, proximity);
3253 if (!sched)
3254 return -1;
3256 isl_union_map_free(sched);
3257 return 0;
3260 int test_special_schedule(isl_ctx *ctx, const char *domain,
3261 const char *validity, const char *proximity, const char *expected_sched)
3263 isl_union_map *sched1, *sched2;
3264 int equal;
3266 sched1 = compute_schedule(ctx, domain, validity, proximity);
3267 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
3269 equal = isl_union_map_is_equal(sched1, sched2);
3270 isl_union_map_free(sched1);
3271 isl_union_map_free(sched2);
3273 if (equal < 0)
3274 return -1;
3275 if (!equal)
3276 isl_die(ctx, isl_error_unknown, "unexpected schedule",
3277 return -1);
3279 return 0;
3282 /* Check that the schedule map is properly padded, even after being
3283 * reconstructed from the band forest.
3285 static int test_padded_schedule(isl_ctx *ctx)
3287 const char *str;
3288 isl_union_set *D;
3289 isl_union_map *validity, *proximity;
3290 isl_schedule_constraints *sc;
3291 isl_schedule *sched;
3292 isl_union_map *map1, *map2;
3293 isl_band_list *list;
3294 int equal;
3296 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
3297 D = isl_union_set_read_from_str(ctx, str);
3298 validity = isl_union_map_empty(isl_union_set_get_space(D));
3299 proximity = isl_union_map_copy(validity);
3300 sc = isl_schedule_constraints_on_domain(D);
3301 sc = isl_schedule_constraints_set_validity(sc, validity);
3302 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3303 sched = isl_schedule_constraints_compute_schedule(sc);
3304 map1 = isl_schedule_get_map(sched);
3305 list = isl_schedule_get_band_forest(sched);
3306 isl_band_list_free(list);
3307 map2 = isl_schedule_get_map(sched);
3308 isl_schedule_free(sched);
3309 equal = isl_union_map_is_equal(map1, map2);
3310 isl_union_map_free(map1);
3311 isl_union_map_free(map2);
3313 if (equal < 0)
3314 return -1;
3315 if (!equal)
3316 isl_die(ctx, isl_error_unknown,
3317 "reconstructed schedule map not the same as original",
3318 return -1);
3320 return 0;
3323 /* Check that conditional validity constraints are also taken into
3324 * account across bands.
3325 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
3326 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
3327 * and then check that the adjacent order constraint C[2,1]->D[2,0]
3328 * is enforced by the rest of the schedule.
3330 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
3332 const char *str;
3333 isl_union_set *domain;
3334 isl_union_map *validity, *proximity, *condition;
3335 isl_union_map *sink, *source, *dep;
3336 isl_schedule_constraints *sc;
3337 isl_schedule *schedule;
3338 isl_union_access_info *access;
3339 isl_union_flow *flow;
3340 int empty;
3342 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3343 "A[k] : k >= 1 and k <= -1 + n; "
3344 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3345 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
3346 domain = isl_union_set_read_from_str(ctx, str);
3347 sc = isl_schedule_constraints_on_domain(domain);
3348 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
3349 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3350 "D[k, i] -> C[1 + k, i] : "
3351 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3352 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
3353 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
3354 validity = isl_union_map_read_from_str(ctx, str);
3355 sc = isl_schedule_constraints_set_validity(sc, validity);
3356 str = "[n] -> { C[k, i] -> D[k, i] : "
3357 "0 <= i <= -1 + k and k <= -1 + n }";
3358 proximity = isl_union_map_read_from_str(ctx, str);
3359 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3360 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
3361 "i <= -1 + k and i >= 1 and k <= -2 + n; "
3362 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
3363 "k <= -1 + n and i >= 0 and i <= -2 + k }";
3364 condition = isl_union_map_read_from_str(ctx, str);
3365 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
3366 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3367 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
3368 "i >= 0 and i <= -1 + k and k <= -1 + n and "
3369 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
3370 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
3371 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3372 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
3373 "k <= -1 + n and i >= 0 and i <= -1 + k and "
3374 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
3375 validity = isl_union_map_read_from_str(ctx, str);
3376 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3377 validity);
3378 schedule = isl_schedule_constraints_compute_schedule(sc);
3379 str = "{ D[2,0] -> [] }";
3380 sink = isl_union_map_read_from_str(ctx, str);
3381 access = isl_union_access_info_from_sink(sink);
3382 str = "{ C[2,1] -> [] }";
3383 source = isl_union_map_read_from_str(ctx, str);
3384 access = isl_union_access_info_set_must_source(access, source);
3385 access = isl_union_access_info_set_schedule(access, schedule);
3386 flow = isl_union_access_info_compute_flow(access);
3387 dep = isl_union_flow_get_must_dependence(flow);
3388 isl_union_flow_free(flow);
3389 empty = isl_union_map_is_empty(dep);
3390 isl_union_map_free(dep);
3392 if (empty < 0)
3393 return -1;
3394 if (empty)
3395 isl_die(ctx, isl_error_unknown,
3396 "conditional validity not respected", return -1);
3398 return 0;
3401 /* Input for testing of schedule construction based on
3402 * conditional constraints.
3404 * domain is the iteration domain
3405 * flow are the flow dependences, which determine the validity and
3406 * proximity constraints
3407 * condition are the conditions on the conditional validity constraints
3408 * conditional_validity are the conditional validity constraints
3409 * outer_band_n is the expected number of members in the outer band
3411 struct {
3412 const char *domain;
3413 const char *flow;
3414 const char *condition;
3415 const char *conditional_validity;
3416 int outer_band_n;
3417 } live_range_tests[] = {
3418 /* Contrived example that illustrates that we need to keep
3419 * track of tagged condition dependences and
3420 * tagged conditional validity dependences
3421 * in isl_sched_edge separately.
3422 * In particular, the conditional validity constraints on A
3423 * cannot be satisfied,
3424 * but they can be ignored because there are no corresponding
3425 * condition constraints. However, we do have an additional
3426 * conditional validity constraint that maps to the same
3427 * dependence relation
3428 * as the condition constraint on B. If we did not make a distinction
3429 * between tagged condition and tagged conditional validity
3430 * dependences, then we
3431 * could end up treating this shared dependence as an condition
3432 * constraint on A, forcing a localization of the conditions,
3433 * which is impossible.
3435 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
3436 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
3437 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
3438 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3439 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3440 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
3443 /* TACO 2013 Fig. 7 */
3444 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3445 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3446 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3447 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
3448 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
3449 "0 <= i < n and 0 <= j < n - 1 }",
3450 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
3451 "0 <= i < n and 0 <= j < j' < n;"
3452 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
3453 "0 <= i < i' < n and 0 <= j,j' < n;"
3454 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
3455 "0 <= i,j,j' < n and j < j' }",
3458 /* TACO 2013 Fig. 7, without tags */
3459 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3460 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3461 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3462 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3463 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3464 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
3465 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
3466 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
3469 /* TACO 2013 Fig. 12 */
3470 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
3471 "S3[i,3] : 0 <= i <= 1 }",
3472 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
3473 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
3474 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
3475 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
3476 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3477 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
3478 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3479 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
3480 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
3481 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
3482 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
3487 /* Test schedule construction based on conditional constraints.
3488 * In particular, check the number of members in the outer band node
3489 * as an indication of whether tiling is possible or not.
3491 static int test_conditional_schedule_constraints(isl_ctx *ctx)
3493 int i;
3494 isl_union_set *domain;
3495 isl_union_map *condition;
3496 isl_union_map *flow;
3497 isl_union_map *validity;
3498 isl_schedule_constraints *sc;
3499 isl_schedule *schedule;
3500 isl_schedule_node *node;
3501 int n_member;
3503 if (test_special_conditional_schedule_constraints(ctx) < 0)
3504 return -1;
3506 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
3507 domain = isl_union_set_read_from_str(ctx,
3508 live_range_tests[i].domain);
3509 flow = isl_union_map_read_from_str(ctx,
3510 live_range_tests[i].flow);
3511 condition = isl_union_map_read_from_str(ctx,
3512 live_range_tests[i].condition);
3513 validity = isl_union_map_read_from_str(ctx,
3514 live_range_tests[i].conditional_validity);
3515 sc = isl_schedule_constraints_on_domain(domain);
3516 sc = isl_schedule_constraints_set_validity(sc,
3517 isl_union_map_copy(flow));
3518 sc = isl_schedule_constraints_set_proximity(sc, flow);
3519 sc = isl_schedule_constraints_set_conditional_validity(sc,
3520 condition, validity);
3521 schedule = isl_schedule_constraints_compute_schedule(sc);
3522 node = isl_schedule_get_root(schedule);
3523 while (node &&
3524 isl_schedule_node_get_type(node) != isl_schedule_node_band)
3525 node = isl_schedule_node_first_child(node);
3526 n_member = isl_schedule_node_band_n_member(node);
3527 isl_schedule_node_free(node);
3528 isl_schedule_free(schedule);
3530 if (!schedule)
3531 return -1;
3532 if (n_member != live_range_tests[i].outer_band_n)
3533 isl_die(ctx, isl_error_unknown,
3534 "unexpected number of members in outer band",
3535 return -1);
3537 return 0;
3540 /* Check that the schedule computed for the given instance set and
3541 * dependence relation strongly satisfies the dependences.
3542 * In particular, check that no instance is scheduled before
3543 * or together with an instance on which it depends.
3544 * Earlier versions of isl would produce a schedule that
3545 * only weakly satisfies the dependences.
3547 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
3549 const char *domain, *dep;
3550 isl_union_map *D, *schedule;
3551 isl_map *map, *ge;
3552 int empty;
3554 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
3555 "A[i0] : 0 <= i0 <= 1 }";
3556 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
3557 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
3558 schedule = compute_schedule(ctx, domain, dep, dep);
3559 D = isl_union_map_read_from_str(ctx, dep);
3560 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
3561 D = isl_union_map_apply_range(D, schedule);
3562 map = isl_map_from_union_map(D);
3563 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3564 map = isl_map_intersect(map, ge);
3565 empty = isl_map_is_empty(map);
3566 isl_map_free(map);
3568 if (empty < 0)
3569 return -1;
3570 if (!empty)
3571 isl_die(ctx, isl_error_unknown,
3572 "dependences not strongly satisfied", return -1);
3574 return 0;
3577 /* Compute a schedule for input where the instance set constraints
3578 * conflict with the context constraints.
3579 * Earlier versions of isl did not properly handle this situation.
3581 static int test_conflicting_context_schedule(isl_ctx *ctx)
3583 isl_union_map *schedule;
3584 const char *domain, *context;
3586 domain = "[n] -> { A[] : n >= 0 }";
3587 context = "[n] -> { : n < 0 }";
3588 schedule = compute_schedule_with_context(ctx,
3589 domain, "{}", "{}", context);
3590 isl_union_map_free(schedule);
3592 if (!schedule)
3593 return -1;
3595 return 0;
3598 /* Check that the dependence carrying step is not confused by
3599 * a bound on the coefficient size.
3600 * In particular, force the scheduler to move to a dependence carrying
3601 * step by demanding outer coincidence and bound the size of
3602 * the coefficients. Earlier versions of isl would take this
3603 * bound into account while carrying dependences, breaking
3604 * fundamental assumptions.
3606 static int test_bounded_coefficients_schedule(isl_ctx *ctx)
3608 const char *domain, *dep;
3609 isl_union_set *I;
3610 isl_union_map *D;
3611 isl_schedule_constraints *sc;
3612 isl_schedule *schedule;
3614 domain = "{ C[i0, i1] : 2 <= i0 <= 3999 and 0 <= i1 <= -1 + i0 }";
3615 dep = "{ C[i0, i1] -> C[i0, 1 + i1] : i0 <= 3999 and i1 >= 0 and "
3616 "i1 <= -2 + i0; "
3617 "C[i0, -1 + i0] -> C[1 + i0, 0] : i0 <= 3998 and i0 >= 1 }";
3618 I = isl_union_set_read_from_str(ctx, domain);
3619 D = isl_union_map_read_from_str(ctx, dep);
3620 sc = isl_schedule_constraints_on_domain(I);
3621 sc = isl_schedule_constraints_set_validity(sc, isl_union_map_copy(D));
3622 sc = isl_schedule_constraints_set_coincidence(sc, D);
3623 isl_options_set_schedule_outer_coincidence(ctx, 1);
3624 isl_options_set_schedule_max_coefficient(ctx, 20);
3625 schedule = isl_schedule_constraints_compute_schedule(sc);
3626 isl_options_set_schedule_max_coefficient(ctx, -1);
3627 isl_options_set_schedule_outer_coincidence(ctx, 0);
3628 isl_schedule_free(schedule);
3630 if (!schedule)
3631 return -1;
3633 return 0;
3636 int test_schedule(isl_ctx *ctx)
3638 const char *D, *W, *R, *V, *P, *S;
3639 int max_coincidence;
3641 /* Handle resulting schedule with zero bands. */
3642 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
3643 return -1;
3645 /* Jacobi */
3646 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
3647 W = "{ S1[t,i] -> a[t,i] }";
3648 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
3649 "S1[t,i] -> a[t-1,i+1] }";
3650 S = "{ S1[t,i] -> [t,i] }";
3651 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3652 return -1;
3654 /* Fig. 5 of CC2008 */
3655 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
3656 "j <= -1 + N }";
3657 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
3658 "j >= 2 and j <= -1 + N }";
3659 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
3660 "j >= 2 and j <= -1 + N; "
3661 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
3662 "j >= 2 and j <= -1 + N }";
3663 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3664 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3665 return -1;
3667 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
3668 W = "{ S1[i] -> a[i] }";
3669 R = "{ S2[i] -> a[i+1] }";
3670 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3671 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3672 return -1;
3674 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
3675 W = "{ S1[i] -> a[i] }";
3676 R = "{ S2[i] -> a[9-i] }";
3677 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3678 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3679 return -1;
3681 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
3682 W = "{ S1[i] -> a[i] }";
3683 R = "[N] -> { S2[i] -> a[N-1-i] }";
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]; S2[i] -> b[i] }";
3690 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
3691 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
3692 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3693 return -1;
3695 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3696 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
3697 R = "{ S2[i,j] -> a[i-1,j] }";
3698 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3699 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3700 return -1;
3702 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3703 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
3704 R = "{ S2[i,j] -> a[i,j-1] }";
3705 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3706 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3707 return -1;
3709 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
3710 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
3711 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
3712 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
3713 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
3714 "S_0[] -> [0, 0, 0] }";
3715 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
3716 return -1;
3717 ctx->opt->schedule_parametric = 0;
3718 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3719 return -1;
3720 ctx->opt->schedule_parametric = 1;
3722 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
3723 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
3724 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
3725 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
3726 "S4[i] -> a[i,N] }";
3727 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
3728 "S4[i] -> [4,i,0] }";
3729 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
3730 isl_options_set_schedule_maximize_coincidence(ctx, 0);
3731 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3732 return -1;
3733 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
3735 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
3736 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3737 "j <= N }";
3738 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3739 "j <= N; "
3740 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
3741 "j <= N }";
3742 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3743 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3744 return -1;
3746 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
3747 " S_2[t] : t >= 0 and t <= -1 + N; "
3748 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
3749 "i <= -1 + N }";
3750 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
3751 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
3752 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
3753 "i >= 0 and i <= -1 + N }";
3754 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
3755 "i >= 0 and i <= -1 + N; "
3756 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
3757 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
3758 " S_0[t] -> [0, t, 0] }";
3760 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3761 return -1;
3762 ctx->opt->schedule_parametric = 0;
3763 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3764 return -1;
3765 ctx->opt->schedule_parametric = 1;
3767 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
3768 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
3769 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
3770 return -1;
3772 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
3773 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
3774 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
3775 "j >= 0 and j <= -1 + N; "
3776 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3777 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
3778 "j >= 0 and j <= -1 + N; "
3779 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3780 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
3781 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3782 return -1;
3784 D = "{ S_0[i] : i >= 0 }";
3785 W = "{ S_0[i] -> a[i] : i >= 0 }";
3786 R = "{ S_0[i] -> a[0] : i >= 0 }";
3787 S = "{ S_0[i] -> [0, i, 0] }";
3788 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3789 return -1;
3791 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
3792 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
3793 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
3794 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
3795 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3796 return -1;
3798 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
3799 "k <= -1 + n and k >= 0 }";
3800 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
3801 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
3802 "k <= -1 + n and k >= 0; "
3803 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
3804 "k <= -1 + n and k >= 0; "
3805 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
3806 "k <= -1 + n and k >= 0 }";
3807 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
3808 ctx->opt->schedule_outer_coincidence = 1;
3809 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3810 return -1;
3811 ctx->opt->schedule_outer_coincidence = 0;
3813 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3814 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3815 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3816 "Stmt_for_body24[i0, i1, 1, 0]:"
3817 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3818 "Stmt_for_body7[i0, i1, i2]:"
3819 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3820 "i2 <= 7 }";
3822 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3823 "Stmt_for_body24[1, i1, i2, i3]:"
3824 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3825 "i2 >= 1;"
3826 "Stmt_for_body24[0, i1, i2, i3] -> "
3827 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3828 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3829 "i3 >= 0;"
3830 "Stmt_for_body24[0, i1, i2, i3] ->"
3831 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3832 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3833 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3834 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3835 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3836 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3837 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3838 "i1 <= 6 and i1 >= 0;"
3839 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3840 "Stmt_for_body7[i0, i1, i2] -> "
3841 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3842 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3843 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3844 "Stmt_for_body7[i0, i1, i2] -> "
3845 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3846 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3847 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3848 P = V;
3849 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3850 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3851 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3853 if (test_special_schedule(ctx, D, V, P, S) < 0)
3854 return -1;
3856 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3857 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3858 "j >= 1 and j <= 7;"
3859 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3860 "j >= 1 and j <= 8 }";
3861 P = "{ }";
3862 S = "{ S_0[i, j] -> [i + j, j] }";
3863 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3864 if (test_special_schedule(ctx, D, V, P, S) < 0)
3865 return -1;
3866 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3868 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3869 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3870 "j >= 0 and j <= -1 + i }";
3871 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3872 "i <= -1 + N and j >= 0;"
3873 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3874 "i <= -2 + N }";
3875 P = "{ }";
3876 S = "{ S_0[i, j] -> [i, 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 /* Test both algorithms on a case with only proximity dependences. */
3883 D = "{ S[i,j] : 0 <= i <= 10 }";
3884 V = "{ }";
3885 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3886 S = "{ S[i, j] -> [j, i] }";
3887 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3888 if (test_special_schedule(ctx, D, V, P, S) < 0)
3889 return -1;
3890 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3891 if (test_special_schedule(ctx, D, V, P, S) < 0)
3892 return -1;
3894 D = "{ A[a]; B[] }";
3895 V = "{}";
3896 P = "{ A[a] -> B[] }";
3897 if (test_has_schedule(ctx, D, V, P) < 0)
3898 return -1;
3900 if (test_padded_schedule(ctx) < 0)
3901 return -1;
3903 /* Check that check for progress is not confused by rational
3904 * solution.
3906 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3907 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3908 "i0 <= -2 + N; "
3909 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3910 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3911 P = "{}";
3912 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3913 if (test_has_schedule(ctx, D, V, P) < 0)
3914 return -1;
3915 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3917 /* Check that we allow schedule rows that are only non-trivial
3918 * on some full-dimensional domains.
3920 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
3921 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
3922 "S1[j] -> S2[1] : 0 <= j <= 1 }";
3923 P = "{}";
3924 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3925 if (test_has_schedule(ctx, D, V, P) < 0)
3926 return -1;
3927 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3929 if (test_conditional_schedule_constraints(ctx) < 0)
3930 return -1;
3932 if (test_strongly_satisfying_schedule(ctx) < 0)
3933 return -1;
3935 if (test_conflicting_context_schedule(ctx) < 0)
3936 return -1;
3938 if (test_bounded_coefficients_schedule(ctx) < 0)
3939 return -1;
3941 return 0;
3944 /* Perform scheduling tests using the whole component scheduler.
3946 static int test_schedule_whole(isl_ctx *ctx)
3948 int whole;
3949 int r;
3951 whole = isl_options_get_schedule_whole_component(ctx);
3952 isl_options_set_schedule_whole_component(ctx, 1);
3953 r = test_schedule(ctx);
3954 isl_options_set_schedule_whole_component(ctx, whole);
3956 return r;
3959 /* Perform scheduling tests using the incremental scheduler.
3961 static int test_schedule_incremental(isl_ctx *ctx)
3963 int whole;
3964 int r;
3966 whole = isl_options_get_schedule_whole_component(ctx);
3967 isl_options_set_schedule_whole_component(ctx, 0);
3968 r = test_schedule(ctx);
3969 isl_options_set_schedule_whole_component(ctx, whole);
3971 return r;
3974 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3976 isl_union_map *umap;
3977 int test;
3979 umap = isl_union_map_read_from_str(ctx, str);
3980 test = isl_union_map_plain_is_injective(umap);
3981 isl_union_map_free(umap);
3982 if (test < 0)
3983 return -1;
3984 if (test == injective)
3985 return 0;
3986 if (injective)
3987 isl_die(ctx, isl_error_unknown,
3988 "map not detected as injective", return -1);
3989 else
3990 isl_die(ctx, isl_error_unknown,
3991 "map detected as injective", return -1);
3994 int test_injective(isl_ctx *ctx)
3996 const char *str;
3998 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
3999 return -1;
4000 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4001 return -1;
4002 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4003 return -1;
4004 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4005 return -1;
4006 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4007 return -1;
4008 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4009 return -1;
4010 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4011 return -1;
4012 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4013 return -1;
4015 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4016 if (test_plain_injective(ctx, str, 1))
4017 return -1;
4018 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4019 if (test_plain_injective(ctx, str, 0))
4020 return -1;
4022 return 0;
4025 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4027 isl_aff *aff2;
4028 int equal;
4030 if (!aff)
4031 return -1;
4033 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4034 equal = isl_aff_plain_is_equal(aff, aff2);
4035 isl_aff_free(aff2);
4037 return equal;
4040 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4042 int equal;
4044 equal = aff_plain_is_equal(aff, str);
4045 if (equal < 0)
4046 return -1;
4047 if (!equal)
4048 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4049 "result not as expected", return -1);
4050 return 0;
4053 struct {
4054 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4055 __isl_take isl_aff *aff2);
4056 } aff_bin_op[] = {
4057 ['+'] = { &isl_aff_add },
4058 ['-'] = { &isl_aff_sub },
4059 ['*'] = { &isl_aff_mul },
4060 ['/'] = { &isl_aff_div },
4063 struct {
4064 const char *arg1;
4065 unsigned char op;
4066 const char *arg2;
4067 const char *res;
4068 } aff_bin_tests[] = {
4069 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
4070 "{ [i] -> [2i] }" },
4071 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
4072 "{ [i] -> [0] }" },
4073 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
4074 "{ [i] -> [2i] }" },
4075 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
4076 "{ [i] -> [2i] }" },
4077 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
4078 "{ [i] -> [i/2] }" },
4079 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
4080 "{ [i] -> [i] }" },
4081 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
4082 "{ [i] -> [NaN] }" },
4083 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
4084 "{ [i] -> [NaN] }" },
4085 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
4086 "{ [i] -> [NaN] }" },
4087 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
4088 "{ [i] -> [NaN] }" },
4089 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
4090 "{ [i] -> [NaN] }" },
4091 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
4092 "{ [i] -> [NaN] }" },
4093 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
4094 "{ [i] -> [NaN] }" },
4095 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
4096 "{ [i] -> [NaN] }" },
4097 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
4098 "{ [i] -> [NaN] }" },
4099 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
4100 "{ [i] -> [NaN] }" },
4101 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
4102 "{ [i] -> [NaN] }" },
4103 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
4104 "{ [i] -> [NaN] }" },
4107 /* Perform some basic tests of binary operations on isl_aff objects.
4109 static int test_bin_aff(isl_ctx *ctx)
4111 int i;
4112 isl_aff *aff1, *aff2, *res;
4113 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4114 __isl_take isl_aff *aff2);
4115 int ok;
4117 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
4118 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
4119 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
4120 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
4121 fn = aff_bin_op[aff_bin_tests[i].op].fn;
4122 aff1 = fn(aff1, aff2);
4123 if (isl_aff_is_nan(res))
4124 ok = isl_aff_is_nan(aff1);
4125 else
4126 ok = isl_aff_plain_is_equal(aff1, res);
4127 isl_aff_free(aff1);
4128 isl_aff_free(res);
4129 if (ok < 0)
4130 return -1;
4131 if (!ok)
4132 isl_die(ctx, isl_error_unknown,
4133 "unexpected result", return -1);
4136 return 0;
4139 struct {
4140 __isl_give isl_union_pw_multi_aff *(*fn)(
4141 __isl_take isl_union_pw_multi_aff *upma1,
4142 __isl_take isl_union_pw_multi_aff *upma2);
4143 const char *arg1;
4144 const char *arg2;
4145 const char *res;
4146 } upma_bin_tests[] = {
4147 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
4148 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
4149 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
4150 "{ B[x] -> [2] : x >= 0 }",
4151 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
4152 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
4153 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
4154 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
4155 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
4156 "D[i] -> B[2] : i >= 5 }" },
4157 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4158 "{ B[x] -> C[2] : x > 0 }",
4159 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
4160 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4161 "{ B[x] -> A[2] : x >= 0 }",
4162 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
4165 /* Perform some basic tests of binary operations on
4166 * isl_union_pw_multi_aff objects.
4168 static int test_bin_upma(isl_ctx *ctx)
4170 int i;
4171 isl_union_pw_multi_aff *upma1, *upma2, *res;
4172 int ok;
4174 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
4175 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4176 upma_bin_tests[i].arg1);
4177 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4178 upma_bin_tests[i].arg2);
4179 res = isl_union_pw_multi_aff_read_from_str(ctx,
4180 upma_bin_tests[i].res);
4181 upma1 = upma_bin_tests[i].fn(upma1, upma2);
4182 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
4183 isl_union_pw_multi_aff_free(upma1);
4184 isl_union_pw_multi_aff_free(res);
4185 if (ok < 0)
4186 return -1;
4187 if (!ok)
4188 isl_die(ctx, isl_error_unknown,
4189 "unexpected result", return -1);
4192 return 0;
4195 struct {
4196 __isl_give isl_union_pw_multi_aff *(*fn)(
4197 __isl_take isl_union_pw_multi_aff *upma1,
4198 __isl_take isl_union_pw_multi_aff *upma2);
4199 const char *arg1;
4200 const char *arg2;
4201 } upma_bin_fail_tests[] = {
4202 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4203 "{ B[x] -> C[2] : x >= 0 }" },
4206 /* Perform some basic tests of binary operations on
4207 * isl_union_pw_multi_aff objects that are expected to fail.
4209 static int test_bin_upma_fail(isl_ctx *ctx)
4211 int i, n;
4212 isl_union_pw_multi_aff *upma1, *upma2;
4213 int on_error;
4215 on_error = isl_options_get_on_error(ctx);
4216 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
4217 n = ARRAY_SIZE(upma_bin_fail_tests);
4218 for (i = 0; i < n; ++i) {
4219 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4220 upma_bin_fail_tests[i].arg1);
4221 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4222 upma_bin_fail_tests[i].arg2);
4223 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
4224 isl_union_pw_multi_aff_free(upma1);
4225 if (upma1)
4226 break;
4228 isl_options_set_on_error(ctx, on_error);
4229 if (i < n)
4230 isl_die(ctx, isl_error_unknown,
4231 "operation not expected to succeed", return -1);
4233 return 0;
4236 int test_aff(isl_ctx *ctx)
4238 const char *str;
4239 isl_set *set;
4240 isl_space *space;
4241 isl_local_space *ls;
4242 isl_aff *aff;
4243 int zero, equal;
4245 if (test_bin_aff(ctx) < 0)
4246 return -1;
4247 if (test_bin_upma(ctx) < 0)
4248 return -1;
4249 if (test_bin_upma_fail(ctx) < 0)
4250 return -1;
4252 space = isl_space_set_alloc(ctx, 0, 1);
4253 ls = isl_local_space_from_space(space);
4254 aff = isl_aff_zero_on_domain(ls);
4256 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4257 aff = isl_aff_scale_down_ui(aff, 3);
4258 aff = isl_aff_floor(aff);
4259 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4260 aff = isl_aff_scale_down_ui(aff, 2);
4261 aff = isl_aff_floor(aff);
4262 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4264 str = "{ [10] }";
4265 set = isl_set_read_from_str(ctx, str);
4266 aff = isl_aff_gist(aff, set);
4268 aff = isl_aff_add_constant_si(aff, -16);
4269 zero = isl_aff_plain_is_zero(aff);
4270 isl_aff_free(aff);
4272 if (zero < 0)
4273 return -1;
4274 if (!zero)
4275 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4277 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
4278 aff = isl_aff_scale_down_ui(aff, 64);
4279 aff = isl_aff_floor(aff);
4280 equal = aff_check_plain_equal(aff, "{ [-1] }");
4281 isl_aff_free(aff);
4282 if (equal < 0)
4283 return -1;
4285 return 0;
4288 int test_dim_max(isl_ctx *ctx)
4290 int equal;
4291 const char *str;
4292 isl_set *set1, *set2;
4293 isl_set *set;
4294 isl_map *map;
4295 isl_pw_aff *pwaff;
4297 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
4298 set = isl_set_read_from_str(ctx, str);
4299 pwaff = isl_set_dim_max(set, 0);
4300 set1 = isl_set_from_pw_aff(pwaff);
4301 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
4302 set2 = isl_set_read_from_str(ctx, str);
4303 equal = isl_set_is_equal(set1, set2);
4304 isl_set_free(set1);
4305 isl_set_free(set2);
4306 if (equal < 0)
4307 return -1;
4308 if (!equal)
4309 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4311 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
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] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
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 <= 2N or 0 <= i <= 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,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
4340 "0 <= i < N and 0 <= j < M }";
4341 map = isl_map_read_from_str(ctx, str);
4342 set = isl_map_range(map);
4344 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
4345 set1 = isl_set_from_pw_aff(pwaff);
4346 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
4347 set2 = isl_set_read_from_str(ctx, str);
4348 equal = isl_set_is_equal(set1, set2);
4349 isl_set_free(set1);
4350 isl_set_free(set2);
4352 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
4353 set1 = isl_set_from_pw_aff(pwaff);
4354 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
4355 set2 = isl_set_read_from_str(ctx, str);
4356 if (equal >= 0 && equal)
4357 equal = isl_set_is_equal(set1, set2);
4358 isl_set_free(set1);
4359 isl_set_free(set2);
4361 isl_set_free(set);
4363 if (equal < 0)
4364 return -1;
4365 if (!equal)
4366 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4368 /* Check that solutions are properly merged. */
4369 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
4370 "c <= -1 + n - 4a - 2b and c >= -2b and "
4371 "4a >= -4 + n and c >= 0 }";
4372 set = isl_set_read_from_str(ctx, str);
4373 pwaff = isl_set_dim_min(set, 2);
4374 set1 = isl_set_from_pw_aff(pwaff);
4375 str = "[n] -> { [(0)] : n >= 1 }";
4376 set2 = isl_set_read_from_str(ctx, str);
4377 equal = isl_set_is_equal(set1, set2);
4378 isl_set_free(set1);
4379 isl_set_free(set2);
4381 if (equal < 0)
4382 return -1;
4383 if (!equal)
4384 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4386 /* Check that empty solution lie in the right space. */
4387 str = "[n] -> { [t,a] : 1 = 0 }";
4388 set = isl_set_read_from_str(ctx, str);
4389 pwaff = isl_set_dim_max(set, 0);
4390 set1 = isl_set_from_pw_aff(pwaff);
4391 str = "[n] -> { [t] : 1 = 0 }";
4392 set2 = isl_set_read_from_str(ctx, str);
4393 equal = isl_set_is_equal(set1, set2);
4394 isl_set_free(set1);
4395 isl_set_free(set2);
4397 if (equal < 0)
4398 return -1;
4399 if (!equal)
4400 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4402 return 0;
4405 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
4407 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
4408 const char *str)
4410 isl_ctx *ctx;
4411 isl_pw_multi_aff *pma2;
4412 int equal;
4414 if (!pma)
4415 return -1;
4417 ctx = isl_pw_multi_aff_get_ctx(pma);
4418 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4419 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
4420 isl_pw_multi_aff_free(pma2);
4422 return equal;
4425 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
4426 * represented by "str".
4428 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
4429 const char *str)
4431 int equal;
4433 equal = pw_multi_aff_plain_is_equal(pma, str);
4434 if (equal < 0)
4435 return -1;
4436 if (!equal)
4437 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
4438 "result not as expected", return -1);
4439 return 0;
4442 /* Basic test for isl_pw_multi_aff_product.
4444 * Check that multiple pieces are properly handled.
4446 static int test_product_pma(isl_ctx *ctx)
4448 int equal;
4449 const char *str;
4450 isl_pw_multi_aff *pma1, *pma2;
4452 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
4453 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4454 str = "{ C[] -> D[] }";
4455 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4456 pma1 = isl_pw_multi_aff_product(pma1, pma2);
4457 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
4458 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
4459 equal = pw_multi_aff_check_plain_equal(pma1, str);
4460 isl_pw_multi_aff_free(pma1);
4461 if (equal < 0)
4462 return -1;
4464 return 0;
4467 int test_product(isl_ctx *ctx)
4469 const char *str;
4470 isl_set *set;
4471 isl_union_set *uset1, *uset2;
4472 int ok;
4474 str = "{ A[i] }";
4475 set = isl_set_read_from_str(ctx, str);
4476 set = isl_set_product(set, isl_set_copy(set));
4477 ok = isl_set_is_wrapping(set);
4478 isl_set_free(set);
4479 if (ok < 0)
4480 return -1;
4481 if (!ok)
4482 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4484 str = "{ [] }";
4485 uset1 = isl_union_set_read_from_str(ctx, str);
4486 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
4487 str = "{ [[] -> []] }";
4488 uset2 = isl_union_set_read_from_str(ctx, str);
4489 ok = isl_union_set_is_equal(uset1, uset2);
4490 isl_union_set_free(uset1);
4491 isl_union_set_free(uset2);
4492 if (ok < 0)
4493 return -1;
4494 if (!ok)
4495 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4497 if (test_product_pma(ctx) < 0)
4498 return -1;
4500 return 0;
4503 /* Check that two sets are not considered disjoint just because
4504 * they have a different set of (named) parameters.
4506 static int test_disjoint(isl_ctx *ctx)
4508 const char *str;
4509 isl_set *set, *set2;
4510 int disjoint;
4512 str = "[n] -> { [[]->[]] }";
4513 set = isl_set_read_from_str(ctx, str);
4514 str = "{ [[]->[]] }";
4515 set2 = isl_set_read_from_str(ctx, str);
4516 disjoint = isl_set_is_disjoint(set, set2);
4517 isl_set_free(set);
4518 isl_set_free(set2);
4519 if (disjoint < 0)
4520 return -1;
4521 if (disjoint)
4522 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4524 return 0;
4527 int test_equal(isl_ctx *ctx)
4529 const char *str;
4530 isl_set *set, *set2;
4531 int equal;
4533 str = "{ S_6[i] }";
4534 set = isl_set_read_from_str(ctx, str);
4535 str = "{ S_7[i] }";
4536 set2 = isl_set_read_from_str(ctx, str);
4537 equal = isl_set_is_equal(set, set2);
4538 isl_set_free(set);
4539 isl_set_free(set2);
4540 if (equal < 0)
4541 return -1;
4542 if (equal)
4543 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4545 return 0;
4548 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
4549 enum isl_dim_type type, unsigned pos, int fixed)
4551 int test;
4553 test = isl_map_plain_is_fixed(map, type, pos, NULL);
4554 isl_map_free(map);
4555 if (test < 0)
4556 return -1;
4557 if (test == fixed)
4558 return 0;
4559 if (fixed)
4560 isl_die(ctx, isl_error_unknown,
4561 "map not detected as fixed", return -1);
4562 else
4563 isl_die(ctx, isl_error_unknown,
4564 "map detected as fixed", return -1);
4567 int test_fixed(isl_ctx *ctx)
4569 const char *str;
4570 isl_map *map;
4572 str = "{ [i] -> [i] }";
4573 map = isl_map_read_from_str(ctx, str);
4574 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
4575 return -1;
4576 str = "{ [i] -> [1] }";
4577 map = isl_map_read_from_str(ctx, str);
4578 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4579 return -1;
4580 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
4581 map = isl_map_read_from_str(ctx, str);
4582 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4583 return -1;
4584 map = isl_map_read_from_str(ctx, str);
4585 map = isl_map_neg(map);
4586 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4587 return -1;
4589 return 0;
4592 struct isl_vertices_test_data {
4593 const char *set;
4594 int n;
4595 const char *vertex[2];
4596 } vertices_tests[] = {
4597 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
4598 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
4599 { "{ A[t, i] : t = 14 and i = 1 }",
4600 1, { "{ A[14, 1] }" } },
4603 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
4605 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
4607 struct isl_vertices_test_data *data = user;
4608 isl_ctx *ctx;
4609 isl_multi_aff *ma;
4610 isl_basic_set *bset;
4611 isl_pw_multi_aff *pma;
4612 int i;
4613 isl_bool equal;
4615 ctx = isl_vertex_get_ctx(vertex);
4616 bset = isl_vertex_get_domain(vertex);
4617 ma = isl_vertex_get_expr(vertex);
4618 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
4620 for (i = 0; i < data->n; ++i) {
4621 isl_pw_multi_aff *pma_i;
4623 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
4624 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
4625 isl_pw_multi_aff_free(pma_i);
4627 if (equal < 0 || equal)
4628 break;
4631 isl_pw_multi_aff_free(pma);
4632 isl_vertex_free(vertex);
4634 if (equal < 0)
4635 return isl_stat_error;
4637 return equal ? isl_stat_ok : isl_stat_error;
4640 int test_vertices(isl_ctx *ctx)
4642 int i;
4644 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
4645 isl_basic_set *bset;
4646 isl_vertices *vertices;
4647 int ok = 1;
4648 int n;
4650 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
4651 vertices = isl_basic_set_compute_vertices(bset);
4652 n = isl_vertices_get_n_vertices(vertices);
4653 if (vertices_tests[i].n != n)
4654 ok = 0;
4655 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
4656 &vertices_tests[i]) < 0)
4657 ok = 0;
4658 isl_vertices_free(vertices);
4659 isl_basic_set_free(bset);
4661 if (!vertices)
4662 return -1;
4663 if (!ok)
4664 isl_die(ctx, isl_error_unknown, "unexpected vertices",
4665 return -1);
4668 return 0;
4671 int test_union_pw(isl_ctx *ctx)
4673 int equal;
4674 const char *str;
4675 isl_union_set *uset;
4676 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
4678 str = "{ [x] -> x^2 }";
4679 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
4680 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
4681 uset = isl_union_pw_qpolynomial_domain(upwqp1);
4682 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
4683 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
4684 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
4685 isl_union_pw_qpolynomial_free(upwqp1);
4686 isl_union_pw_qpolynomial_free(upwqp2);
4687 if (equal < 0)
4688 return -1;
4689 if (!equal)
4690 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4692 return 0;
4695 /* Test that isl_union_pw_qpolynomial_eval picks up the function
4696 * defined over the correct domain space.
4698 static int test_eval(isl_ctx *ctx)
4700 const char *str;
4701 isl_point *pnt;
4702 isl_set *set;
4703 isl_union_pw_qpolynomial *upwqp;
4704 isl_val *v;
4705 int cmp;
4707 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
4708 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
4709 str = "{ A[6] }";
4710 set = isl_set_read_from_str(ctx, str);
4711 pnt = isl_set_sample_point(set);
4712 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
4713 cmp = isl_val_cmp_si(v, 36);
4714 isl_val_free(v);
4716 if (!v)
4717 return -1;
4718 if (cmp != 0)
4719 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
4721 return 0;
4724 int test_output(isl_ctx *ctx)
4726 char *s;
4727 const char *str;
4728 isl_pw_aff *pa;
4729 isl_printer *p;
4730 int equal;
4732 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
4733 pa = isl_pw_aff_read_from_str(ctx, str);
4735 p = isl_printer_to_str(ctx);
4736 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
4737 p = isl_printer_print_pw_aff(p, pa);
4738 s = isl_printer_get_str(p);
4739 isl_printer_free(p);
4740 isl_pw_aff_free(pa);
4741 if (!s)
4742 equal = -1;
4743 else
4744 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
4745 free(s);
4746 if (equal < 0)
4747 return -1;
4748 if (!equal)
4749 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4751 return 0;
4754 int test_sample(isl_ctx *ctx)
4756 const char *str;
4757 isl_basic_set *bset1, *bset2;
4758 int empty, subset;
4760 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
4761 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
4762 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
4763 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
4764 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
4765 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
4766 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
4767 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
4768 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
4769 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
4770 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
4771 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
4772 "d - 1073741821e and "
4773 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
4774 "3j >= 1 - a + b + 2e and "
4775 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
4776 "3i <= 4 - a + 4b - e and "
4777 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
4778 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
4779 "c <= -1 + a and 3i >= -2 - a + 3e and "
4780 "1073741822e <= 1073741823 - a + 1073741822b + c and "
4781 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
4782 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
4783 "1073741823e >= 1 + 1073741823b - d and "
4784 "3i >= 1073741823b + c - 1073741823e - f and "
4785 "3i >= 1 + 2b + e + 3g }";
4786 bset1 = isl_basic_set_read_from_str(ctx, str);
4787 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
4788 empty = isl_basic_set_is_empty(bset2);
4789 subset = isl_basic_set_is_subset(bset2, bset1);
4790 isl_basic_set_free(bset1);
4791 isl_basic_set_free(bset2);
4792 if (empty < 0 || subset < 0)
4793 return -1;
4794 if (empty)
4795 isl_die(ctx, isl_error_unknown, "point not found", return -1);
4796 if (!subset)
4797 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
4799 return 0;
4802 int test_fixed_power(isl_ctx *ctx)
4804 const char *str;
4805 isl_map *map;
4806 isl_int exp;
4807 int equal;
4809 isl_int_init(exp);
4810 str = "{ [i] -> [i + 1] }";
4811 map = isl_map_read_from_str(ctx, str);
4812 isl_int_set_si(exp, 23);
4813 map = isl_map_fixed_power(map, exp);
4814 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
4815 isl_int_clear(exp);
4816 isl_map_free(map);
4817 if (equal < 0)
4818 return -1;
4820 return 0;
4823 int test_slice(isl_ctx *ctx)
4825 const char *str;
4826 isl_map *map;
4827 int equal;
4829 str = "{ [i] -> [j] }";
4830 map = isl_map_read_from_str(ctx, str);
4831 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
4832 equal = map_check_equal(map, "{ [i] -> [i] }");
4833 isl_map_free(map);
4834 if (equal < 0)
4835 return -1;
4837 str = "{ [i] -> [j] }";
4838 map = isl_map_read_from_str(ctx, str);
4839 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
4840 equal = map_check_equal(map, "{ [i] -> [j] }");
4841 isl_map_free(map);
4842 if (equal < 0)
4843 return -1;
4845 str = "{ [i] -> [j] }";
4846 map = isl_map_read_from_str(ctx, str);
4847 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
4848 equal = map_check_equal(map, "{ [i] -> [-i] }");
4849 isl_map_free(map);
4850 if (equal < 0)
4851 return -1;
4853 str = "{ [i] -> [j] }";
4854 map = isl_map_read_from_str(ctx, str);
4855 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
4856 equal = map_check_equal(map, "{ [0] -> [j] }");
4857 isl_map_free(map);
4858 if (equal < 0)
4859 return -1;
4861 str = "{ [i] -> [j] }";
4862 map = isl_map_read_from_str(ctx, str);
4863 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
4864 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
4865 isl_map_free(map);
4866 if (equal < 0)
4867 return -1;
4869 str = "{ [i] -> [j] }";
4870 map = isl_map_read_from_str(ctx, str);
4871 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
4872 equal = map_check_equal(map, "{ [i] -> [j] : false }");
4873 isl_map_free(map);
4874 if (equal < 0)
4875 return -1;
4877 return 0;
4880 int test_eliminate(isl_ctx *ctx)
4882 const char *str;
4883 isl_map *map;
4884 int equal;
4886 str = "{ [i] -> [j] : i = 2j }";
4887 map = isl_map_read_from_str(ctx, str);
4888 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
4889 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
4890 isl_map_free(map);
4891 if (equal < 0)
4892 return -1;
4894 return 0;
4897 /* Check that isl_set_dim_residue_class detects that the values of j
4898 * in the set below are all odd and that it does not detect any spurious
4899 * strides.
4901 static int test_residue_class(isl_ctx *ctx)
4903 const char *str;
4904 isl_set *set;
4905 isl_int m, r;
4906 int res;
4908 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
4909 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
4910 set = isl_set_read_from_str(ctx, str);
4911 isl_int_init(m);
4912 isl_int_init(r);
4913 res = isl_set_dim_residue_class(set, 1, &m, &r);
4914 if (res >= 0 &&
4915 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
4916 isl_die(ctx, isl_error_unknown, "incorrect residue class",
4917 res = -1);
4918 isl_int_clear(r);
4919 isl_int_clear(m);
4920 isl_set_free(set);
4922 return res;
4925 int test_align_parameters(isl_ctx *ctx)
4927 const char *str;
4928 isl_space *space;
4929 isl_multi_aff *ma1, *ma2;
4930 int equal;
4932 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
4933 ma1 = isl_multi_aff_read_from_str(ctx, str);
4935 space = isl_space_params_alloc(ctx, 1);
4936 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
4937 ma1 = isl_multi_aff_align_params(ma1, space);
4939 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
4940 ma2 = isl_multi_aff_read_from_str(ctx, str);
4942 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4944 isl_multi_aff_free(ma1);
4945 isl_multi_aff_free(ma2);
4947 if (equal < 0)
4948 return -1;
4949 if (!equal)
4950 isl_die(ctx, isl_error_unknown,
4951 "result not as expected", return -1);
4953 return 0;
4956 static int test_list(isl_ctx *ctx)
4958 isl_id *a, *b, *c, *d, *id;
4959 isl_id_list *list;
4960 int ok;
4962 a = isl_id_alloc(ctx, "a", NULL);
4963 b = isl_id_alloc(ctx, "b", NULL);
4964 c = isl_id_alloc(ctx, "c", NULL);
4965 d = isl_id_alloc(ctx, "d", NULL);
4967 list = isl_id_list_alloc(ctx, 4);
4968 list = isl_id_list_add(list, a);
4969 list = isl_id_list_add(list, b);
4970 list = isl_id_list_add(list, c);
4971 list = isl_id_list_add(list, d);
4972 list = isl_id_list_drop(list, 1, 1);
4974 if (isl_id_list_n_id(list) != 3) {
4975 isl_id_list_free(list);
4976 isl_die(ctx, isl_error_unknown,
4977 "unexpected number of elements in list", return -1);
4980 id = isl_id_list_get_id(list, 0);
4981 ok = id == a;
4982 isl_id_free(id);
4983 id = isl_id_list_get_id(list, 1);
4984 ok = ok && id == c;
4985 isl_id_free(id);
4986 id = isl_id_list_get_id(list, 2);
4987 ok = ok && id == d;
4988 isl_id_free(id);
4990 isl_id_list_free(list);
4992 if (!ok)
4993 isl_die(ctx, isl_error_unknown,
4994 "unexpected elements in list", return -1);
4996 return 0;
4999 const char *set_conversion_tests[] = {
5000 "[N] -> { [i] : N - 1 <= 2 i <= N }",
5001 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
5002 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5003 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5004 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
5005 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
5006 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
5007 "-3 + c <= d <= 28 + c) }",
5010 /* Check that converting from isl_set to isl_pw_multi_aff and back
5011 * to isl_set produces the original isl_set.
5013 static int test_set_conversion(isl_ctx *ctx)
5015 int i;
5016 const char *str;
5017 isl_set *set1, *set2;
5018 isl_pw_multi_aff *pma;
5019 int equal;
5021 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
5022 str = set_conversion_tests[i];
5023 set1 = isl_set_read_from_str(ctx, str);
5024 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
5025 set2 = isl_set_from_pw_multi_aff(pma);
5026 equal = isl_set_is_equal(set1, set2);
5027 isl_set_free(set1);
5028 isl_set_free(set2);
5030 if (equal < 0)
5031 return -1;
5032 if (!equal)
5033 isl_die(ctx, isl_error_unknown, "bad conversion",
5034 return -1);
5037 return 0;
5040 const char *conversion_tests[] = {
5041 "{ [a, b, c, d] -> s0[a, b, e, f] : "
5042 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
5043 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
5044 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
5045 "9e <= -2 - 2a) }",
5046 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
5047 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
5048 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
5051 /* Check that converting from isl_map to isl_pw_multi_aff and back
5052 * to isl_map produces the original isl_map.
5054 static int test_map_conversion(isl_ctx *ctx)
5056 int i;
5057 isl_map *map1, *map2;
5058 isl_pw_multi_aff *pma;
5059 int equal;
5061 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
5062 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
5063 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
5064 map2 = isl_map_from_pw_multi_aff(pma);
5065 equal = isl_map_is_equal(map1, map2);
5066 isl_map_free(map1);
5067 isl_map_free(map2);
5069 if (equal < 0)
5070 return -1;
5071 if (!equal)
5072 isl_die(ctx, isl_error_unknown, "bad conversion",
5073 return -1);
5076 return 0;
5079 static int test_conversion(isl_ctx *ctx)
5081 if (test_set_conversion(ctx) < 0)
5082 return -1;
5083 if (test_map_conversion(ctx) < 0)
5084 return -1;
5085 return 0;
5088 /* Check that isl_basic_map_curry does not modify input.
5090 static int test_curry(isl_ctx *ctx)
5092 const char *str;
5093 isl_basic_map *bmap1, *bmap2;
5094 int equal;
5096 str = "{ [A[] -> B[]] -> C[] }";
5097 bmap1 = isl_basic_map_read_from_str(ctx, str);
5098 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
5099 equal = isl_basic_map_is_equal(bmap1, bmap2);
5100 isl_basic_map_free(bmap1);
5101 isl_basic_map_free(bmap2);
5103 if (equal < 0)
5104 return -1;
5105 if (equal)
5106 isl_die(ctx, isl_error_unknown,
5107 "curried map should not be equal to original",
5108 return -1);
5110 return 0;
5113 struct {
5114 const char *set;
5115 const char *ma;
5116 const char *res;
5117 } preimage_tests[] = {
5118 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
5119 "{ A[j,i] -> B[i,j] }",
5120 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
5121 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5122 "{ A[a,b] -> B[a/2,b/6] }",
5123 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
5124 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5125 "{ A[a,b] -> B[a/2,b/6] }",
5126 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
5127 "exists i,j : a = 2 i and b = 6 j }" },
5128 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
5129 "[n] -> { : 0 <= n <= 100 }" },
5130 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5131 "{ A[a] -> B[2a] }",
5132 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
5133 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5134 "{ A[a] -> B[([a/2])] }",
5135 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
5136 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
5137 "{ A[a] -> B[a,a,a/3] }",
5138 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
5139 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
5140 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
5143 static int test_preimage_basic_set(isl_ctx *ctx)
5145 int i;
5146 isl_basic_set *bset1, *bset2;
5147 isl_multi_aff *ma;
5148 int equal;
5150 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
5151 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
5152 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
5153 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
5154 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
5155 equal = isl_basic_set_is_equal(bset1, bset2);
5156 isl_basic_set_free(bset1);
5157 isl_basic_set_free(bset2);
5158 if (equal < 0)
5159 return -1;
5160 if (!equal)
5161 isl_die(ctx, isl_error_unknown, "bad preimage",
5162 return -1);
5165 return 0;
5168 struct {
5169 const char *map;
5170 const char *ma;
5171 const char *res;
5172 } preimage_domain_tests[] = {
5173 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
5174 "{ A[j,i] -> B[i,j] }",
5175 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
5176 { "{ B[i] -> C[i]; D[i] -> E[i] }",
5177 "{ A[i] -> B[i + 1] }",
5178 "{ A[i] -> C[i + 1] }" },
5179 { "{ B[i] -> C[i]; B[i] -> E[i] }",
5180 "{ A[i] -> B[i + 1] }",
5181 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
5182 { "{ B[i] -> C[([i/2])] }",
5183 "{ A[i] -> B[2i] }",
5184 "{ A[i] -> C[i] }" },
5185 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
5186 "{ A[i] -> B[([i/5]), ([i/7])] }",
5187 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
5188 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
5189 "[N] -> { A[] -> B[([N/5])] }",
5190 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
5191 { "{ B[i] -> C[i] : exists a : i = 5 a }",
5192 "{ A[i] -> B[2i] }",
5193 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
5194 { "{ B[i] -> C[i] : exists a : i = 2 a; "
5195 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
5196 "{ A[i] -> B[2i] }",
5197 "{ A[i] -> C[2i] }" },
5198 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
5199 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
5202 static int test_preimage_union_map(isl_ctx *ctx)
5204 int i;
5205 isl_union_map *umap1, *umap2;
5206 isl_multi_aff *ma;
5207 int equal;
5209 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
5210 umap1 = isl_union_map_read_from_str(ctx,
5211 preimage_domain_tests[i].map);
5212 ma = isl_multi_aff_read_from_str(ctx,
5213 preimage_domain_tests[i].ma);
5214 umap2 = isl_union_map_read_from_str(ctx,
5215 preimage_domain_tests[i].res);
5216 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
5217 equal = isl_union_map_is_equal(umap1, umap2);
5218 isl_union_map_free(umap1);
5219 isl_union_map_free(umap2);
5220 if (equal < 0)
5221 return -1;
5222 if (!equal)
5223 isl_die(ctx, isl_error_unknown, "bad preimage",
5224 return -1);
5227 return 0;
5230 static int test_preimage(isl_ctx *ctx)
5232 if (test_preimage_basic_set(ctx) < 0)
5233 return -1;
5234 if (test_preimage_union_map(ctx) < 0)
5235 return -1;
5237 return 0;
5240 struct {
5241 const char *ma1;
5242 const char *ma;
5243 const char *res;
5244 } pullback_tests[] = {
5245 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
5246 "{ A[a,b] -> C[b + 2a] }" },
5247 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
5248 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
5249 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
5250 "{ A[a] -> C[(a)/6] }" },
5251 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
5252 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
5253 "{ A[a] -> C[(2a)/3] }" },
5254 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
5255 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
5256 "{ A[i,j] -> C[i + j, i + j] }"},
5257 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
5258 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
5259 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
5260 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
5261 "{ [i, j] -> [floor((i)/3), j] }",
5262 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
5265 static int test_pullback(isl_ctx *ctx)
5267 int i;
5268 isl_multi_aff *ma1, *ma2;
5269 isl_multi_aff *ma;
5270 int equal;
5272 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
5273 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
5274 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
5275 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
5276 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
5277 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5278 isl_multi_aff_free(ma1);
5279 isl_multi_aff_free(ma2);
5280 if (equal < 0)
5281 return -1;
5282 if (!equal)
5283 isl_die(ctx, isl_error_unknown, "bad pullback",
5284 return -1);
5287 return 0;
5290 /* Check that negation is printed correctly and that equal expressions
5291 * are correctly identified.
5293 static int test_ast(isl_ctx *ctx)
5295 isl_ast_expr *expr, *expr1, *expr2, *expr3;
5296 char *str;
5297 int ok, equal;
5299 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5300 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5301 expr = isl_ast_expr_add(expr1, expr2);
5302 expr2 = isl_ast_expr_copy(expr);
5303 expr = isl_ast_expr_neg(expr);
5304 expr2 = isl_ast_expr_neg(expr2);
5305 equal = isl_ast_expr_is_equal(expr, expr2);
5306 str = isl_ast_expr_to_str(expr);
5307 ok = str ? !strcmp(str, "-(A + B)") : -1;
5308 free(str);
5309 isl_ast_expr_free(expr);
5310 isl_ast_expr_free(expr2);
5312 if (ok < 0 || equal < 0)
5313 return -1;
5314 if (!equal)
5315 isl_die(ctx, isl_error_unknown,
5316 "equal expressions not considered equal", return -1);
5317 if (!ok)
5318 isl_die(ctx, isl_error_unknown,
5319 "isl_ast_expr printed incorrectly", return -1);
5321 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5322 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5323 expr = isl_ast_expr_add(expr1, expr2);
5324 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
5325 expr = isl_ast_expr_sub(expr3, expr);
5326 str = isl_ast_expr_to_str(expr);
5327 ok = str ? !strcmp(str, "C - (A + B)") : -1;
5328 free(str);
5329 isl_ast_expr_free(expr);
5331 if (ok < 0)
5332 return -1;
5333 if (!ok)
5334 isl_die(ctx, isl_error_unknown,
5335 "isl_ast_expr printed incorrectly", return -1);
5337 return 0;
5340 /* Check that isl_ast_build_expr_from_set returns a valid expression
5341 * for an empty set. Note that isl_ast_build_expr_from_set getting
5342 * called on an empty set probably indicates a bug in the caller.
5344 static int test_ast_build(isl_ctx *ctx)
5346 isl_set *set;
5347 isl_ast_build *build;
5348 isl_ast_expr *expr;
5350 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5351 build = isl_ast_build_from_context(set);
5353 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
5354 expr = isl_ast_build_expr_from_set(build, set);
5356 isl_ast_expr_free(expr);
5357 isl_ast_build_free(build);
5359 if (!expr)
5360 return -1;
5362 return 0;
5365 /* Internal data structure for before_for and after_for callbacks.
5367 * depth is the current depth
5368 * before is the number of times before_for has been called
5369 * after is the number of times after_for has been called
5371 struct isl_test_codegen_data {
5372 int depth;
5373 int before;
5374 int after;
5377 /* This function is called before each for loop in the AST generated
5378 * from test_ast_gen1.
5380 * Increment the number of calls and the depth.
5381 * Check that the space returned by isl_ast_build_get_schedule_space
5382 * matches the target space of the schedule returned by
5383 * isl_ast_build_get_schedule.
5384 * Return an isl_id that is checked by the corresponding call
5385 * to after_for.
5387 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
5388 void *user)
5390 struct isl_test_codegen_data *data = user;
5391 isl_ctx *ctx;
5392 isl_space *space;
5393 isl_union_map *schedule;
5394 isl_union_set *uset;
5395 isl_set *set;
5396 int empty;
5397 char name[] = "d0";
5399 ctx = isl_ast_build_get_ctx(build);
5401 if (data->before >= 3)
5402 isl_die(ctx, isl_error_unknown,
5403 "unexpected number of for nodes", return NULL);
5404 if (data->depth >= 2)
5405 isl_die(ctx, isl_error_unknown,
5406 "unexpected depth", return NULL);
5408 snprintf(name, sizeof(name), "d%d", data->depth);
5409 data->before++;
5410 data->depth++;
5412 schedule = isl_ast_build_get_schedule(build);
5413 uset = isl_union_map_range(schedule);
5414 if (!uset)
5415 return NULL;
5416 if (isl_union_set_n_set(uset) != 1) {
5417 isl_union_set_free(uset);
5418 isl_die(ctx, isl_error_unknown,
5419 "expecting single range space", return NULL);
5422 space = isl_ast_build_get_schedule_space(build);
5423 set = isl_union_set_extract_set(uset, space);
5424 isl_union_set_free(uset);
5425 empty = isl_set_is_empty(set);
5426 isl_set_free(set);
5428 if (empty < 0)
5429 return NULL;
5430 if (empty)
5431 isl_die(ctx, isl_error_unknown,
5432 "spaces don't match", return NULL);
5434 return isl_id_alloc(ctx, name, NULL);
5437 /* This function is called after each for loop in the AST generated
5438 * from test_ast_gen1.
5440 * Increment the number of calls and decrement the depth.
5441 * Check that the annotation attached to the node matches
5442 * the isl_id returned by the corresponding call to before_for.
5444 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
5445 __isl_keep isl_ast_build *build, void *user)
5447 struct isl_test_codegen_data *data = user;
5448 isl_id *id;
5449 const char *name;
5450 int valid;
5452 data->after++;
5453 data->depth--;
5455 if (data->after > data->before)
5456 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5457 "mismatch in number of for nodes",
5458 return isl_ast_node_free(node));
5460 id = isl_ast_node_get_annotation(node);
5461 if (!id)
5462 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5463 "missing annotation", return isl_ast_node_free(node));
5465 name = isl_id_get_name(id);
5466 valid = name && atoi(name + 1) == data->depth;
5467 isl_id_free(id);
5469 if (!valid)
5470 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5471 "wrong annotation", return isl_ast_node_free(node));
5473 return node;
5476 /* Check that the before_each_for and after_each_for callbacks
5477 * are called for each for loop in the generated code,
5478 * that they are called in the right order and that the isl_id
5479 * returned from the before_each_for callback is attached to
5480 * the isl_ast_node passed to the corresponding after_each_for call.
5482 static int test_ast_gen1(isl_ctx *ctx)
5484 const char *str;
5485 isl_set *set;
5486 isl_union_map *schedule;
5487 isl_ast_build *build;
5488 isl_ast_node *tree;
5489 struct isl_test_codegen_data data;
5491 str = "[N] -> { : N >= 10 }";
5492 set = isl_set_read_from_str(ctx, str);
5493 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
5494 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
5495 schedule = isl_union_map_read_from_str(ctx, str);
5497 data.before = 0;
5498 data.after = 0;
5499 data.depth = 0;
5500 build = isl_ast_build_from_context(set);
5501 build = isl_ast_build_set_before_each_for(build,
5502 &before_for, &data);
5503 build = isl_ast_build_set_after_each_for(build,
5504 &after_for, &data);
5505 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5506 isl_ast_build_free(build);
5507 if (!tree)
5508 return -1;
5510 isl_ast_node_free(tree);
5512 if (data.before != 3 || data.after != 3)
5513 isl_die(ctx, isl_error_unknown,
5514 "unexpected number of for nodes", return -1);
5516 return 0;
5519 /* Check that the AST generator handles domains that are integrally disjoint
5520 * but not rationally disjoint.
5522 static int test_ast_gen2(isl_ctx *ctx)
5524 const char *str;
5525 isl_set *set;
5526 isl_union_map *schedule;
5527 isl_union_map *options;
5528 isl_ast_build *build;
5529 isl_ast_node *tree;
5531 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
5532 schedule = isl_union_map_read_from_str(ctx, str);
5533 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5534 build = isl_ast_build_from_context(set);
5536 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
5537 options = isl_union_map_read_from_str(ctx, str);
5538 build = isl_ast_build_set_options(build, options);
5539 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5540 isl_ast_build_free(build);
5541 if (!tree)
5542 return -1;
5543 isl_ast_node_free(tree);
5545 return 0;
5548 /* Increment *user on each call.
5550 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
5551 __isl_keep isl_ast_build *build, void *user)
5553 int *n = user;
5555 (*n)++;
5557 return node;
5560 /* Test that unrolling tries to minimize the number of instances.
5561 * In particular, for the schedule given below, make sure it generates
5562 * 3 nodes (rather than 101).
5564 static int test_ast_gen3(isl_ctx *ctx)
5566 const char *str;
5567 isl_set *set;
5568 isl_union_map *schedule;
5569 isl_union_map *options;
5570 isl_ast_build *build;
5571 isl_ast_node *tree;
5572 int n_domain = 0;
5574 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
5575 schedule = isl_union_map_read_from_str(ctx, str);
5576 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5578 str = "{ [i] -> unroll[0] }";
5579 options = isl_union_map_read_from_str(ctx, str);
5581 build = isl_ast_build_from_context(set);
5582 build = isl_ast_build_set_options(build, options);
5583 build = isl_ast_build_set_at_each_domain(build,
5584 &count_domains, &n_domain);
5585 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5586 isl_ast_build_free(build);
5587 if (!tree)
5588 return -1;
5590 isl_ast_node_free(tree);
5592 if (n_domain != 3)
5593 isl_die(ctx, isl_error_unknown,
5594 "unexpected number of for nodes", return -1);
5596 return 0;
5599 /* Check that if the ast_build_exploit_nested_bounds options is set,
5600 * we do not get an outer if node in the generated AST,
5601 * while we do get such an outer if node if the options is not set.
5603 static int test_ast_gen4(isl_ctx *ctx)
5605 const char *str;
5606 isl_set *set;
5607 isl_union_map *schedule;
5608 isl_ast_build *build;
5609 isl_ast_node *tree;
5610 enum isl_ast_node_type type;
5611 int enb;
5613 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
5614 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
5616 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
5618 schedule = isl_union_map_read_from_str(ctx, str);
5619 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5620 build = isl_ast_build_from_context(set);
5621 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5622 isl_ast_build_free(build);
5623 if (!tree)
5624 return -1;
5626 type = isl_ast_node_get_type(tree);
5627 isl_ast_node_free(tree);
5629 if (type == isl_ast_node_if)
5630 isl_die(ctx, isl_error_unknown,
5631 "not expecting if node", return -1);
5633 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
5635 schedule = isl_union_map_read_from_str(ctx, str);
5636 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5637 build = isl_ast_build_from_context(set);
5638 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5639 isl_ast_build_free(build);
5640 if (!tree)
5641 return -1;
5643 type = isl_ast_node_get_type(tree);
5644 isl_ast_node_free(tree);
5646 if (type != isl_ast_node_if)
5647 isl_die(ctx, isl_error_unknown,
5648 "expecting if node", return -1);
5650 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
5652 return 0;
5655 /* This function is called for each leaf in the AST generated
5656 * from test_ast_gen5.
5658 * We finalize the AST generation by extending the outer schedule
5659 * with a zero-dimensional schedule. If this results in any for loops,
5660 * then this means that we did not pass along enough information
5661 * about the outer schedule to the inner AST generation.
5663 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
5664 void *user)
5666 isl_union_map *schedule, *extra;
5667 isl_ast_node *tree;
5669 schedule = isl_ast_build_get_schedule(build);
5670 extra = isl_union_map_copy(schedule);
5671 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
5672 schedule = isl_union_map_range_product(schedule, extra);
5673 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5674 isl_ast_build_free(build);
5676 if (!tree)
5677 return NULL;
5679 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
5680 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
5681 "code should not contain any for loop",
5682 return isl_ast_node_free(tree));
5684 return tree;
5687 /* Check that we do not lose any information when going back and
5688 * forth between internal and external schedule.
5690 * In particular, we create an AST where we unroll the only
5691 * non-constant dimension in the schedule. We therefore do
5692 * not expect any for loops in the AST. However, older versions
5693 * of isl would not pass along enough information about the outer
5694 * schedule when performing an inner code generation from a create_leaf
5695 * callback, resulting in the inner code generation producing a for loop.
5697 static int test_ast_gen5(isl_ctx *ctx)
5699 const char *str;
5700 isl_set *set;
5701 isl_union_map *schedule, *options;
5702 isl_ast_build *build;
5703 isl_ast_node *tree;
5705 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
5706 schedule = isl_union_map_read_from_str(ctx, str);
5708 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
5709 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
5710 options = isl_union_map_read_from_str(ctx, str);
5712 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5713 build = isl_ast_build_from_context(set);
5714 build = isl_ast_build_set_options(build, options);
5715 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
5716 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5717 isl_ast_build_free(build);
5718 isl_ast_node_free(tree);
5719 if (!tree)
5720 return -1;
5722 return 0;
5725 static int test_ast_gen(isl_ctx *ctx)
5727 if (test_ast_gen1(ctx) < 0)
5728 return -1;
5729 if (test_ast_gen2(ctx) < 0)
5730 return -1;
5731 if (test_ast_gen3(ctx) < 0)
5732 return -1;
5733 if (test_ast_gen4(ctx) < 0)
5734 return -1;
5735 if (test_ast_gen5(ctx) < 0)
5736 return -1;
5737 return 0;
5740 /* Check if dropping output dimensions from an isl_pw_multi_aff
5741 * works properly.
5743 static int test_pw_multi_aff(isl_ctx *ctx)
5745 const char *str;
5746 isl_pw_multi_aff *pma1, *pma2;
5747 int equal;
5749 str = "{ [i,j] -> [i+j, 4i-j] }";
5750 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
5751 str = "{ [i,j] -> [4i-j] }";
5752 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5754 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
5756 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
5758 isl_pw_multi_aff_free(pma1);
5759 isl_pw_multi_aff_free(pma2);
5760 if (equal < 0)
5761 return -1;
5762 if (!equal)
5763 isl_die(ctx, isl_error_unknown,
5764 "expressions not equal", return -1);
5766 return 0;
5769 /* Check that we can properly parse multi piecewise affine expressions
5770 * where the piecewise affine expressions have different domains.
5772 static int test_multi_pw_aff(isl_ctx *ctx)
5774 const char *str;
5775 isl_set *dom, *dom2;
5776 isl_multi_pw_aff *mpa1, *mpa2;
5777 isl_pw_aff *pa;
5778 int equal;
5779 int equal_domain;
5781 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
5782 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
5783 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
5784 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
5785 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
5786 str = "{ [i] -> [(i : i > 0), 2i] }";
5787 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
5789 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
5791 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
5792 dom = isl_pw_aff_domain(pa);
5793 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
5794 dom2 = isl_pw_aff_domain(pa);
5795 equal_domain = isl_set_is_equal(dom, dom2);
5797 isl_set_free(dom);
5798 isl_set_free(dom2);
5799 isl_multi_pw_aff_free(mpa1);
5800 isl_multi_pw_aff_free(mpa2);
5802 if (equal < 0)
5803 return -1;
5804 if (!equal)
5805 isl_die(ctx, isl_error_unknown,
5806 "expressions not equal", return -1);
5808 if (equal_domain < 0)
5809 return -1;
5810 if (equal_domain)
5811 isl_die(ctx, isl_error_unknown,
5812 "domains unexpectedly equal", return -1);
5814 return 0;
5817 /* This is a regression test for a bug where isl_basic_map_simplify
5818 * would end up in an infinite loop. In particular, we construct
5819 * an empty basic set that is not obviously empty.
5820 * isl_basic_set_is_empty marks the basic set as empty.
5821 * After projecting out i3, the variable can be dropped completely,
5822 * but isl_basic_map_simplify refrains from doing so if the basic set
5823 * is empty and would end up in an infinite loop if it didn't test
5824 * explicitly for empty basic maps in the outer loop.
5826 static int test_simplify_1(isl_ctx *ctx)
5828 const char *str;
5829 isl_basic_set *bset;
5830 int empty;
5832 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
5833 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
5834 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
5835 "i3 >= i2 }";
5836 bset = isl_basic_set_read_from_str(ctx, str);
5837 empty = isl_basic_set_is_empty(bset);
5838 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
5839 isl_basic_set_free(bset);
5840 if (!bset)
5841 return -1;
5842 if (!empty)
5843 isl_die(ctx, isl_error_unknown,
5844 "basic set should be empty", return -1);
5846 return 0;
5849 /* Check that the equality in the set description below
5850 * is simplified away.
5852 static int test_simplify_2(isl_ctx *ctx)
5854 const char *str;
5855 isl_basic_set *bset;
5856 isl_bool universe;
5858 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
5859 bset = isl_basic_set_read_from_str(ctx, str);
5860 universe = isl_basic_set_plain_is_universe(bset);
5861 isl_basic_set_free(bset);
5863 if (universe < 0)
5864 return -1;
5865 if (!universe)
5866 isl_die(ctx, isl_error_unknown,
5867 "equality not simplified away", return -1);
5868 return 0;
5871 /* Some simplification tests.
5873 static int test_simplify(isl_ctx *ctx)
5875 if (test_simplify_1(ctx) < 0)
5876 return -1;
5877 if (test_simplify_2(ctx) < 0)
5878 return -1;
5879 return 0;
5882 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
5883 * with gbr context would fail to disable the use of the shifted tableau
5884 * when transferring equalities for the input to the context, resulting
5885 * in invalid sample values.
5887 static int test_partial_lexmin(isl_ctx *ctx)
5889 const char *str;
5890 isl_basic_set *bset;
5891 isl_basic_map *bmap;
5892 isl_map *map;
5894 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
5895 bmap = isl_basic_map_read_from_str(ctx, str);
5896 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
5897 bset = isl_basic_set_read_from_str(ctx, str);
5898 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
5899 isl_map_free(map);
5901 if (!map)
5902 return -1;
5904 return 0;
5907 /* Check that the variable compression performed on the existentially
5908 * quantified variables inside isl_basic_set_compute_divs is not confused
5909 * by the implicit equalities among the parameters.
5911 static int test_compute_divs(isl_ctx *ctx)
5913 const char *str;
5914 isl_basic_set *bset;
5915 isl_set *set;
5917 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
5918 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
5919 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
5920 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
5921 bset = isl_basic_set_read_from_str(ctx, str);
5922 set = isl_basic_set_compute_divs(bset);
5923 isl_set_free(set);
5924 if (!set)
5925 return -1;
5927 return 0;
5930 /* Check that the reaching domain elements and the prefix schedule
5931 * at a leaf node are the same before and after grouping.
5933 static int test_schedule_tree_group_1(isl_ctx *ctx)
5935 int equal;
5936 const char *str;
5937 isl_id *id;
5938 isl_union_set *uset;
5939 isl_multi_union_pw_aff *mupa;
5940 isl_union_pw_multi_aff *upma1, *upma2;
5941 isl_union_set *domain1, *domain2;
5942 isl_union_map *umap1, *umap2;
5943 isl_schedule_node *node;
5945 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
5946 uset = isl_union_set_read_from_str(ctx, str);
5947 node = isl_schedule_node_from_domain(uset);
5948 node = isl_schedule_node_child(node, 0);
5949 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
5950 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5951 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5952 node = isl_schedule_node_child(node, 0);
5953 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
5954 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5955 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5956 node = isl_schedule_node_child(node, 0);
5957 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
5958 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
5959 domain1 = isl_schedule_node_get_domain(node);
5960 id = isl_id_alloc(ctx, "group", NULL);
5961 node = isl_schedule_node_parent(node);
5962 node = isl_schedule_node_group(node, id);
5963 node = isl_schedule_node_child(node, 0);
5964 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
5965 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
5966 domain2 = isl_schedule_node_get_domain(node);
5967 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
5968 if (equal >= 0 && equal)
5969 equal = isl_union_set_is_equal(domain1, domain2);
5970 if (equal >= 0 && equal)
5971 equal = isl_union_map_is_equal(umap1, umap2);
5972 isl_union_map_free(umap1);
5973 isl_union_map_free(umap2);
5974 isl_union_set_free(domain1);
5975 isl_union_set_free(domain2);
5976 isl_union_pw_multi_aff_free(upma1);
5977 isl_union_pw_multi_aff_free(upma2);
5978 isl_schedule_node_free(node);
5980 if (equal < 0)
5981 return -1;
5982 if (!equal)
5983 isl_die(ctx, isl_error_unknown,
5984 "expressions not equal", return -1);
5986 return 0;
5989 /* Check that we can have nested groupings and that the union map
5990 * schedule representation is the same before and after the grouping.
5991 * Note that after the grouping, the union map representation contains
5992 * the domain constraints from the ranges of the expansion nodes,
5993 * while they are missing from the union map representation of
5994 * the tree without expansion nodes.
5996 * Also check that the global expansion is as expected.
5998 static int test_schedule_tree_group_2(isl_ctx *ctx)
6000 int equal, equal_expansion;
6001 const char *str;
6002 isl_id *id;
6003 isl_union_set *uset;
6004 isl_union_map *umap1, *umap2;
6005 isl_union_map *expansion1, *expansion2;
6006 isl_union_set_list *filters;
6007 isl_multi_union_pw_aff *mupa;
6008 isl_schedule *schedule;
6009 isl_schedule_node *node;
6011 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
6012 "S3[i,j] : 0 <= i,j < 10 }";
6013 uset = isl_union_set_read_from_str(ctx, str);
6014 node = isl_schedule_node_from_domain(uset);
6015 node = isl_schedule_node_child(node, 0);
6016 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
6017 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6018 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6019 node = isl_schedule_node_child(node, 0);
6020 str = "{ S1[i,j] }";
6021 uset = isl_union_set_read_from_str(ctx, str);
6022 filters = isl_union_set_list_from_union_set(uset);
6023 str = "{ S2[i,j]; S3[i,j] }";
6024 uset = isl_union_set_read_from_str(ctx, str);
6025 filters = isl_union_set_list_add(filters, uset);
6026 node = isl_schedule_node_insert_sequence(node, filters);
6027 node = isl_schedule_node_child(node, 1);
6028 node = isl_schedule_node_child(node, 0);
6029 str = "{ S2[i,j] }";
6030 uset = isl_union_set_read_from_str(ctx, str);
6031 filters = isl_union_set_list_from_union_set(uset);
6032 str = "{ S3[i,j] }";
6033 uset = isl_union_set_read_from_str(ctx, str);
6034 filters = isl_union_set_list_add(filters, uset);
6035 node = isl_schedule_node_insert_sequence(node, filters);
6037 schedule = isl_schedule_node_get_schedule(node);
6038 umap1 = isl_schedule_get_map(schedule);
6039 uset = isl_schedule_get_domain(schedule);
6040 umap1 = isl_union_map_intersect_domain(umap1, uset);
6041 isl_schedule_free(schedule);
6043 node = isl_schedule_node_parent(node);
6044 node = isl_schedule_node_parent(node);
6045 id = isl_id_alloc(ctx, "group1", NULL);
6046 node = isl_schedule_node_group(node, id);
6047 node = isl_schedule_node_child(node, 1);
6048 node = isl_schedule_node_child(node, 0);
6049 id = isl_id_alloc(ctx, "group2", NULL);
6050 node = isl_schedule_node_group(node, id);
6052 schedule = isl_schedule_node_get_schedule(node);
6053 umap2 = isl_schedule_get_map(schedule);
6054 isl_schedule_free(schedule);
6056 node = isl_schedule_node_root(node);
6057 node = isl_schedule_node_child(node, 0);
6058 expansion1 = isl_schedule_node_get_subtree_expansion(node);
6059 isl_schedule_node_free(node);
6061 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
6062 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
6063 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
6065 expansion2 = isl_union_map_read_from_str(ctx, str);
6067 equal = isl_union_map_is_equal(umap1, umap2);
6068 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
6070 isl_union_map_free(umap1);
6071 isl_union_map_free(umap2);
6072 isl_union_map_free(expansion1);
6073 isl_union_map_free(expansion2);
6075 if (equal < 0 || equal_expansion < 0)
6076 return -1;
6077 if (!equal)
6078 isl_die(ctx, isl_error_unknown,
6079 "expressions not equal", return -1);
6080 if (!equal_expansion)
6081 isl_die(ctx, isl_error_unknown,
6082 "unexpected expansion", return -1);
6084 return 0;
6087 /* Some tests for the isl_schedule_node_group function.
6089 static int test_schedule_tree_group(isl_ctx *ctx)
6091 if (test_schedule_tree_group_1(ctx) < 0)
6092 return -1;
6093 if (test_schedule_tree_group_2(ctx) < 0)
6094 return -1;
6095 return 0;
6098 struct {
6099 const char *set;
6100 const char *dual;
6101 } coef_tests[] = {
6102 { "{ rat: [i] : 0 <= i <= 10 }",
6103 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
6104 { "{ rat: [i] : FALSE }",
6105 "{ rat: coefficients[[cst] -> [a]] }" },
6106 { "{ rat: [i] : }",
6107 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
6110 struct {
6111 const char *set;
6112 const char *dual;
6113 } sol_tests[] = {
6114 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
6115 "{ rat: [i] : 0 <= i <= 10 }" },
6116 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
6117 "{ rat: [i] }" },
6118 { "{ rat: coefficients[[cst] -> [a]] }",
6119 "{ rat: [i] : FALSE }" },
6122 /* Test the basic functionality of isl_basic_set_coefficients and
6123 * isl_basic_set_solutions.
6125 static int test_dual(isl_ctx *ctx)
6127 int i;
6129 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
6130 int equal;
6131 isl_basic_set *bset1, *bset2;
6133 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
6134 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
6135 bset1 = isl_basic_set_coefficients(bset1);
6136 equal = isl_basic_set_is_equal(bset1, bset2);
6137 isl_basic_set_free(bset1);
6138 isl_basic_set_free(bset2);
6139 if (equal < 0)
6140 return -1;
6141 if (!equal)
6142 isl_die(ctx, isl_error_unknown,
6143 "incorrect dual", return -1);
6146 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
6147 int equal;
6148 isl_basic_set *bset1, *bset2;
6150 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
6151 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
6152 bset1 = isl_basic_set_solutions(bset1);
6153 equal = isl_basic_set_is_equal(bset1, bset2);
6154 isl_basic_set_free(bset1);
6155 isl_basic_set_free(bset2);
6156 if (equal < 0)
6157 return -1;
6158 if (!equal)
6159 isl_die(ctx, isl_error_unknown,
6160 "incorrect dual", return -1);
6163 return 0;
6166 struct {
6167 int scale_tile;
6168 int shift_point;
6169 const char *domain;
6170 const char *schedule;
6171 const char *sizes;
6172 const char *tile;
6173 const char *point;
6174 } tile_tests[] = {
6175 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6176 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6177 "{ [32,32] }",
6178 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6179 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6181 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6182 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6183 "{ [32,32] }",
6184 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6185 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6187 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6188 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6189 "{ [32,32] }",
6190 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6191 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6193 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6194 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6195 "{ [32,32] }",
6196 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6197 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6201 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
6202 * tile the band and then check if the tile and point bands have the
6203 * expected partial schedule.
6205 static int test_tile(isl_ctx *ctx)
6207 int i;
6208 int scale;
6209 int shift;
6211 scale = isl_options_get_tile_scale_tile_loops(ctx);
6212 shift = isl_options_get_tile_shift_point_loops(ctx);
6214 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
6215 int opt;
6216 int equal;
6217 const char *str;
6218 isl_union_set *domain;
6219 isl_multi_union_pw_aff *mupa, *mupa2;
6220 isl_schedule_node *node;
6221 isl_multi_val *sizes;
6223 opt = tile_tests[i].scale_tile;
6224 isl_options_set_tile_scale_tile_loops(ctx, opt);
6225 opt = tile_tests[i].shift_point;
6226 isl_options_set_tile_shift_point_loops(ctx, opt);
6228 str = tile_tests[i].domain;
6229 domain = isl_union_set_read_from_str(ctx, str);
6230 node = isl_schedule_node_from_domain(domain);
6231 node = isl_schedule_node_child(node, 0);
6232 str = tile_tests[i].schedule;
6233 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6234 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6235 str = tile_tests[i].sizes;
6236 sizes = isl_multi_val_read_from_str(ctx, str);
6237 node = isl_schedule_node_band_tile(node, sizes);
6239 str = tile_tests[i].tile;
6240 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6241 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6242 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
6243 isl_multi_union_pw_aff_free(mupa);
6244 isl_multi_union_pw_aff_free(mupa2);
6246 node = isl_schedule_node_child(node, 0);
6248 str = tile_tests[i].point;
6249 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6250 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6251 if (equal >= 0 && equal)
6252 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
6253 mupa2);
6254 isl_multi_union_pw_aff_free(mupa);
6255 isl_multi_union_pw_aff_free(mupa2);
6257 isl_schedule_node_free(node);
6259 if (equal < 0)
6260 return -1;
6261 if (!equal)
6262 isl_die(ctx, isl_error_unknown,
6263 "unexpected result", return -1);
6266 isl_options_set_tile_scale_tile_loops(ctx, scale);
6267 isl_options_set_tile_shift_point_loops(ctx, shift);
6269 return 0;
6272 /* Check that the domain hash of a space is equal to the hash
6273 * of the domain of the space.
6275 static int test_domain_hash(isl_ctx *ctx)
6277 isl_map *map;
6278 isl_space *space;
6279 uint32_t hash1, hash2;
6281 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
6282 space = isl_map_get_space(map);
6283 isl_map_free(map);
6284 hash1 = isl_space_get_domain_hash(space);
6285 space = isl_space_domain(space);
6286 hash2 = isl_space_get_hash(space);
6287 isl_space_free(space);
6289 if (!space)
6290 return -1;
6291 if (hash1 != hash2)
6292 isl_die(ctx, isl_error_unknown,
6293 "domain hash not equal to hash of domain", return -1);
6295 return 0;
6298 /* Check that a universe basic set that is not obviously equal to the universe
6299 * is still recognized as being equal to the universe.
6301 static int test_universe(isl_ctx *ctx)
6303 const char *s;
6304 isl_basic_set *bset;
6305 isl_bool is_univ;
6307 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
6308 bset = isl_basic_set_read_from_str(ctx, s);
6309 is_univ = isl_basic_set_is_universe(bset);
6310 isl_basic_set_free(bset);
6312 if (is_univ < 0)
6313 return -1;
6314 if (!is_univ)
6315 isl_die(ctx, isl_error_unknown,
6316 "not recognized as universe set", return -1);
6318 return 0;
6321 struct {
6322 const char *name;
6323 int (*fn)(isl_ctx *ctx);
6324 } tests [] = {
6325 { "universe", &test_universe },
6326 { "domain hash", &test_domain_hash },
6327 { "dual", &test_dual },
6328 { "dependence analysis", &test_flow },
6329 { "val", &test_val },
6330 { "compute divs", &test_compute_divs },
6331 { "partial lexmin", &test_partial_lexmin },
6332 { "simplify", &test_simplify },
6333 { "curry", &test_curry },
6334 { "piecewise multi affine expressions", &test_pw_multi_aff },
6335 { "multi piecewise affine expressions", &test_multi_pw_aff },
6336 { "conversion", &test_conversion },
6337 { "list", &test_list },
6338 { "align parameters", &test_align_parameters },
6339 { "preimage", &test_preimage },
6340 { "pullback", &test_pullback },
6341 { "AST", &test_ast },
6342 { "AST build", &test_ast_build },
6343 { "AST generation", &test_ast_gen },
6344 { "eliminate", &test_eliminate },
6345 { "residue class", &test_residue_class },
6346 { "div", &test_div },
6347 { "slice", &test_slice },
6348 { "fixed power", &test_fixed_power },
6349 { "sample", &test_sample },
6350 { "output", &test_output },
6351 { "vertices", &test_vertices },
6352 { "fixed", &test_fixed },
6353 { "equal", &test_equal },
6354 { "disjoint", &test_disjoint },
6355 { "product", &test_product },
6356 { "dim_max", &test_dim_max },
6357 { "affine", &test_aff },
6358 { "injective", &test_injective },
6359 { "schedule (whole component)", &test_schedule_whole },
6360 { "schedule (incremental)", &test_schedule_incremental },
6361 { "schedule tree grouping", &test_schedule_tree_group },
6362 { "tile", &test_tile },
6363 { "union_pw", &test_union_pw },
6364 { "eval", &test_eval },
6365 { "parse", &test_parse },
6366 { "single-valued", &test_sv },
6367 { "affine hull", &test_affine_hull },
6368 { "simple_hull", &test_simple_hull },
6369 { "coalesce", &test_coalesce },
6370 { "factorize", &test_factorize },
6371 { "subset", &test_subset },
6372 { "subtract", &test_subtract },
6373 { "intersect", &test_intersect },
6374 { "lexmin", &test_lexmin },
6375 { "min", &test_min },
6376 { "gist", &test_gist },
6377 { "piecewise quasi-polynomials", &test_pwqp },
6378 { "lift", &test_lift },
6379 { "bound", &test_bound },
6380 { "union", &test_union },
6381 { "split periods", &test_split_periods },
6382 { "lexicographic order", &test_lex },
6383 { "bijectivity", &test_bijective },
6384 { "dataflow analysis", &test_dep },
6385 { "reading", &test_read },
6386 { "bounded", &test_bounded },
6387 { "construction", &test_construction },
6388 { "dimension manipulation", &test_dim },
6389 { "map application", &test_application },
6390 { "convex hull", &test_convex_hull },
6391 { "transitive closure", &test_closure },
6394 int main(int argc, char **argv)
6396 int i;
6397 struct isl_ctx *ctx;
6398 struct isl_options *options;
6400 srcdir = getenv("srcdir");
6401 assert(srcdir);
6403 options = isl_options_new_with_defaults();
6404 assert(options);
6405 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
6407 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
6408 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
6409 printf("%s\n", tests[i].name);
6410 if (tests[i].fn(ctx) < 0)
6411 goto error;
6413 isl_ctx_free(ctx);
6414 return 0;
6415 error:
6416 isl_ctx_free(ctx);
6417 return -1;