isl_scheduler.c: extract out node_add_coefficient_constraints
[isl.git] / isl_test.c
blob28506fd143350492f1c1b872dd902c36e1e9266e
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <assert.h>
19 #include <stdio.h>
20 #include <limits.h>
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl_space_private.h>
25 #include <isl/set.h>
26 #include <isl/flow.h>
27 #include <isl_constraint_private.h>
28 #include <isl/polynomial.h>
29 #include <isl/union_set.h>
30 #include <isl/union_map.h>
31 #include <isl_factorization.h>
32 #include <isl/schedule.h>
33 #include <isl/schedule_node.h>
34 #include <isl_options_private.h>
35 #include <isl/vertices.h>
36 #include <isl/ast_build.h>
37 #include <isl/val.h>
38 #include <isl/ilp.h>
39 #include <isl_ast_build_expr.h>
40 #include <isl/options.h>
42 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
44 static char *srcdir;
46 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
47 char *filename;
48 int length;
49 char *pattern = "%s/test_inputs/%s.%s";
51 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
52 + strlen(suffix) + 1;
53 filename = isl_alloc_array(ctx, char, length);
55 if (!filename)
56 return NULL;
58 sprintf(filename, pattern, srcdir, name, suffix);
60 return filename;
63 void test_parse_map(isl_ctx *ctx, const char *str)
65 isl_map *map;
67 map = isl_map_read_from_str(ctx, str);
68 assert(map);
69 isl_map_free(map);
72 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
74 isl_map *map, *map2;
75 int equal;
77 map = isl_map_read_from_str(ctx, str);
78 map2 = isl_map_read_from_str(ctx, str2);
79 equal = isl_map_is_equal(map, map2);
80 isl_map_free(map);
81 isl_map_free(map2);
83 if (equal < 0)
84 return -1;
85 if (!equal)
86 isl_die(ctx, isl_error_unknown, "maps not equal",
87 return -1);
89 return 0;
92 void test_parse_pwqp(isl_ctx *ctx, const char *str)
94 isl_pw_qpolynomial *pwqp;
96 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
97 assert(pwqp);
98 isl_pw_qpolynomial_free(pwqp);
101 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
103 isl_pw_aff *pwaff;
105 pwaff = isl_pw_aff_read_from_str(ctx, str);
106 assert(pwaff);
107 isl_pw_aff_free(pwaff);
110 /* Check that we can read an isl_multi_val from "str" without errors.
112 static int test_parse_multi_val(isl_ctx *ctx, const char *str)
114 isl_multi_val *mv;
116 mv = isl_multi_val_read_from_str(ctx, str);
117 isl_multi_val_free(mv);
119 return mv ? 0 : -1;
122 /* Pairs of binary relation representations that should represent
123 * the same binary relations.
125 struct {
126 const char *map1;
127 const char *map2;
128 } parse_map_equal_tests[] = {
129 { "{ [x,y] : [([x/2]+y)/3] >= 1 }",
130 "{ [x, y] : 2y >= 6 - x }" },
131 { "{ [x,y] : x <= min(y, 2*y+3) }",
132 "{ [x,y] : x <= y, 2*y + 3 }" },
133 { "{ [x,y] : x >= min(y, 2*y+3) }",
134 "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }" },
135 { "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }",
136 "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }" },
137 { "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }",
138 "{ [i,j] -> [min(i,j)] }" },
139 { "{ [i,j] : i != j }",
140 "{ [i,j] : i < j or i > j }" },
141 { "{ [i,j] : (i+1)*2 >= j }",
142 "{ [i, j] : j <= 2 + 2i }" },
143 { "{ [i] -> [i > 0 ? 4 : 5] }",
144 "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }" },
145 { "[N=2,M] -> { [i=[(M+N)/4]] }",
146 "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }" },
147 { "{ [x] : x >= 0 }",
148 "{ [x] : x-0 >= 0 }" },
149 { "{ [i] : ((i > 10)) }",
150 "{ [i] : i >= 11 }" },
151 { "{ [i] -> [0] }",
152 "{ [i] -> [0 * i] }" },
153 { "{ [a] -> [b] : (not false) }",
154 "{ [a] -> [b] : true }" },
155 { "{ [i] : i/2 <= 5 }",
156 "{ [i] : i <= 10 }" },
157 { "{Sym=[n] [i] : i <= n }",
158 "[n] -> { [i] : i <= n }" },
159 { "{ [*] }",
160 "{ [a] }" },
161 { "{ [i] : 2*floor(i/2) = i }",
162 "{ [i] : exists a : i = 2 a }" },
163 { "{ [a] -> [b] : a = 5 implies b = 5 }",
164 "{ [a] -> [b] : a != 5 or b = 5 }" },
165 { "{ [a] -> [a - 1 : a > 0] }",
166 "{ [a] -> [a - 1] : a > 0 }" },
167 { "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
168 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }" },
169 { "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
170 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
171 { "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
172 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
173 { "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
174 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
175 { "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
176 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
177 { "{ [a,b] -> [i,j] : a,b << i,j }",
178 "{ [a,b] -> [i,j] : a < i or (a = i and b < j) }" },
179 { "{ [a,b] -> [i,j] : a,b <<= i,j }",
180 "{ [a,b] -> [i,j] : a < i or (a = i and b <= j) }" },
181 { "{ [a,b] -> [i,j] : a,b >> i,j }",
182 "{ [a,b] -> [i,j] : a > i or (a = i and b > j) }" },
183 { "{ [a,b] -> [i,j] : a,b >>= i,j }",
184 "{ [a,b] -> [i,j] : a > i or (a = i and b >= j) }" },
185 { "{ [n] -> [i] : exists (a, b, c: 8b <= i - 32a and "
186 "8b >= -7 + i - 32 a and b >= 0 and b <= 3 and "
187 "8c < n - 32a and i < n and c >= 0 and "
188 "c <= 3 and c >= -4a) }",
189 "{ [n] -> [i] : 0 <= i < n }" },
190 { "{ [x] -> [] : exists (a, b: 0 <= a <= 1 and 0 <= b <= 3 and "
191 "2b <= x - 8a and 2b >= -1 + x - 8a) }",
192 "{ [x] -> [] : 0 <= x <= 15 }" },
195 int test_parse(struct isl_ctx *ctx)
197 int i;
198 isl_map *map, *map2;
199 const char *str, *str2;
201 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
202 return -1;
203 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
204 return -1;
205 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
206 return -1;
208 str = "{ [i] -> [-i] }";
209 map = isl_map_read_from_str(ctx, str);
210 assert(map);
211 isl_map_free(map);
213 str = "{ A[i] -> L[([i/3])] }";
214 map = isl_map_read_from_str(ctx, str);
215 assert(map);
216 isl_map_free(map);
218 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
219 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
220 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
222 for (i = 0; i < ARRAY_SIZE(parse_map_equal_tests); ++i) {
223 str = parse_map_equal_tests[i].map1;
224 str2 = parse_map_equal_tests[i].map2;
225 if (test_parse_map_equal(ctx, str, str2) < 0)
226 return -1;
229 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
230 map = isl_map_read_from_str(ctx, str);
231 str = "{ [new, old] -> [o0, o1] : "
232 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
233 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
234 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
235 map2 = isl_map_read_from_str(ctx, str);
236 assert(isl_map_is_equal(map, map2));
237 isl_map_free(map);
238 isl_map_free(map2);
240 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
241 map = isl_map_read_from_str(ctx, str);
242 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
243 map2 = isl_map_read_from_str(ctx, str);
244 assert(isl_map_is_equal(map, map2));
245 isl_map_free(map);
246 isl_map_free(map2);
248 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
249 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
250 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
251 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
253 return 0;
256 static int test_read(isl_ctx *ctx)
258 char *filename;
259 FILE *input;
260 isl_basic_set *bset1, *bset2;
261 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
262 int equal;
264 filename = get_filename(ctx, "set", "omega");
265 assert(filename);
266 input = fopen(filename, "r");
267 assert(input);
269 bset1 = isl_basic_set_read_from_file(ctx, input);
270 bset2 = isl_basic_set_read_from_str(ctx, str);
272 equal = isl_basic_set_is_equal(bset1, bset2);
274 isl_basic_set_free(bset1);
275 isl_basic_set_free(bset2);
276 free(filename);
278 fclose(input);
280 if (equal < 0)
281 return -1;
282 if (!equal)
283 isl_die(ctx, isl_error_unknown,
284 "read sets not equal", return -1);
286 return 0;
289 static int test_bounded(isl_ctx *ctx)
291 isl_set *set;
292 int bounded;
294 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
295 bounded = isl_set_is_bounded(set);
296 isl_set_free(set);
298 if (bounded < 0)
299 return -1;
300 if (!bounded)
301 isl_die(ctx, isl_error_unknown,
302 "set not considered bounded", return -1);
304 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
305 bounded = isl_set_is_bounded(set);
306 assert(!bounded);
307 isl_set_free(set);
309 if (bounded < 0)
310 return -1;
311 if (bounded)
312 isl_die(ctx, isl_error_unknown,
313 "set considered bounded", return -1);
315 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
316 bounded = isl_set_is_bounded(set);
317 isl_set_free(set);
319 if (bounded < 0)
320 return -1;
321 if (bounded)
322 isl_die(ctx, isl_error_unknown,
323 "set considered bounded", return -1);
325 return 0;
328 /* Construct the basic set { [i] : 5 <= i <= N } */
329 static int test_construction(isl_ctx *ctx)
331 isl_int v;
332 isl_space *dim;
333 isl_local_space *ls;
334 isl_basic_set *bset;
335 isl_constraint *c;
337 isl_int_init(v);
339 dim = isl_space_set_alloc(ctx, 1, 1);
340 bset = isl_basic_set_universe(isl_space_copy(dim));
341 ls = isl_local_space_from_space(dim);
343 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
344 isl_int_set_si(v, -1);
345 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
346 isl_int_set_si(v, 1);
347 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
348 bset = isl_basic_set_add_constraint(bset, c);
350 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
351 isl_int_set_si(v, 1);
352 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
353 isl_int_set_si(v, -5);
354 isl_constraint_set_constant(c, v);
355 bset = isl_basic_set_add_constraint(bset, c);
357 isl_local_space_free(ls);
358 isl_basic_set_free(bset);
360 isl_int_clear(v);
362 return 0;
365 static int test_dim(isl_ctx *ctx)
367 const char *str;
368 isl_map *map1, *map2;
369 int equal;
371 map1 = isl_map_read_from_str(ctx,
372 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
373 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
374 map2 = isl_map_read_from_str(ctx,
375 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
376 equal = isl_map_is_equal(map1, map2);
377 isl_map_free(map2);
379 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
380 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
381 if (equal >= 0 && equal)
382 equal = isl_map_is_equal(map1, map2);
384 isl_map_free(map1);
385 isl_map_free(map2);
387 if (equal < 0)
388 return -1;
389 if (!equal)
390 isl_die(ctx, isl_error_unknown,
391 "unexpected result", return -1);
393 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
394 map1 = isl_map_read_from_str(ctx, str);
395 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
396 map2 = isl_map_read_from_str(ctx, str);
397 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
398 equal = isl_map_is_equal(map1, map2);
399 isl_map_free(map1);
400 isl_map_free(map2);
402 if (equal < 0)
403 return -1;
404 if (!equal)
405 isl_die(ctx, isl_error_unknown,
406 "unexpected result", return -1);
408 return 0;
411 struct {
412 __isl_give isl_val *(*op)(__isl_take isl_val *v);
413 const char *arg;
414 const char *res;
415 } val_un_tests[] = {
416 { &isl_val_neg, "0", "0" },
417 { &isl_val_abs, "0", "0" },
418 { &isl_val_2exp, "0", "1" },
419 { &isl_val_floor, "0", "0" },
420 { &isl_val_ceil, "0", "0" },
421 { &isl_val_neg, "1", "-1" },
422 { &isl_val_neg, "-1", "1" },
423 { &isl_val_neg, "1/2", "-1/2" },
424 { &isl_val_neg, "-1/2", "1/2" },
425 { &isl_val_neg, "infty", "-infty" },
426 { &isl_val_neg, "-infty", "infty" },
427 { &isl_val_neg, "NaN", "NaN" },
428 { &isl_val_abs, "1", "1" },
429 { &isl_val_abs, "-1", "1" },
430 { &isl_val_abs, "1/2", "1/2" },
431 { &isl_val_abs, "-1/2", "1/2" },
432 { &isl_val_abs, "infty", "infty" },
433 { &isl_val_abs, "-infty", "infty" },
434 { &isl_val_abs, "NaN", "NaN" },
435 { &isl_val_floor, "1", "1" },
436 { &isl_val_floor, "-1", "-1" },
437 { &isl_val_floor, "1/2", "0" },
438 { &isl_val_floor, "-1/2", "-1" },
439 { &isl_val_floor, "infty", "infty" },
440 { &isl_val_floor, "-infty", "-infty" },
441 { &isl_val_floor, "NaN", "NaN" },
442 { &isl_val_ceil, "1", "1" },
443 { &isl_val_ceil, "-1", "-1" },
444 { &isl_val_ceil, "1/2", "1" },
445 { &isl_val_ceil, "-1/2", "0" },
446 { &isl_val_ceil, "infty", "infty" },
447 { &isl_val_ceil, "-infty", "-infty" },
448 { &isl_val_ceil, "NaN", "NaN" },
449 { &isl_val_2exp, "-3", "1/8" },
450 { &isl_val_2exp, "-1", "1/2" },
451 { &isl_val_2exp, "1", "2" },
452 { &isl_val_2exp, "2", "4" },
453 { &isl_val_2exp, "3", "8" },
454 { &isl_val_inv, "1", "1" },
455 { &isl_val_inv, "2", "1/2" },
456 { &isl_val_inv, "1/2", "2" },
457 { &isl_val_inv, "-2", "-1/2" },
458 { &isl_val_inv, "-1/2", "-2" },
459 { &isl_val_inv, "0", "NaN" },
460 { &isl_val_inv, "NaN", "NaN" },
461 { &isl_val_inv, "infty", "0" },
462 { &isl_val_inv, "-infty", "0" },
465 /* Perform some basic tests of unary operations on isl_val objects.
467 static int test_un_val(isl_ctx *ctx)
469 int i;
470 isl_val *v, *res;
471 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
472 int ok;
474 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
475 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
476 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
477 fn = val_un_tests[i].op;
478 v = fn(v);
479 if (isl_val_is_nan(res))
480 ok = isl_val_is_nan(v);
481 else
482 ok = isl_val_eq(v, res);
483 isl_val_free(v);
484 isl_val_free(res);
485 if (ok < 0)
486 return -1;
487 if (!ok)
488 isl_die(ctx, isl_error_unknown,
489 "unexpected result", return -1);
492 return 0;
495 struct {
496 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
497 __isl_take isl_val *v2);
498 } val_bin_op[] = {
499 ['+'] = { &isl_val_add },
500 ['-'] = { &isl_val_sub },
501 ['*'] = { &isl_val_mul },
502 ['/'] = { &isl_val_div },
503 ['g'] = { &isl_val_gcd },
504 ['m'] = { &isl_val_min },
505 ['M'] = { &isl_val_max },
508 struct {
509 const char *arg1;
510 unsigned char op;
511 const char *arg2;
512 const char *res;
513 } val_bin_tests[] = {
514 { "0", '+', "0", "0" },
515 { "1", '+', "0", "1" },
516 { "1", '+', "1", "2" },
517 { "1", '-', "1", "0" },
518 { "1", '*', "1", "1" },
519 { "1", '/', "1", "1" },
520 { "2", '*', "3", "6" },
521 { "2", '*', "1/2", "1" },
522 { "2", '*', "1/3", "2/3" },
523 { "2/3", '*', "3/5", "2/5" },
524 { "2/3", '*', "7/5", "14/15" },
525 { "2", '/', "1/2", "4" },
526 { "-2", '/', "-1/2", "4" },
527 { "-2", '/', "1/2", "-4" },
528 { "2", '/', "-1/2", "-4" },
529 { "2", '/', "2", "1" },
530 { "2", '/', "3", "2/3" },
531 { "2/3", '/', "5/3", "2/5" },
532 { "2/3", '/', "5/7", "14/15" },
533 { "0", '/', "0", "NaN" },
534 { "42", '/', "0", "NaN" },
535 { "-42", '/', "0", "NaN" },
536 { "infty", '/', "0", "NaN" },
537 { "-infty", '/', "0", "NaN" },
538 { "NaN", '/', "0", "NaN" },
539 { "0", '/', "NaN", "NaN" },
540 { "42", '/', "NaN", "NaN" },
541 { "-42", '/', "NaN", "NaN" },
542 { "infty", '/', "NaN", "NaN" },
543 { "-infty", '/', "NaN", "NaN" },
544 { "NaN", '/', "NaN", "NaN" },
545 { "0", '/', "infty", "0" },
546 { "42", '/', "infty", "0" },
547 { "-42", '/', "infty", "0" },
548 { "infty", '/', "infty", "NaN" },
549 { "-infty", '/', "infty", "NaN" },
550 { "NaN", '/', "infty", "NaN" },
551 { "0", '/', "-infty", "0" },
552 { "42", '/', "-infty", "0" },
553 { "-42", '/', "-infty", "0" },
554 { "infty", '/', "-infty", "NaN" },
555 { "-infty", '/', "-infty", "NaN" },
556 { "NaN", '/', "-infty", "NaN" },
557 { "1", '-', "1/3", "2/3" },
558 { "1/3", '+', "1/2", "5/6" },
559 { "1/2", '+', "1/2", "1" },
560 { "3/4", '-', "1/4", "1/2" },
561 { "1/2", '-', "1/3", "1/6" },
562 { "infty", '+', "42", "infty" },
563 { "infty", '+', "infty", "infty" },
564 { "42", '+', "infty", "infty" },
565 { "infty", '-', "infty", "NaN" },
566 { "infty", '*', "infty", "infty" },
567 { "infty", '*', "-infty", "-infty" },
568 { "-infty", '*', "infty", "-infty" },
569 { "-infty", '*', "-infty", "infty" },
570 { "0", '*', "infty", "NaN" },
571 { "1", '*', "infty", "infty" },
572 { "infty", '*', "0", "NaN" },
573 { "infty", '*', "42", "infty" },
574 { "42", '-', "infty", "-infty" },
575 { "infty", '+', "-infty", "NaN" },
576 { "4", 'g', "6", "2" },
577 { "5", 'g', "6", "1" },
578 { "42", 'm', "3", "3" },
579 { "42", 'M', "3", "42" },
580 { "3", 'm', "42", "3" },
581 { "3", 'M', "42", "42" },
582 { "42", 'm', "infty", "42" },
583 { "42", 'M', "infty", "infty" },
584 { "42", 'm', "-infty", "-infty" },
585 { "42", 'M', "-infty", "42" },
586 { "42", 'm', "NaN", "NaN" },
587 { "42", 'M', "NaN", "NaN" },
588 { "infty", 'm', "-infty", "-infty" },
589 { "infty", 'M', "-infty", "infty" },
592 /* Perform some basic tests of binary operations on isl_val objects.
594 static int test_bin_val(isl_ctx *ctx)
596 int i;
597 isl_val *v1, *v2, *res;
598 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
599 __isl_take isl_val *v2);
600 int ok;
602 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
603 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
604 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
605 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
606 fn = val_bin_op[val_bin_tests[i].op].fn;
607 v1 = fn(v1, v2);
608 if (isl_val_is_nan(res))
609 ok = isl_val_is_nan(v1);
610 else
611 ok = isl_val_eq(v1, res);
612 isl_val_free(v1);
613 isl_val_free(res);
614 if (ok < 0)
615 return -1;
616 if (!ok)
617 isl_die(ctx, isl_error_unknown,
618 "unexpected result", return -1);
621 return 0;
624 /* Perform some basic tests on isl_val objects.
626 static int test_val(isl_ctx *ctx)
628 if (test_un_val(ctx) < 0)
629 return -1;
630 if (test_bin_val(ctx) < 0)
631 return -1;
632 return 0;
635 static int test_div(isl_ctx *ctx)
637 unsigned n;
638 const char *str;
639 int empty;
640 isl_int v;
641 isl_space *dim;
642 isl_set *set;
643 isl_local_space *ls;
644 struct isl_basic_set *bset;
645 struct isl_constraint *c;
647 isl_int_init(v);
649 /* test 1 */
650 dim = isl_space_set_alloc(ctx, 0, 3);
651 bset = isl_basic_set_universe(isl_space_copy(dim));
652 ls = isl_local_space_from_space(dim);
654 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
655 isl_int_set_si(v, -1);
656 isl_constraint_set_constant(c, v);
657 isl_int_set_si(v, 1);
658 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
659 isl_int_set_si(v, 3);
660 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
661 bset = isl_basic_set_add_constraint(bset, c);
663 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
664 isl_int_set_si(v, 1);
665 isl_constraint_set_constant(c, v);
666 isl_int_set_si(v, -1);
667 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
668 isl_int_set_si(v, 3);
669 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
670 bset = isl_basic_set_add_constraint(bset, c);
672 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
674 assert(bset && bset->n_div == 1);
675 isl_local_space_free(ls);
676 isl_basic_set_free(bset);
678 /* test 2 */
679 dim = isl_space_set_alloc(ctx, 0, 3);
680 bset = isl_basic_set_universe(isl_space_copy(dim));
681 ls = isl_local_space_from_space(dim);
683 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
684 isl_int_set_si(v, 1);
685 isl_constraint_set_constant(c, v);
686 isl_int_set_si(v, -1);
687 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
688 isl_int_set_si(v, 3);
689 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
690 bset = isl_basic_set_add_constraint(bset, c);
692 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
693 isl_int_set_si(v, -1);
694 isl_constraint_set_constant(c, v);
695 isl_int_set_si(v, 1);
696 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
697 isl_int_set_si(v, 3);
698 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
699 bset = isl_basic_set_add_constraint(bset, c);
701 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
703 assert(bset && bset->n_div == 1);
704 isl_local_space_free(ls);
705 isl_basic_set_free(bset);
707 /* test 3 */
708 dim = isl_space_set_alloc(ctx, 0, 3);
709 bset = isl_basic_set_universe(isl_space_copy(dim));
710 ls = isl_local_space_from_space(dim);
712 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
713 isl_int_set_si(v, 1);
714 isl_constraint_set_constant(c, v);
715 isl_int_set_si(v, -1);
716 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
717 isl_int_set_si(v, 3);
718 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
719 bset = isl_basic_set_add_constraint(bset, c);
721 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
722 isl_int_set_si(v, -3);
723 isl_constraint_set_constant(c, v);
724 isl_int_set_si(v, 1);
725 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
726 isl_int_set_si(v, 4);
727 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
728 bset = isl_basic_set_add_constraint(bset, c);
730 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
732 assert(bset && bset->n_div == 1);
733 isl_local_space_free(ls);
734 isl_basic_set_free(bset);
736 /* test 4 */
737 dim = isl_space_set_alloc(ctx, 0, 3);
738 bset = isl_basic_set_universe(isl_space_copy(dim));
739 ls = isl_local_space_from_space(dim);
741 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
742 isl_int_set_si(v, 2);
743 isl_constraint_set_constant(c, v);
744 isl_int_set_si(v, -1);
745 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
746 isl_int_set_si(v, 3);
747 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
748 bset = isl_basic_set_add_constraint(bset, c);
750 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
751 isl_int_set_si(v, -1);
752 isl_constraint_set_constant(c, v);
753 isl_int_set_si(v, 1);
754 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
755 isl_int_set_si(v, 6);
756 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
757 bset = isl_basic_set_add_constraint(bset, c);
759 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
761 assert(isl_basic_set_is_empty(bset));
762 isl_local_space_free(ls);
763 isl_basic_set_free(bset);
765 /* test 5 */
766 dim = isl_space_set_alloc(ctx, 0, 3);
767 bset = isl_basic_set_universe(isl_space_copy(dim));
768 ls = isl_local_space_from_space(dim);
770 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
771 isl_int_set_si(v, -1);
772 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
773 isl_int_set_si(v, 3);
774 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
775 bset = isl_basic_set_add_constraint(bset, c);
777 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
778 isl_int_set_si(v, 1);
779 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
780 isl_int_set_si(v, -3);
781 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
782 bset = isl_basic_set_add_constraint(bset, c);
784 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
786 assert(bset && bset->n_div == 0);
787 isl_basic_set_free(bset);
788 isl_local_space_free(ls);
790 /* test 6 */
791 dim = isl_space_set_alloc(ctx, 0, 3);
792 bset = isl_basic_set_universe(isl_space_copy(dim));
793 ls = isl_local_space_from_space(dim);
795 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
796 isl_int_set_si(v, -1);
797 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
798 isl_int_set_si(v, 6);
799 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
800 bset = isl_basic_set_add_constraint(bset, c);
802 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
803 isl_int_set_si(v, 1);
804 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
805 isl_int_set_si(v, -3);
806 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
807 bset = isl_basic_set_add_constraint(bset, c);
809 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
811 assert(bset && bset->n_div == 1);
812 isl_basic_set_free(bset);
813 isl_local_space_free(ls);
815 /* test 7 */
816 /* This test is a bit tricky. We set up an equality
817 * a + 3b + 3c = 6 e0
818 * Normalization of divs creates _two_ divs
819 * a = 3 e0
820 * c - b - e0 = 2 e1
821 * Afterwards e0 is removed again because it has coefficient -1
822 * and we end up with the original equality and div again.
823 * Perhaps we can avoid the introduction of this temporary div.
825 dim = isl_space_set_alloc(ctx, 0, 4);
826 bset = isl_basic_set_universe(isl_space_copy(dim));
827 ls = isl_local_space_from_space(dim);
829 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
830 isl_int_set_si(v, -1);
831 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
832 isl_int_set_si(v, -3);
833 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
834 isl_int_set_si(v, -3);
835 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
836 isl_int_set_si(v, 6);
837 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
838 bset = isl_basic_set_add_constraint(bset, c);
840 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
842 /* Test disabled for now */
844 assert(bset && bset->n_div == 1);
846 isl_local_space_free(ls);
847 isl_basic_set_free(bset);
849 /* test 8 */
850 dim = isl_space_set_alloc(ctx, 0, 5);
851 bset = isl_basic_set_universe(isl_space_copy(dim));
852 ls = isl_local_space_from_space(dim);
854 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
855 isl_int_set_si(v, -1);
856 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
857 isl_int_set_si(v, -3);
858 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
859 isl_int_set_si(v, -3);
860 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
861 isl_int_set_si(v, 6);
862 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
863 bset = isl_basic_set_add_constraint(bset, c);
865 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
866 isl_int_set_si(v, -1);
867 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
868 isl_int_set_si(v, 1);
869 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
870 isl_int_set_si(v, 1);
871 isl_constraint_set_constant(c, v);
872 bset = isl_basic_set_add_constraint(bset, c);
874 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
876 /* Test disabled for now */
878 assert(bset && bset->n_div == 1);
880 isl_local_space_free(ls);
881 isl_basic_set_free(bset);
883 /* test 9 */
884 dim = isl_space_set_alloc(ctx, 0, 4);
885 bset = isl_basic_set_universe(isl_space_copy(dim));
886 ls = isl_local_space_from_space(dim);
888 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
889 isl_int_set_si(v, 1);
890 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
891 isl_int_set_si(v, -1);
892 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
893 isl_int_set_si(v, -2);
894 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
895 bset = isl_basic_set_add_constraint(bset, c);
897 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
898 isl_int_set_si(v, -1);
899 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
900 isl_int_set_si(v, 3);
901 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
902 isl_int_set_si(v, 2);
903 isl_constraint_set_constant(c, v);
904 bset = isl_basic_set_add_constraint(bset, c);
906 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
908 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
910 assert(!isl_basic_set_is_empty(bset));
912 isl_local_space_free(ls);
913 isl_basic_set_free(bset);
915 /* test 10 */
916 dim = isl_space_set_alloc(ctx, 0, 3);
917 bset = isl_basic_set_universe(isl_space_copy(dim));
918 ls = isl_local_space_from_space(dim);
920 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
921 isl_int_set_si(v, 1);
922 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
923 isl_int_set_si(v, -2);
924 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
925 bset = isl_basic_set_add_constraint(bset, c);
927 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
929 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
931 isl_local_space_free(ls);
932 isl_basic_set_free(bset);
934 isl_int_clear(v);
936 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
937 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
938 set = isl_set_read_from_str(ctx, str);
939 set = isl_set_compute_divs(set);
940 isl_set_free(set);
941 if (!set)
942 return -1;
944 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
945 bset = isl_basic_set_read_from_str(ctx, str);
946 n = isl_basic_set_dim(bset, isl_dim_div);
947 isl_basic_set_free(bset);
948 if (!bset)
949 return -1;
950 if (n != 0)
951 isl_die(ctx, isl_error_unknown,
952 "expecting no existentials", return -1);
954 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
955 set = isl_set_read_from_str(ctx, str);
956 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
957 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
958 empty = isl_set_is_empty(set);
959 isl_set_free(set);
960 if (empty < 0)
961 return -1;
962 if (!empty)
963 isl_die(ctx, isl_error_unknown,
964 "result not as accurate as expected", return -1);
966 return 0;
969 void test_application_case(struct isl_ctx *ctx, const char *name)
971 char *filename;
972 FILE *input;
973 struct isl_basic_set *bset1, *bset2;
974 struct isl_basic_map *bmap;
976 filename = get_filename(ctx, name, "omega");
977 assert(filename);
978 input = fopen(filename, "r");
979 assert(input);
981 bset1 = isl_basic_set_read_from_file(ctx, input);
982 bmap = isl_basic_map_read_from_file(ctx, input);
984 bset1 = isl_basic_set_apply(bset1, bmap);
986 bset2 = isl_basic_set_read_from_file(ctx, input);
988 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
990 isl_basic_set_free(bset1);
991 isl_basic_set_free(bset2);
992 free(filename);
994 fclose(input);
997 static int test_application(isl_ctx *ctx)
999 test_application_case(ctx, "application");
1000 test_application_case(ctx, "application2");
1002 return 0;
1005 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1007 char *filename;
1008 FILE *input;
1009 struct isl_basic_set *bset1, *bset2;
1011 filename = get_filename(ctx, name, "polylib");
1012 assert(filename);
1013 input = fopen(filename, "r");
1014 assert(input);
1016 bset1 = isl_basic_set_read_from_file(ctx, input);
1017 bset2 = isl_basic_set_read_from_file(ctx, input);
1019 bset1 = isl_basic_set_affine_hull(bset1);
1021 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1023 isl_basic_set_free(bset1);
1024 isl_basic_set_free(bset2);
1025 free(filename);
1027 fclose(input);
1030 int test_affine_hull(struct isl_ctx *ctx)
1032 const char *str;
1033 isl_set *set;
1034 isl_basic_set *bset, *bset2;
1035 int n;
1036 int subset;
1038 test_affine_hull_case(ctx, "affine2");
1039 test_affine_hull_case(ctx, "affine");
1040 test_affine_hull_case(ctx, "affine3");
1042 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1043 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1044 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1045 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1046 set = isl_set_read_from_str(ctx, str);
1047 bset = isl_set_affine_hull(set);
1048 n = isl_basic_set_dim(bset, isl_dim_div);
1049 isl_basic_set_free(bset);
1050 if (n != 0)
1051 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1052 return -1);
1054 /* Check that isl_map_affine_hull is not confused by
1055 * the reordering of divs in isl_map_align_divs.
1057 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1058 "32e0 = b and 32e1 = c); "
1059 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1060 set = isl_set_read_from_str(ctx, str);
1061 bset = isl_set_affine_hull(set);
1062 isl_basic_set_free(bset);
1063 if (!bset)
1064 return -1;
1066 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1067 "32e2 = 31 + 31e0 }";
1068 set = isl_set_read_from_str(ctx, str);
1069 bset = isl_set_affine_hull(set);
1070 str = "{ [a] : exists e : a = 32 e }";
1071 bset2 = isl_basic_set_read_from_str(ctx, str);
1072 subset = isl_basic_set_is_subset(bset, bset2);
1073 isl_basic_set_free(bset);
1074 isl_basic_set_free(bset2);
1075 if (subset < 0)
1076 return -1;
1077 if (!subset)
1078 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1079 return -1);
1081 return 0;
1084 /* Pairs of maps and the corresponding expected results of
1085 * isl_map_plain_unshifted_simple_hull.
1087 struct {
1088 const char *map;
1089 const char *hull;
1090 } plain_unshifted_simple_hull_tests[] = {
1091 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1092 "{ [i] -> [j] : i >= 1 }" },
1093 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1094 "(j mod 4 = 2 and k mod 6 = n) }",
1095 "{ [n] -> [i,j,k] : j mod 4 = 2 }" },
1098 /* Basic tests for isl_map_plain_unshifted_simple_hull.
1100 static int test_plain_unshifted_simple_hull(isl_ctx *ctx)
1102 int i;
1103 isl_map *map;
1104 isl_basic_map *hull, *expected;
1105 isl_bool equal;
1107 for (i = 0; i < ARRAY_SIZE(plain_unshifted_simple_hull_tests); ++i) {
1108 const char *str;
1109 str = plain_unshifted_simple_hull_tests[i].map;
1110 map = isl_map_read_from_str(ctx, str);
1111 str = plain_unshifted_simple_hull_tests[i].hull;
1112 expected = isl_basic_map_read_from_str(ctx, str);
1113 hull = isl_map_plain_unshifted_simple_hull(map);
1114 equal = isl_basic_map_is_equal(hull, expected);
1115 isl_basic_map_free(hull);
1116 isl_basic_map_free(expected);
1117 if (equal < 0)
1118 return -1;
1119 if (!equal)
1120 isl_die(ctx, isl_error_unknown, "unexpected hull",
1121 return -1);
1124 return 0;
1127 /* Pairs of sets and the corresponding expected results of
1128 * isl_set_unshifted_simple_hull.
1130 struct {
1131 const char *set;
1132 const char *hull;
1133 } unshifted_simple_hull_tests[] = {
1134 { "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1135 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1138 /* Basic tests for isl_set_unshifted_simple_hull.
1140 static int test_unshifted_simple_hull(isl_ctx *ctx)
1142 int i;
1143 isl_set *set;
1144 isl_basic_set *hull, *expected;
1145 isl_bool equal;
1147 for (i = 0; i < ARRAY_SIZE(unshifted_simple_hull_tests); ++i) {
1148 const char *str;
1149 str = unshifted_simple_hull_tests[i].set;
1150 set = isl_set_read_from_str(ctx, str);
1151 str = unshifted_simple_hull_tests[i].hull;
1152 expected = isl_basic_set_read_from_str(ctx, str);
1153 hull = isl_set_unshifted_simple_hull(set);
1154 equal = isl_basic_set_is_equal(hull, expected);
1155 isl_basic_set_free(hull);
1156 isl_basic_set_free(expected);
1157 if (equal < 0)
1158 return -1;
1159 if (!equal)
1160 isl_die(ctx, isl_error_unknown, "unexpected hull",
1161 return -1);
1164 return 0;
1167 static int test_simple_hull(struct isl_ctx *ctx)
1169 const char *str;
1170 isl_set *set;
1171 isl_basic_set *bset;
1172 isl_bool is_empty;
1174 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1175 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1176 set = isl_set_read_from_str(ctx, str);
1177 bset = isl_set_simple_hull(set);
1178 is_empty = isl_basic_set_is_empty(bset);
1179 isl_basic_set_free(bset);
1181 if (is_empty == isl_bool_error)
1182 return -1;
1184 if (is_empty == isl_bool_false)
1185 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1186 return -1);
1188 if (test_plain_unshifted_simple_hull(ctx) < 0)
1189 return -1;
1190 if (test_unshifted_simple_hull(ctx) < 0)
1191 return -1;
1193 return 0;
1196 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1198 char *filename;
1199 FILE *input;
1200 struct isl_basic_set *bset1, *bset2;
1201 struct isl_set *set;
1203 filename = get_filename(ctx, name, "polylib");
1204 assert(filename);
1205 input = fopen(filename, "r");
1206 assert(input);
1208 bset1 = isl_basic_set_read_from_file(ctx, input);
1209 bset2 = isl_basic_set_read_from_file(ctx, input);
1211 set = isl_basic_set_union(bset1, bset2);
1212 bset1 = isl_set_convex_hull(set);
1214 bset2 = isl_basic_set_read_from_file(ctx, input);
1216 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1218 isl_basic_set_free(bset1);
1219 isl_basic_set_free(bset2);
1220 free(filename);
1222 fclose(input);
1225 struct {
1226 const char *set;
1227 const char *hull;
1228 } convex_hull_tests[] = {
1229 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1230 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1231 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1232 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1233 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1234 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1235 "i2 <= 5 and i2 >= 4; "
1236 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1237 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1238 "i2 <= 5 + i0 and i2 >= i0 }" },
1239 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1240 "{ [x, y] : 1 = 0 }" },
1243 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1245 int i;
1246 int orig_convex = ctx->opt->convex;
1247 ctx->opt->convex = convex;
1249 test_convex_hull_case(ctx, "convex0");
1250 test_convex_hull_case(ctx, "convex1");
1251 test_convex_hull_case(ctx, "convex2");
1252 test_convex_hull_case(ctx, "convex3");
1253 test_convex_hull_case(ctx, "convex4");
1254 test_convex_hull_case(ctx, "convex5");
1255 test_convex_hull_case(ctx, "convex6");
1256 test_convex_hull_case(ctx, "convex7");
1257 test_convex_hull_case(ctx, "convex8");
1258 test_convex_hull_case(ctx, "convex9");
1259 test_convex_hull_case(ctx, "convex10");
1260 test_convex_hull_case(ctx, "convex11");
1261 test_convex_hull_case(ctx, "convex12");
1262 test_convex_hull_case(ctx, "convex13");
1263 test_convex_hull_case(ctx, "convex14");
1264 test_convex_hull_case(ctx, "convex15");
1266 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1267 isl_set *set1, *set2;
1268 int equal;
1270 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1271 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1272 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1273 equal = isl_set_is_equal(set1, set2);
1274 isl_set_free(set1);
1275 isl_set_free(set2);
1277 if (equal < 0)
1278 return -1;
1279 if (!equal)
1280 isl_die(ctx, isl_error_unknown,
1281 "unexpected convex hull", return -1);
1284 ctx->opt->convex = orig_convex;
1286 return 0;
1289 static int test_convex_hull(isl_ctx *ctx)
1291 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1292 return -1;
1293 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1294 return -1;
1295 return 0;
1298 void test_gist_case(struct isl_ctx *ctx, const char *name)
1300 char *filename;
1301 FILE *input;
1302 struct isl_basic_set *bset1, *bset2;
1304 filename = get_filename(ctx, name, "polylib");
1305 assert(filename);
1306 input = fopen(filename, "r");
1307 assert(input);
1309 bset1 = isl_basic_set_read_from_file(ctx, input);
1310 bset2 = isl_basic_set_read_from_file(ctx, input);
1312 bset1 = isl_basic_set_gist(bset1, bset2);
1314 bset2 = isl_basic_set_read_from_file(ctx, input);
1316 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1318 isl_basic_set_free(bset1);
1319 isl_basic_set_free(bset2);
1320 free(filename);
1322 fclose(input);
1325 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1327 struct {
1328 const char *map;
1329 const char *context;
1330 const char *gist;
1331 } plain_gist_tests[] = {
1332 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1333 "{ [i] -> [j] : i >= 1 }",
1334 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1335 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1336 "(j mod 4 = 2 and k mod 6 = n) }",
1337 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1338 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1339 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1340 "{ [i] -> [j] : i > j }",
1341 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1344 /* Basic tests for isl_map_plain_gist_basic_map.
1346 static int test_plain_gist(isl_ctx *ctx)
1348 int i;
1350 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1351 const char *str;
1352 int equal;
1353 isl_map *map, *gist;
1354 isl_basic_map *context;
1356 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1357 str = plain_gist_tests[i].context;
1358 context = isl_basic_map_read_from_str(ctx, str);
1359 map = isl_map_plain_gist_basic_map(map, context);
1360 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1361 equal = isl_map_is_equal(map, gist);
1362 isl_map_free(map);
1363 isl_map_free(gist);
1364 if (equal < 0)
1365 return -1;
1366 if (!equal)
1367 isl_die(ctx, isl_error_unknown,
1368 "incorrect gist result", return -1);
1371 return 0;
1374 struct {
1375 const char *set;
1376 const char *context;
1377 const char *gist;
1378 } gist_tests[] = {
1379 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1380 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1381 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1382 "{ [a, b, c] : a <= 15 }" },
1383 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1384 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1385 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1386 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1387 { "{ [m, n, a, b] : a <= 2147 + n }",
1388 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1389 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1390 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1391 "b >= 0) }",
1392 "{ [m, n, ku, kl] }" },
1393 { "{ [a, a, b] : a >= 10 }",
1394 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1395 "{ [a, a, b] : a >= 10 }" },
1396 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1397 "{ [0, j] : j >= 0 }" },
1398 /* Check that no constraints on i6 are introduced in the gist */
1399 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1400 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1401 "5e0 <= 381 - t1 and i4 <= 1) }",
1402 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1403 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1404 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1405 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1406 "20e0 >= 1511 - 4t1 - 5i4) }" },
1407 /* Check that no constraints on i6 are introduced in the gist */
1408 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1409 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1410 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1411 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1412 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1413 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1414 "20e1 <= 1530 - 4t1 - 5i4 and "
1415 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1416 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1417 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1418 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1419 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1420 "10e0 <= -91 + 5i4 + 4i6 and "
1421 "10e0 >= -105 + 5i4 + 4i6) }",
1422 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1423 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1424 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1425 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1426 "{ [a, b, q, p] : b >= 1 + a }",
1427 "{ [a, b, q, p] : false }" },
1428 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1429 "[n] -> { [x] : x mod 32 = 0 }",
1430 "[n] -> { [x = n] }" },
1431 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1432 "{ [x] : x mod 2 = 0 }" },
1433 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1434 "{ [x] : x mod 128 = 0 }" },
1435 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1436 "{ [x] : x mod 3200 = 0 }" },
1437 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1438 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1439 "{ [a, b, c = a] }" },
1440 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1441 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1442 "{ [a, b, c = a] : a mod 3 = 0 }" },
1443 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1444 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1445 "{ [x] }" },
1446 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1447 "{ [x,y] : 1 <= y <= 3 }",
1448 "{ [x,y] }" },
1451 /* Check that isl_set_gist behaves as expected.
1453 * For the test cases in gist_tests, besides checking that the result
1454 * is as expected, also check that applying the gist operation does
1455 * not modify the input set (an earlier version of isl would do that) and
1456 * that the test case is consistent, i.e., that the gist has the same
1457 * intersection with the context as the input set.
1459 static int test_gist(struct isl_ctx *ctx)
1461 int i;
1462 const char *str;
1463 isl_basic_set *bset1, *bset2;
1464 isl_map *map1, *map2;
1465 int equal;
1467 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1468 int equal_input, equal_intersection;
1469 isl_set *set1, *set2, *copy, *context;
1471 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1472 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1473 copy = isl_set_copy(set1);
1474 set1 = isl_set_gist(set1, isl_set_copy(context));
1475 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1476 equal = isl_set_is_equal(set1, set2);
1477 isl_set_free(set1);
1478 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1479 equal_input = isl_set_is_equal(set1, copy);
1480 isl_set_free(copy);
1481 set1 = isl_set_intersect(set1, isl_set_copy(context));
1482 set2 = isl_set_intersect(set2, context);
1483 equal_intersection = isl_set_is_equal(set1, set2);
1484 isl_set_free(set2);
1485 isl_set_free(set1);
1486 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1487 return -1;
1488 if (!equal)
1489 isl_die(ctx, isl_error_unknown,
1490 "incorrect gist result", return -1);
1491 if (!equal_input)
1492 isl_die(ctx, isl_error_unknown,
1493 "gist modified input", return -1);
1494 if (!equal_input)
1495 isl_die(ctx, isl_error_unknown,
1496 "inconsistent gist test case", return -1);
1499 test_gist_case(ctx, "gist1");
1501 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1502 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1503 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1504 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1505 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1506 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1507 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1508 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1509 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1510 bset1 = isl_basic_set_read_from_str(ctx, str);
1511 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1512 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1513 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1514 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1515 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1516 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1517 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1518 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1519 bset2 = isl_basic_set_read_from_str(ctx, str);
1520 bset1 = isl_basic_set_gist(bset1, bset2);
1521 assert(bset1 && bset1->n_div == 0);
1522 isl_basic_set_free(bset1);
1524 /* Check that the integer divisions of the second disjunct
1525 * do not spread to the first disjunct.
1527 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1528 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1529 "(exists (e0 = [(-1 + t1)/16], "
1530 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1531 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1532 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1533 "o0 <= 4294967295 and t1 <= -1)) }";
1534 map1 = isl_map_read_from_str(ctx, str);
1535 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1536 map2 = isl_map_read_from_str(ctx, str);
1537 map1 = isl_map_gist(map1, map2);
1538 if (!map1)
1539 return -1;
1540 if (map1->n != 1)
1541 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1542 isl_map_free(map1); return -1);
1543 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1544 isl_die(ctx, isl_error_unknown, "expecting single div",
1545 isl_map_free(map1); return -1);
1546 isl_map_free(map1);
1548 if (test_plain_gist(ctx) < 0)
1549 return -1;
1551 return 0;
1554 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1556 isl_set *set, *set2;
1557 int equal;
1558 int one;
1560 set = isl_set_read_from_str(ctx, str);
1561 set = isl_set_coalesce(set);
1562 set2 = isl_set_read_from_str(ctx, str);
1563 equal = isl_set_is_equal(set, set2);
1564 one = set && set->n == 1;
1565 isl_set_free(set);
1566 isl_set_free(set2);
1568 if (equal < 0)
1569 return -1;
1570 if (!equal)
1571 isl_die(ctx, isl_error_unknown,
1572 "coalesced set not equal to input", return -1);
1573 if (check_one && !one)
1574 isl_die(ctx, isl_error_unknown,
1575 "coalesced set should not be a union", return -1);
1577 return 0;
1580 /* Inputs for coalescing tests with unbounded wrapping.
1581 * "str" is a string representation of the input set.
1582 * "single_disjunct" is set if we expect the result to consist of
1583 * a single disjunct.
1585 struct {
1586 int single_disjunct;
1587 const char *str;
1588 } coalesce_unbounded_tests[] = {
1589 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1590 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1591 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1592 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1593 "-10 <= y <= 0}" },
1594 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1595 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1596 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1597 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1600 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1601 * option turned off.
1603 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1605 int i;
1606 int r = 0;
1607 int bounded;
1609 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1610 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1612 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1613 const char *str = coalesce_unbounded_tests[i].str;
1614 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1615 if (test_coalesce_set(ctx, str, check_one) >= 0)
1616 continue;
1617 r = -1;
1618 break;
1621 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1623 return r;
1626 /* Inputs for coalescing tests.
1627 * "str" is a string representation of the input set.
1628 * "single_disjunct" is set if we expect the result to consist of
1629 * a single disjunct.
1631 struct {
1632 int single_disjunct;
1633 const char *str;
1634 } coalesce_tests[] = {
1635 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1636 "y >= x & x >= 2 & 5 >= y }" },
1637 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1638 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1639 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1640 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1641 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1642 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1643 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1644 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1645 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1646 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1647 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1648 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1649 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1650 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1651 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1652 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1653 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1654 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1655 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1656 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1657 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1658 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1659 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1660 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1661 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1662 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1663 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1664 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1665 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1666 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1667 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1668 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1669 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1670 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1671 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1672 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1673 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1674 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1675 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1676 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1677 "[o0, o1, o2, o3, o4, o5, o6]] : "
1678 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1679 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1680 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1681 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1682 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1683 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1684 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1685 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1686 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1687 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1688 "o6 >= i3 + i6 - o3 and M >= 0 and "
1689 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1690 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1691 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1692 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1693 "(o0 = 0 and M >= 2 and N >= 3) or "
1694 "(M = 0 and o0 = 0 and N >= 3) }" },
1695 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1696 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1697 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1698 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1699 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1700 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1701 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1702 "(y = 3 and x = 1) }" },
1703 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1704 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1705 "i1 <= M and i3 <= M and i4 <= M) or "
1706 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1707 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1708 "i4 <= -1 + M) }" },
1709 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1710 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1711 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1712 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1713 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1714 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1715 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1716 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1717 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1718 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1719 { 0, "{ [a, b] : exists e : 2e = a and "
1720 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1721 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1722 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1723 "j >= 1 and j' <= i + j - i' and i >= 1; "
1724 "[1, 1, 1, 1] }" },
1725 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1726 "[i,j] : exists a : j = 3a }" },
1727 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1728 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1729 "a >= 3) or "
1730 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1731 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1732 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1733 "c <= 6 + 8a and a >= 3; "
1734 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1735 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1736 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1737 "[x,0] : 3 <= x <= 4 }" },
1738 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1739 "[x,0] : 4 <= x <= 5 }" },
1740 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1741 "[x,0] : 3 <= x <= 5 }" },
1742 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1743 "[x,0] : 3 <= x <= 4 }" },
1744 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1745 "i1 <= 0; "
1746 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1747 { 1, "{ [0,0]; [1,1] }" },
1748 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1749 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1750 "ii <= k;"
1751 "[k, 0, k] : k <= 6 and k >= 1 }" },
1752 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1753 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1754 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1755 { 1, "[n] -> { [1] : n >= 0;"
1756 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1757 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1758 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1759 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1760 "2e0 <= x and 2e0 <= n);"
1761 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1762 "n >= 0) }" },
1763 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1764 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1765 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1766 "t1 = 1 }" },
1767 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1768 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1769 "[0, 0] }" },
1770 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1771 "t1 >= 13 and t1 <= 16);"
1772 "[t1] : t1 <= 15 and t1 >= 12 }" },
1773 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
1774 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
1775 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
1776 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
1777 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
1778 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
1779 "i <= 4j + 2 }" },
1780 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
1781 "(exists (e0 : 3e0 = -2 + c0)) }" },
1782 { 0, "[n, b0, t0] -> "
1783 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1784 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1785 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1786 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1787 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
1788 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
1789 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
1790 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
1791 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1792 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1793 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1794 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
1795 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
1796 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
1797 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
1798 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
1799 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
1800 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
1801 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
1802 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
1803 { 0, "{ [i0, i1, i2] : "
1804 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
1805 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
1806 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
1807 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
1808 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
1809 "32e0 <= 31 + i0)) or "
1810 "i0 >= 0 }" },
1811 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
1812 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
1813 "2*floor((c)/2) = c and 0 <= a <= 192;"
1814 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
1818 /* A specialized coalescing test case that would result
1819 * in a segmentation fault or a failed assertion in earlier versions of isl.
1821 static int test_coalesce_special(struct isl_ctx *ctx)
1823 const char *str;
1824 isl_map *map1, *map2;
1826 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1827 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
1828 "(y = 201 and o1 <= 239 and o1 >= 212) or "
1829 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
1830 "o1 <= 239 and o1 >= 212)) or "
1831 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
1832 "o1 <= 241 and o1 >= 240));"
1833 "[S_L220_OUT[] -> T7[]] -> "
1834 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
1835 "(y = 2 and o1 <= 241 and o1 >= 212) or "
1836 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
1837 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
1838 map1 = isl_map_read_from_str(ctx, str);
1839 map1 = isl_map_align_divs(map1);
1840 map1 = isl_map_coalesce(map1);
1841 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1842 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
1843 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
1844 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
1845 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
1846 map2 = isl_map_read_from_str(ctx, str);
1847 map2 = isl_map_union(map2, map1);
1848 map2 = isl_map_align_divs(map2);
1849 map2 = isl_map_coalesce(map2);
1850 isl_map_free(map2);
1851 if (!map2)
1852 return -1;
1854 return 0;
1857 /* Test the functionality of isl_set_coalesce.
1858 * That is, check that the output is always equal to the input
1859 * and in some cases that the result consists of a single disjunct.
1861 static int test_coalesce(struct isl_ctx *ctx)
1863 int i;
1865 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1866 const char *str = coalesce_tests[i].str;
1867 int check_one = coalesce_tests[i].single_disjunct;
1868 if (test_coalesce_set(ctx, str, check_one) < 0)
1869 return -1;
1872 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1873 return -1;
1874 if (test_coalesce_special(ctx) < 0)
1875 return -1;
1877 return 0;
1880 /* Construct a representation of the graph on the right of Figure 1
1881 * in "Computing the Transitive Closure of a Union of
1882 * Affine Integer Tuple Relations".
1884 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
1886 isl_set *dom;
1887 isl_map *up, *right;
1889 dom = isl_set_read_from_str(ctx,
1890 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1891 "2 x - 3 y + 3 >= 0 }");
1892 right = isl_map_read_from_str(ctx,
1893 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1894 up = isl_map_read_from_str(ctx,
1895 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1896 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1897 right = isl_map_intersect_range(right, isl_set_copy(dom));
1898 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1899 up = isl_map_intersect_range(up, dom);
1900 return isl_map_union(up, right);
1903 /* Construct a representation of the power of the graph
1904 * on the right of Figure 1 in "Computing the Transitive Closure of
1905 * a Union of Affine Integer Tuple Relations".
1907 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
1909 return isl_map_read_from_str(ctx,
1910 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
1911 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
1912 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
1915 /* Construct a representation of the transitive closure of the graph
1916 * on the right of Figure 1 in "Computing the Transitive Closure of
1917 * a Union of Affine Integer Tuple Relations".
1919 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
1921 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
1924 static int test_closure(isl_ctx *ctx)
1926 const char *str;
1927 isl_map *map, *map2;
1928 int exact, equal;
1930 /* COCOA example 1 */
1931 map = isl_map_read_from_str(ctx,
1932 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1933 "1 <= i and i < n and 1 <= j and j < n or "
1934 "i2 = i + 1 and j2 = j - 1 and "
1935 "1 <= i and i < n and 2 <= j and j <= n }");
1936 map = isl_map_power(map, &exact);
1937 assert(exact);
1938 isl_map_free(map);
1940 /* COCOA example 1 */
1941 map = isl_map_read_from_str(ctx,
1942 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1943 "1 <= i and i < n and 1 <= j and j < n or "
1944 "i2 = i + 1 and j2 = j - 1 and "
1945 "1 <= i and i < n and 2 <= j and j <= n }");
1946 map = isl_map_transitive_closure(map, &exact);
1947 assert(exact);
1948 map2 = isl_map_read_from_str(ctx,
1949 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1950 "1 <= i and i < n and 1 <= j and j <= n and "
1951 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1952 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1953 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1954 assert(isl_map_is_equal(map, map2));
1955 isl_map_free(map2);
1956 isl_map_free(map);
1958 map = isl_map_read_from_str(ctx,
1959 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1960 " 0 <= y and y <= n }");
1961 map = isl_map_transitive_closure(map, &exact);
1962 map2 = isl_map_read_from_str(ctx,
1963 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1964 " 0 <= y and y <= n }");
1965 assert(isl_map_is_equal(map, map2));
1966 isl_map_free(map2);
1967 isl_map_free(map);
1969 /* COCOA example 2 */
1970 map = isl_map_read_from_str(ctx,
1971 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1972 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1973 "i2 = i + 2 and j2 = j - 2 and "
1974 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1975 map = isl_map_transitive_closure(map, &exact);
1976 assert(exact);
1977 map2 = isl_map_read_from_str(ctx,
1978 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1979 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1980 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1981 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1982 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1983 assert(isl_map_is_equal(map, map2));
1984 isl_map_free(map);
1985 isl_map_free(map2);
1987 /* COCOA Fig.2 left */
1988 map = isl_map_read_from_str(ctx,
1989 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1990 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1991 "j <= n or "
1992 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1993 "j <= 2 i - 3 and j <= n - 2 or "
1994 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1995 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1996 map = isl_map_transitive_closure(map, &exact);
1997 assert(exact);
1998 isl_map_free(map);
2000 /* COCOA Fig.2 right */
2001 map = isl_map_read_from_str(ctx,
2002 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2003 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2004 "j <= n or "
2005 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2006 "j <= 2 i - 4 and j <= n - 3 or "
2007 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2008 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2009 map = isl_map_power(map, &exact);
2010 assert(exact);
2011 isl_map_free(map);
2013 /* COCOA Fig.2 right */
2014 map = isl_map_read_from_str(ctx,
2015 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2016 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2017 "j <= n or "
2018 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2019 "j <= 2 i - 4 and j <= n - 3 or "
2020 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2021 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2022 map = isl_map_transitive_closure(map, &exact);
2023 assert(exact);
2024 map2 = isl_map_read_from_str(ctx,
2025 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2026 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2027 "j <= n and 3 + i + 2 j <= 3 n and "
2028 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2029 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2030 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2031 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2032 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2033 assert(isl_map_is_equal(map, map2));
2034 isl_map_free(map2);
2035 isl_map_free(map);
2037 map = cocoa_fig_1_right_graph(ctx);
2038 map = isl_map_transitive_closure(map, &exact);
2039 assert(exact);
2040 map2 = cocoa_fig_1_right_tc(ctx);
2041 assert(isl_map_is_equal(map, map2));
2042 isl_map_free(map2);
2043 isl_map_free(map);
2045 map = cocoa_fig_1_right_graph(ctx);
2046 map = isl_map_power(map, &exact);
2047 map2 = cocoa_fig_1_right_power(ctx);
2048 equal = isl_map_is_equal(map, map2);
2049 isl_map_free(map2);
2050 isl_map_free(map);
2051 if (equal < 0)
2052 return -1;
2053 if (!exact)
2054 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2055 if (!equal)
2056 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2058 /* COCOA Theorem 1 counter example */
2059 map = isl_map_read_from_str(ctx,
2060 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2061 "i2 = 1 and j2 = j or "
2062 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2063 map = isl_map_transitive_closure(map, &exact);
2064 assert(exact);
2065 isl_map_free(map);
2067 map = isl_map_read_from_str(ctx,
2068 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2069 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2070 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2071 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2072 map = isl_map_transitive_closure(map, &exact);
2073 assert(exact);
2074 isl_map_free(map);
2076 /* Kelly et al 1996, fig 12 */
2077 map = isl_map_read_from_str(ctx,
2078 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2079 "1 <= i,j,j+1 <= n or "
2080 "j = n and j2 = 1 and i2 = i + 1 and "
2081 "1 <= i,i+1 <= n }");
2082 map = isl_map_transitive_closure(map, &exact);
2083 assert(exact);
2084 map2 = isl_map_read_from_str(ctx,
2085 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2086 "1 <= i <= n and i = i2 or "
2087 "1 <= i < i2 <= n and 1 <= j <= n and "
2088 "1 <= j2 <= n }");
2089 assert(isl_map_is_equal(map, map2));
2090 isl_map_free(map2);
2091 isl_map_free(map);
2093 /* Omega's closure4 */
2094 map = isl_map_read_from_str(ctx,
2095 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2096 "1 <= x,y <= 10 or "
2097 "x2 = x + 1 and y2 = y and "
2098 "1 <= x <= 20 && 5 <= y <= 15 }");
2099 map = isl_map_transitive_closure(map, &exact);
2100 assert(exact);
2101 isl_map_free(map);
2103 map = isl_map_read_from_str(ctx,
2104 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2105 map = isl_map_transitive_closure(map, &exact);
2106 assert(!exact);
2107 map2 = isl_map_read_from_str(ctx,
2108 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2109 assert(isl_map_is_equal(map, map2));
2110 isl_map_free(map);
2111 isl_map_free(map2);
2113 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2114 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2115 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2116 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2117 map = isl_map_read_from_str(ctx, str);
2118 map = isl_map_transitive_closure(map, &exact);
2119 assert(exact);
2120 map2 = isl_map_read_from_str(ctx, str);
2121 assert(isl_map_is_equal(map, map2));
2122 isl_map_free(map);
2123 isl_map_free(map2);
2125 str = "{[0] -> [1]; [2] -> [3]}";
2126 map = isl_map_read_from_str(ctx, str);
2127 map = isl_map_transitive_closure(map, &exact);
2128 assert(exact);
2129 map2 = isl_map_read_from_str(ctx, str);
2130 assert(isl_map_is_equal(map, map2));
2131 isl_map_free(map);
2132 isl_map_free(map2);
2134 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2135 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2136 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2137 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2138 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2139 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2140 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2141 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2142 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2143 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2144 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2145 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2146 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2147 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2148 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2149 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2150 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2151 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2152 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2153 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2154 map = isl_map_read_from_str(ctx, str);
2155 map = isl_map_transitive_closure(map, NULL);
2156 assert(map);
2157 isl_map_free(map);
2159 return 0;
2162 static int test_lex(struct isl_ctx *ctx)
2164 isl_space *dim;
2165 isl_map *map;
2166 int empty;
2168 dim = isl_space_set_alloc(ctx, 0, 0);
2169 map = isl_map_lex_le(dim);
2170 empty = isl_map_is_empty(map);
2171 isl_map_free(map);
2173 if (empty < 0)
2174 return -1;
2175 if (empty)
2176 isl_die(ctx, isl_error_unknown,
2177 "expecting non-empty result", return -1);
2179 return 0;
2182 static int test_lexmin(struct isl_ctx *ctx)
2184 int equal;
2185 const char *str;
2186 isl_basic_map *bmap;
2187 isl_map *map, *map2;
2188 isl_set *set;
2189 isl_set *set2;
2190 isl_pw_multi_aff *pma;
2192 str = "[p0, p1] -> { [] -> [] : "
2193 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2194 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2195 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2196 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2197 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2198 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2199 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2200 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2201 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2202 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2203 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2204 map = isl_map_read_from_str(ctx, str);
2205 map = isl_map_lexmin(map);
2206 isl_map_free(map);
2208 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2209 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2210 set = isl_set_read_from_str(ctx, str);
2211 set = isl_set_lexmax(set);
2212 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2213 set2 = isl_set_read_from_str(ctx, str);
2214 set = isl_set_intersect(set, set2);
2215 assert(!isl_set_is_empty(set));
2216 isl_set_free(set);
2218 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
2219 map = isl_map_read_from_str(ctx, str);
2220 map = isl_map_lexmin(map);
2221 str = "{ [x] -> [5] : 6 <= x <= 8; "
2222 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
2223 map2 = isl_map_read_from_str(ctx, str);
2224 assert(isl_map_is_equal(map, map2));
2225 isl_map_free(map);
2226 isl_map_free(map2);
2228 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
2229 map = isl_map_read_from_str(ctx, str);
2230 map2 = isl_map_copy(map);
2231 map = isl_map_lexmin(map);
2232 assert(isl_map_is_equal(map, map2));
2233 isl_map_free(map);
2234 isl_map_free(map2);
2236 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
2237 map = isl_map_read_from_str(ctx, str);
2238 map = isl_map_lexmin(map);
2239 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
2240 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2241 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2242 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
2243 map2 = isl_map_read_from_str(ctx, str);
2244 assert(isl_map_is_equal(map, map2));
2245 isl_map_free(map);
2246 isl_map_free(map2);
2248 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2249 " 8i' <= i and 8i' >= -7 + i }";
2250 bmap = isl_basic_map_read_from_str(ctx, str);
2251 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2252 map2 = isl_map_from_pw_multi_aff(pma);
2253 map = isl_map_from_basic_map(bmap);
2254 assert(isl_map_is_equal(map, map2));
2255 isl_map_free(map);
2256 isl_map_free(map2);
2258 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
2259 map = isl_map_read_from_str(ctx, str);
2260 map = isl_map_lexmin(map);
2261 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
2262 map2 = isl_map_read_from_str(ctx, str);
2263 assert(isl_map_is_equal(map, map2));
2264 isl_map_free(map);
2265 isl_map_free(map2);
2267 /* Check that empty pieces are properly combined. */
2268 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2269 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2270 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
2271 map = isl_map_read_from_str(ctx, str);
2272 map = isl_map_lexmin(map);
2273 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2274 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2275 "x >= 4 }";
2276 map2 = isl_map_read_from_str(ctx, str);
2277 assert(isl_map_is_equal(map, map2));
2278 isl_map_free(map);
2279 isl_map_free(map2);
2281 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2282 " 8i' <= i and 8i' >= -7 + i }";
2283 set = isl_set_read_from_str(ctx, str);
2284 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2285 set2 = isl_set_from_pw_multi_aff(pma);
2286 equal = isl_set_is_equal(set, set2);
2287 isl_set_free(set);
2288 isl_set_free(set2);
2289 if (equal < 0)
2290 return -1;
2291 if (!equal)
2292 isl_die(ctx, isl_error_unknown,
2293 "unexpected difference between set and "
2294 "piecewise affine expression", return -1);
2296 return 0;
2299 struct {
2300 const char *set;
2301 const char *obj;
2302 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2303 __isl_keep isl_aff *obj);
2304 const char *res;
2305 } opt_tests[] = {
2306 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2307 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2308 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2309 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2310 &isl_set_max_val, "30" },
2314 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2315 * In particular, check the results on non-convex inputs.
2317 static int test_min(struct isl_ctx *ctx)
2319 int i;
2320 isl_set *set;
2321 isl_aff *obj;
2322 isl_val *val, *res;
2323 isl_bool ok;
2325 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2326 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2327 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2328 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2329 val = opt_tests[i].fn(set, obj);
2330 ok = isl_val_eq(res, val);
2331 isl_val_free(res);
2332 isl_val_free(val);
2333 isl_aff_free(obj);
2334 isl_set_free(set);
2336 if (ok < 0)
2337 return -1;
2338 if (!ok)
2339 isl_die(ctx, isl_error_unknown,
2340 "unexpected optimum", return -1);
2343 return 0;
2346 struct must_may {
2347 isl_map *must;
2348 isl_map *may;
2351 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2352 void *dep_user, void *user)
2354 struct must_may *mm = (struct must_may *)user;
2356 if (must)
2357 mm->must = isl_map_union(mm->must, dep);
2358 else
2359 mm->may = isl_map_union(mm->may, dep);
2361 return isl_stat_ok;
2364 static int common_space(void *first, void *second)
2366 int depth = *(int *)first;
2367 return 2 * depth;
2370 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2372 isl_map *map2;
2373 int equal;
2375 if (!map)
2376 return -1;
2378 map2 = isl_map_read_from_str(map->ctx, str);
2379 equal = isl_map_is_equal(map, map2);
2380 isl_map_free(map2);
2382 return equal;
2385 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2387 int equal;
2389 equal = map_is_equal(map, str);
2390 if (equal < 0)
2391 return -1;
2392 if (!equal)
2393 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2394 "result not as expected", return -1);
2395 return 0;
2398 static int test_dep(struct isl_ctx *ctx)
2400 const char *str;
2401 isl_space *dim;
2402 isl_map *map;
2403 isl_access_info *ai;
2404 isl_flow *flow;
2405 int depth;
2406 struct must_may mm;
2408 depth = 3;
2410 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2411 map = isl_map_read_from_str(ctx, str);
2412 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2414 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2415 map = isl_map_read_from_str(ctx, str);
2416 ai = isl_access_info_add_source(ai, map, 1, &depth);
2418 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2419 map = isl_map_read_from_str(ctx, str);
2420 ai = isl_access_info_add_source(ai, map, 1, &depth);
2422 flow = isl_access_info_compute_flow(ai);
2423 dim = isl_space_alloc(ctx, 0, 3, 3);
2424 mm.must = isl_map_empty(isl_space_copy(dim));
2425 mm.may = isl_map_empty(dim);
2427 isl_flow_foreach(flow, collect_must_may, &mm);
2429 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2430 " [1,10,0] -> [2,5,0] }";
2431 assert(map_is_equal(mm.must, str));
2432 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2433 assert(map_is_equal(mm.may, str));
2435 isl_map_free(mm.must);
2436 isl_map_free(mm.may);
2437 isl_flow_free(flow);
2440 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2441 map = isl_map_read_from_str(ctx, str);
2442 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2444 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2445 map = isl_map_read_from_str(ctx, str);
2446 ai = isl_access_info_add_source(ai, map, 1, &depth);
2448 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2449 map = isl_map_read_from_str(ctx, str);
2450 ai = isl_access_info_add_source(ai, map, 0, &depth);
2452 flow = isl_access_info_compute_flow(ai);
2453 dim = isl_space_alloc(ctx, 0, 3, 3);
2454 mm.must = isl_map_empty(isl_space_copy(dim));
2455 mm.may = isl_map_empty(dim);
2457 isl_flow_foreach(flow, collect_must_may, &mm);
2459 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2460 assert(map_is_equal(mm.must, str));
2461 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2462 assert(map_is_equal(mm.may, str));
2464 isl_map_free(mm.must);
2465 isl_map_free(mm.may);
2466 isl_flow_free(flow);
2469 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2470 map = isl_map_read_from_str(ctx, str);
2471 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2473 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2474 map = isl_map_read_from_str(ctx, str);
2475 ai = isl_access_info_add_source(ai, map, 0, &depth);
2477 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2478 map = isl_map_read_from_str(ctx, str);
2479 ai = isl_access_info_add_source(ai, map, 0, &depth);
2481 flow = isl_access_info_compute_flow(ai);
2482 dim = isl_space_alloc(ctx, 0, 3, 3);
2483 mm.must = isl_map_empty(isl_space_copy(dim));
2484 mm.may = isl_map_empty(dim);
2486 isl_flow_foreach(flow, collect_must_may, &mm);
2488 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2489 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2490 assert(map_is_equal(mm.may, str));
2491 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2492 assert(map_is_equal(mm.must, str));
2494 isl_map_free(mm.must);
2495 isl_map_free(mm.may);
2496 isl_flow_free(flow);
2499 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2500 map = isl_map_read_from_str(ctx, str);
2501 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2503 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2504 map = isl_map_read_from_str(ctx, str);
2505 ai = isl_access_info_add_source(ai, map, 0, &depth);
2507 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2508 map = isl_map_read_from_str(ctx, str);
2509 ai = isl_access_info_add_source(ai, map, 0, &depth);
2511 flow = isl_access_info_compute_flow(ai);
2512 dim = isl_space_alloc(ctx, 0, 3, 3);
2513 mm.must = isl_map_empty(isl_space_copy(dim));
2514 mm.may = isl_map_empty(dim);
2516 isl_flow_foreach(flow, collect_must_may, &mm);
2518 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2519 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2520 assert(map_is_equal(mm.may, str));
2521 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2522 assert(map_is_equal(mm.must, str));
2524 isl_map_free(mm.must);
2525 isl_map_free(mm.may);
2526 isl_flow_free(flow);
2529 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2530 map = isl_map_read_from_str(ctx, str);
2531 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2533 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2534 map = isl_map_read_from_str(ctx, str);
2535 ai = isl_access_info_add_source(ai, map, 0, &depth);
2537 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2538 map = isl_map_read_from_str(ctx, str);
2539 ai = isl_access_info_add_source(ai, map, 0, &depth);
2541 flow = isl_access_info_compute_flow(ai);
2542 dim = isl_space_alloc(ctx, 0, 3, 3);
2543 mm.must = isl_map_empty(isl_space_copy(dim));
2544 mm.may = isl_map_empty(dim);
2546 isl_flow_foreach(flow, collect_must_may, &mm);
2548 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2549 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2550 assert(map_is_equal(mm.may, str));
2551 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2552 assert(map_is_equal(mm.must, str));
2554 isl_map_free(mm.must);
2555 isl_map_free(mm.may);
2556 isl_flow_free(flow);
2559 depth = 5;
2561 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2562 map = isl_map_read_from_str(ctx, str);
2563 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2565 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2566 map = isl_map_read_from_str(ctx, str);
2567 ai = isl_access_info_add_source(ai, map, 1, &depth);
2569 flow = isl_access_info_compute_flow(ai);
2570 dim = isl_space_alloc(ctx, 0, 5, 5);
2571 mm.must = isl_map_empty(isl_space_copy(dim));
2572 mm.may = isl_map_empty(dim);
2574 isl_flow_foreach(flow, collect_must_may, &mm);
2576 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2577 assert(map_is_equal(mm.must, str));
2578 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2579 assert(map_is_equal(mm.may, str));
2581 isl_map_free(mm.must);
2582 isl_map_free(mm.may);
2583 isl_flow_free(flow);
2585 return 0;
2588 /* Check that the dependence analysis proceeds without errors.
2589 * Earlier versions of isl would break down during the analysis
2590 * due to the use of the wrong spaces.
2592 static int test_flow(isl_ctx *ctx)
2594 const char *str;
2595 isl_union_map *access, *schedule;
2596 isl_union_map *must_dep, *may_dep;
2597 int r;
2599 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2600 access = isl_union_map_read_from_str(ctx, str);
2601 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2602 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2603 "S2[] -> [1,0,0,0]; "
2604 "S3[] -> [-1,0,0,0] }";
2605 schedule = isl_union_map_read_from_str(ctx, str);
2606 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2607 isl_union_map_copy(access), schedule,
2608 &must_dep, &may_dep, NULL, NULL);
2609 isl_union_map_free(may_dep);
2610 isl_union_map_free(must_dep);
2612 return r;
2615 struct {
2616 const char *map;
2617 int sv;
2618 } sv_tests[] = {
2619 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2620 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2621 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2622 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2623 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2624 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2625 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2626 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2627 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2630 int test_sv(isl_ctx *ctx)
2632 isl_union_map *umap;
2633 int i;
2634 int sv;
2636 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2637 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2638 sv = isl_union_map_is_single_valued(umap);
2639 isl_union_map_free(umap);
2640 if (sv < 0)
2641 return -1;
2642 if (sv_tests[i].sv && !sv)
2643 isl_die(ctx, isl_error_internal,
2644 "map not detected as single valued", return -1);
2645 if (!sv_tests[i].sv && sv)
2646 isl_die(ctx, isl_error_internal,
2647 "map detected as single valued", return -1);
2650 return 0;
2653 struct {
2654 const char *str;
2655 int bijective;
2656 } bijective_tests[] = {
2657 { "[N,M]->{[i,j] -> [i]}", 0 },
2658 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
2659 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
2660 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
2661 { "[N,M]->{[i,j] -> [j,i]}", 1 },
2662 { "[N,M]->{[i,j] -> [i+j]}", 0 },
2663 { "[N,M]->{[i,j] -> []}", 0 },
2664 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
2665 { "[N,M]->{[i,j] -> [2i]}", 0 },
2666 { "[N,M]->{[i,j] -> [i,i]}", 0 },
2667 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
2668 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
2669 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
2672 static int test_bijective(struct isl_ctx *ctx)
2674 isl_map *map;
2675 int i;
2676 int bijective;
2678 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
2679 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
2680 bijective = isl_map_is_bijective(map);
2681 isl_map_free(map);
2682 if (bijective < 0)
2683 return -1;
2684 if (bijective_tests[i].bijective && !bijective)
2685 isl_die(ctx, isl_error_internal,
2686 "map not detected as bijective", return -1);
2687 if (!bijective_tests[i].bijective && bijective)
2688 isl_die(ctx, isl_error_internal,
2689 "map detected as bijective", return -1);
2692 return 0;
2695 /* Inputs for isl_pw_qpolynomial_gist tests.
2696 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2698 struct {
2699 const char *pwqp;
2700 const char *set;
2701 const char *gist;
2702 } pwqp_gist_tests[] = {
2703 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2704 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2705 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2706 "{ [i] -> -1/2 + 1/2 * i }" },
2707 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2710 static int test_pwqp(struct isl_ctx *ctx)
2712 int i;
2713 const char *str;
2714 isl_set *set;
2715 isl_pw_qpolynomial *pwqp1, *pwqp2;
2716 int equal;
2718 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2719 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2721 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2722 isl_dim_in, 1, 1);
2724 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2725 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2727 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2729 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2731 isl_pw_qpolynomial_free(pwqp1);
2733 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2734 str = pwqp_gist_tests[i].pwqp;
2735 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2736 str = pwqp_gist_tests[i].set;
2737 set = isl_set_read_from_str(ctx, str);
2738 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2739 str = pwqp_gist_tests[i].gist;
2740 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2741 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2742 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2743 isl_pw_qpolynomial_free(pwqp1);
2745 if (equal < 0)
2746 return -1;
2747 if (!equal)
2748 isl_die(ctx, isl_error_unknown,
2749 "unexpected result", return -1);
2752 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2753 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2754 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2755 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2757 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2759 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2761 isl_pw_qpolynomial_free(pwqp1);
2763 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2764 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2765 str = "{ [x] -> x }";
2766 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2768 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2770 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2772 isl_pw_qpolynomial_free(pwqp1);
2774 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2775 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2776 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2777 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2778 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2779 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2780 isl_pw_qpolynomial_free(pwqp1);
2782 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2783 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2784 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2785 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2786 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2787 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2788 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2789 isl_pw_qpolynomial_free(pwqp1);
2790 isl_pw_qpolynomial_free(pwqp2);
2791 if (equal < 0)
2792 return -1;
2793 if (!equal)
2794 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2796 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2797 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2798 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2799 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2800 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2801 isl_val_one(ctx));
2802 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2803 isl_pw_qpolynomial_free(pwqp1);
2804 isl_pw_qpolynomial_free(pwqp2);
2805 if (equal < 0)
2806 return -1;
2807 if (!equal)
2808 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2810 return 0;
2813 static int test_split_periods(isl_ctx *ctx)
2815 const char *str;
2816 isl_pw_qpolynomial *pwqp;
2818 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2819 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2820 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2821 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2823 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2825 isl_pw_qpolynomial_free(pwqp);
2827 if (!pwqp)
2828 return -1;
2830 return 0;
2833 static int test_union(isl_ctx *ctx)
2835 const char *str;
2836 isl_union_set *uset1, *uset2;
2837 isl_union_map *umap1, *umap2;
2838 int equal;
2840 str = "{ [i] : 0 <= i <= 1 }";
2841 uset1 = isl_union_set_read_from_str(ctx, str);
2842 str = "{ [1] -> [0] }";
2843 umap1 = isl_union_map_read_from_str(ctx, str);
2845 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2846 equal = isl_union_map_is_equal(umap1, umap2);
2848 isl_union_map_free(umap1);
2849 isl_union_map_free(umap2);
2851 if (equal < 0)
2852 return -1;
2853 if (!equal)
2854 isl_die(ctx, isl_error_unknown, "union maps not equal",
2855 return -1);
2857 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2858 umap1 = isl_union_map_read_from_str(ctx, str);
2859 str = "{ A[i]; B[i] }";
2860 uset1 = isl_union_set_read_from_str(ctx, str);
2862 uset2 = isl_union_map_domain(umap1);
2864 equal = isl_union_set_is_equal(uset1, uset2);
2866 isl_union_set_free(uset1);
2867 isl_union_set_free(uset2);
2869 if (equal < 0)
2870 return -1;
2871 if (!equal)
2872 isl_die(ctx, isl_error_unknown, "union sets not equal",
2873 return -1);
2875 return 0;
2878 /* Check that computing a bound of a non-zero polynomial over an unbounded
2879 * domain does not produce a rational value.
2880 * In particular, check that the upper bound is infinity.
2882 static int test_bound_unbounded_domain(isl_ctx *ctx)
2884 const char *str;
2885 isl_pw_qpolynomial *pwqp;
2886 isl_pw_qpolynomial_fold *pwf, *pwf2;
2887 isl_bool equal;
2889 str = "{ [m,n] -> -m * n }";
2890 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2891 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2892 str = "{ infty }";
2893 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2894 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2895 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
2896 isl_pw_qpolynomial_fold_free(pwf);
2897 isl_pw_qpolynomial_fold_free(pwf2);
2899 if (equal < 0)
2900 return -1;
2901 if (!equal)
2902 isl_die(ctx, isl_error_unknown,
2903 "expecting infinite polynomial bound", return -1);
2905 return 0;
2908 static int test_bound(isl_ctx *ctx)
2910 const char *str;
2911 unsigned dim;
2912 isl_pw_qpolynomial *pwqp;
2913 isl_pw_qpolynomial_fold *pwf;
2915 if (test_bound_unbounded_domain(ctx) < 0)
2916 return -1;
2918 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2919 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2920 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2921 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
2922 isl_pw_qpolynomial_fold_free(pwf);
2923 if (dim != 4)
2924 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
2925 return -1);
2927 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2928 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2929 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2930 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
2931 isl_pw_qpolynomial_fold_free(pwf);
2932 if (dim != 1)
2933 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
2934 return -1);
2936 return 0;
2939 static int test_lift(isl_ctx *ctx)
2941 const char *str;
2942 isl_basic_map *bmap;
2943 isl_basic_set *bset;
2945 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2946 bset = isl_basic_set_read_from_str(ctx, str);
2947 bset = isl_basic_set_lift(bset);
2948 bmap = isl_basic_map_from_range(bset);
2949 bset = isl_basic_map_domain(bmap);
2950 isl_basic_set_free(bset);
2952 return 0;
2955 struct {
2956 const char *set1;
2957 const char *set2;
2958 int subset;
2959 } subset_tests[] = {
2960 { "{ [112, 0] }",
2961 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2962 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2963 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2964 { "{ [65] }",
2965 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2966 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2967 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2968 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2969 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2970 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2971 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2972 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2973 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2974 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2975 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2976 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2977 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2978 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
2979 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
2980 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
2981 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
2982 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
2983 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
2984 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
2985 "4e0 <= -57 + i0 + i1)) or "
2986 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
2987 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
2988 "4e0 >= -61 + i0 + i1)) or "
2989 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
2990 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
2993 static int test_subset(isl_ctx *ctx)
2995 int i;
2996 isl_set *set1, *set2;
2997 int subset;
2999 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3000 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3001 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3002 subset = isl_set_is_subset(set1, set2);
3003 isl_set_free(set1);
3004 isl_set_free(set2);
3005 if (subset < 0)
3006 return -1;
3007 if (subset != subset_tests[i].subset)
3008 isl_die(ctx, isl_error_unknown,
3009 "incorrect subset result", return -1);
3012 return 0;
3015 struct {
3016 const char *minuend;
3017 const char *subtrahend;
3018 const char *difference;
3019 } subtract_domain_tests[] = {
3020 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3021 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3022 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3025 static int test_subtract(isl_ctx *ctx)
3027 int i;
3028 isl_union_map *umap1, *umap2;
3029 isl_union_pw_multi_aff *upma1, *upma2;
3030 isl_union_set *uset;
3031 int equal;
3033 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3034 umap1 = isl_union_map_read_from_str(ctx,
3035 subtract_domain_tests[i].minuend);
3036 uset = isl_union_set_read_from_str(ctx,
3037 subtract_domain_tests[i].subtrahend);
3038 umap2 = isl_union_map_read_from_str(ctx,
3039 subtract_domain_tests[i].difference);
3040 umap1 = isl_union_map_subtract_domain(umap1, uset);
3041 equal = isl_union_map_is_equal(umap1, umap2);
3042 isl_union_map_free(umap1);
3043 isl_union_map_free(umap2);
3044 if (equal < 0)
3045 return -1;
3046 if (!equal)
3047 isl_die(ctx, isl_error_unknown,
3048 "incorrect subtract domain result", return -1);
3051 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3052 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3053 subtract_domain_tests[i].minuend);
3054 uset = isl_union_set_read_from_str(ctx,
3055 subtract_domain_tests[i].subtrahend);
3056 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3057 subtract_domain_tests[i].difference);
3058 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3059 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3060 isl_union_pw_multi_aff_free(upma1);
3061 isl_union_pw_multi_aff_free(upma2);
3062 if (equal < 0)
3063 return -1;
3064 if (!equal)
3065 isl_die(ctx, isl_error_unknown,
3066 "incorrect subtract domain result", return -1);
3069 return 0;
3072 /* Check that intersecting the empty basic set with another basic set
3073 * does not increase the number of constraints. In particular,
3074 * the empty basic set should maintain its canonical representation.
3076 static int test_intersect(isl_ctx *ctx)
3078 int n1, n2;
3079 isl_basic_set *bset1, *bset2;
3081 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3082 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3083 n1 = isl_basic_set_n_constraint(bset1);
3084 bset1 = isl_basic_set_intersect(bset1, bset2);
3085 n2 = isl_basic_set_n_constraint(bset1);
3086 isl_basic_set_free(bset1);
3087 if (!bset1)
3088 return -1;
3089 if (n1 != n2)
3090 isl_die(ctx, isl_error_unknown,
3091 "number of constraints of empty set changed",
3092 return -1);
3094 return 0;
3097 int test_factorize(isl_ctx *ctx)
3099 const char *str;
3100 isl_basic_set *bset;
3101 isl_factorizer *f;
3103 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3104 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3105 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3106 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3107 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3108 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3109 "3i5 >= -2i0 - i2 + 3i4 }";
3110 bset = isl_basic_set_read_from_str(ctx, str);
3111 f = isl_basic_set_factorizer(bset);
3112 isl_basic_set_free(bset);
3113 isl_factorizer_free(f);
3114 if (!f)
3115 isl_die(ctx, isl_error_unknown,
3116 "failed to construct factorizer", return -1);
3118 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3119 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3120 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3121 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3122 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3123 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3124 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3125 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3126 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3127 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3128 bset = isl_basic_set_read_from_str(ctx, str);
3129 f = isl_basic_set_factorizer(bset);
3130 isl_basic_set_free(bset);
3131 isl_factorizer_free(f);
3132 if (!f)
3133 isl_die(ctx, isl_error_unknown,
3134 "failed to construct factorizer", return -1);
3136 return 0;
3139 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3141 int *injective = user;
3143 *injective = isl_map_is_injective(map);
3144 isl_map_free(map);
3146 if (*injective < 0 || !*injective)
3147 return isl_stat_error;
3149 return isl_stat_ok;
3152 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3153 const char *r, const char *s, int tilable, int parallel)
3155 int i;
3156 isl_union_set *D;
3157 isl_union_map *W, *R, *S;
3158 isl_union_map *empty;
3159 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3160 isl_union_map *validity, *proximity, *coincidence;
3161 isl_union_map *schedule;
3162 isl_union_map *test;
3163 isl_union_set *delta;
3164 isl_union_set *domain;
3165 isl_set *delta_set;
3166 isl_set *slice;
3167 isl_set *origin;
3168 isl_schedule_constraints *sc;
3169 isl_schedule *sched;
3170 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3172 D = isl_union_set_read_from_str(ctx, d);
3173 W = isl_union_map_read_from_str(ctx, w);
3174 R = isl_union_map_read_from_str(ctx, r);
3175 S = isl_union_map_read_from_str(ctx, s);
3177 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3178 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3180 empty = isl_union_map_empty(isl_union_map_get_space(S));
3181 isl_union_map_compute_flow(isl_union_map_copy(R),
3182 isl_union_map_copy(W), empty,
3183 isl_union_map_copy(S),
3184 &dep_raw, NULL, NULL, NULL);
3185 isl_union_map_compute_flow(isl_union_map_copy(W),
3186 isl_union_map_copy(W),
3187 isl_union_map_copy(R),
3188 isl_union_map_copy(S),
3189 &dep_waw, &dep_war, NULL, NULL);
3191 dep = isl_union_map_union(dep_waw, dep_war);
3192 dep = isl_union_map_union(dep, dep_raw);
3193 validity = isl_union_map_copy(dep);
3194 coincidence = isl_union_map_copy(dep);
3195 proximity = isl_union_map_copy(dep);
3197 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3198 sc = isl_schedule_constraints_set_validity(sc, validity);
3199 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3200 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3201 sched = isl_schedule_constraints_compute_schedule(sc);
3202 schedule = isl_schedule_get_map(sched);
3203 isl_schedule_free(sched);
3204 isl_union_map_free(W);
3205 isl_union_map_free(R);
3206 isl_union_map_free(S);
3208 is_injection = 1;
3209 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3211 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3212 is_complete = isl_union_set_is_subset(D, domain);
3213 isl_union_set_free(D);
3214 isl_union_set_free(domain);
3216 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3217 test = isl_union_map_apply_range(test, dep);
3218 test = isl_union_map_apply_range(test, schedule);
3220 delta = isl_union_map_deltas(test);
3221 if (isl_union_set_n_set(delta) == 0) {
3222 is_tilable = 1;
3223 is_parallel = 1;
3224 is_nonneg = 1;
3225 isl_union_set_free(delta);
3226 } else {
3227 delta_set = isl_set_from_union_set(delta);
3229 slice = isl_set_universe(isl_set_get_space(delta_set));
3230 for (i = 0; i < tilable; ++i)
3231 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3232 is_tilable = isl_set_is_subset(delta_set, slice);
3233 isl_set_free(slice);
3235 slice = isl_set_universe(isl_set_get_space(delta_set));
3236 for (i = 0; i < parallel; ++i)
3237 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3238 is_parallel = isl_set_is_subset(delta_set, slice);
3239 isl_set_free(slice);
3241 origin = isl_set_universe(isl_set_get_space(delta_set));
3242 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
3243 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3245 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3246 delta_set = isl_set_lexmin(delta_set);
3248 is_nonneg = isl_set_is_equal(delta_set, origin);
3250 isl_set_free(origin);
3251 isl_set_free(delta_set);
3254 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3255 is_injection < 0 || is_complete < 0)
3256 return -1;
3257 if (!is_complete)
3258 isl_die(ctx, isl_error_unknown,
3259 "generated schedule incomplete", return -1);
3260 if (!is_injection)
3261 isl_die(ctx, isl_error_unknown,
3262 "generated schedule not injective on each statement",
3263 return -1);
3264 if (!is_nonneg)
3265 isl_die(ctx, isl_error_unknown,
3266 "negative dependences in generated schedule",
3267 return -1);
3268 if (!is_tilable)
3269 isl_die(ctx, isl_error_unknown,
3270 "generated schedule not as tilable as expected",
3271 return -1);
3272 if (!is_parallel)
3273 isl_die(ctx, isl_error_unknown,
3274 "generated schedule not as parallel as expected",
3275 return -1);
3277 return 0;
3280 /* Compute a schedule for the given instance set, validity constraints,
3281 * proximity constraints and context and return a corresponding union map
3282 * representation.
3284 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3285 const char *domain, const char *validity, const char *proximity,
3286 const char *context)
3288 isl_set *con;
3289 isl_union_set *dom;
3290 isl_union_map *dep;
3291 isl_union_map *prox;
3292 isl_schedule_constraints *sc;
3293 isl_schedule *schedule;
3294 isl_union_map *sched;
3296 con = isl_set_read_from_str(ctx, context);
3297 dom = isl_union_set_read_from_str(ctx, domain);
3298 dep = isl_union_map_read_from_str(ctx, validity);
3299 prox = isl_union_map_read_from_str(ctx, proximity);
3300 sc = isl_schedule_constraints_on_domain(dom);
3301 sc = isl_schedule_constraints_set_context(sc, con);
3302 sc = isl_schedule_constraints_set_validity(sc, dep);
3303 sc = isl_schedule_constraints_set_proximity(sc, prox);
3304 schedule = isl_schedule_constraints_compute_schedule(sc);
3305 sched = isl_schedule_get_map(schedule);
3306 isl_schedule_free(schedule);
3308 return sched;
3311 /* Compute a schedule for the given instance set, validity constraints and
3312 * proximity constraints and return a corresponding union map representation.
3314 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
3315 const char *domain, const char *validity, const char *proximity)
3317 return compute_schedule_with_context(ctx, domain, validity, proximity,
3318 "{ : }");
3321 /* Check that a schedule can be constructed on the given domain
3322 * with the given validity and proximity constraints.
3324 static int test_has_schedule(isl_ctx *ctx, const char *domain,
3325 const char *validity, const char *proximity)
3327 isl_union_map *sched;
3329 sched = compute_schedule(ctx, domain, validity, proximity);
3330 if (!sched)
3331 return -1;
3333 isl_union_map_free(sched);
3334 return 0;
3337 int test_special_schedule(isl_ctx *ctx, const char *domain,
3338 const char *validity, const char *proximity, const char *expected_sched)
3340 isl_union_map *sched1, *sched2;
3341 int equal;
3343 sched1 = compute_schedule(ctx, domain, validity, proximity);
3344 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
3346 equal = isl_union_map_is_equal(sched1, sched2);
3347 isl_union_map_free(sched1);
3348 isl_union_map_free(sched2);
3350 if (equal < 0)
3351 return -1;
3352 if (!equal)
3353 isl_die(ctx, isl_error_unknown, "unexpected schedule",
3354 return -1);
3356 return 0;
3359 /* Check that the schedule map is properly padded, even after being
3360 * reconstructed from the band forest.
3362 static int test_padded_schedule(isl_ctx *ctx)
3364 const char *str;
3365 isl_union_set *D;
3366 isl_union_map *validity, *proximity;
3367 isl_schedule_constraints *sc;
3368 isl_schedule *sched;
3369 isl_union_map *map1, *map2;
3370 isl_band_list *list;
3371 int equal;
3373 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
3374 D = isl_union_set_read_from_str(ctx, str);
3375 validity = isl_union_map_empty(isl_union_set_get_space(D));
3376 proximity = isl_union_map_copy(validity);
3377 sc = isl_schedule_constraints_on_domain(D);
3378 sc = isl_schedule_constraints_set_validity(sc, validity);
3379 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3380 sched = isl_schedule_constraints_compute_schedule(sc);
3381 map1 = isl_schedule_get_map(sched);
3382 list = isl_schedule_get_band_forest(sched);
3383 isl_band_list_free(list);
3384 map2 = isl_schedule_get_map(sched);
3385 isl_schedule_free(sched);
3386 equal = isl_union_map_is_equal(map1, map2);
3387 isl_union_map_free(map1);
3388 isl_union_map_free(map2);
3390 if (equal < 0)
3391 return -1;
3392 if (!equal)
3393 isl_die(ctx, isl_error_unknown,
3394 "reconstructed schedule map not the same as original",
3395 return -1);
3397 return 0;
3400 /* Check that conditional validity constraints are also taken into
3401 * account across bands.
3402 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
3403 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
3404 * and then check that the adjacent order constraint C[2,1]->D[2,0]
3405 * is enforced by the rest of the schedule.
3407 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
3409 const char *str;
3410 isl_union_set *domain;
3411 isl_union_map *validity, *proximity, *condition;
3412 isl_union_map *sink, *source, *dep;
3413 isl_schedule_constraints *sc;
3414 isl_schedule *schedule;
3415 isl_union_access_info *access;
3416 isl_union_flow *flow;
3417 int empty;
3419 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3420 "A[k] : k >= 1 and k <= -1 + n; "
3421 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3422 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
3423 domain = isl_union_set_read_from_str(ctx, str);
3424 sc = isl_schedule_constraints_on_domain(domain);
3425 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
3426 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3427 "D[k, i] -> C[1 + k, i] : "
3428 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3429 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
3430 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
3431 validity = isl_union_map_read_from_str(ctx, str);
3432 sc = isl_schedule_constraints_set_validity(sc, validity);
3433 str = "[n] -> { C[k, i] -> D[k, i] : "
3434 "0 <= i <= -1 + k and k <= -1 + n }";
3435 proximity = isl_union_map_read_from_str(ctx, str);
3436 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3437 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
3438 "i <= -1 + k and i >= 1 and k <= -2 + n; "
3439 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
3440 "k <= -1 + n and i >= 0 and i <= -2 + k }";
3441 condition = isl_union_map_read_from_str(ctx, str);
3442 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
3443 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3444 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
3445 "i >= 0 and i <= -1 + k and k <= -1 + n and "
3446 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
3447 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
3448 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3449 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
3450 "k <= -1 + n and i >= 0 and i <= -1 + k and "
3451 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
3452 validity = isl_union_map_read_from_str(ctx, str);
3453 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3454 validity);
3455 schedule = isl_schedule_constraints_compute_schedule(sc);
3456 str = "{ D[2,0] -> [] }";
3457 sink = isl_union_map_read_from_str(ctx, str);
3458 access = isl_union_access_info_from_sink(sink);
3459 str = "{ C[2,1] -> [] }";
3460 source = isl_union_map_read_from_str(ctx, str);
3461 access = isl_union_access_info_set_must_source(access, source);
3462 access = isl_union_access_info_set_schedule(access, schedule);
3463 flow = isl_union_access_info_compute_flow(access);
3464 dep = isl_union_flow_get_must_dependence(flow);
3465 isl_union_flow_free(flow);
3466 empty = isl_union_map_is_empty(dep);
3467 isl_union_map_free(dep);
3469 if (empty < 0)
3470 return -1;
3471 if (empty)
3472 isl_die(ctx, isl_error_unknown,
3473 "conditional validity not respected", return -1);
3475 return 0;
3478 /* Input for testing of schedule construction based on
3479 * conditional constraints.
3481 * domain is the iteration domain
3482 * flow are the flow dependences, which determine the validity and
3483 * proximity constraints
3484 * condition are the conditions on the conditional validity constraints
3485 * conditional_validity are the conditional validity constraints
3486 * outer_band_n is the expected number of members in the outer band
3488 struct {
3489 const char *domain;
3490 const char *flow;
3491 const char *condition;
3492 const char *conditional_validity;
3493 int outer_band_n;
3494 } live_range_tests[] = {
3495 /* Contrived example that illustrates that we need to keep
3496 * track of tagged condition dependences and
3497 * tagged conditional validity dependences
3498 * in isl_sched_edge separately.
3499 * In particular, the conditional validity constraints on A
3500 * cannot be satisfied,
3501 * but they can be ignored because there are no corresponding
3502 * condition constraints. However, we do have an additional
3503 * conditional validity constraint that maps to the same
3504 * dependence relation
3505 * as the condition constraint on B. If we did not make a distinction
3506 * between tagged condition and tagged conditional validity
3507 * dependences, then we
3508 * could end up treating this shared dependence as an condition
3509 * constraint on A, forcing a localization of the conditions,
3510 * which is impossible.
3512 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
3513 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
3514 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
3515 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3516 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3517 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
3520 /* TACO 2013 Fig. 7 */
3521 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3522 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3523 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3524 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
3525 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
3526 "0 <= i < n and 0 <= j < n - 1 }",
3527 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
3528 "0 <= i < n and 0 <= j < j' < n;"
3529 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
3530 "0 <= i < i' < n and 0 <= j,j' < n;"
3531 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
3532 "0 <= i,j,j' < n and j < j' }",
3535 /* TACO 2013 Fig. 7, without tags */
3536 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3537 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3538 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3539 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3540 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3541 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
3542 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
3543 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
3546 /* TACO 2013 Fig. 12 */
3547 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
3548 "S3[i,3] : 0 <= i <= 1 }",
3549 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
3550 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
3551 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
3552 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
3553 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3554 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
3555 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3556 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
3557 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
3558 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
3559 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
3564 /* Test schedule construction based on conditional constraints.
3565 * In particular, check the number of members in the outer band node
3566 * as an indication of whether tiling is possible or not.
3568 static int test_conditional_schedule_constraints(isl_ctx *ctx)
3570 int i;
3571 isl_union_set *domain;
3572 isl_union_map *condition;
3573 isl_union_map *flow;
3574 isl_union_map *validity;
3575 isl_schedule_constraints *sc;
3576 isl_schedule *schedule;
3577 isl_schedule_node *node;
3578 int n_member;
3580 if (test_special_conditional_schedule_constraints(ctx) < 0)
3581 return -1;
3583 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
3584 domain = isl_union_set_read_from_str(ctx,
3585 live_range_tests[i].domain);
3586 flow = isl_union_map_read_from_str(ctx,
3587 live_range_tests[i].flow);
3588 condition = isl_union_map_read_from_str(ctx,
3589 live_range_tests[i].condition);
3590 validity = isl_union_map_read_from_str(ctx,
3591 live_range_tests[i].conditional_validity);
3592 sc = isl_schedule_constraints_on_domain(domain);
3593 sc = isl_schedule_constraints_set_validity(sc,
3594 isl_union_map_copy(flow));
3595 sc = isl_schedule_constraints_set_proximity(sc, flow);
3596 sc = isl_schedule_constraints_set_conditional_validity(sc,
3597 condition, validity);
3598 schedule = isl_schedule_constraints_compute_schedule(sc);
3599 node = isl_schedule_get_root(schedule);
3600 while (node &&
3601 isl_schedule_node_get_type(node) != isl_schedule_node_band)
3602 node = isl_schedule_node_first_child(node);
3603 n_member = isl_schedule_node_band_n_member(node);
3604 isl_schedule_node_free(node);
3605 isl_schedule_free(schedule);
3607 if (!schedule)
3608 return -1;
3609 if (n_member != live_range_tests[i].outer_band_n)
3610 isl_die(ctx, isl_error_unknown,
3611 "unexpected number of members in outer band",
3612 return -1);
3614 return 0;
3617 /* Check that the schedule computed for the given instance set and
3618 * dependence relation strongly satisfies the dependences.
3619 * In particular, check that no instance is scheduled before
3620 * or together with an instance on which it depends.
3621 * Earlier versions of isl would produce a schedule that
3622 * only weakly satisfies the dependences.
3624 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
3626 const char *domain, *dep;
3627 isl_union_map *D, *schedule;
3628 isl_map *map, *ge;
3629 int empty;
3631 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
3632 "A[i0] : 0 <= i0 <= 1 }";
3633 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
3634 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
3635 schedule = compute_schedule(ctx, domain, dep, dep);
3636 D = isl_union_map_read_from_str(ctx, dep);
3637 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
3638 D = isl_union_map_apply_range(D, schedule);
3639 map = isl_map_from_union_map(D);
3640 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3641 map = isl_map_intersect(map, ge);
3642 empty = isl_map_is_empty(map);
3643 isl_map_free(map);
3645 if (empty < 0)
3646 return -1;
3647 if (!empty)
3648 isl_die(ctx, isl_error_unknown,
3649 "dependences not strongly satisfied", return -1);
3651 return 0;
3654 /* Compute a schedule for input where the instance set constraints
3655 * conflict with the context constraints.
3656 * Earlier versions of isl did not properly handle this situation.
3658 static int test_conflicting_context_schedule(isl_ctx *ctx)
3660 isl_union_map *schedule;
3661 const char *domain, *context;
3663 domain = "[n] -> { A[] : n >= 0 }";
3664 context = "[n] -> { : n < 0 }";
3665 schedule = compute_schedule_with_context(ctx,
3666 domain, "{}", "{}", context);
3667 isl_union_map_free(schedule);
3669 if (!schedule)
3670 return -1;
3672 return 0;
3675 /* Check that the dependence carrying step is not confused by
3676 * a bound on the coefficient size.
3677 * In particular, force the scheduler to move to a dependence carrying
3678 * step by demanding outer coincidence and bound the size of
3679 * the coefficients. Earlier versions of isl would take this
3680 * bound into account while carrying dependences, breaking
3681 * fundamental assumptions.
3683 static int test_bounded_coefficients_schedule(isl_ctx *ctx)
3685 const char *domain, *dep;
3686 isl_union_set *I;
3687 isl_union_map *D;
3688 isl_schedule_constraints *sc;
3689 isl_schedule *schedule;
3691 domain = "{ C[i0, i1] : 2 <= i0 <= 3999 and 0 <= i1 <= -1 + i0 }";
3692 dep = "{ C[i0, i1] -> C[i0, 1 + i1] : i0 <= 3999 and i1 >= 0 and "
3693 "i1 <= -2 + i0; "
3694 "C[i0, -1 + i0] -> C[1 + i0, 0] : i0 <= 3998 and i0 >= 1 }";
3695 I = isl_union_set_read_from_str(ctx, domain);
3696 D = isl_union_map_read_from_str(ctx, dep);
3697 sc = isl_schedule_constraints_on_domain(I);
3698 sc = isl_schedule_constraints_set_validity(sc, isl_union_map_copy(D));
3699 sc = isl_schedule_constraints_set_coincidence(sc, D);
3700 isl_options_set_schedule_outer_coincidence(ctx, 1);
3701 isl_options_set_schedule_max_coefficient(ctx, 20);
3702 schedule = isl_schedule_constraints_compute_schedule(sc);
3703 isl_options_set_schedule_max_coefficient(ctx, -1);
3704 isl_options_set_schedule_outer_coincidence(ctx, 0);
3705 isl_schedule_free(schedule);
3707 if (!schedule)
3708 return -1;
3710 return 0;
3713 int test_schedule(isl_ctx *ctx)
3715 const char *D, *W, *R, *V, *P, *S;
3716 int max_coincidence;
3718 /* Handle resulting schedule with zero bands. */
3719 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
3720 return -1;
3722 /* Jacobi */
3723 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
3724 W = "{ S1[t,i] -> a[t,i] }";
3725 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
3726 "S1[t,i] -> a[t-1,i+1] }";
3727 S = "{ S1[t,i] -> [t,i] }";
3728 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3729 return -1;
3731 /* Fig. 5 of CC2008 */
3732 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
3733 "j <= -1 + N }";
3734 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
3735 "j >= 2 and j <= -1 + N }";
3736 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
3737 "j >= 2 and j <= -1 + N; "
3738 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
3739 "j >= 2 and j <= -1 + N }";
3740 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3741 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3742 return -1;
3744 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
3745 W = "{ S1[i] -> a[i] }";
3746 R = "{ S2[i] -> a[i+1] }";
3747 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3748 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3749 return -1;
3751 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
3752 W = "{ S1[i] -> a[i] }";
3753 R = "{ S2[i] -> a[9-i] }";
3754 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3755 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3756 return -1;
3758 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
3759 W = "{ S1[i] -> a[i] }";
3760 R = "[N] -> { S2[i] -> a[N-1-i] }";
3761 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3762 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3763 return -1;
3765 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
3766 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
3767 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
3768 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
3769 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3770 return -1;
3772 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3773 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
3774 R = "{ S2[i,j] -> a[i-1,j] }";
3775 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3776 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3777 return -1;
3779 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3780 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
3781 R = "{ S2[i,j] -> a[i,j-1] }";
3782 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3783 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3784 return -1;
3786 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
3787 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
3788 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
3789 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
3790 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
3791 "S_0[] -> [0, 0, 0] }";
3792 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
3793 return -1;
3794 ctx->opt->schedule_parametric = 0;
3795 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3796 return -1;
3797 ctx->opt->schedule_parametric = 1;
3799 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
3800 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
3801 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
3802 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
3803 "S4[i] -> a[i,N] }";
3804 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
3805 "S4[i] -> [4,i,0] }";
3806 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
3807 isl_options_set_schedule_maximize_coincidence(ctx, 0);
3808 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3809 return -1;
3810 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
3812 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
3813 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3814 "j <= N }";
3815 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3816 "j <= N; "
3817 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
3818 "j <= N }";
3819 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3820 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3821 return -1;
3823 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
3824 " S_2[t] : t >= 0 and t <= -1 + N; "
3825 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
3826 "i <= -1 + N }";
3827 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
3828 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
3829 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
3830 "i >= 0 and i <= -1 + N }";
3831 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
3832 "i >= 0 and i <= -1 + N; "
3833 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
3834 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
3835 " S_0[t] -> [0, t, 0] }";
3837 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3838 return -1;
3839 ctx->opt->schedule_parametric = 0;
3840 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3841 return -1;
3842 ctx->opt->schedule_parametric = 1;
3844 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
3845 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
3846 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
3847 return -1;
3849 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
3850 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
3851 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
3852 "j >= 0 and j <= -1 + N; "
3853 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3854 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
3855 "j >= 0 and j <= -1 + N; "
3856 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3857 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
3858 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3859 return -1;
3861 D = "{ S_0[i] : i >= 0 }";
3862 W = "{ S_0[i] -> a[i] : i >= 0 }";
3863 R = "{ S_0[i] -> a[0] : i >= 0 }";
3864 S = "{ S_0[i] -> [0, i, 0] }";
3865 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3866 return -1;
3868 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
3869 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
3870 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
3871 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
3872 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3873 return -1;
3875 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
3876 "k <= -1 + n and k >= 0 }";
3877 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
3878 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
3879 "k <= -1 + n and k >= 0; "
3880 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
3881 "k <= -1 + n and k >= 0; "
3882 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
3883 "k <= -1 + n and k >= 0 }";
3884 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
3885 ctx->opt->schedule_outer_coincidence = 1;
3886 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3887 return -1;
3888 ctx->opt->schedule_outer_coincidence = 0;
3890 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3891 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3892 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3893 "Stmt_for_body24[i0, i1, 1, 0]:"
3894 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3895 "Stmt_for_body7[i0, i1, i2]:"
3896 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3897 "i2 <= 7 }";
3899 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3900 "Stmt_for_body24[1, i1, i2, i3]:"
3901 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3902 "i2 >= 1;"
3903 "Stmt_for_body24[0, i1, i2, i3] -> "
3904 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3905 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3906 "i3 >= 0;"
3907 "Stmt_for_body24[0, i1, i2, i3] ->"
3908 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3909 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3910 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3911 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3912 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3913 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3914 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3915 "i1 <= 6 and i1 >= 0;"
3916 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3917 "Stmt_for_body7[i0, i1, i2] -> "
3918 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3919 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3920 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3921 "Stmt_for_body7[i0, i1, i2] -> "
3922 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3923 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3924 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3925 P = V;
3926 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3927 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3928 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3930 if (test_special_schedule(ctx, D, V, P, S) < 0)
3931 return -1;
3933 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3934 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3935 "j >= 1 and j <= 7;"
3936 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3937 "j >= 1 and j <= 8 }";
3938 P = "{ }";
3939 S = "{ S_0[i, j] -> [i + j, j] }";
3940 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3941 if (test_special_schedule(ctx, D, V, P, S) < 0)
3942 return -1;
3943 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3945 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3946 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3947 "j >= 0 and j <= -1 + i }";
3948 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3949 "i <= -1 + N and j >= 0;"
3950 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3951 "i <= -2 + N }";
3952 P = "{ }";
3953 S = "{ S_0[i, j] -> [i, j] }";
3954 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3955 if (test_special_schedule(ctx, D, V, P, S) < 0)
3956 return -1;
3957 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3959 /* Test both algorithms on a case with only proximity dependences. */
3960 D = "{ S[i,j] : 0 <= i <= 10 }";
3961 V = "{ }";
3962 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3963 S = "{ S[i, j] -> [j, i] }";
3964 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3965 if (test_special_schedule(ctx, D, V, P, S) < 0)
3966 return -1;
3967 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3968 if (test_special_schedule(ctx, D, V, P, S) < 0)
3969 return -1;
3971 D = "{ A[a]; B[] }";
3972 V = "{}";
3973 P = "{ A[a] -> B[] }";
3974 if (test_has_schedule(ctx, D, V, P) < 0)
3975 return -1;
3977 if (test_padded_schedule(ctx) < 0)
3978 return -1;
3980 /* Check that check for progress is not confused by rational
3981 * solution.
3983 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3984 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3985 "i0 <= -2 + N; "
3986 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3987 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3988 P = "{}";
3989 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3990 if (test_has_schedule(ctx, D, V, P) < 0)
3991 return -1;
3992 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3994 /* Check that we allow schedule rows that are only non-trivial
3995 * on some full-dimensional domains.
3997 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
3998 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
3999 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4000 P = "{}";
4001 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4002 if (test_has_schedule(ctx, D, V, P) < 0)
4003 return -1;
4004 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4006 if (test_conditional_schedule_constraints(ctx) < 0)
4007 return -1;
4009 if (test_strongly_satisfying_schedule(ctx) < 0)
4010 return -1;
4012 if (test_conflicting_context_schedule(ctx) < 0)
4013 return -1;
4015 if (test_bounded_coefficients_schedule(ctx) < 0)
4016 return -1;
4018 return 0;
4021 /* Perform scheduling tests using the whole component scheduler.
4023 static int test_schedule_whole(isl_ctx *ctx)
4025 int whole;
4026 int r;
4028 whole = isl_options_get_schedule_whole_component(ctx);
4029 isl_options_set_schedule_whole_component(ctx, 1);
4030 r = test_schedule(ctx);
4031 isl_options_set_schedule_whole_component(ctx, whole);
4033 return r;
4036 /* Perform scheduling tests using the incremental scheduler.
4038 static int test_schedule_incremental(isl_ctx *ctx)
4040 int whole;
4041 int r;
4043 whole = isl_options_get_schedule_whole_component(ctx);
4044 isl_options_set_schedule_whole_component(ctx, 0);
4045 r = test_schedule(ctx);
4046 isl_options_set_schedule_whole_component(ctx, whole);
4048 return r;
4051 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4053 isl_union_map *umap;
4054 int test;
4056 umap = isl_union_map_read_from_str(ctx, str);
4057 test = isl_union_map_plain_is_injective(umap);
4058 isl_union_map_free(umap);
4059 if (test < 0)
4060 return -1;
4061 if (test == injective)
4062 return 0;
4063 if (injective)
4064 isl_die(ctx, isl_error_unknown,
4065 "map not detected as injective", return -1);
4066 else
4067 isl_die(ctx, isl_error_unknown,
4068 "map detected as injective", return -1);
4071 int test_injective(isl_ctx *ctx)
4073 const char *str;
4075 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4076 return -1;
4077 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4078 return -1;
4079 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4080 return -1;
4081 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4082 return -1;
4083 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4084 return -1;
4085 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4086 return -1;
4087 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4088 return -1;
4089 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4090 return -1;
4092 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4093 if (test_plain_injective(ctx, str, 1))
4094 return -1;
4095 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4096 if (test_plain_injective(ctx, str, 0))
4097 return -1;
4099 return 0;
4102 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4104 isl_aff *aff2;
4105 int equal;
4107 if (!aff)
4108 return -1;
4110 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4111 equal = isl_aff_plain_is_equal(aff, aff2);
4112 isl_aff_free(aff2);
4114 return equal;
4117 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4119 int equal;
4121 equal = aff_plain_is_equal(aff, str);
4122 if (equal < 0)
4123 return -1;
4124 if (!equal)
4125 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4126 "result not as expected", return -1);
4127 return 0;
4130 struct {
4131 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4132 __isl_take isl_aff *aff2);
4133 } aff_bin_op[] = {
4134 ['+'] = { &isl_aff_add },
4135 ['-'] = { &isl_aff_sub },
4136 ['*'] = { &isl_aff_mul },
4137 ['/'] = { &isl_aff_div },
4140 struct {
4141 const char *arg1;
4142 unsigned char op;
4143 const char *arg2;
4144 const char *res;
4145 } aff_bin_tests[] = {
4146 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
4147 "{ [i] -> [2i] }" },
4148 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
4149 "{ [i] -> [0] }" },
4150 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
4151 "{ [i] -> [2i] }" },
4152 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
4153 "{ [i] -> [2i] }" },
4154 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
4155 "{ [i] -> [i/2] }" },
4156 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
4157 "{ [i] -> [i] }" },
4158 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
4159 "{ [i] -> [NaN] }" },
4160 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
4161 "{ [i] -> [NaN] }" },
4162 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
4163 "{ [i] -> [NaN] }" },
4164 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
4165 "{ [i] -> [NaN] }" },
4166 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
4167 "{ [i] -> [NaN] }" },
4168 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
4169 "{ [i] -> [NaN] }" },
4170 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
4171 "{ [i] -> [NaN] }" },
4172 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
4173 "{ [i] -> [NaN] }" },
4174 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
4175 "{ [i] -> [NaN] }" },
4176 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
4177 "{ [i] -> [NaN] }" },
4178 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
4179 "{ [i] -> [NaN] }" },
4180 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
4181 "{ [i] -> [NaN] }" },
4184 /* Perform some basic tests of binary operations on isl_aff objects.
4186 static int test_bin_aff(isl_ctx *ctx)
4188 int i;
4189 isl_aff *aff1, *aff2, *res;
4190 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4191 __isl_take isl_aff *aff2);
4192 int ok;
4194 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
4195 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
4196 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
4197 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
4198 fn = aff_bin_op[aff_bin_tests[i].op].fn;
4199 aff1 = fn(aff1, aff2);
4200 if (isl_aff_is_nan(res))
4201 ok = isl_aff_is_nan(aff1);
4202 else
4203 ok = isl_aff_plain_is_equal(aff1, res);
4204 isl_aff_free(aff1);
4205 isl_aff_free(res);
4206 if (ok < 0)
4207 return -1;
4208 if (!ok)
4209 isl_die(ctx, isl_error_unknown,
4210 "unexpected result", return -1);
4213 return 0;
4216 struct {
4217 __isl_give isl_union_pw_multi_aff *(*fn)(
4218 __isl_take isl_union_pw_multi_aff *upma1,
4219 __isl_take isl_union_pw_multi_aff *upma2);
4220 const char *arg1;
4221 const char *arg2;
4222 const char *res;
4223 } upma_bin_tests[] = {
4224 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
4225 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
4226 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
4227 "{ B[x] -> [2] : x >= 0 }",
4228 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
4229 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
4230 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
4231 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
4232 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
4233 "D[i] -> B[2] : i >= 5 }" },
4234 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4235 "{ B[x] -> C[2] : x > 0 }",
4236 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
4237 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4238 "{ B[x] -> A[2] : x >= 0 }",
4239 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
4242 /* Perform some basic tests of binary operations on
4243 * isl_union_pw_multi_aff objects.
4245 static int test_bin_upma(isl_ctx *ctx)
4247 int i;
4248 isl_union_pw_multi_aff *upma1, *upma2, *res;
4249 int ok;
4251 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
4252 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4253 upma_bin_tests[i].arg1);
4254 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4255 upma_bin_tests[i].arg2);
4256 res = isl_union_pw_multi_aff_read_from_str(ctx,
4257 upma_bin_tests[i].res);
4258 upma1 = upma_bin_tests[i].fn(upma1, upma2);
4259 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
4260 isl_union_pw_multi_aff_free(upma1);
4261 isl_union_pw_multi_aff_free(res);
4262 if (ok < 0)
4263 return -1;
4264 if (!ok)
4265 isl_die(ctx, isl_error_unknown,
4266 "unexpected result", return -1);
4269 return 0;
4272 struct {
4273 __isl_give isl_union_pw_multi_aff *(*fn)(
4274 __isl_take isl_union_pw_multi_aff *upma1,
4275 __isl_take isl_union_pw_multi_aff *upma2);
4276 const char *arg1;
4277 const char *arg2;
4278 } upma_bin_fail_tests[] = {
4279 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4280 "{ B[x] -> C[2] : x >= 0 }" },
4283 /* Perform some basic tests of binary operations on
4284 * isl_union_pw_multi_aff objects that are expected to fail.
4286 static int test_bin_upma_fail(isl_ctx *ctx)
4288 int i, n;
4289 isl_union_pw_multi_aff *upma1, *upma2;
4290 int on_error;
4292 on_error = isl_options_get_on_error(ctx);
4293 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
4294 n = ARRAY_SIZE(upma_bin_fail_tests);
4295 for (i = 0; i < n; ++i) {
4296 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4297 upma_bin_fail_tests[i].arg1);
4298 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4299 upma_bin_fail_tests[i].arg2);
4300 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
4301 isl_union_pw_multi_aff_free(upma1);
4302 if (upma1)
4303 break;
4305 isl_options_set_on_error(ctx, on_error);
4306 if (i < n)
4307 isl_die(ctx, isl_error_unknown,
4308 "operation not expected to succeed", return -1);
4310 return 0;
4313 int test_aff(isl_ctx *ctx)
4315 const char *str;
4316 isl_set *set;
4317 isl_space *space;
4318 isl_local_space *ls;
4319 isl_aff *aff;
4320 int zero, equal;
4322 if (test_bin_aff(ctx) < 0)
4323 return -1;
4324 if (test_bin_upma(ctx) < 0)
4325 return -1;
4326 if (test_bin_upma_fail(ctx) < 0)
4327 return -1;
4329 space = isl_space_set_alloc(ctx, 0, 1);
4330 ls = isl_local_space_from_space(space);
4331 aff = isl_aff_zero_on_domain(ls);
4333 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4334 aff = isl_aff_scale_down_ui(aff, 3);
4335 aff = isl_aff_floor(aff);
4336 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4337 aff = isl_aff_scale_down_ui(aff, 2);
4338 aff = isl_aff_floor(aff);
4339 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4341 str = "{ [10] }";
4342 set = isl_set_read_from_str(ctx, str);
4343 aff = isl_aff_gist(aff, set);
4345 aff = isl_aff_add_constant_si(aff, -16);
4346 zero = isl_aff_plain_is_zero(aff);
4347 isl_aff_free(aff);
4349 if (zero < 0)
4350 return -1;
4351 if (!zero)
4352 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4354 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
4355 aff = isl_aff_scale_down_ui(aff, 64);
4356 aff = isl_aff_floor(aff);
4357 equal = aff_check_plain_equal(aff, "{ [-1] }");
4358 isl_aff_free(aff);
4359 if (equal < 0)
4360 return -1;
4362 return 0;
4365 int test_dim_max(isl_ctx *ctx)
4367 int equal;
4368 const char *str;
4369 isl_set *set1, *set2;
4370 isl_set *set;
4371 isl_map *map;
4372 isl_pw_aff *pwaff;
4374 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
4375 set = isl_set_read_from_str(ctx, str);
4376 pwaff = isl_set_dim_max(set, 0);
4377 set1 = isl_set_from_pw_aff(pwaff);
4378 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
4379 set2 = isl_set_read_from_str(ctx, str);
4380 equal = isl_set_is_equal(set1, set2);
4381 isl_set_free(set1);
4382 isl_set_free(set2);
4383 if (equal < 0)
4384 return -1;
4385 if (!equal)
4386 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4388 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
4389 set = isl_set_read_from_str(ctx, str);
4390 pwaff = isl_set_dim_max(set, 0);
4391 set1 = isl_set_from_pw_aff(pwaff);
4392 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4393 set2 = isl_set_read_from_str(ctx, str);
4394 equal = isl_set_is_equal(set1, set2);
4395 isl_set_free(set1);
4396 isl_set_free(set2);
4397 if (equal < 0)
4398 return -1;
4399 if (!equal)
4400 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4402 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
4403 set = isl_set_read_from_str(ctx, str);
4404 pwaff = isl_set_dim_max(set, 0);
4405 set1 = isl_set_from_pw_aff(pwaff);
4406 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4407 set2 = isl_set_read_from_str(ctx, str);
4408 equal = isl_set_is_equal(set1, set2);
4409 isl_set_free(set1);
4410 isl_set_free(set2);
4411 if (equal < 0)
4412 return -1;
4413 if (!equal)
4414 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4416 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
4417 "0 <= i < N and 0 <= j < M }";
4418 map = isl_map_read_from_str(ctx, str);
4419 set = isl_map_range(map);
4421 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
4422 set1 = isl_set_from_pw_aff(pwaff);
4423 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
4424 set2 = isl_set_read_from_str(ctx, str);
4425 equal = isl_set_is_equal(set1, set2);
4426 isl_set_free(set1);
4427 isl_set_free(set2);
4429 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
4430 set1 = isl_set_from_pw_aff(pwaff);
4431 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
4432 set2 = isl_set_read_from_str(ctx, str);
4433 if (equal >= 0 && equal)
4434 equal = isl_set_is_equal(set1, set2);
4435 isl_set_free(set1);
4436 isl_set_free(set2);
4438 isl_set_free(set);
4440 if (equal < 0)
4441 return -1;
4442 if (!equal)
4443 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4445 /* Check that solutions are properly merged. */
4446 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
4447 "c <= -1 + n - 4a - 2b and c >= -2b and "
4448 "4a >= -4 + n and c >= 0 }";
4449 set = isl_set_read_from_str(ctx, str);
4450 pwaff = isl_set_dim_min(set, 2);
4451 set1 = isl_set_from_pw_aff(pwaff);
4452 str = "[n] -> { [(0)] : n >= 1 }";
4453 set2 = isl_set_read_from_str(ctx, str);
4454 equal = isl_set_is_equal(set1, set2);
4455 isl_set_free(set1);
4456 isl_set_free(set2);
4458 if (equal < 0)
4459 return -1;
4460 if (!equal)
4461 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4463 /* Check that empty solution lie in the right space. */
4464 str = "[n] -> { [t,a] : 1 = 0 }";
4465 set = isl_set_read_from_str(ctx, str);
4466 pwaff = isl_set_dim_max(set, 0);
4467 set1 = isl_set_from_pw_aff(pwaff);
4468 str = "[n] -> { [t] : 1 = 0 }";
4469 set2 = isl_set_read_from_str(ctx, str);
4470 equal = isl_set_is_equal(set1, set2);
4471 isl_set_free(set1);
4472 isl_set_free(set2);
4474 if (equal < 0)
4475 return -1;
4476 if (!equal)
4477 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4479 return 0;
4482 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
4484 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
4485 const char *str)
4487 isl_ctx *ctx;
4488 isl_pw_multi_aff *pma2;
4489 int equal;
4491 if (!pma)
4492 return -1;
4494 ctx = isl_pw_multi_aff_get_ctx(pma);
4495 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4496 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
4497 isl_pw_multi_aff_free(pma2);
4499 return equal;
4502 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
4503 * represented by "str".
4505 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
4506 const char *str)
4508 int equal;
4510 equal = pw_multi_aff_plain_is_equal(pma, str);
4511 if (equal < 0)
4512 return -1;
4513 if (!equal)
4514 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
4515 "result not as expected", return -1);
4516 return 0;
4519 /* Basic test for isl_pw_multi_aff_product.
4521 * Check that multiple pieces are properly handled.
4523 static int test_product_pma(isl_ctx *ctx)
4525 int equal;
4526 const char *str;
4527 isl_pw_multi_aff *pma1, *pma2;
4529 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
4530 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4531 str = "{ C[] -> D[] }";
4532 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4533 pma1 = isl_pw_multi_aff_product(pma1, pma2);
4534 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
4535 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
4536 equal = pw_multi_aff_check_plain_equal(pma1, str);
4537 isl_pw_multi_aff_free(pma1);
4538 if (equal < 0)
4539 return -1;
4541 return 0;
4544 int test_product(isl_ctx *ctx)
4546 const char *str;
4547 isl_set *set;
4548 isl_union_set *uset1, *uset2;
4549 int ok;
4551 str = "{ A[i] }";
4552 set = isl_set_read_from_str(ctx, str);
4553 set = isl_set_product(set, isl_set_copy(set));
4554 ok = isl_set_is_wrapping(set);
4555 isl_set_free(set);
4556 if (ok < 0)
4557 return -1;
4558 if (!ok)
4559 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4561 str = "{ [] }";
4562 uset1 = isl_union_set_read_from_str(ctx, str);
4563 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
4564 str = "{ [[] -> []] }";
4565 uset2 = isl_union_set_read_from_str(ctx, str);
4566 ok = isl_union_set_is_equal(uset1, uset2);
4567 isl_union_set_free(uset1);
4568 isl_union_set_free(uset2);
4569 if (ok < 0)
4570 return -1;
4571 if (!ok)
4572 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4574 if (test_product_pma(ctx) < 0)
4575 return -1;
4577 return 0;
4580 /* Check that two sets are not considered disjoint just because
4581 * they have a different set of (named) parameters.
4583 static int test_disjoint(isl_ctx *ctx)
4585 const char *str;
4586 isl_set *set, *set2;
4587 int disjoint;
4589 str = "[n] -> { [[]->[]] }";
4590 set = isl_set_read_from_str(ctx, str);
4591 str = "{ [[]->[]] }";
4592 set2 = isl_set_read_from_str(ctx, str);
4593 disjoint = isl_set_is_disjoint(set, set2);
4594 isl_set_free(set);
4595 isl_set_free(set2);
4596 if (disjoint < 0)
4597 return -1;
4598 if (disjoint)
4599 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4601 return 0;
4604 int test_equal(isl_ctx *ctx)
4606 const char *str;
4607 isl_set *set, *set2;
4608 int equal;
4610 str = "{ S_6[i] }";
4611 set = isl_set_read_from_str(ctx, str);
4612 str = "{ S_7[i] }";
4613 set2 = isl_set_read_from_str(ctx, str);
4614 equal = isl_set_is_equal(set, set2);
4615 isl_set_free(set);
4616 isl_set_free(set2);
4617 if (equal < 0)
4618 return -1;
4619 if (equal)
4620 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4622 return 0;
4625 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
4626 enum isl_dim_type type, unsigned pos, int fixed)
4628 int test;
4630 test = isl_map_plain_is_fixed(map, type, pos, NULL);
4631 isl_map_free(map);
4632 if (test < 0)
4633 return -1;
4634 if (test == fixed)
4635 return 0;
4636 if (fixed)
4637 isl_die(ctx, isl_error_unknown,
4638 "map not detected as fixed", return -1);
4639 else
4640 isl_die(ctx, isl_error_unknown,
4641 "map detected as fixed", return -1);
4644 int test_fixed(isl_ctx *ctx)
4646 const char *str;
4647 isl_map *map;
4649 str = "{ [i] -> [i] }";
4650 map = isl_map_read_from_str(ctx, str);
4651 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
4652 return -1;
4653 str = "{ [i] -> [1] }";
4654 map = isl_map_read_from_str(ctx, str);
4655 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4656 return -1;
4657 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
4658 map = isl_map_read_from_str(ctx, str);
4659 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4660 return -1;
4661 map = isl_map_read_from_str(ctx, str);
4662 map = isl_map_neg(map);
4663 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4664 return -1;
4666 return 0;
4669 struct isl_vertices_test_data {
4670 const char *set;
4671 int n;
4672 const char *vertex[2];
4673 } vertices_tests[] = {
4674 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
4675 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
4676 { "{ A[t, i] : t = 14 and i = 1 }",
4677 1, { "{ A[14, 1] }" } },
4680 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
4682 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
4684 struct isl_vertices_test_data *data = user;
4685 isl_ctx *ctx;
4686 isl_multi_aff *ma;
4687 isl_basic_set *bset;
4688 isl_pw_multi_aff *pma;
4689 int i;
4690 isl_bool equal;
4692 ctx = isl_vertex_get_ctx(vertex);
4693 bset = isl_vertex_get_domain(vertex);
4694 ma = isl_vertex_get_expr(vertex);
4695 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
4697 for (i = 0; i < data->n; ++i) {
4698 isl_pw_multi_aff *pma_i;
4700 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
4701 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
4702 isl_pw_multi_aff_free(pma_i);
4704 if (equal < 0 || equal)
4705 break;
4708 isl_pw_multi_aff_free(pma);
4709 isl_vertex_free(vertex);
4711 if (equal < 0)
4712 return isl_stat_error;
4714 return equal ? isl_stat_ok : isl_stat_error;
4717 int test_vertices(isl_ctx *ctx)
4719 int i;
4721 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
4722 isl_basic_set *bset;
4723 isl_vertices *vertices;
4724 int ok = 1;
4725 int n;
4727 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
4728 vertices = isl_basic_set_compute_vertices(bset);
4729 n = isl_vertices_get_n_vertices(vertices);
4730 if (vertices_tests[i].n != n)
4731 ok = 0;
4732 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
4733 &vertices_tests[i]) < 0)
4734 ok = 0;
4735 isl_vertices_free(vertices);
4736 isl_basic_set_free(bset);
4738 if (!vertices)
4739 return -1;
4740 if (!ok)
4741 isl_die(ctx, isl_error_unknown, "unexpected vertices",
4742 return -1);
4745 return 0;
4748 int test_union_pw(isl_ctx *ctx)
4750 int equal;
4751 const char *str;
4752 isl_union_set *uset;
4753 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
4755 str = "{ [x] -> x^2 }";
4756 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
4757 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
4758 uset = isl_union_pw_qpolynomial_domain(upwqp1);
4759 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
4760 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
4761 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
4762 isl_union_pw_qpolynomial_free(upwqp1);
4763 isl_union_pw_qpolynomial_free(upwqp2);
4764 if (equal < 0)
4765 return -1;
4766 if (!equal)
4767 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4769 return 0;
4772 /* Test that isl_union_pw_qpolynomial_eval picks up the function
4773 * defined over the correct domain space.
4775 static int test_eval(isl_ctx *ctx)
4777 const char *str;
4778 isl_point *pnt;
4779 isl_set *set;
4780 isl_union_pw_qpolynomial *upwqp;
4781 isl_val *v;
4782 int cmp;
4784 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
4785 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
4786 str = "{ A[6] }";
4787 set = isl_set_read_from_str(ctx, str);
4788 pnt = isl_set_sample_point(set);
4789 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
4790 cmp = isl_val_cmp_si(v, 36);
4791 isl_val_free(v);
4793 if (!v)
4794 return -1;
4795 if (cmp != 0)
4796 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
4798 return 0;
4801 int test_output(isl_ctx *ctx)
4803 char *s;
4804 const char *str;
4805 isl_pw_aff *pa;
4806 isl_printer *p;
4807 int equal;
4809 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
4810 pa = isl_pw_aff_read_from_str(ctx, str);
4812 p = isl_printer_to_str(ctx);
4813 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
4814 p = isl_printer_print_pw_aff(p, pa);
4815 s = isl_printer_get_str(p);
4816 isl_printer_free(p);
4817 isl_pw_aff_free(pa);
4818 if (!s)
4819 equal = -1;
4820 else
4821 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
4822 free(s);
4823 if (equal < 0)
4824 return -1;
4825 if (!equal)
4826 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4828 return 0;
4831 int test_sample(isl_ctx *ctx)
4833 const char *str;
4834 isl_basic_set *bset1, *bset2;
4835 int empty, subset;
4837 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
4838 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
4839 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
4840 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
4841 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
4842 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
4843 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
4844 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
4845 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
4846 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
4847 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
4848 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
4849 "d - 1073741821e and "
4850 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
4851 "3j >= 1 - a + b + 2e and "
4852 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
4853 "3i <= 4 - a + 4b - e and "
4854 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
4855 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
4856 "c <= -1 + a and 3i >= -2 - a + 3e and "
4857 "1073741822e <= 1073741823 - a + 1073741822b + c and "
4858 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
4859 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
4860 "1073741823e >= 1 + 1073741823b - d and "
4861 "3i >= 1073741823b + c - 1073741823e - f and "
4862 "3i >= 1 + 2b + e + 3g }";
4863 bset1 = isl_basic_set_read_from_str(ctx, str);
4864 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
4865 empty = isl_basic_set_is_empty(bset2);
4866 subset = isl_basic_set_is_subset(bset2, bset1);
4867 isl_basic_set_free(bset1);
4868 isl_basic_set_free(bset2);
4869 if (empty < 0 || subset < 0)
4870 return -1;
4871 if (empty)
4872 isl_die(ctx, isl_error_unknown, "point not found", return -1);
4873 if (!subset)
4874 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
4876 return 0;
4879 int test_fixed_power(isl_ctx *ctx)
4881 const char *str;
4882 isl_map *map;
4883 isl_int exp;
4884 int equal;
4886 isl_int_init(exp);
4887 str = "{ [i] -> [i + 1] }";
4888 map = isl_map_read_from_str(ctx, str);
4889 isl_int_set_si(exp, 23);
4890 map = isl_map_fixed_power(map, exp);
4891 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
4892 isl_int_clear(exp);
4893 isl_map_free(map);
4894 if (equal < 0)
4895 return -1;
4897 return 0;
4900 int test_slice(isl_ctx *ctx)
4902 const char *str;
4903 isl_map *map;
4904 int equal;
4906 str = "{ [i] -> [j] }";
4907 map = isl_map_read_from_str(ctx, str);
4908 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
4909 equal = map_check_equal(map, "{ [i] -> [i] }");
4910 isl_map_free(map);
4911 if (equal < 0)
4912 return -1;
4914 str = "{ [i] -> [j] }";
4915 map = isl_map_read_from_str(ctx, str);
4916 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
4917 equal = map_check_equal(map, "{ [i] -> [j] }");
4918 isl_map_free(map);
4919 if (equal < 0)
4920 return -1;
4922 str = "{ [i] -> [j] }";
4923 map = isl_map_read_from_str(ctx, str);
4924 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
4925 equal = map_check_equal(map, "{ [i] -> [-i] }");
4926 isl_map_free(map);
4927 if (equal < 0)
4928 return -1;
4930 str = "{ [i] -> [j] }";
4931 map = isl_map_read_from_str(ctx, str);
4932 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
4933 equal = map_check_equal(map, "{ [0] -> [j] }");
4934 isl_map_free(map);
4935 if (equal < 0)
4936 return -1;
4938 str = "{ [i] -> [j] }";
4939 map = isl_map_read_from_str(ctx, str);
4940 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
4941 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
4942 isl_map_free(map);
4943 if (equal < 0)
4944 return -1;
4946 str = "{ [i] -> [j] }";
4947 map = isl_map_read_from_str(ctx, str);
4948 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
4949 equal = map_check_equal(map, "{ [i] -> [j] : false }");
4950 isl_map_free(map);
4951 if (equal < 0)
4952 return -1;
4954 return 0;
4957 int test_eliminate(isl_ctx *ctx)
4959 const char *str;
4960 isl_map *map;
4961 int equal;
4963 str = "{ [i] -> [j] : i = 2j }";
4964 map = isl_map_read_from_str(ctx, str);
4965 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
4966 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
4967 isl_map_free(map);
4968 if (equal < 0)
4969 return -1;
4971 return 0;
4974 /* Check that isl_set_dim_residue_class detects that the values of j
4975 * in the set below are all odd and that it does not detect any spurious
4976 * strides.
4978 static int test_residue_class(isl_ctx *ctx)
4980 const char *str;
4981 isl_set *set;
4982 isl_int m, r;
4983 int res;
4985 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
4986 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
4987 set = isl_set_read_from_str(ctx, str);
4988 isl_int_init(m);
4989 isl_int_init(r);
4990 res = isl_set_dim_residue_class(set, 1, &m, &r);
4991 if (res >= 0 &&
4992 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
4993 isl_die(ctx, isl_error_unknown, "incorrect residue class",
4994 res = -1);
4995 isl_int_clear(r);
4996 isl_int_clear(m);
4997 isl_set_free(set);
4999 return res;
5002 int test_align_parameters(isl_ctx *ctx)
5004 const char *str;
5005 isl_space *space;
5006 isl_multi_aff *ma1, *ma2;
5007 int equal;
5009 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
5010 ma1 = isl_multi_aff_read_from_str(ctx, str);
5012 space = isl_space_params_alloc(ctx, 1);
5013 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
5014 ma1 = isl_multi_aff_align_params(ma1, space);
5016 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
5017 ma2 = isl_multi_aff_read_from_str(ctx, str);
5019 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5021 isl_multi_aff_free(ma1);
5022 isl_multi_aff_free(ma2);
5024 if (equal < 0)
5025 return -1;
5026 if (!equal)
5027 isl_die(ctx, isl_error_unknown,
5028 "result not as expected", return -1);
5030 return 0;
5033 static int test_list(isl_ctx *ctx)
5035 isl_id *a, *b, *c, *d, *id;
5036 isl_id_list *list;
5037 int ok;
5039 a = isl_id_alloc(ctx, "a", NULL);
5040 b = isl_id_alloc(ctx, "b", NULL);
5041 c = isl_id_alloc(ctx, "c", NULL);
5042 d = isl_id_alloc(ctx, "d", NULL);
5044 list = isl_id_list_alloc(ctx, 4);
5045 list = isl_id_list_add(list, a);
5046 list = isl_id_list_add(list, b);
5047 list = isl_id_list_add(list, c);
5048 list = isl_id_list_add(list, d);
5049 list = isl_id_list_drop(list, 1, 1);
5051 if (isl_id_list_n_id(list) != 3) {
5052 isl_id_list_free(list);
5053 isl_die(ctx, isl_error_unknown,
5054 "unexpected number of elements in list", return -1);
5057 id = isl_id_list_get_id(list, 0);
5058 ok = id == a;
5059 isl_id_free(id);
5060 id = isl_id_list_get_id(list, 1);
5061 ok = ok && id == c;
5062 isl_id_free(id);
5063 id = isl_id_list_get_id(list, 2);
5064 ok = ok && id == d;
5065 isl_id_free(id);
5067 isl_id_list_free(list);
5069 if (!ok)
5070 isl_die(ctx, isl_error_unknown,
5071 "unexpected elements in list", return -1);
5073 return 0;
5076 const char *set_conversion_tests[] = {
5077 "[N] -> { [i] : N - 1 <= 2 i <= N }",
5078 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
5079 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5080 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5081 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
5082 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
5083 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
5084 "-3 + c <= d <= 28 + c) }",
5087 /* Check that converting from isl_set to isl_pw_multi_aff and back
5088 * to isl_set produces the original isl_set.
5090 static int test_set_conversion(isl_ctx *ctx)
5092 int i;
5093 const char *str;
5094 isl_set *set1, *set2;
5095 isl_pw_multi_aff *pma;
5096 int equal;
5098 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
5099 str = set_conversion_tests[i];
5100 set1 = isl_set_read_from_str(ctx, str);
5101 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
5102 set2 = isl_set_from_pw_multi_aff(pma);
5103 equal = isl_set_is_equal(set1, set2);
5104 isl_set_free(set1);
5105 isl_set_free(set2);
5107 if (equal < 0)
5108 return -1;
5109 if (!equal)
5110 isl_die(ctx, isl_error_unknown, "bad conversion",
5111 return -1);
5114 return 0;
5117 const char *conversion_tests[] = {
5118 "{ [a, b, c, d] -> s0[a, b, e, f] : "
5119 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
5120 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
5121 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
5122 "9e <= -2 - 2a) }",
5123 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
5124 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
5125 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
5128 /* Check that converting from isl_map to isl_pw_multi_aff and back
5129 * to isl_map produces the original isl_map.
5131 static int test_map_conversion(isl_ctx *ctx)
5133 int i;
5134 isl_map *map1, *map2;
5135 isl_pw_multi_aff *pma;
5136 int equal;
5138 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
5139 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
5140 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
5141 map2 = isl_map_from_pw_multi_aff(pma);
5142 equal = isl_map_is_equal(map1, map2);
5143 isl_map_free(map1);
5144 isl_map_free(map2);
5146 if (equal < 0)
5147 return -1;
5148 if (!equal)
5149 isl_die(ctx, isl_error_unknown, "bad conversion",
5150 return -1);
5153 return 0;
5156 static int test_conversion(isl_ctx *ctx)
5158 if (test_set_conversion(ctx) < 0)
5159 return -1;
5160 if (test_map_conversion(ctx) < 0)
5161 return -1;
5162 return 0;
5165 /* Check that isl_basic_map_curry does not modify input.
5167 static int test_curry(isl_ctx *ctx)
5169 const char *str;
5170 isl_basic_map *bmap1, *bmap2;
5171 int equal;
5173 str = "{ [A[] -> B[]] -> C[] }";
5174 bmap1 = isl_basic_map_read_from_str(ctx, str);
5175 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
5176 equal = isl_basic_map_is_equal(bmap1, bmap2);
5177 isl_basic_map_free(bmap1);
5178 isl_basic_map_free(bmap2);
5180 if (equal < 0)
5181 return -1;
5182 if (equal)
5183 isl_die(ctx, isl_error_unknown,
5184 "curried map should not be equal to original",
5185 return -1);
5187 return 0;
5190 struct {
5191 const char *set;
5192 const char *ma;
5193 const char *res;
5194 } preimage_tests[] = {
5195 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
5196 "{ A[j,i] -> B[i,j] }",
5197 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
5198 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5199 "{ A[a,b] -> B[a/2,b/6] }",
5200 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
5201 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5202 "{ A[a,b] -> B[a/2,b/6] }",
5203 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
5204 "exists i,j : a = 2 i and b = 6 j }" },
5205 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
5206 "[n] -> { : 0 <= n <= 100 }" },
5207 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5208 "{ A[a] -> B[2a] }",
5209 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
5210 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5211 "{ A[a] -> B[([a/2])] }",
5212 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
5213 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
5214 "{ A[a] -> B[a,a,a/3] }",
5215 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
5216 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
5217 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
5220 static int test_preimage_basic_set(isl_ctx *ctx)
5222 int i;
5223 isl_basic_set *bset1, *bset2;
5224 isl_multi_aff *ma;
5225 int equal;
5227 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
5228 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
5229 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
5230 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
5231 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
5232 equal = isl_basic_set_is_equal(bset1, bset2);
5233 isl_basic_set_free(bset1);
5234 isl_basic_set_free(bset2);
5235 if (equal < 0)
5236 return -1;
5237 if (!equal)
5238 isl_die(ctx, isl_error_unknown, "bad preimage",
5239 return -1);
5242 return 0;
5245 struct {
5246 const char *map;
5247 const char *ma;
5248 const char *res;
5249 } preimage_domain_tests[] = {
5250 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
5251 "{ A[j,i] -> B[i,j] }",
5252 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
5253 { "{ B[i] -> C[i]; D[i] -> E[i] }",
5254 "{ A[i] -> B[i + 1] }",
5255 "{ A[i] -> C[i + 1] }" },
5256 { "{ B[i] -> C[i]; B[i] -> E[i] }",
5257 "{ A[i] -> B[i + 1] }",
5258 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
5259 { "{ B[i] -> C[([i/2])] }",
5260 "{ A[i] -> B[2i] }",
5261 "{ A[i] -> C[i] }" },
5262 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
5263 "{ A[i] -> B[([i/5]), ([i/7])] }",
5264 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
5265 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
5266 "[N] -> { A[] -> B[([N/5])] }",
5267 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
5268 { "{ B[i] -> C[i] : exists a : i = 5 a }",
5269 "{ A[i] -> B[2i] }",
5270 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
5271 { "{ B[i] -> C[i] : exists a : i = 2 a; "
5272 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
5273 "{ A[i] -> B[2i] }",
5274 "{ A[i] -> C[2i] }" },
5275 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
5276 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
5279 static int test_preimage_union_map(isl_ctx *ctx)
5281 int i;
5282 isl_union_map *umap1, *umap2;
5283 isl_multi_aff *ma;
5284 int equal;
5286 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
5287 umap1 = isl_union_map_read_from_str(ctx,
5288 preimage_domain_tests[i].map);
5289 ma = isl_multi_aff_read_from_str(ctx,
5290 preimage_domain_tests[i].ma);
5291 umap2 = isl_union_map_read_from_str(ctx,
5292 preimage_domain_tests[i].res);
5293 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
5294 equal = isl_union_map_is_equal(umap1, umap2);
5295 isl_union_map_free(umap1);
5296 isl_union_map_free(umap2);
5297 if (equal < 0)
5298 return -1;
5299 if (!equal)
5300 isl_die(ctx, isl_error_unknown, "bad preimage",
5301 return -1);
5304 return 0;
5307 static int test_preimage(isl_ctx *ctx)
5309 if (test_preimage_basic_set(ctx) < 0)
5310 return -1;
5311 if (test_preimage_union_map(ctx) < 0)
5312 return -1;
5314 return 0;
5317 struct {
5318 const char *ma1;
5319 const char *ma;
5320 const char *res;
5321 } pullback_tests[] = {
5322 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
5323 "{ A[a,b] -> C[b + 2a] }" },
5324 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
5325 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
5326 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
5327 "{ A[a] -> C[(a)/6] }" },
5328 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
5329 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
5330 "{ A[a] -> C[(2a)/3] }" },
5331 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
5332 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
5333 "{ A[i,j] -> C[i + j, i + j] }"},
5334 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
5335 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
5336 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
5337 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
5338 "{ [i, j] -> [floor((i)/3), j] }",
5339 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
5342 static int test_pullback(isl_ctx *ctx)
5344 int i;
5345 isl_multi_aff *ma1, *ma2;
5346 isl_multi_aff *ma;
5347 int equal;
5349 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
5350 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
5351 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
5352 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
5353 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
5354 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5355 isl_multi_aff_free(ma1);
5356 isl_multi_aff_free(ma2);
5357 if (equal < 0)
5358 return -1;
5359 if (!equal)
5360 isl_die(ctx, isl_error_unknown, "bad pullback",
5361 return -1);
5364 return 0;
5367 /* Check that negation is printed correctly and that equal expressions
5368 * are correctly identified.
5370 static int test_ast(isl_ctx *ctx)
5372 isl_ast_expr *expr, *expr1, *expr2, *expr3;
5373 char *str;
5374 int ok, equal;
5376 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5377 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5378 expr = isl_ast_expr_add(expr1, expr2);
5379 expr2 = isl_ast_expr_copy(expr);
5380 expr = isl_ast_expr_neg(expr);
5381 expr2 = isl_ast_expr_neg(expr2);
5382 equal = isl_ast_expr_is_equal(expr, expr2);
5383 str = isl_ast_expr_to_str(expr);
5384 ok = str ? !strcmp(str, "-(A + B)") : -1;
5385 free(str);
5386 isl_ast_expr_free(expr);
5387 isl_ast_expr_free(expr2);
5389 if (ok < 0 || equal < 0)
5390 return -1;
5391 if (!equal)
5392 isl_die(ctx, isl_error_unknown,
5393 "equal expressions not considered equal", return -1);
5394 if (!ok)
5395 isl_die(ctx, isl_error_unknown,
5396 "isl_ast_expr printed incorrectly", return -1);
5398 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
5399 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
5400 expr = isl_ast_expr_add(expr1, expr2);
5401 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
5402 expr = isl_ast_expr_sub(expr3, expr);
5403 str = isl_ast_expr_to_str(expr);
5404 ok = str ? !strcmp(str, "C - (A + B)") : -1;
5405 free(str);
5406 isl_ast_expr_free(expr);
5408 if (ok < 0)
5409 return -1;
5410 if (!ok)
5411 isl_die(ctx, isl_error_unknown,
5412 "isl_ast_expr printed incorrectly", return -1);
5414 return 0;
5417 /* Check that isl_ast_build_expr_from_set returns a valid expression
5418 * for an empty set. Note that isl_ast_build_expr_from_set getting
5419 * called on an empty set probably indicates a bug in the caller.
5421 static int test_ast_build(isl_ctx *ctx)
5423 isl_set *set;
5424 isl_ast_build *build;
5425 isl_ast_expr *expr;
5427 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5428 build = isl_ast_build_from_context(set);
5430 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
5431 expr = isl_ast_build_expr_from_set(build, set);
5433 isl_ast_expr_free(expr);
5434 isl_ast_build_free(build);
5436 if (!expr)
5437 return -1;
5439 return 0;
5442 /* Internal data structure for before_for and after_for callbacks.
5444 * depth is the current depth
5445 * before is the number of times before_for has been called
5446 * after is the number of times after_for has been called
5448 struct isl_test_codegen_data {
5449 int depth;
5450 int before;
5451 int after;
5454 /* This function is called before each for loop in the AST generated
5455 * from test_ast_gen1.
5457 * Increment the number of calls and the depth.
5458 * Check that the space returned by isl_ast_build_get_schedule_space
5459 * matches the target space of the schedule returned by
5460 * isl_ast_build_get_schedule.
5461 * Return an isl_id that is checked by the corresponding call
5462 * to after_for.
5464 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
5465 void *user)
5467 struct isl_test_codegen_data *data = user;
5468 isl_ctx *ctx;
5469 isl_space *space;
5470 isl_union_map *schedule;
5471 isl_union_set *uset;
5472 isl_set *set;
5473 int empty;
5474 char name[] = "d0";
5476 ctx = isl_ast_build_get_ctx(build);
5478 if (data->before >= 3)
5479 isl_die(ctx, isl_error_unknown,
5480 "unexpected number of for nodes", return NULL);
5481 if (data->depth >= 2)
5482 isl_die(ctx, isl_error_unknown,
5483 "unexpected depth", return NULL);
5485 snprintf(name, sizeof(name), "d%d", data->depth);
5486 data->before++;
5487 data->depth++;
5489 schedule = isl_ast_build_get_schedule(build);
5490 uset = isl_union_map_range(schedule);
5491 if (!uset)
5492 return NULL;
5493 if (isl_union_set_n_set(uset) != 1) {
5494 isl_union_set_free(uset);
5495 isl_die(ctx, isl_error_unknown,
5496 "expecting single range space", return NULL);
5499 space = isl_ast_build_get_schedule_space(build);
5500 set = isl_union_set_extract_set(uset, space);
5501 isl_union_set_free(uset);
5502 empty = isl_set_is_empty(set);
5503 isl_set_free(set);
5505 if (empty < 0)
5506 return NULL;
5507 if (empty)
5508 isl_die(ctx, isl_error_unknown,
5509 "spaces don't match", return NULL);
5511 return isl_id_alloc(ctx, name, NULL);
5514 /* This function is called after each for loop in the AST generated
5515 * from test_ast_gen1.
5517 * Increment the number of calls and decrement the depth.
5518 * Check that the annotation attached to the node matches
5519 * the isl_id returned by the corresponding call to before_for.
5521 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
5522 __isl_keep isl_ast_build *build, void *user)
5524 struct isl_test_codegen_data *data = user;
5525 isl_id *id;
5526 const char *name;
5527 int valid;
5529 data->after++;
5530 data->depth--;
5532 if (data->after > data->before)
5533 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5534 "mismatch in number of for nodes",
5535 return isl_ast_node_free(node));
5537 id = isl_ast_node_get_annotation(node);
5538 if (!id)
5539 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5540 "missing annotation", return isl_ast_node_free(node));
5542 name = isl_id_get_name(id);
5543 valid = name && atoi(name + 1) == data->depth;
5544 isl_id_free(id);
5546 if (!valid)
5547 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
5548 "wrong annotation", return isl_ast_node_free(node));
5550 return node;
5553 /* Check that the before_each_for and after_each_for callbacks
5554 * are called for each for loop in the generated code,
5555 * that they are called in the right order and that the isl_id
5556 * returned from the before_each_for callback is attached to
5557 * the isl_ast_node passed to the corresponding after_each_for call.
5559 static int test_ast_gen1(isl_ctx *ctx)
5561 const char *str;
5562 isl_set *set;
5563 isl_union_map *schedule;
5564 isl_ast_build *build;
5565 isl_ast_node *tree;
5566 struct isl_test_codegen_data data;
5568 str = "[N] -> { : N >= 10 }";
5569 set = isl_set_read_from_str(ctx, str);
5570 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
5571 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
5572 schedule = isl_union_map_read_from_str(ctx, str);
5574 data.before = 0;
5575 data.after = 0;
5576 data.depth = 0;
5577 build = isl_ast_build_from_context(set);
5578 build = isl_ast_build_set_before_each_for(build,
5579 &before_for, &data);
5580 build = isl_ast_build_set_after_each_for(build,
5581 &after_for, &data);
5582 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5583 isl_ast_build_free(build);
5584 if (!tree)
5585 return -1;
5587 isl_ast_node_free(tree);
5589 if (data.before != 3 || data.after != 3)
5590 isl_die(ctx, isl_error_unknown,
5591 "unexpected number of for nodes", return -1);
5593 return 0;
5596 /* Check that the AST generator handles domains that are integrally disjoint
5597 * but not rationally disjoint.
5599 static int test_ast_gen2(isl_ctx *ctx)
5601 const char *str;
5602 isl_set *set;
5603 isl_union_map *schedule;
5604 isl_union_map *options;
5605 isl_ast_build *build;
5606 isl_ast_node *tree;
5608 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
5609 schedule = isl_union_map_read_from_str(ctx, str);
5610 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5611 build = isl_ast_build_from_context(set);
5613 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
5614 options = isl_union_map_read_from_str(ctx, str);
5615 build = isl_ast_build_set_options(build, options);
5616 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5617 isl_ast_build_free(build);
5618 if (!tree)
5619 return -1;
5620 isl_ast_node_free(tree);
5622 return 0;
5625 /* Increment *user on each call.
5627 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
5628 __isl_keep isl_ast_build *build, void *user)
5630 int *n = user;
5632 (*n)++;
5634 return node;
5637 /* Test that unrolling tries to minimize the number of instances.
5638 * In particular, for the schedule given below, make sure it generates
5639 * 3 nodes (rather than 101).
5641 static int test_ast_gen3(isl_ctx *ctx)
5643 const char *str;
5644 isl_set *set;
5645 isl_union_map *schedule;
5646 isl_union_map *options;
5647 isl_ast_build *build;
5648 isl_ast_node *tree;
5649 int n_domain = 0;
5651 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
5652 schedule = isl_union_map_read_from_str(ctx, str);
5653 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5655 str = "{ [i] -> unroll[0] }";
5656 options = isl_union_map_read_from_str(ctx, str);
5658 build = isl_ast_build_from_context(set);
5659 build = isl_ast_build_set_options(build, options);
5660 build = isl_ast_build_set_at_each_domain(build,
5661 &count_domains, &n_domain);
5662 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5663 isl_ast_build_free(build);
5664 if (!tree)
5665 return -1;
5667 isl_ast_node_free(tree);
5669 if (n_domain != 3)
5670 isl_die(ctx, isl_error_unknown,
5671 "unexpected number of for nodes", return -1);
5673 return 0;
5676 /* Check that if the ast_build_exploit_nested_bounds options is set,
5677 * we do not get an outer if node in the generated AST,
5678 * while we do get such an outer if node if the options is not set.
5680 static int test_ast_gen4(isl_ctx *ctx)
5682 const char *str;
5683 isl_set *set;
5684 isl_union_map *schedule;
5685 isl_ast_build *build;
5686 isl_ast_node *tree;
5687 enum isl_ast_node_type type;
5688 int enb;
5690 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
5691 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
5693 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
5695 schedule = isl_union_map_read_from_str(ctx, str);
5696 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5697 build = isl_ast_build_from_context(set);
5698 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5699 isl_ast_build_free(build);
5700 if (!tree)
5701 return -1;
5703 type = isl_ast_node_get_type(tree);
5704 isl_ast_node_free(tree);
5706 if (type == isl_ast_node_if)
5707 isl_die(ctx, isl_error_unknown,
5708 "not expecting if node", return -1);
5710 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
5712 schedule = isl_union_map_read_from_str(ctx, str);
5713 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5714 build = isl_ast_build_from_context(set);
5715 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5716 isl_ast_build_free(build);
5717 if (!tree)
5718 return -1;
5720 type = isl_ast_node_get_type(tree);
5721 isl_ast_node_free(tree);
5723 if (type != isl_ast_node_if)
5724 isl_die(ctx, isl_error_unknown,
5725 "expecting if node", return -1);
5727 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
5729 return 0;
5732 /* This function is called for each leaf in the AST generated
5733 * from test_ast_gen5.
5735 * We finalize the AST generation by extending the outer schedule
5736 * with a zero-dimensional schedule. If this results in any for loops,
5737 * then this means that we did not pass along enough information
5738 * about the outer schedule to the inner AST generation.
5740 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
5741 void *user)
5743 isl_union_map *schedule, *extra;
5744 isl_ast_node *tree;
5746 schedule = isl_ast_build_get_schedule(build);
5747 extra = isl_union_map_copy(schedule);
5748 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
5749 schedule = isl_union_map_range_product(schedule, extra);
5750 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5751 isl_ast_build_free(build);
5753 if (!tree)
5754 return NULL;
5756 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
5757 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
5758 "code should not contain any for loop",
5759 return isl_ast_node_free(tree));
5761 return tree;
5764 /* Check that we do not lose any information when going back and
5765 * forth between internal and external schedule.
5767 * In particular, we create an AST where we unroll the only
5768 * non-constant dimension in the schedule. We therefore do
5769 * not expect any for loops in the AST. However, older versions
5770 * of isl would not pass along enough information about the outer
5771 * schedule when performing an inner code generation from a create_leaf
5772 * callback, resulting in the inner code generation producing a for loop.
5774 static int test_ast_gen5(isl_ctx *ctx)
5776 const char *str;
5777 isl_set *set;
5778 isl_union_map *schedule, *options;
5779 isl_ast_build *build;
5780 isl_ast_node *tree;
5782 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
5783 schedule = isl_union_map_read_from_str(ctx, str);
5785 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
5786 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
5787 options = isl_union_map_read_from_str(ctx, str);
5789 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5790 build = isl_ast_build_from_context(set);
5791 build = isl_ast_build_set_options(build, options);
5792 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
5793 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5794 isl_ast_build_free(build);
5795 isl_ast_node_free(tree);
5796 if (!tree)
5797 return -1;
5799 return 0;
5802 static int test_ast_gen(isl_ctx *ctx)
5804 if (test_ast_gen1(ctx) < 0)
5805 return -1;
5806 if (test_ast_gen2(ctx) < 0)
5807 return -1;
5808 if (test_ast_gen3(ctx) < 0)
5809 return -1;
5810 if (test_ast_gen4(ctx) < 0)
5811 return -1;
5812 if (test_ast_gen5(ctx) < 0)
5813 return -1;
5814 return 0;
5817 /* Check if dropping output dimensions from an isl_pw_multi_aff
5818 * works properly.
5820 static int test_pw_multi_aff(isl_ctx *ctx)
5822 const char *str;
5823 isl_pw_multi_aff *pma1, *pma2;
5824 int equal;
5826 str = "{ [i,j] -> [i+j, 4i-j] }";
5827 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
5828 str = "{ [i,j] -> [4i-j] }";
5829 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5831 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
5833 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
5835 isl_pw_multi_aff_free(pma1);
5836 isl_pw_multi_aff_free(pma2);
5837 if (equal < 0)
5838 return -1;
5839 if (!equal)
5840 isl_die(ctx, isl_error_unknown,
5841 "expressions not equal", return -1);
5843 return 0;
5846 /* Check that we can properly parse multi piecewise affine expressions
5847 * where the piecewise affine expressions have different domains.
5849 static int test_multi_pw_aff(isl_ctx *ctx)
5851 const char *str;
5852 isl_set *dom, *dom2;
5853 isl_multi_pw_aff *mpa1, *mpa2;
5854 isl_pw_aff *pa;
5855 int equal;
5856 int equal_domain;
5858 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
5859 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
5860 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
5861 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
5862 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
5863 str = "{ [i] -> [(i : i > 0), 2i] }";
5864 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
5866 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
5868 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
5869 dom = isl_pw_aff_domain(pa);
5870 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
5871 dom2 = isl_pw_aff_domain(pa);
5872 equal_domain = isl_set_is_equal(dom, dom2);
5874 isl_set_free(dom);
5875 isl_set_free(dom2);
5876 isl_multi_pw_aff_free(mpa1);
5877 isl_multi_pw_aff_free(mpa2);
5879 if (equal < 0)
5880 return -1;
5881 if (!equal)
5882 isl_die(ctx, isl_error_unknown,
5883 "expressions not equal", return -1);
5885 if (equal_domain < 0)
5886 return -1;
5887 if (equal_domain)
5888 isl_die(ctx, isl_error_unknown,
5889 "domains unexpectedly equal", return -1);
5891 return 0;
5894 /* This is a regression test for a bug where isl_basic_map_simplify
5895 * would end up in an infinite loop. In particular, we construct
5896 * an empty basic set that is not obviously empty.
5897 * isl_basic_set_is_empty marks the basic set as empty.
5898 * After projecting out i3, the variable can be dropped completely,
5899 * but isl_basic_map_simplify refrains from doing so if the basic set
5900 * is empty and would end up in an infinite loop if it didn't test
5901 * explicitly for empty basic maps in the outer loop.
5903 static int test_simplify_1(isl_ctx *ctx)
5905 const char *str;
5906 isl_basic_set *bset;
5907 int empty;
5909 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
5910 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
5911 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
5912 "i3 >= i2 }";
5913 bset = isl_basic_set_read_from_str(ctx, str);
5914 empty = isl_basic_set_is_empty(bset);
5915 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
5916 isl_basic_set_free(bset);
5917 if (!bset)
5918 return -1;
5919 if (!empty)
5920 isl_die(ctx, isl_error_unknown,
5921 "basic set should be empty", return -1);
5923 return 0;
5926 /* Check that the equality in the set description below
5927 * is simplified away.
5929 static int test_simplify_2(isl_ctx *ctx)
5931 const char *str;
5932 isl_basic_set *bset;
5933 isl_bool universe;
5935 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
5936 bset = isl_basic_set_read_from_str(ctx, str);
5937 universe = isl_basic_set_plain_is_universe(bset);
5938 isl_basic_set_free(bset);
5940 if (universe < 0)
5941 return -1;
5942 if (!universe)
5943 isl_die(ctx, isl_error_unknown,
5944 "equality not simplified away", return -1);
5945 return 0;
5948 /* Some simplification tests.
5950 static int test_simplify(isl_ctx *ctx)
5952 if (test_simplify_1(ctx) < 0)
5953 return -1;
5954 if (test_simplify_2(ctx) < 0)
5955 return -1;
5956 return 0;
5959 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
5960 * with gbr context would fail to disable the use of the shifted tableau
5961 * when transferring equalities for the input to the context, resulting
5962 * in invalid sample values.
5964 static int test_partial_lexmin(isl_ctx *ctx)
5966 const char *str;
5967 isl_basic_set *bset;
5968 isl_basic_map *bmap;
5969 isl_map *map;
5971 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
5972 bmap = isl_basic_map_read_from_str(ctx, str);
5973 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
5974 bset = isl_basic_set_read_from_str(ctx, str);
5975 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
5976 isl_map_free(map);
5978 if (!map)
5979 return -1;
5981 return 0;
5984 /* Check that the variable compression performed on the existentially
5985 * quantified variables inside isl_basic_set_compute_divs is not confused
5986 * by the implicit equalities among the parameters.
5988 static int test_compute_divs(isl_ctx *ctx)
5990 const char *str;
5991 isl_basic_set *bset;
5992 isl_set *set;
5994 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
5995 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
5996 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
5997 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
5998 bset = isl_basic_set_read_from_str(ctx, str);
5999 set = isl_basic_set_compute_divs(bset);
6000 isl_set_free(set);
6001 if (!set)
6002 return -1;
6004 return 0;
6007 /* Check that the reaching domain elements and the prefix schedule
6008 * at a leaf node are the same before and after grouping.
6010 static int test_schedule_tree_group_1(isl_ctx *ctx)
6012 int equal;
6013 const char *str;
6014 isl_id *id;
6015 isl_union_set *uset;
6016 isl_multi_union_pw_aff *mupa;
6017 isl_union_pw_multi_aff *upma1, *upma2;
6018 isl_union_set *domain1, *domain2;
6019 isl_union_map *umap1, *umap2;
6020 isl_schedule_node *node;
6022 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
6023 uset = isl_union_set_read_from_str(ctx, str);
6024 node = isl_schedule_node_from_domain(uset);
6025 node = isl_schedule_node_child(node, 0);
6026 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
6027 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6028 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6029 node = isl_schedule_node_child(node, 0);
6030 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
6031 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6032 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6033 node = isl_schedule_node_child(node, 0);
6034 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
6035 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
6036 domain1 = isl_schedule_node_get_domain(node);
6037 id = isl_id_alloc(ctx, "group", NULL);
6038 node = isl_schedule_node_parent(node);
6039 node = isl_schedule_node_group(node, id);
6040 node = isl_schedule_node_child(node, 0);
6041 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
6042 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
6043 domain2 = isl_schedule_node_get_domain(node);
6044 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
6045 if (equal >= 0 && equal)
6046 equal = isl_union_set_is_equal(domain1, domain2);
6047 if (equal >= 0 && equal)
6048 equal = isl_union_map_is_equal(umap1, umap2);
6049 isl_union_map_free(umap1);
6050 isl_union_map_free(umap2);
6051 isl_union_set_free(domain1);
6052 isl_union_set_free(domain2);
6053 isl_union_pw_multi_aff_free(upma1);
6054 isl_union_pw_multi_aff_free(upma2);
6055 isl_schedule_node_free(node);
6057 if (equal < 0)
6058 return -1;
6059 if (!equal)
6060 isl_die(ctx, isl_error_unknown,
6061 "expressions not equal", return -1);
6063 return 0;
6066 /* Check that we can have nested groupings and that the union map
6067 * schedule representation is the same before and after the grouping.
6068 * Note that after the grouping, the union map representation contains
6069 * the domain constraints from the ranges of the expansion nodes,
6070 * while they are missing from the union map representation of
6071 * the tree without expansion nodes.
6073 * Also check that the global expansion is as expected.
6075 static int test_schedule_tree_group_2(isl_ctx *ctx)
6077 int equal, equal_expansion;
6078 const char *str;
6079 isl_id *id;
6080 isl_union_set *uset;
6081 isl_union_map *umap1, *umap2;
6082 isl_union_map *expansion1, *expansion2;
6083 isl_union_set_list *filters;
6084 isl_multi_union_pw_aff *mupa;
6085 isl_schedule *schedule;
6086 isl_schedule_node *node;
6088 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
6089 "S3[i,j] : 0 <= i,j < 10 }";
6090 uset = isl_union_set_read_from_str(ctx, str);
6091 node = isl_schedule_node_from_domain(uset);
6092 node = isl_schedule_node_child(node, 0);
6093 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
6094 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6095 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6096 node = isl_schedule_node_child(node, 0);
6097 str = "{ S1[i,j] }";
6098 uset = isl_union_set_read_from_str(ctx, str);
6099 filters = isl_union_set_list_from_union_set(uset);
6100 str = "{ S2[i,j]; S3[i,j] }";
6101 uset = isl_union_set_read_from_str(ctx, str);
6102 filters = isl_union_set_list_add(filters, uset);
6103 node = isl_schedule_node_insert_sequence(node, filters);
6104 node = isl_schedule_node_child(node, 1);
6105 node = isl_schedule_node_child(node, 0);
6106 str = "{ S2[i,j] }";
6107 uset = isl_union_set_read_from_str(ctx, str);
6108 filters = isl_union_set_list_from_union_set(uset);
6109 str = "{ S3[i,j] }";
6110 uset = isl_union_set_read_from_str(ctx, str);
6111 filters = isl_union_set_list_add(filters, uset);
6112 node = isl_schedule_node_insert_sequence(node, filters);
6114 schedule = isl_schedule_node_get_schedule(node);
6115 umap1 = isl_schedule_get_map(schedule);
6116 uset = isl_schedule_get_domain(schedule);
6117 umap1 = isl_union_map_intersect_domain(umap1, uset);
6118 isl_schedule_free(schedule);
6120 node = isl_schedule_node_parent(node);
6121 node = isl_schedule_node_parent(node);
6122 id = isl_id_alloc(ctx, "group1", NULL);
6123 node = isl_schedule_node_group(node, id);
6124 node = isl_schedule_node_child(node, 1);
6125 node = isl_schedule_node_child(node, 0);
6126 id = isl_id_alloc(ctx, "group2", NULL);
6127 node = isl_schedule_node_group(node, id);
6129 schedule = isl_schedule_node_get_schedule(node);
6130 umap2 = isl_schedule_get_map(schedule);
6131 isl_schedule_free(schedule);
6133 node = isl_schedule_node_root(node);
6134 node = isl_schedule_node_child(node, 0);
6135 expansion1 = isl_schedule_node_get_subtree_expansion(node);
6136 isl_schedule_node_free(node);
6138 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
6139 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
6140 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
6142 expansion2 = isl_union_map_read_from_str(ctx, str);
6144 equal = isl_union_map_is_equal(umap1, umap2);
6145 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
6147 isl_union_map_free(umap1);
6148 isl_union_map_free(umap2);
6149 isl_union_map_free(expansion1);
6150 isl_union_map_free(expansion2);
6152 if (equal < 0 || equal_expansion < 0)
6153 return -1;
6154 if (!equal)
6155 isl_die(ctx, isl_error_unknown,
6156 "expressions not equal", return -1);
6157 if (!equal_expansion)
6158 isl_die(ctx, isl_error_unknown,
6159 "unexpected expansion", return -1);
6161 return 0;
6164 /* Some tests for the isl_schedule_node_group function.
6166 static int test_schedule_tree_group(isl_ctx *ctx)
6168 if (test_schedule_tree_group_1(ctx) < 0)
6169 return -1;
6170 if (test_schedule_tree_group_2(ctx) < 0)
6171 return -1;
6172 return 0;
6175 struct {
6176 const char *set;
6177 const char *dual;
6178 } coef_tests[] = {
6179 { "{ rat: [i] : 0 <= i <= 10 }",
6180 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
6181 { "{ rat: [i] : FALSE }",
6182 "{ rat: coefficients[[cst] -> [a]] }" },
6183 { "{ rat: [i] : }",
6184 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
6187 struct {
6188 const char *set;
6189 const char *dual;
6190 } sol_tests[] = {
6191 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
6192 "{ rat: [i] : 0 <= i <= 10 }" },
6193 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
6194 "{ rat: [i] }" },
6195 { "{ rat: coefficients[[cst] -> [a]] }",
6196 "{ rat: [i] : FALSE }" },
6199 /* Test the basic functionality of isl_basic_set_coefficients and
6200 * isl_basic_set_solutions.
6202 static int test_dual(isl_ctx *ctx)
6204 int i;
6206 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
6207 int equal;
6208 isl_basic_set *bset1, *bset2;
6210 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
6211 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
6212 bset1 = isl_basic_set_coefficients(bset1);
6213 equal = isl_basic_set_is_equal(bset1, bset2);
6214 isl_basic_set_free(bset1);
6215 isl_basic_set_free(bset2);
6216 if (equal < 0)
6217 return -1;
6218 if (!equal)
6219 isl_die(ctx, isl_error_unknown,
6220 "incorrect dual", return -1);
6223 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
6224 int equal;
6225 isl_basic_set *bset1, *bset2;
6227 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
6228 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
6229 bset1 = isl_basic_set_solutions(bset1);
6230 equal = isl_basic_set_is_equal(bset1, bset2);
6231 isl_basic_set_free(bset1);
6232 isl_basic_set_free(bset2);
6233 if (equal < 0)
6234 return -1;
6235 if (!equal)
6236 isl_die(ctx, isl_error_unknown,
6237 "incorrect dual", return -1);
6240 return 0;
6243 struct {
6244 int scale_tile;
6245 int shift_point;
6246 const char *domain;
6247 const char *schedule;
6248 const char *sizes;
6249 const char *tile;
6250 const char *point;
6251 } tile_tests[] = {
6252 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6253 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6254 "{ [32,32] }",
6255 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6256 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6258 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6259 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6260 "{ [32,32] }",
6261 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6262 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6264 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6265 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6266 "{ [32,32] }",
6267 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6268 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6270 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6271 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6272 "{ [32,32] }",
6273 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6274 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6278 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
6279 * tile the band and then check if the tile and point bands have the
6280 * expected partial schedule.
6282 static int test_tile(isl_ctx *ctx)
6284 int i;
6285 int scale;
6286 int shift;
6288 scale = isl_options_get_tile_scale_tile_loops(ctx);
6289 shift = isl_options_get_tile_shift_point_loops(ctx);
6291 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
6292 int opt;
6293 int equal;
6294 const char *str;
6295 isl_union_set *domain;
6296 isl_multi_union_pw_aff *mupa, *mupa2;
6297 isl_schedule_node *node;
6298 isl_multi_val *sizes;
6300 opt = tile_tests[i].scale_tile;
6301 isl_options_set_tile_scale_tile_loops(ctx, opt);
6302 opt = tile_tests[i].shift_point;
6303 isl_options_set_tile_shift_point_loops(ctx, opt);
6305 str = tile_tests[i].domain;
6306 domain = isl_union_set_read_from_str(ctx, str);
6307 node = isl_schedule_node_from_domain(domain);
6308 node = isl_schedule_node_child(node, 0);
6309 str = tile_tests[i].schedule;
6310 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6311 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6312 str = tile_tests[i].sizes;
6313 sizes = isl_multi_val_read_from_str(ctx, str);
6314 node = isl_schedule_node_band_tile(node, sizes);
6316 str = tile_tests[i].tile;
6317 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6318 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6319 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
6320 isl_multi_union_pw_aff_free(mupa);
6321 isl_multi_union_pw_aff_free(mupa2);
6323 node = isl_schedule_node_child(node, 0);
6325 str = tile_tests[i].point;
6326 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6327 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
6328 if (equal >= 0 && equal)
6329 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
6330 mupa2);
6331 isl_multi_union_pw_aff_free(mupa);
6332 isl_multi_union_pw_aff_free(mupa2);
6334 isl_schedule_node_free(node);
6336 if (equal < 0)
6337 return -1;
6338 if (!equal)
6339 isl_die(ctx, isl_error_unknown,
6340 "unexpected result", return -1);
6343 isl_options_set_tile_scale_tile_loops(ctx, scale);
6344 isl_options_set_tile_shift_point_loops(ctx, shift);
6346 return 0;
6349 /* Check that the domain hash of a space is equal to the hash
6350 * of the domain of the space.
6352 static int test_domain_hash(isl_ctx *ctx)
6354 isl_map *map;
6355 isl_space *space;
6356 uint32_t hash1, hash2;
6358 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
6359 space = isl_map_get_space(map);
6360 isl_map_free(map);
6361 hash1 = isl_space_get_domain_hash(space);
6362 space = isl_space_domain(space);
6363 hash2 = isl_space_get_hash(space);
6364 isl_space_free(space);
6366 if (!space)
6367 return -1;
6368 if (hash1 != hash2)
6369 isl_die(ctx, isl_error_unknown,
6370 "domain hash not equal to hash of domain", return -1);
6372 return 0;
6375 /* Check that a universe basic set that is not obviously equal to the universe
6376 * is still recognized as being equal to the universe.
6378 static int test_universe(isl_ctx *ctx)
6380 const char *s;
6381 isl_basic_set *bset;
6382 isl_bool is_univ;
6384 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
6385 bset = isl_basic_set_read_from_str(ctx, s);
6386 is_univ = isl_basic_set_is_universe(bset);
6387 isl_basic_set_free(bset);
6389 if (is_univ < 0)
6390 return -1;
6391 if (!is_univ)
6392 isl_die(ctx, isl_error_unknown,
6393 "not recognized as universe set", return -1);
6395 return 0;
6398 struct {
6399 const char *name;
6400 int (*fn)(isl_ctx *ctx);
6401 } tests [] = {
6402 { "universe", &test_universe },
6403 { "domain hash", &test_domain_hash },
6404 { "dual", &test_dual },
6405 { "dependence analysis", &test_flow },
6406 { "val", &test_val },
6407 { "compute divs", &test_compute_divs },
6408 { "partial lexmin", &test_partial_lexmin },
6409 { "simplify", &test_simplify },
6410 { "curry", &test_curry },
6411 { "piecewise multi affine expressions", &test_pw_multi_aff },
6412 { "multi piecewise affine expressions", &test_multi_pw_aff },
6413 { "conversion", &test_conversion },
6414 { "list", &test_list },
6415 { "align parameters", &test_align_parameters },
6416 { "preimage", &test_preimage },
6417 { "pullback", &test_pullback },
6418 { "AST", &test_ast },
6419 { "AST build", &test_ast_build },
6420 { "AST generation", &test_ast_gen },
6421 { "eliminate", &test_eliminate },
6422 { "residue class", &test_residue_class },
6423 { "div", &test_div },
6424 { "slice", &test_slice },
6425 { "fixed power", &test_fixed_power },
6426 { "sample", &test_sample },
6427 { "output", &test_output },
6428 { "vertices", &test_vertices },
6429 { "fixed", &test_fixed },
6430 { "equal", &test_equal },
6431 { "disjoint", &test_disjoint },
6432 { "product", &test_product },
6433 { "dim_max", &test_dim_max },
6434 { "affine", &test_aff },
6435 { "injective", &test_injective },
6436 { "schedule (whole component)", &test_schedule_whole },
6437 { "schedule (incremental)", &test_schedule_incremental },
6438 { "schedule tree grouping", &test_schedule_tree_group },
6439 { "tile", &test_tile },
6440 { "union_pw", &test_union_pw },
6441 { "eval", &test_eval },
6442 { "parse", &test_parse },
6443 { "single-valued", &test_sv },
6444 { "affine hull", &test_affine_hull },
6445 { "simple_hull", &test_simple_hull },
6446 { "coalesce", &test_coalesce },
6447 { "factorize", &test_factorize },
6448 { "subset", &test_subset },
6449 { "subtract", &test_subtract },
6450 { "intersect", &test_intersect },
6451 { "lexmin", &test_lexmin },
6452 { "min", &test_min },
6453 { "gist", &test_gist },
6454 { "piecewise quasi-polynomials", &test_pwqp },
6455 { "lift", &test_lift },
6456 { "bound", &test_bound },
6457 { "union", &test_union },
6458 { "split periods", &test_split_periods },
6459 { "lexicographic order", &test_lex },
6460 { "bijectivity", &test_bijective },
6461 { "dataflow analysis", &test_dep },
6462 { "reading", &test_read },
6463 { "bounded", &test_bounded },
6464 { "construction", &test_construction },
6465 { "dimension manipulation", &test_dim },
6466 { "map application", &test_application },
6467 { "convex hull", &test_convex_hull },
6468 { "transitive closure", &test_closure },
6471 int main(int argc, char **argv)
6473 int i;
6474 struct isl_ctx *ctx;
6475 struct isl_options *options;
6477 srcdir = getenv("srcdir");
6478 assert(srcdir);
6480 options = isl_options_new_with_defaults();
6481 assert(options);
6482 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
6484 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
6485 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
6486 printf("%s\n", tests[i].name);
6487 if (tests[i].fn(ctx) < 0)
6488 goto error;
6490 isl_ctx_free(ctx);
6491 return 0;
6492 error:
6493 isl_ctx_free(ctx);
6494 return -1;