drop isl_basic_map_foreach_lexopt
[isl.git] / isl_test.c
blob46b18dcc96e38d91a3e750e28e0ee609bedef9aa
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 isl_bool ok, is_nan;
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 is_nan = isl_val_is_nan(res);
526 if (is_nan < 0)
527 ok = isl_bool_error;
528 else if (is_nan)
529 ok = isl_val_is_nan(v);
530 else
531 ok = isl_val_eq(v, res);
532 isl_val_free(v);
533 isl_val_free(res);
534 if (ok < 0)
535 return -1;
536 if (!ok)
537 isl_die(ctx, isl_error_unknown,
538 "unexpected result", return -1);
541 return 0;
544 struct {
545 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
546 __isl_take isl_val *v2);
547 } val_bin_op[] = {
548 ['+'] = { &isl_val_add },
549 ['-'] = { &isl_val_sub },
550 ['*'] = { &isl_val_mul },
551 ['/'] = { &isl_val_div },
552 ['g'] = { &isl_val_gcd },
553 ['m'] = { &isl_val_min },
554 ['M'] = { &isl_val_max },
557 struct {
558 const char *arg1;
559 unsigned char op;
560 const char *arg2;
561 const char *res;
562 } val_bin_tests[] = {
563 { "0", '+', "0", "0" },
564 { "1", '+', "0", "1" },
565 { "1", '+', "1", "2" },
566 { "1", '-', "1", "0" },
567 { "1", '*', "1", "1" },
568 { "1", '/', "1", "1" },
569 { "2", '*', "3", "6" },
570 { "2", '*', "1/2", "1" },
571 { "2", '*', "1/3", "2/3" },
572 { "2/3", '*', "3/5", "2/5" },
573 { "2/3", '*', "7/5", "14/15" },
574 { "2", '/', "1/2", "4" },
575 { "-2", '/', "-1/2", "4" },
576 { "-2", '/', "1/2", "-4" },
577 { "2", '/', "-1/2", "-4" },
578 { "2", '/', "2", "1" },
579 { "2", '/', "3", "2/3" },
580 { "2/3", '/', "5/3", "2/5" },
581 { "2/3", '/', "5/7", "14/15" },
582 { "0", '/', "0", "NaN" },
583 { "42", '/', "0", "NaN" },
584 { "-42", '/', "0", "NaN" },
585 { "infty", '/', "0", "NaN" },
586 { "-infty", '/', "0", "NaN" },
587 { "NaN", '/', "0", "NaN" },
588 { "0", '/', "NaN", "NaN" },
589 { "42", '/', "NaN", "NaN" },
590 { "-42", '/', "NaN", "NaN" },
591 { "infty", '/', "NaN", "NaN" },
592 { "-infty", '/', "NaN", "NaN" },
593 { "NaN", '/', "NaN", "NaN" },
594 { "0", '/', "infty", "0" },
595 { "42", '/', "infty", "0" },
596 { "-42", '/', "infty", "0" },
597 { "infty", '/', "infty", "NaN" },
598 { "-infty", '/', "infty", "NaN" },
599 { "NaN", '/', "infty", "NaN" },
600 { "0", '/', "-infty", "0" },
601 { "42", '/', "-infty", "0" },
602 { "-42", '/', "-infty", "0" },
603 { "infty", '/', "-infty", "NaN" },
604 { "-infty", '/', "-infty", "NaN" },
605 { "NaN", '/', "-infty", "NaN" },
606 { "1", '-', "1/3", "2/3" },
607 { "1/3", '+', "1/2", "5/6" },
608 { "1/2", '+', "1/2", "1" },
609 { "3/4", '-', "1/4", "1/2" },
610 { "1/2", '-', "1/3", "1/6" },
611 { "infty", '+', "42", "infty" },
612 { "infty", '+', "infty", "infty" },
613 { "42", '+', "infty", "infty" },
614 { "infty", '-', "infty", "NaN" },
615 { "infty", '*', "infty", "infty" },
616 { "infty", '*', "-infty", "-infty" },
617 { "-infty", '*', "infty", "-infty" },
618 { "-infty", '*', "-infty", "infty" },
619 { "0", '*', "infty", "NaN" },
620 { "1", '*', "infty", "infty" },
621 { "infty", '*', "0", "NaN" },
622 { "infty", '*', "42", "infty" },
623 { "42", '-', "infty", "-infty" },
624 { "infty", '+', "-infty", "NaN" },
625 { "4", 'g', "6", "2" },
626 { "5", 'g', "6", "1" },
627 { "42", 'm', "3", "3" },
628 { "42", 'M', "3", "42" },
629 { "3", 'm', "42", "3" },
630 { "3", 'M', "42", "42" },
631 { "42", 'm', "infty", "42" },
632 { "42", 'M', "infty", "infty" },
633 { "42", 'm', "-infty", "-infty" },
634 { "42", 'M', "-infty", "42" },
635 { "42", 'm', "NaN", "NaN" },
636 { "42", 'M', "NaN", "NaN" },
637 { "infty", 'm', "-infty", "-infty" },
638 { "infty", 'M', "-infty", "infty" },
641 /* Perform some basic tests of binary operations on isl_val objects.
643 static int test_bin_val(isl_ctx *ctx)
645 int i;
646 isl_val *v1, *v2, *res;
647 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
648 __isl_take isl_val *v2);
649 int ok;
651 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
652 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
653 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
654 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
655 fn = val_bin_op[val_bin_tests[i].op].fn;
656 v1 = fn(v1, v2);
657 if (isl_val_is_nan(res))
658 ok = isl_val_is_nan(v1);
659 else
660 ok = isl_val_eq(v1, res);
661 isl_val_free(v1);
662 isl_val_free(res);
663 if (ok < 0)
664 return -1;
665 if (!ok)
666 isl_die(ctx, isl_error_unknown,
667 "unexpected result", return -1);
670 return 0;
673 /* Perform some basic tests on isl_val objects.
675 static int test_val(isl_ctx *ctx)
677 if (test_un_val(ctx) < 0)
678 return -1;
679 if (test_bin_val(ctx) < 0)
680 return -1;
681 return 0;
684 /* Sets described using existentially quantified variables that
685 * can also be described without.
687 static const char *elimination_tests[] = {
688 "{ [i,j] : 2 * [i/2] + 3 * [j/4] <= 10 and 2 i = j }",
689 "{ [m, w] : exists a : w - 2m - 5 <= 3a <= m - 2w }",
690 "{ [m, w] : exists a : w >= 0 and a < m and -1 + w <= a <= 2m - w }",
693 /* Check that redundant existentially quantified variables are
694 * getting removed.
696 static int test_elimination(isl_ctx *ctx)
698 int i;
699 unsigned n;
700 isl_basic_set *bset;
702 for (i = 0; i < ARRAY_SIZE(elimination_tests); ++i) {
703 bset = isl_basic_set_read_from_str(ctx, elimination_tests[i]);
704 n = isl_basic_set_dim(bset, isl_dim_div);
705 isl_basic_set_free(bset);
706 if (!bset)
707 return -1;
708 if (n != 0)
709 isl_die(ctx, isl_error_unknown,
710 "expecting no existentials", return -1);
713 return 0;
716 static int test_div(isl_ctx *ctx)
718 const char *str;
719 int empty;
720 isl_int v;
721 isl_space *dim;
722 isl_set *set;
723 isl_local_space *ls;
724 struct isl_basic_set *bset;
725 struct isl_constraint *c;
727 isl_int_init(v);
729 /* test 1 */
730 dim = isl_space_set_alloc(ctx, 0, 3);
731 bset = isl_basic_set_universe(isl_space_copy(dim));
732 ls = isl_local_space_from_space(dim);
734 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
735 isl_int_set_si(v, -1);
736 c = isl_constraint_set_constant(c, v);
737 isl_int_set_si(v, 1);
738 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
739 isl_int_set_si(v, 3);
740 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
741 bset = isl_basic_set_add_constraint(bset, c);
743 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
744 isl_int_set_si(v, 1);
745 c = isl_constraint_set_constant(c, v);
746 isl_int_set_si(v, -1);
747 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
748 isl_int_set_si(v, 3);
749 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
750 bset = isl_basic_set_add_constraint(bset, c);
752 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
754 assert(bset && bset->n_div == 1);
755 isl_local_space_free(ls);
756 isl_basic_set_free(bset);
758 /* test 2 */
759 dim = isl_space_set_alloc(ctx, 0, 3);
760 bset = isl_basic_set_universe(isl_space_copy(dim));
761 ls = isl_local_space_from_space(dim);
763 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
764 isl_int_set_si(v, 1);
765 c = isl_constraint_set_constant(c, v);
766 isl_int_set_si(v, -1);
767 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
768 isl_int_set_si(v, 3);
769 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
770 bset = isl_basic_set_add_constraint(bset, c);
772 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
773 isl_int_set_si(v, -1);
774 c = isl_constraint_set_constant(c, v);
775 isl_int_set_si(v, 1);
776 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
777 isl_int_set_si(v, 3);
778 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
779 bset = isl_basic_set_add_constraint(bset, c);
781 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
783 assert(bset && bset->n_div == 1);
784 isl_local_space_free(ls);
785 isl_basic_set_free(bset);
787 /* test 3 */
788 dim = isl_space_set_alloc(ctx, 0, 3);
789 bset = isl_basic_set_universe(isl_space_copy(dim));
790 ls = isl_local_space_from_space(dim);
792 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
793 isl_int_set_si(v, 1);
794 c = isl_constraint_set_constant(c, v);
795 isl_int_set_si(v, -1);
796 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
797 isl_int_set_si(v, 3);
798 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
799 bset = isl_basic_set_add_constraint(bset, c);
801 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
802 isl_int_set_si(v, -3);
803 c = isl_constraint_set_constant(c, v);
804 isl_int_set_si(v, 1);
805 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
806 isl_int_set_si(v, 4);
807 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
808 bset = isl_basic_set_add_constraint(bset, c);
810 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
812 assert(bset && bset->n_div == 1);
813 isl_local_space_free(ls);
814 isl_basic_set_free(bset);
816 /* test 4 */
817 dim = isl_space_set_alloc(ctx, 0, 3);
818 bset = isl_basic_set_universe(isl_space_copy(dim));
819 ls = isl_local_space_from_space(dim);
821 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
822 isl_int_set_si(v, 2);
823 c = isl_constraint_set_constant(c, v);
824 isl_int_set_si(v, -1);
825 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
826 isl_int_set_si(v, 3);
827 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
828 bset = isl_basic_set_add_constraint(bset, c);
830 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
831 isl_int_set_si(v, -1);
832 c = isl_constraint_set_constant(c, v);
833 isl_int_set_si(v, 1);
834 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
835 isl_int_set_si(v, 6);
836 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
837 bset = isl_basic_set_add_constraint(bset, c);
839 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
841 assert(isl_basic_set_is_empty(bset));
842 isl_local_space_free(ls);
843 isl_basic_set_free(bset);
845 /* test 5 */
846 dim = isl_space_set_alloc(ctx, 0, 3);
847 bset = isl_basic_set_universe(isl_space_copy(dim));
848 ls = isl_local_space_from_space(dim);
850 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
851 isl_int_set_si(v, -1);
852 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
853 isl_int_set_si(v, 3);
854 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
855 bset = isl_basic_set_add_constraint(bset, c);
857 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
858 isl_int_set_si(v, 1);
859 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
860 isl_int_set_si(v, -3);
861 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
862 bset = isl_basic_set_add_constraint(bset, c);
864 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
866 assert(bset && bset->n_div == 0);
867 isl_basic_set_free(bset);
868 isl_local_space_free(ls);
870 /* test 6 */
871 dim = isl_space_set_alloc(ctx, 0, 3);
872 bset = isl_basic_set_universe(isl_space_copy(dim));
873 ls = isl_local_space_from_space(dim);
875 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
876 isl_int_set_si(v, -1);
877 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
878 isl_int_set_si(v, 6);
879 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
880 bset = isl_basic_set_add_constraint(bset, c);
882 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
883 isl_int_set_si(v, 1);
884 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
885 isl_int_set_si(v, -3);
886 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
887 bset = isl_basic_set_add_constraint(bset, c);
889 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
891 assert(bset && bset->n_div == 1);
892 isl_basic_set_free(bset);
893 isl_local_space_free(ls);
895 /* test 7 */
896 /* This test is a bit tricky. We set up an equality
897 * a + 3b + 3c = 6 e0
898 * Normalization of divs creates _two_ divs
899 * a = 3 e0
900 * c - b - e0 = 2 e1
901 * Afterwards e0 is removed again because it has coefficient -1
902 * and we end up with the original equality and div again.
903 * Perhaps we can avoid the introduction of this temporary div.
905 dim = isl_space_set_alloc(ctx, 0, 4);
906 bset = isl_basic_set_universe(isl_space_copy(dim));
907 ls = isl_local_space_from_space(dim);
909 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
910 isl_int_set_si(v, -1);
911 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
912 isl_int_set_si(v, -3);
913 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
914 isl_int_set_si(v, -3);
915 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
916 isl_int_set_si(v, 6);
917 c = isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
918 bset = isl_basic_set_add_constraint(bset, c);
920 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
922 /* Test disabled for now */
924 assert(bset && bset->n_div == 1);
926 isl_local_space_free(ls);
927 isl_basic_set_free(bset);
929 /* test 8 */
930 dim = isl_space_set_alloc(ctx, 0, 5);
931 bset = isl_basic_set_universe(isl_space_copy(dim));
932 ls = isl_local_space_from_space(dim);
934 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
935 isl_int_set_si(v, -1);
936 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
937 isl_int_set_si(v, -3);
938 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
939 isl_int_set_si(v, -3);
940 c = isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
941 isl_int_set_si(v, 6);
942 c = isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
943 bset = isl_basic_set_add_constraint(bset, c);
945 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
946 isl_int_set_si(v, -1);
947 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
948 isl_int_set_si(v, 1);
949 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
950 isl_int_set_si(v, 1);
951 c = isl_constraint_set_constant(c, v);
952 bset = isl_basic_set_add_constraint(bset, c);
954 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
956 /* Test disabled for now */
958 assert(bset && bset->n_div == 1);
960 isl_local_space_free(ls);
961 isl_basic_set_free(bset);
963 /* test 9 */
964 dim = isl_space_set_alloc(ctx, 0, 4);
965 bset = isl_basic_set_universe(isl_space_copy(dim));
966 ls = isl_local_space_from_space(dim);
968 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
969 isl_int_set_si(v, 1);
970 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
971 isl_int_set_si(v, -1);
972 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
973 isl_int_set_si(v, -2);
974 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
975 bset = isl_basic_set_add_constraint(bset, c);
977 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
978 isl_int_set_si(v, -1);
979 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
980 isl_int_set_si(v, 3);
981 c = isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
982 isl_int_set_si(v, 2);
983 c = isl_constraint_set_constant(c, v);
984 bset = isl_basic_set_add_constraint(bset, c);
986 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
988 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
990 assert(!isl_basic_set_is_empty(bset));
992 isl_local_space_free(ls);
993 isl_basic_set_free(bset);
995 /* test 10 */
996 dim = isl_space_set_alloc(ctx, 0, 3);
997 bset = isl_basic_set_universe(isl_space_copy(dim));
998 ls = isl_local_space_from_space(dim);
1000 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1001 isl_int_set_si(v, 1);
1002 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
1003 isl_int_set_si(v, -2);
1004 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
1005 bset = isl_basic_set_add_constraint(bset, c);
1007 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1009 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1011 isl_local_space_free(ls);
1012 isl_basic_set_free(bset);
1014 isl_int_clear(v);
1016 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1017 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
1018 set = isl_set_read_from_str(ctx, str);
1019 set = isl_set_compute_divs(set);
1020 isl_set_free(set);
1021 if (!set)
1022 return -1;
1024 if (test_elimination(ctx) < 0)
1025 return -1;
1027 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
1028 set = isl_set_read_from_str(ctx, str);
1029 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
1030 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
1031 empty = isl_set_is_empty(set);
1032 isl_set_free(set);
1033 if (empty < 0)
1034 return -1;
1035 if (!empty)
1036 isl_die(ctx, isl_error_unknown,
1037 "result not as accurate as expected", return -1);
1039 return 0;
1042 void test_application_case(struct isl_ctx *ctx, const char *name)
1044 char *filename;
1045 FILE *input;
1046 struct isl_basic_set *bset1, *bset2;
1047 struct isl_basic_map *bmap;
1049 filename = get_filename(ctx, name, "omega");
1050 assert(filename);
1051 input = fopen(filename, "r");
1052 assert(input);
1054 bset1 = isl_basic_set_read_from_file(ctx, input);
1055 bmap = isl_basic_map_read_from_file(ctx, input);
1057 bset1 = isl_basic_set_apply(bset1, bmap);
1059 bset2 = isl_basic_set_read_from_file(ctx, input);
1061 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1063 isl_basic_set_free(bset1);
1064 isl_basic_set_free(bset2);
1065 free(filename);
1067 fclose(input);
1070 static int test_application(isl_ctx *ctx)
1072 test_application_case(ctx, "application");
1073 test_application_case(ctx, "application2");
1075 return 0;
1078 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1080 char *filename;
1081 FILE *input;
1082 struct isl_basic_set *bset1, *bset2;
1084 filename = get_filename(ctx, name, "polylib");
1085 assert(filename);
1086 input = fopen(filename, "r");
1087 assert(input);
1089 bset1 = isl_basic_set_read_from_file(ctx, input);
1090 bset2 = isl_basic_set_read_from_file(ctx, input);
1092 bset1 = isl_basic_set_affine_hull(bset1);
1094 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1096 isl_basic_set_free(bset1);
1097 isl_basic_set_free(bset2);
1098 free(filename);
1100 fclose(input);
1103 int test_affine_hull(struct isl_ctx *ctx)
1105 const char *str;
1106 isl_set *set;
1107 isl_basic_set *bset, *bset2;
1108 int n;
1109 int subset;
1111 test_affine_hull_case(ctx, "affine2");
1112 test_affine_hull_case(ctx, "affine");
1113 test_affine_hull_case(ctx, "affine3");
1115 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1116 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1117 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1118 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1119 set = isl_set_read_from_str(ctx, str);
1120 bset = isl_set_affine_hull(set);
1121 n = isl_basic_set_dim(bset, isl_dim_div);
1122 isl_basic_set_free(bset);
1123 if (n != 0)
1124 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1125 return -1);
1127 /* Check that isl_map_affine_hull is not confused by
1128 * the reordering of divs in isl_map_align_divs.
1130 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1131 "32e0 = b and 32e1 = c); "
1132 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1133 set = isl_set_read_from_str(ctx, str);
1134 bset = isl_set_affine_hull(set);
1135 isl_basic_set_free(bset);
1136 if (!bset)
1137 return -1;
1139 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1140 "32e2 = 31 + 31e0 }";
1141 set = isl_set_read_from_str(ctx, str);
1142 bset = isl_set_affine_hull(set);
1143 str = "{ [a] : exists e : a = 32 e }";
1144 bset2 = isl_basic_set_read_from_str(ctx, str);
1145 subset = isl_basic_set_is_subset(bset, bset2);
1146 isl_basic_set_free(bset);
1147 isl_basic_set_free(bset2);
1148 if (subset < 0)
1149 return -1;
1150 if (!subset)
1151 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1152 return -1);
1154 return 0;
1157 /* Pairs of maps and the corresponding expected results of
1158 * isl_map_plain_unshifted_simple_hull.
1160 struct {
1161 const char *map;
1162 const char *hull;
1163 } plain_unshifted_simple_hull_tests[] = {
1164 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1165 "{ [i] -> [j] : i >= 1 }" },
1166 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1167 "(j mod 4 = 2 and k mod 6 = n) }",
1168 "{ [n] -> [i,j,k] : j mod 4 = 2 }" },
1171 /* Basic tests for isl_map_plain_unshifted_simple_hull.
1173 static int test_plain_unshifted_simple_hull(isl_ctx *ctx)
1175 int i;
1176 isl_map *map;
1177 isl_basic_map *hull, *expected;
1178 isl_bool equal;
1180 for (i = 0; i < ARRAY_SIZE(plain_unshifted_simple_hull_tests); ++i) {
1181 const char *str;
1182 str = plain_unshifted_simple_hull_tests[i].map;
1183 map = isl_map_read_from_str(ctx, str);
1184 str = plain_unshifted_simple_hull_tests[i].hull;
1185 expected = isl_basic_map_read_from_str(ctx, str);
1186 hull = isl_map_plain_unshifted_simple_hull(map);
1187 equal = isl_basic_map_is_equal(hull, expected);
1188 isl_basic_map_free(hull);
1189 isl_basic_map_free(expected);
1190 if (equal < 0)
1191 return -1;
1192 if (!equal)
1193 isl_die(ctx, isl_error_unknown, "unexpected hull",
1194 return -1);
1197 return 0;
1200 /* Pairs of sets and the corresponding expected results of
1201 * isl_set_unshifted_simple_hull.
1203 struct {
1204 const char *set;
1205 const char *hull;
1206 } unshifted_simple_hull_tests[] = {
1207 { "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1208 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1211 /* Basic tests for isl_set_unshifted_simple_hull.
1213 static int test_unshifted_simple_hull(isl_ctx *ctx)
1215 int i;
1216 isl_set *set;
1217 isl_basic_set *hull, *expected;
1218 isl_bool equal;
1220 for (i = 0; i < ARRAY_SIZE(unshifted_simple_hull_tests); ++i) {
1221 const char *str;
1222 str = unshifted_simple_hull_tests[i].set;
1223 set = isl_set_read_from_str(ctx, str);
1224 str = unshifted_simple_hull_tests[i].hull;
1225 expected = isl_basic_set_read_from_str(ctx, str);
1226 hull = isl_set_unshifted_simple_hull(set);
1227 equal = isl_basic_set_is_equal(hull, expected);
1228 isl_basic_set_free(hull);
1229 isl_basic_set_free(expected);
1230 if (equal < 0)
1231 return -1;
1232 if (!equal)
1233 isl_die(ctx, isl_error_unknown, "unexpected hull",
1234 return -1);
1237 return 0;
1240 static int test_simple_hull(struct isl_ctx *ctx)
1242 const char *str;
1243 isl_set *set;
1244 isl_basic_set *bset;
1245 isl_bool is_empty;
1247 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1248 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1249 set = isl_set_read_from_str(ctx, str);
1250 bset = isl_set_simple_hull(set);
1251 is_empty = isl_basic_set_is_empty(bset);
1252 isl_basic_set_free(bset);
1254 if (is_empty == isl_bool_error)
1255 return -1;
1257 if (is_empty == isl_bool_false)
1258 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1259 return -1);
1261 if (test_plain_unshifted_simple_hull(ctx) < 0)
1262 return -1;
1263 if (test_unshifted_simple_hull(ctx) < 0)
1264 return -1;
1266 return 0;
1269 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1271 char *filename;
1272 FILE *input;
1273 struct isl_basic_set *bset1, *bset2;
1274 struct isl_set *set;
1276 filename = get_filename(ctx, name, "polylib");
1277 assert(filename);
1278 input = fopen(filename, "r");
1279 assert(input);
1281 bset1 = isl_basic_set_read_from_file(ctx, input);
1282 bset2 = isl_basic_set_read_from_file(ctx, input);
1284 set = isl_basic_set_union(bset1, bset2);
1285 bset1 = isl_set_convex_hull(set);
1287 bset2 = isl_basic_set_read_from_file(ctx, input);
1289 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1291 isl_basic_set_free(bset1);
1292 isl_basic_set_free(bset2);
1293 free(filename);
1295 fclose(input);
1298 struct {
1299 const char *set;
1300 const char *hull;
1301 } convex_hull_tests[] = {
1302 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1303 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1304 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1305 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1306 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1307 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1308 "i2 <= 5 and i2 >= 4; "
1309 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1310 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1311 "i2 <= 5 + i0 and i2 >= i0 }" },
1312 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1313 "{ [x, y] : 1 = 0 }" },
1314 { "{ [x, y, z] : 0 <= x, y, z <= 10; [x, y, 0] : x >= 0 and y > 0; "
1315 "[x, y, 0] : x >= 0 and y < 0 }",
1316 "{ [x, y, z] : x >= 0 and 0 <= z <= 10 }" },
1319 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1321 int i;
1322 int orig_convex = ctx->opt->convex;
1323 ctx->opt->convex = convex;
1325 test_convex_hull_case(ctx, "convex0");
1326 test_convex_hull_case(ctx, "convex1");
1327 test_convex_hull_case(ctx, "convex2");
1328 test_convex_hull_case(ctx, "convex3");
1329 test_convex_hull_case(ctx, "convex4");
1330 test_convex_hull_case(ctx, "convex5");
1331 test_convex_hull_case(ctx, "convex6");
1332 test_convex_hull_case(ctx, "convex7");
1333 test_convex_hull_case(ctx, "convex8");
1334 test_convex_hull_case(ctx, "convex9");
1335 test_convex_hull_case(ctx, "convex10");
1336 test_convex_hull_case(ctx, "convex11");
1337 test_convex_hull_case(ctx, "convex12");
1338 test_convex_hull_case(ctx, "convex13");
1339 test_convex_hull_case(ctx, "convex14");
1340 test_convex_hull_case(ctx, "convex15");
1342 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1343 isl_set *set1, *set2;
1344 int equal;
1346 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1347 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1348 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1349 equal = isl_set_is_equal(set1, set2);
1350 isl_set_free(set1);
1351 isl_set_free(set2);
1353 if (equal < 0)
1354 return -1;
1355 if (!equal)
1356 isl_die(ctx, isl_error_unknown,
1357 "unexpected convex hull", return -1);
1360 ctx->opt->convex = orig_convex;
1362 return 0;
1365 static int test_convex_hull(isl_ctx *ctx)
1367 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1368 return -1;
1369 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1370 return -1;
1371 return 0;
1374 void test_gist_case(struct isl_ctx *ctx, const char *name)
1376 char *filename;
1377 FILE *input;
1378 struct isl_basic_set *bset1, *bset2;
1380 filename = get_filename(ctx, name, "polylib");
1381 assert(filename);
1382 input = fopen(filename, "r");
1383 assert(input);
1385 bset1 = isl_basic_set_read_from_file(ctx, input);
1386 bset2 = isl_basic_set_read_from_file(ctx, input);
1388 bset1 = isl_basic_set_gist(bset1, bset2);
1390 bset2 = isl_basic_set_read_from_file(ctx, input);
1392 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1394 isl_basic_set_free(bset1);
1395 isl_basic_set_free(bset2);
1396 free(filename);
1398 fclose(input);
1401 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1403 struct {
1404 const char *map;
1405 const char *context;
1406 const char *gist;
1407 } plain_gist_tests[] = {
1408 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1409 "{ [i] -> [j] : i >= 1 }",
1410 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1411 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1412 "(j mod 4 = 2 and k mod 6 = n) }",
1413 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1414 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1415 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1416 "{ [i] -> [j] : i > j }",
1417 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1420 /* Basic tests for isl_map_plain_gist_basic_map.
1422 static int test_plain_gist(isl_ctx *ctx)
1424 int i;
1426 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1427 const char *str;
1428 int equal;
1429 isl_map *map, *gist;
1430 isl_basic_map *context;
1432 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1433 str = plain_gist_tests[i].context;
1434 context = isl_basic_map_read_from_str(ctx, str);
1435 map = isl_map_plain_gist_basic_map(map, context);
1436 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1437 equal = isl_map_is_equal(map, gist);
1438 isl_map_free(map);
1439 isl_map_free(gist);
1440 if (equal < 0)
1441 return -1;
1442 if (!equal)
1443 isl_die(ctx, isl_error_unknown,
1444 "incorrect gist result", return -1);
1447 return 0;
1450 struct {
1451 const char *set;
1452 const char *context;
1453 const char *gist;
1454 } gist_tests[] = {
1455 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1456 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1457 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1458 "{ [a, b, c] : a <= 15 }" },
1459 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1460 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1461 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1462 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1463 { "{ [m, n, a, b] : a <= 2147 + n }",
1464 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1465 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1466 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1467 "b >= 0) }",
1468 "{ [m, n, ku, kl] }" },
1469 { "{ [a, a, b] : a >= 10 }",
1470 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1471 "{ [a, a, b] : a >= 10 }" },
1472 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1473 "{ [0, j] : j >= 0 }" },
1474 /* Check that no constraints on i6 are introduced in the gist */
1475 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1476 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1477 "5e0 <= 381 - t1 and i4 <= 1) }",
1478 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1479 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1480 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1481 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1482 "20e0 >= 1511 - 4t1 - 5i4) }" },
1483 /* Check that no constraints on i6 are introduced in the gist */
1484 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1485 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1486 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1487 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1488 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1489 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1490 "20e1 <= 1530 - 4t1 - 5i4 and "
1491 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1492 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1493 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1494 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1495 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1496 "10e0 <= -91 + 5i4 + 4i6 and "
1497 "10e0 >= -105 + 5i4 + 4i6) }",
1498 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1499 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1500 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1501 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1502 "{ [a, b, q, p] : b >= 1 + a }",
1503 "{ [a, b, q, p] : false }" },
1504 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1505 "[n] -> { [x] : x mod 32 = 0 }",
1506 "[n] -> { [x = n] }" },
1507 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1508 "{ [x] : x mod 2 = 0 }" },
1509 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1510 "{ [x] : x mod 128 = 0 }" },
1511 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1512 "{ [x] : x mod 3200 = 0 }" },
1513 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1514 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1515 "{ [a, b, c = a] }" },
1516 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1517 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1518 "{ [a, b, c = a] : a mod 3 = 0 }" },
1519 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1520 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1521 "{ [x] }" },
1522 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1523 "{ [x,y] : 1 <= y <= 3 }",
1524 "{ [x,y] }" },
1527 /* Check that isl_set_gist behaves as expected.
1529 * For the test cases in gist_tests, besides checking that the result
1530 * is as expected, also check that applying the gist operation does
1531 * not modify the input set (an earlier version of isl would do that) and
1532 * that the test case is consistent, i.e., that the gist has the same
1533 * intersection with the context as the input set.
1535 static int test_gist(struct isl_ctx *ctx)
1537 int i;
1538 const char *str;
1539 isl_basic_set *bset1, *bset2;
1540 isl_map *map1, *map2;
1541 int equal;
1543 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1544 int equal_input, equal_intersection;
1545 isl_set *set1, *set2, *copy, *context;
1547 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1548 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1549 copy = isl_set_copy(set1);
1550 set1 = isl_set_gist(set1, isl_set_copy(context));
1551 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1552 equal = isl_set_is_equal(set1, set2);
1553 isl_set_free(set1);
1554 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1555 equal_input = isl_set_is_equal(set1, copy);
1556 isl_set_free(copy);
1557 set1 = isl_set_intersect(set1, isl_set_copy(context));
1558 set2 = isl_set_intersect(set2, context);
1559 equal_intersection = isl_set_is_equal(set1, set2);
1560 isl_set_free(set2);
1561 isl_set_free(set1);
1562 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1563 return -1;
1564 if (!equal)
1565 isl_die(ctx, isl_error_unknown,
1566 "incorrect gist result", return -1);
1567 if (!equal_input)
1568 isl_die(ctx, isl_error_unknown,
1569 "gist modified input", return -1);
1570 if (!equal_input)
1571 isl_die(ctx, isl_error_unknown,
1572 "inconsistent gist test case", return -1);
1575 test_gist_case(ctx, "gist1");
1577 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1578 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1579 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1580 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1581 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1582 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1583 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1584 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1585 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1586 bset1 = isl_basic_set_read_from_str(ctx, str);
1587 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1588 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1589 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1590 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1591 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1592 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1593 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1594 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1595 bset2 = isl_basic_set_read_from_str(ctx, str);
1596 bset1 = isl_basic_set_gist(bset1, bset2);
1597 assert(bset1 && bset1->n_div == 0);
1598 isl_basic_set_free(bset1);
1600 /* Check that the integer divisions of the second disjunct
1601 * do not spread to the first disjunct.
1603 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1604 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1605 "(exists (e0 = [(-1 + t1)/16], "
1606 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1607 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1608 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1609 "o0 <= 4294967295 and t1 <= -1)) }";
1610 map1 = isl_map_read_from_str(ctx, str);
1611 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1612 map2 = isl_map_read_from_str(ctx, str);
1613 map1 = isl_map_gist(map1, map2);
1614 if (!map1)
1615 return -1;
1616 if (map1->n != 1)
1617 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1618 isl_map_free(map1); return -1);
1619 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1620 isl_die(ctx, isl_error_unknown, "expecting single div",
1621 isl_map_free(map1); return -1);
1622 isl_map_free(map1);
1624 if (test_plain_gist(ctx) < 0)
1625 return -1;
1627 return 0;
1630 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1632 isl_set *set, *set2;
1633 int equal;
1634 int one;
1636 set = isl_set_read_from_str(ctx, str);
1637 set = isl_set_coalesce(set);
1638 set2 = isl_set_read_from_str(ctx, str);
1639 equal = isl_set_is_equal(set, set2);
1640 one = set && set->n == 1;
1641 isl_set_free(set);
1642 isl_set_free(set2);
1644 if (equal < 0)
1645 return -1;
1646 if (!equal)
1647 isl_die(ctx, isl_error_unknown,
1648 "coalesced set not equal to input", return -1);
1649 if (check_one && !one)
1650 isl_die(ctx, isl_error_unknown,
1651 "coalesced set should not be a union", return -1);
1653 return 0;
1656 /* Inputs for coalescing tests with unbounded wrapping.
1657 * "str" is a string representation of the input set.
1658 * "single_disjunct" is set if we expect the result to consist of
1659 * a single disjunct.
1661 struct {
1662 int single_disjunct;
1663 const char *str;
1664 } coalesce_unbounded_tests[] = {
1665 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1666 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1667 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1668 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1669 "-10 <= y <= 0}" },
1670 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1671 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1672 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1673 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1676 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1677 * option turned off.
1679 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1681 int i;
1682 int r = 0;
1683 int bounded;
1685 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1686 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1688 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1689 const char *str = coalesce_unbounded_tests[i].str;
1690 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1691 if (test_coalesce_set(ctx, str, check_one) >= 0)
1692 continue;
1693 r = -1;
1694 break;
1697 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1699 return r;
1702 /* Inputs for coalescing tests.
1703 * "str" is a string representation of the input set.
1704 * "single_disjunct" is set if we expect the result to consist of
1705 * a single disjunct.
1707 struct {
1708 int single_disjunct;
1709 const char *str;
1710 } coalesce_tests[] = {
1711 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1712 "y >= x & x >= 2 & 5 >= y }" },
1713 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1714 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1715 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1716 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1717 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1718 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1719 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1720 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1721 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1722 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1723 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1724 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1725 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1726 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1727 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1728 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1729 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1730 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1731 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1732 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1733 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1734 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1735 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1736 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1737 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1738 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1739 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1740 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1741 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1742 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1743 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1744 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1745 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1746 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1747 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1748 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1749 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1750 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1751 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1752 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1753 "[o0, o1, o2, o3, o4, o5, o6]] : "
1754 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1755 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1756 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1757 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1758 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1759 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1760 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1761 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1762 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1763 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1764 "o6 >= i3 + i6 - o3 and M >= 0 and "
1765 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1766 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1767 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1768 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1769 "(o0 = 0 and M >= 2 and N >= 3) or "
1770 "(M = 0 and o0 = 0 and N >= 3) }" },
1771 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1772 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1773 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1774 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1775 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1776 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1777 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1778 "(y = 3 and x = 1) }" },
1779 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1780 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1781 "i1 <= M and i3 <= M and i4 <= M) or "
1782 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1783 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1784 "i4 <= -1 + M) }" },
1785 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1786 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1787 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1788 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1789 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1790 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1791 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1792 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1793 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1794 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1795 { 0, "{ [a, b] : exists e : 2e = a and "
1796 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1797 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1798 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1799 "j >= 1 and j' <= i + j - i' and i >= 1; "
1800 "[1, 1, 1, 1] }" },
1801 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1802 "[i,j] : exists a : j = 3a }" },
1803 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1804 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1805 "a >= 3) or "
1806 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1807 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1808 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1809 "c <= 6 + 8a and a >= 3; "
1810 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1811 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1812 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1813 "[x,0] : 3 <= x <= 4 }" },
1814 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1815 "[x,0] : 4 <= x <= 5 }" },
1816 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1817 "[x,0] : 3 <= x <= 5 }" },
1818 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1819 "[x,0] : 3 <= x <= 4 }" },
1820 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1821 "i1 <= 0; "
1822 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1823 { 1, "{ [0,0]; [1,1] }" },
1824 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1825 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1826 "ii <= k;"
1827 "[k, 0, k] : k <= 6 and k >= 1 }" },
1828 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1829 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1830 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1831 { 1, "[n] -> { [1] : n >= 0;"
1832 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1833 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1834 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1835 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1836 "2e0 <= x and 2e0 <= n);"
1837 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1838 "n >= 0) }" },
1839 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1840 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1841 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1842 "t1 = 1 }" },
1843 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1844 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1845 "[0, 0] }" },
1846 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1847 "t1 >= 13 and t1 <= 16);"
1848 "[t1] : t1 <= 15 and t1 >= 12 }" },
1849 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
1850 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
1851 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
1852 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
1853 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
1854 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
1855 "i <= 4j + 2 }" },
1856 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
1857 "(exists (e0 : 3e0 = -2 + c0)) }" },
1858 { 0, "[n, b0, t0] -> "
1859 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1860 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1861 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1862 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1863 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
1864 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
1865 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
1866 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
1867 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1868 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1869 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1870 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
1871 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
1872 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
1873 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
1874 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
1875 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
1876 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
1877 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
1878 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
1879 { 0, "{ [i0, i1, i2] : "
1880 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
1881 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
1882 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
1883 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
1884 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
1885 "32e0 <= 31 + i0)) or "
1886 "i0 >= 0 }" },
1887 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
1888 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
1889 "2*floor((c)/2) = c and 0 <= a <= 192;"
1890 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
1892 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
1893 "(0 <= a <= b <= n) }" },
1894 { 1, "{ [a, b] : 0 <= a <= 2 and b >= 0 and "
1895 "((0 < b <= 13) or (2*floor((a + b)/2) >= -5 + a + 2b)) }" },
1896 { 1, "{ [a] : (2 <= a <= 5) or (a mod 2 = 1 and 1 <= a <= 5) }" },
1897 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
1898 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1899 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
1900 "b = 3 and 9e0 <= -19 + 2c)) }" },
1901 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
1902 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1903 "(a = 4 and b = 3 and "
1904 "9*floor((-16 + 2c)/9) <= -19 + 2c) }" },
1905 { 0, "{ [a, b, c] : (b <= 2 and b <= -2 + a) or "
1906 "(b = -1 + a and 0 < a <= 3 and "
1907 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1908 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
1909 "b = 3 and 9e0 <= -19 + 2c)) }" },
1910 { 1, "{ [y, x] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
1911 "[1, 0] }" },
1912 { 1, "{ [x, y] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
1913 "[0, 1] }" },
1914 { 1, "{ [1, y] : -1 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
1915 { 1, "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
1916 { 1, "{ [x, y] : 0 <= x <= 10 and x - 4*floor(x/4) <= 1 and y <= 0; "
1917 "[x, y] : 0 <= x <= 10 and x - 4*floor(x/4) > 1 and y <= 0; "
1918 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) <= 1 and 0 < y; "
1919 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) > 1 and 0 < y }" },
1920 { 1, "{ [x, 0] : 0 <= x <= 10 and x mod 2 = 0; "
1921 "[x, 0] : 0 <= x <= 10 and x mod 2 = 1; "
1922 "[x, y] : 0 <= x <= 10 and 1 <= y <= 10 }" },
1923 { 1, "{ [a] : a <= 8 and "
1924 "(a mod 10 = 7 or a mod 10 = 8 or a mod 10 = 9) }" },
1925 { 1, "{ [x, y] : 2y = -x and x <= 0 or "
1926 "x <= -1 and 2y <= -x - 1 and 2y >= x - 1 }" },
1927 { 0, "{ [x, y] : 2y = -x and x <= 0 or "
1928 "x <= -2 and 2y <= -x - 1 and 2y >= x - 1 }" },
1929 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
1930 "(a < 0 and 3*floor((a)/3) < a) }" },
1931 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
1932 "(a < -1 and 3*floor((a)/3) < a) }" },
1933 { 1, "{ [a, b] : a <= 1024 and b >= 0 and "
1934 "((-31 - a + b <= 32*floor((-1 - a)/32) <= -33 + b and "
1935 "32*floor((-1 - a)/32) <= -16 + b + 16*floor((-1 - a)/16))"
1936 "or (2 <= a <= 15 and b < a)) }" },
1937 { 1, "{ [a] : a > 0 and ((16*floor((a)/16) < a and "
1938 "32*floor((a)/32) < a) or a <= 15) }" },
1939 { 1, "{ [a, b, c, d] : (-a + d) mod 64 = 0 and a <= 8 and b <= 1 and "
1940 "10 - a <= c <= 3 and d >= 5 and 9 - 64b <= d <= 70;"
1941 "[a, b = 1, c, d] : (-a + d) mod 64 = 0 and a <= 8 and c >= 4 and "
1942 "10 - a <= c <= 5 and 5 <= d <= 73 - c }" },
1943 { 1, "[n, m] -> { S_0[i] : (-n + i) mod 3 = 0 and m >= 3 + n and "
1944 "i >= n and 3*floor((2 + n + 2m)/3) <= n + 3m - i; "
1945 "S_0[n] : n <= m <= 2 + n }" },
1946 { 1, "{ [a, b] : exists (e0: 0 <= a <= 1 and b >= 0 and "
1947 "2e0 >= -5 + a + 2b and 2e0 >= -1 + a + b and "
1948 "2e0 <= a + b); "
1949 "[a, b] : exists (e0: 0 <= a <= 1 and 2e0 >= -5 + a + 2b and "
1950 "2e0 >= -1 - a + b and 2e0 <= -a + b and "
1951 "2e0 < -a + 2b) }" },
1952 { 1, "{ [i, j, i - 8j] : 8 <= i <= 63 and -7 + i <= 8j <= i; "
1953 "[i, 0, i] : 0 <= i <= 7 }" },
1954 { 1, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [1, 1] }" },
1955 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [0, 2] }" },
1956 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [-1, 3] }" },
1957 { 1, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [1, 1] }" },
1958 { 0, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [2, 1] }" },
1961 /* A specialized coalescing test case that would result
1962 * in a segmentation fault or a failed assertion in earlier versions of isl.
1964 static int test_coalesce_special(struct isl_ctx *ctx)
1966 const char *str;
1967 isl_map *map1, *map2;
1969 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1970 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
1971 "(y = 201 and o1 <= 239 and o1 >= 212) or "
1972 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
1973 "o1 <= 239 and o1 >= 212)) or "
1974 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
1975 "o1 <= 241 and o1 >= 240));"
1976 "[S_L220_OUT[] -> T7[]] -> "
1977 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
1978 "(y = 2 and o1 <= 241 and o1 >= 212) or "
1979 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
1980 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
1981 map1 = isl_map_read_from_str(ctx, str);
1982 map1 = isl_map_align_divs_internal(map1);
1983 map1 = isl_map_coalesce(map1);
1984 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1985 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
1986 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
1987 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
1988 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
1989 map2 = isl_map_read_from_str(ctx, str);
1990 map2 = isl_map_union(map2, map1);
1991 map2 = isl_map_align_divs_internal(map2);
1992 map2 = isl_map_coalesce(map2);
1993 isl_map_free(map2);
1994 if (!map2)
1995 return -1;
1997 return 0;
2000 /* A specialized coalescing test case that would result in an assertion
2001 * in an earlier version of isl.
2002 * The explicit call to isl_basic_set_union prevents the implicit
2003 * equality constraints in the first basic map from being detected prior
2004 * to the call to isl_set_coalesce, at least at the point
2005 * where this test case was introduced.
2007 static int test_coalesce_special2(struct isl_ctx *ctx)
2009 const char *str;
2010 isl_basic_set *bset1, *bset2;
2011 isl_set *set;
2013 str = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
2014 bset1 = isl_basic_set_read_from_str(ctx, str);
2015 str = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }" ;
2016 bset2 = isl_basic_set_read_from_str(ctx, str);
2017 set = isl_basic_set_union(bset1, bset2);
2018 set = isl_set_coalesce(set);
2019 isl_set_free(set);
2021 if (!set)
2022 return -1;
2023 return 0;
2026 /* Check that calling isl_set_coalesce does not leave other sets
2027 * that may share some information with the input to isl_set_coalesce
2028 * in an inconsistent state.
2029 * In particular, older versions of isl would modify all copies
2030 * of the basic sets in the isl_set_coalesce input in a way
2031 * that could leave them in an inconsistent state.
2032 * The result of printing any other set containing one of these
2033 * basic sets would then result in an invalid set description.
2035 static int test_coalesce_special3(isl_ctx *ctx)
2037 const char *str;
2038 char *s;
2039 isl_set *set1, *set2;
2040 isl_printer *p;
2042 set1 = isl_set_read_from_str(ctx, "{ [0, 0, 0] }");
2043 str = "{ [a, b, a + b] : a >= 0 and b >= 0 and 0 < a + b }";
2044 set2 = isl_set_read_from_str(ctx, str);
2045 set1 = isl_set_union(set1, isl_set_copy(set2));
2046 set1 = isl_set_coalesce(set1);
2047 isl_set_free(set1);
2049 p = isl_printer_to_str(ctx);
2050 p = isl_printer_print_set(p, set2);
2051 isl_set_free(set2);
2052 s = isl_printer_get_str(p);
2053 isl_printer_free(p);
2054 set1 = isl_set_read_from_str(ctx, s);
2055 free(s);
2056 isl_set_free(set1);
2058 if (!set1)
2059 return -1;
2061 return 0;
2064 /* Test the functionality of isl_set_coalesce.
2065 * That is, check that the output is always equal to the input
2066 * and in some cases that the result consists of a single disjunct.
2068 static int test_coalesce(struct isl_ctx *ctx)
2070 int i;
2072 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
2073 const char *str = coalesce_tests[i].str;
2074 int check_one = coalesce_tests[i].single_disjunct;
2075 if (test_coalesce_set(ctx, str, check_one) < 0)
2076 return -1;
2079 if (test_coalesce_unbounded_wrapping(ctx) < 0)
2080 return -1;
2081 if (test_coalesce_special(ctx) < 0)
2082 return -1;
2083 if (test_coalesce_special2(ctx) < 0)
2084 return -1;
2085 if (test_coalesce_special3(ctx) < 0)
2086 return -1;
2088 return 0;
2091 /* Construct a representation of the graph on the right of Figure 1
2092 * in "Computing the Transitive Closure of a Union of
2093 * Affine Integer Tuple Relations".
2095 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2097 isl_set *dom;
2098 isl_map *up, *right;
2100 dom = isl_set_read_from_str(ctx,
2101 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2102 "2 x - 3 y + 3 >= 0 }");
2103 right = isl_map_read_from_str(ctx,
2104 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2105 up = isl_map_read_from_str(ctx,
2106 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2107 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2108 right = isl_map_intersect_range(right, isl_set_copy(dom));
2109 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2110 up = isl_map_intersect_range(up, dom);
2111 return isl_map_union(up, right);
2114 /* Construct a representation of the power of the graph
2115 * on the right of Figure 1 in "Computing the Transitive Closure of
2116 * a Union of Affine Integer Tuple Relations".
2118 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2120 return isl_map_read_from_str(ctx,
2121 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2122 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2123 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2126 /* Construct a representation of the transitive closure of the graph
2127 * on the right of Figure 1 in "Computing the Transitive Closure of
2128 * a Union of Affine Integer Tuple Relations".
2130 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2132 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2135 static int test_closure(isl_ctx *ctx)
2137 const char *str;
2138 isl_map *map, *map2;
2139 int exact, equal;
2141 /* COCOA example 1 */
2142 map = isl_map_read_from_str(ctx,
2143 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2144 "1 <= i and i < n and 1 <= j and j < n or "
2145 "i2 = i + 1 and j2 = j - 1 and "
2146 "1 <= i and i < n and 2 <= j and j <= n }");
2147 map = isl_map_power(map, &exact);
2148 assert(exact);
2149 isl_map_free(map);
2151 /* COCOA example 1 */
2152 map = isl_map_read_from_str(ctx,
2153 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2154 "1 <= i and i < n and 1 <= j and j < n or "
2155 "i2 = i + 1 and j2 = j - 1 and "
2156 "1 <= i and i < n and 2 <= j and j <= n }");
2157 map = isl_map_transitive_closure(map, &exact);
2158 assert(exact);
2159 map2 = isl_map_read_from_str(ctx,
2160 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2161 "1 <= i and i < n and 1 <= j and j <= n and "
2162 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2163 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2164 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2165 assert(isl_map_is_equal(map, map2));
2166 isl_map_free(map2);
2167 isl_map_free(map);
2169 map = isl_map_read_from_str(ctx,
2170 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2171 " 0 <= y and y <= n }");
2172 map = isl_map_transitive_closure(map, &exact);
2173 map2 = isl_map_read_from_str(ctx,
2174 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2175 " 0 <= y and y <= n }");
2176 assert(isl_map_is_equal(map, map2));
2177 isl_map_free(map2);
2178 isl_map_free(map);
2180 /* COCOA example 2 */
2181 map = isl_map_read_from_str(ctx,
2182 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2183 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2184 "i2 = i + 2 and j2 = j - 2 and "
2185 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2186 map = isl_map_transitive_closure(map, &exact);
2187 assert(exact);
2188 map2 = isl_map_read_from_str(ctx,
2189 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2190 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2191 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2192 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2193 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2194 assert(isl_map_is_equal(map, map2));
2195 isl_map_free(map);
2196 isl_map_free(map2);
2198 /* COCOA Fig.2 left */
2199 map = isl_map_read_from_str(ctx,
2200 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2201 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2202 "j <= n or "
2203 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2204 "j <= 2 i - 3 and j <= n - 2 or "
2205 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2206 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2207 map = isl_map_transitive_closure(map, &exact);
2208 assert(exact);
2209 isl_map_free(map);
2211 /* COCOA Fig.2 right */
2212 map = isl_map_read_from_str(ctx,
2213 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2214 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2215 "j <= n or "
2216 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2217 "j <= 2 i - 4 and j <= n - 3 or "
2218 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2219 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2220 map = isl_map_power(map, &exact);
2221 assert(exact);
2222 isl_map_free(map);
2224 /* COCOA Fig.2 right */
2225 map = isl_map_read_from_str(ctx,
2226 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2227 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2228 "j <= n or "
2229 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2230 "j <= 2 i - 4 and j <= n - 3 or "
2231 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2232 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2233 map = isl_map_transitive_closure(map, &exact);
2234 assert(exact);
2235 map2 = isl_map_read_from_str(ctx,
2236 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2237 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2238 "j <= n and 3 + i + 2 j <= 3 n and "
2239 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2240 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2241 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2242 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2243 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2244 assert(isl_map_is_equal(map, map2));
2245 isl_map_free(map2);
2246 isl_map_free(map);
2248 map = cocoa_fig_1_right_graph(ctx);
2249 map = isl_map_transitive_closure(map, &exact);
2250 assert(exact);
2251 map2 = cocoa_fig_1_right_tc(ctx);
2252 assert(isl_map_is_equal(map, map2));
2253 isl_map_free(map2);
2254 isl_map_free(map);
2256 map = cocoa_fig_1_right_graph(ctx);
2257 map = isl_map_power(map, &exact);
2258 map2 = cocoa_fig_1_right_power(ctx);
2259 equal = isl_map_is_equal(map, map2);
2260 isl_map_free(map2);
2261 isl_map_free(map);
2262 if (equal < 0)
2263 return -1;
2264 if (!exact)
2265 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2266 if (!equal)
2267 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2269 /* COCOA Theorem 1 counter example */
2270 map = isl_map_read_from_str(ctx,
2271 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2272 "i2 = 1 and j2 = j or "
2273 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2274 map = isl_map_transitive_closure(map, &exact);
2275 assert(exact);
2276 isl_map_free(map);
2278 map = isl_map_read_from_str(ctx,
2279 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2280 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2281 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2282 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2283 map = isl_map_transitive_closure(map, &exact);
2284 assert(exact);
2285 isl_map_free(map);
2287 /* Kelly et al 1996, fig 12 */
2288 map = isl_map_read_from_str(ctx,
2289 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2290 "1 <= i,j,j+1 <= n or "
2291 "j = n and j2 = 1 and i2 = i + 1 and "
2292 "1 <= i,i+1 <= n }");
2293 map = isl_map_transitive_closure(map, &exact);
2294 assert(exact);
2295 map2 = isl_map_read_from_str(ctx,
2296 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2297 "1 <= i <= n and i = i2 or "
2298 "1 <= i < i2 <= n and 1 <= j <= n and "
2299 "1 <= j2 <= n }");
2300 assert(isl_map_is_equal(map, map2));
2301 isl_map_free(map2);
2302 isl_map_free(map);
2304 /* Omega's closure4 */
2305 map = isl_map_read_from_str(ctx,
2306 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2307 "1 <= x,y <= 10 or "
2308 "x2 = x + 1 and y2 = y and "
2309 "1 <= x <= 20 && 5 <= y <= 15 }");
2310 map = isl_map_transitive_closure(map, &exact);
2311 assert(exact);
2312 isl_map_free(map);
2314 map = isl_map_read_from_str(ctx,
2315 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2316 map = isl_map_transitive_closure(map, &exact);
2317 assert(!exact);
2318 map2 = isl_map_read_from_str(ctx,
2319 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2320 assert(isl_map_is_equal(map, map2));
2321 isl_map_free(map);
2322 isl_map_free(map2);
2324 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2325 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2326 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2327 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2328 map = isl_map_read_from_str(ctx, str);
2329 map = isl_map_transitive_closure(map, &exact);
2330 assert(exact);
2331 map2 = isl_map_read_from_str(ctx, str);
2332 assert(isl_map_is_equal(map, map2));
2333 isl_map_free(map);
2334 isl_map_free(map2);
2336 str = "{[0] -> [1]; [2] -> [3]}";
2337 map = isl_map_read_from_str(ctx, str);
2338 map = isl_map_transitive_closure(map, &exact);
2339 assert(exact);
2340 map2 = isl_map_read_from_str(ctx, str);
2341 assert(isl_map_is_equal(map, map2));
2342 isl_map_free(map);
2343 isl_map_free(map2);
2345 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2346 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2347 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2348 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2349 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2350 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2351 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2352 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2353 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2354 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2355 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2356 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2357 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2358 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2359 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2360 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2361 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2362 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2363 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2364 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2365 map = isl_map_read_from_str(ctx, str);
2366 map = isl_map_transitive_closure(map, NULL);
2367 assert(map);
2368 isl_map_free(map);
2370 return 0;
2373 static int test_lex(struct isl_ctx *ctx)
2375 isl_space *dim;
2376 isl_map *map;
2377 int empty;
2379 dim = isl_space_set_alloc(ctx, 0, 0);
2380 map = isl_map_lex_le(dim);
2381 empty = isl_map_is_empty(map);
2382 isl_map_free(map);
2384 if (empty < 0)
2385 return -1;
2386 if (empty)
2387 isl_die(ctx, isl_error_unknown,
2388 "expecting non-empty result", return -1);
2390 return 0;
2393 /* Inputs for isl_map_lexmin tests.
2394 * "map" is the input and "lexmin" is the expected result.
2396 struct {
2397 const char *map;
2398 const char *lexmin;
2399 } lexmin_tests [] = {
2400 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
2401 "{ [x] -> [5] : 6 <= x <= 8; "
2402 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
2403 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
2404 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
2405 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
2406 "{ [x] -> [y] : (4y = x and x >= 0) or "
2407 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2408 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2409 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
2410 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
2411 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
2412 /* Check that empty pieces are properly combined. */
2413 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2414 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2415 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
2416 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2417 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2418 "x >= 4 }" },
2419 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
2420 "a <= 255 and c <= 255 and d <= 255 - j and "
2421 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
2422 "247d <= 247 + k - j and 247d <= 247 + k - b and "
2423 "247d <= 247 + i and 248 - b <= 248d <= c and "
2424 "254d >= i - a + b and 254d >= -a + b and "
2425 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
2426 "{ [i, k, j] -> "
2427 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
2428 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
2429 "247j >= 62738 - i and 509j <= 129795 + i and "
2430 "742j >= 188724 - i; "
2431 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
2432 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
2433 "16*floor((8 + b)/16) <= 7 + b; "
2434 "[a] -> [1] }",
2435 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
2436 "[a] -> [b = 0] : 0 < a <= 509 }" },
2437 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
2438 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
2441 static int test_lexmin(struct isl_ctx *ctx)
2443 int i;
2444 int equal;
2445 const char *str;
2446 isl_basic_map *bmap;
2447 isl_map *map, *map2;
2448 isl_set *set;
2449 isl_set *set2;
2450 isl_pw_multi_aff *pma;
2452 str = "[p0, p1] -> { [] -> [] : "
2453 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2454 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2455 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2456 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2457 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2458 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2459 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2460 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2461 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2462 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2463 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2464 map = isl_map_read_from_str(ctx, str);
2465 map = isl_map_lexmin(map);
2466 isl_map_free(map);
2468 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2469 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2470 set = isl_set_read_from_str(ctx, str);
2471 set = isl_set_lexmax(set);
2472 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2473 set2 = isl_set_read_from_str(ctx, str);
2474 set = isl_set_intersect(set, set2);
2475 assert(!isl_set_is_empty(set));
2476 isl_set_free(set);
2478 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
2479 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
2480 map = isl_map_lexmin(map);
2481 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
2482 equal = isl_map_is_equal(map, map2);
2483 isl_map_free(map);
2484 isl_map_free(map2);
2486 if (equal < 0)
2487 return -1;
2488 if (!equal)
2489 isl_die(ctx, isl_error_unknown,
2490 "unexpected result", return -1);
2493 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2494 " 8i' <= i and 8i' >= -7 + i }";
2495 bmap = isl_basic_map_read_from_str(ctx, str);
2496 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2497 map2 = isl_map_from_pw_multi_aff(pma);
2498 map = isl_map_from_basic_map(bmap);
2499 assert(isl_map_is_equal(map, map2));
2500 isl_map_free(map);
2501 isl_map_free(map2);
2503 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2504 " 8i' <= i and 8i' >= -7 + i }";
2505 set = isl_set_read_from_str(ctx, str);
2506 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2507 set2 = isl_set_from_pw_multi_aff(pma);
2508 equal = isl_set_is_equal(set, set2);
2509 isl_set_free(set);
2510 isl_set_free(set2);
2511 if (equal < 0)
2512 return -1;
2513 if (!equal)
2514 isl_die(ctx, isl_error_unknown,
2515 "unexpected difference between set and "
2516 "piecewise affine expression", return -1);
2518 return 0;
2521 /* A specialized isl_set_min_val test case that would return the wrong result
2522 * in earlier versions of isl.
2523 * The explicit call to isl_basic_set_union prevents the second basic set
2524 * from being determined to be empty prior to the call to isl_set_min_val,
2525 * at least at the point where this test case was introduced.
2527 static int test_min_special(isl_ctx *ctx)
2529 const char *str;
2530 isl_basic_set *bset1, *bset2;
2531 isl_set *set;
2532 isl_aff *obj;
2533 isl_val *res;
2534 int ok;
2536 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
2537 bset1 = isl_basic_set_read_from_str(ctx, str);
2538 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
2539 bset2 = isl_basic_set_read_from_str(ctx, str);
2540 set = isl_basic_set_union(bset1, bset2);
2541 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
2543 res = isl_set_min_val(set, obj);
2544 ok = isl_val_cmp_si(res, 5) == 0;
2546 isl_aff_free(obj);
2547 isl_set_free(set);
2548 isl_val_free(res);
2550 if (!res)
2551 return -1;
2552 if (!ok)
2553 isl_die(ctx, isl_error_unknown, "unexpected minimum",
2554 return -1);
2556 return 0;
2559 /* A specialized isl_set_min_val test case that would return an error
2560 * in earlier versions of isl.
2562 static int test_min_special2(isl_ctx *ctx)
2564 const char *str;
2565 isl_basic_set *bset;
2566 isl_aff *obj;
2567 isl_val *res;
2569 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
2570 bset = isl_basic_set_read_from_str(ctx, str);
2572 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
2574 res = isl_basic_set_max_val(bset, obj);
2576 isl_basic_set_free(bset);
2577 isl_aff_free(obj);
2578 isl_val_free(res);
2580 if (!res)
2581 return -1;
2583 return 0;
2586 struct {
2587 const char *set;
2588 const char *obj;
2589 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2590 __isl_keep isl_aff *obj);
2591 const char *res;
2592 } opt_tests[] = {
2593 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2594 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2595 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2596 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2597 &isl_set_max_val, "30" },
2601 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2602 * In particular, check the results on non-convex inputs.
2604 static int test_min(struct isl_ctx *ctx)
2606 int i;
2607 isl_set *set;
2608 isl_aff *obj;
2609 isl_val *val, *res;
2610 isl_bool ok;
2612 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2613 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2614 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2615 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2616 val = opt_tests[i].fn(set, obj);
2617 ok = isl_val_eq(res, val);
2618 isl_val_free(res);
2619 isl_val_free(val);
2620 isl_aff_free(obj);
2621 isl_set_free(set);
2623 if (ok < 0)
2624 return -1;
2625 if (!ok)
2626 isl_die(ctx, isl_error_unknown,
2627 "unexpected optimum", return -1);
2630 if (test_min_special(ctx) < 0)
2631 return -1;
2632 if (test_min_special2(ctx) < 0)
2633 return -1;
2635 return 0;
2638 struct must_may {
2639 isl_map *must;
2640 isl_map *may;
2643 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2644 void *dep_user, void *user)
2646 struct must_may *mm = (struct must_may *)user;
2648 if (must)
2649 mm->must = isl_map_union(mm->must, dep);
2650 else
2651 mm->may = isl_map_union(mm->may, dep);
2653 return isl_stat_ok;
2656 static int common_space(void *first, void *second)
2658 int depth = *(int *)first;
2659 return 2 * depth;
2662 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2664 isl_map *map2;
2665 int equal;
2667 if (!map)
2668 return -1;
2670 map2 = isl_map_read_from_str(map->ctx, str);
2671 equal = isl_map_is_equal(map, map2);
2672 isl_map_free(map2);
2674 return equal;
2677 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2679 int equal;
2681 equal = map_is_equal(map, str);
2682 if (equal < 0)
2683 return -1;
2684 if (!equal)
2685 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2686 "result not as expected", return -1);
2687 return 0;
2690 static int test_dep(struct isl_ctx *ctx)
2692 const char *str;
2693 isl_space *dim;
2694 isl_map *map;
2695 isl_access_info *ai;
2696 isl_flow *flow;
2697 int depth;
2698 struct must_may mm;
2700 depth = 3;
2702 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2703 map = isl_map_read_from_str(ctx, str);
2704 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2706 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2707 map = isl_map_read_from_str(ctx, str);
2708 ai = isl_access_info_add_source(ai, map, 1, &depth);
2710 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2711 map = isl_map_read_from_str(ctx, str);
2712 ai = isl_access_info_add_source(ai, map, 1, &depth);
2714 flow = isl_access_info_compute_flow(ai);
2715 dim = isl_space_alloc(ctx, 0, 3, 3);
2716 mm.must = isl_map_empty(isl_space_copy(dim));
2717 mm.may = isl_map_empty(dim);
2719 isl_flow_foreach(flow, collect_must_may, &mm);
2721 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2722 " [1,10,0] -> [2,5,0] }";
2723 assert(map_is_equal(mm.must, str));
2724 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2725 assert(map_is_equal(mm.may, str));
2727 isl_map_free(mm.must);
2728 isl_map_free(mm.may);
2729 isl_flow_free(flow);
2732 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2733 map = isl_map_read_from_str(ctx, str);
2734 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2736 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2737 map = isl_map_read_from_str(ctx, str);
2738 ai = isl_access_info_add_source(ai, map, 1, &depth);
2740 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2741 map = isl_map_read_from_str(ctx, str);
2742 ai = isl_access_info_add_source(ai, map, 0, &depth);
2744 flow = isl_access_info_compute_flow(ai);
2745 dim = isl_space_alloc(ctx, 0, 3, 3);
2746 mm.must = isl_map_empty(isl_space_copy(dim));
2747 mm.may = isl_map_empty(dim);
2749 isl_flow_foreach(flow, collect_must_may, &mm);
2751 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2752 assert(map_is_equal(mm.must, str));
2753 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2754 assert(map_is_equal(mm.may, str));
2756 isl_map_free(mm.must);
2757 isl_map_free(mm.may);
2758 isl_flow_free(flow);
2761 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2762 map = isl_map_read_from_str(ctx, str);
2763 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2765 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2766 map = isl_map_read_from_str(ctx, str);
2767 ai = isl_access_info_add_source(ai, map, 0, &depth);
2769 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2770 map = isl_map_read_from_str(ctx, str);
2771 ai = isl_access_info_add_source(ai, map, 0, &depth);
2773 flow = isl_access_info_compute_flow(ai);
2774 dim = isl_space_alloc(ctx, 0, 3, 3);
2775 mm.must = isl_map_empty(isl_space_copy(dim));
2776 mm.may = isl_map_empty(dim);
2778 isl_flow_foreach(flow, collect_must_may, &mm);
2780 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2781 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2782 assert(map_is_equal(mm.may, str));
2783 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2784 assert(map_is_equal(mm.must, str));
2786 isl_map_free(mm.must);
2787 isl_map_free(mm.may);
2788 isl_flow_free(flow);
2791 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2792 map = isl_map_read_from_str(ctx, str);
2793 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2795 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2796 map = isl_map_read_from_str(ctx, str);
2797 ai = isl_access_info_add_source(ai, map, 0, &depth);
2799 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2800 map = isl_map_read_from_str(ctx, str);
2801 ai = isl_access_info_add_source(ai, map, 0, &depth);
2803 flow = isl_access_info_compute_flow(ai);
2804 dim = isl_space_alloc(ctx, 0, 3, 3);
2805 mm.must = isl_map_empty(isl_space_copy(dim));
2806 mm.may = isl_map_empty(dim);
2808 isl_flow_foreach(flow, collect_must_may, &mm);
2810 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2811 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2812 assert(map_is_equal(mm.may, str));
2813 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2814 assert(map_is_equal(mm.must, str));
2816 isl_map_free(mm.must);
2817 isl_map_free(mm.may);
2818 isl_flow_free(flow);
2821 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2822 map = isl_map_read_from_str(ctx, str);
2823 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2825 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2826 map = isl_map_read_from_str(ctx, str);
2827 ai = isl_access_info_add_source(ai, map, 0, &depth);
2829 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2830 map = isl_map_read_from_str(ctx, str);
2831 ai = isl_access_info_add_source(ai, map, 0, &depth);
2833 flow = isl_access_info_compute_flow(ai);
2834 dim = isl_space_alloc(ctx, 0, 3, 3);
2835 mm.must = isl_map_empty(isl_space_copy(dim));
2836 mm.may = isl_map_empty(dim);
2838 isl_flow_foreach(flow, collect_must_may, &mm);
2840 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2841 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2842 assert(map_is_equal(mm.may, str));
2843 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2844 assert(map_is_equal(mm.must, str));
2846 isl_map_free(mm.must);
2847 isl_map_free(mm.may);
2848 isl_flow_free(flow);
2851 depth = 5;
2853 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2854 map = isl_map_read_from_str(ctx, str);
2855 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2857 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2858 map = isl_map_read_from_str(ctx, str);
2859 ai = isl_access_info_add_source(ai, map, 1, &depth);
2861 flow = isl_access_info_compute_flow(ai);
2862 dim = isl_space_alloc(ctx, 0, 5, 5);
2863 mm.must = isl_map_empty(isl_space_copy(dim));
2864 mm.may = isl_map_empty(dim);
2866 isl_flow_foreach(flow, collect_must_may, &mm);
2868 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2869 assert(map_is_equal(mm.must, str));
2870 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2871 assert(map_is_equal(mm.may, str));
2873 isl_map_free(mm.must);
2874 isl_map_free(mm.may);
2875 isl_flow_free(flow);
2877 return 0;
2880 /* Check that the dependence analysis proceeds without errors.
2881 * Earlier versions of isl would break down during the analysis
2882 * due to the use of the wrong spaces.
2884 static int test_flow(isl_ctx *ctx)
2886 const char *str;
2887 isl_union_map *access, *schedule;
2888 isl_union_map *must_dep, *may_dep;
2889 int r;
2891 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2892 access = isl_union_map_read_from_str(ctx, str);
2893 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2894 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2895 "S2[] -> [1,0,0,0]; "
2896 "S3[] -> [-1,0,0,0] }";
2897 schedule = isl_union_map_read_from_str(ctx, str);
2898 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2899 isl_union_map_copy(access), schedule,
2900 &must_dep, &may_dep, NULL, NULL);
2901 isl_union_map_free(may_dep);
2902 isl_union_map_free(must_dep);
2904 return r;
2907 struct {
2908 const char *map;
2909 int sv;
2910 } sv_tests[] = {
2911 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2912 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2913 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2914 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2915 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2916 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2917 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2918 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2919 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2922 int test_sv(isl_ctx *ctx)
2924 isl_union_map *umap;
2925 int i;
2926 int sv;
2928 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2929 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2930 sv = isl_union_map_is_single_valued(umap);
2931 isl_union_map_free(umap);
2932 if (sv < 0)
2933 return -1;
2934 if (sv_tests[i].sv && !sv)
2935 isl_die(ctx, isl_error_internal,
2936 "map not detected as single valued", return -1);
2937 if (!sv_tests[i].sv && sv)
2938 isl_die(ctx, isl_error_internal,
2939 "map detected as single valued", return -1);
2942 return 0;
2945 struct {
2946 const char *str;
2947 int bijective;
2948 } bijective_tests[] = {
2949 { "[N,M]->{[i,j] -> [i]}", 0 },
2950 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
2951 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
2952 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
2953 { "[N,M]->{[i,j] -> [j,i]}", 1 },
2954 { "[N,M]->{[i,j] -> [i+j]}", 0 },
2955 { "[N,M]->{[i,j] -> []}", 0 },
2956 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
2957 { "[N,M]->{[i,j] -> [2i]}", 0 },
2958 { "[N,M]->{[i,j] -> [i,i]}", 0 },
2959 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
2960 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
2961 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
2964 static int test_bijective(struct isl_ctx *ctx)
2966 isl_map *map;
2967 int i;
2968 int bijective;
2970 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
2971 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
2972 bijective = isl_map_is_bijective(map);
2973 isl_map_free(map);
2974 if (bijective < 0)
2975 return -1;
2976 if (bijective_tests[i].bijective && !bijective)
2977 isl_die(ctx, isl_error_internal,
2978 "map not detected as bijective", return -1);
2979 if (!bijective_tests[i].bijective && bijective)
2980 isl_die(ctx, isl_error_internal,
2981 "map detected as bijective", return -1);
2984 return 0;
2987 /* Inputs for isl_pw_qpolynomial_gist tests.
2988 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2990 struct {
2991 const char *pwqp;
2992 const char *set;
2993 const char *gist;
2994 } pwqp_gist_tests[] = {
2995 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2996 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2997 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2998 "{ [i] -> -1/2 + 1/2 * i }" },
2999 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
3002 static int test_pwqp(struct isl_ctx *ctx)
3004 int i;
3005 const char *str;
3006 isl_set *set;
3007 isl_pw_qpolynomial *pwqp1, *pwqp2;
3008 int equal;
3010 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3011 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3013 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
3014 isl_dim_in, 1, 1);
3016 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3017 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3019 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3021 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3023 isl_pw_qpolynomial_free(pwqp1);
3025 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
3026 str = pwqp_gist_tests[i].pwqp;
3027 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3028 str = pwqp_gist_tests[i].set;
3029 set = isl_set_read_from_str(ctx, str);
3030 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
3031 str = pwqp_gist_tests[i].gist;
3032 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3033 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3034 equal = isl_pw_qpolynomial_is_zero(pwqp1);
3035 isl_pw_qpolynomial_free(pwqp1);
3037 if (equal < 0)
3038 return -1;
3039 if (!equal)
3040 isl_die(ctx, isl_error_unknown,
3041 "unexpected result", return -1);
3044 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
3045 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3046 str = "{ [i] -> ([(2 * [i/2])/5]) }";
3047 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3049 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3051 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3053 isl_pw_qpolynomial_free(pwqp1);
3055 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
3056 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3057 str = "{ [x] -> x }";
3058 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3060 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3062 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3064 isl_pw_qpolynomial_free(pwqp1);
3066 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3067 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3068 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3069 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3070 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3071 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3072 isl_pw_qpolynomial_free(pwqp1);
3074 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3075 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3076 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3077 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3078 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3079 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3080 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3081 isl_pw_qpolynomial_free(pwqp1);
3082 isl_pw_qpolynomial_free(pwqp2);
3083 if (equal < 0)
3084 return -1;
3085 if (!equal)
3086 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3088 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3089 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3090 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3091 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3092 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3093 isl_val_one(ctx));
3094 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3095 isl_pw_qpolynomial_free(pwqp1);
3096 isl_pw_qpolynomial_free(pwqp2);
3097 if (equal < 0)
3098 return -1;
3099 if (!equal)
3100 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3102 return 0;
3105 static int test_split_periods(isl_ctx *ctx)
3107 const char *str;
3108 isl_pw_qpolynomial *pwqp;
3110 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3111 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3112 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3113 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3115 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3117 isl_pw_qpolynomial_free(pwqp);
3119 if (!pwqp)
3120 return -1;
3122 return 0;
3125 static int test_union(isl_ctx *ctx)
3127 const char *str;
3128 isl_union_set *uset1, *uset2;
3129 isl_union_map *umap1, *umap2;
3130 int equal;
3132 str = "{ [i] : 0 <= i <= 1 }";
3133 uset1 = isl_union_set_read_from_str(ctx, str);
3134 str = "{ [1] -> [0] }";
3135 umap1 = isl_union_map_read_from_str(ctx, str);
3137 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3138 equal = isl_union_map_is_equal(umap1, umap2);
3140 isl_union_map_free(umap1);
3141 isl_union_map_free(umap2);
3143 if (equal < 0)
3144 return -1;
3145 if (!equal)
3146 isl_die(ctx, isl_error_unknown, "union maps not equal",
3147 return -1);
3149 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3150 umap1 = isl_union_map_read_from_str(ctx, str);
3151 str = "{ A[i]; B[i] }";
3152 uset1 = isl_union_set_read_from_str(ctx, str);
3154 uset2 = isl_union_map_domain(umap1);
3156 equal = isl_union_set_is_equal(uset1, uset2);
3158 isl_union_set_free(uset1);
3159 isl_union_set_free(uset2);
3161 if (equal < 0)
3162 return -1;
3163 if (!equal)
3164 isl_die(ctx, isl_error_unknown, "union sets not equal",
3165 return -1);
3167 return 0;
3170 /* Check that computing a bound of a non-zero polynomial over an unbounded
3171 * domain does not produce a rational value.
3172 * In particular, check that the upper bound is infinity.
3174 static int test_bound_unbounded_domain(isl_ctx *ctx)
3176 const char *str;
3177 isl_pw_qpolynomial *pwqp;
3178 isl_pw_qpolynomial_fold *pwf, *pwf2;
3179 isl_bool equal;
3181 str = "{ [m,n] -> -m * n }";
3182 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3183 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3184 str = "{ infty }";
3185 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3186 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3187 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
3188 isl_pw_qpolynomial_fold_free(pwf);
3189 isl_pw_qpolynomial_fold_free(pwf2);
3191 if (equal < 0)
3192 return -1;
3193 if (!equal)
3194 isl_die(ctx, isl_error_unknown,
3195 "expecting infinite polynomial bound", return -1);
3197 return 0;
3200 static int test_bound(isl_ctx *ctx)
3202 const char *str;
3203 unsigned dim;
3204 isl_pw_qpolynomial *pwqp;
3205 isl_pw_qpolynomial_fold *pwf;
3207 if (test_bound_unbounded_domain(ctx) < 0)
3208 return -1;
3210 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
3211 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3212 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3213 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3214 isl_pw_qpolynomial_fold_free(pwf);
3215 if (dim != 4)
3216 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3217 return -1);
3219 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
3220 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3221 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3222 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3223 isl_pw_qpolynomial_fold_free(pwf);
3224 if (dim != 1)
3225 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3226 return -1);
3228 return 0;
3231 static int test_lift(isl_ctx *ctx)
3233 const char *str;
3234 isl_basic_map *bmap;
3235 isl_basic_set *bset;
3237 str = "{ [i0] : exists e0 : i0 = 4e0 }";
3238 bset = isl_basic_set_read_from_str(ctx, str);
3239 bset = isl_basic_set_lift(bset);
3240 bmap = isl_basic_map_from_range(bset);
3241 bset = isl_basic_map_domain(bmap);
3242 isl_basic_set_free(bset);
3244 return 0;
3247 struct {
3248 const char *set1;
3249 const char *set2;
3250 int subset;
3251 } subset_tests[] = {
3252 { "{ [112, 0] }",
3253 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
3254 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
3255 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
3256 { "{ [65] }",
3257 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
3258 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
3259 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
3260 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
3261 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
3262 "256e0 <= 255i and 256e0 >= -255 + 255i and "
3263 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
3264 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
3265 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
3266 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
3267 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
3268 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
3269 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
3270 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
3271 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
3272 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
3273 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
3274 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
3275 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
3276 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
3277 "4e0 <= -57 + i0 + i1)) or "
3278 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
3279 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
3280 "4e0 >= -61 + i0 + i1)) or "
3281 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
3282 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
3285 static int test_subset(isl_ctx *ctx)
3287 int i;
3288 isl_set *set1, *set2;
3289 int subset;
3291 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3292 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3293 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3294 subset = isl_set_is_subset(set1, set2);
3295 isl_set_free(set1);
3296 isl_set_free(set2);
3297 if (subset < 0)
3298 return -1;
3299 if (subset != subset_tests[i].subset)
3300 isl_die(ctx, isl_error_unknown,
3301 "incorrect subset result", return -1);
3304 return 0;
3307 struct {
3308 const char *minuend;
3309 const char *subtrahend;
3310 const char *difference;
3311 } subtract_domain_tests[] = {
3312 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3313 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3314 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3317 static int test_subtract(isl_ctx *ctx)
3319 int i;
3320 isl_union_map *umap1, *umap2;
3321 isl_union_pw_multi_aff *upma1, *upma2;
3322 isl_union_set *uset;
3323 int equal;
3325 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3326 umap1 = isl_union_map_read_from_str(ctx,
3327 subtract_domain_tests[i].minuend);
3328 uset = isl_union_set_read_from_str(ctx,
3329 subtract_domain_tests[i].subtrahend);
3330 umap2 = isl_union_map_read_from_str(ctx,
3331 subtract_domain_tests[i].difference);
3332 umap1 = isl_union_map_subtract_domain(umap1, uset);
3333 equal = isl_union_map_is_equal(umap1, umap2);
3334 isl_union_map_free(umap1);
3335 isl_union_map_free(umap2);
3336 if (equal < 0)
3337 return -1;
3338 if (!equal)
3339 isl_die(ctx, isl_error_unknown,
3340 "incorrect subtract domain result", return -1);
3343 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3344 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3345 subtract_domain_tests[i].minuend);
3346 uset = isl_union_set_read_from_str(ctx,
3347 subtract_domain_tests[i].subtrahend);
3348 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3349 subtract_domain_tests[i].difference);
3350 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3351 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3352 isl_union_pw_multi_aff_free(upma1);
3353 isl_union_pw_multi_aff_free(upma2);
3354 if (equal < 0)
3355 return -1;
3356 if (!equal)
3357 isl_die(ctx, isl_error_unknown,
3358 "incorrect subtract domain result", return -1);
3361 return 0;
3364 /* Check that intersecting the empty basic set with another basic set
3365 * does not increase the number of constraints. In particular,
3366 * the empty basic set should maintain its canonical representation.
3368 static int test_intersect(isl_ctx *ctx)
3370 int n1, n2;
3371 isl_basic_set *bset1, *bset2;
3373 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3374 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3375 n1 = isl_basic_set_n_constraint(bset1);
3376 bset1 = isl_basic_set_intersect(bset1, bset2);
3377 n2 = isl_basic_set_n_constraint(bset1);
3378 isl_basic_set_free(bset1);
3379 if (!bset1)
3380 return -1;
3381 if (n1 != n2)
3382 isl_die(ctx, isl_error_unknown,
3383 "number of constraints of empty set changed",
3384 return -1);
3386 return 0;
3389 int test_factorize(isl_ctx *ctx)
3391 const char *str;
3392 isl_basic_set *bset;
3393 isl_factorizer *f;
3395 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3396 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3397 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3398 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3399 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3400 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3401 "3i5 >= -2i0 - i2 + 3i4 }";
3402 bset = isl_basic_set_read_from_str(ctx, str);
3403 f = isl_basic_set_factorizer(bset);
3404 isl_basic_set_free(bset);
3405 isl_factorizer_free(f);
3406 if (!f)
3407 isl_die(ctx, isl_error_unknown,
3408 "failed to construct factorizer", return -1);
3410 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3411 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3412 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3413 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3414 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3415 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3416 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3417 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3418 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3419 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3420 bset = isl_basic_set_read_from_str(ctx, str);
3421 f = isl_basic_set_factorizer(bset);
3422 isl_basic_set_free(bset);
3423 isl_factorizer_free(f);
3424 if (!f)
3425 isl_die(ctx, isl_error_unknown,
3426 "failed to construct factorizer", return -1);
3428 return 0;
3431 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3433 int *injective = user;
3435 *injective = isl_map_is_injective(map);
3436 isl_map_free(map);
3438 if (*injective < 0 || !*injective)
3439 return isl_stat_error;
3441 return isl_stat_ok;
3444 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3445 const char *r, const char *s, int tilable, int parallel)
3447 int i;
3448 isl_union_set *D;
3449 isl_union_map *W, *R, *S;
3450 isl_union_map *empty;
3451 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3452 isl_union_map *validity, *proximity, *coincidence;
3453 isl_union_map *schedule;
3454 isl_union_map *test;
3455 isl_union_set *delta;
3456 isl_union_set *domain;
3457 isl_set *delta_set;
3458 isl_set *slice;
3459 isl_set *origin;
3460 isl_schedule_constraints *sc;
3461 isl_schedule *sched;
3462 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3464 D = isl_union_set_read_from_str(ctx, d);
3465 W = isl_union_map_read_from_str(ctx, w);
3466 R = isl_union_map_read_from_str(ctx, r);
3467 S = isl_union_map_read_from_str(ctx, s);
3469 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3470 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3472 empty = isl_union_map_empty(isl_union_map_get_space(S));
3473 isl_union_map_compute_flow(isl_union_map_copy(R),
3474 isl_union_map_copy(W), empty,
3475 isl_union_map_copy(S),
3476 &dep_raw, NULL, NULL, NULL);
3477 isl_union_map_compute_flow(isl_union_map_copy(W),
3478 isl_union_map_copy(W),
3479 isl_union_map_copy(R),
3480 isl_union_map_copy(S),
3481 &dep_waw, &dep_war, NULL, NULL);
3483 dep = isl_union_map_union(dep_waw, dep_war);
3484 dep = isl_union_map_union(dep, dep_raw);
3485 validity = isl_union_map_copy(dep);
3486 coincidence = isl_union_map_copy(dep);
3487 proximity = isl_union_map_copy(dep);
3489 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3490 sc = isl_schedule_constraints_set_validity(sc, validity);
3491 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3492 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3493 sched = isl_schedule_constraints_compute_schedule(sc);
3494 schedule = isl_schedule_get_map(sched);
3495 isl_schedule_free(sched);
3496 isl_union_map_free(W);
3497 isl_union_map_free(R);
3498 isl_union_map_free(S);
3500 is_injection = 1;
3501 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3503 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3504 is_complete = isl_union_set_is_subset(D, domain);
3505 isl_union_set_free(D);
3506 isl_union_set_free(domain);
3508 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3509 test = isl_union_map_apply_range(test, dep);
3510 test = isl_union_map_apply_range(test, schedule);
3512 delta = isl_union_map_deltas(test);
3513 if (isl_union_set_n_set(delta) == 0) {
3514 is_tilable = 1;
3515 is_parallel = 1;
3516 is_nonneg = 1;
3517 isl_union_set_free(delta);
3518 } else {
3519 delta_set = isl_set_from_union_set(delta);
3521 slice = isl_set_universe(isl_set_get_space(delta_set));
3522 for (i = 0; i < tilable; ++i)
3523 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3524 is_tilable = isl_set_is_subset(delta_set, slice);
3525 isl_set_free(slice);
3527 slice = isl_set_universe(isl_set_get_space(delta_set));
3528 for (i = 0; i < parallel; ++i)
3529 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3530 is_parallel = isl_set_is_subset(delta_set, slice);
3531 isl_set_free(slice);
3533 origin = isl_set_universe(isl_set_get_space(delta_set));
3534 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
3535 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3537 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3538 delta_set = isl_set_lexmin(delta_set);
3540 is_nonneg = isl_set_is_equal(delta_set, origin);
3542 isl_set_free(origin);
3543 isl_set_free(delta_set);
3546 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3547 is_injection < 0 || is_complete < 0)
3548 return -1;
3549 if (!is_complete)
3550 isl_die(ctx, isl_error_unknown,
3551 "generated schedule incomplete", return -1);
3552 if (!is_injection)
3553 isl_die(ctx, isl_error_unknown,
3554 "generated schedule not injective on each statement",
3555 return -1);
3556 if (!is_nonneg)
3557 isl_die(ctx, isl_error_unknown,
3558 "negative dependences in generated schedule",
3559 return -1);
3560 if (!is_tilable)
3561 isl_die(ctx, isl_error_unknown,
3562 "generated schedule not as tilable as expected",
3563 return -1);
3564 if (!is_parallel)
3565 isl_die(ctx, isl_error_unknown,
3566 "generated schedule not as parallel as expected",
3567 return -1);
3569 return 0;
3572 /* Compute a schedule for the given instance set, validity constraints,
3573 * proximity constraints and context and return a corresponding union map
3574 * representation.
3576 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3577 const char *domain, const char *validity, const char *proximity,
3578 const char *context)
3580 isl_set *con;
3581 isl_union_set *dom;
3582 isl_union_map *dep;
3583 isl_union_map *prox;
3584 isl_schedule_constraints *sc;
3585 isl_schedule *schedule;
3586 isl_union_map *sched;
3588 con = isl_set_read_from_str(ctx, context);
3589 dom = isl_union_set_read_from_str(ctx, domain);
3590 dep = isl_union_map_read_from_str(ctx, validity);
3591 prox = isl_union_map_read_from_str(ctx, proximity);
3592 sc = isl_schedule_constraints_on_domain(dom);
3593 sc = isl_schedule_constraints_set_context(sc, con);
3594 sc = isl_schedule_constraints_set_validity(sc, dep);
3595 sc = isl_schedule_constraints_set_proximity(sc, prox);
3596 schedule = isl_schedule_constraints_compute_schedule(sc);
3597 sched = isl_schedule_get_map(schedule);
3598 isl_schedule_free(schedule);
3600 return sched;
3603 /* Compute a schedule for the given instance set, validity constraints and
3604 * proximity constraints and return a corresponding union map representation.
3606 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
3607 const char *domain, const char *validity, const char *proximity)
3609 return compute_schedule_with_context(ctx, domain, validity, proximity,
3610 "{ : }");
3613 /* Check that a schedule can be constructed on the given domain
3614 * with the given validity and proximity constraints.
3616 static int test_has_schedule(isl_ctx *ctx, const char *domain,
3617 const char *validity, const char *proximity)
3619 isl_union_map *sched;
3621 sched = compute_schedule(ctx, domain, validity, proximity);
3622 if (!sched)
3623 return -1;
3625 isl_union_map_free(sched);
3626 return 0;
3629 int test_special_schedule(isl_ctx *ctx, const char *domain,
3630 const char *validity, const char *proximity, const char *expected_sched)
3632 isl_union_map *sched1, *sched2;
3633 int equal;
3635 sched1 = compute_schedule(ctx, domain, validity, proximity);
3636 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
3638 equal = isl_union_map_is_equal(sched1, sched2);
3639 isl_union_map_free(sched1);
3640 isl_union_map_free(sched2);
3642 if (equal < 0)
3643 return -1;
3644 if (!equal)
3645 isl_die(ctx, isl_error_unknown, "unexpected schedule",
3646 return -1);
3648 return 0;
3651 /* Check that the schedule map is properly padded, even after being
3652 * reconstructed from the band forest.
3654 static int test_padded_schedule(isl_ctx *ctx)
3656 const char *str;
3657 isl_union_set *D;
3658 isl_union_map *validity, *proximity;
3659 isl_schedule_constraints *sc;
3660 isl_schedule *sched;
3661 isl_union_map *map1, *map2;
3662 isl_band_list *list;
3663 int equal;
3665 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
3666 D = isl_union_set_read_from_str(ctx, str);
3667 validity = isl_union_map_empty(isl_union_set_get_space(D));
3668 proximity = isl_union_map_copy(validity);
3669 sc = isl_schedule_constraints_on_domain(D);
3670 sc = isl_schedule_constraints_set_validity(sc, validity);
3671 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3672 sched = isl_schedule_constraints_compute_schedule(sc);
3673 map1 = isl_schedule_get_map(sched);
3674 list = isl_schedule_get_band_forest(sched);
3675 isl_band_list_free(list);
3676 map2 = isl_schedule_get_map(sched);
3677 isl_schedule_free(sched);
3678 equal = isl_union_map_is_equal(map1, map2);
3679 isl_union_map_free(map1);
3680 isl_union_map_free(map2);
3682 if (equal < 0)
3683 return -1;
3684 if (!equal)
3685 isl_die(ctx, isl_error_unknown,
3686 "reconstructed schedule map not the same as original",
3687 return -1);
3689 return 0;
3692 /* Check that conditional validity constraints are also taken into
3693 * account across bands.
3694 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
3695 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
3696 * and then check that the adjacent order constraint C[2,1]->D[2,0]
3697 * is enforced by the rest of the schedule.
3699 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
3701 const char *str;
3702 isl_union_set *domain;
3703 isl_union_map *validity, *proximity, *condition;
3704 isl_union_map *sink, *source, *dep;
3705 isl_schedule_constraints *sc;
3706 isl_schedule *schedule;
3707 isl_union_access_info *access;
3708 isl_union_flow *flow;
3709 int empty;
3711 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3712 "A[k] : k >= 1 and k <= -1 + n; "
3713 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3714 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
3715 domain = isl_union_set_read_from_str(ctx, str);
3716 sc = isl_schedule_constraints_on_domain(domain);
3717 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
3718 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3719 "D[k, i] -> C[1 + k, i] : "
3720 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3721 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
3722 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
3723 validity = isl_union_map_read_from_str(ctx, str);
3724 sc = isl_schedule_constraints_set_validity(sc, validity);
3725 str = "[n] -> { C[k, i] -> D[k, i] : "
3726 "0 <= i <= -1 + k and k <= -1 + n }";
3727 proximity = isl_union_map_read_from_str(ctx, str);
3728 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3729 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
3730 "i <= -1 + k and i >= 1 and k <= -2 + n; "
3731 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
3732 "k <= -1 + n and i >= 0 and i <= -2 + k }";
3733 condition = isl_union_map_read_from_str(ctx, str);
3734 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
3735 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3736 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
3737 "i >= 0 and i <= -1 + k and k <= -1 + n and "
3738 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
3739 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
3740 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3741 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
3742 "k <= -1 + n and i >= 0 and i <= -1 + k and "
3743 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
3744 validity = isl_union_map_read_from_str(ctx, str);
3745 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3746 validity);
3747 schedule = isl_schedule_constraints_compute_schedule(sc);
3748 str = "{ D[2,0] -> [] }";
3749 sink = isl_union_map_read_from_str(ctx, str);
3750 access = isl_union_access_info_from_sink(sink);
3751 str = "{ C[2,1] -> [] }";
3752 source = isl_union_map_read_from_str(ctx, str);
3753 access = isl_union_access_info_set_must_source(access, source);
3754 access = isl_union_access_info_set_schedule(access, schedule);
3755 flow = isl_union_access_info_compute_flow(access);
3756 dep = isl_union_flow_get_must_dependence(flow);
3757 isl_union_flow_free(flow);
3758 empty = isl_union_map_is_empty(dep);
3759 isl_union_map_free(dep);
3761 if (empty < 0)
3762 return -1;
3763 if (empty)
3764 isl_die(ctx, isl_error_unknown,
3765 "conditional validity not respected", return -1);
3767 return 0;
3770 /* Check that the test for violated conditional validity constraints
3771 * is not confused by domain compression.
3772 * In particular, earlier versions of isl would apply
3773 * a schedule on the compressed domains to the original domains,
3774 * resulting in a failure to detect that the default schedule
3775 * violates the conditional validity constraints.
3777 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
3779 const char *str;
3780 isl_bool empty;
3781 isl_union_set *domain;
3782 isl_union_map *validity, *condition;
3783 isl_schedule_constraints *sc;
3784 isl_schedule *schedule;
3785 isl_union_map *umap;
3786 isl_map *map, *ge;
3788 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
3789 domain = isl_union_set_read_from_str(ctx, str);
3790 sc = isl_schedule_constraints_on_domain(domain);
3791 str = "{ B[1, i] -> A[0, i + 1] }";
3792 condition = isl_union_map_read_from_str(ctx, str);
3793 str = "{ A[0, i] -> B[1, i - 1] }";
3794 validity = isl_union_map_read_from_str(ctx, str);
3795 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3796 isl_union_map_copy(validity));
3797 schedule = isl_schedule_constraints_compute_schedule(sc);
3798 umap = isl_schedule_get_map(schedule);
3799 isl_schedule_free(schedule);
3800 validity = isl_union_map_apply_domain(validity,
3801 isl_union_map_copy(umap));
3802 validity = isl_union_map_apply_range(validity, umap);
3803 map = isl_map_from_union_map(validity);
3804 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3805 map = isl_map_intersect(map, ge);
3806 empty = isl_map_is_empty(map);
3807 isl_map_free(map);
3809 if (empty < 0)
3810 return -1;
3811 if (!empty)
3812 isl_die(ctx, isl_error_unknown,
3813 "conditional validity constraints not satisfied",
3814 return -1);
3816 return 0;
3819 /* Input for testing of schedule construction based on
3820 * conditional constraints.
3822 * domain is the iteration domain
3823 * flow are the flow dependences, which determine the validity and
3824 * proximity constraints
3825 * condition are the conditions on the conditional validity constraints
3826 * conditional_validity are the conditional validity constraints
3827 * outer_band_n is the expected number of members in the outer band
3829 struct {
3830 const char *domain;
3831 const char *flow;
3832 const char *condition;
3833 const char *conditional_validity;
3834 int outer_band_n;
3835 } live_range_tests[] = {
3836 /* Contrived example that illustrates that we need to keep
3837 * track of tagged condition dependences and
3838 * tagged conditional validity dependences
3839 * in isl_sched_edge separately.
3840 * In particular, the conditional validity constraints on A
3841 * cannot be satisfied,
3842 * but they can be ignored because there are no corresponding
3843 * condition constraints. However, we do have an additional
3844 * conditional validity constraint that maps to the same
3845 * dependence relation
3846 * as the condition constraint on B. If we did not make a distinction
3847 * between tagged condition and tagged conditional validity
3848 * dependences, then we
3849 * could end up treating this shared dependence as an condition
3850 * constraint on A, forcing a localization of the conditions,
3851 * which is impossible.
3853 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
3854 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
3855 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
3856 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3857 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3858 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
3861 /* TACO 2013 Fig. 7 */
3862 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3863 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3864 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3865 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
3866 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
3867 "0 <= i < n and 0 <= j < n - 1 }",
3868 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
3869 "0 <= i < n and 0 <= j < j' < n;"
3870 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
3871 "0 <= i < i' < n and 0 <= j,j' < n;"
3872 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
3873 "0 <= i,j,j' < n and j < j' }",
3876 /* TACO 2013 Fig. 7, without tags */
3877 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3878 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3879 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3880 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3881 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3882 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
3883 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
3884 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
3887 /* TACO 2013 Fig. 12 */
3888 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
3889 "S3[i,3] : 0 <= i <= 1 }",
3890 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
3891 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
3892 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
3893 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
3894 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3895 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
3896 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3897 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
3898 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
3899 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
3900 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
3905 /* Test schedule construction based on conditional constraints.
3906 * In particular, check the number of members in the outer band node
3907 * as an indication of whether tiling is possible or not.
3909 static int test_conditional_schedule_constraints(isl_ctx *ctx)
3911 int i;
3912 isl_union_set *domain;
3913 isl_union_map *condition;
3914 isl_union_map *flow;
3915 isl_union_map *validity;
3916 isl_schedule_constraints *sc;
3917 isl_schedule *schedule;
3918 isl_schedule_node *node;
3919 int n_member;
3921 if (test_special_conditional_schedule_constraints(ctx) < 0)
3922 return -1;
3923 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
3924 return -1;
3926 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
3927 domain = isl_union_set_read_from_str(ctx,
3928 live_range_tests[i].domain);
3929 flow = isl_union_map_read_from_str(ctx,
3930 live_range_tests[i].flow);
3931 condition = isl_union_map_read_from_str(ctx,
3932 live_range_tests[i].condition);
3933 validity = isl_union_map_read_from_str(ctx,
3934 live_range_tests[i].conditional_validity);
3935 sc = isl_schedule_constraints_on_domain(domain);
3936 sc = isl_schedule_constraints_set_validity(sc,
3937 isl_union_map_copy(flow));
3938 sc = isl_schedule_constraints_set_proximity(sc, flow);
3939 sc = isl_schedule_constraints_set_conditional_validity(sc,
3940 condition, validity);
3941 schedule = isl_schedule_constraints_compute_schedule(sc);
3942 node = isl_schedule_get_root(schedule);
3943 while (node &&
3944 isl_schedule_node_get_type(node) != isl_schedule_node_band)
3945 node = isl_schedule_node_first_child(node);
3946 n_member = isl_schedule_node_band_n_member(node);
3947 isl_schedule_node_free(node);
3948 isl_schedule_free(schedule);
3950 if (!schedule)
3951 return -1;
3952 if (n_member != live_range_tests[i].outer_band_n)
3953 isl_die(ctx, isl_error_unknown,
3954 "unexpected number of members in outer band",
3955 return -1);
3957 return 0;
3960 /* Check that the schedule computed for the given instance set and
3961 * dependence relation strongly satisfies the dependences.
3962 * In particular, check that no instance is scheduled before
3963 * or together with an instance on which it depends.
3964 * Earlier versions of isl would produce a schedule that
3965 * only weakly satisfies the dependences.
3967 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
3969 const char *domain, *dep;
3970 isl_union_map *D, *schedule;
3971 isl_map *map, *ge;
3972 int empty;
3974 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
3975 "A[i0] : 0 <= i0 <= 1 }";
3976 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
3977 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
3978 schedule = compute_schedule(ctx, domain, dep, dep);
3979 D = isl_union_map_read_from_str(ctx, dep);
3980 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
3981 D = isl_union_map_apply_range(D, schedule);
3982 map = isl_map_from_union_map(D);
3983 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3984 map = isl_map_intersect(map, ge);
3985 empty = isl_map_is_empty(map);
3986 isl_map_free(map);
3988 if (empty < 0)
3989 return -1;
3990 if (!empty)
3991 isl_die(ctx, isl_error_unknown,
3992 "dependences not strongly satisfied", return -1);
3994 return 0;
3997 /* Compute a schedule for input where the instance set constraints
3998 * conflict with the context constraints.
3999 * Earlier versions of isl did not properly handle this situation.
4001 static int test_conflicting_context_schedule(isl_ctx *ctx)
4003 isl_union_map *schedule;
4004 const char *domain, *context;
4006 domain = "[n] -> { A[] : n >= 0 }";
4007 context = "[n] -> { : n < 0 }";
4008 schedule = compute_schedule_with_context(ctx,
4009 domain, "{}", "{}", context);
4010 isl_union_map_free(schedule);
4012 if (!schedule)
4013 return -1;
4015 return 0;
4018 /* Check that the dependence carrying step is not confused by
4019 * a bound on the coefficient size.
4020 * In particular, force the scheduler to move to a dependence carrying
4021 * step by demanding outer coincidence and bound the size of
4022 * the coefficients. Earlier versions of isl would take this
4023 * bound into account while carrying dependences, breaking
4024 * fundamental assumptions.
4025 * On the other hand, the dependence carrying step now tries
4026 * to prevent loop coalescing by default, so check that indeed
4027 * no loop coalescing occurs by comparing the computed schedule
4028 * to the expected non-coalescing schedule.
4030 static int test_bounded_coefficients_schedule(isl_ctx *ctx)
4032 const char *domain, *dep;
4033 isl_union_set *I;
4034 isl_union_map *D;
4035 isl_schedule_constraints *sc;
4036 isl_schedule *schedule;
4037 isl_union_map *sched1, *sched2;
4038 isl_bool equal;
4040 domain = "{ C[i0, i1] : 2 <= i0 <= 3999 and 0 <= i1 <= -1 + i0 }";
4041 dep = "{ C[i0, i1] -> C[i0, 1 + i1] : i0 <= 3999 and i1 >= 0 and "
4042 "i1 <= -2 + i0; "
4043 "C[i0, -1 + i0] -> C[1 + i0, 0] : i0 <= 3998 and i0 >= 1 }";
4044 I = isl_union_set_read_from_str(ctx, domain);
4045 D = isl_union_map_read_from_str(ctx, dep);
4046 sc = isl_schedule_constraints_on_domain(I);
4047 sc = isl_schedule_constraints_set_validity(sc, isl_union_map_copy(D));
4048 sc = isl_schedule_constraints_set_coincidence(sc, D);
4049 isl_options_set_schedule_outer_coincidence(ctx, 1);
4050 isl_options_set_schedule_max_coefficient(ctx, 20);
4051 schedule = isl_schedule_constraints_compute_schedule(sc);
4052 isl_options_set_schedule_max_coefficient(ctx, -1);
4053 isl_options_set_schedule_outer_coincidence(ctx, 0);
4054 sched1 = isl_schedule_get_map(schedule);
4055 isl_schedule_free(schedule);
4057 sched2 = isl_union_map_read_from_str(ctx, "{ C[x,y] -> [x,y] }");
4058 equal = isl_union_map_is_equal(sched1, sched2);
4059 isl_union_map_free(sched1);
4060 isl_union_map_free(sched2);
4062 if (equal < 0)
4063 return -1;
4064 if (!equal)
4065 isl_die(ctx, isl_error_unknown,
4066 "unexpected schedule", return -1);
4068 return 0;
4071 /* Check that the bounds on the coefficients are respected.
4072 * This function checks for a particular output schedule,
4073 * but the exact output is not important, only that it does
4074 * not contain any coefficients greater than 4.
4075 * It is, however, easier to check for a particular output.
4076 * This test is only run for the whole component scheduler
4077 * because the incremental scheduler produces a slightly different schedule.
4079 static int test_bounded_coefficients_schedule_whole(isl_ctx *ctx)
4081 const char *domain, *dep, *str;
4082 isl_union_set *I;
4083 isl_union_map *D;
4084 isl_schedule_constraints *sc;
4085 isl_schedule *schedule;
4086 isl_union_map *sched1, *sched2;
4087 isl_bool equal;
4089 domain = "{ S_4[i, j, k] : 0 <= i < j <= 10 and 0 <= k <= 100; "
4090 "S_2[i, j] : 0 <= i < j <= 10; S_6[i, j] : 0 <= i < j <= 10 }";
4091 dep = "{ S_2[0, j] -> S_4[0, j, 0] : 0 < j <= 10; "
4092 "S_4[0, j, 100] -> S_6[0, j] : 0 < j <= 10 }";
4093 I = isl_union_set_read_from_str(ctx, domain);
4094 D = isl_union_map_read_from_str(ctx, dep);
4095 sc = isl_schedule_constraints_on_domain(I);
4096 sc = isl_schedule_constraints_set_validity(sc, D);
4097 isl_options_set_schedule_max_constant_term(ctx, 10);
4098 isl_options_set_schedule_max_coefficient(ctx, 4);
4099 schedule = isl_schedule_constraints_compute_schedule(sc);
4100 isl_options_set_schedule_max_coefficient(ctx, -1);
4101 isl_options_set_schedule_max_constant_term(ctx, -1);
4102 sched1 = isl_schedule_get_map(schedule);
4103 isl_schedule_free(schedule);
4105 str = "{ S_4[i, j, k] -> [i, j, 10 - k]; "
4106 "S_2[i, j] -> [0, i, j]; S_6[i, j] -> [0, 10 + i, j] }";
4107 sched2 = isl_union_map_read_from_str(ctx, str);
4108 equal = isl_union_map_is_equal(sched1, sched2);
4109 isl_union_map_free(sched1);
4110 isl_union_map_free(sched2);
4112 if (equal < 0)
4113 return -1;
4114 if (!equal)
4115 isl_die(ctx, isl_error_unknown,
4116 "unexpected schedule", return -1);
4118 return 0;
4121 /* Check that a set of schedule constraints that only allow for
4122 * a coalescing schedule still produces a schedule even if the user
4123 * request a non-coalescing schedule. Earlier versions of isl
4124 * would not handle this case correctly.
4126 static int test_coalescing_schedule(isl_ctx *ctx)
4128 const char *domain, *dep;
4129 isl_union_set *I;
4130 isl_union_map *D;
4131 isl_schedule_constraints *sc;
4132 isl_schedule *schedule;
4133 int treat_coalescing;
4135 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
4136 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
4137 I = isl_union_set_read_from_str(ctx, domain);
4138 D = isl_union_map_read_from_str(ctx, dep);
4139 sc = isl_schedule_constraints_on_domain(I);
4140 sc = isl_schedule_constraints_set_validity(sc, D);
4141 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4142 isl_options_set_schedule_treat_coalescing(ctx, 1);
4143 schedule = isl_schedule_constraints_compute_schedule(sc);
4144 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4145 isl_schedule_free(schedule);
4146 if (!schedule)
4147 return -1;
4148 return 0;
4151 /* Check that the scheduler does not perform any needless
4152 * compound skewing. Earlier versions of isl would compute
4153 * schedules in terms of transformed schedule coefficients and
4154 * would not accurately keep track of the sum of the original
4155 * schedule coefficients. It could then produce the schedule
4156 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
4157 * for the input below instead of the schedule below.
4159 static int test_skewing_schedule(isl_ctx *ctx)
4161 const char *D, *V, *P, *S;
4163 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
4164 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
4165 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
4166 "-1 <= a-i + b-j + c-k <= 1 }";
4167 P = "{ }";
4168 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
4170 return test_special_schedule(ctx, D, V, P, S);
4173 int test_schedule(isl_ctx *ctx)
4175 const char *D, *W, *R, *V, *P, *S;
4176 int max_coincidence;
4177 int treat_coalescing;
4179 /* Handle resulting schedule with zero bands. */
4180 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
4181 return -1;
4183 /* Jacobi */
4184 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
4185 W = "{ S1[t,i] -> a[t,i] }";
4186 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
4187 "S1[t,i] -> a[t-1,i+1] }";
4188 S = "{ S1[t,i] -> [t,i] }";
4189 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4190 return -1;
4192 /* Fig. 5 of CC2008 */
4193 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
4194 "j <= -1 + N }";
4195 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
4196 "j >= 2 and j <= -1 + N }";
4197 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
4198 "j >= 2 and j <= -1 + N; "
4199 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
4200 "j >= 2 and j <= -1 + N }";
4201 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4202 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4203 return -1;
4205 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
4206 W = "{ S1[i] -> a[i] }";
4207 R = "{ S2[i] -> a[i+1] }";
4208 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4209 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4210 return -1;
4212 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
4213 W = "{ S1[i] -> a[i] }";
4214 R = "{ S2[i] -> a[9-i] }";
4215 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4216 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4217 return -1;
4219 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
4220 W = "{ S1[i] -> a[i] }";
4221 R = "[N] -> { S2[i] -> a[N-1-i] }";
4222 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4223 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4224 return -1;
4226 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
4227 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
4228 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
4229 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
4230 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4231 return -1;
4233 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4234 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
4235 R = "{ S2[i,j] -> a[i-1,j] }";
4236 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4237 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4238 return -1;
4240 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4241 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
4242 R = "{ S2[i,j] -> a[i,j-1] }";
4243 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4244 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4245 return -1;
4247 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
4248 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
4249 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
4250 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
4251 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
4252 "S_0[] -> [0, 0, 0] }";
4253 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
4254 return -1;
4255 ctx->opt->schedule_parametric = 0;
4256 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4257 return -1;
4258 ctx->opt->schedule_parametric = 1;
4260 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
4261 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
4262 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
4263 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
4264 "S4[i] -> a[i,N] }";
4265 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
4266 "S4[i] -> [4,i,0] }";
4267 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
4268 isl_options_set_schedule_maximize_coincidence(ctx, 0);
4269 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4270 return -1;
4271 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
4273 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
4274 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4275 "j <= N }";
4276 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4277 "j <= N; "
4278 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
4279 "j <= N }";
4280 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4281 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4282 return -1;
4284 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
4285 " S_2[t] : t >= 0 and t <= -1 + N; "
4286 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
4287 "i <= -1 + N }";
4288 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
4289 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
4290 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
4291 "i >= 0 and i <= -1 + N }";
4292 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
4293 "i >= 0 and i <= -1 + N; "
4294 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
4295 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
4296 " S_0[t] -> [0, t, 0] }";
4298 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4299 return -1;
4300 ctx->opt->schedule_parametric = 0;
4301 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4302 return -1;
4303 ctx->opt->schedule_parametric = 1;
4305 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
4306 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
4307 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
4308 return -1;
4310 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
4311 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
4312 W = "[M, N] -> { S_0[i, j] -> a[j] : 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 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
4316 "j >= 0 and j <= -1 + N; "
4317 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4318 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
4319 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4320 return -1;
4322 D = "{ S_0[i] : i >= 0 }";
4323 W = "{ S_0[i] -> a[i] : i >= 0 }";
4324 R = "{ S_0[i] -> a[0] : i >= 0 }";
4325 S = "{ S_0[i] -> [0, i, 0] }";
4326 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4327 return -1;
4329 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
4330 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
4331 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
4332 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
4333 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4334 return -1;
4336 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
4337 "k <= -1 + n and k >= 0 }";
4338 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
4339 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
4340 "k <= -1 + n and k >= 0; "
4341 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
4342 "k <= -1 + n and k >= 0; "
4343 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
4344 "k <= -1 + n and k >= 0 }";
4345 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
4346 ctx->opt->schedule_outer_coincidence = 1;
4347 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4348 return -1;
4349 ctx->opt->schedule_outer_coincidence = 0;
4351 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
4352 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
4353 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
4354 "Stmt_for_body24[i0, i1, 1, 0]:"
4355 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
4356 "Stmt_for_body7[i0, i1, i2]:"
4357 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
4358 "i2 <= 7 }";
4360 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
4361 "Stmt_for_body24[1, i1, i2, i3]:"
4362 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
4363 "i2 >= 1;"
4364 "Stmt_for_body24[0, i1, i2, i3] -> "
4365 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
4366 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
4367 "i3 >= 0;"
4368 "Stmt_for_body24[0, i1, i2, i3] ->"
4369 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4370 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4371 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4372 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4373 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4374 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4375 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4376 "i1 <= 6 and i1 >= 0;"
4377 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4378 "Stmt_for_body7[i0, i1, i2] -> "
4379 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4380 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4381 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4382 "Stmt_for_body7[i0, i1, i2] -> "
4383 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4384 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4385 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4386 P = V;
4388 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4389 isl_options_set_schedule_treat_coalescing(ctx, 0);
4390 if (test_has_schedule(ctx, D, V, P) < 0)
4391 return -1;
4392 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4394 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4395 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4396 "j >= 1 and j <= 7;"
4397 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4398 "j >= 1 and j <= 8 }";
4399 P = "{ }";
4400 S = "{ S_0[i, j] -> [i + j, i] }";
4401 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4402 if (test_special_schedule(ctx, D, V, P, S) < 0)
4403 return -1;
4404 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4406 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4407 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4408 "j >= 0 and j <= -1 + i }";
4409 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4410 "i <= -1 + N and j >= 0;"
4411 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4412 "i <= -2 + N }";
4413 P = "{ }";
4414 S = "{ S_0[i, j] -> [i, j] }";
4415 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4416 if (test_special_schedule(ctx, D, V, P, S) < 0)
4417 return -1;
4418 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4420 /* Test both algorithms on a case with only proximity dependences. */
4421 D = "{ S[i,j] : 0 <= i <= 10 }";
4422 V = "{ }";
4423 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4424 S = "{ S[i, j] -> [j, i] }";
4425 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4426 if (test_special_schedule(ctx, D, V, P, S) < 0)
4427 return -1;
4428 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4429 if (test_special_schedule(ctx, D, V, P, S) < 0)
4430 return -1;
4432 D = "{ A[a]; B[] }";
4433 V = "{}";
4434 P = "{ A[a] -> B[] }";
4435 if (test_has_schedule(ctx, D, V, P) < 0)
4436 return -1;
4438 if (test_padded_schedule(ctx) < 0)
4439 return -1;
4441 /* Check that check for progress is not confused by rational
4442 * solution.
4444 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4445 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4446 "i0 <= -2 + N; "
4447 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4448 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4449 P = "{}";
4450 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4451 if (test_has_schedule(ctx, D, V, P) < 0)
4452 return -1;
4453 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4455 /* Check that we allow schedule rows that are only non-trivial
4456 * on some full-dimensional domains.
4458 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4459 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4460 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4461 P = "{}";
4462 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4463 if (test_has_schedule(ctx, D, V, P) < 0)
4464 return -1;
4465 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4467 if (test_conditional_schedule_constraints(ctx) < 0)
4468 return -1;
4470 if (test_strongly_satisfying_schedule(ctx) < 0)
4471 return -1;
4473 if (test_conflicting_context_schedule(ctx) < 0)
4474 return -1;
4476 if (test_bounded_coefficients_schedule(ctx) < 0)
4477 return -1;
4478 if (test_coalescing_schedule(ctx) < 0)
4479 return -1;
4480 if (test_skewing_schedule(ctx) < 0)
4481 return -1;
4483 return 0;
4486 /* Perform scheduling tests using the whole component scheduler.
4488 static int test_schedule_whole(isl_ctx *ctx)
4490 int whole;
4491 int r;
4493 whole = isl_options_get_schedule_whole_component(ctx);
4494 isl_options_set_schedule_whole_component(ctx, 1);
4495 r = test_schedule(ctx);
4496 if (r >= 0)
4497 r = test_bounded_coefficients_schedule_whole(ctx);
4498 isl_options_set_schedule_whole_component(ctx, whole);
4500 return r;
4503 /* Perform scheduling tests using the incremental scheduler.
4505 static int test_schedule_incremental(isl_ctx *ctx)
4507 int whole;
4508 int r;
4510 whole = isl_options_get_schedule_whole_component(ctx);
4511 isl_options_set_schedule_whole_component(ctx, 0);
4512 r = test_schedule(ctx);
4513 isl_options_set_schedule_whole_component(ctx, whole);
4515 return r;
4518 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4520 isl_union_map *umap;
4521 int test;
4523 umap = isl_union_map_read_from_str(ctx, str);
4524 test = isl_union_map_plain_is_injective(umap);
4525 isl_union_map_free(umap);
4526 if (test < 0)
4527 return -1;
4528 if (test == injective)
4529 return 0;
4530 if (injective)
4531 isl_die(ctx, isl_error_unknown,
4532 "map not detected as injective", return -1);
4533 else
4534 isl_die(ctx, isl_error_unknown,
4535 "map detected as injective", return -1);
4538 int test_injective(isl_ctx *ctx)
4540 const char *str;
4542 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4543 return -1;
4544 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4545 return -1;
4546 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4547 return -1;
4548 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4549 return -1;
4550 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4551 return -1;
4552 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4553 return -1;
4554 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4555 return -1;
4556 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4557 return -1;
4559 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4560 if (test_plain_injective(ctx, str, 1))
4561 return -1;
4562 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4563 if (test_plain_injective(ctx, str, 0))
4564 return -1;
4566 return 0;
4569 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4571 isl_aff *aff2;
4572 int equal;
4574 if (!aff)
4575 return -1;
4577 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4578 equal = isl_aff_plain_is_equal(aff, aff2);
4579 isl_aff_free(aff2);
4581 return equal;
4584 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4586 int equal;
4588 equal = aff_plain_is_equal(aff, str);
4589 if (equal < 0)
4590 return -1;
4591 if (!equal)
4592 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4593 "result not as expected", return -1);
4594 return 0;
4597 struct {
4598 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4599 __isl_take isl_aff *aff2);
4600 } aff_bin_op[] = {
4601 ['+'] = { &isl_aff_add },
4602 ['-'] = { &isl_aff_sub },
4603 ['*'] = { &isl_aff_mul },
4604 ['/'] = { &isl_aff_div },
4607 struct {
4608 const char *arg1;
4609 unsigned char op;
4610 const char *arg2;
4611 const char *res;
4612 } aff_bin_tests[] = {
4613 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
4614 "{ [i] -> [2i] }" },
4615 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
4616 "{ [i] -> [0] }" },
4617 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
4618 "{ [i] -> [2i] }" },
4619 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
4620 "{ [i] -> [2i] }" },
4621 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
4622 "{ [i] -> [i/2] }" },
4623 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
4624 "{ [i] -> [i] }" },
4625 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
4626 "{ [i] -> [NaN] }" },
4627 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
4628 "{ [i] -> [NaN] }" },
4629 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
4630 "{ [i] -> [NaN] }" },
4631 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
4632 "{ [i] -> [NaN] }" },
4633 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
4634 "{ [i] -> [NaN] }" },
4635 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
4636 "{ [i] -> [NaN] }" },
4637 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
4638 "{ [i] -> [NaN] }" },
4639 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
4640 "{ [i] -> [NaN] }" },
4641 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
4642 "{ [i] -> [NaN] }" },
4643 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
4644 "{ [i] -> [NaN] }" },
4645 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
4646 "{ [i] -> [NaN] }" },
4647 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
4648 "{ [i] -> [NaN] }" },
4651 /* Perform some basic tests of binary operations on isl_aff objects.
4653 static int test_bin_aff(isl_ctx *ctx)
4655 int i;
4656 isl_aff *aff1, *aff2, *res;
4657 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4658 __isl_take isl_aff *aff2);
4659 int ok;
4661 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
4662 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
4663 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
4664 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
4665 fn = aff_bin_op[aff_bin_tests[i].op].fn;
4666 aff1 = fn(aff1, aff2);
4667 if (isl_aff_is_nan(res))
4668 ok = isl_aff_is_nan(aff1);
4669 else
4670 ok = isl_aff_plain_is_equal(aff1, res);
4671 isl_aff_free(aff1);
4672 isl_aff_free(res);
4673 if (ok < 0)
4674 return -1;
4675 if (!ok)
4676 isl_die(ctx, isl_error_unknown,
4677 "unexpected result", return -1);
4680 return 0;
4683 struct {
4684 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
4685 __isl_take isl_pw_aff *pa2);
4686 } pw_aff_bin_op[] = {
4687 ['m'] = { &isl_pw_aff_min },
4688 ['M'] = { &isl_pw_aff_max },
4691 /* Inputs for binary isl_pw_aff operation tests.
4692 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
4693 * defined by pw_aff_bin_op, and "res" is the expected result.
4695 struct {
4696 const char *arg1;
4697 unsigned char op;
4698 const char *arg2;
4699 const char *res;
4700 } pw_aff_bin_tests[] = {
4701 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
4702 "{ [i] -> [i] }" },
4703 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
4704 "{ [i] -> [i] }" },
4705 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
4706 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
4707 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
4708 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
4709 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
4710 "{ [i] -> [NaN] }" },
4711 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
4712 "{ [i] -> [NaN] }" },
4715 /* Perform some basic tests of binary operations on isl_pw_aff objects.
4717 static int test_bin_pw_aff(isl_ctx *ctx)
4719 int i;
4720 isl_bool ok;
4721 isl_pw_aff *pa1, *pa2, *res;
4723 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
4724 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
4725 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
4726 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
4727 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
4728 if (isl_pw_aff_involves_nan(res))
4729 ok = isl_pw_aff_involves_nan(pa1);
4730 else
4731 ok = isl_pw_aff_plain_is_equal(pa1, res);
4732 isl_pw_aff_free(pa1);
4733 isl_pw_aff_free(res);
4734 if (ok < 0)
4735 return -1;
4736 if (!ok)
4737 isl_die(ctx, isl_error_unknown,
4738 "unexpected result", return -1);
4741 return 0;
4744 struct {
4745 __isl_give isl_union_pw_multi_aff *(*fn)(
4746 __isl_take isl_union_pw_multi_aff *upma1,
4747 __isl_take isl_union_pw_multi_aff *upma2);
4748 const char *arg1;
4749 const char *arg2;
4750 const char *res;
4751 } upma_bin_tests[] = {
4752 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
4753 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
4754 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
4755 "{ B[x] -> [2] : x >= 0 }",
4756 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
4757 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
4758 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
4759 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
4760 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
4761 "D[i] -> B[2] : i >= 5 }" },
4762 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4763 "{ B[x] -> C[2] : x > 0 }",
4764 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
4765 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4766 "{ B[x] -> A[2] : x >= 0 }",
4767 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
4770 /* Perform some basic tests of binary operations on
4771 * isl_union_pw_multi_aff objects.
4773 static int test_bin_upma(isl_ctx *ctx)
4775 int i;
4776 isl_union_pw_multi_aff *upma1, *upma2, *res;
4777 int ok;
4779 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
4780 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4781 upma_bin_tests[i].arg1);
4782 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4783 upma_bin_tests[i].arg2);
4784 res = isl_union_pw_multi_aff_read_from_str(ctx,
4785 upma_bin_tests[i].res);
4786 upma1 = upma_bin_tests[i].fn(upma1, upma2);
4787 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
4788 isl_union_pw_multi_aff_free(upma1);
4789 isl_union_pw_multi_aff_free(res);
4790 if (ok < 0)
4791 return -1;
4792 if (!ok)
4793 isl_die(ctx, isl_error_unknown,
4794 "unexpected result", return -1);
4797 return 0;
4800 struct {
4801 __isl_give isl_union_pw_multi_aff *(*fn)(
4802 __isl_take isl_union_pw_multi_aff *upma1,
4803 __isl_take isl_union_pw_multi_aff *upma2);
4804 const char *arg1;
4805 const char *arg2;
4806 } upma_bin_fail_tests[] = {
4807 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4808 "{ B[x] -> C[2] : x >= 0 }" },
4811 /* Perform some basic tests of binary operations on
4812 * isl_union_pw_multi_aff objects that are expected to fail.
4814 static int test_bin_upma_fail(isl_ctx *ctx)
4816 int i, n;
4817 isl_union_pw_multi_aff *upma1, *upma2;
4818 int on_error;
4820 on_error = isl_options_get_on_error(ctx);
4821 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
4822 n = ARRAY_SIZE(upma_bin_fail_tests);
4823 for (i = 0; i < n; ++i) {
4824 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4825 upma_bin_fail_tests[i].arg1);
4826 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4827 upma_bin_fail_tests[i].arg2);
4828 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
4829 isl_union_pw_multi_aff_free(upma1);
4830 if (upma1)
4831 break;
4833 isl_options_set_on_error(ctx, on_error);
4834 if (i < n)
4835 isl_die(ctx, isl_error_unknown,
4836 "operation not expected to succeed", return -1);
4838 return 0;
4841 int test_aff(isl_ctx *ctx)
4843 const char *str;
4844 isl_set *set;
4845 isl_space *space;
4846 isl_local_space *ls;
4847 isl_aff *aff;
4848 int zero, equal;
4850 if (test_bin_aff(ctx) < 0)
4851 return -1;
4852 if (test_bin_pw_aff(ctx) < 0)
4853 return -1;
4854 if (test_bin_upma(ctx) < 0)
4855 return -1;
4856 if (test_bin_upma_fail(ctx) < 0)
4857 return -1;
4859 space = isl_space_set_alloc(ctx, 0, 1);
4860 ls = isl_local_space_from_space(space);
4861 aff = isl_aff_zero_on_domain(ls);
4863 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4864 aff = isl_aff_scale_down_ui(aff, 3);
4865 aff = isl_aff_floor(aff);
4866 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4867 aff = isl_aff_scale_down_ui(aff, 2);
4868 aff = isl_aff_floor(aff);
4869 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4871 str = "{ [10] }";
4872 set = isl_set_read_from_str(ctx, str);
4873 aff = isl_aff_gist(aff, set);
4875 aff = isl_aff_add_constant_si(aff, -16);
4876 zero = isl_aff_plain_is_zero(aff);
4877 isl_aff_free(aff);
4879 if (zero < 0)
4880 return -1;
4881 if (!zero)
4882 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4884 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
4885 aff = isl_aff_scale_down_ui(aff, 64);
4886 aff = isl_aff_floor(aff);
4887 equal = aff_check_plain_equal(aff, "{ [-1] }");
4888 isl_aff_free(aff);
4889 if (equal < 0)
4890 return -1;
4892 return 0;
4895 /* Check that "pa" consists of a single expression.
4897 static int check_single_piece(isl_ctx *ctx, __isl_take isl_pw_aff *pa)
4899 int n;
4901 n = isl_pw_aff_n_piece(pa);
4902 isl_pw_aff_free(pa);
4904 if (!pa)
4905 return -1;
4906 if (n != 1)
4907 isl_die(ctx, isl_error_unknown, "expecting single expression",
4908 return -1);
4910 return 0;
4913 /* Check that the computation below results in a single expression.
4914 * One or two expressions may result depending on which constraint
4915 * ends up being considered as redundant with respect to the other
4916 * constraints after the projection that is performed internally
4917 * by isl_set_dim_min.
4919 static int test_dim_max_1(isl_ctx *ctx)
4921 const char *str;
4922 isl_set *set;
4923 isl_pw_aff *pa;
4925 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
4926 "-4a <= b <= 3 and b < n - 4a }";
4927 set = isl_set_read_from_str(ctx, str);
4928 pa = isl_set_dim_min(set, 0);
4929 return check_single_piece(ctx, pa);
4932 /* Check that the computation below results in a single expression.
4933 * The PIP problem corresponding to these constraints has a row
4934 * that causes a split of the solution domain. The solver should
4935 * first pick rows that split off empty parts such that the actual
4936 * solution domain does not get split.
4937 * Note that the description contains some redundant constraints.
4938 * If these constraints get removed first, then the row mentioned
4939 * above does not appear in the PIP problem.
4941 static int test_dim_max_2(isl_ctx *ctx)
4943 const char *str;
4944 isl_set *set;
4945 isl_pw_aff *pa;
4947 str = "[P, N] -> { [a] : a < N and a >= 0 and N > P and a <= P and "
4948 "N > 0 and P >= 0 }";
4949 set = isl_set_read_from_str(ctx, str);
4950 pa = isl_set_dim_max(set, 0);
4951 return check_single_piece(ctx, pa);
4954 int test_dim_max(isl_ctx *ctx)
4956 int equal;
4957 const char *str;
4958 isl_set *set1, *set2;
4959 isl_set *set;
4960 isl_map *map;
4961 isl_pw_aff *pwaff;
4963 if (test_dim_max_1(ctx) < 0)
4964 return -1;
4965 if (test_dim_max_2(ctx) < 0)
4966 return -1;
4968 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
4969 set = isl_set_read_from_str(ctx, str);
4970 pwaff = isl_set_dim_max(set, 0);
4971 set1 = isl_set_from_pw_aff(pwaff);
4972 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
4973 set2 = isl_set_read_from_str(ctx, str);
4974 equal = isl_set_is_equal(set1, set2);
4975 isl_set_free(set1);
4976 isl_set_free(set2);
4977 if (equal < 0)
4978 return -1;
4979 if (!equal)
4980 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4982 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
4983 set = isl_set_read_from_str(ctx, str);
4984 pwaff = isl_set_dim_max(set, 0);
4985 set1 = isl_set_from_pw_aff(pwaff);
4986 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4987 set2 = isl_set_read_from_str(ctx, str);
4988 equal = isl_set_is_equal(set1, set2);
4989 isl_set_free(set1);
4990 isl_set_free(set2);
4991 if (equal < 0)
4992 return -1;
4993 if (!equal)
4994 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4996 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
4997 set = isl_set_read_from_str(ctx, str);
4998 pwaff = isl_set_dim_max(set, 0);
4999 set1 = isl_set_from_pw_aff(pwaff);
5000 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
5001 set2 = isl_set_read_from_str(ctx, str);
5002 equal = isl_set_is_equal(set1, set2);
5003 isl_set_free(set1);
5004 isl_set_free(set2);
5005 if (equal < 0)
5006 return -1;
5007 if (!equal)
5008 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5010 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
5011 "0 <= i < N and 0 <= j < M }";
5012 map = isl_map_read_from_str(ctx, str);
5013 set = isl_map_range(map);
5015 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
5016 set1 = isl_set_from_pw_aff(pwaff);
5017 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
5018 set2 = isl_set_read_from_str(ctx, str);
5019 equal = isl_set_is_equal(set1, set2);
5020 isl_set_free(set1);
5021 isl_set_free(set2);
5023 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
5024 set1 = isl_set_from_pw_aff(pwaff);
5025 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
5026 set2 = isl_set_read_from_str(ctx, str);
5027 if (equal >= 0 && equal)
5028 equal = isl_set_is_equal(set1, set2);
5029 isl_set_free(set1);
5030 isl_set_free(set2);
5032 isl_set_free(set);
5034 if (equal < 0)
5035 return -1;
5036 if (!equal)
5037 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5039 /* Check that solutions are properly merged. */
5040 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
5041 "c <= -1 + n - 4a - 2b and c >= -2b and "
5042 "4a >= -4 + n and c >= 0 }";
5043 set = isl_set_read_from_str(ctx, str);
5044 pwaff = isl_set_dim_min(set, 2);
5045 set1 = isl_set_from_pw_aff(pwaff);
5046 str = "[n] -> { [(0)] : n >= 1 }";
5047 set2 = isl_set_read_from_str(ctx, str);
5048 equal = isl_set_is_equal(set1, set2);
5049 isl_set_free(set1);
5050 isl_set_free(set2);
5052 if (equal < 0)
5053 return -1;
5054 if (!equal)
5055 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5057 /* Check that empty solution lie in the right space. */
5058 str = "[n] -> { [t,a] : 1 = 0 }";
5059 set = isl_set_read_from_str(ctx, str);
5060 pwaff = isl_set_dim_max(set, 0);
5061 set1 = isl_set_from_pw_aff(pwaff);
5062 str = "[n] -> { [t] : 1 = 0 }";
5063 set2 = isl_set_read_from_str(ctx, str);
5064 equal = isl_set_is_equal(set1, set2);
5065 isl_set_free(set1);
5066 isl_set_free(set2);
5068 if (equal < 0)
5069 return -1;
5070 if (!equal)
5071 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5073 return 0;
5076 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
5078 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
5079 const char *str)
5081 isl_ctx *ctx;
5082 isl_pw_multi_aff *pma2;
5083 int equal;
5085 if (!pma)
5086 return -1;
5088 ctx = isl_pw_multi_aff_get_ctx(pma);
5089 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5090 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
5091 isl_pw_multi_aff_free(pma2);
5093 return equal;
5096 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
5097 * represented by "str".
5099 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
5100 const char *str)
5102 int equal;
5104 equal = pw_multi_aff_plain_is_equal(pma, str);
5105 if (equal < 0)
5106 return -1;
5107 if (!equal)
5108 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
5109 "result not as expected", return -1);
5110 return 0;
5113 /* Basic test for isl_pw_multi_aff_product.
5115 * Check that multiple pieces are properly handled.
5117 static int test_product_pma(isl_ctx *ctx)
5119 int equal;
5120 const char *str;
5121 isl_pw_multi_aff *pma1, *pma2;
5123 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
5124 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
5125 str = "{ C[] -> D[] }";
5126 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5127 pma1 = isl_pw_multi_aff_product(pma1, pma2);
5128 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
5129 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
5130 equal = pw_multi_aff_check_plain_equal(pma1, str);
5131 isl_pw_multi_aff_free(pma1);
5132 if (equal < 0)
5133 return -1;
5135 return 0;
5138 int test_product(isl_ctx *ctx)
5140 const char *str;
5141 isl_set *set;
5142 isl_union_set *uset1, *uset2;
5143 int ok;
5145 str = "{ A[i] }";
5146 set = isl_set_read_from_str(ctx, str);
5147 set = isl_set_product(set, isl_set_copy(set));
5148 ok = isl_set_is_wrapping(set);
5149 isl_set_free(set);
5150 if (ok < 0)
5151 return -1;
5152 if (!ok)
5153 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5155 str = "{ [] }";
5156 uset1 = isl_union_set_read_from_str(ctx, str);
5157 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
5158 str = "{ [[] -> []] }";
5159 uset2 = isl_union_set_read_from_str(ctx, str);
5160 ok = isl_union_set_is_equal(uset1, uset2);
5161 isl_union_set_free(uset1);
5162 isl_union_set_free(uset2);
5163 if (ok < 0)
5164 return -1;
5165 if (!ok)
5166 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5168 if (test_product_pma(ctx) < 0)
5169 return -1;
5171 return 0;
5174 /* Check that two sets are not considered disjoint just because
5175 * they have a different set of (named) parameters.
5177 static int test_disjoint(isl_ctx *ctx)
5179 const char *str;
5180 isl_set *set, *set2;
5181 int disjoint;
5183 str = "[n] -> { [[]->[]] }";
5184 set = isl_set_read_from_str(ctx, str);
5185 str = "{ [[]->[]] }";
5186 set2 = isl_set_read_from_str(ctx, str);
5187 disjoint = isl_set_is_disjoint(set, set2);
5188 isl_set_free(set);
5189 isl_set_free(set2);
5190 if (disjoint < 0)
5191 return -1;
5192 if (disjoint)
5193 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5195 return 0;
5198 /* Inputs for isl_pw_multi_aff_is_equal tests.
5199 * "f1" and "f2" are the two function that need to be compared.
5200 * "equal" is the expected result.
5202 struct {
5203 int equal;
5204 const char *f1;
5205 const char *f2;
5206 } pma_equal_tests[] = {
5207 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
5208 "[N] -> { [0] : 0 <= N <= 1 }" },
5209 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
5210 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
5211 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
5212 "[N] -> { [0] : 0 <= N <= 1 }" },
5213 { 0, "{ [NaN] }", "{ [NaN] }" },
5216 int test_equal(isl_ctx *ctx)
5218 int i;
5219 const char *str;
5220 isl_set *set, *set2;
5221 int equal;
5223 str = "{ S_6[i] }";
5224 set = isl_set_read_from_str(ctx, str);
5225 str = "{ S_7[i] }";
5226 set2 = isl_set_read_from_str(ctx, str);
5227 equal = isl_set_is_equal(set, set2);
5228 isl_set_free(set);
5229 isl_set_free(set2);
5230 if (equal < 0)
5231 return -1;
5232 if (equal)
5233 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5235 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
5236 int expected = pma_equal_tests[i].equal;
5237 isl_pw_multi_aff *f1, *f2;
5239 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
5240 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
5241 equal = isl_pw_multi_aff_is_equal(f1, f2);
5242 isl_pw_multi_aff_free(f1);
5243 isl_pw_multi_aff_free(f2);
5244 if (equal < 0)
5245 return -1;
5246 if (equal != expected)
5247 isl_die(ctx, isl_error_unknown,
5248 "unexpected equality result", return -1);
5251 return 0;
5254 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
5255 enum isl_dim_type type, unsigned pos, int fixed)
5257 isl_bool test;
5259 test = isl_map_plain_is_fixed(map, type, pos, NULL);
5260 isl_map_free(map);
5261 if (test < 0)
5262 return -1;
5263 if (test == fixed)
5264 return 0;
5265 if (fixed)
5266 isl_die(ctx, isl_error_unknown,
5267 "map not detected as fixed", return -1);
5268 else
5269 isl_die(ctx, isl_error_unknown,
5270 "map detected as fixed", return -1);
5273 int test_fixed(isl_ctx *ctx)
5275 const char *str;
5276 isl_map *map;
5278 str = "{ [i] -> [i] }";
5279 map = isl_map_read_from_str(ctx, str);
5280 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
5281 return -1;
5282 str = "{ [i] -> [1] }";
5283 map = isl_map_read_from_str(ctx, str);
5284 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5285 return -1;
5286 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
5287 map = isl_map_read_from_str(ctx, str);
5288 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5289 return -1;
5290 map = isl_map_read_from_str(ctx, str);
5291 map = isl_map_neg(map);
5292 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5293 return -1;
5295 return 0;
5298 struct isl_vertices_test_data {
5299 const char *set;
5300 int n;
5301 const char *vertex[6];
5302 } vertices_tests[] = {
5303 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
5304 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
5305 { "{ A[t, i] : t = 14 and i = 1 }",
5306 1, { "{ A[14, 1] }" } },
5307 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
5308 "c <= m and m <= n and m > 0 }",
5309 6, {
5310 "[n, m] -> { [n, m, m] : 0 < m <= n }",
5311 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
5312 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
5313 "[n, m] -> { [m, m, m] : 0 < m <= n }",
5314 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
5315 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
5316 } },
5319 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
5321 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
5323 struct isl_vertices_test_data *data = user;
5324 isl_ctx *ctx;
5325 isl_multi_aff *ma;
5326 isl_basic_set *bset;
5327 isl_pw_multi_aff *pma;
5328 int i;
5329 isl_bool equal;
5331 ctx = isl_vertex_get_ctx(vertex);
5332 bset = isl_vertex_get_domain(vertex);
5333 ma = isl_vertex_get_expr(vertex);
5334 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
5336 for (i = 0; i < data->n; ++i) {
5337 isl_pw_multi_aff *pma_i;
5339 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
5340 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
5341 isl_pw_multi_aff_free(pma_i);
5343 if (equal < 0 || equal)
5344 break;
5347 isl_pw_multi_aff_free(pma);
5348 isl_vertex_free(vertex);
5350 if (equal < 0)
5351 return isl_stat_error;
5353 return equal ? isl_stat_ok : isl_stat_error;
5356 int test_vertices(isl_ctx *ctx)
5358 int i;
5360 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
5361 isl_basic_set *bset;
5362 isl_vertices *vertices;
5363 int ok = 1;
5364 int n;
5366 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
5367 vertices = isl_basic_set_compute_vertices(bset);
5368 n = isl_vertices_get_n_vertices(vertices);
5369 if (vertices_tests[i].n != n)
5370 ok = 0;
5371 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
5372 &vertices_tests[i]) < 0)
5373 ok = 0;
5374 isl_vertices_free(vertices);
5375 isl_basic_set_free(bset);
5377 if (!vertices)
5378 return -1;
5379 if (!ok)
5380 isl_die(ctx, isl_error_unknown, "unexpected vertices",
5381 return -1);
5384 return 0;
5387 int test_union_pw(isl_ctx *ctx)
5389 int equal;
5390 const char *str;
5391 isl_union_set *uset;
5392 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
5394 str = "{ [x] -> x^2 }";
5395 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
5396 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
5397 uset = isl_union_pw_qpolynomial_domain(upwqp1);
5398 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
5399 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
5400 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
5401 isl_union_pw_qpolynomial_free(upwqp1);
5402 isl_union_pw_qpolynomial_free(upwqp2);
5403 if (equal < 0)
5404 return -1;
5405 if (!equal)
5406 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5408 return 0;
5411 /* Test that isl_union_pw_qpolynomial_eval picks up the function
5412 * defined over the correct domain space.
5414 static int test_eval_1(isl_ctx *ctx)
5416 const char *str;
5417 isl_point *pnt;
5418 isl_set *set;
5419 isl_union_pw_qpolynomial *upwqp;
5420 isl_val *v;
5421 int cmp;
5423 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
5424 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
5425 str = "{ A[6] }";
5426 set = isl_set_read_from_str(ctx, str);
5427 pnt = isl_set_sample_point(set);
5428 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
5429 cmp = isl_val_cmp_si(v, 36);
5430 isl_val_free(v);
5432 if (!v)
5433 return -1;
5434 if (cmp != 0)
5435 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
5437 return 0;
5440 /* Check that isl_qpolynomial_eval handles getting called on a void point.
5442 static int test_eval_2(isl_ctx *ctx)
5444 const char *str;
5445 isl_point *pnt;
5446 isl_set *set;
5447 isl_qpolynomial *qp;
5448 isl_val *v;
5449 isl_bool ok;
5451 str = "{ A[x] -> [x] }";
5452 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
5453 str = "{ A[x] : false }";
5454 set = isl_set_read_from_str(ctx, str);
5455 pnt = isl_set_sample_point(set);
5456 v = isl_qpolynomial_eval(qp, pnt);
5457 ok = isl_val_is_nan(v);
5458 isl_val_free(v);
5460 if (ok < 0)
5461 return -1;
5462 if (!ok)
5463 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
5465 return 0;
5468 /* Perform basic polynomial evaluation tests.
5470 static int test_eval(isl_ctx *ctx)
5472 if (test_eval_1(ctx) < 0)
5473 return -1;
5474 if (test_eval_2(ctx) < 0)
5475 return -1;
5476 return 0;
5479 /* Descriptions of sets that are tested for reparsing after printing.
5481 const char *output_tests[] = {
5482 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
5483 "{ [x] : 1 = 0 }",
5484 "{ [x] : false }",
5485 "{ [x] : x mod 2 = 0 }",
5486 "{ [x] : x mod 2 = 1 }",
5487 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) < x }",
5488 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) < x }",
5489 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
5490 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
5491 "[n] -> { [y, x] : 2*((x + 2y) mod 3) = n }",
5492 "{ [x, y] : (2*floor(x/3) + 3*floor(y/4)) mod 5 = x }",
5495 /* Check that printing a set and reparsing a set from the printed output
5496 * results in the same set.
5498 static int test_output_set(isl_ctx *ctx)
5500 int i;
5501 char *str;
5502 isl_set *set1, *set2;
5503 isl_bool equal;
5505 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
5506 set1 = isl_set_read_from_str(ctx, output_tests[i]);
5507 str = isl_set_to_str(set1);
5508 set2 = isl_set_read_from_str(ctx, str);
5509 free(str);
5510 equal = isl_set_is_equal(set1, set2);
5511 isl_set_free(set1);
5512 isl_set_free(set2);
5513 if (equal < 0)
5514 return -1;
5515 if (!equal)
5516 isl_die(ctx, isl_error_unknown,
5517 "parsed output not the same", return -1);
5520 return 0;
5523 int test_output(isl_ctx *ctx)
5525 char *s;
5526 const char *str;
5527 isl_pw_aff *pa;
5528 isl_printer *p;
5529 int equal;
5531 if (test_output_set(ctx) < 0)
5532 return -1;
5534 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
5535 pa = isl_pw_aff_read_from_str(ctx, str);
5537 p = isl_printer_to_str(ctx);
5538 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
5539 p = isl_printer_print_pw_aff(p, pa);
5540 s = isl_printer_get_str(p);
5541 isl_printer_free(p);
5542 isl_pw_aff_free(pa);
5543 if (!s)
5544 equal = -1;
5545 else
5546 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
5547 free(s);
5548 if (equal < 0)
5549 return -1;
5550 if (!equal)
5551 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5553 return 0;
5556 int test_sample(isl_ctx *ctx)
5558 const char *str;
5559 isl_basic_set *bset1, *bset2;
5560 int empty, subset;
5562 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
5563 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
5564 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
5565 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
5566 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
5567 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
5568 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
5569 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
5570 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
5571 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
5572 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
5573 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
5574 "d - 1073741821e and "
5575 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
5576 "3j >= 1 - a + b + 2e and "
5577 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
5578 "3i <= 4 - a + 4b - e and "
5579 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
5580 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
5581 "c <= -1 + a and 3i >= -2 - a + 3e and "
5582 "1073741822e <= 1073741823 - a + 1073741822b + c and "
5583 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
5584 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
5585 "1073741823e >= 1 + 1073741823b - d and "
5586 "3i >= 1073741823b + c - 1073741823e - f and "
5587 "3i >= 1 + 2b + e + 3g }";
5588 bset1 = isl_basic_set_read_from_str(ctx, str);
5589 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
5590 empty = isl_basic_set_is_empty(bset2);
5591 subset = isl_basic_set_is_subset(bset2, bset1);
5592 isl_basic_set_free(bset1);
5593 isl_basic_set_free(bset2);
5594 if (empty < 0 || subset < 0)
5595 return -1;
5596 if (empty)
5597 isl_die(ctx, isl_error_unknown, "point not found", return -1);
5598 if (!subset)
5599 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
5601 return 0;
5604 int test_fixed_power(isl_ctx *ctx)
5606 const char *str;
5607 isl_map *map;
5608 isl_int exp;
5609 int equal;
5611 isl_int_init(exp);
5612 str = "{ [i] -> [i + 1] }";
5613 map = isl_map_read_from_str(ctx, str);
5614 isl_int_set_si(exp, 23);
5615 map = isl_map_fixed_power(map, exp);
5616 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
5617 isl_int_clear(exp);
5618 isl_map_free(map);
5619 if (equal < 0)
5620 return -1;
5622 return 0;
5625 int test_slice(isl_ctx *ctx)
5627 const char *str;
5628 isl_map *map;
5629 int equal;
5631 str = "{ [i] -> [j] }";
5632 map = isl_map_read_from_str(ctx, str);
5633 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
5634 equal = map_check_equal(map, "{ [i] -> [i] }");
5635 isl_map_free(map);
5636 if (equal < 0)
5637 return -1;
5639 str = "{ [i] -> [j] }";
5640 map = isl_map_read_from_str(ctx, str);
5641 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
5642 equal = map_check_equal(map, "{ [i] -> [j] }");
5643 isl_map_free(map);
5644 if (equal < 0)
5645 return -1;
5647 str = "{ [i] -> [j] }";
5648 map = isl_map_read_from_str(ctx, str);
5649 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
5650 equal = map_check_equal(map, "{ [i] -> [-i] }");
5651 isl_map_free(map);
5652 if (equal < 0)
5653 return -1;
5655 str = "{ [i] -> [j] }";
5656 map = isl_map_read_from_str(ctx, str);
5657 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
5658 equal = map_check_equal(map, "{ [0] -> [j] }");
5659 isl_map_free(map);
5660 if (equal < 0)
5661 return -1;
5663 str = "{ [i] -> [j] }";
5664 map = isl_map_read_from_str(ctx, str);
5665 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
5666 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
5667 isl_map_free(map);
5668 if (equal < 0)
5669 return -1;
5671 str = "{ [i] -> [j] }";
5672 map = isl_map_read_from_str(ctx, str);
5673 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
5674 equal = map_check_equal(map, "{ [i] -> [j] : false }");
5675 isl_map_free(map);
5676 if (equal < 0)
5677 return -1;
5679 return 0;
5682 int test_eliminate(isl_ctx *ctx)
5684 const char *str;
5685 isl_map *map;
5686 int equal;
5688 str = "{ [i] -> [j] : i = 2j }";
5689 map = isl_map_read_from_str(ctx, str);
5690 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
5691 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
5692 isl_map_free(map);
5693 if (equal < 0)
5694 return -1;
5696 return 0;
5699 /* Check that isl_set_dim_residue_class detects that the values of j
5700 * in the set below are all odd and that it does not detect any spurious
5701 * strides.
5703 static int test_residue_class(isl_ctx *ctx)
5705 const char *str;
5706 isl_set *set;
5707 isl_int m, r;
5708 isl_stat res;
5710 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
5711 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
5712 set = isl_set_read_from_str(ctx, str);
5713 isl_int_init(m);
5714 isl_int_init(r);
5715 res = isl_set_dim_residue_class(set, 1, &m, &r);
5716 if (res >= 0 &&
5717 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
5718 isl_die(ctx, isl_error_unknown, "incorrect residue class",
5719 res = isl_stat_error);
5720 isl_int_clear(r);
5721 isl_int_clear(m);
5722 isl_set_free(set);
5724 return res;
5727 int test_align_parameters(isl_ctx *ctx)
5729 const char *str;
5730 isl_space *space;
5731 isl_multi_aff *ma1, *ma2;
5732 int equal;
5734 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
5735 ma1 = isl_multi_aff_read_from_str(ctx, str);
5737 space = isl_space_params_alloc(ctx, 1);
5738 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
5739 ma1 = isl_multi_aff_align_params(ma1, space);
5741 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
5742 ma2 = isl_multi_aff_read_from_str(ctx, str);
5744 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5746 isl_multi_aff_free(ma1);
5747 isl_multi_aff_free(ma2);
5749 if (equal < 0)
5750 return -1;
5751 if (!equal)
5752 isl_die(ctx, isl_error_unknown,
5753 "result not as expected", return -1);
5755 return 0;
5758 static int test_list(isl_ctx *ctx)
5760 isl_id *a, *b, *c, *d, *id;
5761 isl_id_list *list;
5762 int ok;
5764 a = isl_id_alloc(ctx, "a", NULL);
5765 b = isl_id_alloc(ctx, "b", NULL);
5766 c = isl_id_alloc(ctx, "c", NULL);
5767 d = isl_id_alloc(ctx, "d", NULL);
5769 list = isl_id_list_alloc(ctx, 4);
5770 list = isl_id_list_add(list, b);
5771 list = isl_id_list_insert(list, 0, a);
5772 list = isl_id_list_add(list, c);
5773 list = isl_id_list_add(list, d);
5774 list = isl_id_list_drop(list, 1, 1);
5776 if (!list)
5777 return -1;
5778 if (isl_id_list_n_id(list) != 3) {
5779 isl_id_list_free(list);
5780 isl_die(ctx, isl_error_unknown,
5781 "unexpected number of elements in list", return -1);
5784 id = isl_id_list_get_id(list, 0);
5785 ok = id == a;
5786 isl_id_free(id);
5787 id = isl_id_list_get_id(list, 1);
5788 ok = ok && id == c;
5789 isl_id_free(id);
5790 id = isl_id_list_get_id(list, 2);
5791 ok = ok && id == d;
5792 isl_id_free(id);
5794 isl_id_list_free(list);
5796 if (!ok)
5797 isl_die(ctx, isl_error_unknown,
5798 "unexpected elements in list", return -1);
5800 return 0;
5803 const char *set_conversion_tests[] = {
5804 "[N] -> { [i] : N - 1 <= 2 i <= N }",
5805 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
5806 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5807 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5808 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
5809 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
5810 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
5811 "-3 + c <= d <= 28 + c) }",
5814 /* Check that converting from isl_set to isl_pw_multi_aff and back
5815 * to isl_set produces the original isl_set.
5817 static int test_set_conversion(isl_ctx *ctx)
5819 int i;
5820 const char *str;
5821 isl_set *set1, *set2;
5822 isl_pw_multi_aff *pma;
5823 int equal;
5825 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
5826 str = set_conversion_tests[i];
5827 set1 = isl_set_read_from_str(ctx, str);
5828 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
5829 set2 = isl_set_from_pw_multi_aff(pma);
5830 equal = isl_set_is_equal(set1, set2);
5831 isl_set_free(set1);
5832 isl_set_free(set2);
5834 if (equal < 0)
5835 return -1;
5836 if (!equal)
5837 isl_die(ctx, isl_error_unknown, "bad conversion",
5838 return -1);
5841 return 0;
5844 const char *conversion_tests[] = {
5845 "{ [a, b, c, d] -> s0[a, b, e, f] : "
5846 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
5847 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
5848 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
5849 "9e <= -2 - 2a) }",
5850 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
5851 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
5852 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
5855 /* Check that converting from isl_map to isl_pw_multi_aff and back
5856 * to isl_map produces the original isl_map.
5858 static int test_map_conversion(isl_ctx *ctx)
5860 int i;
5861 isl_map *map1, *map2;
5862 isl_pw_multi_aff *pma;
5863 int equal;
5865 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
5866 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
5867 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
5868 map2 = isl_map_from_pw_multi_aff(pma);
5869 equal = isl_map_is_equal(map1, map2);
5870 isl_map_free(map1);
5871 isl_map_free(map2);
5873 if (equal < 0)
5874 return -1;
5875 if (!equal)
5876 isl_die(ctx, isl_error_unknown, "bad conversion",
5877 return -1);
5880 return 0;
5883 static int test_conversion(isl_ctx *ctx)
5885 if (test_set_conversion(ctx) < 0)
5886 return -1;
5887 if (test_map_conversion(ctx) < 0)
5888 return -1;
5889 return 0;
5892 /* Check that isl_basic_map_curry does not modify input.
5894 static int test_curry(isl_ctx *ctx)
5896 const char *str;
5897 isl_basic_map *bmap1, *bmap2;
5898 int equal;
5900 str = "{ [A[] -> B[]] -> C[] }";
5901 bmap1 = isl_basic_map_read_from_str(ctx, str);
5902 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
5903 equal = isl_basic_map_is_equal(bmap1, bmap2);
5904 isl_basic_map_free(bmap1);
5905 isl_basic_map_free(bmap2);
5907 if (equal < 0)
5908 return -1;
5909 if (equal)
5910 isl_die(ctx, isl_error_unknown,
5911 "curried map should not be equal to original",
5912 return -1);
5914 return 0;
5917 struct {
5918 const char *set;
5919 const char *ma;
5920 const char *res;
5921 } preimage_tests[] = {
5922 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
5923 "{ A[j,i] -> B[i,j] }",
5924 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
5925 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5926 "{ A[a,b] -> B[a/2,b/6] }",
5927 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
5928 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5929 "{ A[a,b] -> B[a/2,b/6] }",
5930 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
5931 "exists i,j : a = 2 i and b = 6 j }" },
5932 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
5933 "[n] -> { : 0 <= n <= 100 }" },
5934 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5935 "{ A[a] -> B[2a] }",
5936 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
5937 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5938 "{ A[a] -> B[([a/2])] }",
5939 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
5940 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
5941 "{ A[a] -> B[a,a,a/3] }",
5942 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
5943 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
5944 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
5947 static int test_preimage_basic_set(isl_ctx *ctx)
5949 int i;
5950 isl_basic_set *bset1, *bset2;
5951 isl_multi_aff *ma;
5952 int equal;
5954 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
5955 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
5956 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
5957 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
5958 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
5959 equal = isl_basic_set_is_equal(bset1, bset2);
5960 isl_basic_set_free(bset1);
5961 isl_basic_set_free(bset2);
5962 if (equal < 0)
5963 return -1;
5964 if (!equal)
5965 isl_die(ctx, isl_error_unknown, "bad preimage",
5966 return -1);
5969 return 0;
5972 struct {
5973 const char *map;
5974 const char *ma;
5975 const char *res;
5976 } preimage_domain_tests[] = {
5977 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
5978 "{ A[j,i] -> B[i,j] }",
5979 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
5980 { "{ B[i] -> C[i]; D[i] -> E[i] }",
5981 "{ A[i] -> B[i + 1] }",
5982 "{ A[i] -> C[i + 1] }" },
5983 { "{ B[i] -> C[i]; B[i] -> E[i] }",
5984 "{ A[i] -> B[i + 1] }",
5985 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
5986 { "{ B[i] -> C[([i/2])] }",
5987 "{ A[i] -> B[2i] }",
5988 "{ A[i] -> C[i] }" },
5989 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
5990 "{ A[i] -> B[([i/5]), ([i/7])] }",
5991 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
5992 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
5993 "[N] -> { A[] -> B[([N/5])] }",
5994 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
5995 { "{ B[i] -> C[i] : exists a : i = 5 a }",
5996 "{ A[i] -> B[2i] }",
5997 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
5998 { "{ B[i] -> C[i] : exists a : i = 2 a; "
5999 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
6000 "{ A[i] -> B[2i] }",
6001 "{ A[i] -> C[2i] }" },
6002 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
6003 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
6006 static int test_preimage_union_map(isl_ctx *ctx)
6008 int i;
6009 isl_union_map *umap1, *umap2;
6010 isl_multi_aff *ma;
6011 int equal;
6013 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
6014 umap1 = isl_union_map_read_from_str(ctx,
6015 preimage_domain_tests[i].map);
6016 ma = isl_multi_aff_read_from_str(ctx,
6017 preimage_domain_tests[i].ma);
6018 umap2 = isl_union_map_read_from_str(ctx,
6019 preimage_domain_tests[i].res);
6020 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
6021 equal = isl_union_map_is_equal(umap1, umap2);
6022 isl_union_map_free(umap1);
6023 isl_union_map_free(umap2);
6024 if (equal < 0)
6025 return -1;
6026 if (!equal)
6027 isl_die(ctx, isl_error_unknown, "bad preimage",
6028 return -1);
6031 return 0;
6034 static int test_preimage(isl_ctx *ctx)
6036 if (test_preimage_basic_set(ctx) < 0)
6037 return -1;
6038 if (test_preimage_union_map(ctx) < 0)
6039 return -1;
6041 return 0;
6044 struct {
6045 const char *ma1;
6046 const char *ma;
6047 const char *res;
6048 } pullback_tests[] = {
6049 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
6050 "{ A[a,b] -> C[b + 2a] }" },
6051 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
6052 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
6053 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
6054 "{ A[a] -> C[(a)/6] }" },
6055 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
6056 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
6057 "{ A[a] -> C[(2a)/3] }" },
6058 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
6059 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
6060 "{ A[i,j] -> C[i + j, i + j] }"},
6061 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
6062 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
6063 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
6064 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
6065 "{ [i, j] -> [floor((i)/3), j] }",
6066 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
6069 static int test_pullback(isl_ctx *ctx)
6071 int i;
6072 isl_multi_aff *ma1, *ma2;
6073 isl_multi_aff *ma;
6074 int equal;
6076 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
6077 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
6078 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
6079 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
6080 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
6081 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
6082 isl_multi_aff_free(ma1);
6083 isl_multi_aff_free(ma2);
6084 if (equal < 0)
6085 return -1;
6086 if (!equal)
6087 isl_die(ctx, isl_error_unknown, "bad pullback",
6088 return -1);
6091 return 0;
6094 /* Check that negation is printed correctly and that equal expressions
6095 * are correctly identified.
6097 static int test_ast(isl_ctx *ctx)
6099 isl_ast_expr *expr, *expr1, *expr2, *expr3;
6100 char *str;
6101 int ok, equal;
6103 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
6104 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
6105 expr = isl_ast_expr_add(expr1, expr2);
6106 expr2 = isl_ast_expr_copy(expr);
6107 expr = isl_ast_expr_neg(expr);
6108 expr2 = isl_ast_expr_neg(expr2);
6109 equal = isl_ast_expr_is_equal(expr, expr2);
6110 str = isl_ast_expr_to_C_str(expr);
6111 ok = str ? !strcmp(str, "-(A + B)") : -1;
6112 free(str);
6113 isl_ast_expr_free(expr);
6114 isl_ast_expr_free(expr2);
6116 if (ok < 0 || equal < 0)
6117 return -1;
6118 if (!equal)
6119 isl_die(ctx, isl_error_unknown,
6120 "equal expressions not considered equal", return -1);
6121 if (!ok)
6122 isl_die(ctx, isl_error_unknown,
6123 "isl_ast_expr printed incorrectly", return -1);
6125 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
6126 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
6127 expr = isl_ast_expr_add(expr1, expr2);
6128 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
6129 expr = isl_ast_expr_sub(expr3, expr);
6130 str = isl_ast_expr_to_C_str(expr);
6131 ok = str ? !strcmp(str, "C - (A + B)") : -1;
6132 free(str);
6133 isl_ast_expr_free(expr);
6135 if (ok < 0)
6136 return -1;
6137 if (!ok)
6138 isl_die(ctx, isl_error_unknown,
6139 "isl_ast_expr printed incorrectly", return -1);
6141 return 0;
6144 /* Check that isl_ast_build_expr_from_set returns a valid expression
6145 * for an empty set. Note that isl_ast_build_expr_from_set getting
6146 * called on an empty set probably indicates a bug in the caller.
6148 static int test_ast_build(isl_ctx *ctx)
6150 isl_set *set;
6151 isl_ast_build *build;
6152 isl_ast_expr *expr;
6154 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6155 build = isl_ast_build_from_context(set);
6157 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
6158 expr = isl_ast_build_expr_from_set(build, set);
6160 isl_ast_expr_free(expr);
6161 isl_ast_build_free(build);
6163 if (!expr)
6164 return -1;
6166 return 0;
6169 /* Internal data structure for before_for and after_for callbacks.
6171 * depth is the current depth
6172 * before is the number of times before_for has been called
6173 * after is the number of times after_for has been called
6175 struct isl_test_codegen_data {
6176 int depth;
6177 int before;
6178 int after;
6181 /* This function is called before each for loop in the AST generated
6182 * from test_ast_gen1.
6184 * Increment the number of calls and the depth.
6185 * Check that the space returned by isl_ast_build_get_schedule_space
6186 * matches the target space of the schedule returned by
6187 * isl_ast_build_get_schedule.
6188 * Return an isl_id that is checked by the corresponding call
6189 * to after_for.
6191 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
6192 void *user)
6194 struct isl_test_codegen_data *data = user;
6195 isl_ctx *ctx;
6196 isl_space *space;
6197 isl_union_map *schedule;
6198 isl_union_set *uset;
6199 isl_set *set;
6200 int empty;
6201 char name[] = "d0";
6203 ctx = isl_ast_build_get_ctx(build);
6205 if (data->before >= 3)
6206 isl_die(ctx, isl_error_unknown,
6207 "unexpected number of for nodes", return NULL);
6208 if (data->depth >= 2)
6209 isl_die(ctx, isl_error_unknown,
6210 "unexpected depth", return NULL);
6212 snprintf(name, sizeof(name), "d%d", data->depth);
6213 data->before++;
6214 data->depth++;
6216 schedule = isl_ast_build_get_schedule(build);
6217 uset = isl_union_map_range(schedule);
6218 if (!uset)
6219 return NULL;
6220 if (isl_union_set_n_set(uset) != 1) {
6221 isl_union_set_free(uset);
6222 isl_die(ctx, isl_error_unknown,
6223 "expecting single range space", return NULL);
6226 space = isl_ast_build_get_schedule_space(build);
6227 set = isl_union_set_extract_set(uset, space);
6228 isl_union_set_free(uset);
6229 empty = isl_set_is_empty(set);
6230 isl_set_free(set);
6232 if (empty < 0)
6233 return NULL;
6234 if (empty)
6235 isl_die(ctx, isl_error_unknown,
6236 "spaces don't match", return NULL);
6238 return isl_id_alloc(ctx, name, NULL);
6241 /* This function is called after each for loop in the AST generated
6242 * from test_ast_gen1.
6244 * Increment the number of calls and decrement the depth.
6245 * Check that the annotation attached to the node matches
6246 * the isl_id returned by the corresponding call to before_for.
6248 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
6249 __isl_keep isl_ast_build *build, void *user)
6251 struct isl_test_codegen_data *data = user;
6252 isl_id *id;
6253 const char *name;
6254 int valid;
6256 data->after++;
6257 data->depth--;
6259 if (data->after > data->before)
6260 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
6261 "mismatch in number of for nodes",
6262 return isl_ast_node_free(node));
6264 id = isl_ast_node_get_annotation(node);
6265 if (!id)
6266 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
6267 "missing annotation", return isl_ast_node_free(node));
6269 name = isl_id_get_name(id);
6270 valid = name && atoi(name + 1) == data->depth;
6271 isl_id_free(id);
6273 if (!valid)
6274 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
6275 "wrong annotation", return isl_ast_node_free(node));
6277 return node;
6280 /* Check that the before_each_for and after_each_for callbacks
6281 * are called for each for loop in the generated code,
6282 * that they are called in the right order and that the isl_id
6283 * returned from the before_each_for callback is attached to
6284 * the isl_ast_node passed to the corresponding after_each_for call.
6286 static int test_ast_gen1(isl_ctx *ctx)
6288 const char *str;
6289 isl_set *set;
6290 isl_union_map *schedule;
6291 isl_ast_build *build;
6292 isl_ast_node *tree;
6293 struct isl_test_codegen_data data;
6295 str = "[N] -> { : N >= 10 }";
6296 set = isl_set_read_from_str(ctx, str);
6297 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
6298 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
6299 schedule = isl_union_map_read_from_str(ctx, str);
6301 data.before = 0;
6302 data.after = 0;
6303 data.depth = 0;
6304 build = isl_ast_build_from_context(set);
6305 build = isl_ast_build_set_before_each_for(build,
6306 &before_for, &data);
6307 build = isl_ast_build_set_after_each_for(build,
6308 &after_for, &data);
6309 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6310 isl_ast_build_free(build);
6311 if (!tree)
6312 return -1;
6314 isl_ast_node_free(tree);
6316 if (data.before != 3 || data.after != 3)
6317 isl_die(ctx, isl_error_unknown,
6318 "unexpected number of for nodes", return -1);
6320 return 0;
6323 /* Check that the AST generator handles domains that are integrally disjoint
6324 * but not rationally disjoint.
6326 static int test_ast_gen2(isl_ctx *ctx)
6328 const char *str;
6329 isl_set *set;
6330 isl_union_map *schedule;
6331 isl_union_map *options;
6332 isl_ast_build *build;
6333 isl_ast_node *tree;
6335 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
6336 schedule = isl_union_map_read_from_str(ctx, str);
6337 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6338 build = isl_ast_build_from_context(set);
6340 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
6341 options = isl_union_map_read_from_str(ctx, str);
6342 build = isl_ast_build_set_options(build, options);
6343 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6344 isl_ast_build_free(build);
6345 if (!tree)
6346 return -1;
6347 isl_ast_node_free(tree);
6349 return 0;
6352 /* Increment *user on each call.
6354 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
6355 __isl_keep isl_ast_build *build, void *user)
6357 int *n = user;
6359 (*n)++;
6361 return node;
6364 /* Test that unrolling tries to minimize the number of instances.
6365 * In particular, for the schedule given below, make sure it generates
6366 * 3 nodes (rather than 101).
6368 static int test_ast_gen3(isl_ctx *ctx)
6370 const char *str;
6371 isl_set *set;
6372 isl_union_map *schedule;
6373 isl_union_map *options;
6374 isl_ast_build *build;
6375 isl_ast_node *tree;
6376 int n_domain = 0;
6378 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
6379 schedule = isl_union_map_read_from_str(ctx, str);
6380 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6382 str = "{ [i] -> unroll[0] }";
6383 options = isl_union_map_read_from_str(ctx, str);
6385 build = isl_ast_build_from_context(set);
6386 build = isl_ast_build_set_options(build, options);
6387 build = isl_ast_build_set_at_each_domain(build,
6388 &count_domains, &n_domain);
6389 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6390 isl_ast_build_free(build);
6391 if (!tree)
6392 return -1;
6394 isl_ast_node_free(tree);
6396 if (n_domain != 3)
6397 isl_die(ctx, isl_error_unknown,
6398 "unexpected number of for nodes", return -1);
6400 return 0;
6403 /* Check that if the ast_build_exploit_nested_bounds options is set,
6404 * we do not get an outer if node in the generated AST,
6405 * while we do get such an outer if node if the options is not set.
6407 static int test_ast_gen4(isl_ctx *ctx)
6409 const char *str;
6410 isl_set *set;
6411 isl_union_map *schedule;
6412 isl_ast_build *build;
6413 isl_ast_node *tree;
6414 enum isl_ast_node_type type;
6415 int enb;
6417 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
6418 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
6420 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
6422 schedule = isl_union_map_read_from_str(ctx, str);
6423 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6424 build = isl_ast_build_from_context(set);
6425 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6426 isl_ast_build_free(build);
6427 if (!tree)
6428 return -1;
6430 type = isl_ast_node_get_type(tree);
6431 isl_ast_node_free(tree);
6433 if (type == isl_ast_node_if)
6434 isl_die(ctx, isl_error_unknown,
6435 "not expecting if node", return -1);
6437 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
6439 schedule = isl_union_map_read_from_str(ctx, str);
6440 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6441 build = isl_ast_build_from_context(set);
6442 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6443 isl_ast_build_free(build);
6444 if (!tree)
6445 return -1;
6447 type = isl_ast_node_get_type(tree);
6448 isl_ast_node_free(tree);
6450 if (type != isl_ast_node_if)
6451 isl_die(ctx, isl_error_unknown,
6452 "expecting if node", return -1);
6454 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
6456 return 0;
6459 /* This function is called for each leaf in the AST generated
6460 * from test_ast_gen5.
6462 * We finalize the AST generation by extending the outer schedule
6463 * with a zero-dimensional schedule. If this results in any for loops,
6464 * then this means that we did not pass along enough information
6465 * about the outer schedule to the inner AST generation.
6467 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
6468 void *user)
6470 isl_union_map *schedule, *extra;
6471 isl_ast_node *tree;
6473 schedule = isl_ast_build_get_schedule(build);
6474 extra = isl_union_map_copy(schedule);
6475 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
6476 schedule = isl_union_map_range_product(schedule, extra);
6477 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6478 isl_ast_build_free(build);
6480 if (!tree)
6481 return NULL;
6483 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
6484 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
6485 "code should not contain any for loop",
6486 return isl_ast_node_free(tree));
6488 return tree;
6491 /* Check that we do not lose any information when going back and
6492 * forth between internal and external schedule.
6494 * In particular, we create an AST where we unroll the only
6495 * non-constant dimension in the schedule. We therefore do
6496 * not expect any for loops in the AST. However, older versions
6497 * of isl would not pass along enough information about the outer
6498 * schedule when performing an inner code generation from a create_leaf
6499 * callback, resulting in the inner code generation producing a for loop.
6501 static int test_ast_gen5(isl_ctx *ctx)
6503 const char *str;
6504 isl_set *set;
6505 isl_union_map *schedule, *options;
6506 isl_ast_build *build;
6507 isl_ast_node *tree;
6509 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
6510 schedule = isl_union_map_read_from_str(ctx, str);
6512 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
6513 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
6514 options = isl_union_map_read_from_str(ctx, str);
6516 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6517 build = isl_ast_build_from_context(set);
6518 build = isl_ast_build_set_options(build, options);
6519 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
6520 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6521 isl_ast_build_free(build);
6522 isl_ast_node_free(tree);
6523 if (!tree)
6524 return -1;
6526 return 0;
6529 /* Check that the expression
6531 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
6533 * is not combined into
6535 * min(n/2, 0)
6537 * as this would result in n/2 being evaluated in parts of
6538 * the definition domain where n is not a multiple of 2.
6540 static int test_ast_expr(isl_ctx *ctx)
6542 const char *str;
6543 isl_pw_aff *pa;
6544 isl_ast_build *build;
6545 isl_ast_expr *expr;
6546 int min_max;
6547 int is_min;
6549 min_max = isl_options_get_ast_build_detect_min_max(ctx);
6550 isl_options_set_ast_build_detect_min_max(ctx, 1);
6552 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
6553 pa = isl_pw_aff_read_from_str(ctx, str);
6554 build = isl_ast_build_alloc(ctx);
6555 expr = isl_ast_build_expr_from_pw_aff(build, pa);
6556 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
6557 isl_ast_expr_get_op_type(expr) == isl_ast_op_min;
6558 isl_ast_build_free(build);
6559 isl_ast_expr_free(expr);
6561 isl_options_set_ast_build_detect_min_max(ctx, min_max);
6563 if (!expr)
6564 return -1;
6565 if (is_min)
6566 isl_die(ctx, isl_error_unknown,
6567 "expressions should not be combined", return -1);
6569 return 0;
6572 static int test_ast_gen(isl_ctx *ctx)
6574 if (test_ast_gen1(ctx) < 0)
6575 return -1;
6576 if (test_ast_gen2(ctx) < 0)
6577 return -1;
6578 if (test_ast_gen3(ctx) < 0)
6579 return -1;
6580 if (test_ast_gen4(ctx) < 0)
6581 return -1;
6582 if (test_ast_gen5(ctx) < 0)
6583 return -1;
6584 if (test_ast_expr(ctx) < 0)
6585 return -1;
6586 return 0;
6589 /* Check if dropping output dimensions from an isl_pw_multi_aff
6590 * works properly.
6592 static int test_pw_multi_aff(isl_ctx *ctx)
6594 const char *str;
6595 isl_pw_multi_aff *pma1, *pma2;
6596 int equal;
6598 str = "{ [i,j] -> [i+j, 4i-j] }";
6599 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
6600 str = "{ [i,j] -> [4i-j] }";
6601 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6603 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
6605 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6607 isl_pw_multi_aff_free(pma1);
6608 isl_pw_multi_aff_free(pma2);
6609 if (equal < 0)
6610 return -1;
6611 if (!equal)
6612 isl_die(ctx, isl_error_unknown,
6613 "expressions not equal", return -1);
6615 return 0;
6618 /* Check that we can properly parse multi piecewise affine expressions
6619 * where the piecewise affine expressions have different domains.
6621 static int test_multi_pw_aff(isl_ctx *ctx)
6623 const char *str;
6624 isl_set *dom, *dom2;
6625 isl_multi_pw_aff *mpa1, *mpa2;
6626 isl_pw_aff *pa;
6627 int equal;
6628 int equal_domain;
6630 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
6631 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
6632 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
6633 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
6634 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
6635 str = "{ [i] -> [(i : i > 0), 2i] }";
6636 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
6638 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
6640 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
6641 dom = isl_pw_aff_domain(pa);
6642 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
6643 dom2 = isl_pw_aff_domain(pa);
6644 equal_domain = isl_set_is_equal(dom, dom2);
6646 isl_set_free(dom);
6647 isl_set_free(dom2);
6648 isl_multi_pw_aff_free(mpa1);
6649 isl_multi_pw_aff_free(mpa2);
6651 if (equal < 0)
6652 return -1;
6653 if (!equal)
6654 isl_die(ctx, isl_error_unknown,
6655 "expressions not equal", return -1);
6657 if (equal_domain < 0)
6658 return -1;
6659 if (equal_domain)
6660 isl_die(ctx, isl_error_unknown,
6661 "domains unexpectedly equal", return -1);
6663 return 0;
6666 /* This is a regression test for a bug where isl_basic_map_simplify
6667 * would end up in an infinite loop. In particular, we construct
6668 * an empty basic set that is not obviously empty.
6669 * isl_basic_set_is_empty marks the basic set as empty.
6670 * After projecting out i3, the variable can be dropped completely,
6671 * but isl_basic_map_simplify refrains from doing so if the basic set
6672 * is empty and would end up in an infinite loop if it didn't test
6673 * explicitly for empty basic maps in the outer loop.
6675 static int test_simplify_1(isl_ctx *ctx)
6677 const char *str;
6678 isl_basic_set *bset;
6679 int empty;
6681 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
6682 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
6683 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
6684 "i3 >= i2 }";
6685 bset = isl_basic_set_read_from_str(ctx, str);
6686 empty = isl_basic_set_is_empty(bset);
6687 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
6688 isl_basic_set_free(bset);
6689 if (!bset)
6690 return -1;
6691 if (!empty)
6692 isl_die(ctx, isl_error_unknown,
6693 "basic set should be empty", return -1);
6695 return 0;
6698 /* Check that the equality in the set description below
6699 * is simplified away.
6701 static int test_simplify_2(isl_ctx *ctx)
6703 const char *str;
6704 isl_basic_set *bset;
6705 isl_bool universe;
6707 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
6708 bset = isl_basic_set_read_from_str(ctx, str);
6709 universe = isl_basic_set_plain_is_universe(bset);
6710 isl_basic_set_free(bset);
6712 if (universe < 0)
6713 return -1;
6714 if (!universe)
6715 isl_die(ctx, isl_error_unknown,
6716 "equality not simplified away", return -1);
6717 return 0;
6720 /* Some simplification tests.
6722 static int test_simplify(isl_ctx *ctx)
6724 if (test_simplify_1(ctx) < 0)
6725 return -1;
6726 if (test_simplify_2(ctx) < 0)
6727 return -1;
6728 return 0;
6731 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
6732 * with gbr context would fail to disable the use of the shifted tableau
6733 * when transferring equalities for the input to the context, resulting
6734 * in invalid sample values.
6736 static int test_partial_lexmin(isl_ctx *ctx)
6738 const char *str;
6739 isl_basic_set *bset;
6740 isl_basic_map *bmap;
6741 isl_map *map;
6743 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
6744 bmap = isl_basic_map_read_from_str(ctx, str);
6745 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
6746 bset = isl_basic_set_read_from_str(ctx, str);
6747 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
6748 isl_map_free(map);
6750 if (!map)
6751 return -1;
6753 return 0;
6756 /* Check that the variable compression performed on the existentially
6757 * quantified variables inside isl_basic_set_compute_divs is not confused
6758 * by the implicit equalities among the parameters.
6760 static int test_compute_divs(isl_ctx *ctx)
6762 const char *str;
6763 isl_basic_set *bset;
6764 isl_set *set;
6766 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
6767 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
6768 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
6769 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
6770 bset = isl_basic_set_read_from_str(ctx, str);
6771 set = isl_basic_set_compute_divs(bset);
6772 isl_set_free(set);
6773 if (!set)
6774 return -1;
6776 return 0;
6779 /* Check that the reaching domain elements and the prefix schedule
6780 * at a leaf node are the same before and after grouping.
6782 static int test_schedule_tree_group_1(isl_ctx *ctx)
6784 int equal;
6785 const char *str;
6786 isl_id *id;
6787 isl_union_set *uset;
6788 isl_multi_union_pw_aff *mupa;
6789 isl_union_pw_multi_aff *upma1, *upma2;
6790 isl_union_set *domain1, *domain2;
6791 isl_union_map *umap1, *umap2;
6792 isl_schedule_node *node;
6794 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
6795 uset = isl_union_set_read_from_str(ctx, str);
6796 node = isl_schedule_node_from_domain(uset);
6797 node = isl_schedule_node_child(node, 0);
6798 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
6799 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6800 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6801 node = isl_schedule_node_child(node, 0);
6802 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
6803 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6804 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6805 node = isl_schedule_node_child(node, 0);
6806 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
6807 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
6808 domain1 = isl_schedule_node_get_domain(node);
6809 id = isl_id_alloc(ctx, "group", NULL);
6810 node = isl_schedule_node_parent(node);
6811 node = isl_schedule_node_group(node, id);
6812 node = isl_schedule_node_child(node, 0);
6813 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
6814 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
6815 domain2 = isl_schedule_node_get_domain(node);
6816 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
6817 if (equal >= 0 && equal)
6818 equal = isl_union_set_is_equal(domain1, domain2);
6819 if (equal >= 0 && equal)
6820 equal = isl_union_map_is_equal(umap1, umap2);
6821 isl_union_map_free(umap1);
6822 isl_union_map_free(umap2);
6823 isl_union_set_free(domain1);
6824 isl_union_set_free(domain2);
6825 isl_union_pw_multi_aff_free(upma1);
6826 isl_union_pw_multi_aff_free(upma2);
6827 isl_schedule_node_free(node);
6829 if (equal < 0)
6830 return -1;
6831 if (!equal)
6832 isl_die(ctx, isl_error_unknown,
6833 "expressions not equal", return -1);
6835 return 0;
6838 /* Check that we can have nested groupings and that the union map
6839 * schedule representation is the same before and after the grouping.
6840 * Note that after the grouping, the union map representation contains
6841 * the domain constraints from the ranges of the expansion nodes,
6842 * while they are missing from the union map representation of
6843 * the tree without expansion nodes.
6845 * Also check that the global expansion is as expected.
6847 static int test_schedule_tree_group_2(isl_ctx *ctx)
6849 int equal, equal_expansion;
6850 const char *str;
6851 isl_id *id;
6852 isl_union_set *uset;
6853 isl_union_map *umap1, *umap2;
6854 isl_union_map *expansion1, *expansion2;
6855 isl_union_set_list *filters;
6856 isl_multi_union_pw_aff *mupa;
6857 isl_schedule *schedule;
6858 isl_schedule_node *node;
6860 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
6861 "S3[i,j] : 0 <= i,j < 10 }";
6862 uset = isl_union_set_read_from_str(ctx, str);
6863 node = isl_schedule_node_from_domain(uset);
6864 node = isl_schedule_node_child(node, 0);
6865 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
6866 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6867 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6868 node = isl_schedule_node_child(node, 0);
6869 str = "{ S1[i,j] }";
6870 uset = isl_union_set_read_from_str(ctx, str);
6871 filters = isl_union_set_list_from_union_set(uset);
6872 str = "{ S2[i,j]; S3[i,j] }";
6873 uset = isl_union_set_read_from_str(ctx, str);
6874 filters = isl_union_set_list_add(filters, uset);
6875 node = isl_schedule_node_insert_sequence(node, filters);
6876 node = isl_schedule_node_child(node, 1);
6877 node = isl_schedule_node_child(node, 0);
6878 str = "{ S2[i,j] }";
6879 uset = isl_union_set_read_from_str(ctx, str);
6880 filters = isl_union_set_list_from_union_set(uset);
6881 str = "{ S3[i,j] }";
6882 uset = isl_union_set_read_from_str(ctx, str);
6883 filters = isl_union_set_list_add(filters, uset);
6884 node = isl_schedule_node_insert_sequence(node, filters);
6886 schedule = isl_schedule_node_get_schedule(node);
6887 umap1 = isl_schedule_get_map(schedule);
6888 uset = isl_schedule_get_domain(schedule);
6889 umap1 = isl_union_map_intersect_domain(umap1, uset);
6890 isl_schedule_free(schedule);
6892 node = isl_schedule_node_parent(node);
6893 node = isl_schedule_node_parent(node);
6894 id = isl_id_alloc(ctx, "group1", NULL);
6895 node = isl_schedule_node_group(node, id);
6896 node = isl_schedule_node_child(node, 1);
6897 node = isl_schedule_node_child(node, 0);
6898 id = isl_id_alloc(ctx, "group2", NULL);
6899 node = isl_schedule_node_group(node, id);
6901 schedule = isl_schedule_node_get_schedule(node);
6902 umap2 = isl_schedule_get_map(schedule);
6903 isl_schedule_free(schedule);
6905 node = isl_schedule_node_root(node);
6906 node = isl_schedule_node_child(node, 0);
6907 expansion1 = isl_schedule_node_get_subtree_expansion(node);
6908 isl_schedule_node_free(node);
6910 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
6911 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
6912 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
6914 expansion2 = isl_union_map_read_from_str(ctx, str);
6916 equal = isl_union_map_is_equal(umap1, umap2);
6917 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
6919 isl_union_map_free(umap1);
6920 isl_union_map_free(umap2);
6921 isl_union_map_free(expansion1);
6922 isl_union_map_free(expansion2);
6924 if (equal < 0 || equal_expansion < 0)
6925 return -1;
6926 if (!equal)
6927 isl_die(ctx, isl_error_unknown,
6928 "expressions not equal", return -1);
6929 if (!equal_expansion)
6930 isl_die(ctx, isl_error_unknown,
6931 "unexpected expansion", return -1);
6933 return 0;
6936 /* Some tests for the isl_schedule_node_group function.
6938 static int test_schedule_tree_group(isl_ctx *ctx)
6940 if (test_schedule_tree_group_1(ctx) < 0)
6941 return -1;
6942 if (test_schedule_tree_group_2(ctx) < 0)
6943 return -1;
6944 return 0;
6947 struct {
6948 const char *set;
6949 const char *dual;
6950 } coef_tests[] = {
6951 { "{ rat: [i] : 0 <= i <= 10 }",
6952 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
6953 { "{ rat: [i] : FALSE }",
6954 "{ rat: coefficients[[cst] -> [a]] }" },
6955 { "{ rat: [i] : }",
6956 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
6959 struct {
6960 const char *set;
6961 const char *dual;
6962 } sol_tests[] = {
6963 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
6964 "{ rat: [i] : 0 <= i <= 10 }" },
6965 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
6966 "{ rat: [i] }" },
6967 { "{ rat: coefficients[[cst] -> [a]] }",
6968 "{ rat: [i] : FALSE }" },
6971 /* Test the basic functionality of isl_basic_set_coefficients and
6972 * isl_basic_set_solutions.
6974 static int test_dual(isl_ctx *ctx)
6976 int i;
6978 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
6979 int equal;
6980 isl_basic_set *bset1, *bset2;
6982 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
6983 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
6984 bset1 = isl_basic_set_coefficients(bset1);
6985 equal = isl_basic_set_is_equal(bset1, bset2);
6986 isl_basic_set_free(bset1);
6987 isl_basic_set_free(bset2);
6988 if (equal < 0)
6989 return -1;
6990 if (!equal)
6991 isl_die(ctx, isl_error_unknown,
6992 "incorrect dual", return -1);
6995 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
6996 int equal;
6997 isl_basic_set *bset1, *bset2;
6999 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
7000 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
7001 bset1 = isl_basic_set_solutions(bset1);
7002 equal = isl_basic_set_is_equal(bset1, bset2);
7003 isl_basic_set_free(bset1);
7004 isl_basic_set_free(bset2);
7005 if (equal < 0)
7006 return -1;
7007 if (!equal)
7008 isl_die(ctx, isl_error_unknown,
7009 "incorrect dual", return -1);
7012 return 0;
7015 struct {
7016 int scale_tile;
7017 int shift_point;
7018 const char *domain;
7019 const char *schedule;
7020 const char *sizes;
7021 const char *tile;
7022 const char *point;
7023 } tile_tests[] = {
7024 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
7025 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7026 "{ [32,32] }",
7027 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
7028 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7030 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
7031 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7032 "{ [32,32] }",
7033 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
7034 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7036 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
7037 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7038 "{ [32,32] }",
7039 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
7040 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
7042 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
7043 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7044 "{ [32,32] }",
7045 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
7046 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
7050 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
7051 * tile the band and then check if the tile and point bands have the
7052 * expected partial schedule.
7054 static int test_tile(isl_ctx *ctx)
7056 int i;
7057 int scale;
7058 int shift;
7060 scale = isl_options_get_tile_scale_tile_loops(ctx);
7061 shift = isl_options_get_tile_shift_point_loops(ctx);
7063 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
7064 int opt;
7065 int equal;
7066 const char *str;
7067 isl_union_set *domain;
7068 isl_multi_union_pw_aff *mupa, *mupa2;
7069 isl_schedule_node *node;
7070 isl_multi_val *sizes;
7072 opt = tile_tests[i].scale_tile;
7073 isl_options_set_tile_scale_tile_loops(ctx, opt);
7074 opt = tile_tests[i].shift_point;
7075 isl_options_set_tile_shift_point_loops(ctx, opt);
7077 str = tile_tests[i].domain;
7078 domain = isl_union_set_read_from_str(ctx, str);
7079 node = isl_schedule_node_from_domain(domain);
7080 node = isl_schedule_node_child(node, 0);
7081 str = tile_tests[i].schedule;
7082 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7083 node = isl_schedule_node_insert_partial_schedule(node, mupa);
7084 str = tile_tests[i].sizes;
7085 sizes = isl_multi_val_read_from_str(ctx, str);
7086 node = isl_schedule_node_band_tile(node, sizes);
7088 str = tile_tests[i].tile;
7089 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7090 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
7091 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
7092 isl_multi_union_pw_aff_free(mupa);
7093 isl_multi_union_pw_aff_free(mupa2);
7095 node = isl_schedule_node_child(node, 0);
7097 str = tile_tests[i].point;
7098 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7099 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
7100 if (equal >= 0 && equal)
7101 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
7102 mupa2);
7103 isl_multi_union_pw_aff_free(mupa);
7104 isl_multi_union_pw_aff_free(mupa2);
7106 isl_schedule_node_free(node);
7108 if (equal < 0)
7109 return -1;
7110 if (!equal)
7111 isl_die(ctx, isl_error_unknown,
7112 "unexpected result", return -1);
7115 isl_options_set_tile_scale_tile_loops(ctx, scale);
7116 isl_options_set_tile_shift_point_loops(ctx, shift);
7118 return 0;
7121 /* Check that the domain hash of a space is equal to the hash
7122 * of the domain of the space.
7124 static int test_domain_hash(isl_ctx *ctx)
7126 isl_map *map;
7127 isl_space *space;
7128 uint32_t hash1, hash2;
7130 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
7131 space = isl_map_get_space(map);
7132 isl_map_free(map);
7133 hash1 = isl_space_get_domain_hash(space);
7134 space = isl_space_domain(space);
7135 hash2 = isl_space_get_hash(space);
7136 isl_space_free(space);
7138 if (!space)
7139 return -1;
7140 if (hash1 != hash2)
7141 isl_die(ctx, isl_error_unknown,
7142 "domain hash not equal to hash of domain", return -1);
7144 return 0;
7147 /* Check that a universe basic set that is not obviously equal to the universe
7148 * is still recognized as being equal to the universe.
7150 static int test_universe(isl_ctx *ctx)
7152 const char *s;
7153 isl_basic_set *bset;
7154 isl_bool is_univ;
7156 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
7157 bset = isl_basic_set_read_from_str(ctx, s);
7158 is_univ = isl_basic_set_is_universe(bset);
7159 isl_basic_set_free(bset);
7161 if (is_univ < 0)
7162 return -1;
7163 if (!is_univ)
7164 isl_die(ctx, isl_error_unknown,
7165 "not recognized as universe set", return -1);
7167 return 0;
7170 /* Sets for which chambers are computed and checked.
7172 const char *chambers_tests[] = {
7173 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
7174 "z >= 0 and z <= C - y and z <= B - x - y }",
7177 /* Add the domain of "cell" to "cells".
7179 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
7181 isl_basic_set_list **cells = user;
7182 isl_basic_set *dom;
7184 dom = isl_cell_get_domain(cell);
7185 isl_cell_free(cell);
7186 *cells = isl_basic_set_list_add(*cells, dom);
7188 return *cells ? isl_stat_ok : isl_stat_error;
7191 /* Check that the elements of "list" are pairwise disjoint.
7193 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
7195 int i, j, n;
7197 if (!list)
7198 return isl_stat_error;
7200 n = isl_basic_set_list_n_basic_set(list);
7201 for (i = 0; i < n; ++i) {
7202 isl_basic_set *bset_i;
7204 bset_i = isl_basic_set_list_get_basic_set(list, i);
7205 for (j = i + 1; j < n; ++j) {
7206 isl_basic_set *bset_j;
7207 isl_bool disjoint;
7209 bset_j = isl_basic_set_list_get_basic_set(list, j);
7210 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
7211 isl_basic_set_free(bset_j);
7212 if (!disjoint)
7213 isl_die(isl_basic_set_list_get_ctx(list),
7214 isl_error_unknown, "not disjoint",
7215 break);
7216 if (disjoint < 0 || !disjoint)
7217 break;
7219 isl_basic_set_free(bset_i);
7220 if (j < n)
7221 return isl_stat_error;
7224 return isl_stat_ok;
7227 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
7228 * are pairwise disjoint.
7230 static int test_chambers(isl_ctx *ctx)
7232 int i;
7234 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
7235 isl_basic_set *bset;
7236 isl_vertices *vertices;
7237 isl_basic_set_list *cells;
7238 isl_stat ok;
7240 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
7241 vertices = isl_basic_set_compute_vertices(bset);
7242 cells = isl_basic_set_list_alloc(ctx, 0);
7243 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
7244 &cells) < 0)
7245 cells = isl_basic_set_list_free(cells);
7246 ok = check_pairwise_disjoint(cells);
7247 isl_basic_set_list_free(cells);
7248 isl_vertices_free(vertices);
7249 isl_basic_set_free(bset);
7251 if (ok < 0)
7252 return -1;
7255 return 0;
7258 struct {
7259 const char *name;
7260 int (*fn)(isl_ctx *ctx);
7261 } tests [] = {
7262 { "universe", &test_universe },
7263 { "domain hash", &test_domain_hash },
7264 { "dual", &test_dual },
7265 { "dependence analysis", &test_flow },
7266 { "val", &test_val },
7267 { "compute divs", &test_compute_divs },
7268 { "partial lexmin", &test_partial_lexmin },
7269 { "simplify", &test_simplify },
7270 { "curry", &test_curry },
7271 { "piecewise multi affine expressions", &test_pw_multi_aff },
7272 { "multi piecewise affine expressions", &test_multi_pw_aff },
7273 { "conversion", &test_conversion },
7274 { "list", &test_list },
7275 { "align parameters", &test_align_parameters },
7276 { "preimage", &test_preimage },
7277 { "pullback", &test_pullback },
7278 { "AST", &test_ast },
7279 { "AST build", &test_ast_build },
7280 { "AST generation", &test_ast_gen },
7281 { "eliminate", &test_eliminate },
7282 { "residue class", &test_residue_class },
7283 { "div", &test_div },
7284 { "slice", &test_slice },
7285 { "fixed power", &test_fixed_power },
7286 { "sample", &test_sample },
7287 { "output", &test_output },
7288 { "vertices", &test_vertices },
7289 { "chambers", &test_chambers },
7290 { "fixed", &test_fixed },
7291 { "equal", &test_equal },
7292 { "disjoint", &test_disjoint },
7293 { "product", &test_product },
7294 { "dim_max", &test_dim_max },
7295 { "affine", &test_aff },
7296 { "injective", &test_injective },
7297 { "schedule (whole component)", &test_schedule_whole },
7298 { "schedule (incremental)", &test_schedule_incremental },
7299 { "schedule tree grouping", &test_schedule_tree_group },
7300 { "tile", &test_tile },
7301 { "union_pw", &test_union_pw },
7302 { "eval", &test_eval },
7303 { "parse", &test_parse },
7304 { "single-valued", &test_sv },
7305 { "affine hull", &test_affine_hull },
7306 { "simple_hull", &test_simple_hull },
7307 { "coalesce", &test_coalesce },
7308 { "factorize", &test_factorize },
7309 { "subset", &test_subset },
7310 { "subtract", &test_subtract },
7311 { "intersect", &test_intersect },
7312 { "lexmin", &test_lexmin },
7313 { "min", &test_min },
7314 { "gist", &test_gist },
7315 { "piecewise quasi-polynomials", &test_pwqp },
7316 { "lift", &test_lift },
7317 { "bound", &test_bound },
7318 { "union", &test_union },
7319 { "split periods", &test_split_periods },
7320 { "lexicographic order", &test_lex },
7321 { "bijectivity", &test_bijective },
7322 { "dataflow analysis", &test_dep },
7323 { "reading", &test_read },
7324 { "bounded", &test_bounded },
7325 { "construction", &test_construction },
7326 { "dimension manipulation", &test_dim },
7327 { "map application", &test_application },
7328 { "convex hull", &test_convex_hull },
7329 { "transitive closure", &test_closure },
7332 int main(int argc, char **argv)
7334 int i;
7335 struct isl_ctx *ctx;
7336 struct isl_options *options;
7338 options = isl_options_new_with_defaults();
7339 assert(options);
7340 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
7342 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
7343 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
7344 printf("%s\n", tests[i].name);
7345 if (tests[i].fn(ctx) < 0)
7346 goto error;
7348 isl_ctx_free(ctx);
7349 return 0;
7350 error:
7351 isl_ctx_free(ctx);
7352 return -1;