isl_multi_union_pw_aff_zero: check that input is not a parameter space
[isl.git] / isl_test.c
bloba3285ca74b0e1e403fda9aeec7f86c04558b7ee9
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 }" },
193 { "{ [x] -> [x] : }",
194 "{ [x] -> [x] }" },
197 int test_parse(struct isl_ctx *ctx)
199 int i;
200 isl_map *map, *map2;
201 const char *str, *str2;
203 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
204 return -1;
205 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
206 return -1;
207 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
208 return -1;
210 str = "{ [i] -> [-i] }";
211 map = isl_map_read_from_str(ctx, str);
212 assert(map);
213 isl_map_free(map);
215 str = "{ A[i] -> L[([i/3])] }";
216 map = isl_map_read_from_str(ctx, str);
217 assert(map);
218 isl_map_free(map);
220 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
221 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
222 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
224 for (i = 0; i < ARRAY_SIZE(parse_map_equal_tests); ++i) {
225 str = parse_map_equal_tests[i].map1;
226 str2 = parse_map_equal_tests[i].map2;
227 if (test_parse_map_equal(ctx, str, str2) < 0)
228 return -1;
231 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
232 map = isl_map_read_from_str(ctx, str);
233 str = "{ [new, old] -> [o0, o1] : "
234 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
235 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
236 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
237 map2 = isl_map_read_from_str(ctx, str);
238 assert(isl_map_is_equal(map, map2));
239 isl_map_free(map);
240 isl_map_free(map2);
242 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
243 map = isl_map_read_from_str(ctx, str);
244 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
245 map2 = isl_map_read_from_str(ctx, str);
246 assert(isl_map_is_equal(map, map2));
247 isl_map_free(map);
248 isl_map_free(map2);
250 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
251 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
252 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
253 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
254 test_parse_pwaff(ctx, "{ [] -> [(100)] }");
256 return 0;
259 static int test_read(isl_ctx *ctx)
261 char *filename;
262 FILE *input;
263 isl_basic_set *bset1, *bset2;
264 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
265 int equal;
267 filename = get_filename(ctx, "set", "omega");
268 assert(filename);
269 input = fopen(filename, "r");
270 assert(input);
272 bset1 = isl_basic_set_read_from_file(ctx, input);
273 bset2 = isl_basic_set_read_from_str(ctx, str);
275 equal = isl_basic_set_is_equal(bset1, bset2);
277 isl_basic_set_free(bset1);
278 isl_basic_set_free(bset2);
279 free(filename);
281 fclose(input);
283 if (equal < 0)
284 return -1;
285 if (!equal)
286 isl_die(ctx, isl_error_unknown,
287 "read sets not equal", return -1);
289 return 0;
292 static int test_bounded(isl_ctx *ctx)
294 isl_set *set;
295 int bounded;
297 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
298 bounded = isl_set_is_bounded(set);
299 isl_set_free(set);
301 if (bounded < 0)
302 return -1;
303 if (!bounded)
304 isl_die(ctx, isl_error_unknown,
305 "set not considered bounded", return -1);
307 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
308 bounded = isl_set_is_bounded(set);
309 assert(!bounded);
310 isl_set_free(set);
312 if (bounded < 0)
313 return -1;
314 if (bounded)
315 isl_die(ctx, isl_error_unknown,
316 "set considered bounded", return -1);
318 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
319 bounded = isl_set_is_bounded(set);
320 isl_set_free(set);
322 if (bounded < 0)
323 return -1;
324 if (bounded)
325 isl_die(ctx, isl_error_unknown,
326 "set considered bounded", return -1);
328 return 0;
331 /* Construct the basic set { [i] : 5 <= i <= N } */
332 static int test_construction(isl_ctx *ctx)
334 isl_int v;
335 isl_space *dim;
336 isl_local_space *ls;
337 isl_basic_set *bset;
338 isl_constraint *c;
340 isl_int_init(v);
342 dim = isl_space_set_alloc(ctx, 1, 1);
343 bset = isl_basic_set_universe(isl_space_copy(dim));
344 ls = isl_local_space_from_space(dim);
346 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
347 isl_int_set_si(v, -1);
348 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
349 isl_int_set_si(v, 1);
350 c = isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
351 bset = isl_basic_set_add_constraint(bset, c);
353 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
354 isl_int_set_si(v, 1);
355 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
356 isl_int_set_si(v, -5);
357 c = isl_constraint_set_constant(c, v);
358 bset = isl_basic_set_add_constraint(bset, c);
360 isl_local_space_free(ls);
361 isl_basic_set_free(bset);
363 isl_int_clear(v);
365 return 0;
368 static int test_dim(isl_ctx *ctx)
370 const char *str;
371 isl_map *map1, *map2;
372 int equal;
374 map1 = isl_map_read_from_str(ctx,
375 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
376 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
377 map2 = isl_map_read_from_str(ctx,
378 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
379 equal = isl_map_is_equal(map1, map2);
380 isl_map_free(map2);
382 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
383 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
384 if (equal >= 0 && equal)
385 equal = isl_map_is_equal(map1, map2);
387 isl_map_free(map1);
388 isl_map_free(map2);
390 if (equal < 0)
391 return -1;
392 if (!equal)
393 isl_die(ctx, isl_error_unknown,
394 "unexpected result", return -1);
396 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
397 map1 = isl_map_read_from_str(ctx, str);
398 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
399 map2 = isl_map_read_from_str(ctx, str);
400 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
401 equal = isl_map_is_equal(map1, map2);
402 isl_map_free(map1);
403 isl_map_free(map2);
405 if (equal < 0)
406 return -1;
407 if (!equal)
408 isl_die(ctx, isl_error_unknown,
409 "unexpected result", return -1);
411 return 0;
414 struct {
415 __isl_give isl_val *(*op)(__isl_take isl_val *v);
416 const char *arg;
417 const char *res;
418 } val_un_tests[] = {
419 { &isl_val_neg, "0", "0" },
420 { &isl_val_abs, "0", "0" },
421 { &isl_val_2exp, "0", "1" },
422 { &isl_val_floor, "0", "0" },
423 { &isl_val_ceil, "0", "0" },
424 { &isl_val_neg, "1", "-1" },
425 { &isl_val_neg, "-1", "1" },
426 { &isl_val_neg, "1/2", "-1/2" },
427 { &isl_val_neg, "-1/2", "1/2" },
428 { &isl_val_neg, "infty", "-infty" },
429 { &isl_val_neg, "-infty", "infty" },
430 { &isl_val_neg, "NaN", "NaN" },
431 { &isl_val_abs, "1", "1" },
432 { &isl_val_abs, "-1", "1" },
433 { &isl_val_abs, "1/2", "1/2" },
434 { &isl_val_abs, "-1/2", "1/2" },
435 { &isl_val_abs, "infty", "infty" },
436 { &isl_val_abs, "-infty", "infty" },
437 { &isl_val_abs, "NaN", "NaN" },
438 { &isl_val_floor, "1", "1" },
439 { &isl_val_floor, "-1", "-1" },
440 { &isl_val_floor, "1/2", "0" },
441 { &isl_val_floor, "-1/2", "-1" },
442 { &isl_val_floor, "infty", "infty" },
443 { &isl_val_floor, "-infty", "-infty" },
444 { &isl_val_floor, "NaN", "NaN" },
445 { &isl_val_ceil, "1", "1" },
446 { &isl_val_ceil, "-1", "-1" },
447 { &isl_val_ceil, "1/2", "1" },
448 { &isl_val_ceil, "-1/2", "0" },
449 { &isl_val_ceil, "infty", "infty" },
450 { &isl_val_ceil, "-infty", "-infty" },
451 { &isl_val_ceil, "NaN", "NaN" },
452 { &isl_val_2exp, "-3", "1/8" },
453 { &isl_val_2exp, "-1", "1/2" },
454 { &isl_val_2exp, "1", "2" },
455 { &isl_val_2exp, "2", "4" },
456 { &isl_val_2exp, "3", "8" },
457 { &isl_val_inv, "1", "1" },
458 { &isl_val_inv, "2", "1/2" },
459 { &isl_val_inv, "1/2", "2" },
460 { &isl_val_inv, "-2", "-1/2" },
461 { &isl_val_inv, "-1/2", "-2" },
462 { &isl_val_inv, "0", "NaN" },
463 { &isl_val_inv, "NaN", "NaN" },
464 { &isl_val_inv, "infty", "0" },
465 { &isl_val_inv, "-infty", "0" },
468 /* Perform some basic tests of unary operations on isl_val objects.
470 static int test_un_val(isl_ctx *ctx)
472 int i;
473 isl_val *v, *res;
474 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
475 int ok;
477 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
478 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
479 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
480 fn = val_un_tests[i].op;
481 v = fn(v);
482 if (isl_val_is_nan(res))
483 ok = isl_val_is_nan(v);
484 else
485 ok = isl_val_eq(v, res);
486 isl_val_free(v);
487 isl_val_free(res);
488 if (ok < 0)
489 return -1;
490 if (!ok)
491 isl_die(ctx, isl_error_unknown,
492 "unexpected result", return -1);
495 return 0;
498 struct {
499 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
500 __isl_take isl_val *v2);
501 } val_bin_op[] = {
502 ['+'] = { &isl_val_add },
503 ['-'] = { &isl_val_sub },
504 ['*'] = { &isl_val_mul },
505 ['/'] = { &isl_val_div },
506 ['g'] = { &isl_val_gcd },
507 ['m'] = { &isl_val_min },
508 ['M'] = { &isl_val_max },
511 struct {
512 const char *arg1;
513 unsigned char op;
514 const char *arg2;
515 const char *res;
516 } val_bin_tests[] = {
517 { "0", '+', "0", "0" },
518 { "1", '+', "0", "1" },
519 { "1", '+', "1", "2" },
520 { "1", '-', "1", "0" },
521 { "1", '*', "1", "1" },
522 { "1", '/', "1", "1" },
523 { "2", '*', "3", "6" },
524 { "2", '*', "1/2", "1" },
525 { "2", '*', "1/3", "2/3" },
526 { "2/3", '*', "3/5", "2/5" },
527 { "2/3", '*', "7/5", "14/15" },
528 { "2", '/', "1/2", "4" },
529 { "-2", '/', "-1/2", "4" },
530 { "-2", '/', "1/2", "-4" },
531 { "2", '/', "-1/2", "-4" },
532 { "2", '/', "2", "1" },
533 { "2", '/', "3", "2/3" },
534 { "2/3", '/', "5/3", "2/5" },
535 { "2/3", '/', "5/7", "14/15" },
536 { "0", '/', "0", "NaN" },
537 { "42", '/', "0", "NaN" },
538 { "-42", '/', "0", "NaN" },
539 { "infty", '/', "0", "NaN" },
540 { "-infty", '/', "0", "NaN" },
541 { "NaN", '/', "0", "NaN" },
542 { "0", '/', "NaN", "NaN" },
543 { "42", '/', "NaN", "NaN" },
544 { "-42", '/', "NaN", "NaN" },
545 { "infty", '/', "NaN", "NaN" },
546 { "-infty", '/', "NaN", "NaN" },
547 { "NaN", '/', "NaN", "NaN" },
548 { "0", '/', "infty", "0" },
549 { "42", '/', "infty", "0" },
550 { "-42", '/', "infty", "0" },
551 { "infty", '/', "infty", "NaN" },
552 { "-infty", '/', "infty", "NaN" },
553 { "NaN", '/', "infty", "NaN" },
554 { "0", '/', "-infty", "0" },
555 { "42", '/', "-infty", "0" },
556 { "-42", '/', "-infty", "0" },
557 { "infty", '/', "-infty", "NaN" },
558 { "-infty", '/', "-infty", "NaN" },
559 { "NaN", '/', "-infty", "NaN" },
560 { "1", '-', "1/3", "2/3" },
561 { "1/3", '+', "1/2", "5/6" },
562 { "1/2", '+', "1/2", "1" },
563 { "3/4", '-', "1/4", "1/2" },
564 { "1/2", '-', "1/3", "1/6" },
565 { "infty", '+', "42", "infty" },
566 { "infty", '+', "infty", "infty" },
567 { "42", '+', "infty", "infty" },
568 { "infty", '-', "infty", "NaN" },
569 { "infty", '*', "infty", "infty" },
570 { "infty", '*', "-infty", "-infty" },
571 { "-infty", '*', "infty", "-infty" },
572 { "-infty", '*', "-infty", "infty" },
573 { "0", '*', "infty", "NaN" },
574 { "1", '*', "infty", "infty" },
575 { "infty", '*', "0", "NaN" },
576 { "infty", '*', "42", "infty" },
577 { "42", '-', "infty", "-infty" },
578 { "infty", '+', "-infty", "NaN" },
579 { "4", 'g', "6", "2" },
580 { "5", 'g', "6", "1" },
581 { "42", 'm', "3", "3" },
582 { "42", 'M', "3", "42" },
583 { "3", 'm', "42", "3" },
584 { "3", 'M', "42", "42" },
585 { "42", 'm', "infty", "42" },
586 { "42", 'M', "infty", "infty" },
587 { "42", 'm', "-infty", "-infty" },
588 { "42", 'M', "-infty", "42" },
589 { "42", 'm', "NaN", "NaN" },
590 { "42", 'M', "NaN", "NaN" },
591 { "infty", 'm', "-infty", "-infty" },
592 { "infty", 'M', "-infty", "infty" },
595 /* Perform some basic tests of binary operations on isl_val objects.
597 static int test_bin_val(isl_ctx *ctx)
599 int i;
600 isl_val *v1, *v2, *res;
601 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
602 __isl_take isl_val *v2);
603 int ok;
605 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
606 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
607 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
608 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
609 fn = val_bin_op[val_bin_tests[i].op].fn;
610 v1 = fn(v1, v2);
611 if (isl_val_is_nan(res))
612 ok = isl_val_is_nan(v1);
613 else
614 ok = isl_val_eq(v1, res);
615 isl_val_free(v1);
616 isl_val_free(res);
617 if (ok < 0)
618 return -1;
619 if (!ok)
620 isl_die(ctx, isl_error_unknown,
621 "unexpected result", return -1);
624 return 0;
627 /* Perform some basic tests on isl_val objects.
629 static int test_val(isl_ctx *ctx)
631 if (test_un_val(ctx) < 0)
632 return -1;
633 if (test_bin_val(ctx) < 0)
634 return -1;
635 return 0;
638 /* Sets described using existentially quantified variables that
639 * can also be described without.
641 static const char *elimination_tests[] = {
642 "{ [i,j] : 2 * [i/2] + 3 * [j/4] <= 10 and 2 i = j }",
643 "{ [m, w] : exists a : w - 2m - 5 <= 3a <= m - 2w }",
644 "{ [m, w] : exists a : w >= 0 and a < m and -1 + w <= a <= 2m - w }",
647 /* Check that redundant existentially quantified variables are
648 * getting removed.
650 static int test_elimination(isl_ctx *ctx)
652 int i;
653 unsigned n;
654 isl_basic_set *bset;
656 for (i = 0; i < ARRAY_SIZE(elimination_tests); ++i) {
657 bset = isl_basic_set_read_from_str(ctx, elimination_tests[i]);
658 n = isl_basic_set_dim(bset, isl_dim_div);
659 isl_basic_set_free(bset);
660 if (!bset)
661 return -1;
662 if (n != 0)
663 isl_die(ctx, isl_error_unknown,
664 "expecting no existentials", return -1);
667 return 0;
670 static int test_div(isl_ctx *ctx)
672 const char *str;
673 int empty;
674 isl_int v;
675 isl_space *dim;
676 isl_set *set;
677 isl_local_space *ls;
678 struct isl_basic_set *bset;
679 struct isl_constraint *c;
681 isl_int_init(v);
683 /* test 1 */
684 dim = isl_space_set_alloc(ctx, 0, 3);
685 bset = isl_basic_set_universe(isl_space_copy(dim));
686 ls = isl_local_space_from_space(dim);
688 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
689 isl_int_set_si(v, -1);
690 c = isl_constraint_set_constant(c, v);
691 isl_int_set_si(v, 1);
692 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
693 isl_int_set_si(v, 3);
694 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
695 bset = isl_basic_set_add_constraint(bset, c);
697 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
698 isl_int_set_si(v, 1);
699 c = isl_constraint_set_constant(c, v);
700 isl_int_set_si(v, -1);
701 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
702 isl_int_set_si(v, 3);
703 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
704 bset = isl_basic_set_add_constraint(bset, c);
706 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
708 assert(bset && bset->n_div == 1);
709 isl_local_space_free(ls);
710 isl_basic_set_free(bset);
712 /* test 2 */
713 dim = isl_space_set_alloc(ctx, 0, 3);
714 bset = isl_basic_set_universe(isl_space_copy(dim));
715 ls = isl_local_space_from_space(dim);
717 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
718 isl_int_set_si(v, 1);
719 c = isl_constraint_set_constant(c, v);
720 isl_int_set_si(v, -1);
721 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
722 isl_int_set_si(v, 3);
723 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
724 bset = isl_basic_set_add_constraint(bset, c);
726 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
727 isl_int_set_si(v, -1);
728 c = isl_constraint_set_constant(c, v);
729 isl_int_set_si(v, 1);
730 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
731 isl_int_set_si(v, 3);
732 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
733 bset = isl_basic_set_add_constraint(bset, c);
735 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
737 assert(bset && bset->n_div == 1);
738 isl_local_space_free(ls);
739 isl_basic_set_free(bset);
741 /* test 3 */
742 dim = isl_space_set_alloc(ctx, 0, 3);
743 bset = isl_basic_set_universe(isl_space_copy(dim));
744 ls = isl_local_space_from_space(dim);
746 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
747 isl_int_set_si(v, 1);
748 c = isl_constraint_set_constant(c, v);
749 isl_int_set_si(v, -1);
750 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
751 isl_int_set_si(v, 3);
752 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
753 bset = isl_basic_set_add_constraint(bset, c);
755 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
756 isl_int_set_si(v, -3);
757 c = isl_constraint_set_constant(c, v);
758 isl_int_set_si(v, 1);
759 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
760 isl_int_set_si(v, 4);
761 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
762 bset = isl_basic_set_add_constraint(bset, c);
764 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
766 assert(bset && bset->n_div == 1);
767 isl_local_space_free(ls);
768 isl_basic_set_free(bset);
770 /* test 4 */
771 dim = isl_space_set_alloc(ctx, 0, 3);
772 bset = isl_basic_set_universe(isl_space_copy(dim));
773 ls = isl_local_space_from_space(dim);
775 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
776 isl_int_set_si(v, 2);
777 c = isl_constraint_set_constant(c, v);
778 isl_int_set_si(v, -1);
779 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
780 isl_int_set_si(v, 3);
781 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
782 bset = isl_basic_set_add_constraint(bset, c);
784 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
785 isl_int_set_si(v, -1);
786 c = isl_constraint_set_constant(c, v);
787 isl_int_set_si(v, 1);
788 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
789 isl_int_set_si(v, 6);
790 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
791 bset = isl_basic_set_add_constraint(bset, c);
793 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
795 assert(isl_basic_set_is_empty(bset));
796 isl_local_space_free(ls);
797 isl_basic_set_free(bset);
799 /* test 5 */
800 dim = isl_space_set_alloc(ctx, 0, 3);
801 bset = isl_basic_set_universe(isl_space_copy(dim));
802 ls = isl_local_space_from_space(dim);
804 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
805 isl_int_set_si(v, -1);
806 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
807 isl_int_set_si(v, 3);
808 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
809 bset = isl_basic_set_add_constraint(bset, c);
811 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
812 isl_int_set_si(v, 1);
813 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
814 isl_int_set_si(v, -3);
815 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
816 bset = isl_basic_set_add_constraint(bset, c);
818 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
820 assert(bset && bset->n_div == 0);
821 isl_basic_set_free(bset);
822 isl_local_space_free(ls);
824 /* test 6 */
825 dim = isl_space_set_alloc(ctx, 0, 3);
826 bset = isl_basic_set_universe(isl_space_copy(dim));
827 ls = isl_local_space_from_space(dim);
829 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
830 isl_int_set_si(v, -1);
831 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
832 isl_int_set_si(v, 6);
833 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
834 bset = isl_basic_set_add_constraint(bset, c);
836 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
837 isl_int_set_si(v, 1);
838 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
839 isl_int_set_si(v, -3);
840 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
841 bset = isl_basic_set_add_constraint(bset, c);
843 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
845 assert(bset && bset->n_div == 1);
846 isl_basic_set_free(bset);
847 isl_local_space_free(ls);
849 /* test 7 */
850 /* This test is a bit tricky. We set up an equality
851 * a + 3b + 3c = 6 e0
852 * Normalization of divs creates _two_ divs
853 * a = 3 e0
854 * c - b - e0 = 2 e1
855 * Afterwards e0 is removed again because it has coefficient -1
856 * and we end up with the original equality and div again.
857 * Perhaps we can avoid the introduction of this temporary div.
859 dim = isl_space_set_alloc(ctx, 0, 4);
860 bset = isl_basic_set_universe(isl_space_copy(dim));
861 ls = isl_local_space_from_space(dim);
863 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
864 isl_int_set_si(v, -1);
865 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
866 isl_int_set_si(v, -3);
867 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
868 isl_int_set_si(v, -3);
869 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
870 isl_int_set_si(v, 6);
871 c = isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
872 bset = isl_basic_set_add_constraint(bset, c);
874 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
876 /* Test disabled for now */
878 assert(bset && bset->n_div == 1);
880 isl_local_space_free(ls);
881 isl_basic_set_free(bset);
883 /* test 8 */
884 dim = isl_space_set_alloc(ctx, 0, 5);
885 bset = isl_basic_set_universe(isl_space_copy(dim));
886 ls = isl_local_space_from_space(dim);
888 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
889 isl_int_set_si(v, -1);
890 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
891 isl_int_set_si(v, -3);
892 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
893 isl_int_set_si(v, -3);
894 c = isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
895 isl_int_set_si(v, 6);
896 c = isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
897 bset = isl_basic_set_add_constraint(bset, c);
899 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
900 isl_int_set_si(v, -1);
901 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
902 isl_int_set_si(v, 1);
903 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
904 isl_int_set_si(v, 1);
905 c = isl_constraint_set_constant(c, v);
906 bset = isl_basic_set_add_constraint(bset, c);
908 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
910 /* Test disabled for now */
912 assert(bset && bset->n_div == 1);
914 isl_local_space_free(ls);
915 isl_basic_set_free(bset);
917 /* test 9 */
918 dim = isl_space_set_alloc(ctx, 0, 4);
919 bset = isl_basic_set_universe(isl_space_copy(dim));
920 ls = isl_local_space_from_space(dim);
922 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
923 isl_int_set_si(v, 1);
924 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
925 isl_int_set_si(v, -1);
926 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
927 isl_int_set_si(v, -2);
928 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
929 bset = isl_basic_set_add_constraint(bset, c);
931 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
932 isl_int_set_si(v, -1);
933 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
934 isl_int_set_si(v, 3);
935 c = isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
936 isl_int_set_si(v, 2);
937 c = isl_constraint_set_constant(c, v);
938 bset = isl_basic_set_add_constraint(bset, c);
940 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
942 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
944 assert(!isl_basic_set_is_empty(bset));
946 isl_local_space_free(ls);
947 isl_basic_set_free(bset);
949 /* test 10 */
950 dim = isl_space_set_alloc(ctx, 0, 3);
951 bset = isl_basic_set_universe(isl_space_copy(dim));
952 ls = isl_local_space_from_space(dim);
954 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
955 isl_int_set_si(v, 1);
956 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
957 isl_int_set_si(v, -2);
958 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
959 bset = isl_basic_set_add_constraint(bset, c);
961 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
963 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
965 isl_local_space_free(ls);
966 isl_basic_set_free(bset);
968 isl_int_clear(v);
970 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
971 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
972 set = isl_set_read_from_str(ctx, str);
973 set = isl_set_compute_divs(set);
974 isl_set_free(set);
975 if (!set)
976 return -1;
978 if (test_elimination(ctx) < 0)
979 return -1;
981 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
982 set = isl_set_read_from_str(ctx, str);
983 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
984 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
985 empty = isl_set_is_empty(set);
986 isl_set_free(set);
987 if (empty < 0)
988 return -1;
989 if (!empty)
990 isl_die(ctx, isl_error_unknown,
991 "result not as accurate as expected", return -1);
993 return 0;
996 void test_application_case(struct isl_ctx *ctx, const char *name)
998 char *filename;
999 FILE *input;
1000 struct isl_basic_set *bset1, *bset2;
1001 struct isl_basic_map *bmap;
1003 filename = get_filename(ctx, name, "omega");
1004 assert(filename);
1005 input = fopen(filename, "r");
1006 assert(input);
1008 bset1 = isl_basic_set_read_from_file(ctx, input);
1009 bmap = isl_basic_map_read_from_file(ctx, input);
1011 bset1 = isl_basic_set_apply(bset1, bmap);
1013 bset2 = isl_basic_set_read_from_file(ctx, input);
1015 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1017 isl_basic_set_free(bset1);
1018 isl_basic_set_free(bset2);
1019 free(filename);
1021 fclose(input);
1024 static int test_application(isl_ctx *ctx)
1026 test_application_case(ctx, "application");
1027 test_application_case(ctx, "application2");
1029 return 0;
1032 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1034 char *filename;
1035 FILE *input;
1036 struct isl_basic_set *bset1, *bset2;
1038 filename = get_filename(ctx, name, "polylib");
1039 assert(filename);
1040 input = fopen(filename, "r");
1041 assert(input);
1043 bset1 = isl_basic_set_read_from_file(ctx, input);
1044 bset2 = isl_basic_set_read_from_file(ctx, input);
1046 bset1 = isl_basic_set_affine_hull(bset1);
1048 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1050 isl_basic_set_free(bset1);
1051 isl_basic_set_free(bset2);
1052 free(filename);
1054 fclose(input);
1057 int test_affine_hull(struct isl_ctx *ctx)
1059 const char *str;
1060 isl_set *set;
1061 isl_basic_set *bset, *bset2;
1062 int n;
1063 int subset;
1065 test_affine_hull_case(ctx, "affine2");
1066 test_affine_hull_case(ctx, "affine");
1067 test_affine_hull_case(ctx, "affine3");
1069 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1070 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1071 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1072 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1073 set = isl_set_read_from_str(ctx, str);
1074 bset = isl_set_affine_hull(set);
1075 n = isl_basic_set_dim(bset, isl_dim_div);
1076 isl_basic_set_free(bset);
1077 if (n != 0)
1078 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1079 return -1);
1081 /* Check that isl_map_affine_hull is not confused by
1082 * the reordering of divs in isl_map_align_divs.
1084 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1085 "32e0 = b and 32e1 = c); "
1086 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1087 set = isl_set_read_from_str(ctx, str);
1088 bset = isl_set_affine_hull(set);
1089 isl_basic_set_free(bset);
1090 if (!bset)
1091 return -1;
1093 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1094 "32e2 = 31 + 31e0 }";
1095 set = isl_set_read_from_str(ctx, str);
1096 bset = isl_set_affine_hull(set);
1097 str = "{ [a] : exists e : a = 32 e }";
1098 bset2 = isl_basic_set_read_from_str(ctx, str);
1099 subset = isl_basic_set_is_subset(bset, bset2);
1100 isl_basic_set_free(bset);
1101 isl_basic_set_free(bset2);
1102 if (subset < 0)
1103 return -1;
1104 if (!subset)
1105 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1106 return -1);
1108 return 0;
1111 /* Pairs of maps and the corresponding expected results of
1112 * isl_map_plain_unshifted_simple_hull.
1114 struct {
1115 const char *map;
1116 const char *hull;
1117 } plain_unshifted_simple_hull_tests[] = {
1118 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1119 "{ [i] -> [j] : i >= 1 }" },
1120 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1121 "(j mod 4 = 2 and k mod 6 = n) }",
1122 "{ [n] -> [i,j,k] : j mod 4 = 2 }" },
1125 /* Basic tests for isl_map_plain_unshifted_simple_hull.
1127 static int test_plain_unshifted_simple_hull(isl_ctx *ctx)
1129 int i;
1130 isl_map *map;
1131 isl_basic_map *hull, *expected;
1132 isl_bool equal;
1134 for (i = 0; i < ARRAY_SIZE(plain_unshifted_simple_hull_tests); ++i) {
1135 const char *str;
1136 str = plain_unshifted_simple_hull_tests[i].map;
1137 map = isl_map_read_from_str(ctx, str);
1138 str = plain_unshifted_simple_hull_tests[i].hull;
1139 expected = isl_basic_map_read_from_str(ctx, str);
1140 hull = isl_map_plain_unshifted_simple_hull(map);
1141 equal = isl_basic_map_is_equal(hull, expected);
1142 isl_basic_map_free(hull);
1143 isl_basic_map_free(expected);
1144 if (equal < 0)
1145 return -1;
1146 if (!equal)
1147 isl_die(ctx, isl_error_unknown, "unexpected hull",
1148 return -1);
1151 return 0;
1154 /* Pairs of sets and the corresponding expected results of
1155 * isl_set_unshifted_simple_hull.
1157 struct {
1158 const char *set;
1159 const char *hull;
1160 } unshifted_simple_hull_tests[] = {
1161 { "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1162 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1165 /* Basic tests for isl_set_unshifted_simple_hull.
1167 static int test_unshifted_simple_hull(isl_ctx *ctx)
1169 int i;
1170 isl_set *set;
1171 isl_basic_set *hull, *expected;
1172 isl_bool equal;
1174 for (i = 0; i < ARRAY_SIZE(unshifted_simple_hull_tests); ++i) {
1175 const char *str;
1176 str = unshifted_simple_hull_tests[i].set;
1177 set = isl_set_read_from_str(ctx, str);
1178 str = unshifted_simple_hull_tests[i].hull;
1179 expected = isl_basic_set_read_from_str(ctx, str);
1180 hull = isl_set_unshifted_simple_hull(set);
1181 equal = isl_basic_set_is_equal(hull, expected);
1182 isl_basic_set_free(hull);
1183 isl_basic_set_free(expected);
1184 if (equal < 0)
1185 return -1;
1186 if (!equal)
1187 isl_die(ctx, isl_error_unknown, "unexpected hull",
1188 return -1);
1191 return 0;
1194 static int test_simple_hull(struct isl_ctx *ctx)
1196 const char *str;
1197 isl_set *set;
1198 isl_basic_set *bset;
1199 isl_bool is_empty;
1201 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1202 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1203 set = isl_set_read_from_str(ctx, str);
1204 bset = isl_set_simple_hull(set);
1205 is_empty = isl_basic_set_is_empty(bset);
1206 isl_basic_set_free(bset);
1208 if (is_empty == isl_bool_error)
1209 return -1;
1211 if (is_empty == isl_bool_false)
1212 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1213 return -1);
1215 if (test_plain_unshifted_simple_hull(ctx) < 0)
1216 return -1;
1217 if (test_unshifted_simple_hull(ctx) < 0)
1218 return -1;
1220 return 0;
1223 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1225 char *filename;
1226 FILE *input;
1227 struct isl_basic_set *bset1, *bset2;
1228 struct isl_set *set;
1230 filename = get_filename(ctx, name, "polylib");
1231 assert(filename);
1232 input = fopen(filename, "r");
1233 assert(input);
1235 bset1 = isl_basic_set_read_from_file(ctx, input);
1236 bset2 = isl_basic_set_read_from_file(ctx, input);
1238 set = isl_basic_set_union(bset1, bset2);
1239 bset1 = isl_set_convex_hull(set);
1241 bset2 = isl_basic_set_read_from_file(ctx, input);
1243 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1245 isl_basic_set_free(bset1);
1246 isl_basic_set_free(bset2);
1247 free(filename);
1249 fclose(input);
1252 struct {
1253 const char *set;
1254 const char *hull;
1255 } convex_hull_tests[] = {
1256 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1257 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1258 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1259 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1260 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1261 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1262 "i2 <= 5 and i2 >= 4; "
1263 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1264 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1265 "i2 <= 5 + i0 and i2 >= i0 }" },
1266 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1267 "{ [x, y] : 1 = 0 }" },
1268 { "{ [x, y, z] : 0 <= x, y, z <= 10; [x, y, 0] : x >= 0 and y > 0; "
1269 "[x, y, 0] : x >= 0 and y < 0 }",
1270 "{ [x, y, z] : x >= 0 and 0 <= z <= 10 }" },
1273 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1275 int i;
1276 int orig_convex = ctx->opt->convex;
1277 ctx->opt->convex = convex;
1279 test_convex_hull_case(ctx, "convex0");
1280 test_convex_hull_case(ctx, "convex1");
1281 test_convex_hull_case(ctx, "convex2");
1282 test_convex_hull_case(ctx, "convex3");
1283 test_convex_hull_case(ctx, "convex4");
1284 test_convex_hull_case(ctx, "convex5");
1285 test_convex_hull_case(ctx, "convex6");
1286 test_convex_hull_case(ctx, "convex7");
1287 test_convex_hull_case(ctx, "convex8");
1288 test_convex_hull_case(ctx, "convex9");
1289 test_convex_hull_case(ctx, "convex10");
1290 test_convex_hull_case(ctx, "convex11");
1291 test_convex_hull_case(ctx, "convex12");
1292 test_convex_hull_case(ctx, "convex13");
1293 test_convex_hull_case(ctx, "convex14");
1294 test_convex_hull_case(ctx, "convex15");
1296 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1297 isl_set *set1, *set2;
1298 int equal;
1300 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1301 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1302 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1303 equal = isl_set_is_equal(set1, set2);
1304 isl_set_free(set1);
1305 isl_set_free(set2);
1307 if (equal < 0)
1308 return -1;
1309 if (!equal)
1310 isl_die(ctx, isl_error_unknown,
1311 "unexpected convex hull", return -1);
1314 ctx->opt->convex = orig_convex;
1316 return 0;
1319 static int test_convex_hull(isl_ctx *ctx)
1321 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1322 return -1;
1323 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1324 return -1;
1325 return 0;
1328 void test_gist_case(struct isl_ctx *ctx, const char *name)
1330 char *filename;
1331 FILE *input;
1332 struct isl_basic_set *bset1, *bset2;
1334 filename = get_filename(ctx, name, "polylib");
1335 assert(filename);
1336 input = fopen(filename, "r");
1337 assert(input);
1339 bset1 = isl_basic_set_read_from_file(ctx, input);
1340 bset2 = isl_basic_set_read_from_file(ctx, input);
1342 bset1 = isl_basic_set_gist(bset1, bset2);
1344 bset2 = isl_basic_set_read_from_file(ctx, input);
1346 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1348 isl_basic_set_free(bset1);
1349 isl_basic_set_free(bset2);
1350 free(filename);
1352 fclose(input);
1355 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1357 struct {
1358 const char *map;
1359 const char *context;
1360 const char *gist;
1361 } plain_gist_tests[] = {
1362 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1363 "{ [i] -> [j] : i >= 1 }",
1364 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1365 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1366 "(j mod 4 = 2 and k mod 6 = n) }",
1367 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1368 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1369 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1370 "{ [i] -> [j] : i > j }",
1371 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1374 /* Basic tests for isl_map_plain_gist_basic_map.
1376 static int test_plain_gist(isl_ctx *ctx)
1378 int i;
1380 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1381 const char *str;
1382 int equal;
1383 isl_map *map, *gist;
1384 isl_basic_map *context;
1386 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1387 str = plain_gist_tests[i].context;
1388 context = isl_basic_map_read_from_str(ctx, str);
1389 map = isl_map_plain_gist_basic_map(map, context);
1390 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1391 equal = isl_map_is_equal(map, gist);
1392 isl_map_free(map);
1393 isl_map_free(gist);
1394 if (equal < 0)
1395 return -1;
1396 if (!equal)
1397 isl_die(ctx, isl_error_unknown,
1398 "incorrect gist result", return -1);
1401 return 0;
1404 struct {
1405 const char *set;
1406 const char *context;
1407 const char *gist;
1408 } gist_tests[] = {
1409 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1410 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1411 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1412 "{ [a, b, c] : a <= 15 }" },
1413 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1414 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1415 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1416 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1417 { "{ [m, n, a, b] : a <= 2147 + n }",
1418 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1419 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1420 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1421 "b >= 0) }",
1422 "{ [m, n, ku, kl] }" },
1423 { "{ [a, a, b] : a >= 10 }",
1424 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1425 "{ [a, a, b] : a >= 10 }" },
1426 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1427 "{ [0, j] : j >= 0 }" },
1428 /* Check that no constraints on i6 are introduced in the gist */
1429 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1430 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1431 "5e0 <= 381 - t1 and i4 <= 1) }",
1432 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1433 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1434 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1435 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1436 "20e0 >= 1511 - 4t1 - 5i4) }" },
1437 /* Check that no constraints on i6 are introduced in the gist */
1438 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1439 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1440 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1441 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1442 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1443 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1444 "20e1 <= 1530 - 4t1 - 5i4 and "
1445 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1446 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1447 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1448 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1449 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1450 "10e0 <= -91 + 5i4 + 4i6 and "
1451 "10e0 >= -105 + 5i4 + 4i6) }",
1452 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1453 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1454 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1455 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1456 "{ [a, b, q, p] : b >= 1 + a }",
1457 "{ [a, b, q, p] : false }" },
1458 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1459 "[n] -> { [x] : x mod 32 = 0 }",
1460 "[n] -> { [x = n] }" },
1461 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1462 "{ [x] : x mod 2 = 0 }" },
1463 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1464 "{ [x] : x mod 128 = 0 }" },
1465 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1466 "{ [x] : x mod 3200 = 0 }" },
1467 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1468 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1469 "{ [a, b, c = a] }" },
1470 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1471 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1472 "{ [a, b, c = a] : a mod 3 = 0 }" },
1473 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1474 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1475 "{ [x] }" },
1476 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1477 "{ [x,y] : 1 <= y <= 3 }",
1478 "{ [x,y] }" },
1481 /* Check that isl_set_gist behaves as expected.
1483 * For the test cases in gist_tests, besides checking that the result
1484 * is as expected, also check that applying the gist operation does
1485 * not modify the input set (an earlier version of isl would do that) and
1486 * that the test case is consistent, i.e., that the gist has the same
1487 * intersection with the context as the input set.
1489 static int test_gist(struct isl_ctx *ctx)
1491 int i;
1492 const char *str;
1493 isl_basic_set *bset1, *bset2;
1494 isl_map *map1, *map2;
1495 int equal;
1497 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1498 int equal_input, equal_intersection;
1499 isl_set *set1, *set2, *copy, *context;
1501 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1502 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1503 copy = isl_set_copy(set1);
1504 set1 = isl_set_gist(set1, isl_set_copy(context));
1505 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1506 equal = isl_set_is_equal(set1, set2);
1507 isl_set_free(set1);
1508 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1509 equal_input = isl_set_is_equal(set1, copy);
1510 isl_set_free(copy);
1511 set1 = isl_set_intersect(set1, isl_set_copy(context));
1512 set2 = isl_set_intersect(set2, context);
1513 equal_intersection = isl_set_is_equal(set1, set2);
1514 isl_set_free(set2);
1515 isl_set_free(set1);
1516 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1517 return -1;
1518 if (!equal)
1519 isl_die(ctx, isl_error_unknown,
1520 "incorrect gist result", return -1);
1521 if (!equal_input)
1522 isl_die(ctx, isl_error_unknown,
1523 "gist modified input", return -1);
1524 if (!equal_input)
1525 isl_die(ctx, isl_error_unknown,
1526 "inconsistent gist test case", return -1);
1529 test_gist_case(ctx, "gist1");
1531 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1532 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1533 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1534 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1535 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1536 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1537 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1538 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1539 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1540 bset1 = isl_basic_set_read_from_str(ctx, str);
1541 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1542 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1543 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1544 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1545 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1546 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1547 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1548 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1549 bset2 = isl_basic_set_read_from_str(ctx, str);
1550 bset1 = isl_basic_set_gist(bset1, bset2);
1551 assert(bset1 && bset1->n_div == 0);
1552 isl_basic_set_free(bset1);
1554 /* Check that the integer divisions of the second disjunct
1555 * do not spread to the first disjunct.
1557 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1558 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1559 "(exists (e0 = [(-1 + t1)/16], "
1560 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1561 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1562 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1563 "o0 <= 4294967295 and t1 <= -1)) }";
1564 map1 = isl_map_read_from_str(ctx, str);
1565 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1566 map2 = isl_map_read_from_str(ctx, str);
1567 map1 = isl_map_gist(map1, map2);
1568 if (!map1)
1569 return -1;
1570 if (map1->n != 1)
1571 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1572 isl_map_free(map1); return -1);
1573 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1574 isl_die(ctx, isl_error_unknown, "expecting single div",
1575 isl_map_free(map1); return -1);
1576 isl_map_free(map1);
1578 if (test_plain_gist(ctx) < 0)
1579 return -1;
1581 return 0;
1584 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1586 isl_set *set, *set2;
1587 int equal;
1588 int one;
1590 set = isl_set_read_from_str(ctx, str);
1591 set = isl_set_coalesce(set);
1592 set2 = isl_set_read_from_str(ctx, str);
1593 equal = isl_set_is_equal(set, set2);
1594 one = set && set->n == 1;
1595 isl_set_free(set);
1596 isl_set_free(set2);
1598 if (equal < 0)
1599 return -1;
1600 if (!equal)
1601 isl_die(ctx, isl_error_unknown,
1602 "coalesced set not equal to input", return -1);
1603 if (check_one && !one)
1604 isl_die(ctx, isl_error_unknown,
1605 "coalesced set should not be a union", return -1);
1607 return 0;
1610 /* Inputs for coalescing tests with unbounded wrapping.
1611 * "str" is a string representation of the input set.
1612 * "single_disjunct" is set if we expect the result to consist of
1613 * a single disjunct.
1615 struct {
1616 int single_disjunct;
1617 const char *str;
1618 } coalesce_unbounded_tests[] = {
1619 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1620 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1621 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1622 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1623 "-10 <= y <= 0}" },
1624 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1625 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1626 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1627 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1630 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1631 * option turned off.
1633 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1635 int i;
1636 int r = 0;
1637 int bounded;
1639 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1640 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1642 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1643 const char *str = coalesce_unbounded_tests[i].str;
1644 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1645 if (test_coalesce_set(ctx, str, check_one) >= 0)
1646 continue;
1647 r = -1;
1648 break;
1651 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1653 return r;
1656 /* Inputs for coalescing tests.
1657 * "str" is a string representation of the input set.
1658 * "single_disjunct" is set if we expect the result to consist of
1659 * a single disjunct.
1661 struct {
1662 int single_disjunct;
1663 const char *str;
1664 } coalesce_tests[] = {
1665 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1666 "y >= x & x >= 2 & 5 >= y }" },
1667 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1668 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1669 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1670 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1671 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1672 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1673 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1674 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1675 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1676 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1677 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1678 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1679 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1680 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1681 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1682 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1683 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1684 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1685 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1686 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1687 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1688 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1689 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1690 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1691 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1692 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1693 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1694 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1695 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1696 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1697 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1698 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1699 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1700 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1701 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1702 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1703 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1704 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1705 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1706 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1707 "[o0, o1, o2, o3, o4, o5, o6]] : "
1708 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1709 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1710 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1711 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1712 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1713 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1714 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1715 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1716 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1717 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1718 "o6 >= i3 + i6 - o3 and M >= 0 and "
1719 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1720 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1721 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1722 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1723 "(o0 = 0 and M >= 2 and N >= 3) or "
1724 "(M = 0 and o0 = 0 and N >= 3) }" },
1725 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1726 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1727 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1728 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1729 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1730 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1731 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1732 "(y = 3 and x = 1) }" },
1733 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1734 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1735 "i1 <= M and i3 <= M and i4 <= M) or "
1736 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1737 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1738 "i4 <= -1 + M) }" },
1739 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1740 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1741 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1742 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1743 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1744 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1745 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1746 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1747 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1748 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1749 { 0, "{ [a, b] : exists e : 2e = a and "
1750 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1751 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1752 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1753 "j >= 1 and j' <= i + j - i' and i >= 1; "
1754 "[1, 1, 1, 1] }" },
1755 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1756 "[i,j] : exists a : j = 3a }" },
1757 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1758 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1759 "a >= 3) or "
1760 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1761 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1762 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1763 "c <= 6 + 8a and a >= 3; "
1764 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1765 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1766 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1767 "[x,0] : 3 <= x <= 4 }" },
1768 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1769 "[x,0] : 4 <= x <= 5 }" },
1770 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1771 "[x,0] : 3 <= x <= 5 }" },
1772 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1773 "[x,0] : 3 <= x <= 4 }" },
1774 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1775 "i1 <= 0; "
1776 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1777 { 1, "{ [0,0]; [1,1] }" },
1778 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1779 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1780 "ii <= k;"
1781 "[k, 0, k] : k <= 6 and k >= 1 }" },
1782 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1783 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1784 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1785 { 1, "[n] -> { [1] : n >= 0;"
1786 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1787 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1788 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1789 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1790 "2e0 <= x and 2e0 <= n);"
1791 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1792 "n >= 0) }" },
1793 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1794 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1795 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1796 "t1 = 1 }" },
1797 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1798 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1799 "[0, 0] }" },
1800 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1801 "t1 >= 13 and t1 <= 16);"
1802 "[t1] : t1 <= 15 and t1 >= 12 }" },
1803 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
1804 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
1805 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
1806 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
1807 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
1808 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
1809 "i <= 4j + 2 }" },
1810 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
1811 "(exists (e0 : 3e0 = -2 + c0)) }" },
1812 { 0, "[n, b0, t0] -> "
1813 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1814 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1815 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1816 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1817 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
1818 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
1819 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
1820 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
1821 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1822 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1823 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1824 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
1825 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
1826 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
1827 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
1828 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
1829 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
1830 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
1831 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
1832 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
1833 { 0, "{ [i0, i1, i2] : "
1834 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
1835 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
1836 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
1837 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
1838 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
1839 "32e0 <= 31 + i0)) or "
1840 "i0 >= 0 }" },
1841 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
1842 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
1843 "2*floor((c)/2) = c and 0 <= a <= 192;"
1844 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
1846 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
1847 "(0 <= a <= b <= n) }" },
1848 { 1, "{ [a, b] : 0 <= a <= 2 and b >= 0 and "
1849 "((0 < b <= 13) or (2*floor((a + b)/2) >= -5 + a + 2b)) }" },
1850 { 1, "{ [a] : (2 <= a <= 5) or (a mod 2 = 1 and 1 <= a <= 5) }" },
1851 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
1852 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1853 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
1854 "b = 3 and 9e0 <= -19 + 2c)) }" },
1855 { 0, "{ [a, b, c] : (b <= 2 and b <= -2 + a) or "
1856 "(b = -1 + a and 0 < a <= 3 and "
1857 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1858 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
1859 "b = 3 and 9e0 <= -19 + 2c)) }" },
1860 { 1, "{ [y, x] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
1861 "[1, 0] }" },
1862 { 1, "{ [x, y] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
1863 "[0, 1] }" },
1864 { 1, "{ [1, y] : -1 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
1865 { 1, "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
1866 { 1, "{ [x, y] : 0 <= x <= 10 and x - 4*floor(x/4) <= 1 and y <= 0; "
1867 "[x, y] : 0 <= x <= 10 and x - 4*floor(x/4) > 1 and y <= 0; "
1868 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) <= 1 and 0 < y; "
1869 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) > 1 and 0 < y }" },
1870 { 1, "{ [x, 0] : 0 <= x <= 10 and x mod 2 = 0; "
1871 "[x, 0] : 0 <= x <= 10 and x mod 2 = 1; "
1872 "[x, y] : 0 <= x <= 10 and 1 <= y <= 10 }" },
1873 { 1, "{ [a] : a <= 8 and "
1874 "(a mod 10 = 7 or a mod 10 = 8 or a mod 10 = 9) }" },
1877 /* A specialized coalescing test case that would result
1878 * in a segmentation fault or a failed assertion in earlier versions of isl.
1880 static int test_coalesce_special(struct isl_ctx *ctx)
1882 const char *str;
1883 isl_map *map1, *map2;
1885 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1886 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
1887 "(y = 201 and o1 <= 239 and o1 >= 212) or "
1888 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
1889 "o1 <= 239 and o1 >= 212)) or "
1890 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
1891 "o1 <= 241 and o1 >= 240));"
1892 "[S_L220_OUT[] -> T7[]] -> "
1893 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
1894 "(y = 2 and o1 <= 241 and o1 >= 212) or "
1895 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
1896 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
1897 map1 = isl_map_read_from_str(ctx, str);
1898 map1 = isl_map_align_divs(map1);
1899 map1 = isl_map_coalesce(map1);
1900 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1901 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
1902 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
1903 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
1904 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
1905 map2 = isl_map_read_from_str(ctx, str);
1906 map2 = isl_map_union(map2, map1);
1907 map2 = isl_map_align_divs(map2);
1908 map2 = isl_map_coalesce(map2);
1909 isl_map_free(map2);
1910 if (!map2)
1911 return -1;
1913 return 0;
1916 /* A specialized coalescing test case that would result in an assertion
1917 * in an earlier version of isl.
1918 * The explicit call to isl_basic_set_union prevents the implicit
1919 * equality constraints in the first basic map from being detected prior
1920 * to the call to isl_set_coalesce, at least at the point
1921 * where this test case was introduced.
1923 static int test_coalesce_special2(struct isl_ctx *ctx)
1925 const char *str;
1926 isl_basic_set *bset1, *bset2;
1927 isl_set *set;
1929 str = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
1930 bset1 = isl_basic_set_read_from_str(ctx, str);
1931 str = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }" ;
1932 bset2 = isl_basic_set_read_from_str(ctx, str);
1933 set = isl_basic_set_union(bset1, bset2);
1934 set = isl_set_coalesce(set);
1935 isl_set_free(set);
1937 if (!set)
1938 return -1;
1939 return 0;
1942 /* Check that calling isl_set_coalesce does not leave other sets
1943 * that may share some information with the input to isl_set_coalesce
1944 * in an inconsistent state.
1945 * In particular, older versions of isl would modify all copies
1946 * of the basic sets in the isl_set_coalesce input in a way
1947 * that could leave them in an inconsistent state.
1948 * The result of printing any other set containing one of these
1949 * basic sets would then result in an invalid set description.
1951 static int test_coalesce_special3(isl_ctx *ctx)
1953 const char *str;
1954 char *s;
1955 isl_set *set1, *set2;
1956 isl_printer *p;
1958 set1 = isl_set_read_from_str(ctx, "{ [0, 0, 0] }");
1959 str = "{ [a, b, a + b] : a >= 0 and b >= 0 and 0 < a + b }";
1960 set2 = isl_set_read_from_str(ctx, str);
1961 set1 = isl_set_union(set1, isl_set_copy(set2));
1962 set1 = isl_set_coalesce(set1);
1963 isl_set_free(set1);
1965 p = isl_printer_to_str(ctx);
1966 p = isl_printer_print_set(p, set2);
1967 isl_set_free(set2);
1968 s = isl_printer_get_str(p);
1969 isl_printer_free(p);
1970 set1 = isl_set_read_from_str(ctx, s);
1971 free(s);
1972 isl_set_free(set1);
1974 if (!set1)
1975 return -1;
1977 return 0;
1980 /* Test the functionality of isl_set_coalesce.
1981 * That is, check that the output is always equal to the input
1982 * and in some cases that the result consists of a single disjunct.
1984 static int test_coalesce(struct isl_ctx *ctx)
1986 int i;
1988 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1989 const char *str = coalesce_tests[i].str;
1990 int check_one = coalesce_tests[i].single_disjunct;
1991 if (test_coalesce_set(ctx, str, check_one) < 0)
1992 return -1;
1995 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1996 return -1;
1997 if (test_coalesce_special(ctx) < 0)
1998 return -1;
1999 if (test_coalesce_special2(ctx) < 0)
2000 return -1;
2001 if (test_coalesce_special3(ctx) < 0)
2002 return -1;
2004 return 0;
2007 /* Construct a representation of the graph on the right of Figure 1
2008 * in "Computing the Transitive Closure of a Union of
2009 * Affine Integer Tuple Relations".
2011 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2013 isl_set *dom;
2014 isl_map *up, *right;
2016 dom = isl_set_read_from_str(ctx,
2017 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2018 "2 x - 3 y + 3 >= 0 }");
2019 right = isl_map_read_from_str(ctx,
2020 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2021 up = isl_map_read_from_str(ctx,
2022 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2023 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2024 right = isl_map_intersect_range(right, isl_set_copy(dom));
2025 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2026 up = isl_map_intersect_range(up, dom);
2027 return isl_map_union(up, right);
2030 /* Construct a representation of the power of the graph
2031 * on the right of Figure 1 in "Computing the Transitive Closure of
2032 * a Union of Affine Integer Tuple Relations".
2034 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2036 return isl_map_read_from_str(ctx,
2037 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2038 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2039 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2042 /* Construct a representation of the transitive closure of the graph
2043 * on the right of Figure 1 in "Computing the Transitive Closure of
2044 * a Union of Affine Integer Tuple Relations".
2046 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2048 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2051 static int test_closure(isl_ctx *ctx)
2053 const char *str;
2054 isl_map *map, *map2;
2055 int exact, equal;
2057 /* COCOA example 1 */
2058 map = isl_map_read_from_str(ctx,
2059 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2060 "1 <= i and i < n and 1 <= j and j < n or "
2061 "i2 = i + 1 and j2 = j - 1 and "
2062 "1 <= i and i < n and 2 <= j and j <= n }");
2063 map = isl_map_power(map, &exact);
2064 assert(exact);
2065 isl_map_free(map);
2067 /* COCOA example 1 */
2068 map = isl_map_read_from_str(ctx,
2069 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2070 "1 <= i and i < n and 1 <= j and j < n or "
2071 "i2 = i + 1 and j2 = j - 1 and "
2072 "1 <= i and i < n and 2 <= j and j <= n }");
2073 map = isl_map_transitive_closure(map, &exact);
2074 assert(exact);
2075 map2 = isl_map_read_from_str(ctx,
2076 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2077 "1 <= i and i < n and 1 <= j and j <= n and "
2078 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2079 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2080 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2081 assert(isl_map_is_equal(map, map2));
2082 isl_map_free(map2);
2083 isl_map_free(map);
2085 map = isl_map_read_from_str(ctx,
2086 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2087 " 0 <= y and y <= n }");
2088 map = isl_map_transitive_closure(map, &exact);
2089 map2 = isl_map_read_from_str(ctx,
2090 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2091 " 0 <= y and y <= n }");
2092 assert(isl_map_is_equal(map, map2));
2093 isl_map_free(map2);
2094 isl_map_free(map);
2096 /* COCOA example 2 */
2097 map = isl_map_read_from_str(ctx,
2098 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2099 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2100 "i2 = i + 2 and j2 = j - 2 and "
2101 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2102 map = isl_map_transitive_closure(map, &exact);
2103 assert(exact);
2104 map2 = isl_map_read_from_str(ctx,
2105 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2106 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2107 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2108 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2109 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2110 assert(isl_map_is_equal(map, map2));
2111 isl_map_free(map);
2112 isl_map_free(map2);
2114 /* COCOA Fig.2 left */
2115 map = isl_map_read_from_str(ctx,
2116 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2117 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2118 "j <= n or "
2119 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2120 "j <= 2 i - 3 and j <= n - 2 or "
2121 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2122 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2123 map = isl_map_transitive_closure(map, &exact);
2124 assert(exact);
2125 isl_map_free(map);
2127 /* COCOA Fig.2 right */
2128 map = isl_map_read_from_str(ctx,
2129 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2130 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2131 "j <= n or "
2132 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2133 "j <= 2 i - 4 and j <= n - 3 or "
2134 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2135 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2136 map = isl_map_power(map, &exact);
2137 assert(exact);
2138 isl_map_free(map);
2140 /* COCOA Fig.2 right */
2141 map = isl_map_read_from_str(ctx,
2142 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2143 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2144 "j <= n or "
2145 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2146 "j <= 2 i - 4 and j <= n - 3 or "
2147 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2148 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2149 map = isl_map_transitive_closure(map, &exact);
2150 assert(exact);
2151 map2 = isl_map_read_from_str(ctx,
2152 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2153 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2154 "j <= n and 3 + i + 2 j <= 3 n and "
2155 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2156 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2157 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2158 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2159 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2160 assert(isl_map_is_equal(map, map2));
2161 isl_map_free(map2);
2162 isl_map_free(map);
2164 map = cocoa_fig_1_right_graph(ctx);
2165 map = isl_map_transitive_closure(map, &exact);
2166 assert(exact);
2167 map2 = cocoa_fig_1_right_tc(ctx);
2168 assert(isl_map_is_equal(map, map2));
2169 isl_map_free(map2);
2170 isl_map_free(map);
2172 map = cocoa_fig_1_right_graph(ctx);
2173 map = isl_map_power(map, &exact);
2174 map2 = cocoa_fig_1_right_power(ctx);
2175 equal = isl_map_is_equal(map, map2);
2176 isl_map_free(map2);
2177 isl_map_free(map);
2178 if (equal < 0)
2179 return -1;
2180 if (!exact)
2181 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2182 if (!equal)
2183 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2185 /* COCOA Theorem 1 counter example */
2186 map = isl_map_read_from_str(ctx,
2187 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2188 "i2 = 1 and j2 = j or "
2189 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2190 map = isl_map_transitive_closure(map, &exact);
2191 assert(exact);
2192 isl_map_free(map);
2194 map = isl_map_read_from_str(ctx,
2195 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2196 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2197 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2198 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2199 map = isl_map_transitive_closure(map, &exact);
2200 assert(exact);
2201 isl_map_free(map);
2203 /* Kelly et al 1996, fig 12 */
2204 map = isl_map_read_from_str(ctx,
2205 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2206 "1 <= i,j,j+1 <= n or "
2207 "j = n and j2 = 1 and i2 = i + 1 and "
2208 "1 <= i,i+1 <= n }");
2209 map = isl_map_transitive_closure(map, &exact);
2210 assert(exact);
2211 map2 = isl_map_read_from_str(ctx,
2212 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2213 "1 <= i <= n and i = i2 or "
2214 "1 <= i < i2 <= n and 1 <= j <= n and "
2215 "1 <= j2 <= n }");
2216 assert(isl_map_is_equal(map, map2));
2217 isl_map_free(map2);
2218 isl_map_free(map);
2220 /* Omega's closure4 */
2221 map = isl_map_read_from_str(ctx,
2222 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2223 "1 <= x,y <= 10 or "
2224 "x2 = x + 1 and y2 = y and "
2225 "1 <= x <= 20 && 5 <= y <= 15 }");
2226 map = isl_map_transitive_closure(map, &exact);
2227 assert(exact);
2228 isl_map_free(map);
2230 map = isl_map_read_from_str(ctx,
2231 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2232 map = isl_map_transitive_closure(map, &exact);
2233 assert(!exact);
2234 map2 = isl_map_read_from_str(ctx,
2235 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2236 assert(isl_map_is_equal(map, map2));
2237 isl_map_free(map);
2238 isl_map_free(map2);
2240 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2241 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2242 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2243 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2244 map = isl_map_read_from_str(ctx, str);
2245 map = isl_map_transitive_closure(map, &exact);
2246 assert(exact);
2247 map2 = isl_map_read_from_str(ctx, str);
2248 assert(isl_map_is_equal(map, map2));
2249 isl_map_free(map);
2250 isl_map_free(map2);
2252 str = "{[0] -> [1]; [2] -> [3]}";
2253 map = isl_map_read_from_str(ctx, str);
2254 map = isl_map_transitive_closure(map, &exact);
2255 assert(exact);
2256 map2 = isl_map_read_from_str(ctx, str);
2257 assert(isl_map_is_equal(map, map2));
2258 isl_map_free(map);
2259 isl_map_free(map2);
2261 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2262 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2263 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2264 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2265 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2266 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2267 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2268 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2269 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2270 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2271 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2272 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2273 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2274 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2275 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2276 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2277 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2278 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2279 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2280 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2281 map = isl_map_read_from_str(ctx, str);
2282 map = isl_map_transitive_closure(map, NULL);
2283 assert(map);
2284 isl_map_free(map);
2286 return 0;
2289 static int test_lex(struct isl_ctx *ctx)
2291 isl_space *dim;
2292 isl_map *map;
2293 int empty;
2295 dim = isl_space_set_alloc(ctx, 0, 0);
2296 map = isl_map_lex_le(dim);
2297 empty = isl_map_is_empty(map);
2298 isl_map_free(map);
2300 if (empty < 0)
2301 return -1;
2302 if (empty)
2303 isl_die(ctx, isl_error_unknown,
2304 "expecting non-empty result", return -1);
2306 return 0;
2309 /* Inputs for isl_map_lexmin tests.
2310 * "map" is the input and "lexmin" is the expected result.
2312 struct {
2313 const char *map;
2314 const char *lexmin;
2315 } lexmin_tests [] = {
2316 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
2317 "{ [x] -> [5] : 6 <= x <= 8; "
2318 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
2319 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
2320 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
2321 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
2322 "{ [x] -> [y] : (4y = x and x >= 0) or "
2323 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2324 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2325 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
2326 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
2327 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
2328 /* Check that empty pieces are properly combined. */
2329 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2330 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2331 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
2332 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2333 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2334 "x >= 4 }" },
2335 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
2336 "a <= 255 and c <= 255 and d <= 255 - j and "
2337 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
2338 "247d <= 247 + k - j and 247d <= 247 + k - b and "
2339 "247d <= 247 + i and 248 - b <= 248d <= c and "
2340 "254d >= i - a + b and 254d >= -a + b and "
2341 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
2342 "{ [i, k, j] -> "
2343 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
2344 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
2345 "247j >= 62738 - i and 509j <= 129795 + i and "
2346 "742j >= 188724 - i; "
2347 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
2348 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
2349 "16*floor((8 + b)/16) <= 7 + b; "
2350 "[a] -> [1] }",
2351 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
2352 "[a] -> [b = 0] : 0 < a <= 509 }" },
2353 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
2354 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
2357 static int test_lexmin(struct isl_ctx *ctx)
2359 int i;
2360 int equal;
2361 const char *str;
2362 isl_basic_map *bmap;
2363 isl_map *map, *map2;
2364 isl_set *set;
2365 isl_set *set2;
2366 isl_pw_multi_aff *pma;
2368 str = "[p0, p1] -> { [] -> [] : "
2369 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2370 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2371 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2372 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2373 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2374 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2375 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2376 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2377 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2378 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2379 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2380 map = isl_map_read_from_str(ctx, str);
2381 map = isl_map_lexmin(map);
2382 isl_map_free(map);
2384 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2385 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2386 set = isl_set_read_from_str(ctx, str);
2387 set = isl_set_lexmax(set);
2388 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2389 set2 = isl_set_read_from_str(ctx, str);
2390 set = isl_set_intersect(set, set2);
2391 assert(!isl_set_is_empty(set));
2392 isl_set_free(set);
2394 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
2395 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
2396 map = isl_map_lexmin(map);
2397 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
2398 equal = isl_map_is_equal(map, map2);
2399 isl_map_free(map);
2400 isl_map_free(map2);
2402 if (equal < 0)
2403 return -1;
2404 if (!equal)
2405 isl_die(ctx, isl_error_unknown,
2406 "unexpected result", return -1);
2409 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2410 " 8i' <= i and 8i' >= -7 + i }";
2411 bmap = isl_basic_map_read_from_str(ctx, str);
2412 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2413 map2 = isl_map_from_pw_multi_aff(pma);
2414 map = isl_map_from_basic_map(bmap);
2415 assert(isl_map_is_equal(map, map2));
2416 isl_map_free(map);
2417 isl_map_free(map2);
2419 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2420 " 8i' <= i and 8i' >= -7 + i }";
2421 set = isl_set_read_from_str(ctx, str);
2422 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2423 set2 = isl_set_from_pw_multi_aff(pma);
2424 equal = isl_set_is_equal(set, set2);
2425 isl_set_free(set);
2426 isl_set_free(set2);
2427 if (equal < 0)
2428 return -1;
2429 if (!equal)
2430 isl_die(ctx, isl_error_unknown,
2431 "unexpected difference between set and "
2432 "piecewise affine expression", return -1);
2434 return 0;
2437 /* A specialized isl_set_min_val test case that would return the wrong result
2438 * in earlier versions of isl.
2439 * The explicit call to isl_basic_set_union prevents the second basic set
2440 * from being determined to be empty prior to the call to isl_set_min_val,
2441 * at least at the point where this test case was introduced.
2443 static int test_min_special(isl_ctx *ctx)
2445 const char *str;
2446 isl_basic_set *bset1, *bset2;
2447 isl_set *set;
2448 isl_aff *obj;
2449 isl_val *res;
2450 int ok;
2452 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
2453 bset1 = isl_basic_set_read_from_str(ctx, str);
2454 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
2455 bset2 = isl_basic_set_read_from_str(ctx, str);
2456 set = isl_basic_set_union(bset1, bset2);
2457 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
2459 res = isl_set_min_val(set, obj);
2460 ok = isl_val_cmp_si(res, 5) == 0;
2462 isl_aff_free(obj);
2463 isl_set_free(set);
2464 isl_val_free(res);
2466 if (!res)
2467 return -1;
2468 if (!ok)
2469 isl_die(ctx, isl_error_unknown, "unexpected minimum",
2470 return -1);
2472 return 0;
2475 /* A specialized isl_set_min_val test case that would return an error
2476 * in earlier versions of isl.
2478 static int test_min_special2(isl_ctx *ctx)
2480 const char *str;
2481 isl_basic_set *bset;
2482 isl_aff *obj;
2483 isl_val *res;
2485 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
2486 bset = isl_basic_set_read_from_str(ctx, str);
2488 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
2490 res = isl_basic_set_max_val(bset, obj);
2492 isl_basic_set_free(bset);
2493 isl_aff_free(obj);
2494 isl_val_free(res);
2496 if (!res)
2497 return -1;
2499 return 0;
2502 struct {
2503 const char *set;
2504 const char *obj;
2505 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2506 __isl_keep isl_aff *obj);
2507 const char *res;
2508 } opt_tests[] = {
2509 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2510 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2511 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2512 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2513 &isl_set_max_val, "30" },
2517 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2518 * In particular, check the results on non-convex inputs.
2520 static int test_min(struct isl_ctx *ctx)
2522 int i;
2523 isl_set *set;
2524 isl_aff *obj;
2525 isl_val *val, *res;
2526 isl_bool ok;
2528 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2529 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2530 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2531 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2532 val = opt_tests[i].fn(set, obj);
2533 ok = isl_val_eq(res, val);
2534 isl_val_free(res);
2535 isl_val_free(val);
2536 isl_aff_free(obj);
2537 isl_set_free(set);
2539 if (ok < 0)
2540 return -1;
2541 if (!ok)
2542 isl_die(ctx, isl_error_unknown,
2543 "unexpected optimum", return -1);
2546 if (test_min_special(ctx) < 0)
2547 return -1;
2548 if (test_min_special2(ctx) < 0)
2549 return -1;
2551 return 0;
2554 struct must_may {
2555 isl_map *must;
2556 isl_map *may;
2559 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2560 void *dep_user, void *user)
2562 struct must_may *mm = (struct must_may *)user;
2564 if (must)
2565 mm->must = isl_map_union(mm->must, dep);
2566 else
2567 mm->may = isl_map_union(mm->may, dep);
2569 return isl_stat_ok;
2572 static int common_space(void *first, void *second)
2574 int depth = *(int *)first;
2575 return 2 * depth;
2578 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2580 isl_map *map2;
2581 int equal;
2583 if (!map)
2584 return -1;
2586 map2 = isl_map_read_from_str(map->ctx, str);
2587 equal = isl_map_is_equal(map, map2);
2588 isl_map_free(map2);
2590 return equal;
2593 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2595 int equal;
2597 equal = map_is_equal(map, str);
2598 if (equal < 0)
2599 return -1;
2600 if (!equal)
2601 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2602 "result not as expected", return -1);
2603 return 0;
2606 static int test_dep(struct isl_ctx *ctx)
2608 const char *str;
2609 isl_space *dim;
2610 isl_map *map;
2611 isl_access_info *ai;
2612 isl_flow *flow;
2613 int depth;
2614 struct must_may mm;
2616 depth = 3;
2618 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2619 map = isl_map_read_from_str(ctx, str);
2620 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2622 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2623 map = isl_map_read_from_str(ctx, str);
2624 ai = isl_access_info_add_source(ai, map, 1, &depth);
2626 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2627 map = isl_map_read_from_str(ctx, str);
2628 ai = isl_access_info_add_source(ai, map, 1, &depth);
2630 flow = isl_access_info_compute_flow(ai);
2631 dim = isl_space_alloc(ctx, 0, 3, 3);
2632 mm.must = isl_map_empty(isl_space_copy(dim));
2633 mm.may = isl_map_empty(dim);
2635 isl_flow_foreach(flow, collect_must_may, &mm);
2637 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2638 " [1,10,0] -> [2,5,0] }";
2639 assert(map_is_equal(mm.must, str));
2640 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2641 assert(map_is_equal(mm.may, str));
2643 isl_map_free(mm.must);
2644 isl_map_free(mm.may);
2645 isl_flow_free(flow);
2648 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2649 map = isl_map_read_from_str(ctx, str);
2650 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2652 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2653 map = isl_map_read_from_str(ctx, str);
2654 ai = isl_access_info_add_source(ai, map, 1, &depth);
2656 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2657 map = isl_map_read_from_str(ctx, str);
2658 ai = isl_access_info_add_source(ai, map, 0, &depth);
2660 flow = isl_access_info_compute_flow(ai);
2661 dim = isl_space_alloc(ctx, 0, 3, 3);
2662 mm.must = isl_map_empty(isl_space_copy(dim));
2663 mm.may = isl_map_empty(dim);
2665 isl_flow_foreach(flow, collect_must_may, &mm);
2667 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2668 assert(map_is_equal(mm.must, str));
2669 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2670 assert(map_is_equal(mm.may, str));
2672 isl_map_free(mm.must);
2673 isl_map_free(mm.may);
2674 isl_flow_free(flow);
2677 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2678 map = isl_map_read_from_str(ctx, str);
2679 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2681 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2682 map = isl_map_read_from_str(ctx, str);
2683 ai = isl_access_info_add_source(ai, map, 0, &depth);
2685 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2686 map = isl_map_read_from_str(ctx, str);
2687 ai = isl_access_info_add_source(ai, map, 0, &depth);
2689 flow = isl_access_info_compute_flow(ai);
2690 dim = isl_space_alloc(ctx, 0, 3, 3);
2691 mm.must = isl_map_empty(isl_space_copy(dim));
2692 mm.may = isl_map_empty(dim);
2694 isl_flow_foreach(flow, collect_must_may, &mm);
2696 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2697 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2698 assert(map_is_equal(mm.may, str));
2699 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2700 assert(map_is_equal(mm.must, str));
2702 isl_map_free(mm.must);
2703 isl_map_free(mm.may);
2704 isl_flow_free(flow);
2707 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2708 map = isl_map_read_from_str(ctx, str);
2709 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2711 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2712 map = isl_map_read_from_str(ctx, str);
2713 ai = isl_access_info_add_source(ai, map, 0, &depth);
2715 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2716 map = isl_map_read_from_str(ctx, str);
2717 ai = isl_access_info_add_source(ai, map, 0, &depth);
2719 flow = isl_access_info_compute_flow(ai);
2720 dim = isl_space_alloc(ctx, 0, 3, 3);
2721 mm.must = isl_map_empty(isl_space_copy(dim));
2722 mm.may = isl_map_empty(dim);
2724 isl_flow_foreach(flow, collect_must_may, &mm);
2726 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2727 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2728 assert(map_is_equal(mm.may, str));
2729 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2730 assert(map_is_equal(mm.must, str));
2732 isl_map_free(mm.must);
2733 isl_map_free(mm.may);
2734 isl_flow_free(flow);
2737 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2738 map = isl_map_read_from_str(ctx, str);
2739 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2741 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2742 map = isl_map_read_from_str(ctx, str);
2743 ai = isl_access_info_add_source(ai, map, 0, &depth);
2745 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2746 map = isl_map_read_from_str(ctx, str);
2747 ai = isl_access_info_add_source(ai, map, 0, &depth);
2749 flow = isl_access_info_compute_flow(ai);
2750 dim = isl_space_alloc(ctx, 0, 3, 3);
2751 mm.must = isl_map_empty(isl_space_copy(dim));
2752 mm.may = isl_map_empty(dim);
2754 isl_flow_foreach(flow, collect_must_may, &mm);
2756 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2757 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2758 assert(map_is_equal(mm.may, str));
2759 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2760 assert(map_is_equal(mm.must, str));
2762 isl_map_free(mm.must);
2763 isl_map_free(mm.may);
2764 isl_flow_free(flow);
2767 depth = 5;
2769 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2770 map = isl_map_read_from_str(ctx, str);
2771 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2773 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2774 map = isl_map_read_from_str(ctx, str);
2775 ai = isl_access_info_add_source(ai, map, 1, &depth);
2777 flow = isl_access_info_compute_flow(ai);
2778 dim = isl_space_alloc(ctx, 0, 5, 5);
2779 mm.must = isl_map_empty(isl_space_copy(dim));
2780 mm.may = isl_map_empty(dim);
2782 isl_flow_foreach(flow, collect_must_may, &mm);
2784 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2785 assert(map_is_equal(mm.must, str));
2786 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2787 assert(map_is_equal(mm.may, str));
2789 isl_map_free(mm.must);
2790 isl_map_free(mm.may);
2791 isl_flow_free(flow);
2793 return 0;
2796 /* Check that the dependence analysis proceeds without errors.
2797 * Earlier versions of isl would break down during the analysis
2798 * due to the use of the wrong spaces.
2800 static int test_flow(isl_ctx *ctx)
2802 const char *str;
2803 isl_union_map *access, *schedule;
2804 isl_union_map *must_dep, *may_dep;
2805 int r;
2807 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2808 access = isl_union_map_read_from_str(ctx, str);
2809 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2810 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2811 "S2[] -> [1,0,0,0]; "
2812 "S3[] -> [-1,0,0,0] }";
2813 schedule = isl_union_map_read_from_str(ctx, str);
2814 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2815 isl_union_map_copy(access), schedule,
2816 &must_dep, &may_dep, NULL, NULL);
2817 isl_union_map_free(may_dep);
2818 isl_union_map_free(must_dep);
2820 return r;
2823 struct {
2824 const char *map;
2825 int sv;
2826 } sv_tests[] = {
2827 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2828 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2829 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2830 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2831 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2832 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2833 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2834 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2835 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2838 int test_sv(isl_ctx *ctx)
2840 isl_union_map *umap;
2841 int i;
2842 int sv;
2844 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2845 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2846 sv = isl_union_map_is_single_valued(umap);
2847 isl_union_map_free(umap);
2848 if (sv < 0)
2849 return -1;
2850 if (sv_tests[i].sv && !sv)
2851 isl_die(ctx, isl_error_internal,
2852 "map not detected as single valued", return -1);
2853 if (!sv_tests[i].sv && sv)
2854 isl_die(ctx, isl_error_internal,
2855 "map detected as single valued", return -1);
2858 return 0;
2861 struct {
2862 const char *str;
2863 int bijective;
2864 } bijective_tests[] = {
2865 { "[N,M]->{[i,j] -> [i]}", 0 },
2866 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
2867 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
2868 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
2869 { "[N,M]->{[i,j] -> [j,i]}", 1 },
2870 { "[N,M]->{[i,j] -> [i+j]}", 0 },
2871 { "[N,M]->{[i,j] -> []}", 0 },
2872 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
2873 { "[N,M]->{[i,j] -> [2i]}", 0 },
2874 { "[N,M]->{[i,j] -> [i,i]}", 0 },
2875 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
2876 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
2877 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
2880 static int test_bijective(struct isl_ctx *ctx)
2882 isl_map *map;
2883 int i;
2884 int bijective;
2886 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
2887 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
2888 bijective = isl_map_is_bijective(map);
2889 isl_map_free(map);
2890 if (bijective < 0)
2891 return -1;
2892 if (bijective_tests[i].bijective && !bijective)
2893 isl_die(ctx, isl_error_internal,
2894 "map not detected as bijective", return -1);
2895 if (!bijective_tests[i].bijective && bijective)
2896 isl_die(ctx, isl_error_internal,
2897 "map detected as bijective", return -1);
2900 return 0;
2903 /* Inputs for isl_pw_qpolynomial_gist tests.
2904 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2906 struct {
2907 const char *pwqp;
2908 const char *set;
2909 const char *gist;
2910 } pwqp_gist_tests[] = {
2911 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2912 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2913 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2914 "{ [i] -> -1/2 + 1/2 * i }" },
2915 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2918 static int test_pwqp(struct isl_ctx *ctx)
2920 int i;
2921 const char *str;
2922 isl_set *set;
2923 isl_pw_qpolynomial *pwqp1, *pwqp2;
2924 int equal;
2926 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2927 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2929 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2930 isl_dim_in, 1, 1);
2932 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2933 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2935 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2937 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2939 isl_pw_qpolynomial_free(pwqp1);
2941 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2942 str = pwqp_gist_tests[i].pwqp;
2943 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2944 str = pwqp_gist_tests[i].set;
2945 set = isl_set_read_from_str(ctx, str);
2946 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2947 str = pwqp_gist_tests[i].gist;
2948 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2949 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2950 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2951 isl_pw_qpolynomial_free(pwqp1);
2953 if (equal < 0)
2954 return -1;
2955 if (!equal)
2956 isl_die(ctx, isl_error_unknown,
2957 "unexpected result", return -1);
2960 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2961 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2962 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2963 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2965 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2967 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2969 isl_pw_qpolynomial_free(pwqp1);
2971 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2972 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2973 str = "{ [x] -> x }";
2974 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2976 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2978 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2980 isl_pw_qpolynomial_free(pwqp1);
2982 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2983 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2984 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2985 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2986 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2987 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2988 isl_pw_qpolynomial_free(pwqp1);
2990 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2991 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2992 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2993 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2994 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2995 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2996 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2997 isl_pw_qpolynomial_free(pwqp1);
2998 isl_pw_qpolynomial_free(pwqp2);
2999 if (equal < 0)
3000 return -1;
3001 if (!equal)
3002 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3004 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3005 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3006 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3007 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3008 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3009 isl_val_one(ctx));
3010 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3011 isl_pw_qpolynomial_free(pwqp1);
3012 isl_pw_qpolynomial_free(pwqp2);
3013 if (equal < 0)
3014 return -1;
3015 if (!equal)
3016 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3018 return 0;
3021 static int test_split_periods(isl_ctx *ctx)
3023 const char *str;
3024 isl_pw_qpolynomial *pwqp;
3026 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3027 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3028 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3029 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3031 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3033 isl_pw_qpolynomial_free(pwqp);
3035 if (!pwqp)
3036 return -1;
3038 return 0;
3041 static int test_union(isl_ctx *ctx)
3043 const char *str;
3044 isl_union_set *uset1, *uset2;
3045 isl_union_map *umap1, *umap2;
3046 int equal;
3048 str = "{ [i] : 0 <= i <= 1 }";
3049 uset1 = isl_union_set_read_from_str(ctx, str);
3050 str = "{ [1] -> [0] }";
3051 umap1 = isl_union_map_read_from_str(ctx, str);
3053 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3054 equal = isl_union_map_is_equal(umap1, umap2);
3056 isl_union_map_free(umap1);
3057 isl_union_map_free(umap2);
3059 if (equal < 0)
3060 return -1;
3061 if (!equal)
3062 isl_die(ctx, isl_error_unknown, "union maps not equal",
3063 return -1);
3065 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3066 umap1 = isl_union_map_read_from_str(ctx, str);
3067 str = "{ A[i]; B[i] }";
3068 uset1 = isl_union_set_read_from_str(ctx, str);
3070 uset2 = isl_union_map_domain(umap1);
3072 equal = isl_union_set_is_equal(uset1, uset2);
3074 isl_union_set_free(uset1);
3075 isl_union_set_free(uset2);
3077 if (equal < 0)
3078 return -1;
3079 if (!equal)
3080 isl_die(ctx, isl_error_unknown, "union sets not equal",
3081 return -1);
3083 return 0;
3086 /* Check that computing a bound of a non-zero polynomial over an unbounded
3087 * domain does not produce a rational value.
3088 * In particular, check that the upper bound is infinity.
3090 static int test_bound_unbounded_domain(isl_ctx *ctx)
3092 const char *str;
3093 isl_pw_qpolynomial *pwqp;
3094 isl_pw_qpolynomial_fold *pwf, *pwf2;
3095 isl_bool equal;
3097 str = "{ [m,n] -> -m * n }";
3098 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3099 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3100 str = "{ infty }";
3101 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3102 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3103 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
3104 isl_pw_qpolynomial_fold_free(pwf);
3105 isl_pw_qpolynomial_fold_free(pwf2);
3107 if (equal < 0)
3108 return -1;
3109 if (!equal)
3110 isl_die(ctx, isl_error_unknown,
3111 "expecting infinite polynomial bound", return -1);
3113 return 0;
3116 static int test_bound(isl_ctx *ctx)
3118 const char *str;
3119 unsigned dim;
3120 isl_pw_qpolynomial *pwqp;
3121 isl_pw_qpolynomial_fold *pwf;
3123 if (test_bound_unbounded_domain(ctx) < 0)
3124 return -1;
3126 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
3127 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3128 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3129 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3130 isl_pw_qpolynomial_fold_free(pwf);
3131 if (dim != 4)
3132 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3133 return -1);
3135 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
3136 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3137 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3138 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3139 isl_pw_qpolynomial_fold_free(pwf);
3140 if (dim != 1)
3141 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3142 return -1);
3144 return 0;
3147 static int test_lift(isl_ctx *ctx)
3149 const char *str;
3150 isl_basic_map *bmap;
3151 isl_basic_set *bset;
3153 str = "{ [i0] : exists e0 : i0 = 4e0 }";
3154 bset = isl_basic_set_read_from_str(ctx, str);
3155 bset = isl_basic_set_lift(bset);
3156 bmap = isl_basic_map_from_range(bset);
3157 bset = isl_basic_map_domain(bmap);
3158 isl_basic_set_free(bset);
3160 return 0;
3163 struct {
3164 const char *set1;
3165 const char *set2;
3166 int subset;
3167 } subset_tests[] = {
3168 { "{ [112, 0] }",
3169 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
3170 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
3171 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
3172 { "{ [65] }",
3173 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
3174 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
3175 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
3176 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
3177 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
3178 "256e0 <= 255i and 256e0 >= -255 + 255i and "
3179 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
3180 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
3181 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
3182 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
3183 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
3184 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
3185 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
3186 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
3187 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
3188 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
3189 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
3190 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
3191 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
3192 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
3193 "4e0 <= -57 + i0 + i1)) or "
3194 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
3195 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
3196 "4e0 >= -61 + i0 + i1)) or "
3197 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
3198 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
3201 static int test_subset(isl_ctx *ctx)
3203 int i;
3204 isl_set *set1, *set2;
3205 int subset;
3207 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3208 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3209 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3210 subset = isl_set_is_subset(set1, set2);
3211 isl_set_free(set1);
3212 isl_set_free(set2);
3213 if (subset < 0)
3214 return -1;
3215 if (subset != subset_tests[i].subset)
3216 isl_die(ctx, isl_error_unknown,
3217 "incorrect subset result", return -1);
3220 return 0;
3223 struct {
3224 const char *minuend;
3225 const char *subtrahend;
3226 const char *difference;
3227 } subtract_domain_tests[] = {
3228 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3229 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3230 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3233 static int test_subtract(isl_ctx *ctx)
3235 int i;
3236 isl_union_map *umap1, *umap2;
3237 isl_union_pw_multi_aff *upma1, *upma2;
3238 isl_union_set *uset;
3239 int equal;
3241 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3242 umap1 = isl_union_map_read_from_str(ctx,
3243 subtract_domain_tests[i].minuend);
3244 uset = isl_union_set_read_from_str(ctx,
3245 subtract_domain_tests[i].subtrahend);
3246 umap2 = isl_union_map_read_from_str(ctx,
3247 subtract_domain_tests[i].difference);
3248 umap1 = isl_union_map_subtract_domain(umap1, uset);
3249 equal = isl_union_map_is_equal(umap1, umap2);
3250 isl_union_map_free(umap1);
3251 isl_union_map_free(umap2);
3252 if (equal < 0)
3253 return -1;
3254 if (!equal)
3255 isl_die(ctx, isl_error_unknown,
3256 "incorrect subtract domain result", return -1);
3259 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3260 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3261 subtract_domain_tests[i].minuend);
3262 uset = isl_union_set_read_from_str(ctx,
3263 subtract_domain_tests[i].subtrahend);
3264 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3265 subtract_domain_tests[i].difference);
3266 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3267 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3268 isl_union_pw_multi_aff_free(upma1);
3269 isl_union_pw_multi_aff_free(upma2);
3270 if (equal < 0)
3271 return -1;
3272 if (!equal)
3273 isl_die(ctx, isl_error_unknown,
3274 "incorrect subtract domain result", return -1);
3277 return 0;
3280 /* Check that intersecting the empty basic set with another basic set
3281 * does not increase the number of constraints. In particular,
3282 * the empty basic set should maintain its canonical representation.
3284 static int test_intersect(isl_ctx *ctx)
3286 int n1, n2;
3287 isl_basic_set *bset1, *bset2;
3289 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3290 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3291 n1 = isl_basic_set_n_constraint(bset1);
3292 bset1 = isl_basic_set_intersect(bset1, bset2);
3293 n2 = isl_basic_set_n_constraint(bset1);
3294 isl_basic_set_free(bset1);
3295 if (!bset1)
3296 return -1;
3297 if (n1 != n2)
3298 isl_die(ctx, isl_error_unknown,
3299 "number of constraints of empty set changed",
3300 return -1);
3302 return 0;
3305 int test_factorize(isl_ctx *ctx)
3307 const char *str;
3308 isl_basic_set *bset;
3309 isl_factorizer *f;
3311 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3312 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3313 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3314 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3315 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3316 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3317 "3i5 >= -2i0 - i2 + 3i4 }";
3318 bset = isl_basic_set_read_from_str(ctx, str);
3319 f = isl_basic_set_factorizer(bset);
3320 isl_basic_set_free(bset);
3321 isl_factorizer_free(f);
3322 if (!f)
3323 isl_die(ctx, isl_error_unknown,
3324 "failed to construct factorizer", return -1);
3326 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3327 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3328 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3329 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3330 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3331 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3332 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3333 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3334 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3335 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3336 bset = isl_basic_set_read_from_str(ctx, str);
3337 f = isl_basic_set_factorizer(bset);
3338 isl_basic_set_free(bset);
3339 isl_factorizer_free(f);
3340 if (!f)
3341 isl_die(ctx, isl_error_unknown,
3342 "failed to construct factorizer", return -1);
3344 return 0;
3347 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3349 int *injective = user;
3351 *injective = isl_map_is_injective(map);
3352 isl_map_free(map);
3354 if (*injective < 0 || !*injective)
3355 return isl_stat_error;
3357 return isl_stat_ok;
3360 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3361 const char *r, const char *s, int tilable, int parallel)
3363 int i;
3364 isl_union_set *D;
3365 isl_union_map *W, *R, *S;
3366 isl_union_map *empty;
3367 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3368 isl_union_map *validity, *proximity, *coincidence;
3369 isl_union_map *schedule;
3370 isl_union_map *test;
3371 isl_union_set *delta;
3372 isl_union_set *domain;
3373 isl_set *delta_set;
3374 isl_set *slice;
3375 isl_set *origin;
3376 isl_schedule_constraints *sc;
3377 isl_schedule *sched;
3378 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3380 D = isl_union_set_read_from_str(ctx, d);
3381 W = isl_union_map_read_from_str(ctx, w);
3382 R = isl_union_map_read_from_str(ctx, r);
3383 S = isl_union_map_read_from_str(ctx, s);
3385 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3386 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3388 empty = isl_union_map_empty(isl_union_map_get_space(S));
3389 isl_union_map_compute_flow(isl_union_map_copy(R),
3390 isl_union_map_copy(W), empty,
3391 isl_union_map_copy(S),
3392 &dep_raw, NULL, NULL, NULL);
3393 isl_union_map_compute_flow(isl_union_map_copy(W),
3394 isl_union_map_copy(W),
3395 isl_union_map_copy(R),
3396 isl_union_map_copy(S),
3397 &dep_waw, &dep_war, NULL, NULL);
3399 dep = isl_union_map_union(dep_waw, dep_war);
3400 dep = isl_union_map_union(dep, dep_raw);
3401 validity = isl_union_map_copy(dep);
3402 coincidence = isl_union_map_copy(dep);
3403 proximity = isl_union_map_copy(dep);
3405 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3406 sc = isl_schedule_constraints_set_validity(sc, validity);
3407 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3408 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3409 sched = isl_schedule_constraints_compute_schedule(sc);
3410 schedule = isl_schedule_get_map(sched);
3411 isl_schedule_free(sched);
3412 isl_union_map_free(W);
3413 isl_union_map_free(R);
3414 isl_union_map_free(S);
3416 is_injection = 1;
3417 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3419 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3420 is_complete = isl_union_set_is_subset(D, domain);
3421 isl_union_set_free(D);
3422 isl_union_set_free(domain);
3424 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3425 test = isl_union_map_apply_range(test, dep);
3426 test = isl_union_map_apply_range(test, schedule);
3428 delta = isl_union_map_deltas(test);
3429 if (isl_union_set_n_set(delta) == 0) {
3430 is_tilable = 1;
3431 is_parallel = 1;
3432 is_nonneg = 1;
3433 isl_union_set_free(delta);
3434 } else {
3435 delta_set = isl_set_from_union_set(delta);
3437 slice = isl_set_universe(isl_set_get_space(delta_set));
3438 for (i = 0; i < tilable; ++i)
3439 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3440 is_tilable = isl_set_is_subset(delta_set, slice);
3441 isl_set_free(slice);
3443 slice = isl_set_universe(isl_set_get_space(delta_set));
3444 for (i = 0; i < parallel; ++i)
3445 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3446 is_parallel = isl_set_is_subset(delta_set, slice);
3447 isl_set_free(slice);
3449 origin = isl_set_universe(isl_set_get_space(delta_set));
3450 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
3451 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3453 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3454 delta_set = isl_set_lexmin(delta_set);
3456 is_nonneg = isl_set_is_equal(delta_set, origin);
3458 isl_set_free(origin);
3459 isl_set_free(delta_set);
3462 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3463 is_injection < 0 || is_complete < 0)
3464 return -1;
3465 if (!is_complete)
3466 isl_die(ctx, isl_error_unknown,
3467 "generated schedule incomplete", return -1);
3468 if (!is_injection)
3469 isl_die(ctx, isl_error_unknown,
3470 "generated schedule not injective on each statement",
3471 return -1);
3472 if (!is_nonneg)
3473 isl_die(ctx, isl_error_unknown,
3474 "negative dependences in generated schedule",
3475 return -1);
3476 if (!is_tilable)
3477 isl_die(ctx, isl_error_unknown,
3478 "generated schedule not as tilable as expected",
3479 return -1);
3480 if (!is_parallel)
3481 isl_die(ctx, isl_error_unknown,
3482 "generated schedule not as parallel as expected",
3483 return -1);
3485 return 0;
3488 /* Compute a schedule for the given instance set, validity constraints,
3489 * proximity constraints and context and return a corresponding union map
3490 * representation.
3492 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3493 const char *domain, const char *validity, const char *proximity,
3494 const char *context)
3496 isl_set *con;
3497 isl_union_set *dom;
3498 isl_union_map *dep;
3499 isl_union_map *prox;
3500 isl_schedule_constraints *sc;
3501 isl_schedule *schedule;
3502 isl_union_map *sched;
3504 con = isl_set_read_from_str(ctx, context);
3505 dom = isl_union_set_read_from_str(ctx, domain);
3506 dep = isl_union_map_read_from_str(ctx, validity);
3507 prox = isl_union_map_read_from_str(ctx, proximity);
3508 sc = isl_schedule_constraints_on_domain(dom);
3509 sc = isl_schedule_constraints_set_context(sc, con);
3510 sc = isl_schedule_constraints_set_validity(sc, dep);
3511 sc = isl_schedule_constraints_set_proximity(sc, prox);
3512 schedule = isl_schedule_constraints_compute_schedule(sc);
3513 sched = isl_schedule_get_map(schedule);
3514 isl_schedule_free(schedule);
3516 return sched;
3519 /* Compute a schedule for the given instance set, validity constraints and
3520 * proximity constraints and return a corresponding union map representation.
3522 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
3523 const char *domain, const char *validity, const char *proximity)
3525 return compute_schedule_with_context(ctx, domain, validity, proximity,
3526 "{ : }");
3529 /* Check that a schedule can be constructed on the given domain
3530 * with the given validity and proximity constraints.
3532 static int test_has_schedule(isl_ctx *ctx, const char *domain,
3533 const char *validity, const char *proximity)
3535 isl_union_map *sched;
3537 sched = compute_schedule(ctx, domain, validity, proximity);
3538 if (!sched)
3539 return -1;
3541 isl_union_map_free(sched);
3542 return 0;
3545 int test_special_schedule(isl_ctx *ctx, const char *domain,
3546 const char *validity, const char *proximity, const char *expected_sched)
3548 isl_union_map *sched1, *sched2;
3549 int equal;
3551 sched1 = compute_schedule(ctx, domain, validity, proximity);
3552 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
3554 equal = isl_union_map_is_equal(sched1, sched2);
3555 isl_union_map_free(sched1);
3556 isl_union_map_free(sched2);
3558 if (equal < 0)
3559 return -1;
3560 if (!equal)
3561 isl_die(ctx, isl_error_unknown, "unexpected schedule",
3562 return -1);
3564 return 0;
3567 /* Check that the schedule map is properly padded, even after being
3568 * reconstructed from the band forest.
3570 static int test_padded_schedule(isl_ctx *ctx)
3572 const char *str;
3573 isl_union_set *D;
3574 isl_union_map *validity, *proximity;
3575 isl_schedule_constraints *sc;
3576 isl_schedule *sched;
3577 isl_union_map *map1, *map2;
3578 isl_band_list *list;
3579 int equal;
3581 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
3582 D = isl_union_set_read_from_str(ctx, str);
3583 validity = isl_union_map_empty(isl_union_set_get_space(D));
3584 proximity = isl_union_map_copy(validity);
3585 sc = isl_schedule_constraints_on_domain(D);
3586 sc = isl_schedule_constraints_set_validity(sc, validity);
3587 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3588 sched = isl_schedule_constraints_compute_schedule(sc);
3589 map1 = isl_schedule_get_map(sched);
3590 list = isl_schedule_get_band_forest(sched);
3591 isl_band_list_free(list);
3592 map2 = isl_schedule_get_map(sched);
3593 isl_schedule_free(sched);
3594 equal = isl_union_map_is_equal(map1, map2);
3595 isl_union_map_free(map1);
3596 isl_union_map_free(map2);
3598 if (equal < 0)
3599 return -1;
3600 if (!equal)
3601 isl_die(ctx, isl_error_unknown,
3602 "reconstructed schedule map not the same as original",
3603 return -1);
3605 return 0;
3608 /* Check that conditional validity constraints are also taken into
3609 * account across bands.
3610 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
3611 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
3612 * and then check that the adjacent order constraint C[2,1]->D[2,0]
3613 * is enforced by the rest of the schedule.
3615 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
3617 const char *str;
3618 isl_union_set *domain;
3619 isl_union_map *validity, *proximity, *condition;
3620 isl_union_map *sink, *source, *dep;
3621 isl_schedule_constraints *sc;
3622 isl_schedule *schedule;
3623 isl_union_access_info *access;
3624 isl_union_flow *flow;
3625 int empty;
3627 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3628 "A[k] : k >= 1 and k <= -1 + n; "
3629 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3630 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
3631 domain = isl_union_set_read_from_str(ctx, str);
3632 sc = isl_schedule_constraints_on_domain(domain);
3633 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
3634 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3635 "D[k, i] -> C[1 + k, i] : "
3636 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3637 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
3638 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
3639 validity = isl_union_map_read_from_str(ctx, str);
3640 sc = isl_schedule_constraints_set_validity(sc, validity);
3641 str = "[n] -> { C[k, i] -> D[k, i] : "
3642 "0 <= i <= -1 + k and k <= -1 + n }";
3643 proximity = isl_union_map_read_from_str(ctx, str);
3644 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3645 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
3646 "i <= -1 + k and i >= 1 and k <= -2 + n; "
3647 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
3648 "k <= -1 + n and i >= 0 and i <= -2 + k }";
3649 condition = isl_union_map_read_from_str(ctx, str);
3650 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
3651 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3652 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
3653 "i >= 0 and i <= -1 + k and k <= -1 + n and "
3654 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
3655 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
3656 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3657 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
3658 "k <= -1 + n and i >= 0 and i <= -1 + k and "
3659 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
3660 validity = isl_union_map_read_from_str(ctx, str);
3661 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3662 validity);
3663 schedule = isl_schedule_constraints_compute_schedule(sc);
3664 str = "{ D[2,0] -> [] }";
3665 sink = isl_union_map_read_from_str(ctx, str);
3666 access = isl_union_access_info_from_sink(sink);
3667 str = "{ C[2,1] -> [] }";
3668 source = isl_union_map_read_from_str(ctx, str);
3669 access = isl_union_access_info_set_must_source(access, source);
3670 access = isl_union_access_info_set_schedule(access, schedule);
3671 flow = isl_union_access_info_compute_flow(access);
3672 dep = isl_union_flow_get_must_dependence(flow);
3673 isl_union_flow_free(flow);
3674 empty = isl_union_map_is_empty(dep);
3675 isl_union_map_free(dep);
3677 if (empty < 0)
3678 return -1;
3679 if (empty)
3680 isl_die(ctx, isl_error_unknown,
3681 "conditional validity not respected", return -1);
3683 return 0;
3686 /* Check that the test for violated conditional validity constraints
3687 * is not confused by domain compression.
3688 * In particular, earlier versions of isl would apply
3689 * a schedule on the compressed domains to the original domains,
3690 * resulting in a failure to detect that the default schedule
3691 * violates the conditional validity constraints.
3693 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
3695 const char *str;
3696 isl_bool empty;
3697 isl_union_set *domain;
3698 isl_union_map *validity, *condition;
3699 isl_schedule_constraints *sc;
3700 isl_schedule *schedule;
3701 isl_union_map *umap;
3702 isl_map *map, *ge;
3704 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
3705 domain = isl_union_set_read_from_str(ctx, str);
3706 sc = isl_schedule_constraints_on_domain(domain);
3707 str = "{ B[1, i] -> A[0, i + 1] }";
3708 condition = isl_union_map_read_from_str(ctx, str);
3709 str = "{ A[0, i] -> B[1, i - 1] }";
3710 validity = isl_union_map_read_from_str(ctx, str);
3711 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3712 isl_union_map_copy(validity));
3713 schedule = isl_schedule_constraints_compute_schedule(sc);
3714 umap = isl_schedule_get_map(schedule);
3715 isl_schedule_free(schedule);
3716 validity = isl_union_map_apply_domain(validity,
3717 isl_union_map_copy(umap));
3718 validity = isl_union_map_apply_range(validity, umap);
3719 map = isl_map_from_union_map(validity);
3720 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3721 map = isl_map_intersect(map, ge);
3722 empty = isl_map_is_empty(map);
3723 isl_map_free(map);
3725 if (empty < 0)
3726 return -1;
3727 if (!empty)
3728 isl_die(ctx, isl_error_unknown,
3729 "conditional validity constraints not satisfied",
3730 return -1);
3732 return 0;
3735 /* Input for testing of schedule construction based on
3736 * conditional constraints.
3738 * domain is the iteration domain
3739 * flow are the flow dependences, which determine the validity and
3740 * proximity constraints
3741 * condition are the conditions on the conditional validity constraints
3742 * conditional_validity are the conditional validity constraints
3743 * outer_band_n is the expected number of members in the outer band
3745 struct {
3746 const char *domain;
3747 const char *flow;
3748 const char *condition;
3749 const char *conditional_validity;
3750 int outer_band_n;
3751 } live_range_tests[] = {
3752 /* Contrived example that illustrates that we need to keep
3753 * track of tagged condition dependences and
3754 * tagged conditional validity dependences
3755 * in isl_sched_edge separately.
3756 * In particular, the conditional validity constraints on A
3757 * cannot be satisfied,
3758 * but they can be ignored because there are no corresponding
3759 * condition constraints. However, we do have an additional
3760 * conditional validity constraint that maps to the same
3761 * dependence relation
3762 * as the condition constraint on B. If we did not make a distinction
3763 * between tagged condition and tagged conditional validity
3764 * dependences, then we
3765 * could end up treating this shared dependence as an condition
3766 * constraint on A, forcing a localization of the conditions,
3767 * which is impossible.
3769 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
3770 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
3771 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
3772 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3773 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3774 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
3777 /* TACO 2013 Fig. 7 */
3778 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3779 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3780 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3781 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
3782 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
3783 "0 <= i < n and 0 <= j < n - 1 }",
3784 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
3785 "0 <= i < n and 0 <= j < j' < n;"
3786 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
3787 "0 <= i < i' < n and 0 <= j,j' < n;"
3788 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
3789 "0 <= i,j,j' < n and j < j' }",
3792 /* TACO 2013 Fig. 7, without tags */
3793 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3794 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3795 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3796 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3797 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3798 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
3799 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
3800 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
3803 /* TACO 2013 Fig. 12 */
3804 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
3805 "S3[i,3] : 0 <= i <= 1 }",
3806 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
3807 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
3808 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
3809 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
3810 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3811 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
3812 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3813 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
3814 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
3815 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
3816 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
3821 /* Test schedule construction based on conditional constraints.
3822 * In particular, check the number of members in the outer band node
3823 * as an indication of whether tiling is possible or not.
3825 static int test_conditional_schedule_constraints(isl_ctx *ctx)
3827 int i;
3828 isl_union_set *domain;
3829 isl_union_map *condition;
3830 isl_union_map *flow;
3831 isl_union_map *validity;
3832 isl_schedule_constraints *sc;
3833 isl_schedule *schedule;
3834 isl_schedule_node *node;
3835 int n_member;
3837 if (test_special_conditional_schedule_constraints(ctx) < 0)
3838 return -1;
3839 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
3840 return -1;
3842 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
3843 domain = isl_union_set_read_from_str(ctx,
3844 live_range_tests[i].domain);
3845 flow = isl_union_map_read_from_str(ctx,
3846 live_range_tests[i].flow);
3847 condition = isl_union_map_read_from_str(ctx,
3848 live_range_tests[i].condition);
3849 validity = isl_union_map_read_from_str(ctx,
3850 live_range_tests[i].conditional_validity);
3851 sc = isl_schedule_constraints_on_domain(domain);
3852 sc = isl_schedule_constraints_set_validity(sc,
3853 isl_union_map_copy(flow));
3854 sc = isl_schedule_constraints_set_proximity(sc, flow);
3855 sc = isl_schedule_constraints_set_conditional_validity(sc,
3856 condition, validity);
3857 schedule = isl_schedule_constraints_compute_schedule(sc);
3858 node = isl_schedule_get_root(schedule);
3859 while (node &&
3860 isl_schedule_node_get_type(node) != isl_schedule_node_band)
3861 node = isl_schedule_node_first_child(node);
3862 n_member = isl_schedule_node_band_n_member(node);
3863 isl_schedule_node_free(node);
3864 isl_schedule_free(schedule);
3866 if (!schedule)
3867 return -1;
3868 if (n_member != live_range_tests[i].outer_band_n)
3869 isl_die(ctx, isl_error_unknown,
3870 "unexpected number of members in outer band",
3871 return -1);
3873 return 0;
3876 /* Check that the schedule computed for the given instance set and
3877 * dependence relation strongly satisfies the dependences.
3878 * In particular, check that no instance is scheduled before
3879 * or together with an instance on which it depends.
3880 * Earlier versions of isl would produce a schedule that
3881 * only weakly satisfies the dependences.
3883 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
3885 const char *domain, *dep;
3886 isl_union_map *D, *schedule;
3887 isl_map *map, *ge;
3888 int empty;
3890 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
3891 "A[i0] : 0 <= i0 <= 1 }";
3892 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
3893 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
3894 schedule = compute_schedule(ctx, domain, dep, dep);
3895 D = isl_union_map_read_from_str(ctx, dep);
3896 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
3897 D = isl_union_map_apply_range(D, schedule);
3898 map = isl_map_from_union_map(D);
3899 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3900 map = isl_map_intersect(map, ge);
3901 empty = isl_map_is_empty(map);
3902 isl_map_free(map);
3904 if (empty < 0)
3905 return -1;
3906 if (!empty)
3907 isl_die(ctx, isl_error_unknown,
3908 "dependences not strongly satisfied", return -1);
3910 return 0;
3913 /* Compute a schedule for input where the instance set constraints
3914 * conflict with the context constraints.
3915 * Earlier versions of isl did not properly handle this situation.
3917 static int test_conflicting_context_schedule(isl_ctx *ctx)
3919 isl_union_map *schedule;
3920 const char *domain, *context;
3922 domain = "[n] -> { A[] : n >= 0 }";
3923 context = "[n] -> { : n < 0 }";
3924 schedule = compute_schedule_with_context(ctx,
3925 domain, "{}", "{}", context);
3926 isl_union_map_free(schedule);
3928 if (!schedule)
3929 return -1;
3931 return 0;
3934 /* Check that the dependence carrying step is not confused by
3935 * a bound on the coefficient size.
3936 * In particular, force the scheduler to move to a dependence carrying
3937 * step by demanding outer coincidence and bound the size of
3938 * the coefficients. Earlier versions of isl would take this
3939 * bound into account while carrying dependences, breaking
3940 * fundamental assumptions.
3941 * On the other hand, the dependence carrying step now tries
3942 * to prevent loop coalescing by default, so check that indeed
3943 * no loop coalescing occurs by comparing the computed schedule
3944 * to the expected non-coalescing schedule.
3946 static int test_bounded_coefficients_schedule(isl_ctx *ctx)
3948 const char *domain, *dep;
3949 isl_union_set *I;
3950 isl_union_map *D;
3951 isl_schedule_constraints *sc;
3952 isl_schedule *schedule;
3953 isl_union_map *sched1, *sched2;
3954 isl_bool equal;
3956 domain = "{ C[i0, i1] : 2 <= i0 <= 3999 and 0 <= i1 <= -1 + i0 }";
3957 dep = "{ C[i0, i1] -> C[i0, 1 + i1] : i0 <= 3999 and i1 >= 0 and "
3958 "i1 <= -2 + i0; "
3959 "C[i0, -1 + i0] -> C[1 + i0, 0] : i0 <= 3998 and i0 >= 1 }";
3960 I = isl_union_set_read_from_str(ctx, domain);
3961 D = isl_union_map_read_from_str(ctx, dep);
3962 sc = isl_schedule_constraints_on_domain(I);
3963 sc = isl_schedule_constraints_set_validity(sc, isl_union_map_copy(D));
3964 sc = isl_schedule_constraints_set_coincidence(sc, D);
3965 isl_options_set_schedule_outer_coincidence(ctx, 1);
3966 isl_options_set_schedule_max_coefficient(ctx, 20);
3967 schedule = isl_schedule_constraints_compute_schedule(sc);
3968 isl_options_set_schedule_max_coefficient(ctx, -1);
3969 isl_options_set_schedule_outer_coincidence(ctx, 0);
3970 sched1 = isl_schedule_get_map(schedule);
3971 isl_schedule_free(schedule);
3973 sched2 = isl_union_map_read_from_str(ctx, "{ C[x,y] -> [x,y] }");
3974 equal = isl_union_map_is_equal(sched1, sched2);
3975 isl_union_map_free(sched1);
3976 isl_union_map_free(sched2);
3978 if (equal < 0)
3979 return -1;
3980 if (!equal)
3981 isl_die(ctx, isl_error_unknown,
3982 "unexpected schedule", return -1);
3984 return 0;
3987 /* Check that a set of schedule constraints that only allow for
3988 * a coalescing schedule still produces a schedule even if the user
3989 * request a non-coalescing schedule. Earlier versions of isl
3990 * would not handle this case correctly.
3992 static int test_coalescing_schedule(isl_ctx *ctx)
3994 const char *domain, *dep;
3995 isl_union_set *I;
3996 isl_union_map *D;
3997 isl_schedule_constraints *sc;
3998 isl_schedule *schedule;
3999 int treat_coalescing;
4001 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
4002 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
4003 I = isl_union_set_read_from_str(ctx, domain);
4004 D = isl_union_map_read_from_str(ctx, dep);
4005 sc = isl_schedule_constraints_on_domain(I);
4006 sc = isl_schedule_constraints_set_validity(sc, D);
4007 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4008 isl_options_set_schedule_treat_coalescing(ctx, 1);
4009 schedule = isl_schedule_constraints_compute_schedule(sc);
4010 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4011 isl_schedule_free(schedule);
4012 if (!schedule)
4013 return -1;
4014 return 0;
4017 int test_schedule(isl_ctx *ctx)
4019 const char *D, *W, *R, *V, *P, *S;
4020 int max_coincidence;
4021 int treat_coalescing;
4023 /* Handle resulting schedule with zero bands. */
4024 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
4025 return -1;
4027 /* Jacobi */
4028 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
4029 W = "{ S1[t,i] -> a[t,i] }";
4030 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
4031 "S1[t,i] -> a[t-1,i+1] }";
4032 S = "{ S1[t,i] -> [t,i] }";
4033 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4034 return -1;
4036 /* Fig. 5 of CC2008 */
4037 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
4038 "j <= -1 + N }";
4039 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
4040 "j >= 2 and j <= -1 + N }";
4041 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
4042 "j >= 2 and j <= -1 + N; "
4043 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
4044 "j >= 2 and j <= -1 + N }";
4045 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4046 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4047 return -1;
4049 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
4050 W = "{ S1[i] -> a[i] }";
4051 R = "{ S2[i] -> a[i+1] }";
4052 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4053 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4054 return -1;
4056 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
4057 W = "{ S1[i] -> a[i] }";
4058 R = "{ S2[i] -> a[9-i] }";
4059 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4060 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4061 return -1;
4063 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
4064 W = "{ S1[i] -> a[i] }";
4065 R = "[N] -> { S2[i] -> a[N-1-i] }";
4066 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4067 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4068 return -1;
4070 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
4071 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
4072 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
4073 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
4074 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4075 return -1;
4077 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4078 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
4079 R = "{ S2[i,j] -> a[i-1,j] }";
4080 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4081 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4082 return -1;
4084 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4085 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
4086 R = "{ S2[i,j] -> a[i,j-1] }";
4087 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4088 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4089 return -1;
4091 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
4092 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
4093 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
4094 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
4095 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
4096 "S_0[] -> [0, 0, 0] }";
4097 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
4098 return -1;
4099 ctx->opt->schedule_parametric = 0;
4100 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4101 return -1;
4102 ctx->opt->schedule_parametric = 1;
4104 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
4105 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
4106 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
4107 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
4108 "S4[i] -> a[i,N] }";
4109 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
4110 "S4[i] -> [4,i,0] }";
4111 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
4112 isl_options_set_schedule_maximize_coincidence(ctx, 0);
4113 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4114 return -1;
4115 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
4117 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
4118 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4119 "j <= N }";
4120 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4121 "j <= N; "
4122 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
4123 "j <= N }";
4124 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4125 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4126 return -1;
4128 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
4129 " S_2[t] : t >= 0 and t <= -1 + N; "
4130 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
4131 "i <= -1 + N }";
4132 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
4133 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
4134 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
4135 "i >= 0 and i <= -1 + N }";
4136 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
4137 "i >= 0 and i <= -1 + N; "
4138 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
4139 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
4140 " S_0[t] -> [0, t, 0] }";
4142 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4143 return -1;
4144 ctx->opt->schedule_parametric = 0;
4145 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4146 return -1;
4147 ctx->opt->schedule_parametric = 1;
4149 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
4150 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
4151 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
4152 return -1;
4154 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
4155 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
4156 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
4157 "j >= 0 and j <= -1 + N; "
4158 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4159 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
4160 "j >= 0 and j <= -1 + N; "
4161 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4162 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
4163 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4164 return -1;
4166 D = "{ S_0[i] : i >= 0 }";
4167 W = "{ S_0[i] -> a[i] : i >= 0 }";
4168 R = "{ S_0[i] -> a[0] : i >= 0 }";
4169 S = "{ S_0[i] -> [0, i, 0] }";
4170 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4171 return -1;
4173 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
4174 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
4175 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
4176 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
4177 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4178 return -1;
4180 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
4181 "k <= -1 + n and k >= 0 }";
4182 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
4183 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
4184 "k <= -1 + n and k >= 0; "
4185 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
4186 "k <= -1 + n and k >= 0; "
4187 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
4188 "k <= -1 + n and k >= 0 }";
4189 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
4190 ctx->opt->schedule_outer_coincidence = 1;
4191 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4192 return -1;
4193 ctx->opt->schedule_outer_coincidence = 0;
4195 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
4196 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
4197 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
4198 "Stmt_for_body24[i0, i1, 1, 0]:"
4199 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
4200 "Stmt_for_body7[i0, i1, i2]:"
4201 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
4202 "i2 <= 7 }";
4204 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
4205 "Stmt_for_body24[1, i1, i2, i3]:"
4206 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
4207 "i2 >= 1;"
4208 "Stmt_for_body24[0, i1, i2, i3] -> "
4209 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
4210 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
4211 "i3 >= 0;"
4212 "Stmt_for_body24[0, i1, i2, i3] ->"
4213 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4214 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4215 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4216 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4217 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4218 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4219 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4220 "i1 <= 6 and i1 >= 0;"
4221 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4222 "Stmt_for_body7[i0, i1, i2] -> "
4223 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4224 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4225 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4226 "Stmt_for_body7[i0, i1, i2] -> "
4227 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4228 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4229 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4230 P = V;
4231 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
4232 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
4233 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
4235 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4236 isl_options_set_schedule_treat_coalescing(ctx, 0);
4237 if (test_special_schedule(ctx, D, V, P, S) < 0)
4238 return -1;
4239 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4241 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4242 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4243 "j >= 1 and j <= 7;"
4244 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4245 "j >= 1 and j <= 8 }";
4246 P = "{ }";
4247 S = "{ S_0[i, j] -> [i + j, j] }";
4248 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4249 if (test_special_schedule(ctx, D, V, P, S) < 0)
4250 return -1;
4251 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4253 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4254 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4255 "j >= 0 and j <= -1 + i }";
4256 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4257 "i <= -1 + N and j >= 0;"
4258 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4259 "i <= -2 + N }";
4260 P = "{ }";
4261 S = "{ S_0[i, j] -> [i, j] }";
4262 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4263 if (test_special_schedule(ctx, D, V, P, S) < 0)
4264 return -1;
4265 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4267 /* Test both algorithms on a case with only proximity dependences. */
4268 D = "{ S[i,j] : 0 <= i <= 10 }";
4269 V = "{ }";
4270 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4271 S = "{ S[i, j] -> [j, i] }";
4272 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4273 if (test_special_schedule(ctx, D, V, P, S) < 0)
4274 return -1;
4275 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4276 if (test_special_schedule(ctx, D, V, P, S) < 0)
4277 return -1;
4279 D = "{ A[a]; B[] }";
4280 V = "{}";
4281 P = "{ A[a] -> B[] }";
4282 if (test_has_schedule(ctx, D, V, P) < 0)
4283 return -1;
4285 if (test_padded_schedule(ctx) < 0)
4286 return -1;
4288 /* Check that check for progress is not confused by rational
4289 * solution.
4291 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4292 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4293 "i0 <= -2 + N; "
4294 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4295 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4296 P = "{}";
4297 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4298 if (test_has_schedule(ctx, D, V, P) < 0)
4299 return -1;
4300 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4302 /* Check that we allow schedule rows that are only non-trivial
4303 * on some full-dimensional domains.
4305 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4306 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4307 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4308 P = "{}";
4309 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4310 if (test_has_schedule(ctx, D, V, P) < 0)
4311 return -1;
4312 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4314 if (test_conditional_schedule_constraints(ctx) < 0)
4315 return -1;
4317 if (test_strongly_satisfying_schedule(ctx) < 0)
4318 return -1;
4320 if (test_conflicting_context_schedule(ctx) < 0)
4321 return -1;
4323 if (test_bounded_coefficients_schedule(ctx) < 0)
4324 return -1;
4325 if (test_coalescing_schedule(ctx) < 0)
4326 return -1;
4328 return 0;
4331 /* Perform scheduling tests using the whole component scheduler.
4333 static int test_schedule_whole(isl_ctx *ctx)
4335 int whole;
4336 int r;
4338 whole = isl_options_get_schedule_whole_component(ctx);
4339 isl_options_set_schedule_whole_component(ctx, 1);
4340 r = test_schedule(ctx);
4341 isl_options_set_schedule_whole_component(ctx, whole);
4343 return r;
4346 /* Perform scheduling tests using the incremental scheduler.
4348 static int test_schedule_incremental(isl_ctx *ctx)
4350 int whole;
4351 int r;
4353 whole = isl_options_get_schedule_whole_component(ctx);
4354 isl_options_set_schedule_whole_component(ctx, 0);
4355 r = test_schedule(ctx);
4356 isl_options_set_schedule_whole_component(ctx, whole);
4358 return r;
4361 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4363 isl_union_map *umap;
4364 int test;
4366 umap = isl_union_map_read_from_str(ctx, str);
4367 test = isl_union_map_plain_is_injective(umap);
4368 isl_union_map_free(umap);
4369 if (test < 0)
4370 return -1;
4371 if (test == injective)
4372 return 0;
4373 if (injective)
4374 isl_die(ctx, isl_error_unknown,
4375 "map not detected as injective", return -1);
4376 else
4377 isl_die(ctx, isl_error_unknown,
4378 "map detected as injective", return -1);
4381 int test_injective(isl_ctx *ctx)
4383 const char *str;
4385 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4386 return -1;
4387 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4388 return -1;
4389 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4390 return -1;
4391 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4392 return -1;
4393 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4394 return -1;
4395 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4396 return -1;
4397 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4398 return -1;
4399 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4400 return -1;
4402 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4403 if (test_plain_injective(ctx, str, 1))
4404 return -1;
4405 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4406 if (test_plain_injective(ctx, str, 0))
4407 return -1;
4409 return 0;
4412 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4414 isl_aff *aff2;
4415 int equal;
4417 if (!aff)
4418 return -1;
4420 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4421 equal = isl_aff_plain_is_equal(aff, aff2);
4422 isl_aff_free(aff2);
4424 return equal;
4427 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4429 int equal;
4431 equal = aff_plain_is_equal(aff, str);
4432 if (equal < 0)
4433 return -1;
4434 if (!equal)
4435 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4436 "result not as expected", return -1);
4437 return 0;
4440 struct {
4441 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4442 __isl_take isl_aff *aff2);
4443 } aff_bin_op[] = {
4444 ['+'] = { &isl_aff_add },
4445 ['-'] = { &isl_aff_sub },
4446 ['*'] = { &isl_aff_mul },
4447 ['/'] = { &isl_aff_div },
4450 struct {
4451 const char *arg1;
4452 unsigned char op;
4453 const char *arg2;
4454 const char *res;
4455 } aff_bin_tests[] = {
4456 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
4457 "{ [i] -> [2i] }" },
4458 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
4459 "{ [i] -> [0] }" },
4460 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
4461 "{ [i] -> [2i] }" },
4462 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
4463 "{ [i] -> [2i] }" },
4464 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
4465 "{ [i] -> [i/2] }" },
4466 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
4467 "{ [i] -> [i] }" },
4468 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
4469 "{ [i] -> [NaN] }" },
4470 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
4471 "{ [i] -> [NaN] }" },
4472 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
4473 "{ [i] -> [NaN] }" },
4474 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
4475 "{ [i] -> [NaN] }" },
4476 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
4477 "{ [i] -> [NaN] }" },
4478 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
4479 "{ [i] -> [NaN] }" },
4480 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
4481 "{ [i] -> [NaN] }" },
4482 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
4483 "{ [i] -> [NaN] }" },
4484 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
4485 "{ [i] -> [NaN] }" },
4486 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
4487 "{ [i] -> [NaN] }" },
4488 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
4489 "{ [i] -> [NaN] }" },
4490 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
4491 "{ [i] -> [NaN] }" },
4494 /* Perform some basic tests of binary operations on isl_aff objects.
4496 static int test_bin_aff(isl_ctx *ctx)
4498 int i;
4499 isl_aff *aff1, *aff2, *res;
4500 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4501 __isl_take isl_aff *aff2);
4502 int ok;
4504 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
4505 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
4506 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
4507 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
4508 fn = aff_bin_op[aff_bin_tests[i].op].fn;
4509 aff1 = fn(aff1, aff2);
4510 if (isl_aff_is_nan(res))
4511 ok = isl_aff_is_nan(aff1);
4512 else
4513 ok = isl_aff_plain_is_equal(aff1, res);
4514 isl_aff_free(aff1);
4515 isl_aff_free(res);
4516 if (ok < 0)
4517 return -1;
4518 if (!ok)
4519 isl_die(ctx, isl_error_unknown,
4520 "unexpected result", return -1);
4523 return 0;
4526 struct {
4527 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
4528 __isl_take isl_pw_aff *pa2);
4529 } pw_aff_bin_op[] = {
4530 ['m'] = { &isl_pw_aff_min },
4531 ['M'] = { &isl_pw_aff_max },
4534 /* Inputs for binary isl_pw_aff operation tests.
4535 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
4536 * defined by pw_aff_bin_op, and "res" is the expected result.
4538 struct {
4539 const char *arg1;
4540 unsigned char op;
4541 const char *arg2;
4542 const char *res;
4543 } pw_aff_bin_tests[] = {
4544 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
4545 "{ [i] -> [i] }" },
4546 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
4547 "{ [i] -> [i] }" },
4548 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
4549 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
4550 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
4551 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
4552 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
4553 "{ [i] -> [NaN] }" },
4554 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
4555 "{ [i] -> [NaN] }" },
4558 /* Perform some basic tests of binary operations on isl_pw_aff objects.
4560 static int test_bin_pw_aff(isl_ctx *ctx)
4562 int i;
4563 isl_bool ok;
4564 isl_pw_aff *pa1, *pa2, *res;
4566 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
4567 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
4568 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
4569 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
4570 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
4571 if (isl_pw_aff_involves_nan(res))
4572 ok = isl_pw_aff_involves_nan(pa1);
4573 else
4574 ok = isl_pw_aff_plain_is_equal(pa1, res);
4575 isl_pw_aff_free(pa1);
4576 isl_pw_aff_free(res);
4577 if (ok < 0)
4578 return -1;
4579 if (!ok)
4580 isl_die(ctx, isl_error_unknown,
4581 "unexpected result", return -1);
4584 return 0;
4587 struct {
4588 __isl_give isl_union_pw_multi_aff *(*fn)(
4589 __isl_take isl_union_pw_multi_aff *upma1,
4590 __isl_take isl_union_pw_multi_aff *upma2);
4591 const char *arg1;
4592 const char *arg2;
4593 const char *res;
4594 } upma_bin_tests[] = {
4595 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
4596 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
4597 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
4598 "{ B[x] -> [2] : x >= 0 }",
4599 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
4600 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
4601 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
4602 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
4603 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
4604 "D[i] -> B[2] : i >= 5 }" },
4605 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4606 "{ B[x] -> C[2] : x > 0 }",
4607 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
4608 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4609 "{ B[x] -> A[2] : x >= 0 }",
4610 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
4613 /* Perform some basic tests of binary operations on
4614 * isl_union_pw_multi_aff objects.
4616 static int test_bin_upma(isl_ctx *ctx)
4618 int i;
4619 isl_union_pw_multi_aff *upma1, *upma2, *res;
4620 int ok;
4622 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
4623 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4624 upma_bin_tests[i].arg1);
4625 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4626 upma_bin_tests[i].arg2);
4627 res = isl_union_pw_multi_aff_read_from_str(ctx,
4628 upma_bin_tests[i].res);
4629 upma1 = upma_bin_tests[i].fn(upma1, upma2);
4630 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
4631 isl_union_pw_multi_aff_free(upma1);
4632 isl_union_pw_multi_aff_free(res);
4633 if (ok < 0)
4634 return -1;
4635 if (!ok)
4636 isl_die(ctx, isl_error_unknown,
4637 "unexpected result", return -1);
4640 return 0;
4643 struct {
4644 __isl_give isl_union_pw_multi_aff *(*fn)(
4645 __isl_take isl_union_pw_multi_aff *upma1,
4646 __isl_take isl_union_pw_multi_aff *upma2);
4647 const char *arg1;
4648 const char *arg2;
4649 } upma_bin_fail_tests[] = {
4650 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4651 "{ B[x] -> C[2] : x >= 0 }" },
4654 /* Perform some basic tests of binary operations on
4655 * isl_union_pw_multi_aff objects that are expected to fail.
4657 static int test_bin_upma_fail(isl_ctx *ctx)
4659 int i, n;
4660 isl_union_pw_multi_aff *upma1, *upma2;
4661 int on_error;
4663 on_error = isl_options_get_on_error(ctx);
4664 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
4665 n = ARRAY_SIZE(upma_bin_fail_tests);
4666 for (i = 0; i < n; ++i) {
4667 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4668 upma_bin_fail_tests[i].arg1);
4669 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4670 upma_bin_fail_tests[i].arg2);
4671 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
4672 isl_union_pw_multi_aff_free(upma1);
4673 if (upma1)
4674 break;
4676 isl_options_set_on_error(ctx, on_error);
4677 if (i < n)
4678 isl_die(ctx, isl_error_unknown,
4679 "operation not expected to succeed", return -1);
4681 return 0;
4684 int test_aff(isl_ctx *ctx)
4686 const char *str;
4687 isl_set *set;
4688 isl_space *space;
4689 isl_local_space *ls;
4690 isl_aff *aff;
4691 int zero, equal;
4693 if (test_bin_aff(ctx) < 0)
4694 return -1;
4695 if (test_bin_pw_aff(ctx) < 0)
4696 return -1;
4697 if (test_bin_upma(ctx) < 0)
4698 return -1;
4699 if (test_bin_upma_fail(ctx) < 0)
4700 return -1;
4702 space = isl_space_set_alloc(ctx, 0, 1);
4703 ls = isl_local_space_from_space(space);
4704 aff = isl_aff_zero_on_domain(ls);
4706 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4707 aff = isl_aff_scale_down_ui(aff, 3);
4708 aff = isl_aff_floor(aff);
4709 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4710 aff = isl_aff_scale_down_ui(aff, 2);
4711 aff = isl_aff_floor(aff);
4712 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4714 str = "{ [10] }";
4715 set = isl_set_read_from_str(ctx, str);
4716 aff = isl_aff_gist(aff, set);
4718 aff = isl_aff_add_constant_si(aff, -16);
4719 zero = isl_aff_plain_is_zero(aff);
4720 isl_aff_free(aff);
4722 if (zero < 0)
4723 return -1;
4724 if (!zero)
4725 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4727 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
4728 aff = isl_aff_scale_down_ui(aff, 64);
4729 aff = isl_aff_floor(aff);
4730 equal = aff_check_plain_equal(aff, "{ [-1] }");
4731 isl_aff_free(aff);
4732 if (equal < 0)
4733 return -1;
4735 return 0;
4738 /* Check that the computation below results in a single expression.
4739 * One or two expressions may result depending on which constraint
4740 * ends up being considered as redundant with respect to the other
4741 * constraints after the projection that is performed internally
4742 * by isl_set_dim_min.
4744 static int test_dim_max_1(isl_ctx *ctx)
4746 const char *str;
4747 isl_set *set;
4748 isl_pw_aff *pa;
4749 int n;
4751 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
4752 "-4a <= b <= 3 and b < n - 4a }";
4753 set = isl_set_read_from_str(ctx, str);
4754 pa = isl_set_dim_min(set, 0);
4755 n = isl_pw_aff_n_piece(pa);
4756 isl_pw_aff_free(pa);
4758 if (!pa)
4759 return -1;
4760 if (n != 1)
4761 isl_die(ctx, isl_error_unknown, "expecting single expression",
4762 return -1);
4764 return 0;
4767 int test_dim_max(isl_ctx *ctx)
4769 int equal;
4770 const char *str;
4771 isl_set *set1, *set2;
4772 isl_set *set;
4773 isl_map *map;
4774 isl_pw_aff *pwaff;
4776 if (test_dim_max_1(ctx) < 0)
4777 return -1;
4779 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
4780 set = isl_set_read_from_str(ctx, str);
4781 pwaff = isl_set_dim_max(set, 0);
4782 set1 = isl_set_from_pw_aff(pwaff);
4783 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
4784 set2 = isl_set_read_from_str(ctx, str);
4785 equal = isl_set_is_equal(set1, set2);
4786 isl_set_free(set1);
4787 isl_set_free(set2);
4788 if (equal < 0)
4789 return -1;
4790 if (!equal)
4791 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4793 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
4794 set = isl_set_read_from_str(ctx, str);
4795 pwaff = isl_set_dim_max(set, 0);
4796 set1 = isl_set_from_pw_aff(pwaff);
4797 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4798 set2 = isl_set_read_from_str(ctx, str);
4799 equal = isl_set_is_equal(set1, set2);
4800 isl_set_free(set1);
4801 isl_set_free(set2);
4802 if (equal < 0)
4803 return -1;
4804 if (!equal)
4805 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4807 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
4808 set = isl_set_read_from_str(ctx, str);
4809 pwaff = isl_set_dim_max(set, 0);
4810 set1 = isl_set_from_pw_aff(pwaff);
4811 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4812 set2 = isl_set_read_from_str(ctx, str);
4813 equal = isl_set_is_equal(set1, set2);
4814 isl_set_free(set1);
4815 isl_set_free(set2);
4816 if (equal < 0)
4817 return -1;
4818 if (!equal)
4819 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4821 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
4822 "0 <= i < N and 0 <= j < M }";
4823 map = isl_map_read_from_str(ctx, str);
4824 set = isl_map_range(map);
4826 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
4827 set1 = isl_set_from_pw_aff(pwaff);
4828 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
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);
4834 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
4835 set1 = isl_set_from_pw_aff(pwaff);
4836 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
4837 set2 = isl_set_read_from_str(ctx, str);
4838 if (equal >= 0 && equal)
4839 equal = isl_set_is_equal(set1, set2);
4840 isl_set_free(set1);
4841 isl_set_free(set2);
4843 isl_set_free(set);
4845 if (equal < 0)
4846 return -1;
4847 if (!equal)
4848 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4850 /* Check that solutions are properly merged. */
4851 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
4852 "c <= -1 + n - 4a - 2b and c >= -2b and "
4853 "4a >= -4 + n and c >= 0 }";
4854 set = isl_set_read_from_str(ctx, str);
4855 pwaff = isl_set_dim_min(set, 2);
4856 set1 = isl_set_from_pw_aff(pwaff);
4857 str = "[n] -> { [(0)] : n >= 1 }";
4858 set2 = isl_set_read_from_str(ctx, str);
4859 equal = isl_set_is_equal(set1, set2);
4860 isl_set_free(set1);
4861 isl_set_free(set2);
4863 if (equal < 0)
4864 return -1;
4865 if (!equal)
4866 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4868 /* Check that empty solution lie in the right space. */
4869 str = "[n] -> { [t,a] : 1 = 0 }";
4870 set = isl_set_read_from_str(ctx, str);
4871 pwaff = isl_set_dim_max(set, 0);
4872 set1 = isl_set_from_pw_aff(pwaff);
4873 str = "[n] -> { [t] : 1 = 0 }";
4874 set2 = isl_set_read_from_str(ctx, str);
4875 equal = isl_set_is_equal(set1, set2);
4876 isl_set_free(set1);
4877 isl_set_free(set2);
4879 if (equal < 0)
4880 return -1;
4881 if (!equal)
4882 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4884 return 0;
4887 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
4889 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
4890 const char *str)
4892 isl_ctx *ctx;
4893 isl_pw_multi_aff *pma2;
4894 int equal;
4896 if (!pma)
4897 return -1;
4899 ctx = isl_pw_multi_aff_get_ctx(pma);
4900 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4901 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
4902 isl_pw_multi_aff_free(pma2);
4904 return equal;
4907 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
4908 * represented by "str".
4910 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
4911 const char *str)
4913 int equal;
4915 equal = pw_multi_aff_plain_is_equal(pma, str);
4916 if (equal < 0)
4917 return -1;
4918 if (!equal)
4919 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
4920 "result not as expected", return -1);
4921 return 0;
4924 /* Basic test for isl_pw_multi_aff_product.
4926 * Check that multiple pieces are properly handled.
4928 static int test_product_pma(isl_ctx *ctx)
4930 int equal;
4931 const char *str;
4932 isl_pw_multi_aff *pma1, *pma2;
4934 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
4935 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4936 str = "{ C[] -> D[] }";
4937 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4938 pma1 = isl_pw_multi_aff_product(pma1, pma2);
4939 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
4940 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
4941 equal = pw_multi_aff_check_plain_equal(pma1, str);
4942 isl_pw_multi_aff_free(pma1);
4943 if (equal < 0)
4944 return -1;
4946 return 0;
4949 int test_product(isl_ctx *ctx)
4951 const char *str;
4952 isl_set *set;
4953 isl_union_set *uset1, *uset2;
4954 int ok;
4956 str = "{ A[i] }";
4957 set = isl_set_read_from_str(ctx, str);
4958 set = isl_set_product(set, isl_set_copy(set));
4959 ok = isl_set_is_wrapping(set);
4960 isl_set_free(set);
4961 if (ok < 0)
4962 return -1;
4963 if (!ok)
4964 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4966 str = "{ [] }";
4967 uset1 = isl_union_set_read_from_str(ctx, str);
4968 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
4969 str = "{ [[] -> []] }";
4970 uset2 = isl_union_set_read_from_str(ctx, str);
4971 ok = isl_union_set_is_equal(uset1, uset2);
4972 isl_union_set_free(uset1);
4973 isl_union_set_free(uset2);
4974 if (ok < 0)
4975 return -1;
4976 if (!ok)
4977 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4979 if (test_product_pma(ctx) < 0)
4980 return -1;
4982 return 0;
4985 /* Check that two sets are not considered disjoint just because
4986 * they have a different set of (named) parameters.
4988 static int test_disjoint(isl_ctx *ctx)
4990 const char *str;
4991 isl_set *set, *set2;
4992 int disjoint;
4994 str = "[n] -> { [[]->[]] }";
4995 set = isl_set_read_from_str(ctx, str);
4996 str = "{ [[]->[]] }";
4997 set2 = isl_set_read_from_str(ctx, str);
4998 disjoint = isl_set_is_disjoint(set, set2);
4999 isl_set_free(set);
5000 isl_set_free(set2);
5001 if (disjoint < 0)
5002 return -1;
5003 if (disjoint)
5004 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5006 return 0;
5009 int test_equal(isl_ctx *ctx)
5011 const char *str;
5012 isl_set *set, *set2;
5013 int equal;
5015 str = "{ S_6[i] }";
5016 set = isl_set_read_from_str(ctx, str);
5017 str = "{ S_7[i] }";
5018 set2 = isl_set_read_from_str(ctx, str);
5019 equal = isl_set_is_equal(set, set2);
5020 isl_set_free(set);
5021 isl_set_free(set2);
5022 if (equal < 0)
5023 return -1;
5024 if (equal)
5025 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5027 return 0;
5030 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
5031 enum isl_dim_type type, unsigned pos, int fixed)
5033 int test;
5035 test = isl_map_plain_is_fixed(map, type, pos, NULL);
5036 isl_map_free(map);
5037 if (test < 0)
5038 return -1;
5039 if (test == fixed)
5040 return 0;
5041 if (fixed)
5042 isl_die(ctx, isl_error_unknown,
5043 "map not detected as fixed", return -1);
5044 else
5045 isl_die(ctx, isl_error_unknown,
5046 "map detected as fixed", return -1);
5049 int test_fixed(isl_ctx *ctx)
5051 const char *str;
5052 isl_map *map;
5054 str = "{ [i] -> [i] }";
5055 map = isl_map_read_from_str(ctx, str);
5056 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
5057 return -1;
5058 str = "{ [i] -> [1] }";
5059 map = isl_map_read_from_str(ctx, str);
5060 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5061 return -1;
5062 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
5063 map = isl_map_read_from_str(ctx, str);
5064 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5065 return -1;
5066 map = isl_map_read_from_str(ctx, str);
5067 map = isl_map_neg(map);
5068 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5069 return -1;
5071 return 0;
5074 struct isl_vertices_test_data {
5075 const char *set;
5076 int n;
5077 const char *vertex[6];
5078 } vertices_tests[] = {
5079 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
5080 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
5081 { "{ A[t, i] : t = 14 and i = 1 }",
5082 1, { "{ A[14, 1] }" } },
5083 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
5084 "c <= m and m <= n and m > 0 }",
5085 6, {
5086 "[n, m] -> { [n, m, m] : 0 < m <= n }",
5087 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
5088 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
5089 "[n, m] -> { [m, m, m] : 0 < m <= n }",
5090 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
5091 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
5092 } },
5095 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
5097 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
5099 struct isl_vertices_test_data *data = user;
5100 isl_ctx *ctx;
5101 isl_multi_aff *ma;
5102 isl_basic_set *bset;
5103 isl_pw_multi_aff *pma;
5104 int i;
5105 isl_bool equal;
5107 ctx = isl_vertex_get_ctx(vertex);
5108 bset = isl_vertex_get_domain(vertex);
5109 ma = isl_vertex_get_expr(vertex);
5110 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
5112 for (i = 0; i < data->n; ++i) {
5113 isl_pw_multi_aff *pma_i;
5115 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
5116 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
5117 isl_pw_multi_aff_free(pma_i);
5119 if (equal < 0 || equal)
5120 break;
5123 isl_pw_multi_aff_free(pma);
5124 isl_vertex_free(vertex);
5126 if (equal < 0)
5127 return isl_stat_error;
5129 return equal ? isl_stat_ok : isl_stat_error;
5132 int test_vertices(isl_ctx *ctx)
5134 int i;
5136 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
5137 isl_basic_set *bset;
5138 isl_vertices *vertices;
5139 int ok = 1;
5140 int n;
5142 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
5143 vertices = isl_basic_set_compute_vertices(bset);
5144 n = isl_vertices_get_n_vertices(vertices);
5145 if (vertices_tests[i].n != n)
5146 ok = 0;
5147 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
5148 &vertices_tests[i]) < 0)
5149 ok = 0;
5150 isl_vertices_free(vertices);
5151 isl_basic_set_free(bset);
5153 if (!vertices)
5154 return -1;
5155 if (!ok)
5156 isl_die(ctx, isl_error_unknown, "unexpected vertices",
5157 return -1);
5160 return 0;
5163 int test_union_pw(isl_ctx *ctx)
5165 int equal;
5166 const char *str;
5167 isl_union_set *uset;
5168 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
5170 str = "{ [x] -> x^2 }";
5171 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
5172 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
5173 uset = isl_union_pw_qpolynomial_domain(upwqp1);
5174 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
5175 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
5176 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
5177 isl_union_pw_qpolynomial_free(upwqp1);
5178 isl_union_pw_qpolynomial_free(upwqp2);
5179 if (equal < 0)
5180 return -1;
5181 if (!equal)
5182 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5184 return 0;
5187 /* Test that isl_union_pw_qpolynomial_eval picks up the function
5188 * defined over the correct domain space.
5190 static int test_eval_1(isl_ctx *ctx)
5192 const char *str;
5193 isl_point *pnt;
5194 isl_set *set;
5195 isl_union_pw_qpolynomial *upwqp;
5196 isl_val *v;
5197 int cmp;
5199 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
5200 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
5201 str = "{ A[6] }";
5202 set = isl_set_read_from_str(ctx, str);
5203 pnt = isl_set_sample_point(set);
5204 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
5205 cmp = isl_val_cmp_si(v, 36);
5206 isl_val_free(v);
5208 if (!v)
5209 return -1;
5210 if (cmp != 0)
5211 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
5213 return 0;
5216 /* Check that isl_qpolynomial_eval handles getting called on a void point.
5218 static int test_eval_2(isl_ctx *ctx)
5220 const char *str;
5221 isl_point *pnt;
5222 isl_set *set;
5223 isl_qpolynomial *qp;
5224 isl_val *v;
5225 isl_bool ok;
5227 str = "{ A[x] -> [x] }";
5228 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
5229 str = "{ A[x] : false }";
5230 set = isl_set_read_from_str(ctx, str);
5231 pnt = isl_set_sample_point(set);
5232 v = isl_qpolynomial_eval(qp, pnt);
5233 ok = isl_val_is_nan(v);
5234 isl_val_free(v);
5236 if (ok < 0)
5237 return -1;
5238 if (!ok)
5239 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
5241 return 0;
5244 /* Perform basic polynomial evaluation tests.
5246 static int test_eval(isl_ctx *ctx)
5248 if (test_eval_1(ctx) < 0)
5249 return -1;
5250 if (test_eval_2(ctx) < 0)
5251 return -1;
5252 return 0;
5255 /* Descriptions of sets that are tested for reparsing after printing.
5257 const char *output_tests[] = {
5258 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
5261 /* Check that printing a set and reparsing a set from the printed output
5262 * results in the same set.
5264 static int test_output_set(isl_ctx *ctx)
5266 int i;
5267 char *str;
5268 isl_set *set1, *set2;
5269 isl_bool equal;
5271 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
5272 set1 = isl_set_read_from_str(ctx, output_tests[i]);
5273 str = isl_set_to_str(set1);
5274 set2 = isl_set_read_from_str(ctx, str);
5275 free(str);
5276 equal = isl_set_is_equal(set1, set2);
5277 isl_set_free(set1);
5278 isl_set_free(set2);
5279 if (equal < 0)
5280 return -1;
5281 if (!equal)
5282 isl_die(ctx, isl_error_unknown,
5283 "parsed output not the same", return -1);
5286 return 0;
5289 int test_output(isl_ctx *ctx)
5291 char *s;
5292 const char *str;
5293 isl_pw_aff *pa;
5294 isl_printer *p;
5295 int equal;
5297 if (test_output_set(ctx) < 0)
5298 return -1;
5300 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
5301 pa = isl_pw_aff_read_from_str(ctx, str);
5303 p = isl_printer_to_str(ctx);
5304 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
5305 p = isl_printer_print_pw_aff(p, pa);
5306 s = isl_printer_get_str(p);
5307 isl_printer_free(p);
5308 isl_pw_aff_free(pa);
5309 if (!s)
5310 equal = -1;
5311 else
5312 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
5313 free(s);
5314 if (equal < 0)
5315 return -1;
5316 if (!equal)
5317 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5319 return 0;
5322 int test_sample(isl_ctx *ctx)
5324 const char *str;
5325 isl_basic_set *bset1, *bset2;
5326 int empty, subset;
5328 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
5329 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
5330 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
5331 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
5332 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
5333 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
5334 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
5335 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
5336 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
5337 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
5338 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
5339 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
5340 "d - 1073741821e and "
5341 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
5342 "3j >= 1 - a + b + 2e and "
5343 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
5344 "3i <= 4 - a + 4b - e and "
5345 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
5346 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
5347 "c <= -1 + a and 3i >= -2 - a + 3e and "
5348 "1073741822e <= 1073741823 - a + 1073741822b + c and "
5349 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
5350 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
5351 "1073741823e >= 1 + 1073741823b - d and "
5352 "3i >= 1073741823b + c - 1073741823e - f and "
5353 "3i >= 1 + 2b + e + 3g }";
5354 bset1 = isl_basic_set_read_from_str(ctx, str);
5355 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
5356 empty = isl_basic_set_is_empty(bset2);
5357 subset = isl_basic_set_is_subset(bset2, bset1);
5358 isl_basic_set_free(bset1);
5359 isl_basic_set_free(bset2);
5360 if (empty < 0 || subset < 0)
5361 return -1;
5362 if (empty)
5363 isl_die(ctx, isl_error_unknown, "point not found", return -1);
5364 if (!subset)
5365 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
5367 return 0;
5370 int test_fixed_power(isl_ctx *ctx)
5372 const char *str;
5373 isl_map *map;
5374 isl_int exp;
5375 int equal;
5377 isl_int_init(exp);
5378 str = "{ [i] -> [i + 1] }";
5379 map = isl_map_read_from_str(ctx, str);
5380 isl_int_set_si(exp, 23);
5381 map = isl_map_fixed_power(map, exp);
5382 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
5383 isl_int_clear(exp);
5384 isl_map_free(map);
5385 if (equal < 0)
5386 return -1;
5388 return 0;
5391 int test_slice(isl_ctx *ctx)
5393 const char *str;
5394 isl_map *map;
5395 int equal;
5397 str = "{ [i] -> [j] }";
5398 map = isl_map_read_from_str(ctx, str);
5399 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
5400 equal = map_check_equal(map, "{ [i] -> [i] }");
5401 isl_map_free(map);
5402 if (equal < 0)
5403 return -1;
5405 str = "{ [i] -> [j] }";
5406 map = isl_map_read_from_str(ctx, str);
5407 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
5408 equal = map_check_equal(map, "{ [i] -> [j] }");
5409 isl_map_free(map);
5410 if (equal < 0)
5411 return -1;
5413 str = "{ [i] -> [j] }";
5414 map = isl_map_read_from_str(ctx, str);
5415 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
5416 equal = map_check_equal(map, "{ [i] -> [-i] }");
5417 isl_map_free(map);
5418 if (equal < 0)
5419 return -1;
5421 str = "{ [i] -> [j] }";
5422 map = isl_map_read_from_str(ctx, str);
5423 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
5424 equal = map_check_equal(map, "{ [0] -> [j] }");
5425 isl_map_free(map);
5426 if (equal < 0)
5427 return -1;
5429 str = "{ [i] -> [j] }";
5430 map = isl_map_read_from_str(ctx, str);
5431 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
5432 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
5433 isl_map_free(map);
5434 if (equal < 0)
5435 return -1;
5437 str = "{ [i] -> [j] }";
5438 map = isl_map_read_from_str(ctx, str);
5439 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
5440 equal = map_check_equal(map, "{ [i] -> [j] : false }");
5441 isl_map_free(map);
5442 if (equal < 0)
5443 return -1;
5445 return 0;
5448 int test_eliminate(isl_ctx *ctx)
5450 const char *str;
5451 isl_map *map;
5452 int equal;
5454 str = "{ [i] -> [j] : i = 2j }";
5455 map = isl_map_read_from_str(ctx, str);
5456 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
5457 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
5458 isl_map_free(map);
5459 if (equal < 0)
5460 return -1;
5462 return 0;
5465 /* Check that isl_set_dim_residue_class detects that the values of j
5466 * in the set below are all odd and that it does not detect any spurious
5467 * strides.
5469 static int test_residue_class(isl_ctx *ctx)
5471 const char *str;
5472 isl_set *set;
5473 isl_int m, r;
5474 int res;
5476 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
5477 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
5478 set = isl_set_read_from_str(ctx, str);
5479 isl_int_init(m);
5480 isl_int_init(r);
5481 res = isl_set_dim_residue_class(set, 1, &m, &r);
5482 if (res >= 0 &&
5483 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
5484 isl_die(ctx, isl_error_unknown, "incorrect residue class",
5485 res = -1);
5486 isl_int_clear(r);
5487 isl_int_clear(m);
5488 isl_set_free(set);
5490 return res;
5493 int test_align_parameters(isl_ctx *ctx)
5495 const char *str;
5496 isl_space *space;
5497 isl_multi_aff *ma1, *ma2;
5498 int equal;
5500 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
5501 ma1 = isl_multi_aff_read_from_str(ctx, str);
5503 space = isl_space_params_alloc(ctx, 1);
5504 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
5505 ma1 = isl_multi_aff_align_params(ma1, space);
5507 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
5508 ma2 = isl_multi_aff_read_from_str(ctx, str);
5510 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5512 isl_multi_aff_free(ma1);
5513 isl_multi_aff_free(ma2);
5515 if (equal < 0)
5516 return -1;
5517 if (!equal)
5518 isl_die(ctx, isl_error_unknown,
5519 "result not as expected", return -1);
5521 return 0;
5524 static int test_list(isl_ctx *ctx)
5526 isl_id *a, *b, *c, *d, *id;
5527 isl_id_list *list;
5528 int ok;
5530 a = isl_id_alloc(ctx, "a", NULL);
5531 b = isl_id_alloc(ctx, "b", NULL);
5532 c = isl_id_alloc(ctx, "c", NULL);
5533 d = isl_id_alloc(ctx, "d", NULL);
5535 list = isl_id_list_alloc(ctx, 4);
5536 list = isl_id_list_add(list, b);
5537 list = isl_id_list_insert(list, 0, a);
5538 list = isl_id_list_add(list, c);
5539 list = isl_id_list_add(list, d);
5540 list = isl_id_list_drop(list, 1, 1);
5542 if (isl_id_list_n_id(list) != 3) {
5543 isl_id_list_free(list);
5544 isl_die(ctx, isl_error_unknown,
5545 "unexpected number of elements in list", return -1);
5548 id = isl_id_list_get_id(list, 0);
5549 ok = id == a;
5550 isl_id_free(id);
5551 id = isl_id_list_get_id(list, 1);
5552 ok = ok && id == c;
5553 isl_id_free(id);
5554 id = isl_id_list_get_id(list, 2);
5555 ok = ok && id == d;
5556 isl_id_free(id);
5558 isl_id_list_free(list);
5560 if (!ok)
5561 isl_die(ctx, isl_error_unknown,
5562 "unexpected elements in list", return -1);
5564 return 0;
5567 const char *set_conversion_tests[] = {
5568 "[N] -> { [i] : N - 1 <= 2 i <= N }",
5569 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
5570 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5571 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5572 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
5573 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
5574 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
5575 "-3 + c <= d <= 28 + c) }",
5578 /* Check that converting from isl_set to isl_pw_multi_aff and back
5579 * to isl_set produces the original isl_set.
5581 static int test_set_conversion(isl_ctx *ctx)
5583 int i;
5584 const char *str;
5585 isl_set *set1, *set2;
5586 isl_pw_multi_aff *pma;
5587 int equal;
5589 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
5590 str = set_conversion_tests[i];
5591 set1 = isl_set_read_from_str(ctx, str);
5592 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
5593 set2 = isl_set_from_pw_multi_aff(pma);
5594 equal = isl_set_is_equal(set1, set2);
5595 isl_set_free(set1);
5596 isl_set_free(set2);
5598 if (equal < 0)
5599 return -1;
5600 if (!equal)
5601 isl_die(ctx, isl_error_unknown, "bad conversion",
5602 return -1);
5605 return 0;
5608 const char *conversion_tests[] = {
5609 "{ [a, b, c, d] -> s0[a, b, e, f] : "
5610 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
5611 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
5612 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
5613 "9e <= -2 - 2a) }",
5614 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
5615 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
5616 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
5619 /* Check that converting from isl_map to isl_pw_multi_aff and back
5620 * to isl_map produces the original isl_map.
5622 static int test_map_conversion(isl_ctx *ctx)
5624 int i;
5625 isl_map *map1, *map2;
5626 isl_pw_multi_aff *pma;
5627 int equal;
5629 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
5630 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
5631 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
5632 map2 = isl_map_from_pw_multi_aff(pma);
5633 equal = isl_map_is_equal(map1, map2);
5634 isl_map_free(map1);
5635 isl_map_free(map2);
5637 if (equal < 0)
5638 return -1;
5639 if (!equal)
5640 isl_die(ctx, isl_error_unknown, "bad conversion",
5641 return -1);
5644 return 0;
5647 static int test_conversion(isl_ctx *ctx)
5649 if (test_set_conversion(ctx) < 0)
5650 return -1;
5651 if (test_map_conversion(ctx) < 0)
5652 return -1;
5653 return 0;
5656 /* Check that isl_basic_map_curry does not modify input.
5658 static int test_curry(isl_ctx *ctx)
5660 const char *str;
5661 isl_basic_map *bmap1, *bmap2;
5662 int equal;
5664 str = "{ [A[] -> B[]] -> C[] }";
5665 bmap1 = isl_basic_map_read_from_str(ctx, str);
5666 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
5667 equal = isl_basic_map_is_equal(bmap1, bmap2);
5668 isl_basic_map_free(bmap1);
5669 isl_basic_map_free(bmap2);
5671 if (equal < 0)
5672 return -1;
5673 if (equal)
5674 isl_die(ctx, isl_error_unknown,
5675 "curried map should not be equal to original",
5676 return -1);
5678 return 0;
5681 struct {
5682 const char *set;
5683 const char *ma;
5684 const char *res;
5685 } preimage_tests[] = {
5686 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
5687 "{ A[j,i] -> B[i,j] }",
5688 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
5689 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5690 "{ A[a,b] -> B[a/2,b/6] }",
5691 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
5692 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5693 "{ A[a,b] -> B[a/2,b/6] }",
5694 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
5695 "exists i,j : a = 2 i and b = 6 j }" },
5696 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
5697 "[n] -> { : 0 <= n <= 100 }" },
5698 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5699 "{ A[a] -> B[2a] }",
5700 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
5701 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5702 "{ A[a] -> B[([a/2])] }",
5703 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
5704 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
5705 "{ A[a] -> B[a,a,a/3] }",
5706 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
5707 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
5708 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
5711 static int test_preimage_basic_set(isl_ctx *ctx)
5713 int i;
5714 isl_basic_set *bset1, *bset2;
5715 isl_multi_aff *ma;
5716 int equal;
5718 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
5719 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
5720 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
5721 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
5722 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
5723 equal = isl_basic_set_is_equal(bset1, bset2);
5724 isl_basic_set_free(bset1);
5725 isl_basic_set_free(bset2);
5726 if (equal < 0)
5727 return -1;
5728 if (!equal)
5729 isl_die(ctx, isl_error_unknown, "bad preimage",
5730 return -1);
5733 return 0;
5736 struct {
5737 const char *map;
5738 const char *ma;
5739 const char *res;
5740 } preimage_domain_tests[] = {
5741 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
5742 "{ A[j,i] -> B[i,j] }",
5743 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
5744 { "{ B[i] -> C[i]; D[i] -> E[i] }",
5745 "{ A[i] -> B[i + 1] }",
5746 "{ A[i] -> C[i + 1] }" },
5747 { "{ B[i] -> C[i]; B[i] -> E[i] }",
5748 "{ A[i] -> B[i + 1] }",
5749 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
5750 { "{ B[i] -> C[([i/2])] }",
5751 "{ A[i] -> B[2i] }",
5752 "{ A[i] -> C[i] }" },
5753 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
5754 "{ A[i] -> B[([i/5]), ([i/7])] }",
5755 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
5756 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
5757 "[N] -> { A[] -> B[([N/5])] }",
5758 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
5759 { "{ B[i] -> C[i] : exists a : i = 5 a }",
5760 "{ A[i] -> B[2i] }",
5761 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
5762 { "{ B[i] -> C[i] : exists a : i = 2 a; "
5763 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
5764 "{ A[i] -> B[2i] }",
5765 "{ A[i] -> C[2i] }" },
5766 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
5767 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
5770 static int test_preimage_union_map(isl_ctx *ctx)
5772 int i;
5773 isl_union_map *umap1, *umap2;
5774 isl_multi_aff *ma;
5775 int equal;
5777 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
5778 umap1 = isl_union_map_read_from_str(ctx,
5779 preimage_domain_tests[i].map);
5780 ma = isl_multi_aff_read_from_str(ctx,
5781 preimage_domain_tests[i].ma);
5782 umap2 = isl_union_map_read_from_str(ctx,
5783 preimage_domain_tests[i].res);
5784 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
5785 equal = isl_union_map_is_equal(umap1, umap2);
5786 isl_union_map_free(umap1);
5787 isl_union_map_free(umap2);
5788 if (equal < 0)
5789 return -1;
5790 if (!equal)
5791 isl_die(ctx, isl_error_unknown, "bad preimage",
5792 return -1);
5795 return 0;
5798 static int test_preimage(isl_ctx *ctx)
5800 if (test_preimage_basic_set(ctx) < 0)
5801 return -1;
5802 if (test_preimage_union_map(ctx) < 0)
5803 return -1;
5805 return 0;
5808 struct {
5809 const char *ma1;
5810 const char *ma;
5811 const char *res;
5812 } pullback_tests[] = {
5813 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
5814 "{ A[a,b] -> C[b + 2a] }" },
5815 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
5816 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
5817 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
5818 "{ A[a] -> C[(a)/6] }" },
5819 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
5820 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
5821 "{ A[a] -> C[(2a)/3] }" },
5822 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
5823 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
5824 "{ A[i,j] -> C[i + j, i + j] }"},
5825 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
5826 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
5827 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
5828 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
5829 "{ [i, j] -> [floor((i)/3), j] }",
5830 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
5833 static int test_pullback(isl_ctx *ctx)
5835 int i;
5836 isl_multi_aff *ma1, *ma2;
5837 isl_multi_aff *ma;
5838 int equal;
5840 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
5841 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
5842 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
5843 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
5844 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
5845 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5846 isl_multi_aff_free(ma1);
5847 isl_multi_aff_free(ma2);
5848 if (equal < 0)
5849 return -1;
5850 if (!equal)
5851 isl_die(ctx, isl_error_unknown, "bad pullback",
5852 return -1);
5855 return 0;
5858 /* Check that negation is printed correctly and that equal expressions
5859 * are correctly identified.
5861 static int test_ast(isl_ctx *ctx)
5863 isl_ast_expr *expr, *expr1, *expr2, *expr3;
5864 char *str;
5865 int ok, equal;
5867 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5868 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5869 expr = isl_ast_expr_add(expr1, expr2);
5870 expr2 = isl_ast_expr_copy(expr);
5871 expr = isl_ast_expr_neg(expr);
5872 expr2 = isl_ast_expr_neg(expr2);
5873 equal = isl_ast_expr_is_equal(expr, expr2);
5874 str = isl_ast_expr_to_C_str(expr);
5875 ok = str ? !strcmp(str, "-(A + B)") : -1;
5876 free(str);
5877 isl_ast_expr_free(expr);
5878 isl_ast_expr_free(expr2);
5880 if (ok < 0 || equal < 0)
5881 return -1;
5882 if (!equal)
5883 isl_die(ctx, isl_error_unknown,
5884 "equal expressions not considered equal", return -1);
5885 if (!ok)
5886 isl_die(ctx, isl_error_unknown,
5887 "isl_ast_expr printed incorrectly", return -1);
5889 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5890 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5891 expr = isl_ast_expr_add(expr1, expr2);
5892 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
5893 expr = isl_ast_expr_sub(expr3, expr);
5894 str = isl_ast_expr_to_C_str(expr);
5895 ok = str ? !strcmp(str, "C - (A + B)") : -1;
5896 free(str);
5897 isl_ast_expr_free(expr);
5899 if (ok < 0)
5900 return -1;
5901 if (!ok)
5902 isl_die(ctx, isl_error_unknown,
5903 "isl_ast_expr printed incorrectly", return -1);
5905 return 0;
5908 /* Check that isl_ast_build_expr_from_set returns a valid expression
5909 * for an empty set. Note that isl_ast_build_expr_from_set getting
5910 * called on an empty set probably indicates a bug in the caller.
5912 static int test_ast_build(isl_ctx *ctx)
5914 isl_set *set;
5915 isl_ast_build *build;
5916 isl_ast_expr *expr;
5918 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5919 build = isl_ast_build_from_context(set);
5921 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
5922 expr = isl_ast_build_expr_from_set(build, set);
5924 isl_ast_expr_free(expr);
5925 isl_ast_build_free(build);
5927 if (!expr)
5928 return -1;
5930 return 0;
5933 /* Internal data structure for before_for and after_for callbacks.
5935 * depth is the current depth
5936 * before is the number of times before_for has been called
5937 * after is the number of times after_for has been called
5939 struct isl_test_codegen_data {
5940 int depth;
5941 int before;
5942 int after;
5945 /* This function is called before each for loop in the AST generated
5946 * from test_ast_gen1.
5948 * Increment the number of calls and the depth.
5949 * Check that the space returned by isl_ast_build_get_schedule_space
5950 * matches the target space of the schedule returned by
5951 * isl_ast_build_get_schedule.
5952 * Return an isl_id that is checked by the corresponding call
5953 * to after_for.
5955 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
5956 void *user)
5958 struct isl_test_codegen_data *data = user;
5959 isl_ctx *ctx;
5960 isl_space *space;
5961 isl_union_map *schedule;
5962 isl_union_set *uset;
5963 isl_set *set;
5964 int empty;
5965 char name[] = "d0";
5967 ctx = isl_ast_build_get_ctx(build);
5969 if (data->before >= 3)
5970 isl_die(ctx, isl_error_unknown,
5971 "unexpected number of for nodes", return NULL);
5972 if (data->depth >= 2)
5973 isl_die(ctx, isl_error_unknown,
5974 "unexpected depth", return NULL);
5976 snprintf(name, sizeof(name), "d%d", data->depth);
5977 data->before++;
5978 data->depth++;
5980 schedule = isl_ast_build_get_schedule(build);
5981 uset = isl_union_map_range(schedule);
5982 if (!uset)
5983 return NULL;
5984 if (isl_union_set_n_set(uset) != 1) {
5985 isl_union_set_free(uset);
5986 isl_die(ctx, isl_error_unknown,
5987 "expecting single range space", return NULL);
5990 space = isl_ast_build_get_schedule_space(build);
5991 set = isl_union_set_extract_set(uset, space);
5992 isl_union_set_free(uset);
5993 empty = isl_set_is_empty(set);
5994 isl_set_free(set);
5996 if (empty < 0)
5997 return NULL;
5998 if (empty)
5999 isl_die(ctx, isl_error_unknown,
6000 "spaces don't match", return NULL);
6002 return isl_id_alloc(ctx, name, NULL);
6005 /* This function is called after each for loop in the AST generated
6006 * from test_ast_gen1.
6008 * Increment the number of calls and decrement the depth.
6009 * Check that the annotation attached to the node matches
6010 * the isl_id returned by the corresponding call to before_for.
6012 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
6013 __isl_keep isl_ast_build *build, void *user)
6015 struct isl_test_codegen_data *data = user;
6016 isl_id *id;
6017 const char *name;
6018 int valid;
6020 data->after++;
6021 data->depth--;
6023 if (data->after > data->before)
6024 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
6025 "mismatch in number of for nodes",
6026 return isl_ast_node_free(node));
6028 id = isl_ast_node_get_annotation(node);
6029 if (!id)
6030 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
6031 "missing annotation", return isl_ast_node_free(node));
6033 name = isl_id_get_name(id);
6034 valid = name && atoi(name + 1) == data->depth;
6035 isl_id_free(id);
6037 if (!valid)
6038 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
6039 "wrong annotation", return isl_ast_node_free(node));
6041 return node;
6044 /* Check that the before_each_for and after_each_for callbacks
6045 * are called for each for loop in the generated code,
6046 * that they are called in the right order and that the isl_id
6047 * returned from the before_each_for callback is attached to
6048 * the isl_ast_node passed to the corresponding after_each_for call.
6050 static int test_ast_gen1(isl_ctx *ctx)
6052 const char *str;
6053 isl_set *set;
6054 isl_union_map *schedule;
6055 isl_ast_build *build;
6056 isl_ast_node *tree;
6057 struct isl_test_codegen_data data;
6059 str = "[N] -> { : N >= 10 }";
6060 set = isl_set_read_from_str(ctx, str);
6061 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
6062 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
6063 schedule = isl_union_map_read_from_str(ctx, str);
6065 data.before = 0;
6066 data.after = 0;
6067 data.depth = 0;
6068 build = isl_ast_build_from_context(set);
6069 build = isl_ast_build_set_before_each_for(build,
6070 &before_for, &data);
6071 build = isl_ast_build_set_after_each_for(build,
6072 &after_for, &data);
6073 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6074 isl_ast_build_free(build);
6075 if (!tree)
6076 return -1;
6078 isl_ast_node_free(tree);
6080 if (data.before != 3 || data.after != 3)
6081 isl_die(ctx, isl_error_unknown,
6082 "unexpected number of for nodes", return -1);
6084 return 0;
6087 /* Check that the AST generator handles domains that are integrally disjoint
6088 * but not rationally disjoint.
6090 static int test_ast_gen2(isl_ctx *ctx)
6092 const char *str;
6093 isl_set *set;
6094 isl_union_map *schedule;
6095 isl_union_map *options;
6096 isl_ast_build *build;
6097 isl_ast_node *tree;
6099 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
6100 schedule = isl_union_map_read_from_str(ctx, str);
6101 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6102 build = isl_ast_build_from_context(set);
6104 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
6105 options = isl_union_map_read_from_str(ctx, str);
6106 build = isl_ast_build_set_options(build, options);
6107 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6108 isl_ast_build_free(build);
6109 if (!tree)
6110 return -1;
6111 isl_ast_node_free(tree);
6113 return 0;
6116 /* Increment *user on each call.
6118 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
6119 __isl_keep isl_ast_build *build, void *user)
6121 int *n = user;
6123 (*n)++;
6125 return node;
6128 /* Test that unrolling tries to minimize the number of instances.
6129 * In particular, for the schedule given below, make sure it generates
6130 * 3 nodes (rather than 101).
6132 static int test_ast_gen3(isl_ctx *ctx)
6134 const char *str;
6135 isl_set *set;
6136 isl_union_map *schedule;
6137 isl_union_map *options;
6138 isl_ast_build *build;
6139 isl_ast_node *tree;
6140 int n_domain = 0;
6142 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
6143 schedule = isl_union_map_read_from_str(ctx, str);
6144 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6146 str = "{ [i] -> unroll[0] }";
6147 options = isl_union_map_read_from_str(ctx, str);
6149 build = isl_ast_build_from_context(set);
6150 build = isl_ast_build_set_options(build, options);
6151 build = isl_ast_build_set_at_each_domain(build,
6152 &count_domains, &n_domain);
6153 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6154 isl_ast_build_free(build);
6155 if (!tree)
6156 return -1;
6158 isl_ast_node_free(tree);
6160 if (n_domain != 3)
6161 isl_die(ctx, isl_error_unknown,
6162 "unexpected number of for nodes", return -1);
6164 return 0;
6167 /* Check that if the ast_build_exploit_nested_bounds options is set,
6168 * we do not get an outer if node in the generated AST,
6169 * while we do get such an outer if node if the options is not set.
6171 static int test_ast_gen4(isl_ctx *ctx)
6173 const char *str;
6174 isl_set *set;
6175 isl_union_map *schedule;
6176 isl_ast_build *build;
6177 isl_ast_node *tree;
6178 enum isl_ast_node_type type;
6179 int enb;
6181 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
6182 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
6184 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
6186 schedule = isl_union_map_read_from_str(ctx, str);
6187 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6188 build = isl_ast_build_from_context(set);
6189 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6190 isl_ast_build_free(build);
6191 if (!tree)
6192 return -1;
6194 type = isl_ast_node_get_type(tree);
6195 isl_ast_node_free(tree);
6197 if (type == isl_ast_node_if)
6198 isl_die(ctx, isl_error_unknown,
6199 "not expecting if node", return -1);
6201 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
6203 schedule = isl_union_map_read_from_str(ctx, str);
6204 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6205 build = isl_ast_build_from_context(set);
6206 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6207 isl_ast_build_free(build);
6208 if (!tree)
6209 return -1;
6211 type = isl_ast_node_get_type(tree);
6212 isl_ast_node_free(tree);
6214 if (type != isl_ast_node_if)
6215 isl_die(ctx, isl_error_unknown,
6216 "expecting if node", return -1);
6218 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
6220 return 0;
6223 /* This function is called for each leaf in the AST generated
6224 * from test_ast_gen5.
6226 * We finalize the AST generation by extending the outer schedule
6227 * with a zero-dimensional schedule. If this results in any for loops,
6228 * then this means that we did not pass along enough information
6229 * about the outer schedule to the inner AST generation.
6231 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
6232 void *user)
6234 isl_union_map *schedule, *extra;
6235 isl_ast_node *tree;
6237 schedule = isl_ast_build_get_schedule(build);
6238 extra = isl_union_map_copy(schedule);
6239 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
6240 schedule = isl_union_map_range_product(schedule, extra);
6241 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6242 isl_ast_build_free(build);
6244 if (!tree)
6245 return NULL;
6247 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
6248 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
6249 "code should not contain any for loop",
6250 return isl_ast_node_free(tree));
6252 return tree;
6255 /* Check that we do not lose any information when going back and
6256 * forth between internal and external schedule.
6258 * In particular, we create an AST where we unroll the only
6259 * non-constant dimension in the schedule. We therefore do
6260 * not expect any for loops in the AST. However, older versions
6261 * of isl would not pass along enough information about the outer
6262 * schedule when performing an inner code generation from a create_leaf
6263 * callback, resulting in the inner code generation producing a for loop.
6265 static int test_ast_gen5(isl_ctx *ctx)
6267 const char *str;
6268 isl_set *set;
6269 isl_union_map *schedule, *options;
6270 isl_ast_build *build;
6271 isl_ast_node *tree;
6273 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
6274 schedule = isl_union_map_read_from_str(ctx, str);
6276 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
6277 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
6278 options = isl_union_map_read_from_str(ctx, str);
6280 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6281 build = isl_ast_build_from_context(set);
6282 build = isl_ast_build_set_options(build, options);
6283 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
6284 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6285 isl_ast_build_free(build);
6286 isl_ast_node_free(tree);
6287 if (!tree)
6288 return -1;
6290 return 0;
6293 /* Check that the expression
6295 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
6297 * is not combined into
6299 * min(n/2, 0)
6301 * as this would result in n/2 being evaluated in parts of
6302 * the definition domain where n is not a multiple of 2.
6304 static int test_ast_expr(isl_ctx *ctx)
6306 const char *str;
6307 isl_pw_aff *pa;
6308 isl_ast_build *build;
6309 isl_ast_expr *expr;
6310 int min_max;
6311 int is_min;
6313 min_max = isl_options_get_ast_build_detect_min_max(ctx);
6314 isl_options_set_ast_build_detect_min_max(ctx, 1);
6316 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
6317 pa = isl_pw_aff_read_from_str(ctx, str);
6318 build = isl_ast_build_alloc(ctx);
6319 expr = isl_ast_build_expr_from_pw_aff(build, pa);
6320 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
6321 isl_ast_expr_get_op_type(expr) == isl_ast_op_min;
6322 isl_ast_build_free(build);
6323 isl_ast_expr_free(expr);
6325 isl_options_set_ast_build_detect_min_max(ctx, min_max);
6327 if (!expr)
6328 return -1;
6329 if (is_min)
6330 isl_die(ctx, isl_error_unknown,
6331 "expressions should not be combined", return -1);
6333 return 0;
6336 static int test_ast_gen(isl_ctx *ctx)
6338 if (test_ast_gen1(ctx) < 0)
6339 return -1;
6340 if (test_ast_gen2(ctx) < 0)
6341 return -1;
6342 if (test_ast_gen3(ctx) < 0)
6343 return -1;
6344 if (test_ast_gen4(ctx) < 0)
6345 return -1;
6346 if (test_ast_gen5(ctx) < 0)
6347 return -1;
6348 if (test_ast_expr(ctx) < 0)
6349 return -1;
6350 return 0;
6353 /* Check if dropping output dimensions from an isl_pw_multi_aff
6354 * works properly.
6356 static int test_pw_multi_aff(isl_ctx *ctx)
6358 const char *str;
6359 isl_pw_multi_aff *pma1, *pma2;
6360 int equal;
6362 str = "{ [i,j] -> [i+j, 4i-j] }";
6363 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
6364 str = "{ [i,j] -> [4i-j] }";
6365 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6367 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
6369 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6371 isl_pw_multi_aff_free(pma1);
6372 isl_pw_multi_aff_free(pma2);
6373 if (equal < 0)
6374 return -1;
6375 if (!equal)
6376 isl_die(ctx, isl_error_unknown,
6377 "expressions not equal", return -1);
6379 return 0;
6382 /* Check that we can properly parse multi piecewise affine expressions
6383 * where the piecewise affine expressions have different domains.
6385 static int test_multi_pw_aff(isl_ctx *ctx)
6387 const char *str;
6388 isl_set *dom, *dom2;
6389 isl_multi_pw_aff *mpa1, *mpa2;
6390 isl_pw_aff *pa;
6391 int equal;
6392 int equal_domain;
6394 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
6395 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
6396 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
6397 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
6398 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
6399 str = "{ [i] -> [(i : i > 0), 2i] }";
6400 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
6402 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
6404 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
6405 dom = isl_pw_aff_domain(pa);
6406 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
6407 dom2 = isl_pw_aff_domain(pa);
6408 equal_domain = isl_set_is_equal(dom, dom2);
6410 isl_set_free(dom);
6411 isl_set_free(dom2);
6412 isl_multi_pw_aff_free(mpa1);
6413 isl_multi_pw_aff_free(mpa2);
6415 if (equal < 0)
6416 return -1;
6417 if (!equal)
6418 isl_die(ctx, isl_error_unknown,
6419 "expressions not equal", return -1);
6421 if (equal_domain < 0)
6422 return -1;
6423 if (equal_domain)
6424 isl_die(ctx, isl_error_unknown,
6425 "domains unexpectedly equal", return -1);
6427 return 0;
6430 /* This is a regression test for a bug where isl_basic_map_simplify
6431 * would end up in an infinite loop. In particular, we construct
6432 * an empty basic set that is not obviously empty.
6433 * isl_basic_set_is_empty marks the basic set as empty.
6434 * After projecting out i3, the variable can be dropped completely,
6435 * but isl_basic_map_simplify refrains from doing so if the basic set
6436 * is empty and would end up in an infinite loop if it didn't test
6437 * explicitly for empty basic maps in the outer loop.
6439 static int test_simplify_1(isl_ctx *ctx)
6441 const char *str;
6442 isl_basic_set *bset;
6443 int empty;
6445 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
6446 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
6447 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
6448 "i3 >= i2 }";
6449 bset = isl_basic_set_read_from_str(ctx, str);
6450 empty = isl_basic_set_is_empty(bset);
6451 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
6452 isl_basic_set_free(bset);
6453 if (!bset)
6454 return -1;
6455 if (!empty)
6456 isl_die(ctx, isl_error_unknown,
6457 "basic set should be empty", return -1);
6459 return 0;
6462 /* Check that the equality in the set description below
6463 * is simplified away.
6465 static int test_simplify_2(isl_ctx *ctx)
6467 const char *str;
6468 isl_basic_set *bset;
6469 isl_bool universe;
6471 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
6472 bset = isl_basic_set_read_from_str(ctx, str);
6473 universe = isl_basic_set_plain_is_universe(bset);
6474 isl_basic_set_free(bset);
6476 if (universe < 0)
6477 return -1;
6478 if (!universe)
6479 isl_die(ctx, isl_error_unknown,
6480 "equality not simplified away", return -1);
6481 return 0;
6484 /* Some simplification tests.
6486 static int test_simplify(isl_ctx *ctx)
6488 if (test_simplify_1(ctx) < 0)
6489 return -1;
6490 if (test_simplify_2(ctx) < 0)
6491 return -1;
6492 return 0;
6495 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
6496 * with gbr context would fail to disable the use of the shifted tableau
6497 * when transferring equalities for the input to the context, resulting
6498 * in invalid sample values.
6500 static int test_partial_lexmin(isl_ctx *ctx)
6502 const char *str;
6503 isl_basic_set *bset;
6504 isl_basic_map *bmap;
6505 isl_map *map;
6507 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
6508 bmap = isl_basic_map_read_from_str(ctx, str);
6509 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
6510 bset = isl_basic_set_read_from_str(ctx, str);
6511 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
6512 isl_map_free(map);
6514 if (!map)
6515 return -1;
6517 return 0;
6520 /* Check that the variable compression performed on the existentially
6521 * quantified variables inside isl_basic_set_compute_divs is not confused
6522 * by the implicit equalities among the parameters.
6524 static int test_compute_divs(isl_ctx *ctx)
6526 const char *str;
6527 isl_basic_set *bset;
6528 isl_set *set;
6530 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
6531 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
6532 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
6533 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
6534 bset = isl_basic_set_read_from_str(ctx, str);
6535 set = isl_basic_set_compute_divs(bset);
6536 isl_set_free(set);
6537 if (!set)
6538 return -1;
6540 return 0;
6543 /* Check that the reaching domain elements and the prefix schedule
6544 * at a leaf node are the same before and after grouping.
6546 static int test_schedule_tree_group_1(isl_ctx *ctx)
6548 int equal;
6549 const char *str;
6550 isl_id *id;
6551 isl_union_set *uset;
6552 isl_multi_union_pw_aff *mupa;
6553 isl_union_pw_multi_aff *upma1, *upma2;
6554 isl_union_set *domain1, *domain2;
6555 isl_union_map *umap1, *umap2;
6556 isl_schedule_node *node;
6558 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
6559 uset = isl_union_set_read_from_str(ctx, str);
6560 node = isl_schedule_node_from_domain(uset);
6561 node = isl_schedule_node_child(node, 0);
6562 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
6563 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6564 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6565 node = isl_schedule_node_child(node, 0);
6566 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
6567 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6568 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6569 node = isl_schedule_node_child(node, 0);
6570 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
6571 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
6572 domain1 = isl_schedule_node_get_domain(node);
6573 id = isl_id_alloc(ctx, "group", NULL);
6574 node = isl_schedule_node_parent(node);
6575 node = isl_schedule_node_group(node, id);
6576 node = isl_schedule_node_child(node, 0);
6577 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
6578 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
6579 domain2 = isl_schedule_node_get_domain(node);
6580 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
6581 if (equal >= 0 && equal)
6582 equal = isl_union_set_is_equal(domain1, domain2);
6583 if (equal >= 0 && equal)
6584 equal = isl_union_map_is_equal(umap1, umap2);
6585 isl_union_map_free(umap1);
6586 isl_union_map_free(umap2);
6587 isl_union_set_free(domain1);
6588 isl_union_set_free(domain2);
6589 isl_union_pw_multi_aff_free(upma1);
6590 isl_union_pw_multi_aff_free(upma2);
6591 isl_schedule_node_free(node);
6593 if (equal < 0)
6594 return -1;
6595 if (!equal)
6596 isl_die(ctx, isl_error_unknown,
6597 "expressions not equal", return -1);
6599 return 0;
6602 /* Check that we can have nested groupings and that the union map
6603 * schedule representation is the same before and after the grouping.
6604 * Note that after the grouping, the union map representation contains
6605 * the domain constraints from the ranges of the expansion nodes,
6606 * while they are missing from the union map representation of
6607 * the tree without expansion nodes.
6609 * Also check that the global expansion is as expected.
6611 static int test_schedule_tree_group_2(isl_ctx *ctx)
6613 int equal, equal_expansion;
6614 const char *str;
6615 isl_id *id;
6616 isl_union_set *uset;
6617 isl_union_map *umap1, *umap2;
6618 isl_union_map *expansion1, *expansion2;
6619 isl_union_set_list *filters;
6620 isl_multi_union_pw_aff *mupa;
6621 isl_schedule *schedule;
6622 isl_schedule_node *node;
6624 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
6625 "S3[i,j] : 0 <= i,j < 10 }";
6626 uset = isl_union_set_read_from_str(ctx, str);
6627 node = isl_schedule_node_from_domain(uset);
6628 node = isl_schedule_node_child(node, 0);
6629 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
6630 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6631 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6632 node = isl_schedule_node_child(node, 0);
6633 str = "{ S1[i,j] }";
6634 uset = isl_union_set_read_from_str(ctx, str);
6635 filters = isl_union_set_list_from_union_set(uset);
6636 str = "{ S2[i,j]; S3[i,j] }";
6637 uset = isl_union_set_read_from_str(ctx, str);
6638 filters = isl_union_set_list_add(filters, uset);
6639 node = isl_schedule_node_insert_sequence(node, filters);
6640 node = isl_schedule_node_child(node, 1);
6641 node = isl_schedule_node_child(node, 0);
6642 str = "{ S2[i,j] }";
6643 uset = isl_union_set_read_from_str(ctx, str);
6644 filters = isl_union_set_list_from_union_set(uset);
6645 str = "{ S3[i,j] }";
6646 uset = isl_union_set_read_from_str(ctx, str);
6647 filters = isl_union_set_list_add(filters, uset);
6648 node = isl_schedule_node_insert_sequence(node, filters);
6650 schedule = isl_schedule_node_get_schedule(node);
6651 umap1 = isl_schedule_get_map(schedule);
6652 uset = isl_schedule_get_domain(schedule);
6653 umap1 = isl_union_map_intersect_domain(umap1, uset);
6654 isl_schedule_free(schedule);
6656 node = isl_schedule_node_parent(node);
6657 node = isl_schedule_node_parent(node);
6658 id = isl_id_alloc(ctx, "group1", NULL);
6659 node = isl_schedule_node_group(node, id);
6660 node = isl_schedule_node_child(node, 1);
6661 node = isl_schedule_node_child(node, 0);
6662 id = isl_id_alloc(ctx, "group2", NULL);
6663 node = isl_schedule_node_group(node, id);
6665 schedule = isl_schedule_node_get_schedule(node);
6666 umap2 = isl_schedule_get_map(schedule);
6667 isl_schedule_free(schedule);
6669 node = isl_schedule_node_root(node);
6670 node = isl_schedule_node_child(node, 0);
6671 expansion1 = isl_schedule_node_get_subtree_expansion(node);
6672 isl_schedule_node_free(node);
6674 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
6675 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
6676 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
6678 expansion2 = isl_union_map_read_from_str(ctx, str);
6680 equal = isl_union_map_is_equal(umap1, umap2);
6681 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
6683 isl_union_map_free(umap1);
6684 isl_union_map_free(umap2);
6685 isl_union_map_free(expansion1);
6686 isl_union_map_free(expansion2);
6688 if (equal < 0 || equal_expansion < 0)
6689 return -1;
6690 if (!equal)
6691 isl_die(ctx, isl_error_unknown,
6692 "expressions not equal", return -1);
6693 if (!equal_expansion)
6694 isl_die(ctx, isl_error_unknown,
6695 "unexpected expansion", return -1);
6697 return 0;
6700 /* Some tests for the isl_schedule_node_group function.
6702 static int test_schedule_tree_group(isl_ctx *ctx)
6704 if (test_schedule_tree_group_1(ctx) < 0)
6705 return -1;
6706 if (test_schedule_tree_group_2(ctx) < 0)
6707 return -1;
6708 return 0;
6711 struct {
6712 const char *set;
6713 const char *dual;
6714 } coef_tests[] = {
6715 { "{ rat: [i] : 0 <= i <= 10 }",
6716 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
6717 { "{ rat: [i] : FALSE }",
6718 "{ rat: coefficients[[cst] -> [a]] }" },
6719 { "{ rat: [i] : }",
6720 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
6723 struct {
6724 const char *set;
6725 const char *dual;
6726 } sol_tests[] = {
6727 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
6728 "{ rat: [i] : 0 <= i <= 10 }" },
6729 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
6730 "{ rat: [i] }" },
6731 { "{ rat: coefficients[[cst] -> [a]] }",
6732 "{ rat: [i] : FALSE }" },
6735 /* Test the basic functionality of isl_basic_set_coefficients and
6736 * isl_basic_set_solutions.
6738 static int test_dual(isl_ctx *ctx)
6740 int i;
6742 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
6743 int equal;
6744 isl_basic_set *bset1, *bset2;
6746 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
6747 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
6748 bset1 = isl_basic_set_coefficients(bset1);
6749 equal = isl_basic_set_is_equal(bset1, bset2);
6750 isl_basic_set_free(bset1);
6751 isl_basic_set_free(bset2);
6752 if (equal < 0)
6753 return -1;
6754 if (!equal)
6755 isl_die(ctx, isl_error_unknown,
6756 "incorrect dual", return -1);
6759 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
6760 int equal;
6761 isl_basic_set *bset1, *bset2;
6763 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
6764 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
6765 bset1 = isl_basic_set_solutions(bset1);
6766 equal = isl_basic_set_is_equal(bset1, bset2);
6767 isl_basic_set_free(bset1);
6768 isl_basic_set_free(bset2);
6769 if (equal < 0)
6770 return -1;
6771 if (!equal)
6772 isl_die(ctx, isl_error_unknown,
6773 "incorrect dual", return -1);
6776 return 0;
6779 struct {
6780 int scale_tile;
6781 int shift_point;
6782 const char *domain;
6783 const char *schedule;
6784 const char *sizes;
6785 const char *tile;
6786 const char *point;
6787 } tile_tests[] = {
6788 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6789 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6790 "{ [32,32] }",
6791 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6792 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6794 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6795 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6796 "{ [32,32] }",
6797 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6798 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6800 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6801 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6802 "{ [32,32] }",
6803 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6804 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6806 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6807 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6808 "{ [32,32] }",
6809 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6810 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6814 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
6815 * tile the band and then check if the tile and point bands have the
6816 * expected partial schedule.
6818 static int test_tile(isl_ctx *ctx)
6820 int i;
6821 int scale;
6822 int shift;
6824 scale = isl_options_get_tile_scale_tile_loops(ctx);
6825 shift = isl_options_get_tile_shift_point_loops(ctx);
6827 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
6828 int opt;
6829 int equal;
6830 const char *str;
6831 isl_union_set *domain;
6832 isl_multi_union_pw_aff *mupa, *mupa2;
6833 isl_schedule_node *node;
6834 isl_multi_val *sizes;
6836 opt = tile_tests[i].scale_tile;
6837 isl_options_set_tile_scale_tile_loops(ctx, opt);
6838 opt = tile_tests[i].shift_point;
6839 isl_options_set_tile_shift_point_loops(ctx, opt);
6841 str = tile_tests[i].domain;
6842 domain = isl_union_set_read_from_str(ctx, str);
6843 node = isl_schedule_node_from_domain(domain);
6844 node = isl_schedule_node_child(node, 0);
6845 str = tile_tests[i].schedule;
6846 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6847 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6848 str = tile_tests[i].sizes;
6849 sizes = isl_multi_val_read_from_str(ctx, str);
6850 node = isl_schedule_node_band_tile(node, sizes);
6852 str = tile_tests[i].tile;
6853 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6854 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6855 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
6856 isl_multi_union_pw_aff_free(mupa);
6857 isl_multi_union_pw_aff_free(mupa2);
6859 node = isl_schedule_node_child(node, 0);
6861 str = tile_tests[i].point;
6862 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6863 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6864 if (equal >= 0 && equal)
6865 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
6866 mupa2);
6867 isl_multi_union_pw_aff_free(mupa);
6868 isl_multi_union_pw_aff_free(mupa2);
6870 isl_schedule_node_free(node);
6872 if (equal < 0)
6873 return -1;
6874 if (!equal)
6875 isl_die(ctx, isl_error_unknown,
6876 "unexpected result", return -1);
6879 isl_options_set_tile_scale_tile_loops(ctx, scale);
6880 isl_options_set_tile_shift_point_loops(ctx, shift);
6882 return 0;
6885 /* Check that the domain hash of a space is equal to the hash
6886 * of the domain of the space.
6888 static int test_domain_hash(isl_ctx *ctx)
6890 isl_map *map;
6891 isl_space *space;
6892 uint32_t hash1, hash2;
6894 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
6895 space = isl_map_get_space(map);
6896 isl_map_free(map);
6897 hash1 = isl_space_get_domain_hash(space);
6898 space = isl_space_domain(space);
6899 hash2 = isl_space_get_hash(space);
6900 isl_space_free(space);
6902 if (!space)
6903 return -1;
6904 if (hash1 != hash2)
6905 isl_die(ctx, isl_error_unknown,
6906 "domain hash not equal to hash of domain", return -1);
6908 return 0;
6911 /* Check that a universe basic set that is not obviously equal to the universe
6912 * is still recognized as being equal to the universe.
6914 static int test_universe(isl_ctx *ctx)
6916 const char *s;
6917 isl_basic_set *bset;
6918 isl_bool is_univ;
6920 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
6921 bset = isl_basic_set_read_from_str(ctx, s);
6922 is_univ = isl_basic_set_is_universe(bset);
6923 isl_basic_set_free(bset);
6925 if (is_univ < 0)
6926 return -1;
6927 if (!is_univ)
6928 isl_die(ctx, isl_error_unknown,
6929 "not recognized as universe set", return -1);
6931 return 0;
6934 /* Sets for which chambers are computed and checked.
6936 const char *chambers_tests[] = {
6937 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
6938 "z >= 0 and z <= C - y and z <= B - x - y }",
6941 /* Add the domain of "cell" to "cells".
6943 static int add_cell(__isl_take isl_cell *cell, void *user)
6945 isl_basic_set_list **cells = user;
6946 isl_basic_set *dom;
6948 dom = isl_cell_get_domain(cell);
6949 isl_cell_free(cell);
6950 *cells = isl_basic_set_list_add(*cells, dom);
6952 return *cells ? 0 : -1;
6955 /* Check that the elements of "list" are pairwise disjoint.
6957 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
6959 int i, j, n;
6961 if (!list)
6962 return isl_stat_error;
6964 n = isl_basic_set_list_n_basic_set(list);
6965 for (i = 0; i < n; ++i) {
6966 isl_basic_set *bset_i;
6968 bset_i = isl_basic_set_list_get_basic_set(list, i);
6969 for (j = i + 1; j < n; ++j) {
6970 isl_basic_set *bset_j;
6971 isl_bool disjoint;
6973 bset_j = isl_basic_set_list_get_basic_set(list, j);
6974 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
6975 isl_basic_set_free(bset_j);
6976 if (!disjoint)
6977 isl_die(isl_basic_set_list_get_ctx(list),
6978 isl_error_unknown, "not disjoint",
6979 break);
6980 if (disjoint < 0 || !disjoint)
6981 break;
6983 isl_basic_set_free(bset_i);
6984 if (j < n)
6985 return isl_stat_error;
6988 return isl_stat_ok;
6991 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
6992 * are pairwise disjoint.
6994 static int test_chambers(isl_ctx *ctx)
6996 int i;
6998 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
6999 isl_basic_set *bset;
7000 isl_vertices *vertices;
7001 isl_basic_set_list *cells;
7002 isl_stat ok;
7004 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
7005 vertices = isl_basic_set_compute_vertices(bset);
7006 cells = isl_basic_set_list_alloc(ctx, 0);
7007 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
7008 &cells) < 0)
7009 cells = isl_basic_set_list_free(cells);
7010 ok = check_pairwise_disjoint(cells);
7011 isl_basic_set_list_free(cells);
7012 isl_vertices_free(vertices);
7013 isl_basic_set_free(bset);
7015 if (ok < 0)
7016 return -1;
7019 return 0;
7022 struct {
7023 const char *name;
7024 int (*fn)(isl_ctx *ctx);
7025 } tests [] = {
7026 { "universe", &test_universe },
7027 { "domain hash", &test_domain_hash },
7028 { "dual", &test_dual },
7029 { "dependence analysis", &test_flow },
7030 { "val", &test_val },
7031 { "compute divs", &test_compute_divs },
7032 { "partial lexmin", &test_partial_lexmin },
7033 { "simplify", &test_simplify },
7034 { "curry", &test_curry },
7035 { "piecewise multi affine expressions", &test_pw_multi_aff },
7036 { "multi piecewise affine expressions", &test_multi_pw_aff },
7037 { "conversion", &test_conversion },
7038 { "list", &test_list },
7039 { "align parameters", &test_align_parameters },
7040 { "preimage", &test_preimage },
7041 { "pullback", &test_pullback },
7042 { "AST", &test_ast },
7043 { "AST build", &test_ast_build },
7044 { "AST generation", &test_ast_gen },
7045 { "eliminate", &test_eliminate },
7046 { "residue class", &test_residue_class },
7047 { "div", &test_div },
7048 { "slice", &test_slice },
7049 { "fixed power", &test_fixed_power },
7050 { "sample", &test_sample },
7051 { "output", &test_output },
7052 { "vertices", &test_vertices },
7053 { "chambers", &test_chambers },
7054 { "fixed", &test_fixed },
7055 { "equal", &test_equal },
7056 { "disjoint", &test_disjoint },
7057 { "product", &test_product },
7058 { "dim_max", &test_dim_max },
7059 { "affine", &test_aff },
7060 { "injective", &test_injective },
7061 { "schedule (whole component)", &test_schedule_whole },
7062 { "schedule (incremental)", &test_schedule_incremental },
7063 { "schedule tree grouping", &test_schedule_tree_group },
7064 { "tile", &test_tile },
7065 { "union_pw", &test_union_pw },
7066 { "eval", &test_eval },
7067 { "parse", &test_parse },
7068 { "single-valued", &test_sv },
7069 { "affine hull", &test_affine_hull },
7070 { "simple_hull", &test_simple_hull },
7071 { "coalesce", &test_coalesce },
7072 { "factorize", &test_factorize },
7073 { "subset", &test_subset },
7074 { "subtract", &test_subtract },
7075 { "intersect", &test_intersect },
7076 { "lexmin", &test_lexmin },
7077 { "min", &test_min },
7078 { "gist", &test_gist },
7079 { "piecewise quasi-polynomials", &test_pwqp },
7080 { "lift", &test_lift },
7081 { "bound", &test_bound },
7082 { "union", &test_union },
7083 { "split periods", &test_split_periods },
7084 { "lexicographic order", &test_lex },
7085 { "bijectivity", &test_bijective },
7086 { "dataflow analysis", &test_dep },
7087 { "reading", &test_read },
7088 { "bounded", &test_bounded },
7089 { "construction", &test_construction },
7090 { "dimension manipulation", &test_dim },
7091 { "map application", &test_application },
7092 { "convex hull", &test_convex_hull },
7093 { "transitive closure", &test_closure },
7096 int main(int argc, char **argv)
7098 int i;
7099 struct isl_ctx *ctx;
7100 struct isl_options *options;
7102 options = isl_options_new_with_defaults();
7103 assert(options);
7104 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
7106 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
7107 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
7108 printf("%s\n", tests[i].name);
7109 if (tests[i].fn(ctx) < 0)
7110 goto error;
7112 isl_ctx_free(ctx);
7113 return 0;
7114 error:
7115 isl_ctx_free(ctx);
7116 return -1;