add isl_basic_set_variable_compression_with_id
[isl.git] / isl_test.c
blob4f5eae982c95692a2d796babf3de06041302ea0a
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 isl_bool 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 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
347 isl_int_set_si(v, 1);
348 c = 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 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
354 isl_int_set_si(v, -5);
355 c = 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 c = isl_constraint_set_constant(c, v);
689 isl_int_set_si(v, 1);
690 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
691 isl_int_set_si(v, 3);
692 c = 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 c = isl_constraint_set_constant(c, v);
698 isl_int_set_si(v, -1);
699 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
700 isl_int_set_si(v, 3);
701 c = 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 c = isl_constraint_set_constant(c, v);
718 isl_int_set_si(v, -1);
719 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
720 isl_int_set_si(v, 3);
721 c = 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 c = isl_constraint_set_constant(c, v);
727 isl_int_set_si(v, 1);
728 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
729 isl_int_set_si(v, 3);
730 c = 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 c = isl_constraint_set_constant(c, v);
747 isl_int_set_si(v, -1);
748 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
749 isl_int_set_si(v, 3);
750 c = 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 c = isl_constraint_set_constant(c, v);
756 isl_int_set_si(v, 1);
757 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
758 isl_int_set_si(v, 4);
759 c = 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 c = isl_constraint_set_constant(c, v);
776 isl_int_set_si(v, -1);
777 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
778 isl_int_set_si(v, 3);
779 c = 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 c = isl_constraint_set_constant(c, v);
785 isl_int_set_si(v, 1);
786 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
787 isl_int_set_si(v, 6);
788 c = 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 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
805 isl_int_set_si(v, 3);
806 c = 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 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
812 isl_int_set_si(v, -3);
813 c = 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 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
830 isl_int_set_si(v, 6);
831 c = 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 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
837 isl_int_set_si(v, -3);
838 c = 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 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
864 isl_int_set_si(v, -3);
865 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
866 isl_int_set_si(v, -3);
867 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
868 isl_int_set_si(v, 6);
869 c = 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 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
889 isl_int_set_si(v, -3);
890 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
891 isl_int_set_si(v, -3);
892 c = isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
893 isl_int_set_si(v, 6);
894 c = 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 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
900 isl_int_set_si(v, 1);
901 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
902 isl_int_set_si(v, 1);
903 c = 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 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
923 isl_int_set_si(v, -1);
924 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
925 isl_int_set_si(v, -2);
926 c = 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 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
932 isl_int_set_si(v, 3);
933 c = isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
934 isl_int_set_si(v, 2);
935 c = 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 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
955 isl_int_set_si(v, -2);
956 c = 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_internal(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_internal(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 /* Check that calling isl_set_coalesce does not leave other sets
1974 * that may share some information with the input to isl_set_coalesce
1975 * in an inconsistent state.
1976 * In particular, older versions of isl would modify all copies
1977 * of the basic sets in the isl_set_coalesce input in a way
1978 * that could leave them in an inconsistent state.
1979 * The result of printing any other set containing one of these
1980 * basic sets would then result in an invalid set description.
1982 static int test_coalesce_special3(isl_ctx *ctx)
1984 const char *str;
1985 char *s;
1986 isl_set *set1, *set2;
1987 isl_printer *p;
1989 set1 = isl_set_read_from_str(ctx, "{ [0, 0, 0] }");
1990 str = "{ [a, b, a + b] : a >= 0 and b >= 0 and 0 < a + b }";
1991 set2 = isl_set_read_from_str(ctx, str);
1992 set1 = isl_set_union(set1, isl_set_copy(set2));
1993 set1 = isl_set_coalesce(set1);
1994 isl_set_free(set1);
1996 p = isl_printer_to_str(ctx);
1997 p = isl_printer_print_set(p, set2);
1998 isl_set_free(set2);
1999 s = isl_printer_get_str(p);
2000 isl_printer_free(p);
2001 set1 = isl_set_read_from_str(ctx, s);
2002 free(s);
2003 isl_set_free(set1);
2005 if (!set1)
2006 return -1;
2008 return 0;
2011 /* Test the functionality of isl_set_coalesce.
2012 * That is, check that the output is always equal to the input
2013 * and in some cases that the result consists of a single disjunct.
2015 static int test_coalesce(struct isl_ctx *ctx)
2017 int i;
2019 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
2020 const char *str = coalesce_tests[i].str;
2021 int check_one = coalesce_tests[i].single_disjunct;
2022 if (test_coalesce_set(ctx, str, check_one) < 0)
2023 return -1;
2026 if (test_coalesce_unbounded_wrapping(ctx) < 0)
2027 return -1;
2028 if (test_coalesce_special(ctx) < 0)
2029 return -1;
2030 if (test_coalesce_special2(ctx) < 0)
2031 return -1;
2032 if (test_coalesce_special3(ctx) < 0)
2033 return -1;
2035 return 0;
2038 /* Construct a representation of the graph on the right of Figure 1
2039 * in "Computing the Transitive Closure of a Union of
2040 * Affine Integer Tuple Relations".
2042 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2044 isl_set *dom;
2045 isl_map *up, *right;
2047 dom = isl_set_read_from_str(ctx,
2048 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2049 "2 x - 3 y + 3 >= 0 }");
2050 right = isl_map_read_from_str(ctx,
2051 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2052 up = isl_map_read_from_str(ctx,
2053 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2054 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2055 right = isl_map_intersect_range(right, isl_set_copy(dom));
2056 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2057 up = isl_map_intersect_range(up, dom);
2058 return isl_map_union(up, right);
2061 /* Construct a representation of the power of the graph
2062 * on the right of Figure 1 in "Computing the Transitive Closure of
2063 * a Union of Affine Integer Tuple Relations".
2065 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2067 return isl_map_read_from_str(ctx,
2068 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2069 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2070 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2073 /* Construct a representation of the transitive closure of the graph
2074 * on the right of Figure 1 in "Computing the Transitive Closure of
2075 * a Union of Affine Integer Tuple Relations".
2077 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2079 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2082 static int test_closure(isl_ctx *ctx)
2084 const char *str;
2085 isl_map *map, *map2;
2086 int exact, equal;
2088 /* COCOA example 1 */
2089 map = isl_map_read_from_str(ctx,
2090 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2091 "1 <= i and i < n and 1 <= j and j < n or "
2092 "i2 = i + 1 and j2 = j - 1 and "
2093 "1 <= i and i < n and 2 <= j and j <= n }");
2094 map = isl_map_power(map, &exact);
2095 assert(exact);
2096 isl_map_free(map);
2098 /* COCOA example 1 */
2099 map = isl_map_read_from_str(ctx,
2100 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2101 "1 <= i and i < n and 1 <= j and j < n or "
2102 "i2 = i + 1 and j2 = j - 1 and "
2103 "1 <= i and i < n and 2 <= j and j <= n }");
2104 map = isl_map_transitive_closure(map, &exact);
2105 assert(exact);
2106 map2 = isl_map_read_from_str(ctx,
2107 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2108 "1 <= i and i < n and 1 <= j and j <= n and "
2109 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2110 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2111 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2112 assert(isl_map_is_equal(map, map2));
2113 isl_map_free(map2);
2114 isl_map_free(map);
2116 map = isl_map_read_from_str(ctx,
2117 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2118 " 0 <= y and y <= n }");
2119 map = isl_map_transitive_closure(map, &exact);
2120 map2 = isl_map_read_from_str(ctx,
2121 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2122 " 0 <= y and y <= n }");
2123 assert(isl_map_is_equal(map, map2));
2124 isl_map_free(map2);
2125 isl_map_free(map);
2127 /* COCOA example 2 */
2128 map = isl_map_read_from_str(ctx,
2129 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2130 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2131 "i2 = i + 2 and j2 = j - 2 and "
2132 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2133 map = isl_map_transitive_closure(map, &exact);
2134 assert(exact);
2135 map2 = isl_map_read_from_str(ctx,
2136 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2137 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2138 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2139 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2140 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2141 assert(isl_map_is_equal(map, map2));
2142 isl_map_free(map);
2143 isl_map_free(map2);
2145 /* COCOA Fig.2 left */
2146 map = isl_map_read_from_str(ctx,
2147 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2148 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2149 "j <= n or "
2150 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2151 "j <= 2 i - 3 and j <= n - 2 or "
2152 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2153 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2154 map = isl_map_transitive_closure(map, &exact);
2155 assert(exact);
2156 isl_map_free(map);
2158 /* COCOA Fig.2 right */
2159 map = isl_map_read_from_str(ctx,
2160 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2161 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2162 "j <= n or "
2163 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2164 "j <= 2 i - 4 and j <= n - 3 or "
2165 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2166 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2167 map = isl_map_power(map, &exact);
2168 assert(exact);
2169 isl_map_free(map);
2171 /* COCOA Fig.2 right */
2172 map = isl_map_read_from_str(ctx,
2173 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2174 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2175 "j <= n or "
2176 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2177 "j <= 2 i - 4 and j <= n - 3 or "
2178 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2179 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2180 map = isl_map_transitive_closure(map, &exact);
2181 assert(exact);
2182 map2 = isl_map_read_from_str(ctx,
2183 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2184 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2185 "j <= n and 3 + i + 2 j <= 3 n and "
2186 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2187 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2188 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2189 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2190 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2191 assert(isl_map_is_equal(map, map2));
2192 isl_map_free(map2);
2193 isl_map_free(map);
2195 map = cocoa_fig_1_right_graph(ctx);
2196 map = isl_map_transitive_closure(map, &exact);
2197 assert(exact);
2198 map2 = cocoa_fig_1_right_tc(ctx);
2199 assert(isl_map_is_equal(map, map2));
2200 isl_map_free(map2);
2201 isl_map_free(map);
2203 map = cocoa_fig_1_right_graph(ctx);
2204 map = isl_map_power(map, &exact);
2205 map2 = cocoa_fig_1_right_power(ctx);
2206 equal = isl_map_is_equal(map, map2);
2207 isl_map_free(map2);
2208 isl_map_free(map);
2209 if (equal < 0)
2210 return -1;
2211 if (!exact)
2212 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2213 if (!equal)
2214 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2216 /* COCOA Theorem 1 counter example */
2217 map = isl_map_read_from_str(ctx,
2218 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2219 "i2 = 1 and j2 = j or "
2220 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2221 map = isl_map_transitive_closure(map, &exact);
2222 assert(exact);
2223 isl_map_free(map);
2225 map = isl_map_read_from_str(ctx,
2226 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2227 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2228 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2229 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2230 map = isl_map_transitive_closure(map, &exact);
2231 assert(exact);
2232 isl_map_free(map);
2234 /* Kelly et al 1996, fig 12 */
2235 map = isl_map_read_from_str(ctx,
2236 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2237 "1 <= i,j,j+1 <= n or "
2238 "j = n and j2 = 1 and i2 = i + 1 and "
2239 "1 <= i,i+1 <= n }");
2240 map = isl_map_transitive_closure(map, &exact);
2241 assert(exact);
2242 map2 = isl_map_read_from_str(ctx,
2243 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2244 "1 <= i <= n and i = i2 or "
2245 "1 <= i < i2 <= n and 1 <= j <= n and "
2246 "1 <= j2 <= n }");
2247 assert(isl_map_is_equal(map, map2));
2248 isl_map_free(map2);
2249 isl_map_free(map);
2251 /* Omega's closure4 */
2252 map = isl_map_read_from_str(ctx,
2253 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2254 "1 <= x,y <= 10 or "
2255 "x2 = x + 1 and y2 = y and "
2256 "1 <= x <= 20 && 5 <= y <= 15 }");
2257 map = isl_map_transitive_closure(map, &exact);
2258 assert(exact);
2259 isl_map_free(map);
2261 map = isl_map_read_from_str(ctx,
2262 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2263 map = isl_map_transitive_closure(map, &exact);
2264 assert(!exact);
2265 map2 = isl_map_read_from_str(ctx,
2266 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2267 assert(isl_map_is_equal(map, map2));
2268 isl_map_free(map);
2269 isl_map_free(map2);
2271 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2272 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2273 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2274 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2275 map = isl_map_read_from_str(ctx, str);
2276 map = isl_map_transitive_closure(map, &exact);
2277 assert(exact);
2278 map2 = isl_map_read_from_str(ctx, str);
2279 assert(isl_map_is_equal(map, map2));
2280 isl_map_free(map);
2281 isl_map_free(map2);
2283 str = "{[0] -> [1]; [2] -> [3]}";
2284 map = isl_map_read_from_str(ctx, str);
2285 map = isl_map_transitive_closure(map, &exact);
2286 assert(exact);
2287 map2 = isl_map_read_from_str(ctx, str);
2288 assert(isl_map_is_equal(map, map2));
2289 isl_map_free(map);
2290 isl_map_free(map2);
2292 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2293 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2294 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2295 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2296 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2297 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2298 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2299 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2300 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2301 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2302 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2303 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2304 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2305 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2306 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2307 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2308 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2309 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2310 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2311 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2312 map = isl_map_read_from_str(ctx, str);
2313 map = isl_map_transitive_closure(map, NULL);
2314 assert(map);
2315 isl_map_free(map);
2317 return 0;
2320 static int test_lex(struct isl_ctx *ctx)
2322 isl_space *dim;
2323 isl_map *map;
2324 int empty;
2326 dim = isl_space_set_alloc(ctx, 0, 0);
2327 map = isl_map_lex_le(dim);
2328 empty = isl_map_is_empty(map);
2329 isl_map_free(map);
2331 if (empty < 0)
2332 return -1;
2333 if (empty)
2334 isl_die(ctx, isl_error_unknown,
2335 "expecting non-empty result", return -1);
2337 return 0;
2340 /* Inputs for isl_map_lexmin tests.
2341 * "map" is the input and "lexmin" is the expected result.
2343 struct {
2344 const char *map;
2345 const char *lexmin;
2346 } lexmin_tests [] = {
2347 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
2348 "{ [x] -> [5] : 6 <= x <= 8; "
2349 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
2350 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
2351 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
2352 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
2353 "{ [x] -> [y] : (4y = x and x >= 0) or "
2354 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2355 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2356 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
2357 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
2358 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
2359 /* Check that empty pieces are properly combined. */
2360 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2361 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2362 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
2363 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2364 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2365 "x >= 4 }" },
2366 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
2367 "a <= 255 and c <= 255 and d <= 255 - j and "
2368 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
2369 "247d <= 247 + k - j and 247d <= 247 + k - b and "
2370 "247d <= 247 + i and 248 - b <= 248d <= c and "
2371 "254d >= i - a + b and 254d >= -a + b and "
2372 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
2373 "{ [i, k, j] -> "
2374 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
2375 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
2376 "247j >= 62738 - i and 509j <= 129795 + i and "
2377 "742j >= 188724 - i; "
2378 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
2379 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
2380 "16*floor((8 + b)/16) <= 7 + b; "
2381 "[a] -> [1] }",
2382 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
2383 "[a] -> [b = 0] : 0 < a <= 509 }" },
2384 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
2385 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
2388 static int test_lexmin(struct isl_ctx *ctx)
2390 int i;
2391 int equal;
2392 const char *str;
2393 isl_basic_map *bmap;
2394 isl_map *map, *map2;
2395 isl_set *set;
2396 isl_set *set2;
2397 isl_pw_multi_aff *pma;
2399 str = "[p0, p1] -> { [] -> [] : "
2400 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2401 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2402 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2403 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2404 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2405 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2406 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2407 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2408 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2409 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2410 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2411 map = isl_map_read_from_str(ctx, str);
2412 map = isl_map_lexmin(map);
2413 isl_map_free(map);
2415 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2416 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2417 set = isl_set_read_from_str(ctx, str);
2418 set = isl_set_lexmax(set);
2419 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2420 set2 = isl_set_read_from_str(ctx, str);
2421 set = isl_set_intersect(set, set2);
2422 assert(!isl_set_is_empty(set));
2423 isl_set_free(set);
2425 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
2426 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
2427 map = isl_map_lexmin(map);
2428 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
2429 equal = isl_map_is_equal(map, map2);
2430 isl_map_free(map);
2431 isl_map_free(map2);
2433 if (equal < 0)
2434 return -1;
2435 if (!equal)
2436 isl_die(ctx, isl_error_unknown,
2437 "unexpected result", return -1);
2440 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2441 " 8i' <= i and 8i' >= -7 + i }";
2442 bmap = isl_basic_map_read_from_str(ctx, str);
2443 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2444 map2 = isl_map_from_pw_multi_aff(pma);
2445 map = isl_map_from_basic_map(bmap);
2446 assert(isl_map_is_equal(map, map2));
2447 isl_map_free(map);
2448 isl_map_free(map2);
2450 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2451 " 8i' <= i and 8i' >= -7 + i }";
2452 set = isl_set_read_from_str(ctx, str);
2453 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2454 set2 = isl_set_from_pw_multi_aff(pma);
2455 equal = isl_set_is_equal(set, set2);
2456 isl_set_free(set);
2457 isl_set_free(set2);
2458 if (equal < 0)
2459 return -1;
2460 if (!equal)
2461 isl_die(ctx, isl_error_unknown,
2462 "unexpected difference between set and "
2463 "piecewise affine expression", return -1);
2465 return 0;
2468 /* A specialized isl_set_min_val test case that would return the wrong result
2469 * in earlier versions of isl.
2470 * The explicit call to isl_basic_set_union prevents the second basic set
2471 * from being determined to be empty prior to the call to isl_set_min_val,
2472 * at least at the point where this test case was introduced.
2474 static int test_min_special(isl_ctx *ctx)
2476 const char *str;
2477 isl_basic_set *bset1, *bset2;
2478 isl_set *set;
2479 isl_aff *obj;
2480 isl_val *res;
2481 int ok;
2483 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
2484 bset1 = isl_basic_set_read_from_str(ctx, str);
2485 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
2486 bset2 = isl_basic_set_read_from_str(ctx, str);
2487 set = isl_basic_set_union(bset1, bset2);
2488 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
2490 res = isl_set_min_val(set, obj);
2491 ok = isl_val_cmp_si(res, 5) == 0;
2493 isl_aff_free(obj);
2494 isl_set_free(set);
2495 isl_val_free(res);
2497 if (!res)
2498 return -1;
2499 if (!ok)
2500 isl_die(ctx, isl_error_unknown, "unexpected minimum",
2501 return -1);
2503 return 0;
2506 /* A specialized isl_set_min_val test case that would return an error
2507 * in earlier versions of isl.
2509 static int test_min_special2(isl_ctx *ctx)
2511 const char *str;
2512 isl_basic_set *bset;
2513 isl_aff *obj;
2514 isl_val *res;
2516 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
2517 bset = isl_basic_set_read_from_str(ctx, str);
2519 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
2521 res = isl_basic_set_max_val(bset, obj);
2523 isl_basic_set_free(bset);
2524 isl_aff_free(obj);
2525 isl_val_free(res);
2527 if (!res)
2528 return -1;
2530 return 0;
2533 struct {
2534 const char *set;
2535 const char *obj;
2536 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2537 __isl_keep isl_aff *obj);
2538 const char *res;
2539 } opt_tests[] = {
2540 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2541 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2542 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2543 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2544 &isl_set_max_val, "30" },
2548 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2549 * In particular, check the results on non-convex inputs.
2551 static int test_min(struct isl_ctx *ctx)
2553 int i;
2554 isl_set *set;
2555 isl_aff *obj;
2556 isl_val *val, *res;
2557 isl_bool ok;
2559 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2560 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2561 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2562 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2563 val = opt_tests[i].fn(set, obj);
2564 ok = isl_val_eq(res, val);
2565 isl_val_free(res);
2566 isl_val_free(val);
2567 isl_aff_free(obj);
2568 isl_set_free(set);
2570 if (ok < 0)
2571 return -1;
2572 if (!ok)
2573 isl_die(ctx, isl_error_unknown,
2574 "unexpected optimum", return -1);
2577 if (test_min_special(ctx) < 0)
2578 return -1;
2579 if (test_min_special2(ctx) < 0)
2580 return -1;
2582 return 0;
2585 struct must_may {
2586 isl_map *must;
2587 isl_map *may;
2590 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2591 void *dep_user, void *user)
2593 struct must_may *mm = (struct must_may *)user;
2595 if (must)
2596 mm->must = isl_map_union(mm->must, dep);
2597 else
2598 mm->may = isl_map_union(mm->may, dep);
2600 return isl_stat_ok;
2603 static int common_space(void *first, void *second)
2605 int depth = *(int *)first;
2606 return 2 * depth;
2609 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2611 isl_map *map2;
2612 int equal;
2614 if (!map)
2615 return -1;
2617 map2 = isl_map_read_from_str(map->ctx, str);
2618 equal = isl_map_is_equal(map, map2);
2619 isl_map_free(map2);
2621 return equal;
2624 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2626 int equal;
2628 equal = map_is_equal(map, str);
2629 if (equal < 0)
2630 return -1;
2631 if (!equal)
2632 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2633 "result not as expected", return -1);
2634 return 0;
2637 static int test_dep(struct isl_ctx *ctx)
2639 const char *str;
2640 isl_space *dim;
2641 isl_map *map;
2642 isl_access_info *ai;
2643 isl_flow *flow;
2644 int depth;
2645 struct must_may mm;
2647 depth = 3;
2649 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2650 map = isl_map_read_from_str(ctx, str);
2651 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2653 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2654 map = isl_map_read_from_str(ctx, str);
2655 ai = isl_access_info_add_source(ai, map, 1, &depth);
2657 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2658 map = isl_map_read_from_str(ctx, str);
2659 ai = isl_access_info_add_source(ai, map, 1, &depth);
2661 flow = isl_access_info_compute_flow(ai);
2662 dim = isl_space_alloc(ctx, 0, 3, 3);
2663 mm.must = isl_map_empty(isl_space_copy(dim));
2664 mm.may = isl_map_empty(dim);
2666 isl_flow_foreach(flow, collect_must_may, &mm);
2668 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2669 " [1,10,0] -> [2,5,0] }";
2670 assert(map_is_equal(mm.must, str));
2671 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2672 assert(map_is_equal(mm.may, str));
2674 isl_map_free(mm.must);
2675 isl_map_free(mm.may);
2676 isl_flow_free(flow);
2679 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2680 map = isl_map_read_from_str(ctx, str);
2681 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2683 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2684 map = isl_map_read_from_str(ctx, str);
2685 ai = isl_access_info_add_source(ai, map, 1, &depth);
2687 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2688 map = isl_map_read_from_str(ctx, str);
2689 ai = isl_access_info_add_source(ai, map, 0, &depth);
2691 flow = isl_access_info_compute_flow(ai);
2692 dim = isl_space_alloc(ctx, 0, 3, 3);
2693 mm.must = isl_map_empty(isl_space_copy(dim));
2694 mm.may = isl_map_empty(dim);
2696 isl_flow_foreach(flow, collect_must_may, &mm);
2698 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2699 assert(map_is_equal(mm.must, str));
2700 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2701 assert(map_is_equal(mm.may, str));
2703 isl_map_free(mm.must);
2704 isl_map_free(mm.may);
2705 isl_flow_free(flow);
2708 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2709 map = isl_map_read_from_str(ctx, str);
2710 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2712 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2713 map = isl_map_read_from_str(ctx, str);
2714 ai = isl_access_info_add_source(ai, map, 0, &depth);
2716 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2717 map = isl_map_read_from_str(ctx, str);
2718 ai = isl_access_info_add_source(ai, map, 0, &depth);
2720 flow = isl_access_info_compute_flow(ai);
2721 dim = isl_space_alloc(ctx, 0, 3, 3);
2722 mm.must = isl_map_empty(isl_space_copy(dim));
2723 mm.may = isl_map_empty(dim);
2725 isl_flow_foreach(flow, collect_must_may, &mm);
2727 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2728 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2729 assert(map_is_equal(mm.may, str));
2730 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2731 assert(map_is_equal(mm.must, str));
2733 isl_map_free(mm.must);
2734 isl_map_free(mm.may);
2735 isl_flow_free(flow);
2738 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2739 map = isl_map_read_from_str(ctx, str);
2740 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2742 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2743 map = isl_map_read_from_str(ctx, str);
2744 ai = isl_access_info_add_source(ai, map, 0, &depth);
2746 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2747 map = isl_map_read_from_str(ctx, str);
2748 ai = isl_access_info_add_source(ai, map, 0, &depth);
2750 flow = isl_access_info_compute_flow(ai);
2751 dim = isl_space_alloc(ctx, 0, 3, 3);
2752 mm.must = isl_map_empty(isl_space_copy(dim));
2753 mm.may = isl_map_empty(dim);
2755 isl_flow_foreach(flow, collect_must_may, &mm);
2757 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2758 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2759 assert(map_is_equal(mm.may, str));
2760 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2761 assert(map_is_equal(mm.must, str));
2763 isl_map_free(mm.must);
2764 isl_map_free(mm.may);
2765 isl_flow_free(flow);
2768 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2769 map = isl_map_read_from_str(ctx, str);
2770 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2772 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2773 map = isl_map_read_from_str(ctx, str);
2774 ai = isl_access_info_add_source(ai, map, 0, &depth);
2776 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2777 map = isl_map_read_from_str(ctx, str);
2778 ai = isl_access_info_add_source(ai, map, 0, &depth);
2780 flow = isl_access_info_compute_flow(ai);
2781 dim = isl_space_alloc(ctx, 0, 3, 3);
2782 mm.must = isl_map_empty(isl_space_copy(dim));
2783 mm.may = isl_map_empty(dim);
2785 isl_flow_foreach(flow, collect_must_may, &mm);
2787 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2788 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2789 assert(map_is_equal(mm.may, str));
2790 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2791 assert(map_is_equal(mm.must, str));
2793 isl_map_free(mm.must);
2794 isl_map_free(mm.may);
2795 isl_flow_free(flow);
2798 depth = 5;
2800 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2801 map = isl_map_read_from_str(ctx, str);
2802 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2804 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2805 map = isl_map_read_from_str(ctx, str);
2806 ai = isl_access_info_add_source(ai, map, 1, &depth);
2808 flow = isl_access_info_compute_flow(ai);
2809 dim = isl_space_alloc(ctx, 0, 5, 5);
2810 mm.must = isl_map_empty(isl_space_copy(dim));
2811 mm.may = isl_map_empty(dim);
2813 isl_flow_foreach(flow, collect_must_may, &mm);
2815 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2816 assert(map_is_equal(mm.must, str));
2817 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2818 assert(map_is_equal(mm.may, str));
2820 isl_map_free(mm.must);
2821 isl_map_free(mm.may);
2822 isl_flow_free(flow);
2824 return 0;
2827 /* Check that the dependence analysis proceeds without errors.
2828 * Earlier versions of isl would break down during the analysis
2829 * due to the use of the wrong spaces.
2831 static int test_flow(isl_ctx *ctx)
2833 const char *str;
2834 isl_union_map *access, *schedule;
2835 isl_union_map *must_dep, *may_dep;
2836 int r;
2838 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2839 access = isl_union_map_read_from_str(ctx, str);
2840 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2841 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2842 "S2[] -> [1,0,0,0]; "
2843 "S3[] -> [-1,0,0,0] }";
2844 schedule = isl_union_map_read_from_str(ctx, str);
2845 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2846 isl_union_map_copy(access), schedule,
2847 &must_dep, &may_dep, NULL, NULL);
2848 isl_union_map_free(may_dep);
2849 isl_union_map_free(must_dep);
2851 return r;
2854 struct {
2855 const char *map;
2856 int sv;
2857 } sv_tests[] = {
2858 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2859 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2860 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2861 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2862 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2863 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2864 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2865 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2866 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2869 int test_sv(isl_ctx *ctx)
2871 isl_union_map *umap;
2872 int i;
2873 int sv;
2875 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2876 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2877 sv = isl_union_map_is_single_valued(umap);
2878 isl_union_map_free(umap);
2879 if (sv < 0)
2880 return -1;
2881 if (sv_tests[i].sv && !sv)
2882 isl_die(ctx, isl_error_internal,
2883 "map not detected as single valued", return -1);
2884 if (!sv_tests[i].sv && sv)
2885 isl_die(ctx, isl_error_internal,
2886 "map detected as single valued", return -1);
2889 return 0;
2892 struct {
2893 const char *str;
2894 int bijective;
2895 } bijective_tests[] = {
2896 { "[N,M]->{[i,j] -> [i]}", 0 },
2897 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
2898 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
2899 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
2900 { "[N,M]->{[i,j] -> [j,i]}", 1 },
2901 { "[N,M]->{[i,j] -> [i+j]}", 0 },
2902 { "[N,M]->{[i,j] -> []}", 0 },
2903 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
2904 { "[N,M]->{[i,j] -> [2i]}", 0 },
2905 { "[N,M]->{[i,j] -> [i,i]}", 0 },
2906 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
2907 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
2908 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
2911 static int test_bijective(struct isl_ctx *ctx)
2913 isl_map *map;
2914 int i;
2915 int bijective;
2917 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
2918 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
2919 bijective = isl_map_is_bijective(map);
2920 isl_map_free(map);
2921 if (bijective < 0)
2922 return -1;
2923 if (bijective_tests[i].bijective && !bijective)
2924 isl_die(ctx, isl_error_internal,
2925 "map not detected as bijective", return -1);
2926 if (!bijective_tests[i].bijective && bijective)
2927 isl_die(ctx, isl_error_internal,
2928 "map detected as bijective", return -1);
2931 return 0;
2934 /* Inputs for isl_pw_qpolynomial_gist tests.
2935 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2937 struct {
2938 const char *pwqp;
2939 const char *set;
2940 const char *gist;
2941 } pwqp_gist_tests[] = {
2942 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2943 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2944 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2945 "{ [i] -> -1/2 + 1/2 * i }" },
2946 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2949 static int test_pwqp(struct isl_ctx *ctx)
2951 int i;
2952 const char *str;
2953 isl_set *set;
2954 isl_pw_qpolynomial *pwqp1, *pwqp2;
2955 int equal;
2957 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2958 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2960 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2961 isl_dim_in, 1, 1);
2963 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2964 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2966 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2968 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2970 isl_pw_qpolynomial_free(pwqp1);
2972 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2973 str = pwqp_gist_tests[i].pwqp;
2974 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2975 str = pwqp_gist_tests[i].set;
2976 set = isl_set_read_from_str(ctx, str);
2977 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2978 str = pwqp_gist_tests[i].gist;
2979 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2980 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2981 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2982 isl_pw_qpolynomial_free(pwqp1);
2984 if (equal < 0)
2985 return -1;
2986 if (!equal)
2987 isl_die(ctx, isl_error_unknown,
2988 "unexpected result", return -1);
2991 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2992 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2993 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2994 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2996 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2998 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3000 isl_pw_qpolynomial_free(pwqp1);
3002 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
3003 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3004 str = "{ [x] -> x }";
3005 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3007 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3009 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3011 isl_pw_qpolynomial_free(pwqp1);
3013 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3014 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3015 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3016 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3017 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3018 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3019 isl_pw_qpolynomial_free(pwqp1);
3021 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3022 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3023 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3024 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3025 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3026 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3027 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3028 isl_pw_qpolynomial_free(pwqp1);
3029 isl_pw_qpolynomial_free(pwqp2);
3030 if (equal < 0)
3031 return -1;
3032 if (!equal)
3033 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3035 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3036 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3037 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3038 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3039 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3040 isl_val_one(ctx));
3041 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3042 isl_pw_qpolynomial_free(pwqp1);
3043 isl_pw_qpolynomial_free(pwqp2);
3044 if (equal < 0)
3045 return -1;
3046 if (!equal)
3047 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3049 return 0;
3052 static int test_split_periods(isl_ctx *ctx)
3054 const char *str;
3055 isl_pw_qpolynomial *pwqp;
3057 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3058 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3059 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3060 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3062 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3064 isl_pw_qpolynomial_free(pwqp);
3066 if (!pwqp)
3067 return -1;
3069 return 0;
3072 static int test_union(isl_ctx *ctx)
3074 const char *str;
3075 isl_union_set *uset1, *uset2;
3076 isl_union_map *umap1, *umap2;
3077 int equal;
3079 str = "{ [i] : 0 <= i <= 1 }";
3080 uset1 = isl_union_set_read_from_str(ctx, str);
3081 str = "{ [1] -> [0] }";
3082 umap1 = isl_union_map_read_from_str(ctx, str);
3084 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3085 equal = isl_union_map_is_equal(umap1, umap2);
3087 isl_union_map_free(umap1);
3088 isl_union_map_free(umap2);
3090 if (equal < 0)
3091 return -1;
3092 if (!equal)
3093 isl_die(ctx, isl_error_unknown, "union maps not equal",
3094 return -1);
3096 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3097 umap1 = isl_union_map_read_from_str(ctx, str);
3098 str = "{ A[i]; B[i] }";
3099 uset1 = isl_union_set_read_from_str(ctx, str);
3101 uset2 = isl_union_map_domain(umap1);
3103 equal = isl_union_set_is_equal(uset1, uset2);
3105 isl_union_set_free(uset1);
3106 isl_union_set_free(uset2);
3108 if (equal < 0)
3109 return -1;
3110 if (!equal)
3111 isl_die(ctx, isl_error_unknown, "union sets not equal",
3112 return -1);
3114 return 0;
3117 /* Check that computing a bound of a non-zero polynomial over an unbounded
3118 * domain does not produce a rational value.
3119 * In particular, check that the upper bound is infinity.
3121 static int test_bound_unbounded_domain(isl_ctx *ctx)
3123 const char *str;
3124 isl_pw_qpolynomial *pwqp;
3125 isl_pw_qpolynomial_fold *pwf, *pwf2;
3126 isl_bool equal;
3128 str = "{ [m,n] -> -m * n }";
3129 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3130 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3131 str = "{ infty }";
3132 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3133 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3134 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
3135 isl_pw_qpolynomial_fold_free(pwf);
3136 isl_pw_qpolynomial_fold_free(pwf2);
3138 if (equal < 0)
3139 return -1;
3140 if (!equal)
3141 isl_die(ctx, isl_error_unknown,
3142 "expecting infinite polynomial bound", return -1);
3144 return 0;
3147 static int test_bound(isl_ctx *ctx)
3149 const char *str;
3150 unsigned dim;
3151 isl_pw_qpolynomial *pwqp;
3152 isl_pw_qpolynomial_fold *pwf;
3154 if (test_bound_unbounded_domain(ctx) < 0)
3155 return -1;
3157 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
3158 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3159 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3160 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3161 isl_pw_qpolynomial_fold_free(pwf);
3162 if (dim != 4)
3163 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3164 return -1);
3166 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
3167 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3168 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3169 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3170 isl_pw_qpolynomial_fold_free(pwf);
3171 if (dim != 1)
3172 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3173 return -1);
3175 return 0;
3178 static int test_lift(isl_ctx *ctx)
3180 const char *str;
3181 isl_basic_map *bmap;
3182 isl_basic_set *bset;
3184 str = "{ [i0] : exists e0 : i0 = 4e0 }";
3185 bset = isl_basic_set_read_from_str(ctx, str);
3186 bset = isl_basic_set_lift(bset);
3187 bmap = isl_basic_map_from_range(bset);
3188 bset = isl_basic_map_domain(bmap);
3189 isl_basic_set_free(bset);
3191 return 0;
3194 struct {
3195 const char *set1;
3196 const char *set2;
3197 int subset;
3198 } subset_tests[] = {
3199 { "{ [112, 0] }",
3200 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
3201 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
3202 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
3203 { "{ [65] }",
3204 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
3205 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
3206 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
3207 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
3208 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
3209 "256e0 <= 255i and 256e0 >= -255 + 255i and "
3210 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
3211 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
3212 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
3213 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
3214 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
3215 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
3216 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
3217 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
3218 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
3219 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
3220 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
3221 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
3222 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
3223 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
3224 "4e0 <= -57 + i0 + i1)) or "
3225 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
3226 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
3227 "4e0 >= -61 + i0 + i1)) or "
3228 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
3229 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
3232 static int test_subset(isl_ctx *ctx)
3234 int i;
3235 isl_set *set1, *set2;
3236 int subset;
3238 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3239 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3240 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3241 subset = isl_set_is_subset(set1, set2);
3242 isl_set_free(set1);
3243 isl_set_free(set2);
3244 if (subset < 0)
3245 return -1;
3246 if (subset != subset_tests[i].subset)
3247 isl_die(ctx, isl_error_unknown,
3248 "incorrect subset result", return -1);
3251 return 0;
3254 struct {
3255 const char *minuend;
3256 const char *subtrahend;
3257 const char *difference;
3258 } subtract_domain_tests[] = {
3259 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3260 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3261 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3264 static int test_subtract(isl_ctx *ctx)
3266 int i;
3267 isl_union_map *umap1, *umap2;
3268 isl_union_pw_multi_aff *upma1, *upma2;
3269 isl_union_set *uset;
3270 int equal;
3272 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3273 umap1 = isl_union_map_read_from_str(ctx,
3274 subtract_domain_tests[i].minuend);
3275 uset = isl_union_set_read_from_str(ctx,
3276 subtract_domain_tests[i].subtrahend);
3277 umap2 = isl_union_map_read_from_str(ctx,
3278 subtract_domain_tests[i].difference);
3279 umap1 = isl_union_map_subtract_domain(umap1, uset);
3280 equal = isl_union_map_is_equal(umap1, umap2);
3281 isl_union_map_free(umap1);
3282 isl_union_map_free(umap2);
3283 if (equal < 0)
3284 return -1;
3285 if (!equal)
3286 isl_die(ctx, isl_error_unknown,
3287 "incorrect subtract domain result", return -1);
3290 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3291 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3292 subtract_domain_tests[i].minuend);
3293 uset = isl_union_set_read_from_str(ctx,
3294 subtract_domain_tests[i].subtrahend);
3295 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3296 subtract_domain_tests[i].difference);
3297 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3298 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3299 isl_union_pw_multi_aff_free(upma1);
3300 isl_union_pw_multi_aff_free(upma2);
3301 if (equal < 0)
3302 return -1;
3303 if (!equal)
3304 isl_die(ctx, isl_error_unknown,
3305 "incorrect subtract domain result", return -1);
3308 return 0;
3311 /* Check that intersecting the empty basic set with another basic set
3312 * does not increase the number of constraints. In particular,
3313 * the empty basic set should maintain its canonical representation.
3315 static int test_intersect(isl_ctx *ctx)
3317 int n1, n2;
3318 isl_basic_set *bset1, *bset2;
3320 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3321 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3322 n1 = isl_basic_set_n_constraint(bset1);
3323 bset1 = isl_basic_set_intersect(bset1, bset2);
3324 n2 = isl_basic_set_n_constraint(bset1);
3325 isl_basic_set_free(bset1);
3326 if (!bset1)
3327 return -1;
3328 if (n1 != n2)
3329 isl_die(ctx, isl_error_unknown,
3330 "number of constraints of empty set changed",
3331 return -1);
3333 return 0;
3336 int test_factorize(isl_ctx *ctx)
3338 const char *str;
3339 isl_basic_set *bset;
3340 isl_factorizer *f;
3342 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3343 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3344 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3345 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3346 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3347 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3348 "3i5 >= -2i0 - i2 + 3i4 }";
3349 bset = isl_basic_set_read_from_str(ctx, str);
3350 f = isl_basic_set_factorizer(bset);
3351 isl_basic_set_free(bset);
3352 isl_factorizer_free(f);
3353 if (!f)
3354 isl_die(ctx, isl_error_unknown,
3355 "failed to construct factorizer", return -1);
3357 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3358 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3359 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3360 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3361 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3362 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3363 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3364 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3365 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3366 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3367 bset = isl_basic_set_read_from_str(ctx, str);
3368 f = isl_basic_set_factorizer(bset);
3369 isl_basic_set_free(bset);
3370 isl_factorizer_free(f);
3371 if (!f)
3372 isl_die(ctx, isl_error_unknown,
3373 "failed to construct factorizer", return -1);
3375 return 0;
3378 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3380 int *injective = user;
3382 *injective = isl_map_is_injective(map);
3383 isl_map_free(map);
3385 if (*injective < 0 || !*injective)
3386 return isl_stat_error;
3388 return isl_stat_ok;
3391 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3392 const char *r, const char *s, int tilable, int parallel)
3394 int i;
3395 isl_union_set *D;
3396 isl_union_map *W, *R, *S;
3397 isl_union_map *empty;
3398 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3399 isl_union_map *validity, *proximity, *coincidence;
3400 isl_union_map *schedule;
3401 isl_union_map *test;
3402 isl_union_set *delta;
3403 isl_union_set *domain;
3404 isl_set *delta_set;
3405 isl_set *slice;
3406 isl_set *origin;
3407 isl_schedule_constraints *sc;
3408 isl_schedule *sched;
3409 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3411 D = isl_union_set_read_from_str(ctx, d);
3412 W = isl_union_map_read_from_str(ctx, w);
3413 R = isl_union_map_read_from_str(ctx, r);
3414 S = isl_union_map_read_from_str(ctx, s);
3416 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3417 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3419 empty = isl_union_map_empty(isl_union_map_get_space(S));
3420 isl_union_map_compute_flow(isl_union_map_copy(R),
3421 isl_union_map_copy(W), empty,
3422 isl_union_map_copy(S),
3423 &dep_raw, NULL, NULL, NULL);
3424 isl_union_map_compute_flow(isl_union_map_copy(W),
3425 isl_union_map_copy(W),
3426 isl_union_map_copy(R),
3427 isl_union_map_copy(S),
3428 &dep_waw, &dep_war, NULL, NULL);
3430 dep = isl_union_map_union(dep_waw, dep_war);
3431 dep = isl_union_map_union(dep, dep_raw);
3432 validity = isl_union_map_copy(dep);
3433 coincidence = isl_union_map_copy(dep);
3434 proximity = isl_union_map_copy(dep);
3436 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3437 sc = isl_schedule_constraints_set_validity(sc, validity);
3438 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3439 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3440 sched = isl_schedule_constraints_compute_schedule(sc);
3441 schedule = isl_schedule_get_map(sched);
3442 isl_schedule_free(sched);
3443 isl_union_map_free(W);
3444 isl_union_map_free(R);
3445 isl_union_map_free(S);
3447 is_injection = 1;
3448 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3450 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3451 is_complete = isl_union_set_is_subset(D, domain);
3452 isl_union_set_free(D);
3453 isl_union_set_free(domain);
3455 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3456 test = isl_union_map_apply_range(test, dep);
3457 test = isl_union_map_apply_range(test, schedule);
3459 delta = isl_union_map_deltas(test);
3460 if (isl_union_set_n_set(delta) == 0) {
3461 is_tilable = 1;
3462 is_parallel = 1;
3463 is_nonneg = 1;
3464 isl_union_set_free(delta);
3465 } else {
3466 delta_set = isl_set_from_union_set(delta);
3468 slice = isl_set_universe(isl_set_get_space(delta_set));
3469 for (i = 0; i < tilable; ++i)
3470 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3471 is_tilable = isl_set_is_subset(delta_set, slice);
3472 isl_set_free(slice);
3474 slice = isl_set_universe(isl_set_get_space(delta_set));
3475 for (i = 0; i < parallel; ++i)
3476 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3477 is_parallel = isl_set_is_subset(delta_set, slice);
3478 isl_set_free(slice);
3480 origin = isl_set_universe(isl_set_get_space(delta_set));
3481 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
3482 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3484 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3485 delta_set = isl_set_lexmin(delta_set);
3487 is_nonneg = isl_set_is_equal(delta_set, origin);
3489 isl_set_free(origin);
3490 isl_set_free(delta_set);
3493 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3494 is_injection < 0 || is_complete < 0)
3495 return -1;
3496 if (!is_complete)
3497 isl_die(ctx, isl_error_unknown,
3498 "generated schedule incomplete", return -1);
3499 if (!is_injection)
3500 isl_die(ctx, isl_error_unknown,
3501 "generated schedule not injective on each statement",
3502 return -1);
3503 if (!is_nonneg)
3504 isl_die(ctx, isl_error_unknown,
3505 "negative dependences in generated schedule",
3506 return -1);
3507 if (!is_tilable)
3508 isl_die(ctx, isl_error_unknown,
3509 "generated schedule not as tilable as expected",
3510 return -1);
3511 if (!is_parallel)
3512 isl_die(ctx, isl_error_unknown,
3513 "generated schedule not as parallel as expected",
3514 return -1);
3516 return 0;
3519 /* Compute a schedule for the given instance set, validity constraints,
3520 * proximity constraints and context and return a corresponding union map
3521 * representation.
3523 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3524 const char *domain, const char *validity, const char *proximity,
3525 const char *context)
3527 isl_set *con;
3528 isl_union_set *dom;
3529 isl_union_map *dep;
3530 isl_union_map *prox;
3531 isl_schedule_constraints *sc;
3532 isl_schedule *schedule;
3533 isl_union_map *sched;
3535 con = isl_set_read_from_str(ctx, context);
3536 dom = isl_union_set_read_from_str(ctx, domain);
3537 dep = isl_union_map_read_from_str(ctx, validity);
3538 prox = isl_union_map_read_from_str(ctx, proximity);
3539 sc = isl_schedule_constraints_on_domain(dom);
3540 sc = isl_schedule_constraints_set_context(sc, con);
3541 sc = isl_schedule_constraints_set_validity(sc, dep);
3542 sc = isl_schedule_constraints_set_proximity(sc, prox);
3543 schedule = isl_schedule_constraints_compute_schedule(sc);
3544 sched = isl_schedule_get_map(schedule);
3545 isl_schedule_free(schedule);
3547 return sched;
3550 /* Compute a schedule for the given instance set, validity constraints and
3551 * proximity constraints and return a corresponding union map representation.
3553 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
3554 const char *domain, const char *validity, const char *proximity)
3556 return compute_schedule_with_context(ctx, domain, validity, proximity,
3557 "{ : }");
3560 /* Check that a schedule can be constructed on the given domain
3561 * with the given validity and proximity constraints.
3563 static int test_has_schedule(isl_ctx *ctx, const char *domain,
3564 const char *validity, const char *proximity)
3566 isl_union_map *sched;
3568 sched = compute_schedule(ctx, domain, validity, proximity);
3569 if (!sched)
3570 return -1;
3572 isl_union_map_free(sched);
3573 return 0;
3576 int test_special_schedule(isl_ctx *ctx, const char *domain,
3577 const char *validity, const char *proximity, const char *expected_sched)
3579 isl_union_map *sched1, *sched2;
3580 int equal;
3582 sched1 = compute_schedule(ctx, domain, validity, proximity);
3583 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
3585 equal = isl_union_map_is_equal(sched1, sched2);
3586 isl_union_map_free(sched1);
3587 isl_union_map_free(sched2);
3589 if (equal < 0)
3590 return -1;
3591 if (!equal)
3592 isl_die(ctx, isl_error_unknown, "unexpected schedule",
3593 return -1);
3595 return 0;
3598 /* Check that the schedule map is properly padded, even after being
3599 * reconstructed from the band forest.
3601 static int test_padded_schedule(isl_ctx *ctx)
3603 const char *str;
3604 isl_union_set *D;
3605 isl_union_map *validity, *proximity;
3606 isl_schedule_constraints *sc;
3607 isl_schedule *sched;
3608 isl_union_map *map1, *map2;
3609 isl_band_list *list;
3610 int equal;
3612 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
3613 D = isl_union_set_read_from_str(ctx, str);
3614 validity = isl_union_map_empty(isl_union_set_get_space(D));
3615 proximity = isl_union_map_copy(validity);
3616 sc = isl_schedule_constraints_on_domain(D);
3617 sc = isl_schedule_constraints_set_validity(sc, validity);
3618 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3619 sched = isl_schedule_constraints_compute_schedule(sc);
3620 map1 = isl_schedule_get_map(sched);
3621 list = isl_schedule_get_band_forest(sched);
3622 isl_band_list_free(list);
3623 map2 = isl_schedule_get_map(sched);
3624 isl_schedule_free(sched);
3625 equal = isl_union_map_is_equal(map1, map2);
3626 isl_union_map_free(map1);
3627 isl_union_map_free(map2);
3629 if (equal < 0)
3630 return -1;
3631 if (!equal)
3632 isl_die(ctx, isl_error_unknown,
3633 "reconstructed schedule map not the same as original",
3634 return -1);
3636 return 0;
3639 /* Check that conditional validity constraints are also taken into
3640 * account across bands.
3641 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
3642 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
3643 * and then check that the adjacent order constraint C[2,1]->D[2,0]
3644 * is enforced by the rest of the schedule.
3646 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
3648 const char *str;
3649 isl_union_set *domain;
3650 isl_union_map *validity, *proximity, *condition;
3651 isl_union_map *sink, *source, *dep;
3652 isl_schedule_constraints *sc;
3653 isl_schedule *schedule;
3654 isl_union_access_info *access;
3655 isl_union_flow *flow;
3656 int empty;
3658 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3659 "A[k] : k >= 1 and k <= -1 + n; "
3660 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3661 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
3662 domain = isl_union_set_read_from_str(ctx, str);
3663 sc = isl_schedule_constraints_on_domain(domain);
3664 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
3665 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3666 "D[k, i] -> C[1 + k, i] : "
3667 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3668 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
3669 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
3670 validity = isl_union_map_read_from_str(ctx, str);
3671 sc = isl_schedule_constraints_set_validity(sc, validity);
3672 str = "[n] -> { C[k, i] -> D[k, i] : "
3673 "0 <= i <= -1 + k and k <= -1 + n }";
3674 proximity = isl_union_map_read_from_str(ctx, str);
3675 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3676 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
3677 "i <= -1 + k and i >= 1 and k <= -2 + n; "
3678 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
3679 "k <= -1 + n and i >= 0 and i <= -2 + k }";
3680 condition = isl_union_map_read_from_str(ctx, str);
3681 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
3682 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3683 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
3684 "i >= 0 and i <= -1 + k and k <= -1 + n and "
3685 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
3686 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
3687 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3688 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
3689 "k <= -1 + n and i >= 0 and i <= -1 + k and "
3690 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
3691 validity = isl_union_map_read_from_str(ctx, str);
3692 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3693 validity);
3694 schedule = isl_schedule_constraints_compute_schedule(sc);
3695 str = "{ D[2,0] -> [] }";
3696 sink = isl_union_map_read_from_str(ctx, str);
3697 access = isl_union_access_info_from_sink(sink);
3698 str = "{ C[2,1] -> [] }";
3699 source = isl_union_map_read_from_str(ctx, str);
3700 access = isl_union_access_info_set_must_source(access, source);
3701 access = isl_union_access_info_set_schedule(access, schedule);
3702 flow = isl_union_access_info_compute_flow(access);
3703 dep = isl_union_flow_get_must_dependence(flow);
3704 isl_union_flow_free(flow);
3705 empty = isl_union_map_is_empty(dep);
3706 isl_union_map_free(dep);
3708 if (empty < 0)
3709 return -1;
3710 if (empty)
3711 isl_die(ctx, isl_error_unknown,
3712 "conditional validity not respected", return -1);
3714 return 0;
3717 /* Check that the test for violated conditional validity constraints
3718 * is not confused by domain compression.
3719 * In particular, earlier versions of isl would apply
3720 * a schedule on the compressed domains to the original domains,
3721 * resulting in a failure to detect that the default schedule
3722 * violates the conditional validity constraints.
3724 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
3726 const char *str;
3727 isl_bool empty;
3728 isl_union_set *domain;
3729 isl_union_map *validity, *condition;
3730 isl_schedule_constraints *sc;
3731 isl_schedule *schedule;
3732 isl_union_map *umap;
3733 isl_map *map, *ge;
3735 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
3736 domain = isl_union_set_read_from_str(ctx, str);
3737 sc = isl_schedule_constraints_on_domain(domain);
3738 str = "{ B[1, i] -> A[0, i + 1] }";
3739 condition = isl_union_map_read_from_str(ctx, str);
3740 str = "{ A[0, i] -> B[1, i - 1] }";
3741 validity = isl_union_map_read_from_str(ctx, str);
3742 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3743 isl_union_map_copy(validity));
3744 schedule = isl_schedule_constraints_compute_schedule(sc);
3745 umap = isl_schedule_get_map(schedule);
3746 isl_schedule_free(schedule);
3747 validity = isl_union_map_apply_domain(validity,
3748 isl_union_map_copy(umap));
3749 validity = isl_union_map_apply_range(validity, umap);
3750 map = isl_map_from_union_map(validity);
3751 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3752 map = isl_map_intersect(map, ge);
3753 empty = isl_map_is_empty(map);
3754 isl_map_free(map);
3756 if (empty < 0)
3757 return -1;
3758 if (!empty)
3759 isl_die(ctx, isl_error_unknown,
3760 "conditional validity constraints not satisfied",
3761 return -1);
3763 return 0;
3766 /* Input for testing of schedule construction based on
3767 * conditional constraints.
3769 * domain is the iteration domain
3770 * flow are the flow dependences, which determine the validity and
3771 * proximity constraints
3772 * condition are the conditions on the conditional validity constraints
3773 * conditional_validity are the conditional validity constraints
3774 * outer_band_n is the expected number of members in the outer band
3776 struct {
3777 const char *domain;
3778 const char *flow;
3779 const char *condition;
3780 const char *conditional_validity;
3781 int outer_band_n;
3782 } live_range_tests[] = {
3783 /* Contrived example that illustrates that we need to keep
3784 * track of tagged condition dependences and
3785 * tagged conditional validity dependences
3786 * in isl_sched_edge separately.
3787 * In particular, the conditional validity constraints on A
3788 * cannot be satisfied,
3789 * but they can be ignored because there are no corresponding
3790 * condition constraints. However, we do have an additional
3791 * conditional validity constraint that maps to the same
3792 * dependence relation
3793 * as the condition constraint on B. If we did not make a distinction
3794 * between tagged condition and tagged conditional validity
3795 * dependences, then we
3796 * could end up treating this shared dependence as an condition
3797 * constraint on A, forcing a localization of the conditions,
3798 * which is impossible.
3800 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
3801 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
3802 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
3803 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3804 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3805 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
3808 /* TACO 2013 Fig. 7 */
3809 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3810 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3811 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3812 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
3813 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
3814 "0 <= i < n and 0 <= j < n - 1 }",
3815 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
3816 "0 <= i < n and 0 <= j < j' < n;"
3817 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
3818 "0 <= i < i' < n and 0 <= j,j' < n;"
3819 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
3820 "0 <= i,j,j' < n and j < j' }",
3823 /* TACO 2013 Fig. 7, without tags */
3824 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3825 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3826 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3827 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3828 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3829 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
3830 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
3831 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
3834 /* TACO 2013 Fig. 12 */
3835 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
3836 "S3[i,3] : 0 <= i <= 1 }",
3837 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
3838 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
3839 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
3840 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
3841 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3842 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
3843 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3844 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
3845 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
3846 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
3847 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
3852 /* Test schedule construction based on conditional constraints.
3853 * In particular, check the number of members in the outer band node
3854 * as an indication of whether tiling is possible or not.
3856 static int test_conditional_schedule_constraints(isl_ctx *ctx)
3858 int i;
3859 isl_union_set *domain;
3860 isl_union_map *condition;
3861 isl_union_map *flow;
3862 isl_union_map *validity;
3863 isl_schedule_constraints *sc;
3864 isl_schedule *schedule;
3865 isl_schedule_node *node;
3866 int n_member;
3868 if (test_special_conditional_schedule_constraints(ctx) < 0)
3869 return -1;
3870 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
3871 return -1;
3873 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
3874 domain = isl_union_set_read_from_str(ctx,
3875 live_range_tests[i].domain);
3876 flow = isl_union_map_read_from_str(ctx,
3877 live_range_tests[i].flow);
3878 condition = isl_union_map_read_from_str(ctx,
3879 live_range_tests[i].condition);
3880 validity = isl_union_map_read_from_str(ctx,
3881 live_range_tests[i].conditional_validity);
3882 sc = isl_schedule_constraints_on_domain(domain);
3883 sc = isl_schedule_constraints_set_validity(sc,
3884 isl_union_map_copy(flow));
3885 sc = isl_schedule_constraints_set_proximity(sc, flow);
3886 sc = isl_schedule_constraints_set_conditional_validity(sc,
3887 condition, validity);
3888 schedule = isl_schedule_constraints_compute_schedule(sc);
3889 node = isl_schedule_get_root(schedule);
3890 while (node &&
3891 isl_schedule_node_get_type(node) != isl_schedule_node_band)
3892 node = isl_schedule_node_first_child(node);
3893 n_member = isl_schedule_node_band_n_member(node);
3894 isl_schedule_node_free(node);
3895 isl_schedule_free(schedule);
3897 if (!schedule)
3898 return -1;
3899 if (n_member != live_range_tests[i].outer_band_n)
3900 isl_die(ctx, isl_error_unknown,
3901 "unexpected number of members in outer band",
3902 return -1);
3904 return 0;
3907 /* Check that the schedule computed for the given instance set and
3908 * dependence relation strongly satisfies the dependences.
3909 * In particular, check that no instance is scheduled before
3910 * or together with an instance on which it depends.
3911 * Earlier versions of isl would produce a schedule that
3912 * only weakly satisfies the dependences.
3914 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
3916 const char *domain, *dep;
3917 isl_union_map *D, *schedule;
3918 isl_map *map, *ge;
3919 int empty;
3921 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
3922 "A[i0] : 0 <= i0 <= 1 }";
3923 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
3924 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
3925 schedule = compute_schedule(ctx, domain, dep, dep);
3926 D = isl_union_map_read_from_str(ctx, dep);
3927 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
3928 D = isl_union_map_apply_range(D, schedule);
3929 map = isl_map_from_union_map(D);
3930 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3931 map = isl_map_intersect(map, ge);
3932 empty = isl_map_is_empty(map);
3933 isl_map_free(map);
3935 if (empty < 0)
3936 return -1;
3937 if (!empty)
3938 isl_die(ctx, isl_error_unknown,
3939 "dependences not strongly satisfied", return -1);
3941 return 0;
3944 /* Compute a schedule for input where the instance set constraints
3945 * conflict with the context constraints.
3946 * Earlier versions of isl did not properly handle this situation.
3948 static int test_conflicting_context_schedule(isl_ctx *ctx)
3950 isl_union_map *schedule;
3951 const char *domain, *context;
3953 domain = "[n] -> { A[] : n >= 0 }";
3954 context = "[n] -> { : n < 0 }";
3955 schedule = compute_schedule_with_context(ctx,
3956 domain, "{}", "{}", context);
3957 isl_union_map_free(schedule);
3959 if (!schedule)
3960 return -1;
3962 return 0;
3965 /* Check that the dependence carrying step is not confused by
3966 * a bound on the coefficient size.
3967 * In particular, force the scheduler to move to a dependence carrying
3968 * step by demanding outer coincidence and bound the size of
3969 * the coefficients. Earlier versions of isl would take this
3970 * bound into account while carrying dependences, breaking
3971 * fundamental assumptions.
3972 * On the other hand, the dependence carrying step now tries
3973 * to prevent loop coalescing by default, so check that indeed
3974 * no loop coalescing occurs by comparing the computed schedule
3975 * to the expected non-coalescing schedule.
3977 static int test_bounded_coefficients_schedule(isl_ctx *ctx)
3979 const char *domain, *dep;
3980 isl_union_set *I;
3981 isl_union_map *D;
3982 isl_schedule_constraints *sc;
3983 isl_schedule *schedule;
3984 isl_union_map *sched1, *sched2;
3985 isl_bool equal;
3987 domain = "{ C[i0, i1] : 2 <= i0 <= 3999 and 0 <= i1 <= -1 + i0 }";
3988 dep = "{ C[i0, i1] -> C[i0, 1 + i1] : i0 <= 3999 and i1 >= 0 and "
3989 "i1 <= -2 + i0; "
3990 "C[i0, -1 + i0] -> C[1 + i0, 0] : i0 <= 3998 and i0 >= 1 }";
3991 I = isl_union_set_read_from_str(ctx, domain);
3992 D = isl_union_map_read_from_str(ctx, dep);
3993 sc = isl_schedule_constraints_on_domain(I);
3994 sc = isl_schedule_constraints_set_validity(sc, isl_union_map_copy(D));
3995 sc = isl_schedule_constraints_set_coincidence(sc, D);
3996 isl_options_set_schedule_outer_coincidence(ctx, 1);
3997 isl_options_set_schedule_max_coefficient(ctx, 20);
3998 schedule = isl_schedule_constraints_compute_schedule(sc);
3999 isl_options_set_schedule_max_coefficient(ctx, -1);
4000 isl_options_set_schedule_outer_coincidence(ctx, 0);
4001 sched1 = isl_schedule_get_map(schedule);
4002 isl_schedule_free(schedule);
4004 sched2 = isl_union_map_read_from_str(ctx, "{ C[x,y] -> [x,y] }");
4005 equal = isl_union_map_is_equal(sched1, sched2);
4006 isl_union_map_free(sched1);
4007 isl_union_map_free(sched2);
4009 if (equal < 0)
4010 return -1;
4011 if (!equal)
4012 isl_die(ctx, isl_error_unknown,
4013 "unexpected schedule", return -1);
4015 return 0;
4018 /* Check that a set of schedule constraints that only allow for
4019 * a coalescing schedule still produces a schedule even if the user
4020 * request a non-coalescing schedule. Earlier versions of isl
4021 * would not handle this case correctly.
4023 static int test_coalescing_schedule(isl_ctx *ctx)
4025 const char *domain, *dep;
4026 isl_union_set *I;
4027 isl_union_map *D;
4028 isl_schedule_constraints *sc;
4029 isl_schedule *schedule;
4030 int treat_coalescing;
4032 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
4033 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
4034 I = isl_union_set_read_from_str(ctx, domain);
4035 D = isl_union_map_read_from_str(ctx, dep);
4036 sc = isl_schedule_constraints_on_domain(I);
4037 sc = isl_schedule_constraints_set_validity(sc, D);
4038 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4039 isl_options_set_schedule_treat_coalescing(ctx, 1);
4040 schedule = isl_schedule_constraints_compute_schedule(sc);
4041 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4042 isl_schedule_free(schedule);
4043 if (!schedule)
4044 return -1;
4045 return 0;
4048 int test_schedule(isl_ctx *ctx)
4050 const char *D, *W, *R, *V, *P, *S;
4051 int max_coincidence;
4052 int treat_coalescing;
4054 /* Handle resulting schedule with zero bands. */
4055 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
4056 return -1;
4058 /* Jacobi */
4059 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
4060 W = "{ S1[t,i] -> a[t,i] }";
4061 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
4062 "S1[t,i] -> a[t-1,i+1] }";
4063 S = "{ S1[t,i] -> [t,i] }";
4064 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4065 return -1;
4067 /* Fig. 5 of CC2008 */
4068 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
4069 "j <= -1 + N }";
4070 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
4071 "j >= 2 and j <= -1 + N }";
4072 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
4073 "j >= 2 and j <= -1 + N; "
4074 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
4075 "j >= 2 and j <= -1 + N }";
4076 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4077 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4078 return -1;
4080 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
4081 W = "{ S1[i] -> a[i] }";
4082 R = "{ S2[i] -> a[i+1] }";
4083 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4084 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4085 return -1;
4087 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
4088 W = "{ S1[i] -> a[i] }";
4089 R = "{ S2[i] -> a[9-i] }";
4090 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4091 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4092 return -1;
4094 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
4095 W = "{ S1[i] -> a[i] }";
4096 R = "[N] -> { S2[i] -> a[N-1-i] }";
4097 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4098 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4099 return -1;
4101 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
4102 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
4103 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
4104 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
4105 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4106 return -1;
4108 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4109 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
4110 R = "{ S2[i,j] -> a[i-1,j] }";
4111 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4112 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4113 return -1;
4115 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4116 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
4117 R = "{ S2[i,j] -> a[i,j-1] }";
4118 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4119 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4120 return -1;
4122 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
4123 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
4124 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
4125 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
4126 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
4127 "S_0[] -> [0, 0, 0] }";
4128 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
4129 return -1;
4130 ctx->opt->schedule_parametric = 0;
4131 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4132 return -1;
4133 ctx->opt->schedule_parametric = 1;
4135 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
4136 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
4137 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
4138 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
4139 "S4[i] -> a[i,N] }";
4140 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
4141 "S4[i] -> [4,i,0] }";
4142 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
4143 isl_options_set_schedule_maximize_coincidence(ctx, 0);
4144 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4145 return -1;
4146 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
4148 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
4149 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4150 "j <= N }";
4151 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4152 "j <= N; "
4153 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
4154 "j <= N }";
4155 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4156 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4157 return -1;
4159 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
4160 " S_2[t] : t >= 0 and t <= -1 + N; "
4161 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
4162 "i <= -1 + N }";
4163 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
4164 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
4165 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
4166 "i >= 0 and i <= -1 + N }";
4167 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
4168 "i >= 0 and i <= -1 + N; "
4169 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
4170 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
4171 " S_0[t] -> [0, t, 0] }";
4173 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4174 return -1;
4175 ctx->opt->schedule_parametric = 0;
4176 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4177 return -1;
4178 ctx->opt->schedule_parametric = 1;
4180 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
4181 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
4182 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
4183 return -1;
4185 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
4186 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
4187 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
4188 "j >= 0 and j <= -1 + N; "
4189 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4190 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
4191 "j >= 0 and j <= -1 + N; "
4192 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4193 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
4194 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4195 return -1;
4197 D = "{ S_0[i] : i >= 0 }";
4198 W = "{ S_0[i] -> a[i] : i >= 0 }";
4199 R = "{ S_0[i] -> a[0] : i >= 0 }";
4200 S = "{ S_0[i] -> [0, i, 0] }";
4201 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4202 return -1;
4204 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
4205 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
4206 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
4207 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
4208 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4209 return -1;
4211 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
4212 "k <= -1 + n and k >= 0 }";
4213 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
4214 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
4215 "k <= -1 + n and k >= 0; "
4216 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
4217 "k <= -1 + n and k >= 0; "
4218 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
4219 "k <= -1 + n and k >= 0 }";
4220 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
4221 ctx->opt->schedule_outer_coincidence = 1;
4222 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4223 return -1;
4224 ctx->opt->schedule_outer_coincidence = 0;
4226 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
4227 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
4228 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
4229 "Stmt_for_body24[i0, i1, 1, 0]:"
4230 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
4231 "Stmt_for_body7[i0, i1, i2]:"
4232 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
4233 "i2 <= 7 }";
4235 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
4236 "Stmt_for_body24[1, i1, i2, i3]:"
4237 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
4238 "i2 >= 1;"
4239 "Stmt_for_body24[0, i1, i2, i3] -> "
4240 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
4241 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
4242 "i3 >= 0;"
4243 "Stmt_for_body24[0, i1, i2, i3] ->"
4244 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4245 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4246 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4247 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4248 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4249 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4250 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4251 "i1 <= 6 and i1 >= 0;"
4252 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4253 "Stmt_for_body7[i0, i1, i2] -> "
4254 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4255 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4256 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4257 "Stmt_for_body7[i0, i1, i2] -> "
4258 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4259 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4260 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4261 P = V;
4262 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
4263 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
4264 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
4266 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4267 isl_options_set_schedule_treat_coalescing(ctx, 0);
4268 if (test_special_schedule(ctx, D, V, P, S) < 0)
4269 return -1;
4270 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4272 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4273 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4274 "j >= 1 and j <= 7;"
4275 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4276 "j >= 1 and j <= 8 }";
4277 P = "{ }";
4278 S = "{ S_0[i, j] -> [i + j, j] }";
4279 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4280 if (test_special_schedule(ctx, D, V, P, S) < 0)
4281 return -1;
4282 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4284 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4285 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4286 "j >= 0 and j <= -1 + i }";
4287 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4288 "i <= -1 + N and j >= 0;"
4289 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4290 "i <= -2 + N }";
4291 P = "{ }";
4292 S = "{ S_0[i, j] -> [i, j] }";
4293 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4294 if (test_special_schedule(ctx, D, V, P, S) < 0)
4295 return -1;
4296 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4298 /* Test both algorithms on a case with only proximity dependences. */
4299 D = "{ S[i,j] : 0 <= i <= 10 }";
4300 V = "{ }";
4301 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4302 S = "{ S[i, j] -> [j, i] }";
4303 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4304 if (test_special_schedule(ctx, D, V, P, S) < 0)
4305 return -1;
4306 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4307 if (test_special_schedule(ctx, D, V, P, S) < 0)
4308 return -1;
4310 D = "{ A[a]; B[] }";
4311 V = "{}";
4312 P = "{ A[a] -> B[] }";
4313 if (test_has_schedule(ctx, D, V, P) < 0)
4314 return -1;
4316 if (test_padded_schedule(ctx) < 0)
4317 return -1;
4319 /* Check that check for progress is not confused by rational
4320 * solution.
4322 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4323 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4324 "i0 <= -2 + N; "
4325 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4326 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4327 P = "{}";
4328 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4329 if (test_has_schedule(ctx, D, V, P) < 0)
4330 return -1;
4331 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4333 /* Check that we allow schedule rows that are only non-trivial
4334 * on some full-dimensional domains.
4336 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4337 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4338 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4339 P = "{}";
4340 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4341 if (test_has_schedule(ctx, D, V, P) < 0)
4342 return -1;
4343 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4345 if (test_conditional_schedule_constraints(ctx) < 0)
4346 return -1;
4348 if (test_strongly_satisfying_schedule(ctx) < 0)
4349 return -1;
4351 if (test_conflicting_context_schedule(ctx) < 0)
4352 return -1;
4354 if (test_bounded_coefficients_schedule(ctx) < 0)
4355 return -1;
4356 if (test_coalescing_schedule(ctx) < 0)
4357 return -1;
4359 return 0;
4362 /* Perform scheduling tests using the whole component scheduler.
4364 static int test_schedule_whole(isl_ctx *ctx)
4366 int whole;
4367 int r;
4369 whole = isl_options_get_schedule_whole_component(ctx);
4370 isl_options_set_schedule_whole_component(ctx, 1);
4371 r = test_schedule(ctx);
4372 isl_options_set_schedule_whole_component(ctx, whole);
4374 return r;
4377 /* Perform scheduling tests using the incremental scheduler.
4379 static int test_schedule_incremental(isl_ctx *ctx)
4381 int whole;
4382 int r;
4384 whole = isl_options_get_schedule_whole_component(ctx);
4385 isl_options_set_schedule_whole_component(ctx, 0);
4386 r = test_schedule(ctx);
4387 isl_options_set_schedule_whole_component(ctx, whole);
4389 return r;
4392 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4394 isl_union_map *umap;
4395 int test;
4397 umap = isl_union_map_read_from_str(ctx, str);
4398 test = isl_union_map_plain_is_injective(umap);
4399 isl_union_map_free(umap);
4400 if (test < 0)
4401 return -1;
4402 if (test == injective)
4403 return 0;
4404 if (injective)
4405 isl_die(ctx, isl_error_unknown,
4406 "map not detected as injective", return -1);
4407 else
4408 isl_die(ctx, isl_error_unknown,
4409 "map detected as injective", return -1);
4412 int test_injective(isl_ctx *ctx)
4414 const char *str;
4416 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4417 return -1;
4418 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4419 return -1;
4420 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4421 return -1;
4422 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4423 return -1;
4424 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4425 return -1;
4426 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4427 return -1;
4428 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4429 return -1;
4430 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4431 return -1;
4433 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4434 if (test_plain_injective(ctx, str, 1))
4435 return -1;
4436 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4437 if (test_plain_injective(ctx, str, 0))
4438 return -1;
4440 return 0;
4443 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4445 isl_aff *aff2;
4446 int equal;
4448 if (!aff)
4449 return -1;
4451 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4452 equal = isl_aff_plain_is_equal(aff, aff2);
4453 isl_aff_free(aff2);
4455 return equal;
4458 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4460 int equal;
4462 equal = aff_plain_is_equal(aff, str);
4463 if (equal < 0)
4464 return -1;
4465 if (!equal)
4466 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4467 "result not as expected", return -1);
4468 return 0;
4471 struct {
4472 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4473 __isl_take isl_aff *aff2);
4474 } aff_bin_op[] = {
4475 ['+'] = { &isl_aff_add },
4476 ['-'] = { &isl_aff_sub },
4477 ['*'] = { &isl_aff_mul },
4478 ['/'] = { &isl_aff_div },
4481 struct {
4482 const char *arg1;
4483 unsigned char op;
4484 const char *arg2;
4485 const char *res;
4486 } aff_bin_tests[] = {
4487 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
4488 "{ [i] -> [2i] }" },
4489 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
4490 "{ [i] -> [0] }" },
4491 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
4492 "{ [i] -> [2i] }" },
4493 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
4494 "{ [i] -> [2i] }" },
4495 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
4496 "{ [i] -> [i/2] }" },
4497 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
4498 "{ [i] -> [i] }" },
4499 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
4500 "{ [i] -> [NaN] }" },
4501 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
4502 "{ [i] -> [NaN] }" },
4503 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
4504 "{ [i] -> [NaN] }" },
4505 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
4506 "{ [i] -> [NaN] }" },
4507 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
4508 "{ [i] -> [NaN] }" },
4509 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
4510 "{ [i] -> [NaN] }" },
4511 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
4512 "{ [i] -> [NaN] }" },
4513 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
4514 "{ [i] -> [NaN] }" },
4515 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
4516 "{ [i] -> [NaN] }" },
4517 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
4518 "{ [i] -> [NaN] }" },
4519 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
4520 "{ [i] -> [NaN] }" },
4521 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
4522 "{ [i] -> [NaN] }" },
4525 /* Perform some basic tests of binary operations on isl_aff objects.
4527 static int test_bin_aff(isl_ctx *ctx)
4529 int i;
4530 isl_aff *aff1, *aff2, *res;
4531 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4532 __isl_take isl_aff *aff2);
4533 int ok;
4535 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
4536 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
4537 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
4538 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
4539 fn = aff_bin_op[aff_bin_tests[i].op].fn;
4540 aff1 = fn(aff1, aff2);
4541 if (isl_aff_is_nan(res))
4542 ok = isl_aff_is_nan(aff1);
4543 else
4544 ok = isl_aff_plain_is_equal(aff1, res);
4545 isl_aff_free(aff1);
4546 isl_aff_free(res);
4547 if (ok < 0)
4548 return -1;
4549 if (!ok)
4550 isl_die(ctx, isl_error_unknown,
4551 "unexpected result", return -1);
4554 return 0;
4557 struct {
4558 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
4559 __isl_take isl_pw_aff *pa2);
4560 } pw_aff_bin_op[] = {
4561 ['m'] = { &isl_pw_aff_min },
4562 ['M'] = { &isl_pw_aff_max },
4565 /* Inputs for binary isl_pw_aff operation tests.
4566 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
4567 * defined by pw_aff_bin_op, and "res" is the expected result.
4569 struct {
4570 const char *arg1;
4571 unsigned char op;
4572 const char *arg2;
4573 const char *res;
4574 } pw_aff_bin_tests[] = {
4575 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
4576 "{ [i] -> [i] }" },
4577 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
4578 "{ [i] -> [i] }" },
4579 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
4580 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
4581 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
4582 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
4583 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
4584 "{ [i] -> [NaN] }" },
4585 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
4586 "{ [i] -> [NaN] }" },
4589 /* Perform some basic tests of binary operations on isl_pw_aff objects.
4591 static int test_bin_pw_aff(isl_ctx *ctx)
4593 int i;
4594 isl_bool ok;
4595 isl_pw_aff *pa1, *pa2, *res;
4597 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
4598 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
4599 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
4600 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
4601 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
4602 if (isl_pw_aff_involves_nan(res))
4603 ok = isl_pw_aff_involves_nan(pa1);
4604 else
4605 ok = isl_pw_aff_plain_is_equal(pa1, res);
4606 isl_pw_aff_free(pa1);
4607 isl_pw_aff_free(res);
4608 if (ok < 0)
4609 return -1;
4610 if (!ok)
4611 isl_die(ctx, isl_error_unknown,
4612 "unexpected result", return -1);
4615 return 0;
4618 struct {
4619 __isl_give isl_union_pw_multi_aff *(*fn)(
4620 __isl_take isl_union_pw_multi_aff *upma1,
4621 __isl_take isl_union_pw_multi_aff *upma2);
4622 const char *arg1;
4623 const char *arg2;
4624 const char *res;
4625 } upma_bin_tests[] = {
4626 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
4627 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
4628 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
4629 "{ B[x] -> [2] : x >= 0 }",
4630 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
4631 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
4632 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
4633 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
4634 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
4635 "D[i] -> B[2] : i >= 5 }" },
4636 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4637 "{ B[x] -> C[2] : x > 0 }",
4638 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
4639 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4640 "{ B[x] -> A[2] : x >= 0 }",
4641 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
4644 /* Perform some basic tests of binary operations on
4645 * isl_union_pw_multi_aff objects.
4647 static int test_bin_upma(isl_ctx *ctx)
4649 int i;
4650 isl_union_pw_multi_aff *upma1, *upma2, *res;
4651 int ok;
4653 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
4654 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4655 upma_bin_tests[i].arg1);
4656 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4657 upma_bin_tests[i].arg2);
4658 res = isl_union_pw_multi_aff_read_from_str(ctx,
4659 upma_bin_tests[i].res);
4660 upma1 = upma_bin_tests[i].fn(upma1, upma2);
4661 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
4662 isl_union_pw_multi_aff_free(upma1);
4663 isl_union_pw_multi_aff_free(res);
4664 if (ok < 0)
4665 return -1;
4666 if (!ok)
4667 isl_die(ctx, isl_error_unknown,
4668 "unexpected result", return -1);
4671 return 0;
4674 struct {
4675 __isl_give isl_union_pw_multi_aff *(*fn)(
4676 __isl_take isl_union_pw_multi_aff *upma1,
4677 __isl_take isl_union_pw_multi_aff *upma2);
4678 const char *arg1;
4679 const char *arg2;
4680 } upma_bin_fail_tests[] = {
4681 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4682 "{ B[x] -> C[2] : x >= 0 }" },
4685 /* Perform some basic tests of binary operations on
4686 * isl_union_pw_multi_aff objects that are expected to fail.
4688 static int test_bin_upma_fail(isl_ctx *ctx)
4690 int i, n;
4691 isl_union_pw_multi_aff *upma1, *upma2;
4692 int on_error;
4694 on_error = isl_options_get_on_error(ctx);
4695 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
4696 n = ARRAY_SIZE(upma_bin_fail_tests);
4697 for (i = 0; i < n; ++i) {
4698 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4699 upma_bin_fail_tests[i].arg1);
4700 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4701 upma_bin_fail_tests[i].arg2);
4702 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
4703 isl_union_pw_multi_aff_free(upma1);
4704 if (upma1)
4705 break;
4707 isl_options_set_on_error(ctx, on_error);
4708 if (i < n)
4709 isl_die(ctx, isl_error_unknown,
4710 "operation not expected to succeed", return -1);
4712 return 0;
4715 int test_aff(isl_ctx *ctx)
4717 const char *str;
4718 isl_set *set;
4719 isl_space *space;
4720 isl_local_space *ls;
4721 isl_aff *aff;
4722 int zero, equal;
4724 if (test_bin_aff(ctx) < 0)
4725 return -1;
4726 if (test_bin_pw_aff(ctx) < 0)
4727 return -1;
4728 if (test_bin_upma(ctx) < 0)
4729 return -1;
4730 if (test_bin_upma_fail(ctx) < 0)
4731 return -1;
4733 space = isl_space_set_alloc(ctx, 0, 1);
4734 ls = isl_local_space_from_space(space);
4735 aff = isl_aff_zero_on_domain(ls);
4737 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4738 aff = isl_aff_scale_down_ui(aff, 3);
4739 aff = isl_aff_floor(aff);
4740 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4741 aff = isl_aff_scale_down_ui(aff, 2);
4742 aff = isl_aff_floor(aff);
4743 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4745 str = "{ [10] }";
4746 set = isl_set_read_from_str(ctx, str);
4747 aff = isl_aff_gist(aff, set);
4749 aff = isl_aff_add_constant_si(aff, -16);
4750 zero = isl_aff_plain_is_zero(aff);
4751 isl_aff_free(aff);
4753 if (zero < 0)
4754 return -1;
4755 if (!zero)
4756 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4758 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
4759 aff = isl_aff_scale_down_ui(aff, 64);
4760 aff = isl_aff_floor(aff);
4761 equal = aff_check_plain_equal(aff, "{ [-1] }");
4762 isl_aff_free(aff);
4763 if (equal < 0)
4764 return -1;
4766 return 0;
4769 /* Check that the computation below results in a single expression.
4770 * One or two expressions may result depending on which constraint
4771 * ends up being considered as redundant with respect to the other
4772 * constraints after the projection that is performed internally
4773 * by isl_set_dim_min.
4775 static int test_dim_max_1(isl_ctx *ctx)
4777 const char *str;
4778 isl_set *set;
4779 isl_pw_aff *pa;
4780 int n;
4782 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
4783 "-4a <= b <= 3 and b < n - 4a }";
4784 set = isl_set_read_from_str(ctx, str);
4785 pa = isl_set_dim_min(set, 0);
4786 n = isl_pw_aff_n_piece(pa);
4787 isl_pw_aff_free(pa);
4789 if (!pa)
4790 return -1;
4791 if (n != 1)
4792 isl_die(ctx, isl_error_unknown, "expecting single expression",
4793 return -1);
4795 return 0;
4798 int test_dim_max(isl_ctx *ctx)
4800 int equal;
4801 const char *str;
4802 isl_set *set1, *set2;
4803 isl_set *set;
4804 isl_map *map;
4805 isl_pw_aff *pwaff;
4807 if (test_dim_max_1(ctx) < 0)
4808 return -1;
4810 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
4811 set = isl_set_read_from_str(ctx, str);
4812 pwaff = isl_set_dim_max(set, 0);
4813 set1 = isl_set_from_pw_aff(pwaff);
4814 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
4815 set2 = isl_set_read_from_str(ctx, str);
4816 equal = isl_set_is_equal(set1, set2);
4817 isl_set_free(set1);
4818 isl_set_free(set2);
4819 if (equal < 0)
4820 return -1;
4821 if (!equal)
4822 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4824 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
4825 set = isl_set_read_from_str(ctx, str);
4826 pwaff = isl_set_dim_max(set, 0);
4827 set1 = isl_set_from_pw_aff(pwaff);
4828 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4829 set2 = isl_set_read_from_str(ctx, str);
4830 equal = isl_set_is_equal(set1, set2);
4831 isl_set_free(set1);
4832 isl_set_free(set2);
4833 if (equal < 0)
4834 return -1;
4835 if (!equal)
4836 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4838 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
4839 set = isl_set_read_from_str(ctx, str);
4840 pwaff = isl_set_dim_max(set, 0);
4841 set1 = isl_set_from_pw_aff(pwaff);
4842 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4843 set2 = isl_set_read_from_str(ctx, str);
4844 equal = isl_set_is_equal(set1, set2);
4845 isl_set_free(set1);
4846 isl_set_free(set2);
4847 if (equal < 0)
4848 return -1;
4849 if (!equal)
4850 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4852 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
4853 "0 <= i < N and 0 <= j < M }";
4854 map = isl_map_read_from_str(ctx, str);
4855 set = isl_map_range(map);
4857 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
4858 set1 = isl_set_from_pw_aff(pwaff);
4859 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
4860 set2 = isl_set_read_from_str(ctx, str);
4861 equal = isl_set_is_equal(set1, set2);
4862 isl_set_free(set1);
4863 isl_set_free(set2);
4865 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
4866 set1 = isl_set_from_pw_aff(pwaff);
4867 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
4868 set2 = isl_set_read_from_str(ctx, str);
4869 if (equal >= 0 && equal)
4870 equal = isl_set_is_equal(set1, set2);
4871 isl_set_free(set1);
4872 isl_set_free(set2);
4874 isl_set_free(set);
4876 if (equal < 0)
4877 return -1;
4878 if (!equal)
4879 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4881 /* Check that solutions are properly merged. */
4882 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
4883 "c <= -1 + n - 4a - 2b and c >= -2b and "
4884 "4a >= -4 + n and c >= 0 }";
4885 set = isl_set_read_from_str(ctx, str);
4886 pwaff = isl_set_dim_min(set, 2);
4887 set1 = isl_set_from_pw_aff(pwaff);
4888 str = "[n] -> { [(0)] : n >= 1 }";
4889 set2 = isl_set_read_from_str(ctx, str);
4890 equal = isl_set_is_equal(set1, set2);
4891 isl_set_free(set1);
4892 isl_set_free(set2);
4894 if (equal < 0)
4895 return -1;
4896 if (!equal)
4897 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4899 /* Check that empty solution lie in the right space. */
4900 str = "[n] -> { [t,a] : 1 = 0 }";
4901 set = isl_set_read_from_str(ctx, str);
4902 pwaff = isl_set_dim_max(set, 0);
4903 set1 = isl_set_from_pw_aff(pwaff);
4904 str = "[n] -> { [t] : 1 = 0 }";
4905 set2 = isl_set_read_from_str(ctx, str);
4906 equal = isl_set_is_equal(set1, set2);
4907 isl_set_free(set1);
4908 isl_set_free(set2);
4910 if (equal < 0)
4911 return -1;
4912 if (!equal)
4913 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4915 return 0;
4918 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
4920 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
4921 const char *str)
4923 isl_ctx *ctx;
4924 isl_pw_multi_aff *pma2;
4925 int equal;
4927 if (!pma)
4928 return -1;
4930 ctx = isl_pw_multi_aff_get_ctx(pma);
4931 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4932 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
4933 isl_pw_multi_aff_free(pma2);
4935 return equal;
4938 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
4939 * represented by "str".
4941 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
4942 const char *str)
4944 int equal;
4946 equal = pw_multi_aff_plain_is_equal(pma, str);
4947 if (equal < 0)
4948 return -1;
4949 if (!equal)
4950 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
4951 "result not as expected", return -1);
4952 return 0;
4955 /* Basic test for isl_pw_multi_aff_product.
4957 * Check that multiple pieces are properly handled.
4959 static int test_product_pma(isl_ctx *ctx)
4961 int equal;
4962 const char *str;
4963 isl_pw_multi_aff *pma1, *pma2;
4965 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
4966 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4967 str = "{ C[] -> D[] }";
4968 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4969 pma1 = isl_pw_multi_aff_product(pma1, pma2);
4970 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
4971 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
4972 equal = pw_multi_aff_check_plain_equal(pma1, str);
4973 isl_pw_multi_aff_free(pma1);
4974 if (equal < 0)
4975 return -1;
4977 return 0;
4980 int test_product(isl_ctx *ctx)
4982 const char *str;
4983 isl_set *set;
4984 isl_union_set *uset1, *uset2;
4985 int ok;
4987 str = "{ A[i] }";
4988 set = isl_set_read_from_str(ctx, str);
4989 set = isl_set_product(set, isl_set_copy(set));
4990 ok = isl_set_is_wrapping(set);
4991 isl_set_free(set);
4992 if (ok < 0)
4993 return -1;
4994 if (!ok)
4995 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4997 str = "{ [] }";
4998 uset1 = isl_union_set_read_from_str(ctx, str);
4999 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
5000 str = "{ [[] -> []] }";
5001 uset2 = isl_union_set_read_from_str(ctx, str);
5002 ok = isl_union_set_is_equal(uset1, uset2);
5003 isl_union_set_free(uset1);
5004 isl_union_set_free(uset2);
5005 if (ok < 0)
5006 return -1;
5007 if (!ok)
5008 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5010 if (test_product_pma(ctx) < 0)
5011 return -1;
5013 return 0;
5016 /* Check that two sets are not considered disjoint just because
5017 * they have a different set of (named) parameters.
5019 static int test_disjoint(isl_ctx *ctx)
5021 const char *str;
5022 isl_set *set, *set2;
5023 int disjoint;
5025 str = "[n] -> { [[]->[]] }";
5026 set = isl_set_read_from_str(ctx, str);
5027 str = "{ [[]->[]] }";
5028 set2 = isl_set_read_from_str(ctx, str);
5029 disjoint = isl_set_is_disjoint(set, set2);
5030 isl_set_free(set);
5031 isl_set_free(set2);
5032 if (disjoint < 0)
5033 return -1;
5034 if (disjoint)
5035 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5037 return 0;
5040 /* Inputs for isl_pw_multi_aff_is_equal tests.
5041 * "f1" and "f2" are the two function that need to be compared.
5042 * "equal" is the expected result.
5044 struct {
5045 int equal;
5046 const char *f1;
5047 const char *f2;
5048 } pma_equal_tests[] = {
5049 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
5050 "[N] -> { [0] : 0 <= N <= 1 }" },
5051 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
5052 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
5053 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
5054 "[N] -> { [0] : 0 <= N <= 1 }" },
5055 { 0, "{ [NaN] }", "{ [NaN] }" },
5058 int test_equal(isl_ctx *ctx)
5060 int i;
5061 const char *str;
5062 isl_set *set, *set2;
5063 int equal;
5065 str = "{ S_6[i] }";
5066 set = isl_set_read_from_str(ctx, str);
5067 str = "{ S_7[i] }";
5068 set2 = isl_set_read_from_str(ctx, str);
5069 equal = isl_set_is_equal(set, set2);
5070 isl_set_free(set);
5071 isl_set_free(set2);
5072 if (equal < 0)
5073 return -1;
5074 if (equal)
5075 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5077 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
5078 int expected = pma_equal_tests[i].equal;
5079 isl_pw_multi_aff *f1, *f2;
5081 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
5082 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
5083 equal = isl_pw_multi_aff_is_equal(f1, f2);
5084 isl_pw_multi_aff_free(f1);
5085 isl_pw_multi_aff_free(f2);
5086 if (equal < 0)
5087 return -1;
5088 if (equal != expected)
5089 isl_die(ctx, isl_error_unknown,
5090 "unexpected equality result", return -1);
5093 return 0;
5096 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
5097 enum isl_dim_type type, unsigned pos, int fixed)
5099 isl_bool test;
5101 test = isl_map_plain_is_fixed(map, type, pos, NULL);
5102 isl_map_free(map);
5103 if (test < 0)
5104 return -1;
5105 if (test == fixed)
5106 return 0;
5107 if (fixed)
5108 isl_die(ctx, isl_error_unknown,
5109 "map not detected as fixed", return -1);
5110 else
5111 isl_die(ctx, isl_error_unknown,
5112 "map detected as fixed", return -1);
5115 int test_fixed(isl_ctx *ctx)
5117 const char *str;
5118 isl_map *map;
5120 str = "{ [i] -> [i] }";
5121 map = isl_map_read_from_str(ctx, str);
5122 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
5123 return -1;
5124 str = "{ [i] -> [1] }";
5125 map = isl_map_read_from_str(ctx, str);
5126 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5127 return -1;
5128 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
5129 map = isl_map_read_from_str(ctx, str);
5130 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5131 return -1;
5132 map = isl_map_read_from_str(ctx, str);
5133 map = isl_map_neg(map);
5134 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5135 return -1;
5137 return 0;
5140 struct isl_vertices_test_data {
5141 const char *set;
5142 int n;
5143 const char *vertex[6];
5144 } vertices_tests[] = {
5145 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
5146 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
5147 { "{ A[t, i] : t = 14 and i = 1 }",
5148 1, { "{ A[14, 1] }" } },
5149 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
5150 "c <= m and m <= n and m > 0 }",
5151 6, {
5152 "[n, m] -> { [n, m, m] : 0 < m <= n }",
5153 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
5154 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
5155 "[n, m] -> { [m, m, m] : 0 < m <= n }",
5156 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
5157 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
5158 } },
5161 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
5163 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
5165 struct isl_vertices_test_data *data = user;
5166 isl_ctx *ctx;
5167 isl_multi_aff *ma;
5168 isl_basic_set *bset;
5169 isl_pw_multi_aff *pma;
5170 int i;
5171 isl_bool equal;
5173 ctx = isl_vertex_get_ctx(vertex);
5174 bset = isl_vertex_get_domain(vertex);
5175 ma = isl_vertex_get_expr(vertex);
5176 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
5178 for (i = 0; i < data->n; ++i) {
5179 isl_pw_multi_aff *pma_i;
5181 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
5182 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
5183 isl_pw_multi_aff_free(pma_i);
5185 if (equal < 0 || equal)
5186 break;
5189 isl_pw_multi_aff_free(pma);
5190 isl_vertex_free(vertex);
5192 if (equal < 0)
5193 return isl_stat_error;
5195 return equal ? isl_stat_ok : isl_stat_error;
5198 int test_vertices(isl_ctx *ctx)
5200 int i;
5202 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
5203 isl_basic_set *bset;
5204 isl_vertices *vertices;
5205 int ok = 1;
5206 int n;
5208 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
5209 vertices = isl_basic_set_compute_vertices(bset);
5210 n = isl_vertices_get_n_vertices(vertices);
5211 if (vertices_tests[i].n != n)
5212 ok = 0;
5213 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
5214 &vertices_tests[i]) < 0)
5215 ok = 0;
5216 isl_vertices_free(vertices);
5217 isl_basic_set_free(bset);
5219 if (!vertices)
5220 return -1;
5221 if (!ok)
5222 isl_die(ctx, isl_error_unknown, "unexpected vertices",
5223 return -1);
5226 return 0;
5229 int test_union_pw(isl_ctx *ctx)
5231 int equal;
5232 const char *str;
5233 isl_union_set *uset;
5234 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
5236 str = "{ [x] -> x^2 }";
5237 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
5238 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
5239 uset = isl_union_pw_qpolynomial_domain(upwqp1);
5240 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
5241 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
5242 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
5243 isl_union_pw_qpolynomial_free(upwqp1);
5244 isl_union_pw_qpolynomial_free(upwqp2);
5245 if (equal < 0)
5246 return -1;
5247 if (!equal)
5248 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5250 return 0;
5253 /* Test that isl_union_pw_qpolynomial_eval picks up the function
5254 * defined over the correct domain space.
5256 static int test_eval_1(isl_ctx *ctx)
5258 const char *str;
5259 isl_point *pnt;
5260 isl_set *set;
5261 isl_union_pw_qpolynomial *upwqp;
5262 isl_val *v;
5263 int cmp;
5265 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
5266 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
5267 str = "{ A[6] }";
5268 set = isl_set_read_from_str(ctx, str);
5269 pnt = isl_set_sample_point(set);
5270 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
5271 cmp = isl_val_cmp_si(v, 36);
5272 isl_val_free(v);
5274 if (!v)
5275 return -1;
5276 if (cmp != 0)
5277 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
5279 return 0;
5282 /* Check that isl_qpolynomial_eval handles getting called on a void point.
5284 static int test_eval_2(isl_ctx *ctx)
5286 const char *str;
5287 isl_point *pnt;
5288 isl_set *set;
5289 isl_qpolynomial *qp;
5290 isl_val *v;
5291 isl_bool ok;
5293 str = "{ A[x] -> [x] }";
5294 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
5295 str = "{ A[x] : false }";
5296 set = isl_set_read_from_str(ctx, str);
5297 pnt = isl_set_sample_point(set);
5298 v = isl_qpolynomial_eval(qp, pnt);
5299 ok = isl_val_is_nan(v);
5300 isl_val_free(v);
5302 if (ok < 0)
5303 return -1;
5304 if (!ok)
5305 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
5307 return 0;
5310 /* Perform basic polynomial evaluation tests.
5312 static int test_eval(isl_ctx *ctx)
5314 if (test_eval_1(ctx) < 0)
5315 return -1;
5316 if (test_eval_2(ctx) < 0)
5317 return -1;
5318 return 0;
5321 /* Descriptions of sets that are tested for reparsing after printing.
5323 const char *output_tests[] = {
5324 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
5327 /* Check that printing a set and reparsing a set from the printed output
5328 * results in the same set.
5330 static int test_output_set(isl_ctx *ctx)
5332 int i;
5333 char *str;
5334 isl_set *set1, *set2;
5335 isl_bool equal;
5337 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
5338 set1 = isl_set_read_from_str(ctx, output_tests[i]);
5339 str = isl_set_to_str(set1);
5340 set2 = isl_set_read_from_str(ctx, str);
5341 free(str);
5342 equal = isl_set_is_equal(set1, set2);
5343 isl_set_free(set1);
5344 isl_set_free(set2);
5345 if (equal < 0)
5346 return -1;
5347 if (!equal)
5348 isl_die(ctx, isl_error_unknown,
5349 "parsed output not the same", return -1);
5352 return 0;
5355 int test_output(isl_ctx *ctx)
5357 char *s;
5358 const char *str;
5359 isl_pw_aff *pa;
5360 isl_printer *p;
5361 int equal;
5363 if (test_output_set(ctx) < 0)
5364 return -1;
5366 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
5367 pa = isl_pw_aff_read_from_str(ctx, str);
5369 p = isl_printer_to_str(ctx);
5370 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
5371 p = isl_printer_print_pw_aff(p, pa);
5372 s = isl_printer_get_str(p);
5373 isl_printer_free(p);
5374 isl_pw_aff_free(pa);
5375 if (!s)
5376 equal = -1;
5377 else
5378 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
5379 free(s);
5380 if (equal < 0)
5381 return -1;
5382 if (!equal)
5383 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5385 return 0;
5388 int test_sample(isl_ctx *ctx)
5390 const char *str;
5391 isl_basic_set *bset1, *bset2;
5392 int empty, subset;
5394 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
5395 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
5396 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
5397 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
5398 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
5399 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
5400 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
5401 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
5402 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
5403 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
5404 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
5405 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
5406 "d - 1073741821e and "
5407 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
5408 "3j >= 1 - a + b + 2e and "
5409 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
5410 "3i <= 4 - a + 4b - e and "
5411 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
5412 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
5413 "c <= -1 + a and 3i >= -2 - a + 3e and "
5414 "1073741822e <= 1073741823 - a + 1073741822b + c and "
5415 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
5416 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
5417 "1073741823e >= 1 + 1073741823b - d and "
5418 "3i >= 1073741823b + c - 1073741823e - f and "
5419 "3i >= 1 + 2b + e + 3g }";
5420 bset1 = isl_basic_set_read_from_str(ctx, str);
5421 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
5422 empty = isl_basic_set_is_empty(bset2);
5423 subset = isl_basic_set_is_subset(bset2, bset1);
5424 isl_basic_set_free(bset1);
5425 isl_basic_set_free(bset2);
5426 if (empty < 0 || subset < 0)
5427 return -1;
5428 if (empty)
5429 isl_die(ctx, isl_error_unknown, "point not found", return -1);
5430 if (!subset)
5431 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
5433 return 0;
5436 int test_fixed_power(isl_ctx *ctx)
5438 const char *str;
5439 isl_map *map;
5440 isl_int exp;
5441 int equal;
5443 isl_int_init(exp);
5444 str = "{ [i] -> [i + 1] }";
5445 map = isl_map_read_from_str(ctx, str);
5446 isl_int_set_si(exp, 23);
5447 map = isl_map_fixed_power(map, exp);
5448 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
5449 isl_int_clear(exp);
5450 isl_map_free(map);
5451 if (equal < 0)
5452 return -1;
5454 return 0;
5457 int test_slice(isl_ctx *ctx)
5459 const char *str;
5460 isl_map *map;
5461 int equal;
5463 str = "{ [i] -> [j] }";
5464 map = isl_map_read_from_str(ctx, str);
5465 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
5466 equal = map_check_equal(map, "{ [i] -> [i] }");
5467 isl_map_free(map);
5468 if (equal < 0)
5469 return -1;
5471 str = "{ [i] -> [j] }";
5472 map = isl_map_read_from_str(ctx, str);
5473 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
5474 equal = map_check_equal(map, "{ [i] -> [j] }");
5475 isl_map_free(map);
5476 if (equal < 0)
5477 return -1;
5479 str = "{ [i] -> [j] }";
5480 map = isl_map_read_from_str(ctx, str);
5481 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
5482 equal = map_check_equal(map, "{ [i] -> [-i] }");
5483 isl_map_free(map);
5484 if (equal < 0)
5485 return -1;
5487 str = "{ [i] -> [j] }";
5488 map = isl_map_read_from_str(ctx, str);
5489 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
5490 equal = map_check_equal(map, "{ [0] -> [j] }");
5491 isl_map_free(map);
5492 if (equal < 0)
5493 return -1;
5495 str = "{ [i] -> [j] }";
5496 map = isl_map_read_from_str(ctx, str);
5497 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
5498 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
5499 isl_map_free(map);
5500 if (equal < 0)
5501 return -1;
5503 str = "{ [i] -> [j] }";
5504 map = isl_map_read_from_str(ctx, str);
5505 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
5506 equal = map_check_equal(map, "{ [i] -> [j] : false }");
5507 isl_map_free(map);
5508 if (equal < 0)
5509 return -1;
5511 return 0;
5514 int test_eliminate(isl_ctx *ctx)
5516 const char *str;
5517 isl_map *map;
5518 int equal;
5520 str = "{ [i] -> [j] : i = 2j }";
5521 map = isl_map_read_from_str(ctx, str);
5522 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
5523 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
5524 isl_map_free(map);
5525 if (equal < 0)
5526 return -1;
5528 return 0;
5531 /* Check that isl_set_dim_residue_class detects that the values of j
5532 * in the set below are all odd and that it does not detect any spurious
5533 * strides.
5535 static int test_residue_class(isl_ctx *ctx)
5537 const char *str;
5538 isl_set *set;
5539 isl_int m, r;
5540 isl_stat res;
5542 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
5543 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
5544 set = isl_set_read_from_str(ctx, str);
5545 isl_int_init(m);
5546 isl_int_init(r);
5547 res = isl_set_dim_residue_class(set, 1, &m, &r);
5548 if (res >= 0 &&
5549 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
5550 isl_die(ctx, isl_error_unknown, "incorrect residue class",
5551 res = isl_stat_error);
5552 isl_int_clear(r);
5553 isl_int_clear(m);
5554 isl_set_free(set);
5556 return res;
5559 int test_align_parameters(isl_ctx *ctx)
5561 const char *str;
5562 isl_space *space;
5563 isl_multi_aff *ma1, *ma2;
5564 int equal;
5566 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
5567 ma1 = isl_multi_aff_read_from_str(ctx, str);
5569 space = isl_space_params_alloc(ctx, 1);
5570 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
5571 ma1 = isl_multi_aff_align_params(ma1, space);
5573 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
5574 ma2 = isl_multi_aff_read_from_str(ctx, str);
5576 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5578 isl_multi_aff_free(ma1);
5579 isl_multi_aff_free(ma2);
5581 if (equal < 0)
5582 return -1;
5583 if (!equal)
5584 isl_die(ctx, isl_error_unknown,
5585 "result not as expected", return -1);
5587 return 0;
5590 static int test_list(isl_ctx *ctx)
5592 isl_id *a, *b, *c, *d, *id;
5593 isl_id_list *list;
5594 int ok;
5596 a = isl_id_alloc(ctx, "a", NULL);
5597 b = isl_id_alloc(ctx, "b", NULL);
5598 c = isl_id_alloc(ctx, "c", NULL);
5599 d = isl_id_alloc(ctx, "d", NULL);
5601 list = isl_id_list_alloc(ctx, 4);
5602 list = isl_id_list_add(list, b);
5603 list = isl_id_list_insert(list, 0, a);
5604 list = isl_id_list_add(list, c);
5605 list = isl_id_list_add(list, d);
5606 list = isl_id_list_drop(list, 1, 1);
5608 if (isl_id_list_n_id(list) != 3) {
5609 isl_id_list_free(list);
5610 isl_die(ctx, isl_error_unknown,
5611 "unexpected number of elements in list", return -1);
5614 id = isl_id_list_get_id(list, 0);
5615 ok = id == a;
5616 isl_id_free(id);
5617 id = isl_id_list_get_id(list, 1);
5618 ok = ok && id == c;
5619 isl_id_free(id);
5620 id = isl_id_list_get_id(list, 2);
5621 ok = ok && id == d;
5622 isl_id_free(id);
5624 isl_id_list_free(list);
5626 if (!ok)
5627 isl_die(ctx, isl_error_unknown,
5628 "unexpected elements in list", return -1);
5630 return 0;
5633 const char *set_conversion_tests[] = {
5634 "[N] -> { [i] : N - 1 <= 2 i <= N }",
5635 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
5636 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5637 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5638 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
5639 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
5640 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
5641 "-3 + c <= d <= 28 + c) }",
5644 /* Check that converting from isl_set to isl_pw_multi_aff and back
5645 * to isl_set produces the original isl_set.
5647 static int test_set_conversion(isl_ctx *ctx)
5649 int i;
5650 const char *str;
5651 isl_set *set1, *set2;
5652 isl_pw_multi_aff *pma;
5653 int equal;
5655 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
5656 str = set_conversion_tests[i];
5657 set1 = isl_set_read_from_str(ctx, str);
5658 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
5659 set2 = isl_set_from_pw_multi_aff(pma);
5660 equal = isl_set_is_equal(set1, set2);
5661 isl_set_free(set1);
5662 isl_set_free(set2);
5664 if (equal < 0)
5665 return -1;
5666 if (!equal)
5667 isl_die(ctx, isl_error_unknown, "bad conversion",
5668 return -1);
5671 return 0;
5674 const char *conversion_tests[] = {
5675 "{ [a, b, c, d] -> s0[a, b, e, f] : "
5676 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
5677 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
5678 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
5679 "9e <= -2 - 2a) }",
5680 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
5681 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
5682 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
5685 /* Check that converting from isl_map to isl_pw_multi_aff and back
5686 * to isl_map produces the original isl_map.
5688 static int test_map_conversion(isl_ctx *ctx)
5690 int i;
5691 isl_map *map1, *map2;
5692 isl_pw_multi_aff *pma;
5693 int equal;
5695 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
5696 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
5697 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
5698 map2 = isl_map_from_pw_multi_aff(pma);
5699 equal = isl_map_is_equal(map1, map2);
5700 isl_map_free(map1);
5701 isl_map_free(map2);
5703 if (equal < 0)
5704 return -1;
5705 if (!equal)
5706 isl_die(ctx, isl_error_unknown, "bad conversion",
5707 return -1);
5710 return 0;
5713 static int test_conversion(isl_ctx *ctx)
5715 if (test_set_conversion(ctx) < 0)
5716 return -1;
5717 if (test_map_conversion(ctx) < 0)
5718 return -1;
5719 return 0;
5722 /* Check that isl_basic_map_curry does not modify input.
5724 static int test_curry(isl_ctx *ctx)
5726 const char *str;
5727 isl_basic_map *bmap1, *bmap2;
5728 int equal;
5730 str = "{ [A[] -> B[]] -> C[] }";
5731 bmap1 = isl_basic_map_read_from_str(ctx, str);
5732 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
5733 equal = isl_basic_map_is_equal(bmap1, bmap2);
5734 isl_basic_map_free(bmap1);
5735 isl_basic_map_free(bmap2);
5737 if (equal < 0)
5738 return -1;
5739 if (equal)
5740 isl_die(ctx, isl_error_unknown,
5741 "curried map should not be equal to original",
5742 return -1);
5744 return 0;
5747 struct {
5748 const char *set;
5749 const char *ma;
5750 const char *res;
5751 } preimage_tests[] = {
5752 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
5753 "{ A[j,i] -> B[i,j] }",
5754 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
5755 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5756 "{ A[a,b] -> B[a/2,b/6] }",
5757 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
5758 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5759 "{ A[a,b] -> B[a/2,b/6] }",
5760 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
5761 "exists i,j : a = 2 i and b = 6 j }" },
5762 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
5763 "[n] -> { : 0 <= n <= 100 }" },
5764 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5765 "{ A[a] -> B[2a] }",
5766 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
5767 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5768 "{ A[a] -> B[([a/2])] }",
5769 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
5770 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
5771 "{ A[a] -> B[a,a,a/3] }",
5772 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
5773 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
5774 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
5777 static int test_preimage_basic_set(isl_ctx *ctx)
5779 int i;
5780 isl_basic_set *bset1, *bset2;
5781 isl_multi_aff *ma;
5782 int equal;
5784 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
5785 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
5786 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
5787 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
5788 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
5789 equal = isl_basic_set_is_equal(bset1, bset2);
5790 isl_basic_set_free(bset1);
5791 isl_basic_set_free(bset2);
5792 if (equal < 0)
5793 return -1;
5794 if (!equal)
5795 isl_die(ctx, isl_error_unknown, "bad preimage",
5796 return -1);
5799 return 0;
5802 struct {
5803 const char *map;
5804 const char *ma;
5805 const char *res;
5806 } preimage_domain_tests[] = {
5807 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
5808 "{ A[j,i] -> B[i,j] }",
5809 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
5810 { "{ B[i] -> C[i]; D[i] -> E[i] }",
5811 "{ A[i] -> B[i + 1] }",
5812 "{ A[i] -> C[i + 1] }" },
5813 { "{ B[i] -> C[i]; B[i] -> E[i] }",
5814 "{ A[i] -> B[i + 1] }",
5815 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
5816 { "{ B[i] -> C[([i/2])] }",
5817 "{ A[i] -> B[2i] }",
5818 "{ A[i] -> C[i] }" },
5819 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
5820 "{ A[i] -> B[([i/5]), ([i/7])] }",
5821 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
5822 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
5823 "[N] -> { A[] -> B[([N/5])] }",
5824 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
5825 { "{ B[i] -> C[i] : exists a : i = 5 a }",
5826 "{ A[i] -> B[2i] }",
5827 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
5828 { "{ B[i] -> C[i] : exists a : i = 2 a; "
5829 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
5830 "{ A[i] -> B[2i] }",
5831 "{ A[i] -> C[2i] }" },
5832 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
5833 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
5836 static int test_preimage_union_map(isl_ctx *ctx)
5838 int i;
5839 isl_union_map *umap1, *umap2;
5840 isl_multi_aff *ma;
5841 int equal;
5843 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
5844 umap1 = isl_union_map_read_from_str(ctx,
5845 preimage_domain_tests[i].map);
5846 ma = isl_multi_aff_read_from_str(ctx,
5847 preimage_domain_tests[i].ma);
5848 umap2 = isl_union_map_read_from_str(ctx,
5849 preimage_domain_tests[i].res);
5850 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
5851 equal = isl_union_map_is_equal(umap1, umap2);
5852 isl_union_map_free(umap1);
5853 isl_union_map_free(umap2);
5854 if (equal < 0)
5855 return -1;
5856 if (!equal)
5857 isl_die(ctx, isl_error_unknown, "bad preimage",
5858 return -1);
5861 return 0;
5864 static int test_preimage(isl_ctx *ctx)
5866 if (test_preimage_basic_set(ctx) < 0)
5867 return -1;
5868 if (test_preimage_union_map(ctx) < 0)
5869 return -1;
5871 return 0;
5874 struct {
5875 const char *ma1;
5876 const char *ma;
5877 const char *res;
5878 } pullback_tests[] = {
5879 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
5880 "{ A[a,b] -> C[b + 2a] }" },
5881 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
5882 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
5883 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
5884 "{ A[a] -> C[(a)/6] }" },
5885 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
5886 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
5887 "{ A[a] -> C[(2a)/3] }" },
5888 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
5889 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
5890 "{ A[i,j] -> C[i + j, i + j] }"},
5891 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
5892 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
5893 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
5894 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
5895 "{ [i, j] -> [floor((i)/3), j] }",
5896 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
5899 static int test_pullback(isl_ctx *ctx)
5901 int i;
5902 isl_multi_aff *ma1, *ma2;
5903 isl_multi_aff *ma;
5904 int equal;
5906 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
5907 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
5908 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
5909 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
5910 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
5911 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5912 isl_multi_aff_free(ma1);
5913 isl_multi_aff_free(ma2);
5914 if (equal < 0)
5915 return -1;
5916 if (!equal)
5917 isl_die(ctx, isl_error_unknown, "bad pullback",
5918 return -1);
5921 return 0;
5924 /* Check that negation is printed correctly and that equal expressions
5925 * are correctly identified.
5927 static int test_ast(isl_ctx *ctx)
5929 isl_ast_expr *expr, *expr1, *expr2, *expr3;
5930 char *str;
5931 int ok, equal;
5933 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5934 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5935 expr = isl_ast_expr_add(expr1, expr2);
5936 expr2 = isl_ast_expr_copy(expr);
5937 expr = isl_ast_expr_neg(expr);
5938 expr2 = isl_ast_expr_neg(expr2);
5939 equal = isl_ast_expr_is_equal(expr, expr2);
5940 str = isl_ast_expr_to_C_str(expr);
5941 ok = str ? !strcmp(str, "-(A + B)") : -1;
5942 free(str);
5943 isl_ast_expr_free(expr);
5944 isl_ast_expr_free(expr2);
5946 if (ok < 0 || equal < 0)
5947 return -1;
5948 if (!equal)
5949 isl_die(ctx, isl_error_unknown,
5950 "equal expressions not considered equal", return -1);
5951 if (!ok)
5952 isl_die(ctx, isl_error_unknown,
5953 "isl_ast_expr printed incorrectly", return -1);
5955 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5956 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5957 expr = isl_ast_expr_add(expr1, expr2);
5958 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
5959 expr = isl_ast_expr_sub(expr3, expr);
5960 str = isl_ast_expr_to_C_str(expr);
5961 ok = str ? !strcmp(str, "C - (A + B)") : -1;
5962 free(str);
5963 isl_ast_expr_free(expr);
5965 if (ok < 0)
5966 return -1;
5967 if (!ok)
5968 isl_die(ctx, isl_error_unknown,
5969 "isl_ast_expr printed incorrectly", return -1);
5971 return 0;
5974 /* Check that isl_ast_build_expr_from_set returns a valid expression
5975 * for an empty set. Note that isl_ast_build_expr_from_set getting
5976 * called on an empty set probably indicates a bug in the caller.
5978 static int test_ast_build(isl_ctx *ctx)
5980 isl_set *set;
5981 isl_ast_build *build;
5982 isl_ast_expr *expr;
5984 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5985 build = isl_ast_build_from_context(set);
5987 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
5988 expr = isl_ast_build_expr_from_set(build, set);
5990 isl_ast_expr_free(expr);
5991 isl_ast_build_free(build);
5993 if (!expr)
5994 return -1;
5996 return 0;
5999 /* Internal data structure for before_for and after_for callbacks.
6001 * depth is the current depth
6002 * before is the number of times before_for has been called
6003 * after is the number of times after_for has been called
6005 struct isl_test_codegen_data {
6006 int depth;
6007 int before;
6008 int after;
6011 /* This function is called before each for loop in the AST generated
6012 * from test_ast_gen1.
6014 * Increment the number of calls and the depth.
6015 * Check that the space returned by isl_ast_build_get_schedule_space
6016 * matches the target space of the schedule returned by
6017 * isl_ast_build_get_schedule.
6018 * Return an isl_id that is checked by the corresponding call
6019 * to after_for.
6021 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
6022 void *user)
6024 struct isl_test_codegen_data *data = user;
6025 isl_ctx *ctx;
6026 isl_space *space;
6027 isl_union_map *schedule;
6028 isl_union_set *uset;
6029 isl_set *set;
6030 int empty;
6031 char name[] = "d0";
6033 ctx = isl_ast_build_get_ctx(build);
6035 if (data->before >= 3)
6036 isl_die(ctx, isl_error_unknown,
6037 "unexpected number of for nodes", return NULL);
6038 if (data->depth >= 2)
6039 isl_die(ctx, isl_error_unknown,
6040 "unexpected depth", return NULL);
6042 snprintf(name, sizeof(name), "d%d", data->depth);
6043 data->before++;
6044 data->depth++;
6046 schedule = isl_ast_build_get_schedule(build);
6047 uset = isl_union_map_range(schedule);
6048 if (!uset)
6049 return NULL;
6050 if (isl_union_set_n_set(uset) != 1) {
6051 isl_union_set_free(uset);
6052 isl_die(ctx, isl_error_unknown,
6053 "expecting single range space", return NULL);
6056 space = isl_ast_build_get_schedule_space(build);
6057 set = isl_union_set_extract_set(uset, space);
6058 isl_union_set_free(uset);
6059 empty = isl_set_is_empty(set);
6060 isl_set_free(set);
6062 if (empty < 0)
6063 return NULL;
6064 if (empty)
6065 isl_die(ctx, isl_error_unknown,
6066 "spaces don't match", return NULL);
6068 return isl_id_alloc(ctx, name, NULL);
6071 /* This function is called after each for loop in the AST generated
6072 * from test_ast_gen1.
6074 * Increment the number of calls and decrement the depth.
6075 * Check that the annotation attached to the node matches
6076 * the isl_id returned by the corresponding call to before_for.
6078 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
6079 __isl_keep isl_ast_build *build, void *user)
6081 struct isl_test_codegen_data *data = user;
6082 isl_id *id;
6083 const char *name;
6084 int valid;
6086 data->after++;
6087 data->depth--;
6089 if (data->after > data->before)
6090 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
6091 "mismatch in number of for nodes",
6092 return isl_ast_node_free(node));
6094 id = isl_ast_node_get_annotation(node);
6095 if (!id)
6096 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
6097 "missing annotation", return isl_ast_node_free(node));
6099 name = isl_id_get_name(id);
6100 valid = name && atoi(name + 1) == data->depth;
6101 isl_id_free(id);
6103 if (!valid)
6104 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
6105 "wrong annotation", return isl_ast_node_free(node));
6107 return node;
6110 /* Check that the before_each_for and after_each_for callbacks
6111 * are called for each for loop in the generated code,
6112 * that they are called in the right order and that the isl_id
6113 * returned from the before_each_for callback is attached to
6114 * the isl_ast_node passed to the corresponding after_each_for call.
6116 static int test_ast_gen1(isl_ctx *ctx)
6118 const char *str;
6119 isl_set *set;
6120 isl_union_map *schedule;
6121 isl_ast_build *build;
6122 isl_ast_node *tree;
6123 struct isl_test_codegen_data data;
6125 str = "[N] -> { : N >= 10 }";
6126 set = isl_set_read_from_str(ctx, str);
6127 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
6128 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
6129 schedule = isl_union_map_read_from_str(ctx, str);
6131 data.before = 0;
6132 data.after = 0;
6133 data.depth = 0;
6134 build = isl_ast_build_from_context(set);
6135 build = isl_ast_build_set_before_each_for(build,
6136 &before_for, &data);
6137 build = isl_ast_build_set_after_each_for(build,
6138 &after_for, &data);
6139 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6140 isl_ast_build_free(build);
6141 if (!tree)
6142 return -1;
6144 isl_ast_node_free(tree);
6146 if (data.before != 3 || data.after != 3)
6147 isl_die(ctx, isl_error_unknown,
6148 "unexpected number of for nodes", return -1);
6150 return 0;
6153 /* Check that the AST generator handles domains that are integrally disjoint
6154 * but not rationally disjoint.
6156 static int test_ast_gen2(isl_ctx *ctx)
6158 const char *str;
6159 isl_set *set;
6160 isl_union_map *schedule;
6161 isl_union_map *options;
6162 isl_ast_build *build;
6163 isl_ast_node *tree;
6165 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
6166 schedule = isl_union_map_read_from_str(ctx, str);
6167 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6168 build = isl_ast_build_from_context(set);
6170 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
6171 options = isl_union_map_read_from_str(ctx, str);
6172 build = isl_ast_build_set_options(build, options);
6173 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6174 isl_ast_build_free(build);
6175 if (!tree)
6176 return -1;
6177 isl_ast_node_free(tree);
6179 return 0;
6182 /* Increment *user on each call.
6184 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
6185 __isl_keep isl_ast_build *build, void *user)
6187 int *n = user;
6189 (*n)++;
6191 return node;
6194 /* Test that unrolling tries to minimize the number of instances.
6195 * In particular, for the schedule given below, make sure it generates
6196 * 3 nodes (rather than 101).
6198 static int test_ast_gen3(isl_ctx *ctx)
6200 const char *str;
6201 isl_set *set;
6202 isl_union_map *schedule;
6203 isl_union_map *options;
6204 isl_ast_build *build;
6205 isl_ast_node *tree;
6206 int n_domain = 0;
6208 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
6209 schedule = isl_union_map_read_from_str(ctx, str);
6210 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6212 str = "{ [i] -> unroll[0] }";
6213 options = isl_union_map_read_from_str(ctx, str);
6215 build = isl_ast_build_from_context(set);
6216 build = isl_ast_build_set_options(build, options);
6217 build = isl_ast_build_set_at_each_domain(build,
6218 &count_domains, &n_domain);
6219 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6220 isl_ast_build_free(build);
6221 if (!tree)
6222 return -1;
6224 isl_ast_node_free(tree);
6226 if (n_domain != 3)
6227 isl_die(ctx, isl_error_unknown,
6228 "unexpected number of for nodes", return -1);
6230 return 0;
6233 /* Check that if the ast_build_exploit_nested_bounds options is set,
6234 * we do not get an outer if node in the generated AST,
6235 * while we do get such an outer if node if the options is not set.
6237 static int test_ast_gen4(isl_ctx *ctx)
6239 const char *str;
6240 isl_set *set;
6241 isl_union_map *schedule;
6242 isl_ast_build *build;
6243 isl_ast_node *tree;
6244 enum isl_ast_node_type type;
6245 int enb;
6247 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
6248 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
6250 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
6252 schedule = isl_union_map_read_from_str(ctx, str);
6253 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6254 build = isl_ast_build_from_context(set);
6255 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6256 isl_ast_build_free(build);
6257 if (!tree)
6258 return -1;
6260 type = isl_ast_node_get_type(tree);
6261 isl_ast_node_free(tree);
6263 if (type == isl_ast_node_if)
6264 isl_die(ctx, isl_error_unknown,
6265 "not expecting if node", return -1);
6267 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
6269 schedule = isl_union_map_read_from_str(ctx, str);
6270 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6271 build = isl_ast_build_from_context(set);
6272 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6273 isl_ast_build_free(build);
6274 if (!tree)
6275 return -1;
6277 type = isl_ast_node_get_type(tree);
6278 isl_ast_node_free(tree);
6280 if (type != isl_ast_node_if)
6281 isl_die(ctx, isl_error_unknown,
6282 "expecting if node", return -1);
6284 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
6286 return 0;
6289 /* This function is called for each leaf in the AST generated
6290 * from test_ast_gen5.
6292 * We finalize the AST generation by extending the outer schedule
6293 * with a zero-dimensional schedule. If this results in any for loops,
6294 * then this means that we did not pass along enough information
6295 * about the outer schedule to the inner AST generation.
6297 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
6298 void *user)
6300 isl_union_map *schedule, *extra;
6301 isl_ast_node *tree;
6303 schedule = isl_ast_build_get_schedule(build);
6304 extra = isl_union_map_copy(schedule);
6305 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
6306 schedule = isl_union_map_range_product(schedule, extra);
6307 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6308 isl_ast_build_free(build);
6310 if (!tree)
6311 return NULL;
6313 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
6314 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
6315 "code should not contain any for loop",
6316 return isl_ast_node_free(tree));
6318 return tree;
6321 /* Check that we do not lose any information when going back and
6322 * forth between internal and external schedule.
6324 * In particular, we create an AST where we unroll the only
6325 * non-constant dimension in the schedule. We therefore do
6326 * not expect any for loops in the AST. However, older versions
6327 * of isl would not pass along enough information about the outer
6328 * schedule when performing an inner code generation from a create_leaf
6329 * callback, resulting in the inner code generation producing a for loop.
6331 static int test_ast_gen5(isl_ctx *ctx)
6333 const char *str;
6334 isl_set *set;
6335 isl_union_map *schedule, *options;
6336 isl_ast_build *build;
6337 isl_ast_node *tree;
6339 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
6340 schedule = isl_union_map_read_from_str(ctx, str);
6342 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
6343 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
6344 options = isl_union_map_read_from_str(ctx, str);
6346 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6347 build = isl_ast_build_from_context(set);
6348 build = isl_ast_build_set_options(build, options);
6349 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
6350 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6351 isl_ast_build_free(build);
6352 isl_ast_node_free(tree);
6353 if (!tree)
6354 return -1;
6356 return 0;
6359 /* Check that the expression
6361 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
6363 * is not combined into
6365 * min(n/2, 0)
6367 * as this would result in n/2 being evaluated in parts of
6368 * the definition domain where n is not a multiple of 2.
6370 static int test_ast_expr(isl_ctx *ctx)
6372 const char *str;
6373 isl_pw_aff *pa;
6374 isl_ast_build *build;
6375 isl_ast_expr *expr;
6376 int min_max;
6377 int is_min;
6379 min_max = isl_options_get_ast_build_detect_min_max(ctx);
6380 isl_options_set_ast_build_detect_min_max(ctx, 1);
6382 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
6383 pa = isl_pw_aff_read_from_str(ctx, str);
6384 build = isl_ast_build_alloc(ctx);
6385 expr = isl_ast_build_expr_from_pw_aff(build, pa);
6386 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
6387 isl_ast_expr_get_op_type(expr) == isl_ast_op_min;
6388 isl_ast_build_free(build);
6389 isl_ast_expr_free(expr);
6391 isl_options_set_ast_build_detect_min_max(ctx, min_max);
6393 if (!expr)
6394 return -1;
6395 if (is_min)
6396 isl_die(ctx, isl_error_unknown,
6397 "expressions should not be combined", return -1);
6399 return 0;
6402 static int test_ast_gen(isl_ctx *ctx)
6404 if (test_ast_gen1(ctx) < 0)
6405 return -1;
6406 if (test_ast_gen2(ctx) < 0)
6407 return -1;
6408 if (test_ast_gen3(ctx) < 0)
6409 return -1;
6410 if (test_ast_gen4(ctx) < 0)
6411 return -1;
6412 if (test_ast_gen5(ctx) < 0)
6413 return -1;
6414 if (test_ast_expr(ctx) < 0)
6415 return -1;
6416 return 0;
6419 /* Check if dropping output dimensions from an isl_pw_multi_aff
6420 * works properly.
6422 static int test_pw_multi_aff(isl_ctx *ctx)
6424 const char *str;
6425 isl_pw_multi_aff *pma1, *pma2;
6426 int equal;
6428 str = "{ [i,j] -> [i+j, 4i-j] }";
6429 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
6430 str = "{ [i,j] -> [4i-j] }";
6431 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6433 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
6435 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6437 isl_pw_multi_aff_free(pma1);
6438 isl_pw_multi_aff_free(pma2);
6439 if (equal < 0)
6440 return -1;
6441 if (!equal)
6442 isl_die(ctx, isl_error_unknown,
6443 "expressions not equal", return -1);
6445 return 0;
6448 /* Check that we can properly parse multi piecewise affine expressions
6449 * where the piecewise affine expressions have different domains.
6451 static int test_multi_pw_aff(isl_ctx *ctx)
6453 const char *str;
6454 isl_set *dom, *dom2;
6455 isl_multi_pw_aff *mpa1, *mpa2;
6456 isl_pw_aff *pa;
6457 int equal;
6458 int equal_domain;
6460 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
6461 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
6462 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
6463 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
6464 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
6465 str = "{ [i] -> [(i : i > 0), 2i] }";
6466 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
6468 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
6470 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
6471 dom = isl_pw_aff_domain(pa);
6472 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
6473 dom2 = isl_pw_aff_domain(pa);
6474 equal_domain = isl_set_is_equal(dom, dom2);
6476 isl_set_free(dom);
6477 isl_set_free(dom2);
6478 isl_multi_pw_aff_free(mpa1);
6479 isl_multi_pw_aff_free(mpa2);
6481 if (equal < 0)
6482 return -1;
6483 if (!equal)
6484 isl_die(ctx, isl_error_unknown,
6485 "expressions not equal", return -1);
6487 if (equal_domain < 0)
6488 return -1;
6489 if (equal_domain)
6490 isl_die(ctx, isl_error_unknown,
6491 "domains unexpectedly equal", return -1);
6493 return 0;
6496 /* This is a regression test for a bug where isl_basic_map_simplify
6497 * would end up in an infinite loop. In particular, we construct
6498 * an empty basic set that is not obviously empty.
6499 * isl_basic_set_is_empty marks the basic set as empty.
6500 * After projecting out i3, the variable can be dropped completely,
6501 * but isl_basic_map_simplify refrains from doing so if the basic set
6502 * is empty and would end up in an infinite loop if it didn't test
6503 * explicitly for empty basic maps in the outer loop.
6505 static int test_simplify_1(isl_ctx *ctx)
6507 const char *str;
6508 isl_basic_set *bset;
6509 int empty;
6511 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
6512 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
6513 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
6514 "i3 >= i2 }";
6515 bset = isl_basic_set_read_from_str(ctx, str);
6516 empty = isl_basic_set_is_empty(bset);
6517 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
6518 isl_basic_set_free(bset);
6519 if (!bset)
6520 return -1;
6521 if (!empty)
6522 isl_die(ctx, isl_error_unknown,
6523 "basic set should be empty", return -1);
6525 return 0;
6528 /* Check that the equality in the set description below
6529 * is simplified away.
6531 static int test_simplify_2(isl_ctx *ctx)
6533 const char *str;
6534 isl_basic_set *bset;
6535 isl_bool universe;
6537 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
6538 bset = isl_basic_set_read_from_str(ctx, str);
6539 universe = isl_basic_set_plain_is_universe(bset);
6540 isl_basic_set_free(bset);
6542 if (universe < 0)
6543 return -1;
6544 if (!universe)
6545 isl_die(ctx, isl_error_unknown,
6546 "equality not simplified away", return -1);
6547 return 0;
6550 /* Some simplification tests.
6552 static int test_simplify(isl_ctx *ctx)
6554 if (test_simplify_1(ctx) < 0)
6555 return -1;
6556 if (test_simplify_2(ctx) < 0)
6557 return -1;
6558 return 0;
6561 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
6562 * with gbr context would fail to disable the use of the shifted tableau
6563 * when transferring equalities for the input to the context, resulting
6564 * in invalid sample values.
6566 static int test_partial_lexmin(isl_ctx *ctx)
6568 const char *str;
6569 isl_basic_set *bset;
6570 isl_basic_map *bmap;
6571 isl_map *map;
6573 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
6574 bmap = isl_basic_map_read_from_str(ctx, str);
6575 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
6576 bset = isl_basic_set_read_from_str(ctx, str);
6577 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
6578 isl_map_free(map);
6580 if (!map)
6581 return -1;
6583 return 0;
6586 /* Check that the variable compression performed on the existentially
6587 * quantified variables inside isl_basic_set_compute_divs is not confused
6588 * by the implicit equalities among the parameters.
6590 static int test_compute_divs(isl_ctx *ctx)
6592 const char *str;
6593 isl_basic_set *bset;
6594 isl_set *set;
6596 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
6597 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
6598 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
6599 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
6600 bset = isl_basic_set_read_from_str(ctx, str);
6601 set = isl_basic_set_compute_divs(bset);
6602 isl_set_free(set);
6603 if (!set)
6604 return -1;
6606 return 0;
6609 /* Check that the reaching domain elements and the prefix schedule
6610 * at a leaf node are the same before and after grouping.
6612 static int test_schedule_tree_group_1(isl_ctx *ctx)
6614 int equal;
6615 const char *str;
6616 isl_id *id;
6617 isl_union_set *uset;
6618 isl_multi_union_pw_aff *mupa;
6619 isl_union_pw_multi_aff *upma1, *upma2;
6620 isl_union_set *domain1, *domain2;
6621 isl_union_map *umap1, *umap2;
6622 isl_schedule_node *node;
6624 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
6625 uset = isl_union_set_read_from_str(ctx, str);
6626 node = isl_schedule_node_from_domain(uset);
6627 node = isl_schedule_node_child(node, 0);
6628 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
6629 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6630 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6631 node = isl_schedule_node_child(node, 0);
6632 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
6633 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6634 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6635 node = isl_schedule_node_child(node, 0);
6636 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
6637 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
6638 domain1 = isl_schedule_node_get_domain(node);
6639 id = isl_id_alloc(ctx, "group", NULL);
6640 node = isl_schedule_node_parent(node);
6641 node = isl_schedule_node_group(node, id);
6642 node = isl_schedule_node_child(node, 0);
6643 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
6644 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
6645 domain2 = isl_schedule_node_get_domain(node);
6646 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
6647 if (equal >= 0 && equal)
6648 equal = isl_union_set_is_equal(domain1, domain2);
6649 if (equal >= 0 && equal)
6650 equal = isl_union_map_is_equal(umap1, umap2);
6651 isl_union_map_free(umap1);
6652 isl_union_map_free(umap2);
6653 isl_union_set_free(domain1);
6654 isl_union_set_free(domain2);
6655 isl_union_pw_multi_aff_free(upma1);
6656 isl_union_pw_multi_aff_free(upma2);
6657 isl_schedule_node_free(node);
6659 if (equal < 0)
6660 return -1;
6661 if (!equal)
6662 isl_die(ctx, isl_error_unknown,
6663 "expressions not equal", return -1);
6665 return 0;
6668 /* Check that we can have nested groupings and that the union map
6669 * schedule representation is the same before and after the grouping.
6670 * Note that after the grouping, the union map representation contains
6671 * the domain constraints from the ranges of the expansion nodes,
6672 * while they are missing from the union map representation of
6673 * the tree without expansion nodes.
6675 * Also check that the global expansion is as expected.
6677 static int test_schedule_tree_group_2(isl_ctx *ctx)
6679 int equal, equal_expansion;
6680 const char *str;
6681 isl_id *id;
6682 isl_union_set *uset;
6683 isl_union_map *umap1, *umap2;
6684 isl_union_map *expansion1, *expansion2;
6685 isl_union_set_list *filters;
6686 isl_multi_union_pw_aff *mupa;
6687 isl_schedule *schedule;
6688 isl_schedule_node *node;
6690 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
6691 "S3[i,j] : 0 <= i,j < 10 }";
6692 uset = isl_union_set_read_from_str(ctx, str);
6693 node = isl_schedule_node_from_domain(uset);
6694 node = isl_schedule_node_child(node, 0);
6695 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
6696 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6697 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6698 node = isl_schedule_node_child(node, 0);
6699 str = "{ S1[i,j] }";
6700 uset = isl_union_set_read_from_str(ctx, str);
6701 filters = isl_union_set_list_from_union_set(uset);
6702 str = "{ S2[i,j]; S3[i,j] }";
6703 uset = isl_union_set_read_from_str(ctx, str);
6704 filters = isl_union_set_list_add(filters, uset);
6705 node = isl_schedule_node_insert_sequence(node, filters);
6706 node = isl_schedule_node_child(node, 1);
6707 node = isl_schedule_node_child(node, 0);
6708 str = "{ S2[i,j] }";
6709 uset = isl_union_set_read_from_str(ctx, str);
6710 filters = isl_union_set_list_from_union_set(uset);
6711 str = "{ S3[i,j] }";
6712 uset = isl_union_set_read_from_str(ctx, str);
6713 filters = isl_union_set_list_add(filters, uset);
6714 node = isl_schedule_node_insert_sequence(node, filters);
6716 schedule = isl_schedule_node_get_schedule(node);
6717 umap1 = isl_schedule_get_map(schedule);
6718 uset = isl_schedule_get_domain(schedule);
6719 umap1 = isl_union_map_intersect_domain(umap1, uset);
6720 isl_schedule_free(schedule);
6722 node = isl_schedule_node_parent(node);
6723 node = isl_schedule_node_parent(node);
6724 id = isl_id_alloc(ctx, "group1", NULL);
6725 node = isl_schedule_node_group(node, id);
6726 node = isl_schedule_node_child(node, 1);
6727 node = isl_schedule_node_child(node, 0);
6728 id = isl_id_alloc(ctx, "group2", NULL);
6729 node = isl_schedule_node_group(node, id);
6731 schedule = isl_schedule_node_get_schedule(node);
6732 umap2 = isl_schedule_get_map(schedule);
6733 isl_schedule_free(schedule);
6735 node = isl_schedule_node_root(node);
6736 node = isl_schedule_node_child(node, 0);
6737 expansion1 = isl_schedule_node_get_subtree_expansion(node);
6738 isl_schedule_node_free(node);
6740 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
6741 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
6742 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
6744 expansion2 = isl_union_map_read_from_str(ctx, str);
6746 equal = isl_union_map_is_equal(umap1, umap2);
6747 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
6749 isl_union_map_free(umap1);
6750 isl_union_map_free(umap2);
6751 isl_union_map_free(expansion1);
6752 isl_union_map_free(expansion2);
6754 if (equal < 0 || equal_expansion < 0)
6755 return -1;
6756 if (!equal)
6757 isl_die(ctx, isl_error_unknown,
6758 "expressions not equal", return -1);
6759 if (!equal_expansion)
6760 isl_die(ctx, isl_error_unknown,
6761 "unexpected expansion", return -1);
6763 return 0;
6766 /* Some tests for the isl_schedule_node_group function.
6768 static int test_schedule_tree_group(isl_ctx *ctx)
6770 if (test_schedule_tree_group_1(ctx) < 0)
6771 return -1;
6772 if (test_schedule_tree_group_2(ctx) < 0)
6773 return -1;
6774 return 0;
6777 struct {
6778 const char *set;
6779 const char *dual;
6780 } coef_tests[] = {
6781 { "{ rat: [i] : 0 <= i <= 10 }",
6782 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
6783 { "{ rat: [i] : FALSE }",
6784 "{ rat: coefficients[[cst] -> [a]] }" },
6785 { "{ rat: [i] : }",
6786 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
6789 struct {
6790 const char *set;
6791 const char *dual;
6792 } sol_tests[] = {
6793 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
6794 "{ rat: [i] : 0 <= i <= 10 }" },
6795 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
6796 "{ rat: [i] }" },
6797 { "{ rat: coefficients[[cst] -> [a]] }",
6798 "{ rat: [i] : FALSE }" },
6801 /* Test the basic functionality of isl_basic_set_coefficients and
6802 * isl_basic_set_solutions.
6804 static int test_dual(isl_ctx *ctx)
6806 int i;
6808 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
6809 int equal;
6810 isl_basic_set *bset1, *bset2;
6812 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
6813 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
6814 bset1 = isl_basic_set_coefficients(bset1);
6815 equal = isl_basic_set_is_equal(bset1, bset2);
6816 isl_basic_set_free(bset1);
6817 isl_basic_set_free(bset2);
6818 if (equal < 0)
6819 return -1;
6820 if (!equal)
6821 isl_die(ctx, isl_error_unknown,
6822 "incorrect dual", return -1);
6825 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
6826 int equal;
6827 isl_basic_set *bset1, *bset2;
6829 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
6830 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
6831 bset1 = isl_basic_set_solutions(bset1);
6832 equal = isl_basic_set_is_equal(bset1, bset2);
6833 isl_basic_set_free(bset1);
6834 isl_basic_set_free(bset2);
6835 if (equal < 0)
6836 return -1;
6837 if (!equal)
6838 isl_die(ctx, isl_error_unknown,
6839 "incorrect dual", return -1);
6842 return 0;
6845 struct {
6846 int scale_tile;
6847 int shift_point;
6848 const char *domain;
6849 const char *schedule;
6850 const char *sizes;
6851 const char *tile;
6852 const char *point;
6853 } tile_tests[] = {
6854 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6855 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6856 "{ [32,32] }",
6857 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6858 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6860 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6861 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6862 "{ [32,32] }",
6863 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6864 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6866 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6867 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6868 "{ [32,32] }",
6869 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6870 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6872 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6873 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6874 "{ [32,32] }",
6875 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6876 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6880 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
6881 * tile the band and then check if the tile and point bands have the
6882 * expected partial schedule.
6884 static int test_tile(isl_ctx *ctx)
6886 int i;
6887 int scale;
6888 int shift;
6890 scale = isl_options_get_tile_scale_tile_loops(ctx);
6891 shift = isl_options_get_tile_shift_point_loops(ctx);
6893 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
6894 int opt;
6895 int equal;
6896 const char *str;
6897 isl_union_set *domain;
6898 isl_multi_union_pw_aff *mupa, *mupa2;
6899 isl_schedule_node *node;
6900 isl_multi_val *sizes;
6902 opt = tile_tests[i].scale_tile;
6903 isl_options_set_tile_scale_tile_loops(ctx, opt);
6904 opt = tile_tests[i].shift_point;
6905 isl_options_set_tile_shift_point_loops(ctx, opt);
6907 str = tile_tests[i].domain;
6908 domain = isl_union_set_read_from_str(ctx, str);
6909 node = isl_schedule_node_from_domain(domain);
6910 node = isl_schedule_node_child(node, 0);
6911 str = tile_tests[i].schedule;
6912 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6913 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6914 str = tile_tests[i].sizes;
6915 sizes = isl_multi_val_read_from_str(ctx, str);
6916 node = isl_schedule_node_band_tile(node, sizes);
6918 str = tile_tests[i].tile;
6919 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6920 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6921 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
6922 isl_multi_union_pw_aff_free(mupa);
6923 isl_multi_union_pw_aff_free(mupa2);
6925 node = isl_schedule_node_child(node, 0);
6927 str = tile_tests[i].point;
6928 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6929 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6930 if (equal >= 0 && equal)
6931 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
6932 mupa2);
6933 isl_multi_union_pw_aff_free(mupa);
6934 isl_multi_union_pw_aff_free(mupa2);
6936 isl_schedule_node_free(node);
6938 if (equal < 0)
6939 return -1;
6940 if (!equal)
6941 isl_die(ctx, isl_error_unknown,
6942 "unexpected result", return -1);
6945 isl_options_set_tile_scale_tile_loops(ctx, scale);
6946 isl_options_set_tile_shift_point_loops(ctx, shift);
6948 return 0;
6951 /* Check that the domain hash of a space is equal to the hash
6952 * of the domain of the space.
6954 static int test_domain_hash(isl_ctx *ctx)
6956 isl_map *map;
6957 isl_space *space;
6958 uint32_t hash1, hash2;
6960 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
6961 space = isl_map_get_space(map);
6962 isl_map_free(map);
6963 hash1 = isl_space_get_domain_hash(space);
6964 space = isl_space_domain(space);
6965 hash2 = isl_space_get_hash(space);
6966 isl_space_free(space);
6968 if (!space)
6969 return -1;
6970 if (hash1 != hash2)
6971 isl_die(ctx, isl_error_unknown,
6972 "domain hash not equal to hash of domain", return -1);
6974 return 0;
6977 /* Check that a universe basic set that is not obviously equal to the universe
6978 * is still recognized as being equal to the universe.
6980 static int test_universe(isl_ctx *ctx)
6982 const char *s;
6983 isl_basic_set *bset;
6984 isl_bool is_univ;
6986 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
6987 bset = isl_basic_set_read_from_str(ctx, s);
6988 is_univ = isl_basic_set_is_universe(bset);
6989 isl_basic_set_free(bset);
6991 if (is_univ < 0)
6992 return -1;
6993 if (!is_univ)
6994 isl_die(ctx, isl_error_unknown,
6995 "not recognized as universe set", return -1);
6997 return 0;
7000 /* Sets for which chambers are computed and checked.
7002 const char *chambers_tests[] = {
7003 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
7004 "z >= 0 and z <= C - y and z <= B - x - y }",
7007 /* Add the domain of "cell" to "cells".
7009 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
7011 isl_basic_set_list **cells = user;
7012 isl_basic_set *dom;
7014 dom = isl_cell_get_domain(cell);
7015 isl_cell_free(cell);
7016 *cells = isl_basic_set_list_add(*cells, dom);
7018 return *cells ? isl_stat_ok : isl_stat_error;
7021 /* Check that the elements of "list" are pairwise disjoint.
7023 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
7025 int i, j, n;
7027 if (!list)
7028 return isl_stat_error;
7030 n = isl_basic_set_list_n_basic_set(list);
7031 for (i = 0; i < n; ++i) {
7032 isl_basic_set *bset_i;
7034 bset_i = isl_basic_set_list_get_basic_set(list, i);
7035 for (j = i + 1; j < n; ++j) {
7036 isl_basic_set *bset_j;
7037 isl_bool disjoint;
7039 bset_j = isl_basic_set_list_get_basic_set(list, j);
7040 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
7041 isl_basic_set_free(bset_j);
7042 if (!disjoint)
7043 isl_die(isl_basic_set_list_get_ctx(list),
7044 isl_error_unknown, "not disjoint",
7045 break);
7046 if (disjoint < 0 || !disjoint)
7047 break;
7049 isl_basic_set_free(bset_i);
7050 if (j < n)
7051 return isl_stat_error;
7054 return isl_stat_ok;
7057 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
7058 * are pairwise disjoint.
7060 static int test_chambers(isl_ctx *ctx)
7062 int i;
7064 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
7065 isl_basic_set *bset;
7066 isl_vertices *vertices;
7067 isl_basic_set_list *cells;
7068 isl_stat ok;
7070 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
7071 vertices = isl_basic_set_compute_vertices(bset);
7072 cells = isl_basic_set_list_alloc(ctx, 0);
7073 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
7074 &cells) < 0)
7075 cells = isl_basic_set_list_free(cells);
7076 ok = check_pairwise_disjoint(cells);
7077 isl_basic_set_list_free(cells);
7078 isl_vertices_free(vertices);
7079 isl_basic_set_free(bset);
7081 if (ok < 0)
7082 return -1;
7085 return 0;
7088 struct {
7089 const char *name;
7090 int (*fn)(isl_ctx *ctx);
7091 } tests [] = {
7092 { "universe", &test_universe },
7093 { "domain hash", &test_domain_hash },
7094 { "dual", &test_dual },
7095 { "dependence analysis", &test_flow },
7096 { "val", &test_val },
7097 { "compute divs", &test_compute_divs },
7098 { "partial lexmin", &test_partial_lexmin },
7099 { "simplify", &test_simplify },
7100 { "curry", &test_curry },
7101 { "piecewise multi affine expressions", &test_pw_multi_aff },
7102 { "multi piecewise affine expressions", &test_multi_pw_aff },
7103 { "conversion", &test_conversion },
7104 { "list", &test_list },
7105 { "align parameters", &test_align_parameters },
7106 { "preimage", &test_preimage },
7107 { "pullback", &test_pullback },
7108 { "AST", &test_ast },
7109 { "AST build", &test_ast_build },
7110 { "AST generation", &test_ast_gen },
7111 { "eliminate", &test_eliminate },
7112 { "residue class", &test_residue_class },
7113 { "div", &test_div },
7114 { "slice", &test_slice },
7115 { "fixed power", &test_fixed_power },
7116 { "sample", &test_sample },
7117 { "output", &test_output },
7118 { "vertices", &test_vertices },
7119 { "chambers", &test_chambers },
7120 { "fixed", &test_fixed },
7121 { "equal", &test_equal },
7122 { "disjoint", &test_disjoint },
7123 { "product", &test_product },
7124 { "dim_max", &test_dim_max },
7125 { "affine", &test_aff },
7126 { "injective", &test_injective },
7127 { "schedule (whole component)", &test_schedule_whole },
7128 { "schedule (incremental)", &test_schedule_incremental },
7129 { "schedule tree grouping", &test_schedule_tree_group },
7130 { "tile", &test_tile },
7131 { "union_pw", &test_union_pw },
7132 { "eval", &test_eval },
7133 { "parse", &test_parse },
7134 { "single-valued", &test_sv },
7135 { "affine hull", &test_affine_hull },
7136 { "simple_hull", &test_simple_hull },
7137 { "coalesce", &test_coalesce },
7138 { "factorize", &test_factorize },
7139 { "subset", &test_subset },
7140 { "subtract", &test_subtract },
7141 { "intersect", &test_intersect },
7142 { "lexmin", &test_lexmin },
7143 { "min", &test_min },
7144 { "gist", &test_gist },
7145 { "piecewise quasi-polynomials", &test_pwqp },
7146 { "lift", &test_lift },
7147 { "bound", &test_bound },
7148 { "union", &test_union },
7149 { "split periods", &test_split_periods },
7150 { "lexicographic order", &test_lex },
7151 { "bijectivity", &test_bijective },
7152 { "dataflow analysis", &test_dep },
7153 { "reading", &test_read },
7154 { "bounded", &test_bounded },
7155 { "construction", &test_construction },
7156 { "dimension manipulation", &test_dim },
7157 { "map application", &test_application },
7158 { "convex hull", &test_convex_hull },
7159 { "transitive closure", &test_closure },
7162 int main(int argc, char **argv)
7164 int i;
7165 struct isl_ctx *ctx;
7166 struct isl_options *options;
7168 options = isl_options_new_with_defaults();
7169 assert(options);
7170 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
7172 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
7173 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
7174 printf("%s\n", tests[i].name);
7175 if (tests[i].fn(ctx) < 0)
7176 goto error;
7178 isl_ctx_free(ctx);
7179 return 0;
7180 error:
7181 isl_ctx_free(ctx);
7182 return -1;