isl_polynomial.c: reduce_divs: do not assume input is a monomial
[isl.git] / isl_test.c
blobb634e39bb21c13dcfce15d3abc52f3a9768f47ee
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <assert.h>
19 #include <stdio.h>
20 #include <limits.h>
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl_space_private.h>
25 #include <isl/set.h>
26 #include <isl/flow.h>
27 #include <isl_constraint_private.h>
28 #include <isl/polynomial.h>
29 #include <isl/union_set.h>
30 #include <isl/union_map.h>
31 #include <isl_factorization.h>
32 #include <isl/schedule.h>
33 #include <isl/schedule_node.h>
34 #include <isl_options_private.h>
35 #include <isl_vertices_private.h>
36 #include <isl/ast_build.h>
37 #include <isl/val.h>
38 #include <isl/ilp.h>
39 #include <isl_ast_build_expr.h>
40 #include <isl/options.h>
42 #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]) }");
252 test_parse_pwaff(ctx, "{ [] -> [(100)] }");
254 return 0;
257 static int test_read(isl_ctx *ctx)
259 char *filename;
260 FILE *input;
261 isl_basic_set *bset1, *bset2;
262 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
263 int equal;
265 filename = get_filename(ctx, "set", "omega");
266 assert(filename);
267 input = fopen(filename, "r");
268 assert(input);
270 bset1 = isl_basic_set_read_from_file(ctx, input);
271 bset2 = isl_basic_set_read_from_str(ctx, str);
273 equal = isl_basic_set_is_equal(bset1, bset2);
275 isl_basic_set_free(bset1);
276 isl_basic_set_free(bset2);
277 free(filename);
279 fclose(input);
281 if (equal < 0)
282 return -1;
283 if (!equal)
284 isl_die(ctx, isl_error_unknown,
285 "read sets not equal", return -1);
287 return 0;
290 static int test_bounded(isl_ctx *ctx)
292 isl_set *set;
293 int bounded;
295 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
296 bounded = isl_set_is_bounded(set);
297 isl_set_free(set);
299 if (bounded < 0)
300 return -1;
301 if (!bounded)
302 isl_die(ctx, isl_error_unknown,
303 "set not considered bounded", return -1);
305 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
306 bounded = isl_set_is_bounded(set);
307 assert(!bounded);
308 isl_set_free(set);
310 if (bounded < 0)
311 return -1;
312 if (bounded)
313 isl_die(ctx, isl_error_unknown,
314 "set considered bounded", return -1);
316 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
317 bounded = isl_set_is_bounded(set);
318 isl_set_free(set);
320 if (bounded < 0)
321 return -1;
322 if (bounded)
323 isl_die(ctx, isl_error_unknown,
324 "set considered bounded", return -1);
326 return 0;
329 /* Construct the basic set { [i] : 5 <= i <= N } */
330 static int test_construction(isl_ctx *ctx)
332 isl_int v;
333 isl_space *dim;
334 isl_local_space *ls;
335 isl_basic_set *bset;
336 isl_constraint *c;
338 isl_int_init(v);
340 dim = isl_space_set_alloc(ctx, 1, 1);
341 bset = isl_basic_set_universe(isl_space_copy(dim));
342 ls = isl_local_space_from_space(dim);
344 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
345 isl_int_set_si(v, -1);
346 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
347 isl_int_set_si(v, 1);
348 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
349 bset = isl_basic_set_add_constraint(bset, c);
351 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
352 isl_int_set_si(v, 1);
353 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
354 isl_int_set_si(v, -5);
355 isl_constraint_set_constant(c, v);
356 bset = isl_basic_set_add_constraint(bset, c);
358 isl_local_space_free(ls);
359 isl_basic_set_free(bset);
361 isl_int_clear(v);
363 return 0;
366 static int test_dim(isl_ctx *ctx)
368 const char *str;
369 isl_map *map1, *map2;
370 int equal;
372 map1 = isl_map_read_from_str(ctx,
373 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
374 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
375 map2 = isl_map_read_from_str(ctx,
376 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
377 equal = isl_map_is_equal(map1, map2);
378 isl_map_free(map2);
380 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
381 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
382 if (equal >= 0 && equal)
383 equal = isl_map_is_equal(map1, map2);
385 isl_map_free(map1);
386 isl_map_free(map2);
388 if (equal < 0)
389 return -1;
390 if (!equal)
391 isl_die(ctx, isl_error_unknown,
392 "unexpected result", return -1);
394 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
395 map1 = isl_map_read_from_str(ctx, str);
396 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
397 map2 = isl_map_read_from_str(ctx, str);
398 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
399 equal = isl_map_is_equal(map1, map2);
400 isl_map_free(map1);
401 isl_map_free(map2);
403 if (equal < 0)
404 return -1;
405 if (!equal)
406 isl_die(ctx, isl_error_unknown,
407 "unexpected result", return -1);
409 return 0;
412 struct {
413 __isl_give isl_val *(*op)(__isl_take isl_val *v);
414 const char *arg;
415 const char *res;
416 } val_un_tests[] = {
417 { &isl_val_neg, "0", "0" },
418 { &isl_val_abs, "0", "0" },
419 { &isl_val_2exp, "0", "1" },
420 { &isl_val_floor, "0", "0" },
421 { &isl_val_ceil, "0", "0" },
422 { &isl_val_neg, "1", "-1" },
423 { &isl_val_neg, "-1", "1" },
424 { &isl_val_neg, "1/2", "-1/2" },
425 { &isl_val_neg, "-1/2", "1/2" },
426 { &isl_val_neg, "infty", "-infty" },
427 { &isl_val_neg, "-infty", "infty" },
428 { &isl_val_neg, "NaN", "NaN" },
429 { &isl_val_abs, "1", "1" },
430 { &isl_val_abs, "-1", "1" },
431 { &isl_val_abs, "1/2", "1/2" },
432 { &isl_val_abs, "-1/2", "1/2" },
433 { &isl_val_abs, "infty", "infty" },
434 { &isl_val_abs, "-infty", "infty" },
435 { &isl_val_abs, "NaN", "NaN" },
436 { &isl_val_floor, "1", "1" },
437 { &isl_val_floor, "-1", "-1" },
438 { &isl_val_floor, "1/2", "0" },
439 { &isl_val_floor, "-1/2", "-1" },
440 { &isl_val_floor, "infty", "infty" },
441 { &isl_val_floor, "-infty", "-infty" },
442 { &isl_val_floor, "NaN", "NaN" },
443 { &isl_val_ceil, "1", "1" },
444 { &isl_val_ceil, "-1", "-1" },
445 { &isl_val_ceil, "1/2", "1" },
446 { &isl_val_ceil, "-1/2", "0" },
447 { &isl_val_ceil, "infty", "infty" },
448 { &isl_val_ceil, "-infty", "-infty" },
449 { &isl_val_ceil, "NaN", "NaN" },
450 { &isl_val_2exp, "-3", "1/8" },
451 { &isl_val_2exp, "-1", "1/2" },
452 { &isl_val_2exp, "1", "2" },
453 { &isl_val_2exp, "2", "4" },
454 { &isl_val_2exp, "3", "8" },
455 { &isl_val_inv, "1", "1" },
456 { &isl_val_inv, "2", "1/2" },
457 { &isl_val_inv, "1/2", "2" },
458 { &isl_val_inv, "-2", "-1/2" },
459 { &isl_val_inv, "-1/2", "-2" },
460 { &isl_val_inv, "0", "NaN" },
461 { &isl_val_inv, "NaN", "NaN" },
462 { &isl_val_inv, "infty", "0" },
463 { &isl_val_inv, "-infty", "0" },
466 /* Perform some basic tests of unary operations on isl_val objects.
468 static int test_un_val(isl_ctx *ctx)
470 int i;
471 isl_val *v, *res;
472 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
473 int ok;
475 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
476 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
477 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
478 fn = val_un_tests[i].op;
479 v = fn(v);
480 if (isl_val_is_nan(res))
481 ok = isl_val_is_nan(v);
482 else
483 ok = isl_val_eq(v, res);
484 isl_val_free(v);
485 isl_val_free(res);
486 if (ok < 0)
487 return -1;
488 if (!ok)
489 isl_die(ctx, isl_error_unknown,
490 "unexpected result", return -1);
493 return 0;
496 struct {
497 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
498 __isl_take isl_val *v2);
499 } val_bin_op[] = {
500 ['+'] = { &isl_val_add },
501 ['-'] = { &isl_val_sub },
502 ['*'] = { &isl_val_mul },
503 ['/'] = { &isl_val_div },
504 ['g'] = { &isl_val_gcd },
505 ['m'] = { &isl_val_min },
506 ['M'] = { &isl_val_max },
509 struct {
510 const char *arg1;
511 unsigned char op;
512 const char *arg2;
513 const char *res;
514 } val_bin_tests[] = {
515 { "0", '+', "0", "0" },
516 { "1", '+', "0", "1" },
517 { "1", '+', "1", "2" },
518 { "1", '-', "1", "0" },
519 { "1", '*', "1", "1" },
520 { "1", '/', "1", "1" },
521 { "2", '*', "3", "6" },
522 { "2", '*', "1/2", "1" },
523 { "2", '*', "1/3", "2/3" },
524 { "2/3", '*', "3/5", "2/5" },
525 { "2/3", '*', "7/5", "14/15" },
526 { "2", '/', "1/2", "4" },
527 { "-2", '/', "-1/2", "4" },
528 { "-2", '/', "1/2", "-4" },
529 { "2", '/', "-1/2", "-4" },
530 { "2", '/', "2", "1" },
531 { "2", '/', "3", "2/3" },
532 { "2/3", '/', "5/3", "2/5" },
533 { "2/3", '/', "5/7", "14/15" },
534 { "0", '/', "0", "NaN" },
535 { "42", '/', "0", "NaN" },
536 { "-42", '/', "0", "NaN" },
537 { "infty", '/', "0", "NaN" },
538 { "-infty", '/', "0", "NaN" },
539 { "NaN", '/', "0", "NaN" },
540 { "0", '/', "NaN", "NaN" },
541 { "42", '/', "NaN", "NaN" },
542 { "-42", '/', "NaN", "NaN" },
543 { "infty", '/', "NaN", "NaN" },
544 { "-infty", '/', "NaN", "NaN" },
545 { "NaN", '/', "NaN", "NaN" },
546 { "0", '/', "infty", "0" },
547 { "42", '/', "infty", "0" },
548 { "-42", '/', "infty", "0" },
549 { "infty", '/', "infty", "NaN" },
550 { "-infty", '/', "infty", "NaN" },
551 { "NaN", '/', "infty", "NaN" },
552 { "0", '/', "-infty", "0" },
553 { "42", '/', "-infty", "0" },
554 { "-42", '/', "-infty", "0" },
555 { "infty", '/', "-infty", "NaN" },
556 { "-infty", '/', "-infty", "NaN" },
557 { "NaN", '/', "-infty", "NaN" },
558 { "1", '-', "1/3", "2/3" },
559 { "1/3", '+', "1/2", "5/6" },
560 { "1/2", '+', "1/2", "1" },
561 { "3/4", '-', "1/4", "1/2" },
562 { "1/2", '-', "1/3", "1/6" },
563 { "infty", '+', "42", "infty" },
564 { "infty", '+', "infty", "infty" },
565 { "42", '+', "infty", "infty" },
566 { "infty", '-', "infty", "NaN" },
567 { "infty", '*', "infty", "infty" },
568 { "infty", '*', "-infty", "-infty" },
569 { "-infty", '*', "infty", "-infty" },
570 { "-infty", '*', "-infty", "infty" },
571 { "0", '*', "infty", "NaN" },
572 { "1", '*', "infty", "infty" },
573 { "infty", '*', "0", "NaN" },
574 { "infty", '*', "42", "infty" },
575 { "42", '-', "infty", "-infty" },
576 { "infty", '+', "-infty", "NaN" },
577 { "4", 'g', "6", "2" },
578 { "5", 'g', "6", "1" },
579 { "42", 'm', "3", "3" },
580 { "42", 'M', "3", "42" },
581 { "3", 'm', "42", "3" },
582 { "3", 'M', "42", "42" },
583 { "42", 'm', "infty", "42" },
584 { "42", 'M', "infty", "infty" },
585 { "42", 'm', "-infty", "-infty" },
586 { "42", 'M', "-infty", "42" },
587 { "42", 'm', "NaN", "NaN" },
588 { "42", 'M', "NaN", "NaN" },
589 { "infty", 'm', "-infty", "-infty" },
590 { "infty", 'M', "-infty", "infty" },
593 /* Perform some basic tests of binary operations on isl_val objects.
595 static int test_bin_val(isl_ctx *ctx)
597 int i;
598 isl_val *v1, *v2, *res;
599 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
600 __isl_take isl_val *v2);
601 int ok;
603 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
604 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
605 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
606 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
607 fn = val_bin_op[val_bin_tests[i].op].fn;
608 v1 = fn(v1, v2);
609 if (isl_val_is_nan(res))
610 ok = isl_val_is_nan(v1);
611 else
612 ok = isl_val_eq(v1, res);
613 isl_val_free(v1);
614 isl_val_free(res);
615 if (ok < 0)
616 return -1;
617 if (!ok)
618 isl_die(ctx, isl_error_unknown,
619 "unexpected result", return -1);
622 return 0;
625 /* Perform some basic tests on isl_val objects.
627 static int test_val(isl_ctx *ctx)
629 if (test_un_val(ctx) < 0)
630 return -1;
631 if (test_bin_val(ctx) < 0)
632 return -1;
633 return 0;
636 static int test_div(isl_ctx *ctx)
638 unsigned n;
639 const char *str;
640 int empty;
641 isl_int v;
642 isl_space *dim;
643 isl_set *set;
644 isl_local_space *ls;
645 struct isl_basic_set *bset;
646 struct isl_constraint *c;
648 isl_int_init(v);
650 /* test 1 */
651 dim = isl_space_set_alloc(ctx, 0, 3);
652 bset = isl_basic_set_universe(isl_space_copy(dim));
653 ls = isl_local_space_from_space(dim);
655 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
656 isl_int_set_si(v, -1);
657 isl_constraint_set_constant(c, v);
658 isl_int_set_si(v, 1);
659 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
660 isl_int_set_si(v, 3);
661 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
662 bset = isl_basic_set_add_constraint(bset, c);
664 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
665 isl_int_set_si(v, 1);
666 isl_constraint_set_constant(c, v);
667 isl_int_set_si(v, -1);
668 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
669 isl_int_set_si(v, 3);
670 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
671 bset = isl_basic_set_add_constraint(bset, c);
673 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
675 assert(bset && bset->n_div == 1);
676 isl_local_space_free(ls);
677 isl_basic_set_free(bset);
679 /* test 2 */
680 dim = isl_space_set_alloc(ctx, 0, 3);
681 bset = isl_basic_set_universe(isl_space_copy(dim));
682 ls = isl_local_space_from_space(dim);
684 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
685 isl_int_set_si(v, 1);
686 isl_constraint_set_constant(c, v);
687 isl_int_set_si(v, -1);
688 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
689 isl_int_set_si(v, 3);
690 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
691 bset = isl_basic_set_add_constraint(bset, c);
693 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
694 isl_int_set_si(v, -1);
695 isl_constraint_set_constant(c, v);
696 isl_int_set_si(v, 1);
697 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
698 isl_int_set_si(v, 3);
699 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
700 bset = isl_basic_set_add_constraint(bset, c);
702 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
704 assert(bset && bset->n_div == 1);
705 isl_local_space_free(ls);
706 isl_basic_set_free(bset);
708 /* test 3 */
709 dim = isl_space_set_alloc(ctx, 0, 3);
710 bset = isl_basic_set_universe(isl_space_copy(dim));
711 ls = isl_local_space_from_space(dim);
713 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
714 isl_int_set_si(v, 1);
715 isl_constraint_set_constant(c, v);
716 isl_int_set_si(v, -1);
717 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
718 isl_int_set_si(v, 3);
719 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
720 bset = isl_basic_set_add_constraint(bset, c);
722 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
723 isl_int_set_si(v, -3);
724 isl_constraint_set_constant(c, v);
725 isl_int_set_si(v, 1);
726 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
727 isl_int_set_si(v, 4);
728 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
729 bset = isl_basic_set_add_constraint(bset, c);
731 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
733 assert(bset && bset->n_div == 1);
734 isl_local_space_free(ls);
735 isl_basic_set_free(bset);
737 /* test 4 */
738 dim = isl_space_set_alloc(ctx, 0, 3);
739 bset = isl_basic_set_universe(isl_space_copy(dim));
740 ls = isl_local_space_from_space(dim);
742 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
743 isl_int_set_si(v, 2);
744 isl_constraint_set_constant(c, v);
745 isl_int_set_si(v, -1);
746 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
747 isl_int_set_si(v, 3);
748 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
749 bset = isl_basic_set_add_constraint(bset, c);
751 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
752 isl_int_set_si(v, -1);
753 isl_constraint_set_constant(c, v);
754 isl_int_set_si(v, 1);
755 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
756 isl_int_set_si(v, 6);
757 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
758 bset = isl_basic_set_add_constraint(bset, c);
760 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
762 assert(isl_basic_set_is_empty(bset));
763 isl_local_space_free(ls);
764 isl_basic_set_free(bset);
766 /* test 5 */
767 dim = isl_space_set_alloc(ctx, 0, 3);
768 bset = isl_basic_set_universe(isl_space_copy(dim));
769 ls = isl_local_space_from_space(dim);
771 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
772 isl_int_set_si(v, -1);
773 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
774 isl_int_set_si(v, 3);
775 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
776 bset = isl_basic_set_add_constraint(bset, c);
778 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
779 isl_int_set_si(v, 1);
780 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
781 isl_int_set_si(v, -3);
782 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
783 bset = isl_basic_set_add_constraint(bset, c);
785 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
787 assert(bset && bset->n_div == 0);
788 isl_basic_set_free(bset);
789 isl_local_space_free(ls);
791 /* test 6 */
792 dim = isl_space_set_alloc(ctx, 0, 3);
793 bset = isl_basic_set_universe(isl_space_copy(dim));
794 ls = isl_local_space_from_space(dim);
796 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
797 isl_int_set_si(v, -1);
798 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
799 isl_int_set_si(v, 6);
800 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
801 bset = isl_basic_set_add_constraint(bset, c);
803 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
804 isl_int_set_si(v, 1);
805 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
806 isl_int_set_si(v, -3);
807 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
808 bset = isl_basic_set_add_constraint(bset, c);
810 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
812 assert(bset && bset->n_div == 1);
813 isl_basic_set_free(bset);
814 isl_local_space_free(ls);
816 /* test 7 */
817 /* This test is a bit tricky. We set up an equality
818 * a + 3b + 3c = 6 e0
819 * Normalization of divs creates _two_ divs
820 * a = 3 e0
821 * c - b - e0 = 2 e1
822 * Afterwards e0 is removed again because it has coefficient -1
823 * and we end up with the original equality and div again.
824 * Perhaps we can avoid the introduction of this temporary div.
826 dim = isl_space_set_alloc(ctx, 0, 4);
827 bset = isl_basic_set_universe(isl_space_copy(dim));
828 ls = isl_local_space_from_space(dim);
830 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
831 isl_int_set_si(v, -1);
832 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
833 isl_int_set_si(v, -3);
834 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
835 isl_int_set_si(v, -3);
836 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
837 isl_int_set_si(v, 6);
838 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
839 bset = isl_basic_set_add_constraint(bset, c);
841 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
843 /* Test disabled for now */
845 assert(bset && bset->n_div == 1);
847 isl_local_space_free(ls);
848 isl_basic_set_free(bset);
850 /* test 8 */
851 dim = isl_space_set_alloc(ctx, 0, 5);
852 bset = isl_basic_set_universe(isl_space_copy(dim));
853 ls = isl_local_space_from_space(dim);
855 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
856 isl_int_set_si(v, -1);
857 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
858 isl_int_set_si(v, -3);
859 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
860 isl_int_set_si(v, -3);
861 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
862 isl_int_set_si(v, 6);
863 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
864 bset = isl_basic_set_add_constraint(bset, c);
866 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
867 isl_int_set_si(v, -1);
868 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
869 isl_int_set_si(v, 1);
870 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
871 isl_int_set_si(v, 1);
872 isl_constraint_set_constant(c, v);
873 bset = isl_basic_set_add_constraint(bset, c);
875 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
877 /* Test disabled for now */
879 assert(bset && bset->n_div == 1);
881 isl_local_space_free(ls);
882 isl_basic_set_free(bset);
884 /* test 9 */
885 dim = isl_space_set_alloc(ctx, 0, 4);
886 bset = isl_basic_set_universe(isl_space_copy(dim));
887 ls = isl_local_space_from_space(dim);
889 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
890 isl_int_set_si(v, 1);
891 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
892 isl_int_set_si(v, -1);
893 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
894 isl_int_set_si(v, -2);
895 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
896 bset = isl_basic_set_add_constraint(bset, c);
898 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
899 isl_int_set_si(v, -1);
900 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
901 isl_int_set_si(v, 3);
902 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
903 isl_int_set_si(v, 2);
904 isl_constraint_set_constant(c, v);
905 bset = isl_basic_set_add_constraint(bset, c);
907 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
909 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
911 assert(!isl_basic_set_is_empty(bset));
913 isl_local_space_free(ls);
914 isl_basic_set_free(bset);
916 /* test 10 */
917 dim = isl_space_set_alloc(ctx, 0, 3);
918 bset = isl_basic_set_universe(isl_space_copy(dim));
919 ls = isl_local_space_from_space(dim);
921 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
922 isl_int_set_si(v, 1);
923 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
924 isl_int_set_si(v, -2);
925 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
926 bset = isl_basic_set_add_constraint(bset, c);
928 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
930 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
932 isl_local_space_free(ls);
933 isl_basic_set_free(bset);
935 isl_int_clear(v);
937 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
938 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
939 set = isl_set_read_from_str(ctx, str);
940 set = isl_set_compute_divs(set);
941 isl_set_free(set);
942 if (!set)
943 return -1;
945 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
946 bset = isl_basic_set_read_from_str(ctx, str);
947 n = isl_basic_set_dim(bset, isl_dim_div);
948 isl_basic_set_free(bset);
949 if (!bset)
950 return -1;
951 if (n != 0)
952 isl_die(ctx, isl_error_unknown,
953 "expecting no existentials", return -1);
955 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
956 set = isl_set_read_from_str(ctx, str);
957 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
958 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
959 empty = isl_set_is_empty(set);
960 isl_set_free(set);
961 if (empty < 0)
962 return -1;
963 if (!empty)
964 isl_die(ctx, isl_error_unknown,
965 "result not as accurate as expected", return -1);
967 return 0;
970 void test_application_case(struct isl_ctx *ctx, const char *name)
972 char *filename;
973 FILE *input;
974 struct isl_basic_set *bset1, *bset2;
975 struct isl_basic_map *bmap;
977 filename = get_filename(ctx, name, "omega");
978 assert(filename);
979 input = fopen(filename, "r");
980 assert(input);
982 bset1 = isl_basic_set_read_from_file(ctx, input);
983 bmap = isl_basic_map_read_from_file(ctx, input);
985 bset1 = isl_basic_set_apply(bset1, bmap);
987 bset2 = isl_basic_set_read_from_file(ctx, input);
989 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
991 isl_basic_set_free(bset1);
992 isl_basic_set_free(bset2);
993 free(filename);
995 fclose(input);
998 static int test_application(isl_ctx *ctx)
1000 test_application_case(ctx, "application");
1001 test_application_case(ctx, "application2");
1003 return 0;
1006 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1008 char *filename;
1009 FILE *input;
1010 struct isl_basic_set *bset1, *bset2;
1012 filename = get_filename(ctx, name, "polylib");
1013 assert(filename);
1014 input = fopen(filename, "r");
1015 assert(input);
1017 bset1 = isl_basic_set_read_from_file(ctx, input);
1018 bset2 = isl_basic_set_read_from_file(ctx, input);
1020 bset1 = isl_basic_set_affine_hull(bset1);
1022 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1024 isl_basic_set_free(bset1);
1025 isl_basic_set_free(bset2);
1026 free(filename);
1028 fclose(input);
1031 int test_affine_hull(struct isl_ctx *ctx)
1033 const char *str;
1034 isl_set *set;
1035 isl_basic_set *bset, *bset2;
1036 int n;
1037 int subset;
1039 test_affine_hull_case(ctx, "affine2");
1040 test_affine_hull_case(ctx, "affine");
1041 test_affine_hull_case(ctx, "affine3");
1043 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1044 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1045 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1046 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1047 set = isl_set_read_from_str(ctx, str);
1048 bset = isl_set_affine_hull(set);
1049 n = isl_basic_set_dim(bset, isl_dim_div);
1050 isl_basic_set_free(bset);
1051 if (n != 0)
1052 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1053 return -1);
1055 /* Check that isl_map_affine_hull is not confused by
1056 * the reordering of divs in isl_map_align_divs.
1058 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1059 "32e0 = b and 32e1 = c); "
1060 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1061 set = isl_set_read_from_str(ctx, str);
1062 bset = isl_set_affine_hull(set);
1063 isl_basic_set_free(bset);
1064 if (!bset)
1065 return -1;
1067 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1068 "32e2 = 31 + 31e0 }";
1069 set = isl_set_read_from_str(ctx, str);
1070 bset = isl_set_affine_hull(set);
1071 str = "{ [a] : exists e : a = 32 e }";
1072 bset2 = isl_basic_set_read_from_str(ctx, str);
1073 subset = isl_basic_set_is_subset(bset, bset2);
1074 isl_basic_set_free(bset);
1075 isl_basic_set_free(bset2);
1076 if (subset < 0)
1077 return -1;
1078 if (!subset)
1079 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1080 return -1);
1082 return 0;
1085 /* Pairs of maps and the corresponding expected results of
1086 * isl_map_plain_unshifted_simple_hull.
1088 struct {
1089 const char *map;
1090 const char *hull;
1091 } plain_unshifted_simple_hull_tests[] = {
1092 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1093 "{ [i] -> [j] : i >= 1 }" },
1094 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1095 "(j mod 4 = 2 and k mod 6 = n) }",
1096 "{ [n] -> [i,j,k] : j mod 4 = 2 }" },
1099 /* Basic tests for isl_map_plain_unshifted_simple_hull.
1101 static int test_plain_unshifted_simple_hull(isl_ctx *ctx)
1103 int i;
1104 isl_map *map;
1105 isl_basic_map *hull, *expected;
1106 isl_bool equal;
1108 for (i = 0; i < ARRAY_SIZE(plain_unshifted_simple_hull_tests); ++i) {
1109 const char *str;
1110 str = plain_unshifted_simple_hull_tests[i].map;
1111 map = isl_map_read_from_str(ctx, str);
1112 str = plain_unshifted_simple_hull_tests[i].hull;
1113 expected = isl_basic_map_read_from_str(ctx, str);
1114 hull = isl_map_plain_unshifted_simple_hull(map);
1115 equal = isl_basic_map_is_equal(hull, expected);
1116 isl_basic_map_free(hull);
1117 isl_basic_map_free(expected);
1118 if (equal < 0)
1119 return -1;
1120 if (!equal)
1121 isl_die(ctx, isl_error_unknown, "unexpected hull",
1122 return -1);
1125 return 0;
1128 /* Pairs of sets and the corresponding expected results of
1129 * isl_set_unshifted_simple_hull.
1131 struct {
1132 const char *set;
1133 const char *hull;
1134 } unshifted_simple_hull_tests[] = {
1135 { "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1136 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1139 /* Basic tests for isl_set_unshifted_simple_hull.
1141 static int test_unshifted_simple_hull(isl_ctx *ctx)
1143 int i;
1144 isl_set *set;
1145 isl_basic_set *hull, *expected;
1146 isl_bool equal;
1148 for (i = 0; i < ARRAY_SIZE(unshifted_simple_hull_tests); ++i) {
1149 const char *str;
1150 str = unshifted_simple_hull_tests[i].set;
1151 set = isl_set_read_from_str(ctx, str);
1152 str = unshifted_simple_hull_tests[i].hull;
1153 expected = isl_basic_set_read_from_str(ctx, str);
1154 hull = isl_set_unshifted_simple_hull(set);
1155 equal = isl_basic_set_is_equal(hull, expected);
1156 isl_basic_set_free(hull);
1157 isl_basic_set_free(expected);
1158 if (equal < 0)
1159 return -1;
1160 if (!equal)
1161 isl_die(ctx, isl_error_unknown, "unexpected hull",
1162 return -1);
1165 return 0;
1168 static int test_simple_hull(struct isl_ctx *ctx)
1170 const char *str;
1171 isl_set *set;
1172 isl_basic_set *bset;
1173 isl_bool is_empty;
1175 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1176 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1177 set = isl_set_read_from_str(ctx, str);
1178 bset = isl_set_simple_hull(set);
1179 is_empty = isl_basic_set_is_empty(bset);
1180 isl_basic_set_free(bset);
1182 if (is_empty == isl_bool_error)
1183 return -1;
1185 if (is_empty == isl_bool_false)
1186 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1187 return -1);
1189 if (test_plain_unshifted_simple_hull(ctx) < 0)
1190 return -1;
1191 if (test_unshifted_simple_hull(ctx) < 0)
1192 return -1;
1194 return 0;
1197 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1199 char *filename;
1200 FILE *input;
1201 struct isl_basic_set *bset1, *bset2;
1202 struct isl_set *set;
1204 filename = get_filename(ctx, name, "polylib");
1205 assert(filename);
1206 input = fopen(filename, "r");
1207 assert(input);
1209 bset1 = isl_basic_set_read_from_file(ctx, input);
1210 bset2 = isl_basic_set_read_from_file(ctx, input);
1212 set = isl_basic_set_union(bset1, bset2);
1213 bset1 = isl_set_convex_hull(set);
1215 bset2 = isl_basic_set_read_from_file(ctx, input);
1217 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1219 isl_basic_set_free(bset1);
1220 isl_basic_set_free(bset2);
1221 free(filename);
1223 fclose(input);
1226 struct {
1227 const char *set;
1228 const char *hull;
1229 } convex_hull_tests[] = {
1230 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1231 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1232 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1233 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1234 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1235 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1236 "i2 <= 5 and i2 >= 4; "
1237 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1238 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1239 "i2 <= 5 + i0 and i2 >= i0 }" },
1240 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1241 "{ [x, y] : 1 = 0 }" },
1242 { "{ [x, y, z] : 0 <= x, y, z <= 10; [x, y, 0] : x >= 0 and y > 0; "
1243 "[x, y, 0] : x >= 0 and y < 0 }",
1244 "{ [x, y, z] : x >= 0 and 0 <= z <= 10 }" },
1247 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1249 int i;
1250 int orig_convex = ctx->opt->convex;
1251 ctx->opt->convex = convex;
1253 test_convex_hull_case(ctx, "convex0");
1254 test_convex_hull_case(ctx, "convex1");
1255 test_convex_hull_case(ctx, "convex2");
1256 test_convex_hull_case(ctx, "convex3");
1257 test_convex_hull_case(ctx, "convex4");
1258 test_convex_hull_case(ctx, "convex5");
1259 test_convex_hull_case(ctx, "convex6");
1260 test_convex_hull_case(ctx, "convex7");
1261 test_convex_hull_case(ctx, "convex8");
1262 test_convex_hull_case(ctx, "convex9");
1263 test_convex_hull_case(ctx, "convex10");
1264 test_convex_hull_case(ctx, "convex11");
1265 test_convex_hull_case(ctx, "convex12");
1266 test_convex_hull_case(ctx, "convex13");
1267 test_convex_hull_case(ctx, "convex14");
1268 test_convex_hull_case(ctx, "convex15");
1270 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1271 isl_set *set1, *set2;
1272 int equal;
1274 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1275 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1276 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1277 equal = isl_set_is_equal(set1, set2);
1278 isl_set_free(set1);
1279 isl_set_free(set2);
1281 if (equal < 0)
1282 return -1;
1283 if (!equal)
1284 isl_die(ctx, isl_error_unknown,
1285 "unexpected convex hull", return -1);
1288 ctx->opt->convex = orig_convex;
1290 return 0;
1293 static int test_convex_hull(isl_ctx *ctx)
1295 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1296 return -1;
1297 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1298 return -1;
1299 return 0;
1302 void test_gist_case(struct isl_ctx *ctx, const char *name)
1304 char *filename;
1305 FILE *input;
1306 struct isl_basic_set *bset1, *bset2;
1308 filename = get_filename(ctx, name, "polylib");
1309 assert(filename);
1310 input = fopen(filename, "r");
1311 assert(input);
1313 bset1 = isl_basic_set_read_from_file(ctx, input);
1314 bset2 = isl_basic_set_read_from_file(ctx, input);
1316 bset1 = isl_basic_set_gist(bset1, bset2);
1318 bset2 = isl_basic_set_read_from_file(ctx, input);
1320 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1322 isl_basic_set_free(bset1);
1323 isl_basic_set_free(bset2);
1324 free(filename);
1326 fclose(input);
1329 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1331 struct {
1332 const char *map;
1333 const char *context;
1334 const char *gist;
1335 } plain_gist_tests[] = {
1336 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1337 "{ [i] -> [j] : i >= 1 }",
1338 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1339 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1340 "(j mod 4 = 2 and k mod 6 = n) }",
1341 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1342 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1343 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1344 "{ [i] -> [j] : i > j }",
1345 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1348 /* Basic tests for isl_map_plain_gist_basic_map.
1350 static int test_plain_gist(isl_ctx *ctx)
1352 int i;
1354 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1355 const char *str;
1356 int equal;
1357 isl_map *map, *gist;
1358 isl_basic_map *context;
1360 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1361 str = plain_gist_tests[i].context;
1362 context = isl_basic_map_read_from_str(ctx, str);
1363 map = isl_map_plain_gist_basic_map(map, context);
1364 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1365 equal = isl_map_is_equal(map, gist);
1366 isl_map_free(map);
1367 isl_map_free(gist);
1368 if (equal < 0)
1369 return -1;
1370 if (!equal)
1371 isl_die(ctx, isl_error_unknown,
1372 "incorrect gist result", return -1);
1375 return 0;
1378 struct {
1379 const char *set;
1380 const char *context;
1381 const char *gist;
1382 } gist_tests[] = {
1383 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1384 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1385 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1386 "{ [a, b, c] : a <= 15 }" },
1387 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1388 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1389 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1390 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1391 { "{ [m, n, a, b] : a <= 2147 + n }",
1392 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1393 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1394 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1395 "b >= 0) }",
1396 "{ [m, n, ku, kl] }" },
1397 { "{ [a, a, b] : a >= 10 }",
1398 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1399 "{ [a, a, b] : a >= 10 }" },
1400 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1401 "{ [0, j] : j >= 0 }" },
1402 /* Check that no constraints on i6 are introduced in the gist */
1403 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1404 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1405 "5e0 <= 381 - t1 and i4 <= 1) }",
1406 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1407 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1408 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1409 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1410 "20e0 >= 1511 - 4t1 - 5i4) }" },
1411 /* Check that no constraints on i6 are introduced in the gist */
1412 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1413 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1414 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1415 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1416 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1417 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1418 "20e1 <= 1530 - 4t1 - 5i4 and "
1419 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1420 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1421 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1422 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1423 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1424 "10e0 <= -91 + 5i4 + 4i6 and "
1425 "10e0 >= -105 + 5i4 + 4i6) }",
1426 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1427 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1428 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1429 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1430 "{ [a, b, q, p] : b >= 1 + a }",
1431 "{ [a, b, q, p] : false }" },
1432 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1433 "[n] -> { [x] : x mod 32 = 0 }",
1434 "[n] -> { [x = n] }" },
1435 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1436 "{ [x] : x mod 2 = 0 }" },
1437 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1438 "{ [x] : x mod 128 = 0 }" },
1439 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1440 "{ [x] : x mod 3200 = 0 }" },
1441 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1442 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1443 "{ [a, b, c = a] }" },
1444 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1445 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1446 "{ [a, b, c = a] : a mod 3 = 0 }" },
1447 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1448 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1449 "{ [x] }" },
1450 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1451 "{ [x,y] : 1 <= y <= 3 }",
1452 "{ [x,y] }" },
1455 /* Check that isl_set_gist behaves as expected.
1457 * For the test cases in gist_tests, besides checking that the result
1458 * is as expected, also check that applying the gist operation does
1459 * not modify the input set (an earlier version of isl would do that) and
1460 * that the test case is consistent, i.e., that the gist has the same
1461 * intersection with the context as the input set.
1463 static int test_gist(struct isl_ctx *ctx)
1465 int i;
1466 const char *str;
1467 isl_basic_set *bset1, *bset2;
1468 isl_map *map1, *map2;
1469 int equal;
1471 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1472 int equal_input, equal_intersection;
1473 isl_set *set1, *set2, *copy, *context;
1475 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1476 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1477 copy = isl_set_copy(set1);
1478 set1 = isl_set_gist(set1, isl_set_copy(context));
1479 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1480 equal = isl_set_is_equal(set1, set2);
1481 isl_set_free(set1);
1482 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1483 equal_input = isl_set_is_equal(set1, copy);
1484 isl_set_free(copy);
1485 set1 = isl_set_intersect(set1, isl_set_copy(context));
1486 set2 = isl_set_intersect(set2, context);
1487 equal_intersection = isl_set_is_equal(set1, set2);
1488 isl_set_free(set2);
1489 isl_set_free(set1);
1490 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1491 return -1;
1492 if (!equal)
1493 isl_die(ctx, isl_error_unknown,
1494 "incorrect gist result", return -1);
1495 if (!equal_input)
1496 isl_die(ctx, isl_error_unknown,
1497 "gist modified input", return -1);
1498 if (!equal_input)
1499 isl_die(ctx, isl_error_unknown,
1500 "inconsistent gist test case", return -1);
1503 test_gist_case(ctx, "gist1");
1505 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1506 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1507 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1508 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1509 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1510 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1511 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1512 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1513 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1514 bset1 = isl_basic_set_read_from_str(ctx, str);
1515 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1516 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1517 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1518 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1519 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1520 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1521 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1522 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1523 bset2 = isl_basic_set_read_from_str(ctx, str);
1524 bset1 = isl_basic_set_gist(bset1, bset2);
1525 assert(bset1 && bset1->n_div == 0);
1526 isl_basic_set_free(bset1);
1528 /* Check that the integer divisions of the second disjunct
1529 * do not spread to the first disjunct.
1531 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1532 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1533 "(exists (e0 = [(-1 + t1)/16], "
1534 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1535 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1536 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1537 "o0 <= 4294967295 and t1 <= -1)) }";
1538 map1 = isl_map_read_from_str(ctx, str);
1539 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1540 map2 = isl_map_read_from_str(ctx, str);
1541 map1 = isl_map_gist(map1, map2);
1542 if (!map1)
1543 return -1;
1544 if (map1->n != 1)
1545 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1546 isl_map_free(map1); return -1);
1547 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1548 isl_die(ctx, isl_error_unknown, "expecting single div",
1549 isl_map_free(map1); return -1);
1550 isl_map_free(map1);
1552 if (test_plain_gist(ctx) < 0)
1553 return -1;
1555 return 0;
1558 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1560 isl_set *set, *set2;
1561 int equal;
1562 int one;
1564 set = isl_set_read_from_str(ctx, str);
1565 set = isl_set_coalesce(set);
1566 set2 = isl_set_read_from_str(ctx, str);
1567 equal = isl_set_is_equal(set, set2);
1568 one = set && set->n == 1;
1569 isl_set_free(set);
1570 isl_set_free(set2);
1572 if (equal < 0)
1573 return -1;
1574 if (!equal)
1575 isl_die(ctx, isl_error_unknown,
1576 "coalesced set not equal to input", return -1);
1577 if (check_one && !one)
1578 isl_die(ctx, isl_error_unknown,
1579 "coalesced set should not be a union", return -1);
1581 return 0;
1584 /* Inputs for coalescing tests with unbounded wrapping.
1585 * "str" is a string representation of the input set.
1586 * "single_disjunct" is set if we expect the result to consist of
1587 * a single disjunct.
1589 struct {
1590 int single_disjunct;
1591 const char *str;
1592 } coalesce_unbounded_tests[] = {
1593 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1594 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1595 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1596 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1597 "-10 <= y <= 0}" },
1598 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1599 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1600 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1601 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1604 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1605 * option turned off.
1607 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1609 int i;
1610 int r = 0;
1611 int bounded;
1613 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1614 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1616 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1617 const char *str = coalesce_unbounded_tests[i].str;
1618 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1619 if (test_coalesce_set(ctx, str, check_one) >= 0)
1620 continue;
1621 r = -1;
1622 break;
1625 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1627 return r;
1630 /* Inputs for coalescing tests.
1631 * "str" is a string representation of the input set.
1632 * "single_disjunct" is set if we expect the result to consist of
1633 * a single disjunct.
1635 struct {
1636 int single_disjunct;
1637 const char *str;
1638 } coalesce_tests[] = {
1639 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1640 "y >= x & x >= 2 & 5 >= y }" },
1641 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1642 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1643 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1644 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1645 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1646 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1647 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1648 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1649 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1650 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1651 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1652 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1653 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1654 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1655 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1656 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1657 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1658 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1659 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1660 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1661 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1662 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1663 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1664 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1665 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1666 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1667 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1668 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1669 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1670 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1671 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1672 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1673 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1674 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1675 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1676 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1677 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1678 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1679 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1680 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1681 "[o0, o1, o2, o3, o4, o5, o6]] : "
1682 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1683 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1684 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1685 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1686 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1687 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1688 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1689 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1690 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1691 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1692 "o6 >= i3 + i6 - o3 and M >= 0 and "
1693 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1694 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1695 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1696 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1697 "(o0 = 0 and M >= 2 and N >= 3) or "
1698 "(M = 0 and o0 = 0 and N >= 3) }" },
1699 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1700 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1701 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1702 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1703 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1704 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1705 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1706 "(y = 3 and x = 1) }" },
1707 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1708 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1709 "i1 <= M and i3 <= M and i4 <= M) or "
1710 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1711 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1712 "i4 <= -1 + M) }" },
1713 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1714 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1715 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1716 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1717 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1718 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1719 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1720 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1721 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1722 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1723 { 0, "{ [a, b] : exists e : 2e = a and "
1724 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1725 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1726 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1727 "j >= 1 and j' <= i + j - i' and i >= 1; "
1728 "[1, 1, 1, 1] }" },
1729 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1730 "[i,j] : exists a : j = 3a }" },
1731 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1732 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1733 "a >= 3) or "
1734 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1735 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1736 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1737 "c <= 6 + 8a and a >= 3; "
1738 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1739 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1740 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1741 "[x,0] : 3 <= x <= 4 }" },
1742 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1743 "[x,0] : 4 <= x <= 5 }" },
1744 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1745 "[x,0] : 3 <= x <= 5 }" },
1746 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1747 "[x,0] : 3 <= x <= 4 }" },
1748 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1749 "i1 <= 0; "
1750 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1751 { 1, "{ [0,0]; [1,1] }" },
1752 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1753 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1754 "ii <= k;"
1755 "[k, 0, k] : k <= 6 and k >= 1 }" },
1756 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1757 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1758 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1759 { 1, "[n] -> { [1] : n >= 0;"
1760 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1761 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1762 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1763 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1764 "2e0 <= x and 2e0 <= n);"
1765 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1766 "n >= 0) }" },
1767 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1768 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1769 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1770 "t1 = 1 }" },
1771 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1772 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1773 "[0, 0] }" },
1774 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1775 "t1 >= 13 and t1 <= 16);"
1776 "[t1] : t1 <= 15 and t1 >= 12 }" },
1777 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
1778 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
1779 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
1780 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
1781 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
1782 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
1783 "i <= 4j + 2 }" },
1784 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
1785 "(exists (e0 : 3e0 = -2 + c0)) }" },
1786 { 0, "[n, b0, t0] -> "
1787 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1788 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1789 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1790 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1791 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
1792 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
1793 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
1794 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
1795 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1796 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1797 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1798 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
1799 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
1800 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
1801 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
1802 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
1803 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
1804 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
1805 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
1806 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
1807 { 0, "{ [i0, i1, i2] : "
1808 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
1809 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
1810 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
1811 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
1812 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
1813 "32e0 <= 31 + i0)) or "
1814 "i0 >= 0 }" },
1815 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
1816 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
1817 "2*floor((c)/2) = c and 0 <= a <= 192;"
1818 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
1820 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
1821 "(0 <= a <= b <= n) }" },
1824 /* A specialized coalescing test case that would result
1825 * in a segmentation fault or a failed assertion in earlier versions of isl.
1827 static int test_coalesce_special(struct isl_ctx *ctx)
1829 const char *str;
1830 isl_map *map1, *map2;
1832 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1833 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
1834 "(y = 201 and o1 <= 239 and o1 >= 212) or "
1835 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
1836 "o1 <= 239 and o1 >= 212)) or "
1837 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
1838 "o1 <= 241 and o1 >= 240));"
1839 "[S_L220_OUT[] -> T7[]] -> "
1840 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
1841 "(y = 2 and o1 <= 241 and o1 >= 212) or "
1842 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
1843 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
1844 map1 = isl_map_read_from_str(ctx, str);
1845 map1 = isl_map_align_divs(map1);
1846 map1 = isl_map_coalesce(map1);
1847 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1848 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
1849 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
1850 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
1851 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
1852 map2 = isl_map_read_from_str(ctx, str);
1853 map2 = isl_map_union(map2, map1);
1854 map2 = isl_map_align_divs(map2);
1855 map2 = isl_map_coalesce(map2);
1856 isl_map_free(map2);
1857 if (!map2)
1858 return -1;
1860 return 0;
1863 /* Test the functionality of isl_set_coalesce.
1864 * That is, check that the output is always equal to the input
1865 * and in some cases that the result consists of a single disjunct.
1867 static int test_coalesce(struct isl_ctx *ctx)
1869 int i;
1871 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1872 const char *str = coalesce_tests[i].str;
1873 int check_one = coalesce_tests[i].single_disjunct;
1874 if (test_coalesce_set(ctx, str, check_one) < 0)
1875 return -1;
1878 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1879 return -1;
1880 if (test_coalesce_special(ctx) < 0)
1881 return -1;
1883 return 0;
1886 /* Construct a representation of the graph on the right of Figure 1
1887 * in "Computing the Transitive Closure of a Union of
1888 * Affine Integer Tuple Relations".
1890 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
1892 isl_set *dom;
1893 isl_map *up, *right;
1895 dom = isl_set_read_from_str(ctx,
1896 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1897 "2 x - 3 y + 3 >= 0 }");
1898 right = isl_map_read_from_str(ctx,
1899 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1900 up = isl_map_read_from_str(ctx,
1901 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1902 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1903 right = isl_map_intersect_range(right, isl_set_copy(dom));
1904 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1905 up = isl_map_intersect_range(up, dom);
1906 return isl_map_union(up, right);
1909 /* Construct a representation of the power of the graph
1910 * on the right of Figure 1 in "Computing the Transitive Closure of
1911 * a Union of Affine Integer Tuple Relations".
1913 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
1915 return isl_map_read_from_str(ctx,
1916 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
1917 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
1918 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
1921 /* Construct a representation of the transitive closure of the graph
1922 * on the right of Figure 1 in "Computing the Transitive Closure of
1923 * a Union of Affine Integer Tuple Relations".
1925 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
1927 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
1930 static int test_closure(isl_ctx *ctx)
1932 const char *str;
1933 isl_map *map, *map2;
1934 int exact, equal;
1936 /* COCOA example 1 */
1937 map = isl_map_read_from_str(ctx,
1938 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1939 "1 <= i and i < n and 1 <= j and j < n or "
1940 "i2 = i + 1 and j2 = j - 1 and "
1941 "1 <= i and i < n and 2 <= j and j <= n }");
1942 map = isl_map_power(map, &exact);
1943 assert(exact);
1944 isl_map_free(map);
1946 /* COCOA example 1 */
1947 map = isl_map_read_from_str(ctx,
1948 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1949 "1 <= i and i < n and 1 <= j and j < n or "
1950 "i2 = i + 1 and j2 = j - 1 and "
1951 "1 <= i and i < n and 2 <= j and j <= n }");
1952 map = isl_map_transitive_closure(map, &exact);
1953 assert(exact);
1954 map2 = isl_map_read_from_str(ctx,
1955 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1956 "1 <= i and i < n and 1 <= j and j <= n and "
1957 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1958 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1959 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1960 assert(isl_map_is_equal(map, map2));
1961 isl_map_free(map2);
1962 isl_map_free(map);
1964 map = isl_map_read_from_str(ctx,
1965 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1966 " 0 <= y and y <= n }");
1967 map = isl_map_transitive_closure(map, &exact);
1968 map2 = isl_map_read_from_str(ctx,
1969 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1970 " 0 <= y and y <= n }");
1971 assert(isl_map_is_equal(map, map2));
1972 isl_map_free(map2);
1973 isl_map_free(map);
1975 /* COCOA example 2 */
1976 map = isl_map_read_from_str(ctx,
1977 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1978 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1979 "i2 = i + 2 and j2 = j - 2 and "
1980 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1981 map = isl_map_transitive_closure(map, &exact);
1982 assert(exact);
1983 map2 = isl_map_read_from_str(ctx,
1984 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1985 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1986 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1987 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1988 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1989 assert(isl_map_is_equal(map, map2));
1990 isl_map_free(map);
1991 isl_map_free(map2);
1993 /* COCOA Fig.2 left */
1994 map = isl_map_read_from_str(ctx,
1995 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1996 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1997 "j <= n or "
1998 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1999 "j <= 2 i - 3 and j <= n - 2 or "
2000 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2001 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2002 map = isl_map_transitive_closure(map, &exact);
2003 assert(exact);
2004 isl_map_free(map);
2006 /* COCOA Fig.2 right */
2007 map = isl_map_read_from_str(ctx,
2008 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2009 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2010 "j <= n or "
2011 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2012 "j <= 2 i - 4 and j <= n - 3 or "
2013 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2014 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2015 map = isl_map_power(map, &exact);
2016 assert(exact);
2017 isl_map_free(map);
2019 /* COCOA Fig.2 right */
2020 map = isl_map_read_from_str(ctx,
2021 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2022 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2023 "j <= n or "
2024 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2025 "j <= 2 i - 4 and j <= n - 3 or "
2026 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2027 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2028 map = isl_map_transitive_closure(map, &exact);
2029 assert(exact);
2030 map2 = isl_map_read_from_str(ctx,
2031 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2032 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2033 "j <= n and 3 + i + 2 j <= 3 n and "
2034 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2035 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2036 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2037 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2038 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2039 assert(isl_map_is_equal(map, map2));
2040 isl_map_free(map2);
2041 isl_map_free(map);
2043 map = cocoa_fig_1_right_graph(ctx);
2044 map = isl_map_transitive_closure(map, &exact);
2045 assert(exact);
2046 map2 = cocoa_fig_1_right_tc(ctx);
2047 assert(isl_map_is_equal(map, map2));
2048 isl_map_free(map2);
2049 isl_map_free(map);
2051 map = cocoa_fig_1_right_graph(ctx);
2052 map = isl_map_power(map, &exact);
2053 map2 = cocoa_fig_1_right_power(ctx);
2054 equal = isl_map_is_equal(map, map2);
2055 isl_map_free(map2);
2056 isl_map_free(map);
2057 if (equal < 0)
2058 return -1;
2059 if (!exact)
2060 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2061 if (!equal)
2062 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2064 /* COCOA Theorem 1 counter example */
2065 map = isl_map_read_from_str(ctx,
2066 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2067 "i2 = 1 and j2 = j or "
2068 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2069 map = isl_map_transitive_closure(map, &exact);
2070 assert(exact);
2071 isl_map_free(map);
2073 map = isl_map_read_from_str(ctx,
2074 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2075 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2076 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2077 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2078 map = isl_map_transitive_closure(map, &exact);
2079 assert(exact);
2080 isl_map_free(map);
2082 /* Kelly et al 1996, fig 12 */
2083 map = isl_map_read_from_str(ctx,
2084 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2085 "1 <= i,j,j+1 <= n or "
2086 "j = n and j2 = 1 and i2 = i + 1 and "
2087 "1 <= i,i+1 <= n }");
2088 map = isl_map_transitive_closure(map, &exact);
2089 assert(exact);
2090 map2 = isl_map_read_from_str(ctx,
2091 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2092 "1 <= i <= n and i = i2 or "
2093 "1 <= i < i2 <= n and 1 <= j <= n and "
2094 "1 <= j2 <= n }");
2095 assert(isl_map_is_equal(map, map2));
2096 isl_map_free(map2);
2097 isl_map_free(map);
2099 /* Omega's closure4 */
2100 map = isl_map_read_from_str(ctx,
2101 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2102 "1 <= x,y <= 10 or "
2103 "x2 = x + 1 and y2 = y and "
2104 "1 <= x <= 20 && 5 <= y <= 15 }");
2105 map = isl_map_transitive_closure(map, &exact);
2106 assert(exact);
2107 isl_map_free(map);
2109 map = isl_map_read_from_str(ctx,
2110 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2111 map = isl_map_transitive_closure(map, &exact);
2112 assert(!exact);
2113 map2 = isl_map_read_from_str(ctx,
2114 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2115 assert(isl_map_is_equal(map, map2));
2116 isl_map_free(map);
2117 isl_map_free(map2);
2119 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2120 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2121 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2122 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2123 map = isl_map_read_from_str(ctx, str);
2124 map = isl_map_transitive_closure(map, &exact);
2125 assert(exact);
2126 map2 = isl_map_read_from_str(ctx, str);
2127 assert(isl_map_is_equal(map, map2));
2128 isl_map_free(map);
2129 isl_map_free(map2);
2131 str = "{[0] -> [1]; [2] -> [3]}";
2132 map = isl_map_read_from_str(ctx, str);
2133 map = isl_map_transitive_closure(map, &exact);
2134 assert(exact);
2135 map2 = isl_map_read_from_str(ctx, str);
2136 assert(isl_map_is_equal(map, map2));
2137 isl_map_free(map);
2138 isl_map_free(map2);
2140 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2141 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2142 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2143 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2144 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2145 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2146 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2147 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2148 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2149 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2150 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2151 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2152 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2153 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2154 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2155 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2156 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2157 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2158 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2159 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2160 map = isl_map_read_from_str(ctx, str);
2161 map = isl_map_transitive_closure(map, NULL);
2162 assert(map);
2163 isl_map_free(map);
2165 return 0;
2168 static int test_lex(struct isl_ctx *ctx)
2170 isl_space *dim;
2171 isl_map *map;
2172 int empty;
2174 dim = isl_space_set_alloc(ctx, 0, 0);
2175 map = isl_map_lex_le(dim);
2176 empty = isl_map_is_empty(map);
2177 isl_map_free(map);
2179 if (empty < 0)
2180 return -1;
2181 if (empty)
2182 isl_die(ctx, isl_error_unknown,
2183 "expecting non-empty result", return -1);
2185 return 0;
2188 static int test_lexmin(struct isl_ctx *ctx)
2190 int equal;
2191 const char *str;
2192 isl_basic_map *bmap;
2193 isl_map *map, *map2;
2194 isl_set *set;
2195 isl_set *set2;
2196 isl_pw_multi_aff *pma;
2198 str = "[p0, p1] -> { [] -> [] : "
2199 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2200 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2201 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2202 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2203 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2204 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2205 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2206 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2207 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2208 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2209 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2210 map = isl_map_read_from_str(ctx, str);
2211 map = isl_map_lexmin(map);
2212 isl_map_free(map);
2214 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2215 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2216 set = isl_set_read_from_str(ctx, str);
2217 set = isl_set_lexmax(set);
2218 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2219 set2 = isl_set_read_from_str(ctx, str);
2220 set = isl_set_intersect(set, set2);
2221 assert(!isl_set_is_empty(set));
2222 isl_set_free(set);
2224 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
2225 map = isl_map_read_from_str(ctx, str);
2226 map = isl_map_lexmin(map);
2227 str = "{ [x] -> [5] : 6 <= x <= 8; "
2228 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
2229 map2 = isl_map_read_from_str(ctx, str);
2230 assert(isl_map_is_equal(map, map2));
2231 isl_map_free(map);
2232 isl_map_free(map2);
2234 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
2235 map = isl_map_read_from_str(ctx, str);
2236 map2 = isl_map_copy(map);
2237 map = isl_map_lexmin(map);
2238 assert(isl_map_is_equal(map, map2));
2239 isl_map_free(map);
2240 isl_map_free(map2);
2242 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
2243 map = isl_map_read_from_str(ctx, str);
2244 map = isl_map_lexmin(map);
2245 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
2246 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2247 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2248 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
2249 map2 = isl_map_read_from_str(ctx, str);
2250 assert(isl_map_is_equal(map, map2));
2251 isl_map_free(map);
2252 isl_map_free(map2);
2254 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2255 " 8i' <= i and 8i' >= -7 + i }";
2256 bmap = isl_basic_map_read_from_str(ctx, str);
2257 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2258 map2 = isl_map_from_pw_multi_aff(pma);
2259 map = isl_map_from_basic_map(bmap);
2260 assert(isl_map_is_equal(map, map2));
2261 isl_map_free(map);
2262 isl_map_free(map2);
2264 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
2265 map = isl_map_read_from_str(ctx, str);
2266 map = isl_map_lexmin(map);
2267 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
2268 map2 = isl_map_read_from_str(ctx, str);
2269 assert(isl_map_is_equal(map, map2));
2270 isl_map_free(map);
2271 isl_map_free(map2);
2273 /* Check that empty pieces are properly combined. */
2274 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2275 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2276 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
2277 map = isl_map_read_from_str(ctx, str);
2278 map = isl_map_lexmin(map);
2279 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2280 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2281 "x >= 4 }";
2282 map2 = isl_map_read_from_str(ctx, str);
2283 assert(isl_map_is_equal(map, map2));
2284 isl_map_free(map);
2285 isl_map_free(map2);
2287 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2288 " 8i' <= i and 8i' >= -7 + i }";
2289 set = isl_set_read_from_str(ctx, str);
2290 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2291 set2 = isl_set_from_pw_multi_aff(pma);
2292 equal = isl_set_is_equal(set, set2);
2293 isl_set_free(set);
2294 isl_set_free(set2);
2295 if (equal < 0)
2296 return -1;
2297 if (!equal)
2298 isl_die(ctx, isl_error_unknown,
2299 "unexpected difference between set and "
2300 "piecewise affine expression", return -1);
2302 return 0;
2305 /* A specialized isl_set_min_val test case that would return the wrong result
2306 * in earlier versions of isl.
2307 * The explicit call to isl_basic_set_union prevents the second basic set
2308 * from being determined to be empty prior to the call to isl_set_min_val,
2309 * at least at the point where this test case was introduced.
2311 static int test_min_special(isl_ctx *ctx)
2313 const char *str;
2314 isl_basic_set *bset1, *bset2;
2315 isl_set *set;
2316 isl_aff *obj;
2317 isl_val *res;
2318 int ok;
2320 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
2321 bset1 = isl_basic_set_read_from_str(ctx, str);
2322 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
2323 bset2 = isl_basic_set_read_from_str(ctx, str);
2324 set = isl_basic_set_union(bset1, bset2);
2325 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
2327 res = isl_set_min_val(set, obj);
2328 ok = isl_val_cmp_si(res, 5) == 0;
2330 isl_aff_free(obj);
2331 isl_set_free(set);
2332 isl_val_free(res);
2334 if (!res)
2335 return -1;
2336 if (!ok)
2337 isl_die(ctx, isl_error_unknown, "unexpected minimum",
2338 return -1);
2340 return 0;
2343 struct {
2344 const char *set;
2345 const char *obj;
2346 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2347 __isl_keep isl_aff *obj);
2348 const char *res;
2349 } opt_tests[] = {
2350 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2351 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2352 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2353 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2354 &isl_set_max_val, "30" },
2358 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2359 * In particular, check the results on non-convex inputs.
2361 static int test_min(struct isl_ctx *ctx)
2363 int i;
2364 isl_set *set;
2365 isl_aff *obj;
2366 isl_val *val, *res;
2367 isl_bool ok;
2369 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2370 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2371 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2372 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2373 val = opt_tests[i].fn(set, obj);
2374 ok = isl_val_eq(res, val);
2375 isl_val_free(res);
2376 isl_val_free(val);
2377 isl_aff_free(obj);
2378 isl_set_free(set);
2380 if (ok < 0)
2381 return -1;
2382 if (!ok)
2383 isl_die(ctx, isl_error_unknown,
2384 "unexpected optimum", return -1);
2387 if (test_min_special(ctx) < 0)
2388 return -1;
2390 return 0;
2393 struct must_may {
2394 isl_map *must;
2395 isl_map *may;
2398 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2399 void *dep_user, void *user)
2401 struct must_may *mm = (struct must_may *)user;
2403 if (must)
2404 mm->must = isl_map_union(mm->must, dep);
2405 else
2406 mm->may = isl_map_union(mm->may, dep);
2408 return isl_stat_ok;
2411 static int common_space(void *first, void *second)
2413 int depth = *(int *)first;
2414 return 2 * depth;
2417 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2419 isl_map *map2;
2420 int equal;
2422 if (!map)
2423 return -1;
2425 map2 = isl_map_read_from_str(map->ctx, str);
2426 equal = isl_map_is_equal(map, map2);
2427 isl_map_free(map2);
2429 return equal;
2432 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2434 int equal;
2436 equal = map_is_equal(map, str);
2437 if (equal < 0)
2438 return -1;
2439 if (!equal)
2440 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2441 "result not as expected", return -1);
2442 return 0;
2445 static int test_dep(struct isl_ctx *ctx)
2447 const char *str;
2448 isl_space *dim;
2449 isl_map *map;
2450 isl_access_info *ai;
2451 isl_flow *flow;
2452 int depth;
2453 struct must_may mm;
2455 depth = 3;
2457 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2458 map = isl_map_read_from_str(ctx, str);
2459 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2461 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2462 map = isl_map_read_from_str(ctx, str);
2463 ai = isl_access_info_add_source(ai, map, 1, &depth);
2465 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2466 map = isl_map_read_from_str(ctx, str);
2467 ai = isl_access_info_add_source(ai, map, 1, &depth);
2469 flow = isl_access_info_compute_flow(ai);
2470 dim = isl_space_alloc(ctx, 0, 3, 3);
2471 mm.must = isl_map_empty(isl_space_copy(dim));
2472 mm.may = isl_map_empty(dim);
2474 isl_flow_foreach(flow, collect_must_may, &mm);
2476 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2477 " [1,10,0] -> [2,5,0] }";
2478 assert(map_is_equal(mm.must, str));
2479 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2480 assert(map_is_equal(mm.may, str));
2482 isl_map_free(mm.must);
2483 isl_map_free(mm.may);
2484 isl_flow_free(flow);
2487 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2488 map = isl_map_read_from_str(ctx, str);
2489 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2491 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2492 map = isl_map_read_from_str(ctx, str);
2493 ai = isl_access_info_add_source(ai, map, 1, &depth);
2495 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2496 map = isl_map_read_from_str(ctx, str);
2497 ai = isl_access_info_add_source(ai, map, 0, &depth);
2499 flow = isl_access_info_compute_flow(ai);
2500 dim = isl_space_alloc(ctx, 0, 3, 3);
2501 mm.must = isl_map_empty(isl_space_copy(dim));
2502 mm.may = isl_map_empty(dim);
2504 isl_flow_foreach(flow, collect_must_may, &mm);
2506 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2507 assert(map_is_equal(mm.must, str));
2508 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2509 assert(map_is_equal(mm.may, str));
2511 isl_map_free(mm.must);
2512 isl_map_free(mm.may);
2513 isl_flow_free(flow);
2516 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2517 map = isl_map_read_from_str(ctx, str);
2518 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2520 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2521 map = isl_map_read_from_str(ctx, str);
2522 ai = isl_access_info_add_source(ai, map, 0, &depth);
2524 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2525 map = isl_map_read_from_str(ctx, str);
2526 ai = isl_access_info_add_source(ai, map, 0, &depth);
2528 flow = isl_access_info_compute_flow(ai);
2529 dim = isl_space_alloc(ctx, 0, 3, 3);
2530 mm.must = isl_map_empty(isl_space_copy(dim));
2531 mm.may = isl_map_empty(dim);
2533 isl_flow_foreach(flow, collect_must_may, &mm);
2535 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2536 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2537 assert(map_is_equal(mm.may, str));
2538 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2539 assert(map_is_equal(mm.must, str));
2541 isl_map_free(mm.must);
2542 isl_map_free(mm.may);
2543 isl_flow_free(flow);
2546 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2547 map = isl_map_read_from_str(ctx, str);
2548 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2550 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2551 map = isl_map_read_from_str(ctx, str);
2552 ai = isl_access_info_add_source(ai, map, 0, &depth);
2554 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2555 map = isl_map_read_from_str(ctx, str);
2556 ai = isl_access_info_add_source(ai, map, 0, &depth);
2558 flow = isl_access_info_compute_flow(ai);
2559 dim = isl_space_alloc(ctx, 0, 3, 3);
2560 mm.must = isl_map_empty(isl_space_copy(dim));
2561 mm.may = isl_map_empty(dim);
2563 isl_flow_foreach(flow, collect_must_may, &mm);
2565 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2566 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2567 assert(map_is_equal(mm.may, str));
2568 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2569 assert(map_is_equal(mm.must, str));
2571 isl_map_free(mm.must);
2572 isl_map_free(mm.may);
2573 isl_flow_free(flow);
2576 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2577 map = isl_map_read_from_str(ctx, str);
2578 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2580 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2581 map = isl_map_read_from_str(ctx, str);
2582 ai = isl_access_info_add_source(ai, map, 0, &depth);
2584 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2585 map = isl_map_read_from_str(ctx, str);
2586 ai = isl_access_info_add_source(ai, map, 0, &depth);
2588 flow = isl_access_info_compute_flow(ai);
2589 dim = isl_space_alloc(ctx, 0, 3, 3);
2590 mm.must = isl_map_empty(isl_space_copy(dim));
2591 mm.may = isl_map_empty(dim);
2593 isl_flow_foreach(flow, collect_must_may, &mm);
2595 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2596 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2597 assert(map_is_equal(mm.may, str));
2598 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2599 assert(map_is_equal(mm.must, str));
2601 isl_map_free(mm.must);
2602 isl_map_free(mm.may);
2603 isl_flow_free(flow);
2606 depth = 5;
2608 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2609 map = isl_map_read_from_str(ctx, str);
2610 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2612 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2613 map = isl_map_read_from_str(ctx, str);
2614 ai = isl_access_info_add_source(ai, map, 1, &depth);
2616 flow = isl_access_info_compute_flow(ai);
2617 dim = isl_space_alloc(ctx, 0, 5, 5);
2618 mm.must = isl_map_empty(isl_space_copy(dim));
2619 mm.may = isl_map_empty(dim);
2621 isl_flow_foreach(flow, collect_must_may, &mm);
2623 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2624 assert(map_is_equal(mm.must, str));
2625 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2626 assert(map_is_equal(mm.may, str));
2628 isl_map_free(mm.must);
2629 isl_map_free(mm.may);
2630 isl_flow_free(flow);
2632 return 0;
2635 /* Check that the dependence analysis proceeds without errors.
2636 * Earlier versions of isl would break down during the analysis
2637 * due to the use of the wrong spaces.
2639 static int test_flow(isl_ctx *ctx)
2641 const char *str;
2642 isl_union_map *access, *schedule;
2643 isl_union_map *must_dep, *may_dep;
2644 int r;
2646 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2647 access = isl_union_map_read_from_str(ctx, str);
2648 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2649 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2650 "S2[] -> [1,0,0,0]; "
2651 "S3[] -> [-1,0,0,0] }";
2652 schedule = isl_union_map_read_from_str(ctx, str);
2653 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2654 isl_union_map_copy(access), schedule,
2655 &must_dep, &may_dep, NULL, NULL);
2656 isl_union_map_free(may_dep);
2657 isl_union_map_free(must_dep);
2659 return r;
2662 struct {
2663 const char *map;
2664 int sv;
2665 } sv_tests[] = {
2666 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2667 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2668 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2669 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2670 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2671 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2672 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2673 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2674 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2677 int test_sv(isl_ctx *ctx)
2679 isl_union_map *umap;
2680 int i;
2681 int sv;
2683 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2684 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2685 sv = isl_union_map_is_single_valued(umap);
2686 isl_union_map_free(umap);
2687 if (sv < 0)
2688 return -1;
2689 if (sv_tests[i].sv && !sv)
2690 isl_die(ctx, isl_error_internal,
2691 "map not detected as single valued", return -1);
2692 if (!sv_tests[i].sv && sv)
2693 isl_die(ctx, isl_error_internal,
2694 "map detected as single valued", return -1);
2697 return 0;
2700 struct {
2701 const char *str;
2702 int bijective;
2703 } bijective_tests[] = {
2704 { "[N,M]->{[i,j] -> [i]}", 0 },
2705 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
2706 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
2707 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
2708 { "[N,M]->{[i,j] -> [j,i]}", 1 },
2709 { "[N,M]->{[i,j] -> [i+j]}", 0 },
2710 { "[N,M]->{[i,j] -> []}", 0 },
2711 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
2712 { "[N,M]->{[i,j] -> [2i]}", 0 },
2713 { "[N,M]->{[i,j] -> [i,i]}", 0 },
2714 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
2715 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
2716 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
2719 static int test_bijective(struct isl_ctx *ctx)
2721 isl_map *map;
2722 int i;
2723 int bijective;
2725 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
2726 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
2727 bijective = isl_map_is_bijective(map);
2728 isl_map_free(map);
2729 if (bijective < 0)
2730 return -1;
2731 if (bijective_tests[i].bijective && !bijective)
2732 isl_die(ctx, isl_error_internal,
2733 "map not detected as bijective", return -1);
2734 if (!bijective_tests[i].bijective && bijective)
2735 isl_die(ctx, isl_error_internal,
2736 "map detected as bijective", return -1);
2739 return 0;
2742 /* Inputs for isl_pw_qpolynomial_gist tests.
2743 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2745 struct {
2746 const char *pwqp;
2747 const char *set;
2748 const char *gist;
2749 } pwqp_gist_tests[] = {
2750 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2751 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2752 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2753 "{ [i] -> -1/2 + 1/2 * i }" },
2754 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2757 static int test_pwqp(struct isl_ctx *ctx)
2759 int i;
2760 const char *str;
2761 isl_set *set;
2762 isl_pw_qpolynomial *pwqp1, *pwqp2;
2763 int equal;
2765 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2766 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2768 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2769 isl_dim_in, 1, 1);
2771 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2772 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2774 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2776 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2778 isl_pw_qpolynomial_free(pwqp1);
2780 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2781 str = pwqp_gist_tests[i].pwqp;
2782 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2783 str = pwqp_gist_tests[i].set;
2784 set = isl_set_read_from_str(ctx, str);
2785 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2786 str = pwqp_gist_tests[i].gist;
2787 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2788 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2789 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2790 isl_pw_qpolynomial_free(pwqp1);
2792 if (equal < 0)
2793 return -1;
2794 if (!equal)
2795 isl_die(ctx, isl_error_unknown,
2796 "unexpected result", return -1);
2799 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2800 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2801 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2802 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2804 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2806 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2808 isl_pw_qpolynomial_free(pwqp1);
2810 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2811 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2812 str = "{ [x] -> x }";
2813 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2815 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2817 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2819 isl_pw_qpolynomial_free(pwqp1);
2821 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2822 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2823 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2824 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2825 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2826 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2827 isl_pw_qpolynomial_free(pwqp1);
2829 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2830 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2831 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2832 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2833 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2834 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2835 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2836 isl_pw_qpolynomial_free(pwqp1);
2837 isl_pw_qpolynomial_free(pwqp2);
2838 if (equal < 0)
2839 return -1;
2840 if (!equal)
2841 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2843 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2844 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2845 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2846 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2847 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2848 isl_val_one(ctx));
2849 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2850 isl_pw_qpolynomial_free(pwqp1);
2851 isl_pw_qpolynomial_free(pwqp2);
2852 if (equal < 0)
2853 return -1;
2854 if (!equal)
2855 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2857 return 0;
2860 static int test_split_periods(isl_ctx *ctx)
2862 const char *str;
2863 isl_pw_qpolynomial *pwqp;
2865 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2866 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2867 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2868 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2870 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2872 isl_pw_qpolynomial_free(pwqp);
2874 if (!pwqp)
2875 return -1;
2877 return 0;
2880 static int test_union(isl_ctx *ctx)
2882 const char *str;
2883 isl_union_set *uset1, *uset2;
2884 isl_union_map *umap1, *umap2;
2885 int equal;
2887 str = "{ [i] : 0 <= i <= 1 }";
2888 uset1 = isl_union_set_read_from_str(ctx, str);
2889 str = "{ [1] -> [0] }";
2890 umap1 = isl_union_map_read_from_str(ctx, str);
2892 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2893 equal = isl_union_map_is_equal(umap1, umap2);
2895 isl_union_map_free(umap1);
2896 isl_union_map_free(umap2);
2898 if (equal < 0)
2899 return -1;
2900 if (!equal)
2901 isl_die(ctx, isl_error_unknown, "union maps not equal",
2902 return -1);
2904 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2905 umap1 = isl_union_map_read_from_str(ctx, str);
2906 str = "{ A[i]; B[i] }";
2907 uset1 = isl_union_set_read_from_str(ctx, str);
2909 uset2 = isl_union_map_domain(umap1);
2911 equal = isl_union_set_is_equal(uset1, uset2);
2913 isl_union_set_free(uset1);
2914 isl_union_set_free(uset2);
2916 if (equal < 0)
2917 return -1;
2918 if (!equal)
2919 isl_die(ctx, isl_error_unknown, "union sets not equal",
2920 return -1);
2922 return 0;
2925 /* Check that computing a bound of a non-zero polynomial over an unbounded
2926 * domain does not produce a rational value.
2927 * In particular, check that the upper bound is infinity.
2929 static int test_bound_unbounded_domain(isl_ctx *ctx)
2931 const char *str;
2932 isl_pw_qpolynomial *pwqp;
2933 isl_pw_qpolynomial_fold *pwf, *pwf2;
2934 isl_bool equal;
2936 str = "{ [m,n] -> -m * n }";
2937 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2938 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2939 str = "{ infty }";
2940 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2941 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2942 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
2943 isl_pw_qpolynomial_fold_free(pwf);
2944 isl_pw_qpolynomial_fold_free(pwf2);
2946 if (equal < 0)
2947 return -1;
2948 if (!equal)
2949 isl_die(ctx, isl_error_unknown,
2950 "expecting infinite polynomial bound", return -1);
2952 return 0;
2955 static int test_bound(isl_ctx *ctx)
2957 const char *str;
2958 unsigned dim;
2959 isl_pw_qpolynomial *pwqp;
2960 isl_pw_qpolynomial_fold *pwf;
2962 if (test_bound_unbounded_domain(ctx) < 0)
2963 return -1;
2965 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2966 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2967 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2968 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
2969 isl_pw_qpolynomial_fold_free(pwf);
2970 if (dim != 4)
2971 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
2972 return -1);
2974 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2975 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2976 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2977 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
2978 isl_pw_qpolynomial_fold_free(pwf);
2979 if (dim != 1)
2980 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
2981 return -1);
2983 return 0;
2986 static int test_lift(isl_ctx *ctx)
2988 const char *str;
2989 isl_basic_map *bmap;
2990 isl_basic_set *bset;
2992 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2993 bset = isl_basic_set_read_from_str(ctx, str);
2994 bset = isl_basic_set_lift(bset);
2995 bmap = isl_basic_map_from_range(bset);
2996 bset = isl_basic_map_domain(bmap);
2997 isl_basic_set_free(bset);
2999 return 0;
3002 struct {
3003 const char *set1;
3004 const char *set2;
3005 int subset;
3006 } subset_tests[] = {
3007 { "{ [112, 0] }",
3008 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
3009 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
3010 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
3011 { "{ [65] }",
3012 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
3013 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
3014 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
3015 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
3016 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
3017 "256e0 <= 255i and 256e0 >= -255 + 255i and "
3018 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
3019 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
3020 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
3021 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
3022 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
3023 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
3024 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
3025 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
3026 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
3027 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
3028 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
3029 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
3030 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
3031 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
3032 "4e0 <= -57 + i0 + i1)) or "
3033 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
3034 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
3035 "4e0 >= -61 + i0 + i1)) or "
3036 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
3037 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
3040 static int test_subset(isl_ctx *ctx)
3042 int i;
3043 isl_set *set1, *set2;
3044 int subset;
3046 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3047 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3048 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3049 subset = isl_set_is_subset(set1, set2);
3050 isl_set_free(set1);
3051 isl_set_free(set2);
3052 if (subset < 0)
3053 return -1;
3054 if (subset != subset_tests[i].subset)
3055 isl_die(ctx, isl_error_unknown,
3056 "incorrect subset result", return -1);
3059 return 0;
3062 struct {
3063 const char *minuend;
3064 const char *subtrahend;
3065 const char *difference;
3066 } subtract_domain_tests[] = {
3067 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3068 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3069 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3072 static int test_subtract(isl_ctx *ctx)
3074 int i;
3075 isl_union_map *umap1, *umap2;
3076 isl_union_pw_multi_aff *upma1, *upma2;
3077 isl_union_set *uset;
3078 int equal;
3080 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3081 umap1 = isl_union_map_read_from_str(ctx,
3082 subtract_domain_tests[i].minuend);
3083 uset = isl_union_set_read_from_str(ctx,
3084 subtract_domain_tests[i].subtrahend);
3085 umap2 = isl_union_map_read_from_str(ctx,
3086 subtract_domain_tests[i].difference);
3087 umap1 = isl_union_map_subtract_domain(umap1, uset);
3088 equal = isl_union_map_is_equal(umap1, umap2);
3089 isl_union_map_free(umap1);
3090 isl_union_map_free(umap2);
3091 if (equal < 0)
3092 return -1;
3093 if (!equal)
3094 isl_die(ctx, isl_error_unknown,
3095 "incorrect subtract domain result", return -1);
3098 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3099 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3100 subtract_domain_tests[i].minuend);
3101 uset = isl_union_set_read_from_str(ctx,
3102 subtract_domain_tests[i].subtrahend);
3103 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3104 subtract_domain_tests[i].difference);
3105 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3106 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3107 isl_union_pw_multi_aff_free(upma1);
3108 isl_union_pw_multi_aff_free(upma2);
3109 if (equal < 0)
3110 return -1;
3111 if (!equal)
3112 isl_die(ctx, isl_error_unknown,
3113 "incorrect subtract domain result", return -1);
3116 return 0;
3119 /* Check that intersecting the empty basic set with another basic set
3120 * does not increase the number of constraints. In particular,
3121 * the empty basic set should maintain its canonical representation.
3123 static int test_intersect(isl_ctx *ctx)
3125 int n1, n2;
3126 isl_basic_set *bset1, *bset2;
3128 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3129 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3130 n1 = isl_basic_set_n_constraint(bset1);
3131 bset1 = isl_basic_set_intersect(bset1, bset2);
3132 n2 = isl_basic_set_n_constraint(bset1);
3133 isl_basic_set_free(bset1);
3134 if (!bset1)
3135 return -1;
3136 if (n1 != n2)
3137 isl_die(ctx, isl_error_unknown,
3138 "number of constraints of empty set changed",
3139 return -1);
3141 return 0;
3144 int test_factorize(isl_ctx *ctx)
3146 const char *str;
3147 isl_basic_set *bset;
3148 isl_factorizer *f;
3150 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3151 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3152 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3153 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3154 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3155 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3156 "3i5 >= -2i0 - i2 + 3i4 }";
3157 bset = isl_basic_set_read_from_str(ctx, str);
3158 f = isl_basic_set_factorizer(bset);
3159 isl_basic_set_free(bset);
3160 isl_factorizer_free(f);
3161 if (!f)
3162 isl_die(ctx, isl_error_unknown,
3163 "failed to construct factorizer", return -1);
3165 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3166 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3167 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3168 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3169 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3170 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3171 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3172 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3173 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3174 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3175 bset = isl_basic_set_read_from_str(ctx, str);
3176 f = isl_basic_set_factorizer(bset);
3177 isl_basic_set_free(bset);
3178 isl_factorizer_free(f);
3179 if (!f)
3180 isl_die(ctx, isl_error_unknown,
3181 "failed to construct factorizer", return -1);
3183 return 0;
3186 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3188 int *injective = user;
3190 *injective = isl_map_is_injective(map);
3191 isl_map_free(map);
3193 if (*injective < 0 || !*injective)
3194 return isl_stat_error;
3196 return isl_stat_ok;
3199 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3200 const char *r, const char *s, int tilable, int parallel)
3202 int i;
3203 isl_union_set *D;
3204 isl_union_map *W, *R, *S;
3205 isl_union_map *empty;
3206 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3207 isl_union_map *validity, *proximity, *coincidence;
3208 isl_union_map *schedule;
3209 isl_union_map *test;
3210 isl_union_set *delta;
3211 isl_union_set *domain;
3212 isl_set *delta_set;
3213 isl_set *slice;
3214 isl_set *origin;
3215 isl_schedule_constraints *sc;
3216 isl_schedule *sched;
3217 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3219 D = isl_union_set_read_from_str(ctx, d);
3220 W = isl_union_map_read_from_str(ctx, w);
3221 R = isl_union_map_read_from_str(ctx, r);
3222 S = isl_union_map_read_from_str(ctx, s);
3224 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3225 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3227 empty = isl_union_map_empty(isl_union_map_get_space(S));
3228 isl_union_map_compute_flow(isl_union_map_copy(R),
3229 isl_union_map_copy(W), empty,
3230 isl_union_map_copy(S),
3231 &dep_raw, NULL, NULL, NULL);
3232 isl_union_map_compute_flow(isl_union_map_copy(W),
3233 isl_union_map_copy(W),
3234 isl_union_map_copy(R),
3235 isl_union_map_copy(S),
3236 &dep_waw, &dep_war, NULL, NULL);
3238 dep = isl_union_map_union(dep_waw, dep_war);
3239 dep = isl_union_map_union(dep, dep_raw);
3240 validity = isl_union_map_copy(dep);
3241 coincidence = isl_union_map_copy(dep);
3242 proximity = isl_union_map_copy(dep);
3244 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3245 sc = isl_schedule_constraints_set_validity(sc, validity);
3246 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3247 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3248 sched = isl_schedule_constraints_compute_schedule(sc);
3249 schedule = isl_schedule_get_map(sched);
3250 isl_schedule_free(sched);
3251 isl_union_map_free(W);
3252 isl_union_map_free(R);
3253 isl_union_map_free(S);
3255 is_injection = 1;
3256 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3258 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3259 is_complete = isl_union_set_is_subset(D, domain);
3260 isl_union_set_free(D);
3261 isl_union_set_free(domain);
3263 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3264 test = isl_union_map_apply_range(test, dep);
3265 test = isl_union_map_apply_range(test, schedule);
3267 delta = isl_union_map_deltas(test);
3268 if (isl_union_set_n_set(delta) == 0) {
3269 is_tilable = 1;
3270 is_parallel = 1;
3271 is_nonneg = 1;
3272 isl_union_set_free(delta);
3273 } else {
3274 delta_set = isl_set_from_union_set(delta);
3276 slice = isl_set_universe(isl_set_get_space(delta_set));
3277 for (i = 0; i < tilable; ++i)
3278 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3279 is_tilable = isl_set_is_subset(delta_set, slice);
3280 isl_set_free(slice);
3282 slice = isl_set_universe(isl_set_get_space(delta_set));
3283 for (i = 0; i < parallel; ++i)
3284 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3285 is_parallel = isl_set_is_subset(delta_set, slice);
3286 isl_set_free(slice);
3288 origin = isl_set_universe(isl_set_get_space(delta_set));
3289 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
3290 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3292 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3293 delta_set = isl_set_lexmin(delta_set);
3295 is_nonneg = isl_set_is_equal(delta_set, origin);
3297 isl_set_free(origin);
3298 isl_set_free(delta_set);
3301 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3302 is_injection < 0 || is_complete < 0)
3303 return -1;
3304 if (!is_complete)
3305 isl_die(ctx, isl_error_unknown,
3306 "generated schedule incomplete", return -1);
3307 if (!is_injection)
3308 isl_die(ctx, isl_error_unknown,
3309 "generated schedule not injective on each statement",
3310 return -1);
3311 if (!is_nonneg)
3312 isl_die(ctx, isl_error_unknown,
3313 "negative dependences in generated schedule",
3314 return -1);
3315 if (!is_tilable)
3316 isl_die(ctx, isl_error_unknown,
3317 "generated schedule not as tilable as expected",
3318 return -1);
3319 if (!is_parallel)
3320 isl_die(ctx, isl_error_unknown,
3321 "generated schedule not as parallel as expected",
3322 return -1);
3324 return 0;
3327 /* Compute a schedule for the given instance set, validity constraints,
3328 * proximity constraints and context and return a corresponding union map
3329 * representation.
3331 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3332 const char *domain, const char *validity, const char *proximity,
3333 const char *context)
3335 isl_set *con;
3336 isl_union_set *dom;
3337 isl_union_map *dep;
3338 isl_union_map *prox;
3339 isl_schedule_constraints *sc;
3340 isl_schedule *schedule;
3341 isl_union_map *sched;
3343 con = isl_set_read_from_str(ctx, context);
3344 dom = isl_union_set_read_from_str(ctx, domain);
3345 dep = isl_union_map_read_from_str(ctx, validity);
3346 prox = isl_union_map_read_from_str(ctx, proximity);
3347 sc = isl_schedule_constraints_on_domain(dom);
3348 sc = isl_schedule_constraints_set_context(sc, con);
3349 sc = isl_schedule_constraints_set_validity(sc, dep);
3350 sc = isl_schedule_constraints_set_proximity(sc, prox);
3351 schedule = isl_schedule_constraints_compute_schedule(sc);
3352 sched = isl_schedule_get_map(schedule);
3353 isl_schedule_free(schedule);
3355 return sched;
3358 /* Compute a schedule for the given instance set, validity constraints and
3359 * proximity constraints and return a corresponding union map representation.
3361 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
3362 const char *domain, const char *validity, const char *proximity)
3364 return compute_schedule_with_context(ctx, domain, validity, proximity,
3365 "{ : }");
3368 /* Check that a schedule can be constructed on the given domain
3369 * with the given validity and proximity constraints.
3371 static int test_has_schedule(isl_ctx *ctx, const char *domain,
3372 const char *validity, const char *proximity)
3374 isl_union_map *sched;
3376 sched = compute_schedule(ctx, domain, validity, proximity);
3377 if (!sched)
3378 return -1;
3380 isl_union_map_free(sched);
3381 return 0;
3384 int test_special_schedule(isl_ctx *ctx, const char *domain,
3385 const char *validity, const char *proximity, const char *expected_sched)
3387 isl_union_map *sched1, *sched2;
3388 int equal;
3390 sched1 = compute_schedule(ctx, domain, validity, proximity);
3391 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
3393 equal = isl_union_map_is_equal(sched1, sched2);
3394 isl_union_map_free(sched1);
3395 isl_union_map_free(sched2);
3397 if (equal < 0)
3398 return -1;
3399 if (!equal)
3400 isl_die(ctx, isl_error_unknown, "unexpected schedule",
3401 return -1);
3403 return 0;
3406 /* Check that the schedule map is properly padded, even after being
3407 * reconstructed from the band forest.
3409 static int test_padded_schedule(isl_ctx *ctx)
3411 const char *str;
3412 isl_union_set *D;
3413 isl_union_map *validity, *proximity;
3414 isl_schedule_constraints *sc;
3415 isl_schedule *sched;
3416 isl_union_map *map1, *map2;
3417 isl_band_list *list;
3418 int equal;
3420 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
3421 D = isl_union_set_read_from_str(ctx, str);
3422 validity = isl_union_map_empty(isl_union_set_get_space(D));
3423 proximity = isl_union_map_copy(validity);
3424 sc = isl_schedule_constraints_on_domain(D);
3425 sc = isl_schedule_constraints_set_validity(sc, validity);
3426 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3427 sched = isl_schedule_constraints_compute_schedule(sc);
3428 map1 = isl_schedule_get_map(sched);
3429 list = isl_schedule_get_band_forest(sched);
3430 isl_band_list_free(list);
3431 map2 = isl_schedule_get_map(sched);
3432 isl_schedule_free(sched);
3433 equal = isl_union_map_is_equal(map1, map2);
3434 isl_union_map_free(map1);
3435 isl_union_map_free(map2);
3437 if (equal < 0)
3438 return -1;
3439 if (!equal)
3440 isl_die(ctx, isl_error_unknown,
3441 "reconstructed schedule map not the same as original",
3442 return -1);
3444 return 0;
3447 /* Check that conditional validity constraints are also taken into
3448 * account across bands.
3449 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
3450 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
3451 * and then check that the adjacent order constraint C[2,1]->D[2,0]
3452 * is enforced by the rest of the schedule.
3454 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
3456 const char *str;
3457 isl_union_set *domain;
3458 isl_union_map *validity, *proximity, *condition;
3459 isl_union_map *sink, *source, *dep;
3460 isl_schedule_constraints *sc;
3461 isl_schedule *schedule;
3462 isl_union_access_info *access;
3463 isl_union_flow *flow;
3464 int empty;
3466 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3467 "A[k] : k >= 1 and k <= -1 + n; "
3468 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3469 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
3470 domain = isl_union_set_read_from_str(ctx, str);
3471 sc = isl_schedule_constraints_on_domain(domain);
3472 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
3473 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3474 "D[k, i] -> C[1 + k, i] : "
3475 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3476 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
3477 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
3478 validity = isl_union_map_read_from_str(ctx, str);
3479 sc = isl_schedule_constraints_set_validity(sc, validity);
3480 str = "[n] -> { C[k, i] -> D[k, i] : "
3481 "0 <= i <= -1 + k and k <= -1 + n }";
3482 proximity = isl_union_map_read_from_str(ctx, str);
3483 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3484 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
3485 "i <= -1 + k and i >= 1 and k <= -2 + n; "
3486 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
3487 "k <= -1 + n and i >= 0 and i <= -2 + k }";
3488 condition = isl_union_map_read_from_str(ctx, str);
3489 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
3490 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3491 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
3492 "i >= 0 and i <= -1 + k and k <= -1 + n and "
3493 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
3494 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
3495 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3496 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
3497 "k <= -1 + n and i >= 0 and i <= -1 + k and "
3498 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
3499 validity = isl_union_map_read_from_str(ctx, str);
3500 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3501 validity);
3502 schedule = isl_schedule_constraints_compute_schedule(sc);
3503 str = "{ D[2,0] -> [] }";
3504 sink = isl_union_map_read_from_str(ctx, str);
3505 access = isl_union_access_info_from_sink(sink);
3506 str = "{ C[2,1] -> [] }";
3507 source = isl_union_map_read_from_str(ctx, str);
3508 access = isl_union_access_info_set_must_source(access, source);
3509 access = isl_union_access_info_set_schedule(access, schedule);
3510 flow = isl_union_access_info_compute_flow(access);
3511 dep = isl_union_flow_get_must_dependence(flow);
3512 isl_union_flow_free(flow);
3513 empty = isl_union_map_is_empty(dep);
3514 isl_union_map_free(dep);
3516 if (empty < 0)
3517 return -1;
3518 if (empty)
3519 isl_die(ctx, isl_error_unknown,
3520 "conditional validity not respected", return -1);
3522 return 0;
3525 /* Input for testing of schedule construction based on
3526 * conditional constraints.
3528 * domain is the iteration domain
3529 * flow are the flow dependences, which determine the validity and
3530 * proximity constraints
3531 * condition are the conditions on the conditional validity constraints
3532 * conditional_validity are the conditional validity constraints
3533 * outer_band_n is the expected number of members in the outer band
3535 struct {
3536 const char *domain;
3537 const char *flow;
3538 const char *condition;
3539 const char *conditional_validity;
3540 int outer_band_n;
3541 } live_range_tests[] = {
3542 /* Contrived example that illustrates that we need to keep
3543 * track of tagged condition dependences and
3544 * tagged conditional validity dependences
3545 * in isl_sched_edge separately.
3546 * In particular, the conditional validity constraints on A
3547 * cannot be satisfied,
3548 * but they can be ignored because there are no corresponding
3549 * condition constraints. However, we do have an additional
3550 * conditional validity constraint that maps to the same
3551 * dependence relation
3552 * as the condition constraint on B. If we did not make a distinction
3553 * between tagged condition and tagged conditional validity
3554 * dependences, then we
3555 * could end up treating this shared dependence as an condition
3556 * constraint on A, forcing a localization of the conditions,
3557 * which is impossible.
3559 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
3560 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
3561 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
3562 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3563 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3564 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
3567 /* TACO 2013 Fig. 7 */
3568 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3569 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3570 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3571 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
3572 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
3573 "0 <= i < n and 0 <= j < n - 1 }",
3574 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
3575 "0 <= i < n and 0 <= j < j' < n;"
3576 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
3577 "0 <= i < i' < n and 0 <= j,j' < n;"
3578 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
3579 "0 <= i,j,j' < n and j < j' }",
3582 /* TACO 2013 Fig. 7, without tags */
3583 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3584 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3585 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3586 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3587 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3588 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
3589 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
3590 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
3593 /* TACO 2013 Fig. 12 */
3594 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
3595 "S3[i,3] : 0 <= i <= 1 }",
3596 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
3597 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
3598 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
3599 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
3600 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3601 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
3602 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3603 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
3604 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
3605 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
3606 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
3611 /* Test schedule construction based on conditional constraints.
3612 * In particular, check the number of members in the outer band node
3613 * as an indication of whether tiling is possible or not.
3615 static int test_conditional_schedule_constraints(isl_ctx *ctx)
3617 int i;
3618 isl_union_set *domain;
3619 isl_union_map *condition;
3620 isl_union_map *flow;
3621 isl_union_map *validity;
3622 isl_schedule_constraints *sc;
3623 isl_schedule *schedule;
3624 isl_schedule_node *node;
3625 int n_member;
3627 if (test_special_conditional_schedule_constraints(ctx) < 0)
3628 return -1;
3630 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
3631 domain = isl_union_set_read_from_str(ctx,
3632 live_range_tests[i].domain);
3633 flow = isl_union_map_read_from_str(ctx,
3634 live_range_tests[i].flow);
3635 condition = isl_union_map_read_from_str(ctx,
3636 live_range_tests[i].condition);
3637 validity = isl_union_map_read_from_str(ctx,
3638 live_range_tests[i].conditional_validity);
3639 sc = isl_schedule_constraints_on_domain(domain);
3640 sc = isl_schedule_constraints_set_validity(sc,
3641 isl_union_map_copy(flow));
3642 sc = isl_schedule_constraints_set_proximity(sc, flow);
3643 sc = isl_schedule_constraints_set_conditional_validity(sc,
3644 condition, validity);
3645 schedule = isl_schedule_constraints_compute_schedule(sc);
3646 node = isl_schedule_get_root(schedule);
3647 while (node &&
3648 isl_schedule_node_get_type(node) != isl_schedule_node_band)
3649 node = isl_schedule_node_first_child(node);
3650 n_member = isl_schedule_node_band_n_member(node);
3651 isl_schedule_node_free(node);
3652 isl_schedule_free(schedule);
3654 if (!schedule)
3655 return -1;
3656 if (n_member != live_range_tests[i].outer_band_n)
3657 isl_die(ctx, isl_error_unknown,
3658 "unexpected number of members in outer band",
3659 return -1);
3661 return 0;
3664 /* Check that the schedule computed for the given instance set and
3665 * dependence relation strongly satisfies the dependences.
3666 * In particular, check that no instance is scheduled before
3667 * or together with an instance on which it depends.
3668 * Earlier versions of isl would produce a schedule that
3669 * only weakly satisfies the dependences.
3671 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
3673 const char *domain, *dep;
3674 isl_union_map *D, *schedule;
3675 isl_map *map, *ge;
3676 int empty;
3678 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
3679 "A[i0] : 0 <= i0 <= 1 }";
3680 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
3681 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
3682 schedule = compute_schedule(ctx, domain, dep, dep);
3683 D = isl_union_map_read_from_str(ctx, dep);
3684 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
3685 D = isl_union_map_apply_range(D, schedule);
3686 map = isl_map_from_union_map(D);
3687 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3688 map = isl_map_intersect(map, ge);
3689 empty = isl_map_is_empty(map);
3690 isl_map_free(map);
3692 if (empty < 0)
3693 return -1;
3694 if (!empty)
3695 isl_die(ctx, isl_error_unknown,
3696 "dependences not strongly satisfied", return -1);
3698 return 0;
3701 /* Compute a schedule for input where the instance set constraints
3702 * conflict with the context constraints.
3703 * Earlier versions of isl did not properly handle this situation.
3705 static int test_conflicting_context_schedule(isl_ctx *ctx)
3707 isl_union_map *schedule;
3708 const char *domain, *context;
3710 domain = "[n] -> { A[] : n >= 0 }";
3711 context = "[n] -> { : n < 0 }";
3712 schedule = compute_schedule_with_context(ctx,
3713 domain, "{}", "{}", context);
3714 isl_union_map_free(schedule);
3716 if (!schedule)
3717 return -1;
3719 return 0;
3722 /* Check that the dependence carrying step is not confused by
3723 * a bound on the coefficient size.
3724 * In particular, force the scheduler to move to a dependence carrying
3725 * step by demanding outer coincidence and bound the size of
3726 * the coefficients. Earlier versions of isl would take this
3727 * bound into account while carrying dependences, breaking
3728 * fundamental assumptions.
3729 * On the other hand, the dependence carrying step now tries
3730 * to prevent loop coalescing by default, so check that indeed
3731 * no loop coalescing occurs by comparing the computed schedule
3732 * to the expected non-coalescing schedule.
3734 static int test_bounded_coefficients_schedule(isl_ctx *ctx)
3736 const char *domain, *dep;
3737 isl_union_set *I;
3738 isl_union_map *D;
3739 isl_schedule_constraints *sc;
3740 isl_schedule *schedule;
3741 isl_union_map *sched1, *sched2;
3742 isl_bool equal;
3744 domain = "{ C[i0, i1] : 2 <= i0 <= 3999 and 0 <= i1 <= -1 + i0 }";
3745 dep = "{ C[i0, i1] -> C[i0, 1 + i1] : i0 <= 3999 and i1 >= 0 and "
3746 "i1 <= -2 + i0; "
3747 "C[i0, -1 + i0] -> C[1 + i0, 0] : i0 <= 3998 and i0 >= 1 }";
3748 I = isl_union_set_read_from_str(ctx, domain);
3749 D = isl_union_map_read_from_str(ctx, dep);
3750 sc = isl_schedule_constraints_on_domain(I);
3751 sc = isl_schedule_constraints_set_validity(sc, isl_union_map_copy(D));
3752 sc = isl_schedule_constraints_set_coincidence(sc, D);
3753 isl_options_set_schedule_outer_coincidence(ctx, 1);
3754 isl_options_set_schedule_max_coefficient(ctx, 20);
3755 schedule = isl_schedule_constraints_compute_schedule(sc);
3756 isl_options_set_schedule_max_coefficient(ctx, -1);
3757 isl_options_set_schedule_outer_coincidence(ctx, 0);
3758 sched1 = isl_schedule_get_map(schedule);
3759 isl_schedule_free(schedule);
3761 sched2 = isl_union_map_read_from_str(ctx, "{ C[x,y] -> [x,y] }");
3762 equal = isl_union_map_is_equal(sched1, sched2);
3763 isl_union_map_free(sched1);
3764 isl_union_map_free(sched2);
3766 if (equal < 0)
3767 return -1;
3768 if (!equal)
3769 isl_die(ctx, isl_error_unknown,
3770 "unexpected schedule", return -1);
3772 return 0;
3775 /* Check that a set of schedule constraints that only allow for
3776 * a coalescing schedule still produces a schedule even if the user
3777 * request a non-coalescing schedule. Earlier versions of isl
3778 * would not handle this case correctly.
3780 static int test_coalescing_schedule(isl_ctx *ctx)
3782 const char *domain, *dep;
3783 isl_union_set *I;
3784 isl_union_map *D;
3785 isl_schedule_constraints *sc;
3786 isl_schedule *schedule;
3787 int treat_coalescing;
3789 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
3790 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
3791 I = isl_union_set_read_from_str(ctx, domain);
3792 D = isl_union_map_read_from_str(ctx, dep);
3793 sc = isl_schedule_constraints_on_domain(I);
3794 sc = isl_schedule_constraints_set_validity(sc, D);
3795 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
3796 isl_options_set_schedule_treat_coalescing(ctx, 1);
3797 schedule = isl_schedule_constraints_compute_schedule(sc);
3798 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
3799 isl_schedule_free(schedule);
3800 if (!schedule)
3801 return -1;
3802 return 0;
3805 int test_schedule(isl_ctx *ctx)
3807 const char *D, *W, *R, *V, *P, *S;
3808 int max_coincidence;
3809 int treat_coalescing;
3811 /* Handle resulting schedule with zero bands. */
3812 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
3813 return -1;
3815 /* Jacobi */
3816 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
3817 W = "{ S1[t,i] -> a[t,i] }";
3818 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
3819 "S1[t,i] -> a[t-1,i+1] }";
3820 S = "{ S1[t,i] -> [t,i] }";
3821 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3822 return -1;
3824 /* Fig. 5 of CC2008 */
3825 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
3826 "j <= -1 + N }";
3827 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
3828 "j >= 2 and j <= -1 + N }";
3829 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
3830 "j >= 2 and j <= -1 + N; "
3831 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
3832 "j >= 2 and j <= -1 + N }";
3833 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3834 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3835 return -1;
3837 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
3838 W = "{ S1[i] -> a[i] }";
3839 R = "{ S2[i] -> a[i+1] }";
3840 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3841 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3842 return -1;
3844 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
3845 W = "{ S1[i] -> a[i] }";
3846 R = "{ S2[i] -> a[9-i] }";
3847 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3848 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3849 return -1;
3851 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
3852 W = "{ S1[i] -> a[i] }";
3853 R = "[N] -> { S2[i] -> a[N-1-i] }";
3854 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3855 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3856 return -1;
3858 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
3859 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
3860 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
3861 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
3862 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3863 return -1;
3865 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3866 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
3867 R = "{ S2[i,j] -> a[i-1,j] }";
3868 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3869 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3870 return -1;
3872 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3873 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
3874 R = "{ S2[i,j] -> a[i,j-1] }";
3875 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3876 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3877 return -1;
3879 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
3880 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
3881 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
3882 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
3883 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
3884 "S_0[] -> [0, 0, 0] }";
3885 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
3886 return -1;
3887 ctx->opt->schedule_parametric = 0;
3888 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3889 return -1;
3890 ctx->opt->schedule_parametric = 1;
3892 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
3893 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
3894 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
3895 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
3896 "S4[i] -> a[i,N] }";
3897 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
3898 "S4[i] -> [4,i,0] }";
3899 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
3900 isl_options_set_schedule_maximize_coincidence(ctx, 0);
3901 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3902 return -1;
3903 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
3905 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
3906 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3907 "j <= N }";
3908 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3909 "j <= N; "
3910 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
3911 "j <= N }";
3912 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3913 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3914 return -1;
3916 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
3917 " S_2[t] : t >= 0 and t <= -1 + N; "
3918 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
3919 "i <= -1 + N }";
3920 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
3921 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
3922 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
3923 "i >= 0 and i <= -1 + N }";
3924 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
3925 "i >= 0 and i <= -1 + N; "
3926 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
3927 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
3928 " S_0[t] -> [0, t, 0] }";
3930 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3931 return -1;
3932 ctx->opt->schedule_parametric = 0;
3933 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3934 return -1;
3935 ctx->opt->schedule_parametric = 1;
3937 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
3938 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
3939 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
3940 return -1;
3942 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
3943 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
3944 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
3945 "j >= 0 and j <= -1 + N; "
3946 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3947 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
3948 "j >= 0 and j <= -1 + N; "
3949 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3950 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
3951 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3952 return -1;
3954 D = "{ S_0[i] : i >= 0 }";
3955 W = "{ S_0[i] -> a[i] : i >= 0 }";
3956 R = "{ S_0[i] -> a[0] : i >= 0 }";
3957 S = "{ S_0[i] -> [0, i, 0] }";
3958 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3959 return -1;
3961 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
3962 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
3963 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
3964 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
3965 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3966 return -1;
3968 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
3969 "k <= -1 + n and k >= 0 }";
3970 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
3971 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
3972 "k <= -1 + n and k >= 0; "
3973 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
3974 "k <= -1 + n and k >= 0; "
3975 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
3976 "k <= -1 + n and k >= 0 }";
3977 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
3978 ctx->opt->schedule_outer_coincidence = 1;
3979 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3980 return -1;
3981 ctx->opt->schedule_outer_coincidence = 0;
3983 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3984 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3985 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3986 "Stmt_for_body24[i0, i1, 1, 0]:"
3987 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3988 "Stmt_for_body7[i0, i1, i2]:"
3989 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3990 "i2 <= 7 }";
3992 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3993 "Stmt_for_body24[1, i1, i2, i3]:"
3994 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3995 "i2 >= 1;"
3996 "Stmt_for_body24[0, i1, i2, i3] -> "
3997 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3998 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3999 "i3 >= 0;"
4000 "Stmt_for_body24[0, i1, i2, i3] ->"
4001 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4002 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4003 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4004 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4005 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4006 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4007 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4008 "i1 <= 6 and i1 >= 0;"
4009 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4010 "Stmt_for_body7[i0, i1, i2] -> "
4011 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4012 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4013 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4014 "Stmt_for_body7[i0, i1, i2] -> "
4015 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4016 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4017 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4018 P = V;
4019 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
4020 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
4021 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
4023 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4024 isl_options_set_schedule_treat_coalescing(ctx, 0);
4025 if (test_special_schedule(ctx, D, V, P, S) < 0)
4026 return -1;
4027 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4029 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4030 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4031 "j >= 1 and j <= 7;"
4032 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4033 "j >= 1 and j <= 8 }";
4034 P = "{ }";
4035 S = "{ S_0[i, j] -> [i + j, j] }";
4036 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4037 if (test_special_schedule(ctx, D, V, P, S) < 0)
4038 return -1;
4039 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4041 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4042 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4043 "j >= 0 and j <= -1 + i }";
4044 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4045 "i <= -1 + N and j >= 0;"
4046 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4047 "i <= -2 + N }";
4048 P = "{ }";
4049 S = "{ S_0[i, j] -> [i, j] }";
4050 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4051 if (test_special_schedule(ctx, D, V, P, S) < 0)
4052 return -1;
4053 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4055 /* Test both algorithms on a case with only proximity dependences. */
4056 D = "{ S[i,j] : 0 <= i <= 10 }";
4057 V = "{ }";
4058 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4059 S = "{ S[i, j] -> [j, i] }";
4060 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4061 if (test_special_schedule(ctx, D, V, P, S) < 0)
4062 return -1;
4063 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4064 if (test_special_schedule(ctx, D, V, P, S) < 0)
4065 return -1;
4067 D = "{ A[a]; B[] }";
4068 V = "{}";
4069 P = "{ A[a] -> B[] }";
4070 if (test_has_schedule(ctx, D, V, P) < 0)
4071 return -1;
4073 if (test_padded_schedule(ctx) < 0)
4074 return -1;
4076 /* Check that check for progress is not confused by rational
4077 * solution.
4079 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4080 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4081 "i0 <= -2 + N; "
4082 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4083 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4084 P = "{}";
4085 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4086 if (test_has_schedule(ctx, D, V, P) < 0)
4087 return -1;
4088 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4090 /* Check that we allow schedule rows that are only non-trivial
4091 * on some full-dimensional domains.
4093 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4094 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4095 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4096 P = "{}";
4097 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4098 if (test_has_schedule(ctx, D, V, P) < 0)
4099 return -1;
4100 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4102 if (test_conditional_schedule_constraints(ctx) < 0)
4103 return -1;
4105 if (test_strongly_satisfying_schedule(ctx) < 0)
4106 return -1;
4108 if (test_conflicting_context_schedule(ctx) < 0)
4109 return -1;
4111 if (test_bounded_coefficients_schedule(ctx) < 0)
4112 return -1;
4113 if (test_coalescing_schedule(ctx) < 0)
4114 return -1;
4116 return 0;
4119 /* Perform scheduling tests using the whole component scheduler.
4121 static int test_schedule_whole(isl_ctx *ctx)
4123 int whole;
4124 int r;
4126 whole = isl_options_get_schedule_whole_component(ctx);
4127 isl_options_set_schedule_whole_component(ctx, 1);
4128 r = test_schedule(ctx);
4129 isl_options_set_schedule_whole_component(ctx, whole);
4131 return r;
4134 /* Perform scheduling tests using the incremental scheduler.
4136 static int test_schedule_incremental(isl_ctx *ctx)
4138 int whole;
4139 int r;
4141 whole = isl_options_get_schedule_whole_component(ctx);
4142 isl_options_set_schedule_whole_component(ctx, 0);
4143 r = test_schedule(ctx);
4144 isl_options_set_schedule_whole_component(ctx, whole);
4146 return r;
4149 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4151 isl_union_map *umap;
4152 int test;
4154 umap = isl_union_map_read_from_str(ctx, str);
4155 test = isl_union_map_plain_is_injective(umap);
4156 isl_union_map_free(umap);
4157 if (test < 0)
4158 return -1;
4159 if (test == injective)
4160 return 0;
4161 if (injective)
4162 isl_die(ctx, isl_error_unknown,
4163 "map not detected as injective", return -1);
4164 else
4165 isl_die(ctx, isl_error_unknown,
4166 "map detected as injective", return -1);
4169 int test_injective(isl_ctx *ctx)
4171 const char *str;
4173 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4174 return -1;
4175 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4176 return -1;
4177 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4178 return -1;
4179 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4180 return -1;
4181 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4182 return -1;
4183 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4184 return -1;
4185 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4186 return -1;
4187 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4188 return -1;
4190 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4191 if (test_plain_injective(ctx, str, 1))
4192 return -1;
4193 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4194 if (test_plain_injective(ctx, str, 0))
4195 return -1;
4197 return 0;
4200 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4202 isl_aff *aff2;
4203 int equal;
4205 if (!aff)
4206 return -1;
4208 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4209 equal = isl_aff_plain_is_equal(aff, aff2);
4210 isl_aff_free(aff2);
4212 return equal;
4215 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4217 int equal;
4219 equal = aff_plain_is_equal(aff, str);
4220 if (equal < 0)
4221 return -1;
4222 if (!equal)
4223 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4224 "result not as expected", return -1);
4225 return 0;
4228 struct {
4229 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4230 __isl_take isl_aff *aff2);
4231 } aff_bin_op[] = {
4232 ['+'] = { &isl_aff_add },
4233 ['-'] = { &isl_aff_sub },
4234 ['*'] = { &isl_aff_mul },
4235 ['/'] = { &isl_aff_div },
4238 struct {
4239 const char *arg1;
4240 unsigned char op;
4241 const char *arg2;
4242 const char *res;
4243 } aff_bin_tests[] = {
4244 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
4245 "{ [i] -> [2i] }" },
4246 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
4247 "{ [i] -> [0] }" },
4248 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
4249 "{ [i] -> [2i] }" },
4250 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
4251 "{ [i] -> [2i] }" },
4252 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
4253 "{ [i] -> [i/2] }" },
4254 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
4255 "{ [i] -> [i] }" },
4256 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
4257 "{ [i] -> [NaN] }" },
4258 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
4259 "{ [i] -> [NaN] }" },
4260 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
4261 "{ [i] -> [NaN] }" },
4262 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
4263 "{ [i] -> [NaN] }" },
4264 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
4265 "{ [i] -> [NaN] }" },
4266 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
4267 "{ [i] -> [NaN] }" },
4268 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
4269 "{ [i] -> [NaN] }" },
4270 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
4271 "{ [i] -> [NaN] }" },
4272 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
4273 "{ [i] -> [NaN] }" },
4274 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
4275 "{ [i] -> [NaN] }" },
4276 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
4277 "{ [i] -> [NaN] }" },
4278 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
4279 "{ [i] -> [NaN] }" },
4282 /* Perform some basic tests of binary operations on isl_aff objects.
4284 static int test_bin_aff(isl_ctx *ctx)
4286 int i;
4287 isl_aff *aff1, *aff2, *res;
4288 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4289 __isl_take isl_aff *aff2);
4290 int ok;
4292 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
4293 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
4294 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
4295 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
4296 fn = aff_bin_op[aff_bin_tests[i].op].fn;
4297 aff1 = fn(aff1, aff2);
4298 if (isl_aff_is_nan(res))
4299 ok = isl_aff_is_nan(aff1);
4300 else
4301 ok = isl_aff_plain_is_equal(aff1, res);
4302 isl_aff_free(aff1);
4303 isl_aff_free(res);
4304 if (ok < 0)
4305 return -1;
4306 if (!ok)
4307 isl_die(ctx, isl_error_unknown,
4308 "unexpected result", return -1);
4311 return 0;
4314 struct {
4315 __isl_give isl_union_pw_multi_aff *(*fn)(
4316 __isl_take isl_union_pw_multi_aff *upma1,
4317 __isl_take isl_union_pw_multi_aff *upma2);
4318 const char *arg1;
4319 const char *arg2;
4320 const char *res;
4321 } upma_bin_tests[] = {
4322 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
4323 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
4324 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
4325 "{ B[x] -> [2] : x >= 0 }",
4326 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
4327 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
4328 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
4329 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
4330 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
4331 "D[i] -> B[2] : i >= 5 }" },
4332 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4333 "{ B[x] -> C[2] : x > 0 }",
4334 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
4335 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4336 "{ B[x] -> A[2] : x >= 0 }",
4337 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
4340 /* Perform some basic tests of binary operations on
4341 * isl_union_pw_multi_aff objects.
4343 static int test_bin_upma(isl_ctx *ctx)
4345 int i;
4346 isl_union_pw_multi_aff *upma1, *upma2, *res;
4347 int ok;
4349 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
4350 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4351 upma_bin_tests[i].arg1);
4352 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4353 upma_bin_tests[i].arg2);
4354 res = isl_union_pw_multi_aff_read_from_str(ctx,
4355 upma_bin_tests[i].res);
4356 upma1 = upma_bin_tests[i].fn(upma1, upma2);
4357 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
4358 isl_union_pw_multi_aff_free(upma1);
4359 isl_union_pw_multi_aff_free(res);
4360 if (ok < 0)
4361 return -1;
4362 if (!ok)
4363 isl_die(ctx, isl_error_unknown,
4364 "unexpected result", return -1);
4367 return 0;
4370 struct {
4371 __isl_give isl_union_pw_multi_aff *(*fn)(
4372 __isl_take isl_union_pw_multi_aff *upma1,
4373 __isl_take isl_union_pw_multi_aff *upma2);
4374 const char *arg1;
4375 const char *arg2;
4376 } upma_bin_fail_tests[] = {
4377 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4378 "{ B[x] -> C[2] : x >= 0 }" },
4381 /* Perform some basic tests of binary operations on
4382 * isl_union_pw_multi_aff objects that are expected to fail.
4384 static int test_bin_upma_fail(isl_ctx *ctx)
4386 int i, n;
4387 isl_union_pw_multi_aff *upma1, *upma2;
4388 int on_error;
4390 on_error = isl_options_get_on_error(ctx);
4391 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
4392 n = ARRAY_SIZE(upma_bin_fail_tests);
4393 for (i = 0; i < n; ++i) {
4394 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4395 upma_bin_fail_tests[i].arg1);
4396 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4397 upma_bin_fail_tests[i].arg2);
4398 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
4399 isl_union_pw_multi_aff_free(upma1);
4400 if (upma1)
4401 break;
4403 isl_options_set_on_error(ctx, on_error);
4404 if (i < n)
4405 isl_die(ctx, isl_error_unknown,
4406 "operation not expected to succeed", return -1);
4408 return 0;
4411 int test_aff(isl_ctx *ctx)
4413 const char *str;
4414 isl_set *set;
4415 isl_space *space;
4416 isl_local_space *ls;
4417 isl_aff *aff;
4418 int zero, equal;
4420 if (test_bin_aff(ctx) < 0)
4421 return -1;
4422 if (test_bin_upma(ctx) < 0)
4423 return -1;
4424 if (test_bin_upma_fail(ctx) < 0)
4425 return -1;
4427 space = isl_space_set_alloc(ctx, 0, 1);
4428 ls = isl_local_space_from_space(space);
4429 aff = isl_aff_zero_on_domain(ls);
4431 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4432 aff = isl_aff_scale_down_ui(aff, 3);
4433 aff = isl_aff_floor(aff);
4434 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4435 aff = isl_aff_scale_down_ui(aff, 2);
4436 aff = isl_aff_floor(aff);
4437 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4439 str = "{ [10] }";
4440 set = isl_set_read_from_str(ctx, str);
4441 aff = isl_aff_gist(aff, set);
4443 aff = isl_aff_add_constant_si(aff, -16);
4444 zero = isl_aff_plain_is_zero(aff);
4445 isl_aff_free(aff);
4447 if (zero < 0)
4448 return -1;
4449 if (!zero)
4450 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4452 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
4453 aff = isl_aff_scale_down_ui(aff, 64);
4454 aff = isl_aff_floor(aff);
4455 equal = aff_check_plain_equal(aff, "{ [-1] }");
4456 isl_aff_free(aff);
4457 if (equal < 0)
4458 return -1;
4460 return 0;
4463 int test_dim_max(isl_ctx *ctx)
4465 int equal;
4466 const char *str;
4467 isl_set *set1, *set2;
4468 isl_set *set;
4469 isl_map *map;
4470 isl_pw_aff *pwaff;
4472 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
4473 set = isl_set_read_from_str(ctx, str);
4474 pwaff = isl_set_dim_max(set, 0);
4475 set1 = isl_set_from_pw_aff(pwaff);
4476 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
4477 set2 = isl_set_read_from_str(ctx, str);
4478 equal = isl_set_is_equal(set1, set2);
4479 isl_set_free(set1);
4480 isl_set_free(set2);
4481 if (equal < 0)
4482 return -1;
4483 if (!equal)
4484 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4486 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
4487 set = isl_set_read_from_str(ctx, str);
4488 pwaff = isl_set_dim_max(set, 0);
4489 set1 = isl_set_from_pw_aff(pwaff);
4490 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4491 set2 = isl_set_read_from_str(ctx, str);
4492 equal = isl_set_is_equal(set1, set2);
4493 isl_set_free(set1);
4494 isl_set_free(set2);
4495 if (equal < 0)
4496 return -1;
4497 if (!equal)
4498 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4500 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
4501 set = isl_set_read_from_str(ctx, str);
4502 pwaff = isl_set_dim_max(set, 0);
4503 set1 = isl_set_from_pw_aff(pwaff);
4504 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4505 set2 = isl_set_read_from_str(ctx, str);
4506 equal = isl_set_is_equal(set1, set2);
4507 isl_set_free(set1);
4508 isl_set_free(set2);
4509 if (equal < 0)
4510 return -1;
4511 if (!equal)
4512 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4514 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
4515 "0 <= i < N and 0 <= j < M }";
4516 map = isl_map_read_from_str(ctx, str);
4517 set = isl_map_range(map);
4519 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
4520 set1 = isl_set_from_pw_aff(pwaff);
4521 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
4522 set2 = isl_set_read_from_str(ctx, str);
4523 equal = isl_set_is_equal(set1, set2);
4524 isl_set_free(set1);
4525 isl_set_free(set2);
4527 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
4528 set1 = isl_set_from_pw_aff(pwaff);
4529 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
4530 set2 = isl_set_read_from_str(ctx, str);
4531 if (equal >= 0 && equal)
4532 equal = isl_set_is_equal(set1, set2);
4533 isl_set_free(set1);
4534 isl_set_free(set2);
4536 isl_set_free(set);
4538 if (equal < 0)
4539 return -1;
4540 if (!equal)
4541 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4543 /* Check that solutions are properly merged. */
4544 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
4545 "c <= -1 + n - 4a - 2b and c >= -2b and "
4546 "4a >= -4 + n and c >= 0 }";
4547 set = isl_set_read_from_str(ctx, str);
4548 pwaff = isl_set_dim_min(set, 2);
4549 set1 = isl_set_from_pw_aff(pwaff);
4550 str = "[n] -> { [(0)] : n >= 1 }";
4551 set2 = isl_set_read_from_str(ctx, str);
4552 equal = isl_set_is_equal(set1, set2);
4553 isl_set_free(set1);
4554 isl_set_free(set2);
4556 if (equal < 0)
4557 return -1;
4558 if (!equal)
4559 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4561 /* Check that empty solution lie in the right space. */
4562 str = "[n] -> { [t,a] : 1 = 0 }";
4563 set = isl_set_read_from_str(ctx, str);
4564 pwaff = isl_set_dim_max(set, 0);
4565 set1 = isl_set_from_pw_aff(pwaff);
4566 str = "[n] -> { [t] : 1 = 0 }";
4567 set2 = isl_set_read_from_str(ctx, str);
4568 equal = isl_set_is_equal(set1, set2);
4569 isl_set_free(set1);
4570 isl_set_free(set2);
4572 if (equal < 0)
4573 return -1;
4574 if (!equal)
4575 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4577 return 0;
4580 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
4582 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
4583 const char *str)
4585 isl_ctx *ctx;
4586 isl_pw_multi_aff *pma2;
4587 int equal;
4589 if (!pma)
4590 return -1;
4592 ctx = isl_pw_multi_aff_get_ctx(pma);
4593 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4594 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
4595 isl_pw_multi_aff_free(pma2);
4597 return equal;
4600 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
4601 * represented by "str".
4603 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
4604 const char *str)
4606 int equal;
4608 equal = pw_multi_aff_plain_is_equal(pma, str);
4609 if (equal < 0)
4610 return -1;
4611 if (!equal)
4612 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
4613 "result not as expected", return -1);
4614 return 0;
4617 /* Basic test for isl_pw_multi_aff_product.
4619 * Check that multiple pieces are properly handled.
4621 static int test_product_pma(isl_ctx *ctx)
4623 int equal;
4624 const char *str;
4625 isl_pw_multi_aff *pma1, *pma2;
4627 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
4628 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4629 str = "{ C[] -> D[] }";
4630 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4631 pma1 = isl_pw_multi_aff_product(pma1, pma2);
4632 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
4633 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
4634 equal = pw_multi_aff_check_plain_equal(pma1, str);
4635 isl_pw_multi_aff_free(pma1);
4636 if (equal < 0)
4637 return -1;
4639 return 0;
4642 int test_product(isl_ctx *ctx)
4644 const char *str;
4645 isl_set *set;
4646 isl_union_set *uset1, *uset2;
4647 int ok;
4649 str = "{ A[i] }";
4650 set = isl_set_read_from_str(ctx, str);
4651 set = isl_set_product(set, isl_set_copy(set));
4652 ok = isl_set_is_wrapping(set);
4653 isl_set_free(set);
4654 if (ok < 0)
4655 return -1;
4656 if (!ok)
4657 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4659 str = "{ [] }";
4660 uset1 = isl_union_set_read_from_str(ctx, str);
4661 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
4662 str = "{ [[] -> []] }";
4663 uset2 = isl_union_set_read_from_str(ctx, str);
4664 ok = isl_union_set_is_equal(uset1, uset2);
4665 isl_union_set_free(uset1);
4666 isl_union_set_free(uset2);
4667 if (ok < 0)
4668 return -1;
4669 if (!ok)
4670 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4672 if (test_product_pma(ctx) < 0)
4673 return -1;
4675 return 0;
4678 /* Check that two sets are not considered disjoint just because
4679 * they have a different set of (named) parameters.
4681 static int test_disjoint(isl_ctx *ctx)
4683 const char *str;
4684 isl_set *set, *set2;
4685 int disjoint;
4687 str = "[n] -> { [[]->[]] }";
4688 set = isl_set_read_from_str(ctx, str);
4689 str = "{ [[]->[]] }";
4690 set2 = isl_set_read_from_str(ctx, str);
4691 disjoint = isl_set_is_disjoint(set, set2);
4692 isl_set_free(set);
4693 isl_set_free(set2);
4694 if (disjoint < 0)
4695 return -1;
4696 if (disjoint)
4697 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4699 return 0;
4702 int test_equal(isl_ctx *ctx)
4704 const char *str;
4705 isl_set *set, *set2;
4706 int equal;
4708 str = "{ S_6[i] }";
4709 set = isl_set_read_from_str(ctx, str);
4710 str = "{ S_7[i] }";
4711 set2 = isl_set_read_from_str(ctx, str);
4712 equal = isl_set_is_equal(set, set2);
4713 isl_set_free(set);
4714 isl_set_free(set2);
4715 if (equal < 0)
4716 return -1;
4717 if (equal)
4718 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4720 return 0;
4723 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
4724 enum isl_dim_type type, unsigned pos, int fixed)
4726 int test;
4728 test = isl_map_plain_is_fixed(map, type, pos, NULL);
4729 isl_map_free(map);
4730 if (test < 0)
4731 return -1;
4732 if (test == fixed)
4733 return 0;
4734 if (fixed)
4735 isl_die(ctx, isl_error_unknown,
4736 "map not detected as fixed", return -1);
4737 else
4738 isl_die(ctx, isl_error_unknown,
4739 "map detected as fixed", return -1);
4742 int test_fixed(isl_ctx *ctx)
4744 const char *str;
4745 isl_map *map;
4747 str = "{ [i] -> [i] }";
4748 map = isl_map_read_from_str(ctx, str);
4749 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
4750 return -1;
4751 str = "{ [i] -> [1] }";
4752 map = isl_map_read_from_str(ctx, str);
4753 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4754 return -1;
4755 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
4756 map = isl_map_read_from_str(ctx, str);
4757 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4758 return -1;
4759 map = isl_map_read_from_str(ctx, str);
4760 map = isl_map_neg(map);
4761 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4762 return -1;
4764 return 0;
4767 struct isl_vertices_test_data {
4768 const char *set;
4769 int n;
4770 const char *vertex[6];
4771 } vertices_tests[] = {
4772 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
4773 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
4774 { "{ A[t, i] : t = 14 and i = 1 }",
4775 1, { "{ A[14, 1] }" } },
4776 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
4777 "c <= m and m <= n and m > 0 }",
4778 6, {
4779 "[n, m] -> { [n, m, m] : 0 < m <= n }",
4780 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
4781 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
4782 "[n, m] -> { [m, m, m] : 0 < m <= n }",
4783 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
4784 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
4785 } },
4788 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
4790 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
4792 struct isl_vertices_test_data *data = user;
4793 isl_ctx *ctx;
4794 isl_multi_aff *ma;
4795 isl_basic_set *bset;
4796 isl_pw_multi_aff *pma;
4797 int i;
4798 isl_bool equal;
4800 ctx = isl_vertex_get_ctx(vertex);
4801 bset = isl_vertex_get_domain(vertex);
4802 ma = isl_vertex_get_expr(vertex);
4803 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
4805 for (i = 0; i < data->n; ++i) {
4806 isl_pw_multi_aff *pma_i;
4808 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
4809 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
4810 isl_pw_multi_aff_free(pma_i);
4812 if (equal < 0 || equal)
4813 break;
4816 isl_pw_multi_aff_free(pma);
4817 isl_vertex_free(vertex);
4819 if (equal < 0)
4820 return isl_stat_error;
4822 return equal ? isl_stat_ok : isl_stat_error;
4825 int test_vertices(isl_ctx *ctx)
4827 int i;
4829 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
4830 isl_basic_set *bset;
4831 isl_vertices *vertices;
4832 int ok = 1;
4833 int n;
4835 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
4836 vertices = isl_basic_set_compute_vertices(bset);
4837 n = isl_vertices_get_n_vertices(vertices);
4838 if (vertices_tests[i].n != n)
4839 ok = 0;
4840 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
4841 &vertices_tests[i]) < 0)
4842 ok = 0;
4843 isl_vertices_free(vertices);
4844 isl_basic_set_free(bset);
4846 if (!vertices)
4847 return -1;
4848 if (!ok)
4849 isl_die(ctx, isl_error_unknown, "unexpected vertices",
4850 return -1);
4853 return 0;
4856 int test_union_pw(isl_ctx *ctx)
4858 int equal;
4859 const char *str;
4860 isl_union_set *uset;
4861 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
4863 str = "{ [x] -> x^2 }";
4864 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
4865 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
4866 uset = isl_union_pw_qpolynomial_domain(upwqp1);
4867 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
4868 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
4869 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
4870 isl_union_pw_qpolynomial_free(upwqp1);
4871 isl_union_pw_qpolynomial_free(upwqp2);
4872 if (equal < 0)
4873 return -1;
4874 if (!equal)
4875 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4877 return 0;
4880 /* Test that isl_union_pw_qpolynomial_eval picks up the function
4881 * defined over the correct domain space.
4883 static int test_eval(isl_ctx *ctx)
4885 const char *str;
4886 isl_point *pnt;
4887 isl_set *set;
4888 isl_union_pw_qpolynomial *upwqp;
4889 isl_val *v;
4890 int cmp;
4892 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
4893 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
4894 str = "{ A[6] }";
4895 set = isl_set_read_from_str(ctx, str);
4896 pnt = isl_set_sample_point(set);
4897 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
4898 cmp = isl_val_cmp_si(v, 36);
4899 isl_val_free(v);
4901 if (!v)
4902 return -1;
4903 if (cmp != 0)
4904 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
4906 return 0;
4909 /* Descriptions of sets that are tested for reparsing after printing.
4911 const char *output_tests[] = {
4912 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
4915 /* Check that printing a set and reparsing a set from the printed output
4916 * results in the same set.
4918 static int test_output_set(isl_ctx *ctx)
4920 int i;
4921 char *str;
4922 isl_set *set1, *set2;
4923 isl_bool equal;
4925 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
4926 set1 = isl_set_read_from_str(ctx, output_tests[i]);
4927 str = isl_set_to_str(set1);
4928 set2 = isl_set_read_from_str(ctx, str);
4929 free(str);
4930 equal = isl_set_is_equal(set1, set2);
4931 isl_set_free(set1);
4932 isl_set_free(set2);
4933 if (equal < 0)
4934 return -1;
4935 if (!equal)
4936 isl_die(ctx, isl_error_unknown,
4937 "parsed output not the same", return -1);
4940 return 0;
4943 int test_output(isl_ctx *ctx)
4945 char *s;
4946 const char *str;
4947 isl_pw_aff *pa;
4948 isl_printer *p;
4949 int equal;
4951 if (test_output_set(ctx) < 0)
4952 return -1;
4954 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
4955 pa = isl_pw_aff_read_from_str(ctx, str);
4957 p = isl_printer_to_str(ctx);
4958 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
4959 p = isl_printer_print_pw_aff(p, pa);
4960 s = isl_printer_get_str(p);
4961 isl_printer_free(p);
4962 isl_pw_aff_free(pa);
4963 if (!s)
4964 equal = -1;
4965 else
4966 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
4967 free(s);
4968 if (equal < 0)
4969 return -1;
4970 if (!equal)
4971 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4973 return 0;
4976 int test_sample(isl_ctx *ctx)
4978 const char *str;
4979 isl_basic_set *bset1, *bset2;
4980 int empty, subset;
4982 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
4983 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
4984 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
4985 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
4986 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
4987 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
4988 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
4989 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
4990 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
4991 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
4992 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
4993 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
4994 "d - 1073741821e and "
4995 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
4996 "3j >= 1 - a + b + 2e and "
4997 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
4998 "3i <= 4 - a + 4b - e and "
4999 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
5000 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
5001 "c <= -1 + a and 3i >= -2 - a + 3e and "
5002 "1073741822e <= 1073741823 - a + 1073741822b + c and "
5003 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
5004 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
5005 "1073741823e >= 1 + 1073741823b - d and "
5006 "3i >= 1073741823b + c - 1073741823e - f and "
5007 "3i >= 1 + 2b + e + 3g }";
5008 bset1 = isl_basic_set_read_from_str(ctx, str);
5009 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
5010 empty = isl_basic_set_is_empty(bset2);
5011 subset = isl_basic_set_is_subset(bset2, bset1);
5012 isl_basic_set_free(bset1);
5013 isl_basic_set_free(bset2);
5014 if (empty < 0 || subset < 0)
5015 return -1;
5016 if (empty)
5017 isl_die(ctx, isl_error_unknown, "point not found", return -1);
5018 if (!subset)
5019 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
5021 return 0;
5024 int test_fixed_power(isl_ctx *ctx)
5026 const char *str;
5027 isl_map *map;
5028 isl_int exp;
5029 int equal;
5031 isl_int_init(exp);
5032 str = "{ [i] -> [i + 1] }";
5033 map = isl_map_read_from_str(ctx, str);
5034 isl_int_set_si(exp, 23);
5035 map = isl_map_fixed_power(map, exp);
5036 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
5037 isl_int_clear(exp);
5038 isl_map_free(map);
5039 if (equal < 0)
5040 return -1;
5042 return 0;
5045 int test_slice(isl_ctx *ctx)
5047 const char *str;
5048 isl_map *map;
5049 int equal;
5051 str = "{ [i] -> [j] }";
5052 map = isl_map_read_from_str(ctx, str);
5053 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
5054 equal = map_check_equal(map, "{ [i] -> [i] }");
5055 isl_map_free(map);
5056 if (equal < 0)
5057 return -1;
5059 str = "{ [i] -> [j] }";
5060 map = isl_map_read_from_str(ctx, str);
5061 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
5062 equal = map_check_equal(map, "{ [i] -> [j] }");
5063 isl_map_free(map);
5064 if (equal < 0)
5065 return -1;
5067 str = "{ [i] -> [j] }";
5068 map = isl_map_read_from_str(ctx, str);
5069 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
5070 equal = map_check_equal(map, "{ [i] -> [-i] }");
5071 isl_map_free(map);
5072 if (equal < 0)
5073 return -1;
5075 str = "{ [i] -> [j] }";
5076 map = isl_map_read_from_str(ctx, str);
5077 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
5078 equal = map_check_equal(map, "{ [0] -> [j] }");
5079 isl_map_free(map);
5080 if (equal < 0)
5081 return -1;
5083 str = "{ [i] -> [j] }";
5084 map = isl_map_read_from_str(ctx, str);
5085 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
5086 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
5087 isl_map_free(map);
5088 if (equal < 0)
5089 return -1;
5091 str = "{ [i] -> [j] }";
5092 map = isl_map_read_from_str(ctx, str);
5093 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
5094 equal = map_check_equal(map, "{ [i] -> [j] : false }");
5095 isl_map_free(map);
5096 if (equal < 0)
5097 return -1;
5099 return 0;
5102 int test_eliminate(isl_ctx *ctx)
5104 const char *str;
5105 isl_map *map;
5106 int equal;
5108 str = "{ [i] -> [j] : i = 2j }";
5109 map = isl_map_read_from_str(ctx, str);
5110 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
5111 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
5112 isl_map_free(map);
5113 if (equal < 0)
5114 return -1;
5116 return 0;
5119 /* Check that isl_set_dim_residue_class detects that the values of j
5120 * in the set below are all odd and that it does not detect any spurious
5121 * strides.
5123 static int test_residue_class(isl_ctx *ctx)
5125 const char *str;
5126 isl_set *set;
5127 isl_int m, r;
5128 int res;
5130 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
5131 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
5132 set = isl_set_read_from_str(ctx, str);
5133 isl_int_init(m);
5134 isl_int_init(r);
5135 res = isl_set_dim_residue_class(set, 1, &m, &r);
5136 if (res >= 0 &&
5137 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
5138 isl_die(ctx, isl_error_unknown, "incorrect residue class",
5139 res = -1);
5140 isl_int_clear(r);
5141 isl_int_clear(m);
5142 isl_set_free(set);
5144 return res;
5147 int test_align_parameters(isl_ctx *ctx)
5149 const char *str;
5150 isl_space *space;
5151 isl_multi_aff *ma1, *ma2;
5152 int equal;
5154 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
5155 ma1 = isl_multi_aff_read_from_str(ctx, str);
5157 space = isl_space_params_alloc(ctx, 1);
5158 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
5159 ma1 = isl_multi_aff_align_params(ma1, space);
5161 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
5162 ma2 = isl_multi_aff_read_from_str(ctx, str);
5164 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5166 isl_multi_aff_free(ma1);
5167 isl_multi_aff_free(ma2);
5169 if (equal < 0)
5170 return -1;
5171 if (!equal)
5172 isl_die(ctx, isl_error_unknown,
5173 "result not as expected", return -1);
5175 return 0;
5178 static int test_list(isl_ctx *ctx)
5180 isl_id *a, *b, *c, *d, *id;
5181 isl_id_list *list;
5182 int ok;
5184 a = isl_id_alloc(ctx, "a", NULL);
5185 b = isl_id_alloc(ctx, "b", NULL);
5186 c = isl_id_alloc(ctx, "c", NULL);
5187 d = isl_id_alloc(ctx, "d", NULL);
5189 list = isl_id_list_alloc(ctx, 4);
5190 list = isl_id_list_add(list, a);
5191 list = isl_id_list_add(list, b);
5192 list = isl_id_list_add(list, c);
5193 list = isl_id_list_add(list, d);
5194 list = isl_id_list_drop(list, 1, 1);
5196 if (isl_id_list_n_id(list) != 3) {
5197 isl_id_list_free(list);
5198 isl_die(ctx, isl_error_unknown,
5199 "unexpected number of elements in list", return -1);
5202 id = isl_id_list_get_id(list, 0);
5203 ok = id == a;
5204 isl_id_free(id);
5205 id = isl_id_list_get_id(list, 1);
5206 ok = ok && id == c;
5207 isl_id_free(id);
5208 id = isl_id_list_get_id(list, 2);
5209 ok = ok && id == d;
5210 isl_id_free(id);
5212 isl_id_list_free(list);
5214 if (!ok)
5215 isl_die(ctx, isl_error_unknown,
5216 "unexpected elements in list", return -1);
5218 return 0;
5221 const char *set_conversion_tests[] = {
5222 "[N] -> { [i] : N - 1 <= 2 i <= N }",
5223 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
5224 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5225 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5226 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
5227 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
5228 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
5229 "-3 + c <= d <= 28 + c) }",
5232 /* Check that converting from isl_set to isl_pw_multi_aff and back
5233 * to isl_set produces the original isl_set.
5235 static int test_set_conversion(isl_ctx *ctx)
5237 int i;
5238 const char *str;
5239 isl_set *set1, *set2;
5240 isl_pw_multi_aff *pma;
5241 int equal;
5243 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
5244 str = set_conversion_tests[i];
5245 set1 = isl_set_read_from_str(ctx, str);
5246 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
5247 set2 = isl_set_from_pw_multi_aff(pma);
5248 equal = isl_set_is_equal(set1, set2);
5249 isl_set_free(set1);
5250 isl_set_free(set2);
5252 if (equal < 0)
5253 return -1;
5254 if (!equal)
5255 isl_die(ctx, isl_error_unknown, "bad conversion",
5256 return -1);
5259 return 0;
5262 const char *conversion_tests[] = {
5263 "{ [a, b, c, d] -> s0[a, b, e, f] : "
5264 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
5265 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
5266 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
5267 "9e <= -2 - 2a) }",
5268 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
5269 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
5270 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
5273 /* Check that converting from isl_map to isl_pw_multi_aff and back
5274 * to isl_map produces the original isl_map.
5276 static int test_map_conversion(isl_ctx *ctx)
5278 int i;
5279 isl_map *map1, *map2;
5280 isl_pw_multi_aff *pma;
5281 int equal;
5283 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
5284 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
5285 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
5286 map2 = isl_map_from_pw_multi_aff(pma);
5287 equal = isl_map_is_equal(map1, map2);
5288 isl_map_free(map1);
5289 isl_map_free(map2);
5291 if (equal < 0)
5292 return -1;
5293 if (!equal)
5294 isl_die(ctx, isl_error_unknown, "bad conversion",
5295 return -1);
5298 return 0;
5301 static int test_conversion(isl_ctx *ctx)
5303 if (test_set_conversion(ctx) < 0)
5304 return -1;
5305 if (test_map_conversion(ctx) < 0)
5306 return -1;
5307 return 0;
5310 /* Check that isl_basic_map_curry does not modify input.
5312 static int test_curry(isl_ctx *ctx)
5314 const char *str;
5315 isl_basic_map *bmap1, *bmap2;
5316 int equal;
5318 str = "{ [A[] -> B[]] -> C[] }";
5319 bmap1 = isl_basic_map_read_from_str(ctx, str);
5320 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
5321 equal = isl_basic_map_is_equal(bmap1, bmap2);
5322 isl_basic_map_free(bmap1);
5323 isl_basic_map_free(bmap2);
5325 if (equal < 0)
5326 return -1;
5327 if (equal)
5328 isl_die(ctx, isl_error_unknown,
5329 "curried map should not be equal to original",
5330 return -1);
5332 return 0;
5335 struct {
5336 const char *set;
5337 const char *ma;
5338 const char *res;
5339 } preimage_tests[] = {
5340 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
5341 "{ A[j,i] -> B[i,j] }",
5342 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
5343 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5344 "{ A[a,b] -> B[a/2,b/6] }",
5345 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
5346 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5347 "{ A[a,b] -> B[a/2,b/6] }",
5348 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
5349 "exists i,j : a = 2 i and b = 6 j }" },
5350 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
5351 "[n] -> { : 0 <= n <= 100 }" },
5352 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5353 "{ A[a] -> B[2a] }",
5354 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
5355 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5356 "{ A[a] -> B[([a/2])] }",
5357 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
5358 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
5359 "{ A[a] -> B[a,a,a/3] }",
5360 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
5361 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
5362 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
5365 static int test_preimage_basic_set(isl_ctx *ctx)
5367 int i;
5368 isl_basic_set *bset1, *bset2;
5369 isl_multi_aff *ma;
5370 int equal;
5372 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
5373 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
5374 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
5375 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
5376 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
5377 equal = isl_basic_set_is_equal(bset1, bset2);
5378 isl_basic_set_free(bset1);
5379 isl_basic_set_free(bset2);
5380 if (equal < 0)
5381 return -1;
5382 if (!equal)
5383 isl_die(ctx, isl_error_unknown, "bad preimage",
5384 return -1);
5387 return 0;
5390 struct {
5391 const char *map;
5392 const char *ma;
5393 const char *res;
5394 } preimage_domain_tests[] = {
5395 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
5396 "{ A[j,i] -> B[i,j] }",
5397 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
5398 { "{ B[i] -> C[i]; D[i] -> E[i] }",
5399 "{ A[i] -> B[i + 1] }",
5400 "{ A[i] -> C[i + 1] }" },
5401 { "{ B[i] -> C[i]; B[i] -> E[i] }",
5402 "{ A[i] -> B[i + 1] }",
5403 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
5404 { "{ B[i] -> C[([i/2])] }",
5405 "{ A[i] -> B[2i] }",
5406 "{ A[i] -> C[i] }" },
5407 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
5408 "{ A[i] -> B[([i/5]), ([i/7])] }",
5409 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
5410 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
5411 "[N] -> { A[] -> B[([N/5])] }",
5412 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
5413 { "{ B[i] -> C[i] : exists a : i = 5 a }",
5414 "{ A[i] -> B[2i] }",
5415 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
5416 { "{ B[i] -> C[i] : exists a : i = 2 a; "
5417 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
5418 "{ A[i] -> B[2i] }",
5419 "{ A[i] -> C[2i] }" },
5420 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
5421 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
5424 static int test_preimage_union_map(isl_ctx *ctx)
5426 int i;
5427 isl_union_map *umap1, *umap2;
5428 isl_multi_aff *ma;
5429 int equal;
5431 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
5432 umap1 = isl_union_map_read_from_str(ctx,
5433 preimage_domain_tests[i].map);
5434 ma = isl_multi_aff_read_from_str(ctx,
5435 preimage_domain_tests[i].ma);
5436 umap2 = isl_union_map_read_from_str(ctx,
5437 preimage_domain_tests[i].res);
5438 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
5439 equal = isl_union_map_is_equal(umap1, umap2);
5440 isl_union_map_free(umap1);
5441 isl_union_map_free(umap2);
5442 if (equal < 0)
5443 return -1;
5444 if (!equal)
5445 isl_die(ctx, isl_error_unknown, "bad preimage",
5446 return -1);
5449 return 0;
5452 static int test_preimage(isl_ctx *ctx)
5454 if (test_preimage_basic_set(ctx) < 0)
5455 return -1;
5456 if (test_preimage_union_map(ctx) < 0)
5457 return -1;
5459 return 0;
5462 struct {
5463 const char *ma1;
5464 const char *ma;
5465 const char *res;
5466 } pullback_tests[] = {
5467 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
5468 "{ A[a,b] -> C[b + 2a] }" },
5469 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
5470 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
5471 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
5472 "{ A[a] -> C[(a)/6] }" },
5473 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
5474 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
5475 "{ A[a] -> C[(2a)/3] }" },
5476 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
5477 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
5478 "{ A[i,j] -> C[i + j, i + j] }"},
5479 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
5480 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
5481 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
5482 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
5483 "{ [i, j] -> [floor((i)/3), j] }",
5484 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
5487 static int test_pullback(isl_ctx *ctx)
5489 int i;
5490 isl_multi_aff *ma1, *ma2;
5491 isl_multi_aff *ma;
5492 int equal;
5494 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
5495 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
5496 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
5497 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
5498 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
5499 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5500 isl_multi_aff_free(ma1);
5501 isl_multi_aff_free(ma2);
5502 if (equal < 0)
5503 return -1;
5504 if (!equal)
5505 isl_die(ctx, isl_error_unknown, "bad pullback",
5506 return -1);
5509 return 0;
5512 /* Check that negation is printed correctly and that equal expressions
5513 * are correctly identified.
5515 static int test_ast(isl_ctx *ctx)
5517 isl_ast_expr *expr, *expr1, *expr2, *expr3;
5518 char *str;
5519 int ok, equal;
5521 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5522 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5523 expr = isl_ast_expr_add(expr1, expr2);
5524 expr2 = isl_ast_expr_copy(expr);
5525 expr = isl_ast_expr_neg(expr);
5526 expr2 = isl_ast_expr_neg(expr2);
5527 equal = isl_ast_expr_is_equal(expr, expr2);
5528 str = isl_ast_expr_to_str(expr);
5529 ok = str ? !strcmp(str, "-(A + B)") : -1;
5530 free(str);
5531 isl_ast_expr_free(expr);
5532 isl_ast_expr_free(expr2);
5534 if (ok < 0 || equal < 0)
5535 return -1;
5536 if (!equal)
5537 isl_die(ctx, isl_error_unknown,
5538 "equal expressions not considered equal", return -1);
5539 if (!ok)
5540 isl_die(ctx, isl_error_unknown,
5541 "isl_ast_expr printed incorrectly", return -1);
5543 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5544 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5545 expr = isl_ast_expr_add(expr1, expr2);
5546 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
5547 expr = isl_ast_expr_sub(expr3, expr);
5548 str = isl_ast_expr_to_str(expr);
5549 ok = str ? !strcmp(str, "C - (A + B)") : -1;
5550 free(str);
5551 isl_ast_expr_free(expr);
5553 if (ok < 0)
5554 return -1;
5555 if (!ok)
5556 isl_die(ctx, isl_error_unknown,
5557 "isl_ast_expr printed incorrectly", return -1);
5559 return 0;
5562 /* Check that isl_ast_build_expr_from_set returns a valid expression
5563 * for an empty set. Note that isl_ast_build_expr_from_set getting
5564 * called on an empty set probably indicates a bug in the caller.
5566 static int test_ast_build(isl_ctx *ctx)
5568 isl_set *set;
5569 isl_ast_build *build;
5570 isl_ast_expr *expr;
5572 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5573 build = isl_ast_build_from_context(set);
5575 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
5576 expr = isl_ast_build_expr_from_set(build, set);
5578 isl_ast_expr_free(expr);
5579 isl_ast_build_free(build);
5581 if (!expr)
5582 return -1;
5584 return 0;
5587 /* Internal data structure for before_for and after_for callbacks.
5589 * depth is the current depth
5590 * before is the number of times before_for has been called
5591 * after is the number of times after_for has been called
5593 struct isl_test_codegen_data {
5594 int depth;
5595 int before;
5596 int after;
5599 /* This function is called before each for loop in the AST generated
5600 * from test_ast_gen1.
5602 * Increment the number of calls and the depth.
5603 * Check that the space returned by isl_ast_build_get_schedule_space
5604 * matches the target space of the schedule returned by
5605 * isl_ast_build_get_schedule.
5606 * Return an isl_id that is checked by the corresponding call
5607 * to after_for.
5609 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
5610 void *user)
5612 struct isl_test_codegen_data *data = user;
5613 isl_ctx *ctx;
5614 isl_space *space;
5615 isl_union_map *schedule;
5616 isl_union_set *uset;
5617 isl_set *set;
5618 int empty;
5619 char name[] = "d0";
5621 ctx = isl_ast_build_get_ctx(build);
5623 if (data->before >= 3)
5624 isl_die(ctx, isl_error_unknown,
5625 "unexpected number of for nodes", return NULL);
5626 if (data->depth >= 2)
5627 isl_die(ctx, isl_error_unknown,
5628 "unexpected depth", return NULL);
5630 snprintf(name, sizeof(name), "d%d", data->depth);
5631 data->before++;
5632 data->depth++;
5634 schedule = isl_ast_build_get_schedule(build);
5635 uset = isl_union_map_range(schedule);
5636 if (!uset)
5637 return NULL;
5638 if (isl_union_set_n_set(uset) != 1) {
5639 isl_union_set_free(uset);
5640 isl_die(ctx, isl_error_unknown,
5641 "expecting single range space", return NULL);
5644 space = isl_ast_build_get_schedule_space(build);
5645 set = isl_union_set_extract_set(uset, space);
5646 isl_union_set_free(uset);
5647 empty = isl_set_is_empty(set);
5648 isl_set_free(set);
5650 if (empty < 0)
5651 return NULL;
5652 if (empty)
5653 isl_die(ctx, isl_error_unknown,
5654 "spaces don't match", return NULL);
5656 return isl_id_alloc(ctx, name, NULL);
5659 /* This function is called after each for loop in the AST generated
5660 * from test_ast_gen1.
5662 * Increment the number of calls and decrement the depth.
5663 * Check that the annotation attached to the node matches
5664 * the isl_id returned by the corresponding call to before_for.
5666 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
5667 __isl_keep isl_ast_build *build, void *user)
5669 struct isl_test_codegen_data *data = user;
5670 isl_id *id;
5671 const char *name;
5672 int valid;
5674 data->after++;
5675 data->depth--;
5677 if (data->after > data->before)
5678 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5679 "mismatch in number of for nodes",
5680 return isl_ast_node_free(node));
5682 id = isl_ast_node_get_annotation(node);
5683 if (!id)
5684 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5685 "missing annotation", return isl_ast_node_free(node));
5687 name = isl_id_get_name(id);
5688 valid = name && atoi(name + 1) == data->depth;
5689 isl_id_free(id);
5691 if (!valid)
5692 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5693 "wrong annotation", return isl_ast_node_free(node));
5695 return node;
5698 /* Check that the before_each_for and after_each_for callbacks
5699 * are called for each for loop in the generated code,
5700 * that they are called in the right order and that the isl_id
5701 * returned from the before_each_for callback is attached to
5702 * the isl_ast_node passed to the corresponding after_each_for call.
5704 static int test_ast_gen1(isl_ctx *ctx)
5706 const char *str;
5707 isl_set *set;
5708 isl_union_map *schedule;
5709 isl_ast_build *build;
5710 isl_ast_node *tree;
5711 struct isl_test_codegen_data data;
5713 str = "[N] -> { : N >= 10 }";
5714 set = isl_set_read_from_str(ctx, str);
5715 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
5716 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
5717 schedule = isl_union_map_read_from_str(ctx, str);
5719 data.before = 0;
5720 data.after = 0;
5721 data.depth = 0;
5722 build = isl_ast_build_from_context(set);
5723 build = isl_ast_build_set_before_each_for(build,
5724 &before_for, &data);
5725 build = isl_ast_build_set_after_each_for(build,
5726 &after_for, &data);
5727 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5728 isl_ast_build_free(build);
5729 if (!tree)
5730 return -1;
5732 isl_ast_node_free(tree);
5734 if (data.before != 3 || data.after != 3)
5735 isl_die(ctx, isl_error_unknown,
5736 "unexpected number of for nodes", return -1);
5738 return 0;
5741 /* Check that the AST generator handles domains that are integrally disjoint
5742 * but not rationally disjoint.
5744 static int test_ast_gen2(isl_ctx *ctx)
5746 const char *str;
5747 isl_set *set;
5748 isl_union_map *schedule;
5749 isl_union_map *options;
5750 isl_ast_build *build;
5751 isl_ast_node *tree;
5753 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
5754 schedule = isl_union_map_read_from_str(ctx, str);
5755 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5756 build = isl_ast_build_from_context(set);
5758 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
5759 options = isl_union_map_read_from_str(ctx, str);
5760 build = isl_ast_build_set_options(build, options);
5761 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5762 isl_ast_build_free(build);
5763 if (!tree)
5764 return -1;
5765 isl_ast_node_free(tree);
5767 return 0;
5770 /* Increment *user on each call.
5772 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
5773 __isl_keep isl_ast_build *build, void *user)
5775 int *n = user;
5777 (*n)++;
5779 return node;
5782 /* Test that unrolling tries to minimize the number of instances.
5783 * In particular, for the schedule given below, make sure it generates
5784 * 3 nodes (rather than 101).
5786 static int test_ast_gen3(isl_ctx *ctx)
5788 const char *str;
5789 isl_set *set;
5790 isl_union_map *schedule;
5791 isl_union_map *options;
5792 isl_ast_build *build;
5793 isl_ast_node *tree;
5794 int n_domain = 0;
5796 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
5797 schedule = isl_union_map_read_from_str(ctx, str);
5798 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5800 str = "{ [i] -> unroll[0] }";
5801 options = isl_union_map_read_from_str(ctx, str);
5803 build = isl_ast_build_from_context(set);
5804 build = isl_ast_build_set_options(build, options);
5805 build = isl_ast_build_set_at_each_domain(build,
5806 &count_domains, &n_domain);
5807 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5808 isl_ast_build_free(build);
5809 if (!tree)
5810 return -1;
5812 isl_ast_node_free(tree);
5814 if (n_domain != 3)
5815 isl_die(ctx, isl_error_unknown,
5816 "unexpected number of for nodes", return -1);
5818 return 0;
5821 /* Check that if the ast_build_exploit_nested_bounds options is set,
5822 * we do not get an outer if node in the generated AST,
5823 * while we do get such an outer if node if the options is not set.
5825 static int test_ast_gen4(isl_ctx *ctx)
5827 const char *str;
5828 isl_set *set;
5829 isl_union_map *schedule;
5830 isl_ast_build *build;
5831 isl_ast_node *tree;
5832 enum isl_ast_node_type type;
5833 int enb;
5835 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
5836 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
5838 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
5840 schedule = isl_union_map_read_from_str(ctx, str);
5841 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5842 build = isl_ast_build_from_context(set);
5843 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5844 isl_ast_build_free(build);
5845 if (!tree)
5846 return -1;
5848 type = isl_ast_node_get_type(tree);
5849 isl_ast_node_free(tree);
5851 if (type == isl_ast_node_if)
5852 isl_die(ctx, isl_error_unknown,
5853 "not expecting if node", return -1);
5855 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
5857 schedule = isl_union_map_read_from_str(ctx, str);
5858 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5859 build = isl_ast_build_from_context(set);
5860 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5861 isl_ast_build_free(build);
5862 if (!tree)
5863 return -1;
5865 type = isl_ast_node_get_type(tree);
5866 isl_ast_node_free(tree);
5868 if (type != isl_ast_node_if)
5869 isl_die(ctx, isl_error_unknown,
5870 "expecting if node", return -1);
5872 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
5874 return 0;
5877 /* This function is called for each leaf in the AST generated
5878 * from test_ast_gen5.
5880 * We finalize the AST generation by extending the outer schedule
5881 * with a zero-dimensional schedule. If this results in any for loops,
5882 * then this means that we did not pass along enough information
5883 * about the outer schedule to the inner AST generation.
5885 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
5886 void *user)
5888 isl_union_map *schedule, *extra;
5889 isl_ast_node *tree;
5891 schedule = isl_ast_build_get_schedule(build);
5892 extra = isl_union_map_copy(schedule);
5893 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
5894 schedule = isl_union_map_range_product(schedule, extra);
5895 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5896 isl_ast_build_free(build);
5898 if (!tree)
5899 return NULL;
5901 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
5902 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
5903 "code should not contain any for loop",
5904 return isl_ast_node_free(tree));
5906 return tree;
5909 /* Check that we do not lose any information when going back and
5910 * forth between internal and external schedule.
5912 * In particular, we create an AST where we unroll the only
5913 * non-constant dimension in the schedule. We therefore do
5914 * not expect any for loops in the AST. However, older versions
5915 * of isl would not pass along enough information about the outer
5916 * schedule when performing an inner code generation from a create_leaf
5917 * callback, resulting in the inner code generation producing a for loop.
5919 static int test_ast_gen5(isl_ctx *ctx)
5921 const char *str;
5922 isl_set *set;
5923 isl_union_map *schedule, *options;
5924 isl_ast_build *build;
5925 isl_ast_node *tree;
5927 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
5928 schedule = isl_union_map_read_from_str(ctx, str);
5930 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
5931 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
5932 options = isl_union_map_read_from_str(ctx, str);
5934 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5935 build = isl_ast_build_from_context(set);
5936 build = isl_ast_build_set_options(build, options);
5937 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
5938 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5939 isl_ast_build_free(build);
5940 isl_ast_node_free(tree);
5941 if (!tree)
5942 return -1;
5944 return 0;
5947 /* Check that the expression
5949 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
5951 * is not combined into
5953 * min(n/2, 0)
5955 * as this would result in n/2 being evaluated in parts of
5956 * the definition domain where n is not a multiple of 2.
5958 static int test_ast_expr(isl_ctx *ctx)
5960 const char *str;
5961 isl_pw_aff *pa;
5962 isl_ast_build *build;
5963 isl_ast_expr *expr;
5964 int min_max;
5965 int is_min;
5967 min_max = isl_options_get_ast_build_detect_min_max(ctx);
5968 isl_options_set_ast_build_detect_min_max(ctx, 1);
5970 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
5971 pa = isl_pw_aff_read_from_str(ctx, str);
5972 build = isl_ast_build_alloc(ctx);
5973 expr = isl_ast_build_expr_from_pw_aff(build, pa);
5974 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
5975 isl_ast_expr_get_op_type(expr) == isl_ast_op_min;
5976 isl_ast_build_free(build);
5977 isl_ast_expr_free(expr);
5979 isl_options_set_ast_build_detect_min_max(ctx, min_max);
5981 if (!expr)
5982 return -1;
5983 if (is_min)
5984 isl_die(ctx, isl_error_unknown,
5985 "expressions should not be combined", return -1);
5987 return 0;
5990 static int test_ast_gen(isl_ctx *ctx)
5992 if (test_ast_gen1(ctx) < 0)
5993 return -1;
5994 if (test_ast_gen2(ctx) < 0)
5995 return -1;
5996 if (test_ast_gen3(ctx) < 0)
5997 return -1;
5998 if (test_ast_gen4(ctx) < 0)
5999 return -1;
6000 if (test_ast_gen5(ctx) < 0)
6001 return -1;
6002 if (test_ast_expr(ctx) < 0)
6003 return -1;
6004 return 0;
6007 /* Check if dropping output dimensions from an isl_pw_multi_aff
6008 * works properly.
6010 static int test_pw_multi_aff(isl_ctx *ctx)
6012 const char *str;
6013 isl_pw_multi_aff *pma1, *pma2;
6014 int equal;
6016 str = "{ [i,j] -> [i+j, 4i-j] }";
6017 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
6018 str = "{ [i,j] -> [4i-j] }";
6019 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6021 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
6023 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6025 isl_pw_multi_aff_free(pma1);
6026 isl_pw_multi_aff_free(pma2);
6027 if (equal < 0)
6028 return -1;
6029 if (!equal)
6030 isl_die(ctx, isl_error_unknown,
6031 "expressions not equal", return -1);
6033 return 0;
6036 /* Check that we can properly parse multi piecewise affine expressions
6037 * where the piecewise affine expressions have different domains.
6039 static int test_multi_pw_aff(isl_ctx *ctx)
6041 const char *str;
6042 isl_set *dom, *dom2;
6043 isl_multi_pw_aff *mpa1, *mpa2;
6044 isl_pw_aff *pa;
6045 int equal;
6046 int equal_domain;
6048 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
6049 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
6050 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
6051 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
6052 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
6053 str = "{ [i] -> [(i : i > 0), 2i] }";
6054 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
6056 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
6058 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
6059 dom = isl_pw_aff_domain(pa);
6060 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
6061 dom2 = isl_pw_aff_domain(pa);
6062 equal_domain = isl_set_is_equal(dom, dom2);
6064 isl_set_free(dom);
6065 isl_set_free(dom2);
6066 isl_multi_pw_aff_free(mpa1);
6067 isl_multi_pw_aff_free(mpa2);
6069 if (equal < 0)
6070 return -1;
6071 if (!equal)
6072 isl_die(ctx, isl_error_unknown,
6073 "expressions not equal", return -1);
6075 if (equal_domain < 0)
6076 return -1;
6077 if (equal_domain)
6078 isl_die(ctx, isl_error_unknown,
6079 "domains unexpectedly equal", return -1);
6081 return 0;
6084 /* This is a regression test for a bug where isl_basic_map_simplify
6085 * would end up in an infinite loop. In particular, we construct
6086 * an empty basic set that is not obviously empty.
6087 * isl_basic_set_is_empty marks the basic set as empty.
6088 * After projecting out i3, the variable can be dropped completely,
6089 * but isl_basic_map_simplify refrains from doing so if the basic set
6090 * is empty and would end up in an infinite loop if it didn't test
6091 * explicitly for empty basic maps in the outer loop.
6093 static int test_simplify_1(isl_ctx *ctx)
6095 const char *str;
6096 isl_basic_set *bset;
6097 int empty;
6099 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
6100 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
6101 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
6102 "i3 >= i2 }";
6103 bset = isl_basic_set_read_from_str(ctx, str);
6104 empty = isl_basic_set_is_empty(bset);
6105 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
6106 isl_basic_set_free(bset);
6107 if (!bset)
6108 return -1;
6109 if (!empty)
6110 isl_die(ctx, isl_error_unknown,
6111 "basic set should be empty", return -1);
6113 return 0;
6116 /* Check that the equality in the set description below
6117 * is simplified away.
6119 static int test_simplify_2(isl_ctx *ctx)
6121 const char *str;
6122 isl_basic_set *bset;
6123 isl_bool universe;
6125 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
6126 bset = isl_basic_set_read_from_str(ctx, str);
6127 universe = isl_basic_set_plain_is_universe(bset);
6128 isl_basic_set_free(bset);
6130 if (universe < 0)
6131 return -1;
6132 if (!universe)
6133 isl_die(ctx, isl_error_unknown,
6134 "equality not simplified away", return -1);
6135 return 0;
6138 /* Some simplification tests.
6140 static int test_simplify(isl_ctx *ctx)
6142 if (test_simplify_1(ctx) < 0)
6143 return -1;
6144 if (test_simplify_2(ctx) < 0)
6145 return -1;
6146 return 0;
6149 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
6150 * with gbr context would fail to disable the use of the shifted tableau
6151 * when transferring equalities for the input to the context, resulting
6152 * in invalid sample values.
6154 static int test_partial_lexmin(isl_ctx *ctx)
6156 const char *str;
6157 isl_basic_set *bset;
6158 isl_basic_map *bmap;
6159 isl_map *map;
6161 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
6162 bmap = isl_basic_map_read_from_str(ctx, str);
6163 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
6164 bset = isl_basic_set_read_from_str(ctx, str);
6165 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
6166 isl_map_free(map);
6168 if (!map)
6169 return -1;
6171 return 0;
6174 /* Check that the variable compression performed on the existentially
6175 * quantified variables inside isl_basic_set_compute_divs is not confused
6176 * by the implicit equalities among the parameters.
6178 static int test_compute_divs(isl_ctx *ctx)
6180 const char *str;
6181 isl_basic_set *bset;
6182 isl_set *set;
6184 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
6185 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
6186 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
6187 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
6188 bset = isl_basic_set_read_from_str(ctx, str);
6189 set = isl_basic_set_compute_divs(bset);
6190 isl_set_free(set);
6191 if (!set)
6192 return -1;
6194 return 0;
6197 /* Check that the reaching domain elements and the prefix schedule
6198 * at a leaf node are the same before and after grouping.
6200 static int test_schedule_tree_group_1(isl_ctx *ctx)
6202 int equal;
6203 const char *str;
6204 isl_id *id;
6205 isl_union_set *uset;
6206 isl_multi_union_pw_aff *mupa;
6207 isl_union_pw_multi_aff *upma1, *upma2;
6208 isl_union_set *domain1, *domain2;
6209 isl_union_map *umap1, *umap2;
6210 isl_schedule_node *node;
6212 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
6213 uset = isl_union_set_read_from_str(ctx, str);
6214 node = isl_schedule_node_from_domain(uset);
6215 node = isl_schedule_node_child(node, 0);
6216 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
6217 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6218 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6219 node = isl_schedule_node_child(node, 0);
6220 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
6221 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6222 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6223 node = isl_schedule_node_child(node, 0);
6224 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
6225 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
6226 domain1 = isl_schedule_node_get_domain(node);
6227 id = isl_id_alloc(ctx, "group", NULL);
6228 node = isl_schedule_node_parent(node);
6229 node = isl_schedule_node_group(node, id);
6230 node = isl_schedule_node_child(node, 0);
6231 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
6232 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
6233 domain2 = isl_schedule_node_get_domain(node);
6234 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
6235 if (equal >= 0 && equal)
6236 equal = isl_union_set_is_equal(domain1, domain2);
6237 if (equal >= 0 && equal)
6238 equal = isl_union_map_is_equal(umap1, umap2);
6239 isl_union_map_free(umap1);
6240 isl_union_map_free(umap2);
6241 isl_union_set_free(domain1);
6242 isl_union_set_free(domain2);
6243 isl_union_pw_multi_aff_free(upma1);
6244 isl_union_pw_multi_aff_free(upma2);
6245 isl_schedule_node_free(node);
6247 if (equal < 0)
6248 return -1;
6249 if (!equal)
6250 isl_die(ctx, isl_error_unknown,
6251 "expressions not equal", return -1);
6253 return 0;
6256 /* Check that we can have nested groupings and that the union map
6257 * schedule representation is the same before and after the grouping.
6258 * Note that after the grouping, the union map representation contains
6259 * the domain constraints from the ranges of the expansion nodes,
6260 * while they are missing from the union map representation of
6261 * the tree without expansion nodes.
6263 * Also check that the global expansion is as expected.
6265 static int test_schedule_tree_group_2(isl_ctx *ctx)
6267 int equal, equal_expansion;
6268 const char *str;
6269 isl_id *id;
6270 isl_union_set *uset;
6271 isl_union_map *umap1, *umap2;
6272 isl_union_map *expansion1, *expansion2;
6273 isl_union_set_list *filters;
6274 isl_multi_union_pw_aff *mupa;
6275 isl_schedule *schedule;
6276 isl_schedule_node *node;
6278 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
6279 "S3[i,j] : 0 <= i,j < 10 }";
6280 uset = isl_union_set_read_from_str(ctx, str);
6281 node = isl_schedule_node_from_domain(uset);
6282 node = isl_schedule_node_child(node, 0);
6283 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
6284 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6285 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6286 node = isl_schedule_node_child(node, 0);
6287 str = "{ S1[i,j] }";
6288 uset = isl_union_set_read_from_str(ctx, str);
6289 filters = isl_union_set_list_from_union_set(uset);
6290 str = "{ S2[i,j]; S3[i,j] }";
6291 uset = isl_union_set_read_from_str(ctx, str);
6292 filters = isl_union_set_list_add(filters, uset);
6293 node = isl_schedule_node_insert_sequence(node, filters);
6294 node = isl_schedule_node_child(node, 1);
6295 node = isl_schedule_node_child(node, 0);
6296 str = "{ S2[i,j] }";
6297 uset = isl_union_set_read_from_str(ctx, str);
6298 filters = isl_union_set_list_from_union_set(uset);
6299 str = "{ S3[i,j] }";
6300 uset = isl_union_set_read_from_str(ctx, str);
6301 filters = isl_union_set_list_add(filters, uset);
6302 node = isl_schedule_node_insert_sequence(node, filters);
6304 schedule = isl_schedule_node_get_schedule(node);
6305 umap1 = isl_schedule_get_map(schedule);
6306 uset = isl_schedule_get_domain(schedule);
6307 umap1 = isl_union_map_intersect_domain(umap1, uset);
6308 isl_schedule_free(schedule);
6310 node = isl_schedule_node_parent(node);
6311 node = isl_schedule_node_parent(node);
6312 id = isl_id_alloc(ctx, "group1", NULL);
6313 node = isl_schedule_node_group(node, id);
6314 node = isl_schedule_node_child(node, 1);
6315 node = isl_schedule_node_child(node, 0);
6316 id = isl_id_alloc(ctx, "group2", NULL);
6317 node = isl_schedule_node_group(node, id);
6319 schedule = isl_schedule_node_get_schedule(node);
6320 umap2 = isl_schedule_get_map(schedule);
6321 isl_schedule_free(schedule);
6323 node = isl_schedule_node_root(node);
6324 node = isl_schedule_node_child(node, 0);
6325 expansion1 = isl_schedule_node_get_subtree_expansion(node);
6326 isl_schedule_node_free(node);
6328 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
6329 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
6330 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
6332 expansion2 = isl_union_map_read_from_str(ctx, str);
6334 equal = isl_union_map_is_equal(umap1, umap2);
6335 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
6337 isl_union_map_free(umap1);
6338 isl_union_map_free(umap2);
6339 isl_union_map_free(expansion1);
6340 isl_union_map_free(expansion2);
6342 if (equal < 0 || equal_expansion < 0)
6343 return -1;
6344 if (!equal)
6345 isl_die(ctx, isl_error_unknown,
6346 "expressions not equal", return -1);
6347 if (!equal_expansion)
6348 isl_die(ctx, isl_error_unknown,
6349 "unexpected expansion", return -1);
6351 return 0;
6354 /* Some tests for the isl_schedule_node_group function.
6356 static int test_schedule_tree_group(isl_ctx *ctx)
6358 if (test_schedule_tree_group_1(ctx) < 0)
6359 return -1;
6360 if (test_schedule_tree_group_2(ctx) < 0)
6361 return -1;
6362 return 0;
6365 struct {
6366 const char *set;
6367 const char *dual;
6368 } coef_tests[] = {
6369 { "{ rat: [i] : 0 <= i <= 10 }",
6370 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
6371 { "{ rat: [i] : FALSE }",
6372 "{ rat: coefficients[[cst] -> [a]] }" },
6373 { "{ rat: [i] : }",
6374 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
6377 struct {
6378 const char *set;
6379 const char *dual;
6380 } sol_tests[] = {
6381 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
6382 "{ rat: [i] : 0 <= i <= 10 }" },
6383 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
6384 "{ rat: [i] }" },
6385 { "{ rat: coefficients[[cst] -> [a]] }",
6386 "{ rat: [i] : FALSE }" },
6389 /* Test the basic functionality of isl_basic_set_coefficients and
6390 * isl_basic_set_solutions.
6392 static int test_dual(isl_ctx *ctx)
6394 int i;
6396 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
6397 int equal;
6398 isl_basic_set *bset1, *bset2;
6400 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
6401 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
6402 bset1 = isl_basic_set_coefficients(bset1);
6403 equal = isl_basic_set_is_equal(bset1, bset2);
6404 isl_basic_set_free(bset1);
6405 isl_basic_set_free(bset2);
6406 if (equal < 0)
6407 return -1;
6408 if (!equal)
6409 isl_die(ctx, isl_error_unknown,
6410 "incorrect dual", return -1);
6413 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
6414 int equal;
6415 isl_basic_set *bset1, *bset2;
6417 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
6418 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
6419 bset1 = isl_basic_set_solutions(bset1);
6420 equal = isl_basic_set_is_equal(bset1, bset2);
6421 isl_basic_set_free(bset1);
6422 isl_basic_set_free(bset2);
6423 if (equal < 0)
6424 return -1;
6425 if (!equal)
6426 isl_die(ctx, isl_error_unknown,
6427 "incorrect dual", return -1);
6430 return 0;
6433 struct {
6434 int scale_tile;
6435 int shift_point;
6436 const char *domain;
6437 const char *schedule;
6438 const char *sizes;
6439 const char *tile;
6440 const char *point;
6441 } tile_tests[] = {
6442 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6443 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6444 "{ [32,32] }",
6445 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6446 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6448 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6449 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6450 "{ [32,32] }",
6451 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6452 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6454 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6455 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6456 "{ [32,32] }",
6457 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6458 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6460 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6461 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6462 "{ [32,32] }",
6463 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6464 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6468 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
6469 * tile the band and then check if the tile and point bands have the
6470 * expected partial schedule.
6472 static int test_tile(isl_ctx *ctx)
6474 int i;
6475 int scale;
6476 int shift;
6478 scale = isl_options_get_tile_scale_tile_loops(ctx);
6479 shift = isl_options_get_tile_shift_point_loops(ctx);
6481 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
6482 int opt;
6483 int equal;
6484 const char *str;
6485 isl_union_set *domain;
6486 isl_multi_union_pw_aff *mupa, *mupa2;
6487 isl_schedule_node *node;
6488 isl_multi_val *sizes;
6490 opt = tile_tests[i].scale_tile;
6491 isl_options_set_tile_scale_tile_loops(ctx, opt);
6492 opt = tile_tests[i].shift_point;
6493 isl_options_set_tile_shift_point_loops(ctx, opt);
6495 str = tile_tests[i].domain;
6496 domain = isl_union_set_read_from_str(ctx, str);
6497 node = isl_schedule_node_from_domain(domain);
6498 node = isl_schedule_node_child(node, 0);
6499 str = tile_tests[i].schedule;
6500 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6501 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6502 str = tile_tests[i].sizes;
6503 sizes = isl_multi_val_read_from_str(ctx, str);
6504 node = isl_schedule_node_band_tile(node, sizes);
6506 str = tile_tests[i].tile;
6507 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6508 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6509 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
6510 isl_multi_union_pw_aff_free(mupa);
6511 isl_multi_union_pw_aff_free(mupa2);
6513 node = isl_schedule_node_child(node, 0);
6515 str = tile_tests[i].point;
6516 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6517 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6518 if (equal >= 0 && equal)
6519 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
6520 mupa2);
6521 isl_multi_union_pw_aff_free(mupa);
6522 isl_multi_union_pw_aff_free(mupa2);
6524 isl_schedule_node_free(node);
6526 if (equal < 0)
6527 return -1;
6528 if (!equal)
6529 isl_die(ctx, isl_error_unknown,
6530 "unexpected result", return -1);
6533 isl_options_set_tile_scale_tile_loops(ctx, scale);
6534 isl_options_set_tile_shift_point_loops(ctx, shift);
6536 return 0;
6539 /* Check that the domain hash of a space is equal to the hash
6540 * of the domain of the space.
6542 static int test_domain_hash(isl_ctx *ctx)
6544 isl_map *map;
6545 isl_space *space;
6546 uint32_t hash1, hash2;
6548 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
6549 space = isl_map_get_space(map);
6550 isl_map_free(map);
6551 hash1 = isl_space_get_domain_hash(space);
6552 space = isl_space_domain(space);
6553 hash2 = isl_space_get_hash(space);
6554 isl_space_free(space);
6556 if (!space)
6557 return -1;
6558 if (hash1 != hash2)
6559 isl_die(ctx, isl_error_unknown,
6560 "domain hash not equal to hash of domain", return -1);
6562 return 0;
6565 /* Check that a universe basic set that is not obviously equal to the universe
6566 * is still recognized as being equal to the universe.
6568 static int test_universe(isl_ctx *ctx)
6570 const char *s;
6571 isl_basic_set *bset;
6572 isl_bool is_univ;
6574 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
6575 bset = isl_basic_set_read_from_str(ctx, s);
6576 is_univ = isl_basic_set_is_universe(bset);
6577 isl_basic_set_free(bset);
6579 if (is_univ < 0)
6580 return -1;
6581 if (!is_univ)
6582 isl_die(ctx, isl_error_unknown,
6583 "not recognized as universe set", return -1);
6585 return 0;
6588 /* Sets for which chambers are computed and checked.
6590 const char *chambers_tests[] = {
6591 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
6592 "z >= 0 and z <= C - y and z <= B - x - y }",
6595 /* Add the domain of "cell" to "cells".
6597 static int add_cell(__isl_take isl_cell *cell, void *user)
6599 isl_basic_set_list **cells = user;
6600 isl_basic_set *dom;
6602 dom = isl_cell_get_domain(cell);
6603 isl_cell_free(cell);
6604 *cells = isl_basic_set_list_add(*cells, dom);
6606 return *cells ? 0 : -1;
6609 /* Check that the elements of "list" are pairwise disjoint.
6611 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
6613 int i, j, n;
6615 if (!list)
6616 return isl_stat_error;
6618 n = isl_basic_set_list_n_basic_set(list);
6619 for (i = 0; i < n; ++i) {
6620 isl_basic_set *bset_i;
6622 bset_i = isl_basic_set_list_get_basic_set(list, i);
6623 for (j = i + 1; j < n; ++j) {
6624 isl_basic_set *bset_j;
6625 isl_bool disjoint;
6627 bset_j = isl_basic_set_list_get_basic_set(list, j);
6628 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
6629 isl_basic_set_free(bset_j);
6630 if (!disjoint)
6631 isl_die(isl_basic_set_list_get_ctx(list),
6632 isl_error_unknown, "not disjoint",
6633 break);
6634 if (disjoint < 0 || !disjoint)
6635 break;
6637 isl_basic_set_free(bset_i);
6638 if (j < n)
6639 return isl_stat_error;
6642 return isl_stat_ok;
6645 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
6646 * are pairwise disjoint.
6648 static int test_chambers(isl_ctx *ctx)
6650 int i;
6652 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
6653 isl_basic_set *bset;
6654 isl_vertices *vertices;
6655 isl_basic_set_list *cells;
6656 isl_stat ok;
6658 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
6659 vertices = isl_basic_set_compute_vertices(bset);
6660 cells = isl_basic_set_list_alloc(ctx, 0);
6661 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
6662 &cells) < 0)
6663 cells = isl_basic_set_list_free(cells);
6664 ok = check_pairwise_disjoint(cells);
6665 isl_basic_set_list_free(cells);
6666 isl_vertices_free(vertices);
6667 isl_basic_set_free(bset);
6669 if (ok < 0)
6670 return -1;
6673 return 0;
6676 struct {
6677 const char *name;
6678 int (*fn)(isl_ctx *ctx);
6679 } tests [] = {
6680 { "universe", &test_universe },
6681 { "domain hash", &test_domain_hash },
6682 { "dual", &test_dual },
6683 { "dependence analysis", &test_flow },
6684 { "val", &test_val },
6685 { "compute divs", &test_compute_divs },
6686 { "partial lexmin", &test_partial_lexmin },
6687 { "simplify", &test_simplify },
6688 { "curry", &test_curry },
6689 { "piecewise multi affine expressions", &test_pw_multi_aff },
6690 { "multi piecewise affine expressions", &test_multi_pw_aff },
6691 { "conversion", &test_conversion },
6692 { "list", &test_list },
6693 { "align parameters", &test_align_parameters },
6694 { "preimage", &test_preimage },
6695 { "pullback", &test_pullback },
6696 { "AST", &test_ast },
6697 { "AST build", &test_ast_build },
6698 { "AST generation", &test_ast_gen },
6699 { "eliminate", &test_eliminate },
6700 { "residue class", &test_residue_class },
6701 { "div", &test_div },
6702 { "slice", &test_slice },
6703 { "fixed power", &test_fixed_power },
6704 { "sample", &test_sample },
6705 { "output", &test_output },
6706 { "vertices", &test_vertices },
6707 { "chambers", &test_chambers },
6708 { "fixed", &test_fixed },
6709 { "equal", &test_equal },
6710 { "disjoint", &test_disjoint },
6711 { "product", &test_product },
6712 { "dim_max", &test_dim_max },
6713 { "affine", &test_aff },
6714 { "injective", &test_injective },
6715 { "schedule (whole component)", &test_schedule_whole },
6716 { "schedule (incremental)", &test_schedule_incremental },
6717 { "schedule tree grouping", &test_schedule_tree_group },
6718 { "tile", &test_tile },
6719 { "union_pw", &test_union_pw },
6720 { "eval", &test_eval },
6721 { "parse", &test_parse },
6722 { "single-valued", &test_sv },
6723 { "affine hull", &test_affine_hull },
6724 { "simple_hull", &test_simple_hull },
6725 { "coalesce", &test_coalesce },
6726 { "factorize", &test_factorize },
6727 { "subset", &test_subset },
6728 { "subtract", &test_subtract },
6729 { "intersect", &test_intersect },
6730 { "lexmin", &test_lexmin },
6731 { "min", &test_min },
6732 { "gist", &test_gist },
6733 { "piecewise quasi-polynomials", &test_pwqp },
6734 { "lift", &test_lift },
6735 { "bound", &test_bound },
6736 { "union", &test_union },
6737 { "split periods", &test_split_periods },
6738 { "lexicographic order", &test_lex },
6739 { "bijectivity", &test_bijective },
6740 { "dataflow analysis", &test_dep },
6741 { "reading", &test_read },
6742 { "bounded", &test_bounded },
6743 { "construction", &test_construction },
6744 { "dimension manipulation", &test_dim },
6745 { "map application", &test_application },
6746 { "convex hull", &test_convex_hull },
6747 { "transitive closure", &test_closure },
6750 int main(int argc, char **argv)
6752 int i;
6753 struct isl_ctx *ctx;
6754 struct isl_options *options;
6756 srcdir = getenv("srcdir");
6757 assert(srcdir);
6759 options = isl_options_new_with_defaults();
6760 assert(options);
6761 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
6763 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
6764 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
6765 printf("%s\n", tests[i].name);
6766 if (tests[i].fn(ctx) < 0)
6767 goto error;
6769 isl_ctx_free(ctx);
6770 return 0;
6771 error:
6772 isl_ctx_free(ctx);
6773 return -1;