add isl_mat_row_basis_extension
[isl.git] / isl_test.c
blob4942debe4db897d5331bbe2f68b5577c34460ef6
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <assert.h>
19 #include <stdio.h>
20 #include <limits.h>
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl_space_private.h>
25 #include <isl/set.h>
26 #include <isl/flow.h>
27 #include <isl_constraint_private.h>
28 #include <isl/polynomial.h>
29 #include <isl/union_set.h>
30 #include <isl/union_map.h>
31 #include <isl_factorization.h>
32 #include <isl/schedule.h>
33 #include <isl/schedule_node.h>
34 #include <isl_options_private.h>
35 #include <isl_vertices_private.h>
36 #include <isl/ast_build.h>
37 #include <isl/val.h>
38 #include <isl/ilp.h>
39 #include <isl_ast_build_expr.h>
40 #include <isl/options.h>
42 #include "isl_srcdir.c"
44 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
46 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
47 char *filename;
48 int length;
49 char *pattern = "%s/test_inputs/%s.%s";
51 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
52 + strlen(suffix) + 1;
53 filename = isl_alloc_array(ctx, char, length);
55 if (!filename)
56 return NULL;
58 sprintf(filename, pattern, srcdir, name, suffix);
60 return filename;
63 void test_parse_map(isl_ctx *ctx, const char *str)
65 isl_map *map;
67 map = isl_map_read_from_str(ctx, str);
68 assert(map);
69 isl_map_free(map);
72 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
74 isl_map *map, *map2;
75 int equal;
77 map = isl_map_read_from_str(ctx, str);
78 map2 = isl_map_read_from_str(ctx, str2);
79 equal = isl_map_is_equal(map, map2);
80 isl_map_free(map);
81 isl_map_free(map2);
83 if (equal < 0)
84 return -1;
85 if (!equal)
86 isl_die(ctx, isl_error_unknown, "maps not equal",
87 return -1);
89 return 0;
92 void test_parse_pwqp(isl_ctx *ctx, const char *str)
94 isl_pw_qpolynomial *pwqp;
96 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
97 assert(pwqp);
98 isl_pw_qpolynomial_free(pwqp);
101 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
103 isl_pw_aff *pwaff;
105 pwaff = isl_pw_aff_read_from_str(ctx, str);
106 assert(pwaff);
107 isl_pw_aff_free(pwaff);
110 /* Check that we can read an isl_multi_val from "str" without errors.
112 static int test_parse_multi_val(isl_ctx *ctx, const char *str)
114 isl_multi_val *mv;
116 mv = isl_multi_val_read_from_str(ctx, str);
117 isl_multi_val_free(mv);
119 return mv ? 0 : -1;
122 /* Pairs of binary relation representations that should represent
123 * the same binary relations.
125 struct {
126 const char *map1;
127 const char *map2;
128 } parse_map_equal_tests[] = {
129 { "{ [x,y] : [([x/2]+y)/3] >= 1 }",
130 "{ [x, y] : 2y >= 6 - x }" },
131 { "{ [x,y] : x <= min(y, 2*y+3) }",
132 "{ [x,y] : x <= y, 2*y + 3 }" },
133 { "{ [x,y] : x >= min(y, 2*y+3) }",
134 "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }" },
135 { "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }",
136 "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }" },
137 { "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }",
138 "{ [i,j] -> [min(i,j)] }" },
139 { "{ [i,j] : i != j }",
140 "{ [i,j] : i < j or i > j }" },
141 { "{ [i,j] : (i+1)*2 >= j }",
142 "{ [i, j] : j <= 2 + 2i }" },
143 { "{ [i] -> [i > 0 ? 4 : 5] }",
144 "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }" },
145 { "[N=2,M] -> { [i=[(M+N)/4]] }",
146 "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }" },
147 { "{ [x] : x >= 0 }",
148 "{ [x] : x-0 >= 0 }" },
149 { "{ [i] : ((i > 10)) }",
150 "{ [i] : i >= 11 }" },
151 { "{ [i] -> [0] }",
152 "{ [i] -> [0 * i] }" },
153 { "{ [a] -> [b] : (not false) }",
154 "{ [a] -> [b] : true }" },
155 { "{ [i] : i/2 <= 5 }",
156 "{ [i] : i <= 10 }" },
157 { "{Sym=[n] [i] : i <= n }",
158 "[n] -> { [i] : i <= n }" },
159 { "{ [*] }",
160 "{ [a] }" },
161 { "{ [i] : 2*floor(i/2) = i }",
162 "{ [i] : exists a : i = 2 a }" },
163 { "{ [a] -> [b] : a = 5 implies b = 5 }",
164 "{ [a] -> [b] : a != 5 or b = 5 }" },
165 { "{ [a] -> [a - 1 : a > 0] }",
166 "{ [a] -> [a - 1] : a > 0 }" },
167 { "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
168 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }" },
169 { "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
170 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
171 { "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
172 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
173 { "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
174 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
175 { "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
176 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
177 { "{ [a,b] -> [i,j] : a,b << i,j }",
178 "{ [a,b] -> [i,j] : a < i or (a = i and b < j) }" },
179 { "{ [a,b] -> [i,j] : a,b <<= i,j }",
180 "{ [a,b] -> [i,j] : a < i or (a = i and b <= j) }" },
181 { "{ [a,b] -> [i,j] : a,b >> i,j }",
182 "{ [a,b] -> [i,j] : a > i or (a = i and b > j) }" },
183 { "{ [a,b] -> [i,j] : a,b >>= i,j }",
184 "{ [a,b] -> [i,j] : a > i or (a = i and b >= j) }" },
185 { "{ [n] -> [i] : exists (a, b, c: 8b <= i - 32a and "
186 "8b >= -7 + i - 32 a and b >= 0 and b <= 3 and "
187 "8c < n - 32a and i < n and c >= 0 and "
188 "c <= 3 and c >= -4a) }",
189 "{ [n] -> [i] : 0 <= i < n }" },
190 { "{ [x] -> [] : exists (a, b: 0 <= a <= 1 and 0 <= b <= 3 and "
191 "2b <= x - 8a and 2b >= -1 + x - 8a) }",
192 "{ [x] -> [] : 0 <= x <= 15 }" },
193 { "{ [x] -> [x] : }",
194 "{ [x] -> [x] }" },
197 int test_parse(struct isl_ctx *ctx)
199 int i;
200 isl_map *map, *map2;
201 const char *str, *str2;
203 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
204 return -1;
205 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
206 return -1;
207 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
208 return -1;
210 str = "{ [i] -> [-i] }";
211 map = isl_map_read_from_str(ctx, str);
212 assert(map);
213 isl_map_free(map);
215 str = "{ A[i] -> L[([i/3])] }";
216 map = isl_map_read_from_str(ctx, str);
217 assert(map);
218 isl_map_free(map);
220 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
221 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
222 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
224 for (i = 0; i < ARRAY_SIZE(parse_map_equal_tests); ++i) {
225 str = parse_map_equal_tests[i].map1;
226 str2 = parse_map_equal_tests[i].map2;
227 if (test_parse_map_equal(ctx, str, str2) < 0)
228 return -1;
231 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
232 map = isl_map_read_from_str(ctx, str);
233 str = "{ [new, old] -> [o0, o1] : "
234 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
235 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
236 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
237 map2 = isl_map_read_from_str(ctx, str);
238 assert(isl_map_is_equal(map, map2));
239 isl_map_free(map);
240 isl_map_free(map2);
242 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
243 map = isl_map_read_from_str(ctx, str);
244 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
245 map2 = isl_map_read_from_str(ctx, str);
246 assert(isl_map_is_equal(map, map2));
247 isl_map_free(map);
248 isl_map_free(map2);
250 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
251 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
252 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
253 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
254 test_parse_pwaff(ctx, "{ [] -> [(100)] }");
256 return 0;
259 static int test_read(isl_ctx *ctx)
261 char *filename;
262 FILE *input;
263 isl_basic_set *bset1, *bset2;
264 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
265 int equal;
267 filename = get_filename(ctx, "set", "omega");
268 assert(filename);
269 input = fopen(filename, "r");
270 assert(input);
272 bset1 = isl_basic_set_read_from_file(ctx, input);
273 bset2 = isl_basic_set_read_from_str(ctx, str);
275 equal = isl_basic_set_is_equal(bset1, bset2);
277 isl_basic_set_free(bset1);
278 isl_basic_set_free(bset2);
279 free(filename);
281 fclose(input);
283 if (equal < 0)
284 return -1;
285 if (!equal)
286 isl_die(ctx, isl_error_unknown,
287 "read sets not equal", return -1);
289 return 0;
292 static int test_bounded(isl_ctx *ctx)
294 isl_set *set;
295 isl_bool bounded;
297 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
298 bounded = isl_set_is_bounded(set);
299 isl_set_free(set);
301 if (bounded < 0)
302 return -1;
303 if (!bounded)
304 isl_die(ctx, isl_error_unknown,
305 "set not considered bounded", return -1);
307 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
308 bounded = isl_set_is_bounded(set);
309 assert(!bounded);
310 isl_set_free(set);
312 if (bounded < 0)
313 return -1;
314 if (bounded)
315 isl_die(ctx, isl_error_unknown,
316 "set considered bounded", return -1);
318 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
319 bounded = isl_set_is_bounded(set);
320 isl_set_free(set);
322 if (bounded < 0)
323 return -1;
324 if (bounded)
325 isl_die(ctx, isl_error_unknown,
326 "set considered bounded", return -1);
328 return 0;
331 /* Construct the basic set { [i] : 5 <= i <= N } */
332 static int test_construction_1(isl_ctx *ctx)
334 isl_int v;
335 isl_space *dim;
336 isl_local_space *ls;
337 isl_basic_set *bset;
338 isl_constraint *c;
340 isl_int_init(v);
342 dim = isl_space_set_alloc(ctx, 1, 1);
343 bset = isl_basic_set_universe(isl_space_copy(dim));
344 ls = isl_local_space_from_space(dim);
346 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
347 isl_int_set_si(v, -1);
348 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
349 isl_int_set_si(v, 1);
350 c = isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
351 bset = isl_basic_set_add_constraint(bset, c);
353 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
354 isl_int_set_si(v, 1);
355 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
356 isl_int_set_si(v, -5);
357 c = isl_constraint_set_constant(c, v);
358 bset = isl_basic_set_add_constraint(bset, c);
360 isl_local_space_free(ls);
361 isl_basic_set_free(bset);
363 isl_int_clear(v);
365 return 0;
368 /* Construct the basic set { [x] : -100 <= x <= 100 }
369 * using isl_basic_set_{lower,upper}_bound_val and
370 * check that it is equal the same basic set parsed from a string.
372 static int test_construction_2(isl_ctx *ctx)
374 isl_bool equal;
375 isl_val *v;
376 isl_space *space;
377 isl_basic_set *bset1, *bset2;
379 v = isl_val_int_from_si(ctx, 100);
380 space = isl_space_set_alloc(ctx, 0, 1);
381 bset1 = isl_basic_set_universe(space);
382 bset1 = isl_basic_set_upper_bound_val(bset1, isl_dim_set, 0,
383 isl_val_copy(v));
384 bset1 = isl_basic_set_lower_bound_val(bset1, isl_dim_set, 0,
385 isl_val_neg(v));
386 bset2 = isl_basic_set_read_from_str(ctx, "{ [x] : -100 <= x <= 100 }");
387 equal = isl_basic_set_is_equal(bset1, bset2);
388 isl_basic_set_free(bset1);
389 isl_basic_set_free(bset2);
391 if (equal < 0)
392 return -1;
393 if (!equal)
394 isl_die(ctx, isl_error_unknown,
395 "failed construction", return -1);
397 return 0;
400 /* Basic tests for constructing basic sets.
402 static int test_construction(isl_ctx *ctx)
404 if (test_construction_1(ctx) < 0)
405 return -1;
406 if (test_construction_2(ctx) < 0)
407 return -1;
408 return 0;
411 static int test_dim(isl_ctx *ctx)
413 const char *str;
414 isl_map *map1, *map2;
415 int equal;
417 map1 = isl_map_read_from_str(ctx,
418 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
419 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
420 map2 = isl_map_read_from_str(ctx,
421 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
422 equal = isl_map_is_equal(map1, map2);
423 isl_map_free(map2);
425 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
426 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
427 if (equal >= 0 && equal)
428 equal = isl_map_is_equal(map1, map2);
430 isl_map_free(map1);
431 isl_map_free(map2);
433 if (equal < 0)
434 return -1;
435 if (!equal)
436 isl_die(ctx, isl_error_unknown,
437 "unexpected result", return -1);
439 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
440 map1 = isl_map_read_from_str(ctx, str);
441 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
442 map2 = isl_map_read_from_str(ctx, str);
443 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
444 equal = isl_map_is_equal(map1, map2);
445 isl_map_free(map1);
446 isl_map_free(map2);
448 if (equal < 0)
449 return -1;
450 if (!equal)
451 isl_die(ctx, isl_error_unknown,
452 "unexpected result", return -1);
454 return 0;
457 struct {
458 __isl_give isl_val *(*op)(__isl_take isl_val *v);
459 const char *arg;
460 const char *res;
461 } val_un_tests[] = {
462 { &isl_val_neg, "0", "0" },
463 { &isl_val_abs, "0", "0" },
464 { &isl_val_2exp, "0", "1" },
465 { &isl_val_floor, "0", "0" },
466 { &isl_val_ceil, "0", "0" },
467 { &isl_val_neg, "1", "-1" },
468 { &isl_val_neg, "-1", "1" },
469 { &isl_val_neg, "1/2", "-1/2" },
470 { &isl_val_neg, "-1/2", "1/2" },
471 { &isl_val_neg, "infty", "-infty" },
472 { &isl_val_neg, "-infty", "infty" },
473 { &isl_val_neg, "NaN", "NaN" },
474 { &isl_val_abs, "1", "1" },
475 { &isl_val_abs, "-1", "1" },
476 { &isl_val_abs, "1/2", "1/2" },
477 { &isl_val_abs, "-1/2", "1/2" },
478 { &isl_val_abs, "infty", "infty" },
479 { &isl_val_abs, "-infty", "infty" },
480 { &isl_val_abs, "NaN", "NaN" },
481 { &isl_val_floor, "1", "1" },
482 { &isl_val_floor, "-1", "-1" },
483 { &isl_val_floor, "1/2", "0" },
484 { &isl_val_floor, "-1/2", "-1" },
485 { &isl_val_floor, "infty", "infty" },
486 { &isl_val_floor, "-infty", "-infty" },
487 { &isl_val_floor, "NaN", "NaN" },
488 { &isl_val_ceil, "1", "1" },
489 { &isl_val_ceil, "-1", "-1" },
490 { &isl_val_ceil, "1/2", "1" },
491 { &isl_val_ceil, "-1/2", "0" },
492 { &isl_val_ceil, "infty", "infty" },
493 { &isl_val_ceil, "-infty", "-infty" },
494 { &isl_val_ceil, "NaN", "NaN" },
495 { &isl_val_2exp, "-3", "1/8" },
496 { &isl_val_2exp, "-1", "1/2" },
497 { &isl_val_2exp, "1", "2" },
498 { &isl_val_2exp, "2", "4" },
499 { &isl_val_2exp, "3", "8" },
500 { &isl_val_inv, "1", "1" },
501 { &isl_val_inv, "2", "1/2" },
502 { &isl_val_inv, "1/2", "2" },
503 { &isl_val_inv, "-2", "-1/2" },
504 { &isl_val_inv, "-1/2", "-2" },
505 { &isl_val_inv, "0", "NaN" },
506 { &isl_val_inv, "NaN", "NaN" },
507 { &isl_val_inv, "infty", "0" },
508 { &isl_val_inv, "-infty", "0" },
511 /* Perform some basic tests of unary operations on isl_val objects.
513 static int test_un_val(isl_ctx *ctx)
515 int i;
516 isl_val *v, *res;
517 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
518 int ok;
520 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
521 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
522 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
523 fn = val_un_tests[i].op;
524 v = fn(v);
525 if (isl_val_is_nan(res))
526 ok = isl_val_is_nan(v);
527 else
528 ok = isl_val_eq(v, res);
529 isl_val_free(v);
530 isl_val_free(res);
531 if (ok < 0)
532 return -1;
533 if (!ok)
534 isl_die(ctx, isl_error_unknown,
535 "unexpected result", return -1);
538 return 0;
541 struct {
542 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
543 __isl_take isl_val *v2);
544 } val_bin_op[] = {
545 ['+'] = { &isl_val_add },
546 ['-'] = { &isl_val_sub },
547 ['*'] = { &isl_val_mul },
548 ['/'] = { &isl_val_div },
549 ['g'] = { &isl_val_gcd },
550 ['m'] = { &isl_val_min },
551 ['M'] = { &isl_val_max },
554 struct {
555 const char *arg1;
556 unsigned char op;
557 const char *arg2;
558 const char *res;
559 } val_bin_tests[] = {
560 { "0", '+', "0", "0" },
561 { "1", '+', "0", "1" },
562 { "1", '+', "1", "2" },
563 { "1", '-', "1", "0" },
564 { "1", '*', "1", "1" },
565 { "1", '/', "1", "1" },
566 { "2", '*', "3", "6" },
567 { "2", '*', "1/2", "1" },
568 { "2", '*', "1/3", "2/3" },
569 { "2/3", '*', "3/5", "2/5" },
570 { "2/3", '*', "7/5", "14/15" },
571 { "2", '/', "1/2", "4" },
572 { "-2", '/', "-1/2", "4" },
573 { "-2", '/', "1/2", "-4" },
574 { "2", '/', "-1/2", "-4" },
575 { "2", '/', "2", "1" },
576 { "2", '/', "3", "2/3" },
577 { "2/3", '/', "5/3", "2/5" },
578 { "2/3", '/', "5/7", "14/15" },
579 { "0", '/', "0", "NaN" },
580 { "42", '/', "0", "NaN" },
581 { "-42", '/', "0", "NaN" },
582 { "infty", '/', "0", "NaN" },
583 { "-infty", '/', "0", "NaN" },
584 { "NaN", '/', "0", "NaN" },
585 { "0", '/', "NaN", "NaN" },
586 { "42", '/', "NaN", "NaN" },
587 { "-42", '/', "NaN", "NaN" },
588 { "infty", '/', "NaN", "NaN" },
589 { "-infty", '/', "NaN", "NaN" },
590 { "NaN", '/', "NaN", "NaN" },
591 { "0", '/', "infty", "0" },
592 { "42", '/', "infty", "0" },
593 { "-42", '/', "infty", "0" },
594 { "infty", '/', "infty", "NaN" },
595 { "-infty", '/', "infty", "NaN" },
596 { "NaN", '/', "infty", "NaN" },
597 { "0", '/', "-infty", "0" },
598 { "42", '/', "-infty", "0" },
599 { "-42", '/', "-infty", "0" },
600 { "infty", '/', "-infty", "NaN" },
601 { "-infty", '/', "-infty", "NaN" },
602 { "NaN", '/', "-infty", "NaN" },
603 { "1", '-', "1/3", "2/3" },
604 { "1/3", '+', "1/2", "5/6" },
605 { "1/2", '+', "1/2", "1" },
606 { "3/4", '-', "1/4", "1/2" },
607 { "1/2", '-', "1/3", "1/6" },
608 { "infty", '+', "42", "infty" },
609 { "infty", '+', "infty", "infty" },
610 { "42", '+', "infty", "infty" },
611 { "infty", '-', "infty", "NaN" },
612 { "infty", '*', "infty", "infty" },
613 { "infty", '*', "-infty", "-infty" },
614 { "-infty", '*', "infty", "-infty" },
615 { "-infty", '*', "-infty", "infty" },
616 { "0", '*', "infty", "NaN" },
617 { "1", '*', "infty", "infty" },
618 { "infty", '*', "0", "NaN" },
619 { "infty", '*', "42", "infty" },
620 { "42", '-', "infty", "-infty" },
621 { "infty", '+', "-infty", "NaN" },
622 { "4", 'g', "6", "2" },
623 { "5", 'g', "6", "1" },
624 { "42", 'm', "3", "3" },
625 { "42", 'M', "3", "42" },
626 { "3", 'm', "42", "3" },
627 { "3", 'M', "42", "42" },
628 { "42", 'm', "infty", "42" },
629 { "42", 'M', "infty", "infty" },
630 { "42", 'm', "-infty", "-infty" },
631 { "42", 'M', "-infty", "42" },
632 { "42", 'm', "NaN", "NaN" },
633 { "42", 'M', "NaN", "NaN" },
634 { "infty", 'm', "-infty", "-infty" },
635 { "infty", 'M', "-infty", "infty" },
638 /* Perform some basic tests of binary operations on isl_val objects.
640 static int test_bin_val(isl_ctx *ctx)
642 int i;
643 isl_val *v1, *v2, *res;
644 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
645 __isl_take isl_val *v2);
646 int ok;
648 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
649 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
650 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
651 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
652 fn = val_bin_op[val_bin_tests[i].op].fn;
653 v1 = fn(v1, v2);
654 if (isl_val_is_nan(res))
655 ok = isl_val_is_nan(v1);
656 else
657 ok = isl_val_eq(v1, res);
658 isl_val_free(v1);
659 isl_val_free(res);
660 if (ok < 0)
661 return -1;
662 if (!ok)
663 isl_die(ctx, isl_error_unknown,
664 "unexpected result", return -1);
667 return 0;
670 /* Perform some basic tests on isl_val objects.
672 static int test_val(isl_ctx *ctx)
674 if (test_un_val(ctx) < 0)
675 return -1;
676 if (test_bin_val(ctx) < 0)
677 return -1;
678 return 0;
681 /* Sets described using existentially quantified variables that
682 * can also be described without.
684 static const char *elimination_tests[] = {
685 "{ [i,j] : 2 * [i/2] + 3 * [j/4] <= 10 and 2 i = j }",
686 "{ [m, w] : exists a : w - 2m - 5 <= 3a <= m - 2w }",
687 "{ [m, w] : exists a : w >= 0 and a < m and -1 + w <= a <= 2m - w }",
690 /* Check that redundant existentially quantified variables are
691 * getting removed.
693 static int test_elimination(isl_ctx *ctx)
695 int i;
696 unsigned n;
697 isl_basic_set *bset;
699 for (i = 0; i < ARRAY_SIZE(elimination_tests); ++i) {
700 bset = isl_basic_set_read_from_str(ctx, elimination_tests[i]);
701 n = isl_basic_set_dim(bset, isl_dim_div);
702 isl_basic_set_free(bset);
703 if (!bset)
704 return -1;
705 if (n != 0)
706 isl_die(ctx, isl_error_unknown,
707 "expecting no existentials", return -1);
710 return 0;
713 static int test_div(isl_ctx *ctx)
715 const char *str;
716 int empty;
717 isl_int v;
718 isl_space *dim;
719 isl_set *set;
720 isl_local_space *ls;
721 struct isl_basic_set *bset;
722 struct isl_constraint *c;
724 isl_int_init(v);
726 /* test 1 */
727 dim = isl_space_set_alloc(ctx, 0, 3);
728 bset = isl_basic_set_universe(isl_space_copy(dim));
729 ls = isl_local_space_from_space(dim);
731 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
732 isl_int_set_si(v, -1);
733 c = isl_constraint_set_constant(c, v);
734 isl_int_set_si(v, 1);
735 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
736 isl_int_set_si(v, 3);
737 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
738 bset = isl_basic_set_add_constraint(bset, c);
740 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
741 isl_int_set_si(v, 1);
742 c = isl_constraint_set_constant(c, v);
743 isl_int_set_si(v, -1);
744 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
745 isl_int_set_si(v, 3);
746 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
747 bset = isl_basic_set_add_constraint(bset, c);
749 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
751 assert(bset && bset->n_div == 1);
752 isl_local_space_free(ls);
753 isl_basic_set_free(bset);
755 /* test 2 */
756 dim = isl_space_set_alloc(ctx, 0, 3);
757 bset = isl_basic_set_universe(isl_space_copy(dim));
758 ls = isl_local_space_from_space(dim);
760 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
761 isl_int_set_si(v, 1);
762 c = isl_constraint_set_constant(c, v);
763 isl_int_set_si(v, -1);
764 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
765 isl_int_set_si(v, 3);
766 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
767 bset = isl_basic_set_add_constraint(bset, c);
769 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
770 isl_int_set_si(v, -1);
771 c = isl_constraint_set_constant(c, v);
772 isl_int_set_si(v, 1);
773 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
774 isl_int_set_si(v, 3);
775 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
776 bset = isl_basic_set_add_constraint(bset, c);
778 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
780 assert(bset && bset->n_div == 1);
781 isl_local_space_free(ls);
782 isl_basic_set_free(bset);
784 /* test 3 */
785 dim = isl_space_set_alloc(ctx, 0, 3);
786 bset = isl_basic_set_universe(isl_space_copy(dim));
787 ls = isl_local_space_from_space(dim);
789 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
790 isl_int_set_si(v, 1);
791 c = isl_constraint_set_constant(c, v);
792 isl_int_set_si(v, -1);
793 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
794 isl_int_set_si(v, 3);
795 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
796 bset = isl_basic_set_add_constraint(bset, c);
798 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
799 isl_int_set_si(v, -3);
800 c = isl_constraint_set_constant(c, v);
801 isl_int_set_si(v, 1);
802 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
803 isl_int_set_si(v, 4);
804 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
805 bset = isl_basic_set_add_constraint(bset, c);
807 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
809 assert(bset && bset->n_div == 1);
810 isl_local_space_free(ls);
811 isl_basic_set_free(bset);
813 /* test 4 */
814 dim = isl_space_set_alloc(ctx, 0, 3);
815 bset = isl_basic_set_universe(isl_space_copy(dim));
816 ls = isl_local_space_from_space(dim);
818 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
819 isl_int_set_si(v, 2);
820 c = isl_constraint_set_constant(c, v);
821 isl_int_set_si(v, -1);
822 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
823 isl_int_set_si(v, 3);
824 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
825 bset = isl_basic_set_add_constraint(bset, c);
827 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
828 isl_int_set_si(v, -1);
829 c = isl_constraint_set_constant(c, v);
830 isl_int_set_si(v, 1);
831 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
832 isl_int_set_si(v, 6);
833 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
834 bset = isl_basic_set_add_constraint(bset, c);
836 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
838 assert(isl_basic_set_is_empty(bset));
839 isl_local_space_free(ls);
840 isl_basic_set_free(bset);
842 /* test 5 */
843 dim = isl_space_set_alloc(ctx, 0, 3);
844 bset = isl_basic_set_universe(isl_space_copy(dim));
845 ls = isl_local_space_from_space(dim);
847 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
848 isl_int_set_si(v, -1);
849 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
850 isl_int_set_si(v, 3);
851 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
852 bset = isl_basic_set_add_constraint(bset, c);
854 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
855 isl_int_set_si(v, 1);
856 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
857 isl_int_set_si(v, -3);
858 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
859 bset = isl_basic_set_add_constraint(bset, c);
861 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
863 assert(bset && bset->n_div == 0);
864 isl_basic_set_free(bset);
865 isl_local_space_free(ls);
867 /* test 6 */
868 dim = isl_space_set_alloc(ctx, 0, 3);
869 bset = isl_basic_set_universe(isl_space_copy(dim));
870 ls = isl_local_space_from_space(dim);
872 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
873 isl_int_set_si(v, -1);
874 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
875 isl_int_set_si(v, 6);
876 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
877 bset = isl_basic_set_add_constraint(bset, c);
879 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
880 isl_int_set_si(v, 1);
881 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
882 isl_int_set_si(v, -3);
883 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
884 bset = isl_basic_set_add_constraint(bset, c);
886 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
888 assert(bset && bset->n_div == 1);
889 isl_basic_set_free(bset);
890 isl_local_space_free(ls);
892 /* test 7 */
893 /* This test is a bit tricky. We set up an equality
894 * a + 3b + 3c = 6 e0
895 * Normalization of divs creates _two_ divs
896 * a = 3 e0
897 * c - b - e0 = 2 e1
898 * Afterwards e0 is removed again because it has coefficient -1
899 * and we end up with the original equality and div again.
900 * Perhaps we can avoid the introduction of this temporary div.
902 dim = isl_space_set_alloc(ctx, 0, 4);
903 bset = isl_basic_set_universe(isl_space_copy(dim));
904 ls = isl_local_space_from_space(dim);
906 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
907 isl_int_set_si(v, -1);
908 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
909 isl_int_set_si(v, -3);
910 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
911 isl_int_set_si(v, -3);
912 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
913 isl_int_set_si(v, 6);
914 c = isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
915 bset = isl_basic_set_add_constraint(bset, c);
917 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
919 /* Test disabled for now */
921 assert(bset && bset->n_div == 1);
923 isl_local_space_free(ls);
924 isl_basic_set_free(bset);
926 /* test 8 */
927 dim = isl_space_set_alloc(ctx, 0, 5);
928 bset = isl_basic_set_universe(isl_space_copy(dim));
929 ls = isl_local_space_from_space(dim);
931 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
932 isl_int_set_si(v, -1);
933 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
934 isl_int_set_si(v, -3);
935 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
936 isl_int_set_si(v, -3);
937 c = isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
938 isl_int_set_si(v, 6);
939 c = isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
940 bset = isl_basic_set_add_constraint(bset, c);
942 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
943 isl_int_set_si(v, -1);
944 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
945 isl_int_set_si(v, 1);
946 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
947 isl_int_set_si(v, 1);
948 c = isl_constraint_set_constant(c, v);
949 bset = isl_basic_set_add_constraint(bset, c);
951 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
953 /* Test disabled for now */
955 assert(bset && bset->n_div == 1);
957 isl_local_space_free(ls);
958 isl_basic_set_free(bset);
960 /* test 9 */
961 dim = isl_space_set_alloc(ctx, 0, 4);
962 bset = isl_basic_set_universe(isl_space_copy(dim));
963 ls = isl_local_space_from_space(dim);
965 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
966 isl_int_set_si(v, 1);
967 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
968 isl_int_set_si(v, -1);
969 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
970 isl_int_set_si(v, -2);
971 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
972 bset = isl_basic_set_add_constraint(bset, c);
974 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
975 isl_int_set_si(v, -1);
976 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
977 isl_int_set_si(v, 3);
978 c = isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
979 isl_int_set_si(v, 2);
980 c = isl_constraint_set_constant(c, v);
981 bset = isl_basic_set_add_constraint(bset, c);
983 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
985 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
987 assert(!isl_basic_set_is_empty(bset));
989 isl_local_space_free(ls);
990 isl_basic_set_free(bset);
992 /* test 10 */
993 dim = isl_space_set_alloc(ctx, 0, 3);
994 bset = isl_basic_set_universe(isl_space_copy(dim));
995 ls = isl_local_space_from_space(dim);
997 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
998 isl_int_set_si(v, 1);
999 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
1000 isl_int_set_si(v, -2);
1001 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
1002 bset = isl_basic_set_add_constraint(bset, c);
1004 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1006 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1008 isl_local_space_free(ls);
1009 isl_basic_set_free(bset);
1011 isl_int_clear(v);
1013 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1014 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
1015 set = isl_set_read_from_str(ctx, str);
1016 set = isl_set_compute_divs(set);
1017 isl_set_free(set);
1018 if (!set)
1019 return -1;
1021 if (test_elimination(ctx) < 0)
1022 return -1;
1024 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
1025 set = isl_set_read_from_str(ctx, str);
1026 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
1027 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
1028 empty = isl_set_is_empty(set);
1029 isl_set_free(set);
1030 if (empty < 0)
1031 return -1;
1032 if (!empty)
1033 isl_die(ctx, isl_error_unknown,
1034 "result not as accurate as expected", return -1);
1036 return 0;
1039 void test_application_case(struct isl_ctx *ctx, const char *name)
1041 char *filename;
1042 FILE *input;
1043 struct isl_basic_set *bset1, *bset2;
1044 struct isl_basic_map *bmap;
1046 filename = get_filename(ctx, name, "omega");
1047 assert(filename);
1048 input = fopen(filename, "r");
1049 assert(input);
1051 bset1 = isl_basic_set_read_from_file(ctx, input);
1052 bmap = isl_basic_map_read_from_file(ctx, input);
1054 bset1 = isl_basic_set_apply(bset1, bmap);
1056 bset2 = isl_basic_set_read_from_file(ctx, input);
1058 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1060 isl_basic_set_free(bset1);
1061 isl_basic_set_free(bset2);
1062 free(filename);
1064 fclose(input);
1067 static int test_application(isl_ctx *ctx)
1069 test_application_case(ctx, "application");
1070 test_application_case(ctx, "application2");
1072 return 0;
1075 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1077 char *filename;
1078 FILE *input;
1079 struct isl_basic_set *bset1, *bset2;
1081 filename = get_filename(ctx, name, "polylib");
1082 assert(filename);
1083 input = fopen(filename, "r");
1084 assert(input);
1086 bset1 = isl_basic_set_read_from_file(ctx, input);
1087 bset2 = isl_basic_set_read_from_file(ctx, input);
1089 bset1 = isl_basic_set_affine_hull(bset1);
1091 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1093 isl_basic_set_free(bset1);
1094 isl_basic_set_free(bset2);
1095 free(filename);
1097 fclose(input);
1100 int test_affine_hull(struct isl_ctx *ctx)
1102 const char *str;
1103 isl_set *set;
1104 isl_basic_set *bset, *bset2;
1105 int n;
1106 int subset;
1108 test_affine_hull_case(ctx, "affine2");
1109 test_affine_hull_case(ctx, "affine");
1110 test_affine_hull_case(ctx, "affine3");
1112 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1113 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1114 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1115 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1116 set = isl_set_read_from_str(ctx, str);
1117 bset = isl_set_affine_hull(set);
1118 n = isl_basic_set_dim(bset, isl_dim_div);
1119 isl_basic_set_free(bset);
1120 if (n != 0)
1121 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1122 return -1);
1124 /* Check that isl_map_affine_hull is not confused by
1125 * the reordering of divs in isl_map_align_divs.
1127 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1128 "32e0 = b and 32e1 = c); "
1129 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1130 set = isl_set_read_from_str(ctx, str);
1131 bset = isl_set_affine_hull(set);
1132 isl_basic_set_free(bset);
1133 if (!bset)
1134 return -1;
1136 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1137 "32e2 = 31 + 31e0 }";
1138 set = isl_set_read_from_str(ctx, str);
1139 bset = isl_set_affine_hull(set);
1140 str = "{ [a] : exists e : a = 32 e }";
1141 bset2 = isl_basic_set_read_from_str(ctx, str);
1142 subset = isl_basic_set_is_subset(bset, bset2);
1143 isl_basic_set_free(bset);
1144 isl_basic_set_free(bset2);
1145 if (subset < 0)
1146 return -1;
1147 if (!subset)
1148 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1149 return -1);
1151 return 0;
1154 /* Pairs of maps and the corresponding expected results of
1155 * isl_map_plain_unshifted_simple_hull.
1157 struct {
1158 const char *map;
1159 const char *hull;
1160 } plain_unshifted_simple_hull_tests[] = {
1161 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1162 "{ [i] -> [j] : i >= 1 }" },
1163 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1164 "(j mod 4 = 2 and k mod 6 = n) }",
1165 "{ [n] -> [i,j,k] : j mod 4 = 2 }" },
1168 /* Basic tests for isl_map_plain_unshifted_simple_hull.
1170 static int test_plain_unshifted_simple_hull(isl_ctx *ctx)
1172 int i;
1173 isl_map *map;
1174 isl_basic_map *hull, *expected;
1175 isl_bool equal;
1177 for (i = 0; i < ARRAY_SIZE(plain_unshifted_simple_hull_tests); ++i) {
1178 const char *str;
1179 str = plain_unshifted_simple_hull_tests[i].map;
1180 map = isl_map_read_from_str(ctx, str);
1181 str = plain_unshifted_simple_hull_tests[i].hull;
1182 expected = isl_basic_map_read_from_str(ctx, str);
1183 hull = isl_map_plain_unshifted_simple_hull(map);
1184 equal = isl_basic_map_is_equal(hull, expected);
1185 isl_basic_map_free(hull);
1186 isl_basic_map_free(expected);
1187 if (equal < 0)
1188 return -1;
1189 if (!equal)
1190 isl_die(ctx, isl_error_unknown, "unexpected hull",
1191 return -1);
1194 return 0;
1197 /* Pairs of sets and the corresponding expected results of
1198 * isl_set_unshifted_simple_hull.
1200 struct {
1201 const char *set;
1202 const char *hull;
1203 } unshifted_simple_hull_tests[] = {
1204 { "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1205 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1208 /* Basic tests for isl_set_unshifted_simple_hull.
1210 static int test_unshifted_simple_hull(isl_ctx *ctx)
1212 int i;
1213 isl_set *set;
1214 isl_basic_set *hull, *expected;
1215 isl_bool equal;
1217 for (i = 0; i < ARRAY_SIZE(unshifted_simple_hull_tests); ++i) {
1218 const char *str;
1219 str = unshifted_simple_hull_tests[i].set;
1220 set = isl_set_read_from_str(ctx, str);
1221 str = unshifted_simple_hull_tests[i].hull;
1222 expected = isl_basic_set_read_from_str(ctx, str);
1223 hull = isl_set_unshifted_simple_hull(set);
1224 equal = isl_basic_set_is_equal(hull, expected);
1225 isl_basic_set_free(hull);
1226 isl_basic_set_free(expected);
1227 if (equal < 0)
1228 return -1;
1229 if (!equal)
1230 isl_die(ctx, isl_error_unknown, "unexpected hull",
1231 return -1);
1234 return 0;
1237 static int test_simple_hull(struct isl_ctx *ctx)
1239 const char *str;
1240 isl_set *set;
1241 isl_basic_set *bset;
1242 isl_bool is_empty;
1244 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1245 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1246 set = isl_set_read_from_str(ctx, str);
1247 bset = isl_set_simple_hull(set);
1248 is_empty = isl_basic_set_is_empty(bset);
1249 isl_basic_set_free(bset);
1251 if (is_empty == isl_bool_error)
1252 return -1;
1254 if (is_empty == isl_bool_false)
1255 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1256 return -1);
1258 if (test_plain_unshifted_simple_hull(ctx) < 0)
1259 return -1;
1260 if (test_unshifted_simple_hull(ctx) < 0)
1261 return -1;
1263 return 0;
1266 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1268 char *filename;
1269 FILE *input;
1270 struct isl_basic_set *bset1, *bset2;
1271 struct isl_set *set;
1273 filename = get_filename(ctx, name, "polylib");
1274 assert(filename);
1275 input = fopen(filename, "r");
1276 assert(input);
1278 bset1 = isl_basic_set_read_from_file(ctx, input);
1279 bset2 = isl_basic_set_read_from_file(ctx, input);
1281 set = isl_basic_set_union(bset1, bset2);
1282 bset1 = isl_set_convex_hull(set);
1284 bset2 = isl_basic_set_read_from_file(ctx, input);
1286 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1288 isl_basic_set_free(bset1);
1289 isl_basic_set_free(bset2);
1290 free(filename);
1292 fclose(input);
1295 struct {
1296 const char *set;
1297 const char *hull;
1298 } convex_hull_tests[] = {
1299 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1300 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1301 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1302 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1303 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1304 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1305 "i2 <= 5 and i2 >= 4; "
1306 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1307 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1308 "i2 <= 5 + i0 and i2 >= i0 }" },
1309 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1310 "{ [x, y] : 1 = 0 }" },
1311 { "{ [x, y, z] : 0 <= x, y, z <= 10; [x, y, 0] : x >= 0 and y > 0; "
1312 "[x, y, 0] : x >= 0 and y < 0 }",
1313 "{ [x, y, z] : x >= 0 and 0 <= z <= 10 }" },
1316 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1318 int i;
1319 int orig_convex = ctx->opt->convex;
1320 ctx->opt->convex = convex;
1322 test_convex_hull_case(ctx, "convex0");
1323 test_convex_hull_case(ctx, "convex1");
1324 test_convex_hull_case(ctx, "convex2");
1325 test_convex_hull_case(ctx, "convex3");
1326 test_convex_hull_case(ctx, "convex4");
1327 test_convex_hull_case(ctx, "convex5");
1328 test_convex_hull_case(ctx, "convex6");
1329 test_convex_hull_case(ctx, "convex7");
1330 test_convex_hull_case(ctx, "convex8");
1331 test_convex_hull_case(ctx, "convex9");
1332 test_convex_hull_case(ctx, "convex10");
1333 test_convex_hull_case(ctx, "convex11");
1334 test_convex_hull_case(ctx, "convex12");
1335 test_convex_hull_case(ctx, "convex13");
1336 test_convex_hull_case(ctx, "convex14");
1337 test_convex_hull_case(ctx, "convex15");
1339 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1340 isl_set *set1, *set2;
1341 int equal;
1343 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1344 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1345 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1346 equal = isl_set_is_equal(set1, set2);
1347 isl_set_free(set1);
1348 isl_set_free(set2);
1350 if (equal < 0)
1351 return -1;
1352 if (!equal)
1353 isl_die(ctx, isl_error_unknown,
1354 "unexpected convex hull", return -1);
1357 ctx->opt->convex = orig_convex;
1359 return 0;
1362 static int test_convex_hull(isl_ctx *ctx)
1364 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1365 return -1;
1366 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1367 return -1;
1368 return 0;
1371 void test_gist_case(struct isl_ctx *ctx, const char *name)
1373 char *filename;
1374 FILE *input;
1375 struct isl_basic_set *bset1, *bset2;
1377 filename = get_filename(ctx, name, "polylib");
1378 assert(filename);
1379 input = fopen(filename, "r");
1380 assert(input);
1382 bset1 = isl_basic_set_read_from_file(ctx, input);
1383 bset2 = isl_basic_set_read_from_file(ctx, input);
1385 bset1 = isl_basic_set_gist(bset1, bset2);
1387 bset2 = isl_basic_set_read_from_file(ctx, input);
1389 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1391 isl_basic_set_free(bset1);
1392 isl_basic_set_free(bset2);
1393 free(filename);
1395 fclose(input);
1398 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1400 struct {
1401 const char *map;
1402 const char *context;
1403 const char *gist;
1404 } plain_gist_tests[] = {
1405 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1406 "{ [i] -> [j] : i >= 1 }",
1407 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1408 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1409 "(j mod 4 = 2 and k mod 6 = n) }",
1410 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1411 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1412 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1413 "{ [i] -> [j] : i > j }",
1414 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1417 /* Basic tests for isl_map_plain_gist_basic_map.
1419 static int test_plain_gist(isl_ctx *ctx)
1421 int i;
1423 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1424 const char *str;
1425 int equal;
1426 isl_map *map, *gist;
1427 isl_basic_map *context;
1429 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1430 str = plain_gist_tests[i].context;
1431 context = isl_basic_map_read_from_str(ctx, str);
1432 map = isl_map_plain_gist_basic_map(map, context);
1433 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1434 equal = isl_map_is_equal(map, gist);
1435 isl_map_free(map);
1436 isl_map_free(gist);
1437 if (equal < 0)
1438 return -1;
1439 if (!equal)
1440 isl_die(ctx, isl_error_unknown,
1441 "incorrect gist result", return -1);
1444 return 0;
1447 struct {
1448 const char *set;
1449 const char *context;
1450 const char *gist;
1451 } gist_tests[] = {
1452 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1453 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1454 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1455 "{ [a, b, c] : a <= 15 }" },
1456 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1457 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1458 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1459 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1460 { "{ [m, n, a, b] : a <= 2147 + n }",
1461 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1462 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1463 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1464 "b >= 0) }",
1465 "{ [m, n, ku, kl] }" },
1466 { "{ [a, a, b] : a >= 10 }",
1467 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1468 "{ [a, a, b] : a >= 10 }" },
1469 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1470 "{ [0, j] : j >= 0 }" },
1471 /* Check that no constraints on i6 are introduced in the gist */
1472 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1473 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1474 "5e0 <= 381 - t1 and i4 <= 1) }",
1475 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1476 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1477 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1478 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1479 "20e0 >= 1511 - 4t1 - 5i4) }" },
1480 /* Check that no constraints on i6 are introduced in the gist */
1481 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1482 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1483 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1484 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1485 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1486 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1487 "20e1 <= 1530 - 4t1 - 5i4 and "
1488 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1489 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1490 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1491 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1492 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1493 "10e0 <= -91 + 5i4 + 4i6 and "
1494 "10e0 >= -105 + 5i4 + 4i6) }",
1495 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1496 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1497 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1498 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1499 "{ [a, b, q, p] : b >= 1 + a }",
1500 "{ [a, b, q, p] : false }" },
1501 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1502 "[n] -> { [x] : x mod 32 = 0 }",
1503 "[n] -> { [x = n] }" },
1504 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1505 "{ [x] : x mod 2 = 0 }" },
1506 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1507 "{ [x] : x mod 128 = 0 }" },
1508 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1509 "{ [x] : x mod 3200 = 0 }" },
1510 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1511 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1512 "{ [a, b, c = a] }" },
1513 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1514 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1515 "{ [a, b, c = a] : a mod 3 = 0 }" },
1516 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1517 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1518 "{ [x] }" },
1519 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1520 "{ [x,y] : 1 <= y <= 3 }",
1521 "{ [x,y] }" },
1524 /* Check that isl_set_gist behaves as expected.
1526 * For the test cases in gist_tests, besides checking that the result
1527 * is as expected, also check that applying the gist operation does
1528 * not modify the input set (an earlier version of isl would do that) and
1529 * that the test case is consistent, i.e., that the gist has the same
1530 * intersection with the context as the input set.
1532 static int test_gist(struct isl_ctx *ctx)
1534 int i;
1535 const char *str;
1536 isl_basic_set *bset1, *bset2;
1537 isl_map *map1, *map2;
1538 int equal;
1540 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1541 int equal_input, equal_intersection;
1542 isl_set *set1, *set2, *copy, *context;
1544 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1545 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1546 copy = isl_set_copy(set1);
1547 set1 = isl_set_gist(set1, isl_set_copy(context));
1548 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1549 equal = isl_set_is_equal(set1, set2);
1550 isl_set_free(set1);
1551 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1552 equal_input = isl_set_is_equal(set1, copy);
1553 isl_set_free(copy);
1554 set1 = isl_set_intersect(set1, isl_set_copy(context));
1555 set2 = isl_set_intersect(set2, context);
1556 equal_intersection = isl_set_is_equal(set1, set2);
1557 isl_set_free(set2);
1558 isl_set_free(set1);
1559 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1560 return -1;
1561 if (!equal)
1562 isl_die(ctx, isl_error_unknown,
1563 "incorrect gist result", return -1);
1564 if (!equal_input)
1565 isl_die(ctx, isl_error_unknown,
1566 "gist modified input", return -1);
1567 if (!equal_input)
1568 isl_die(ctx, isl_error_unknown,
1569 "inconsistent gist test case", return -1);
1572 test_gist_case(ctx, "gist1");
1574 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1575 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1576 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1577 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1578 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1579 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1580 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1581 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1582 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1583 bset1 = isl_basic_set_read_from_str(ctx, str);
1584 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1585 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1586 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1587 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1588 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1589 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1590 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1591 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1592 bset2 = isl_basic_set_read_from_str(ctx, str);
1593 bset1 = isl_basic_set_gist(bset1, bset2);
1594 assert(bset1 && bset1->n_div == 0);
1595 isl_basic_set_free(bset1);
1597 /* Check that the integer divisions of the second disjunct
1598 * do not spread to the first disjunct.
1600 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1601 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1602 "(exists (e0 = [(-1 + t1)/16], "
1603 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1604 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1605 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1606 "o0 <= 4294967295 and t1 <= -1)) }";
1607 map1 = isl_map_read_from_str(ctx, str);
1608 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1609 map2 = isl_map_read_from_str(ctx, str);
1610 map1 = isl_map_gist(map1, map2);
1611 if (!map1)
1612 return -1;
1613 if (map1->n != 1)
1614 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1615 isl_map_free(map1); return -1);
1616 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1617 isl_die(ctx, isl_error_unknown, "expecting single div",
1618 isl_map_free(map1); return -1);
1619 isl_map_free(map1);
1621 if (test_plain_gist(ctx) < 0)
1622 return -1;
1624 return 0;
1627 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1629 isl_set *set, *set2;
1630 int equal;
1631 int one;
1633 set = isl_set_read_from_str(ctx, str);
1634 set = isl_set_coalesce(set);
1635 set2 = isl_set_read_from_str(ctx, str);
1636 equal = isl_set_is_equal(set, set2);
1637 one = set && set->n == 1;
1638 isl_set_free(set);
1639 isl_set_free(set2);
1641 if (equal < 0)
1642 return -1;
1643 if (!equal)
1644 isl_die(ctx, isl_error_unknown,
1645 "coalesced set not equal to input", return -1);
1646 if (check_one && !one)
1647 isl_die(ctx, isl_error_unknown,
1648 "coalesced set should not be a union", return -1);
1650 return 0;
1653 /* Inputs for coalescing tests with unbounded wrapping.
1654 * "str" is a string representation of the input set.
1655 * "single_disjunct" is set if we expect the result to consist of
1656 * a single disjunct.
1658 struct {
1659 int single_disjunct;
1660 const char *str;
1661 } coalesce_unbounded_tests[] = {
1662 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1663 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1664 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1665 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1666 "-10 <= y <= 0}" },
1667 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1668 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1669 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1670 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1673 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1674 * option turned off.
1676 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1678 int i;
1679 int r = 0;
1680 int bounded;
1682 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1683 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1685 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1686 const char *str = coalesce_unbounded_tests[i].str;
1687 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1688 if (test_coalesce_set(ctx, str, check_one) >= 0)
1689 continue;
1690 r = -1;
1691 break;
1694 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1696 return r;
1699 /* Inputs for coalescing tests.
1700 * "str" is a string representation of the input set.
1701 * "single_disjunct" is set if we expect the result to consist of
1702 * a single disjunct.
1704 struct {
1705 int single_disjunct;
1706 const char *str;
1707 } coalesce_tests[] = {
1708 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1709 "y >= x & x >= 2 & 5 >= y }" },
1710 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1711 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1712 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1713 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1714 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1715 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1716 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1717 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1718 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1719 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1720 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1721 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1722 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1723 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1724 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1725 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1726 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1727 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1728 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1729 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1730 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1731 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1732 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1733 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1734 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1735 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1736 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1737 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1738 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1739 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1740 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1741 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1742 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1743 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1744 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1745 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1746 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1747 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1748 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1749 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1750 "[o0, o1, o2, o3, o4, o5, o6]] : "
1751 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1752 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1753 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1754 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1755 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1756 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1757 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1758 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1759 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1760 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1761 "o6 >= i3 + i6 - o3 and M >= 0 and "
1762 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1763 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1764 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1765 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1766 "(o0 = 0 and M >= 2 and N >= 3) or "
1767 "(M = 0 and o0 = 0 and N >= 3) }" },
1768 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1769 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1770 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1771 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1772 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1773 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1774 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1775 "(y = 3 and x = 1) }" },
1776 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1777 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1778 "i1 <= M and i3 <= M and i4 <= M) or "
1779 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1780 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1781 "i4 <= -1 + M) }" },
1782 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1783 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1784 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1785 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1786 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1787 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1788 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1789 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1790 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1791 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1792 { 0, "{ [a, b] : exists e : 2e = a and "
1793 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1794 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1795 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1796 "j >= 1 and j' <= i + j - i' and i >= 1; "
1797 "[1, 1, 1, 1] }" },
1798 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1799 "[i,j] : exists a : j = 3a }" },
1800 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1801 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1802 "a >= 3) or "
1803 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1804 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1805 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1806 "c <= 6 + 8a and a >= 3; "
1807 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1808 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1809 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1810 "[x,0] : 3 <= x <= 4 }" },
1811 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1812 "[x,0] : 4 <= x <= 5 }" },
1813 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1814 "[x,0] : 3 <= x <= 5 }" },
1815 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1816 "[x,0] : 3 <= x <= 4 }" },
1817 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1818 "i1 <= 0; "
1819 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1820 { 1, "{ [0,0]; [1,1] }" },
1821 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1822 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1823 "ii <= k;"
1824 "[k, 0, k] : k <= 6 and k >= 1 }" },
1825 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1826 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1827 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1828 { 1, "[n] -> { [1] : n >= 0;"
1829 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1830 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1831 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1832 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1833 "2e0 <= x and 2e0 <= n);"
1834 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1835 "n >= 0) }" },
1836 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1837 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1838 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1839 "t1 = 1 }" },
1840 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1841 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1842 "[0, 0] }" },
1843 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1844 "t1 >= 13 and t1 <= 16);"
1845 "[t1] : t1 <= 15 and t1 >= 12 }" },
1846 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
1847 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
1848 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
1849 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
1850 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
1851 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
1852 "i <= 4j + 2 }" },
1853 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
1854 "(exists (e0 : 3e0 = -2 + c0)) }" },
1855 { 0, "[n, b0, t0] -> "
1856 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1857 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1858 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1859 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1860 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
1861 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
1862 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
1863 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
1864 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1865 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1866 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1867 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
1868 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
1869 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
1870 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
1871 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
1872 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
1873 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
1874 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
1875 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
1876 { 0, "{ [i0, i1, i2] : "
1877 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
1878 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
1879 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
1880 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
1881 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
1882 "32e0 <= 31 + i0)) or "
1883 "i0 >= 0 }" },
1884 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
1885 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
1886 "2*floor((c)/2) = c and 0 <= a <= 192;"
1887 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
1889 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
1890 "(0 <= a <= b <= n) }" },
1891 { 1, "{ [a, b] : 0 <= a <= 2 and b >= 0 and "
1892 "((0 < b <= 13) or (2*floor((a + b)/2) >= -5 + a + 2b)) }" },
1893 { 1, "{ [a] : (2 <= a <= 5) or (a mod 2 = 1 and 1 <= a <= 5) }" },
1894 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
1895 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1896 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
1897 "b = 3 and 9e0 <= -19 + 2c)) }" },
1898 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
1899 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1900 "(a = 4 and b = 3 and "
1901 "9*floor((-16 + 2c)/9) <= -19 + 2c) }" },
1902 { 0, "{ [a, b, c] : (b <= 2 and b <= -2 + a) or "
1903 "(b = -1 + a and 0 < a <= 3 and "
1904 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1905 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
1906 "b = 3 and 9e0 <= -19 + 2c)) }" },
1907 { 1, "{ [y, x] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
1908 "[1, 0] }" },
1909 { 1, "{ [x, y] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
1910 "[0, 1] }" },
1911 { 1, "{ [1, y] : -1 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
1912 { 1, "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
1913 { 1, "{ [x, y] : 0 <= x <= 10 and x - 4*floor(x/4) <= 1 and y <= 0; "
1914 "[x, y] : 0 <= x <= 10 and x - 4*floor(x/4) > 1 and y <= 0; "
1915 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) <= 1 and 0 < y; "
1916 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) > 1 and 0 < y }" },
1917 { 1, "{ [x, 0] : 0 <= x <= 10 and x mod 2 = 0; "
1918 "[x, 0] : 0 <= x <= 10 and x mod 2 = 1; "
1919 "[x, y] : 0 <= x <= 10 and 1 <= y <= 10 }" },
1920 { 1, "{ [a] : a <= 8 and "
1921 "(a mod 10 = 7 or a mod 10 = 8 or a mod 10 = 9) }" },
1922 { 1, "{ [x, y] : 2y = -x and x <= 0 or "
1923 "x <= -1 and 2y <= -x - 1 and 2y >= x - 1 }" },
1924 { 0, "{ [x, y] : 2y = -x and x <= 0 or "
1925 "x <= -2 and 2y <= -x - 1 and 2y >= x - 1 }" },
1926 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
1927 "(a < 0 and 3*floor((a)/3) < a) }" },
1928 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
1929 "(a < -1 and 3*floor((a)/3) < a) }" },
1930 { 1, "{ [a, b] : a <= 1024 and b >= 0 and "
1931 "((-31 - a + b <= 32*floor((-1 - a)/32) <= -33 + b and "
1932 "32*floor((-1 - a)/32) <= -16 + b + 16*floor((-1 - a)/16))"
1933 "or (2 <= a <= 15 and b < a)) }" },
1934 { 1, "{ [a] : a > 0 and ((16*floor((a)/16) < a and "
1935 "32*floor((a)/32) < a) or a <= 15) }" },
1936 { 1, "{ [a, b, c, d] : (-a + d) mod 64 = 0 and a <= 8 and b <= 1 and "
1937 "10 - a <= c <= 3 and d >= 5 and 9 - 64b <= d <= 70;"
1938 "[a, b = 1, c, d] : (-a + d) mod 64 = 0 and a <= 8 and c >= 4 and "
1939 "10 - a <= c <= 5 and 5 <= d <= 73 - c }" },
1940 { 1, "[n, m] -> { S_0[i] : (-n + i) mod 3 = 0 and m >= 3 + n and "
1941 "i >= n and 3*floor((2 + n + 2m)/3) <= n + 3m - i; "
1942 "S_0[n] : n <= m <= 2 + n }" },
1943 { 1, "{ [a, b] : exists (e0: 0 <= a <= 1 and b >= 0 and "
1944 "2e0 >= -5 + a + 2b and 2e0 >= -1 + a + b and "
1945 "2e0 <= a + b); "
1946 "[a, b] : exists (e0: 0 <= a <= 1 and 2e0 >= -5 + a + 2b and "
1947 "2e0 >= -1 - a + b and 2e0 <= -a + b and "
1948 "2e0 < -a + 2b) }" },
1949 { 1, "{ [i, j, i - 8j] : 8 <= i <= 63 and -7 + i <= 8j <= i; "
1950 "[i, 0, i] : 0 <= i <= 7 }" },
1951 { 1, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [1, 1] }" },
1952 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [0, 2] }" },
1953 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [-1, 3] }" },
1954 { 1, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [1, 1] }" },
1955 { 0, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [2, 1] }" },
1958 /* A specialized coalescing test case that would result
1959 * in a segmentation fault or a failed assertion in earlier versions of isl.
1961 static int test_coalesce_special(struct isl_ctx *ctx)
1963 const char *str;
1964 isl_map *map1, *map2;
1966 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1967 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
1968 "(y = 201 and o1 <= 239 and o1 >= 212) or "
1969 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
1970 "o1 <= 239 and o1 >= 212)) or "
1971 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
1972 "o1 <= 241 and o1 >= 240));"
1973 "[S_L220_OUT[] -> T7[]] -> "
1974 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
1975 "(y = 2 and o1 <= 241 and o1 >= 212) or "
1976 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
1977 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
1978 map1 = isl_map_read_from_str(ctx, str);
1979 map1 = isl_map_align_divs_internal(map1);
1980 map1 = isl_map_coalesce(map1);
1981 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1982 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
1983 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
1984 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
1985 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
1986 map2 = isl_map_read_from_str(ctx, str);
1987 map2 = isl_map_union(map2, map1);
1988 map2 = isl_map_align_divs_internal(map2);
1989 map2 = isl_map_coalesce(map2);
1990 isl_map_free(map2);
1991 if (!map2)
1992 return -1;
1994 return 0;
1997 /* A specialized coalescing test case that would result in an assertion
1998 * in an earlier version of isl.
1999 * The explicit call to isl_basic_set_union prevents the implicit
2000 * equality constraints in the first basic map from being detected prior
2001 * to the call to isl_set_coalesce, at least at the point
2002 * where this test case was introduced.
2004 static int test_coalesce_special2(struct isl_ctx *ctx)
2006 const char *str;
2007 isl_basic_set *bset1, *bset2;
2008 isl_set *set;
2010 str = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
2011 bset1 = isl_basic_set_read_from_str(ctx, str);
2012 str = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }" ;
2013 bset2 = isl_basic_set_read_from_str(ctx, str);
2014 set = isl_basic_set_union(bset1, bset2);
2015 set = isl_set_coalesce(set);
2016 isl_set_free(set);
2018 if (!set)
2019 return -1;
2020 return 0;
2023 /* Check that calling isl_set_coalesce does not leave other sets
2024 * that may share some information with the input to isl_set_coalesce
2025 * in an inconsistent state.
2026 * In particular, older versions of isl would modify all copies
2027 * of the basic sets in the isl_set_coalesce input in a way
2028 * that could leave them in an inconsistent state.
2029 * The result of printing any other set containing one of these
2030 * basic sets would then result in an invalid set description.
2032 static int test_coalesce_special3(isl_ctx *ctx)
2034 const char *str;
2035 char *s;
2036 isl_set *set1, *set2;
2037 isl_printer *p;
2039 set1 = isl_set_read_from_str(ctx, "{ [0, 0, 0] }");
2040 str = "{ [a, b, a + b] : a >= 0 and b >= 0 and 0 < a + b }";
2041 set2 = isl_set_read_from_str(ctx, str);
2042 set1 = isl_set_union(set1, isl_set_copy(set2));
2043 set1 = isl_set_coalesce(set1);
2044 isl_set_free(set1);
2046 p = isl_printer_to_str(ctx);
2047 p = isl_printer_print_set(p, set2);
2048 isl_set_free(set2);
2049 s = isl_printer_get_str(p);
2050 isl_printer_free(p);
2051 set1 = isl_set_read_from_str(ctx, s);
2052 free(s);
2053 isl_set_free(set1);
2055 if (!set1)
2056 return -1;
2058 return 0;
2061 /* Test the functionality of isl_set_coalesce.
2062 * That is, check that the output is always equal to the input
2063 * and in some cases that the result consists of a single disjunct.
2065 static int test_coalesce(struct isl_ctx *ctx)
2067 int i;
2069 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
2070 const char *str = coalesce_tests[i].str;
2071 int check_one = coalesce_tests[i].single_disjunct;
2072 if (test_coalesce_set(ctx, str, check_one) < 0)
2073 return -1;
2076 if (test_coalesce_unbounded_wrapping(ctx) < 0)
2077 return -1;
2078 if (test_coalesce_special(ctx) < 0)
2079 return -1;
2080 if (test_coalesce_special2(ctx) < 0)
2081 return -1;
2082 if (test_coalesce_special3(ctx) < 0)
2083 return -1;
2085 return 0;
2088 /* Construct a representation of the graph on the right of Figure 1
2089 * in "Computing the Transitive Closure of a Union of
2090 * Affine Integer Tuple Relations".
2092 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2094 isl_set *dom;
2095 isl_map *up, *right;
2097 dom = isl_set_read_from_str(ctx,
2098 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2099 "2 x - 3 y + 3 >= 0 }");
2100 right = isl_map_read_from_str(ctx,
2101 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2102 up = isl_map_read_from_str(ctx,
2103 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2104 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2105 right = isl_map_intersect_range(right, isl_set_copy(dom));
2106 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2107 up = isl_map_intersect_range(up, dom);
2108 return isl_map_union(up, right);
2111 /* Construct a representation of the power of the graph
2112 * on the right of Figure 1 in "Computing the Transitive Closure of
2113 * a Union of Affine Integer Tuple Relations".
2115 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2117 return isl_map_read_from_str(ctx,
2118 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2119 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2120 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2123 /* Construct a representation of the transitive closure of the graph
2124 * on the right of Figure 1 in "Computing the Transitive Closure of
2125 * a Union of Affine Integer Tuple Relations".
2127 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2129 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2132 static int test_closure(isl_ctx *ctx)
2134 const char *str;
2135 isl_map *map, *map2;
2136 int exact, equal;
2138 /* COCOA example 1 */
2139 map = isl_map_read_from_str(ctx,
2140 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2141 "1 <= i and i < n and 1 <= j and j < n or "
2142 "i2 = i + 1 and j2 = j - 1 and "
2143 "1 <= i and i < n and 2 <= j and j <= n }");
2144 map = isl_map_power(map, &exact);
2145 assert(exact);
2146 isl_map_free(map);
2148 /* COCOA example 1 */
2149 map = isl_map_read_from_str(ctx,
2150 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2151 "1 <= i and i < n and 1 <= j and j < n or "
2152 "i2 = i + 1 and j2 = j - 1 and "
2153 "1 <= i and i < n and 2 <= j and j <= n }");
2154 map = isl_map_transitive_closure(map, &exact);
2155 assert(exact);
2156 map2 = isl_map_read_from_str(ctx,
2157 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2158 "1 <= i and i < n and 1 <= j and j <= n and "
2159 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2160 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2161 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2162 assert(isl_map_is_equal(map, map2));
2163 isl_map_free(map2);
2164 isl_map_free(map);
2166 map = isl_map_read_from_str(ctx,
2167 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2168 " 0 <= y and y <= n }");
2169 map = isl_map_transitive_closure(map, &exact);
2170 map2 = isl_map_read_from_str(ctx,
2171 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2172 " 0 <= y and y <= n }");
2173 assert(isl_map_is_equal(map, map2));
2174 isl_map_free(map2);
2175 isl_map_free(map);
2177 /* COCOA example 2 */
2178 map = isl_map_read_from_str(ctx,
2179 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2180 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2181 "i2 = i + 2 and j2 = j - 2 and "
2182 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2183 map = isl_map_transitive_closure(map, &exact);
2184 assert(exact);
2185 map2 = isl_map_read_from_str(ctx,
2186 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2187 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2188 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2189 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2190 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2191 assert(isl_map_is_equal(map, map2));
2192 isl_map_free(map);
2193 isl_map_free(map2);
2195 /* COCOA Fig.2 left */
2196 map = isl_map_read_from_str(ctx,
2197 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2198 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2199 "j <= n or "
2200 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2201 "j <= 2 i - 3 and j <= n - 2 or "
2202 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2203 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2204 map = isl_map_transitive_closure(map, &exact);
2205 assert(exact);
2206 isl_map_free(map);
2208 /* COCOA Fig.2 right */
2209 map = isl_map_read_from_str(ctx,
2210 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2211 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2212 "j <= n or "
2213 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2214 "j <= 2 i - 4 and j <= n - 3 or "
2215 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2216 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2217 map = isl_map_power(map, &exact);
2218 assert(exact);
2219 isl_map_free(map);
2221 /* COCOA Fig.2 right */
2222 map = isl_map_read_from_str(ctx,
2223 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2224 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2225 "j <= n or "
2226 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2227 "j <= 2 i - 4 and j <= n - 3 or "
2228 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2229 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2230 map = isl_map_transitive_closure(map, &exact);
2231 assert(exact);
2232 map2 = isl_map_read_from_str(ctx,
2233 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2234 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2235 "j <= n and 3 + i + 2 j <= 3 n and "
2236 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2237 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2238 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2239 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2240 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2241 assert(isl_map_is_equal(map, map2));
2242 isl_map_free(map2);
2243 isl_map_free(map);
2245 map = cocoa_fig_1_right_graph(ctx);
2246 map = isl_map_transitive_closure(map, &exact);
2247 assert(exact);
2248 map2 = cocoa_fig_1_right_tc(ctx);
2249 assert(isl_map_is_equal(map, map2));
2250 isl_map_free(map2);
2251 isl_map_free(map);
2253 map = cocoa_fig_1_right_graph(ctx);
2254 map = isl_map_power(map, &exact);
2255 map2 = cocoa_fig_1_right_power(ctx);
2256 equal = isl_map_is_equal(map, map2);
2257 isl_map_free(map2);
2258 isl_map_free(map);
2259 if (equal < 0)
2260 return -1;
2261 if (!exact)
2262 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2263 if (!equal)
2264 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2266 /* COCOA Theorem 1 counter example */
2267 map = isl_map_read_from_str(ctx,
2268 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2269 "i2 = 1 and j2 = j or "
2270 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2271 map = isl_map_transitive_closure(map, &exact);
2272 assert(exact);
2273 isl_map_free(map);
2275 map = isl_map_read_from_str(ctx,
2276 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2277 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2278 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2279 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2280 map = isl_map_transitive_closure(map, &exact);
2281 assert(exact);
2282 isl_map_free(map);
2284 /* Kelly et al 1996, fig 12 */
2285 map = isl_map_read_from_str(ctx,
2286 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2287 "1 <= i,j,j+1 <= n or "
2288 "j = n and j2 = 1 and i2 = i + 1 and "
2289 "1 <= i,i+1 <= n }");
2290 map = isl_map_transitive_closure(map, &exact);
2291 assert(exact);
2292 map2 = isl_map_read_from_str(ctx,
2293 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2294 "1 <= i <= n and i = i2 or "
2295 "1 <= i < i2 <= n and 1 <= j <= n and "
2296 "1 <= j2 <= n }");
2297 assert(isl_map_is_equal(map, map2));
2298 isl_map_free(map2);
2299 isl_map_free(map);
2301 /* Omega's closure4 */
2302 map = isl_map_read_from_str(ctx,
2303 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2304 "1 <= x,y <= 10 or "
2305 "x2 = x + 1 and y2 = y and "
2306 "1 <= x <= 20 && 5 <= y <= 15 }");
2307 map = isl_map_transitive_closure(map, &exact);
2308 assert(exact);
2309 isl_map_free(map);
2311 map = isl_map_read_from_str(ctx,
2312 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2313 map = isl_map_transitive_closure(map, &exact);
2314 assert(!exact);
2315 map2 = isl_map_read_from_str(ctx,
2316 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2317 assert(isl_map_is_equal(map, map2));
2318 isl_map_free(map);
2319 isl_map_free(map2);
2321 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2322 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2323 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2324 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2325 map = isl_map_read_from_str(ctx, str);
2326 map = isl_map_transitive_closure(map, &exact);
2327 assert(exact);
2328 map2 = isl_map_read_from_str(ctx, str);
2329 assert(isl_map_is_equal(map, map2));
2330 isl_map_free(map);
2331 isl_map_free(map2);
2333 str = "{[0] -> [1]; [2] -> [3]}";
2334 map = isl_map_read_from_str(ctx, str);
2335 map = isl_map_transitive_closure(map, &exact);
2336 assert(exact);
2337 map2 = isl_map_read_from_str(ctx, str);
2338 assert(isl_map_is_equal(map, map2));
2339 isl_map_free(map);
2340 isl_map_free(map2);
2342 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2343 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2344 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2345 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2346 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2347 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2348 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2349 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2350 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2351 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2352 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2353 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2354 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2355 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2356 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2357 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2358 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2359 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2360 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2361 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2362 map = isl_map_read_from_str(ctx, str);
2363 map = isl_map_transitive_closure(map, NULL);
2364 assert(map);
2365 isl_map_free(map);
2367 return 0;
2370 static int test_lex(struct isl_ctx *ctx)
2372 isl_space *dim;
2373 isl_map *map;
2374 int empty;
2376 dim = isl_space_set_alloc(ctx, 0, 0);
2377 map = isl_map_lex_le(dim);
2378 empty = isl_map_is_empty(map);
2379 isl_map_free(map);
2381 if (empty < 0)
2382 return -1;
2383 if (empty)
2384 isl_die(ctx, isl_error_unknown,
2385 "expecting non-empty result", return -1);
2387 return 0;
2390 /* Inputs for isl_map_lexmin tests.
2391 * "map" is the input and "lexmin" is the expected result.
2393 struct {
2394 const char *map;
2395 const char *lexmin;
2396 } lexmin_tests [] = {
2397 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
2398 "{ [x] -> [5] : 6 <= x <= 8; "
2399 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
2400 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
2401 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
2402 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
2403 "{ [x] -> [y] : (4y = x and x >= 0) or "
2404 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2405 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2406 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
2407 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
2408 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
2409 /* Check that empty pieces are properly combined. */
2410 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2411 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2412 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
2413 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2414 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2415 "x >= 4 }" },
2416 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
2417 "a <= 255 and c <= 255 and d <= 255 - j and "
2418 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
2419 "247d <= 247 + k - j and 247d <= 247 + k - b and "
2420 "247d <= 247 + i and 248 - b <= 248d <= c and "
2421 "254d >= i - a + b and 254d >= -a + b and "
2422 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
2423 "{ [i, k, j] -> "
2424 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
2425 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
2426 "247j >= 62738 - i and 509j <= 129795 + i and "
2427 "742j >= 188724 - i; "
2428 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
2429 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
2430 "16*floor((8 + b)/16) <= 7 + b; "
2431 "[a] -> [1] }",
2432 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
2433 "[a] -> [b = 0] : 0 < a <= 509 }" },
2434 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
2435 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
2438 static int test_lexmin(struct isl_ctx *ctx)
2440 int i;
2441 int equal;
2442 const char *str;
2443 isl_basic_map *bmap;
2444 isl_map *map, *map2;
2445 isl_set *set;
2446 isl_set *set2;
2447 isl_pw_multi_aff *pma;
2449 str = "[p0, p1] -> { [] -> [] : "
2450 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2451 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2452 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2453 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2454 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2455 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2456 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2457 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2458 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2459 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2460 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2461 map = isl_map_read_from_str(ctx, str);
2462 map = isl_map_lexmin(map);
2463 isl_map_free(map);
2465 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2466 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2467 set = isl_set_read_from_str(ctx, str);
2468 set = isl_set_lexmax(set);
2469 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2470 set2 = isl_set_read_from_str(ctx, str);
2471 set = isl_set_intersect(set, set2);
2472 assert(!isl_set_is_empty(set));
2473 isl_set_free(set);
2475 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
2476 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
2477 map = isl_map_lexmin(map);
2478 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
2479 equal = isl_map_is_equal(map, map2);
2480 isl_map_free(map);
2481 isl_map_free(map2);
2483 if (equal < 0)
2484 return -1;
2485 if (!equal)
2486 isl_die(ctx, isl_error_unknown,
2487 "unexpected result", return -1);
2490 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2491 " 8i' <= i and 8i' >= -7 + i }";
2492 bmap = isl_basic_map_read_from_str(ctx, str);
2493 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2494 map2 = isl_map_from_pw_multi_aff(pma);
2495 map = isl_map_from_basic_map(bmap);
2496 assert(isl_map_is_equal(map, map2));
2497 isl_map_free(map);
2498 isl_map_free(map2);
2500 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2501 " 8i' <= i and 8i' >= -7 + i }";
2502 set = isl_set_read_from_str(ctx, str);
2503 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2504 set2 = isl_set_from_pw_multi_aff(pma);
2505 equal = isl_set_is_equal(set, set2);
2506 isl_set_free(set);
2507 isl_set_free(set2);
2508 if (equal < 0)
2509 return -1;
2510 if (!equal)
2511 isl_die(ctx, isl_error_unknown,
2512 "unexpected difference between set and "
2513 "piecewise affine expression", return -1);
2515 return 0;
2518 /* A specialized isl_set_min_val test case that would return the wrong result
2519 * in earlier versions of isl.
2520 * The explicit call to isl_basic_set_union prevents the second basic set
2521 * from being determined to be empty prior to the call to isl_set_min_val,
2522 * at least at the point where this test case was introduced.
2524 static int test_min_special(isl_ctx *ctx)
2526 const char *str;
2527 isl_basic_set *bset1, *bset2;
2528 isl_set *set;
2529 isl_aff *obj;
2530 isl_val *res;
2531 int ok;
2533 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
2534 bset1 = isl_basic_set_read_from_str(ctx, str);
2535 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
2536 bset2 = isl_basic_set_read_from_str(ctx, str);
2537 set = isl_basic_set_union(bset1, bset2);
2538 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
2540 res = isl_set_min_val(set, obj);
2541 ok = isl_val_cmp_si(res, 5) == 0;
2543 isl_aff_free(obj);
2544 isl_set_free(set);
2545 isl_val_free(res);
2547 if (!res)
2548 return -1;
2549 if (!ok)
2550 isl_die(ctx, isl_error_unknown, "unexpected minimum",
2551 return -1);
2553 return 0;
2556 /* A specialized isl_set_min_val test case that would return an error
2557 * in earlier versions of isl.
2559 static int test_min_special2(isl_ctx *ctx)
2561 const char *str;
2562 isl_basic_set *bset;
2563 isl_aff *obj;
2564 isl_val *res;
2566 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
2567 bset = isl_basic_set_read_from_str(ctx, str);
2569 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
2571 res = isl_basic_set_max_val(bset, obj);
2573 isl_basic_set_free(bset);
2574 isl_aff_free(obj);
2575 isl_val_free(res);
2577 if (!res)
2578 return -1;
2580 return 0;
2583 struct {
2584 const char *set;
2585 const char *obj;
2586 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2587 __isl_keep isl_aff *obj);
2588 const char *res;
2589 } opt_tests[] = {
2590 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2591 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2592 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2593 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2594 &isl_set_max_val, "30" },
2598 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2599 * In particular, check the results on non-convex inputs.
2601 static int test_min(struct isl_ctx *ctx)
2603 int i;
2604 isl_set *set;
2605 isl_aff *obj;
2606 isl_val *val, *res;
2607 isl_bool ok;
2609 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2610 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2611 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2612 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2613 val = opt_tests[i].fn(set, obj);
2614 ok = isl_val_eq(res, val);
2615 isl_val_free(res);
2616 isl_val_free(val);
2617 isl_aff_free(obj);
2618 isl_set_free(set);
2620 if (ok < 0)
2621 return -1;
2622 if (!ok)
2623 isl_die(ctx, isl_error_unknown,
2624 "unexpected optimum", return -1);
2627 if (test_min_special(ctx) < 0)
2628 return -1;
2629 if (test_min_special2(ctx) < 0)
2630 return -1;
2632 return 0;
2635 struct must_may {
2636 isl_map *must;
2637 isl_map *may;
2640 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2641 void *dep_user, void *user)
2643 struct must_may *mm = (struct must_may *)user;
2645 if (must)
2646 mm->must = isl_map_union(mm->must, dep);
2647 else
2648 mm->may = isl_map_union(mm->may, dep);
2650 return isl_stat_ok;
2653 static int common_space(void *first, void *second)
2655 int depth = *(int *)first;
2656 return 2 * depth;
2659 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2661 isl_map *map2;
2662 int equal;
2664 if (!map)
2665 return -1;
2667 map2 = isl_map_read_from_str(map->ctx, str);
2668 equal = isl_map_is_equal(map, map2);
2669 isl_map_free(map2);
2671 return equal;
2674 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2676 int equal;
2678 equal = map_is_equal(map, str);
2679 if (equal < 0)
2680 return -1;
2681 if (!equal)
2682 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2683 "result not as expected", return -1);
2684 return 0;
2687 static int test_dep(struct isl_ctx *ctx)
2689 const char *str;
2690 isl_space *dim;
2691 isl_map *map;
2692 isl_access_info *ai;
2693 isl_flow *flow;
2694 int depth;
2695 struct must_may mm;
2697 depth = 3;
2699 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2700 map = isl_map_read_from_str(ctx, str);
2701 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2703 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2704 map = isl_map_read_from_str(ctx, str);
2705 ai = isl_access_info_add_source(ai, map, 1, &depth);
2707 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2708 map = isl_map_read_from_str(ctx, str);
2709 ai = isl_access_info_add_source(ai, map, 1, &depth);
2711 flow = isl_access_info_compute_flow(ai);
2712 dim = isl_space_alloc(ctx, 0, 3, 3);
2713 mm.must = isl_map_empty(isl_space_copy(dim));
2714 mm.may = isl_map_empty(dim);
2716 isl_flow_foreach(flow, collect_must_may, &mm);
2718 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2719 " [1,10,0] -> [2,5,0] }";
2720 assert(map_is_equal(mm.must, str));
2721 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2722 assert(map_is_equal(mm.may, str));
2724 isl_map_free(mm.must);
2725 isl_map_free(mm.may);
2726 isl_flow_free(flow);
2729 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2730 map = isl_map_read_from_str(ctx, str);
2731 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2733 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2734 map = isl_map_read_from_str(ctx, str);
2735 ai = isl_access_info_add_source(ai, map, 1, &depth);
2737 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2738 map = isl_map_read_from_str(ctx, str);
2739 ai = isl_access_info_add_source(ai, map, 0, &depth);
2741 flow = isl_access_info_compute_flow(ai);
2742 dim = isl_space_alloc(ctx, 0, 3, 3);
2743 mm.must = isl_map_empty(isl_space_copy(dim));
2744 mm.may = isl_map_empty(dim);
2746 isl_flow_foreach(flow, collect_must_may, &mm);
2748 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2749 assert(map_is_equal(mm.must, str));
2750 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2751 assert(map_is_equal(mm.may, str));
2753 isl_map_free(mm.must);
2754 isl_map_free(mm.may);
2755 isl_flow_free(flow);
2758 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2759 map = isl_map_read_from_str(ctx, str);
2760 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2762 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2763 map = isl_map_read_from_str(ctx, str);
2764 ai = isl_access_info_add_source(ai, map, 0, &depth);
2766 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2767 map = isl_map_read_from_str(ctx, str);
2768 ai = isl_access_info_add_source(ai, map, 0, &depth);
2770 flow = isl_access_info_compute_flow(ai);
2771 dim = isl_space_alloc(ctx, 0, 3, 3);
2772 mm.must = isl_map_empty(isl_space_copy(dim));
2773 mm.may = isl_map_empty(dim);
2775 isl_flow_foreach(flow, collect_must_may, &mm);
2777 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2778 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2779 assert(map_is_equal(mm.may, str));
2780 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2781 assert(map_is_equal(mm.must, str));
2783 isl_map_free(mm.must);
2784 isl_map_free(mm.may);
2785 isl_flow_free(flow);
2788 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2789 map = isl_map_read_from_str(ctx, str);
2790 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2792 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2793 map = isl_map_read_from_str(ctx, str);
2794 ai = isl_access_info_add_source(ai, map, 0, &depth);
2796 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2797 map = isl_map_read_from_str(ctx, str);
2798 ai = isl_access_info_add_source(ai, map, 0, &depth);
2800 flow = isl_access_info_compute_flow(ai);
2801 dim = isl_space_alloc(ctx, 0, 3, 3);
2802 mm.must = isl_map_empty(isl_space_copy(dim));
2803 mm.may = isl_map_empty(dim);
2805 isl_flow_foreach(flow, collect_must_may, &mm);
2807 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2808 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2809 assert(map_is_equal(mm.may, str));
2810 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2811 assert(map_is_equal(mm.must, str));
2813 isl_map_free(mm.must);
2814 isl_map_free(mm.may);
2815 isl_flow_free(flow);
2818 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2819 map = isl_map_read_from_str(ctx, str);
2820 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2822 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2823 map = isl_map_read_from_str(ctx, str);
2824 ai = isl_access_info_add_source(ai, map, 0, &depth);
2826 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2827 map = isl_map_read_from_str(ctx, str);
2828 ai = isl_access_info_add_source(ai, map, 0, &depth);
2830 flow = isl_access_info_compute_flow(ai);
2831 dim = isl_space_alloc(ctx, 0, 3, 3);
2832 mm.must = isl_map_empty(isl_space_copy(dim));
2833 mm.may = isl_map_empty(dim);
2835 isl_flow_foreach(flow, collect_must_may, &mm);
2837 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2838 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2839 assert(map_is_equal(mm.may, str));
2840 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2841 assert(map_is_equal(mm.must, str));
2843 isl_map_free(mm.must);
2844 isl_map_free(mm.may);
2845 isl_flow_free(flow);
2848 depth = 5;
2850 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2851 map = isl_map_read_from_str(ctx, str);
2852 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2854 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2855 map = isl_map_read_from_str(ctx, str);
2856 ai = isl_access_info_add_source(ai, map, 1, &depth);
2858 flow = isl_access_info_compute_flow(ai);
2859 dim = isl_space_alloc(ctx, 0, 5, 5);
2860 mm.must = isl_map_empty(isl_space_copy(dim));
2861 mm.may = isl_map_empty(dim);
2863 isl_flow_foreach(flow, collect_must_may, &mm);
2865 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2866 assert(map_is_equal(mm.must, str));
2867 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2868 assert(map_is_equal(mm.may, str));
2870 isl_map_free(mm.must);
2871 isl_map_free(mm.may);
2872 isl_flow_free(flow);
2874 return 0;
2877 /* Check that the dependence analysis proceeds without errors.
2878 * Earlier versions of isl would break down during the analysis
2879 * due to the use of the wrong spaces.
2881 static int test_flow(isl_ctx *ctx)
2883 const char *str;
2884 isl_union_map *access, *schedule;
2885 isl_union_map *must_dep, *may_dep;
2886 int r;
2888 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2889 access = isl_union_map_read_from_str(ctx, str);
2890 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2891 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2892 "S2[] -> [1,0,0,0]; "
2893 "S3[] -> [-1,0,0,0] }";
2894 schedule = isl_union_map_read_from_str(ctx, str);
2895 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2896 isl_union_map_copy(access), schedule,
2897 &must_dep, &may_dep, NULL, NULL);
2898 isl_union_map_free(may_dep);
2899 isl_union_map_free(must_dep);
2901 return r;
2904 struct {
2905 const char *map;
2906 int sv;
2907 } sv_tests[] = {
2908 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2909 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2910 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2911 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2912 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2913 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2914 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2915 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2916 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2919 int test_sv(isl_ctx *ctx)
2921 isl_union_map *umap;
2922 int i;
2923 int sv;
2925 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2926 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2927 sv = isl_union_map_is_single_valued(umap);
2928 isl_union_map_free(umap);
2929 if (sv < 0)
2930 return -1;
2931 if (sv_tests[i].sv && !sv)
2932 isl_die(ctx, isl_error_internal,
2933 "map not detected as single valued", return -1);
2934 if (!sv_tests[i].sv && sv)
2935 isl_die(ctx, isl_error_internal,
2936 "map detected as single valued", return -1);
2939 return 0;
2942 struct {
2943 const char *str;
2944 int bijective;
2945 } bijective_tests[] = {
2946 { "[N,M]->{[i,j] -> [i]}", 0 },
2947 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
2948 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
2949 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
2950 { "[N,M]->{[i,j] -> [j,i]}", 1 },
2951 { "[N,M]->{[i,j] -> [i+j]}", 0 },
2952 { "[N,M]->{[i,j] -> []}", 0 },
2953 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
2954 { "[N,M]->{[i,j] -> [2i]}", 0 },
2955 { "[N,M]->{[i,j] -> [i,i]}", 0 },
2956 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
2957 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
2958 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
2961 static int test_bijective(struct isl_ctx *ctx)
2963 isl_map *map;
2964 int i;
2965 int bijective;
2967 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
2968 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
2969 bijective = isl_map_is_bijective(map);
2970 isl_map_free(map);
2971 if (bijective < 0)
2972 return -1;
2973 if (bijective_tests[i].bijective && !bijective)
2974 isl_die(ctx, isl_error_internal,
2975 "map not detected as bijective", return -1);
2976 if (!bijective_tests[i].bijective && bijective)
2977 isl_die(ctx, isl_error_internal,
2978 "map detected as bijective", return -1);
2981 return 0;
2984 /* Inputs for isl_pw_qpolynomial_gist tests.
2985 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2987 struct {
2988 const char *pwqp;
2989 const char *set;
2990 const char *gist;
2991 } pwqp_gist_tests[] = {
2992 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2993 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2994 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2995 "{ [i] -> -1/2 + 1/2 * i }" },
2996 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2999 static int test_pwqp(struct isl_ctx *ctx)
3001 int i;
3002 const char *str;
3003 isl_set *set;
3004 isl_pw_qpolynomial *pwqp1, *pwqp2;
3005 int equal;
3007 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3008 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3010 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
3011 isl_dim_in, 1, 1);
3013 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3014 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3016 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3018 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3020 isl_pw_qpolynomial_free(pwqp1);
3022 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
3023 str = pwqp_gist_tests[i].pwqp;
3024 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3025 str = pwqp_gist_tests[i].set;
3026 set = isl_set_read_from_str(ctx, str);
3027 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
3028 str = pwqp_gist_tests[i].gist;
3029 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3030 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3031 equal = isl_pw_qpolynomial_is_zero(pwqp1);
3032 isl_pw_qpolynomial_free(pwqp1);
3034 if (equal < 0)
3035 return -1;
3036 if (!equal)
3037 isl_die(ctx, isl_error_unknown,
3038 "unexpected result", return -1);
3041 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
3042 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3043 str = "{ [i] -> ([(2 * [i/2])/5]) }";
3044 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3046 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3048 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3050 isl_pw_qpolynomial_free(pwqp1);
3052 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
3053 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3054 str = "{ [x] -> x }";
3055 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3057 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3059 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3061 isl_pw_qpolynomial_free(pwqp1);
3063 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3064 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3065 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3066 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3067 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3068 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3069 isl_pw_qpolynomial_free(pwqp1);
3071 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3072 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3073 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3074 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3075 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3076 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3077 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3078 isl_pw_qpolynomial_free(pwqp1);
3079 isl_pw_qpolynomial_free(pwqp2);
3080 if (equal < 0)
3081 return -1;
3082 if (!equal)
3083 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3085 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3086 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3087 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3088 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3089 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3090 isl_val_one(ctx));
3091 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3092 isl_pw_qpolynomial_free(pwqp1);
3093 isl_pw_qpolynomial_free(pwqp2);
3094 if (equal < 0)
3095 return -1;
3096 if (!equal)
3097 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3099 return 0;
3102 static int test_split_periods(isl_ctx *ctx)
3104 const char *str;
3105 isl_pw_qpolynomial *pwqp;
3107 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3108 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3109 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3110 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3112 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3114 isl_pw_qpolynomial_free(pwqp);
3116 if (!pwqp)
3117 return -1;
3119 return 0;
3122 static int test_union(isl_ctx *ctx)
3124 const char *str;
3125 isl_union_set *uset1, *uset2;
3126 isl_union_map *umap1, *umap2;
3127 int equal;
3129 str = "{ [i] : 0 <= i <= 1 }";
3130 uset1 = isl_union_set_read_from_str(ctx, str);
3131 str = "{ [1] -> [0] }";
3132 umap1 = isl_union_map_read_from_str(ctx, str);
3134 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3135 equal = isl_union_map_is_equal(umap1, umap2);
3137 isl_union_map_free(umap1);
3138 isl_union_map_free(umap2);
3140 if (equal < 0)
3141 return -1;
3142 if (!equal)
3143 isl_die(ctx, isl_error_unknown, "union maps not equal",
3144 return -1);
3146 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3147 umap1 = isl_union_map_read_from_str(ctx, str);
3148 str = "{ A[i]; B[i] }";
3149 uset1 = isl_union_set_read_from_str(ctx, str);
3151 uset2 = isl_union_map_domain(umap1);
3153 equal = isl_union_set_is_equal(uset1, uset2);
3155 isl_union_set_free(uset1);
3156 isl_union_set_free(uset2);
3158 if (equal < 0)
3159 return -1;
3160 if (!equal)
3161 isl_die(ctx, isl_error_unknown, "union sets not equal",
3162 return -1);
3164 return 0;
3167 /* Check that computing a bound of a non-zero polynomial over an unbounded
3168 * domain does not produce a rational value.
3169 * In particular, check that the upper bound is infinity.
3171 static int test_bound_unbounded_domain(isl_ctx *ctx)
3173 const char *str;
3174 isl_pw_qpolynomial *pwqp;
3175 isl_pw_qpolynomial_fold *pwf, *pwf2;
3176 isl_bool equal;
3178 str = "{ [m,n] -> -m * n }";
3179 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3180 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3181 str = "{ infty }";
3182 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3183 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3184 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
3185 isl_pw_qpolynomial_fold_free(pwf);
3186 isl_pw_qpolynomial_fold_free(pwf2);
3188 if (equal < 0)
3189 return -1;
3190 if (!equal)
3191 isl_die(ctx, isl_error_unknown,
3192 "expecting infinite polynomial bound", return -1);
3194 return 0;
3197 static int test_bound(isl_ctx *ctx)
3199 const char *str;
3200 unsigned dim;
3201 isl_pw_qpolynomial *pwqp;
3202 isl_pw_qpolynomial_fold *pwf;
3204 if (test_bound_unbounded_domain(ctx) < 0)
3205 return -1;
3207 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
3208 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3209 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3210 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3211 isl_pw_qpolynomial_fold_free(pwf);
3212 if (dim != 4)
3213 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3214 return -1);
3216 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
3217 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3218 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3219 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3220 isl_pw_qpolynomial_fold_free(pwf);
3221 if (dim != 1)
3222 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3223 return -1);
3225 return 0;
3228 static int test_lift(isl_ctx *ctx)
3230 const char *str;
3231 isl_basic_map *bmap;
3232 isl_basic_set *bset;
3234 str = "{ [i0] : exists e0 : i0 = 4e0 }";
3235 bset = isl_basic_set_read_from_str(ctx, str);
3236 bset = isl_basic_set_lift(bset);
3237 bmap = isl_basic_map_from_range(bset);
3238 bset = isl_basic_map_domain(bmap);
3239 isl_basic_set_free(bset);
3241 return 0;
3244 struct {
3245 const char *set1;
3246 const char *set2;
3247 int subset;
3248 } subset_tests[] = {
3249 { "{ [112, 0] }",
3250 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
3251 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
3252 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
3253 { "{ [65] }",
3254 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
3255 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
3256 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
3257 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
3258 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
3259 "256e0 <= 255i and 256e0 >= -255 + 255i and "
3260 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
3261 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
3262 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
3263 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
3264 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
3265 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
3266 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
3267 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
3268 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
3269 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
3270 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
3271 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
3272 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
3273 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
3274 "4e0 <= -57 + i0 + i1)) or "
3275 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
3276 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
3277 "4e0 >= -61 + i0 + i1)) or "
3278 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
3279 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
3282 static int test_subset(isl_ctx *ctx)
3284 int i;
3285 isl_set *set1, *set2;
3286 int subset;
3288 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3289 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3290 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3291 subset = isl_set_is_subset(set1, set2);
3292 isl_set_free(set1);
3293 isl_set_free(set2);
3294 if (subset < 0)
3295 return -1;
3296 if (subset != subset_tests[i].subset)
3297 isl_die(ctx, isl_error_unknown,
3298 "incorrect subset result", return -1);
3301 return 0;
3304 struct {
3305 const char *minuend;
3306 const char *subtrahend;
3307 const char *difference;
3308 } subtract_domain_tests[] = {
3309 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3310 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3311 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3314 static int test_subtract(isl_ctx *ctx)
3316 int i;
3317 isl_union_map *umap1, *umap2;
3318 isl_union_pw_multi_aff *upma1, *upma2;
3319 isl_union_set *uset;
3320 int equal;
3322 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3323 umap1 = isl_union_map_read_from_str(ctx,
3324 subtract_domain_tests[i].minuend);
3325 uset = isl_union_set_read_from_str(ctx,
3326 subtract_domain_tests[i].subtrahend);
3327 umap2 = isl_union_map_read_from_str(ctx,
3328 subtract_domain_tests[i].difference);
3329 umap1 = isl_union_map_subtract_domain(umap1, uset);
3330 equal = isl_union_map_is_equal(umap1, umap2);
3331 isl_union_map_free(umap1);
3332 isl_union_map_free(umap2);
3333 if (equal < 0)
3334 return -1;
3335 if (!equal)
3336 isl_die(ctx, isl_error_unknown,
3337 "incorrect subtract domain result", return -1);
3340 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3341 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3342 subtract_domain_tests[i].minuend);
3343 uset = isl_union_set_read_from_str(ctx,
3344 subtract_domain_tests[i].subtrahend);
3345 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3346 subtract_domain_tests[i].difference);
3347 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3348 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3349 isl_union_pw_multi_aff_free(upma1);
3350 isl_union_pw_multi_aff_free(upma2);
3351 if (equal < 0)
3352 return -1;
3353 if (!equal)
3354 isl_die(ctx, isl_error_unknown,
3355 "incorrect subtract domain result", return -1);
3358 return 0;
3361 /* Check that intersecting the empty basic set with another basic set
3362 * does not increase the number of constraints. In particular,
3363 * the empty basic set should maintain its canonical representation.
3365 static int test_intersect(isl_ctx *ctx)
3367 int n1, n2;
3368 isl_basic_set *bset1, *bset2;
3370 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3371 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3372 n1 = isl_basic_set_n_constraint(bset1);
3373 bset1 = isl_basic_set_intersect(bset1, bset2);
3374 n2 = isl_basic_set_n_constraint(bset1);
3375 isl_basic_set_free(bset1);
3376 if (!bset1)
3377 return -1;
3378 if (n1 != n2)
3379 isl_die(ctx, isl_error_unknown,
3380 "number of constraints of empty set changed",
3381 return -1);
3383 return 0;
3386 int test_factorize(isl_ctx *ctx)
3388 const char *str;
3389 isl_basic_set *bset;
3390 isl_factorizer *f;
3392 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3393 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3394 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3395 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3396 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3397 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3398 "3i5 >= -2i0 - i2 + 3i4 }";
3399 bset = isl_basic_set_read_from_str(ctx, str);
3400 f = isl_basic_set_factorizer(bset);
3401 isl_basic_set_free(bset);
3402 isl_factorizer_free(f);
3403 if (!f)
3404 isl_die(ctx, isl_error_unknown,
3405 "failed to construct factorizer", return -1);
3407 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3408 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3409 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3410 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3411 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3412 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3413 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3414 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3415 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3416 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3417 bset = isl_basic_set_read_from_str(ctx, str);
3418 f = isl_basic_set_factorizer(bset);
3419 isl_basic_set_free(bset);
3420 isl_factorizer_free(f);
3421 if (!f)
3422 isl_die(ctx, isl_error_unknown,
3423 "failed to construct factorizer", return -1);
3425 return 0;
3428 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3430 int *injective = user;
3432 *injective = isl_map_is_injective(map);
3433 isl_map_free(map);
3435 if (*injective < 0 || !*injective)
3436 return isl_stat_error;
3438 return isl_stat_ok;
3441 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3442 const char *r, const char *s, int tilable, int parallel)
3444 int i;
3445 isl_union_set *D;
3446 isl_union_map *W, *R, *S;
3447 isl_union_map *empty;
3448 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3449 isl_union_map *validity, *proximity, *coincidence;
3450 isl_union_map *schedule;
3451 isl_union_map *test;
3452 isl_union_set *delta;
3453 isl_union_set *domain;
3454 isl_set *delta_set;
3455 isl_set *slice;
3456 isl_set *origin;
3457 isl_schedule_constraints *sc;
3458 isl_schedule *sched;
3459 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3461 D = isl_union_set_read_from_str(ctx, d);
3462 W = isl_union_map_read_from_str(ctx, w);
3463 R = isl_union_map_read_from_str(ctx, r);
3464 S = isl_union_map_read_from_str(ctx, s);
3466 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3467 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3469 empty = isl_union_map_empty(isl_union_map_get_space(S));
3470 isl_union_map_compute_flow(isl_union_map_copy(R),
3471 isl_union_map_copy(W), empty,
3472 isl_union_map_copy(S),
3473 &dep_raw, NULL, NULL, NULL);
3474 isl_union_map_compute_flow(isl_union_map_copy(W),
3475 isl_union_map_copy(W),
3476 isl_union_map_copy(R),
3477 isl_union_map_copy(S),
3478 &dep_waw, &dep_war, NULL, NULL);
3480 dep = isl_union_map_union(dep_waw, dep_war);
3481 dep = isl_union_map_union(dep, dep_raw);
3482 validity = isl_union_map_copy(dep);
3483 coincidence = isl_union_map_copy(dep);
3484 proximity = isl_union_map_copy(dep);
3486 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3487 sc = isl_schedule_constraints_set_validity(sc, validity);
3488 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3489 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3490 sched = isl_schedule_constraints_compute_schedule(sc);
3491 schedule = isl_schedule_get_map(sched);
3492 isl_schedule_free(sched);
3493 isl_union_map_free(W);
3494 isl_union_map_free(R);
3495 isl_union_map_free(S);
3497 is_injection = 1;
3498 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3500 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3501 is_complete = isl_union_set_is_subset(D, domain);
3502 isl_union_set_free(D);
3503 isl_union_set_free(domain);
3505 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3506 test = isl_union_map_apply_range(test, dep);
3507 test = isl_union_map_apply_range(test, schedule);
3509 delta = isl_union_map_deltas(test);
3510 if (isl_union_set_n_set(delta) == 0) {
3511 is_tilable = 1;
3512 is_parallel = 1;
3513 is_nonneg = 1;
3514 isl_union_set_free(delta);
3515 } else {
3516 delta_set = isl_set_from_union_set(delta);
3518 slice = isl_set_universe(isl_set_get_space(delta_set));
3519 for (i = 0; i < tilable; ++i)
3520 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3521 is_tilable = isl_set_is_subset(delta_set, slice);
3522 isl_set_free(slice);
3524 slice = isl_set_universe(isl_set_get_space(delta_set));
3525 for (i = 0; i < parallel; ++i)
3526 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3527 is_parallel = isl_set_is_subset(delta_set, slice);
3528 isl_set_free(slice);
3530 origin = isl_set_universe(isl_set_get_space(delta_set));
3531 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
3532 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3534 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3535 delta_set = isl_set_lexmin(delta_set);
3537 is_nonneg = isl_set_is_equal(delta_set, origin);
3539 isl_set_free(origin);
3540 isl_set_free(delta_set);
3543 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3544 is_injection < 0 || is_complete < 0)
3545 return -1;
3546 if (!is_complete)
3547 isl_die(ctx, isl_error_unknown,
3548 "generated schedule incomplete", return -1);
3549 if (!is_injection)
3550 isl_die(ctx, isl_error_unknown,
3551 "generated schedule not injective on each statement",
3552 return -1);
3553 if (!is_nonneg)
3554 isl_die(ctx, isl_error_unknown,
3555 "negative dependences in generated schedule",
3556 return -1);
3557 if (!is_tilable)
3558 isl_die(ctx, isl_error_unknown,
3559 "generated schedule not as tilable as expected",
3560 return -1);
3561 if (!is_parallel)
3562 isl_die(ctx, isl_error_unknown,
3563 "generated schedule not as parallel as expected",
3564 return -1);
3566 return 0;
3569 /* Compute a schedule for the given instance set, validity constraints,
3570 * proximity constraints and context and return a corresponding union map
3571 * representation.
3573 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3574 const char *domain, const char *validity, const char *proximity,
3575 const char *context)
3577 isl_set *con;
3578 isl_union_set *dom;
3579 isl_union_map *dep;
3580 isl_union_map *prox;
3581 isl_schedule_constraints *sc;
3582 isl_schedule *schedule;
3583 isl_union_map *sched;
3585 con = isl_set_read_from_str(ctx, context);
3586 dom = isl_union_set_read_from_str(ctx, domain);
3587 dep = isl_union_map_read_from_str(ctx, validity);
3588 prox = isl_union_map_read_from_str(ctx, proximity);
3589 sc = isl_schedule_constraints_on_domain(dom);
3590 sc = isl_schedule_constraints_set_context(sc, con);
3591 sc = isl_schedule_constraints_set_validity(sc, dep);
3592 sc = isl_schedule_constraints_set_proximity(sc, prox);
3593 schedule = isl_schedule_constraints_compute_schedule(sc);
3594 sched = isl_schedule_get_map(schedule);
3595 isl_schedule_free(schedule);
3597 return sched;
3600 /* Compute a schedule for the given instance set, validity constraints and
3601 * proximity constraints and return a corresponding union map representation.
3603 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
3604 const char *domain, const char *validity, const char *proximity)
3606 return compute_schedule_with_context(ctx, domain, validity, proximity,
3607 "{ : }");
3610 /* Check that a schedule can be constructed on the given domain
3611 * with the given validity and proximity constraints.
3613 static int test_has_schedule(isl_ctx *ctx, const char *domain,
3614 const char *validity, const char *proximity)
3616 isl_union_map *sched;
3618 sched = compute_schedule(ctx, domain, validity, proximity);
3619 if (!sched)
3620 return -1;
3622 isl_union_map_free(sched);
3623 return 0;
3626 int test_special_schedule(isl_ctx *ctx, const char *domain,
3627 const char *validity, const char *proximity, const char *expected_sched)
3629 isl_union_map *sched1, *sched2;
3630 int equal;
3632 sched1 = compute_schedule(ctx, domain, validity, proximity);
3633 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
3635 equal = isl_union_map_is_equal(sched1, sched2);
3636 isl_union_map_free(sched1);
3637 isl_union_map_free(sched2);
3639 if (equal < 0)
3640 return -1;
3641 if (!equal)
3642 isl_die(ctx, isl_error_unknown, "unexpected schedule",
3643 return -1);
3645 return 0;
3648 /* Check that the schedule map is properly padded, even after being
3649 * reconstructed from the band forest.
3651 static int test_padded_schedule(isl_ctx *ctx)
3653 const char *str;
3654 isl_union_set *D;
3655 isl_union_map *validity, *proximity;
3656 isl_schedule_constraints *sc;
3657 isl_schedule *sched;
3658 isl_union_map *map1, *map2;
3659 isl_band_list *list;
3660 int equal;
3662 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
3663 D = isl_union_set_read_from_str(ctx, str);
3664 validity = isl_union_map_empty(isl_union_set_get_space(D));
3665 proximity = isl_union_map_copy(validity);
3666 sc = isl_schedule_constraints_on_domain(D);
3667 sc = isl_schedule_constraints_set_validity(sc, validity);
3668 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3669 sched = isl_schedule_constraints_compute_schedule(sc);
3670 map1 = isl_schedule_get_map(sched);
3671 list = isl_schedule_get_band_forest(sched);
3672 isl_band_list_free(list);
3673 map2 = isl_schedule_get_map(sched);
3674 isl_schedule_free(sched);
3675 equal = isl_union_map_is_equal(map1, map2);
3676 isl_union_map_free(map1);
3677 isl_union_map_free(map2);
3679 if (equal < 0)
3680 return -1;
3681 if (!equal)
3682 isl_die(ctx, isl_error_unknown,
3683 "reconstructed schedule map not the same as original",
3684 return -1);
3686 return 0;
3689 /* Check that conditional validity constraints are also taken into
3690 * account across bands.
3691 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
3692 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
3693 * and then check that the adjacent order constraint C[2,1]->D[2,0]
3694 * is enforced by the rest of the schedule.
3696 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
3698 const char *str;
3699 isl_union_set *domain;
3700 isl_union_map *validity, *proximity, *condition;
3701 isl_union_map *sink, *source, *dep;
3702 isl_schedule_constraints *sc;
3703 isl_schedule *schedule;
3704 isl_union_access_info *access;
3705 isl_union_flow *flow;
3706 int empty;
3708 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3709 "A[k] : k >= 1 and k <= -1 + n; "
3710 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3711 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
3712 domain = isl_union_set_read_from_str(ctx, str);
3713 sc = isl_schedule_constraints_on_domain(domain);
3714 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
3715 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3716 "D[k, i] -> C[1 + k, i] : "
3717 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3718 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
3719 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
3720 validity = isl_union_map_read_from_str(ctx, str);
3721 sc = isl_schedule_constraints_set_validity(sc, validity);
3722 str = "[n] -> { C[k, i] -> D[k, i] : "
3723 "0 <= i <= -1 + k and k <= -1 + n }";
3724 proximity = isl_union_map_read_from_str(ctx, str);
3725 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3726 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
3727 "i <= -1 + k and i >= 1 and k <= -2 + n; "
3728 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
3729 "k <= -1 + n and i >= 0 and i <= -2 + k }";
3730 condition = isl_union_map_read_from_str(ctx, str);
3731 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
3732 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3733 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
3734 "i >= 0 and i <= -1 + k and k <= -1 + n and "
3735 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
3736 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
3737 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3738 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
3739 "k <= -1 + n and i >= 0 and i <= -1 + k and "
3740 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
3741 validity = isl_union_map_read_from_str(ctx, str);
3742 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3743 validity);
3744 schedule = isl_schedule_constraints_compute_schedule(sc);
3745 str = "{ D[2,0] -> [] }";
3746 sink = isl_union_map_read_from_str(ctx, str);
3747 access = isl_union_access_info_from_sink(sink);
3748 str = "{ C[2,1] -> [] }";
3749 source = isl_union_map_read_from_str(ctx, str);
3750 access = isl_union_access_info_set_must_source(access, source);
3751 access = isl_union_access_info_set_schedule(access, schedule);
3752 flow = isl_union_access_info_compute_flow(access);
3753 dep = isl_union_flow_get_must_dependence(flow);
3754 isl_union_flow_free(flow);
3755 empty = isl_union_map_is_empty(dep);
3756 isl_union_map_free(dep);
3758 if (empty < 0)
3759 return -1;
3760 if (empty)
3761 isl_die(ctx, isl_error_unknown,
3762 "conditional validity not respected", return -1);
3764 return 0;
3767 /* Check that the test for violated conditional validity constraints
3768 * is not confused by domain compression.
3769 * In particular, earlier versions of isl would apply
3770 * a schedule on the compressed domains to the original domains,
3771 * resulting in a failure to detect that the default schedule
3772 * violates the conditional validity constraints.
3774 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
3776 const char *str;
3777 isl_bool empty;
3778 isl_union_set *domain;
3779 isl_union_map *validity, *condition;
3780 isl_schedule_constraints *sc;
3781 isl_schedule *schedule;
3782 isl_union_map *umap;
3783 isl_map *map, *ge;
3785 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
3786 domain = isl_union_set_read_from_str(ctx, str);
3787 sc = isl_schedule_constraints_on_domain(domain);
3788 str = "{ B[1, i] -> A[0, i + 1] }";
3789 condition = isl_union_map_read_from_str(ctx, str);
3790 str = "{ A[0, i] -> B[1, i - 1] }";
3791 validity = isl_union_map_read_from_str(ctx, str);
3792 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3793 isl_union_map_copy(validity));
3794 schedule = isl_schedule_constraints_compute_schedule(sc);
3795 umap = isl_schedule_get_map(schedule);
3796 isl_schedule_free(schedule);
3797 validity = isl_union_map_apply_domain(validity,
3798 isl_union_map_copy(umap));
3799 validity = isl_union_map_apply_range(validity, umap);
3800 map = isl_map_from_union_map(validity);
3801 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3802 map = isl_map_intersect(map, ge);
3803 empty = isl_map_is_empty(map);
3804 isl_map_free(map);
3806 if (empty < 0)
3807 return -1;
3808 if (!empty)
3809 isl_die(ctx, isl_error_unknown,
3810 "conditional validity constraints not satisfied",
3811 return -1);
3813 return 0;
3816 /* Input for testing of schedule construction based on
3817 * conditional constraints.
3819 * domain is the iteration domain
3820 * flow are the flow dependences, which determine the validity and
3821 * proximity constraints
3822 * condition are the conditions on the conditional validity constraints
3823 * conditional_validity are the conditional validity constraints
3824 * outer_band_n is the expected number of members in the outer band
3826 struct {
3827 const char *domain;
3828 const char *flow;
3829 const char *condition;
3830 const char *conditional_validity;
3831 int outer_band_n;
3832 } live_range_tests[] = {
3833 /* Contrived example that illustrates that we need to keep
3834 * track of tagged condition dependences and
3835 * tagged conditional validity dependences
3836 * in isl_sched_edge separately.
3837 * In particular, the conditional validity constraints on A
3838 * cannot be satisfied,
3839 * but they can be ignored because there are no corresponding
3840 * condition constraints. However, we do have an additional
3841 * conditional validity constraint that maps to the same
3842 * dependence relation
3843 * as the condition constraint on B. If we did not make a distinction
3844 * between tagged condition and tagged conditional validity
3845 * dependences, then we
3846 * could end up treating this shared dependence as an condition
3847 * constraint on A, forcing a localization of the conditions,
3848 * which is impossible.
3850 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
3851 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
3852 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
3853 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3854 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3855 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
3858 /* TACO 2013 Fig. 7 */
3859 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3860 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3861 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3862 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
3863 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
3864 "0 <= i < n and 0 <= j < n - 1 }",
3865 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
3866 "0 <= i < n and 0 <= j < j' < n;"
3867 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
3868 "0 <= i < i' < n and 0 <= j,j' < n;"
3869 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
3870 "0 <= i,j,j' < n and j < j' }",
3873 /* TACO 2013 Fig. 7, without tags */
3874 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3875 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3876 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3877 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3878 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3879 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
3880 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
3881 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
3884 /* TACO 2013 Fig. 12 */
3885 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
3886 "S3[i,3] : 0 <= i <= 1 }",
3887 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
3888 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
3889 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
3890 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
3891 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3892 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
3893 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3894 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
3895 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
3896 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
3897 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
3902 /* Test schedule construction based on conditional constraints.
3903 * In particular, check the number of members in the outer band node
3904 * as an indication of whether tiling is possible or not.
3906 static int test_conditional_schedule_constraints(isl_ctx *ctx)
3908 int i;
3909 isl_union_set *domain;
3910 isl_union_map *condition;
3911 isl_union_map *flow;
3912 isl_union_map *validity;
3913 isl_schedule_constraints *sc;
3914 isl_schedule *schedule;
3915 isl_schedule_node *node;
3916 int n_member;
3918 if (test_special_conditional_schedule_constraints(ctx) < 0)
3919 return -1;
3920 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
3921 return -1;
3923 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
3924 domain = isl_union_set_read_from_str(ctx,
3925 live_range_tests[i].domain);
3926 flow = isl_union_map_read_from_str(ctx,
3927 live_range_tests[i].flow);
3928 condition = isl_union_map_read_from_str(ctx,
3929 live_range_tests[i].condition);
3930 validity = isl_union_map_read_from_str(ctx,
3931 live_range_tests[i].conditional_validity);
3932 sc = isl_schedule_constraints_on_domain(domain);
3933 sc = isl_schedule_constraints_set_validity(sc,
3934 isl_union_map_copy(flow));
3935 sc = isl_schedule_constraints_set_proximity(sc, flow);
3936 sc = isl_schedule_constraints_set_conditional_validity(sc,
3937 condition, validity);
3938 schedule = isl_schedule_constraints_compute_schedule(sc);
3939 node = isl_schedule_get_root(schedule);
3940 while (node &&
3941 isl_schedule_node_get_type(node) != isl_schedule_node_band)
3942 node = isl_schedule_node_first_child(node);
3943 n_member = isl_schedule_node_band_n_member(node);
3944 isl_schedule_node_free(node);
3945 isl_schedule_free(schedule);
3947 if (!schedule)
3948 return -1;
3949 if (n_member != live_range_tests[i].outer_band_n)
3950 isl_die(ctx, isl_error_unknown,
3951 "unexpected number of members in outer band",
3952 return -1);
3954 return 0;
3957 /* Check that the schedule computed for the given instance set and
3958 * dependence relation strongly satisfies the dependences.
3959 * In particular, check that no instance is scheduled before
3960 * or together with an instance on which it depends.
3961 * Earlier versions of isl would produce a schedule that
3962 * only weakly satisfies the dependences.
3964 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
3966 const char *domain, *dep;
3967 isl_union_map *D, *schedule;
3968 isl_map *map, *ge;
3969 int empty;
3971 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
3972 "A[i0] : 0 <= i0 <= 1 }";
3973 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
3974 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
3975 schedule = compute_schedule(ctx, domain, dep, dep);
3976 D = isl_union_map_read_from_str(ctx, dep);
3977 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
3978 D = isl_union_map_apply_range(D, schedule);
3979 map = isl_map_from_union_map(D);
3980 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3981 map = isl_map_intersect(map, ge);
3982 empty = isl_map_is_empty(map);
3983 isl_map_free(map);
3985 if (empty < 0)
3986 return -1;
3987 if (!empty)
3988 isl_die(ctx, isl_error_unknown,
3989 "dependences not strongly satisfied", return -1);
3991 return 0;
3994 /* Compute a schedule for input where the instance set constraints
3995 * conflict with the context constraints.
3996 * Earlier versions of isl did not properly handle this situation.
3998 static int test_conflicting_context_schedule(isl_ctx *ctx)
4000 isl_union_map *schedule;
4001 const char *domain, *context;
4003 domain = "[n] -> { A[] : n >= 0 }";
4004 context = "[n] -> { : n < 0 }";
4005 schedule = compute_schedule_with_context(ctx,
4006 domain, "{}", "{}", context);
4007 isl_union_map_free(schedule);
4009 if (!schedule)
4010 return -1;
4012 return 0;
4015 /* Check that the dependence carrying step is not confused by
4016 * a bound on the coefficient size.
4017 * In particular, force the scheduler to move to a dependence carrying
4018 * step by demanding outer coincidence and bound the size of
4019 * the coefficients. Earlier versions of isl would take this
4020 * bound into account while carrying dependences, breaking
4021 * fundamental assumptions.
4022 * On the other hand, the dependence carrying step now tries
4023 * to prevent loop coalescing by default, so check that indeed
4024 * no loop coalescing occurs by comparing the computed schedule
4025 * to the expected non-coalescing schedule.
4027 static int test_bounded_coefficients_schedule(isl_ctx *ctx)
4029 const char *domain, *dep;
4030 isl_union_set *I;
4031 isl_union_map *D;
4032 isl_schedule_constraints *sc;
4033 isl_schedule *schedule;
4034 isl_union_map *sched1, *sched2;
4035 isl_bool equal;
4037 domain = "{ C[i0, i1] : 2 <= i0 <= 3999 and 0 <= i1 <= -1 + i0 }";
4038 dep = "{ C[i0, i1] -> C[i0, 1 + i1] : i0 <= 3999 and i1 >= 0 and "
4039 "i1 <= -2 + i0; "
4040 "C[i0, -1 + i0] -> C[1 + i0, 0] : i0 <= 3998 and i0 >= 1 }";
4041 I = isl_union_set_read_from_str(ctx, domain);
4042 D = isl_union_map_read_from_str(ctx, dep);
4043 sc = isl_schedule_constraints_on_domain(I);
4044 sc = isl_schedule_constraints_set_validity(sc, isl_union_map_copy(D));
4045 sc = isl_schedule_constraints_set_coincidence(sc, D);
4046 isl_options_set_schedule_outer_coincidence(ctx, 1);
4047 isl_options_set_schedule_max_coefficient(ctx, 20);
4048 schedule = isl_schedule_constraints_compute_schedule(sc);
4049 isl_options_set_schedule_max_coefficient(ctx, -1);
4050 isl_options_set_schedule_outer_coincidence(ctx, 0);
4051 sched1 = isl_schedule_get_map(schedule);
4052 isl_schedule_free(schedule);
4054 sched2 = isl_union_map_read_from_str(ctx, "{ C[x,y] -> [x,y] }");
4055 equal = isl_union_map_is_equal(sched1, sched2);
4056 isl_union_map_free(sched1);
4057 isl_union_map_free(sched2);
4059 if (equal < 0)
4060 return -1;
4061 if (!equal)
4062 isl_die(ctx, isl_error_unknown,
4063 "unexpected schedule", return -1);
4065 return 0;
4068 /* Check that the bounds on the coefficients are respected.
4069 * This function checks for a particular output schedule,
4070 * but the exact output is not important, only that it does
4071 * not contain any coefficients greater than 4.
4072 * It is, however, easier to check for a particular output.
4073 * This test is only run for the whole component scheduler
4074 * because the incremental scheduler produces a slightly different schedule.
4076 static int test_bounded_coefficients_schedule_whole(isl_ctx *ctx)
4078 const char *domain, *dep, *str;
4079 isl_union_set *I;
4080 isl_union_map *D;
4081 isl_schedule_constraints *sc;
4082 isl_schedule *schedule;
4083 isl_union_map *sched1, *sched2;
4084 isl_bool equal;
4086 domain = "{ S_4[i, j, k] : 0 <= i < j <= 10 and 0 <= k <= 100; "
4087 "S_2[i, j] : 0 <= i < j <= 10; S_6[i, j] : 0 <= i < j <= 10 }";
4088 dep = "{ S_2[0, j] -> S_4[0, j, 0] : 0 < j <= 10; "
4089 "S_4[0, j, 100] -> S_6[0, j] : 0 < j <= 10 }";
4090 I = isl_union_set_read_from_str(ctx, domain);
4091 D = isl_union_map_read_from_str(ctx, dep);
4092 sc = isl_schedule_constraints_on_domain(I);
4093 sc = isl_schedule_constraints_set_validity(sc, D);
4094 isl_options_set_schedule_max_constant_term(ctx, 10);
4095 isl_options_set_schedule_max_coefficient(ctx, 4);
4096 schedule = isl_schedule_constraints_compute_schedule(sc);
4097 isl_options_set_schedule_max_coefficient(ctx, -1);
4098 isl_options_set_schedule_max_constant_term(ctx, -1);
4099 sched1 = isl_schedule_get_map(schedule);
4100 isl_schedule_free(schedule);
4102 str = "{ S_4[i, j, k] -> [i, j, 10 - k]; "
4103 "S_2[i, j] -> [0, i, j]; S_6[i, j] -> [0, 10 + i, j] }";
4104 sched2 = isl_union_map_read_from_str(ctx, str);
4105 equal = isl_union_map_is_equal(sched1, sched2);
4106 isl_union_map_free(sched1);
4107 isl_union_map_free(sched2);
4109 if (equal < 0)
4110 return -1;
4111 if (!equal)
4112 isl_die(ctx, isl_error_unknown,
4113 "unexpected schedule", return -1);
4115 return 0;
4118 /* Check that a set of schedule constraints that only allow for
4119 * a coalescing schedule still produces a schedule even if the user
4120 * request a non-coalescing schedule. Earlier versions of isl
4121 * would not handle this case correctly.
4123 static int test_coalescing_schedule(isl_ctx *ctx)
4125 const char *domain, *dep;
4126 isl_union_set *I;
4127 isl_union_map *D;
4128 isl_schedule_constraints *sc;
4129 isl_schedule *schedule;
4130 int treat_coalescing;
4132 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
4133 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
4134 I = isl_union_set_read_from_str(ctx, domain);
4135 D = isl_union_map_read_from_str(ctx, dep);
4136 sc = isl_schedule_constraints_on_domain(I);
4137 sc = isl_schedule_constraints_set_validity(sc, D);
4138 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4139 isl_options_set_schedule_treat_coalescing(ctx, 1);
4140 schedule = isl_schedule_constraints_compute_schedule(sc);
4141 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4142 isl_schedule_free(schedule);
4143 if (!schedule)
4144 return -1;
4145 return 0;
4148 /* Check that the scheduler does not perform any needless
4149 * compound skewing. Earlier versions of isl would compute
4150 * schedules in terms of transformed schedule coefficients and
4151 * would not accurately keep track of the sum of the original
4152 * schedule coefficients. It could then produce the schedule
4153 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
4154 * for the input below instead of the schedule below.
4156 static int test_skewing_schedule(isl_ctx *ctx)
4158 const char *D, *V, *P, *S;
4160 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
4161 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
4162 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
4163 "-1 <= a-i + b-j + c-k <= 1 }";
4164 P = "{ }";
4165 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
4167 return test_special_schedule(ctx, D, V, P, S);
4170 int test_schedule(isl_ctx *ctx)
4172 const char *D, *W, *R, *V, *P, *S;
4173 int max_coincidence;
4174 int treat_coalescing;
4176 /* Handle resulting schedule with zero bands. */
4177 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
4178 return -1;
4180 /* Jacobi */
4181 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
4182 W = "{ S1[t,i] -> a[t,i] }";
4183 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
4184 "S1[t,i] -> a[t-1,i+1] }";
4185 S = "{ S1[t,i] -> [t,i] }";
4186 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4187 return -1;
4189 /* Fig. 5 of CC2008 */
4190 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
4191 "j <= -1 + N }";
4192 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
4193 "j >= 2 and j <= -1 + N }";
4194 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
4195 "j >= 2 and j <= -1 + N; "
4196 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
4197 "j >= 2 and j <= -1 + N }";
4198 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4199 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4200 return -1;
4202 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
4203 W = "{ S1[i] -> a[i] }";
4204 R = "{ S2[i] -> a[i+1] }";
4205 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4206 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4207 return -1;
4209 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
4210 W = "{ S1[i] -> a[i] }";
4211 R = "{ S2[i] -> a[9-i] }";
4212 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4213 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4214 return -1;
4216 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
4217 W = "{ S1[i] -> a[i] }";
4218 R = "[N] -> { S2[i] -> a[N-1-i] }";
4219 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4220 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4221 return -1;
4223 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
4224 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
4225 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
4226 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
4227 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4228 return -1;
4230 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4231 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
4232 R = "{ S2[i,j] -> a[i-1,j] }";
4233 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4234 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4235 return -1;
4237 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4238 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
4239 R = "{ S2[i,j] -> a[i,j-1] }";
4240 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4241 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4242 return -1;
4244 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
4245 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
4246 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
4247 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
4248 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
4249 "S_0[] -> [0, 0, 0] }";
4250 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
4251 return -1;
4252 ctx->opt->schedule_parametric = 0;
4253 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4254 return -1;
4255 ctx->opt->schedule_parametric = 1;
4257 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
4258 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
4259 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
4260 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
4261 "S4[i] -> a[i,N] }";
4262 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
4263 "S4[i] -> [4,i,0] }";
4264 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
4265 isl_options_set_schedule_maximize_coincidence(ctx, 0);
4266 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4267 return -1;
4268 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
4270 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
4271 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4272 "j <= N }";
4273 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4274 "j <= N; "
4275 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
4276 "j <= N }";
4277 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4278 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4279 return -1;
4281 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
4282 " S_2[t] : t >= 0 and t <= -1 + N; "
4283 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
4284 "i <= -1 + N }";
4285 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
4286 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
4287 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
4288 "i >= 0 and i <= -1 + N }";
4289 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
4290 "i >= 0 and i <= -1 + N; "
4291 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
4292 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
4293 " S_0[t] -> [0, t, 0] }";
4295 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4296 return -1;
4297 ctx->opt->schedule_parametric = 0;
4298 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4299 return -1;
4300 ctx->opt->schedule_parametric = 1;
4302 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
4303 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
4304 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
4305 return -1;
4307 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
4308 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
4309 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
4310 "j >= 0 and j <= -1 + N; "
4311 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4312 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
4313 "j >= 0 and j <= -1 + N; "
4314 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4315 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
4316 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4317 return -1;
4319 D = "{ S_0[i] : i >= 0 }";
4320 W = "{ S_0[i] -> a[i] : i >= 0 }";
4321 R = "{ S_0[i] -> a[0] : i >= 0 }";
4322 S = "{ S_0[i] -> [0, i, 0] }";
4323 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4324 return -1;
4326 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
4327 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
4328 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
4329 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
4330 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4331 return -1;
4333 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
4334 "k <= -1 + n and k >= 0 }";
4335 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
4336 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
4337 "k <= -1 + n and k >= 0; "
4338 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
4339 "k <= -1 + n and k >= 0; "
4340 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
4341 "k <= -1 + n and k >= 0 }";
4342 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
4343 ctx->opt->schedule_outer_coincidence = 1;
4344 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4345 return -1;
4346 ctx->opt->schedule_outer_coincidence = 0;
4348 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
4349 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
4350 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
4351 "Stmt_for_body24[i0, i1, 1, 0]:"
4352 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
4353 "Stmt_for_body7[i0, i1, i2]:"
4354 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
4355 "i2 <= 7 }";
4357 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
4358 "Stmt_for_body24[1, i1, i2, i3]:"
4359 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
4360 "i2 >= 1;"
4361 "Stmt_for_body24[0, i1, i2, i3] -> "
4362 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
4363 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
4364 "i3 >= 0;"
4365 "Stmt_for_body24[0, i1, i2, i3] ->"
4366 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4367 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4368 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4369 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4370 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4371 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4372 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4373 "i1 <= 6 and i1 >= 0;"
4374 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4375 "Stmt_for_body7[i0, i1, i2] -> "
4376 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4377 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4378 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4379 "Stmt_for_body7[i0, i1, i2] -> "
4380 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4381 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4382 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4383 P = V;
4385 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4386 isl_options_set_schedule_treat_coalescing(ctx, 0);
4387 if (test_has_schedule(ctx, D, V, P) < 0)
4388 return -1;
4389 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4391 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4392 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4393 "j >= 1 and j <= 7;"
4394 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4395 "j >= 1 and j <= 8 }";
4396 P = "{ }";
4397 S = "{ S_0[i, j] -> [i + j, i] }";
4398 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4399 if (test_special_schedule(ctx, D, V, P, S) < 0)
4400 return -1;
4401 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4403 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4404 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4405 "j >= 0 and j <= -1 + i }";
4406 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4407 "i <= -1 + N and j >= 0;"
4408 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4409 "i <= -2 + N }";
4410 P = "{ }";
4411 S = "{ S_0[i, j] -> [i, j] }";
4412 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4413 if (test_special_schedule(ctx, D, V, P, S) < 0)
4414 return -1;
4415 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4417 /* Test both algorithms on a case with only proximity dependences. */
4418 D = "{ S[i,j] : 0 <= i <= 10 }";
4419 V = "{ }";
4420 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4421 S = "{ S[i, j] -> [j, i] }";
4422 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4423 if (test_special_schedule(ctx, D, V, P, S) < 0)
4424 return -1;
4425 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4426 if (test_special_schedule(ctx, D, V, P, S) < 0)
4427 return -1;
4429 D = "{ A[a]; B[] }";
4430 V = "{}";
4431 P = "{ A[a] -> B[] }";
4432 if (test_has_schedule(ctx, D, V, P) < 0)
4433 return -1;
4435 if (test_padded_schedule(ctx) < 0)
4436 return -1;
4438 /* Check that check for progress is not confused by rational
4439 * solution.
4441 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4442 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4443 "i0 <= -2 + N; "
4444 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4445 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4446 P = "{}";
4447 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4448 if (test_has_schedule(ctx, D, V, P) < 0)
4449 return -1;
4450 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4452 /* Check that we allow schedule rows that are only non-trivial
4453 * on some full-dimensional domains.
4455 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4456 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4457 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4458 P = "{}";
4459 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4460 if (test_has_schedule(ctx, D, V, P) < 0)
4461 return -1;
4462 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4464 if (test_conditional_schedule_constraints(ctx) < 0)
4465 return -1;
4467 if (test_strongly_satisfying_schedule(ctx) < 0)
4468 return -1;
4470 if (test_conflicting_context_schedule(ctx) < 0)
4471 return -1;
4473 if (test_bounded_coefficients_schedule(ctx) < 0)
4474 return -1;
4475 if (test_coalescing_schedule(ctx) < 0)
4476 return -1;
4477 if (test_skewing_schedule(ctx) < 0)
4478 return -1;
4480 return 0;
4483 /* Perform scheduling tests using the whole component scheduler.
4485 static int test_schedule_whole(isl_ctx *ctx)
4487 int whole;
4488 int r;
4490 whole = isl_options_get_schedule_whole_component(ctx);
4491 isl_options_set_schedule_whole_component(ctx, 1);
4492 r = test_schedule(ctx);
4493 if (r >= 0)
4494 r = test_bounded_coefficients_schedule_whole(ctx);
4495 isl_options_set_schedule_whole_component(ctx, whole);
4497 return r;
4500 /* Perform scheduling tests using the incremental scheduler.
4502 static int test_schedule_incremental(isl_ctx *ctx)
4504 int whole;
4505 int r;
4507 whole = isl_options_get_schedule_whole_component(ctx);
4508 isl_options_set_schedule_whole_component(ctx, 0);
4509 r = test_schedule(ctx);
4510 isl_options_set_schedule_whole_component(ctx, whole);
4512 return r;
4515 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4517 isl_union_map *umap;
4518 int test;
4520 umap = isl_union_map_read_from_str(ctx, str);
4521 test = isl_union_map_plain_is_injective(umap);
4522 isl_union_map_free(umap);
4523 if (test < 0)
4524 return -1;
4525 if (test == injective)
4526 return 0;
4527 if (injective)
4528 isl_die(ctx, isl_error_unknown,
4529 "map not detected as injective", return -1);
4530 else
4531 isl_die(ctx, isl_error_unknown,
4532 "map detected as injective", return -1);
4535 int test_injective(isl_ctx *ctx)
4537 const char *str;
4539 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4540 return -1;
4541 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4542 return -1;
4543 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4544 return -1;
4545 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4546 return -1;
4547 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4548 return -1;
4549 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4550 return -1;
4551 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4552 return -1;
4553 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4554 return -1;
4556 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4557 if (test_plain_injective(ctx, str, 1))
4558 return -1;
4559 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4560 if (test_plain_injective(ctx, str, 0))
4561 return -1;
4563 return 0;
4566 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4568 isl_aff *aff2;
4569 int equal;
4571 if (!aff)
4572 return -1;
4574 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4575 equal = isl_aff_plain_is_equal(aff, aff2);
4576 isl_aff_free(aff2);
4578 return equal;
4581 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4583 int equal;
4585 equal = aff_plain_is_equal(aff, str);
4586 if (equal < 0)
4587 return -1;
4588 if (!equal)
4589 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4590 "result not as expected", return -1);
4591 return 0;
4594 struct {
4595 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4596 __isl_take isl_aff *aff2);
4597 } aff_bin_op[] = {
4598 ['+'] = { &isl_aff_add },
4599 ['-'] = { &isl_aff_sub },
4600 ['*'] = { &isl_aff_mul },
4601 ['/'] = { &isl_aff_div },
4604 struct {
4605 const char *arg1;
4606 unsigned char op;
4607 const char *arg2;
4608 const char *res;
4609 } aff_bin_tests[] = {
4610 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
4611 "{ [i] -> [2i] }" },
4612 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
4613 "{ [i] -> [0] }" },
4614 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
4615 "{ [i] -> [2i] }" },
4616 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
4617 "{ [i] -> [2i] }" },
4618 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
4619 "{ [i] -> [i/2] }" },
4620 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
4621 "{ [i] -> [i] }" },
4622 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
4623 "{ [i] -> [NaN] }" },
4624 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
4625 "{ [i] -> [NaN] }" },
4626 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
4627 "{ [i] -> [NaN] }" },
4628 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
4629 "{ [i] -> [NaN] }" },
4630 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
4631 "{ [i] -> [NaN] }" },
4632 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
4633 "{ [i] -> [NaN] }" },
4634 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
4635 "{ [i] -> [NaN] }" },
4636 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
4637 "{ [i] -> [NaN] }" },
4638 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
4639 "{ [i] -> [NaN] }" },
4640 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
4641 "{ [i] -> [NaN] }" },
4642 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
4643 "{ [i] -> [NaN] }" },
4644 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
4645 "{ [i] -> [NaN] }" },
4648 /* Perform some basic tests of binary operations on isl_aff objects.
4650 static int test_bin_aff(isl_ctx *ctx)
4652 int i;
4653 isl_aff *aff1, *aff2, *res;
4654 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4655 __isl_take isl_aff *aff2);
4656 int ok;
4658 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
4659 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
4660 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
4661 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
4662 fn = aff_bin_op[aff_bin_tests[i].op].fn;
4663 aff1 = fn(aff1, aff2);
4664 if (isl_aff_is_nan(res))
4665 ok = isl_aff_is_nan(aff1);
4666 else
4667 ok = isl_aff_plain_is_equal(aff1, res);
4668 isl_aff_free(aff1);
4669 isl_aff_free(res);
4670 if (ok < 0)
4671 return -1;
4672 if (!ok)
4673 isl_die(ctx, isl_error_unknown,
4674 "unexpected result", return -1);
4677 return 0;
4680 struct {
4681 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
4682 __isl_take isl_pw_aff *pa2);
4683 } pw_aff_bin_op[] = {
4684 ['m'] = { &isl_pw_aff_min },
4685 ['M'] = { &isl_pw_aff_max },
4688 /* Inputs for binary isl_pw_aff operation tests.
4689 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
4690 * defined by pw_aff_bin_op, and "res" is the expected result.
4692 struct {
4693 const char *arg1;
4694 unsigned char op;
4695 const char *arg2;
4696 const char *res;
4697 } pw_aff_bin_tests[] = {
4698 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
4699 "{ [i] -> [i] }" },
4700 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
4701 "{ [i] -> [i] }" },
4702 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
4703 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
4704 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
4705 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
4706 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
4707 "{ [i] -> [NaN] }" },
4708 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
4709 "{ [i] -> [NaN] }" },
4712 /* Perform some basic tests of binary operations on isl_pw_aff objects.
4714 static int test_bin_pw_aff(isl_ctx *ctx)
4716 int i;
4717 isl_bool ok;
4718 isl_pw_aff *pa1, *pa2, *res;
4720 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
4721 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
4722 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
4723 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
4724 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
4725 if (isl_pw_aff_involves_nan(res))
4726 ok = isl_pw_aff_involves_nan(pa1);
4727 else
4728 ok = isl_pw_aff_plain_is_equal(pa1, res);
4729 isl_pw_aff_free(pa1);
4730 isl_pw_aff_free(res);
4731 if (ok < 0)
4732 return -1;
4733 if (!ok)
4734 isl_die(ctx, isl_error_unknown,
4735 "unexpected result", return -1);
4738 return 0;
4741 struct {
4742 __isl_give isl_union_pw_multi_aff *(*fn)(
4743 __isl_take isl_union_pw_multi_aff *upma1,
4744 __isl_take isl_union_pw_multi_aff *upma2);
4745 const char *arg1;
4746 const char *arg2;
4747 const char *res;
4748 } upma_bin_tests[] = {
4749 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
4750 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
4751 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
4752 "{ B[x] -> [2] : x >= 0 }",
4753 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
4754 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
4755 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
4756 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
4757 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
4758 "D[i] -> B[2] : i >= 5 }" },
4759 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4760 "{ B[x] -> C[2] : x > 0 }",
4761 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
4762 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4763 "{ B[x] -> A[2] : x >= 0 }",
4764 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
4767 /* Perform some basic tests of binary operations on
4768 * isl_union_pw_multi_aff objects.
4770 static int test_bin_upma(isl_ctx *ctx)
4772 int i;
4773 isl_union_pw_multi_aff *upma1, *upma2, *res;
4774 int ok;
4776 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
4777 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4778 upma_bin_tests[i].arg1);
4779 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4780 upma_bin_tests[i].arg2);
4781 res = isl_union_pw_multi_aff_read_from_str(ctx,
4782 upma_bin_tests[i].res);
4783 upma1 = upma_bin_tests[i].fn(upma1, upma2);
4784 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
4785 isl_union_pw_multi_aff_free(upma1);
4786 isl_union_pw_multi_aff_free(res);
4787 if (ok < 0)
4788 return -1;
4789 if (!ok)
4790 isl_die(ctx, isl_error_unknown,
4791 "unexpected result", return -1);
4794 return 0;
4797 struct {
4798 __isl_give isl_union_pw_multi_aff *(*fn)(
4799 __isl_take isl_union_pw_multi_aff *upma1,
4800 __isl_take isl_union_pw_multi_aff *upma2);
4801 const char *arg1;
4802 const char *arg2;
4803 } upma_bin_fail_tests[] = {
4804 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4805 "{ B[x] -> C[2] : x >= 0 }" },
4808 /* Perform some basic tests of binary operations on
4809 * isl_union_pw_multi_aff objects that are expected to fail.
4811 static int test_bin_upma_fail(isl_ctx *ctx)
4813 int i, n;
4814 isl_union_pw_multi_aff *upma1, *upma2;
4815 int on_error;
4817 on_error = isl_options_get_on_error(ctx);
4818 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
4819 n = ARRAY_SIZE(upma_bin_fail_tests);
4820 for (i = 0; i < n; ++i) {
4821 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4822 upma_bin_fail_tests[i].arg1);
4823 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4824 upma_bin_fail_tests[i].arg2);
4825 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
4826 isl_union_pw_multi_aff_free(upma1);
4827 if (upma1)
4828 break;
4830 isl_options_set_on_error(ctx, on_error);
4831 if (i < n)
4832 isl_die(ctx, isl_error_unknown,
4833 "operation not expected to succeed", return -1);
4835 return 0;
4838 int test_aff(isl_ctx *ctx)
4840 const char *str;
4841 isl_set *set;
4842 isl_space *space;
4843 isl_local_space *ls;
4844 isl_aff *aff;
4845 int zero, equal;
4847 if (test_bin_aff(ctx) < 0)
4848 return -1;
4849 if (test_bin_pw_aff(ctx) < 0)
4850 return -1;
4851 if (test_bin_upma(ctx) < 0)
4852 return -1;
4853 if (test_bin_upma_fail(ctx) < 0)
4854 return -1;
4856 space = isl_space_set_alloc(ctx, 0, 1);
4857 ls = isl_local_space_from_space(space);
4858 aff = isl_aff_zero_on_domain(ls);
4860 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4861 aff = isl_aff_scale_down_ui(aff, 3);
4862 aff = isl_aff_floor(aff);
4863 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4864 aff = isl_aff_scale_down_ui(aff, 2);
4865 aff = isl_aff_floor(aff);
4866 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4868 str = "{ [10] }";
4869 set = isl_set_read_from_str(ctx, str);
4870 aff = isl_aff_gist(aff, set);
4872 aff = isl_aff_add_constant_si(aff, -16);
4873 zero = isl_aff_plain_is_zero(aff);
4874 isl_aff_free(aff);
4876 if (zero < 0)
4877 return -1;
4878 if (!zero)
4879 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4881 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
4882 aff = isl_aff_scale_down_ui(aff, 64);
4883 aff = isl_aff_floor(aff);
4884 equal = aff_check_plain_equal(aff, "{ [-1] }");
4885 isl_aff_free(aff);
4886 if (equal < 0)
4887 return -1;
4889 return 0;
4892 /* Check that the computation below results in a single expression.
4893 * One or two expressions may result depending on which constraint
4894 * ends up being considered as redundant with respect to the other
4895 * constraints after the projection that is performed internally
4896 * by isl_set_dim_min.
4898 static int test_dim_max_1(isl_ctx *ctx)
4900 const char *str;
4901 isl_set *set;
4902 isl_pw_aff *pa;
4903 int n;
4905 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
4906 "-4a <= b <= 3 and b < n - 4a }";
4907 set = isl_set_read_from_str(ctx, str);
4908 pa = isl_set_dim_min(set, 0);
4909 n = isl_pw_aff_n_piece(pa);
4910 isl_pw_aff_free(pa);
4912 if (!pa)
4913 return -1;
4914 if (n != 1)
4915 isl_die(ctx, isl_error_unknown, "expecting single expression",
4916 return -1);
4918 return 0;
4921 int test_dim_max(isl_ctx *ctx)
4923 int equal;
4924 const char *str;
4925 isl_set *set1, *set2;
4926 isl_set *set;
4927 isl_map *map;
4928 isl_pw_aff *pwaff;
4930 if (test_dim_max_1(ctx) < 0)
4931 return -1;
4933 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
4934 set = isl_set_read_from_str(ctx, str);
4935 pwaff = isl_set_dim_max(set, 0);
4936 set1 = isl_set_from_pw_aff(pwaff);
4937 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
4938 set2 = isl_set_read_from_str(ctx, str);
4939 equal = isl_set_is_equal(set1, set2);
4940 isl_set_free(set1);
4941 isl_set_free(set2);
4942 if (equal < 0)
4943 return -1;
4944 if (!equal)
4945 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4947 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
4948 set = isl_set_read_from_str(ctx, str);
4949 pwaff = isl_set_dim_max(set, 0);
4950 set1 = isl_set_from_pw_aff(pwaff);
4951 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4952 set2 = isl_set_read_from_str(ctx, str);
4953 equal = isl_set_is_equal(set1, set2);
4954 isl_set_free(set1);
4955 isl_set_free(set2);
4956 if (equal < 0)
4957 return -1;
4958 if (!equal)
4959 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4961 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
4962 set = isl_set_read_from_str(ctx, str);
4963 pwaff = isl_set_dim_max(set, 0);
4964 set1 = isl_set_from_pw_aff(pwaff);
4965 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4966 set2 = isl_set_read_from_str(ctx, str);
4967 equal = isl_set_is_equal(set1, set2);
4968 isl_set_free(set1);
4969 isl_set_free(set2);
4970 if (equal < 0)
4971 return -1;
4972 if (!equal)
4973 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4975 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
4976 "0 <= i < N and 0 <= j < M }";
4977 map = isl_map_read_from_str(ctx, str);
4978 set = isl_map_range(map);
4980 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
4981 set1 = isl_set_from_pw_aff(pwaff);
4982 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
4983 set2 = isl_set_read_from_str(ctx, str);
4984 equal = isl_set_is_equal(set1, set2);
4985 isl_set_free(set1);
4986 isl_set_free(set2);
4988 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
4989 set1 = isl_set_from_pw_aff(pwaff);
4990 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
4991 set2 = isl_set_read_from_str(ctx, str);
4992 if (equal >= 0 && equal)
4993 equal = isl_set_is_equal(set1, set2);
4994 isl_set_free(set1);
4995 isl_set_free(set2);
4997 isl_set_free(set);
4999 if (equal < 0)
5000 return -1;
5001 if (!equal)
5002 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5004 /* Check that solutions are properly merged. */
5005 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
5006 "c <= -1 + n - 4a - 2b and c >= -2b and "
5007 "4a >= -4 + n and c >= 0 }";
5008 set = isl_set_read_from_str(ctx, str);
5009 pwaff = isl_set_dim_min(set, 2);
5010 set1 = isl_set_from_pw_aff(pwaff);
5011 str = "[n] -> { [(0)] : n >= 1 }";
5012 set2 = isl_set_read_from_str(ctx, str);
5013 equal = isl_set_is_equal(set1, set2);
5014 isl_set_free(set1);
5015 isl_set_free(set2);
5017 if (equal < 0)
5018 return -1;
5019 if (!equal)
5020 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5022 /* Check that empty solution lie in the right space. */
5023 str = "[n] -> { [t,a] : 1 = 0 }";
5024 set = isl_set_read_from_str(ctx, str);
5025 pwaff = isl_set_dim_max(set, 0);
5026 set1 = isl_set_from_pw_aff(pwaff);
5027 str = "[n] -> { [t] : 1 = 0 }";
5028 set2 = isl_set_read_from_str(ctx, str);
5029 equal = isl_set_is_equal(set1, set2);
5030 isl_set_free(set1);
5031 isl_set_free(set2);
5033 if (equal < 0)
5034 return -1;
5035 if (!equal)
5036 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5038 return 0;
5041 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
5043 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
5044 const char *str)
5046 isl_ctx *ctx;
5047 isl_pw_multi_aff *pma2;
5048 int equal;
5050 if (!pma)
5051 return -1;
5053 ctx = isl_pw_multi_aff_get_ctx(pma);
5054 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5055 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
5056 isl_pw_multi_aff_free(pma2);
5058 return equal;
5061 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
5062 * represented by "str".
5064 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
5065 const char *str)
5067 int equal;
5069 equal = pw_multi_aff_plain_is_equal(pma, str);
5070 if (equal < 0)
5071 return -1;
5072 if (!equal)
5073 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
5074 "result not as expected", return -1);
5075 return 0;
5078 /* Basic test for isl_pw_multi_aff_product.
5080 * Check that multiple pieces are properly handled.
5082 static int test_product_pma(isl_ctx *ctx)
5084 int equal;
5085 const char *str;
5086 isl_pw_multi_aff *pma1, *pma2;
5088 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
5089 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
5090 str = "{ C[] -> D[] }";
5091 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5092 pma1 = isl_pw_multi_aff_product(pma1, pma2);
5093 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
5094 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
5095 equal = pw_multi_aff_check_plain_equal(pma1, str);
5096 isl_pw_multi_aff_free(pma1);
5097 if (equal < 0)
5098 return -1;
5100 return 0;
5103 int test_product(isl_ctx *ctx)
5105 const char *str;
5106 isl_set *set;
5107 isl_union_set *uset1, *uset2;
5108 int ok;
5110 str = "{ A[i] }";
5111 set = isl_set_read_from_str(ctx, str);
5112 set = isl_set_product(set, isl_set_copy(set));
5113 ok = isl_set_is_wrapping(set);
5114 isl_set_free(set);
5115 if (ok < 0)
5116 return -1;
5117 if (!ok)
5118 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5120 str = "{ [] }";
5121 uset1 = isl_union_set_read_from_str(ctx, str);
5122 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
5123 str = "{ [[] -> []] }";
5124 uset2 = isl_union_set_read_from_str(ctx, str);
5125 ok = isl_union_set_is_equal(uset1, uset2);
5126 isl_union_set_free(uset1);
5127 isl_union_set_free(uset2);
5128 if (ok < 0)
5129 return -1;
5130 if (!ok)
5131 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5133 if (test_product_pma(ctx) < 0)
5134 return -1;
5136 return 0;
5139 /* Check that two sets are not considered disjoint just because
5140 * they have a different set of (named) parameters.
5142 static int test_disjoint(isl_ctx *ctx)
5144 const char *str;
5145 isl_set *set, *set2;
5146 int disjoint;
5148 str = "[n] -> { [[]->[]] }";
5149 set = isl_set_read_from_str(ctx, str);
5150 str = "{ [[]->[]] }";
5151 set2 = isl_set_read_from_str(ctx, str);
5152 disjoint = isl_set_is_disjoint(set, set2);
5153 isl_set_free(set);
5154 isl_set_free(set2);
5155 if (disjoint < 0)
5156 return -1;
5157 if (disjoint)
5158 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5160 return 0;
5163 /* Inputs for isl_pw_multi_aff_is_equal tests.
5164 * "f1" and "f2" are the two function that need to be compared.
5165 * "equal" is the expected result.
5167 struct {
5168 int equal;
5169 const char *f1;
5170 const char *f2;
5171 } pma_equal_tests[] = {
5172 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
5173 "[N] -> { [0] : 0 <= N <= 1 }" },
5174 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
5175 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
5176 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
5177 "[N] -> { [0] : 0 <= N <= 1 }" },
5178 { 0, "{ [NaN] }", "{ [NaN] }" },
5181 int test_equal(isl_ctx *ctx)
5183 int i;
5184 const char *str;
5185 isl_set *set, *set2;
5186 int equal;
5188 str = "{ S_6[i] }";
5189 set = isl_set_read_from_str(ctx, str);
5190 str = "{ S_7[i] }";
5191 set2 = isl_set_read_from_str(ctx, str);
5192 equal = isl_set_is_equal(set, set2);
5193 isl_set_free(set);
5194 isl_set_free(set2);
5195 if (equal < 0)
5196 return -1;
5197 if (equal)
5198 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5200 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
5201 int expected = pma_equal_tests[i].equal;
5202 isl_pw_multi_aff *f1, *f2;
5204 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
5205 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
5206 equal = isl_pw_multi_aff_is_equal(f1, f2);
5207 isl_pw_multi_aff_free(f1);
5208 isl_pw_multi_aff_free(f2);
5209 if (equal < 0)
5210 return -1;
5211 if (equal != expected)
5212 isl_die(ctx, isl_error_unknown,
5213 "unexpected equality result", return -1);
5216 return 0;
5219 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
5220 enum isl_dim_type type, unsigned pos, int fixed)
5222 isl_bool test;
5224 test = isl_map_plain_is_fixed(map, type, pos, NULL);
5225 isl_map_free(map);
5226 if (test < 0)
5227 return -1;
5228 if (test == fixed)
5229 return 0;
5230 if (fixed)
5231 isl_die(ctx, isl_error_unknown,
5232 "map not detected as fixed", return -1);
5233 else
5234 isl_die(ctx, isl_error_unknown,
5235 "map detected as fixed", return -1);
5238 int test_fixed(isl_ctx *ctx)
5240 const char *str;
5241 isl_map *map;
5243 str = "{ [i] -> [i] }";
5244 map = isl_map_read_from_str(ctx, str);
5245 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
5246 return -1;
5247 str = "{ [i] -> [1] }";
5248 map = isl_map_read_from_str(ctx, str);
5249 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5250 return -1;
5251 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
5252 map = isl_map_read_from_str(ctx, str);
5253 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5254 return -1;
5255 map = isl_map_read_from_str(ctx, str);
5256 map = isl_map_neg(map);
5257 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5258 return -1;
5260 return 0;
5263 struct isl_vertices_test_data {
5264 const char *set;
5265 int n;
5266 const char *vertex[6];
5267 } vertices_tests[] = {
5268 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
5269 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
5270 { "{ A[t, i] : t = 14 and i = 1 }",
5271 1, { "{ A[14, 1] }" } },
5272 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
5273 "c <= m and m <= n and m > 0 }",
5274 6, {
5275 "[n, m] -> { [n, m, m] : 0 < m <= n }",
5276 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
5277 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
5278 "[n, m] -> { [m, m, m] : 0 < m <= n }",
5279 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
5280 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
5281 } },
5284 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
5286 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
5288 struct isl_vertices_test_data *data = user;
5289 isl_ctx *ctx;
5290 isl_multi_aff *ma;
5291 isl_basic_set *bset;
5292 isl_pw_multi_aff *pma;
5293 int i;
5294 isl_bool equal;
5296 ctx = isl_vertex_get_ctx(vertex);
5297 bset = isl_vertex_get_domain(vertex);
5298 ma = isl_vertex_get_expr(vertex);
5299 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
5301 for (i = 0; i < data->n; ++i) {
5302 isl_pw_multi_aff *pma_i;
5304 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
5305 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
5306 isl_pw_multi_aff_free(pma_i);
5308 if (equal < 0 || equal)
5309 break;
5312 isl_pw_multi_aff_free(pma);
5313 isl_vertex_free(vertex);
5315 if (equal < 0)
5316 return isl_stat_error;
5318 return equal ? isl_stat_ok : isl_stat_error;
5321 int test_vertices(isl_ctx *ctx)
5323 int i;
5325 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
5326 isl_basic_set *bset;
5327 isl_vertices *vertices;
5328 int ok = 1;
5329 int n;
5331 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
5332 vertices = isl_basic_set_compute_vertices(bset);
5333 n = isl_vertices_get_n_vertices(vertices);
5334 if (vertices_tests[i].n != n)
5335 ok = 0;
5336 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
5337 &vertices_tests[i]) < 0)
5338 ok = 0;
5339 isl_vertices_free(vertices);
5340 isl_basic_set_free(bset);
5342 if (!vertices)
5343 return -1;
5344 if (!ok)
5345 isl_die(ctx, isl_error_unknown, "unexpected vertices",
5346 return -1);
5349 return 0;
5352 int test_union_pw(isl_ctx *ctx)
5354 int equal;
5355 const char *str;
5356 isl_union_set *uset;
5357 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
5359 str = "{ [x] -> x^2 }";
5360 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
5361 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
5362 uset = isl_union_pw_qpolynomial_domain(upwqp1);
5363 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
5364 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
5365 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
5366 isl_union_pw_qpolynomial_free(upwqp1);
5367 isl_union_pw_qpolynomial_free(upwqp2);
5368 if (equal < 0)
5369 return -1;
5370 if (!equal)
5371 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5373 return 0;
5376 /* Test that isl_union_pw_qpolynomial_eval picks up the function
5377 * defined over the correct domain space.
5379 static int test_eval_1(isl_ctx *ctx)
5381 const char *str;
5382 isl_point *pnt;
5383 isl_set *set;
5384 isl_union_pw_qpolynomial *upwqp;
5385 isl_val *v;
5386 int cmp;
5388 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
5389 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
5390 str = "{ A[6] }";
5391 set = isl_set_read_from_str(ctx, str);
5392 pnt = isl_set_sample_point(set);
5393 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
5394 cmp = isl_val_cmp_si(v, 36);
5395 isl_val_free(v);
5397 if (!v)
5398 return -1;
5399 if (cmp != 0)
5400 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
5402 return 0;
5405 /* Check that isl_qpolynomial_eval handles getting called on a void point.
5407 static int test_eval_2(isl_ctx *ctx)
5409 const char *str;
5410 isl_point *pnt;
5411 isl_set *set;
5412 isl_qpolynomial *qp;
5413 isl_val *v;
5414 isl_bool ok;
5416 str = "{ A[x] -> [x] }";
5417 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
5418 str = "{ A[x] : false }";
5419 set = isl_set_read_from_str(ctx, str);
5420 pnt = isl_set_sample_point(set);
5421 v = isl_qpolynomial_eval(qp, pnt);
5422 ok = isl_val_is_nan(v);
5423 isl_val_free(v);
5425 if (ok < 0)
5426 return -1;
5427 if (!ok)
5428 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
5430 return 0;
5433 /* Perform basic polynomial evaluation tests.
5435 static int test_eval(isl_ctx *ctx)
5437 if (test_eval_1(ctx) < 0)
5438 return -1;
5439 if (test_eval_2(ctx) < 0)
5440 return -1;
5441 return 0;
5444 /* Descriptions of sets that are tested for reparsing after printing.
5446 const char *output_tests[] = {
5447 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
5448 "{ [x] : 1 = 0 }",
5449 "{ [x] : false }",
5450 "{ [x] : x mod 2 = 0 }",
5451 "{ [x] : x mod 2 = 1 }",
5452 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) < x }",
5453 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) < x }",
5454 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
5455 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
5456 "[n] -> { [y, x] : 2*((x + 2y) mod 3) = n }",
5457 "{ [x, y] : (2*floor(x/3) + 3*floor(y/4)) mod 5 = x }",
5460 /* Check that printing a set and reparsing a set from the printed output
5461 * results in the same set.
5463 static int test_output_set(isl_ctx *ctx)
5465 int i;
5466 char *str;
5467 isl_set *set1, *set2;
5468 isl_bool equal;
5470 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
5471 set1 = isl_set_read_from_str(ctx, output_tests[i]);
5472 str = isl_set_to_str(set1);
5473 set2 = isl_set_read_from_str(ctx, str);
5474 free(str);
5475 equal = isl_set_is_equal(set1, set2);
5476 isl_set_free(set1);
5477 isl_set_free(set2);
5478 if (equal < 0)
5479 return -1;
5480 if (!equal)
5481 isl_die(ctx, isl_error_unknown,
5482 "parsed output not the same", return -1);
5485 return 0;
5488 int test_output(isl_ctx *ctx)
5490 char *s;
5491 const char *str;
5492 isl_pw_aff *pa;
5493 isl_printer *p;
5494 int equal;
5496 if (test_output_set(ctx) < 0)
5497 return -1;
5499 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
5500 pa = isl_pw_aff_read_from_str(ctx, str);
5502 p = isl_printer_to_str(ctx);
5503 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
5504 p = isl_printer_print_pw_aff(p, pa);
5505 s = isl_printer_get_str(p);
5506 isl_printer_free(p);
5507 isl_pw_aff_free(pa);
5508 if (!s)
5509 equal = -1;
5510 else
5511 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
5512 free(s);
5513 if (equal < 0)
5514 return -1;
5515 if (!equal)
5516 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5518 return 0;
5521 int test_sample(isl_ctx *ctx)
5523 const char *str;
5524 isl_basic_set *bset1, *bset2;
5525 int empty, subset;
5527 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
5528 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
5529 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
5530 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
5531 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
5532 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
5533 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
5534 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
5535 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
5536 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
5537 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
5538 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
5539 "d - 1073741821e and "
5540 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
5541 "3j >= 1 - a + b + 2e and "
5542 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
5543 "3i <= 4 - a + 4b - e and "
5544 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
5545 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
5546 "c <= -1 + a and 3i >= -2 - a + 3e and "
5547 "1073741822e <= 1073741823 - a + 1073741822b + c and "
5548 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
5549 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
5550 "1073741823e >= 1 + 1073741823b - d and "
5551 "3i >= 1073741823b + c - 1073741823e - f and "
5552 "3i >= 1 + 2b + e + 3g }";
5553 bset1 = isl_basic_set_read_from_str(ctx, str);
5554 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
5555 empty = isl_basic_set_is_empty(bset2);
5556 subset = isl_basic_set_is_subset(bset2, bset1);
5557 isl_basic_set_free(bset1);
5558 isl_basic_set_free(bset2);
5559 if (empty < 0 || subset < 0)
5560 return -1;
5561 if (empty)
5562 isl_die(ctx, isl_error_unknown, "point not found", return -1);
5563 if (!subset)
5564 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
5566 return 0;
5569 int test_fixed_power(isl_ctx *ctx)
5571 const char *str;
5572 isl_map *map;
5573 isl_int exp;
5574 int equal;
5576 isl_int_init(exp);
5577 str = "{ [i] -> [i + 1] }";
5578 map = isl_map_read_from_str(ctx, str);
5579 isl_int_set_si(exp, 23);
5580 map = isl_map_fixed_power(map, exp);
5581 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
5582 isl_int_clear(exp);
5583 isl_map_free(map);
5584 if (equal < 0)
5585 return -1;
5587 return 0;
5590 int test_slice(isl_ctx *ctx)
5592 const char *str;
5593 isl_map *map;
5594 int equal;
5596 str = "{ [i] -> [j] }";
5597 map = isl_map_read_from_str(ctx, str);
5598 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
5599 equal = map_check_equal(map, "{ [i] -> [i] }");
5600 isl_map_free(map);
5601 if (equal < 0)
5602 return -1;
5604 str = "{ [i] -> [j] }";
5605 map = isl_map_read_from_str(ctx, str);
5606 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
5607 equal = map_check_equal(map, "{ [i] -> [j] }");
5608 isl_map_free(map);
5609 if (equal < 0)
5610 return -1;
5612 str = "{ [i] -> [j] }";
5613 map = isl_map_read_from_str(ctx, str);
5614 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
5615 equal = map_check_equal(map, "{ [i] -> [-i] }");
5616 isl_map_free(map);
5617 if (equal < 0)
5618 return -1;
5620 str = "{ [i] -> [j] }";
5621 map = isl_map_read_from_str(ctx, str);
5622 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
5623 equal = map_check_equal(map, "{ [0] -> [j] }");
5624 isl_map_free(map);
5625 if (equal < 0)
5626 return -1;
5628 str = "{ [i] -> [j] }";
5629 map = isl_map_read_from_str(ctx, str);
5630 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
5631 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
5632 isl_map_free(map);
5633 if (equal < 0)
5634 return -1;
5636 str = "{ [i] -> [j] }";
5637 map = isl_map_read_from_str(ctx, str);
5638 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
5639 equal = map_check_equal(map, "{ [i] -> [j] : false }");
5640 isl_map_free(map);
5641 if (equal < 0)
5642 return -1;
5644 return 0;
5647 int test_eliminate(isl_ctx *ctx)
5649 const char *str;
5650 isl_map *map;
5651 int equal;
5653 str = "{ [i] -> [j] : i = 2j }";
5654 map = isl_map_read_from_str(ctx, str);
5655 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
5656 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
5657 isl_map_free(map);
5658 if (equal < 0)
5659 return -1;
5661 return 0;
5664 /* Check that isl_set_dim_residue_class detects that the values of j
5665 * in the set below are all odd and that it does not detect any spurious
5666 * strides.
5668 static int test_residue_class(isl_ctx *ctx)
5670 const char *str;
5671 isl_set *set;
5672 isl_int m, r;
5673 isl_stat res;
5675 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
5676 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
5677 set = isl_set_read_from_str(ctx, str);
5678 isl_int_init(m);
5679 isl_int_init(r);
5680 res = isl_set_dim_residue_class(set, 1, &m, &r);
5681 if (res >= 0 &&
5682 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
5683 isl_die(ctx, isl_error_unknown, "incorrect residue class",
5684 res = isl_stat_error);
5685 isl_int_clear(r);
5686 isl_int_clear(m);
5687 isl_set_free(set);
5689 return res;
5692 int test_align_parameters(isl_ctx *ctx)
5694 const char *str;
5695 isl_space *space;
5696 isl_multi_aff *ma1, *ma2;
5697 int equal;
5699 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
5700 ma1 = isl_multi_aff_read_from_str(ctx, str);
5702 space = isl_space_params_alloc(ctx, 1);
5703 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
5704 ma1 = isl_multi_aff_align_params(ma1, space);
5706 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
5707 ma2 = isl_multi_aff_read_from_str(ctx, str);
5709 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5711 isl_multi_aff_free(ma1);
5712 isl_multi_aff_free(ma2);
5714 if (equal < 0)
5715 return -1;
5716 if (!equal)
5717 isl_die(ctx, isl_error_unknown,
5718 "result not as expected", return -1);
5720 return 0;
5723 static int test_list(isl_ctx *ctx)
5725 isl_id *a, *b, *c, *d, *id;
5726 isl_id_list *list;
5727 int ok;
5729 a = isl_id_alloc(ctx, "a", NULL);
5730 b = isl_id_alloc(ctx, "b", NULL);
5731 c = isl_id_alloc(ctx, "c", NULL);
5732 d = isl_id_alloc(ctx, "d", NULL);
5734 list = isl_id_list_alloc(ctx, 4);
5735 list = isl_id_list_add(list, b);
5736 list = isl_id_list_insert(list, 0, a);
5737 list = isl_id_list_add(list, c);
5738 list = isl_id_list_add(list, d);
5739 list = isl_id_list_drop(list, 1, 1);
5741 if (isl_id_list_n_id(list) != 3) {
5742 isl_id_list_free(list);
5743 isl_die(ctx, isl_error_unknown,
5744 "unexpected number of elements in list", return -1);
5747 id = isl_id_list_get_id(list, 0);
5748 ok = id == a;
5749 isl_id_free(id);
5750 id = isl_id_list_get_id(list, 1);
5751 ok = ok && id == c;
5752 isl_id_free(id);
5753 id = isl_id_list_get_id(list, 2);
5754 ok = ok && id == d;
5755 isl_id_free(id);
5757 isl_id_list_free(list);
5759 if (!ok)
5760 isl_die(ctx, isl_error_unknown,
5761 "unexpected elements in list", return -1);
5763 return 0;
5766 const char *set_conversion_tests[] = {
5767 "[N] -> { [i] : N - 1 <= 2 i <= N }",
5768 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
5769 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5770 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5771 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
5772 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
5773 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
5774 "-3 + c <= d <= 28 + c) }",
5777 /* Check that converting from isl_set to isl_pw_multi_aff and back
5778 * to isl_set produces the original isl_set.
5780 static int test_set_conversion(isl_ctx *ctx)
5782 int i;
5783 const char *str;
5784 isl_set *set1, *set2;
5785 isl_pw_multi_aff *pma;
5786 int equal;
5788 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
5789 str = set_conversion_tests[i];
5790 set1 = isl_set_read_from_str(ctx, str);
5791 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
5792 set2 = isl_set_from_pw_multi_aff(pma);
5793 equal = isl_set_is_equal(set1, set2);
5794 isl_set_free(set1);
5795 isl_set_free(set2);
5797 if (equal < 0)
5798 return -1;
5799 if (!equal)
5800 isl_die(ctx, isl_error_unknown, "bad conversion",
5801 return -1);
5804 return 0;
5807 const char *conversion_tests[] = {
5808 "{ [a, b, c, d] -> s0[a, b, e, f] : "
5809 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
5810 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
5811 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
5812 "9e <= -2 - 2a) }",
5813 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
5814 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
5815 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
5818 /* Check that converting from isl_map to isl_pw_multi_aff and back
5819 * to isl_map produces the original isl_map.
5821 static int test_map_conversion(isl_ctx *ctx)
5823 int i;
5824 isl_map *map1, *map2;
5825 isl_pw_multi_aff *pma;
5826 int equal;
5828 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
5829 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
5830 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
5831 map2 = isl_map_from_pw_multi_aff(pma);
5832 equal = isl_map_is_equal(map1, map2);
5833 isl_map_free(map1);
5834 isl_map_free(map2);
5836 if (equal < 0)
5837 return -1;
5838 if (!equal)
5839 isl_die(ctx, isl_error_unknown, "bad conversion",
5840 return -1);
5843 return 0;
5846 static int test_conversion(isl_ctx *ctx)
5848 if (test_set_conversion(ctx) < 0)
5849 return -1;
5850 if (test_map_conversion(ctx) < 0)
5851 return -1;
5852 return 0;
5855 /* Check that isl_basic_map_curry does not modify input.
5857 static int test_curry(isl_ctx *ctx)
5859 const char *str;
5860 isl_basic_map *bmap1, *bmap2;
5861 int equal;
5863 str = "{ [A[] -> B[]] -> C[] }";
5864 bmap1 = isl_basic_map_read_from_str(ctx, str);
5865 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
5866 equal = isl_basic_map_is_equal(bmap1, bmap2);
5867 isl_basic_map_free(bmap1);
5868 isl_basic_map_free(bmap2);
5870 if (equal < 0)
5871 return -1;
5872 if (equal)
5873 isl_die(ctx, isl_error_unknown,
5874 "curried map should not be equal to original",
5875 return -1);
5877 return 0;
5880 struct {
5881 const char *set;
5882 const char *ma;
5883 const char *res;
5884 } preimage_tests[] = {
5885 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
5886 "{ A[j,i] -> B[i,j] }",
5887 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
5888 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5889 "{ A[a,b] -> B[a/2,b/6] }",
5890 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
5891 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5892 "{ A[a,b] -> B[a/2,b/6] }",
5893 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
5894 "exists i,j : a = 2 i and b = 6 j }" },
5895 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
5896 "[n] -> { : 0 <= n <= 100 }" },
5897 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5898 "{ A[a] -> B[2a] }",
5899 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
5900 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5901 "{ A[a] -> B[([a/2])] }",
5902 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
5903 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
5904 "{ A[a] -> B[a,a,a/3] }",
5905 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
5906 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
5907 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
5910 static int test_preimage_basic_set(isl_ctx *ctx)
5912 int i;
5913 isl_basic_set *bset1, *bset2;
5914 isl_multi_aff *ma;
5915 int equal;
5917 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
5918 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
5919 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
5920 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
5921 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
5922 equal = isl_basic_set_is_equal(bset1, bset2);
5923 isl_basic_set_free(bset1);
5924 isl_basic_set_free(bset2);
5925 if (equal < 0)
5926 return -1;
5927 if (!equal)
5928 isl_die(ctx, isl_error_unknown, "bad preimage",
5929 return -1);
5932 return 0;
5935 struct {
5936 const char *map;
5937 const char *ma;
5938 const char *res;
5939 } preimage_domain_tests[] = {
5940 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
5941 "{ A[j,i] -> B[i,j] }",
5942 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
5943 { "{ B[i] -> C[i]; D[i] -> E[i] }",
5944 "{ A[i] -> B[i + 1] }",
5945 "{ A[i] -> C[i + 1] }" },
5946 { "{ B[i] -> C[i]; B[i] -> E[i] }",
5947 "{ A[i] -> B[i + 1] }",
5948 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
5949 { "{ B[i] -> C[([i/2])] }",
5950 "{ A[i] -> B[2i] }",
5951 "{ A[i] -> C[i] }" },
5952 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
5953 "{ A[i] -> B[([i/5]), ([i/7])] }",
5954 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
5955 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
5956 "[N] -> { A[] -> B[([N/5])] }",
5957 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
5958 { "{ B[i] -> C[i] : exists a : i = 5 a }",
5959 "{ A[i] -> B[2i] }",
5960 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
5961 { "{ B[i] -> C[i] : exists a : i = 2 a; "
5962 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
5963 "{ A[i] -> B[2i] }",
5964 "{ A[i] -> C[2i] }" },
5965 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
5966 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
5969 static int test_preimage_union_map(isl_ctx *ctx)
5971 int i;
5972 isl_union_map *umap1, *umap2;
5973 isl_multi_aff *ma;
5974 int equal;
5976 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
5977 umap1 = isl_union_map_read_from_str(ctx,
5978 preimage_domain_tests[i].map);
5979 ma = isl_multi_aff_read_from_str(ctx,
5980 preimage_domain_tests[i].ma);
5981 umap2 = isl_union_map_read_from_str(ctx,
5982 preimage_domain_tests[i].res);
5983 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
5984 equal = isl_union_map_is_equal(umap1, umap2);
5985 isl_union_map_free(umap1);
5986 isl_union_map_free(umap2);
5987 if (equal < 0)
5988 return -1;
5989 if (!equal)
5990 isl_die(ctx, isl_error_unknown, "bad preimage",
5991 return -1);
5994 return 0;
5997 static int test_preimage(isl_ctx *ctx)
5999 if (test_preimage_basic_set(ctx) < 0)
6000 return -1;
6001 if (test_preimage_union_map(ctx) < 0)
6002 return -1;
6004 return 0;
6007 struct {
6008 const char *ma1;
6009 const char *ma;
6010 const char *res;
6011 } pullback_tests[] = {
6012 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
6013 "{ A[a,b] -> C[b + 2a] }" },
6014 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
6015 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
6016 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
6017 "{ A[a] -> C[(a)/6] }" },
6018 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
6019 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
6020 "{ A[a] -> C[(2a)/3] }" },
6021 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
6022 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
6023 "{ A[i,j] -> C[i + j, i + j] }"},
6024 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
6025 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
6026 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
6027 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
6028 "{ [i, j] -> [floor((i)/3), j] }",
6029 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
6032 static int test_pullback(isl_ctx *ctx)
6034 int i;
6035 isl_multi_aff *ma1, *ma2;
6036 isl_multi_aff *ma;
6037 int equal;
6039 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
6040 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
6041 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
6042 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
6043 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
6044 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
6045 isl_multi_aff_free(ma1);
6046 isl_multi_aff_free(ma2);
6047 if (equal < 0)
6048 return -1;
6049 if (!equal)
6050 isl_die(ctx, isl_error_unknown, "bad pullback",
6051 return -1);
6054 return 0;
6057 /* Check that negation is printed correctly and that equal expressions
6058 * are correctly identified.
6060 static int test_ast(isl_ctx *ctx)
6062 isl_ast_expr *expr, *expr1, *expr2, *expr3;
6063 char *str;
6064 int ok, equal;
6066 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
6067 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
6068 expr = isl_ast_expr_add(expr1, expr2);
6069 expr2 = isl_ast_expr_copy(expr);
6070 expr = isl_ast_expr_neg(expr);
6071 expr2 = isl_ast_expr_neg(expr2);
6072 equal = isl_ast_expr_is_equal(expr, expr2);
6073 str = isl_ast_expr_to_C_str(expr);
6074 ok = str ? !strcmp(str, "-(A + B)") : -1;
6075 free(str);
6076 isl_ast_expr_free(expr);
6077 isl_ast_expr_free(expr2);
6079 if (ok < 0 || equal < 0)
6080 return -1;
6081 if (!equal)
6082 isl_die(ctx, isl_error_unknown,
6083 "equal expressions not considered equal", return -1);
6084 if (!ok)
6085 isl_die(ctx, isl_error_unknown,
6086 "isl_ast_expr printed incorrectly", return -1);
6088 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
6089 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
6090 expr = isl_ast_expr_add(expr1, expr2);
6091 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
6092 expr = isl_ast_expr_sub(expr3, expr);
6093 str = isl_ast_expr_to_C_str(expr);
6094 ok = str ? !strcmp(str, "C - (A + B)") : -1;
6095 free(str);
6096 isl_ast_expr_free(expr);
6098 if (ok < 0)
6099 return -1;
6100 if (!ok)
6101 isl_die(ctx, isl_error_unknown,
6102 "isl_ast_expr printed incorrectly", return -1);
6104 return 0;
6107 /* Check that isl_ast_build_expr_from_set returns a valid expression
6108 * for an empty set. Note that isl_ast_build_expr_from_set getting
6109 * called on an empty set probably indicates a bug in the caller.
6111 static int test_ast_build(isl_ctx *ctx)
6113 isl_set *set;
6114 isl_ast_build *build;
6115 isl_ast_expr *expr;
6117 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6118 build = isl_ast_build_from_context(set);
6120 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
6121 expr = isl_ast_build_expr_from_set(build, set);
6123 isl_ast_expr_free(expr);
6124 isl_ast_build_free(build);
6126 if (!expr)
6127 return -1;
6129 return 0;
6132 /* Internal data structure for before_for and after_for callbacks.
6134 * depth is the current depth
6135 * before is the number of times before_for has been called
6136 * after is the number of times after_for has been called
6138 struct isl_test_codegen_data {
6139 int depth;
6140 int before;
6141 int after;
6144 /* This function is called before each for loop in the AST generated
6145 * from test_ast_gen1.
6147 * Increment the number of calls and the depth.
6148 * Check that the space returned by isl_ast_build_get_schedule_space
6149 * matches the target space of the schedule returned by
6150 * isl_ast_build_get_schedule.
6151 * Return an isl_id that is checked by the corresponding call
6152 * to after_for.
6154 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
6155 void *user)
6157 struct isl_test_codegen_data *data = user;
6158 isl_ctx *ctx;
6159 isl_space *space;
6160 isl_union_map *schedule;
6161 isl_union_set *uset;
6162 isl_set *set;
6163 int empty;
6164 char name[] = "d0";
6166 ctx = isl_ast_build_get_ctx(build);
6168 if (data->before >= 3)
6169 isl_die(ctx, isl_error_unknown,
6170 "unexpected number of for nodes", return NULL);
6171 if (data->depth >= 2)
6172 isl_die(ctx, isl_error_unknown,
6173 "unexpected depth", return NULL);
6175 snprintf(name, sizeof(name), "d%d", data->depth);
6176 data->before++;
6177 data->depth++;
6179 schedule = isl_ast_build_get_schedule(build);
6180 uset = isl_union_map_range(schedule);
6181 if (!uset)
6182 return NULL;
6183 if (isl_union_set_n_set(uset) != 1) {
6184 isl_union_set_free(uset);
6185 isl_die(ctx, isl_error_unknown,
6186 "expecting single range space", return NULL);
6189 space = isl_ast_build_get_schedule_space(build);
6190 set = isl_union_set_extract_set(uset, space);
6191 isl_union_set_free(uset);
6192 empty = isl_set_is_empty(set);
6193 isl_set_free(set);
6195 if (empty < 0)
6196 return NULL;
6197 if (empty)
6198 isl_die(ctx, isl_error_unknown,
6199 "spaces don't match", return NULL);
6201 return isl_id_alloc(ctx, name, NULL);
6204 /* This function is called after each for loop in the AST generated
6205 * from test_ast_gen1.
6207 * Increment the number of calls and decrement the depth.
6208 * Check that the annotation attached to the node matches
6209 * the isl_id returned by the corresponding call to before_for.
6211 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
6212 __isl_keep isl_ast_build *build, void *user)
6214 struct isl_test_codegen_data *data = user;
6215 isl_id *id;
6216 const char *name;
6217 int valid;
6219 data->after++;
6220 data->depth--;
6222 if (data->after > data->before)
6223 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
6224 "mismatch in number of for nodes",
6225 return isl_ast_node_free(node));
6227 id = isl_ast_node_get_annotation(node);
6228 if (!id)
6229 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
6230 "missing annotation", return isl_ast_node_free(node));
6232 name = isl_id_get_name(id);
6233 valid = name && atoi(name + 1) == data->depth;
6234 isl_id_free(id);
6236 if (!valid)
6237 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
6238 "wrong annotation", return isl_ast_node_free(node));
6240 return node;
6243 /* Check that the before_each_for and after_each_for callbacks
6244 * are called for each for loop in the generated code,
6245 * that they are called in the right order and that the isl_id
6246 * returned from the before_each_for callback is attached to
6247 * the isl_ast_node passed to the corresponding after_each_for call.
6249 static int test_ast_gen1(isl_ctx *ctx)
6251 const char *str;
6252 isl_set *set;
6253 isl_union_map *schedule;
6254 isl_ast_build *build;
6255 isl_ast_node *tree;
6256 struct isl_test_codegen_data data;
6258 str = "[N] -> { : N >= 10 }";
6259 set = isl_set_read_from_str(ctx, str);
6260 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
6261 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
6262 schedule = isl_union_map_read_from_str(ctx, str);
6264 data.before = 0;
6265 data.after = 0;
6266 data.depth = 0;
6267 build = isl_ast_build_from_context(set);
6268 build = isl_ast_build_set_before_each_for(build,
6269 &before_for, &data);
6270 build = isl_ast_build_set_after_each_for(build,
6271 &after_for, &data);
6272 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6273 isl_ast_build_free(build);
6274 if (!tree)
6275 return -1;
6277 isl_ast_node_free(tree);
6279 if (data.before != 3 || data.after != 3)
6280 isl_die(ctx, isl_error_unknown,
6281 "unexpected number of for nodes", return -1);
6283 return 0;
6286 /* Check that the AST generator handles domains that are integrally disjoint
6287 * but not rationally disjoint.
6289 static int test_ast_gen2(isl_ctx *ctx)
6291 const char *str;
6292 isl_set *set;
6293 isl_union_map *schedule;
6294 isl_union_map *options;
6295 isl_ast_build *build;
6296 isl_ast_node *tree;
6298 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
6299 schedule = isl_union_map_read_from_str(ctx, str);
6300 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6301 build = isl_ast_build_from_context(set);
6303 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
6304 options = isl_union_map_read_from_str(ctx, str);
6305 build = isl_ast_build_set_options(build, options);
6306 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6307 isl_ast_build_free(build);
6308 if (!tree)
6309 return -1;
6310 isl_ast_node_free(tree);
6312 return 0;
6315 /* Increment *user on each call.
6317 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
6318 __isl_keep isl_ast_build *build, void *user)
6320 int *n = user;
6322 (*n)++;
6324 return node;
6327 /* Test that unrolling tries to minimize the number of instances.
6328 * In particular, for the schedule given below, make sure it generates
6329 * 3 nodes (rather than 101).
6331 static int test_ast_gen3(isl_ctx *ctx)
6333 const char *str;
6334 isl_set *set;
6335 isl_union_map *schedule;
6336 isl_union_map *options;
6337 isl_ast_build *build;
6338 isl_ast_node *tree;
6339 int n_domain = 0;
6341 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
6342 schedule = isl_union_map_read_from_str(ctx, str);
6343 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6345 str = "{ [i] -> unroll[0] }";
6346 options = isl_union_map_read_from_str(ctx, str);
6348 build = isl_ast_build_from_context(set);
6349 build = isl_ast_build_set_options(build, options);
6350 build = isl_ast_build_set_at_each_domain(build,
6351 &count_domains, &n_domain);
6352 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6353 isl_ast_build_free(build);
6354 if (!tree)
6355 return -1;
6357 isl_ast_node_free(tree);
6359 if (n_domain != 3)
6360 isl_die(ctx, isl_error_unknown,
6361 "unexpected number of for nodes", return -1);
6363 return 0;
6366 /* Check that if the ast_build_exploit_nested_bounds options is set,
6367 * we do not get an outer if node in the generated AST,
6368 * while we do get such an outer if node if the options is not set.
6370 static int test_ast_gen4(isl_ctx *ctx)
6372 const char *str;
6373 isl_set *set;
6374 isl_union_map *schedule;
6375 isl_ast_build *build;
6376 isl_ast_node *tree;
6377 enum isl_ast_node_type type;
6378 int enb;
6380 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
6381 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
6383 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
6385 schedule = isl_union_map_read_from_str(ctx, str);
6386 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6387 build = isl_ast_build_from_context(set);
6388 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6389 isl_ast_build_free(build);
6390 if (!tree)
6391 return -1;
6393 type = isl_ast_node_get_type(tree);
6394 isl_ast_node_free(tree);
6396 if (type == isl_ast_node_if)
6397 isl_die(ctx, isl_error_unknown,
6398 "not expecting if node", return -1);
6400 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
6402 schedule = isl_union_map_read_from_str(ctx, str);
6403 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6404 build = isl_ast_build_from_context(set);
6405 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6406 isl_ast_build_free(build);
6407 if (!tree)
6408 return -1;
6410 type = isl_ast_node_get_type(tree);
6411 isl_ast_node_free(tree);
6413 if (type != isl_ast_node_if)
6414 isl_die(ctx, isl_error_unknown,
6415 "expecting if node", return -1);
6417 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
6419 return 0;
6422 /* This function is called for each leaf in the AST generated
6423 * from test_ast_gen5.
6425 * We finalize the AST generation by extending the outer schedule
6426 * with a zero-dimensional schedule. If this results in any for loops,
6427 * then this means that we did not pass along enough information
6428 * about the outer schedule to the inner AST generation.
6430 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
6431 void *user)
6433 isl_union_map *schedule, *extra;
6434 isl_ast_node *tree;
6436 schedule = isl_ast_build_get_schedule(build);
6437 extra = isl_union_map_copy(schedule);
6438 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
6439 schedule = isl_union_map_range_product(schedule, extra);
6440 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6441 isl_ast_build_free(build);
6443 if (!tree)
6444 return NULL;
6446 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
6447 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
6448 "code should not contain any for loop",
6449 return isl_ast_node_free(tree));
6451 return tree;
6454 /* Check that we do not lose any information when going back and
6455 * forth between internal and external schedule.
6457 * In particular, we create an AST where we unroll the only
6458 * non-constant dimension in the schedule. We therefore do
6459 * not expect any for loops in the AST. However, older versions
6460 * of isl would not pass along enough information about the outer
6461 * schedule when performing an inner code generation from a create_leaf
6462 * callback, resulting in the inner code generation producing a for loop.
6464 static int test_ast_gen5(isl_ctx *ctx)
6466 const char *str;
6467 isl_set *set;
6468 isl_union_map *schedule, *options;
6469 isl_ast_build *build;
6470 isl_ast_node *tree;
6472 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
6473 schedule = isl_union_map_read_from_str(ctx, str);
6475 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
6476 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
6477 options = isl_union_map_read_from_str(ctx, str);
6479 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6480 build = isl_ast_build_from_context(set);
6481 build = isl_ast_build_set_options(build, options);
6482 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
6483 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6484 isl_ast_build_free(build);
6485 isl_ast_node_free(tree);
6486 if (!tree)
6487 return -1;
6489 return 0;
6492 /* Check that the expression
6494 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
6496 * is not combined into
6498 * min(n/2, 0)
6500 * as this would result in n/2 being evaluated in parts of
6501 * the definition domain where n is not a multiple of 2.
6503 static int test_ast_expr(isl_ctx *ctx)
6505 const char *str;
6506 isl_pw_aff *pa;
6507 isl_ast_build *build;
6508 isl_ast_expr *expr;
6509 int min_max;
6510 int is_min;
6512 min_max = isl_options_get_ast_build_detect_min_max(ctx);
6513 isl_options_set_ast_build_detect_min_max(ctx, 1);
6515 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
6516 pa = isl_pw_aff_read_from_str(ctx, str);
6517 build = isl_ast_build_alloc(ctx);
6518 expr = isl_ast_build_expr_from_pw_aff(build, pa);
6519 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
6520 isl_ast_expr_get_op_type(expr) == isl_ast_op_min;
6521 isl_ast_build_free(build);
6522 isl_ast_expr_free(expr);
6524 isl_options_set_ast_build_detect_min_max(ctx, min_max);
6526 if (!expr)
6527 return -1;
6528 if (is_min)
6529 isl_die(ctx, isl_error_unknown,
6530 "expressions should not be combined", return -1);
6532 return 0;
6535 static int test_ast_gen(isl_ctx *ctx)
6537 if (test_ast_gen1(ctx) < 0)
6538 return -1;
6539 if (test_ast_gen2(ctx) < 0)
6540 return -1;
6541 if (test_ast_gen3(ctx) < 0)
6542 return -1;
6543 if (test_ast_gen4(ctx) < 0)
6544 return -1;
6545 if (test_ast_gen5(ctx) < 0)
6546 return -1;
6547 if (test_ast_expr(ctx) < 0)
6548 return -1;
6549 return 0;
6552 /* Check if dropping output dimensions from an isl_pw_multi_aff
6553 * works properly.
6555 static int test_pw_multi_aff(isl_ctx *ctx)
6557 const char *str;
6558 isl_pw_multi_aff *pma1, *pma2;
6559 int equal;
6561 str = "{ [i,j] -> [i+j, 4i-j] }";
6562 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
6563 str = "{ [i,j] -> [4i-j] }";
6564 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6566 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
6568 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6570 isl_pw_multi_aff_free(pma1);
6571 isl_pw_multi_aff_free(pma2);
6572 if (equal < 0)
6573 return -1;
6574 if (!equal)
6575 isl_die(ctx, isl_error_unknown,
6576 "expressions not equal", return -1);
6578 return 0;
6581 /* Check that we can properly parse multi piecewise affine expressions
6582 * where the piecewise affine expressions have different domains.
6584 static int test_multi_pw_aff(isl_ctx *ctx)
6586 const char *str;
6587 isl_set *dom, *dom2;
6588 isl_multi_pw_aff *mpa1, *mpa2;
6589 isl_pw_aff *pa;
6590 int equal;
6591 int equal_domain;
6593 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
6594 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
6595 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
6596 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
6597 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
6598 str = "{ [i] -> [(i : i > 0), 2i] }";
6599 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
6601 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
6603 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
6604 dom = isl_pw_aff_domain(pa);
6605 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
6606 dom2 = isl_pw_aff_domain(pa);
6607 equal_domain = isl_set_is_equal(dom, dom2);
6609 isl_set_free(dom);
6610 isl_set_free(dom2);
6611 isl_multi_pw_aff_free(mpa1);
6612 isl_multi_pw_aff_free(mpa2);
6614 if (equal < 0)
6615 return -1;
6616 if (!equal)
6617 isl_die(ctx, isl_error_unknown,
6618 "expressions not equal", return -1);
6620 if (equal_domain < 0)
6621 return -1;
6622 if (equal_domain)
6623 isl_die(ctx, isl_error_unknown,
6624 "domains unexpectedly equal", return -1);
6626 return 0;
6629 /* This is a regression test for a bug where isl_basic_map_simplify
6630 * would end up in an infinite loop. In particular, we construct
6631 * an empty basic set that is not obviously empty.
6632 * isl_basic_set_is_empty marks the basic set as empty.
6633 * After projecting out i3, the variable can be dropped completely,
6634 * but isl_basic_map_simplify refrains from doing so if the basic set
6635 * is empty and would end up in an infinite loop if it didn't test
6636 * explicitly for empty basic maps in the outer loop.
6638 static int test_simplify_1(isl_ctx *ctx)
6640 const char *str;
6641 isl_basic_set *bset;
6642 int empty;
6644 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
6645 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
6646 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
6647 "i3 >= i2 }";
6648 bset = isl_basic_set_read_from_str(ctx, str);
6649 empty = isl_basic_set_is_empty(bset);
6650 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
6651 isl_basic_set_free(bset);
6652 if (!bset)
6653 return -1;
6654 if (!empty)
6655 isl_die(ctx, isl_error_unknown,
6656 "basic set should be empty", return -1);
6658 return 0;
6661 /* Check that the equality in the set description below
6662 * is simplified away.
6664 static int test_simplify_2(isl_ctx *ctx)
6666 const char *str;
6667 isl_basic_set *bset;
6668 isl_bool universe;
6670 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
6671 bset = isl_basic_set_read_from_str(ctx, str);
6672 universe = isl_basic_set_plain_is_universe(bset);
6673 isl_basic_set_free(bset);
6675 if (universe < 0)
6676 return -1;
6677 if (!universe)
6678 isl_die(ctx, isl_error_unknown,
6679 "equality not simplified away", return -1);
6680 return 0;
6683 /* Some simplification tests.
6685 static int test_simplify(isl_ctx *ctx)
6687 if (test_simplify_1(ctx) < 0)
6688 return -1;
6689 if (test_simplify_2(ctx) < 0)
6690 return -1;
6691 return 0;
6694 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
6695 * with gbr context would fail to disable the use of the shifted tableau
6696 * when transferring equalities for the input to the context, resulting
6697 * in invalid sample values.
6699 static int test_partial_lexmin(isl_ctx *ctx)
6701 const char *str;
6702 isl_basic_set *bset;
6703 isl_basic_map *bmap;
6704 isl_map *map;
6706 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
6707 bmap = isl_basic_map_read_from_str(ctx, str);
6708 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
6709 bset = isl_basic_set_read_from_str(ctx, str);
6710 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
6711 isl_map_free(map);
6713 if (!map)
6714 return -1;
6716 return 0;
6719 /* Check that the variable compression performed on the existentially
6720 * quantified variables inside isl_basic_set_compute_divs is not confused
6721 * by the implicit equalities among the parameters.
6723 static int test_compute_divs(isl_ctx *ctx)
6725 const char *str;
6726 isl_basic_set *bset;
6727 isl_set *set;
6729 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
6730 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
6731 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
6732 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
6733 bset = isl_basic_set_read_from_str(ctx, str);
6734 set = isl_basic_set_compute_divs(bset);
6735 isl_set_free(set);
6736 if (!set)
6737 return -1;
6739 return 0;
6742 /* Check that the reaching domain elements and the prefix schedule
6743 * at a leaf node are the same before and after grouping.
6745 static int test_schedule_tree_group_1(isl_ctx *ctx)
6747 int equal;
6748 const char *str;
6749 isl_id *id;
6750 isl_union_set *uset;
6751 isl_multi_union_pw_aff *mupa;
6752 isl_union_pw_multi_aff *upma1, *upma2;
6753 isl_union_set *domain1, *domain2;
6754 isl_union_map *umap1, *umap2;
6755 isl_schedule_node *node;
6757 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
6758 uset = isl_union_set_read_from_str(ctx, str);
6759 node = isl_schedule_node_from_domain(uset);
6760 node = isl_schedule_node_child(node, 0);
6761 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
6762 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6763 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6764 node = isl_schedule_node_child(node, 0);
6765 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
6766 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6767 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6768 node = isl_schedule_node_child(node, 0);
6769 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
6770 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
6771 domain1 = isl_schedule_node_get_domain(node);
6772 id = isl_id_alloc(ctx, "group", NULL);
6773 node = isl_schedule_node_parent(node);
6774 node = isl_schedule_node_group(node, id);
6775 node = isl_schedule_node_child(node, 0);
6776 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
6777 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
6778 domain2 = isl_schedule_node_get_domain(node);
6779 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
6780 if (equal >= 0 && equal)
6781 equal = isl_union_set_is_equal(domain1, domain2);
6782 if (equal >= 0 && equal)
6783 equal = isl_union_map_is_equal(umap1, umap2);
6784 isl_union_map_free(umap1);
6785 isl_union_map_free(umap2);
6786 isl_union_set_free(domain1);
6787 isl_union_set_free(domain2);
6788 isl_union_pw_multi_aff_free(upma1);
6789 isl_union_pw_multi_aff_free(upma2);
6790 isl_schedule_node_free(node);
6792 if (equal < 0)
6793 return -1;
6794 if (!equal)
6795 isl_die(ctx, isl_error_unknown,
6796 "expressions not equal", return -1);
6798 return 0;
6801 /* Check that we can have nested groupings and that the union map
6802 * schedule representation is the same before and after the grouping.
6803 * Note that after the grouping, the union map representation contains
6804 * the domain constraints from the ranges of the expansion nodes,
6805 * while they are missing from the union map representation of
6806 * the tree without expansion nodes.
6808 * Also check that the global expansion is as expected.
6810 static int test_schedule_tree_group_2(isl_ctx *ctx)
6812 int equal, equal_expansion;
6813 const char *str;
6814 isl_id *id;
6815 isl_union_set *uset;
6816 isl_union_map *umap1, *umap2;
6817 isl_union_map *expansion1, *expansion2;
6818 isl_union_set_list *filters;
6819 isl_multi_union_pw_aff *mupa;
6820 isl_schedule *schedule;
6821 isl_schedule_node *node;
6823 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
6824 "S3[i,j] : 0 <= i,j < 10 }";
6825 uset = isl_union_set_read_from_str(ctx, str);
6826 node = isl_schedule_node_from_domain(uset);
6827 node = isl_schedule_node_child(node, 0);
6828 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
6829 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6830 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6831 node = isl_schedule_node_child(node, 0);
6832 str = "{ S1[i,j] }";
6833 uset = isl_union_set_read_from_str(ctx, str);
6834 filters = isl_union_set_list_from_union_set(uset);
6835 str = "{ S2[i,j]; S3[i,j] }";
6836 uset = isl_union_set_read_from_str(ctx, str);
6837 filters = isl_union_set_list_add(filters, uset);
6838 node = isl_schedule_node_insert_sequence(node, filters);
6839 node = isl_schedule_node_child(node, 1);
6840 node = isl_schedule_node_child(node, 0);
6841 str = "{ S2[i,j] }";
6842 uset = isl_union_set_read_from_str(ctx, str);
6843 filters = isl_union_set_list_from_union_set(uset);
6844 str = "{ S3[i,j] }";
6845 uset = isl_union_set_read_from_str(ctx, str);
6846 filters = isl_union_set_list_add(filters, uset);
6847 node = isl_schedule_node_insert_sequence(node, filters);
6849 schedule = isl_schedule_node_get_schedule(node);
6850 umap1 = isl_schedule_get_map(schedule);
6851 uset = isl_schedule_get_domain(schedule);
6852 umap1 = isl_union_map_intersect_domain(umap1, uset);
6853 isl_schedule_free(schedule);
6855 node = isl_schedule_node_parent(node);
6856 node = isl_schedule_node_parent(node);
6857 id = isl_id_alloc(ctx, "group1", NULL);
6858 node = isl_schedule_node_group(node, id);
6859 node = isl_schedule_node_child(node, 1);
6860 node = isl_schedule_node_child(node, 0);
6861 id = isl_id_alloc(ctx, "group2", NULL);
6862 node = isl_schedule_node_group(node, id);
6864 schedule = isl_schedule_node_get_schedule(node);
6865 umap2 = isl_schedule_get_map(schedule);
6866 isl_schedule_free(schedule);
6868 node = isl_schedule_node_root(node);
6869 node = isl_schedule_node_child(node, 0);
6870 expansion1 = isl_schedule_node_get_subtree_expansion(node);
6871 isl_schedule_node_free(node);
6873 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
6874 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
6875 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
6877 expansion2 = isl_union_map_read_from_str(ctx, str);
6879 equal = isl_union_map_is_equal(umap1, umap2);
6880 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
6882 isl_union_map_free(umap1);
6883 isl_union_map_free(umap2);
6884 isl_union_map_free(expansion1);
6885 isl_union_map_free(expansion2);
6887 if (equal < 0 || equal_expansion < 0)
6888 return -1;
6889 if (!equal)
6890 isl_die(ctx, isl_error_unknown,
6891 "expressions not equal", return -1);
6892 if (!equal_expansion)
6893 isl_die(ctx, isl_error_unknown,
6894 "unexpected expansion", return -1);
6896 return 0;
6899 /* Some tests for the isl_schedule_node_group function.
6901 static int test_schedule_tree_group(isl_ctx *ctx)
6903 if (test_schedule_tree_group_1(ctx) < 0)
6904 return -1;
6905 if (test_schedule_tree_group_2(ctx) < 0)
6906 return -1;
6907 return 0;
6910 struct {
6911 const char *set;
6912 const char *dual;
6913 } coef_tests[] = {
6914 { "{ rat: [i] : 0 <= i <= 10 }",
6915 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
6916 { "{ rat: [i] : FALSE }",
6917 "{ rat: coefficients[[cst] -> [a]] }" },
6918 { "{ rat: [i] : }",
6919 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
6922 struct {
6923 const char *set;
6924 const char *dual;
6925 } sol_tests[] = {
6926 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
6927 "{ rat: [i] : 0 <= i <= 10 }" },
6928 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
6929 "{ rat: [i] }" },
6930 { "{ rat: coefficients[[cst] -> [a]] }",
6931 "{ rat: [i] : FALSE }" },
6934 /* Test the basic functionality of isl_basic_set_coefficients and
6935 * isl_basic_set_solutions.
6937 static int test_dual(isl_ctx *ctx)
6939 int i;
6941 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
6942 int equal;
6943 isl_basic_set *bset1, *bset2;
6945 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
6946 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
6947 bset1 = isl_basic_set_coefficients(bset1);
6948 equal = isl_basic_set_is_equal(bset1, bset2);
6949 isl_basic_set_free(bset1);
6950 isl_basic_set_free(bset2);
6951 if (equal < 0)
6952 return -1;
6953 if (!equal)
6954 isl_die(ctx, isl_error_unknown,
6955 "incorrect dual", return -1);
6958 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
6959 int equal;
6960 isl_basic_set *bset1, *bset2;
6962 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
6963 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
6964 bset1 = isl_basic_set_solutions(bset1);
6965 equal = isl_basic_set_is_equal(bset1, bset2);
6966 isl_basic_set_free(bset1);
6967 isl_basic_set_free(bset2);
6968 if (equal < 0)
6969 return -1;
6970 if (!equal)
6971 isl_die(ctx, isl_error_unknown,
6972 "incorrect dual", return -1);
6975 return 0;
6978 struct {
6979 int scale_tile;
6980 int shift_point;
6981 const char *domain;
6982 const char *schedule;
6983 const char *sizes;
6984 const char *tile;
6985 const char *point;
6986 } tile_tests[] = {
6987 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6988 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6989 "{ [32,32] }",
6990 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6991 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6993 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6994 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6995 "{ [32,32] }",
6996 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6997 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6999 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
7000 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7001 "{ [32,32] }",
7002 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
7003 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
7005 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
7006 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7007 "{ [32,32] }",
7008 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
7009 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
7013 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
7014 * tile the band and then check if the tile and point bands have the
7015 * expected partial schedule.
7017 static int test_tile(isl_ctx *ctx)
7019 int i;
7020 int scale;
7021 int shift;
7023 scale = isl_options_get_tile_scale_tile_loops(ctx);
7024 shift = isl_options_get_tile_shift_point_loops(ctx);
7026 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
7027 int opt;
7028 int equal;
7029 const char *str;
7030 isl_union_set *domain;
7031 isl_multi_union_pw_aff *mupa, *mupa2;
7032 isl_schedule_node *node;
7033 isl_multi_val *sizes;
7035 opt = tile_tests[i].scale_tile;
7036 isl_options_set_tile_scale_tile_loops(ctx, opt);
7037 opt = tile_tests[i].shift_point;
7038 isl_options_set_tile_shift_point_loops(ctx, opt);
7040 str = tile_tests[i].domain;
7041 domain = isl_union_set_read_from_str(ctx, str);
7042 node = isl_schedule_node_from_domain(domain);
7043 node = isl_schedule_node_child(node, 0);
7044 str = tile_tests[i].schedule;
7045 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7046 node = isl_schedule_node_insert_partial_schedule(node, mupa);
7047 str = tile_tests[i].sizes;
7048 sizes = isl_multi_val_read_from_str(ctx, str);
7049 node = isl_schedule_node_band_tile(node, sizes);
7051 str = tile_tests[i].tile;
7052 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7053 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
7054 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
7055 isl_multi_union_pw_aff_free(mupa);
7056 isl_multi_union_pw_aff_free(mupa2);
7058 node = isl_schedule_node_child(node, 0);
7060 str = tile_tests[i].point;
7061 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7062 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
7063 if (equal >= 0 && equal)
7064 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
7065 mupa2);
7066 isl_multi_union_pw_aff_free(mupa);
7067 isl_multi_union_pw_aff_free(mupa2);
7069 isl_schedule_node_free(node);
7071 if (equal < 0)
7072 return -1;
7073 if (!equal)
7074 isl_die(ctx, isl_error_unknown,
7075 "unexpected result", return -1);
7078 isl_options_set_tile_scale_tile_loops(ctx, scale);
7079 isl_options_set_tile_shift_point_loops(ctx, shift);
7081 return 0;
7084 /* Check that the domain hash of a space is equal to the hash
7085 * of the domain of the space.
7087 static int test_domain_hash(isl_ctx *ctx)
7089 isl_map *map;
7090 isl_space *space;
7091 uint32_t hash1, hash2;
7093 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
7094 space = isl_map_get_space(map);
7095 isl_map_free(map);
7096 hash1 = isl_space_get_domain_hash(space);
7097 space = isl_space_domain(space);
7098 hash2 = isl_space_get_hash(space);
7099 isl_space_free(space);
7101 if (!space)
7102 return -1;
7103 if (hash1 != hash2)
7104 isl_die(ctx, isl_error_unknown,
7105 "domain hash not equal to hash of domain", return -1);
7107 return 0;
7110 /* Check that a universe basic set that is not obviously equal to the universe
7111 * is still recognized as being equal to the universe.
7113 static int test_universe(isl_ctx *ctx)
7115 const char *s;
7116 isl_basic_set *bset;
7117 isl_bool is_univ;
7119 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
7120 bset = isl_basic_set_read_from_str(ctx, s);
7121 is_univ = isl_basic_set_is_universe(bset);
7122 isl_basic_set_free(bset);
7124 if (is_univ < 0)
7125 return -1;
7126 if (!is_univ)
7127 isl_die(ctx, isl_error_unknown,
7128 "not recognized as universe set", return -1);
7130 return 0;
7133 /* Sets for which chambers are computed and checked.
7135 const char *chambers_tests[] = {
7136 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
7137 "z >= 0 and z <= C - y and z <= B - x - y }",
7140 /* Add the domain of "cell" to "cells".
7142 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
7144 isl_basic_set_list **cells = user;
7145 isl_basic_set *dom;
7147 dom = isl_cell_get_domain(cell);
7148 isl_cell_free(cell);
7149 *cells = isl_basic_set_list_add(*cells, dom);
7151 return *cells ? isl_stat_ok : isl_stat_error;
7154 /* Check that the elements of "list" are pairwise disjoint.
7156 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
7158 int i, j, n;
7160 if (!list)
7161 return isl_stat_error;
7163 n = isl_basic_set_list_n_basic_set(list);
7164 for (i = 0; i < n; ++i) {
7165 isl_basic_set *bset_i;
7167 bset_i = isl_basic_set_list_get_basic_set(list, i);
7168 for (j = i + 1; j < n; ++j) {
7169 isl_basic_set *bset_j;
7170 isl_bool disjoint;
7172 bset_j = isl_basic_set_list_get_basic_set(list, j);
7173 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
7174 isl_basic_set_free(bset_j);
7175 if (!disjoint)
7176 isl_die(isl_basic_set_list_get_ctx(list),
7177 isl_error_unknown, "not disjoint",
7178 break);
7179 if (disjoint < 0 || !disjoint)
7180 break;
7182 isl_basic_set_free(bset_i);
7183 if (j < n)
7184 return isl_stat_error;
7187 return isl_stat_ok;
7190 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
7191 * are pairwise disjoint.
7193 static int test_chambers(isl_ctx *ctx)
7195 int i;
7197 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
7198 isl_basic_set *bset;
7199 isl_vertices *vertices;
7200 isl_basic_set_list *cells;
7201 isl_stat ok;
7203 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
7204 vertices = isl_basic_set_compute_vertices(bset);
7205 cells = isl_basic_set_list_alloc(ctx, 0);
7206 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
7207 &cells) < 0)
7208 cells = isl_basic_set_list_free(cells);
7209 ok = check_pairwise_disjoint(cells);
7210 isl_basic_set_list_free(cells);
7211 isl_vertices_free(vertices);
7212 isl_basic_set_free(bset);
7214 if (ok < 0)
7215 return -1;
7218 return 0;
7221 struct {
7222 const char *name;
7223 int (*fn)(isl_ctx *ctx);
7224 } tests [] = {
7225 { "universe", &test_universe },
7226 { "domain hash", &test_domain_hash },
7227 { "dual", &test_dual },
7228 { "dependence analysis", &test_flow },
7229 { "val", &test_val },
7230 { "compute divs", &test_compute_divs },
7231 { "partial lexmin", &test_partial_lexmin },
7232 { "simplify", &test_simplify },
7233 { "curry", &test_curry },
7234 { "piecewise multi affine expressions", &test_pw_multi_aff },
7235 { "multi piecewise affine expressions", &test_multi_pw_aff },
7236 { "conversion", &test_conversion },
7237 { "list", &test_list },
7238 { "align parameters", &test_align_parameters },
7239 { "preimage", &test_preimage },
7240 { "pullback", &test_pullback },
7241 { "AST", &test_ast },
7242 { "AST build", &test_ast_build },
7243 { "AST generation", &test_ast_gen },
7244 { "eliminate", &test_eliminate },
7245 { "residue class", &test_residue_class },
7246 { "div", &test_div },
7247 { "slice", &test_slice },
7248 { "fixed power", &test_fixed_power },
7249 { "sample", &test_sample },
7250 { "output", &test_output },
7251 { "vertices", &test_vertices },
7252 { "chambers", &test_chambers },
7253 { "fixed", &test_fixed },
7254 { "equal", &test_equal },
7255 { "disjoint", &test_disjoint },
7256 { "product", &test_product },
7257 { "dim_max", &test_dim_max },
7258 { "affine", &test_aff },
7259 { "injective", &test_injective },
7260 { "schedule (whole component)", &test_schedule_whole },
7261 { "schedule (incremental)", &test_schedule_incremental },
7262 { "schedule tree grouping", &test_schedule_tree_group },
7263 { "tile", &test_tile },
7264 { "union_pw", &test_union_pw },
7265 { "eval", &test_eval },
7266 { "parse", &test_parse },
7267 { "single-valued", &test_sv },
7268 { "affine hull", &test_affine_hull },
7269 { "simple_hull", &test_simple_hull },
7270 { "coalesce", &test_coalesce },
7271 { "factorize", &test_factorize },
7272 { "subset", &test_subset },
7273 { "subtract", &test_subtract },
7274 { "intersect", &test_intersect },
7275 { "lexmin", &test_lexmin },
7276 { "min", &test_min },
7277 { "gist", &test_gist },
7278 { "piecewise quasi-polynomials", &test_pwqp },
7279 { "lift", &test_lift },
7280 { "bound", &test_bound },
7281 { "union", &test_union },
7282 { "split periods", &test_split_periods },
7283 { "lexicographic order", &test_lex },
7284 { "bijectivity", &test_bijective },
7285 { "dataflow analysis", &test_dep },
7286 { "reading", &test_read },
7287 { "bounded", &test_bounded },
7288 { "construction", &test_construction },
7289 { "dimension manipulation", &test_dim },
7290 { "map application", &test_application },
7291 { "convex hull", &test_convex_hull },
7292 { "transitive closure", &test_closure },
7295 int main(int argc, char **argv)
7297 int i;
7298 struct isl_ctx *ctx;
7299 struct isl_options *options;
7301 options = isl_options_new_with_defaults();
7302 assert(options);
7303 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
7305 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
7306 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
7307 printf("%s\n", tests[i].name);
7308 if (tests[i].fn(ctx) < 0)
7309 goto error;
7311 isl_ctx_free(ctx);
7312 return 0;
7313 error:
7314 isl_ctx_free(ctx);
7315 return -1;