isl_schedule_node_gist: drop identity expansions
[isl.git] / isl_test.c
blob616686511496f14518fec19106061c83f0b8e997
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 }" },
1386 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1387 "[n] -> { [x] : x mod 32 = 0 }",
1388 "[n] -> { [x = n] }" },
1389 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1390 "{ [x] : x mod 2 = 0 }" },
1391 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1392 "{ [x] : x mod 128 = 0 }" },
1393 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1394 "{ [x] : x mod 3200 = 0 }" },
1395 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1396 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1397 "{ [a, b, c = a] }" },
1398 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1399 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1400 "{ [a, b, c = a] : a mod 3 = 0 }" },
1403 /* Check that isl_set_gist behaves as expected.
1405 * For the test cases in gist_tests, besides checking that the result
1406 * is as expected, also check that applying the gist operation does
1407 * not modify the input set (an earlier version of isl would do that) and
1408 * that the test case is consistent, i.e., that the gist has the same
1409 * intersection with the context as the input set.
1411 static int test_gist(struct isl_ctx *ctx)
1413 int i;
1414 const char *str;
1415 isl_basic_set *bset1, *bset2;
1416 isl_map *map1, *map2;
1417 int equal;
1419 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1420 int equal_input, equal_intersection;
1421 isl_set *set1, *set2, *copy, *context;
1423 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1424 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1425 copy = isl_set_copy(set1);
1426 set1 = isl_set_gist(set1, isl_set_copy(context));
1427 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1428 equal = isl_set_is_equal(set1, set2);
1429 isl_set_free(set1);
1430 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1431 equal_input = isl_set_is_equal(set1, copy);
1432 isl_set_free(copy);
1433 set1 = isl_set_intersect(set1, isl_set_copy(context));
1434 set2 = isl_set_intersect(set2, context);
1435 equal_intersection = isl_set_is_equal(set1, set2);
1436 isl_set_free(set2);
1437 isl_set_free(set1);
1438 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1439 return -1;
1440 if (!equal)
1441 isl_die(ctx, isl_error_unknown,
1442 "incorrect gist result", return -1);
1443 if (!equal_input)
1444 isl_die(ctx, isl_error_unknown,
1445 "gist modified input", return -1);
1446 if (!equal_input)
1447 isl_die(ctx, isl_error_unknown,
1448 "inconsistent gist test case", return -1);
1451 test_gist_case(ctx, "gist1");
1453 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1454 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1455 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1456 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1457 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1458 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1459 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1460 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1461 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1462 bset1 = isl_basic_set_read_from_str(ctx, str);
1463 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1464 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1465 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1466 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1467 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1468 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1469 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1470 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1471 bset2 = isl_basic_set_read_from_str(ctx, str);
1472 bset1 = isl_basic_set_gist(bset1, bset2);
1473 assert(bset1 && bset1->n_div == 0);
1474 isl_basic_set_free(bset1);
1476 /* Check that the integer divisions of the second disjunct
1477 * do not spread to the first disjunct.
1479 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1480 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1481 "(exists (e0 = [(-1 + t1)/16], "
1482 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1483 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1484 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1485 "o0 <= 4294967295 and t1 <= -1)) }";
1486 map1 = isl_map_read_from_str(ctx, str);
1487 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1488 map2 = isl_map_read_from_str(ctx, str);
1489 map1 = isl_map_gist(map1, map2);
1490 if (!map1)
1491 return -1;
1492 if (map1->n != 1)
1493 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1494 isl_map_free(map1); return -1);
1495 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1496 isl_die(ctx, isl_error_unknown, "expecting single div",
1497 isl_map_free(map1); return -1);
1498 isl_map_free(map1);
1500 if (test_plain_gist(ctx) < 0)
1501 return -1;
1503 return 0;
1506 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1508 isl_set *set, *set2;
1509 int equal;
1510 int one;
1512 set = isl_set_read_from_str(ctx, str);
1513 set = isl_set_coalesce(set);
1514 set2 = isl_set_read_from_str(ctx, str);
1515 equal = isl_set_is_equal(set, set2);
1516 one = set && set->n == 1;
1517 isl_set_free(set);
1518 isl_set_free(set2);
1520 if (equal < 0)
1521 return -1;
1522 if (!equal)
1523 isl_die(ctx, isl_error_unknown,
1524 "coalesced set not equal to input", return -1);
1525 if (check_one && !one)
1526 isl_die(ctx, isl_error_unknown,
1527 "coalesced set should not be a union", return -1);
1529 return 0;
1532 /* Inputs for coalescing tests with unbounded wrapping.
1533 * "str" is a string representation of the input set.
1534 * "single_disjunct" is set if we expect the result to consist of
1535 * a single disjunct.
1537 struct {
1538 int single_disjunct;
1539 const char *str;
1540 } coalesce_unbounded_tests[] = {
1541 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1542 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1543 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1544 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1545 "-10 <= y <= 0}" },
1546 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1547 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1548 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1549 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1552 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1553 * option turned off.
1555 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1557 int i;
1558 int r = 0;
1559 int bounded;
1561 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1562 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1564 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1565 const char *str = coalesce_unbounded_tests[i].str;
1566 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1567 if (test_coalesce_set(ctx, str, check_one) >= 0)
1568 continue;
1569 r = -1;
1570 break;
1573 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1575 return r;
1578 /* Inputs for coalescing tests.
1579 * "str" is a string representation of the input set.
1580 * "single_disjunct" is set if we expect the result to consist of
1581 * a single disjunct.
1583 struct {
1584 int single_disjunct;
1585 const char *str;
1586 } coalesce_tests[] = {
1587 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1588 "y >= x & x >= 2 & 5 >= y }" },
1589 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1590 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1591 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1592 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1593 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1594 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1595 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1596 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1597 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1598 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1599 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1600 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1601 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1602 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1603 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1604 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1605 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1606 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1607 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1608 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1609 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1610 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1611 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1612 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1613 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1614 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1615 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1616 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1617 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1618 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1619 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1620 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1621 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1622 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1623 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1624 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1625 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1626 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1627 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1628 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1629 "[o0, o1, o2, o3, o4, o5, o6]] : "
1630 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1631 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1632 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1633 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1634 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1635 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1636 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1637 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1638 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1639 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1640 "o6 >= i3 + i6 - o3 and M >= 0 and "
1641 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1642 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1643 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1644 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1645 "(o0 = 0 and M >= 2 and N >= 3) or "
1646 "(M = 0 and o0 = 0 and N >= 3) }" },
1647 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1648 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1649 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1650 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1651 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1652 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1653 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1654 "(y = 3 and x = 1) }" },
1655 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1656 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1657 "i1 <= M and i3 <= M and i4 <= M) or "
1658 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1659 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1660 "i4 <= -1 + M) }" },
1661 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1662 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1663 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1664 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1665 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1666 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1667 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1668 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1669 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1670 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1671 { 0, "{ [a, b] : exists e : 2e = a and "
1672 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1673 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1674 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1675 "j >= 1 and j' <= i + j - i' and i >= 1; "
1676 "[1, 1, 1, 1] }" },
1677 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1678 "[i,j] : exists a : j = 3a }" },
1679 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1680 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1681 "a >= 3) or "
1682 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1683 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1684 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1685 "c <= 6 + 8a and a >= 3; "
1686 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1687 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1688 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1689 "[x,0] : 3 <= x <= 4 }" },
1690 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1691 "[x,0] : 4 <= x <= 5 }" },
1692 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1693 "[x,0] : 3 <= x <= 5 }" },
1694 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1695 "[x,0] : 3 <= x <= 4 }" },
1696 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1697 "i1 <= 0; "
1698 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1699 { 1, "{ [0,0]; [1,1] }" },
1700 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1701 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1702 "ii <= k;"
1703 "[k, 0, k] : k <= 6 and k >= 1 }" },
1704 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1705 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1706 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1707 { 1, "[n] -> { [1] : n >= 0;"
1708 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1709 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1710 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1711 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1712 "2e0 <= x and 2e0 <= n);"
1713 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1714 "n >= 0) }" },
1715 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1716 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1717 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1718 "t1 = 1 }" },
1719 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1720 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1721 "[0, 0] }" },
1722 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1723 "t1 >= 13 and t1 <= 16);"
1724 "[t1] : t1 <= 15 and t1 >= 12 }" },
1725 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
1726 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
1727 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
1728 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
1729 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
1730 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
1731 "i <= 4j + 2 }" },
1732 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
1733 "(exists (e0 : 3e0 = -2 + c0)) }" },
1734 { 0, "[n, b0, t0] -> "
1735 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1736 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1737 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1738 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1739 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
1740 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
1741 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
1742 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
1743 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1744 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1745 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1746 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
1747 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
1748 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
1749 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
1750 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
1751 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
1752 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
1753 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
1754 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
1755 { 0, "{ [i0, i1, i2] : "
1756 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
1757 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
1758 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
1759 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
1760 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
1761 "32e0 <= 31 + i0)) or "
1762 "i0 >= 0 }" },
1763 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
1764 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
1765 "2*floor((c)/2) = c and 0 <= a <= 192;"
1766 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
1770 /* A specialized coalescing test case that would result
1771 * in a segmentation fault or a failed assertion in earlier versions of isl.
1773 static int test_coalesce_special(struct isl_ctx *ctx)
1775 const char *str;
1776 isl_map *map1, *map2;
1778 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1779 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
1780 "(y = 201 and o1 <= 239 and o1 >= 212) or "
1781 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
1782 "o1 <= 239 and o1 >= 212)) or "
1783 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
1784 "o1 <= 241 and o1 >= 240));"
1785 "[S_L220_OUT[] -> T7[]] -> "
1786 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
1787 "(y = 2 and o1 <= 241 and o1 >= 212) or "
1788 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
1789 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
1790 map1 = isl_map_read_from_str(ctx, str);
1791 map1 = isl_map_align_divs(map1);
1792 map1 = isl_map_coalesce(map1);
1793 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1794 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
1795 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
1796 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
1797 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
1798 map2 = isl_map_read_from_str(ctx, str);
1799 map2 = isl_map_union(map2, map1);
1800 map2 = isl_map_align_divs(map2);
1801 map2 = isl_map_coalesce(map2);
1802 isl_map_free(map2);
1803 if (!map2)
1804 return -1;
1806 return 0;
1809 /* Test the functionality of isl_set_coalesce.
1810 * That is, check that the output is always equal to the input
1811 * and in some cases that the result consists of a single disjunct.
1813 static int test_coalesce(struct isl_ctx *ctx)
1815 int i;
1817 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1818 const char *str = coalesce_tests[i].str;
1819 int check_one = coalesce_tests[i].single_disjunct;
1820 if (test_coalesce_set(ctx, str, check_one) < 0)
1821 return -1;
1824 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1825 return -1;
1826 if (test_coalesce_special(ctx) < 0)
1827 return -1;
1829 return 0;
1832 /* Construct a representation of the graph on the right of Figure 1
1833 * in "Computing the Transitive Closure of a Union of
1834 * Affine Integer Tuple Relations".
1836 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
1838 isl_set *dom;
1839 isl_map *up, *right;
1841 dom = isl_set_read_from_str(ctx,
1842 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1843 "2 x - 3 y + 3 >= 0 }");
1844 right = isl_map_read_from_str(ctx,
1845 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1846 up = isl_map_read_from_str(ctx,
1847 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1848 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1849 right = isl_map_intersect_range(right, isl_set_copy(dom));
1850 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1851 up = isl_map_intersect_range(up, dom);
1852 return isl_map_union(up, right);
1855 /* Construct a representation of the power of the graph
1856 * on the right of Figure 1 in "Computing the Transitive Closure of
1857 * a Union of Affine Integer Tuple Relations".
1859 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
1861 return isl_map_read_from_str(ctx,
1862 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
1863 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
1864 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
1867 /* Construct a representation of the transitive closure of the graph
1868 * on the right of Figure 1 in "Computing the Transitive Closure of
1869 * a Union of Affine Integer Tuple Relations".
1871 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
1873 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
1876 static int test_closure(isl_ctx *ctx)
1878 const char *str;
1879 isl_map *map, *map2;
1880 int exact, equal;
1882 /* COCOA example 1 */
1883 map = isl_map_read_from_str(ctx,
1884 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1885 "1 <= i and i < n and 1 <= j and j < n or "
1886 "i2 = i + 1 and j2 = j - 1 and "
1887 "1 <= i and i < n and 2 <= j and j <= n }");
1888 map = isl_map_power(map, &exact);
1889 assert(exact);
1890 isl_map_free(map);
1892 /* COCOA example 1 */
1893 map = isl_map_read_from_str(ctx,
1894 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1895 "1 <= i and i < n and 1 <= j and j < n or "
1896 "i2 = i + 1 and j2 = j - 1 and "
1897 "1 <= i and i < n and 2 <= 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 and 1 <= j and j <= n and "
1903 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1904 "i2 = i + k1 + k2 and j2 = j + k1 - 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(map2);
1908 isl_map_free(map);
1910 map = isl_map_read_from_str(ctx,
1911 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1912 " 0 <= y and y <= n }");
1913 map = isl_map_transitive_closure(map, &exact);
1914 map2 = isl_map_read_from_str(ctx,
1915 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1916 " 0 <= y and y <= n }");
1917 assert(isl_map_is_equal(map, map2));
1918 isl_map_free(map2);
1919 isl_map_free(map);
1921 /* COCOA example 2 */
1922 map = isl_map_read_from_str(ctx,
1923 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1924 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1925 "i2 = i + 2 and j2 = j - 2 and "
1926 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1927 map = isl_map_transitive_closure(map, &exact);
1928 assert(exact);
1929 map2 = isl_map_read_from_str(ctx,
1930 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1931 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1932 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1933 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1934 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1935 assert(isl_map_is_equal(map, map2));
1936 isl_map_free(map);
1937 isl_map_free(map2);
1939 /* COCOA Fig.2 left */
1940 map = isl_map_read_from_str(ctx,
1941 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1942 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1943 "j <= n or "
1944 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1945 "j <= 2 i - 3 and j <= n - 2 or "
1946 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1947 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1948 map = isl_map_transitive_closure(map, &exact);
1949 assert(exact);
1950 isl_map_free(map);
1952 /* COCOA Fig.2 right */
1953 map = isl_map_read_from_str(ctx,
1954 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1955 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1956 "j <= n or "
1957 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1958 "j <= 2 i - 4 and j <= n - 3 or "
1959 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1960 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1961 map = isl_map_power(map, &exact);
1962 assert(exact);
1963 isl_map_free(map);
1965 /* COCOA Fig.2 right */
1966 map = isl_map_read_from_str(ctx,
1967 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1968 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1969 "j <= n or "
1970 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1971 "j <= 2 i - 4 and j <= n - 3 or "
1972 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1973 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1974 map = isl_map_transitive_closure(map, &exact);
1975 assert(exact);
1976 map2 = isl_map_read_from_str(ctx,
1977 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1978 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1979 "j <= n and 3 + i + 2 j <= 3 n and "
1980 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1981 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1982 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1983 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1984 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1985 assert(isl_map_is_equal(map, map2));
1986 isl_map_free(map2);
1987 isl_map_free(map);
1989 map = cocoa_fig_1_right_graph(ctx);
1990 map = isl_map_transitive_closure(map, &exact);
1991 assert(exact);
1992 map2 = cocoa_fig_1_right_tc(ctx);
1993 assert(isl_map_is_equal(map, map2));
1994 isl_map_free(map2);
1995 isl_map_free(map);
1997 map = cocoa_fig_1_right_graph(ctx);
1998 map = isl_map_power(map, &exact);
1999 map2 = cocoa_fig_1_right_power(ctx);
2000 equal = isl_map_is_equal(map, map2);
2001 isl_map_free(map2);
2002 isl_map_free(map);
2003 if (equal < 0)
2004 return -1;
2005 if (!exact)
2006 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2007 if (!equal)
2008 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2010 /* COCOA Theorem 1 counter example */
2011 map = isl_map_read_from_str(ctx,
2012 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2013 "i2 = 1 and j2 = j or "
2014 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2015 map = isl_map_transitive_closure(map, &exact);
2016 assert(exact);
2017 isl_map_free(map);
2019 map = isl_map_read_from_str(ctx,
2020 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2021 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2022 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2023 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2024 map = isl_map_transitive_closure(map, &exact);
2025 assert(exact);
2026 isl_map_free(map);
2028 /* Kelly et al 1996, fig 12 */
2029 map = isl_map_read_from_str(ctx,
2030 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2031 "1 <= i,j,j+1 <= n or "
2032 "j = n and j2 = 1 and i2 = i + 1 and "
2033 "1 <= i,i+1 <= n }");
2034 map = isl_map_transitive_closure(map, &exact);
2035 assert(exact);
2036 map2 = isl_map_read_from_str(ctx,
2037 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2038 "1 <= i <= n and i = i2 or "
2039 "1 <= i < i2 <= n and 1 <= j <= n and "
2040 "1 <= j2 <= n }");
2041 assert(isl_map_is_equal(map, map2));
2042 isl_map_free(map2);
2043 isl_map_free(map);
2045 /* Omega's closure4 */
2046 map = isl_map_read_from_str(ctx,
2047 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2048 "1 <= x,y <= 10 or "
2049 "x2 = x + 1 and y2 = y and "
2050 "1 <= x <= 20 && 5 <= y <= 15 }");
2051 map = isl_map_transitive_closure(map, &exact);
2052 assert(exact);
2053 isl_map_free(map);
2055 map = isl_map_read_from_str(ctx,
2056 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2057 map = isl_map_transitive_closure(map, &exact);
2058 assert(!exact);
2059 map2 = isl_map_read_from_str(ctx,
2060 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2061 assert(isl_map_is_equal(map, map2));
2062 isl_map_free(map);
2063 isl_map_free(map2);
2065 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2066 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2067 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2068 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2069 map = isl_map_read_from_str(ctx, str);
2070 map = isl_map_transitive_closure(map, &exact);
2071 assert(exact);
2072 map2 = isl_map_read_from_str(ctx, str);
2073 assert(isl_map_is_equal(map, map2));
2074 isl_map_free(map);
2075 isl_map_free(map2);
2077 str = "{[0] -> [1]; [2] -> [3]}";
2078 map = isl_map_read_from_str(ctx, str);
2079 map = isl_map_transitive_closure(map, &exact);
2080 assert(exact);
2081 map2 = isl_map_read_from_str(ctx, str);
2082 assert(isl_map_is_equal(map, map2));
2083 isl_map_free(map);
2084 isl_map_free(map2);
2086 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2087 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2088 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2089 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2090 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2091 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2092 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2093 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2094 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2095 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2096 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2097 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2098 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2099 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2100 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2101 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2102 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2103 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2104 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2105 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2106 map = isl_map_read_from_str(ctx, str);
2107 map = isl_map_transitive_closure(map, NULL);
2108 assert(map);
2109 isl_map_free(map);
2111 return 0;
2114 static int test_lex(struct isl_ctx *ctx)
2116 isl_space *dim;
2117 isl_map *map;
2118 int empty;
2120 dim = isl_space_set_alloc(ctx, 0, 0);
2121 map = isl_map_lex_le(dim);
2122 empty = isl_map_is_empty(map);
2123 isl_map_free(map);
2125 if (empty < 0)
2126 return -1;
2127 if (empty)
2128 isl_die(ctx, isl_error_unknown,
2129 "expecting non-empty result", return -1);
2131 return 0;
2134 static int test_lexmin(struct isl_ctx *ctx)
2136 int equal;
2137 const char *str;
2138 isl_basic_map *bmap;
2139 isl_map *map, *map2;
2140 isl_set *set;
2141 isl_set *set2;
2142 isl_pw_multi_aff *pma;
2144 str = "[p0, p1] -> { [] -> [] : "
2145 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2146 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2147 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2148 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2149 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2150 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2151 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2152 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2153 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2154 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2155 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2156 map = isl_map_read_from_str(ctx, str);
2157 map = isl_map_lexmin(map);
2158 isl_map_free(map);
2160 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2161 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2162 set = isl_set_read_from_str(ctx, str);
2163 set = isl_set_lexmax(set);
2164 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2165 set2 = isl_set_read_from_str(ctx, str);
2166 set = isl_set_intersect(set, set2);
2167 assert(!isl_set_is_empty(set));
2168 isl_set_free(set);
2170 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
2171 map = isl_map_read_from_str(ctx, str);
2172 map = isl_map_lexmin(map);
2173 str = "{ [x] -> [5] : 6 <= x <= 8; "
2174 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
2175 map2 = isl_map_read_from_str(ctx, str);
2176 assert(isl_map_is_equal(map, map2));
2177 isl_map_free(map);
2178 isl_map_free(map2);
2180 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
2181 map = isl_map_read_from_str(ctx, str);
2182 map2 = isl_map_copy(map);
2183 map = isl_map_lexmin(map);
2184 assert(isl_map_is_equal(map, map2));
2185 isl_map_free(map);
2186 isl_map_free(map2);
2188 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
2189 map = isl_map_read_from_str(ctx, str);
2190 map = isl_map_lexmin(map);
2191 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
2192 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2193 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2194 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
2195 map2 = isl_map_read_from_str(ctx, str);
2196 assert(isl_map_is_equal(map, map2));
2197 isl_map_free(map);
2198 isl_map_free(map2);
2200 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2201 " 8i' <= i and 8i' >= -7 + i }";
2202 bmap = isl_basic_map_read_from_str(ctx, str);
2203 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2204 map2 = isl_map_from_pw_multi_aff(pma);
2205 map = isl_map_from_basic_map(bmap);
2206 assert(isl_map_is_equal(map, map2));
2207 isl_map_free(map);
2208 isl_map_free(map2);
2210 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
2211 map = isl_map_read_from_str(ctx, str);
2212 map = isl_map_lexmin(map);
2213 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
2214 map2 = isl_map_read_from_str(ctx, str);
2215 assert(isl_map_is_equal(map, map2));
2216 isl_map_free(map);
2217 isl_map_free(map2);
2219 /* Check that empty pieces are properly combined. */
2220 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2221 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2222 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
2223 map = isl_map_read_from_str(ctx, str);
2224 map = isl_map_lexmin(map);
2225 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2226 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2227 "x >= 4 }";
2228 map2 = isl_map_read_from_str(ctx, str);
2229 assert(isl_map_is_equal(map, map2));
2230 isl_map_free(map);
2231 isl_map_free(map2);
2233 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2234 " 8i' <= i and 8i' >= -7 + i }";
2235 set = isl_set_read_from_str(ctx, str);
2236 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2237 set2 = isl_set_from_pw_multi_aff(pma);
2238 equal = isl_set_is_equal(set, set2);
2239 isl_set_free(set);
2240 isl_set_free(set2);
2241 if (equal < 0)
2242 return -1;
2243 if (!equal)
2244 isl_die(ctx, isl_error_unknown,
2245 "unexpected difference between set and "
2246 "piecewise affine expression", return -1);
2248 return 0;
2251 struct {
2252 const char *set;
2253 const char *obj;
2254 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2255 __isl_keep isl_aff *obj);
2256 const char *res;
2257 } opt_tests[] = {
2258 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2259 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2260 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2261 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2262 &isl_set_max_val, "30" },
2266 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2267 * In particular, check the results on non-convex inputs.
2269 static int test_min(struct isl_ctx *ctx)
2271 int i;
2272 isl_set *set;
2273 isl_aff *obj;
2274 isl_val *val, *res;
2275 isl_bool ok;
2277 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2278 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2279 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2280 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2281 val = opt_tests[i].fn(set, obj);
2282 ok = isl_val_eq(res, val);
2283 isl_val_free(res);
2284 isl_val_free(val);
2285 isl_aff_free(obj);
2286 isl_set_free(set);
2288 if (ok < 0)
2289 return -1;
2290 if (!ok)
2291 isl_die(ctx, isl_error_unknown,
2292 "unexpected optimum", return -1);
2295 return 0;
2298 struct must_may {
2299 isl_map *must;
2300 isl_map *may;
2303 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2304 void *dep_user, void *user)
2306 struct must_may *mm = (struct must_may *)user;
2308 if (must)
2309 mm->must = isl_map_union(mm->must, dep);
2310 else
2311 mm->may = isl_map_union(mm->may, dep);
2313 return isl_stat_ok;
2316 static int common_space(void *first, void *second)
2318 int depth = *(int *)first;
2319 return 2 * depth;
2322 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2324 isl_map *map2;
2325 int equal;
2327 if (!map)
2328 return -1;
2330 map2 = isl_map_read_from_str(map->ctx, str);
2331 equal = isl_map_is_equal(map, map2);
2332 isl_map_free(map2);
2334 return equal;
2337 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2339 int equal;
2341 equal = map_is_equal(map, str);
2342 if (equal < 0)
2343 return -1;
2344 if (!equal)
2345 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2346 "result not as expected", return -1);
2347 return 0;
2350 static int test_dep(struct isl_ctx *ctx)
2352 const char *str;
2353 isl_space *dim;
2354 isl_map *map;
2355 isl_access_info *ai;
2356 isl_flow *flow;
2357 int depth;
2358 struct must_may mm;
2360 depth = 3;
2362 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2363 map = isl_map_read_from_str(ctx, str);
2364 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2366 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2367 map = isl_map_read_from_str(ctx, str);
2368 ai = isl_access_info_add_source(ai, map, 1, &depth);
2370 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2371 map = isl_map_read_from_str(ctx, str);
2372 ai = isl_access_info_add_source(ai, map, 1, &depth);
2374 flow = isl_access_info_compute_flow(ai);
2375 dim = isl_space_alloc(ctx, 0, 3, 3);
2376 mm.must = isl_map_empty(isl_space_copy(dim));
2377 mm.may = isl_map_empty(dim);
2379 isl_flow_foreach(flow, collect_must_may, &mm);
2381 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2382 " [1,10,0] -> [2,5,0] }";
2383 assert(map_is_equal(mm.must, str));
2384 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
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, 1, &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 <= 4) or (6 <= i <= 10) }";
2412 assert(map_is_equal(mm.must, str));
2413 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2414 assert(map_is_equal(mm.may, str));
2416 isl_map_free(mm.must);
2417 isl_map_free(mm.may);
2418 isl_flow_free(flow);
2421 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2422 map = isl_map_read_from_str(ctx, str);
2423 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2425 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2426 map = isl_map_read_from_str(ctx, str);
2427 ai = isl_access_info_add_source(ai, map, 0, &depth);
2429 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2430 map = isl_map_read_from_str(ctx, str);
2431 ai = isl_access_info_add_source(ai, map, 0, &depth);
2433 flow = isl_access_info_compute_flow(ai);
2434 dim = isl_space_alloc(ctx, 0, 3, 3);
2435 mm.must = isl_map_empty(isl_space_copy(dim));
2436 mm.may = isl_map_empty(dim);
2438 isl_flow_foreach(flow, collect_must_may, &mm);
2440 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2441 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2442 assert(map_is_equal(mm.may, str));
2443 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2444 assert(map_is_equal(mm.must, str));
2446 isl_map_free(mm.must);
2447 isl_map_free(mm.may);
2448 isl_flow_free(flow);
2451 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2452 map = isl_map_read_from_str(ctx, str);
2453 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2455 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2456 map = isl_map_read_from_str(ctx, str);
2457 ai = isl_access_info_add_source(ai, map, 0, &depth);
2459 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2460 map = isl_map_read_from_str(ctx, str);
2461 ai = isl_access_info_add_source(ai, map, 0, &depth);
2463 flow = isl_access_info_compute_flow(ai);
2464 dim = isl_space_alloc(ctx, 0, 3, 3);
2465 mm.must = isl_map_empty(isl_space_copy(dim));
2466 mm.may = isl_map_empty(dim);
2468 isl_flow_foreach(flow, collect_must_may, &mm);
2470 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2471 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2472 assert(map_is_equal(mm.may, str));
2473 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2474 assert(map_is_equal(mm.must, str));
2476 isl_map_free(mm.must);
2477 isl_map_free(mm.may);
2478 isl_flow_free(flow);
2481 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2482 map = isl_map_read_from_str(ctx, str);
2483 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2485 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2486 map = isl_map_read_from_str(ctx, str);
2487 ai = isl_access_info_add_source(ai, map, 0, &depth);
2489 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2490 map = isl_map_read_from_str(ctx, str);
2491 ai = isl_access_info_add_source(ai, map, 0, &depth);
2493 flow = isl_access_info_compute_flow(ai);
2494 dim = isl_space_alloc(ctx, 0, 3, 3);
2495 mm.must = isl_map_empty(isl_space_copy(dim));
2496 mm.may = isl_map_empty(dim);
2498 isl_flow_foreach(flow, collect_must_may, &mm);
2500 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2501 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2502 assert(map_is_equal(mm.may, str));
2503 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2504 assert(map_is_equal(mm.must, str));
2506 isl_map_free(mm.must);
2507 isl_map_free(mm.may);
2508 isl_flow_free(flow);
2511 depth = 5;
2513 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2514 map = isl_map_read_from_str(ctx, str);
2515 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2517 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2518 map = isl_map_read_from_str(ctx, str);
2519 ai = isl_access_info_add_source(ai, map, 1, &depth);
2521 flow = isl_access_info_compute_flow(ai);
2522 dim = isl_space_alloc(ctx, 0, 5, 5);
2523 mm.must = isl_map_empty(isl_space_copy(dim));
2524 mm.may = isl_map_empty(dim);
2526 isl_flow_foreach(flow, collect_must_may, &mm);
2528 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2529 assert(map_is_equal(mm.must, str));
2530 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2531 assert(map_is_equal(mm.may, str));
2533 isl_map_free(mm.must);
2534 isl_map_free(mm.may);
2535 isl_flow_free(flow);
2537 return 0;
2540 /* Check that the dependence analysis proceeds without errors.
2541 * Earlier versions of isl would break down during the analysis
2542 * due to the use of the wrong spaces.
2544 static int test_flow(isl_ctx *ctx)
2546 const char *str;
2547 isl_union_map *access, *schedule;
2548 isl_union_map *must_dep, *may_dep;
2549 int r;
2551 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2552 access = isl_union_map_read_from_str(ctx, str);
2553 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2554 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2555 "S2[] -> [1,0,0,0]; "
2556 "S3[] -> [-1,0,0,0] }";
2557 schedule = isl_union_map_read_from_str(ctx, str);
2558 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2559 isl_union_map_copy(access), schedule,
2560 &must_dep, &may_dep, NULL, NULL);
2561 isl_union_map_free(may_dep);
2562 isl_union_map_free(must_dep);
2564 return r;
2567 struct {
2568 const char *map;
2569 int sv;
2570 } sv_tests[] = {
2571 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2572 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2573 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2574 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2575 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2576 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2577 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2578 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2579 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2582 int test_sv(isl_ctx *ctx)
2584 isl_union_map *umap;
2585 int i;
2586 int sv;
2588 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2589 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2590 sv = isl_union_map_is_single_valued(umap);
2591 isl_union_map_free(umap);
2592 if (sv < 0)
2593 return -1;
2594 if (sv_tests[i].sv && !sv)
2595 isl_die(ctx, isl_error_internal,
2596 "map not detected as single valued", return -1);
2597 if (!sv_tests[i].sv && sv)
2598 isl_die(ctx, isl_error_internal,
2599 "map detected as single valued", return -1);
2602 return 0;
2605 struct {
2606 const char *str;
2607 int bijective;
2608 } bijective_tests[] = {
2609 { "[N,M]->{[i,j] -> [i]}", 0 },
2610 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
2611 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
2612 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
2613 { "[N,M]->{[i,j] -> [j,i]}", 1 },
2614 { "[N,M]->{[i,j] -> [i+j]}", 0 },
2615 { "[N,M]->{[i,j] -> []}", 0 },
2616 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
2617 { "[N,M]->{[i,j] -> [2i]}", 0 },
2618 { "[N,M]->{[i,j] -> [i,i]}", 0 },
2619 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
2620 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
2621 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
2624 static int test_bijective(struct isl_ctx *ctx)
2626 isl_map *map;
2627 int i;
2628 int bijective;
2630 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
2631 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
2632 bijective = isl_map_is_bijective(map);
2633 isl_map_free(map);
2634 if (bijective < 0)
2635 return -1;
2636 if (bijective_tests[i].bijective && !bijective)
2637 isl_die(ctx, isl_error_internal,
2638 "map not detected as bijective", return -1);
2639 if (!bijective_tests[i].bijective && bijective)
2640 isl_die(ctx, isl_error_internal,
2641 "map detected as bijective", return -1);
2644 return 0;
2647 /* Inputs for isl_pw_qpolynomial_gist tests.
2648 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2650 struct {
2651 const char *pwqp;
2652 const char *set;
2653 const char *gist;
2654 } pwqp_gist_tests[] = {
2655 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2656 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2657 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2658 "{ [i] -> -1/2 + 1/2 * i }" },
2659 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2662 static int test_pwqp(struct isl_ctx *ctx)
2664 int i;
2665 const char *str;
2666 isl_set *set;
2667 isl_pw_qpolynomial *pwqp1, *pwqp2;
2668 int equal;
2670 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2671 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2673 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2674 isl_dim_in, 1, 1);
2676 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2677 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2679 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2681 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2683 isl_pw_qpolynomial_free(pwqp1);
2685 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2686 str = pwqp_gist_tests[i].pwqp;
2687 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2688 str = pwqp_gist_tests[i].set;
2689 set = isl_set_read_from_str(ctx, str);
2690 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2691 str = pwqp_gist_tests[i].gist;
2692 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2693 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2694 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2695 isl_pw_qpolynomial_free(pwqp1);
2697 if (equal < 0)
2698 return -1;
2699 if (!equal)
2700 isl_die(ctx, isl_error_unknown,
2701 "unexpected result", return -1);
2704 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2705 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2706 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2707 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2709 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2711 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2713 isl_pw_qpolynomial_free(pwqp1);
2715 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2716 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2717 str = "{ [x] -> x }";
2718 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2720 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2722 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2724 isl_pw_qpolynomial_free(pwqp1);
2726 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2727 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2728 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2729 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2730 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2731 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2732 isl_pw_qpolynomial_free(pwqp1);
2734 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2735 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2736 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2737 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2738 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2739 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2740 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2741 isl_pw_qpolynomial_free(pwqp1);
2742 isl_pw_qpolynomial_free(pwqp2);
2743 if (equal < 0)
2744 return -1;
2745 if (!equal)
2746 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2748 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2749 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2750 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2751 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2752 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2753 isl_val_one(ctx));
2754 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2755 isl_pw_qpolynomial_free(pwqp1);
2756 isl_pw_qpolynomial_free(pwqp2);
2757 if (equal < 0)
2758 return -1;
2759 if (!equal)
2760 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2762 return 0;
2765 static int test_split_periods(isl_ctx *ctx)
2767 const char *str;
2768 isl_pw_qpolynomial *pwqp;
2770 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2771 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2772 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2773 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2775 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2777 isl_pw_qpolynomial_free(pwqp);
2779 if (!pwqp)
2780 return -1;
2782 return 0;
2785 static int test_union(isl_ctx *ctx)
2787 const char *str;
2788 isl_union_set *uset1, *uset2;
2789 isl_union_map *umap1, *umap2;
2790 int equal;
2792 str = "{ [i] : 0 <= i <= 1 }";
2793 uset1 = isl_union_set_read_from_str(ctx, str);
2794 str = "{ [1] -> [0] }";
2795 umap1 = isl_union_map_read_from_str(ctx, str);
2797 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2798 equal = isl_union_map_is_equal(umap1, umap2);
2800 isl_union_map_free(umap1);
2801 isl_union_map_free(umap2);
2803 if (equal < 0)
2804 return -1;
2805 if (!equal)
2806 isl_die(ctx, isl_error_unknown, "union maps not equal",
2807 return -1);
2809 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2810 umap1 = isl_union_map_read_from_str(ctx, str);
2811 str = "{ A[i]; B[i] }";
2812 uset1 = isl_union_set_read_from_str(ctx, str);
2814 uset2 = isl_union_map_domain(umap1);
2816 equal = isl_union_set_is_equal(uset1, uset2);
2818 isl_union_set_free(uset1);
2819 isl_union_set_free(uset2);
2821 if (equal < 0)
2822 return -1;
2823 if (!equal)
2824 isl_die(ctx, isl_error_unknown, "union sets not equal",
2825 return -1);
2827 return 0;
2830 /* Check that computing a bound of a non-zero polynomial over an unbounded
2831 * domain does not produce a rational value.
2832 * In particular, check that the upper bound is infinity.
2834 static int test_bound_unbounded_domain(isl_ctx *ctx)
2836 const char *str;
2837 isl_pw_qpolynomial *pwqp;
2838 isl_pw_qpolynomial_fold *pwf, *pwf2;
2839 isl_bool equal;
2841 str = "{ [m,n] -> -m * n }";
2842 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2843 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2844 str = "{ infty }";
2845 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2846 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2847 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
2848 isl_pw_qpolynomial_fold_free(pwf);
2849 isl_pw_qpolynomial_fold_free(pwf2);
2851 if (equal < 0)
2852 return -1;
2853 if (!equal)
2854 isl_die(ctx, isl_error_unknown,
2855 "expecting infinite polynomial bound", return -1);
2857 return 0;
2860 static int test_bound(isl_ctx *ctx)
2862 const char *str;
2863 unsigned dim;
2864 isl_pw_qpolynomial *pwqp;
2865 isl_pw_qpolynomial_fold *pwf;
2867 if (test_bound_unbounded_domain(ctx) < 0)
2868 return -1;
2870 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2871 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2872 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2873 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
2874 isl_pw_qpolynomial_fold_free(pwf);
2875 if (dim != 4)
2876 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
2877 return -1);
2879 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2880 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2881 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2882 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
2883 isl_pw_qpolynomial_fold_free(pwf);
2884 if (dim != 1)
2885 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
2886 return -1);
2888 return 0;
2891 static int test_lift(isl_ctx *ctx)
2893 const char *str;
2894 isl_basic_map *bmap;
2895 isl_basic_set *bset;
2897 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2898 bset = isl_basic_set_read_from_str(ctx, str);
2899 bset = isl_basic_set_lift(bset);
2900 bmap = isl_basic_map_from_range(bset);
2901 bset = isl_basic_map_domain(bmap);
2902 isl_basic_set_free(bset);
2904 return 0;
2907 struct {
2908 const char *set1;
2909 const char *set2;
2910 int subset;
2911 } subset_tests[] = {
2912 { "{ [112, 0] }",
2913 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2914 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2915 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2916 { "{ [65] }",
2917 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2918 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2919 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2920 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2921 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2922 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2923 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2924 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2925 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2926 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2927 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2928 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2929 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2930 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
2931 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
2932 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
2933 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
2934 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
2935 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
2936 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
2937 "4e0 <= -57 + i0 + i1)) or "
2938 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
2939 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
2940 "4e0 >= -61 + i0 + i1)) or "
2941 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
2942 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
2945 static int test_subset(isl_ctx *ctx)
2947 int i;
2948 isl_set *set1, *set2;
2949 int subset;
2951 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2952 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2953 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2954 subset = isl_set_is_subset(set1, set2);
2955 isl_set_free(set1);
2956 isl_set_free(set2);
2957 if (subset < 0)
2958 return -1;
2959 if (subset != subset_tests[i].subset)
2960 isl_die(ctx, isl_error_unknown,
2961 "incorrect subset result", return -1);
2964 return 0;
2967 struct {
2968 const char *minuend;
2969 const char *subtrahend;
2970 const char *difference;
2971 } subtract_domain_tests[] = {
2972 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2973 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2974 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2977 static int test_subtract(isl_ctx *ctx)
2979 int i;
2980 isl_union_map *umap1, *umap2;
2981 isl_union_pw_multi_aff *upma1, *upma2;
2982 isl_union_set *uset;
2983 int equal;
2985 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2986 umap1 = isl_union_map_read_from_str(ctx,
2987 subtract_domain_tests[i].minuend);
2988 uset = isl_union_set_read_from_str(ctx,
2989 subtract_domain_tests[i].subtrahend);
2990 umap2 = isl_union_map_read_from_str(ctx,
2991 subtract_domain_tests[i].difference);
2992 umap1 = isl_union_map_subtract_domain(umap1, uset);
2993 equal = isl_union_map_is_equal(umap1, umap2);
2994 isl_union_map_free(umap1);
2995 isl_union_map_free(umap2);
2996 if (equal < 0)
2997 return -1;
2998 if (!equal)
2999 isl_die(ctx, isl_error_unknown,
3000 "incorrect subtract domain result", return -1);
3003 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3004 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3005 subtract_domain_tests[i].minuend);
3006 uset = isl_union_set_read_from_str(ctx,
3007 subtract_domain_tests[i].subtrahend);
3008 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3009 subtract_domain_tests[i].difference);
3010 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3011 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3012 isl_union_pw_multi_aff_free(upma1);
3013 isl_union_pw_multi_aff_free(upma2);
3014 if (equal < 0)
3015 return -1;
3016 if (!equal)
3017 isl_die(ctx, isl_error_unknown,
3018 "incorrect subtract domain result", return -1);
3021 return 0;
3024 /* Check that intersecting the empty basic set with another basic set
3025 * does not increase the number of constraints. In particular,
3026 * the empty basic set should maintain its canonical representation.
3028 static int test_intersect(isl_ctx *ctx)
3030 int n1, n2;
3031 isl_basic_set *bset1, *bset2;
3033 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3034 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3035 n1 = isl_basic_set_n_constraint(bset1);
3036 bset1 = isl_basic_set_intersect(bset1, bset2);
3037 n2 = isl_basic_set_n_constraint(bset1);
3038 isl_basic_set_free(bset1);
3039 if (!bset1)
3040 return -1;
3041 if (n1 != n2)
3042 isl_die(ctx, isl_error_unknown,
3043 "number of constraints of empty set changed",
3044 return -1);
3046 return 0;
3049 int test_factorize(isl_ctx *ctx)
3051 const char *str;
3052 isl_basic_set *bset;
3053 isl_factorizer *f;
3055 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3056 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3057 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3058 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3059 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3060 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3061 "3i5 >= -2i0 - i2 + 3i4 }";
3062 bset = isl_basic_set_read_from_str(ctx, str);
3063 f = isl_basic_set_factorizer(bset);
3064 isl_basic_set_free(bset);
3065 isl_factorizer_free(f);
3066 if (!f)
3067 isl_die(ctx, isl_error_unknown,
3068 "failed to construct factorizer", return -1);
3070 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3071 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3072 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3073 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3074 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3075 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3076 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3077 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3078 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3079 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3080 bset = isl_basic_set_read_from_str(ctx, str);
3081 f = isl_basic_set_factorizer(bset);
3082 isl_basic_set_free(bset);
3083 isl_factorizer_free(f);
3084 if (!f)
3085 isl_die(ctx, isl_error_unknown,
3086 "failed to construct factorizer", return -1);
3088 return 0;
3091 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3093 int *injective = user;
3095 *injective = isl_map_is_injective(map);
3096 isl_map_free(map);
3098 if (*injective < 0 || !*injective)
3099 return isl_stat_error;
3101 return isl_stat_ok;
3104 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3105 const char *r, const char *s, int tilable, int parallel)
3107 int i;
3108 isl_union_set *D;
3109 isl_union_map *W, *R, *S;
3110 isl_union_map *empty;
3111 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3112 isl_union_map *validity, *proximity, *coincidence;
3113 isl_union_map *schedule;
3114 isl_union_map *test;
3115 isl_union_set *delta;
3116 isl_union_set *domain;
3117 isl_set *delta_set;
3118 isl_set *slice;
3119 isl_set *origin;
3120 isl_schedule_constraints *sc;
3121 isl_schedule *sched;
3122 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3124 D = isl_union_set_read_from_str(ctx, d);
3125 W = isl_union_map_read_from_str(ctx, w);
3126 R = isl_union_map_read_from_str(ctx, r);
3127 S = isl_union_map_read_from_str(ctx, s);
3129 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3130 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3132 empty = isl_union_map_empty(isl_union_map_get_space(S));
3133 isl_union_map_compute_flow(isl_union_map_copy(R),
3134 isl_union_map_copy(W), empty,
3135 isl_union_map_copy(S),
3136 &dep_raw, NULL, NULL, NULL);
3137 isl_union_map_compute_flow(isl_union_map_copy(W),
3138 isl_union_map_copy(W),
3139 isl_union_map_copy(R),
3140 isl_union_map_copy(S),
3141 &dep_waw, &dep_war, NULL, NULL);
3143 dep = isl_union_map_union(dep_waw, dep_war);
3144 dep = isl_union_map_union(dep, dep_raw);
3145 validity = isl_union_map_copy(dep);
3146 coincidence = isl_union_map_copy(dep);
3147 proximity = isl_union_map_copy(dep);
3149 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3150 sc = isl_schedule_constraints_set_validity(sc, validity);
3151 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3152 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3153 sched = isl_schedule_constraints_compute_schedule(sc);
3154 schedule = isl_schedule_get_map(sched);
3155 isl_schedule_free(sched);
3156 isl_union_map_free(W);
3157 isl_union_map_free(R);
3158 isl_union_map_free(S);
3160 is_injection = 1;
3161 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3163 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3164 is_complete = isl_union_set_is_subset(D, domain);
3165 isl_union_set_free(D);
3166 isl_union_set_free(domain);
3168 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3169 test = isl_union_map_apply_range(test, dep);
3170 test = isl_union_map_apply_range(test, schedule);
3172 delta = isl_union_map_deltas(test);
3173 if (isl_union_set_n_set(delta) == 0) {
3174 is_tilable = 1;
3175 is_parallel = 1;
3176 is_nonneg = 1;
3177 isl_union_set_free(delta);
3178 } else {
3179 delta_set = isl_set_from_union_set(delta);
3181 slice = isl_set_universe(isl_set_get_space(delta_set));
3182 for (i = 0; i < tilable; ++i)
3183 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3184 is_tilable = isl_set_is_subset(delta_set, slice);
3185 isl_set_free(slice);
3187 slice = isl_set_universe(isl_set_get_space(delta_set));
3188 for (i = 0; i < parallel; ++i)
3189 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3190 is_parallel = isl_set_is_subset(delta_set, slice);
3191 isl_set_free(slice);
3193 origin = isl_set_universe(isl_set_get_space(delta_set));
3194 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
3195 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3197 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3198 delta_set = isl_set_lexmin(delta_set);
3200 is_nonneg = isl_set_is_equal(delta_set, origin);
3202 isl_set_free(origin);
3203 isl_set_free(delta_set);
3206 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3207 is_injection < 0 || is_complete < 0)
3208 return -1;
3209 if (!is_complete)
3210 isl_die(ctx, isl_error_unknown,
3211 "generated schedule incomplete", return -1);
3212 if (!is_injection)
3213 isl_die(ctx, isl_error_unknown,
3214 "generated schedule not injective on each statement",
3215 return -1);
3216 if (!is_nonneg)
3217 isl_die(ctx, isl_error_unknown,
3218 "negative dependences in generated schedule",
3219 return -1);
3220 if (!is_tilable)
3221 isl_die(ctx, isl_error_unknown,
3222 "generated schedule not as tilable as expected",
3223 return -1);
3224 if (!is_parallel)
3225 isl_die(ctx, isl_error_unknown,
3226 "generated schedule not as parallel as expected",
3227 return -1);
3229 return 0;
3232 /* Compute a schedule for the given instance set, validity constraints,
3233 * proximity constraints and context and return a corresponding union map
3234 * representation.
3236 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3237 const char *domain, const char *validity, const char *proximity,
3238 const char *context)
3240 isl_set *con;
3241 isl_union_set *dom;
3242 isl_union_map *dep;
3243 isl_union_map *prox;
3244 isl_schedule_constraints *sc;
3245 isl_schedule *schedule;
3246 isl_union_map *sched;
3248 con = isl_set_read_from_str(ctx, context);
3249 dom = isl_union_set_read_from_str(ctx, domain);
3250 dep = isl_union_map_read_from_str(ctx, validity);
3251 prox = isl_union_map_read_from_str(ctx, proximity);
3252 sc = isl_schedule_constraints_on_domain(dom);
3253 sc = isl_schedule_constraints_set_context(sc, con);
3254 sc = isl_schedule_constraints_set_validity(sc, dep);
3255 sc = isl_schedule_constraints_set_proximity(sc, prox);
3256 schedule = isl_schedule_constraints_compute_schedule(sc);
3257 sched = isl_schedule_get_map(schedule);
3258 isl_schedule_free(schedule);
3260 return sched;
3263 /* Compute a schedule for the given instance set, validity constraints and
3264 * proximity constraints and return a corresponding union map representation.
3266 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
3267 const char *domain, const char *validity, const char *proximity)
3269 return compute_schedule_with_context(ctx, domain, validity, proximity,
3270 "{ : }");
3273 /* Check that a schedule can be constructed on the given domain
3274 * with the given validity and proximity constraints.
3276 static int test_has_schedule(isl_ctx *ctx, const char *domain,
3277 const char *validity, const char *proximity)
3279 isl_union_map *sched;
3281 sched = compute_schedule(ctx, domain, validity, proximity);
3282 if (!sched)
3283 return -1;
3285 isl_union_map_free(sched);
3286 return 0;
3289 int test_special_schedule(isl_ctx *ctx, const char *domain,
3290 const char *validity, const char *proximity, const char *expected_sched)
3292 isl_union_map *sched1, *sched2;
3293 int equal;
3295 sched1 = compute_schedule(ctx, domain, validity, proximity);
3296 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
3298 equal = isl_union_map_is_equal(sched1, sched2);
3299 isl_union_map_free(sched1);
3300 isl_union_map_free(sched2);
3302 if (equal < 0)
3303 return -1;
3304 if (!equal)
3305 isl_die(ctx, isl_error_unknown, "unexpected schedule",
3306 return -1);
3308 return 0;
3311 /* Check that the schedule map is properly padded, even after being
3312 * reconstructed from the band forest.
3314 static int test_padded_schedule(isl_ctx *ctx)
3316 const char *str;
3317 isl_union_set *D;
3318 isl_union_map *validity, *proximity;
3319 isl_schedule_constraints *sc;
3320 isl_schedule *sched;
3321 isl_union_map *map1, *map2;
3322 isl_band_list *list;
3323 int equal;
3325 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
3326 D = isl_union_set_read_from_str(ctx, str);
3327 validity = isl_union_map_empty(isl_union_set_get_space(D));
3328 proximity = isl_union_map_copy(validity);
3329 sc = isl_schedule_constraints_on_domain(D);
3330 sc = isl_schedule_constraints_set_validity(sc, validity);
3331 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3332 sched = isl_schedule_constraints_compute_schedule(sc);
3333 map1 = isl_schedule_get_map(sched);
3334 list = isl_schedule_get_band_forest(sched);
3335 isl_band_list_free(list);
3336 map2 = isl_schedule_get_map(sched);
3337 isl_schedule_free(sched);
3338 equal = isl_union_map_is_equal(map1, map2);
3339 isl_union_map_free(map1);
3340 isl_union_map_free(map2);
3342 if (equal < 0)
3343 return -1;
3344 if (!equal)
3345 isl_die(ctx, isl_error_unknown,
3346 "reconstructed schedule map not the same as original",
3347 return -1);
3349 return 0;
3352 /* Check that conditional validity constraints are also taken into
3353 * account across bands.
3354 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
3355 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
3356 * and then check that the adjacent order constraint C[2,1]->D[2,0]
3357 * is enforced by the rest of the schedule.
3359 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
3361 const char *str;
3362 isl_union_set *domain;
3363 isl_union_map *validity, *proximity, *condition;
3364 isl_union_map *sink, *source, *dep;
3365 isl_schedule_constraints *sc;
3366 isl_schedule *schedule;
3367 isl_union_access_info *access;
3368 isl_union_flow *flow;
3369 int empty;
3371 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3372 "A[k] : k >= 1 and k <= -1 + n; "
3373 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3374 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
3375 domain = isl_union_set_read_from_str(ctx, str);
3376 sc = isl_schedule_constraints_on_domain(domain);
3377 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
3378 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3379 "D[k, i] -> C[1 + k, i] : "
3380 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3381 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
3382 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
3383 validity = isl_union_map_read_from_str(ctx, str);
3384 sc = isl_schedule_constraints_set_validity(sc, validity);
3385 str = "[n] -> { C[k, i] -> D[k, i] : "
3386 "0 <= i <= -1 + k and k <= -1 + n }";
3387 proximity = isl_union_map_read_from_str(ctx, str);
3388 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3389 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
3390 "i <= -1 + k and i >= 1 and k <= -2 + n; "
3391 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
3392 "k <= -1 + n and i >= 0 and i <= -2 + k }";
3393 condition = isl_union_map_read_from_str(ctx, str);
3394 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
3395 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3396 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
3397 "i >= 0 and i <= -1 + k and k <= -1 + n and "
3398 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
3399 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
3400 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3401 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
3402 "k <= -1 + n and i >= 0 and i <= -1 + k and "
3403 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
3404 validity = isl_union_map_read_from_str(ctx, str);
3405 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3406 validity);
3407 schedule = isl_schedule_constraints_compute_schedule(sc);
3408 str = "{ D[2,0] -> [] }";
3409 sink = isl_union_map_read_from_str(ctx, str);
3410 access = isl_union_access_info_from_sink(sink);
3411 str = "{ C[2,1] -> [] }";
3412 source = isl_union_map_read_from_str(ctx, str);
3413 access = isl_union_access_info_set_must_source(access, source);
3414 access = isl_union_access_info_set_schedule(access, schedule);
3415 flow = isl_union_access_info_compute_flow(access);
3416 dep = isl_union_flow_get_must_dependence(flow);
3417 isl_union_flow_free(flow);
3418 empty = isl_union_map_is_empty(dep);
3419 isl_union_map_free(dep);
3421 if (empty < 0)
3422 return -1;
3423 if (empty)
3424 isl_die(ctx, isl_error_unknown,
3425 "conditional validity not respected", return -1);
3427 return 0;
3430 /* Input for testing of schedule construction based on
3431 * conditional constraints.
3433 * domain is the iteration domain
3434 * flow are the flow dependences, which determine the validity and
3435 * proximity constraints
3436 * condition are the conditions on the conditional validity constraints
3437 * conditional_validity are the conditional validity constraints
3438 * outer_band_n is the expected number of members in the outer band
3440 struct {
3441 const char *domain;
3442 const char *flow;
3443 const char *condition;
3444 const char *conditional_validity;
3445 int outer_band_n;
3446 } live_range_tests[] = {
3447 /* Contrived example that illustrates that we need to keep
3448 * track of tagged condition dependences and
3449 * tagged conditional validity dependences
3450 * in isl_sched_edge separately.
3451 * In particular, the conditional validity constraints on A
3452 * cannot be satisfied,
3453 * but they can be ignored because there are no corresponding
3454 * condition constraints. However, we do have an additional
3455 * conditional validity constraint that maps to the same
3456 * dependence relation
3457 * as the condition constraint on B. If we did not make a distinction
3458 * between tagged condition and tagged conditional validity
3459 * dependences, then we
3460 * could end up treating this shared dependence as an condition
3461 * constraint on A, forcing a localization of the conditions,
3462 * which is impossible.
3464 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
3465 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
3466 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
3467 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3468 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3469 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
3472 /* TACO 2013 Fig. 7 */
3473 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3474 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3475 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3476 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
3477 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
3478 "0 <= i < n and 0 <= j < n - 1 }",
3479 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
3480 "0 <= i < n and 0 <= j < j' < n;"
3481 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
3482 "0 <= i < i' < n and 0 <= j,j' < n;"
3483 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
3484 "0 <= i,j,j' < n and j < j' }",
3487 /* TACO 2013 Fig. 7, without tags */
3488 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3489 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3490 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3491 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3492 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3493 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
3494 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
3495 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
3498 /* TACO 2013 Fig. 12 */
3499 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
3500 "S3[i,3] : 0 <= i <= 1 }",
3501 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
3502 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
3503 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
3504 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
3505 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3506 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
3507 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3508 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
3509 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
3510 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
3511 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
3516 /* Test schedule construction based on conditional constraints.
3517 * In particular, check the number of members in the outer band node
3518 * as an indication of whether tiling is possible or not.
3520 static int test_conditional_schedule_constraints(isl_ctx *ctx)
3522 int i;
3523 isl_union_set *domain;
3524 isl_union_map *condition;
3525 isl_union_map *flow;
3526 isl_union_map *validity;
3527 isl_schedule_constraints *sc;
3528 isl_schedule *schedule;
3529 isl_schedule_node *node;
3530 int n_member;
3532 if (test_special_conditional_schedule_constraints(ctx) < 0)
3533 return -1;
3535 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
3536 domain = isl_union_set_read_from_str(ctx,
3537 live_range_tests[i].domain);
3538 flow = isl_union_map_read_from_str(ctx,
3539 live_range_tests[i].flow);
3540 condition = isl_union_map_read_from_str(ctx,
3541 live_range_tests[i].condition);
3542 validity = isl_union_map_read_from_str(ctx,
3543 live_range_tests[i].conditional_validity);
3544 sc = isl_schedule_constraints_on_domain(domain);
3545 sc = isl_schedule_constraints_set_validity(sc,
3546 isl_union_map_copy(flow));
3547 sc = isl_schedule_constraints_set_proximity(sc, flow);
3548 sc = isl_schedule_constraints_set_conditional_validity(sc,
3549 condition, validity);
3550 schedule = isl_schedule_constraints_compute_schedule(sc);
3551 node = isl_schedule_get_root(schedule);
3552 while (node &&
3553 isl_schedule_node_get_type(node) != isl_schedule_node_band)
3554 node = isl_schedule_node_first_child(node);
3555 n_member = isl_schedule_node_band_n_member(node);
3556 isl_schedule_node_free(node);
3557 isl_schedule_free(schedule);
3559 if (!schedule)
3560 return -1;
3561 if (n_member != live_range_tests[i].outer_band_n)
3562 isl_die(ctx, isl_error_unknown,
3563 "unexpected number of members in outer band",
3564 return -1);
3566 return 0;
3569 /* Check that the schedule computed for the given instance set and
3570 * dependence relation strongly satisfies the dependences.
3571 * In particular, check that no instance is scheduled before
3572 * or together with an instance on which it depends.
3573 * Earlier versions of isl would produce a schedule that
3574 * only weakly satisfies the dependences.
3576 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
3578 const char *domain, *dep;
3579 isl_union_map *D, *schedule;
3580 isl_map *map, *ge;
3581 int empty;
3583 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
3584 "A[i0] : 0 <= i0 <= 1 }";
3585 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
3586 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
3587 schedule = compute_schedule(ctx, domain, dep, dep);
3588 D = isl_union_map_read_from_str(ctx, dep);
3589 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
3590 D = isl_union_map_apply_range(D, schedule);
3591 map = isl_map_from_union_map(D);
3592 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3593 map = isl_map_intersect(map, ge);
3594 empty = isl_map_is_empty(map);
3595 isl_map_free(map);
3597 if (empty < 0)
3598 return -1;
3599 if (!empty)
3600 isl_die(ctx, isl_error_unknown,
3601 "dependences not strongly satisfied", return -1);
3603 return 0;
3606 /* Compute a schedule for input where the instance set constraints
3607 * conflict with the context constraints.
3608 * Earlier versions of isl did not properly handle this situation.
3610 static int test_conflicting_context_schedule(isl_ctx *ctx)
3612 isl_union_map *schedule;
3613 const char *domain, *context;
3615 domain = "[n] -> { A[] : n >= 0 }";
3616 context = "[n] -> { : n < 0 }";
3617 schedule = compute_schedule_with_context(ctx,
3618 domain, "{}", "{}", context);
3619 isl_union_map_free(schedule);
3621 if (!schedule)
3622 return -1;
3624 return 0;
3627 /* Check that the dependence carrying step is not confused by
3628 * a bound on the coefficient size.
3629 * In particular, force the scheduler to move to a dependence carrying
3630 * step by demanding outer coincidence and bound the size of
3631 * the coefficients. Earlier versions of isl would take this
3632 * bound into account while carrying dependences, breaking
3633 * fundamental assumptions.
3635 static int test_bounded_coefficients_schedule(isl_ctx *ctx)
3637 const char *domain, *dep;
3638 isl_union_set *I;
3639 isl_union_map *D;
3640 isl_schedule_constraints *sc;
3641 isl_schedule *schedule;
3643 domain = "{ C[i0, i1] : 2 <= i0 <= 3999 and 0 <= i1 <= -1 + i0 }";
3644 dep = "{ C[i0, i1] -> C[i0, 1 + i1] : i0 <= 3999 and i1 >= 0 and "
3645 "i1 <= -2 + i0; "
3646 "C[i0, -1 + i0] -> C[1 + i0, 0] : i0 <= 3998 and i0 >= 1 }";
3647 I = isl_union_set_read_from_str(ctx, domain);
3648 D = isl_union_map_read_from_str(ctx, dep);
3649 sc = isl_schedule_constraints_on_domain(I);
3650 sc = isl_schedule_constraints_set_validity(sc, isl_union_map_copy(D));
3651 sc = isl_schedule_constraints_set_coincidence(sc, D);
3652 isl_options_set_schedule_outer_coincidence(ctx, 1);
3653 isl_options_set_schedule_max_coefficient(ctx, 20);
3654 schedule = isl_schedule_constraints_compute_schedule(sc);
3655 isl_options_set_schedule_max_coefficient(ctx, -1);
3656 isl_options_set_schedule_outer_coincidence(ctx, 0);
3657 isl_schedule_free(schedule);
3659 if (!schedule)
3660 return -1;
3662 return 0;
3665 int test_schedule(isl_ctx *ctx)
3667 const char *D, *W, *R, *V, *P, *S;
3668 int max_coincidence;
3670 /* Handle resulting schedule with zero bands. */
3671 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
3672 return -1;
3674 /* Jacobi */
3675 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
3676 W = "{ S1[t,i] -> a[t,i] }";
3677 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
3678 "S1[t,i] -> a[t-1,i+1] }";
3679 S = "{ S1[t,i] -> [t,i] }";
3680 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3681 return -1;
3683 /* Fig. 5 of CC2008 */
3684 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
3685 "j <= -1 + N }";
3686 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
3687 "j >= 2 and j <= -1 + N }";
3688 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
3689 "j >= 2 and j <= -1 + N; "
3690 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
3691 "j >= 2 and j <= -1 + N }";
3692 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3693 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3694 return -1;
3696 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
3697 W = "{ S1[i] -> a[i] }";
3698 R = "{ S2[i] -> a[i+1] }";
3699 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3700 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3701 return -1;
3703 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
3704 W = "{ S1[i] -> a[i] }";
3705 R = "{ S2[i] -> a[9-i] }";
3706 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3707 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3708 return -1;
3710 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
3711 W = "{ S1[i] -> a[i] }";
3712 R = "[N] -> { S2[i] -> a[N-1-i] }";
3713 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3714 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3715 return -1;
3717 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
3718 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
3719 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
3720 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
3721 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3722 return -1;
3724 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3725 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
3726 R = "{ S2[i,j] -> a[i-1,j] }";
3727 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3728 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3729 return -1;
3731 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3732 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
3733 R = "{ S2[i,j] -> a[i,j-1] }";
3734 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3735 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3736 return -1;
3738 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
3739 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
3740 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
3741 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
3742 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
3743 "S_0[] -> [0, 0, 0] }";
3744 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
3745 return -1;
3746 ctx->opt->schedule_parametric = 0;
3747 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3748 return -1;
3749 ctx->opt->schedule_parametric = 1;
3751 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
3752 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
3753 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
3754 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
3755 "S4[i] -> a[i,N] }";
3756 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
3757 "S4[i] -> [4,i,0] }";
3758 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
3759 isl_options_set_schedule_maximize_coincidence(ctx, 0);
3760 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3761 return -1;
3762 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
3764 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
3765 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3766 "j <= N }";
3767 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3768 "j <= N; "
3769 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
3770 "j <= N }";
3771 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3772 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3773 return -1;
3775 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
3776 " S_2[t] : t >= 0 and t <= -1 + N; "
3777 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
3778 "i <= -1 + N }";
3779 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
3780 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
3781 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
3782 "i >= 0 and i <= -1 + N }";
3783 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
3784 "i >= 0 and i <= -1 + N; "
3785 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
3786 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
3787 " S_0[t] -> [0, t, 0] }";
3789 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3790 return -1;
3791 ctx->opt->schedule_parametric = 0;
3792 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3793 return -1;
3794 ctx->opt->schedule_parametric = 1;
3796 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
3797 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
3798 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
3799 return -1;
3801 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
3802 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
3803 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
3804 "j >= 0 and j <= -1 + N; "
3805 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3806 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
3807 "j >= 0 and j <= -1 + N; "
3808 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3809 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
3810 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3811 return -1;
3813 D = "{ S_0[i] : i >= 0 }";
3814 W = "{ S_0[i] -> a[i] : i >= 0 }";
3815 R = "{ S_0[i] -> a[0] : i >= 0 }";
3816 S = "{ S_0[i] -> [0, i, 0] }";
3817 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3818 return -1;
3820 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
3821 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
3822 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
3823 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
3824 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3825 return -1;
3827 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
3828 "k <= -1 + n and k >= 0 }";
3829 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
3830 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
3831 "k <= -1 + n and k >= 0; "
3832 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
3833 "k <= -1 + n and k >= 0; "
3834 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
3835 "k <= -1 + n and k >= 0 }";
3836 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
3837 ctx->opt->schedule_outer_coincidence = 1;
3838 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3839 return -1;
3840 ctx->opt->schedule_outer_coincidence = 0;
3842 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3843 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3844 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3845 "Stmt_for_body24[i0, i1, 1, 0]:"
3846 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3847 "Stmt_for_body7[i0, i1, i2]:"
3848 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3849 "i2 <= 7 }";
3851 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3852 "Stmt_for_body24[1, i1, i2, i3]:"
3853 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3854 "i2 >= 1;"
3855 "Stmt_for_body24[0, i1, i2, i3] -> "
3856 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3857 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3858 "i3 >= 0;"
3859 "Stmt_for_body24[0, i1, i2, i3] ->"
3860 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3861 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3862 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3863 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3864 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3865 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3866 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3867 "i1 <= 6 and i1 >= 0;"
3868 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3869 "Stmt_for_body7[i0, i1, i2] -> "
3870 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3871 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3872 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3873 "Stmt_for_body7[i0, i1, i2] -> "
3874 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3875 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3876 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3877 P = V;
3878 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3879 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3880 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3882 if (test_special_schedule(ctx, D, V, P, S) < 0)
3883 return -1;
3885 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3886 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3887 "j >= 1 and j <= 7;"
3888 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3889 "j >= 1 and j <= 8 }";
3890 P = "{ }";
3891 S = "{ S_0[i, j] -> [i + j, j] }";
3892 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3893 if (test_special_schedule(ctx, D, V, P, S) < 0)
3894 return -1;
3895 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3897 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3898 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3899 "j >= 0 and j <= -1 + i }";
3900 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3901 "i <= -1 + N and j >= 0;"
3902 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3903 "i <= -2 + N }";
3904 P = "{ }";
3905 S = "{ S_0[i, j] -> [i, j] }";
3906 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3907 if (test_special_schedule(ctx, D, V, P, S) < 0)
3908 return -1;
3909 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3911 /* Test both algorithms on a case with only proximity dependences. */
3912 D = "{ S[i,j] : 0 <= i <= 10 }";
3913 V = "{ }";
3914 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3915 S = "{ S[i, j] -> [j, i] }";
3916 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3917 if (test_special_schedule(ctx, D, V, P, S) < 0)
3918 return -1;
3919 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3920 if (test_special_schedule(ctx, D, V, P, S) < 0)
3921 return -1;
3923 D = "{ A[a]; B[] }";
3924 V = "{}";
3925 P = "{ A[a] -> B[] }";
3926 if (test_has_schedule(ctx, D, V, P) < 0)
3927 return -1;
3929 if (test_padded_schedule(ctx) < 0)
3930 return -1;
3932 /* Check that check for progress is not confused by rational
3933 * solution.
3935 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3936 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3937 "i0 <= -2 + N; "
3938 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3939 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3940 P = "{}";
3941 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3942 if (test_has_schedule(ctx, D, V, P) < 0)
3943 return -1;
3944 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3946 /* Check that we allow schedule rows that are only non-trivial
3947 * on some full-dimensional domains.
3949 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
3950 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
3951 "S1[j] -> S2[1] : 0 <= j <= 1 }";
3952 P = "{}";
3953 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3954 if (test_has_schedule(ctx, D, V, P) < 0)
3955 return -1;
3956 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3958 if (test_conditional_schedule_constraints(ctx) < 0)
3959 return -1;
3961 if (test_strongly_satisfying_schedule(ctx) < 0)
3962 return -1;
3964 if (test_conflicting_context_schedule(ctx) < 0)
3965 return -1;
3967 if (test_bounded_coefficients_schedule(ctx) < 0)
3968 return -1;
3970 return 0;
3973 /* Perform scheduling tests using the whole component scheduler.
3975 static int test_schedule_whole(isl_ctx *ctx)
3977 int whole;
3978 int r;
3980 whole = isl_options_get_schedule_whole_component(ctx);
3981 isl_options_set_schedule_whole_component(ctx, 1);
3982 r = test_schedule(ctx);
3983 isl_options_set_schedule_whole_component(ctx, whole);
3985 return r;
3988 /* Perform scheduling tests using the incremental scheduler.
3990 static int test_schedule_incremental(isl_ctx *ctx)
3992 int whole;
3993 int r;
3995 whole = isl_options_get_schedule_whole_component(ctx);
3996 isl_options_set_schedule_whole_component(ctx, 0);
3997 r = test_schedule(ctx);
3998 isl_options_set_schedule_whole_component(ctx, whole);
4000 return r;
4003 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4005 isl_union_map *umap;
4006 int test;
4008 umap = isl_union_map_read_from_str(ctx, str);
4009 test = isl_union_map_plain_is_injective(umap);
4010 isl_union_map_free(umap);
4011 if (test < 0)
4012 return -1;
4013 if (test == injective)
4014 return 0;
4015 if (injective)
4016 isl_die(ctx, isl_error_unknown,
4017 "map not detected as injective", return -1);
4018 else
4019 isl_die(ctx, isl_error_unknown,
4020 "map detected as injective", return -1);
4023 int test_injective(isl_ctx *ctx)
4025 const char *str;
4027 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4028 return -1;
4029 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4030 return -1;
4031 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4032 return -1;
4033 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4034 return -1;
4035 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4036 return -1;
4037 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4038 return -1;
4039 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4040 return -1;
4041 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4042 return -1;
4044 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4045 if (test_plain_injective(ctx, str, 1))
4046 return -1;
4047 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4048 if (test_plain_injective(ctx, str, 0))
4049 return -1;
4051 return 0;
4054 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4056 isl_aff *aff2;
4057 int equal;
4059 if (!aff)
4060 return -1;
4062 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4063 equal = isl_aff_plain_is_equal(aff, aff2);
4064 isl_aff_free(aff2);
4066 return equal;
4069 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4071 int equal;
4073 equal = aff_plain_is_equal(aff, str);
4074 if (equal < 0)
4075 return -1;
4076 if (!equal)
4077 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4078 "result not as expected", return -1);
4079 return 0;
4082 struct {
4083 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4084 __isl_take isl_aff *aff2);
4085 } aff_bin_op[] = {
4086 ['+'] = { &isl_aff_add },
4087 ['-'] = { &isl_aff_sub },
4088 ['*'] = { &isl_aff_mul },
4089 ['/'] = { &isl_aff_div },
4092 struct {
4093 const char *arg1;
4094 unsigned char op;
4095 const char *arg2;
4096 const char *res;
4097 } aff_bin_tests[] = {
4098 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
4099 "{ [i] -> [2i] }" },
4100 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
4101 "{ [i] -> [0] }" },
4102 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
4103 "{ [i] -> [2i] }" },
4104 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
4105 "{ [i] -> [2i] }" },
4106 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
4107 "{ [i] -> [i/2] }" },
4108 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
4109 "{ [i] -> [i] }" },
4110 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
4111 "{ [i] -> [NaN] }" },
4112 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
4113 "{ [i] -> [NaN] }" },
4114 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
4115 "{ [i] -> [NaN] }" },
4116 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
4117 "{ [i] -> [NaN] }" },
4118 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
4119 "{ [i] -> [NaN] }" },
4120 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
4121 "{ [i] -> [NaN] }" },
4122 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
4123 "{ [i] -> [NaN] }" },
4124 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
4125 "{ [i] -> [NaN] }" },
4126 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
4127 "{ [i] -> [NaN] }" },
4128 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
4129 "{ [i] -> [NaN] }" },
4130 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
4131 "{ [i] -> [NaN] }" },
4132 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
4133 "{ [i] -> [NaN] }" },
4136 /* Perform some basic tests of binary operations on isl_aff objects.
4138 static int test_bin_aff(isl_ctx *ctx)
4140 int i;
4141 isl_aff *aff1, *aff2, *res;
4142 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4143 __isl_take isl_aff *aff2);
4144 int ok;
4146 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
4147 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
4148 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
4149 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
4150 fn = aff_bin_op[aff_bin_tests[i].op].fn;
4151 aff1 = fn(aff1, aff2);
4152 if (isl_aff_is_nan(res))
4153 ok = isl_aff_is_nan(aff1);
4154 else
4155 ok = isl_aff_plain_is_equal(aff1, res);
4156 isl_aff_free(aff1);
4157 isl_aff_free(res);
4158 if (ok < 0)
4159 return -1;
4160 if (!ok)
4161 isl_die(ctx, isl_error_unknown,
4162 "unexpected result", return -1);
4165 return 0;
4168 struct {
4169 __isl_give isl_union_pw_multi_aff *(*fn)(
4170 __isl_take isl_union_pw_multi_aff *upma1,
4171 __isl_take isl_union_pw_multi_aff *upma2);
4172 const char *arg1;
4173 const char *arg2;
4174 const char *res;
4175 } upma_bin_tests[] = {
4176 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
4177 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
4178 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
4179 "{ B[x] -> [2] : x >= 0 }",
4180 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
4181 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
4182 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
4183 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
4184 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
4185 "D[i] -> B[2] : i >= 5 }" },
4186 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4187 "{ B[x] -> C[2] : x > 0 }",
4188 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
4189 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4190 "{ B[x] -> A[2] : x >= 0 }",
4191 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
4194 /* Perform some basic tests of binary operations on
4195 * isl_union_pw_multi_aff objects.
4197 static int test_bin_upma(isl_ctx *ctx)
4199 int i;
4200 isl_union_pw_multi_aff *upma1, *upma2, *res;
4201 int ok;
4203 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
4204 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4205 upma_bin_tests[i].arg1);
4206 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4207 upma_bin_tests[i].arg2);
4208 res = isl_union_pw_multi_aff_read_from_str(ctx,
4209 upma_bin_tests[i].res);
4210 upma1 = upma_bin_tests[i].fn(upma1, upma2);
4211 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
4212 isl_union_pw_multi_aff_free(upma1);
4213 isl_union_pw_multi_aff_free(res);
4214 if (ok < 0)
4215 return -1;
4216 if (!ok)
4217 isl_die(ctx, isl_error_unknown,
4218 "unexpected result", return -1);
4221 return 0;
4224 struct {
4225 __isl_give isl_union_pw_multi_aff *(*fn)(
4226 __isl_take isl_union_pw_multi_aff *upma1,
4227 __isl_take isl_union_pw_multi_aff *upma2);
4228 const char *arg1;
4229 const char *arg2;
4230 } upma_bin_fail_tests[] = {
4231 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4232 "{ B[x] -> C[2] : x >= 0 }" },
4235 /* Perform some basic tests of binary operations on
4236 * isl_union_pw_multi_aff objects that are expected to fail.
4238 static int test_bin_upma_fail(isl_ctx *ctx)
4240 int i, n;
4241 isl_union_pw_multi_aff *upma1, *upma2;
4242 int on_error;
4244 on_error = isl_options_get_on_error(ctx);
4245 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
4246 n = ARRAY_SIZE(upma_bin_fail_tests);
4247 for (i = 0; i < n; ++i) {
4248 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4249 upma_bin_fail_tests[i].arg1);
4250 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4251 upma_bin_fail_tests[i].arg2);
4252 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
4253 isl_union_pw_multi_aff_free(upma1);
4254 if (upma1)
4255 break;
4257 isl_options_set_on_error(ctx, on_error);
4258 if (i < n)
4259 isl_die(ctx, isl_error_unknown,
4260 "operation not expected to succeed", return -1);
4262 return 0;
4265 int test_aff(isl_ctx *ctx)
4267 const char *str;
4268 isl_set *set;
4269 isl_space *space;
4270 isl_local_space *ls;
4271 isl_aff *aff;
4272 int zero, equal;
4274 if (test_bin_aff(ctx) < 0)
4275 return -1;
4276 if (test_bin_upma(ctx) < 0)
4277 return -1;
4278 if (test_bin_upma_fail(ctx) < 0)
4279 return -1;
4281 space = isl_space_set_alloc(ctx, 0, 1);
4282 ls = isl_local_space_from_space(space);
4283 aff = isl_aff_zero_on_domain(ls);
4285 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4286 aff = isl_aff_scale_down_ui(aff, 3);
4287 aff = isl_aff_floor(aff);
4288 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4289 aff = isl_aff_scale_down_ui(aff, 2);
4290 aff = isl_aff_floor(aff);
4291 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4293 str = "{ [10] }";
4294 set = isl_set_read_from_str(ctx, str);
4295 aff = isl_aff_gist(aff, set);
4297 aff = isl_aff_add_constant_si(aff, -16);
4298 zero = isl_aff_plain_is_zero(aff);
4299 isl_aff_free(aff);
4301 if (zero < 0)
4302 return -1;
4303 if (!zero)
4304 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4306 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
4307 aff = isl_aff_scale_down_ui(aff, 64);
4308 aff = isl_aff_floor(aff);
4309 equal = aff_check_plain_equal(aff, "{ [-1] }");
4310 isl_aff_free(aff);
4311 if (equal < 0)
4312 return -1;
4314 return 0;
4317 int test_dim_max(isl_ctx *ctx)
4319 int equal;
4320 const char *str;
4321 isl_set *set1, *set2;
4322 isl_set *set;
4323 isl_map *map;
4324 isl_pw_aff *pwaff;
4326 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
4327 set = isl_set_read_from_str(ctx, str);
4328 pwaff = isl_set_dim_max(set, 0);
4329 set1 = isl_set_from_pw_aff(pwaff);
4330 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
4331 set2 = isl_set_read_from_str(ctx, str);
4332 equal = isl_set_is_equal(set1, set2);
4333 isl_set_free(set1);
4334 isl_set_free(set2);
4335 if (equal < 0)
4336 return -1;
4337 if (!equal)
4338 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4340 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
4341 set = isl_set_read_from_str(ctx, str);
4342 pwaff = isl_set_dim_max(set, 0);
4343 set1 = isl_set_from_pw_aff(pwaff);
4344 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4345 set2 = isl_set_read_from_str(ctx, str);
4346 equal = isl_set_is_equal(set1, set2);
4347 isl_set_free(set1);
4348 isl_set_free(set2);
4349 if (equal < 0)
4350 return -1;
4351 if (!equal)
4352 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4354 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
4355 set = isl_set_read_from_str(ctx, str);
4356 pwaff = isl_set_dim_max(set, 0);
4357 set1 = isl_set_from_pw_aff(pwaff);
4358 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4359 set2 = isl_set_read_from_str(ctx, str);
4360 equal = isl_set_is_equal(set1, set2);
4361 isl_set_free(set1);
4362 isl_set_free(set2);
4363 if (equal < 0)
4364 return -1;
4365 if (!equal)
4366 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4368 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
4369 "0 <= i < N and 0 <= j < M }";
4370 map = isl_map_read_from_str(ctx, str);
4371 set = isl_map_range(map);
4373 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
4374 set1 = isl_set_from_pw_aff(pwaff);
4375 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
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 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
4382 set1 = isl_set_from_pw_aff(pwaff);
4383 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
4384 set2 = isl_set_read_from_str(ctx, str);
4385 if (equal >= 0 && equal)
4386 equal = isl_set_is_equal(set1, set2);
4387 isl_set_free(set1);
4388 isl_set_free(set2);
4390 isl_set_free(set);
4392 if (equal < 0)
4393 return -1;
4394 if (!equal)
4395 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4397 /* Check that solutions are properly merged. */
4398 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
4399 "c <= -1 + n - 4a - 2b and c >= -2b and "
4400 "4a >= -4 + n and c >= 0 }";
4401 set = isl_set_read_from_str(ctx, str);
4402 pwaff = isl_set_dim_min(set, 2);
4403 set1 = isl_set_from_pw_aff(pwaff);
4404 str = "[n] -> { [(0)] : n >= 1 }";
4405 set2 = isl_set_read_from_str(ctx, str);
4406 equal = isl_set_is_equal(set1, set2);
4407 isl_set_free(set1);
4408 isl_set_free(set2);
4410 if (equal < 0)
4411 return -1;
4412 if (!equal)
4413 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4415 /* Check that empty solution lie in the right space. */
4416 str = "[n] -> { [t,a] : 1 = 0 }";
4417 set = isl_set_read_from_str(ctx, str);
4418 pwaff = isl_set_dim_max(set, 0);
4419 set1 = isl_set_from_pw_aff(pwaff);
4420 str = "[n] -> { [t] : 1 = 0 }";
4421 set2 = isl_set_read_from_str(ctx, str);
4422 equal = isl_set_is_equal(set1, set2);
4423 isl_set_free(set1);
4424 isl_set_free(set2);
4426 if (equal < 0)
4427 return -1;
4428 if (!equal)
4429 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4431 return 0;
4434 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
4436 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
4437 const char *str)
4439 isl_ctx *ctx;
4440 isl_pw_multi_aff *pma2;
4441 int equal;
4443 if (!pma)
4444 return -1;
4446 ctx = isl_pw_multi_aff_get_ctx(pma);
4447 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4448 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
4449 isl_pw_multi_aff_free(pma2);
4451 return equal;
4454 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
4455 * represented by "str".
4457 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
4458 const char *str)
4460 int equal;
4462 equal = pw_multi_aff_plain_is_equal(pma, str);
4463 if (equal < 0)
4464 return -1;
4465 if (!equal)
4466 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
4467 "result not as expected", return -1);
4468 return 0;
4471 /* Basic test for isl_pw_multi_aff_product.
4473 * Check that multiple pieces are properly handled.
4475 static int test_product_pma(isl_ctx *ctx)
4477 int equal;
4478 const char *str;
4479 isl_pw_multi_aff *pma1, *pma2;
4481 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
4482 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4483 str = "{ C[] -> D[] }";
4484 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4485 pma1 = isl_pw_multi_aff_product(pma1, pma2);
4486 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
4487 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
4488 equal = pw_multi_aff_check_plain_equal(pma1, str);
4489 isl_pw_multi_aff_free(pma1);
4490 if (equal < 0)
4491 return -1;
4493 return 0;
4496 int test_product(isl_ctx *ctx)
4498 const char *str;
4499 isl_set *set;
4500 isl_union_set *uset1, *uset2;
4501 int ok;
4503 str = "{ A[i] }";
4504 set = isl_set_read_from_str(ctx, str);
4505 set = isl_set_product(set, isl_set_copy(set));
4506 ok = isl_set_is_wrapping(set);
4507 isl_set_free(set);
4508 if (ok < 0)
4509 return -1;
4510 if (!ok)
4511 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4513 str = "{ [] }";
4514 uset1 = isl_union_set_read_from_str(ctx, str);
4515 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
4516 str = "{ [[] -> []] }";
4517 uset2 = isl_union_set_read_from_str(ctx, str);
4518 ok = isl_union_set_is_equal(uset1, uset2);
4519 isl_union_set_free(uset1);
4520 isl_union_set_free(uset2);
4521 if (ok < 0)
4522 return -1;
4523 if (!ok)
4524 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4526 if (test_product_pma(ctx) < 0)
4527 return -1;
4529 return 0;
4532 /* Check that two sets are not considered disjoint just because
4533 * they have a different set of (named) parameters.
4535 static int test_disjoint(isl_ctx *ctx)
4537 const char *str;
4538 isl_set *set, *set2;
4539 int disjoint;
4541 str = "[n] -> { [[]->[]] }";
4542 set = isl_set_read_from_str(ctx, str);
4543 str = "{ [[]->[]] }";
4544 set2 = isl_set_read_from_str(ctx, str);
4545 disjoint = isl_set_is_disjoint(set, set2);
4546 isl_set_free(set);
4547 isl_set_free(set2);
4548 if (disjoint < 0)
4549 return -1;
4550 if (disjoint)
4551 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4553 return 0;
4556 int test_equal(isl_ctx *ctx)
4558 const char *str;
4559 isl_set *set, *set2;
4560 int equal;
4562 str = "{ S_6[i] }";
4563 set = isl_set_read_from_str(ctx, str);
4564 str = "{ S_7[i] }";
4565 set2 = isl_set_read_from_str(ctx, str);
4566 equal = isl_set_is_equal(set, set2);
4567 isl_set_free(set);
4568 isl_set_free(set2);
4569 if (equal < 0)
4570 return -1;
4571 if (equal)
4572 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4574 return 0;
4577 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
4578 enum isl_dim_type type, unsigned pos, int fixed)
4580 int test;
4582 test = isl_map_plain_is_fixed(map, type, pos, NULL);
4583 isl_map_free(map);
4584 if (test < 0)
4585 return -1;
4586 if (test == fixed)
4587 return 0;
4588 if (fixed)
4589 isl_die(ctx, isl_error_unknown,
4590 "map not detected as fixed", return -1);
4591 else
4592 isl_die(ctx, isl_error_unknown,
4593 "map detected as fixed", return -1);
4596 int test_fixed(isl_ctx *ctx)
4598 const char *str;
4599 isl_map *map;
4601 str = "{ [i] -> [i] }";
4602 map = isl_map_read_from_str(ctx, str);
4603 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
4604 return -1;
4605 str = "{ [i] -> [1] }";
4606 map = isl_map_read_from_str(ctx, str);
4607 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4608 return -1;
4609 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
4610 map = isl_map_read_from_str(ctx, str);
4611 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4612 return -1;
4613 map = isl_map_read_from_str(ctx, str);
4614 map = isl_map_neg(map);
4615 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4616 return -1;
4618 return 0;
4621 struct isl_vertices_test_data {
4622 const char *set;
4623 int n;
4624 const char *vertex[2];
4625 } vertices_tests[] = {
4626 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
4627 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
4628 { "{ A[t, i] : t = 14 and i = 1 }",
4629 1, { "{ A[14, 1] }" } },
4632 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
4634 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
4636 struct isl_vertices_test_data *data = user;
4637 isl_ctx *ctx;
4638 isl_multi_aff *ma;
4639 isl_basic_set *bset;
4640 isl_pw_multi_aff *pma;
4641 int i;
4642 isl_bool equal;
4644 ctx = isl_vertex_get_ctx(vertex);
4645 bset = isl_vertex_get_domain(vertex);
4646 ma = isl_vertex_get_expr(vertex);
4647 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
4649 for (i = 0; i < data->n; ++i) {
4650 isl_pw_multi_aff *pma_i;
4652 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
4653 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
4654 isl_pw_multi_aff_free(pma_i);
4656 if (equal < 0 || equal)
4657 break;
4660 isl_pw_multi_aff_free(pma);
4661 isl_vertex_free(vertex);
4663 if (equal < 0)
4664 return isl_stat_error;
4666 return equal ? isl_stat_ok : isl_stat_error;
4669 int test_vertices(isl_ctx *ctx)
4671 int i;
4673 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
4674 isl_basic_set *bset;
4675 isl_vertices *vertices;
4676 int ok = 1;
4677 int n;
4679 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
4680 vertices = isl_basic_set_compute_vertices(bset);
4681 n = isl_vertices_get_n_vertices(vertices);
4682 if (vertices_tests[i].n != n)
4683 ok = 0;
4684 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
4685 &vertices_tests[i]) < 0)
4686 ok = 0;
4687 isl_vertices_free(vertices);
4688 isl_basic_set_free(bset);
4690 if (!vertices)
4691 return -1;
4692 if (!ok)
4693 isl_die(ctx, isl_error_unknown, "unexpected vertices",
4694 return -1);
4697 return 0;
4700 int test_union_pw(isl_ctx *ctx)
4702 int equal;
4703 const char *str;
4704 isl_union_set *uset;
4705 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
4707 str = "{ [x] -> x^2 }";
4708 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
4709 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
4710 uset = isl_union_pw_qpolynomial_domain(upwqp1);
4711 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
4712 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
4713 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
4714 isl_union_pw_qpolynomial_free(upwqp1);
4715 isl_union_pw_qpolynomial_free(upwqp2);
4716 if (equal < 0)
4717 return -1;
4718 if (!equal)
4719 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4721 return 0;
4724 /* Test that isl_union_pw_qpolynomial_eval picks up the function
4725 * defined over the correct domain space.
4727 static int test_eval(isl_ctx *ctx)
4729 const char *str;
4730 isl_point *pnt;
4731 isl_set *set;
4732 isl_union_pw_qpolynomial *upwqp;
4733 isl_val *v;
4734 int cmp;
4736 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
4737 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
4738 str = "{ A[6] }";
4739 set = isl_set_read_from_str(ctx, str);
4740 pnt = isl_set_sample_point(set);
4741 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
4742 cmp = isl_val_cmp_si(v, 36);
4743 isl_val_free(v);
4745 if (!v)
4746 return -1;
4747 if (cmp != 0)
4748 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
4750 return 0;
4753 int test_output(isl_ctx *ctx)
4755 char *s;
4756 const char *str;
4757 isl_pw_aff *pa;
4758 isl_printer *p;
4759 int equal;
4761 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
4762 pa = isl_pw_aff_read_from_str(ctx, str);
4764 p = isl_printer_to_str(ctx);
4765 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
4766 p = isl_printer_print_pw_aff(p, pa);
4767 s = isl_printer_get_str(p);
4768 isl_printer_free(p);
4769 isl_pw_aff_free(pa);
4770 if (!s)
4771 equal = -1;
4772 else
4773 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
4774 free(s);
4775 if (equal < 0)
4776 return -1;
4777 if (!equal)
4778 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4780 return 0;
4783 int test_sample(isl_ctx *ctx)
4785 const char *str;
4786 isl_basic_set *bset1, *bset2;
4787 int empty, subset;
4789 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
4790 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
4791 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
4792 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
4793 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
4794 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
4795 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
4796 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
4797 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
4798 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
4799 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
4800 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
4801 "d - 1073741821e and "
4802 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
4803 "3j >= 1 - a + b + 2e and "
4804 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
4805 "3i <= 4 - a + 4b - e and "
4806 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
4807 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
4808 "c <= -1 + a and 3i >= -2 - a + 3e and "
4809 "1073741822e <= 1073741823 - a + 1073741822b + c and "
4810 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
4811 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
4812 "1073741823e >= 1 + 1073741823b - d and "
4813 "3i >= 1073741823b + c - 1073741823e - f and "
4814 "3i >= 1 + 2b + e + 3g }";
4815 bset1 = isl_basic_set_read_from_str(ctx, str);
4816 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
4817 empty = isl_basic_set_is_empty(bset2);
4818 subset = isl_basic_set_is_subset(bset2, bset1);
4819 isl_basic_set_free(bset1);
4820 isl_basic_set_free(bset2);
4821 if (empty < 0 || subset < 0)
4822 return -1;
4823 if (empty)
4824 isl_die(ctx, isl_error_unknown, "point not found", return -1);
4825 if (!subset)
4826 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
4828 return 0;
4831 int test_fixed_power(isl_ctx *ctx)
4833 const char *str;
4834 isl_map *map;
4835 isl_int exp;
4836 int equal;
4838 isl_int_init(exp);
4839 str = "{ [i] -> [i + 1] }";
4840 map = isl_map_read_from_str(ctx, str);
4841 isl_int_set_si(exp, 23);
4842 map = isl_map_fixed_power(map, exp);
4843 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
4844 isl_int_clear(exp);
4845 isl_map_free(map);
4846 if (equal < 0)
4847 return -1;
4849 return 0;
4852 int test_slice(isl_ctx *ctx)
4854 const char *str;
4855 isl_map *map;
4856 int equal;
4858 str = "{ [i] -> [j] }";
4859 map = isl_map_read_from_str(ctx, str);
4860 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
4861 equal = map_check_equal(map, "{ [i] -> [i] }");
4862 isl_map_free(map);
4863 if (equal < 0)
4864 return -1;
4866 str = "{ [i] -> [j] }";
4867 map = isl_map_read_from_str(ctx, str);
4868 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
4869 equal = map_check_equal(map, "{ [i] -> [j] }");
4870 isl_map_free(map);
4871 if (equal < 0)
4872 return -1;
4874 str = "{ [i] -> [j] }";
4875 map = isl_map_read_from_str(ctx, str);
4876 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
4877 equal = map_check_equal(map, "{ [i] -> [-i] }");
4878 isl_map_free(map);
4879 if (equal < 0)
4880 return -1;
4882 str = "{ [i] -> [j] }";
4883 map = isl_map_read_from_str(ctx, str);
4884 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
4885 equal = map_check_equal(map, "{ [0] -> [j] }");
4886 isl_map_free(map);
4887 if (equal < 0)
4888 return -1;
4890 str = "{ [i] -> [j] }";
4891 map = isl_map_read_from_str(ctx, str);
4892 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
4893 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
4894 isl_map_free(map);
4895 if (equal < 0)
4896 return -1;
4898 str = "{ [i] -> [j] }";
4899 map = isl_map_read_from_str(ctx, str);
4900 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
4901 equal = map_check_equal(map, "{ [i] -> [j] : false }");
4902 isl_map_free(map);
4903 if (equal < 0)
4904 return -1;
4906 return 0;
4909 int test_eliminate(isl_ctx *ctx)
4911 const char *str;
4912 isl_map *map;
4913 int equal;
4915 str = "{ [i] -> [j] : i = 2j }";
4916 map = isl_map_read_from_str(ctx, str);
4917 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
4918 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
4919 isl_map_free(map);
4920 if (equal < 0)
4921 return -1;
4923 return 0;
4926 /* Check that isl_set_dim_residue_class detects that the values of j
4927 * in the set below are all odd and that it does not detect any spurious
4928 * strides.
4930 static int test_residue_class(isl_ctx *ctx)
4932 const char *str;
4933 isl_set *set;
4934 isl_int m, r;
4935 int res;
4937 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
4938 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
4939 set = isl_set_read_from_str(ctx, str);
4940 isl_int_init(m);
4941 isl_int_init(r);
4942 res = isl_set_dim_residue_class(set, 1, &m, &r);
4943 if (res >= 0 &&
4944 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
4945 isl_die(ctx, isl_error_unknown, "incorrect residue class",
4946 res = -1);
4947 isl_int_clear(r);
4948 isl_int_clear(m);
4949 isl_set_free(set);
4951 return res;
4954 int test_align_parameters(isl_ctx *ctx)
4956 const char *str;
4957 isl_space *space;
4958 isl_multi_aff *ma1, *ma2;
4959 int equal;
4961 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
4962 ma1 = isl_multi_aff_read_from_str(ctx, str);
4964 space = isl_space_params_alloc(ctx, 1);
4965 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
4966 ma1 = isl_multi_aff_align_params(ma1, space);
4968 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
4969 ma2 = isl_multi_aff_read_from_str(ctx, str);
4971 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4973 isl_multi_aff_free(ma1);
4974 isl_multi_aff_free(ma2);
4976 if (equal < 0)
4977 return -1;
4978 if (!equal)
4979 isl_die(ctx, isl_error_unknown,
4980 "result not as expected", return -1);
4982 return 0;
4985 static int test_list(isl_ctx *ctx)
4987 isl_id *a, *b, *c, *d, *id;
4988 isl_id_list *list;
4989 int ok;
4991 a = isl_id_alloc(ctx, "a", NULL);
4992 b = isl_id_alloc(ctx, "b", NULL);
4993 c = isl_id_alloc(ctx, "c", NULL);
4994 d = isl_id_alloc(ctx, "d", NULL);
4996 list = isl_id_list_alloc(ctx, 4);
4997 list = isl_id_list_add(list, a);
4998 list = isl_id_list_add(list, b);
4999 list = isl_id_list_add(list, c);
5000 list = isl_id_list_add(list, d);
5001 list = isl_id_list_drop(list, 1, 1);
5003 if (isl_id_list_n_id(list) != 3) {
5004 isl_id_list_free(list);
5005 isl_die(ctx, isl_error_unknown,
5006 "unexpected number of elements in list", return -1);
5009 id = isl_id_list_get_id(list, 0);
5010 ok = id == a;
5011 isl_id_free(id);
5012 id = isl_id_list_get_id(list, 1);
5013 ok = ok && id == c;
5014 isl_id_free(id);
5015 id = isl_id_list_get_id(list, 2);
5016 ok = ok && id == d;
5017 isl_id_free(id);
5019 isl_id_list_free(list);
5021 if (!ok)
5022 isl_die(ctx, isl_error_unknown,
5023 "unexpected elements in list", return -1);
5025 return 0;
5028 const char *set_conversion_tests[] = {
5029 "[N] -> { [i] : N - 1 <= 2 i <= N }",
5030 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
5031 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5032 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5033 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
5034 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
5035 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
5036 "-3 + c <= d <= 28 + c) }",
5039 /* Check that converting from isl_set to isl_pw_multi_aff and back
5040 * to isl_set produces the original isl_set.
5042 static int test_set_conversion(isl_ctx *ctx)
5044 int i;
5045 const char *str;
5046 isl_set *set1, *set2;
5047 isl_pw_multi_aff *pma;
5048 int equal;
5050 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
5051 str = set_conversion_tests[i];
5052 set1 = isl_set_read_from_str(ctx, str);
5053 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
5054 set2 = isl_set_from_pw_multi_aff(pma);
5055 equal = isl_set_is_equal(set1, set2);
5056 isl_set_free(set1);
5057 isl_set_free(set2);
5059 if (equal < 0)
5060 return -1;
5061 if (!equal)
5062 isl_die(ctx, isl_error_unknown, "bad conversion",
5063 return -1);
5066 return 0;
5069 const char *conversion_tests[] = {
5070 "{ [a, b, c, d] -> s0[a, b, e, f] : "
5071 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
5072 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
5073 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
5074 "9e <= -2 - 2a) }",
5075 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
5076 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
5077 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
5080 /* Check that converting from isl_map to isl_pw_multi_aff and back
5081 * to isl_map produces the original isl_map.
5083 static int test_map_conversion(isl_ctx *ctx)
5085 int i;
5086 isl_map *map1, *map2;
5087 isl_pw_multi_aff *pma;
5088 int equal;
5090 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
5091 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
5092 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
5093 map2 = isl_map_from_pw_multi_aff(pma);
5094 equal = isl_map_is_equal(map1, map2);
5095 isl_map_free(map1);
5096 isl_map_free(map2);
5098 if (equal < 0)
5099 return -1;
5100 if (!equal)
5101 isl_die(ctx, isl_error_unknown, "bad conversion",
5102 return -1);
5105 return 0;
5108 static int test_conversion(isl_ctx *ctx)
5110 if (test_set_conversion(ctx) < 0)
5111 return -1;
5112 if (test_map_conversion(ctx) < 0)
5113 return -1;
5114 return 0;
5117 /* Check that isl_basic_map_curry does not modify input.
5119 static int test_curry(isl_ctx *ctx)
5121 const char *str;
5122 isl_basic_map *bmap1, *bmap2;
5123 int equal;
5125 str = "{ [A[] -> B[]] -> C[] }";
5126 bmap1 = isl_basic_map_read_from_str(ctx, str);
5127 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
5128 equal = isl_basic_map_is_equal(bmap1, bmap2);
5129 isl_basic_map_free(bmap1);
5130 isl_basic_map_free(bmap2);
5132 if (equal < 0)
5133 return -1;
5134 if (equal)
5135 isl_die(ctx, isl_error_unknown,
5136 "curried map should not be equal to original",
5137 return -1);
5139 return 0;
5142 struct {
5143 const char *set;
5144 const char *ma;
5145 const char *res;
5146 } preimage_tests[] = {
5147 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
5148 "{ A[j,i] -> B[i,j] }",
5149 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
5150 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5151 "{ A[a,b] -> B[a/2,b/6] }",
5152 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
5153 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5154 "{ A[a,b] -> B[a/2,b/6] }",
5155 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
5156 "exists i,j : a = 2 i and b = 6 j }" },
5157 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
5158 "[n] -> { : 0 <= n <= 100 }" },
5159 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5160 "{ A[a] -> B[2a] }",
5161 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
5162 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5163 "{ A[a] -> B[([a/2])] }",
5164 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
5165 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
5166 "{ A[a] -> B[a,a,a/3] }",
5167 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
5168 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
5169 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
5172 static int test_preimage_basic_set(isl_ctx *ctx)
5174 int i;
5175 isl_basic_set *bset1, *bset2;
5176 isl_multi_aff *ma;
5177 int equal;
5179 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
5180 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
5181 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
5182 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
5183 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
5184 equal = isl_basic_set_is_equal(bset1, bset2);
5185 isl_basic_set_free(bset1);
5186 isl_basic_set_free(bset2);
5187 if (equal < 0)
5188 return -1;
5189 if (!equal)
5190 isl_die(ctx, isl_error_unknown, "bad preimage",
5191 return -1);
5194 return 0;
5197 struct {
5198 const char *map;
5199 const char *ma;
5200 const char *res;
5201 } preimage_domain_tests[] = {
5202 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
5203 "{ A[j,i] -> B[i,j] }",
5204 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
5205 { "{ B[i] -> C[i]; D[i] -> E[i] }",
5206 "{ A[i] -> B[i + 1] }",
5207 "{ A[i] -> C[i + 1] }" },
5208 { "{ B[i] -> C[i]; B[i] -> E[i] }",
5209 "{ A[i] -> B[i + 1] }",
5210 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
5211 { "{ B[i] -> C[([i/2])] }",
5212 "{ A[i] -> B[2i] }",
5213 "{ A[i] -> C[i] }" },
5214 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
5215 "{ A[i] -> B[([i/5]), ([i/7])] }",
5216 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
5217 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
5218 "[N] -> { A[] -> B[([N/5])] }",
5219 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
5220 { "{ B[i] -> C[i] : exists a : i = 5 a }",
5221 "{ A[i] -> B[2i] }",
5222 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
5223 { "{ B[i] -> C[i] : exists a : i = 2 a; "
5224 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
5225 "{ A[i] -> B[2i] }",
5226 "{ A[i] -> C[2i] }" },
5227 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
5228 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
5231 static int test_preimage_union_map(isl_ctx *ctx)
5233 int i;
5234 isl_union_map *umap1, *umap2;
5235 isl_multi_aff *ma;
5236 int equal;
5238 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
5239 umap1 = isl_union_map_read_from_str(ctx,
5240 preimage_domain_tests[i].map);
5241 ma = isl_multi_aff_read_from_str(ctx,
5242 preimage_domain_tests[i].ma);
5243 umap2 = isl_union_map_read_from_str(ctx,
5244 preimage_domain_tests[i].res);
5245 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
5246 equal = isl_union_map_is_equal(umap1, umap2);
5247 isl_union_map_free(umap1);
5248 isl_union_map_free(umap2);
5249 if (equal < 0)
5250 return -1;
5251 if (!equal)
5252 isl_die(ctx, isl_error_unknown, "bad preimage",
5253 return -1);
5256 return 0;
5259 static int test_preimage(isl_ctx *ctx)
5261 if (test_preimage_basic_set(ctx) < 0)
5262 return -1;
5263 if (test_preimage_union_map(ctx) < 0)
5264 return -1;
5266 return 0;
5269 struct {
5270 const char *ma1;
5271 const char *ma;
5272 const char *res;
5273 } pullback_tests[] = {
5274 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
5275 "{ A[a,b] -> C[b + 2a] }" },
5276 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
5277 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
5278 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
5279 "{ A[a] -> C[(a)/6] }" },
5280 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
5281 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
5282 "{ A[a] -> C[(2a)/3] }" },
5283 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
5284 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
5285 "{ A[i,j] -> C[i + j, i + j] }"},
5286 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
5287 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
5288 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
5289 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
5290 "{ [i, j] -> [floor((i)/3), j] }",
5291 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
5294 static int test_pullback(isl_ctx *ctx)
5296 int i;
5297 isl_multi_aff *ma1, *ma2;
5298 isl_multi_aff *ma;
5299 int equal;
5301 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
5302 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
5303 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
5304 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
5305 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
5306 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5307 isl_multi_aff_free(ma1);
5308 isl_multi_aff_free(ma2);
5309 if (equal < 0)
5310 return -1;
5311 if (!equal)
5312 isl_die(ctx, isl_error_unknown, "bad pullback",
5313 return -1);
5316 return 0;
5319 /* Check that negation is printed correctly and that equal expressions
5320 * are correctly identified.
5322 static int test_ast(isl_ctx *ctx)
5324 isl_ast_expr *expr, *expr1, *expr2, *expr3;
5325 char *str;
5326 int ok, equal;
5328 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5329 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5330 expr = isl_ast_expr_add(expr1, expr2);
5331 expr2 = isl_ast_expr_copy(expr);
5332 expr = isl_ast_expr_neg(expr);
5333 expr2 = isl_ast_expr_neg(expr2);
5334 equal = isl_ast_expr_is_equal(expr, expr2);
5335 str = isl_ast_expr_to_str(expr);
5336 ok = str ? !strcmp(str, "-(A + B)") : -1;
5337 free(str);
5338 isl_ast_expr_free(expr);
5339 isl_ast_expr_free(expr2);
5341 if (ok < 0 || equal < 0)
5342 return -1;
5343 if (!equal)
5344 isl_die(ctx, isl_error_unknown,
5345 "equal expressions not considered equal", return -1);
5346 if (!ok)
5347 isl_die(ctx, isl_error_unknown,
5348 "isl_ast_expr printed incorrectly", return -1);
5350 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5351 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5352 expr = isl_ast_expr_add(expr1, expr2);
5353 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
5354 expr = isl_ast_expr_sub(expr3, expr);
5355 str = isl_ast_expr_to_str(expr);
5356 ok = str ? !strcmp(str, "C - (A + B)") : -1;
5357 free(str);
5358 isl_ast_expr_free(expr);
5360 if (ok < 0)
5361 return -1;
5362 if (!ok)
5363 isl_die(ctx, isl_error_unknown,
5364 "isl_ast_expr printed incorrectly", return -1);
5366 return 0;
5369 /* Check that isl_ast_build_expr_from_set returns a valid expression
5370 * for an empty set. Note that isl_ast_build_expr_from_set getting
5371 * called on an empty set probably indicates a bug in the caller.
5373 static int test_ast_build(isl_ctx *ctx)
5375 isl_set *set;
5376 isl_ast_build *build;
5377 isl_ast_expr *expr;
5379 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5380 build = isl_ast_build_from_context(set);
5382 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
5383 expr = isl_ast_build_expr_from_set(build, set);
5385 isl_ast_expr_free(expr);
5386 isl_ast_build_free(build);
5388 if (!expr)
5389 return -1;
5391 return 0;
5394 /* Internal data structure for before_for and after_for callbacks.
5396 * depth is the current depth
5397 * before is the number of times before_for has been called
5398 * after is the number of times after_for has been called
5400 struct isl_test_codegen_data {
5401 int depth;
5402 int before;
5403 int after;
5406 /* This function is called before each for loop in the AST generated
5407 * from test_ast_gen1.
5409 * Increment the number of calls and the depth.
5410 * Check that the space returned by isl_ast_build_get_schedule_space
5411 * matches the target space of the schedule returned by
5412 * isl_ast_build_get_schedule.
5413 * Return an isl_id that is checked by the corresponding call
5414 * to after_for.
5416 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
5417 void *user)
5419 struct isl_test_codegen_data *data = user;
5420 isl_ctx *ctx;
5421 isl_space *space;
5422 isl_union_map *schedule;
5423 isl_union_set *uset;
5424 isl_set *set;
5425 int empty;
5426 char name[] = "d0";
5428 ctx = isl_ast_build_get_ctx(build);
5430 if (data->before >= 3)
5431 isl_die(ctx, isl_error_unknown,
5432 "unexpected number of for nodes", return NULL);
5433 if (data->depth >= 2)
5434 isl_die(ctx, isl_error_unknown,
5435 "unexpected depth", return NULL);
5437 snprintf(name, sizeof(name), "d%d", data->depth);
5438 data->before++;
5439 data->depth++;
5441 schedule = isl_ast_build_get_schedule(build);
5442 uset = isl_union_map_range(schedule);
5443 if (!uset)
5444 return NULL;
5445 if (isl_union_set_n_set(uset) != 1) {
5446 isl_union_set_free(uset);
5447 isl_die(ctx, isl_error_unknown,
5448 "expecting single range space", return NULL);
5451 space = isl_ast_build_get_schedule_space(build);
5452 set = isl_union_set_extract_set(uset, space);
5453 isl_union_set_free(uset);
5454 empty = isl_set_is_empty(set);
5455 isl_set_free(set);
5457 if (empty < 0)
5458 return NULL;
5459 if (empty)
5460 isl_die(ctx, isl_error_unknown,
5461 "spaces don't match", return NULL);
5463 return isl_id_alloc(ctx, name, NULL);
5466 /* This function is called after each for loop in the AST generated
5467 * from test_ast_gen1.
5469 * Increment the number of calls and decrement the depth.
5470 * Check that the annotation attached to the node matches
5471 * the isl_id returned by the corresponding call to before_for.
5473 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
5474 __isl_keep isl_ast_build *build, void *user)
5476 struct isl_test_codegen_data *data = user;
5477 isl_id *id;
5478 const char *name;
5479 int valid;
5481 data->after++;
5482 data->depth--;
5484 if (data->after > data->before)
5485 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5486 "mismatch in number of for nodes",
5487 return isl_ast_node_free(node));
5489 id = isl_ast_node_get_annotation(node);
5490 if (!id)
5491 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5492 "missing annotation", return isl_ast_node_free(node));
5494 name = isl_id_get_name(id);
5495 valid = name && atoi(name + 1) == data->depth;
5496 isl_id_free(id);
5498 if (!valid)
5499 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5500 "wrong annotation", return isl_ast_node_free(node));
5502 return node;
5505 /* Check that the before_each_for and after_each_for callbacks
5506 * are called for each for loop in the generated code,
5507 * that they are called in the right order and that the isl_id
5508 * returned from the before_each_for callback is attached to
5509 * the isl_ast_node passed to the corresponding after_each_for call.
5511 static int test_ast_gen1(isl_ctx *ctx)
5513 const char *str;
5514 isl_set *set;
5515 isl_union_map *schedule;
5516 isl_ast_build *build;
5517 isl_ast_node *tree;
5518 struct isl_test_codegen_data data;
5520 str = "[N] -> { : N >= 10 }";
5521 set = isl_set_read_from_str(ctx, str);
5522 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
5523 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
5524 schedule = isl_union_map_read_from_str(ctx, str);
5526 data.before = 0;
5527 data.after = 0;
5528 data.depth = 0;
5529 build = isl_ast_build_from_context(set);
5530 build = isl_ast_build_set_before_each_for(build,
5531 &before_for, &data);
5532 build = isl_ast_build_set_after_each_for(build,
5533 &after_for, &data);
5534 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5535 isl_ast_build_free(build);
5536 if (!tree)
5537 return -1;
5539 isl_ast_node_free(tree);
5541 if (data.before != 3 || data.after != 3)
5542 isl_die(ctx, isl_error_unknown,
5543 "unexpected number of for nodes", return -1);
5545 return 0;
5548 /* Check that the AST generator handles domains that are integrally disjoint
5549 * but not rationally disjoint.
5551 static int test_ast_gen2(isl_ctx *ctx)
5553 const char *str;
5554 isl_set *set;
5555 isl_union_map *schedule;
5556 isl_union_map *options;
5557 isl_ast_build *build;
5558 isl_ast_node *tree;
5560 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
5561 schedule = isl_union_map_read_from_str(ctx, str);
5562 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5563 build = isl_ast_build_from_context(set);
5565 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
5566 options = isl_union_map_read_from_str(ctx, str);
5567 build = isl_ast_build_set_options(build, options);
5568 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5569 isl_ast_build_free(build);
5570 if (!tree)
5571 return -1;
5572 isl_ast_node_free(tree);
5574 return 0;
5577 /* Increment *user on each call.
5579 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
5580 __isl_keep isl_ast_build *build, void *user)
5582 int *n = user;
5584 (*n)++;
5586 return node;
5589 /* Test that unrolling tries to minimize the number of instances.
5590 * In particular, for the schedule given below, make sure it generates
5591 * 3 nodes (rather than 101).
5593 static int test_ast_gen3(isl_ctx *ctx)
5595 const char *str;
5596 isl_set *set;
5597 isl_union_map *schedule;
5598 isl_union_map *options;
5599 isl_ast_build *build;
5600 isl_ast_node *tree;
5601 int n_domain = 0;
5603 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
5604 schedule = isl_union_map_read_from_str(ctx, str);
5605 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5607 str = "{ [i] -> unroll[0] }";
5608 options = isl_union_map_read_from_str(ctx, str);
5610 build = isl_ast_build_from_context(set);
5611 build = isl_ast_build_set_options(build, options);
5612 build = isl_ast_build_set_at_each_domain(build,
5613 &count_domains, &n_domain);
5614 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5615 isl_ast_build_free(build);
5616 if (!tree)
5617 return -1;
5619 isl_ast_node_free(tree);
5621 if (n_domain != 3)
5622 isl_die(ctx, isl_error_unknown,
5623 "unexpected number of for nodes", return -1);
5625 return 0;
5628 /* Check that if the ast_build_exploit_nested_bounds options is set,
5629 * we do not get an outer if node in the generated AST,
5630 * while we do get such an outer if node if the options is not set.
5632 static int test_ast_gen4(isl_ctx *ctx)
5634 const char *str;
5635 isl_set *set;
5636 isl_union_map *schedule;
5637 isl_ast_build *build;
5638 isl_ast_node *tree;
5639 enum isl_ast_node_type type;
5640 int enb;
5642 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
5643 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
5645 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
5647 schedule = isl_union_map_read_from_str(ctx, str);
5648 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5649 build = isl_ast_build_from_context(set);
5650 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5651 isl_ast_build_free(build);
5652 if (!tree)
5653 return -1;
5655 type = isl_ast_node_get_type(tree);
5656 isl_ast_node_free(tree);
5658 if (type == isl_ast_node_if)
5659 isl_die(ctx, isl_error_unknown,
5660 "not expecting if node", return -1);
5662 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
5664 schedule = isl_union_map_read_from_str(ctx, str);
5665 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5666 build = isl_ast_build_from_context(set);
5667 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5668 isl_ast_build_free(build);
5669 if (!tree)
5670 return -1;
5672 type = isl_ast_node_get_type(tree);
5673 isl_ast_node_free(tree);
5675 if (type != isl_ast_node_if)
5676 isl_die(ctx, isl_error_unknown,
5677 "expecting if node", return -1);
5679 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
5681 return 0;
5684 /* This function is called for each leaf in the AST generated
5685 * from test_ast_gen5.
5687 * We finalize the AST generation by extending the outer schedule
5688 * with a zero-dimensional schedule. If this results in any for loops,
5689 * then this means that we did not pass along enough information
5690 * about the outer schedule to the inner AST generation.
5692 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
5693 void *user)
5695 isl_union_map *schedule, *extra;
5696 isl_ast_node *tree;
5698 schedule = isl_ast_build_get_schedule(build);
5699 extra = isl_union_map_copy(schedule);
5700 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
5701 schedule = isl_union_map_range_product(schedule, extra);
5702 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5703 isl_ast_build_free(build);
5705 if (!tree)
5706 return NULL;
5708 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
5709 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
5710 "code should not contain any for loop",
5711 return isl_ast_node_free(tree));
5713 return tree;
5716 /* Check that we do not lose any information when going back and
5717 * forth between internal and external schedule.
5719 * In particular, we create an AST where we unroll the only
5720 * non-constant dimension in the schedule. We therefore do
5721 * not expect any for loops in the AST. However, older versions
5722 * of isl would not pass along enough information about the outer
5723 * schedule when performing an inner code generation from a create_leaf
5724 * callback, resulting in the inner code generation producing a for loop.
5726 static int test_ast_gen5(isl_ctx *ctx)
5728 const char *str;
5729 isl_set *set;
5730 isl_union_map *schedule, *options;
5731 isl_ast_build *build;
5732 isl_ast_node *tree;
5734 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
5735 schedule = isl_union_map_read_from_str(ctx, str);
5737 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
5738 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
5739 options = isl_union_map_read_from_str(ctx, str);
5741 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5742 build = isl_ast_build_from_context(set);
5743 build = isl_ast_build_set_options(build, options);
5744 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
5745 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5746 isl_ast_build_free(build);
5747 isl_ast_node_free(tree);
5748 if (!tree)
5749 return -1;
5751 return 0;
5754 static int test_ast_gen(isl_ctx *ctx)
5756 if (test_ast_gen1(ctx) < 0)
5757 return -1;
5758 if (test_ast_gen2(ctx) < 0)
5759 return -1;
5760 if (test_ast_gen3(ctx) < 0)
5761 return -1;
5762 if (test_ast_gen4(ctx) < 0)
5763 return -1;
5764 if (test_ast_gen5(ctx) < 0)
5765 return -1;
5766 return 0;
5769 /* Check if dropping output dimensions from an isl_pw_multi_aff
5770 * works properly.
5772 static int test_pw_multi_aff(isl_ctx *ctx)
5774 const char *str;
5775 isl_pw_multi_aff *pma1, *pma2;
5776 int equal;
5778 str = "{ [i,j] -> [i+j, 4i-j] }";
5779 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
5780 str = "{ [i,j] -> [4i-j] }";
5781 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5783 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
5785 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
5787 isl_pw_multi_aff_free(pma1);
5788 isl_pw_multi_aff_free(pma2);
5789 if (equal < 0)
5790 return -1;
5791 if (!equal)
5792 isl_die(ctx, isl_error_unknown,
5793 "expressions not equal", return -1);
5795 return 0;
5798 /* Check that we can properly parse multi piecewise affine expressions
5799 * where the piecewise affine expressions have different domains.
5801 static int test_multi_pw_aff(isl_ctx *ctx)
5803 const char *str;
5804 isl_set *dom, *dom2;
5805 isl_multi_pw_aff *mpa1, *mpa2;
5806 isl_pw_aff *pa;
5807 int equal;
5808 int equal_domain;
5810 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
5811 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
5812 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
5813 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
5814 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
5815 str = "{ [i] -> [(i : i > 0), 2i] }";
5816 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
5818 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
5820 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
5821 dom = isl_pw_aff_domain(pa);
5822 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
5823 dom2 = isl_pw_aff_domain(pa);
5824 equal_domain = isl_set_is_equal(dom, dom2);
5826 isl_set_free(dom);
5827 isl_set_free(dom2);
5828 isl_multi_pw_aff_free(mpa1);
5829 isl_multi_pw_aff_free(mpa2);
5831 if (equal < 0)
5832 return -1;
5833 if (!equal)
5834 isl_die(ctx, isl_error_unknown,
5835 "expressions not equal", return -1);
5837 if (equal_domain < 0)
5838 return -1;
5839 if (equal_domain)
5840 isl_die(ctx, isl_error_unknown,
5841 "domains unexpectedly equal", return -1);
5843 return 0;
5846 /* This is a regression test for a bug where isl_basic_map_simplify
5847 * would end up in an infinite loop. In particular, we construct
5848 * an empty basic set that is not obviously empty.
5849 * isl_basic_set_is_empty marks the basic set as empty.
5850 * After projecting out i3, the variable can be dropped completely,
5851 * but isl_basic_map_simplify refrains from doing so if the basic set
5852 * is empty and would end up in an infinite loop if it didn't test
5853 * explicitly for empty basic maps in the outer loop.
5855 static int test_simplify_1(isl_ctx *ctx)
5857 const char *str;
5858 isl_basic_set *bset;
5859 int empty;
5861 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
5862 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
5863 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
5864 "i3 >= i2 }";
5865 bset = isl_basic_set_read_from_str(ctx, str);
5866 empty = isl_basic_set_is_empty(bset);
5867 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
5868 isl_basic_set_free(bset);
5869 if (!bset)
5870 return -1;
5871 if (!empty)
5872 isl_die(ctx, isl_error_unknown,
5873 "basic set should be empty", return -1);
5875 return 0;
5878 /* Check that the equality in the set description below
5879 * is simplified away.
5881 static int test_simplify_2(isl_ctx *ctx)
5883 const char *str;
5884 isl_basic_set *bset;
5885 isl_bool universe;
5887 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
5888 bset = isl_basic_set_read_from_str(ctx, str);
5889 universe = isl_basic_set_plain_is_universe(bset);
5890 isl_basic_set_free(bset);
5892 if (universe < 0)
5893 return -1;
5894 if (!universe)
5895 isl_die(ctx, isl_error_unknown,
5896 "equality not simplified away", return -1);
5897 return 0;
5900 /* Some simplification tests.
5902 static int test_simplify(isl_ctx *ctx)
5904 if (test_simplify_1(ctx) < 0)
5905 return -1;
5906 if (test_simplify_2(ctx) < 0)
5907 return -1;
5908 return 0;
5911 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
5912 * with gbr context would fail to disable the use of the shifted tableau
5913 * when transferring equalities for the input to the context, resulting
5914 * in invalid sample values.
5916 static int test_partial_lexmin(isl_ctx *ctx)
5918 const char *str;
5919 isl_basic_set *bset;
5920 isl_basic_map *bmap;
5921 isl_map *map;
5923 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
5924 bmap = isl_basic_map_read_from_str(ctx, str);
5925 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
5926 bset = isl_basic_set_read_from_str(ctx, str);
5927 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
5928 isl_map_free(map);
5930 if (!map)
5931 return -1;
5933 return 0;
5936 /* Check that the variable compression performed on the existentially
5937 * quantified variables inside isl_basic_set_compute_divs is not confused
5938 * by the implicit equalities among the parameters.
5940 static int test_compute_divs(isl_ctx *ctx)
5942 const char *str;
5943 isl_basic_set *bset;
5944 isl_set *set;
5946 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
5947 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
5948 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
5949 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
5950 bset = isl_basic_set_read_from_str(ctx, str);
5951 set = isl_basic_set_compute_divs(bset);
5952 isl_set_free(set);
5953 if (!set)
5954 return -1;
5956 return 0;
5959 /* Check that the reaching domain elements and the prefix schedule
5960 * at a leaf node are the same before and after grouping.
5962 static int test_schedule_tree_group_1(isl_ctx *ctx)
5964 int equal;
5965 const char *str;
5966 isl_id *id;
5967 isl_union_set *uset;
5968 isl_multi_union_pw_aff *mupa;
5969 isl_union_pw_multi_aff *upma1, *upma2;
5970 isl_union_set *domain1, *domain2;
5971 isl_union_map *umap1, *umap2;
5972 isl_schedule_node *node;
5974 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
5975 uset = isl_union_set_read_from_str(ctx, str);
5976 node = isl_schedule_node_from_domain(uset);
5977 node = isl_schedule_node_child(node, 0);
5978 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
5979 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5980 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5981 node = isl_schedule_node_child(node, 0);
5982 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
5983 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5984 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5985 node = isl_schedule_node_child(node, 0);
5986 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
5987 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
5988 domain1 = isl_schedule_node_get_domain(node);
5989 id = isl_id_alloc(ctx, "group", NULL);
5990 node = isl_schedule_node_parent(node);
5991 node = isl_schedule_node_group(node, id);
5992 node = isl_schedule_node_child(node, 0);
5993 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
5994 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
5995 domain2 = isl_schedule_node_get_domain(node);
5996 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
5997 if (equal >= 0 && equal)
5998 equal = isl_union_set_is_equal(domain1, domain2);
5999 if (equal >= 0 && equal)
6000 equal = isl_union_map_is_equal(umap1, umap2);
6001 isl_union_map_free(umap1);
6002 isl_union_map_free(umap2);
6003 isl_union_set_free(domain1);
6004 isl_union_set_free(domain2);
6005 isl_union_pw_multi_aff_free(upma1);
6006 isl_union_pw_multi_aff_free(upma2);
6007 isl_schedule_node_free(node);
6009 if (equal < 0)
6010 return -1;
6011 if (!equal)
6012 isl_die(ctx, isl_error_unknown,
6013 "expressions not equal", return -1);
6015 return 0;
6018 /* Check that we can have nested groupings and that the union map
6019 * schedule representation is the same before and after the grouping.
6020 * Note that after the grouping, the union map representation contains
6021 * the domain constraints from the ranges of the expansion nodes,
6022 * while they are missing from the union map representation of
6023 * the tree without expansion nodes.
6025 * Also check that the global expansion is as expected.
6027 static int test_schedule_tree_group_2(isl_ctx *ctx)
6029 int equal, equal_expansion;
6030 const char *str;
6031 isl_id *id;
6032 isl_union_set *uset;
6033 isl_union_map *umap1, *umap2;
6034 isl_union_map *expansion1, *expansion2;
6035 isl_union_set_list *filters;
6036 isl_multi_union_pw_aff *mupa;
6037 isl_schedule *schedule;
6038 isl_schedule_node *node;
6040 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
6041 "S3[i,j] : 0 <= i,j < 10 }";
6042 uset = isl_union_set_read_from_str(ctx, str);
6043 node = isl_schedule_node_from_domain(uset);
6044 node = isl_schedule_node_child(node, 0);
6045 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
6046 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6047 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6048 node = isl_schedule_node_child(node, 0);
6049 str = "{ S1[i,j] }";
6050 uset = isl_union_set_read_from_str(ctx, str);
6051 filters = isl_union_set_list_from_union_set(uset);
6052 str = "{ S2[i,j]; S3[i,j] }";
6053 uset = isl_union_set_read_from_str(ctx, str);
6054 filters = isl_union_set_list_add(filters, uset);
6055 node = isl_schedule_node_insert_sequence(node, filters);
6056 node = isl_schedule_node_child(node, 1);
6057 node = isl_schedule_node_child(node, 0);
6058 str = "{ S2[i,j] }";
6059 uset = isl_union_set_read_from_str(ctx, str);
6060 filters = isl_union_set_list_from_union_set(uset);
6061 str = "{ S3[i,j] }";
6062 uset = isl_union_set_read_from_str(ctx, str);
6063 filters = isl_union_set_list_add(filters, uset);
6064 node = isl_schedule_node_insert_sequence(node, filters);
6066 schedule = isl_schedule_node_get_schedule(node);
6067 umap1 = isl_schedule_get_map(schedule);
6068 uset = isl_schedule_get_domain(schedule);
6069 umap1 = isl_union_map_intersect_domain(umap1, uset);
6070 isl_schedule_free(schedule);
6072 node = isl_schedule_node_parent(node);
6073 node = isl_schedule_node_parent(node);
6074 id = isl_id_alloc(ctx, "group1", NULL);
6075 node = isl_schedule_node_group(node, id);
6076 node = isl_schedule_node_child(node, 1);
6077 node = isl_schedule_node_child(node, 0);
6078 id = isl_id_alloc(ctx, "group2", NULL);
6079 node = isl_schedule_node_group(node, id);
6081 schedule = isl_schedule_node_get_schedule(node);
6082 umap2 = isl_schedule_get_map(schedule);
6083 isl_schedule_free(schedule);
6085 node = isl_schedule_node_root(node);
6086 node = isl_schedule_node_child(node, 0);
6087 expansion1 = isl_schedule_node_get_subtree_expansion(node);
6088 isl_schedule_node_free(node);
6090 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
6091 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
6092 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
6094 expansion2 = isl_union_map_read_from_str(ctx, str);
6096 equal = isl_union_map_is_equal(umap1, umap2);
6097 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
6099 isl_union_map_free(umap1);
6100 isl_union_map_free(umap2);
6101 isl_union_map_free(expansion1);
6102 isl_union_map_free(expansion2);
6104 if (equal < 0 || equal_expansion < 0)
6105 return -1;
6106 if (!equal)
6107 isl_die(ctx, isl_error_unknown,
6108 "expressions not equal", return -1);
6109 if (!equal_expansion)
6110 isl_die(ctx, isl_error_unknown,
6111 "unexpected expansion", return -1);
6113 return 0;
6116 /* Some tests for the isl_schedule_node_group function.
6118 static int test_schedule_tree_group(isl_ctx *ctx)
6120 if (test_schedule_tree_group_1(ctx) < 0)
6121 return -1;
6122 if (test_schedule_tree_group_2(ctx) < 0)
6123 return -1;
6124 return 0;
6127 struct {
6128 const char *set;
6129 const char *dual;
6130 } coef_tests[] = {
6131 { "{ rat: [i] : 0 <= i <= 10 }",
6132 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
6133 { "{ rat: [i] : FALSE }",
6134 "{ rat: coefficients[[cst] -> [a]] }" },
6135 { "{ rat: [i] : }",
6136 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
6139 struct {
6140 const char *set;
6141 const char *dual;
6142 } sol_tests[] = {
6143 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
6144 "{ rat: [i] : 0 <= i <= 10 }" },
6145 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
6146 "{ rat: [i] }" },
6147 { "{ rat: coefficients[[cst] -> [a]] }",
6148 "{ rat: [i] : FALSE }" },
6151 /* Test the basic functionality of isl_basic_set_coefficients and
6152 * isl_basic_set_solutions.
6154 static int test_dual(isl_ctx *ctx)
6156 int i;
6158 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
6159 int equal;
6160 isl_basic_set *bset1, *bset2;
6162 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
6163 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
6164 bset1 = isl_basic_set_coefficients(bset1);
6165 equal = isl_basic_set_is_equal(bset1, bset2);
6166 isl_basic_set_free(bset1);
6167 isl_basic_set_free(bset2);
6168 if (equal < 0)
6169 return -1;
6170 if (!equal)
6171 isl_die(ctx, isl_error_unknown,
6172 "incorrect dual", return -1);
6175 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
6176 int equal;
6177 isl_basic_set *bset1, *bset2;
6179 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
6180 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
6181 bset1 = isl_basic_set_solutions(bset1);
6182 equal = isl_basic_set_is_equal(bset1, bset2);
6183 isl_basic_set_free(bset1);
6184 isl_basic_set_free(bset2);
6185 if (equal < 0)
6186 return -1;
6187 if (!equal)
6188 isl_die(ctx, isl_error_unknown,
6189 "incorrect dual", return -1);
6192 return 0;
6195 struct {
6196 int scale_tile;
6197 int shift_point;
6198 const char *domain;
6199 const char *schedule;
6200 const char *sizes;
6201 const char *tile;
6202 const char *point;
6203 } tile_tests[] = {
6204 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6205 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6206 "{ [32,32] }",
6207 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6208 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6210 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6211 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6212 "{ [32,32] }",
6213 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6214 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6216 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6217 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6218 "{ [32,32] }",
6219 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6220 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6222 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6223 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6224 "{ [32,32] }",
6225 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6226 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6230 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
6231 * tile the band and then check if the tile and point bands have the
6232 * expected partial schedule.
6234 static int test_tile(isl_ctx *ctx)
6236 int i;
6237 int scale;
6238 int shift;
6240 scale = isl_options_get_tile_scale_tile_loops(ctx);
6241 shift = isl_options_get_tile_shift_point_loops(ctx);
6243 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
6244 int opt;
6245 int equal;
6246 const char *str;
6247 isl_union_set *domain;
6248 isl_multi_union_pw_aff *mupa, *mupa2;
6249 isl_schedule_node *node;
6250 isl_multi_val *sizes;
6252 opt = tile_tests[i].scale_tile;
6253 isl_options_set_tile_scale_tile_loops(ctx, opt);
6254 opt = tile_tests[i].shift_point;
6255 isl_options_set_tile_shift_point_loops(ctx, opt);
6257 str = tile_tests[i].domain;
6258 domain = isl_union_set_read_from_str(ctx, str);
6259 node = isl_schedule_node_from_domain(domain);
6260 node = isl_schedule_node_child(node, 0);
6261 str = tile_tests[i].schedule;
6262 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6263 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6264 str = tile_tests[i].sizes;
6265 sizes = isl_multi_val_read_from_str(ctx, str);
6266 node = isl_schedule_node_band_tile(node, sizes);
6268 str = tile_tests[i].tile;
6269 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6270 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6271 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
6272 isl_multi_union_pw_aff_free(mupa);
6273 isl_multi_union_pw_aff_free(mupa2);
6275 node = isl_schedule_node_child(node, 0);
6277 str = tile_tests[i].point;
6278 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6279 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6280 if (equal >= 0 && equal)
6281 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
6282 mupa2);
6283 isl_multi_union_pw_aff_free(mupa);
6284 isl_multi_union_pw_aff_free(mupa2);
6286 isl_schedule_node_free(node);
6288 if (equal < 0)
6289 return -1;
6290 if (!equal)
6291 isl_die(ctx, isl_error_unknown,
6292 "unexpected result", return -1);
6295 isl_options_set_tile_scale_tile_loops(ctx, scale);
6296 isl_options_set_tile_shift_point_loops(ctx, shift);
6298 return 0;
6301 /* Check that the domain hash of a space is equal to the hash
6302 * of the domain of the space.
6304 static int test_domain_hash(isl_ctx *ctx)
6306 isl_map *map;
6307 isl_space *space;
6308 uint32_t hash1, hash2;
6310 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
6311 space = isl_map_get_space(map);
6312 isl_map_free(map);
6313 hash1 = isl_space_get_domain_hash(space);
6314 space = isl_space_domain(space);
6315 hash2 = isl_space_get_hash(space);
6316 isl_space_free(space);
6318 if (!space)
6319 return -1;
6320 if (hash1 != hash2)
6321 isl_die(ctx, isl_error_unknown,
6322 "domain hash not equal to hash of domain", return -1);
6324 return 0;
6327 /* Check that a universe basic set that is not obviously equal to the universe
6328 * is still recognized as being equal to the universe.
6330 static int test_universe(isl_ctx *ctx)
6332 const char *s;
6333 isl_basic_set *bset;
6334 isl_bool is_univ;
6336 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
6337 bset = isl_basic_set_read_from_str(ctx, s);
6338 is_univ = isl_basic_set_is_universe(bset);
6339 isl_basic_set_free(bset);
6341 if (is_univ < 0)
6342 return -1;
6343 if (!is_univ)
6344 isl_die(ctx, isl_error_unknown,
6345 "not recognized as universe set", return -1);
6347 return 0;
6350 struct {
6351 const char *name;
6352 int (*fn)(isl_ctx *ctx);
6353 } tests [] = {
6354 { "universe", &test_universe },
6355 { "domain hash", &test_domain_hash },
6356 { "dual", &test_dual },
6357 { "dependence analysis", &test_flow },
6358 { "val", &test_val },
6359 { "compute divs", &test_compute_divs },
6360 { "partial lexmin", &test_partial_lexmin },
6361 { "simplify", &test_simplify },
6362 { "curry", &test_curry },
6363 { "piecewise multi affine expressions", &test_pw_multi_aff },
6364 { "multi piecewise affine expressions", &test_multi_pw_aff },
6365 { "conversion", &test_conversion },
6366 { "list", &test_list },
6367 { "align parameters", &test_align_parameters },
6368 { "preimage", &test_preimage },
6369 { "pullback", &test_pullback },
6370 { "AST", &test_ast },
6371 { "AST build", &test_ast_build },
6372 { "AST generation", &test_ast_gen },
6373 { "eliminate", &test_eliminate },
6374 { "residue class", &test_residue_class },
6375 { "div", &test_div },
6376 { "slice", &test_slice },
6377 { "fixed power", &test_fixed_power },
6378 { "sample", &test_sample },
6379 { "output", &test_output },
6380 { "vertices", &test_vertices },
6381 { "fixed", &test_fixed },
6382 { "equal", &test_equal },
6383 { "disjoint", &test_disjoint },
6384 { "product", &test_product },
6385 { "dim_max", &test_dim_max },
6386 { "affine", &test_aff },
6387 { "injective", &test_injective },
6388 { "schedule (whole component)", &test_schedule_whole },
6389 { "schedule (incremental)", &test_schedule_incremental },
6390 { "schedule tree grouping", &test_schedule_tree_group },
6391 { "tile", &test_tile },
6392 { "union_pw", &test_union_pw },
6393 { "eval", &test_eval },
6394 { "parse", &test_parse },
6395 { "single-valued", &test_sv },
6396 { "affine hull", &test_affine_hull },
6397 { "simple_hull", &test_simple_hull },
6398 { "coalesce", &test_coalesce },
6399 { "factorize", &test_factorize },
6400 { "subset", &test_subset },
6401 { "subtract", &test_subtract },
6402 { "intersect", &test_intersect },
6403 { "lexmin", &test_lexmin },
6404 { "min", &test_min },
6405 { "gist", &test_gist },
6406 { "piecewise quasi-polynomials", &test_pwqp },
6407 { "lift", &test_lift },
6408 { "bound", &test_bound },
6409 { "union", &test_union },
6410 { "split periods", &test_split_periods },
6411 { "lexicographic order", &test_lex },
6412 { "bijectivity", &test_bijective },
6413 { "dataflow analysis", &test_dep },
6414 { "reading", &test_read },
6415 { "bounded", &test_bounded },
6416 { "construction", &test_construction },
6417 { "dimension manipulation", &test_dim },
6418 { "map application", &test_application },
6419 { "convex hull", &test_convex_hull },
6420 { "transitive closure", &test_closure },
6423 int main(int argc, char **argv)
6425 int i;
6426 struct isl_ctx *ctx;
6427 struct isl_options *options;
6429 srcdir = getenv("srcdir");
6430 assert(srcdir);
6432 options = isl_options_new_with_defaults();
6433 assert(options);
6434 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
6436 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
6437 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
6438 printf("%s\n", tests[i].name);
6439 if (tests[i].fn(ctx) < 0)
6440 goto error;
6442 isl_ctx_free(ctx);
6443 return 0;
6444 error:
6445 isl_ctx_free(ctx);
6446 return -1;