generalize isl_pw_aff_involves_nan
[isl.git] / isl_test.c
blob538648f7cc6b34e3dc87035d0fc170f08528b48d
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <assert.h>
19 #include <stdio.h>
20 #include <limits.h>
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl_space_private.h>
25 #include <isl/set.h>
26 #include <isl/flow.h>
27 #include <isl_constraint_private.h>
28 #include <isl/polynomial.h>
29 #include <isl/union_set.h>
30 #include <isl/union_map.h>
31 #include <isl_factorization.h>
32 #include <isl/schedule.h>
33 #include <isl/schedule_node.h>
34 #include <isl_options_private.h>
35 #include <isl_vertices_private.h>
36 #include <isl/ast_build.h>
37 #include <isl/val.h>
38 #include <isl/ilp.h>
39 #include <isl_ast_build_expr.h>
40 #include <isl/options.h>
42 #include "isl_srcdir.c"
44 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
46 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
47 char *filename;
48 int length;
49 char *pattern = "%s/test_inputs/%s.%s";
51 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
52 + strlen(suffix) + 1;
53 filename = isl_alloc_array(ctx, char, length);
55 if (!filename)
56 return NULL;
58 sprintf(filename, pattern, srcdir, name, suffix);
60 return filename;
63 void test_parse_map(isl_ctx *ctx, const char *str)
65 isl_map *map;
67 map = isl_map_read_from_str(ctx, str);
68 assert(map);
69 isl_map_free(map);
72 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
74 isl_map *map, *map2;
75 int equal;
77 map = isl_map_read_from_str(ctx, str);
78 map2 = isl_map_read_from_str(ctx, str2);
79 equal = isl_map_is_equal(map, map2);
80 isl_map_free(map);
81 isl_map_free(map2);
83 if (equal < 0)
84 return -1;
85 if (!equal)
86 isl_die(ctx, isl_error_unknown, "maps not equal",
87 return -1);
89 return 0;
92 void test_parse_pwqp(isl_ctx *ctx, const char *str)
94 isl_pw_qpolynomial *pwqp;
96 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
97 assert(pwqp);
98 isl_pw_qpolynomial_free(pwqp);
101 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
103 isl_pw_aff *pwaff;
105 pwaff = isl_pw_aff_read_from_str(ctx, str);
106 assert(pwaff);
107 isl_pw_aff_free(pwaff);
110 /* Check that we can read an isl_multi_val from "str" without errors.
112 static int test_parse_multi_val(isl_ctx *ctx, const char *str)
114 isl_multi_val *mv;
116 mv = isl_multi_val_read_from_str(ctx, str);
117 isl_multi_val_free(mv);
119 return mv ? 0 : -1;
122 /* Pairs of binary relation representations that should represent
123 * the same binary relations.
125 struct {
126 const char *map1;
127 const char *map2;
128 } parse_map_equal_tests[] = {
129 { "{ [x,y] : [([x/2]+y)/3] >= 1 }",
130 "{ [x, y] : 2y >= 6 - x }" },
131 { "{ [x,y] : x <= min(y, 2*y+3) }",
132 "{ [x,y] : x <= y, 2*y + 3 }" },
133 { "{ [x,y] : x >= min(y, 2*y+3) }",
134 "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }" },
135 { "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }",
136 "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }" },
137 { "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }",
138 "{ [i,j] -> [min(i,j)] }" },
139 { "{ [i,j] : i != j }",
140 "{ [i,j] : i < j or i > j }" },
141 { "{ [i,j] : (i+1)*2 >= j }",
142 "{ [i, j] : j <= 2 + 2i }" },
143 { "{ [i] -> [i > 0 ? 4 : 5] }",
144 "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }" },
145 { "[N=2,M] -> { [i=[(M+N)/4]] }",
146 "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }" },
147 { "{ [x] : x >= 0 }",
148 "{ [x] : x-0 >= 0 }" },
149 { "{ [i] : ((i > 10)) }",
150 "{ [i] : i >= 11 }" },
151 { "{ [i] -> [0] }",
152 "{ [i] -> [0 * i] }" },
153 { "{ [a] -> [b] : (not false) }",
154 "{ [a] -> [b] : true }" },
155 { "{ [i] : i/2 <= 5 }",
156 "{ [i] : i <= 10 }" },
157 { "{Sym=[n] [i] : i <= n }",
158 "[n] -> { [i] : i <= n }" },
159 { "{ [*] }",
160 "{ [a] }" },
161 { "{ [i] : 2*floor(i/2) = i }",
162 "{ [i] : exists a : i = 2 a }" },
163 { "{ [a] -> [b] : a = 5 implies b = 5 }",
164 "{ [a] -> [b] : a != 5 or b = 5 }" },
165 { "{ [a] -> [a - 1 : a > 0] }",
166 "{ [a] -> [a - 1] : a > 0 }" },
167 { "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
168 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }" },
169 { "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
170 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
171 { "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
172 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
173 { "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
174 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
175 { "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
176 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
177 { "{ [a,b] -> [i,j] : a,b << i,j }",
178 "{ [a,b] -> [i,j] : a < i or (a = i and b < j) }" },
179 { "{ [a,b] -> [i,j] : a,b <<= i,j }",
180 "{ [a,b] -> [i,j] : a < i or (a = i and b <= j) }" },
181 { "{ [a,b] -> [i,j] : a,b >> i,j }",
182 "{ [a,b] -> [i,j] : a > i or (a = i and b > j) }" },
183 { "{ [a,b] -> [i,j] : a,b >>= i,j }",
184 "{ [a,b] -> [i,j] : a > i or (a = i and b >= j) }" },
185 { "{ [n] -> [i] : exists (a, b, c: 8b <= i - 32a and "
186 "8b >= -7 + i - 32 a and b >= 0 and b <= 3 and "
187 "8c < n - 32a and i < n and c >= 0 and "
188 "c <= 3 and c >= -4a) }",
189 "{ [n] -> [i] : 0 <= i < n }" },
190 { "{ [x] -> [] : exists (a, b: 0 <= a <= 1 and 0 <= b <= 3 and "
191 "2b <= x - 8a and 2b >= -1 + x - 8a) }",
192 "{ [x] -> [] : 0 <= x <= 15 }" },
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 /* Sets described using existentially quantified variables that
637 * can also be described without.
639 static const char *elimination_tests[] = {
640 "{ [i,j] : 2 * [i/2] + 3 * [j/4] <= 10 and 2 i = j }",
641 "{ [m, w] : exists a : w - 2m - 5 <= 3a <= m - 2w }",
642 "{ [m, w] : exists a : w >= 0 and a < m and -1 + w <= a <= 2m - w }",
645 /* Check that redundant existentially quantified variables are
646 * getting removed.
648 static int test_elimination(isl_ctx *ctx)
650 int i;
651 unsigned n;
652 isl_basic_set *bset;
654 for (i = 0; i < ARRAY_SIZE(elimination_tests); ++i) {
655 bset = isl_basic_set_read_from_str(ctx, elimination_tests[i]);
656 n = isl_basic_set_dim(bset, isl_dim_div);
657 isl_basic_set_free(bset);
658 if (!bset)
659 return -1;
660 if (n != 0)
661 isl_die(ctx, isl_error_unknown,
662 "expecting no existentials", return -1);
665 return 0;
668 static int test_div(isl_ctx *ctx)
670 const char *str;
671 int empty;
672 isl_int v;
673 isl_space *dim;
674 isl_set *set;
675 isl_local_space *ls;
676 struct isl_basic_set *bset;
677 struct isl_constraint *c;
679 isl_int_init(v);
681 /* test 1 */
682 dim = isl_space_set_alloc(ctx, 0, 3);
683 bset = isl_basic_set_universe(isl_space_copy(dim));
684 ls = isl_local_space_from_space(dim);
686 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
687 isl_int_set_si(v, -1);
688 isl_constraint_set_constant(c, v);
689 isl_int_set_si(v, 1);
690 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
691 isl_int_set_si(v, 3);
692 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
693 bset = isl_basic_set_add_constraint(bset, c);
695 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
696 isl_int_set_si(v, 1);
697 isl_constraint_set_constant(c, v);
698 isl_int_set_si(v, -1);
699 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
700 isl_int_set_si(v, 3);
701 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
702 bset = isl_basic_set_add_constraint(bset, c);
704 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
706 assert(bset && bset->n_div == 1);
707 isl_local_space_free(ls);
708 isl_basic_set_free(bset);
710 /* test 2 */
711 dim = isl_space_set_alloc(ctx, 0, 3);
712 bset = isl_basic_set_universe(isl_space_copy(dim));
713 ls = isl_local_space_from_space(dim);
715 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
716 isl_int_set_si(v, 1);
717 isl_constraint_set_constant(c, v);
718 isl_int_set_si(v, -1);
719 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
720 isl_int_set_si(v, 3);
721 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
722 bset = isl_basic_set_add_constraint(bset, c);
724 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
725 isl_int_set_si(v, -1);
726 isl_constraint_set_constant(c, v);
727 isl_int_set_si(v, 1);
728 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
729 isl_int_set_si(v, 3);
730 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
731 bset = isl_basic_set_add_constraint(bset, c);
733 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
735 assert(bset && bset->n_div == 1);
736 isl_local_space_free(ls);
737 isl_basic_set_free(bset);
739 /* test 3 */
740 dim = isl_space_set_alloc(ctx, 0, 3);
741 bset = isl_basic_set_universe(isl_space_copy(dim));
742 ls = isl_local_space_from_space(dim);
744 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
745 isl_int_set_si(v, 1);
746 isl_constraint_set_constant(c, v);
747 isl_int_set_si(v, -1);
748 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
749 isl_int_set_si(v, 3);
750 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
751 bset = isl_basic_set_add_constraint(bset, c);
753 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
754 isl_int_set_si(v, -3);
755 isl_constraint_set_constant(c, v);
756 isl_int_set_si(v, 1);
757 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
758 isl_int_set_si(v, 4);
759 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
760 bset = isl_basic_set_add_constraint(bset, c);
762 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
764 assert(bset && bset->n_div == 1);
765 isl_local_space_free(ls);
766 isl_basic_set_free(bset);
768 /* test 4 */
769 dim = isl_space_set_alloc(ctx, 0, 3);
770 bset = isl_basic_set_universe(isl_space_copy(dim));
771 ls = isl_local_space_from_space(dim);
773 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
774 isl_int_set_si(v, 2);
775 isl_constraint_set_constant(c, v);
776 isl_int_set_si(v, -1);
777 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
778 isl_int_set_si(v, 3);
779 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
780 bset = isl_basic_set_add_constraint(bset, c);
782 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
783 isl_int_set_si(v, -1);
784 isl_constraint_set_constant(c, v);
785 isl_int_set_si(v, 1);
786 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
787 isl_int_set_si(v, 6);
788 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
789 bset = isl_basic_set_add_constraint(bset, c);
791 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
793 assert(isl_basic_set_is_empty(bset));
794 isl_local_space_free(ls);
795 isl_basic_set_free(bset);
797 /* test 5 */
798 dim = isl_space_set_alloc(ctx, 0, 3);
799 bset = isl_basic_set_universe(isl_space_copy(dim));
800 ls = isl_local_space_from_space(dim);
802 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
803 isl_int_set_si(v, -1);
804 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
805 isl_int_set_si(v, 3);
806 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
807 bset = isl_basic_set_add_constraint(bset, c);
809 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
810 isl_int_set_si(v, 1);
811 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
812 isl_int_set_si(v, -3);
813 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
814 bset = isl_basic_set_add_constraint(bset, c);
816 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
818 assert(bset && bset->n_div == 0);
819 isl_basic_set_free(bset);
820 isl_local_space_free(ls);
822 /* test 6 */
823 dim = isl_space_set_alloc(ctx, 0, 3);
824 bset = isl_basic_set_universe(isl_space_copy(dim));
825 ls = isl_local_space_from_space(dim);
827 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
828 isl_int_set_si(v, -1);
829 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
830 isl_int_set_si(v, 6);
831 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
832 bset = isl_basic_set_add_constraint(bset, c);
834 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
835 isl_int_set_si(v, 1);
836 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
837 isl_int_set_si(v, -3);
838 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
839 bset = isl_basic_set_add_constraint(bset, c);
841 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
843 assert(bset && bset->n_div == 1);
844 isl_basic_set_free(bset);
845 isl_local_space_free(ls);
847 /* test 7 */
848 /* This test is a bit tricky. We set up an equality
849 * a + 3b + 3c = 6 e0
850 * Normalization of divs creates _two_ divs
851 * a = 3 e0
852 * c - b - e0 = 2 e1
853 * Afterwards e0 is removed again because it has coefficient -1
854 * and we end up with the original equality and div again.
855 * Perhaps we can avoid the introduction of this temporary div.
857 dim = isl_space_set_alloc(ctx, 0, 4);
858 bset = isl_basic_set_universe(isl_space_copy(dim));
859 ls = isl_local_space_from_space(dim);
861 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
862 isl_int_set_si(v, -1);
863 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
864 isl_int_set_si(v, -3);
865 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
866 isl_int_set_si(v, -3);
867 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
868 isl_int_set_si(v, 6);
869 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
870 bset = isl_basic_set_add_constraint(bset, c);
872 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
874 /* Test disabled for now */
876 assert(bset && bset->n_div == 1);
878 isl_local_space_free(ls);
879 isl_basic_set_free(bset);
881 /* test 8 */
882 dim = isl_space_set_alloc(ctx, 0, 5);
883 bset = isl_basic_set_universe(isl_space_copy(dim));
884 ls = isl_local_space_from_space(dim);
886 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
887 isl_int_set_si(v, -1);
888 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
889 isl_int_set_si(v, -3);
890 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
891 isl_int_set_si(v, -3);
892 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
893 isl_int_set_si(v, 6);
894 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
895 bset = isl_basic_set_add_constraint(bset, c);
897 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
898 isl_int_set_si(v, -1);
899 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
900 isl_int_set_si(v, 1);
901 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
902 isl_int_set_si(v, 1);
903 isl_constraint_set_constant(c, v);
904 bset = isl_basic_set_add_constraint(bset, c);
906 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
908 /* Test disabled for now */
910 assert(bset && bset->n_div == 1);
912 isl_local_space_free(ls);
913 isl_basic_set_free(bset);
915 /* test 9 */
916 dim = isl_space_set_alloc(ctx, 0, 4);
917 bset = isl_basic_set_universe(isl_space_copy(dim));
918 ls = isl_local_space_from_space(dim);
920 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
921 isl_int_set_si(v, 1);
922 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
923 isl_int_set_si(v, -1);
924 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
925 isl_int_set_si(v, -2);
926 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
927 bset = isl_basic_set_add_constraint(bset, c);
929 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
930 isl_int_set_si(v, -1);
931 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
932 isl_int_set_si(v, 3);
933 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
934 isl_int_set_si(v, 2);
935 isl_constraint_set_constant(c, v);
936 bset = isl_basic_set_add_constraint(bset, c);
938 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
940 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
942 assert(!isl_basic_set_is_empty(bset));
944 isl_local_space_free(ls);
945 isl_basic_set_free(bset);
947 /* test 10 */
948 dim = isl_space_set_alloc(ctx, 0, 3);
949 bset = isl_basic_set_universe(isl_space_copy(dim));
950 ls = isl_local_space_from_space(dim);
952 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
953 isl_int_set_si(v, 1);
954 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
955 isl_int_set_si(v, -2);
956 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
957 bset = isl_basic_set_add_constraint(bset, c);
959 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
961 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
963 isl_local_space_free(ls);
964 isl_basic_set_free(bset);
966 isl_int_clear(v);
968 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
969 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
970 set = isl_set_read_from_str(ctx, str);
971 set = isl_set_compute_divs(set);
972 isl_set_free(set);
973 if (!set)
974 return -1;
976 if (test_elimination(ctx) < 0)
977 return -1;
979 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
980 set = isl_set_read_from_str(ctx, str);
981 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
982 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
983 empty = isl_set_is_empty(set);
984 isl_set_free(set);
985 if (empty < 0)
986 return -1;
987 if (!empty)
988 isl_die(ctx, isl_error_unknown,
989 "result not as accurate as expected", return -1);
991 return 0;
994 void test_application_case(struct isl_ctx *ctx, const char *name)
996 char *filename;
997 FILE *input;
998 struct isl_basic_set *bset1, *bset2;
999 struct isl_basic_map *bmap;
1001 filename = get_filename(ctx, name, "omega");
1002 assert(filename);
1003 input = fopen(filename, "r");
1004 assert(input);
1006 bset1 = isl_basic_set_read_from_file(ctx, input);
1007 bmap = isl_basic_map_read_from_file(ctx, input);
1009 bset1 = isl_basic_set_apply(bset1, bmap);
1011 bset2 = isl_basic_set_read_from_file(ctx, input);
1013 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1015 isl_basic_set_free(bset1);
1016 isl_basic_set_free(bset2);
1017 free(filename);
1019 fclose(input);
1022 static int test_application(isl_ctx *ctx)
1024 test_application_case(ctx, "application");
1025 test_application_case(ctx, "application2");
1027 return 0;
1030 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1032 char *filename;
1033 FILE *input;
1034 struct isl_basic_set *bset1, *bset2;
1036 filename = get_filename(ctx, name, "polylib");
1037 assert(filename);
1038 input = fopen(filename, "r");
1039 assert(input);
1041 bset1 = isl_basic_set_read_from_file(ctx, input);
1042 bset2 = isl_basic_set_read_from_file(ctx, input);
1044 bset1 = isl_basic_set_affine_hull(bset1);
1046 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1048 isl_basic_set_free(bset1);
1049 isl_basic_set_free(bset2);
1050 free(filename);
1052 fclose(input);
1055 int test_affine_hull(struct isl_ctx *ctx)
1057 const char *str;
1058 isl_set *set;
1059 isl_basic_set *bset, *bset2;
1060 int n;
1061 int subset;
1063 test_affine_hull_case(ctx, "affine2");
1064 test_affine_hull_case(ctx, "affine");
1065 test_affine_hull_case(ctx, "affine3");
1067 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1068 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1069 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1070 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1071 set = isl_set_read_from_str(ctx, str);
1072 bset = isl_set_affine_hull(set);
1073 n = isl_basic_set_dim(bset, isl_dim_div);
1074 isl_basic_set_free(bset);
1075 if (n != 0)
1076 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1077 return -1);
1079 /* Check that isl_map_affine_hull is not confused by
1080 * the reordering of divs in isl_map_align_divs.
1082 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1083 "32e0 = b and 32e1 = c); "
1084 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1085 set = isl_set_read_from_str(ctx, str);
1086 bset = isl_set_affine_hull(set);
1087 isl_basic_set_free(bset);
1088 if (!bset)
1089 return -1;
1091 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1092 "32e2 = 31 + 31e0 }";
1093 set = isl_set_read_from_str(ctx, str);
1094 bset = isl_set_affine_hull(set);
1095 str = "{ [a] : exists e : a = 32 e }";
1096 bset2 = isl_basic_set_read_from_str(ctx, str);
1097 subset = isl_basic_set_is_subset(bset, bset2);
1098 isl_basic_set_free(bset);
1099 isl_basic_set_free(bset2);
1100 if (subset < 0)
1101 return -1;
1102 if (!subset)
1103 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1104 return -1);
1106 return 0;
1109 /* Pairs of maps and the corresponding expected results of
1110 * isl_map_plain_unshifted_simple_hull.
1112 struct {
1113 const char *map;
1114 const char *hull;
1115 } plain_unshifted_simple_hull_tests[] = {
1116 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1117 "{ [i] -> [j] : i >= 1 }" },
1118 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1119 "(j mod 4 = 2 and k mod 6 = n) }",
1120 "{ [n] -> [i,j,k] : j mod 4 = 2 }" },
1123 /* Basic tests for isl_map_plain_unshifted_simple_hull.
1125 static int test_plain_unshifted_simple_hull(isl_ctx *ctx)
1127 int i;
1128 isl_map *map;
1129 isl_basic_map *hull, *expected;
1130 isl_bool equal;
1132 for (i = 0; i < ARRAY_SIZE(plain_unshifted_simple_hull_tests); ++i) {
1133 const char *str;
1134 str = plain_unshifted_simple_hull_tests[i].map;
1135 map = isl_map_read_from_str(ctx, str);
1136 str = plain_unshifted_simple_hull_tests[i].hull;
1137 expected = isl_basic_map_read_from_str(ctx, str);
1138 hull = isl_map_plain_unshifted_simple_hull(map);
1139 equal = isl_basic_map_is_equal(hull, expected);
1140 isl_basic_map_free(hull);
1141 isl_basic_map_free(expected);
1142 if (equal < 0)
1143 return -1;
1144 if (!equal)
1145 isl_die(ctx, isl_error_unknown, "unexpected hull",
1146 return -1);
1149 return 0;
1152 /* Pairs of sets and the corresponding expected results of
1153 * isl_set_unshifted_simple_hull.
1155 struct {
1156 const char *set;
1157 const char *hull;
1158 } unshifted_simple_hull_tests[] = {
1159 { "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1160 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1163 /* Basic tests for isl_set_unshifted_simple_hull.
1165 static int test_unshifted_simple_hull(isl_ctx *ctx)
1167 int i;
1168 isl_set *set;
1169 isl_basic_set *hull, *expected;
1170 isl_bool equal;
1172 for (i = 0; i < ARRAY_SIZE(unshifted_simple_hull_tests); ++i) {
1173 const char *str;
1174 str = unshifted_simple_hull_tests[i].set;
1175 set = isl_set_read_from_str(ctx, str);
1176 str = unshifted_simple_hull_tests[i].hull;
1177 expected = isl_basic_set_read_from_str(ctx, str);
1178 hull = isl_set_unshifted_simple_hull(set);
1179 equal = isl_basic_set_is_equal(hull, expected);
1180 isl_basic_set_free(hull);
1181 isl_basic_set_free(expected);
1182 if (equal < 0)
1183 return -1;
1184 if (!equal)
1185 isl_die(ctx, isl_error_unknown, "unexpected hull",
1186 return -1);
1189 return 0;
1192 static int test_simple_hull(struct isl_ctx *ctx)
1194 const char *str;
1195 isl_set *set;
1196 isl_basic_set *bset;
1197 isl_bool is_empty;
1199 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1200 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1201 set = isl_set_read_from_str(ctx, str);
1202 bset = isl_set_simple_hull(set);
1203 is_empty = isl_basic_set_is_empty(bset);
1204 isl_basic_set_free(bset);
1206 if (is_empty == isl_bool_error)
1207 return -1;
1209 if (is_empty == isl_bool_false)
1210 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1211 return -1);
1213 if (test_plain_unshifted_simple_hull(ctx) < 0)
1214 return -1;
1215 if (test_unshifted_simple_hull(ctx) < 0)
1216 return -1;
1218 return 0;
1221 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1223 char *filename;
1224 FILE *input;
1225 struct isl_basic_set *bset1, *bset2;
1226 struct isl_set *set;
1228 filename = get_filename(ctx, name, "polylib");
1229 assert(filename);
1230 input = fopen(filename, "r");
1231 assert(input);
1233 bset1 = isl_basic_set_read_from_file(ctx, input);
1234 bset2 = isl_basic_set_read_from_file(ctx, input);
1236 set = isl_basic_set_union(bset1, bset2);
1237 bset1 = isl_set_convex_hull(set);
1239 bset2 = isl_basic_set_read_from_file(ctx, input);
1241 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1243 isl_basic_set_free(bset1);
1244 isl_basic_set_free(bset2);
1245 free(filename);
1247 fclose(input);
1250 struct {
1251 const char *set;
1252 const char *hull;
1253 } convex_hull_tests[] = {
1254 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1255 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1256 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1257 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1258 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1259 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1260 "i2 <= 5 and i2 >= 4; "
1261 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1262 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1263 "i2 <= 5 + i0 and i2 >= i0 }" },
1264 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1265 "{ [x, y] : 1 = 0 }" },
1266 { "{ [x, y, z] : 0 <= x, y, z <= 10; [x, y, 0] : x >= 0 and y > 0; "
1267 "[x, y, 0] : x >= 0 and y < 0 }",
1268 "{ [x, y, z] : x >= 0 and 0 <= z <= 10 }" },
1271 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1273 int i;
1274 int orig_convex = ctx->opt->convex;
1275 ctx->opt->convex = convex;
1277 test_convex_hull_case(ctx, "convex0");
1278 test_convex_hull_case(ctx, "convex1");
1279 test_convex_hull_case(ctx, "convex2");
1280 test_convex_hull_case(ctx, "convex3");
1281 test_convex_hull_case(ctx, "convex4");
1282 test_convex_hull_case(ctx, "convex5");
1283 test_convex_hull_case(ctx, "convex6");
1284 test_convex_hull_case(ctx, "convex7");
1285 test_convex_hull_case(ctx, "convex8");
1286 test_convex_hull_case(ctx, "convex9");
1287 test_convex_hull_case(ctx, "convex10");
1288 test_convex_hull_case(ctx, "convex11");
1289 test_convex_hull_case(ctx, "convex12");
1290 test_convex_hull_case(ctx, "convex13");
1291 test_convex_hull_case(ctx, "convex14");
1292 test_convex_hull_case(ctx, "convex15");
1294 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1295 isl_set *set1, *set2;
1296 int equal;
1298 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1299 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1300 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1301 equal = isl_set_is_equal(set1, set2);
1302 isl_set_free(set1);
1303 isl_set_free(set2);
1305 if (equal < 0)
1306 return -1;
1307 if (!equal)
1308 isl_die(ctx, isl_error_unknown,
1309 "unexpected convex hull", return -1);
1312 ctx->opt->convex = orig_convex;
1314 return 0;
1317 static int test_convex_hull(isl_ctx *ctx)
1319 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1320 return -1;
1321 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1322 return -1;
1323 return 0;
1326 void test_gist_case(struct isl_ctx *ctx, const char *name)
1328 char *filename;
1329 FILE *input;
1330 struct isl_basic_set *bset1, *bset2;
1332 filename = get_filename(ctx, name, "polylib");
1333 assert(filename);
1334 input = fopen(filename, "r");
1335 assert(input);
1337 bset1 = isl_basic_set_read_from_file(ctx, input);
1338 bset2 = isl_basic_set_read_from_file(ctx, input);
1340 bset1 = isl_basic_set_gist(bset1, bset2);
1342 bset2 = isl_basic_set_read_from_file(ctx, input);
1344 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1346 isl_basic_set_free(bset1);
1347 isl_basic_set_free(bset2);
1348 free(filename);
1350 fclose(input);
1353 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1355 struct {
1356 const char *map;
1357 const char *context;
1358 const char *gist;
1359 } plain_gist_tests[] = {
1360 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1361 "{ [i] -> [j] : i >= 1 }",
1362 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1363 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1364 "(j mod 4 = 2 and k mod 6 = n) }",
1365 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1366 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1367 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1368 "{ [i] -> [j] : i > j }",
1369 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1372 /* Basic tests for isl_map_plain_gist_basic_map.
1374 static int test_plain_gist(isl_ctx *ctx)
1376 int i;
1378 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1379 const char *str;
1380 int equal;
1381 isl_map *map, *gist;
1382 isl_basic_map *context;
1384 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1385 str = plain_gist_tests[i].context;
1386 context = isl_basic_map_read_from_str(ctx, str);
1387 map = isl_map_plain_gist_basic_map(map, context);
1388 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1389 equal = isl_map_is_equal(map, gist);
1390 isl_map_free(map);
1391 isl_map_free(gist);
1392 if (equal < 0)
1393 return -1;
1394 if (!equal)
1395 isl_die(ctx, isl_error_unknown,
1396 "incorrect gist result", return -1);
1399 return 0;
1402 struct {
1403 const char *set;
1404 const char *context;
1405 const char *gist;
1406 } gist_tests[] = {
1407 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1408 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1409 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1410 "{ [a, b, c] : a <= 15 }" },
1411 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1412 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1413 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1414 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1415 { "{ [m, n, a, b] : a <= 2147 + n }",
1416 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1417 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1418 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1419 "b >= 0) }",
1420 "{ [m, n, ku, kl] }" },
1421 { "{ [a, a, b] : a >= 10 }",
1422 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1423 "{ [a, a, b] : a >= 10 }" },
1424 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1425 "{ [0, j] : j >= 0 }" },
1426 /* Check that no constraints on i6 are introduced in the gist */
1427 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1428 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1429 "5e0 <= 381 - t1 and i4 <= 1) }",
1430 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1431 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1432 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1433 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1434 "20e0 >= 1511 - 4t1 - 5i4) }" },
1435 /* Check that no constraints on i6 are introduced in the gist */
1436 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1437 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1438 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1439 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1440 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1441 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1442 "20e1 <= 1530 - 4t1 - 5i4 and "
1443 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1444 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1445 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1446 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1447 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1448 "10e0 <= -91 + 5i4 + 4i6 and "
1449 "10e0 >= -105 + 5i4 + 4i6) }",
1450 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1451 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1452 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1453 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1454 "{ [a, b, q, p] : b >= 1 + a }",
1455 "{ [a, b, q, p] : false }" },
1456 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1457 "[n] -> { [x] : x mod 32 = 0 }",
1458 "[n] -> { [x = n] }" },
1459 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1460 "{ [x] : x mod 2 = 0 }" },
1461 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1462 "{ [x] : x mod 128 = 0 }" },
1463 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1464 "{ [x] : x mod 3200 = 0 }" },
1465 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1466 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1467 "{ [a, b, c = a] }" },
1468 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1469 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1470 "{ [a, b, c = a] : a mod 3 = 0 }" },
1471 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1472 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1473 "{ [x] }" },
1474 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1475 "{ [x,y] : 1 <= y <= 3 }",
1476 "{ [x,y] }" },
1479 /* Check that isl_set_gist behaves as expected.
1481 * For the test cases in gist_tests, besides checking that the result
1482 * is as expected, also check that applying the gist operation does
1483 * not modify the input set (an earlier version of isl would do that) and
1484 * that the test case is consistent, i.e., that the gist has the same
1485 * intersection with the context as the input set.
1487 static int test_gist(struct isl_ctx *ctx)
1489 int i;
1490 const char *str;
1491 isl_basic_set *bset1, *bset2;
1492 isl_map *map1, *map2;
1493 int equal;
1495 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1496 int equal_input, equal_intersection;
1497 isl_set *set1, *set2, *copy, *context;
1499 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1500 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1501 copy = isl_set_copy(set1);
1502 set1 = isl_set_gist(set1, isl_set_copy(context));
1503 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1504 equal = isl_set_is_equal(set1, set2);
1505 isl_set_free(set1);
1506 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1507 equal_input = isl_set_is_equal(set1, copy);
1508 isl_set_free(copy);
1509 set1 = isl_set_intersect(set1, isl_set_copy(context));
1510 set2 = isl_set_intersect(set2, context);
1511 equal_intersection = isl_set_is_equal(set1, set2);
1512 isl_set_free(set2);
1513 isl_set_free(set1);
1514 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1515 return -1;
1516 if (!equal)
1517 isl_die(ctx, isl_error_unknown,
1518 "incorrect gist result", return -1);
1519 if (!equal_input)
1520 isl_die(ctx, isl_error_unknown,
1521 "gist modified input", return -1);
1522 if (!equal_input)
1523 isl_die(ctx, isl_error_unknown,
1524 "inconsistent gist test case", return -1);
1527 test_gist_case(ctx, "gist1");
1529 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1530 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1531 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1532 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1533 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1534 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1535 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1536 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1537 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1538 bset1 = isl_basic_set_read_from_str(ctx, str);
1539 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1540 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1541 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1542 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1543 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1544 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1545 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1546 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1547 bset2 = isl_basic_set_read_from_str(ctx, str);
1548 bset1 = isl_basic_set_gist(bset1, bset2);
1549 assert(bset1 && bset1->n_div == 0);
1550 isl_basic_set_free(bset1);
1552 /* Check that the integer divisions of the second disjunct
1553 * do not spread to the first disjunct.
1555 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1556 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1557 "(exists (e0 = [(-1 + t1)/16], "
1558 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1559 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1560 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1561 "o0 <= 4294967295 and t1 <= -1)) }";
1562 map1 = isl_map_read_from_str(ctx, str);
1563 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1564 map2 = isl_map_read_from_str(ctx, str);
1565 map1 = isl_map_gist(map1, map2);
1566 if (!map1)
1567 return -1;
1568 if (map1->n != 1)
1569 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1570 isl_map_free(map1); return -1);
1571 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1572 isl_die(ctx, isl_error_unknown, "expecting single div",
1573 isl_map_free(map1); return -1);
1574 isl_map_free(map1);
1576 if (test_plain_gist(ctx) < 0)
1577 return -1;
1579 return 0;
1582 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1584 isl_set *set, *set2;
1585 int equal;
1586 int one;
1588 set = isl_set_read_from_str(ctx, str);
1589 set = isl_set_coalesce(set);
1590 set2 = isl_set_read_from_str(ctx, str);
1591 equal = isl_set_is_equal(set, set2);
1592 one = set && set->n == 1;
1593 isl_set_free(set);
1594 isl_set_free(set2);
1596 if (equal < 0)
1597 return -1;
1598 if (!equal)
1599 isl_die(ctx, isl_error_unknown,
1600 "coalesced set not equal to input", return -1);
1601 if (check_one && !one)
1602 isl_die(ctx, isl_error_unknown,
1603 "coalesced set should not be a union", return -1);
1605 return 0;
1608 /* Inputs for coalescing tests with unbounded wrapping.
1609 * "str" is a string representation of the input set.
1610 * "single_disjunct" is set if we expect the result to consist of
1611 * a single disjunct.
1613 struct {
1614 int single_disjunct;
1615 const char *str;
1616 } coalesce_unbounded_tests[] = {
1617 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1618 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1619 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1620 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1621 "-10 <= y <= 0}" },
1622 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1623 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1624 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1625 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1628 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1629 * option turned off.
1631 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1633 int i;
1634 int r = 0;
1635 int bounded;
1637 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1638 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1640 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1641 const char *str = coalesce_unbounded_tests[i].str;
1642 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1643 if (test_coalesce_set(ctx, str, check_one) >= 0)
1644 continue;
1645 r = -1;
1646 break;
1649 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1651 return r;
1654 /* Inputs for coalescing tests.
1655 * "str" is a string representation of the input set.
1656 * "single_disjunct" is set if we expect the result to consist of
1657 * a single disjunct.
1659 struct {
1660 int single_disjunct;
1661 const char *str;
1662 } coalesce_tests[] = {
1663 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1664 "y >= x & x >= 2 & 5 >= y }" },
1665 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1666 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1667 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1668 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1669 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1670 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1671 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1672 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1673 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1674 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1675 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1676 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1677 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1678 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1679 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1680 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1681 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1682 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1683 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1684 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1685 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1686 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1687 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1688 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1689 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1690 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1691 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1692 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1693 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1694 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1695 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1696 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1697 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1698 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1699 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1700 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1701 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1702 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1703 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1704 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1705 "[o0, o1, o2, o3, o4, o5, o6]] : "
1706 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1707 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1708 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1709 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1710 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1711 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1712 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1713 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1714 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1715 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1716 "o6 >= i3 + i6 - o3 and M >= 0 and "
1717 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1718 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1719 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1720 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1721 "(o0 = 0 and M >= 2 and N >= 3) or "
1722 "(M = 0 and o0 = 0 and N >= 3) }" },
1723 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1724 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1725 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1726 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1727 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1728 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1729 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1730 "(y = 3 and x = 1) }" },
1731 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1732 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1733 "i1 <= M and i3 <= M and i4 <= M) or "
1734 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1735 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1736 "i4 <= -1 + M) }" },
1737 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1738 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1739 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1740 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1741 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1742 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1743 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1744 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1745 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1746 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1747 { 0, "{ [a, b] : exists e : 2e = a and "
1748 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1749 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1750 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1751 "j >= 1 and j' <= i + j - i' and i >= 1; "
1752 "[1, 1, 1, 1] }" },
1753 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1754 "[i,j] : exists a : j = 3a }" },
1755 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1756 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1757 "a >= 3) or "
1758 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1759 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1760 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1761 "c <= 6 + 8a and a >= 3; "
1762 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1763 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1764 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1765 "[x,0] : 3 <= x <= 4 }" },
1766 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1767 "[x,0] : 4 <= x <= 5 }" },
1768 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1769 "[x,0] : 3 <= x <= 5 }" },
1770 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1771 "[x,0] : 3 <= x <= 4 }" },
1772 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1773 "i1 <= 0; "
1774 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1775 { 1, "{ [0,0]; [1,1] }" },
1776 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1777 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1778 "ii <= k;"
1779 "[k, 0, k] : k <= 6 and k >= 1 }" },
1780 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1781 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1782 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1783 { 1, "[n] -> { [1] : n >= 0;"
1784 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1785 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1786 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1787 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1788 "2e0 <= x and 2e0 <= n);"
1789 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1790 "n >= 0) }" },
1791 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1792 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1793 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1794 "t1 = 1 }" },
1795 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1796 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1797 "[0, 0] }" },
1798 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1799 "t1 >= 13 and t1 <= 16);"
1800 "[t1] : t1 <= 15 and t1 >= 12 }" },
1801 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
1802 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
1803 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
1804 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
1805 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
1806 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
1807 "i <= 4j + 2 }" },
1808 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
1809 "(exists (e0 : 3e0 = -2 + c0)) }" },
1810 { 0, "[n, b0, t0] -> "
1811 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1812 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1813 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1814 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1815 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
1816 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
1817 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
1818 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
1819 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1820 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1821 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1822 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
1823 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
1824 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
1825 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
1826 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
1827 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
1828 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
1829 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
1830 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
1831 { 0, "{ [i0, i1, i2] : "
1832 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
1833 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
1834 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
1835 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
1836 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
1837 "32e0 <= 31 + i0)) or "
1838 "i0 >= 0 }" },
1839 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
1840 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
1841 "2*floor((c)/2) = c and 0 <= a <= 192;"
1842 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
1844 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
1845 "(0 <= a <= b <= n) }" },
1846 { 1, "{ [a, b] : 0 <= a <= 2 and b >= 0 and "
1847 "((0 < b <= 13) or (2*floor((a + b)/2) >= -5 + a + 2b)) }" },
1848 { 1, "{ [a] : (2 <= a <= 5) or (a mod 2 = 1 and 1 <= a <= 5) }" },
1849 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
1850 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1851 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
1852 "b = 3 and 9e0 <= -19 + 2c)) }" },
1853 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
1854 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1855 "(a = 4 and b = 3 and "
1856 "9*floor((-16 + 2c)/9) <= -19 + 2c) }" },
1857 { 0, "{ [a, b, c] : (b <= 2 and b <= -2 + a) or "
1858 "(b = -1 + a and 0 < a <= 3 and "
1859 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1860 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
1861 "b = 3 and 9e0 <= -19 + 2c)) }" },
1862 { 1, "{ [y, x] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
1863 "[1, 0] }" },
1864 { 1, "{ [x, y] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
1865 "[0, 1] }" },
1866 { 1, "{ [1, y] : -1 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
1867 { 1, "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
1868 { 1, "{ [x, y] : 0 <= x <= 10 and x - 4*floor(x/4) <= 1 and y <= 0; "
1869 "[x, y] : 0 <= x <= 10 and x - 4*floor(x/4) > 1 and y <= 0; "
1870 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) <= 1 and 0 < y; "
1871 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) > 1 and 0 < y }" },
1872 { 1, "{ [x, 0] : 0 <= x <= 10 and x mod 2 = 0; "
1873 "[x, 0] : 0 <= x <= 10 and x mod 2 = 1; "
1874 "[x, y] : 0 <= x <= 10 and 1 <= y <= 10 }" },
1875 { 1, "{ [a] : a <= 8 and "
1876 "(a mod 10 = 7 or a mod 10 = 8 or a mod 10 = 9) }" },
1877 { 1, "{ [x, y] : 2y = -x and x <= 0 or "
1878 "x <= -1 and 2y <= -x - 1 and 2y >= x - 1 }" },
1879 { 0, "{ [x, y] : 2y = -x and x <= 0 or "
1880 "x <= -2 and 2y <= -x - 1 and 2y >= x - 1 }" },
1881 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
1882 "(a < 0 and 3*floor((a)/3) < a) }" },
1883 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
1884 "(a < -1 and 3*floor((a)/3) < a) }" },
1885 { 1, "{ [a, b] : a <= 1024 and b >= 0 and "
1886 "((-31 - a + b <= 32*floor((-1 - a)/32) <= -33 + b and "
1887 "32*floor((-1 - a)/32) <= -16 + b + 16*floor((-1 - a)/16))"
1888 "or (2 <= a <= 15 and b < a)) }" },
1889 { 1, "{ [a] : a > 0 and ((16*floor((a)/16) < a and "
1890 "32*floor((a)/32) < a) or a <= 15) }" },
1891 { 1, "{ [a, b, c, d] : (-a + d) mod 64 = 0 and a <= 8 and b <= 1 and "
1892 "10 - a <= c <= 3 and d >= 5 and 9 - 64b <= d <= 70;"
1893 "[a, b = 1, c, d] : (-a + d) mod 64 = 0 and a <= 8 and c >= 4 and "
1894 "10 - a <= c <= 5 and 5 <= d <= 73 - c }" },
1895 { 1, "[n, m] -> { S_0[i] : (-n + i) mod 3 = 0 and m >= 3 + n and "
1896 "i >= n and 3*floor((2 + n + 2m)/3) <= n + 3m - i; "
1897 "S_0[n] : n <= m <= 2 + n }" },
1898 { 1, "{ [a, b] : exists (e0: 0 <= a <= 1 and b >= 0 and "
1899 "2e0 >= -5 + a + 2b and 2e0 >= -1 + a + b and "
1900 "2e0 <= a + b); "
1901 "[a, b] : exists (e0: 0 <= a <= 1 and 2e0 >= -5 + a + 2b and "
1902 "2e0 >= -1 - a + b and 2e0 <= -a + b and "
1903 "2e0 < -a + 2b) }" },
1904 { 1, "{ [i, j, i - 8j] : 8 <= i <= 63 and -7 + i <= 8j <= i; "
1905 "[i, 0, i] : 0 <= i <= 7 }" },
1908 /* A specialized coalescing test case that would result
1909 * in a segmentation fault or a failed assertion in earlier versions of isl.
1911 static int test_coalesce_special(struct isl_ctx *ctx)
1913 const char *str;
1914 isl_map *map1, *map2;
1916 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1917 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
1918 "(y = 201 and o1 <= 239 and o1 >= 212) or "
1919 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
1920 "o1 <= 239 and o1 >= 212)) or "
1921 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
1922 "o1 <= 241 and o1 >= 240));"
1923 "[S_L220_OUT[] -> T7[]] -> "
1924 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
1925 "(y = 2 and o1 <= 241 and o1 >= 212) or "
1926 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
1927 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
1928 map1 = isl_map_read_from_str(ctx, str);
1929 map1 = isl_map_align_divs(map1);
1930 map1 = isl_map_coalesce(map1);
1931 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1932 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
1933 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
1934 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
1935 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
1936 map2 = isl_map_read_from_str(ctx, str);
1937 map2 = isl_map_union(map2, map1);
1938 map2 = isl_map_align_divs(map2);
1939 map2 = isl_map_coalesce(map2);
1940 isl_map_free(map2);
1941 if (!map2)
1942 return -1;
1944 return 0;
1947 /* A specialized coalescing test case that would result in an assertion
1948 * in an earlier version of isl.
1949 * The explicit call to isl_basic_set_union prevents the implicit
1950 * equality constraints in the first basic map from being detected prior
1951 * to the call to isl_set_coalesce, at least at the point
1952 * where this test case was introduced.
1954 static int test_coalesce_special2(struct isl_ctx *ctx)
1956 const char *str;
1957 isl_basic_set *bset1, *bset2;
1958 isl_set *set;
1960 str = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
1961 bset1 = isl_basic_set_read_from_str(ctx, str);
1962 str = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }" ;
1963 bset2 = isl_basic_set_read_from_str(ctx, str);
1964 set = isl_basic_set_union(bset1, bset2);
1965 set = isl_set_coalesce(set);
1966 isl_set_free(set);
1968 if (!set)
1969 return -1;
1970 return 0;
1973 /* Test the functionality of isl_set_coalesce.
1974 * That is, check that the output is always equal to the input
1975 * and in some cases that the result consists of a single disjunct.
1977 static int test_coalesce(struct isl_ctx *ctx)
1979 int i;
1981 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1982 const char *str = coalesce_tests[i].str;
1983 int check_one = coalesce_tests[i].single_disjunct;
1984 if (test_coalesce_set(ctx, str, check_one) < 0)
1985 return -1;
1988 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1989 return -1;
1990 if (test_coalesce_special(ctx) < 0)
1991 return -1;
1992 if (test_coalesce_special2(ctx) < 0)
1993 return -1;
1995 return 0;
1998 /* Construct a representation of the graph on the right of Figure 1
1999 * in "Computing the Transitive Closure of a Union of
2000 * Affine Integer Tuple Relations".
2002 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2004 isl_set *dom;
2005 isl_map *up, *right;
2007 dom = isl_set_read_from_str(ctx,
2008 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2009 "2 x - 3 y + 3 >= 0 }");
2010 right = isl_map_read_from_str(ctx,
2011 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2012 up = isl_map_read_from_str(ctx,
2013 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2014 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2015 right = isl_map_intersect_range(right, isl_set_copy(dom));
2016 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2017 up = isl_map_intersect_range(up, dom);
2018 return isl_map_union(up, right);
2021 /* Construct a representation of the power of the graph
2022 * on the right of Figure 1 in "Computing the Transitive Closure of
2023 * a Union of Affine Integer Tuple Relations".
2025 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2027 return isl_map_read_from_str(ctx,
2028 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2029 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2030 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2033 /* Construct a representation of the transitive closure of the graph
2034 * on the right of Figure 1 in "Computing the Transitive Closure of
2035 * a Union of Affine Integer Tuple Relations".
2037 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2039 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2042 static int test_closure(isl_ctx *ctx)
2044 const char *str;
2045 isl_map *map, *map2;
2046 int exact, equal;
2048 /* COCOA example 1 */
2049 map = isl_map_read_from_str(ctx,
2050 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2051 "1 <= i and i < n and 1 <= j and j < n or "
2052 "i2 = i + 1 and j2 = j - 1 and "
2053 "1 <= i and i < n and 2 <= j and j <= n }");
2054 map = isl_map_power(map, &exact);
2055 assert(exact);
2056 isl_map_free(map);
2058 /* COCOA example 1 */
2059 map = isl_map_read_from_str(ctx,
2060 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2061 "1 <= i and i < n and 1 <= j and j < n or "
2062 "i2 = i + 1 and j2 = j - 1 and "
2063 "1 <= i and i < n and 2 <= j and j <= n }");
2064 map = isl_map_transitive_closure(map, &exact);
2065 assert(exact);
2066 map2 = isl_map_read_from_str(ctx,
2067 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2068 "1 <= i and i < n and 1 <= j and j <= n and "
2069 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2070 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2071 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2072 assert(isl_map_is_equal(map, map2));
2073 isl_map_free(map2);
2074 isl_map_free(map);
2076 map = isl_map_read_from_str(ctx,
2077 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2078 " 0 <= y and y <= n }");
2079 map = isl_map_transitive_closure(map, &exact);
2080 map2 = isl_map_read_from_str(ctx,
2081 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2082 " 0 <= y and y <= n }");
2083 assert(isl_map_is_equal(map, map2));
2084 isl_map_free(map2);
2085 isl_map_free(map);
2087 /* COCOA example 2 */
2088 map = isl_map_read_from_str(ctx,
2089 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2090 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2091 "i2 = i + 2 and j2 = j - 2 and "
2092 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2093 map = isl_map_transitive_closure(map, &exact);
2094 assert(exact);
2095 map2 = isl_map_read_from_str(ctx,
2096 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2097 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2098 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2099 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2100 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2101 assert(isl_map_is_equal(map, map2));
2102 isl_map_free(map);
2103 isl_map_free(map2);
2105 /* COCOA Fig.2 left */
2106 map = isl_map_read_from_str(ctx,
2107 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2108 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2109 "j <= n or "
2110 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2111 "j <= 2 i - 3 and j <= n - 2 or "
2112 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2113 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2114 map = isl_map_transitive_closure(map, &exact);
2115 assert(exact);
2116 isl_map_free(map);
2118 /* COCOA Fig.2 right */
2119 map = isl_map_read_from_str(ctx,
2120 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2121 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2122 "j <= n or "
2123 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2124 "j <= 2 i - 4 and j <= n - 3 or "
2125 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2126 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2127 map = isl_map_power(map, &exact);
2128 assert(exact);
2129 isl_map_free(map);
2131 /* COCOA Fig.2 right */
2132 map = isl_map_read_from_str(ctx,
2133 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2134 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2135 "j <= n or "
2136 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2137 "j <= 2 i - 4 and j <= n - 3 or "
2138 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2139 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2140 map = isl_map_transitive_closure(map, &exact);
2141 assert(exact);
2142 map2 = isl_map_read_from_str(ctx,
2143 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2144 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2145 "j <= n and 3 + i + 2 j <= 3 n and "
2146 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2147 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2148 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2149 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2150 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2151 assert(isl_map_is_equal(map, map2));
2152 isl_map_free(map2);
2153 isl_map_free(map);
2155 map = cocoa_fig_1_right_graph(ctx);
2156 map = isl_map_transitive_closure(map, &exact);
2157 assert(exact);
2158 map2 = cocoa_fig_1_right_tc(ctx);
2159 assert(isl_map_is_equal(map, map2));
2160 isl_map_free(map2);
2161 isl_map_free(map);
2163 map = cocoa_fig_1_right_graph(ctx);
2164 map = isl_map_power(map, &exact);
2165 map2 = cocoa_fig_1_right_power(ctx);
2166 equal = isl_map_is_equal(map, map2);
2167 isl_map_free(map2);
2168 isl_map_free(map);
2169 if (equal < 0)
2170 return -1;
2171 if (!exact)
2172 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2173 if (!equal)
2174 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2176 /* COCOA Theorem 1 counter example */
2177 map = isl_map_read_from_str(ctx,
2178 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2179 "i2 = 1 and j2 = j or "
2180 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2181 map = isl_map_transitive_closure(map, &exact);
2182 assert(exact);
2183 isl_map_free(map);
2185 map = isl_map_read_from_str(ctx,
2186 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2187 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2188 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2189 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2190 map = isl_map_transitive_closure(map, &exact);
2191 assert(exact);
2192 isl_map_free(map);
2194 /* Kelly et al 1996, fig 12 */
2195 map = isl_map_read_from_str(ctx,
2196 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2197 "1 <= i,j,j+1 <= n or "
2198 "j = n and j2 = 1 and i2 = i + 1 and "
2199 "1 <= i,i+1 <= n }");
2200 map = isl_map_transitive_closure(map, &exact);
2201 assert(exact);
2202 map2 = isl_map_read_from_str(ctx,
2203 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2204 "1 <= i <= n and i = i2 or "
2205 "1 <= i < i2 <= n and 1 <= j <= n and "
2206 "1 <= j2 <= n }");
2207 assert(isl_map_is_equal(map, map2));
2208 isl_map_free(map2);
2209 isl_map_free(map);
2211 /* Omega's closure4 */
2212 map = isl_map_read_from_str(ctx,
2213 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2214 "1 <= x,y <= 10 or "
2215 "x2 = x + 1 and y2 = y and "
2216 "1 <= x <= 20 && 5 <= y <= 15 }");
2217 map = isl_map_transitive_closure(map, &exact);
2218 assert(exact);
2219 isl_map_free(map);
2221 map = isl_map_read_from_str(ctx,
2222 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2223 map = isl_map_transitive_closure(map, &exact);
2224 assert(!exact);
2225 map2 = isl_map_read_from_str(ctx,
2226 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2227 assert(isl_map_is_equal(map, map2));
2228 isl_map_free(map);
2229 isl_map_free(map2);
2231 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2232 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2233 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2234 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2235 map = isl_map_read_from_str(ctx, str);
2236 map = isl_map_transitive_closure(map, &exact);
2237 assert(exact);
2238 map2 = isl_map_read_from_str(ctx, str);
2239 assert(isl_map_is_equal(map, map2));
2240 isl_map_free(map);
2241 isl_map_free(map2);
2243 str = "{[0] -> [1]; [2] -> [3]}";
2244 map = isl_map_read_from_str(ctx, str);
2245 map = isl_map_transitive_closure(map, &exact);
2246 assert(exact);
2247 map2 = isl_map_read_from_str(ctx, str);
2248 assert(isl_map_is_equal(map, map2));
2249 isl_map_free(map);
2250 isl_map_free(map2);
2252 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2253 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2254 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2255 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2256 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2257 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2258 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2259 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2260 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2261 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2262 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2263 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2264 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2265 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2266 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2267 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2268 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2269 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2270 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2271 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2272 map = isl_map_read_from_str(ctx, str);
2273 map = isl_map_transitive_closure(map, NULL);
2274 assert(map);
2275 isl_map_free(map);
2277 return 0;
2280 static int test_lex(struct isl_ctx *ctx)
2282 isl_space *dim;
2283 isl_map *map;
2284 int empty;
2286 dim = isl_space_set_alloc(ctx, 0, 0);
2287 map = isl_map_lex_le(dim);
2288 empty = isl_map_is_empty(map);
2289 isl_map_free(map);
2291 if (empty < 0)
2292 return -1;
2293 if (empty)
2294 isl_die(ctx, isl_error_unknown,
2295 "expecting non-empty result", return -1);
2297 return 0;
2300 /* Inputs for isl_map_lexmin tests.
2301 * "map" is the input and "lexmin" is the expected result.
2303 struct {
2304 const char *map;
2305 const char *lexmin;
2306 } lexmin_tests [] = {
2307 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
2308 "{ [x] -> [5] : 6 <= x <= 8; "
2309 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
2310 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
2311 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
2312 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
2313 "{ [x] -> [y] : (4y = x and x >= 0) or "
2314 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2315 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2316 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
2317 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
2318 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
2319 /* Check that empty pieces are properly combined. */
2320 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2321 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2322 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
2323 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2324 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2325 "x >= 4 }" },
2326 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
2327 "a <= 255 and c <= 255 and d <= 255 - j and "
2328 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
2329 "247d <= 247 + k - j and 247d <= 247 + k - b and "
2330 "247d <= 247 + i and 248 - b <= 248d <= c and "
2331 "254d >= i - a + b and 254d >= -a + b and "
2332 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
2333 "{ [i, k, j] -> "
2334 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
2335 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
2336 "247j >= 62738 - i and 509j <= 129795 + i and "
2337 "742j >= 188724 - i; "
2338 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
2339 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
2340 "16*floor((8 + b)/16) <= 7 + b; "
2341 "[a] -> [1] }",
2342 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
2343 "[a] -> [b = 0] : 0 < a <= 509 }" },
2344 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
2345 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
2348 static int test_lexmin(struct isl_ctx *ctx)
2350 int i;
2351 int equal;
2352 const char *str;
2353 isl_basic_map *bmap;
2354 isl_map *map, *map2;
2355 isl_set *set;
2356 isl_set *set2;
2357 isl_pw_multi_aff *pma;
2359 str = "[p0, p1] -> { [] -> [] : "
2360 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2361 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2362 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2363 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2364 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2365 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2366 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2367 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2368 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2369 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2370 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2371 map = isl_map_read_from_str(ctx, str);
2372 map = isl_map_lexmin(map);
2373 isl_map_free(map);
2375 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2376 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2377 set = isl_set_read_from_str(ctx, str);
2378 set = isl_set_lexmax(set);
2379 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2380 set2 = isl_set_read_from_str(ctx, str);
2381 set = isl_set_intersect(set, set2);
2382 assert(!isl_set_is_empty(set));
2383 isl_set_free(set);
2385 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
2386 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
2387 map = isl_map_lexmin(map);
2388 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
2389 equal = isl_map_is_equal(map, map2);
2390 isl_map_free(map);
2391 isl_map_free(map2);
2393 if (equal < 0)
2394 return -1;
2395 if (!equal)
2396 isl_die(ctx, isl_error_unknown,
2397 "unexpected result", return -1);
2400 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2401 " 8i' <= i and 8i' >= -7 + i }";
2402 bmap = isl_basic_map_read_from_str(ctx, str);
2403 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2404 map2 = isl_map_from_pw_multi_aff(pma);
2405 map = isl_map_from_basic_map(bmap);
2406 assert(isl_map_is_equal(map, map2));
2407 isl_map_free(map);
2408 isl_map_free(map2);
2410 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2411 " 8i' <= i and 8i' >= -7 + i }";
2412 set = isl_set_read_from_str(ctx, str);
2413 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2414 set2 = isl_set_from_pw_multi_aff(pma);
2415 equal = isl_set_is_equal(set, set2);
2416 isl_set_free(set);
2417 isl_set_free(set2);
2418 if (equal < 0)
2419 return -1;
2420 if (!equal)
2421 isl_die(ctx, isl_error_unknown,
2422 "unexpected difference between set and "
2423 "piecewise affine expression", return -1);
2425 return 0;
2428 /* A specialized isl_set_min_val test case that would return the wrong result
2429 * in earlier versions of isl.
2430 * The explicit call to isl_basic_set_union prevents the second basic set
2431 * from being determined to be empty prior to the call to isl_set_min_val,
2432 * at least at the point where this test case was introduced.
2434 static int test_min_special(isl_ctx *ctx)
2436 const char *str;
2437 isl_basic_set *bset1, *bset2;
2438 isl_set *set;
2439 isl_aff *obj;
2440 isl_val *res;
2441 int ok;
2443 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
2444 bset1 = isl_basic_set_read_from_str(ctx, str);
2445 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
2446 bset2 = isl_basic_set_read_from_str(ctx, str);
2447 set = isl_basic_set_union(bset1, bset2);
2448 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
2450 res = isl_set_min_val(set, obj);
2451 ok = isl_val_cmp_si(res, 5) == 0;
2453 isl_aff_free(obj);
2454 isl_set_free(set);
2455 isl_val_free(res);
2457 if (!res)
2458 return -1;
2459 if (!ok)
2460 isl_die(ctx, isl_error_unknown, "unexpected minimum",
2461 return -1);
2463 return 0;
2466 /* A specialized isl_set_min_val test case that would return an error
2467 * in earlier versions of isl.
2469 static int test_min_special2(isl_ctx *ctx)
2471 const char *str;
2472 isl_basic_set *bset;
2473 isl_aff *obj;
2474 isl_val *res;
2476 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
2477 bset = isl_basic_set_read_from_str(ctx, str);
2479 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
2481 res = isl_basic_set_max_val(bset, obj);
2483 isl_basic_set_free(bset);
2484 isl_aff_free(obj);
2485 isl_val_free(res);
2487 if (!res)
2488 return -1;
2490 return 0;
2493 struct {
2494 const char *set;
2495 const char *obj;
2496 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2497 __isl_keep isl_aff *obj);
2498 const char *res;
2499 } opt_tests[] = {
2500 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2501 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2502 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2503 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2504 &isl_set_max_val, "30" },
2508 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2509 * In particular, check the results on non-convex inputs.
2511 static int test_min(struct isl_ctx *ctx)
2513 int i;
2514 isl_set *set;
2515 isl_aff *obj;
2516 isl_val *val, *res;
2517 isl_bool ok;
2519 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2520 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2521 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2522 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2523 val = opt_tests[i].fn(set, obj);
2524 ok = isl_val_eq(res, val);
2525 isl_val_free(res);
2526 isl_val_free(val);
2527 isl_aff_free(obj);
2528 isl_set_free(set);
2530 if (ok < 0)
2531 return -1;
2532 if (!ok)
2533 isl_die(ctx, isl_error_unknown,
2534 "unexpected optimum", return -1);
2537 if (test_min_special(ctx) < 0)
2538 return -1;
2539 if (test_min_special2(ctx) < 0)
2540 return -1;
2542 return 0;
2545 struct must_may {
2546 isl_map *must;
2547 isl_map *may;
2550 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2551 void *dep_user, void *user)
2553 struct must_may *mm = (struct must_may *)user;
2555 if (must)
2556 mm->must = isl_map_union(mm->must, dep);
2557 else
2558 mm->may = isl_map_union(mm->may, dep);
2560 return isl_stat_ok;
2563 static int common_space(void *first, void *second)
2565 int depth = *(int *)first;
2566 return 2 * depth;
2569 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2571 isl_map *map2;
2572 int equal;
2574 if (!map)
2575 return -1;
2577 map2 = isl_map_read_from_str(map->ctx, str);
2578 equal = isl_map_is_equal(map, map2);
2579 isl_map_free(map2);
2581 return equal;
2584 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2586 int equal;
2588 equal = map_is_equal(map, str);
2589 if (equal < 0)
2590 return -1;
2591 if (!equal)
2592 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2593 "result not as expected", return -1);
2594 return 0;
2597 static int test_dep(struct isl_ctx *ctx)
2599 const char *str;
2600 isl_space *dim;
2601 isl_map *map;
2602 isl_access_info *ai;
2603 isl_flow *flow;
2604 int depth;
2605 struct must_may mm;
2607 depth = 3;
2609 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2610 map = isl_map_read_from_str(ctx, str);
2611 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2613 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2614 map = isl_map_read_from_str(ctx, str);
2615 ai = isl_access_info_add_source(ai, map, 1, &depth);
2617 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2618 map = isl_map_read_from_str(ctx, str);
2619 ai = isl_access_info_add_source(ai, map, 1, &depth);
2621 flow = isl_access_info_compute_flow(ai);
2622 dim = isl_space_alloc(ctx, 0, 3, 3);
2623 mm.must = isl_map_empty(isl_space_copy(dim));
2624 mm.may = isl_map_empty(dim);
2626 isl_flow_foreach(flow, collect_must_may, &mm);
2628 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2629 " [1,10,0] -> [2,5,0] }";
2630 assert(map_is_equal(mm.must, str));
2631 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2632 assert(map_is_equal(mm.may, str));
2634 isl_map_free(mm.must);
2635 isl_map_free(mm.may);
2636 isl_flow_free(flow);
2639 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2640 map = isl_map_read_from_str(ctx, str);
2641 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2643 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2644 map = isl_map_read_from_str(ctx, str);
2645 ai = isl_access_info_add_source(ai, map, 1, &depth);
2647 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2648 map = isl_map_read_from_str(ctx, str);
2649 ai = isl_access_info_add_source(ai, map, 0, &depth);
2651 flow = isl_access_info_compute_flow(ai);
2652 dim = isl_space_alloc(ctx, 0, 3, 3);
2653 mm.must = isl_map_empty(isl_space_copy(dim));
2654 mm.may = isl_map_empty(dim);
2656 isl_flow_foreach(flow, collect_must_may, &mm);
2658 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2659 assert(map_is_equal(mm.must, str));
2660 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2661 assert(map_is_equal(mm.may, str));
2663 isl_map_free(mm.must);
2664 isl_map_free(mm.may);
2665 isl_flow_free(flow);
2668 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2669 map = isl_map_read_from_str(ctx, str);
2670 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2672 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2673 map = isl_map_read_from_str(ctx, str);
2674 ai = isl_access_info_add_source(ai, map, 0, &depth);
2676 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2677 map = isl_map_read_from_str(ctx, str);
2678 ai = isl_access_info_add_source(ai, map, 0, &depth);
2680 flow = isl_access_info_compute_flow(ai);
2681 dim = isl_space_alloc(ctx, 0, 3, 3);
2682 mm.must = isl_map_empty(isl_space_copy(dim));
2683 mm.may = isl_map_empty(dim);
2685 isl_flow_foreach(flow, collect_must_may, &mm);
2687 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2688 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2689 assert(map_is_equal(mm.may, str));
2690 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2691 assert(map_is_equal(mm.must, str));
2693 isl_map_free(mm.must);
2694 isl_map_free(mm.may);
2695 isl_flow_free(flow);
2698 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2699 map = isl_map_read_from_str(ctx, str);
2700 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2702 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2703 map = isl_map_read_from_str(ctx, str);
2704 ai = isl_access_info_add_source(ai, map, 0, &depth);
2706 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2707 map = isl_map_read_from_str(ctx, str);
2708 ai = isl_access_info_add_source(ai, map, 0, &depth);
2710 flow = isl_access_info_compute_flow(ai);
2711 dim = isl_space_alloc(ctx, 0, 3, 3);
2712 mm.must = isl_map_empty(isl_space_copy(dim));
2713 mm.may = isl_map_empty(dim);
2715 isl_flow_foreach(flow, collect_must_may, &mm);
2717 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2718 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2719 assert(map_is_equal(mm.may, str));
2720 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2721 assert(map_is_equal(mm.must, str));
2723 isl_map_free(mm.must);
2724 isl_map_free(mm.may);
2725 isl_flow_free(flow);
2728 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2729 map = isl_map_read_from_str(ctx, str);
2730 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2732 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2733 map = isl_map_read_from_str(ctx, str);
2734 ai = isl_access_info_add_source(ai, map, 0, &depth);
2736 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2737 map = isl_map_read_from_str(ctx, str);
2738 ai = isl_access_info_add_source(ai, map, 0, &depth);
2740 flow = isl_access_info_compute_flow(ai);
2741 dim = isl_space_alloc(ctx, 0, 3, 3);
2742 mm.must = isl_map_empty(isl_space_copy(dim));
2743 mm.may = isl_map_empty(dim);
2745 isl_flow_foreach(flow, collect_must_may, &mm);
2747 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2748 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2749 assert(map_is_equal(mm.may, str));
2750 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2751 assert(map_is_equal(mm.must, str));
2753 isl_map_free(mm.must);
2754 isl_map_free(mm.may);
2755 isl_flow_free(flow);
2758 depth = 5;
2760 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2761 map = isl_map_read_from_str(ctx, str);
2762 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2764 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2765 map = isl_map_read_from_str(ctx, str);
2766 ai = isl_access_info_add_source(ai, map, 1, &depth);
2768 flow = isl_access_info_compute_flow(ai);
2769 dim = isl_space_alloc(ctx, 0, 5, 5);
2770 mm.must = isl_map_empty(isl_space_copy(dim));
2771 mm.may = isl_map_empty(dim);
2773 isl_flow_foreach(flow, collect_must_may, &mm);
2775 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2776 assert(map_is_equal(mm.must, str));
2777 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2778 assert(map_is_equal(mm.may, str));
2780 isl_map_free(mm.must);
2781 isl_map_free(mm.may);
2782 isl_flow_free(flow);
2784 return 0;
2787 /* Check that the dependence analysis proceeds without errors.
2788 * Earlier versions of isl would break down during the analysis
2789 * due to the use of the wrong spaces.
2791 static int test_flow(isl_ctx *ctx)
2793 const char *str;
2794 isl_union_map *access, *schedule;
2795 isl_union_map *must_dep, *may_dep;
2796 int r;
2798 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2799 access = isl_union_map_read_from_str(ctx, str);
2800 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2801 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2802 "S2[] -> [1,0,0,0]; "
2803 "S3[] -> [-1,0,0,0] }";
2804 schedule = isl_union_map_read_from_str(ctx, str);
2805 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2806 isl_union_map_copy(access), schedule,
2807 &must_dep, &may_dep, NULL, NULL);
2808 isl_union_map_free(may_dep);
2809 isl_union_map_free(must_dep);
2811 return r;
2814 struct {
2815 const char *map;
2816 int sv;
2817 } sv_tests[] = {
2818 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2819 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2820 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2821 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2822 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2823 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2824 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2825 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2826 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2829 int test_sv(isl_ctx *ctx)
2831 isl_union_map *umap;
2832 int i;
2833 int sv;
2835 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2836 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2837 sv = isl_union_map_is_single_valued(umap);
2838 isl_union_map_free(umap);
2839 if (sv < 0)
2840 return -1;
2841 if (sv_tests[i].sv && !sv)
2842 isl_die(ctx, isl_error_internal,
2843 "map not detected as single valued", return -1);
2844 if (!sv_tests[i].sv && sv)
2845 isl_die(ctx, isl_error_internal,
2846 "map detected as single valued", return -1);
2849 return 0;
2852 struct {
2853 const char *str;
2854 int bijective;
2855 } bijective_tests[] = {
2856 { "[N,M]->{[i,j] -> [i]}", 0 },
2857 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
2858 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
2859 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
2860 { "[N,M]->{[i,j] -> [j,i]}", 1 },
2861 { "[N,M]->{[i,j] -> [i+j]}", 0 },
2862 { "[N,M]->{[i,j] -> []}", 0 },
2863 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
2864 { "[N,M]->{[i,j] -> [2i]}", 0 },
2865 { "[N,M]->{[i,j] -> [i,i]}", 0 },
2866 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
2867 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
2868 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
2871 static int test_bijective(struct isl_ctx *ctx)
2873 isl_map *map;
2874 int i;
2875 int bijective;
2877 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
2878 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
2879 bijective = isl_map_is_bijective(map);
2880 isl_map_free(map);
2881 if (bijective < 0)
2882 return -1;
2883 if (bijective_tests[i].bijective && !bijective)
2884 isl_die(ctx, isl_error_internal,
2885 "map not detected as bijective", return -1);
2886 if (!bijective_tests[i].bijective && bijective)
2887 isl_die(ctx, isl_error_internal,
2888 "map detected as bijective", return -1);
2891 return 0;
2894 /* Inputs for isl_pw_qpolynomial_gist tests.
2895 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2897 struct {
2898 const char *pwqp;
2899 const char *set;
2900 const char *gist;
2901 } pwqp_gist_tests[] = {
2902 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2903 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2904 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2905 "{ [i] -> -1/2 + 1/2 * i }" },
2906 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2909 static int test_pwqp(struct isl_ctx *ctx)
2911 int i;
2912 const char *str;
2913 isl_set *set;
2914 isl_pw_qpolynomial *pwqp1, *pwqp2;
2915 int equal;
2917 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2918 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2920 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2921 isl_dim_in, 1, 1);
2923 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2924 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2926 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2928 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2930 isl_pw_qpolynomial_free(pwqp1);
2932 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2933 str = pwqp_gist_tests[i].pwqp;
2934 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2935 str = pwqp_gist_tests[i].set;
2936 set = isl_set_read_from_str(ctx, str);
2937 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2938 str = pwqp_gist_tests[i].gist;
2939 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2940 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2941 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2942 isl_pw_qpolynomial_free(pwqp1);
2944 if (equal < 0)
2945 return -1;
2946 if (!equal)
2947 isl_die(ctx, isl_error_unknown,
2948 "unexpected result", return -1);
2951 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2952 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2953 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2954 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2956 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2958 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2960 isl_pw_qpolynomial_free(pwqp1);
2962 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2963 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2964 str = "{ [x] -> x }";
2965 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2967 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2969 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2971 isl_pw_qpolynomial_free(pwqp1);
2973 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2974 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2975 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2976 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2977 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2978 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2979 isl_pw_qpolynomial_free(pwqp1);
2981 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2982 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2983 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2984 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2985 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2986 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2987 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2988 isl_pw_qpolynomial_free(pwqp1);
2989 isl_pw_qpolynomial_free(pwqp2);
2990 if (equal < 0)
2991 return -1;
2992 if (!equal)
2993 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2995 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2996 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2997 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2998 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2999 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3000 isl_val_one(ctx));
3001 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3002 isl_pw_qpolynomial_free(pwqp1);
3003 isl_pw_qpolynomial_free(pwqp2);
3004 if (equal < 0)
3005 return -1;
3006 if (!equal)
3007 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3009 return 0;
3012 static int test_split_periods(isl_ctx *ctx)
3014 const char *str;
3015 isl_pw_qpolynomial *pwqp;
3017 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3018 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3019 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3020 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3022 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3024 isl_pw_qpolynomial_free(pwqp);
3026 if (!pwqp)
3027 return -1;
3029 return 0;
3032 static int test_union(isl_ctx *ctx)
3034 const char *str;
3035 isl_union_set *uset1, *uset2;
3036 isl_union_map *umap1, *umap2;
3037 int equal;
3039 str = "{ [i] : 0 <= i <= 1 }";
3040 uset1 = isl_union_set_read_from_str(ctx, str);
3041 str = "{ [1] -> [0] }";
3042 umap1 = isl_union_map_read_from_str(ctx, str);
3044 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3045 equal = isl_union_map_is_equal(umap1, umap2);
3047 isl_union_map_free(umap1);
3048 isl_union_map_free(umap2);
3050 if (equal < 0)
3051 return -1;
3052 if (!equal)
3053 isl_die(ctx, isl_error_unknown, "union maps not equal",
3054 return -1);
3056 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3057 umap1 = isl_union_map_read_from_str(ctx, str);
3058 str = "{ A[i]; B[i] }";
3059 uset1 = isl_union_set_read_from_str(ctx, str);
3061 uset2 = isl_union_map_domain(umap1);
3063 equal = isl_union_set_is_equal(uset1, uset2);
3065 isl_union_set_free(uset1);
3066 isl_union_set_free(uset2);
3068 if (equal < 0)
3069 return -1;
3070 if (!equal)
3071 isl_die(ctx, isl_error_unknown, "union sets not equal",
3072 return -1);
3074 return 0;
3077 /* Check that computing a bound of a non-zero polynomial over an unbounded
3078 * domain does not produce a rational value.
3079 * In particular, check that the upper bound is infinity.
3081 static int test_bound_unbounded_domain(isl_ctx *ctx)
3083 const char *str;
3084 isl_pw_qpolynomial *pwqp;
3085 isl_pw_qpolynomial_fold *pwf, *pwf2;
3086 isl_bool equal;
3088 str = "{ [m,n] -> -m * n }";
3089 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3090 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3091 str = "{ infty }";
3092 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3093 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3094 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
3095 isl_pw_qpolynomial_fold_free(pwf);
3096 isl_pw_qpolynomial_fold_free(pwf2);
3098 if (equal < 0)
3099 return -1;
3100 if (!equal)
3101 isl_die(ctx, isl_error_unknown,
3102 "expecting infinite polynomial bound", return -1);
3104 return 0;
3107 static int test_bound(isl_ctx *ctx)
3109 const char *str;
3110 unsigned dim;
3111 isl_pw_qpolynomial *pwqp;
3112 isl_pw_qpolynomial_fold *pwf;
3114 if (test_bound_unbounded_domain(ctx) < 0)
3115 return -1;
3117 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
3118 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3119 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3120 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3121 isl_pw_qpolynomial_fold_free(pwf);
3122 if (dim != 4)
3123 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3124 return -1);
3126 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
3127 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3128 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3129 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3130 isl_pw_qpolynomial_fold_free(pwf);
3131 if (dim != 1)
3132 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3133 return -1);
3135 return 0;
3138 static int test_lift(isl_ctx *ctx)
3140 const char *str;
3141 isl_basic_map *bmap;
3142 isl_basic_set *bset;
3144 str = "{ [i0] : exists e0 : i0 = 4e0 }";
3145 bset = isl_basic_set_read_from_str(ctx, str);
3146 bset = isl_basic_set_lift(bset);
3147 bmap = isl_basic_map_from_range(bset);
3148 bset = isl_basic_map_domain(bmap);
3149 isl_basic_set_free(bset);
3151 return 0;
3154 struct {
3155 const char *set1;
3156 const char *set2;
3157 int subset;
3158 } subset_tests[] = {
3159 { "{ [112, 0] }",
3160 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
3161 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
3162 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
3163 { "{ [65] }",
3164 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
3165 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
3166 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
3167 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
3168 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
3169 "256e0 <= 255i and 256e0 >= -255 + 255i and "
3170 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
3171 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
3172 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
3173 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
3174 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
3175 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
3176 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
3177 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
3178 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
3179 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
3180 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
3181 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
3182 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
3183 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
3184 "4e0 <= -57 + i0 + i1)) or "
3185 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
3186 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
3187 "4e0 >= -61 + i0 + i1)) or "
3188 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
3189 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
3192 static int test_subset(isl_ctx *ctx)
3194 int i;
3195 isl_set *set1, *set2;
3196 int subset;
3198 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3199 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3200 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3201 subset = isl_set_is_subset(set1, set2);
3202 isl_set_free(set1);
3203 isl_set_free(set2);
3204 if (subset < 0)
3205 return -1;
3206 if (subset != subset_tests[i].subset)
3207 isl_die(ctx, isl_error_unknown,
3208 "incorrect subset result", return -1);
3211 return 0;
3214 struct {
3215 const char *minuend;
3216 const char *subtrahend;
3217 const char *difference;
3218 } subtract_domain_tests[] = {
3219 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3220 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3221 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3224 static int test_subtract(isl_ctx *ctx)
3226 int i;
3227 isl_union_map *umap1, *umap2;
3228 isl_union_pw_multi_aff *upma1, *upma2;
3229 isl_union_set *uset;
3230 int equal;
3232 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3233 umap1 = isl_union_map_read_from_str(ctx,
3234 subtract_domain_tests[i].minuend);
3235 uset = isl_union_set_read_from_str(ctx,
3236 subtract_domain_tests[i].subtrahend);
3237 umap2 = isl_union_map_read_from_str(ctx,
3238 subtract_domain_tests[i].difference);
3239 umap1 = isl_union_map_subtract_domain(umap1, uset);
3240 equal = isl_union_map_is_equal(umap1, umap2);
3241 isl_union_map_free(umap1);
3242 isl_union_map_free(umap2);
3243 if (equal < 0)
3244 return -1;
3245 if (!equal)
3246 isl_die(ctx, isl_error_unknown,
3247 "incorrect subtract domain result", return -1);
3250 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3251 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3252 subtract_domain_tests[i].minuend);
3253 uset = isl_union_set_read_from_str(ctx,
3254 subtract_domain_tests[i].subtrahend);
3255 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3256 subtract_domain_tests[i].difference);
3257 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3258 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3259 isl_union_pw_multi_aff_free(upma1);
3260 isl_union_pw_multi_aff_free(upma2);
3261 if (equal < 0)
3262 return -1;
3263 if (!equal)
3264 isl_die(ctx, isl_error_unknown,
3265 "incorrect subtract domain result", return -1);
3268 return 0;
3271 /* Check that intersecting the empty basic set with another basic set
3272 * does not increase the number of constraints. In particular,
3273 * the empty basic set should maintain its canonical representation.
3275 static int test_intersect(isl_ctx *ctx)
3277 int n1, n2;
3278 isl_basic_set *bset1, *bset2;
3280 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3281 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3282 n1 = isl_basic_set_n_constraint(bset1);
3283 bset1 = isl_basic_set_intersect(bset1, bset2);
3284 n2 = isl_basic_set_n_constraint(bset1);
3285 isl_basic_set_free(bset1);
3286 if (!bset1)
3287 return -1;
3288 if (n1 != n2)
3289 isl_die(ctx, isl_error_unknown,
3290 "number of constraints of empty set changed",
3291 return -1);
3293 return 0;
3296 int test_factorize(isl_ctx *ctx)
3298 const char *str;
3299 isl_basic_set *bset;
3300 isl_factorizer *f;
3302 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3303 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3304 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3305 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3306 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3307 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3308 "3i5 >= -2i0 - i2 + 3i4 }";
3309 bset = isl_basic_set_read_from_str(ctx, str);
3310 f = isl_basic_set_factorizer(bset);
3311 isl_basic_set_free(bset);
3312 isl_factorizer_free(f);
3313 if (!f)
3314 isl_die(ctx, isl_error_unknown,
3315 "failed to construct factorizer", return -1);
3317 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3318 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3319 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3320 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3321 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3322 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3323 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3324 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3325 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3326 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3327 bset = isl_basic_set_read_from_str(ctx, str);
3328 f = isl_basic_set_factorizer(bset);
3329 isl_basic_set_free(bset);
3330 isl_factorizer_free(f);
3331 if (!f)
3332 isl_die(ctx, isl_error_unknown,
3333 "failed to construct factorizer", return -1);
3335 return 0;
3338 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3340 int *injective = user;
3342 *injective = isl_map_is_injective(map);
3343 isl_map_free(map);
3345 if (*injective < 0 || !*injective)
3346 return isl_stat_error;
3348 return isl_stat_ok;
3351 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3352 const char *r, const char *s, int tilable, int parallel)
3354 int i;
3355 isl_union_set *D;
3356 isl_union_map *W, *R, *S;
3357 isl_union_map *empty;
3358 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3359 isl_union_map *validity, *proximity, *coincidence;
3360 isl_union_map *schedule;
3361 isl_union_map *test;
3362 isl_union_set *delta;
3363 isl_union_set *domain;
3364 isl_set *delta_set;
3365 isl_set *slice;
3366 isl_set *origin;
3367 isl_schedule_constraints *sc;
3368 isl_schedule *sched;
3369 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3371 D = isl_union_set_read_from_str(ctx, d);
3372 W = isl_union_map_read_from_str(ctx, w);
3373 R = isl_union_map_read_from_str(ctx, r);
3374 S = isl_union_map_read_from_str(ctx, s);
3376 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3377 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3379 empty = isl_union_map_empty(isl_union_map_get_space(S));
3380 isl_union_map_compute_flow(isl_union_map_copy(R),
3381 isl_union_map_copy(W), empty,
3382 isl_union_map_copy(S),
3383 &dep_raw, NULL, NULL, NULL);
3384 isl_union_map_compute_flow(isl_union_map_copy(W),
3385 isl_union_map_copy(W),
3386 isl_union_map_copy(R),
3387 isl_union_map_copy(S),
3388 &dep_waw, &dep_war, NULL, NULL);
3390 dep = isl_union_map_union(dep_waw, dep_war);
3391 dep = isl_union_map_union(dep, dep_raw);
3392 validity = isl_union_map_copy(dep);
3393 coincidence = isl_union_map_copy(dep);
3394 proximity = isl_union_map_copy(dep);
3396 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3397 sc = isl_schedule_constraints_set_validity(sc, validity);
3398 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3399 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3400 sched = isl_schedule_constraints_compute_schedule(sc);
3401 schedule = isl_schedule_get_map(sched);
3402 isl_schedule_free(sched);
3403 isl_union_map_free(W);
3404 isl_union_map_free(R);
3405 isl_union_map_free(S);
3407 is_injection = 1;
3408 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3410 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3411 is_complete = isl_union_set_is_subset(D, domain);
3412 isl_union_set_free(D);
3413 isl_union_set_free(domain);
3415 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3416 test = isl_union_map_apply_range(test, dep);
3417 test = isl_union_map_apply_range(test, schedule);
3419 delta = isl_union_map_deltas(test);
3420 if (isl_union_set_n_set(delta) == 0) {
3421 is_tilable = 1;
3422 is_parallel = 1;
3423 is_nonneg = 1;
3424 isl_union_set_free(delta);
3425 } else {
3426 delta_set = isl_set_from_union_set(delta);
3428 slice = isl_set_universe(isl_set_get_space(delta_set));
3429 for (i = 0; i < tilable; ++i)
3430 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3431 is_tilable = isl_set_is_subset(delta_set, slice);
3432 isl_set_free(slice);
3434 slice = isl_set_universe(isl_set_get_space(delta_set));
3435 for (i = 0; i < parallel; ++i)
3436 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3437 is_parallel = isl_set_is_subset(delta_set, slice);
3438 isl_set_free(slice);
3440 origin = isl_set_universe(isl_set_get_space(delta_set));
3441 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
3442 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3444 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3445 delta_set = isl_set_lexmin(delta_set);
3447 is_nonneg = isl_set_is_equal(delta_set, origin);
3449 isl_set_free(origin);
3450 isl_set_free(delta_set);
3453 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3454 is_injection < 0 || is_complete < 0)
3455 return -1;
3456 if (!is_complete)
3457 isl_die(ctx, isl_error_unknown,
3458 "generated schedule incomplete", return -1);
3459 if (!is_injection)
3460 isl_die(ctx, isl_error_unknown,
3461 "generated schedule not injective on each statement",
3462 return -1);
3463 if (!is_nonneg)
3464 isl_die(ctx, isl_error_unknown,
3465 "negative dependences in generated schedule",
3466 return -1);
3467 if (!is_tilable)
3468 isl_die(ctx, isl_error_unknown,
3469 "generated schedule not as tilable as expected",
3470 return -1);
3471 if (!is_parallel)
3472 isl_die(ctx, isl_error_unknown,
3473 "generated schedule not as parallel as expected",
3474 return -1);
3476 return 0;
3479 /* Compute a schedule for the given instance set, validity constraints,
3480 * proximity constraints and context and return a corresponding union map
3481 * representation.
3483 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3484 const char *domain, const char *validity, const char *proximity,
3485 const char *context)
3487 isl_set *con;
3488 isl_union_set *dom;
3489 isl_union_map *dep;
3490 isl_union_map *prox;
3491 isl_schedule_constraints *sc;
3492 isl_schedule *schedule;
3493 isl_union_map *sched;
3495 con = isl_set_read_from_str(ctx, context);
3496 dom = isl_union_set_read_from_str(ctx, domain);
3497 dep = isl_union_map_read_from_str(ctx, validity);
3498 prox = isl_union_map_read_from_str(ctx, proximity);
3499 sc = isl_schedule_constraints_on_domain(dom);
3500 sc = isl_schedule_constraints_set_context(sc, con);
3501 sc = isl_schedule_constraints_set_validity(sc, dep);
3502 sc = isl_schedule_constraints_set_proximity(sc, prox);
3503 schedule = isl_schedule_constraints_compute_schedule(sc);
3504 sched = isl_schedule_get_map(schedule);
3505 isl_schedule_free(schedule);
3507 return sched;
3510 /* Compute a schedule for the given instance set, validity constraints and
3511 * proximity constraints and return a corresponding union map representation.
3513 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
3514 const char *domain, const char *validity, const char *proximity)
3516 return compute_schedule_with_context(ctx, domain, validity, proximity,
3517 "{ : }");
3520 /* Check that a schedule can be constructed on the given domain
3521 * with the given validity and proximity constraints.
3523 static int test_has_schedule(isl_ctx *ctx, const char *domain,
3524 const char *validity, const char *proximity)
3526 isl_union_map *sched;
3528 sched = compute_schedule(ctx, domain, validity, proximity);
3529 if (!sched)
3530 return -1;
3532 isl_union_map_free(sched);
3533 return 0;
3536 int test_special_schedule(isl_ctx *ctx, const char *domain,
3537 const char *validity, const char *proximity, const char *expected_sched)
3539 isl_union_map *sched1, *sched2;
3540 int equal;
3542 sched1 = compute_schedule(ctx, domain, validity, proximity);
3543 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
3545 equal = isl_union_map_is_equal(sched1, sched2);
3546 isl_union_map_free(sched1);
3547 isl_union_map_free(sched2);
3549 if (equal < 0)
3550 return -1;
3551 if (!equal)
3552 isl_die(ctx, isl_error_unknown, "unexpected schedule",
3553 return -1);
3555 return 0;
3558 /* Check that the schedule map is properly padded, even after being
3559 * reconstructed from the band forest.
3561 static int test_padded_schedule(isl_ctx *ctx)
3563 const char *str;
3564 isl_union_set *D;
3565 isl_union_map *validity, *proximity;
3566 isl_schedule_constraints *sc;
3567 isl_schedule *sched;
3568 isl_union_map *map1, *map2;
3569 isl_band_list *list;
3570 int equal;
3572 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
3573 D = isl_union_set_read_from_str(ctx, str);
3574 validity = isl_union_map_empty(isl_union_set_get_space(D));
3575 proximity = isl_union_map_copy(validity);
3576 sc = isl_schedule_constraints_on_domain(D);
3577 sc = isl_schedule_constraints_set_validity(sc, validity);
3578 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3579 sched = isl_schedule_constraints_compute_schedule(sc);
3580 map1 = isl_schedule_get_map(sched);
3581 list = isl_schedule_get_band_forest(sched);
3582 isl_band_list_free(list);
3583 map2 = isl_schedule_get_map(sched);
3584 isl_schedule_free(sched);
3585 equal = isl_union_map_is_equal(map1, map2);
3586 isl_union_map_free(map1);
3587 isl_union_map_free(map2);
3589 if (equal < 0)
3590 return -1;
3591 if (!equal)
3592 isl_die(ctx, isl_error_unknown,
3593 "reconstructed schedule map not the same as original",
3594 return -1);
3596 return 0;
3599 /* Check that conditional validity constraints are also taken into
3600 * account across bands.
3601 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
3602 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
3603 * and then check that the adjacent order constraint C[2,1]->D[2,0]
3604 * is enforced by the rest of the schedule.
3606 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
3608 const char *str;
3609 isl_union_set *domain;
3610 isl_union_map *validity, *proximity, *condition;
3611 isl_union_map *sink, *source, *dep;
3612 isl_schedule_constraints *sc;
3613 isl_schedule *schedule;
3614 isl_union_access_info *access;
3615 isl_union_flow *flow;
3616 int empty;
3618 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3619 "A[k] : k >= 1 and k <= -1 + n; "
3620 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3621 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
3622 domain = isl_union_set_read_from_str(ctx, str);
3623 sc = isl_schedule_constraints_on_domain(domain);
3624 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
3625 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3626 "D[k, i] -> C[1 + k, i] : "
3627 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3628 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
3629 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
3630 validity = isl_union_map_read_from_str(ctx, str);
3631 sc = isl_schedule_constraints_set_validity(sc, validity);
3632 str = "[n] -> { C[k, i] -> D[k, i] : "
3633 "0 <= i <= -1 + k and k <= -1 + n }";
3634 proximity = isl_union_map_read_from_str(ctx, str);
3635 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3636 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
3637 "i <= -1 + k and i >= 1 and k <= -2 + n; "
3638 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
3639 "k <= -1 + n and i >= 0 and i <= -2 + k }";
3640 condition = isl_union_map_read_from_str(ctx, str);
3641 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
3642 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3643 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
3644 "i >= 0 and i <= -1 + k and k <= -1 + n and "
3645 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
3646 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
3647 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3648 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
3649 "k <= -1 + n and i >= 0 and i <= -1 + k and "
3650 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
3651 validity = isl_union_map_read_from_str(ctx, str);
3652 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3653 validity);
3654 schedule = isl_schedule_constraints_compute_schedule(sc);
3655 str = "{ D[2,0] -> [] }";
3656 sink = isl_union_map_read_from_str(ctx, str);
3657 access = isl_union_access_info_from_sink(sink);
3658 str = "{ C[2,1] -> [] }";
3659 source = isl_union_map_read_from_str(ctx, str);
3660 access = isl_union_access_info_set_must_source(access, source);
3661 access = isl_union_access_info_set_schedule(access, schedule);
3662 flow = isl_union_access_info_compute_flow(access);
3663 dep = isl_union_flow_get_must_dependence(flow);
3664 isl_union_flow_free(flow);
3665 empty = isl_union_map_is_empty(dep);
3666 isl_union_map_free(dep);
3668 if (empty < 0)
3669 return -1;
3670 if (empty)
3671 isl_die(ctx, isl_error_unknown,
3672 "conditional validity not respected", return -1);
3674 return 0;
3677 /* Input for testing of schedule construction based on
3678 * conditional constraints.
3680 * domain is the iteration domain
3681 * flow are the flow dependences, which determine the validity and
3682 * proximity constraints
3683 * condition are the conditions on the conditional validity constraints
3684 * conditional_validity are the conditional validity constraints
3685 * outer_band_n is the expected number of members in the outer band
3687 struct {
3688 const char *domain;
3689 const char *flow;
3690 const char *condition;
3691 const char *conditional_validity;
3692 int outer_band_n;
3693 } live_range_tests[] = {
3694 /* Contrived example that illustrates that we need to keep
3695 * track of tagged condition dependences and
3696 * tagged conditional validity dependences
3697 * in isl_sched_edge separately.
3698 * In particular, the conditional validity constraints on A
3699 * cannot be satisfied,
3700 * but they can be ignored because there are no corresponding
3701 * condition constraints. However, we do have an additional
3702 * conditional validity constraint that maps to the same
3703 * dependence relation
3704 * as the condition constraint on B. If we did not make a distinction
3705 * between tagged condition and tagged conditional validity
3706 * dependences, then we
3707 * could end up treating this shared dependence as an condition
3708 * constraint on A, forcing a localization of the conditions,
3709 * which is impossible.
3711 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
3712 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
3713 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
3714 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3715 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3716 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
3719 /* TACO 2013 Fig. 7 */
3720 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3721 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3722 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3723 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
3724 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
3725 "0 <= i < n and 0 <= j < n - 1 }",
3726 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
3727 "0 <= i < n and 0 <= j < j' < n;"
3728 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
3729 "0 <= i < i' < n and 0 <= j,j' < n;"
3730 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
3731 "0 <= i,j,j' < n and j < j' }",
3734 /* TACO 2013 Fig. 7, without tags */
3735 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3736 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3737 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3738 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3739 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3740 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
3741 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
3742 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
3745 /* TACO 2013 Fig. 12 */
3746 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
3747 "S3[i,3] : 0 <= i <= 1 }",
3748 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
3749 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
3750 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
3751 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
3752 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3753 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
3754 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3755 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
3756 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
3757 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
3758 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
3763 /* Test schedule construction based on conditional constraints.
3764 * In particular, check the number of members in the outer band node
3765 * as an indication of whether tiling is possible or not.
3767 static int test_conditional_schedule_constraints(isl_ctx *ctx)
3769 int i;
3770 isl_union_set *domain;
3771 isl_union_map *condition;
3772 isl_union_map *flow;
3773 isl_union_map *validity;
3774 isl_schedule_constraints *sc;
3775 isl_schedule *schedule;
3776 isl_schedule_node *node;
3777 int n_member;
3779 if (test_special_conditional_schedule_constraints(ctx) < 0)
3780 return -1;
3782 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
3783 domain = isl_union_set_read_from_str(ctx,
3784 live_range_tests[i].domain);
3785 flow = isl_union_map_read_from_str(ctx,
3786 live_range_tests[i].flow);
3787 condition = isl_union_map_read_from_str(ctx,
3788 live_range_tests[i].condition);
3789 validity = isl_union_map_read_from_str(ctx,
3790 live_range_tests[i].conditional_validity);
3791 sc = isl_schedule_constraints_on_domain(domain);
3792 sc = isl_schedule_constraints_set_validity(sc,
3793 isl_union_map_copy(flow));
3794 sc = isl_schedule_constraints_set_proximity(sc, flow);
3795 sc = isl_schedule_constraints_set_conditional_validity(sc,
3796 condition, validity);
3797 schedule = isl_schedule_constraints_compute_schedule(sc);
3798 node = isl_schedule_get_root(schedule);
3799 while (node &&
3800 isl_schedule_node_get_type(node) != isl_schedule_node_band)
3801 node = isl_schedule_node_first_child(node);
3802 n_member = isl_schedule_node_band_n_member(node);
3803 isl_schedule_node_free(node);
3804 isl_schedule_free(schedule);
3806 if (!schedule)
3807 return -1;
3808 if (n_member != live_range_tests[i].outer_band_n)
3809 isl_die(ctx, isl_error_unknown,
3810 "unexpected number of members in outer band",
3811 return -1);
3813 return 0;
3816 /* Check that the schedule computed for the given instance set and
3817 * dependence relation strongly satisfies the dependences.
3818 * In particular, check that no instance is scheduled before
3819 * or together with an instance on which it depends.
3820 * Earlier versions of isl would produce a schedule that
3821 * only weakly satisfies the dependences.
3823 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
3825 const char *domain, *dep;
3826 isl_union_map *D, *schedule;
3827 isl_map *map, *ge;
3828 int empty;
3830 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
3831 "A[i0] : 0 <= i0 <= 1 }";
3832 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
3833 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
3834 schedule = compute_schedule(ctx, domain, dep, dep);
3835 D = isl_union_map_read_from_str(ctx, dep);
3836 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
3837 D = isl_union_map_apply_range(D, schedule);
3838 map = isl_map_from_union_map(D);
3839 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3840 map = isl_map_intersect(map, ge);
3841 empty = isl_map_is_empty(map);
3842 isl_map_free(map);
3844 if (empty < 0)
3845 return -1;
3846 if (!empty)
3847 isl_die(ctx, isl_error_unknown,
3848 "dependences not strongly satisfied", return -1);
3850 return 0;
3853 /* Compute a schedule for input where the instance set constraints
3854 * conflict with the context constraints.
3855 * Earlier versions of isl did not properly handle this situation.
3857 static int test_conflicting_context_schedule(isl_ctx *ctx)
3859 isl_union_map *schedule;
3860 const char *domain, *context;
3862 domain = "[n] -> { A[] : n >= 0 }";
3863 context = "[n] -> { : n < 0 }";
3864 schedule = compute_schedule_with_context(ctx,
3865 domain, "{}", "{}", context);
3866 isl_union_map_free(schedule);
3868 if (!schedule)
3869 return -1;
3871 return 0;
3874 /* Check that the dependence carrying step is not confused by
3875 * a bound on the coefficient size.
3876 * In particular, force the scheduler to move to a dependence carrying
3877 * step by demanding outer coincidence and bound the size of
3878 * the coefficients. Earlier versions of isl would take this
3879 * bound into account while carrying dependences, breaking
3880 * fundamental assumptions.
3881 * On the other hand, the dependence carrying step now tries
3882 * to prevent loop coalescing by default, so check that indeed
3883 * no loop coalescing occurs by comparing the computed schedule
3884 * to the expected non-coalescing schedule.
3886 static int test_bounded_coefficients_schedule(isl_ctx *ctx)
3888 const char *domain, *dep;
3889 isl_union_set *I;
3890 isl_union_map *D;
3891 isl_schedule_constraints *sc;
3892 isl_schedule *schedule;
3893 isl_union_map *sched1, *sched2;
3894 isl_bool equal;
3896 domain = "{ C[i0, i1] : 2 <= i0 <= 3999 and 0 <= i1 <= -1 + i0 }";
3897 dep = "{ C[i0, i1] -> C[i0, 1 + i1] : i0 <= 3999 and i1 >= 0 and "
3898 "i1 <= -2 + i0; "
3899 "C[i0, -1 + i0] -> C[1 + i0, 0] : i0 <= 3998 and i0 >= 1 }";
3900 I = isl_union_set_read_from_str(ctx, domain);
3901 D = isl_union_map_read_from_str(ctx, dep);
3902 sc = isl_schedule_constraints_on_domain(I);
3903 sc = isl_schedule_constraints_set_validity(sc, isl_union_map_copy(D));
3904 sc = isl_schedule_constraints_set_coincidence(sc, D);
3905 isl_options_set_schedule_outer_coincidence(ctx, 1);
3906 isl_options_set_schedule_max_coefficient(ctx, 20);
3907 schedule = isl_schedule_constraints_compute_schedule(sc);
3908 isl_options_set_schedule_max_coefficient(ctx, -1);
3909 isl_options_set_schedule_outer_coincidence(ctx, 0);
3910 sched1 = isl_schedule_get_map(schedule);
3911 isl_schedule_free(schedule);
3913 sched2 = isl_union_map_read_from_str(ctx, "{ C[x,y] -> [x,y] }");
3914 equal = isl_union_map_is_equal(sched1, sched2);
3915 isl_union_map_free(sched1);
3916 isl_union_map_free(sched2);
3918 if (equal < 0)
3919 return -1;
3920 if (!equal)
3921 isl_die(ctx, isl_error_unknown,
3922 "unexpected schedule", return -1);
3924 return 0;
3927 /* Check that a set of schedule constraints that only allow for
3928 * a coalescing schedule still produces a schedule even if the user
3929 * request a non-coalescing schedule. Earlier versions of isl
3930 * would not handle this case correctly.
3932 static int test_coalescing_schedule(isl_ctx *ctx)
3934 const char *domain, *dep;
3935 isl_union_set *I;
3936 isl_union_map *D;
3937 isl_schedule_constraints *sc;
3938 isl_schedule *schedule;
3939 int treat_coalescing;
3941 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
3942 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
3943 I = isl_union_set_read_from_str(ctx, domain);
3944 D = isl_union_map_read_from_str(ctx, dep);
3945 sc = isl_schedule_constraints_on_domain(I);
3946 sc = isl_schedule_constraints_set_validity(sc, D);
3947 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
3948 isl_options_set_schedule_treat_coalescing(ctx, 1);
3949 schedule = isl_schedule_constraints_compute_schedule(sc);
3950 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
3951 isl_schedule_free(schedule);
3952 if (!schedule)
3953 return -1;
3954 return 0;
3957 int test_schedule(isl_ctx *ctx)
3959 const char *D, *W, *R, *V, *P, *S;
3960 int max_coincidence;
3961 int treat_coalescing;
3963 /* Handle resulting schedule with zero bands. */
3964 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
3965 return -1;
3967 /* Jacobi */
3968 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
3969 W = "{ S1[t,i] -> a[t,i] }";
3970 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
3971 "S1[t,i] -> a[t-1,i+1] }";
3972 S = "{ S1[t,i] -> [t,i] }";
3973 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3974 return -1;
3976 /* Fig. 5 of CC2008 */
3977 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
3978 "j <= -1 + N }";
3979 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
3980 "j >= 2 and j <= -1 + N }";
3981 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
3982 "j >= 2 and j <= -1 + N; "
3983 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
3984 "j >= 2 and j <= -1 + N }";
3985 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3986 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3987 return -1;
3989 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
3990 W = "{ S1[i] -> a[i] }";
3991 R = "{ S2[i] -> a[i+1] }";
3992 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3993 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3994 return -1;
3996 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
3997 W = "{ S1[i] -> a[i] }";
3998 R = "{ S2[i] -> a[9-i] }";
3999 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4000 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4001 return -1;
4003 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
4004 W = "{ S1[i] -> a[i] }";
4005 R = "[N] -> { S2[i] -> a[N-1-i] }";
4006 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4007 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4008 return -1;
4010 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
4011 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
4012 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
4013 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
4014 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4015 return -1;
4017 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4018 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
4019 R = "{ S2[i,j] -> a[i-1,j] }";
4020 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4021 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4022 return -1;
4024 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4025 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
4026 R = "{ S2[i,j] -> a[i,j-1] }";
4027 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4028 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4029 return -1;
4031 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
4032 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
4033 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
4034 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
4035 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
4036 "S_0[] -> [0, 0, 0] }";
4037 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
4038 return -1;
4039 ctx->opt->schedule_parametric = 0;
4040 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4041 return -1;
4042 ctx->opt->schedule_parametric = 1;
4044 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
4045 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
4046 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
4047 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
4048 "S4[i] -> a[i,N] }";
4049 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
4050 "S4[i] -> [4,i,0] }";
4051 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
4052 isl_options_set_schedule_maximize_coincidence(ctx, 0);
4053 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4054 return -1;
4055 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
4057 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
4058 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4059 "j <= N }";
4060 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4061 "j <= N; "
4062 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
4063 "j <= N }";
4064 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4065 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4066 return -1;
4068 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
4069 " S_2[t] : t >= 0 and t <= -1 + N; "
4070 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
4071 "i <= -1 + N }";
4072 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
4073 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
4074 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
4075 "i >= 0 and i <= -1 + N }";
4076 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
4077 "i >= 0 and i <= -1 + N; "
4078 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
4079 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
4080 " S_0[t] -> [0, t, 0] }";
4082 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4083 return -1;
4084 ctx->opt->schedule_parametric = 0;
4085 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4086 return -1;
4087 ctx->opt->schedule_parametric = 1;
4089 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
4090 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
4091 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
4092 return -1;
4094 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
4095 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
4096 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
4097 "j >= 0 and j <= -1 + N; "
4098 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4099 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
4100 "j >= 0 and j <= -1 + N; "
4101 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4102 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
4103 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4104 return -1;
4106 D = "{ S_0[i] : i >= 0 }";
4107 W = "{ S_0[i] -> a[i] : i >= 0 }";
4108 R = "{ S_0[i] -> a[0] : i >= 0 }";
4109 S = "{ S_0[i] -> [0, i, 0] }";
4110 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4111 return -1;
4113 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
4114 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
4115 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
4116 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
4117 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4118 return -1;
4120 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
4121 "k <= -1 + n and k >= 0 }";
4122 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
4123 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
4124 "k <= -1 + n and k >= 0; "
4125 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
4126 "k <= -1 + n and k >= 0; "
4127 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
4128 "k <= -1 + n and k >= 0 }";
4129 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
4130 ctx->opt->schedule_outer_coincidence = 1;
4131 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4132 return -1;
4133 ctx->opt->schedule_outer_coincidence = 0;
4135 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
4136 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
4137 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
4138 "Stmt_for_body24[i0, i1, 1, 0]:"
4139 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
4140 "Stmt_for_body7[i0, i1, i2]:"
4141 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
4142 "i2 <= 7 }";
4144 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
4145 "Stmt_for_body24[1, i1, i2, i3]:"
4146 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
4147 "i2 >= 1;"
4148 "Stmt_for_body24[0, i1, i2, i3] -> "
4149 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
4150 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
4151 "i3 >= 0;"
4152 "Stmt_for_body24[0, i1, i2, i3] ->"
4153 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4154 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4155 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4156 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4157 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4158 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4159 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4160 "i1 <= 6 and i1 >= 0;"
4161 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4162 "Stmt_for_body7[i0, i1, i2] -> "
4163 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4164 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4165 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4166 "Stmt_for_body7[i0, i1, i2] -> "
4167 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4168 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4169 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4170 P = V;
4171 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
4172 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
4173 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
4175 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4176 isl_options_set_schedule_treat_coalescing(ctx, 0);
4177 if (test_special_schedule(ctx, D, V, P, S) < 0)
4178 return -1;
4179 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4181 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4182 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4183 "j >= 1 and j <= 7;"
4184 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4185 "j >= 1 and j <= 8 }";
4186 P = "{ }";
4187 S = "{ S_0[i, j] -> [i + j, j] }";
4188 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4189 if (test_special_schedule(ctx, D, V, P, S) < 0)
4190 return -1;
4191 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4193 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4194 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4195 "j >= 0 and j <= -1 + i }";
4196 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4197 "i <= -1 + N and j >= 0;"
4198 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4199 "i <= -2 + N }";
4200 P = "{ }";
4201 S = "{ S_0[i, j] -> [i, j] }";
4202 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4203 if (test_special_schedule(ctx, D, V, P, S) < 0)
4204 return -1;
4205 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4207 /* Test both algorithms on a case with only proximity dependences. */
4208 D = "{ S[i,j] : 0 <= i <= 10 }";
4209 V = "{ }";
4210 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4211 S = "{ S[i, j] -> [j, i] }";
4212 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4213 if (test_special_schedule(ctx, D, V, P, S) < 0)
4214 return -1;
4215 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4216 if (test_special_schedule(ctx, D, V, P, S) < 0)
4217 return -1;
4219 D = "{ A[a]; B[] }";
4220 V = "{}";
4221 P = "{ A[a] -> B[] }";
4222 if (test_has_schedule(ctx, D, V, P) < 0)
4223 return -1;
4225 if (test_padded_schedule(ctx) < 0)
4226 return -1;
4228 /* Check that check for progress is not confused by rational
4229 * solution.
4231 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4232 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4233 "i0 <= -2 + N; "
4234 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4235 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4236 P = "{}";
4237 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4238 if (test_has_schedule(ctx, D, V, P) < 0)
4239 return -1;
4240 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4242 /* Check that we allow schedule rows that are only non-trivial
4243 * on some full-dimensional domains.
4245 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4246 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4247 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4248 P = "{}";
4249 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4250 if (test_has_schedule(ctx, D, V, P) < 0)
4251 return -1;
4252 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4254 if (test_conditional_schedule_constraints(ctx) < 0)
4255 return -1;
4257 if (test_strongly_satisfying_schedule(ctx) < 0)
4258 return -1;
4260 if (test_conflicting_context_schedule(ctx) < 0)
4261 return -1;
4263 if (test_bounded_coefficients_schedule(ctx) < 0)
4264 return -1;
4265 if (test_coalescing_schedule(ctx) < 0)
4266 return -1;
4268 return 0;
4271 /* Perform scheduling tests using the whole component scheduler.
4273 static int test_schedule_whole(isl_ctx *ctx)
4275 int whole;
4276 int r;
4278 whole = isl_options_get_schedule_whole_component(ctx);
4279 isl_options_set_schedule_whole_component(ctx, 1);
4280 r = test_schedule(ctx);
4281 isl_options_set_schedule_whole_component(ctx, whole);
4283 return r;
4286 /* Perform scheduling tests using the incremental scheduler.
4288 static int test_schedule_incremental(isl_ctx *ctx)
4290 int whole;
4291 int r;
4293 whole = isl_options_get_schedule_whole_component(ctx);
4294 isl_options_set_schedule_whole_component(ctx, 0);
4295 r = test_schedule(ctx);
4296 isl_options_set_schedule_whole_component(ctx, whole);
4298 return r;
4301 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4303 isl_union_map *umap;
4304 int test;
4306 umap = isl_union_map_read_from_str(ctx, str);
4307 test = isl_union_map_plain_is_injective(umap);
4308 isl_union_map_free(umap);
4309 if (test < 0)
4310 return -1;
4311 if (test == injective)
4312 return 0;
4313 if (injective)
4314 isl_die(ctx, isl_error_unknown,
4315 "map not detected as injective", return -1);
4316 else
4317 isl_die(ctx, isl_error_unknown,
4318 "map detected as injective", return -1);
4321 int test_injective(isl_ctx *ctx)
4323 const char *str;
4325 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4326 return -1;
4327 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4328 return -1;
4329 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4330 return -1;
4331 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4332 return -1;
4333 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4334 return -1;
4335 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4336 return -1;
4337 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4338 return -1;
4339 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4340 return -1;
4342 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4343 if (test_plain_injective(ctx, str, 1))
4344 return -1;
4345 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4346 if (test_plain_injective(ctx, str, 0))
4347 return -1;
4349 return 0;
4352 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4354 isl_aff *aff2;
4355 int equal;
4357 if (!aff)
4358 return -1;
4360 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4361 equal = isl_aff_plain_is_equal(aff, aff2);
4362 isl_aff_free(aff2);
4364 return equal;
4367 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4369 int equal;
4371 equal = aff_plain_is_equal(aff, str);
4372 if (equal < 0)
4373 return -1;
4374 if (!equal)
4375 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4376 "result not as expected", return -1);
4377 return 0;
4380 struct {
4381 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4382 __isl_take isl_aff *aff2);
4383 } aff_bin_op[] = {
4384 ['+'] = { &isl_aff_add },
4385 ['-'] = { &isl_aff_sub },
4386 ['*'] = { &isl_aff_mul },
4387 ['/'] = { &isl_aff_div },
4390 struct {
4391 const char *arg1;
4392 unsigned char op;
4393 const char *arg2;
4394 const char *res;
4395 } aff_bin_tests[] = {
4396 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
4397 "{ [i] -> [2i] }" },
4398 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
4399 "{ [i] -> [0] }" },
4400 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
4401 "{ [i] -> [2i] }" },
4402 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
4403 "{ [i] -> [2i] }" },
4404 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
4405 "{ [i] -> [i/2] }" },
4406 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
4407 "{ [i] -> [i] }" },
4408 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
4409 "{ [i] -> [NaN] }" },
4410 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
4411 "{ [i] -> [NaN] }" },
4412 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
4413 "{ [i] -> [NaN] }" },
4414 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
4415 "{ [i] -> [NaN] }" },
4416 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
4417 "{ [i] -> [NaN] }" },
4418 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
4419 "{ [i] -> [NaN] }" },
4420 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
4421 "{ [i] -> [NaN] }" },
4422 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
4423 "{ [i] -> [NaN] }" },
4424 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
4425 "{ [i] -> [NaN] }" },
4426 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
4427 "{ [i] -> [NaN] }" },
4428 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
4429 "{ [i] -> [NaN] }" },
4430 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
4431 "{ [i] -> [NaN] }" },
4434 /* Perform some basic tests of binary operations on isl_aff objects.
4436 static int test_bin_aff(isl_ctx *ctx)
4438 int i;
4439 isl_aff *aff1, *aff2, *res;
4440 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4441 __isl_take isl_aff *aff2);
4442 int ok;
4444 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
4445 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
4446 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
4447 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
4448 fn = aff_bin_op[aff_bin_tests[i].op].fn;
4449 aff1 = fn(aff1, aff2);
4450 if (isl_aff_is_nan(res))
4451 ok = isl_aff_is_nan(aff1);
4452 else
4453 ok = isl_aff_plain_is_equal(aff1, res);
4454 isl_aff_free(aff1);
4455 isl_aff_free(res);
4456 if (ok < 0)
4457 return -1;
4458 if (!ok)
4459 isl_die(ctx, isl_error_unknown,
4460 "unexpected result", return -1);
4463 return 0;
4466 struct {
4467 __isl_give isl_union_pw_multi_aff *(*fn)(
4468 __isl_take isl_union_pw_multi_aff *upma1,
4469 __isl_take isl_union_pw_multi_aff *upma2);
4470 const char *arg1;
4471 const char *arg2;
4472 const char *res;
4473 } upma_bin_tests[] = {
4474 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
4475 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
4476 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
4477 "{ B[x] -> [2] : x >= 0 }",
4478 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
4479 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
4480 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
4481 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
4482 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
4483 "D[i] -> B[2] : i >= 5 }" },
4484 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4485 "{ B[x] -> C[2] : x > 0 }",
4486 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
4487 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4488 "{ B[x] -> A[2] : x >= 0 }",
4489 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
4492 /* Perform some basic tests of binary operations on
4493 * isl_union_pw_multi_aff objects.
4495 static int test_bin_upma(isl_ctx *ctx)
4497 int i;
4498 isl_union_pw_multi_aff *upma1, *upma2, *res;
4499 int ok;
4501 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
4502 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4503 upma_bin_tests[i].arg1);
4504 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4505 upma_bin_tests[i].arg2);
4506 res = isl_union_pw_multi_aff_read_from_str(ctx,
4507 upma_bin_tests[i].res);
4508 upma1 = upma_bin_tests[i].fn(upma1, upma2);
4509 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
4510 isl_union_pw_multi_aff_free(upma1);
4511 isl_union_pw_multi_aff_free(res);
4512 if (ok < 0)
4513 return -1;
4514 if (!ok)
4515 isl_die(ctx, isl_error_unknown,
4516 "unexpected result", return -1);
4519 return 0;
4522 struct {
4523 __isl_give isl_union_pw_multi_aff *(*fn)(
4524 __isl_take isl_union_pw_multi_aff *upma1,
4525 __isl_take isl_union_pw_multi_aff *upma2);
4526 const char *arg1;
4527 const char *arg2;
4528 } upma_bin_fail_tests[] = {
4529 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4530 "{ B[x] -> C[2] : x >= 0 }" },
4533 /* Perform some basic tests of binary operations on
4534 * isl_union_pw_multi_aff objects that are expected to fail.
4536 static int test_bin_upma_fail(isl_ctx *ctx)
4538 int i, n;
4539 isl_union_pw_multi_aff *upma1, *upma2;
4540 int on_error;
4542 on_error = isl_options_get_on_error(ctx);
4543 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
4544 n = ARRAY_SIZE(upma_bin_fail_tests);
4545 for (i = 0; i < n; ++i) {
4546 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4547 upma_bin_fail_tests[i].arg1);
4548 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4549 upma_bin_fail_tests[i].arg2);
4550 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
4551 isl_union_pw_multi_aff_free(upma1);
4552 if (upma1)
4553 break;
4555 isl_options_set_on_error(ctx, on_error);
4556 if (i < n)
4557 isl_die(ctx, isl_error_unknown,
4558 "operation not expected to succeed", return -1);
4560 return 0;
4563 int test_aff(isl_ctx *ctx)
4565 const char *str;
4566 isl_set *set;
4567 isl_space *space;
4568 isl_local_space *ls;
4569 isl_aff *aff;
4570 int zero, equal;
4572 if (test_bin_aff(ctx) < 0)
4573 return -1;
4574 if (test_bin_upma(ctx) < 0)
4575 return -1;
4576 if (test_bin_upma_fail(ctx) < 0)
4577 return -1;
4579 space = isl_space_set_alloc(ctx, 0, 1);
4580 ls = isl_local_space_from_space(space);
4581 aff = isl_aff_zero_on_domain(ls);
4583 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4584 aff = isl_aff_scale_down_ui(aff, 3);
4585 aff = isl_aff_floor(aff);
4586 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4587 aff = isl_aff_scale_down_ui(aff, 2);
4588 aff = isl_aff_floor(aff);
4589 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4591 str = "{ [10] }";
4592 set = isl_set_read_from_str(ctx, str);
4593 aff = isl_aff_gist(aff, set);
4595 aff = isl_aff_add_constant_si(aff, -16);
4596 zero = isl_aff_plain_is_zero(aff);
4597 isl_aff_free(aff);
4599 if (zero < 0)
4600 return -1;
4601 if (!zero)
4602 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4604 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
4605 aff = isl_aff_scale_down_ui(aff, 64);
4606 aff = isl_aff_floor(aff);
4607 equal = aff_check_plain_equal(aff, "{ [-1] }");
4608 isl_aff_free(aff);
4609 if (equal < 0)
4610 return -1;
4612 return 0;
4615 /* Check that the computation below results in a single expression.
4616 * One or two expressions may result depending on which constraint
4617 * ends up being considered as redundant with respect to the other
4618 * constraints after the projection that is performed internally
4619 * by isl_set_dim_min.
4621 static int test_dim_max_1(isl_ctx *ctx)
4623 const char *str;
4624 isl_set *set;
4625 isl_pw_aff *pa;
4626 int n;
4628 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
4629 "-4a <= b <= 3 and b < n - 4a }";
4630 set = isl_set_read_from_str(ctx, str);
4631 pa = isl_set_dim_min(set, 0);
4632 n = isl_pw_aff_n_piece(pa);
4633 isl_pw_aff_free(pa);
4635 if (!pa)
4636 return -1;
4637 if (n != 1)
4638 isl_die(ctx, isl_error_unknown, "expecting single expression",
4639 return -1);
4641 return 0;
4644 int test_dim_max(isl_ctx *ctx)
4646 int equal;
4647 const char *str;
4648 isl_set *set1, *set2;
4649 isl_set *set;
4650 isl_map *map;
4651 isl_pw_aff *pwaff;
4653 if (test_dim_max_1(ctx) < 0)
4654 return -1;
4656 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
4657 set = isl_set_read_from_str(ctx, str);
4658 pwaff = isl_set_dim_max(set, 0);
4659 set1 = isl_set_from_pw_aff(pwaff);
4660 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
4661 set2 = isl_set_read_from_str(ctx, str);
4662 equal = isl_set_is_equal(set1, set2);
4663 isl_set_free(set1);
4664 isl_set_free(set2);
4665 if (equal < 0)
4666 return -1;
4667 if (!equal)
4668 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4670 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
4671 set = isl_set_read_from_str(ctx, str);
4672 pwaff = isl_set_dim_max(set, 0);
4673 set1 = isl_set_from_pw_aff(pwaff);
4674 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4675 set2 = isl_set_read_from_str(ctx, str);
4676 equal = isl_set_is_equal(set1, set2);
4677 isl_set_free(set1);
4678 isl_set_free(set2);
4679 if (equal < 0)
4680 return -1;
4681 if (!equal)
4682 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4684 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
4685 set = isl_set_read_from_str(ctx, str);
4686 pwaff = isl_set_dim_max(set, 0);
4687 set1 = isl_set_from_pw_aff(pwaff);
4688 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4689 set2 = isl_set_read_from_str(ctx, str);
4690 equal = isl_set_is_equal(set1, set2);
4691 isl_set_free(set1);
4692 isl_set_free(set2);
4693 if (equal < 0)
4694 return -1;
4695 if (!equal)
4696 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4698 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
4699 "0 <= i < N and 0 <= j < M }";
4700 map = isl_map_read_from_str(ctx, str);
4701 set = isl_map_range(map);
4703 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
4704 set1 = isl_set_from_pw_aff(pwaff);
4705 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
4706 set2 = isl_set_read_from_str(ctx, str);
4707 equal = isl_set_is_equal(set1, set2);
4708 isl_set_free(set1);
4709 isl_set_free(set2);
4711 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
4712 set1 = isl_set_from_pw_aff(pwaff);
4713 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
4714 set2 = isl_set_read_from_str(ctx, str);
4715 if (equal >= 0 && equal)
4716 equal = isl_set_is_equal(set1, set2);
4717 isl_set_free(set1);
4718 isl_set_free(set2);
4720 isl_set_free(set);
4722 if (equal < 0)
4723 return -1;
4724 if (!equal)
4725 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4727 /* Check that solutions are properly merged. */
4728 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
4729 "c <= -1 + n - 4a - 2b and c >= -2b and "
4730 "4a >= -4 + n and c >= 0 }";
4731 set = isl_set_read_from_str(ctx, str);
4732 pwaff = isl_set_dim_min(set, 2);
4733 set1 = isl_set_from_pw_aff(pwaff);
4734 str = "[n] -> { [(0)] : n >= 1 }";
4735 set2 = isl_set_read_from_str(ctx, str);
4736 equal = isl_set_is_equal(set1, set2);
4737 isl_set_free(set1);
4738 isl_set_free(set2);
4740 if (equal < 0)
4741 return -1;
4742 if (!equal)
4743 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4745 /* Check that empty solution lie in the right space. */
4746 str = "[n] -> { [t,a] : 1 = 0 }";
4747 set = isl_set_read_from_str(ctx, str);
4748 pwaff = isl_set_dim_max(set, 0);
4749 set1 = isl_set_from_pw_aff(pwaff);
4750 str = "[n] -> { [t] : 1 = 0 }";
4751 set2 = isl_set_read_from_str(ctx, str);
4752 equal = isl_set_is_equal(set1, set2);
4753 isl_set_free(set1);
4754 isl_set_free(set2);
4756 if (equal < 0)
4757 return -1;
4758 if (!equal)
4759 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4761 return 0;
4764 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
4766 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
4767 const char *str)
4769 isl_ctx *ctx;
4770 isl_pw_multi_aff *pma2;
4771 int equal;
4773 if (!pma)
4774 return -1;
4776 ctx = isl_pw_multi_aff_get_ctx(pma);
4777 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4778 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
4779 isl_pw_multi_aff_free(pma2);
4781 return equal;
4784 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
4785 * represented by "str".
4787 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
4788 const char *str)
4790 int equal;
4792 equal = pw_multi_aff_plain_is_equal(pma, str);
4793 if (equal < 0)
4794 return -1;
4795 if (!equal)
4796 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
4797 "result not as expected", return -1);
4798 return 0;
4801 /* Basic test for isl_pw_multi_aff_product.
4803 * Check that multiple pieces are properly handled.
4805 static int test_product_pma(isl_ctx *ctx)
4807 int equal;
4808 const char *str;
4809 isl_pw_multi_aff *pma1, *pma2;
4811 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
4812 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4813 str = "{ C[] -> D[] }";
4814 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4815 pma1 = isl_pw_multi_aff_product(pma1, pma2);
4816 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
4817 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
4818 equal = pw_multi_aff_check_plain_equal(pma1, str);
4819 isl_pw_multi_aff_free(pma1);
4820 if (equal < 0)
4821 return -1;
4823 return 0;
4826 int test_product(isl_ctx *ctx)
4828 const char *str;
4829 isl_set *set;
4830 isl_union_set *uset1, *uset2;
4831 int ok;
4833 str = "{ A[i] }";
4834 set = isl_set_read_from_str(ctx, str);
4835 set = isl_set_product(set, isl_set_copy(set));
4836 ok = isl_set_is_wrapping(set);
4837 isl_set_free(set);
4838 if (ok < 0)
4839 return -1;
4840 if (!ok)
4841 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4843 str = "{ [] }";
4844 uset1 = isl_union_set_read_from_str(ctx, str);
4845 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
4846 str = "{ [[] -> []] }";
4847 uset2 = isl_union_set_read_from_str(ctx, str);
4848 ok = isl_union_set_is_equal(uset1, uset2);
4849 isl_union_set_free(uset1);
4850 isl_union_set_free(uset2);
4851 if (ok < 0)
4852 return -1;
4853 if (!ok)
4854 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4856 if (test_product_pma(ctx) < 0)
4857 return -1;
4859 return 0;
4862 /* Check that two sets are not considered disjoint just because
4863 * they have a different set of (named) parameters.
4865 static int test_disjoint(isl_ctx *ctx)
4867 const char *str;
4868 isl_set *set, *set2;
4869 int disjoint;
4871 str = "[n] -> { [[]->[]] }";
4872 set = isl_set_read_from_str(ctx, str);
4873 str = "{ [[]->[]] }";
4874 set2 = isl_set_read_from_str(ctx, str);
4875 disjoint = isl_set_is_disjoint(set, set2);
4876 isl_set_free(set);
4877 isl_set_free(set2);
4878 if (disjoint < 0)
4879 return -1;
4880 if (disjoint)
4881 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4883 return 0;
4886 int test_equal(isl_ctx *ctx)
4888 const char *str;
4889 isl_set *set, *set2;
4890 int equal;
4892 str = "{ S_6[i] }";
4893 set = isl_set_read_from_str(ctx, str);
4894 str = "{ S_7[i] }";
4895 set2 = isl_set_read_from_str(ctx, str);
4896 equal = isl_set_is_equal(set, set2);
4897 isl_set_free(set);
4898 isl_set_free(set2);
4899 if (equal < 0)
4900 return -1;
4901 if (equal)
4902 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4904 return 0;
4907 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
4908 enum isl_dim_type type, unsigned pos, int fixed)
4910 int test;
4912 test = isl_map_plain_is_fixed(map, type, pos, NULL);
4913 isl_map_free(map);
4914 if (test < 0)
4915 return -1;
4916 if (test == fixed)
4917 return 0;
4918 if (fixed)
4919 isl_die(ctx, isl_error_unknown,
4920 "map not detected as fixed", return -1);
4921 else
4922 isl_die(ctx, isl_error_unknown,
4923 "map detected as fixed", return -1);
4926 int test_fixed(isl_ctx *ctx)
4928 const char *str;
4929 isl_map *map;
4931 str = "{ [i] -> [i] }";
4932 map = isl_map_read_from_str(ctx, str);
4933 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
4934 return -1;
4935 str = "{ [i] -> [1] }";
4936 map = isl_map_read_from_str(ctx, str);
4937 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4938 return -1;
4939 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
4940 map = isl_map_read_from_str(ctx, str);
4941 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4942 return -1;
4943 map = isl_map_read_from_str(ctx, str);
4944 map = isl_map_neg(map);
4945 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4946 return -1;
4948 return 0;
4951 struct isl_vertices_test_data {
4952 const char *set;
4953 int n;
4954 const char *vertex[6];
4955 } vertices_tests[] = {
4956 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
4957 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
4958 { "{ A[t, i] : t = 14 and i = 1 }",
4959 1, { "{ A[14, 1] }" } },
4960 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
4961 "c <= m and m <= n and m > 0 }",
4962 6, {
4963 "[n, m] -> { [n, m, m] : 0 < m <= n }",
4964 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
4965 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
4966 "[n, m] -> { [m, m, m] : 0 < m <= n }",
4967 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
4968 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
4969 } },
4972 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
4974 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
4976 struct isl_vertices_test_data *data = user;
4977 isl_ctx *ctx;
4978 isl_multi_aff *ma;
4979 isl_basic_set *bset;
4980 isl_pw_multi_aff *pma;
4981 int i;
4982 isl_bool equal;
4984 ctx = isl_vertex_get_ctx(vertex);
4985 bset = isl_vertex_get_domain(vertex);
4986 ma = isl_vertex_get_expr(vertex);
4987 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
4989 for (i = 0; i < data->n; ++i) {
4990 isl_pw_multi_aff *pma_i;
4992 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
4993 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
4994 isl_pw_multi_aff_free(pma_i);
4996 if (equal < 0 || equal)
4997 break;
5000 isl_pw_multi_aff_free(pma);
5001 isl_vertex_free(vertex);
5003 if (equal < 0)
5004 return isl_stat_error;
5006 return equal ? isl_stat_ok : isl_stat_error;
5009 int test_vertices(isl_ctx *ctx)
5011 int i;
5013 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
5014 isl_basic_set *bset;
5015 isl_vertices *vertices;
5016 int ok = 1;
5017 int n;
5019 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
5020 vertices = isl_basic_set_compute_vertices(bset);
5021 n = isl_vertices_get_n_vertices(vertices);
5022 if (vertices_tests[i].n != n)
5023 ok = 0;
5024 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
5025 &vertices_tests[i]) < 0)
5026 ok = 0;
5027 isl_vertices_free(vertices);
5028 isl_basic_set_free(bset);
5030 if (!vertices)
5031 return -1;
5032 if (!ok)
5033 isl_die(ctx, isl_error_unknown, "unexpected vertices",
5034 return -1);
5037 return 0;
5040 int test_union_pw(isl_ctx *ctx)
5042 int equal;
5043 const char *str;
5044 isl_union_set *uset;
5045 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
5047 str = "{ [x] -> x^2 }";
5048 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
5049 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
5050 uset = isl_union_pw_qpolynomial_domain(upwqp1);
5051 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
5052 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
5053 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
5054 isl_union_pw_qpolynomial_free(upwqp1);
5055 isl_union_pw_qpolynomial_free(upwqp2);
5056 if (equal < 0)
5057 return -1;
5058 if (!equal)
5059 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5061 return 0;
5064 /* Test that isl_union_pw_qpolynomial_eval picks up the function
5065 * defined over the correct domain space.
5067 static int test_eval_1(isl_ctx *ctx)
5069 const char *str;
5070 isl_point *pnt;
5071 isl_set *set;
5072 isl_union_pw_qpolynomial *upwqp;
5073 isl_val *v;
5074 int cmp;
5076 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
5077 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
5078 str = "{ A[6] }";
5079 set = isl_set_read_from_str(ctx, str);
5080 pnt = isl_set_sample_point(set);
5081 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
5082 cmp = isl_val_cmp_si(v, 36);
5083 isl_val_free(v);
5085 if (!v)
5086 return -1;
5087 if (cmp != 0)
5088 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
5090 return 0;
5093 /* Check that isl_qpolynomial_eval handles getting called on a void point.
5095 static int test_eval_2(isl_ctx *ctx)
5097 const char *str;
5098 isl_point *pnt;
5099 isl_set *set;
5100 isl_qpolynomial *qp;
5101 isl_val *v;
5102 isl_bool ok;
5104 str = "{ A[x] -> [x] }";
5105 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
5106 str = "{ A[x] : false }";
5107 set = isl_set_read_from_str(ctx, str);
5108 pnt = isl_set_sample_point(set);
5109 v = isl_qpolynomial_eval(qp, pnt);
5110 ok = isl_val_is_nan(v);
5111 isl_val_free(v);
5113 if (ok < 0)
5114 return -1;
5115 if (!ok)
5116 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
5118 return 0;
5121 /* Perform basic polynomial evaluation tests.
5123 static int test_eval(isl_ctx *ctx)
5125 if (test_eval_1(ctx) < 0)
5126 return -1;
5127 if (test_eval_2(ctx) < 0)
5128 return -1;
5129 return 0;
5132 /* Descriptions of sets that are tested for reparsing after printing.
5134 const char *output_tests[] = {
5135 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
5138 /* Check that printing a set and reparsing a set from the printed output
5139 * results in the same set.
5141 static int test_output_set(isl_ctx *ctx)
5143 int i;
5144 char *str;
5145 isl_set *set1, *set2;
5146 isl_bool equal;
5148 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
5149 set1 = isl_set_read_from_str(ctx, output_tests[i]);
5150 str = isl_set_to_str(set1);
5151 set2 = isl_set_read_from_str(ctx, str);
5152 free(str);
5153 equal = isl_set_is_equal(set1, set2);
5154 isl_set_free(set1);
5155 isl_set_free(set2);
5156 if (equal < 0)
5157 return -1;
5158 if (!equal)
5159 isl_die(ctx, isl_error_unknown,
5160 "parsed output not the same", return -1);
5163 return 0;
5166 int test_output(isl_ctx *ctx)
5168 char *s;
5169 const char *str;
5170 isl_pw_aff *pa;
5171 isl_printer *p;
5172 int equal;
5174 if (test_output_set(ctx) < 0)
5175 return -1;
5177 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
5178 pa = isl_pw_aff_read_from_str(ctx, str);
5180 p = isl_printer_to_str(ctx);
5181 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
5182 p = isl_printer_print_pw_aff(p, pa);
5183 s = isl_printer_get_str(p);
5184 isl_printer_free(p);
5185 isl_pw_aff_free(pa);
5186 if (!s)
5187 equal = -1;
5188 else
5189 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
5190 free(s);
5191 if (equal < 0)
5192 return -1;
5193 if (!equal)
5194 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5196 return 0;
5199 int test_sample(isl_ctx *ctx)
5201 const char *str;
5202 isl_basic_set *bset1, *bset2;
5203 int empty, subset;
5205 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
5206 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
5207 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
5208 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
5209 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
5210 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
5211 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
5212 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
5213 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
5214 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
5215 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
5216 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
5217 "d - 1073741821e and "
5218 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
5219 "3j >= 1 - a + b + 2e and "
5220 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
5221 "3i <= 4 - a + 4b - e and "
5222 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
5223 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
5224 "c <= -1 + a and 3i >= -2 - a + 3e and "
5225 "1073741822e <= 1073741823 - a + 1073741822b + c and "
5226 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
5227 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
5228 "1073741823e >= 1 + 1073741823b - d and "
5229 "3i >= 1073741823b + c - 1073741823e - f and "
5230 "3i >= 1 + 2b + e + 3g }";
5231 bset1 = isl_basic_set_read_from_str(ctx, str);
5232 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
5233 empty = isl_basic_set_is_empty(bset2);
5234 subset = isl_basic_set_is_subset(bset2, bset1);
5235 isl_basic_set_free(bset1);
5236 isl_basic_set_free(bset2);
5237 if (empty < 0 || subset < 0)
5238 return -1;
5239 if (empty)
5240 isl_die(ctx, isl_error_unknown, "point not found", return -1);
5241 if (!subset)
5242 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
5244 return 0;
5247 int test_fixed_power(isl_ctx *ctx)
5249 const char *str;
5250 isl_map *map;
5251 isl_int exp;
5252 int equal;
5254 isl_int_init(exp);
5255 str = "{ [i] -> [i + 1] }";
5256 map = isl_map_read_from_str(ctx, str);
5257 isl_int_set_si(exp, 23);
5258 map = isl_map_fixed_power(map, exp);
5259 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
5260 isl_int_clear(exp);
5261 isl_map_free(map);
5262 if (equal < 0)
5263 return -1;
5265 return 0;
5268 int test_slice(isl_ctx *ctx)
5270 const char *str;
5271 isl_map *map;
5272 int equal;
5274 str = "{ [i] -> [j] }";
5275 map = isl_map_read_from_str(ctx, str);
5276 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
5277 equal = map_check_equal(map, "{ [i] -> [i] }");
5278 isl_map_free(map);
5279 if (equal < 0)
5280 return -1;
5282 str = "{ [i] -> [j] }";
5283 map = isl_map_read_from_str(ctx, str);
5284 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
5285 equal = map_check_equal(map, "{ [i] -> [j] }");
5286 isl_map_free(map);
5287 if (equal < 0)
5288 return -1;
5290 str = "{ [i] -> [j] }";
5291 map = isl_map_read_from_str(ctx, str);
5292 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
5293 equal = map_check_equal(map, "{ [i] -> [-i] }");
5294 isl_map_free(map);
5295 if (equal < 0)
5296 return -1;
5298 str = "{ [i] -> [j] }";
5299 map = isl_map_read_from_str(ctx, str);
5300 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
5301 equal = map_check_equal(map, "{ [0] -> [j] }");
5302 isl_map_free(map);
5303 if (equal < 0)
5304 return -1;
5306 str = "{ [i] -> [j] }";
5307 map = isl_map_read_from_str(ctx, str);
5308 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
5309 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
5310 isl_map_free(map);
5311 if (equal < 0)
5312 return -1;
5314 str = "{ [i] -> [j] }";
5315 map = isl_map_read_from_str(ctx, str);
5316 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
5317 equal = map_check_equal(map, "{ [i] -> [j] : false }");
5318 isl_map_free(map);
5319 if (equal < 0)
5320 return -1;
5322 return 0;
5325 int test_eliminate(isl_ctx *ctx)
5327 const char *str;
5328 isl_map *map;
5329 int equal;
5331 str = "{ [i] -> [j] : i = 2j }";
5332 map = isl_map_read_from_str(ctx, str);
5333 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
5334 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
5335 isl_map_free(map);
5336 if (equal < 0)
5337 return -1;
5339 return 0;
5342 /* Check that isl_set_dim_residue_class detects that the values of j
5343 * in the set below are all odd and that it does not detect any spurious
5344 * strides.
5346 static int test_residue_class(isl_ctx *ctx)
5348 const char *str;
5349 isl_set *set;
5350 isl_int m, r;
5351 int res;
5353 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
5354 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
5355 set = isl_set_read_from_str(ctx, str);
5356 isl_int_init(m);
5357 isl_int_init(r);
5358 res = isl_set_dim_residue_class(set, 1, &m, &r);
5359 if (res >= 0 &&
5360 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
5361 isl_die(ctx, isl_error_unknown, "incorrect residue class",
5362 res = -1);
5363 isl_int_clear(r);
5364 isl_int_clear(m);
5365 isl_set_free(set);
5367 return res;
5370 int test_align_parameters(isl_ctx *ctx)
5372 const char *str;
5373 isl_space *space;
5374 isl_multi_aff *ma1, *ma2;
5375 int equal;
5377 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
5378 ma1 = isl_multi_aff_read_from_str(ctx, str);
5380 space = isl_space_params_alloc(ctx, 1);
5381 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
5382 ma1 = isl_multi_aff_align_params(ma1, space);
5384 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
5385 ma2 = isl_multi_aff_read_from_str(ctx, str);
5387 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5389 isl_multi_aff_free(ma1);
5390 isl_multi_aff_free(ma2);
5392 if (equal < 0)
5393 return -1;
5394 if (!equal)
5395 isl_die(ctx, isl_error_unknown,
5396 "result not as expected", return -1);
5398 return 0;
5401 static int test_list(isl_ctx *ctx)
5403 isl_id *a, *b, *c, *d, *id;
5404 isl_id_list *list;
5405 int ok;
5407 a = isl_id_alloc(ctx, "a", NULL);
5408 b = isl_id_alloc(ctx, "b", NULL);
5409 c = isl_id_alloc(ctx, "c", NULL);
5410 d = isl_id_alloc(ctx, "d", NULL);
5412 list = isl_id_list_alloc(ctx, 4);
5413 list = isl_id_list_add(list, a);
5414 list = isl_id_list_add(list, b);
5415 list = isl_id_list_add(list, c);
5416 list = isl_id_list_add(list, d);
5417 list = isl_id_list_drop(list, 1, 1);
5419 if (isl_id_list_n_id(list) != 3) {
5420 isl_id_list_free(list);
5421 isl_die(ctx, isl_error_unknown,
5422 "unexpected number of elements in list", return -1);
5425 id = isl_id_list_get_id(list, 0);
5426 ok = id == a;
5427 isl_id_free(id);
5428 id = isl_id_list_get_id(list, 1);
5429 ok = ok && id == c;
5430 isl_id_free(id);
5431 id = isl_id_list_get_id(list, 2);
5432 ok = ok && id == d;
5433 isl_id_free(id);
5435 isl_id_list_free(list);
5437 if (!ok)
5438 isl_die(ctx, isl_error_unknown,
5439 "unexpected elements in list", return -1);
5441 return 0;
5444 const char *set_conversion_tests[] = {
5445 "[N] -> { [i] : N - 1 <= 2 i <= N }",
5446 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
5447 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5448 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5449 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
5450 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
5451 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
5452 "-3 + c <= d <= 28 + c) }",
5455 /* Check that converting from isl_set to isl_pw_multi_aff and back
5456 * to isl_set produces the original isl_set.
5458 static int test_set_conversion(isl_ctx *ctx)
5460 int i;
5461 const char *str;
5462 isl_set *set1, *set2;
5463 isl_pw_multi_aff *pma;
5464 int equal;
5466 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
5467 str = set_conversion_tests[i];
5468 set1 = isl_set_read_from_str(ctx, str);
5469 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
5470 set2 = isl_set_from_pw_multi_aff(pma);
5471 equal = isl_set_is_equal(set1, set2);
5472 isl_set_free(set1);
5473 isl_set_free(set2);
5475 if (equal < 0)
5476 return -1;
5477 if (!equal)
5478 isl_die(ctx, isl_error_unknown, "bad conversion",
5479 return -1);
5482 return 0;
5485 const char *conversion_tests[] = {
5486 "{ [a, b, c, d] -> s0[a, b, e, f] : "
5487 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
5488 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
5489 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
5490 "9e <= -2 - 2a) }",
5491 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
5492 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
5493 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
5496 /* Check that converting from isl_map to isl_pw_multi_aff and back
5497 * to isl_map produces the original isl_map.
5499 static int test_map_conversion(isl_ctx *ctx)
5501 int i;
5502 isl_map *map1, *map2;
5503 isl_pw_multi_aff *pma;
5504 int equal;
5506 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
5507 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
5508 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
5509 map2 = isl_map_from_pw_multi_aff(pma);
5510 equal = isl_map_is_equal(map1, map2);
5511 isl_map_free(map1);
5512 isl_map_free(map2);
5514 if (equal < 0)
5515 return -1;
5516 if (!equal)
5517 isl_die(ctx, isl_error_unknown, "bad conversion",
5518 return -1);
5521 return 0;
5524 static int test_conversion(isl_ctx *ctx)
5526 if (test_set_conversion(ctx) < 0)
5527 return -1;
5528 if (test_map_conversion(ctx) < 0)
5529 return -1;
5530 return 0;
5533 /* Check that isl_basic_map_curry does not modify input.
5535 static int test_curry(isl_ctx *ctx)
5537 const char *str;
5538 isl_basic_map *bmap1, *bmap2;
5539 int equal;
5541 str = "{ [A[] -> B[]] -> C[] }";
5542 bmap1 = isl_basic_map_read_from_str(ctx, str);
5543 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
5544 equal = isl_basic_map_is_equal(bmap1, bmap2);
5545 isl_basic_map_free(bmap1);
5546 isl_basic_map_free(bmap2);
5548 if (equal < 0)
5549 return -1;
5550 if (equal)
5551 isl_die(ctx, isl_error_unknown,
5552 "curried map should not be equal to original",
5553 return -1);
5555 return 0;
5558 struct {
5559 const char *set;
5560 const char *ma;
5561 const char *res;
5562 } preimage_tests[] = {
5563 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
5564 "{ A[j,i] -> B[i,j] }",
5565 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
5566 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5567 "{ A[a,b] -> B[a/2,b/6] }",
5568 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
5569 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5570 "{ A[a,b] -> B[a/2,b/6] }",
5571 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
5572 "exists i,j : a = 2 i and b = 6 j }" },
5573 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
5574 "[n] -> { : 0 <= n <= 100 }" },
5575 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5576 "{ A[a] -> B[2a] }",
5577 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
5578 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5579 "{ A[a] -> B[([a/2])] }",
5580 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
5581 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
5582 "{ A[a] -> B[a,a,a/3] }",
5583 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
5584 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
5585 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
5588 static int test_preimage_basic_set(isl_ctx *ctx)
5590 int i;
5591 isl_basic_set *bset1, *bset2;
5592 isl_multi_aff *ma;
5593 int equal;
5595 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
5596 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
5597 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
5598 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
5599 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
5600 equal = isl_basic_set_is_equal(bset1, bset2);
5601 isl_basic_set_free(bset1);
5602 isl_basic_set_free(bset2);
5603 if (equal < 0)
5604 return -1;
5605 if (!equal)
5606 isl_die(ctx, isl_error_unknown, "bad preimage",
5607 return -1);
5610 return 0;
5613 struct {
5614 const char *map;
5615 const char *ma;
5616 const char *res;
5617 } preimage_domain_tests[] = {
5618 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
5619 "{ A[j,i] -> B[i,j] }",
5620 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
5621 { "{ B[i] -> C[i]; D[i] -> E[i] }",
5622 "{ A[i] -> B[i + 1] }",
5623 "{ A[i] -> C[i + 1] }" },
5624 { "{ B[i] -> C[i]; B[i] -> E[i] }",
5625 "{ A[i] -> B[i + 1] }",
5626 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
5627 { "{ B[i] -> C[([i/2])] }",
5628 "{ A[i] -> B[2i] }",
5629 "{ A[i] -> C[i] }" },
5630 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
5631 "{ A[i] -> B[([i/5]), ([i/7])] }",
5632 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
5633 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
5634 "[N] -> { A[] -> B[([N/5])] }",
5635 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
5636 { "{ B[i] -> C[i] : exists a : i = 5 a }",
5637 "{ A[i] -> B[2i] }",
5638 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
5639 { "{ B[i] -> C[i] : exists a : i = 2 a; "
5640 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
5641 "{ A[i] -> B[2i] }",
5642 "{ A[i] -> C[2i] }" },
5643 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
5644 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
5647 static int test_preimage_union_map(isl_ctx *ctx)
5649 int i;
5650 isl_union_map *umap1, *umap2;
5651 isl_multi_aff *ma;
5652 int equal;
5654 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
5655 umap1 = isl_union_map_read_from_str(ctx,
5656 preimage_domain_tests[i].map);
5657 ma = isl_multi_aff_read_from_str(ctx,
5658 preimage_domain_tests[i].ma);
5659 umap2 = isl_union_map_read_from_str(ctx,
5660 preimage_domain_tests[i].res);
5661 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
5662 equal = isl_union_map_is_equal(umap1, umap2);
5663 isl_union_map_free(umap1);
5664 isl_union_map_free(umap2);
5665 if (equal < 0)
5666 return -1;
5667 if (!equal)
5668 isl_die(ctx, isl_error_unknown, "bad preimage",
5669 return -1);
5672 return 0;
5675 static int test_preimage(isl_ctx *ctx)
5677 if (test_preimage_basic_set(ctx) < 0)
5678 return -1;
5679 if (test_preimage_union_map(ctx) < 0)
5680 return -1;
5682 return 0;
5685 struct {
5686 const char *ma1;
5687 const char *ma;
5688 const char *res;
5689 } pullback_tests[] = {
5690 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
5691 "{ A[a,b] -> C[b + 2a] }" },
5692 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
5693 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
5694 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
5695 "{ A[a] -> C[(a)/6] }" },
5696 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
5697 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
5698 "{ A[a] -> C[(2a)/3] }" },
5699 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
5700 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
5701 "{ A[i,j] -> C[i + j, i + j] }"},
5702 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
5703 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
5704 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
5705 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
5706 "{ [i, j] -> [floor((i)/3), j] }",
5707 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
5710 static int test_pullback(isl_ctx *ctx)
5712 int i;
5713 isl_multi_aff *ma1, *ma2;
5714 isl_multi_aff *ma;
5715 int equal;
5717 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
5718 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
5719 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
5720 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
5721 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
5722 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5723 isl_multi_aff_free(ma1);
5724 isl_multi_aff_free(ma2);
5725 if (equal < 0)
5726 return -1;
5727 if (!equal)
5728 isl_die(ctx, isl_error_unknown, "bad pullback",
5729 return -1);
5732 return 0;
5735 /* Check that negation is printed correctly and that equal expressions
5736 * are correctly identified.
5738 static int test_ast(isl_ctx *ctx)
5740 isl_ast_expr *expr, *expr1, *expr2, *expr3;
5741 char *str;
5742 int ok, equal;
5744 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5745 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5746 expr = isl_ast_expr_add(expr1, expr2);
5747 expr2 = isl_ast_expr_copy(expr);
5748 expr = isl_ast_expr_neg(expr);
5749 expr2 = isl_ast_expr_neg(expr2);
5750 equal = isl_ast_expr_is_equal(expr, expr2);
5751 str = isl_ast_expr_to_C_str(expr);
5752 ok = str ? !strcmp(str, "-(A + B)") : -1;
5753 free(str);
5754 isl_ast_expr_free(expr);
5755 isl_ast_expr_free(expr2);
5757 if (ok < 0 || equal < 0)
5758 return -1;
5759 if (!equal)
5760 isl_die(ctx, isl_error_unknown,
5761 "equal expressions not considered equal", return -1);
5762 if (!ok)
5763 isl_die(ctx, isl_error_unknown,
5764 "isl_ast_expr printed incorrectly", return -1);
5766 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5767 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5768 expr = isl_ast_expr_add(expr1, expr2);
5769 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
5770 expr = isl_ast_expr_sub(expr3, expr);
5771 str = isl_ast_expr_to_C_str(expr);
5772 ok = str ? !strcmp(str, "C - (A + B)") : -1;
5773 free(str);
5774 isl_ast_expr_free(expr);
5776 if (ok < 0)
5777 return -1;
5778 if (!ok)
5779 isl_die(ctx, isl_error_unknown,
5780 "isl_ast_expr printed incorrectly", return -1);
5782 return 0;
5785 /* Check that isl_ast_build_expr_from_set returns a valid expression
5786 * for an empty set. Note that isl_ast_build_expr_from_set getting
5787 * called on an empty set probably indicates a bug in the caller.
5789 static int test_ast_build(isl_ctx *ctx)
5791 isl_set *set;
5792 isl_ast_build *build;
5793 isl_ast_expr *expr;
5795 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5796 build = isl_ast_build_from_context(set);
5798 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
5799 expr = isl_ast_build_expr_from_set(build, set);
5801 isl_ast_expr_free(expr);
5802 isl_ast_build_free(build);
5804 if (!expr)
5805 return -1;
5807 return 0;
5810 /* Internal data structure for before_for and after_for callbacks.
5812 * depth is the current depth
5813 * before is the number of times before_for has been called
5814 * after is the number of times after_for has been called
5816 struct isl_test_codegen_data {
5817 int depth;
5818 int before;
5819 int after;
5822 /* This function is called before each for loop in the AST generated
5823 * from test_ast_gen1.
5825 * Increment the number of calls and the depth.
5826 * Check that the space returned by isl_ast_build_get_schedule_space
5827 * matches the target space of the schedule returned by
5828 * isl_ast_build_get_schedule.
5829 * Return an isl_id that is checked by the corresponding call
5830 * to after_for.
5832 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
5833 void *user)
5835 struct isl_test_codegen_data *data = user;
5836 isl_ctx *ctx;
5837 isl_space *space;
5838 isl_union_map *schedule;
5839 isl_union_set *uset;
5840 isl_set *set;
5841 int empty;
5842 char name[] = "d0";
5844 ctx = isl_ast_build_get_ctx(build);
5846 if (data->before >= 3)
5847 isl_die(ctx, isl_error_unknown,
5848 "unexpected number of for nodes", return NULL);
5849 if (data->depth >= 2)
5850 isl_die(ctx, isl_error_unknown,
5851 "unexpected depth", return NULL);
5853 snprintf(name, sizeof(name), "d%d", data->depth);
5854 data->before++;
5855 data->depth++;
5857 schedule = isl_ast_build_get_schedule(build);
5858 uset = isl_union_map_range(schedule);
5859 if (!uset)
5860 return NULL;
5861 if (isl_union_set_n_set(uset) != 1) {
5862 isl_union_set_free(uset);
5863 isl_die(ctx, isl_error_unknown,
5864 "expecting single range space", return NULL);
5867 space = isl_ast_build_get_schedule_space(build);
5868 set = isl_union_set_extract_set(uset, space);
5869 isl_union_set_free(uset);
5870 empty = isl_set_is_empty(set);
5871 isl_set_free(set);
5873 if (empty < 0)
5874 return NULL;
5875 if (empty)
5876 isl_die(ctx, isl_error_unknown,
5877 "spaces don't match", return NULL);
5879 return isl_id_alloc(ctx, name, NULL);
5882 /* This function is called after each for loop in the AST generated
5883 * from test_ast_gen1.
5885 * Increment the number of calls and decrement the depth.
5886 * Check that the annotation attached to the node matches
5887 * the isl_id returned by the corresponding call to before_for.
5889 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
5890 __isl_keep isl_ast_build *build, void *user)
5892 struct isl_test_codegen_data *data = user;
5893 isl_id *id;
5894 const char *name;
5895 int valid;
5897 data->after++;
5898 data->depth--;
5900 if (data->after > data->before)
5901 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5902 "mismatch in number of for nodes",
5903 return isl_ast_node_free(node));
5905 id = isl_ast_node_get_annotation(node);
5906 if (!id)
5907 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5908 "missing annotation", return isl_ast_node_free(node));
5910 name = isl_id_get_name(id);
5911 valid = name && atoi(name + 1) == data->depth;
5912 isl_id_free(id);
5914 if (!valid)
5915 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5916 "wrong annotation", return isl_ast_node_free(node));
5918 return node;
5921 /* Check that the before_each_for and after_each_for callbacks
5922 * are called for each for loop in the generated code,
5923 * that they are called in the right order and that the isl_id
5924 * returned from the before_each_for callback is attached to
5925 * the isl_ast_node passed to the corresponding after_each_for call.
5927 static int test_ast_gen1(isl_ctx *ctx)
5929 const char *str;
5930 isl_set *set;
5931 isl_union_map *schedule;
5932 isl_ast_build *build;
5933 isl_ast_node *tree;
5934 struct isl_test_codegen_data data;
5936 str = "[N] -> { : N >= 10 }";
5937 set = isl_set_read_from_str(ctx, str);
5938 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
5939 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
5940 schedule = isl_union_map_read_from_str(ctx, str);
5942 data.before = 0;
5943 data.after = 0;
5944 data.depth = 0;
5945 build = isl_ast_build_from_context(set);
5946 build = isl_ast_build_set_before_each_for(build,
5947 &before_for, &data);
5948 build = isl_ast_build_set_after_each_for(build,
5949 &after_for, &data);
5950 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5951 isl_ast_build_free(build);
5952 if (!tree)
5953 return -1;
5955 isl_ast_node_free(tree);
5957 if (data.before != 3 || data.after != 3)
5958 isl_die(ctx, isl_error_unknown,
5959 "unexpected number of for nodes", return -1);
5961 return 0;
5964 /* Check that the AST generator handles domains that are integrally disjoint
5965 * but not rationally disjoint.
5967 static int test_ast_gen2(isl_ctx *ctx)
5969 const char *str;
5970 isl_set *set;
5971 isl_union_map *schedule;
5972 isl_union_map *options;
5973 isl_ast_build *build;
5974 isl_ast_node *tree;
5976 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
5977 schedule = isl_union_map_read_from_str(ctx, str);
5978 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5979 build = isl_ast_build_from_context(set);
5981 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
5982 options = isl_union_map_read_from_str(ctx, str);
5983 build = isl_ast_build_set_options(build, options);
5984 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5985 isl_ast_build_free(build);
5986 if (!tree)
5987 return -1;
5988 isl_ast_node_free(tree);
5990 return 0;
5993 /* Increment *user on each call.
5995 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
5996 __isl_keep isl_ast_build *build, void *user)
5998 int *n = user;
6000 (*n)++;
6002 return node;
6005 /* Test that unrolling tries to minimize the number of instances.
6006 * In particular, for the schedule given below, make sure it generates
6007 * 3 nodes (rather than 101).
6009 static int test_ast_gen3(isl_ctx *ctx)
6011 const char *str;
6012 isl_set *set;
6013 isl_union_map *schedule;
6014 isl_union_map *options;
6015 isl_ast_build *build;
6016 isl_ast_node *tree;
6017 int n_domain = 0;
6019 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
6020 schedule = isl_union_map_read_from_str(ctx, str);
6021 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6023 str = "{ [i] -> unroll[0] }";
6024 options = isl_union_map_read_from_str(ctx, str);
6026 build = isl_ast_build_from_context(set);
6027 build = isl_ast_build_set_options(build, options);
6028 build = isl_ast_build_set_at_each_domain(build,
6029 &count_domains, &n_domain);
6030 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6031 isl_ast_build_free(build);
6032 if (!tree)
6033 return -1;
6035 isl_ast_node_free(tree);
6037 if (n_domain != 3)
6038 isl_die(ctx, isl_error_unknown,
6039 "unexpected number of for nodes", return -1);
6041 return 0;
6044 /* Check that if the ast_build_exploit_nested_bounds options is set,
6045 * we do not get an outer if node in the generated AST,
6046 * while we do get such an outer if node if the options is not set.
6048 static int test_ast_gen4(isl_ctx *ctx)
6050 const char *str;
6051 isl_set *set;
6052 isl_union_map *schedule;
6053 isl_ast_build *build;
6054 isl_ast_node *tree;
6055 enum isl_ast_node_type type;
6056 int enb;
6058 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
6059 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
6061 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
6063 schedule = isl_union_map_read_from_str(ctx, str);
6064 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6065 build = isl_ast_build_from_context(set);
6066 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6067 isl_ast_build_free(build);
6068 if (!tree)
6069 return -1;
6071 type = isl_ast_node_get_type(tree);
6072 isl_ast_node_free(tree);
6074 if (type == isl_ast_node_if)
6075 isl_die(ctx, isl_error_unknown,
6076 "not expecting if node", return -1);
6078 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
6080 schedule = isl_union_map_read_from_str(ctx, str);
6081 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6082 build = isl_ast_build_from_context(set);
6083 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6084 isl_ast_build_free(build);
6085 if (!tree)
6086 return -1;
6088 type = isl_ast_node_get_type(tree);
6089 isl_ast_node_free(tree);
6091 if (type != isl_ast_node_if)
6092 isl_die(ctx, isl_error_unknown,
6093 "expecting if node", return -1);
6095 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
6097 return 0;
6100 /* This function is called for each leaf in the AST generated
6101 * from test_ast_gen5.
6103 * We finalize the AST generation by extending the outer schedule
6104 * with a zero-dimensional schedule. If this results in any for loops,
6105 * then this means that we did not pass along enough information
6106 * about the outer schedule to the inner AST generation.
6108 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
6109 void *user)
6111 isl_union_map *schedule, *extra;
6112 isl_ast_node *tree;
6114 schedule = isl_ast_build_get_schedule(build);
6115 extra = isl_union_map_copy(schedule);
6116 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
6117 schedule = isl_union_map_range_product(schedule, extra);
6118 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6119 isl_ast_build_free(build);
6121 if (!tree)
6122 return NULL;
6124 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
6125 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
6126 "code should not contain any for loop",
6127 return isl_ast_node_free(tree));
6129 return tree;
6132 /* Check that we do not lose any information when going back and
6133 * forth between internal and external schedule.
6135 * In particular, we create an AST where we unroll the only
6136 * non-constant dimension in the schedule. We therefore do
6137 * not expect any for loops in the AST. However, older versions
6138 * of isl would not pass along enough information about the outer
6139 * schedule when performing an inner code generation from a create_leaf
6140 * callback, resulting in the inner code generation producing a for loop.
6142 static int test_ast_gen5(isl_ctx *ctx)
6144 const char *str;
6145 isl_set *set;
6146 isl_union_map *schedule, *options;
6147 isl_ast_build *build;
6148 isl_ast_node *tree;
6150 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
6151 schedule = isl_union_map_read_from_str(ctx, str);
6153 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
6154 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
6155 options = isl_union_map_read_from_str(ctx, str);
6157 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6158 build = isl_ast_build_from_context(set);
6159 build = isl_ast_build_set_options(build, options);
6160 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
6161 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6162 isl_ast_build_free(build);
6163 isl_ast_node_free(tree);
6164 if (!tree)
6165 return -1;
6167 return 0;
6170 /* Check that the expression
6172 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
6174 * is not combined into
6176 * min(n/2, 0)
6178 * as this would result in n/2 being evaluated in parts of
6179 * the definition domain where n is not a multiple of 2.
6181 static int test_ast_expr(isl_ctx *ctx)
6183 const char *str;
6184 isl_pw_aff *pa;
6185 isl_ast_build *build;
6186 isl_ast_expr *expr;
6187 int min_max;
6188 int is_min;
6190 min_max = isl_options_get_ast_build_detect_min_max(ctx);
6191 isl_options_set_ast_build_detect_min_max(ctx, 1);
6193 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
6194 pa = isl_pw_aff_read_from_str(ctx, str);
6195 build = isl_ast_build_alloc(ctx);
6196 expr = isl_ast_build_expr_from_pw_aff(build, pa);
6197 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
6198 isl_ast_expr_get_op_type(expr) == isl_ast_op_min;
6199 isl_ast_build_free(build);
6200 isl_ast_expr_free(expr);
6202 isl_options_set_ast_build_detect_min_max(ctx, min_max);
6204 if (!expr)
6205 return -1;
6206 if (is_min)
6207 isl_die(ctx, isl_error_unknown,
6208 "expressions should not be combined", return -1);
6210 return 0;
6213 static int test_ast_gen(isl_ctx *ctx)
6215 if (test_ast_gen1(ctx) < 0)
6216 return -1;
6217 if (test_ast_gen2(ctx) < 0)
6218 return -1;
6219 if (test_ast_gen3(ctx) < 0)
6220 return -1;
6221 if (test_ast_gen4(ctx) < 0)
6222 return -1;
6223 if (test_ast_gen5(ctx) < 0)
6224 return -1;
6225 if (test_ast_expr(ctx) < 0)
6226 return -1;
6227 return 0;
6230 /* Check if dropping output dimensions from an isl_pw_multi_aff
6231 * works properly.
6233 static int test_pw_multi_aff(isl_ctx *ctx)
6235 const char *str;
6236 isl_pw_multi_aff *pma1, *pma2;
6237 int equal;
6239 str = "{ [i,j] -> [i+j, 4i-j] }";
6240 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
6241 str = "{ [i,j] -> [4i-j] }";
6242 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6244 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
6246 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6248 isl_pw_multi_aff_free(pma1);
6249 isl_pw_multi_aff_free(pma2);
6250 if (equal < 0)
6251 return -1;
6252 if (!equal)
6253 isl_die(ctx, isl_error_unknown,
6254 "expressions not equal", return -1);
6256 return 0;
6259 /* Check that we can properly parse multi piecewise affine expressions
6260 * where the piecewise affine expressions have different domains.
6262 static int test_multi_pw_aff(isl_ctx *ctx)
6264 const char *str;
6265 isl_set *dom, *dom2;
6266 isl_multi_pw_aff *mpa1, *mpa2;
6267 isl_pw_aff *pa;
6268 int equal;
6269 int equal_domain;
6271 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
6272 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
6273 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
6274 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
6275 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
6276 str = "{ [i] -> [(i : i > 0), 2i] }";
6277 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
6279 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
6281 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
6282 dom = isl_pw_aff_domain(pa);
6283 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
6284 dom2 = isl_pw_aff_domain(pa);
6285 equal_domain = isl_set_is_equal(dom, dom2);
6287 isl_set_free(dom);
6288 isl_set_free(dom2);
6289 isl_multi_pw_aff_free(mpa1);
6290 isl_multi_pw_aff_free(mpa2);
6292 if (equal < 0)
6293 return -1;
6294 if (!equal)
6295 isl_die(ctx, isl_error_unknown,
6296 "expressions not equal", return -1);
6298 if (equal_domain < 0)
6299 return -1;
6300 if (equal_domain)
6301 isl_die(ctx, isl_error_unknown,
6302 "domains unexpectedly equal", return -1);
6304 return 0;
6307 /* This is a regression test for a bug where isl_basic_map_simplify
6308 * would end up in an infinite loop. In particular, we construct
6309 * an empty basic set that is not obviously empty.
6310 * isl_basic_set_is_empty marks the basic set as empty.
6311 * After projecting out i3, the variable can be dropped completely,
6312 * but isl_basic_map_simplify refrains from doing so if the basic set
6313 * is empty and would end up in an infinite loop if it didn't test
6314 * explicitly for empty basic maps in the outer loop.
6316 static int test_simplify_1(isl_ctx *ctx)
6318 const char *str;
6319 isl_basic_set *bset;
6320 int empty;
6322 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
6323 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
6324 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
6325 "i3 >= i2 }";
6326 bset = isl_basic_set_read_from_str(ctx, str);
6327 empty = isl_basic_set_is_empty(bset);
6328 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
6329 isl_basic_set_free(bset);
6330 if (!bset)
6331 return -1;
6332 if (!empty)
6333 isl_die(ctx, isl_error_unknown,
6334 "basic set should be empty", return -1);
6336 return 0;
6339 /* Check that the equality in the set description below
6340 * is simplified away.
6342 static int test_simplify_2(isl_ctx *ctx)
6344 const char *str;
6345 isl_basic_set *bset;
6346 isl_bool universe;
6348 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
6349 bset = isl_basic_set_read_from_str(ctx, str);
6350 universe = isl_basic_set_plain_is_universe(bset);
6351 isl_basic_set_free(bset);
6353 if (universe < 0)
6354 return -1;
6355 if (!universe)
6356 isl_die(ctx, isl_error_unknown,
6357 "equality not simplified away", return -1);
6358 return 0;
6361 /* Some simplification tests.
6363 static int test_simplify(isl_ctx *ctx)
6365 if (test_simplify_1(ctx) < 0)
6366 return -1;
6367 if (test_simplify_2(ctx) < 0)
6368 return -1;
6369 return 0;
6372 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
6373 * with gbr context would fail to disable the use of the shifted tableau
6374 * when transferring equalities for the input to the context, resulting
6375 * in invalid sample values.
6377 static int test_partial_lexmin(isl_ctx *ctx)
6379 const char *str;
6380 isl_basic_set *bset;
6381 isl_basic_map *bmap;
6382 isl_map *map;
6384 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
6385 bmap = isl_basic_map_read_from_str(ctx, str);
6386 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
6387 bset = isl_basic_set_read_from_str(ctx, str);
6388 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
6389 isl_map_free(map);
6391 if (!map)
6392 return -1;
6394 return 0;
6397 /* Check that the variable compression performed on the existentially
6398 * quantified variables inside isl_basic_set_compute_divs is not confused
6399 * by the implicit equalities among the parameters.
6401 static int test_compute_divs(isl_ctx *ctx)
6403 const char *str;
6404 isl_basic_set *bset;
6405 isl_set *set;
6407 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
6408 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
6409 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
6410 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
6411 bset = isl_basic_set_read_from_str(ctx, str);
6412 set = isl_basic_set_compute_divs(bset);
6413 isl_set_free(set);
6414 if (!set)
6415 return -1;
6417 return 0;
6420 /* Check that the reaching domain elements and the prefix schedule
6421 * at a leaf node are the same before and after grouping.
6423 static int test_schedule_tree_group_1(isl_ctx *ctx)
6425 int equal;
6426 const char *str;
6427 isl_id *id;
6428 isl_union_set *uset;
6429 isl_multi_union_pw_aff *mupa;
6430 isl_union_pw_multi_aff *upma1, *upma2;
6431 isl_union_set *domain1, *domain2;
6432 isl_union_map *umap1, *umap2;
6433 isl_schedule_node *node;
6435 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
6436 uset = isl_union_set_read_from_str(ctx, str);
6437 node = isl_schedule_node_from_domain(uset);
6438 node = isl_schedule_node_child(node, 0);
6439 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
6440 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6441 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6442 node = isl_schedule_node_child(node, 0);
6443 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
6444 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6445 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6446 node = isl_schedule_node_child(node, 0);
6447 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
6448 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
6449 domain1 = isl_schedule_node_get_domain(node);
6450 id = isl_id_alloc(ctx, "group", NULL);
6451 node = isl_schedule_node_parent(node);
6452 node = isl_schedule_node_group(node, id);
6453 node = isl_schedule_node_child(node, 0);
6454 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
6455 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
6456 domain2 = isl_schedule_node_get_domain(node);
6457 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
6458 if (equal >= 0 && equal)
6459 equal = isl_union_set_is_equal(domain1, domain2);
6460 if (equal >= 0 && equal)
6461 equal = isl_union_map_is_equal(umap1, umap2);
6462 isl_union_map_free(umap1);
6463 isl_union_map_free(umap2);
6464 isl_union_set_free(domain1);
6465 isl_union_set_free(domain2);
6466 isl_union_pw_multi_aff_free(upma1);
6467 isl_union_pw_multi_aff_free(upma2);
6468 isl_schedule_node_free(node);
6470 if (equal < 0)
6471 return -1;
6472 if (!equal)
6473 isl_die(ctx, isl_error_unknown,
6474 "expressions not equal", return -1);
6476 return 0;
6479 /* Check that we can have nested groupings and that the union map
6480 * schedule representation is the same before and after the grouping.
6481 * Note that after the grouping, the union map representation contains
6482 * the domain constraints from the ranges of the expansion nodes,
6483 * while they are missing from the union map representation of
6484 * the tree without expansion nodes.
6486 * Also check that the global expansion is as expected.
6488 static int test_schedule_tree_group_2(isl_ctx *ctx)
6490 int equal, equal_expansion;
6491 const char *str;
6492 isl_id *id;
6493 isl_union_set *uset;
6494 isl_union_map *umap1, *umap2;
6495 isl_union_map *expansion1, *expansion2;
6496 isl_union_set_list *filters;
6497 isl_multi_union_pw_aff *mupa;
6498 isl_schedule *schedule;
6499 isl_schedule_node *node;
6501 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
6502 "S3[i,j] : 0 <= i,j < 10 }";
6503 uset = isl_union_set_read_from_str(ctx, str);
6504 node = isl_schedule_node_from_domain(uset);
6505 node = isl_schedule_node_child(node, 0);
6506 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
6507 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6508 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6509 node = isl_schedule_node_child(node, 0);
6510 str = "{ S1[i,j] }";
6511 uset = isl_union_set_read_from_str(ctx, str);
6512 filters = isl_union_set_list_from_union_set(uset);
6513 str = "{ S2[i,j]; S3[i,j] }";
6514 uset = isl_union_set_read_from_str(ctx, str);
6515 filters = isl_union_set_list_add(filters, uset);
6516 node = isl_schedule_node_insert_sequence(node, filters);
6517 node = isl_schedule_node_child(node, 1);
6518 node = isl_schedule_node_child(node, 0);
6519 str = "{ S2[i,j] }";
6520 uset = isl_union_set_read_from_str(ctx, str);
6521 filters = isl_union_set_list_from_union_set(uset);
6522 str = "{ S3[i,j] }";
6523 uset = isl_union_set_read_from_str(ctx, str);
6524 filters = isl_union_set_list_add(filters, uset);
6525 node = isl_schedule_node_insert_sequence(node, filters);
6527 schedule = isl_schedule_node_get_schedule(node);
6528 umap1 = isl_schedule_get_map(schedule);
6529 uset = isl_schedule_get_domain(schedule);
6530 umap1 = isl_union_map_intersect_domain(umap1, uset);
6531 isl_schedule_free(schedule);
6533 node = isl_schedule_node_parent(node);
6534 node = isl_schedule_node_parent(node);
6535 id = isl_id_alloc(ctx, "group1", NULL);
6536 node = isl_schedule_node_group(node, id);
6537 node = isl_schedule_node_child(node, 1);
6538 node = isl_schedule_node_child(node, 0);
6539 id = isl_id_alloc(ctx, "group2", NULL);
6540 node = isl_schedule_node_group(node, id);
6542 schedule = isl_schedule_node_get_schedule(node);
6543 umap2 = isl_schedule_get_map(schedule);
6544 isl_schedule_free(schedule);
6546 node = isl_schedule_node_root(node);
6547 node = isl_schedule_node_child(node, 0);
6548 expansion1 = isl_schedule_node_get_subtree_expansion(node);
6549 isl_schedule_node_free(node);
6551 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
6552 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
6553 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
6555 expansion2 = isl_union_map_read_from_str(ctx, str);
6557 equal = isl_union_map_is_equal(umap1, umap2);
6558 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
6560 isl_union_map_free(umap1);
6561 isl_union_map_free(umap2);
6562 isl_union_map_free(expansion1);
6563 isl_union_map_free(expansion2);
6565 if (equal < 0 || equal_expansion < 0)
6566 return -1;
6567 if (!equal)
6568 isl_die(ctx, isl_error_unknown,
6569 "expressions not equal", return -1);
6570 if (!equal_expansion)
6571 isl_die(ctx, isl_error_unknown,
6572 "unexpected expansion", return -1);
6574 return 0;
6577 /* Some tests for the isl_schedule_node_group function.
6579 static int test_schedule_tree_group(isl_ctx *ctx)
6581 if (test_schedule_tree_group_1(ctx) < 0)
6582 return -1;
6583 if (test_schedule_tree_group_2(ctx) < 0)
6584 return -1;
6585 return 0;
6588 struct {
6589 const char *set;
6590 const char *dual;
6591 } coef_tests[] = {
6592 { "{ rat: [i] : 0 <= i <= 10 }",
6593 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
6594 { "{ rat: [i] : FALSE }",
6595 "{ rat: coefficients[[cst] -> [a]] }" },
6596 { "{ rat: [i] : }",
6597 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
6600 struct {
6601 const char *set;
6602 const char *dual;
6603 } sol_tests[] = {
6604 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
6605 "{ rat: [i] : 0 <= i <= 10 }" },
6606 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
6607 "{ rat: [i] }" },
6608 { "{ rat: coefficients[[cst] -> [a]] }",
6609 "{ rat: [i] : FALSE }" },
6612 /* Test the basic functionality of isl_basic_set_coefficients and
6613 * isl_basic_set_solutions.
6615 static int test_dual(isl_ctx *ctx)
6617 int i;
6619 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
6620 int equal;
6621 isl_basic_set *bset1, *bset2;
6623 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
6624 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
6625 bset1 = isl_basic_set_coefficients(bset1);
6626 equal = isl_basic_set_is_equal(bset1, bset2);
6627 isl_basic_set_free(bset1);
6628 isl_basic_set_free(bset2);
6629 if (equal < 0)
6630 return -1;
6631 if (!equal)
6632 isl_die(ctx, isl_error_unknown,
6633 "incorrect dual", return -1);
6636 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
6637 int equal;
6638 isl_basic_set *bset1, *bset2;
6640 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
6641 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
6642 bset1 = isl_basic_set_solutions(bset1);
6643 equal = isl_basic_set_is_equal(bset1, bset2);
6644 isl_basic_set_free(bset1);
6645 isl_basic_set_free(bset2);
6646 if (equal < 0)
6647 return -1;
6648 if (!equal)
6649 isl_die(ctx, isl_error_unknown,
6650 "incorrect dual", return -1);
6653 return 0;
6656 struct {
6657 int scale_tile;
6658 int shift_point;
6659 const char *domain;
6660 const char *schedule;
6661 const char *sizes;
6662 const char *tile;
6663 const char *point;
6664 } tile_tests[] = {
6665 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6666 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6667 "{ [32,32] }",
6668 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6669 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6671 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6672 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6673 "{ [32,32] }",
6674 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6675 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6677 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6678 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6679 "{ [32,32] }",
6680 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6681 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6683 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6684 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6685 "{ [32,32] }",
6686 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6687 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6691 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
6692 * tile the band and then check if the tile and point bands have the
6693 * expected partial schedule.
6695 static int test_tile(isl_ctx *ctx)
6697 int i;
6698 int scale;
6699 int shift;
6701 scale = isl_options_get_tile_scale_tile_loops(ctx);
6702 shift = isl_options_get_tile_shift_point_loops(ctx);
6704 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
6705 int opt;
6706 int equal;
6707 const char *str;
6708 isl_union_set *domain;
6709 isl_multi_union_pw_aff *mupa, *mupa2;
6710 isl_schedule_node *node;
6711 isl_multi_val *sizes;
6713 opt = tile_tests[i].scale_tile;
6714 isl_options_set_tile_scale_tile_loops(ctx, opt);
6715 opt = tile_tests[i].shift_point;
6716 isl_options_set_tile_shift_point_loops(ctx, opt);
6718 str = tile_tests[i].domain;
6719 domain = isl_union_set_read_from_str(ctx, str);
6720 node = isl_schedule_node_from_domain(domain);
6721 node = isl_schedule_node_child(node, 0);
6722 str = tile_tests[i].schedule;
6723 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6724 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6725 str = tile_tests[i].sizes;
6726 sizes = isl_multi_val_read_from_str(ctx, str);
6727 node = isl_schedule_node_band_tile(node, sizes);
6729 str = tile_tests[i].tile;
6730 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6731 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6732 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
6733 isl_multi_union_pw_aff_free(mupa);
6734 isl_multi_union_pw_aff_free(mupa2);
6736 node = isl_schedule_node_child(node, 0);
6738 str = tile_tests[i].point;
6739 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6740 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6741 if (equal >= 0 && equal)
6742 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
6743 mupa2);
6744 isl_multi_union_pw_aff_free(mupa);
6745 isl_multi_union_pw_aff_free(mupa2);
6747 isl_schedule_node_free(node);
6749 if (equal < 0)
6750 return -1;
6751 if (!equal)
6752 isl_die(ctx, isl_error_unknown,
6753 "unexpected result", return -1);
6756 isl_options_set_tile_scale_tile_loops(ctx, scale);
6757 isl_options_set_tile_shift_point_loops(ctx, shift);
6759 return 0;
6762 /* Check that the domain hash of a space is equal to the hash
6763 * of the domain of the space.
6765 static int test_domain_hash(isl_ctx *ctx)
6767 isl_map *map;
6768 isl_space *space;
6769 uint32_t hash1, hash2;
6771 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
6772 space = isl_map_get_space(map);
6773 isl_map_free(map);
6774 hash1 = isl_space_get_domain_hash(space);
6775 space = isl_space_domain(space);
6776 hash2 = isl_space_get_hash(space);
6777 isl_space_free(space);
6779 if (!space)
6780 return -1;
6781 if (hash1 != hash2)
6782 isl_die(ctx, isl_error_unknown,
6783 "domain hash not equal to hash of domain", return -1);
6785 return 0;
6788 /* Check that a universe basic set that is not obviously equal to the universe
6789 * is still recognized as being equal to the universe.
6791 static int test_universe(isl_ctx *ctx)
6793 const char *s;
6794 isl_basic_set *bset;
6795 isl_bool is_univ;
6797 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
6798 bset = isl_basic_set_read_from_str(ctx, s);
6799 is_univ = isl_basic_set_is_universe(bset);
6800 isl_basic_set_free(bset);
6802 if (is_univ < 0)
6803 return -1;
6804 if (!is_univ)
6805 isl_die(ctx, isl_error_unknown,
6806 "not recognized as universe set", return -1);
6808 return 0;
6811 /* Sets for which chambers are computed and checked.
6813 const char *chambers_tests[] = {
6814 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
6815 "z >= 0 and z <= C - y and z <= B - x - y }",
6818 /* Add the domain of "cell" to "cells".
6820 static int add_cell(__isl_take isl_cell *cell, void *user)
6822 isl_basic_set_list **cells = user;
6823 isl_basic_set *dom;
6825 dom = isl_cell_get_domain(cell);
6826 isl_cell_free(cell);
6827 *cells = isl_basic_set_list_add(*cells, dom);
6829 return *cells ? 0 : -1;
6832 /* Check that the elements of "list" are pairwise disjoint.
6834 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
6836 int i, j, n;
6838 if (!list)
6839 return isl_stat_error;
6841 n = isl_basic_set_list_n_basic_set(list);
6842 for (i = 0; i < n; ++i) {
6843 isl_basic_set *bset_i;
6845 bset_i = isl_basic_set_list_get_basic_set(list, i);
6846 for (j = i + 1; j < n; ++j) {
6847 isl_basic_set *bset_j;
6848 isl_bool disjoint;
6850 bset_j = isl_basic_set_list_get_basic_set(list, j);
6851 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
6852 isl_basic_set_free(bset_j);
6853 if (!disjoint)
6854 isl_die(isl_basic_set_list_get_ctx(list),
6855 isl_error_unknown, "not disjoint",
6856 break);
6857 if (disjoint < 0 || !disjoint)
6858 break;
6860 isl_basic_set_free(bset_i);
6861 if (j < n)
6862 return isl_stat_error;
6865 return isl_stat_ok;
6868 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
6869 * are pairwise disjoint.
6871 static int test_chambers(isl_ctx *ctx)
6873 int i;
6875 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
6876 isl_basic_set *bset;
6877 isl_vertices *vertices;
6878 isl_basic_set_list *cells;
6879 isl_stat ok;
6881 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
6882 vertices = isl_basic_set_compute_vertices(bset);
6883 cells = isl_basic_set_list_alloc(ctx, 0);
6884 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
6885 &cells) < 0)
6886 cells = isl_basic_set_list_free(cells);
6887 ok = check_pairwise_disjoint(cells);
6888 isl_basic_set_list_free(cells);
6889 isl_vertices_free(vertices);
6890 isl_basic_set_free(bset);
6892 if (ok < 0)
6893 return -1;
6896 return 0;
6899 struct {
6900 const char *name;
6901 int (*fn)(isl_ctx *ctx);
6902 } tests [] = {
6903 { "universe", &test_universe },
6904 { "domain hash", &test_domain_hash },
6905 { "dual", &test_dual },
6906 { "dependence analysis", &test_flow },
6907 { "val", &test_val },
6908 { "compute divs", &test_compute_divs },
6909 { "partial lexmin", &test_partial_lexmin },
6910 { "simplify", &test_simplify },
6911 { "curry", &test_curry },
6912 { "piecewise multi affine expressions", &test_pw_multi_aff },
6913 { "multi piecewise affine expressions", &test_multi_pw_aff },
6914 { "conversion", &test_conversion },
6915 { "list", &test_list },
6916 { "align parameters", &test_align_parameters },
6917 { "preimage", &test_preimage },
6918 { "pullback", &test_pullback },
6919 { "AST", &test_ast },
6920 { "AST build", &test_ast_build },
6921 { "AST generation", &test_ast_gen },
6922 { "eliminate", &test_eliminate },
6923 { "residue class", &test_residue_class },
6924 { "div", &test_div },
6925 { "slice", &test_slice },
6926 { "fixed power", &test_fixed_power },
6927 { "sample", &test_sample },
6928 { "output", &test_output },
6929 { "vertices", &test_vertices },
6930 { "chambers", &test_chambers },
6931 { "fixed", &test_fixed },
6932 { "equal", &test_equal },
6933 { "disjoint", &test_disjoint },
6934 { "product", &test_product },
6935 { "dim_max", &test_dim_max },
6936 { "affine", &test_aff },
6937 { "injective", &test_injective },
6938 { "schedule (whole component)", &test_schedule_whole },
6939 { "schedule (incremental)", &test_schedule_incremental },
6940 { "schedule tree grouping", &test_schedule_tree_group },
6941 { "tile", &test_tile },
6942 { "union_pw", &test_union_pw },
6943 { "eval", &test_eval },
6944 { "parse", &test_parse },
6945 { "single-valued", &test_sv },
6946 { "affine hull", &test_affine_hull },
6947 { "simple_hull", &test_simple_hull },
6948 { "coalesce", &test_coalesce },
6949 { "factorize", &test_factorize },
6950 { "subset", &test_subset },
6951 { "subtract", &test_subtract },
6952 { "intersect", &test_intersect },
6953 { "lexmin", &test_lexmin },
6954 { "min", &test_min },
6955 { "gist", &test_gist },
6956 { "piecewise quasi-polynomials", &test_pwqp },
6957 { "lift", &test_lift },
6958 { "bound", &test_bound },
6959 { "union", &test_union },
6960 { "split periods", &test_split_periods },
6961 { "lexicographic order", &test_lex },
6962 { "bijectivity", &test_bijective },
6963 { "dataflow analysis", &test_dep },
6964 { "reading", &test_read },
6965 { "bounded", &test_bounded },
6966 { "construction", &test_construction },
6967 { "dimension manipulation", &test_dim },
6968 { "map application", &test_application },
6969 { "convex hull", &test_convex_hull },
6970 { "transitive closure", &test_closure },
6973 int main(int argc, char **argv)
6975 int i;
6976 struct isl_ctx *ctx;
6977 struct isl_options *options;
6979 options = isl_options_new_with_defaults();
6980 assert(options);
6981 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
6983 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
6984 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
6985 printf("%s\n", tests[i].name);
6986 if (tests[i].fn(ctx) < 0)
6987 goto error;
6989 isl_ctx_free(ctx);
6990 return 0;
6991 error:
6992 isl_ctx_free(ctx);
6993 return -1;