add missing annotations to isl_basic_map_drop_redundant_divs
[isl.git] / isl_test.c
blobcc81e1b28248ca266a416193b34a3580c9388707
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <assert.h>
19 #include <stdio.h>
20 #include <limits.h>
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl_space_private.h>
25 #include <isl/set.h>
26 #include <isl/flow.h>
27 #include <isl_constraint_private.h>
28 #include <isl/polynomial.h>
29 #include <isl/union_set.h>
30 #include <isl/union_map.h>
31 #include <isl_factorization.h>
32 #include <isl/schedule.h>
33 #include <isl/schedule_node.h>
34 #include <isl_options_private.h>
35 #include <isl_vertices_private.h>
36 #include <isl/ast_build.h>
37 #include <isl/val.h>
38 #include <isl/ilp.h>
39 #include <isl_ast_build_expr.h>
40 #include <isl/options.h>
42 #include "isl_srcdir.c"
44 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
46 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
47 char *filename;
48 int length;
49 char *pattern = "%s/test_inputs/%s.%s";
51 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
52 + strlen(suffix) + 1;
53 filename = isl_alloc_array(ctx, char, length);
55 if (!filename)
56 return NULL;
58 sprintf(filename, pattern, srcdir, name, suffix);
60 return filename;
63 void test_parse_map(isl_ctx *ctx, const char *str)
65 isl_map *map;
67 map = isl_map_read_from_str(ctx, str);
68 assert(map);
69 isl_map_free(map);
72 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
74 isl_map *map, *map2;
75 int equal;
77 map = isl_map_read_from_str(ctx, str);
78 map2 = isl_map_read_from_str(ctx, str2);
79 equal = isl_map_is_equal(map, map2);
80 isl_map_free(map);
81 isl_map_free(map2);
83 if (equal < 0)
84 return -1;
85 if (!equal)
86 isl_die(ctx, isl_error_unknown, "maps not equal",
87 return -1);
89 return 0;
92 void test_parse_pwqp(isl_ctx *ctx, const char *str)
94 isl_pw_qpolynomial *pwqp;
96 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
97 assert(pwqp);
98 isl_pw_qpolynomial_free(pwqp);
101 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
103 isl_pw_aff *pwaff;
105 pwaff = isl_pw_aff_read_from_str(ctx, str);
106 assert(pwaff);
107 isl_pw_aff_free(pwaff);
110 /* Check that we can read an isl_multi_val from "str" without errors.
112 static int test_parse_multi_val(isl_ctx *ctx, const char *str)
114 isl_multi_val *mv;
116 mv = isl_multi_val_read_from_str(ctx, str);
117 isl_multi_val_free(mv);
119 return mv ? 0 : -1;
122 /* Pairs of binary relation representations that should represent
123 * the same binary relations.
125 struct {
126 const char *map1;
127 const char *map2;
128 } parse_map_equal_tests[] = {
129 { "{ [x,y] : [([x/2]+y)/3] >= 1 }",
130 "{ [x, y] : 2y >= 6 - x }" },
131 { "{ [x,y] : x <= min(y, 2*y+3) }",
132 "{ [x,y] : x <= y, 2*y + 3 }" },
133 { "{ [x,y] : x >= min(y, 2*y+3) }",
134 "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }" },
135 { "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }",
136 "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }" },
137 { "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }",
138 "{ [i,j] -> [min(i,j)] }" },
139 { "{ [i,j] : i != j }",
140 "{ [i,j] : i < j or i > j }" },
141 { "{ [i,j] : (i+1)*2 >= j }",
142 "{ [i, j] : j <= 2 + 2i }" },
143 { "{ [i] -> [i > 0 ? 4 : 5] }",
144 "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }" },
145 { "[N=2,M] -> { [i=[(M+N)/4]] }",
146 "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }" },
147 { "{ [x] : x >= 0 }",
148 "{ [x] : x-0 >= 0 }" },
149 { "{ [i] : ((i > 10)) }",
150 "{ [i] : i >= 11 }" },
151 { "{ [i] -> [0] }",
152 "{ [i] -> [0 * i] }" },
153 { "{ [a] -> [b] : (not false) }",
154 "{ [a] -> [b] : true }" },
155 { "{ [i] : i/2 <= 5 }",
156 "{ [i] : i <= 10 }" },
157 { "{Sym=[n] [i] : i <= n }",
158 "[n] -> { [i] : i <= n }" },
159 { "{ [*] }",
160 "{ [a] }" },
161 { "{ [i] : 2*floor(i/2) = i }",
162 "{ [i] : exists a : i = 2 a }" },
163 { "{ [a] -> [b] : a = 5 implies b = 5 }",
164 "{ [a] -> [b] : a != 5 or b = 5 }" },
165 { "{ [a] -> [a - 1 : a > 0] }",
166 "{ [a] -> [a - 1] : a > 0 }" },
167 { "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
168 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }" },
169 { "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
170 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
171 { "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
172 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
173 { "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
174 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
175 { "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
176 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
177 { "{ [a,b] -> [i,j] : a,b << i,j }",
178 "{ [a,b] -> [i,j] : a < i or (a = i and b < j) }" },
179 { "{ [a,b] -> [i,j] : a,b <<= i,j }",
180 "{ [a,b] -> [i,j] : a < i or (a = i and b <= j) }" },
181 { "{ [a,b] -> [i,j] : a,b >> i,j }",
182 "{ [a,b] -> [i,j] : a > i or (a = i and b > j) }" },
183 { "{ [a,b] -> [i,j] : a,b >>= i,j }",
184 "{ [a,b] -> [i,j] : a > i or (a = i and b >= j) }" },
185 { "{ [n] -> [i] : exists (a, b, c: 8b <= i - 32a and "
186 "8b >= -7 + i - 32 a and b >= 0 and b <= 3 and "
187 "8c < n - 32a and i < n and c >= 0 and "
188 "c <= 3 and c >= -4a) }",
189 "{ [n] -> [i] : 0 <= i < n }" },
190 { "{ [x] -> [] : exists (a, b: 0 <= a <= 1 and 0 <= b <= 3 and "
191 "2b <= x - 8a and 2b >= -1 + x - 8a) }",
192 "{ [x] -> [] : 0 <= x <= 15 }" },
195 int test_parse(struct isl_ctx *ctx)
197 int i;
198 isl_map *map, *map2;
199 const char *str, *str2;
201 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
202 return -1;
203 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
204 return -1;
205 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
206 return -1;
208 str = "{ [i] -> [-i] }";
209 map = isl_map_read_from_str(ctx, str);
210 assert(map);
211 isl_map_free(map);
213 str = "{ A[i] -> L[([i/3])] }";
214 map = isl_map_read_from_str(ctx, str);
215 assert(map);
216 isl_map_free(map);
218 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
219 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
220 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
222 for (i = 0; i < ARRAY_SIZE(parse_map_equal_tests); ++i) {
223 str = parse_map_equal_tests[i].map1;
224 str2 = parse_map_equal_tests[i].map2;
225 if (test_parse_map_equal(ctx, str, str2) < 0)
226 return -1;
229 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
230 map = isl_map_read_from_str(ctx, str);
231 str = "{ [new, old] -> [o0, o1] : "
232 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
233 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
234 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
235 map2 = isl_map_read_from_str(ctx, str);
236 assert(isl_map_is_equal(map, map2));
237 isl_map_free(map);
238 isl_map_free(map2);
240 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
241 map = isl_map_read_from_str(ctx, str);
242 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
243 map2 = isl_map_read_from_str(ctx, str);
244 assert(isl_map_is_equal(map, map2));
245 isl_map_free(map);
246 isl_map_free(map2);
248 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
249 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
250 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
251 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
252 test_parse_pwaff(ctx, "{ [] -> [(100)] }");
254 return 0;
257 static int test_read(isl_ctx *ctx)
259 char *filename;
260 FILE *input;
261 isl_basic_set *bset1, *bset2;
262 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
263 int equal;
265 filename = get_filename(ctx, "set", "omega");
266 assert(filename);
267 input = fopen(filename, "r");
268 assert(input);
270 bset1 = isl_basic_set_read_from_file(ctx, input);
271 bset2 = isl_basic_set_read_from_str(ctx, str);
273 equal = isl_basic_set_is_equal(bset1, bset2);
275 isl_basic_set_free(bset1);
276 isl_basic_set_free(bset2);
277 free(filename);
279 fclose(input);
281 if (equal < 0)
282 return -1;
283 if (!equal)
284 isl_die(ctx, isl_error_unknown,
285 "read sets not equal", return -1);
287 return 0;
290 static int test_bounded(isl_ctx *ctx)
292 isl_set *set;
293 isl_bool bounded;
295 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
296 bounded = isl_set_is_bounded(set);
297 isl_set_free(set);
299 if (bounded < 0)
300 return -1;
301 if (!bounded)
302 isl_die(ctx, isl_error_unknown,
303 "set not considered bounded", return -1);
305 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
306 bounded = isl_set_is_bounded(set);
307 assert(!bounded);
308 isl_set_free(set);
310 if (bounded < 0)
311 return -1;
312 if (bounded)
313 isl_die(ctx, isl_error_unknown,
314 "set considered bounded", return -1);
316 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
317 bounded = isl_set_is_bounded(set);
318 isl_set_free(set);
320 if (bounded < 0)
321 return -1;
322 if (bounded)
323 isl_die(ctx, isl_error_unknown,
324 "set considered bounded", return -1);
326 return 0;
329 /* Construct the basic set { [i] : 5 <= i <= N } */
330 static int test_construction_1(isl_ctx *ctx)
332 isl_int v;
333 isl_space *dim;
334 isl_local_space *ls;
335 isl_basic_set *bset;
336 isl_constraint *c;
338 isl_int_init(v);
340 dim = isl_space_set_alloc(ctx, 1, 1);
341 bset = isl_basic_set_universe(isl_space_copy(dim));
342 ls = isl_local_space_from_space(dim);
344 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
345 isl_int_set_si(v, -1);
346 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
347 isl_int_set_si(v, 1);
348 c = isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
349 bset = isl_basic_set_add_constraint(bset, c);
351 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
352 isl_int_set_si(v, 1);
353 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
354 isl_int_set_si(v, -5);
355 c = isl_constraint_set_constant(c, v);
356 bset = isl_basic_set_add_constraint(bset, c);
358 isl_local_space_free(ls);
359 isl_basic_set_free(bset);
361 isl_int_clear(v);
363 return 0;
366 /* Construct the basic set { [x] : -100 <= x <= 100 }
367 * using isl_basic_set_{lower,upper}_bound_val and
368 * check that it is equal the same basic set parsed from a string.
370 static int test_construction_2(isl_ctx *ctx)
372 isl_bool equal;
373 isl_val *v;
374 isl_space *space;
375 isl_basic_set *bset1, *bset2;
377 v = isl_val_int_from_si(ctx, 100);
378 space = isl_space_set_alloc(ctx, 0, 1);
379 bset1 = isl_basic_set_universe(space);
380 bset1 = isl_basic_set_upper_bound_val(bset1, isl_dim_set, 0,
381 isl_val_copy(v));
382 bset1 = isl_basic_set_lower_bound_val(bset1, isl_dim_set, 0,
383 isl_val_neg(v));
384 bset2 = isl_basic_set_read_from_str(ctx, "{ [x] : -100 <= x <= 100 }");
385 equal = isl_basic_set_is_equal(bset1, bset2);
386 isl_basic_set_free(bset1);
387 isl_basic_set_free(bset2);
389 if (equal < 0)
390 return -1;
391 if (!equal)
392 isl_die(ctx, isl_error_unknown,
393 "failed construction", return -1);
395 return 0;
398 /* Basic tests for constructing basic sets.
400 static int test_construction(isl_ctx *ctx)
402 if (test_construction_1(ctx) < 0)
403 return -1;
404 if (test_construction_2(ctx) < 0)
405 return -1;
406 return 0;
409 static int test_dim(isl_ctx *ctx)
411 const char *str;
412 isl_map *map1, *map2;
413 int equal;
415 map1 = isl_map_read_from_str(ctx,
416 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
417 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
418 map2 = isl_map_read_from_str(ctx,
419 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
420 equal = isl_map_is_equal(map1, map2);
421 isl_map_free(map2);
423 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
424 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
425 if (equal >= 0 && equal)
426 equal = isl_map_is_equal(map1, map2);
428 isl_map_free(map1);
429 isl_map_free(map2);
431 if (equal < 0)
432 return -1;
433 if (!equal)
434 isl_die(ctx, isl_error_unknown,
435 "unexpected result", return -1);
437 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
438 map1 = isl_map_read_from_str(ctx, str);
439 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
440 map2 = isl_map_read_from_str(ctx, str);
441 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
442 equal = isl_map_is_equal(map1, map2);
443 isl_map_free(map1);
444 isl_map_free(map2);
446 if (equal < 0)
447 return -1;
448 if (!equal)
449 isl_die(ctx, isl_error_unknown,
450 "unexpected result", return -1);
452 return 0;
455 struct {
456 __isl_give isl_val *(*op)(__isl_take isl_val *v);
457 const char *arg;
458 const char *res;
459 } val_un_tests[] = {
460 { &isl_val_neg, "0", "0" },
461 { &isl_val_abs, "0", "0" },
462 { &isl_val_2exp, "0", "1" },
463 { &isl_val_floor, "0", "0" },
464 { &isl_val_ceil, "0", "0" },
465 { &isl_val_neg, "1", "-1" },
466 { &isl_val_neg, "-1", "1" },
467 { &isl_val_neg, "1/2", "-1/2" },
468 { &isl_val_neg, "-1/2", "1/2" },
469 { &isl_val_neg, "infty", "-infty" },
470 { &isl_val_neg, "-infty", "infty" },
471 { &isl_val_neg, "NaN", "NaN" },
472 { &isl_val_abs, "1", "1" },
473 { &isl_val_abs, "-1", "1" },
474 { &isl_val_abs, "1/2", "1/2" },
475 { &isl_val_abs, "-1/2", "1/2" },
476 { &isl_val_abs, "infty", "infty" },
477 { &isl_val_abs, "-infty", "infty" },
478 { &isl_val_abs, "NaN", "NaN" },
479 { &isl_val_floor, "1", "1" },
480 { &isl_val_floor, "-1", "-1" },
481 { &isl_val_floor, "1/2", "0" },
482 { &isl_val_floor, "-1/2", "-1" },
483 { &isl_val_floor, "infty", "infty" },
484 { &isl_val_floor, "-infty", "-infty" },
485 { &isl_val_floor, "NaN", "NaN" },
486 { &isl_val_ceil, "1", "1" },
487 { &isl_val_ceil, "-1", "-1" },
488 { &isl_val_ceil, "1/2", "1" },
489 { &isl_val_ceil, "-1/2", "0" },
490 { &isl_val_ceil, "infty", "infty" },
491 { &isl_val_ceil, "-infty", "-infty" },
492 { &isl_val_ceil, "NaN", "NaN" },
493 { &isl_val_2exp, "-3", "1/8" },
494 { &isl_val_2exp, "-1", "1/2" },
495 { &isl_val_2exp, "1", "2" },
496 { &isl_val_2exp, "2", "4" },
497 { &isl_val_2exp, "3", "8" },
498 { &isl_val_inv, "1", "1" },
499 { &isl_val_inv, "2", "1/2" },
500 { &isl_val_inv, "1/2", "2" },
501 { &isl_val_inv, "-2", "-1/2" },
502 { &isl_val_inv, "-1/2", "-2" },
503 { &isl_val_inv, "0", "NaN" },
504 { &isl_val_inv, "NaN", "NaN" },
505 { &isl_val_inv, "infty", "0" },
506 { &isl_val_inv, "-infty", "0" },
509 /* Perform some basic tests of unary operations on isl_val objects.
511 static int test_un_val(isl_ctx *ctx)
513 int i;
514 isl_val *v, *res;
515 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
516 int ok;
518 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
519 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
520 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
521 fn = val_un_tests[i].op;
522 v = fn(v);
523 if (isl_val_is_nan(res))
524 ok = isl_val_is_nan(v);
525 else
526 ok = isl_val_eq(v, res);
527 isl_val_free(v);
528 isl_val_free(res);
529 if (ok < 0)
530 return -1;
531 if (!ok)
532 isl_die(ctx, isl_error_unknown,
533 "unexpected result", return -1);
536 return 0;
539 struct {
540 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
541 __isl_take isl_val *v2);
542 } val_bin_op[] = {
543 ['+'] = { &isl_val_add },
544 ['-'] = { &isl_val_sub },
545 ['*'] = { &isl_val_mul },
546 ['/'] = { &isl_val_div },
547 ['g'] = { &isl_val_gcd },
548 ['m'] = { &isl_val_min },
549 ['M'] = { &isl_val_max },
552 struct {
553 const char *arg1;
554 unsigned char op;
555 const char *arg2;
556 const char *res;
557 } val_bin_tests[] = {
558 { "0", '+', "0", "0" },
559 { "1", '+', "0", "1" },
560 { "1", '+', "1", "2" },
561 { "1", '-', "1", "0" },
562 { "1", '*', "1", "1" },
563 { "1", '/', "1", "1" },
564 { "2", '*', "3", "6" },
565 { "2", '*', "1/2", "1" },
566 { "2", '*', "1/3", "2/3" },
567 { "2/3", '*', "3/5", "2/5" },
568 { "2/3", '*', "7/5", "14/15" },
569 { "2", '/', "1/2", "4" },
570 { "-2", '/', "-1/2", "4" },
571 { "-2", '/', "1/2", "-4" },
572 { "2", '/', "-1/2", "-4" },
573 { "2", '/', "2", "1" },
574 { "2", '/', "3", "2/3" },
575 { "2/3", '/', "5/3", "2/5" },
576 { "2/3", '/', "5/7", "14/15" },
577 { "0", '/', "0", "NaN" },
578 { "42", '/', "0", "NaN" },
579 { "-42", '/', "0", "NaN" },
580 { "infty", '/', "0", "NaN" },
581 { "-infty", '/', "0", "NaN" },
582 { "NaN", '/', "0", "NaN" },
583 { "0", '/', "NaN", "NaN" },
584 { "42", '/', "NaN", "NaN" },
585 { "-42", '/', "NaN", "NaN" },
586 { "infty", '/', "NaN", "NaN" },
587 { "-infty", '/', "NaN", "NaN" },
588 { "NaN", '/', "NaN", "NaN" },
589 { "0", '/', "infty", "0" },
590 { "42", '/', "infty", "0" },
591 { "-42", '/', "infty", "0" },
592 { "infty", '/', "infty", "NaN" },
593 { "-infty", '/', "infty", "NaN" },
594 { "NaN", '/', "infty", "NaN" },
595 { "0", '/', "-infty", "0" },
596 { "42", '/', "-infty", "0" },
597 { "-42", '/', "-infty", "0" },
598 { "infty", '/', "-infty", "NaN" },
599 { "-infty", '/', "-infty", "NaN" },
600 { "NaN", '/', "-infty", "NaN" },
601 { "1", '-', "1/3", "2/3" },
602 { "1/3", '+', "1/2", "5/6" },
603 { "1/2", '+', "1/2", "1" },
604 { "3/4", '-', "1/4", "1/2" },
605 { "1/2", '-', "1/3", "1/6" },
606 { "infty", '+', "42", "infty" },
607 { "infty", '+', "infty", "infty" },
608 { "42", '+', "infty", "infty" },
609 { "infty", '-', "infty", "NaN" },
610 { "infty", '*', "infty", "infty" },
611 { "infty", '*', "-infty", "-infty" },
612 { "-infty", '*', "infty", "-infty" },
613 { "-infty", '*', "-infty", "infty" },
614 { "0", '*', "infty", "NaN" },
615 { "1", '*', "infty", "infty" },
616 { "infty", '*', "0", "NaN" },
617 { "infty", '*', "42", "infty" },
618 { "42", '-', "infty", "-infty" },
619 { "infty", '+', "-infty", "NaN" },
620 { "4", 'g', "6", "2" },
621 { "5", 'g', "6", "1" },
622 { "42", 'm', "3", "3" },
623 { "42", 'M', "3", "42" },
624 { "3", 'm', "42", "3" },
625 { "3", 'M', "42", "42" },
626 { "42", 'm', "infty", "42" },
627 { "42", 'M', "infty", "infty" },
628 { "42", 'm', "-infty", "-infty" },
629 { "42", 'M', "-infty", "42" },
630 { "42", 'm', "NaN", "NaN" },
631 { "42", 'M', "NaN", "NaN" },
632 { "infty", 'm', "-infty", "-infty" },
633 { "infty", 'M', "-infty", "infty" },
636 /* Perform some basic tests of binary operations on isl_val objects.
638 static int test_bin_val(isl_ctx *ctx)
640 int i;
641 isl_val *v1, *v2, *res;
642 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
643 __isl_take isl_val *v2);
644 int ok;
646 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
647 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
648 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
649 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
650 fn = val_bin_op[val_bin_tests[i].op].fn;
651 v1 = fn(v1, v2);
652 if (isl_val_is_nan(res))
653 ok = isl_val_is_nan(v1);
654 else
655 ok = isl_val_eq(v1, res);
656 isl_val_free(v1);
657 isl_val_free(res);
658 if (ok < 0)
659 return -1;
660 if (!ok)
661 isl_die(ctx, isl_error_unknown,
662 "unexpected result", return -1);
665 return 0;
668 /* Perform some basic tests on isl_val objects.
670 static int test_val(isl_ctx *ctx)
672 if (test_un_val(ctx) < 0)
673 return -1;
674 if (test_bin_val(ctx) < 0)
675 return -1;
676 return 0;
679 /* Sets described using existentially quantified variables that
680 * can also be described without.
682 static const char *elimination_tests[] = {
683 "{ [i,j] : 2 * [i/2] + 3 * [j/4] <= 10 and 2 i = j }",
684 "{ [m, w] : exists a : w - 2m - 5 <= 3a <= m - 2w }",
685 "{ [m, w] : exists a : w >= 0 and a < m and -1 + w <= a <= 2m - w }",
688 /* Check that redundant existentially quantified variables are
689 * getting removed.
691 static int test_elimination(isl_ctx *ctx)
693 int i;
694 unsigned n;
695 isl_basic_set *bset;
697 for (i = 0; i < ARRAY_SIZE(elimination_tests); ++i) {
698 bset = isl_basic_set_read_from_str(ctx, elimination_tests[i]);
699 n = isl_basic_set_dim(bset, isl_dim_div);
700 isl_basic_set_free(bset);
701 if (!bset)
702 return -1;
703 if (n != 0)
704 isl_die(ctx, isl_error_unknown,
705 "expecting no existentials", return -1);
708 return 0;
711 static int test_div(isl_ctx *ctx)
713 const char *str;
714 int empty;
715 isl_int v;
716 isl_space *dim;
717 isl_set *set;
718 isl_local_space *ls;
719 struct isl_basic_set *bset;
720 struct isl_constraint *c;
722 isl_int_init(v);
724 /* test 1 */
725 dim = isl_space_set_alloc(ctx, 0, 3);
726 bset = isl_basic_set_universe(isl_space_copy(dim));
727 ls = isl_local_space_from_space(dim);
729 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
730 isl_int_set_si(v, -1);
731 c = isl_constraint_set_constant(c, v);
732 isl_int_set_si(v, 1);
733 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
734 isl_int_set_si(v, 3);
735 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
736 bset = isl_basic_set_add_constraint(bset, c);
738 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
739 isl_int_set_si(v, 1);
740 c = isl_constraint_set_constant(c, v);
741 isl_int_set_si(v, -1);
742 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
743 isl_int_set_si(v, 3);
744 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
745 bset = isl_basic_set_add_constraint(bset, c);
747 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
749 assert(bset && bset->n_div == 1);
750 isl_local_space_free(ls);
751 isl_basic_set_free(bset);
753 /* test 2 */
754 dim = isl_space_set_alloc(ctx, 0, 3);
755 bset = isl_basic_set_universe(isl_space_copy(dim));
756 ls = isl_local_space_from_space(dim);
758 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
759 isl_int_set_si(v, 1);
760 c = isl_constraint_set_constant(c, v);
761 isl_int_set_si(v, -1);
762 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
763 isl_int_set_si(v, 3);
764 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
765 bset = isl_basic_set_add_constraint(bset, c);
767 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
768 isl_int_set_si(v, -1);
769 c = isl_constraint_set_constant(c, v);
770 isl_int_set_si(v, 1);
771 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
772 isl_int_set_si(v, 3);
773 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
774 bset = isl_basic_set_add_constraint(bset, c);
776 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
778 assert(bset && bset->n_div == 1);
779 isl_local_space_free(ls);
780 isl_basic_set_free(bset);
782 /* test 3 */
783 dim = isl_space_set_alloc(ctx, 0, 3);
784 bset = isl_basic_set_universe(isl_space_copy(dim));
785 ls = isl_local_space_from_space(dim);
787 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
788 isl_int_set_si(v, 1);
789 c = isl_constraint_set_constant(c, v);
790 isl_int_set_si(v, -1);
791 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
792 isl_int_set_si(v, 3);
793 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
794 bset = isl_basic_set_add_constraint(bset, c);
796 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
797 isl_int_set_si(v, -3);
798 c = isl_constraint_set_constant(c, v);
799 isl_int_set_si(v, 1);
800 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
801 isl_int_set_si(v, 4);
802 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
803 bset = isl_basic_set_add_constraint(bset, c);
805 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
807 assert(bset && bset->n_div == 1);
808 isl_local_space_free(ls);
809 isl_basic_set_free(bset);
811 /* test 4 */
812 dim = isl_space_set_alloc(ctx, 0, 3);
813 bset = isl_basic_set_universe(isl_space_copy(dim));
814 ls = isl_local_space_from_space(dim);
816 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
817 isl_int_set_si(v, 2);
818 c = isl_constraint_set_constant(c, v);
819 isl_int_set_si(v, -1);
820 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
821 isl_int_set_si(v, 3);
822 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
823 bset = isl_basic_set_add_constraint(bset, c);
825 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
826 isl_int_set_si(v, -1);
827 c = isl_constraint_set_constant(c, v);
828 isl_int_set_si(v, 1);
829 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
830 isl_int_set_si(v, 6);
831 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
832 bset = isl_basic_set_add_constraint(bset, c);
834 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
836 assert(isl_basic_set_is_empty(bset));
837 isl_local_space_free(ls);
838 isl_basic_set_free(bset);
840 /* test 5 */
841 dim = isl_space_set_alloc(ctx, 0, 3);
842 bset = isl_basic_set_universe(isl_space_copy(dim));
843 ls = isl_local_space_from_space(dim);
845 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
846 isl_int_set_si(v, -1);
847 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
848 isl_int_set_si(v, 3);
849 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
850 bset = isl_basic_set_add_constraint(bset, c);
852 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
853 isl_int_set_si(v, 1);
854 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
855 isl_int_set_si(v, -3);
856 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
857 bset = isl_basic_set_add_constraint(bset, c);
859 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
861 assert(bset && bset->n_div == 0);
862 isl_basic_set_free(bset);
863 isl_local_space_free(ls);
865 /* test 6 */
866 dim = isl_space_set_alloc(ctx, 0, 3);
867 bset = isl_basic_set_universe(isl_space_copy(dim));
868 ls = isl_local_space_from_space(dim);
870 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
871 isl_int_set_si(v, -1);
872 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
873 isl_int_set_si(v, 6);
874 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
875 bset = isl_basic_set_add_constraint(bset, c);
877 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
878 isl_int_set_si(v, 1);
879 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
880 isl_int_set_si(v, -3);
881 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
882 bset = isl_basic_set_add_constraint(bset, c);
884 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
886 assert(bset && bset->n_div == 1);
887 isl_basic_set_free(bset);
888 isl_local_space_free(ls);
890 /* test 7 */
891 /* This test is a bit tricky. We set up an equality
892 * a + 3b + 3c = 6 e0
893 * Normalization of divs creates _two_ divs
894 * a = 3 e0
895 * c - b - e0 = 2 e1
896 * Afterwards e0 is removed again because it has coefficient -1
897 * and we end up with the original equality and div again.
898 * Perhaps we can avoid the introduction of this temporary div.
900 dim = isl_space_set_alloc(ctx, 0, 4);
901 bset = isl_basic_set_universe(isl_space_copy(dim));
902 ls = isl_local_space_from_space(dim);
904 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
905 isl_int_set_si(v, -1);
906 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
907 isl_int_set_si(v, -3);
908 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
909 isl_int_set_si(v, -3);
910 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
911 isl_int_set_si(v, 6);
912 c = isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
913 bset = isl_basic_set_add_constraint(bset, c);
915 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
917 /* Test disabled for now */
919 assert(bset && bset->n_div == 1);
921 isl_local_space_free(ls);
922 isl_basic_set_free(bset);
924 /* test 8 */
925 dim = isl_space_set_alloc(ctx, 0, 5);
926 bset = isl_basic_set_universe(isl_space_copy(dim));
927 ls = isl_local_space_from_space(dim);
929 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
930 isl_int_set_si(v, -1);
931 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
932 isl_int_set_si(v, -3);
933 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
934 isl_int_set_si(v, -3);
935 c = isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
936 isl_int_set_si(v, 6);
937 c = isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
938 bset = isl_basic_set_add_constraint(bset, c);
940 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
941 isl_int_set_si(v, -1);
942 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
943 isl_int_set_si(v, 1);
944 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
945 isl_int_set_si(v, 1);
946 c = isl_constraint_set_constant(c, v);
947 bset = isl_basic_set_add_constraint(bset, c);
949 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
951 /* Test disabled for now */
953 assert(bset && bset->n_div == 1);
955 isl_local_space_free(ls);
956 isl_basic_set_free(bset);
958 /* test 9 */
959 dim = isl_space_set_alloc(ctx, 0, 4);
960 bset = isl_basic_set_universe(isl_space_copy(dim));
961 ls = isl_local_space_from_space(dim);
963 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
964 isl_int_set_si(v, 1);
965 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
966 isl_int_set_si(v, -1);
967 c = isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
968 isl_int_set_si(v, -2);
969 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
970 bset = isl_basic_set_add_constraint(bset, c);
972 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
973 isl_int_set_si(v, -1);
974 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
975 isl_int_set_si(v, 3);
976 c = isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
977 isl_int_set_si(v, 2);
978 c = isl_constraint_set_constant(c, v);
979 bset = isl_basic_set_add_constraint(bset, c);
981 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
983 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
985 assert(!isl_basic_set_is_empty(bset));
987 isl_local_space_free(ls);
988 isl_basic_set_free(bset);
990 /* test 10 */
991 dim = isl_space_set_alloc(ctx, 0, 3);
992 bset = isl_basic_set_universe(isl_space_copy(dim));
993 ls = isl_local_space_from_space(dim);
995 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
996 isl_int_set_si(v, 1);
997 c = isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
998 isl_int_set_si(v, -2);
999 c = isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
1000 bset = isl_basic_set_add_constraint(bset, c);
1002 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1004 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1006 isl_local_space_free(ls);
1007 isl_basic_set_free(bset);
1009 isl_int_clear(v);
1011 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1012 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
1013 set = isl_set_read_from_str(ctx, str);
1014 set = isl_set_compute_divs(set);
1015 isl_set_free(set);
1016 if (!set)
1017 return -1;
1019 if (test_elimination(ctx) < 0)
1020 return -1;
1022 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
1023 set = isl_set_read_from_str(ctx, str);
1024 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
1025 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
1026 empty = isl_set_is_empty(set);
1027 isl_set_free(set);
1028 if (empty < 0)
1029 return -1;
1030 if (!empty)
1031 isl_die(ctx, isl_error_unknown,
1032 "result not as accurate as expected", return -1);
1034 return 0;
1037 void test_application_case(struct isl_ctx *ctx, const char *name)
1039 char *filename;
1040 FILE *input;
1041 struct isl_basic_set *bset1, *bset2;
1042 struct isl_basic_map *bmap;
1044 filename = get_filename(ctx, name, "omega");
1045 assert(filename);
1046 input = fopen(filename, "r");
1047 assert(input);
1049 bset1 = isl_basic_set_read_from_file(ctx, input);
1050 bmap = isl_basic_map_read_from_file(ctx, input);
1052 bset1 = isl_basic_set_apply(bset1, bmap);
1054 bset2 = isl_basic_set_read_from_file(ctx, input);
1056 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1058 isl_basic_set_free(bset1);
1059 isl_basic_set_free(bset2);
1060 free(filename);
1062 fclose(input);
1065 static int test_application(isl_ctx *ctx)
1067 test_application_case(ctx, "application");
1068 test_application_case(ctx, "application2");
1070 return 0;
1073 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1075 char *filename;
1076 FILE *input;
1077 struct isl_basic_set *bset1, *bset2;
1079 filename = get_filename(ctx, name, "polylib");
1080 assert(filename);
1081 input = fopen(filename, "r");
1082 assert(input);
1084 bset1 = isl_basic_set_read_from_file(ctx, input);
1085 bset2 = isl_basic_set_read_from_file(ctx, input);
1087 bset1 = isl_basic_set_affine_hull(bset1);
1089 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1091 isl_basic_set_free(bset1);
1092 isl_basic_set_free(bset2);
1093 free(filename);
1095 fclose(input);
1098 int test_affine_hull(struct isl_ctx *ctx)
1100 const char *str;
1101 isl_set *set;
1102 isl_basic_set *bset, *bset2;
1103 int n;
1104 int subset;
1106 test_affine_hull_case(ctx, "affine2");
1107 test_affine_hull_case(ctx, "affine");
1108 test_affine_hull_case(ctx, "affine3");
1110 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1111 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1112 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1113 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1114 set = isl_set_read_from_str(ctx, str);
1115 bset = isl_set_affine_hull(set);
1116 n = isl_basic_set_dim(bset, isl_dim_div);
1117 isl_basic_set_free(bset);
1118 if (n != 0)
1119 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1120 return -1);
1122 /* Check that isl_map_affine_hull is not confused by
1123 * the reordering of divs in isl_map_align_divs.
1125 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1126 "32e0 = b and 32e1 = c); "
1127 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1128 set = isl_set_read_from_str(ctx, str);
1129 bset = isl_set_affine_hull(set);
1130 isl_basic_set_free(bset);
1131 if (!bset)
1132 return -1;
1134 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1135 "32e2 = 31 + 31e0 }";
1136 set = isl_set_read_from_str(ctx, str);
1137 bset = isl_set_affine_hull(set);
1138 str = "{ [a] : exists e : a = 32 e }";
1139 bset2 = isl_basic_set_read_from_str(ctx, str);
1140 subset = isl_basic_set_is_subset(bset, bset2);
1141 isl_basic_set_free(bset);
1142 isl_basic_set_free(bset2);
1143 if (subset < 0)
1144 return -1;
1145 if (!subset)
1146 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1147 return -1);
1149 return 0;
1152 /* Pairs of maps and the corresponding expected results of
1153 * isl_map_plain_unshifted_simple_hull.
1155 struct {
1156 const char *map;
1157 const char *hull;
1158 } plain_unshifted_simple_hull_tests[] = {
1159 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1160 "{ [i] -> [j] : i >= 1 }" },
1161 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1162 "(j mod 4 = 2 and k mod 6 = n) }",
1163 "{ [n] -> [i,j,k] : j mod 4 = 2 }" },
1166 /* Basic tests for isl_map_plain_unshifted_simple_hull.
1168 static int test_plain_unshifted_simple_hull(isl_ctx *ctx)
1170 int i;
1171 isl_map *map;
1172 isl_basic_map *hull, *expected;
1173 isl_bool equal;
1175 for (i = 0; i < ARRAY_SIZE(plain_unshifted_simple_hull_tests); ++i) {
1176 const char *str;
1177 str = plain_unshifted_simple_hull_tests[i].map;
1178 map = isl_map_read_from_str(ctx, str);
1179 str = plain_unshifted_simple_hull_tests[i].hull;
1180 expected = isl_basic_map_read_from_str(ctx, str);
1181 hull = isl_map_plain_unshifted_simple_hull(map);
1182 equal = isl_basic_map_is_equal(hull, expected);
1183 isl_basic_map_free(hull);
1184 isl_basic_map_free(expected);
1185 if (equal < 0)
1186 return -1;
1187 if (!equal)
1188 isl_die(ctx, isl_error_unknown, "unexpected hull",
1189 return -1);
1192 return 0;
1195 /* Pairs of sets and the corresponding expected results of
1196 * isl_set_unshifted_simple_hull.
1198 struct {
1199 const char *set;
1200 const char *hull;
1201 } unshifted_simple_hull_tests[] = {
1202 { "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1203 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1206 /* Basic tests for isl_set_unshifted_simple_hull.
1208 static int test_unshifted_simple_hull(isl_ctx *ctx)
1210 int i;
1211 isl_set *set;
1212 isl_basic_set *hull, *expected;
1213 isl_bool equal;
1215 for (i = 0; i < ARRAY_SIZE(unshifted_simple_hull_tests); ++i) {
1216 const char *str;
1217 str = unshifted_simple_hull_tests[i].set;
1218 set = isl_set_read_from_str(ctx, str);
1219 str = unshifted_simple_hull_tests[i].hull;
1220 expected = isl_basic_set_read_from_str(ctx, str);
1221 hull = isl_set_unshifted_simple_hull(set);
1222 equal = isl_basic_set_is_equal(hull, expected);
1223 isl_basic_set_free(hull);
1224 isl_basic_set_free(expected);
1225 if (equal < 0)
1226 return -1;
1227 if (!equal)
1228 isl_die(ctx, isl_error_unknown, "unexpected hull",
1229 return -1);
1232 return 0;
1235 static int test_simple_hull(struct isl_ctx *ctx)
1237 const char *str;
1238 isl_set *set;
1239 isl_basic_set *bset;
1240 isl_bool is_empty;
1242 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1243 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1244 set = isl_set_read_from_str(ctx, str);
1245 bset = isl_set_simple_hull(set);
1246 is_empty = isl_basic_set_is_empty(bset);
1247 isl_basic_set_free(bset);
1249 if (is_empty == isl_bool_error)
1250 return -1;
1252 if (is_empty == isl_bool_false)
1253 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1254 return -1);
1256 if (test_plain_unshifted_simple_hull(ctx) < 0)
1257 return -1;
1258 if (test_unshifted_simple_hull(ctx) < 0)
1259 return -1;
1261 return 0;
1264 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1266 char *filename;
1267 FILE *input;
1268 struct isl_basic_set *bset1, *bset2;
1269 struct isl_set *set;
1271 filename = get_filename(ctx, name, "polylib");
1272 assert(filename);
1273 input = fopen(filename, "r");
1274 assert(input);
1276 bset1 = isl_basic_set_read_from_file(ctx, input);
1277 bset2 = isl_basic_set_read_from_file(ctx, input);
1279 set = isl_basic_set_union(bset1, bset2);
1280 bset1 = isl_set_convex_hull(set);
1282 bset2 = isl_basic_set_read_from_file(ctx, input);
1284 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1286 isl_basic_set_free(bset1);
1287 isl_basic_set_free(bset2);
1288 free(filename);
1290 fclose(input);
1293 struct {
1294 const char *set;
1295 const char *hull;
1296 } convex_hull_tests[] = {
1297 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1298 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1299 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1300 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1301 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1302 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1303 "i2 <= 5 and i2 >= 4; "
1304 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1305 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1306 "i2 <= 5 + i0 and i2 >= i0 }" },
1307 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1308 "{ [x, y] : 1 = 0 }" },
1309 { "{ [x, y, z] : 0 <= x, y, z <= 10; [x, y, 0] : x >= 0 and y > 0; "
1310 "[x, y, 0] : x >= 0 and y < 0 }",
1311 "{ [x, y, z] : x >= 0 and 0 <= z <= 10 }" },
1314 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1316 int i;
1317 int orig_convex = ctx->opt->convex;
1318 ctx->opt->convex = convex;
1320 test_convex_hull_case(ctx, "convex0");
1321 test_convex_hull_case(ctx, "convex1");
1322 test_convex_hull_case(ctx, "convex2");
1323 test_convex_hull_case(ctx, "convex3");
1324 test_convex_hull_case(ctx, "convex4");
1325 test_convex_hull_case(ctx, "convex5");
1326 test_convex_hull_case(ctx, "convex6");
1327 test_convex_hull_case(ctx, "convex7");
1328 test_convex_hull_case(ctx, "convex8");
1329 test_convex_hull_case(ctx, "convex9");
1330 test_convex_hull_case(ctx, "convex10");
1331 test_convex_hull_case(ctx, "convex11");
1332 test_convex_hull_case(ctx, "convex12");
1333 test_convex_hull_case(ctx, "convex13");
1334 test_convex_hull_case(ctx, "convex14");
1335 test_convex_hull_case(ctx, "convex15");
1337 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1338 isl_set *set1, *set2;
1339 int equal;
1341 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1342 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1343 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1344 equal = isl_set_is_equal(set1, set2);
1345 isl_set_free(set1);
1346 isl_set_free(set2);
1348 if (equal < 0)
1349 return -1;
1350 if (!equal)
1351 isl_die(ctx, isl_error_unknown,
1352 "unexpected convex hull", return -1);
1355 ctx->opt->convex = orig_convex;
1357 return 0;
1360 static int test_convex_hull(isl_ctx *ctx)
1362 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1363 return -1;
1364 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1365 return -1;
1366 return 0;
1369 void test_gist_case(struct isl_ctx *ctx, const char *name)
1371 char *filename;
1372 FILE *input;
1373 struct isl_basic_set *bset1, *bset2;
1375 filename = get_filename(ctx, name, "polylib");
1376 assert(filename);
1377 input = fopen(filename, "r");
1378 assert(input);
1380 bset1 = isl_basic_set_read_from_file(ctx, input);
1381 bset2 = isl_basic_set_read_from_file(ctx, input);
1383 bset1 = isl_basic_set_gist(bset1, bset2);
1385 bset2 = isl_basic_set_read_from_file(ctx, input);
1387 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1389 isl_basic_set_free(bset1);
1390 isl_basic_set_free(bset2);
1391 free(filename);
1393 fclose(input);
1396 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1398 struct {
1399 const char *map;
1400 const char *context;
1401 const char *gist;
1402 } plain_gist_tests[] = {
1403 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1404 "{ [i] -> [j] : i >= 1 }",
1405 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1406 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1407 "(j mod 4 = 2 and k mod 6 = n) }",
1408 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1409 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1410 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1411 "{ [i] -> [j] : i > j }",
1412 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1415 /* Basic tests for isl_map_plain_gist_basic_map.
1417 static int test_plain_gist(isl_ctx *ctx)
1419 int i;
1421 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1422 const char *str;
1423 int equal;
1424 isl_map *map, *gist;
1425 isl_basic_map *context;
1427 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1428 str = plain_gist_tests[i].context;
1429 context = isl_basic_map_read_from_str(ctx, str);
1430 map = isl_map_plain_gist_basic_map(map, context);
1431 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1432 equal = isl_map_is_equal(map, gist);
1433 isl_map_free(map);
1434 isl_map_free(gist);
1435 if (equal < 0)
1436 return -1;
1437 if (!equal)
1438 isl_die(ctx, isl_error_unknown,
1439 "incorrect gist result", return -1);
1442 return 0;
1445 struct {
1446 const char *set;
1447 const char *context;
1448 const char *gist;
1449 } gist_tests[] = {
1450 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1451 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1452 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1453 "{ [a, b, c] : a <= 15 }" },
1454 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1455 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1456 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1457 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1458 { "{ [m, n, a, b] : a <= 2147 + n }",
1459 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1460 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1461 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1462 "b >= 0) }",
1463 "{ [m, n, ku, kl] }" },
1464 { "{ [a, a, b] : a >= 10 }",
1465 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1466 "{ [a, a, b] : a >= 10 }" },
1467 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1468 "{ [0, j] : j >= 0 }" },
1469 /* Check that no constraints on i6 are introduced in the gist */
1470 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1471 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1472 "5e0 <= 381 - t1 and i4 <= 1) }",
1473 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1474 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1475 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1476 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1477 "20e0 >= 1511 - 4t1 - 5i4) }" },
1478 /* Check that no constraints on i6 are introduced in the gist */
1479 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1480 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1481 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1482 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1483 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1484 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1485 "20e1 <= 1530 - 4t1 - 5i4 and "
1486 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1487 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1488 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1489 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1490 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1491 "10e0 <= -91 + 5i4 + 4i6 and "
1492 "10e0 >= -105 + 5i4 + 4i6) }",
1493 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1494 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1495 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1496 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1497 "{ [a, b, q, p] : b >= 1 + a }",
1498 "{ [a, b, q, p] : false }" },
1499 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1500 "[n] -> { [x] : x mod 32 = 0 }",
1501 "[n] -> { [x = n] }" },
1502 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1503 "{ [x] : x mod 2 = 0 }" },
1504 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1505 "{ [x] : x mod 128 = 0 }" },
1506 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1507 "{ [x] : x mod 3200 = 0 }" },
1508 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1509 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1510 "{ [a, b, c = a] }" },
1511 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1512 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1513 "{ [a, b, c = a] : a mod 3 = 0 }" },
1514 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1515 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1516 "{ [x] }" },
1517 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1518 "{ [x,y] : 1 <= y <= 3 }",
1519 "{ [x,y] }" },
1522 /* Check that isl_set_gist behaves as expected.
1524 * For the test cases in gist_tests, besides checking that the result
1525 * is as expected, also check that applying the gist operation does
1526 * not modify the input set (an earlier version of isl would do that) and
1527 * that the test case is consistent, i.e., that the gist has the same
1528 * intersection with the context as the input set.
1530 static int test_gist(struct isl_ctx *ctx)
1532 int i;
1533 const char *str;
1534 isl_basic_set *bset1, *bset2;
1535 isl_map *map1, *map2;
1536 int equal;
1538 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1539 int equal_input, equal_intersection;
1540 isl_set *set1, *set2, *copy, *context;
1542 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1543 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1544 copy = isl_set_copy(set1);
1545 set1 = isl_set_gist(set1, isl_set_copy(context));
1546 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1547 equal = isl_set_is_equal(set1, set2);
1548 isl_set_free(set1);
1549 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1550 equal_input = isl_set_is_equal(set1, copy);
1551 isl_set_free(copy);
1552 set1 = isl_set_intersect(set1, isl_set_copy(context));
1553 set2 = isl_set_intersect(set2, context);
1554 equal_intersection = isl_set_is_equal(set1, set2);
1555 isl_set_free(set2);
1556 isl_set_free(set1);
1557 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1558 return -1;
1559 if (!equal)
1560 isl_die(ctx, isl_error_unknown,
1561 "incorrect gist result", return -1);
1562 if (!equal_input)
1563 isl_die(ctx, isl_error_unknown,
1564 "gist modified input", return -1);
1565 if (!equal_input)
1566 isl_die(ctx, isl_error_unknown,
1567 "inconsistent gist test case", return -1);
1570 test_gist_case(ctx, "gist1");
1572 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1573 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1574 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1575 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1576 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1577 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1578 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1579 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1580 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1581 bset1 = isl_basic_set_read_from_str(ctx, str);
1582 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1583 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1584 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1585 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1586 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1587 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1588 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1589 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1590 bset2 = isl_basic_set_read_from_str(ctx, str);
1591 bset1 = isl_basic_set_gist(bset1, bset2);
1592 assert(bset1 && bset1->n_div == 0);
1593 isl_basic_set_free(bset1);
1595 /* Check that the integer divisions of the second disjunct
1596 * do not spread to the first disjunct.
1598 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1599 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1600 "(exists (e0 = [(-1 + t1)/16], "
1601 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1602 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1603 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1604 "o0 <= 4294967295 and t1 <= -1)) }";
1605 map1 = isl_map_read_from_str(ctx, str);
1606 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1607 map2 = isl_map_read_from_str(ctx, str);
1608 map1 = isl_map_gist(map1, map2);
1609 if (!map1)
1610 return -1;
1611 if (map1->n != 1)
1612 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1613 isl_map_free(map1); return -1);
1614 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1615 isl_die(ctx, isl_error_unknown, "expecting single div",
1616 isl_map_free(map1); return -1);
1617 isl_map_free(map1);
1619 if (test_plain_gist(ctx) < 0)
1620 return -1;
1622 return 0;
1625 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1627 isl_set *set, *set2;
1628 int equal;
1629 int one;
1631 set = isl_set_read_from_str(ctx, str);
1632 set = isl_set_coalesce(set);
1633 set2 = isl_set_read_from_str(ctx, str);
1634 equal = isl_set_is_equal(set, set2);
1635 one = set && set->n == 1;
1636 isl_set_free(set);
1637 isl_set_free(set2);
1639 if (equal < 0)
1640 return -1;
1641 if (!equal)
1642 isl_die(ctx, isl_error_unknown,
1643 "coalesced set not equal to input", return -1);
1644 if (check_one && !one)
1645 isl_die(ctx, isl_error_unknown,
1646 "coalesced set should not be a union", return -1);
1648 return 0;
1651 /* Inputs for coalescing tests with unbounded wrapping.
1652 * "str" is a string representation of the input set.
1653 * "single_disjunct" is set if we expect the result to consist of
1654 * a single disjunct.
1656 struct {
1657 int single_disjunct;
1658 const char *str;
1659 } coalesce_unbounded_tests[] = {
1660 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1661 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1662 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1663 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1664 "-10 <= y <= 0}" },
1665 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1666 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1667 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1668 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1671 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1672 * option turned off.
1674 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1676 int i;
1677 int r = 0;
1678 int bounded;
1680 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1681 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1683 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1684 const char *str = coalesce_unbounded_tests[i].str;
1685 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1686 if (test_coalesce_set(ctx, str, check_one) >= 0)
1687 continue;
1688 r = -1;
1689 break;
1692 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1694 return r;
1697 /* Inputs for coalescing tests.
1698 * "str" is a string representation of the input set.
1699 * "single_disjunct" is set if we expect the result to consist of
1700 * a single disjunct.
1702 struct {
1703 int single_disjunct;
1704 const char *str;
1705 } coalesce_tests[] = {
1706 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1707 "y >= x & x >= 2 & 5 >= y }" },
1708 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1709 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1710 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1711 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1712 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1713 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1714 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1715 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1716 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1717 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1718 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1719 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1720 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1721 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1722 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1723 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1724 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1725 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1726 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1727 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1728 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1729 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1730 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1731 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1732 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1733 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1734 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1735 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1736 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1737 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1738 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1739 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1740 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1741 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1742 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1743 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1744 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1745 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1746 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1747 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1748 "[o0, o1, o2, o3, o4, o5, o6]] : "
1749 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1750 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1751 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1752 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1753 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1754 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1755 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1756 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1757 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1758 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1759 "o6 >= i3 + i6 - o3 and M >= 0 and "
1760 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1761 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1762 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1763 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1764 "(o0 = 0 and M >= 2 and N >= 3) or "
1765 "(M = 0 and o0 = 0 and N >= 3) }" },
1766 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1767 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1768 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1769 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1770 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1771 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1772 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1773 "(y = 3 and x = 1) }" },
1774 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1775 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1776 "i1 <= M and i3 <= M and i4 <= M) or "
1777 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1778 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1779 "i4 <= -1 + M) }" },
1780 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1781 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1782 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1783 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1784 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1785 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1786 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1787 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1788 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1789 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1790 { 0, "{ [a, b] : exists e : 2e = a and "
1791 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1792 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1793 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1794 "j >= 1 and j' <= i + j - i' and i >= 1; "
1795 "[1, 1, 1, 1] }" },
1796 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1797 "[i,j] : exists a : j = 3a }" },
1798 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1799 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1800 "a >= 3) or "
1801 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1802 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1803 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1804 "c <= 6 + 8a and a >= 3; "
1805 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1806 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1807 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1808 "[x,0] : 3 <= x <= 4 }" },
1809 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1810 "[x,0] : 4 <= x <= 5 }" },
1811 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1812 "[x,0] : 3 <= x <= 5 }" },
1813 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1814 "[x,0] : 3 <= x <= 4 }" },
1815 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1816 "i1 <= 0; "
1817 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1818 { 1, "{ [0,0]; [1,1] }" },
1819 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1820 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1821 "ii <= k;"
1822 "[k, 0, k] : k <= 6 and k >= 1 }" },
1823 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1824 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1825 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1826 { 1, "[n] -> { [1] : n >= 0;"
1827 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1828 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1829 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1830 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1831 "2e0 <= x and 2e0 <= n);"
1832 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1833 "n >= 0) }" },
1834 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1835 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1836 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1837 "t1 = 1 }" },
1838 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1839 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1840 "[0, 0] }" },
1841 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1842 "t1 >= 13 and t1 <= 16);"
1843 "[t1] : t1 <= 15 and t1 >= 12 }" },
1844 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
1845 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
1846 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
1847 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
1848 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
1849 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
1850 "i <= 4j + 2 }" },
1851 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
1852 "(exists (e0 : 3e0 = -2 + c0)) }" },
1853 { 0, "[n, b0, t0] -> "
1854 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1855 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1856 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1857 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1858 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
1859 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
1860 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
1861 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
1862 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1863 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1864 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1865 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
1866 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
1867 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
1868 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
1869 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
1870 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
1871 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
1872 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
1873 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
1874 { 0, "{ [i0, i1, i2] : "
1875 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
1876 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
1877 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
1878 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
1879 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
1880 "32e0 <= 31 + i0)) or "
1881 "i0 >= 0 }" },
1882 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
1883 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
1884 "2*floor((c)/2) = c and 0 <= a <= 192;"
1885 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
1887 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
1888 "(0 <= a <= b <= n) }" },
1889 { 1, "{ [a, b] : 0 <= a <= 2 and b >= 0 and "
1890 "((0 < b <= 13) or (2*floor((a + b)/2) >= -5 + a + 2b)) }" },
1891 { 1, "{ [a] : (2 <= a <= 5) or (a mod 2 = 1 and 1 <= a <= 5) }" },
1892 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
1893 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1894 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
1895 "b = 3 and 9e0 <= -19 + 2c)) }" },
1896 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
1897 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1898 "(a = 4 and b = 3 and "
1899 "9*floor((-16 + 2c)/9) <= -19 + 2c) }" },
1900 { 0, "{ [a, b, c] : (b <= 2 and b <= -2 + a) or "
1901 "(b = -1 + a and 0 < a <= 3 and "
1902 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1903 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
1904 "b = 3 and 9e0 <= -19 + 2c)) }" },
1905 { 1, "{ [y, x] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
1906 "[1, 0] }" },
1907 { 1, "{ [x, y] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
1908 "[0, 1] }" },
1909 { 1, "{ [1, y] : -1 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
1910 { 1, "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
1911 { 1, "{ [x, y] : 0 <= x <= 10 and x - 4*floor(x/4) <= 1 and y <= 0; "
1912 "[x, y] : 0 <= x <= 10 and x - 4*floor(x/4) > 1 and y <= 0; "
1913 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) <= 1 and 0 < y; "
1914 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) > 1 and 0 < y }" },
1915 { 1, "{ [x, 0] : 0 <= x <= 10 and x mod 2 = 0; "
1916 "[x, 0] : 0 <= x <= 10 and x mod 2 = 1; "
1917 "[x, y] : 0 <= x <= 10 and 1 <= y <= 10 }" },
1918 { 1, "{ [a] : a <= 8 and "
1919 "(a mod 10 = 7 or a mod 10 = 8 or a mod 10 = 9) }" },
1920 { 1, "{ [x, y] : 2y = -x and x <= 0 or "
1921 "x <= -1 and 2y <= -x - 1 and 2y >= x - 1 }" },
1922 { 0, "{ [x, y] : 2y = -x and x <= 0 or "
1923 "x <= -2 and 2y <= -x - 1 and 2y >= x - 1 }" },
1924 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
1925 "(a < 0 and 3*floor((a)/3) < a) }" },
1926 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
1927 "(a < -1 and 3*floor((a)/3) < a) }" },
1928 { 1, "{ [a, b] : a <= 1024 and b >= 0 and "
1929 "((-31 - a + b <= 32*floor((-1 - a)/32) <= -33 + b and "
1930 "32*floor((-1 - a)/32) <= -16 + b + 16*floor((-1 - a)/16))"
1931 "or (2 <= a <= 15 and b < a)) }" },
1932 { 1, "{ [a] : a > 0 and ((16*floor((a)/16) < a and "
1933 "32*floor((a)/32) < a) or a <= 15) }" },
1934 { 1, "{ [a, b, c, d] : (-a + d) mod 64 = 0 and a <= 8 and b <= 1 and "
1935 "10 - a <= c <= 3 and d >= 5 and 9 - 64b <= d <= 70;"
1936 "[a, b = 1, c, d] : (-a + d) mod 64 = 0 and a <= 8 and c >= 4 and "
1937 "10 - a <= c <= 5 and 5 <= d <= 73 - c }" },
1938 { 1, "[n, m] -> { S_0[i] : (-n + i) mod 3 = 0 and m >= 3 + n and "
1939 "i >= n and 3*floor((2 + n + 2m)/3) <= n + 3m - i; "
1940 "S_0[n] : n <= m <= 2 + n }" },
1941 { 1, "{ [a, b] : exists (e0: 0 <= a <= 1 and b >= 0 and "
1942 "2e0 >= -5 + a + 2b and 2e0 >= -1 + a + b and "
1943 "2e0 <= a + b); "
1944 "[a, b] : exists (e0: 0 <= a <= 1 and 2e0 >= -5 + a + 2b and "
1945 "2e0 >= -1 - a + b and 2e0 <= -a + b and "
1946 "2e0 < -a + 2b) }" },
1947 { 1, "{ [i, j, i - 8j] : 8 <= i <= 63 and -7 + i <= 8j <= i; "
1948 "[i, 0, i] : 0 <= i <= 7 }" },
1951 /* A specialized coalescing test case that would result
1952 * in a segmentation fault or a failed assertion in earlier versions of isl.
1954 static int test_coalesce_special(struct isl_ctx *ctx)
1956 const char *str;
1957 isl_map *map1, *map2;
1959 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1960 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
1961 "(y = 201 and o1 <= 239 and o1 >= 212) or "
1962 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
1963 "o1 <= 239 and o1 >= 212)) or "
1964 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
1965 "o1 <= 241 and o1 >= 240));"
1966 "[S_L220_OUT[] -> T7[]] -> "
1967 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
1968 "(y = 2 and o1 <= 241 and o1 >= 212) or "
1969 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
1970 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
1971 map1 = isl_map_read_from_str(ctx, str);
1972 map1 = isl_map_align_divs_internal(map1);
1973 map1 = isl_map_coalesce(map1);
1974 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1975 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
1976 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
1977 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
1978 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
1979 map2 = isl_map_read_from_str(ctx, str);
1980 map2 = isl_map_union(map2, map1);
1981 map2 = isl_map_align_divs_internal(map2);
1982 map2 = isl_map_coalesce(map2);
1983 isl_map_free(map2);
1984 if (!map2)
1985 return -1;
1987 return 0;
1990 /* A specialized coalescing test case that would result in an assertion
1991 * in an earlier version of isl.
1992 * The explicit call to isl_basic_set_union prevents the implicit
1993 * equality constraints in the first basic map from being detected prior
1994 * to the call to isl_set_coalesce, at least at the point
1995 * where this test case was introduced.
1997 static int test_coalesce_special2(struct isl_ctx *ctx)
1999 const char *str;
2000 isl_basic_set *bset1, *bset2;
2001 isl_set *set;
2003 str = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
2004 bset1 = isl_basic_set_read_from_str(ctx, str);
2005 str = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }" ;
2006 bset2 = isl_basic_set_read_from_str(ctx, str);
2007 set = isl_basic_set_union(bset1, bset2);
2008 set = isl_set_coalesce(set);
2009 isl_set_free(set);
2011 if (!set)
2012 return -1;
2013 return 0;
2016 /* Check that calling isl_set_coalesce does not leave other sets
2017 * that may share some information with the input to isl_set_coalesce
2018 * in an inconsistent state.
2019 * In particular, older versions of isl would modify all copies
2020 * of the basic sets in the isl_set_coalesce input in a way
2021 * that could leave them in an inconsistent state.
2022 * The result of printing any other set containing one of these
2023 * basic sets would then result in an invalid set description.
2025 static int test_coalesce_special3(isl_ctx *ctx)
2027 const char *str;
2028 char *s;
2029 isl_set *set1, *set2;
2030 isl_printer *p;
2032 set1 = isl_set_read_from_str(ctx, "{ [0, 0, 0] }");
2033 str = "{ [a, b, a + b] : a >= 0 and b >= 0 and 0 < a + b }";
2034 set2 = isl_set_read_from_str(ctx, str);
2035 set1 = isl_set_union(set1, isl_set_copy(set2));
2036 set1 = isl_set_coalesce(set1);
2037 isl_set_free(set1);
2039 p = isl_printer_to_str(ctx);
2040 p = isl_printer_print_set(p, set2);
2041 isl_set_free(set2);
2042 s = isl_printer_get_str(p);
2043 isl_printer_free(p);
2044 set1 = isl_set_read_from_str(ctx, s);
2045 free(s);
2046 isl_set_free(set1);
2048 if (!set1)
2049 return -1;
2051 return 0;
2054 /* Test the functionality of isl_set_coalesce.
2055 * That is, check that the output is always equal to the input
2056 * and in some cases that the result consists of a single disjunct.
2058 static int test_coalesce(struct isl_ctx *ctx)
2060 int i;
2062 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
2063 const char *str = coalesce_tests[i].str;
2064 int check_one = coalesce_tests[i].single_disjunct;
2065 if (test_coalesce_set(ctx, str, check_one) < 0)
2066 return -1;
2069 if (test_coalesce_unbounded_wrapping(ctx) < 0)
2070 return -1;
2071 if (test_coalesce_special(ctx) < 0)
2072 return -1;
2073 if (test_coalesce_special2(ctx) < 0)
2074 return -1;
2075 if (test_coalesce_special3(ctx) < 0)
2076 return -1;
2078 return 0;
2081 /* Construct a representation of the graph on the right of Figure 1
2082 * in "Computing the Transitive Closure of a Union of
2083 * Affine Integer Tuple Relations".
2085 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2087 isl_set *dom;
2088 isl_map *up, *right;
2090 dom = isl_set_read_from_str(ctx,
2091 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2092 "2 x - 3 y + 3 >= 0 }");
2093 right = isl_map_read_from_str(ctx,
2094 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2095 up = isl_map_read_from_str(ctx,
2096 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2097 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2098 right = isl_map_intersect_range(right, isl_set_copy(dom));
2099 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2100 up = isl_map_intersect_range(up, dom);
2101 return isl_map_union(up, right);
2104 /* Construct a representation of the power of the graph
2105 * on the right of Figure 1 in "Computing the Transitive Closure of
2106 * a Union of Affine Integer Tuple Relations".
2108 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2110 return isl_map_read_from_str(ctx,
2111 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2112 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2113 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2116 /* Construct a representation of the transitive closure of the graph
2117 * on the right of Figure 1 in "Computing the Transitive Closure of
2118 * a Union of Affine Integer Tuple Relations".
2120 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2122 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2125 static int test_closure(isl_ctx *ctx)
2127 const char *str;
2128 isl_map *map, *map2;
2129 int exact, equal;
2131 /* COCOA example 1 */
2132 map = isl_map_read_from_str(ctx,
2133 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2134 "1 <= i and i < n and 1 <= j and j < n or "
2135 "i2 = i + 1 and j2 = j - 1 and "
2136 "1 <= i and i < n and 2 <= j and j <= n }");
2137 map = isl_map_power(map, &exact);
2138 assert(exact);
2139 isl_map_free(map);
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_transitive_closure(map, &exact);
2148 assert(exact);
2149 map2 = isl_map_read_from_str(ctx,
2150 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2151 "1 <= i and i < n and 1 <= j and j <= n and "
2152 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2153 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2154 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2155 assert(isl_map_is_equal(map, map2));
2156 isl_map_free(map2);
2157 isl_map_free(map);
2159 map = isl_map_read_from_str(ctx,
2160 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2161 " 0 <= y and y <= n }");
2162 map = isl_map_transitive_closure(map, &exact);
2163 map2 = isl_map_read_from_str(ctx,
2164 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2165 " 0 <= y and y <= n }");
2166 assert(isl_map_is_equal(map, map2));
2167 isl_map_free(map2);
2168 isl_map_free(map);
2170 /* COCOA example 2 */
2171 map = isl_map_read_from_str(ctx,
2172 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2173 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2174 "i2 = i + 2 and j2 = j - 2 and "
2175 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2176 map = isl_map_transitive_closure(map, &exact);
2177 assert(exact);
2178 map2 = isl_map_read_from_str(ctx,
2179 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2180 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2181 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2182 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2183 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2184 assert(isl_map_is_equal(map, map2));
2185 isl_map_free(map);
2186 isl_map_free(map2);
2188 /* COCOA Fig.2 left */
2189 map = isl_map_read_from_str(ctx,
2190 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2191 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2192 "j <= n or "
2193 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2194 "j <= 2 i - 3 and j <= n - 2 or "
2195 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2196 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2197 map = isl_map_transitive_closure(map, &exact);
2198 assert(exact);
2199 isl_map_free(map);
2201 /* COCOA Fig.2 right */
2202 map = isl_map_read_from_str(ctx,
2203 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2204 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2205 "j <= n or "
2206 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2207 "j <= 2 i - 4 and j <= n - 3 or "
2208 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2209 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2210 map = isl_map_power(map, &exact);
2211 assert(exact);
2212 isl_map_free(map);
2214 /* COCOA Fig.2 right */
2215 map = isl_map_read_from_str(ctx,
2216 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2217 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2218 "j <= n or "
2219 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2220 "j <= 2 i - 4 and j <= n - 3 or "
2221 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2222 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2223 map = isl_map_transitive_closure(map, &exact);
2224 assert(exact);
2225 map2 = isl_map_read_from_str(ctx,
2226 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2227 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2228 "j <= n and 3 + i + 2 j <= 3 n and "
2229 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2230 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2231 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2232 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2233 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2234 assert(isl_map_is_equal(map, map2));
2235 isl_map_free(map2);
2236 isl_map_free(map);
2238 map = cocoa_fig_1_right_graph(ctx);
2239 map = isl_map_transitive_closure(map, &exact);
2240 assert(exact);
2241 map2 = cocoa_fig_1_right_tc(ctx);
2242 assert(isl_map_is_equal(map, map2));
2243 isl_map_free(map2);
2244 isl_map_free(map);
2246 map = cocoa_fig_1_right_graph(ctx);
2247 map = isl_map_power(map, &exact);
2248 map2 = cocoa_fig_1_right_power(ctx);
2249 equal = isl_map_is_equal(map, map2);
2250 isl_map_free(map2);
2251 isl_map_free(map);
2252 if (equal < 0)
2253 return -1;
2254 if (!exact)
2255 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2256 if (!equal)
2257 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2259 /* COCOA Theorem 1 counter example */
2260 map = isl_map_read_from_str(ctx,
2261 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2262 "i2 = 1 and j2 = j or "
2263 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2264 map = isl_map_transitive_closure(map, &exact);
2265 assert(exact);
2266 isl_map_free(map);
2268 map = isl_map_read_from_str(ctx,
2269 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2270 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2271 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2272 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2273 map = isl_map_transitive_closure(map, &exact);
2274 assert(exact);
2275 isl_map_free(map);
2277 /* Kelly et al 1996, fig 12 */
2278 map = isl_map_read_from_str(ctx,
2279 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2280 "1 <= i,j,j+1 <= n or "
2281 "j = n and j2 = 1 and i2 = i + 1 and "
2282 "1 <= i,i+1 <= n }");
2283 map = isl_map_transitive_closure(map, &exact);
2284 assert(exact);
2285 map2 = isl_map_read_from_str(ctx,
2286 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2287 "1 <= i <= n and i = i2 or "
2288 "1 <= i < i2 <= n and 1 <= j <= n and "
2289 "1 <= j2 <= n }");
2290 assert(isl_map_is_equal(map, map2));
2291 isl_map_free(map2);
2292 isl_map_free(map);
2294 /* Omega's closure4 */
2295 map = isl_map_read_from_str(ctx,
2296 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2297 "1 <= x,y <= 10 or "
2298 "x2 = x + 1 and y2 = y and "
2299 "1 <= x <= 20 && 5 <= y <= 15 }");
2300 map = isl_map_transitive_closure(map, &exact);
2301 assert(exact);
2302 isl_map_free(map);
2304 map = isl_map_read_from_str(ctx,
2305 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2306 map = isl_map_transitive_closure(map, &exact);
2307 assert(!exact);
2308 map2 = isl_map_read_from_str(ctx,
2309 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2310 assert(isl_map_is_equal(map, map2));
2311 isl_map_free(map);
2312 isl_map_free(map2);
2314 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2315 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2316 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2317 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2318 map = isl_map_read_from_str(ctx, str);
2319 map = isl_map_transitive_closure(map, &exact);
2320 assert(exact);
2321 map2 = isl_map_read_from_str(ctx, str);
2322 assert(isl_map_is_equal(map, map2));
2323 isl_map_free(map);
2324 isl_map_free(map2);
2326 str = "{[0] -> [1]; [2] -> [3]}";
2327 map = isl_map_read_from_str(ctx, str);
2328 map = isl_map_transitive_closure(map, &exact);
2329 assert(exact);
2330 map2 = isl_map_read_from_str(ctx, str);
2331 assert(isl_map_is_equal(map, map2));
2332 isl_map_free(map);
2333 isl_map_free(map2);
2335 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2336 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2337 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2338 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2339 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2340 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2341 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2342 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2343 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2344 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2345 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2346 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2347 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 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, 2]] -> "
2351 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2352 "exists (e0 = [(3 - n)/3]: i5 >= 1 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 map = isl_map_read_from_str(ctx, str);
2356 map = isl_map_transitive_closure(map, NULL);
2357 assert(map);
2358 isl_map_free(map);
2360 return 0;
2363 static int test_lex(struct isl_ctx *ctx)
2365 isl_space *dim;
2366 isl_map *map;
2367 int empty;
2369 dim = isl_space_set_alloc(ctx, 0, 0);
2370 map = isl_map_lex_le(dim);
2371 empty = isl_map_is_empty(map);
2372 isl_map_free(map);
2374 if (empty < 0)
2375 return -1;
2376 if (empty)
2377 isl_die(ctx, isl_error_unknown,
2378 "expecting non-empty result", return -1);
2380 return 0;
2383 /* Inputs for isl_map_lexmin tests.
2384 * "map" is the input and "lexmin" is the expected result.
2386 struct {
2387 const char *map;
2388 const char *lexmin;
2389 } lexmin_tests [] = {
2390 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
2391 "{ [x] -> [5] : 6 <= x <= 8; "
2392 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
2393 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
2394 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
2395 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
2396 "{ [x] -> [y] : (4y = x and x >= 0) or "
2397 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2398 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2399 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
2400 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
2401 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
2402 /* Check that empty pieces are properly combined. */
2403 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2404 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2405 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
2406 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2407 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2408 "x >= 4 }" },
2409 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
2410 "a <= 255 and c <= 255 and d <= 255 - j and "
2411 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
2412 "247d <= 247 + k - j and 247d <= 247 + k - b and "
2413 "247d <= 247 + i and 248 - b <= 248d <= c and "
2414 "254d >= i - a + b and 254d >= -a + b and "
2415 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
2416 "{ [i, k, j] -> "
2417 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
2418 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
2419 "247j >= 62738 - i and 509j <= 129795 + i and "
2420 "742j >= 188724 - i; "
2421 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
2422 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
2423 "16*floor((8 + b)/16) <= 7 + b; "
2424 "[a] -> [1] }",
2425 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
2426 "[a] -> [b = 0] : 0 < a <= 509 }" },
2427 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
2428 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
2431 static int test_lexmin(struct isl_ctx *ctx)
2433 int i;
2434 int equal;
2435 const char *str;
2436 isl_basic_map *bmap;
2437 isl_map *map, *map2;
2438 isl_set *set;
2439 isl_set *set2;
2440 isl_pw_multi_aff *pma;
2442 str = "[p0, p1] -> { [] -> [] : "
2443 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2444 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2445 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2446 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2447 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2448 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2449 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2450 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2451 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2452 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2453 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2454 map = isl_map_read_from_str(ctx, str);
2455 map = isl_map_lexmin(map);
2456 isl_map_free(map);
2458 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2459 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2460 set = isl_set_read_from_str(ctx, str);
2461 set = isl_set_lexmax(set);
2462 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2463 set2 = isl_set_read_from_str(ctx, str);
2464 set = isl_set_intersect(set, set2);
2465 assert(!isl_set_is_empty(set));
2466 isl_set_free(set);
2468 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
2469 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
2470 map = isl_map_lexmin(map);
2471 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
2472 equal = isl_map_is_equal(map, map2);
2473 isl_map_free(map);
2474 isl_map_free(map2);
2476 if (equal < 0)
2477 return -1;
2478 if (!equal)
2479 isl_die(ctx, isl_error_unknown,
2480 "unexpected result", return -1);
2483 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2484 " 8i' <= i and 8i' >= -7 + i }";
2485 bmap = isl_basic_map_read_from_str(ctx, str);
2486 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2487 map2 = isl_map_from_pw_multi_aff(pma);
2488 map = isl_map_from_basic_map(bmap);
2489 assert(isl_map_is_equal(map, map2));
2490 isl_map_free(map);
2491 isl_map_free(map2);
2493 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2494 " 8i' <= i and 8i' >= -7 + i }";
2495 set = isl_set_read_from_str(ctx, str);
2496 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2497 set2 = isl_set_from_pw_multi_aff(pma);
2498 equal = isl_set_is_equal(set, set2);
2499 isl_set_free(set);
2500 isl_set_free(set2);
2501 if (equal < 0)
2502 return -1;
2503 if (!equal)
2504 isl_die(ctx, isl_error_unknown,
2505 "unexpected difference between set and "
2506 "piecewise affine expression", return -1);
2508 return 0;
2511 /* A specialized isl_set_min_val test case that would return the wrong result
2512 * in earlier versions of isl.
2513 * The explicit call to isl_basic_set_union prevents the second basic set
2514 * from being determined to be empty prior to the call to isl_set_min_val,
2515 * at least at the point where this test case was introduced.
2517 static int test_min_special(isl_ctx *ctx)
2519 const char *str;
2520 isl_basic_set *bset1, *bset2;
2521 isl_set *set;
2522 isl_aff *obj;
2523 isl_val *res;
2524 int ok;
2526 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
2527 bset1 = isl_basic_set_read_from_str(ctx, str);
2528 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
2529 bset2 = isl_basic_set_read_from_str(ctx, str);
2530 set = isl_basic_set_union(bset1, bset2);
2531 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
2533 res = isl_set_min_val(set, obj);
2534 ok = isl_val_cmp_si(res, 5) == 0;
2536 isl_aff_free(obj);
2537 isl_set_free(set);
2538 isl_val_free(res);
2540 if (!res)
2541 return -1;
2542 if (!ok)
2543 isl_die(ctx, isl_error_unknown, "unexpected minimum",
2544 return -1);
2546 return 0;
2549 /* A specialized isl_set_min_val test case that would return an error
2550 * in earlier versions of isl.
2552 static int test_min_special2(isl_ctx *ctx)
2554 const char *str;
2555 isl_basic_set *bset;
2556 isl_aff *obj;
2557 isl_val *res;
2559 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
2560 bset = isl_basic_set_read_from_str(ctx, str);
2562 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
2564 res = isl_basic_set_max_val(bset, obj);
2566 isl_basic_set_free(bset);
2567 isl_aff_free(obj);
2568 isl_val_free(res);
2570 if (!res)
2571 return -1;
2573 return 0;
2576 struct {
2577 const char *set;
2578 const char *obj;
2579 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2580 __isl_keep isl_aff *obj);
2581 const char *res;
2582 } opt_tests[] = {
2583 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2584 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2585 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2586 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2587 &isl_set_max_val, "30" },
2591 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2592 * In particular, check the results on non-convex inputs.
2594 static int test_min(struct isl_ctx *ctx)
2596 int i;
2597 isl_set *set;
2598 isl_aff *obj;
2599 isl_val *val, *res;
2600 isl_bool ok;
2602 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2603 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2604 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2605 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2606 val = opt_tests[i].fn(set, obj);
2607 ok = isl_val_eq(res, val);
2608 isl_val_free(res);
2609 isl_val_free(val);
2610 isl_aff_free(obj);
2611 isl_set_free(set);
2613 if (ok < 0)
2614 return -1;
2615 if (!ok)
2616 isl_die(ctx, isl_error_unknown,
2617 "unexpected optimum", return -1);
2620 if (test_min_special(ctx) < 0)
2621 return -1;
2622 if (test_min_special2(ctx) < 0)
2623 return -1;
2625 return 0;
2628 struct must_may {
2629 isl_map *must;
2630 isl_map *may;
2633 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2634 void *dep_user, void *user)
2636 struct must_may *mm = (struct must_may *)user;
2638 if (must)
2639 mm->must = isl_map_union(mm->must, dep);
2640 else
2641 mm->may = isl_map_union(mm->may, dep);
2643 return isl_stat_ok;
2646 static int common_space(void *first, void *second)
2648 int depth = *(int *)first;
2649 return 2 * depth;
2652 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2654 isl_map *map2;
2655 int equal;
2657 if (!map)
2658 return -1;
2660 map2 = isl_map_read_from_str(map->ctx, str);
2661 equal = isl_map_is_equal(map, map2);
2662 isl_map_free(map2);
2664 return equal;
2667 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2669 int equal;
2671 equal = map_is_equal(map, str);
2672 if (equal < 0)
2673 return -1;
2674 if (!equal)
2675 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2676 "result not as expected", return -1);
2677 return 0;
2680 static int test_dep(struct isl_ctx *ctx)
2682 const char *str;
2683 isl_space *dim;
2684 isl_map *map;
2685 isl_access_info *ai;
2686 isl_flow *flow;
2687 int depth;
2688 struct must_may mm;
2690 depth = 3;
2692 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2693 map = isl_map_read_from_str(ctx, str);
2694 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2696 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2697 map = isl_map_read_from_str(ctx, str);
2698 ai = isl_access_info_add_source(ai, map, 1, &depth);
2700 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2701 map = isl_map_read_from_str(ctx, str);
2702 ai = isl_access_info_add_source(ai, map, 1, &depth);
2704 flow = isl_access_info_compute_flow(ai);
2705 dim = isl_space_alloc(ctx, 0, 3, 3);
2706 mm.must = isl_map_empty(isl_space_copy(dim));
2707 mm.may = isl_map_empty(dim);
2709 isl_flow_foreach(flow, collect_must_may, &mm);
2711 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2712 " [1,10,0] -> [2,5,0] }";
2713 assert(map_is_equal(mm.must, str));
2714 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2715 assert(map_is_equal(mm.may, str));
2717 isl_map_free(mm.must);
2718 isl_map_free(mm.may);
2719 isl_flow_free(flow);
2722 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2723 map = isl_map_read_from_str(ctx, str);
2724 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2726 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2727 map = isl_map_read_from_str(ctx, str);
2728 ai = isl_access_info_add_source(ai, map, 1, &depth);
2730 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2731 map = isl_map_read_from_str(ctx, str);
2732 ai = isl_access_info_add_source(ai, map, 0, &depth);
2734 flow = isl_access_info_compute_flow(ai);
2735 dim = isl_space_alloc(ctx, 0, 3, 3);
2736 mm.must = isl_map_empty(isl_space_copy(dim));
2737 mm.may = isl_map_empty(dim);
2739 isl_flow_foreach(flow, collect_must_may, &mm);
2741 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2742 assert(map_is_equal(mm.must, str));
2743 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2744 assert(map_is_equal(mm.may, str));
2746 isl_map_free(mm.must);
2747 isl_map_free(mm.may);
2748 isl_flow_free(flow);
2751 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2752 map = isl_map_read_from_str(ctx, str);
2753 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2755 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2756 map = isl_map_read_from_str(ctx, str);
2757 ai = isl_access_info_add_source(ai, map, 0, &depth);
2759 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2760 map = isl_map_read_from_str(ctx, str);
2761 ai = isl_access_info_add_source(ai, map, 0, &depth);
2763 flow = isl_access_info_compute_flow(ai);
2764 dim = isl_space_alloc(ctx, 0, 3, 3);
2765 mm.must = isl_map_empty(isl_space_copy(dim));
2766 mm.may = isl_map_empty(dim);
2768 isl_flow_foreach(flow, collect_must_may, &mm);
2770 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2771 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2772 assert(map_is_equal(mm.may, str));
2773 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2774 assert(map_is_equal(mm.must, str));
2776 isl_map_free(mm.must);
2777 isl_map_free(mm.may);
2778 isl_flow_free(flow);
2781 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2782 map = isl_map_read_from_str(ctx, str);
2783 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2785 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2786 map = isl_map_read_from_str(ctx, str);
2787 ai = isl_access_info_add_source(ai, map, 0, &depth);
2789 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2790 map = isl_map_read_from_str(ctx, str);
2791 ai = isl_access_info_add_source(ai, map, 0, &depth);
2793 flow = isl_access_info_compute_flow(ai);
2794 dim = isl_space_alloc(ctx, 0, 3, 3);
2795 mm.must = isl_map_empty(isl_space_copy(dim));
2796 mm.may = isl_map_empty(dim);
2798 isl_flow_foreach(flow, collect_must_may, &mm);
2800 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2801 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2802 assert(map_is_equal(mm.may, str));
2803 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2804 assert(map_is_equal(mm.must, str));
2806 isl_map_free(mm.must);
2807 isl_map_free(mm.may);
2808 isl_flow_free(flow);
2811 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2812 map = isl_map_read_from_str(ctx, str);
2813 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2815 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2816 map = isl_map_read_from_str(ctx, str);
2817 ai = isl_access_info_add_source(ai, map, 0, &depth);
2819 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2820 map = isl_map_read_from_str(ctx, str);
2821 ai = isl_access_info_add_source(ai, map, 0, &depth);
2823 flow = isl_access_info_compute_flow(ai);
2824 dim = isl_space_alloc(ctx, 0, 3, 3);
2825 mm.must = isl_map_empty(isl_space_copy(dim));
2826 mm.may = isl_map_empty(dim);
2828 isl_flow_foreach(flow, collect_must_may, &mm);
2830 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2831 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2832 assert(map_is_equal(mm.may, str));
2833 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2834 assert(map_is_equal(mm.must, str));
2836 isl_map_free(mm.must);
2837 isl_map_free(mm.may);
2838 isl_flow_free(flow);
2841 depth = 5;
2843 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2844 map = isl_map_read_from_str(ctx, str);
2845 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2847 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2848 map = isl_map_read_from_str(ctx, str);
2849 ai = isl_access_info_add_source(ai, map, 1, &depth);
2851 flow = isl_access_info_compute_flow(ai);
2852 dim = isl_space_alloc(ctx, 0, 5, 5);
2853 mm.must = isl_map_empty(isl_space_copy(dim));
2854 mm.may = isl_map_empty(dim);
2856 isl_flow_foreach(flow, collect_must_may, &mm);
2858 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2859 assert(map_is_equal(mm.must, str));
2860 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2861 assert(map_is_equal(mm.may, str));
2863 isl_map_free(mm.must);
2864 isl_map_free(mm.may);
2865 isl_flow_free(flow);
2867 return 0;
2870 /* Check that the dependence analysis proceeds without errors.
2871 * Earlier versions of isl would break down during the analysis
2872 * due to the use of the wrong spaces.
2874 static int test_flow(isl_ctx *ctx)
2876 const char *str;
2877 isl_union_map *access, *schedule;
2878 isl_union_map *must_dep, *may_dep;
2879 int r;
2881 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2882 access = isl_union_map_read_from_str(ctx, str);
2883 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2884 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2885 "S2[] -> [1,0,0,0]; "
2886 "S3[] -> [-1,0,0,0] }";
2887 schedule = isl_union_map_read_from_str(ctx, str);
2888 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2889 isl_union_map_copy(access), schedule,
2890 &must_dep, &may_dep, NULL, NULL);
2891 isl_union_map_free(may_dep);
2892 isl_union_map_free(must_dep);
2894 return r;
2897 struct {
2898 const char *map;
2899 int sv;
2900 } sv_tests[] = {
2901 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2902 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2903 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2904 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2905 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2906 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2907 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2908 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2909 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2912 int test_sv(isl_ctx *ctx)
2914 isl_union_map *umap;
2915 int i;
2916 int sv;
2918 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2919 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2920 sv = isl_union_map_is_single_valued(umap);
2921 isl_union_map_free(umap);
2922 if (sv < 0)
2923 return -1;
2924 if (sv_tests[i].sv && !sv)
2925 isl_die(ctx, isl_error_internal,
2926 "map not detected as single valued", return -1);
2927 if (!sv_tests[i].sv && sv)
2928 isl_die(ctx, isl_error_internal,
2929 "map detected as single valued", return -1);
2932 return 0;
2935 struct {
2936 const char *str;
2937 int bijective;
2938 } bijective_tests[] = {
2939 { "[N,M]->{[i,j] -> [i]}", 0 },
2940 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
2941 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
2942 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
2943 { "[N,M]->{[i,j] -> [j,i]}", 1 },
2944 { "[N,M]->{[i,j] -> [i+j]}", 0 },
2945 { "[N,M]->{[i,j] -> []}", 0 },
2946 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
2947 { "[N,M]->{[i,j] -> [2i]}", 0 },
2948 { "[N,M]->{[i,j] -> [i,i]}", 0 },
2949 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
2950 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
2951 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
2954 static int test_bijective(struct isl_ctx *ctx)
2956 isl_map *map;
2957 int i;
2958 int bijective;
2960 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
2961 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
2962 bijective = isl_map_is_bijective(map);
2963 isl_map_free(map);
2964 if (bijective < 0)
2965 return -1;
2966 if (bijective_tests[i].bijective && !bijective)
2967 isl_die(ctx, isl_error_internal,
2968 "map not detected as bijective", return -1);
2969 if (!bijective_tests[i].bijective && bijective)
2970 isl_die(ctx, isl_error_internal,
2971 "map detected as bijective", return -1);
2974 return 0;
2977 /* Inputs for isl_pw_qpolynomial_gist tests.
2978 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2980 struct {
2981 const char *pwqp;
2982 const char *set;
2983 const char *gist;
2984 } pwqp_gist_tests[] = {
2985 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2986 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2987 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2988 "{ [i] -> -1/2 + 1/2 * i }" },
2989 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2992 static int test_pwqp(struct isl_ctx *ctx)
2994 int i;
2995 const char *str;
2996 isl_set *set;
2997 isl_pw_qpolynomial *pwqp1, *pwqp2;
2998 int equal;
3000 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3001 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3003 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
3004 isl_dim_in, 1, 1);
3006 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3007 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3009 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3011 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3013 isl_pw_qpolynomial_free(pwqp1);
3015 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
3016 str = pwqp_gist_tests[i].pwqp;
3017 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3018 str = pwqp_gist_tests[i].set;
3019 set = isl_set_read_from_str(ctx, str);
3020 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
3021 str = pwqp_gist_tests[i].gist;
3022 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3023 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3024 equal = isl_pw_qpolynomial_is_zero(pwqp1);
3025 isl_pw_qpolynomial_free(pwqp1);
3027 if (equal < 0)
3028 return -1;
3029 if (!equal)
3030 isl_die(ctx, isl_error_unknown,
3031 "unexpected result", return -1);
3034 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
3035 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3036 str = "{ [i] -> ([(2 * [i/2])/5]) }";
3037 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3039 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3041 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3043 isl_pw_qpolynomial_free(pwqp1);
3045 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
3046 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3047 str = "{ [x] -> x }";
3048 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3050 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3052 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3054 isl_pw_qpolynomial_free(pwqp1);
3056 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3057 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3058 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3059 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3060 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3061 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3062 isl_pw_qpolynomial_free(pwqp1);
3064 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3065 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3066 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3067 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3068 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3069 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3070 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3071 isl_pw_qpolynomial_free(pwqp1);
3072 isl_pw_qpolynomial_free(pwqp2);
3073 if (equal < 0)
3074 return -1;
3075 if (!equal)
3076 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3078 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3079 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3080 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3081 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3082 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3083 isl_val_one(ctx));
3084 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3085 isl_pw_qpolynomial_free(pwqp1);
3086 isl_pw_qpolynomial_free(pwqp2);
3087 if (equal < 0)
3088 return -1;
3089 if (!equal)
3090 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3092 return 0;
3095 static int test_split_periods(isl_ctx *ctx)
3097 const char *str;
3098 isl_pw_qpolynomial *pwqp;
3100 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3101 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3102 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3103 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3105 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3107 isl_pw_qpolynomial_free(pwqp);
3109 if (!pwqp)
3110 return -1;
3112 return 0;
3115 static int test_union(isl_ctx *ctx)
3117 const char *str;
3118 isl_union_set *uset1, *uset2;
3119 isl_union_map *umap1, *umap2;
3120 int equal;
3122 str = "{ [i] : 0 <= i <= 1 }";
3123 uset1 = isl_union_set_read_from_str(ctx, str);
3124 str = "{ [1] -> [0] }";
3125 umap1 = isl_union_map_read_from_str(ctx, str);
3127 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3128 equal = isl_union_map_is_equal(umap1, umap2);
3130 isl_union_map_free(umap1);
3131 isl_union_map_free(umap2);
3133 if (equal < 0)
3134 return -1;
3135 if (!equal)
3136 isl_die(ctx, isl_error_unknown, "union maps not equal",
3137 return -1);
3139 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3140 umap1 = isl_union_map_read_from_str(ctx, str);
3141 str = "{ A[i]; B[i] }";
3142 uset1 = isl_union_set_read_from_str(ctx, str);
3144 uset2 = isl_union_map_domain(umap1);
3146 equal = isl_union_set_is_equal(uset1, uset2);
3148 isl_union_set_free(uset1);
3149 isl_union_set_free(uset2);
3151 if (equal < 0)
3152 return -1;
3153 if (!equal)
3154 isl_die(ctx, isl_error_unknown, "union sets not equal",
3155 return -1);
3157 return 0;
3160 /* Check that computing a bound of a non-zero polynomial over an unbounded
3161 * domain does not produce a rational value.
3162 * In particular, check that the upper bound is infinity.
3164 static int test_bound_unbounded_domain(isl_ctx *ctx)
3166 const char *str;
3167 isl_pw_qpolynomial *pwqp;
3168 isl_pw_qpolynomial_fold *pwf, *pwf2;
3169 isl_bool equal;
3171 str = "{ [m,n] -> -m * n }";
3172 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3173 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3174 str = "{ infty }";
3175 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3176 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3177 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
3178 isl_pw_qpolynomial_fold_free(pwf);
3179 isl_pw_qpolynomial_fold_free(pwf2);
3181 if (equal < 0)
3182 return -1;
3183 if (!equal)
3184 isl_die(ctx, isl_error_unknown,
3185 "expecting infinite polynomial bound", return -1);
3187 return 0;
3190 static int test_bound(isl_ctx *ctx)
3192 const char *str;
3193 unsigned dim;
3194 isl_pw_qpolynomial *pwqp;
3195 isl_pw_qpolynomial_fold *pwf;
3197 if (test_bound_unbounded_domain(ctx) < 0)
3198 return -1;
3200 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
3201 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3202 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3203 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3204 isl_pw_qpolynomial_fold_free(pwf);
3205 if (dim != 4)
3206 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3207 return -1);
3209 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
3210 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3211 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3212 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3213 isl_pw_qpolynomial_fold_free(pwf);
3214 if (dim != 1)
3215 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3216 return -1);
3218 return 0;
3221 static int test_lift(isl_ctx *ctx)
3223 const char *str;
3224 isl_basic_map *bmap;
3225 isl_basic_set *bset;
3227 str = "{ [i0] : exists e0 : i0 = 4e0 }";
3228 bset = isl_basic_set_read_from_str(ctx, str);
3229 bset = isl_basic_set_lift(bset);
3230 bmap = isl_basic_map_from_range(bset);
3231 bset = isl_basic_map_domain(bmap);
3232 isl_basic_set_free(bset);
3234 return 0;
3237 struct {
3238 const char *set1;
3239 const char *set2;
3240 int subset;
3241 } subset_tests[] = {
3242 { "{ [112, 0] }",
3243 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
3244 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
3245 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
3246 { "{ [65] }",
3247 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
3248 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
3249 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
3250 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
3251 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
3252 "256e0 <= 255i and 256e0 >= -255 + 255i and "
3253 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
3254 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
3255 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
3256 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
3257 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
3258 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
3259 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
3260 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
3261 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
3262 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
3263 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
3264 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
3265 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
3266 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
3267 "4e0 <= -57 + i0 + i1)) or "
3268 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
3269 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
3270 "4e0 >= -61 + i0 + i1)) or "
3271 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
3272 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
3275 static int test_subset(isl_ctx *ctx)
3277 int i;
3278 isl_set *set1, *set2;
3279 int subset;
3281 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3282 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3283 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3284 subset = isl_set_is_subset(set1, set2);
3285 isl_set_free(set1);
3286 isl_set_free(set2);
3287 if (subset < 0)
3288 return -1;
3289 if (subset != subset_tests[i].subset)
3290 isl_die(ctx, isl_error_unknown,
3291 "incorrect subset result", return -1);
3294 return 0;
3297 struct {
3298 const char *minuend;
3299 const char *subtrahend;
3300 const char *difference;
3301 } subtract_domain_tests[] = {
3302 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3303 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3304 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3307 static int test_subtract(isl_ctx *ctx)
3309 int i;
3310 isl_union_map *umap1, *umap2;
3311 isl_union_pw_multi_aff *upma1, *upma2;
3312 isl_union_set *uset;
3313 int equal;
3315 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3316 umap1 = isl_union_map_read_from_str(ctx,
3317 subtract_domain_tests[i].minuend);
3318 uset = isl_union_set_read_from_str(ctx,
3319 subtract_domain_tests[i].subtrahend);
3320 umap2 = isl_union_map_read_from_str(ctx,
3321 subtract_domain_tests[i].difference);
3322 umap1 = isl_union_map_subtract_domain(umap1, uset);
3323 equal = isl_union_map_is_equal(umap1, umap2);
3324 isl_union_map_free(umap1);
3325 isl_union_map_free(umap2);
3326 if (equal < 0)
3327 return -1;
3328 if (!equal)
3329 isl_die(ctx, isl_error_unknown,
3330 "incorrect subtract domain result", return -1);
3333 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3334 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3335 subtract_domain_tests[i].minuend);
3336 uset = isl_union_set_read_from_str(ctx,
3337 subtract_domain_tests[i].subtrahend);
3338 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3339 subtract_domain_tests[i].difference);
3340 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3341 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3342 isl_union_pw_multi_aff_free(upma1);
3343 isl_union_pw_multi_aff_free(upma2);
3344 if (equal < 0)
3345 return -1;
3346 if (!equal)
3347 isl_die(ctx, isl_error_unknown,
3348 "incorrect subtract domain result", return -1);
3351 return 0;
3354 /* Check that intersecting the empty basic set with another basic set
3355 * does not increase the number of constraints. In particular,
3356 * the empty basic set should maintain its canonical representation.
3358 static int test_intersect(isl_ctx *ctx)
3360 int n1, n2;
3361 isl_basic_set *bset1, *bset2;
3363 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3364 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3365 n1 = isl_basic_set_n_constraint(bset1);
3366 bset1 = isl_basic_set_intersect(bset1, bset2);
3367 n2 = isl_basic_set_n_constraint(bset1);
3368 isl_basic_set_free(bset1);
3369 if (!bset1)
3370 return -1;
3371 if (n1 != n2)
3372 isl_die(ctx, isl_error_unknown,
3373 "number of constraints of empty set changed",
3374 return -1);
3376 return 0;
3379 int test_factorize(isl_ctx *ctx)
3381 const char *str;
3382 isl_basic_set *bset;
3383 isl_factorizer *f;
3385 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3386 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3387 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3388 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3389 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3390 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3391 "3i5 >= -2i0 - i2 + 3i4 }";
3392 bset = isl_basic_set_read_from_str(ctx, str);
3393 f = isl_basic_set_factorizer(bset);
3394 isl_basic_set_free(bset);
3395 isl_factorizer_free(f);
3396 if (!f)
3397 isl_die(ctx, isl_error_unknown,
3398 "failed to construct factorizer", return -1);
3400 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3401 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3402 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3403 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3404 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3405 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3406 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3407 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3408 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3409 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3410 bset = isl_basic_set_read_from_str(ctx, str);
3411 f = isl_basic_set_factorizer(bset);
3412 isl_basic_set_free(bset);
3413 isl_factorizer_free(f);
3414 if (!f)
3415 isl_die(ctx, isl_error_unknown,
3416 "failed to construct factorizer", return -1);
3418 return 0;
3421 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3423 int *injective = user;
3425 *injective = isl_map_is_injective(map);
3426 isl_map_free(map);
3428 if (*injective < 0 || !*injective)
3429 return isl_stat_error;
3431 return isl_stat_ok;
3434 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3435 const char *r, const char *s, int tilable, int parallel)
3437 int i;
3438 isl_union_set *D;
3439 isl_union_map *W, *R, *S;
3440 isl_union_map *empty;
3441 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3442 isl_union_map *validity, *proximity, *coincidence;
3443 isl_union_map *schedule;
3444 isl_union_map *test;
3445 isl_union_set *delta;
3446 isl_union_set *domain;
3447 isl_set *delta_set;
3448 isl_set *slice;
3449 isl_set *origin;
3450 isl_schedule_constraints *sc;
3451 isl_schedule *sched;
3452 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3454 D = isl_union_set_read_from_str(ctx, d);
3455 W = isl_union_map_read_from_str(ctx, w);
3456 R = isl_union_map_read_from_str(ctx, r);
3457 S = isl_union_map_read_from_str(ctx, s);
3459 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3460 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3462 empty = isl_union_map_empty(isl_union_map_get_space(S));
3463 isl_union_map_compute_flow(isl_union_map_copy(R),
3464 isl_union_map_copy(W), empty,
3465 isl_union_map_copy(S),
3466 &dep_raw, NULL, NULL, NULL);
3467 isl_union_map_compute_flow(isl_union_map_copy(W),
3468 isl_union_map_copy(W),
3469 isl_union_map_copy(R),
3470 isl_union_map_copy(S),
3471 &dep_waw, &dep_war, NULL, NULL);
3473 dep = isl_union_map_union(dep_waw, dep_war);
3474 dep = isl_union_map_union(dep, dep_raw);
3475 validity = isl_union_map_copy(dep);
3476 coincidence = isl_union_map_copy(dep);
3477 proximity = isl_union_map_copy(dep);
3479 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3480 sc = isl_schedule_constraints_set_validity(sc, validity);
3481 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3482 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3483 sched = isl_schedule_constraints_compute_schedule(sc);
3484 schedule = isl_schedule_get_map(sched);
3485 isl_schedule_free(sched);
3486 isl_union_map_free(W);
3487 isl_union_map_free(R);
3488 isl_union_map_free(S);
3490 is_injection = 1;
3491 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3493 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3494 is_complete = isl_union_set_is_subset(D, domain);
3495 isl_union_set_free(D);
3496 isl_union_set_free(domain);
3498 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3499 test = isl_union_map_apply_range(test, dep);
3500 test = isl_union_map_apply_range(test, schedule);
3502 delta = isl_union_map_deltas(test);
3503 if (isl_union_set_n_set(delta) == 0) {
3504 is_tilable = 1;
3505 is_parallel = 1;
3506 is_nonneg = 1;
3507 isl_union_set_free(delta);
3508 } else {
3509 delta_set = isl_set_from_union_set(delta);
3511 slice = isl_set_universe(isl_set_get_space(delta_set));
3512 for (i = 0; i < tilable; ++i)
3513 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3514 is_tilable = isl_set_is_subset(delta_set, slice);
3515 isl_set_free(slice);
3517 slice = isl_set_universe(isl_set_get_space(delta_set));
3518 for (i = 0; i < parallel; ++i)
3519 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3520 is_parallel = isl_set_is_subset(delta_set, slice);
3521 isl_set_free(slice);
3523 origin = isl_set_universe(isl_set_get_space(delta_set));
3524 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
3525 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3527 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3528 delta_set = isl_set_lexmin(delta_set);
3530 is_nonneg = isl_set_is_equal(delta_set, origin);
3532 isl_set_free(origin);
3533 isl_set_free(delta_set);
3536 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3537 is_injection < 0 || is_complete < 0)
3538 return -1;
3539 if (!is_complete)
3540 isl_die(ctx, isl_error_unknown,
3541 "generated schedule incomplete", return -1);
3542 if (!is_injection)
3543 isl_die(ctx, isl_error_unknown,
3544 "generated schedule not injective on each statement",
3545 return -1);
3546 if (!is_nonneg)
3547 isl_die(ctx, isl_error_unknown,
3548 "negative dependences in generated schedule",
3549 return -1);
3550 if (!is_tilable)
3551 isl_die(ctx, isl_error_unknown,
3552 "generated schedule not as tilable as expected",
3553 return -1);
3554 if (!is_parallel)
3555 isl_die(ctx, isl_error_unknown,
3556 "generated schedule not as parallel as expected",
3557 return -1);
3559 return 0;
3562 /* Compute a schedule for the given instance set, validity constraints,
3563 * proximity constraints and context and return a corresponding union map
3564 * representation.
3566 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3567 const char *domain, const char *validity, const char *proximity,
3568 const char *context)
3570 isl_set *con;
3571 isl_union_set *dom;
3572 isl_union_map *dep;
3573 isl_union_map *prox;
3574 isl_schedule_constraints *sc;
3575 isl_schedule *schedule;
3576 isl_union_map *sched;
3578 con = isl_set_read_from_str(ctx, context);
3579 dom = isl_union_set_read_from_str(ctx, domain);
3580 dep = isl_union_map_read_from_str(ctx, validity);
3581 prox = isl_union_map_read_from_str(ctx, proximity);
3582 sc = isl_schedule_constraints_on_domain(dom);
3583 sc = isl_schedule_constraints_set_context(sc, con);
3584 sc = isl_schedule_constraints_set_validity(sc, dep);
3585 sc = isl_schedule_constraints_set_proximity(sc, prox);
3586 schedule = isl_schedule_constraints_compute_schedule(sc);
3587 sched = isl_schedule_get_map(schedule);
3588 isl_schedule_free(schedule);
3590 return sched;
3593 /* Compute a schedule for the given instance set, validity constraints and
3594 * proximity constraints and return a corresponding union map representation.
3596 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
3597 const char *domain, const char *validity, const char *proximity)
3599 return compute_schedule_with_context(ctx, domain, validity, proximity,
3600 "{ : }");
3603 /* Check that a schedule can be constructed on the given domain
3604 * with the given validity and proximity constraints.
3606 static int test_has_schedule(isl_ctx *ctx, const char *domain,
3607 const char *validity, const char *proximity)
3609 isl_union_map *sched;
3611 sched = compute_schedule(ctx, domain, validity, proximity);
3612 if (!sched)
3613 return -1;
3615 isl_union_map_free(sched);
3616 return 0;
3619 int test_special_schedule(isl_ctx *ctx, const char *domain,
3620 const char *validity, const char *proximity, const char *expected_sched)
3622 isl_union_map *sched1, *sched2;
3623 int equal;
3625 sched1 = compute_schedule(ctx, domain, validity, proximity);
3626 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
3628 equal = isl_union_map_is_equal(sched1, sched2);
3629 isl_union_map_free(sched1);
3630 isl_union_map_free(sched2);
3632 if (equal < 0)
3633 return -1;
3634 if (!equal)
3635 isl_die(ctx, isl_error_unknown, "unexpected schedule",
3636 return -1);
3638 return 0;
3641 /* Check that the schedule map is properly padded, even after being
3642 * reconstructed from the band forest.
3644 static int test_padded_schedule(isl_ctx *ctx)
3646 const char *str;
3647 isl_union_set *D;
3648 isl_union_map *validity, *proximity;
3649 isl_schedule_constraints *sc;
3650 isl_schedule *sched;
3651 isl_union_map *map1, *map2;
3652 isl_band_list *list;
3653 int equal;
3655 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
3656 D = isl_union_set_read_from_str(ctx, str);
3657 validity = isl_union_map_empty(isl_union_set_get_space(D));
3658 proximity = isl_union_map_copy(validity);
3659 sc = isl_schedule_constraints_on_domain(D);
3660 sc = isl_schedule_constraints_set_validity(sc, validity);
3661 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3662 sched = isl_schedule_constraints_compute_schedule(sc);
3663 map1 = isl_schedule_get_map(sched);
3664 list = isl_schedule_get_band_forest(sched);
3665 isl_band_list_free(list);
3666 map2 = isl_schedule_get_map(sched);
3667 isl_schedule_free(sched);
3668 equal = isl_union_map_is_equal(map1, map2);
3669 isl_union_map_free(map1);
3670 isl_union_map_free(map2);
3672 if (equal < 0)
3673 return -1;
3674 if (!equal)
3675 isl_die(ctx, isl_error_unknown,
3676 "reconstructed schedule map not the same as original",
3677 return -1);
3679 return 0;
3682 /* Check that conditional validity constraints are also taken into
3683 * account across bands.
3684 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
3685 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
3686 * and then check that the adjacent order constraint C[2,1]->D[2,0]
3687 * is enforced by the rest of the schedule.
3689 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
3691 const char *str;
3692 isl_union_set *domain;
3693 isl_union_map *validity, *proximity, *condition;
3694 isl_union_map *sink, *source, *dep;
3695 isl_schedule_constraints *sc;
3696 isl_schedule *schedule;
3697 isl_union_access_info *access;
3698 isl_union_flow *flow;
3699 int empty;
3701 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3702 "A[k] : k >= 1 and k <= -1 + n; "
3703 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3704 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
3705 domain = isl_union_set_read_from_str(ctx, str);
3706 sc = isl_schedule_constraints_on_domain(domain);
3707 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
3708 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3709 "D[k, i] -> C[1 + k, i] : "
3710 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3711 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
3712 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
3713 validity = isl_union_map_read_from_str(ctx, str);
3714 sc = isl_schedule_constraints_set_validity(sc, validity);
3715 str = "[n] -> { C[k, i] -> D[k, i] : "
3716 "0 <= i <= -1 + k and k <= -1 + n }";
3717 proximity = isl_union_map_read_from_str(ctx, str);
3718 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3719 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
3720 "i <= -1 + k and i >= 1 and k <= -2 + n; "
3721 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
3722 "k <= -1 + n and i >= 0 and i <= -2 + k }";
3723 condition = isl_union_map_read_from_str(ctx, str);
3724 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
3725 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3726 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
3727 "i >= 0 and i <= -1 + k and k <= -1 + n and "
3728 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
3729 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
3730 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3731 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
3732 "k <= -1 + n and i >= 0 and i <= -1 + k and "
3733 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
3734 validity = isl_union_map_read_from_str(ctx, str);
3735 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3736 validity);
3737 schedule = isl_schedule_constraints_compute_schedule(sc);
3738 str = "{ D[2,0] -> [] }";
3739 sink = isl_union_map_read_from_str(ctx, str);
3740 access = isl_union_access_info_from_sink(sink);
3741 str = "{ C[2,1] -> [] }";
3742 source = isl_union_map_read_from_str(ctx, str);
3743 access = isl_union_access_info_set_must_source(access, source);
3744 access = isl_union_access_info_set_schedule(access, schedule);
3745 flow = isl_union_access_info_compute_flow(access);
3746 dep = isl_union_flow_get_must_dependence(flow);
3747 isl_union_flow_free(flow);
3748 empty = isl_union_map_is_empty(dep);
3749 isl_union_map_free(dep);
3751 if (empty < 0)
3752 return -1;
3753 if (empty)
3754 isl_die(ctx, isl_error_unknown,
3755 "conditional validity not respected", return -1);
3757 return 0;
3760 /* Check that the test for violated conditional validity constraints
3761 * is not confused by domain compression.
3762 * In particular, earlier versions of isl would apply
3763 * a schedule on the compressed domains to the original domains,
3764 * resulting in a failure to detect that the default schedule
3765 * violates the conditional validity constraints.
3767 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
3769 const char *str;
3770 isl_bool empty;
3771 isl_union_set *domain;
3772 isl_union_map *validity, *condition;
3773 isl_schedule_constraints *sc;
3774 isl_schedule *schedule;
3775 isl_union_map *umap;
3776 isl_map *map, *ge;
3778 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
3779 domain = isl_union_set_read_from_str(ctx, str);
3780 sc = isl_schedule_constraints_on_domain(domain);
3781 str = "{ B[1, i] -> A[0, i + 1] }";
3782 condition = isl_union_map_read_from_str(ctx, str);
3783 str = "{ A[0, i] -> B[1, i - 1] }";
3784 validity = isl_union_map_read_from_str(ctx, str);
3785 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3786 isl_union_map_copy(validity));
3787 schedule = isl_schedule_constraints_compute_schedule(sc);
3788 umap = isl_schedule_get_map(schedule);
3789 isl_schedule_free(schedule);
3790 validity = isl_union_map_apply_domain(validity,
3791 isl_union_map_copy(umap));
3792 validity = isl_union_map_apply_range(validity, umap);
3793 map = isl_map_from_union_map(validity);
3794 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3795 map = isl_map_intersect(map, ge);
3796 empty = isl_map_is_empty(map);
3797 isl_map_free(map);
3799 if (empty < 0)
3800 return -1;
3801 if (!empty)
3802 isl_die(ctx, isl_error_unknown,
3803 "conditional validity constraints not satisfied",
3804 return -1);
3806 return 0;
3809 /* Input for testing of schedule construction based on
3810 * conditional constraints.
3812 * domain is the iteration domain
3813 * flow are the flow dependences, which determine the validity and
3814 * proximity constraints
3815 * condition are the conditions on the conditional validity constraints
3816 * conditional_validity are the conditional validity constraints
3817 * outer_band_n is the expected number of members in the outer band
3819 struct {
3820 const char *domain;
3821 const char *flow;
3822 const char *condition;
3823 const char *conditional_validity;
3824 int outer_band_n;
3825 } live_range_tests[] = {
3826 /* Contrived example that illustrates that we need to keep
3827 * track of tagged condition dependences and
3828 * tagged conditional validity dependences
3829 * in isl_sched_edge separately.
3830 * In particular, the conditional validity constraints on A
3831 * cannot be satisfied,
3832 * but they can be ignored because there are no corresponding
3833 * condition constraints. However, we do have an additional
3834 * conditional validity constraint that maps to the same
3835 * dependence relation
3836 * as the condition constraint on B. If we did not make a distinction
3837 * between tagged condition and tagged conditional validity
3838 * dependences, then we
3839 * could end up treating this shared dependence as an condition
3840 * constraint on A, forcing a localization of the conditions,
3841 * which is impossible.
3843 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
3844 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
3845 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
3846 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3847 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3848 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
3851 /* TACO 2013 Fig. 7 */
3852 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3853 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3854 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3855 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
3856 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
3857 "0 <= i < n and 0 <= j < n - 1 }",
3858 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
3859 "0 <= i < n and 0 <= j < j' < n;"
3860 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
3861 "0 <= i < i' < n and 0 <= j,j' < n;"
3862 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
3863 "0 <= i,j,j' < n and j < j' }",
3866 /* TACO 2013 Fig. 7, without tags */
3867 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3868 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3869 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3870 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3871 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3872 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
3873 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
3874 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
3877 /* TACO 2013 Fig. 12 */
3878 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
3879 "S3[i,3] : 0 <= i <= 1 }",
3880 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
3881 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
3882 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
3883 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
3884 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3885 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
3886 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3887 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
3888 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
3889 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
3890 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
3895 /* Test schedule construction based on conditional constraints.
3896 * In particular, check the number of members in the outer band node
3897 * as an indication of whether tiling is possible or not.
3899 static int test_conditional_schedule_constraints(isl_ctx *ctx)
3901 int i;
3902 isl_union_set *domain;
3903 isl_union_map *condition;
3904 isl_union_map *flow;
3905 isl_union_map *validity;
3906 isl_schedule_constraints *sc;
3907 isl_schedule *schedule;
3908 isl_schedule_node *node;
3909 int n_member;
3911 if (test_special_conditional_schedule_constraints(ctx) < 0)
3912 return -1;
3913 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
3914 return -1;
3916 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
3917 domain = isl_union_set_read_from_str(ctx,
3918 live_range_tests[i].domain);
3919 flow = isl_union_map_read_from_str(ctx,
3920 live_range_tests[i].flow);
3921 condition = isl_union_map_read_from_str(ctx,
3922 live_range_tests[i].condition);
3923 validity = isl_union_map_read_from_str(ctx,
3924 live_range_tests[i].conditional_validity);
3925 sc = isl_schedule_constraints_on_domain(domain);
3926 sc = isl_schedule_constraints_set_validity(sc,
3927 isl_union_map_copy(flow));
3928 sc = isl_schedule_constraints_set_proximity(sc, flow);
3929 sc = isl_schedule_constraints_set_conditional_validity(sc,
3930 condition, validity);
3931 schedule = isl_schedule_constraints_compute_schedule(sc);
3932 node = isl_schedule_get_root(schedule);
3933 while (node &&
3934 isl_schedule_node_get_type(node) != isl_schedule_node_band)
3935 node = isl_schedule_node_first_child(node);
3936 n_member = isl_schedule_node_band_n_member(node);
3937 isl_schedule_node_free(node);
3938 isl_schedule_free(schedule);
3940 if (!schedule)
3941 return -1;
3942 if (n_member != live_range_tests[i].outer_band_n)
3943 isl_die(ctx, isl_error_unknown,
3944 "unexpected number of members in outer band",
3945 return -1);
3947 return 0;
3950 /* Check that the schedule computed for the given instance set and
3951 * dependence relation strongly satisfies the dependences.
3952 * In particular, check that no instance is scheduled before
3953 * or together with an instance on which it depends.
3954 * Earlier versions of isl would produce a schedule that
3955 * only weakly satisfies the dependences.
3957 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
3959 const char *domain, *dep;
3960 isl_union_map *D, *schedule;
3961 isl_map *map, *ge;
3962 int empty;
3964 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
3965 "A[i0] : 0 <= i0 <= 1 }";
3966 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
3967 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
3968 schedule = compute_schedule(ctx, domain, dep, dep);
3969 D = isl_union_map_read_from_str(ctx, dep);
3970 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
3971 D = isl_union_map_apply_range(D, schedule);
3972 map = isl_map_from_union_map(D);
3973 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3974 map = isl_map_intersect(map, ge);
3975 empty = isl_map_is_empty(map);
3976 isl_map_free(map);
3978 if (empty < 0)
3979 return -1;
3980 if (!empty)
3981 isl_die(ctx, isl_error_unknown,
3982 "dependences not strongly satisfied", return -1);
3984 return 0;
3987 /* Compute a schedule for input where the instance set constraints
3988 * conflict with the context constraints.
3989 * Earlier versions of isl did not properly handle this situation.
3991 static int test_conflicting_context_schedule(isl_ctx *ctx)
3993 isl_union_map *schedule;
3994 const char *domain, *context;
3996 domain = "[n] -> { A[] : n >= 0 }";
3997 context = "[n] -> { : n < 0 }";
3998 schedule = compute_schedule_with_context(ctx,
3999 domain, "{}", "{}", context);
4000 isl_union_map_free(schedule);
4002 if (!schedule)
4003 return -1;
4005 return 0;
4008 /* Check that the dependence carrying step is not confused by
4009 * a bound on the coefficient size.
4010 * In particular, force the scheduler to move to a dependence carrying
4011 * step by demanding outer coincidence and bound the size of
4012 * the coefficients. Earlier versions of isl would take this
4013 * bound into account while carrying dependences, breaking
4014 * fundamental assumptions.
4015 * On the other hand, the dependence carrying step now tries
4016 * to prevent loop coalescing by default, so check that indeed
4017 * no loop coalescing occurs by comparing the computed schedule
4018 * to the expected non-coalescing schedule.
4020 static int test_bounded_coefficients_schedule(isl_ctx *ctx)
4022 const char *domain, *dep;
4023 isl_union_set *I;
4024 isl_union_map *D;
4025 isl_schedule_constraints *sc;
4026 isl_schedule *schedule;
4027 isl_union_map *sched1, *sched2;
4028 isl_bool equal;
4030 domain = "{ C[i0, i1] : 2 <= i0 <= 3999 and 0 <= i1 <= -1 + i0 }";
4031 dep = "{ C[i0, i1] -> C[i0, 1 + i1] : i0 <= 3999 and i1 >= 0 and "
4032 "i1 <= -2 + i0; "
4033 "C[i0, -1 + i0] -> C[1 + i0, 0] : i0 <= 3998 and i0 >= 1 }";
4034 I = isl_union_set_read_from_str(ctx, domain);
4035 D = isl_union_map_read_from_str(ctx, dep);
4036 sc = isl_schedule_constraints_on_domain(I);
4037 sc = isl_schedule_constraints_set_validity(sc, isl_union_map_copy(D));
4038 sc = isl_schedule_constraints_set_coincidence(sc, D);
4039 isl_options_set_schedule_outer_coincidence(ctx, 1);
4040 isl_options_set_schedule_max_coefficient(ctx, 20);
4041 schedule = isl_schedule_constraints_compute_schedule(sc);
4042 isl_options_set_schedule_max_coefficient(ctx, -1);
4043 isl_options_set_schedule_outer_coincidence(ctx, 0);
4044 sched1 = isl_schedule_get_map(schedule);
4045 isl_schedule_free(schedule);
4047 sched2 = isl_union_map_read_from_str(ctx, "{ C[x,y] -> [x,y] }");
4048 equal = isl_union_map_is_equal(sched1, sched2);
4049 isl_union_map_free(sched1);
4050 isl_union_map_free(sched2);
4052 if (equal < 0)
4053 return -1;
4054 if (!equal)
4055 isl_die(ctx, isl_error_unknown,
4056 "unexpected schedule", return -1);
4058 return 0;
4061 /* Check that the bounds on the coefficients are respected.
4062 * This function checks for a particular output schedule,
4063 * but the exact output is not important, only that it does
4064 * not contain any coefficients greater than 4.
4065 * It is, however, easier to check for a particular output.
4066 * This test is only run for the whole component scheduler
4067 * because the incremental scheduler produces a slightly different schedule.
4069 static int test_bounded_coefficients_schedule_whole(isl_ctx *ctx)
4071 const char *domain, *dep, *str;
4072 isl_union_set *I;
4073 isl_union_map *D;
4074 isl_schedule_constraints *sc;
4075 isl_schedule *schedule;
4076 isl_union_map *sched1, *sched2;
4077 isl_bool equal;
4079 domain = "{ S_4[i, j, k] : 0 <= i < j <= 10 and 0 <= k <= 100; "
4080 "S_2[i, j] : 0 <= i < j <= 10; S_6[i, j] : 0 <= i < j <= 10 }";
4081 dep = "{ S_2[0, j] -> S_4[0, j, 0] : 0 < j <= 10; "
4082 "S_4[0, j, 100] -> S_6[0, j] : 0 < j <= 10 }";
4083 I = isl_union_set_read_from_str(ctx, domain);
4084 D = isl_union_map_read_from_str(ctx, dep);
4085 sc = isl_schedule_constraints_on_domain(I);
4086 sc = isl_schedule_constraints_set_validity(sc, D);
4087 isl_options_set_schedule_max_constant_term(ctx, 10);
4088 isl_options_set_schedule_max_coefficient(ctx, 4);
4089 schedule = isl_schedule_constraints_compute_schedule(sc);
4090 isl_options_set_schedule_max_coefficient(ctx, -1);
4091 isl_options_set_schedule_max_constant_term(ctx, -1);
4092 sched1 = isl_schedule_get_map(schedule);
4093 isl_schedule_free(schedule);
4095 str = "{ S_4[i, j, k] -> [i, j, 10 - k, 1]; "
4096 "S_2[i, j] -> [0, i, j, 0]; S_6[i, j] -> [0, 10 + i, j, 2] }";
4097 sched2 = isl_union_map_read_from_str(ctx, str);
4098 equal = isl_union_map_is_equal(sched1, sched2);
4099 isl_union_map_free(sched1);
4100 isl_union_map_free(sched2);
4102 if (equal < 0)
4103 return -1;
4104 if (!equal)
4105 isl_die(ctx, isl_error_unknown,
4106 "unexpected schedule", return -1);
4108 return 0;
4111 /* Check that a set of schedule constraints that only allow for
4112 * a coalescing schedule still produces a schedule even if the user
4113 * request a non-coalescing schedule. Earlier versions of isl
4114 * would not handle this case correctly.
4116 static int test_coalescing_schedule(isl_ctx *ctx)
4118 const char *domain, *dep;
4119 isl_union_set *I;
4120 isl_union_map *D;
4121 isl_schedule_constraints *sc;
4122 isl_schedule *schedule;
4123 int treat_coalescing;
4125 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
4126 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
4127 I = isl_union_set_read_from_str(ctx, domain);
4128 D = isl_union_map_read_from_str(ctx, dep);
4129 sc = isl_schedule_constraints_on_domain(I);
4130 sc = isl_schedule_constraints_set_validity(sc, D);
4131 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4132 isl_options_set_schedule_treat_coalescing(ctx, 1);
4133 schedule = isl_schedule_constraints_compute_schedule(sc);
4134 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4135 isl_schedule_free(schedule);
4136 if (!schedule)
4137 return -1;
4138 return 0;
4141 /* Check that the scheduler does not perform any needless
4142 * compound skewing. Earlier versions of isl would compute
4143 * schedules in terms of transformed schedule coefficients and
4144 * would not accurately keep track of the sum of the original
4145 * schedule coefficients. It could then produce the schedule
4146 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
4147 * for the input below instead of the schedule below.
4149 static int test_skewing_schedule(isl_ctx *ctx)
4151 const char *D, *V, *P, *S;
4153 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
4154 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
4155 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
4156 "-1 <= a-i + b-j + c-k <= 1 }";
4157 P = "{ }";
4158 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
4160 return test_special_schedule(ctx, D, V, P, S);
4163 int test_schedule(isl_ctx *ctx)
4165 const char *D, *W, *R, *V, *P, *S;
4166 int max_coincidence;
4167 int treat_coalescing;
4169 /* Handle resulting schedule with zero bands. */
4170 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
4171 return -1;
4173 /* Jacobi */
4174 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
4175 W = "{ S1[t,i] -> a[t,i] }";
4176 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
4177 "S1[t,i] -> a[t-1,i+1] }";
4178 S = "{ S1[t,i] -> [t,i] }";
4179 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4180 return -1;
4182 /* Fig. 5 of CC2008 */
4183 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
4184 "j <= -1 + N }";
4185 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
4186 "j >= 2 and j <= -1 + N }";
4187 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
4188 "j >= 2 and j <= -1 + N; "
4189 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
4190 "j >= 2 and j <= -1 + N }";
4191 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4192 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4193 return -1;
4195 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
4196 W = "{ S1[i] -> a[i] }";
4197 R = "{ S2[i] -> a[i+1] }";
4198 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4199 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4200 return -1;
4202 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
4203 W = "{ S1[i] -> a[i] }";
4204 R = "{ S2[i] -> a[9-i] }";
4205 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4206 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4207 return -1;
4209 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
4210 W = "{ S1[i] -> a[i] }";
4211 R = "[N] -> { S2[i] -> a[N-1-i] }";
4212 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4213 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4214 return -1;
4216 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
4217 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
4218 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
4219 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
4220 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4221 return -1;
4223 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4224 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
4225 R = "{ S2[i,j] -> a[i-1,j] }";
4226 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4227 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4228 return -1;
4230 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4231 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
4232 R = "{ S2[i,j] -> a[i,j-1] }";
4233 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4234 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4235 return -1;
4237 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
4238 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
4239 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
4240 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
4241 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
4242 "S_0[] -> [0, 0, 0] }";
4243 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
4244 return -1;
4245 ctx->opt->schedule_parametric = 0;
4246 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4247 return -1;
4248 ctx->opt->schedule_parametric = 1;
4250 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
4251 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
4252 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
4253 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
4254 "S4[i] -> a[i,N] }";
4255 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
4256 "S4[i] -> [4,i,0] }";
4257 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
4258 isl_options_set_schedule_maximize_coincidence(ctx, 0);
4259 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4260 return -1;
4261 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
4263 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
4264 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4265 "j <= N }";
4266 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4267 "j <= N; "
4268 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
4269 "j <= N }";
4270 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4271 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4272 return -1;
4274 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
4275 " S_2[t] : t >= 0 and t <= -1 + N; "
4276 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
4277 "i <= -1 + N }";
4278 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
4279 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
4280 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
4281 "i >= 0 and i <= -1 + N }";
4282 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
4283 "i >= 0 and i <= -1 + N; "
4284 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
4285 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
4286 " S_0[t] -> [0, t, 0] }";
4288 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4289 return -1;
4290 ctx->opt->schedule_parametric = 0;
4291 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4292 return -1;
4293 ctx->opt->schedule_parametric = 1;
4295 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
4296 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
4297 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
4298 return -1;
4300 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
4301 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
4302 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
4303 "j >= 0 and j <= -1 + N; "
4304 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4305 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
4306 "j >= 0 and j <= -1 + N; "
4307 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4308 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
4309 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4310 return -1;
4312 D = "{ S_0[i] : i >= 0 }";
4313 W = "{ S_0[i] -> a[i] : i >= 0 }";
4314 R = "{ S_0[i] -> a[0] : i >= 0 }";
4315 S = "{ S_0[i] -> [0, i, 0] }";
4316 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4317 return -1;
4319 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
4320 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
4321 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
4322 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
4323 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4324 return -1;
4326 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
4327 "k <= -1 + n and k >= 0 }";
4328 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
4329 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
4330 "k <= -1 + n and k >= 0; "
4331 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
4332 "k <= -1 + n and k >= 0; "
4333 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
4334 "k <= -1 + n and k >= 0 }";
4335 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
4336 ctx->opt->schedule_outer_coincidence = 1;
4337 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4338 return -1;
4339 ctx->opt->schedule_outer_coincidence = 0;
4341 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
4342 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
4343 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
4344 "Stmt_for_body24[i0, i1, 1, 0]:"
4345 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
4346 "Stmt_for_body7[i0, i1, i2]:"
4347 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
4348 "i2 <= 7 }";
4350 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
4351 "Stmt_for_body24[1, i1, i2, i3]:"
4352 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
4353 "i2 >= 1;"
4354 "Stmt_for_body24[0, i1, i2, i3] -> "
4355 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
4356 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
4357 "i3 >= 0;"
4358 "Stmt_for_body24[0, i1, i2, i3] ->"
4359 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4360 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4361 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4362 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4363 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4364 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4365 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4366 "i1 <= 6 and i1 >= 0;"
4367 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4368 "Stmt_for_body7[i0, i1, i2] -> "
4369 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4370 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4371 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4372 "Stmt_for_body7[i0, i1, i2] -> "
4373 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4374 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4375 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4376 P = V;
4378 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4379 isl_options_set_schedule_treat_coalescing(ctx, 0);
4380 if (test_has_schedule(ctx, D, V, P) < 0)
4381 return -1;
4382 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4384 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4385 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4386 "j >= 1 and j <= 7;"
4387 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4388 "j >= 1 and j <= 8 }";
4389 P = "{ }";
4390 S = "{ S_0[i, j] -> [i + j, i] }";
4391 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4392 if (test_special_schedule(ctx, D, V, P, S) < 0)
4393 return -1;
4394 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4396 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4397 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4398 "j >= 0 and j <= -1 + i }";
4399 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4400 "i <= -1 + N and j >= 0;"
4401 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4402 "i <= -2 + N }";
4403 P = "{ }";
4404 S = "{ S_0[i, j] -> [i, j] }";
4405 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4406 if (test_special_schedule(ctx, D, V, P, S) < 0)
4407 return -1;
4408 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4410 /* Test both algorithms on a case with only proximity dependences. */
4411 D = "{ S[i,j] : 0 <= i <= 10 }";
4412 V = "{ }";
4413 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4414 S = "{ S[i, j] -> [j, i] }";
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;
4419 if (test_special_schedule(ctx, D, V, P, S) < 0)
4420 return -1;
4422 D = "{ A[a]; B[] }";
4423 V = "{}";
4424 P = "{ A[a] -> B[] }";
4425 if (test_has_schedule(ctx, D, V, P) < 0)
4426 return -1;
4428 if (test_padded_schedule(ctx) < 0)
4429 return -1;
4431 /* Check that check for progress is not confused by rational
4432 * solution.
4434 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4435 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4436 "i0 <= -2 + N; "
4437 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4438 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4439 P = "{}";
4440 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4441 if (test_has_schedule(ctx, D, V, P) < 0)
4442 return -1;
4443 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4445 /* Check that we allow schedule rows that are only non-trivial
4446 * on some full-dimensional domains.
4448 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4449 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4450 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4451 P = "{}";
4452 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4453 if (test_has_schedule(ctx, D, V, P) < 0)
4454 return -1;
4455 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4457 if (test_conditional_schedule_constraints(ctx) < 0)
4458 return -1;
4460 if (test_strongly_satisfying_schedule(ctx) < 0)
4461 return -1;
4463 if (test_conflicting_context_schedule(ctx) < 0)
4464 return -1;
4466 if (test_bounded_coefficients_schedule(ctx) < 0)
4467 return -1;
4468 if (test_coalescing_schedule(ctx) < 0)
4469 return -1;
4470 if (test_skewing_schedule(ctx) < 0)
4471 return -1;
4473 return 0;
4476 /* Perform scheduling tests using the whole component scheduler.
4478 static int test_schedule_whole(isl_ctx *ctx)
4480 int whole;
4481 int r;
4483 whole = isl_options_get_schedule_whole_component(ctx);
4484 isl_options_set_schedule_whole_component(ctx, 1);
4485 r = test_schedule(ctx);
4486 if (r >= 0)
4487 r = test_bounded_coefficients_schedule_whole(ctx);
4488 isl_options_set_schedule_whole_component(ctx, whole);
4490 return r;
4493 /* Perform scheduling tests using the incremental scheduler.
4495 static int test_schedule_incremental(isl_ctx *ctx)
4497 int whole;
4498 int r;
4500 whole = isl_options_get_schedule_whole_component(ctx);
4501 isl_options_set_schedule_whole_component(ctx, 0);
4502 r = test_schedule(ctx);
4503 isl_options_set_schedule_whole_component(ctx, whole);
4505 return r;
4508 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4510 isl_union_map *umap;
4511 int test;
4513 umap = isl_union_map_read_from_str(ctx, str);
4514 test = isl_union_map_plain_is_injective(umap);
4515 isl_union_map_free(umap);
4516 if (test < 0)
4517 return -1;
4518 if (test == injective)
4519 return 0;
4520 if (injective)
4521 isl_die(ctx, isl_error_unknown,
4522 "map not detected as injective", return -1);
4523 else
4524 isl_die(ctx, isl_error_unknown,
4525 "map detected as injective", return -1);
4528 int test_injective(isl_ctx *ctx)
4530 const char *str;
4532 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4533 return -1;
4534 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4535 return -1;
4536 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4537 return -1;
4538 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4539 return -1;
4540 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4541 return -1;
4542 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4543 return -1;
4544 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4545 return -1;
4546 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4547 return -1;
4549 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4550 if (test_plain_injective(ctx, str, 1))
4551 return -1;
4552 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4553 if (test_plain_injective(ctx, str, 0))
4554 return -1;
4556 return 0;
4559 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4561 isl_aff *aff2;
4562 int equal;
4564 if (!aff)
4565 return -1;
4567 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4568 equal = isl_aff_plain_is_equal(aff, aff2);
4569 isl_aff_free(aff2);
4571 return equal;
4574 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4576 int equal;
4578 equal = aff_plain_is_equal(aff, str);
4579 if (equal < 0)
4580 return -1;
4581 if (!equal)
4582 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4583 "result not as expected", return -1);
4584 return 0;
4587 struct {
4588 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4589 __isl_take isl_aff *aff2);
4590 } aff_bin_op[] = {
4591 ['+'] = { &isl_aff_add },
4592 ['-'] = { &isl_aff_sub },
4593 ['*'] = { &isl_aff_mul },
4594 ['/'] = { &isl_aff_div },
4597 struct {
4598 const char *arg1;
4599 unsigned char op;
4600 const char *arg2;
4601 const char *res;
4602 } aff_bin_tests[] = {
4603 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
4604 "{ [i] -> [2i] }" },
4605 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
4606 "{ [i] -> [0] }" },
4607 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
4608 "{ [i] -> [2i] }" },
4609 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
4610 "{ [i] -> [2i] }" },
4611 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
4612 "{ [i] -> [i/2] }" },
4613 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
4614 "{ [i] -> [i] }" },
4615 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
4616 "{ [i] -> [NaN] }" },
4617 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
4618 "{ [i] -> [NaN] }" },
4619 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
4620 "{ [i] -> [NaN] }" },
4621 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
4622 "{ [i] -> [NaN] }" },
4623 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
4624 "{ [i] -> [NaN] }" },
4625 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
4626 "{ [i] -> [NaN] }" },
4627 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
4628 "{ [i] -> [NaN] }" },
4629 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
4630 "{ [i] -> [NaN] }" },
4631 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
4632 "{ [i] -> [NaN] }" },
4633 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
4634 "{ [i] -> [NaN] }" },
4635 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
4636 "{ [i] -> [NaN] }" },
4637 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
4638 "{ [i] -> [NaN] }" },
4641 /* Perform some basic tests of binary operations on isl_aff objects.
4643 static int test_bin_aff(isl_ctx *ctx)
4645 int i;
4646 isl_aff *aff1, *aff2, *res;
4647 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4648 __isl_take isl_aff *aff2);
4649 int ok;
4651 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
4652 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
4653 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
4654 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
4655 fn = aff_bin_op[aff_bin_tests[i].op].fn;
4656 aff1 = fn(aff1, aff2);
4657 if (isl_aff_is_nan(res))
4658 ok = isl_aff_is_nan(aff1);
4659 else
4660 ok = isl_aff_plain_is_equal(aff1, res);
4661 isl_aff_free(aff1);
4662 isl_aff_free(res);
4663 if (ok < 0)
4664 return -1;
4665 if (!ok)
4666 isl_die(ctx, isl_error_unknown,
4667 "unexpected result", return -1);
4670 return 0;
4673 struct {
4674 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
4675 __isl_take isl_pw_aff *pa2);
4676 } pw_aff_bin_op[] = {
4677 ['m'] = { &isl_pw_aff_min },
4678 ['M'] = { &isl_pw_aff_max },
4681 /* Inputs for binary isl_pw_aff operation tests.
4682 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
4683 * defined by pw_aff_bin_op, and "res" is the expected result.
4685 struct {
4686 const char *arg1;
4687 unsigned char op;
4688 const char *arg2;
4689 const char *res;
4690 } pw_aff_bin_tests[] = {
4691 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
4692 "{ [i] -> [i] }" },
4693 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
4694 "{ [i] -> [i] }" },
4695 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
4696 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
4697 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
4698 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
4699 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
4700 "{ [i] -> [NaN] }" },
4701 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
4702 "{ [i] -> [NaN] }" },
4705 /* Perform some basic tests of binary operations on isl_pw_aff objects.
4707 static int test_bin_pw_aff(isl_ctx *ctx)
4709 int i;
4710 isl_bool ok;
4711 isl_pw_aff *pa1, *pa2, *res;
4713 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
4714 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
4715 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
4716 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
4717 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
4718 if (isl_pw_aff_involves_nan(res))
4719 ok = isl_pw_aff_involves_nan(pa1);
4720 else
4721 ok = isl_pw_aff_plain_is_equal(pa1, res);
4722 isl_pw_aff_free(pa1);
4723 isl_pw_aff_free(res);
4724 if (ok < 0)
4725 return -1;
4726 if (!ok)
4727 isl_die(ctx, isl_error_unknown,
4728 "unexpected result", return -1);
4731 return 0;
4734 struct {
4735 __isl_give isl_union_pw_multi_aff *(*fn)(
4736 __isl_take isl_union_pw_multi_aff *upma1,
4737 __isl_take isl_union_pw_multi_aff *upma2);
4738 const char *arg1;
4739 const char *arg2;
4740 const char *res;
4741 } upma_bin_tests[] = {
4742 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
4743 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
4744 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
4745 "{ B[x] -> [2] : x >= 0 }",
4746 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
4747 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
4748 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
4749 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
4750 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
4751 "D[i] -> B[2] : i >= 5 }" },
4752 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4753 "{ B[x] -> C[2] : x > 0 }",
4754 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
4755 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4756 "{ B[x] -> A[2] : x >= 0 }",
4757 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
4760 /* Perform some basic tests of binary operations on
4761 * isl_union_pw_multi_aff objects.
4763 static int test_bin_upma(isl_ctx *ctx)
4765 int i;
4766 isl_union_pw_multi_aff *upma1, *upma2, *res;
4767 int ok;
4769 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
4770 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4771 upma_bin_tests[i].arg1);
4772 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4773 upma_bin_tests[i].arg2);
4774 res = isl_union_pw_multi_aff_read_from_str(ctx,
4775 upma_bin_tests[i].res);
4776 upma1 = upma_bin_tests[i].fn(upma1, upma2);
4777 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
4778 isl_union_pw_multi_aff_free(upma1);
4779 isl_union_pw_multi_aff_free(res);
4780 if (ok < 0)
4781 return -1;
4782 if (!ok)
4783 isl_die(ctx, isl_error_unknown,
4784 "unexpected result", return -1);
4787 return 0;
4790 struct {
4791 __isl_give isl_union_pw_multi_aff *(*fn)(
4792 __isl_take isl_union_pw_multi_aff *upma1,
4793 __isl_take isl_union_pw_multi_aff *upma2);
4794 const char *arg1;
4795 const char *arg2;
4796 } upma_bin_fail_tests[] = {
4797 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4798 "{ B[x] -> C[2] : x >= 0 }" },
4801 /* Perform some basic tests of binary operations on
4802 * isl_union_pw_multi_aff objects that are expected to fail.
4804 static int test_bin_upma_fail(isl_ctx *ctx)
4806 int i, n;
4807 isl_union_pw_multi_aff *upma1, *upma2;
4808 int on_error;
4810 on_error = isl_options_get_on_error(ctx);
4811 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
4812 n = ARRAY_SIZE(upma_bin_fail_tests);
4813 for (i = 0; i < n; ++i) {
4814 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4815 upma_bin_fail_tests[i].arg1);
4816 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4817 upma_bin_fail_tests[i].arg2);
4818 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
4819 isl_union_pw_multi_aff_free(upma1);
4820 if (upma1)
4821 break;
4823 isl_options_set_on_error(ctx, on_error);
4824 if (i < n)
4825 isl_die(ctx, isl_error_unknown,
4826 "operation not expected to succeed", return -1);
4828 return 0;
4831 int test_aff(isl_ctx *ctx)
4833 const char *str;
4834 isl_set *set;
4835 isl_space *space;
4836 isl_local_space *ls;
4837 isl_aff *aff;
4838 int zero, equal;
4840 if (test_bin_aff(ctx) < 0)
4841 return -1;
4842 if (test_bin_pw_aff(ctx) < 0)
4843 return -1;
4844 if (test_bin_upma(ctx) < 0)
4845 return -1;
4846 if (test_bin_upma_fail(ctx) < 0)
4847 return -1;
4849 space = isl_space_set_alloc(ctx, 0, 1);
4850 ls = isl_local_space_from_space(space);
4851 aff = isl_aff_zero_on_domain(ls);
4853 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4854 aff = isl_aff_scale_down_ui(aff, 3);
4855 aff = isl_aff_floor(aff);
4856 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4857 aff = isl_aff_scale_down_ui(aff, 2);
4858 aff = isl_aff_floor(aff);
4859 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4861 str = "{ [10] }";
4862 set = isl_set_read_from_str(ctx, str);
4863 aff = isl_aff_gist(aff, set);
4865 aff = isl_aff_add_constant_si(aff, -16);
4866 zero = isl_aff_plain_is_zero(aff);
4867 isl_aff_free(aff);
4869 if (zero < 0)
4870 return -1;
4871 if (!zero)
4872 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4874 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
4875 aff = isl_aff_scale_down_ui(aff, 64);
4876 aff = isl_aff_floor(aff);
4877 equal = aff_check_plain_equal(aff, "{ [-1] }");
4878 isl_aff_free(aff);
4879 if (equal < 0)
4880 return -1;
4882 return 0;
4885 /* Check that the computation below results in a single expression.
4886 * One or two expressions may result depending on which constraint
4887 * ends up being considered as redundant with respect to the other
4888 * constraints after the projection that is performed internally
4889 * by isl_set_dim_min.
4891 static int test_dim_max_1(isl_ctx *ctx)
4893 const char *str;
4894 isl_set *set;
4895 isl_pw_aff *pa;
4896 int n;
4898 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
4899 "-4a <= b <= 3 and b < n - 4a }";
4900 set = isl_set_read_from_str(ctx, str);
4901 pa = isl_set_dim_min(set, 0);
4902 n = isl_pw_aff_n_piece(pa);
4903 isl_pw_aff_free(pa);
4905 if (!pa)
4906 return -1;
4907 if (n != 1)
4908 isl_die(ctx, isl_error_unknown, "expecting single expression",
4909 return -1);
4911 return 0;
4914 int test_dim_max(isl_ctx *ctx)
4916 int equal;
4917 const char *str;
4918 isl_set *set1, *set2;
4919 isl_set *set;
4920 isl_map *map;
4921 isl_pw_aff *pwaff;
4923 if (test_dim_max_1(ctx) < 0)
4924 return -1;
4926 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
4927 set = isl_set_read_from_str(ctx, str);
4928 pwaff = isl_set_dim_max(set, 0);
4929 set1 = isl_set_from_pw_aff(pwaff);
4930 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
4931 set2 = isl_set_read_from_str(ctx, str);
4932 equal = isl_set_is_equal(set1, set2);
4933 isl_set_free(set1);
4934 isl_set_free(set2);
4935 if (equal < 0)
4936 return -1;
4937 if (!equal)
4938 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4940 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
4941 set = isl_set_read_from_str(ctx, str);
4942 pwaff = isl_set_dim_max(set, 0);
4943 set1 = isl_set_from_pw_aff(pwaff);
4944 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4945 set2 = isl_set_read_from_str(ctx, str);
4946 equal = isl_set_is_equal(set1, set2);
4947 isl_set_free(set1);
4948 isl_set_free(set2);
4949 if (equal < 0)
4950 return -1;
4951 if (!equal)
4952 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4954 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
4955 set = isl_set_read_from_str(ctx, str);
4956 pwaff = isl_set_dim_max(set, 0);
4957 set1 = isl_set_from_pw_aff(pwaff);
4958 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
4959 set2 = isl_set_read_from_str(ctx, str);
4960 equal = isl_set_is_equal(set1, set2);
4961 isl_set_free(set1);
4962 isl_set_free(set2);
4963 if (equal < 0)
4964 return -1;
4965 if (!equal)
4966 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4968 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
4969 "0 <= i < N and 0 <= j < M }";
4970 map = isl_map_read_from_str(ctx, str);
4971 set = isl_map_range(map);
4973 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
4974 set1 = isl_set_from_pw_aff(pwaff);
4975 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
4976 set2 = isl_set_read_from_str(ctx, str);
4977 equal = isl_set_is_equal(set1, set2);
4978 isl_set_free(set1);
4979 isl_set_free(set2);
4981 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
4982 set1 = isl_set_from_pw_aff(pwaff);
4983 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
4984 set2 = isl_set_read_from_str(ctx, str);
4985 if (equal >= 0 && equal)
4986 equal = isl_set_is_equal(set1, set2);
4987 isl_set_free(set1);
4988 isl_set_free(set2);
4990 isl_set_free(set);
4992 if (equal < 0)
4993 return -1;
4994 if (!equal)
4995 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4997 /* Check that solutions are properly merged. */
4998 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
4999 "c <= -1 + n - 4a - 2b and c >= -2b and "
5000 "4a >= -4 + n and c >= 0 }";
5001 set = isl_set_read_from_str(ctx, str);
5002 pwaff = isl_set_dim_min(set, 2);
5003 set1 = isl_set_from_pw_aff(pwaff);
5004 str = "[n] -> { [(0)] : n >= 1 }";
5005 set2 = isl_set_read_from_str(ctx, str);
5006 equal = isl_set_is_equal(set1, set2);
5007 isl_set_free(set1);
5008 isl_set_free(set2);
5010 if (equal < 0)
5011 return -1;
5012 if (!equal)
5013 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5015 /* Check that empty solution lie in the right space. */
5016 str = "[n] -> { [t,a] : 1 = 0 }";
5017 set = isl_set_read_from_str(ctx, str);
5018 pwaff = isl_set_dim_max(set, 0);
5019 set1 = isl_set_from_pw_aff(pwaff);
5020 str = "[n] -> { [t] : 1 = 0 }";
5021 set2 = isl_set_read_from_str(ctx, str);
5022 equal = isl_set_is_equal(set1, set2);
5023 isl_set_free(set1);
5024 isl_set_free(set2);
5026 if (equal < 0)
5027 return -1;
5028 if (!equal)
5029 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5031 return 0;
5034 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
5036 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
5037 const char *str)
5039 isl_ctx *ctx;
5040 isl_pw_multi_aff *pma2;
5041 int equal;
5043 if (!pma)
5044 return -1;
5046 ctx = isl_pw_multi_aff_get_ctx(pma);
5047 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5048 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
5049 isl_pw_multi_aff_free(pma2);
5051 return equal;
5054 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
5055 * represented by "str".
5057 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
5058 const char *str)
5060 int equal;
5062 equal = pw_multi_aff_plain_is_equal(pma, str);
5063 if (equal < 0)
5064 return -1;
5065 if (!equal)
5066 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
5067 "result not as expected", return -1);
5068 return 0;
5071 /* Basic test for isl_pw_multi_aff_product.
5073 * Check that multiple pieces are properly handled.
5075 static int test_product_pma(isl_ctx *ctx)
5077 int equal;
5078 const char *str;
5079 isl_pw_multi_aff *pma1, *pma2;
5081 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
5082 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
5083 str = "{ C[] -> D[] }";
5084 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5085 pma1 = isl_pw_multi_aff_product(pma1, pma2);
5086 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
5087 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
5088 equal = pw_multi_aff_check_plain_equal(pma1, str);
5089 isl_pw_multi_aff_free(pma1);
5090 if (equal < 0)
5091 return -1;
5093 return 0;
5096 int test_product(isl_ctx *ctx)
5098 const char *str;
5099 isl_set *set;
5100 isl_union_set *uset1, *uset2;
5101 int ok;
5103 str = "{ A[i] }";
5104 set = isl_set_read_from_str(ctx, str);
5105 set = isl_set_product(set, isl_set_copy(set));
5106 ok = isl_set_is_wrapping(set);
5107 isl_set_free(set);
5108 if (ok < 0)
5109 return -1;
5110 if (!ok)
5111 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5113 str = "{ [] }";
5114 uset1 = isl_union_set_read_from_str(ctx, str);
5115 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
5116 str = "{ [[] -> []] }";
5117 uset2 = isl_union_set_read_from_str(ctx, str);
5118 ok = isl_union_set_is_equal(uset1, uset2);
5119 isl_union_set_free(uset1);
5120 isl_union_set_free(uset2);
5121 if (ok < 0)
5122 return -1;
5123 if (!ok)
5124 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5126 if (test_product_pma(ctx) < 0)
5127 return -1;
5129 return 0;
5132 /* Check that two sets are not considered disjoint just because
5133 * they have a different set of (named) parameters.
5135 static int test_disjoint(isl_ctx *ctx)
5137 const char *str;
5138 isl_set *set, *set2;
5139 int disjoint;
5141 str = "[n] -> { [[]->[]] }";
5142 set = isl_set_read_from_str(ctx, str);
5143 str = "{ [[]->[]] }";
5144 set2 = isl_set_read_from_str(ctx, str);
5145 disjoint = isl_set_is_disjoint(set, set2);
5146 isl_set_free(set);
5147 isl_set_free(set2);
5148 if (disjoint < 0)
5149 return -1;
5150 if (disjoint)
5151 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5153 return 0;
5156 /* Inputs for isl_pw_multi_aff_is_equal tests.
5157 * "f1" and "f2" are the two function that need to be compared.
5158 * "equal" is the expected result.
5160 struct {
5161 int equal;
5162 const char *f1;
5163 const char *f2;
5164 } pma_equal_tests[] = {
5165 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
5166 "[N] -> { [0] : 0 <= N <= 1 }" },
5167 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
5168 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
5169 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
5170 "[N] -> { [0] : 0 <= N <= 1 }" },
5171 { 0, "{ [NaN] }", "{ [NaN] }" },
5174 int test_equal(isl_ctx *ctx)
5176 int i;
5177 const char *str;
5178 isl_set *set, *set2;
5179 int equal;
5181 str = "{ S_6[i] }";
5182 set = isl_set_read_from_str(ctx, str);
5183 str = "{ S_7[i] }";
5184 set2 = isl_set_read_from_str(ctx, str);
5185 equal = isl_set_is_equal(set, set2);
5186 isl_set_free(set);
5187 isl_set_free(set2);
5188 if (equal < 0)
5189 return -1;
5190 if (equal)
5191 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5193 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
5194 int expected = pma_equal_tests[i].equal;
5195 isl_pw_multi_aff *f1, *f2;
5197 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
5198 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
5199 equal = isl_pw_multi_aff_is_equal(f1, f2);
5200 isl_pw_multi_aff_free(f1);
5201 isl_pw_multi_aff_free(f2);
5202 if (equal < 0)
5203 return -1;
5204 if (equal != expected)
5205 isl_die(ctx, isl_error_unknown,
5206 "unexpected equality result", return -1);
5209 return 0;
5212 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
5213 enum isl_dim_type type, unsigned pos, int fixed)
5215 isl_bool test;
5217 test = isl_map_plain_is_fixed(map, type, pos, NULL);
5218 isl_map_free(map);
5219 if (test < 0)
5220 return -1;
5221 if (test == fixed)
5222 return 0;
5223 if (fixed)
5224 isl_die(ctx, isl_error_unknown,
5225 "map not detected as fixed", return -1);
5226 else
5227 isl_die(ctx, isl_error_unknown,
5228 "map detected as fixed", return -1);
5231 int test_fixed(isl_ctx *ctx)
5233 const char *str;
5234 isl_map *map;
5236 str = "{ [i] -> [i] }";
5237 map = isl_map_read_from_str(ctx, str);
5238 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
5239 return -1;
5240 str = "{ [i] -> [1] }";
5241 map = isl_map_read_from_str(ctx, str);
5242 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5243 return -1;
5244 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
5245 map = isl_map_read_from_str(ctx, str);
5246 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5247 return -1;
5248 map = isl_map_read_from_str(ctx, str);
5249 map = isl_map_neg(map);
5250 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5251 return -1;
5253 return 0;
5256 struct isl_vertices_test_data {
5257 const char *set;
5258 int n;
5259 const char *vertex[6];
5260 } vertices_tests[] = {
5261 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
5262 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
5263 { "{ A[t, i] : t = 14 and i = 1 }",
5264 1, { "{ A[14, 1] }" } },
5265 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
5266 "c <= m and m <= n and m > 0 }",
5267 6, {
5268 "[n, m] -> { [n, m, m] : 0 < m <= n }",
5269 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
5270 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
5271 "[n, m] -> { [m, m, m] : 0 < m <= n }",
5272 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
5273 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
5274 } },
5277 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
5279 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
5281 struct isl_vertices_test_data *data = user;
5282 isl_ctx *ctx;
5283 isl_multi_aff *ma;
5284 isl_basic_set *bset;
5285 isl_pw_multi_aff *pma;
5286 int i;
5287 isl_bool equal;
5289 ctx = isl_vertex_get_ctx(vertex);
5290 bset = isl_vertex_get_domain(vertex);
5291 ma = isl_vertex_get_expr(vertex);
5292 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
5294 for (i = 0; i < data->n; ++i) {
5295 isl_pw_multi_aff *pma_i;
5297 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
5298 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
5299 isl_pw_multi_aff_free(pma_i);
5301 if (equal < 0 || equal)
5302 break;
5305 isl_pw_multi_aff_free(pma);
5306 isl_vertex_free(vertex);
5308 if (equal < 0)
5309 return isl_stat_error;
5311 return equal ? isl_stat_ok : isl_stat_error;
5314 int test_vertices(isl_ctx *ctx)
5316 int i;
5318 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
5319 isl_basic_set *bset;
5320 isl_vertices *vertices;
5321 int ok = 1;
5322 int n;
5324 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
5325 vertices = isl_basic_set_compute_vertices(bset);
5326 n = isl_vertices_get_n_vertices(vertices);
5327 if (vertices_tests[i].n != n)
5328 ok = 0;
5329 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
5330 &vertices_tests[i]) < 0)
5331 ok = 0;
5332 isl_vertices_free(vertices);
5333 isl_basic_set_free(bset);
5335 if (!vertices)
5336 return -1;
5337 if (!ok)
5338 isl_die(ctx, isl_error_unknown, "unexpected vertices",
5339 return -1);
5342 return 0;
5345 int test_union_pw(isl_ctx *ctx)
5347 int equal;
5348 const char *str;
5349 isl_union_set *uset;
5350 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
5352 str = "{ [x] -> x^2 }";
5353 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
5354 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
5355 uset = isl_union_pw_qpolynomial_domain(upwqp1);
5356 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
5357 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
5358 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
5359 isl_union_pw_qpolynomial_free(upwqp1);
5360 isl_union_pw_qpolynomial_free(upwqp2);
5361 if (equal < 0)
5362 return -1;
5363 if (!equal)
5364 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5366 return 0;
5369 /* Test that isl_union_pw_qpolynomial_eval picks up the function
5370 * defined over the correct domain space.
5372 static int test_eval_1(isl_ctx *ctx)
5374 const char *str;
5375 isl_point *pnt;
5376 isl_set *set;
5377 isl_union_pw_qpolynomial *upwqp;
5378 isl_val *v;
5379 int cmp;
5381 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
5382 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
5383 str = "{ A[6] }";
5384 set = isl_set_read_from_str(ctx, str);
5385 pnt = isl_set_sample_point(set);
5386 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
5387 cmp = isl_val_cmp_si(v, 36);
5388 isl_val_free(v);
5390 if (!v)
5391 return -1;
5392 if (cmp != 0)
5393 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
5395 return 0;
5398 /* Check that isl_qpolynomial_eval handles getting called on a void point.
5400 static int test_eval_2(isl_ctx *ctx)
5402 const char *str;
5403 isl_point *pnt;
5404 isl_set *set;
5405 isl_qpolynomial *qp;
5406 isl_val *v;
5407 isl_bool ok;
5409 str = "{ A[x] -> [x] }";
5410 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
5411 str = "{ A[x] : false }";
5412 set = isl_set_read_from_str(ctx, str);
5413 pnt = isl_set_sample_point(set);
5414 v = isl_qpolynomial_eval(qp, pnt);
5415 ok = isl_val_is_nan(v);
5416 isl_val_free(v);
5418 if (ok < 0)
5419 return -1;
5420 if (!ok)
5421 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
5423 return 0;
5426 /* Perform basic polynomial evaluation tests.
5428 static int test_eval(isl_ctx *ctx)
5430 if (test_eval_1(ctx) < 0)
5431 return -1;
5432 if (test_eval_2(ctx) < 0)
5433 return -1;
5434 return 0;
5437 /* Descriptions of sets that are tested for reparsing after printing.
5439 const char *output_tests[] = {
5440 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
5443 /* Check that printing a set and reparsing a set from the printed output
5444 * results in the same set.
5446 static int test_output_set(isl_ctx *ctx)
5448 int i;
5449 char *str;
5450 isl_set *set1, *set2;
5451 isl_bool equal;
5453 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
5454 set1 = isl_set_read_from_str(ctx, output_tests[i]);
5455 str = isl_set_to_str(set1);
5456 set2 = isl_set_read_from_str(ctx, str);
5457 free(str);
5458 equal = isl_set_is_equal(set1, set2);
5459 isl_set_free(set1);
5460 isl_set_free(set2);
5461 if (equal < 0)
5462 return -1;
5463 if (!equal)
5464 isl_die(ctx, isl_error_unknown,
5465 "parsed output not the same", return -1);
5468 return 0;
5471 int test_output(isl_ctx *ctx)
5473 char *s;
5474 const char *str;
5475 isl_pw_aff *pa;
5476 isl_printer *p;
5477 int equal;
5479 if (test_output_set(ctx) < 0)
5480 return -1;
5482 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
5483 pa = isl_pw_aff_read_from_str(ctx, str);
5485 p = isl_printer_to_str(ctx);
5486 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
5487 p = isl_printer_print_pw_aff(p, pa);
5488 s = isl_printer_get_str(p);
5489 isl_printer_free(p);
5490 isl_pw_aff_free(pa);
5491 if (!s)
5492 equal = -1;
5493 else
5494 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
5495 free(s);
5496 if (equal < 0)
5497 return -1;
5498 if (!equal)
5499 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5501 return 0;
5504 int test_sample(isl_ctx *ctx)
5506 const char *str;
5507 isl_basic_set *bset1, *bset2;
5508 int empty, subset;
5510 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
5511 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
5512 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
5513 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
5514 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
5515 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
5516 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
5517 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
5518 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
5519 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
5520 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
5521 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
5522 "d - 1073741821e and "
5523 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
5524 "3j >= 1 - a + b + 2e and "
5525 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
5526 "3i <= 4 - a + 4b - e and "
5527 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
5528 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
5529 "c <= -1 + a and 3i >= -2 - a + 3e and "
5530 "1073741822e <= 1073741823 - a + 1073741822b + c and "
5531 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
5532 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
5533 "1073741823e >= 1 + 1073741823b - d and "
5534 "3i >= 1073741823b + c - 1073741823e - f and "
5535 "3i >= 1 + 2b + e + 3g }";
5536 bset1 = isl_basic_set_read_from_str(ctx, str);
5537 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
5538 empty = isl_basic_set_is_empty(bset2);
5539 subset = isl_basic_set_is_subset(bset2, bset1);
5540 isl_basic_set_free(bset1);
5541 isl_basic_set_free(bset2);
5542 if (empty < 0 || subset < 0)
5543 return -1;
5544 if (empty)
5545 isl_die(ctx, isl_error_unknown, "point not found", return -1);
5546 if (!subset)
5547 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
5549 return 0;
5552 int test_fixed_power(isl_ctx *ctx)
5554 const char *str;
5555 isl_map *map;
5556 isl_int exp;
5557 int equal;
5559 isl_int_init(exp);
5560 str = "{ [i] -> [i + 1] }";
5561 map = isl_map_read_from_str(ctx, str);
5562 isl_int_set_si(exp, 23);
5563 map = isl_map_fixed_power(map, exp);
5564 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
5565 isl_int_clear(exp);
5566 isl_map_free(map);
5567 if (equal < 0)
5568 return -1;
5570 return 0;
5573 int test_slice(isl_ctx *ctx)
5575 const char *str;
5576 isl_map *map;
5577 int equal;
5579 str = "{ [i] -> [j] }";
5580 map = isl_map_read_from_str(ctx, str);
5581 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
5582 equal = map_check_equal(map, "{ [i] -> [i] }");
5583 isl_map_free(map);
5584 if (equal < 0)
5585 return -1;
5587 str = "{ [i] -> [j] }";
5588 map = isl_map_read_from_str(ctx, str);
5589 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
5590 equal = map_check_equal(map, "{ [i] -> [j] }");
5591 isl_map_free(map);
5592 if (equal < 0)
5593 return -1;
5595 str = "{ [i] -> [j] }";
5596 map = isl_map_read_from_str(ctx, str);
5597 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
5598 equal = map_check_equal(map, "{ [i] -> [-i] }");
5599 isl_map_free(map);
5600 if (equal < 0)
5601 return -1;
5603 str = "{ [i] -> [j] }";
5604 map = isl_map_read_from_str(ctx, str);
5605 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
5606 equal = map_check_equal(map, "{ [0] -> [j] }");
5607 isl_map_free(map);
5608 if (equal < 0)
5609 return -1;
5611 str = "{ [i] -> [j] }";
5612 map = isl_map_read_from_str(ctx, str);
5613 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
5614 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
5615 isl_map_free(map);
5616 if (equal < 0)
5617 return -1;
5619 str = "{ [i] -> [j] }";
5620 map = isl_map_read_from_str(ctx, str);
5621 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
5622 equal = map_check_equal(map, "{ [i] -> [j] : false }");
5623 isl_map_free(map);
5624 if (equal < 0)
5625 return -1;
5627 return 0;
5630 int test_eliminate(isl_ctx *ctx)
5632 const char *str;
5633 isl_map *map;
5634 int equal;
5636 str = "{ [i] -> [j] : i = 2j }";
5637 map = isl_map_read_from_str(ctx, str);
5638 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
5639 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
5640 isl_map_free(map);
5641 if (equal < 0)
5642 return -1;
5644 return 0;
5647 /* Check that isl_set_dim_residue_class detects that the values of j
5648 * in the set below are all odd and that it does not detect any spurious
5649 * strides.
5651 static int test_residue_class(isl_ctx *ctx)
5653 const char *str;
5654 isl_set *set;
5655 isl_int m, r;
5656 isl_stat res;
5658 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
5659 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
5660 set = isl_set_read_from_str(ctx, str);
5661 isl_int_init(m);
5662 isl_int_init(r);
5663 res = isl_set_dim_residue_class(set, 1, &m, &r);
5664 if (res >= 0 &&
5665 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
5666 isl_die(ctx, isl_error_unknown, "incorrect residue class",
5667 res = isl_stat_error);
5668 isl_int_clear(r);
5669 isl_int_clear(m);
5670 isl_set_free(set);
5672 return res;
5675 int test_align_parameters(isl_ctx *ctx)
5677 const char *str;
5678 isl_space *space;
5679 isl_multi_aff *ma1, *ma2;
5680 int equal;
5682 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
5683 ma1 = isl_multi_aff_read_from_str(ctx, str);
5685 space = isl_space_params_alloc(ctx, 1);
5686 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
5687 ma1 = isl_multi_aff_align_params(ma1, space);
5689 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
5690 ma2 = isl_multi_aff_read_from_str(ctx, str);
5692 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5694 isl_multi_aff_free(ma1);
5695 isl_multi_aff_free(ma2);
5697 if (equal < 0)
5698 return -1;
5699 if (!equal)
5700 isl_die(ctx, isl_error_unknown,
5701 "result not as expected", return -1);
5703 return 0;
5706 static int test_list(isl_ctx *ctx)
5708 isl_id *a, *b, *c, *d, *id;
5709 isl_id_list *list;
5710 int ok;
5712 a = isl_id_alloc(ctx, "a", NULL);
5713 b = isl_id_alloc(ctx, "b", NULL);
5714 c = isl_id_alloc(ctx, "c", NULL);
5715 d = isl_id_alloc(ctx, "d", NULL);
5717 list = isl_id_list_alloc(ctx, 4);
5718 list = isl_id_list_add(list, b);
5719 list = isl_id_list_insert(list, 0, a);
5720 list = isl_id_list_add(list, c);
5721 list = isl_id_list_add(list, d);
5722 list = isl_id_list_drop(list, 1, 1);
5724 if (isl_id_list_n_id(list) != 3) {
5725 isl_id_list_free(list);
5726 isl_die(ctx, isl_error_unknown,
5727 "unexpected number of elements in list", return -1);
5730 id = isl_id_list_get_id(list, 0);
5731 ok = id == a;
5732 isl_id_free(id);
5733 id = isl_id_list_get_id(list, 1);
5734 ok = ok && id == c;
5735 isl_id_free(id);
5736 id = isl_id_list_get_id(list, 2);
5737 ok = ok && id == d;
5738 isl_id_free(id);
5740 isl_id_list_free(list);
5742 if (!ok)
5743 isl_die(ctx, isl_error_unknown,
5744 "unexpected elements in list", return -1);
5746 return 0;
5749 const char *set_conversion_tests[] = {
5750 "[N] -> { [i] : N - 1 <= 2 i <= N }",
5751 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
5752 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5753 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5754 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
5755 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
5756 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
5757 "-3 + c <= d <= 28 + c) }",
5760 /* Check that converting from isl_set to isl_pw_multi_aff and back
5761 * to isl_set produces the original isl_set.
5763 static int test_set_conversion(isl_ctx *ctx)
5765 int i;
5766 const char *str;
5767 isl_set *set1, *set2;
5768 isl_pw_multi_aff *pma;
5769 int equal;
5771 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
5772 str = set_conversion_tests[i];
5773 set1 = isl_set_read_from_str(ctx, str);
5774 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
5775 set2 = isl_set_from_pw_multi_aff(pma);
5776 equal = isl_set_is_equal(set1, set2);
5777 isl_set_free(set1);
5778 isl_set_free(set2);
5780 if (equal < 0)
5781 return -1;
5782 if (!equal)
5783 isl_die(ctx, isl_error_unknown, "bad conversion",
5784 return -1);
5787 return 0;
5790 const char *conversion_tests[] = {
5791 "{ [a, b, c, d] -> s0[a, b, e, f] : "
5792 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
5793 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
5794 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
5795 "9e <= -2 - 2a) }",
5796 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
5797 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
5798 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
5801 /* Check that converting from isl_map to isl_pw_multi_aff and back
5802 * to isl_map produces the original isl_map.
5804 static int test_map_conversion(isl_ctx *ctx)
5806 int i;
5807 isl_map *map1, *map2;
5808 isl_pw_multi_aff *pma;
5809 int equal;
5811 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
5812 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
5813 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
5814 map2 = isl_map_from_pw_multi_aff(pma);
5815 equal = isl_map_is_equal(map1, map2);
5816 isl_map_free(map1);
5817 isl_map_free(map2);
5819 if (equal < 0)
5820 return -1;
5821 if (!equal)
5822 isl_die(ctx, isl_error_unknown, "bad conversion",
5823 return -1);
5826 return 0;
5829 static int test_conversion(isl_ctx *ctx)
5831 if (test_set_conversion(ctx) < 0)
5832 return -1;
5833 if (test_map_conversion(ctx) < 0)
5834 return -1;
5835 return 0;
5838 /* Check that isl_basic_map_curry does not modify input.
5840 static int test_curry(isl_ctx *ctx)
5842 const char *str;
5843 isl_basic_map *bmap1, *bmap2;
5844 int equal;
5846 str = "{ [A[] -> B[]] -> C[] }";
5847 bmap1 = isl_basic_map_read_from_str(ctx, str);
5848 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
5849 equal = isl_basic_map_is_equal(bmap1, bmap2);
5850 isl_basic_map_free(bmap1);
5851 isl_basic_map_free(bmap2);
5853 if (equal < 0)
5854 return -1;
5855 if (equal)
5856 isl_die(ctx, isl_error_unknown,
5857 "curried map should not be equal to original",
5858 return -1);
5860 return 0;
5863 struct {
5864 const char *set;
5865 const char *ma;
5866 const char *res;
5867 } preimage_tests[] = {
5868 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
5869 "{ A[j,i] -> B[i,j] }",
5870 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
5871 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5872 "{ A[a,b] -> B[a/2,b/6] }",
5873 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
5874 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
5875 "{ A[a,b] -> B[a/2,b/6] }",
5876 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
5877 "exists i,j : a = 2 i and b = 6 j }" },
5878 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
5879 "[n] -> { : 0 <= n <= 100 }" },
5880 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5881 "{ A[a] -> B[2a] }",
5882 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
5883 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
5884 "{ A[a] -> B[([a/2])] }",
5885 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
5886 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
5887 "{ A[a] -> B[a,a,a/3] }",
5888 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
5889 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
5890 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
5893 static int test_preimage_basic_set(isl_ctx *ctx)
5895 int i;
5896 isl_basic_set *bset1, *bset2;
5897 isl_multi_aff *ma;
5898 int equal;
5900 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
5901 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
5902 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
5903 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
5904 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
5905 equal = isl_basic_set_is_equal(bset1, bset2);
5906 isl_basic_set_free(bset1);
5907 isl_basic_set_free(bset2);
5908 if (equal < 0)
5909 return -1;
5910 if (!equal)
5911 isl_die(ctx, isl_error_unknown, "bad preimage",
5912 return -1);
5915 return 0;
5918 struct {
5919 const char *map;
5920 const char *ma;
5921 const char *res;
5922 } preimage_domain_tests[] = {
5923 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
5924 "{ A[j,i] -> B[i,j] }",
5925 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
5926 { "{ B[i] -> C[i]; D[i] -> E[i] }",
5927 "{ A[i] -> B[i + 1] }",
5928 "{ A[i] -> C[i + 1] }" },
5929 { "{ B[i] -> C[i]; B[i] -> E[i] }",
5930 "{ A[i] -> B[i + 1] }",
5931 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
5932 { "{ B[i] -> C[([i/2])] }",
5933 "{ A[i] -> B[2i] }",
5934 "{ A[i] -> C[i] }" },
5935 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
5936 "{ A[i] -> B[([i/5]), ([i/7])] }",
5937 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
5938 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
5939 "[N] -> { A[] -> B[([N/5])] }",
5940 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
5941 { "{ B[i] -> C[i] : exists a : i = 5 a }",
5942 "{ A[i] -> B[2i] }",
5943 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
5944 { "{ B[i] -> C[i] : exists a : i = 2 a; "
5945 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
5946 "{ A[i] -> B[2i] }",
5947 "{ A[i] -> C[2i] }" },
5948 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
5949 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
5952 static int test_preimage_union_map(isl_ctx *ctx)
5954 int i;
5955 isl_union_map *umap1, *umap2;
5956 isl_multi_aff *ma;
5957 int equal;
5959 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
5960 umap1 = isl_union_map_read_from_str(ctx,
5961 preimage_domain_tests[i].map);
5962 ma = isl_multi_aff_read_from_str(ctx,
5963 preimage_domain_tests[i].ma);
5964 umap2 = isl_union_map_read_from_str(ctx,
5965 preimage_domain_tests[i].res);
5966 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
5967 equal = isl_union_map_is_equal(umap1, umap2);
5968 isl_union_map_free(umap1);
5969 isl_union_map_free(umap2);
5970 if (equal < 0)
5971 return -1;
5972 if (!equal)
5973 isl_die(ctx, isl_error_unknown, "bad preimage",
5974 return -1);
5977 return 0;
5980 static int test_preimage(isl_ctx *ctx)
5982 if (test_preimage_basic_set(ctx) < 0)
5983 return -1;
5984 if (test_preimage_union_map(ctx) < 0)
5985 return -1;
5987 return 0;
5990 struct {
5991 const char *ma1;
5992 const char *ma;
5993 const char *res;
5994 } pullback_tests[] = {
5995 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
5996 "{ A[a,b] -> C[b + 2a] }" },
5997 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
5998 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
5999 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
6000 "{ A[a] -> C[(a)/6] }" },
6001 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
6002 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
6003 "{ A[a] -> C[(2a)/3] }" },
6004 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
6005 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
6006 "{ A[i,j] -> C[i + j, i + j] }"},
6007 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
6008 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
6009 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
6010 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
6011 "{ [i, j] -> [floor((i)/3), j] }",
6012 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
6015 static int test_pullback(isl_ctx *ctx)
6017 int i;
6018 isl_multi_aff *ma1, *ma2;
6019 isl_multi_aff *ma;
6020 int equal;
6022 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
6023 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
6024 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
6025 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
6026 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
6027 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
6028 isl_multi_aff_free(ma1);
6029 isl_multi_aff_free(ma2);
6030 if (equal < 0)
6031 return -1;
6032 if (!equal)
6033 isl_die(ctx, isl_error_unknown, "bad pullback",
6034 return -1);
6037 return 0;
6040 /* Check that negation is printed correctly and that equal expressions
6041 * are correctly identified.
6043 static int test_ast(isl_ctx *ctx)
6045 isl_ast_expr *expr, *expr1, *expr2, *expr3;
6046 char *str;
6047 int ok, equal;
6049 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
6050 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
6051 expr = isl_ast_expr_add(expr1, expr2);
6052 expr2 = isl_ast_expr_copy(expr);
6053 expr = isl_ast_expr_neg(expr);
6054 expr2 = isl_ast_expr_neg(expr2);
6055 equal = isl_ast_expr_is_equal(expr, expr2);
6056 str = isl_ast_expr_to_C_str(expr);
6057 ok = str ? !strcmp(str, "-(A + B)") : -1;
6058 free(str);
6059 isl_ast_expr_free(expr);
6060 isl_ast_expr_free(expr2);
6062 if (ok < 0 || equal < 0)
6063 return -1;
6064 if (!equal)
6065 isl_die(ctx, isl_error_unknown,
6066 "equal expressions not considered equal", return -1);
6067 if (!ok)
6068 isl_die(ctx, isl_error_unknown,
6069 "isl_ast_expr printed incorrectly", return -1);
6071 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
6072 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
6073 expr = isl_ast_expr_add(expr1, expr2);
6074 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
6075 expr = isl_ast_expr_sub(expr3, expr);
6076 str = isl_ast_expr_to_C_str(expr);
6077 ok = str ? !strcmp(str, "C - (A + B)") : -1;
6078 free(str);
6079 isl_ast_expr_free(expr);
6081 if (ok < 0)
6082 return -1;
6083 if (!ok)
6084 isl_die(ctx, isl_error_unknown,
6085 "isl_ast_expr printed incorrectly", return -1);
6087 return 0;
6090 /* Check that isl_ast_build_expr_from_set returns a valid expression
6091 * for an empty set. Note that isl_ast_build_expr_from_set getting
6092 * called on an empty set probably indicates a bug in the caller.
6094 static int test_ast_build(isl_ctx *ctx)
6096 isl_set *set;
6097 isl_ast_build *build;
6098 isl_ast_expr *expr;
6100 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6101 build = isl_ast_build_from_context(set);
6103 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
6104 expr = isl_ast_build_expr_from_set(build, set);
6106 isl_ast_expr_free(expr);
6107 isl_ast_build_free(build);
6109 if (!expr)
6110 return -1;
6112 return 0;
6115 /* Internal data structure for before_for and after_for callbacks.
6117 * depth is the current depth
6118 * before is the number of times before_for has been called
6119 * after is the number of times after_for has been called
6121 struct isl_test_codegen_data {
6122 int depth;
6123 int before;
6124 int after;
6127 /* This function is called before each for loop in the AST generated
6128 * from test_ast_gen1.
6130 * Increment the number of calls and the depth.
6131 * Check that the space returned by isl_ast_build_get_schedule_space
6132 * matches the target space of the schedule returned by
6133 * isl_ast_build_get_schedule.
6134 * Return an isl_id that is checked by the corresponding call
6135 * to after_for.
6137 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
6138 void *user)
6140 struct isl_test_codegen_data *data = user;
6141 isl_ctx *ctx;
6142 isl_space *space;
6143 isl_union_map *schedule;
6144 isl_union_set *uset;
6145 isl_set *set;
6146 int empty;
6147 char name[] = "d0";
6149 ctx = isl_ast_build_get_ctx(build);
6151 if (data->before >= 3)
6152 isl_die(ctx, isl_error_unknown,
6153 "unexpected number of for nodes", return NULL);
6154 if (data->depth >= 2)
6155 isl_die(ctx, isl_error_unknown,
6156 "unexpected depth", return NULL);
6158 snprintf(name, sizeof(name), "d%d", data->depth);
6159 data->before++;
6160 data->depth++;
6162 schedule = isl_ast_build_get_schedule(build);
6163 uset = isl_union_map_range(schedule);
6164 if (!uset)
6165 return NULL;
6166 if (isl_union_set_n_set(uset) != 1) {
6167 isl_union_set_free(uset);
6168 isl_die(ctx, isl_error_unknown,
6169 "expecting single range space", return NULL);
6172 space = isl_ast_build_get_schedule_space(build);
6173 set = isl_union_set_extract_set(uset, space);
6174 isl_union_set_free(uset);
6175 empty = isl_set_is_empty(set);
6176 isl_set_free(set);
6178 if (empty < 0)
6179 return NULL;
6180 if (empty)
6181 isl_die(ctx, isl_error_unknown,
6182 "spaces don't match", return NULL);
6184 return isl_id_alloc(ctx, name, NULL);
6187 /* This function is called after each for loop in the AST generated
6188 * from test_ast_gen1.
6190 * Increment the number of calls and decrement the depth.
6191 * Check that the annotation attached to the node matches
6192 * the isl_id returned by the corresponding call to before_for.
6194 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
6195 __isl_keep isl_ast_build *build, void *user)
6197 struct isl_test_codegen_data *data = user;
6198 isl_id *id;
6199 const char *name;
6200 int valid;
6202 data->after++;
6203 data->depth--;
6205 if (data->after > data->before)
6206 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
6207 "mismatch in number of for nodes",
6208 return isl_ast_node_free(node));
6210 id = isl_ast_node_get_annotation(node);
6211 if (!id)
6212 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
6213 "missing annotation", return isl_ast_node_free(node));
6215 name = isl_id_get_name(id);
6216 valid = name && atoi(name + 1) == data->depth;
6217 isl_id_free(id);
6219 if (!valid)
6220 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
6221 "wrong annotation", return isl_ast_node_free(node));
6223 return node;
6226 /* Check that the before_each_for and after_each_for callbacks
6227 * are called for each for loop in the generated code,
6228 * that they are called in the right order and that the isl_id
6229 * returned from the before_each_for callback is attached to
6230 * the isl_ast_node passed to the corresponding after_each_for call.
6232 static int test_ast_gen1(isl_ctx *ctx)
6234 const char *str;
6235 isl_set *set;
6236 isl_union_map *schedule;
6237 isl_ast_build *build;
6238 isl_ast_node *tree;
6239 struct isl_test_codegen_data data;
6241 str = "[N] -> { : N >= 10 }";
6242 set = isl_set_read_from_str(ctx, str);
6243 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
6244 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
6245 schedule = isl_union_map_read_from_str(ctx, str);
6247 data.before = 0;
6248 data.after = 0;
6249 data.depth = 0;
6250 build = isl_ast_build_from_context(set);
6251 build = isl_ast_build_set_before_each_for(build,
6252 &before_for, &data);
6253 build = isl_ast_build_set_after_each_for(build,
6254 &after_for, &data);
6255 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6256 isl_ast_build_free(build);
6257 if (!tree)
6258 return -1;
6260 isl_ast_node_free(tree);
6262 if (data.before != 3 || data.after != 3)
6263 isl_die(ctx, isl_error_unknown,
6264 "unexpected number of for nodes", return -1);
6266 return 0;
6269 /* Check that the AST generator handles domains that are integrally disjoint
6270 * but not rationally disjoint.
6272 static int test_ast_gen2(isl_ctx *ctx)
6274 const char *str;
6275 isl_set *set;
6276 isl_union_map *schedule;
6277 isl_union_map *options;
6278 isl_ast_build *build;
6279 isl_ast_node *tree;
6281 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
6282 schedule = isl_union_map_read_from_str(ctx, str);
6283 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6284 build = isl_ast_build_from_context(set);
6286 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
6287 options = isl_union_map_read_from_str(ctx, str);
6288 build = isl_ast_build_set_options(build, options);
6289 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6290 isl_ast_build_free(build);
6291 if (!tree)
6292 return -1;
6293 isl_ast_node_free(tree);
6295 return 0;
6298 /* Increment *user on each call.
6300 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
6301 __isl_keep isl_ast_build *build, void *user)
6303 int *n = user;
6305 (*n)++;
6307 return node;
6310 /* Test that unrolling tries to minimize the number of instances.
6311 * In particular, for the schedule given below, make sure it generates
6312 * 3 nodes (rather than 101).
6314 static int test_ast_gen3(isl_ctx *ctx)
6316 const char *str;
6317 isl_set *set;
6318 isl_union_map *schedule;
6319 isl_union_map *options;
6320 isl_ast_build *build;
6321 isl_ast_node *tree;
6322 int n_domain = 0;
6324 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
6325 schedule = isl_union_map_read_from_str(ctx, str);
6326 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6328 str = "{ [i] -> unroll[0] }";
6329 options = isl_union_map_read_from_str(ctx, str);
6331 build = isl_ast_build_from_context(set);
6332 build = isl_ast_build_set_options(build, options);
6333 build = isl_ast_build_set_at_each_domain(build,
6334 &count_domains, &n_domain);
6335 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6336 isl_ast_build_free(build);
6337 if (!tree)
6338 return -1;
6340 isl_ast_node_free(tree);
6342 if (n_domain != 3)
6343 isl_die(ctx, isl_error_unknown,
6344 "unexpected number of for nodes", return -1);
6346 return 0;
6349 /* Check that if the ast_build_exploit_nested_bounds options is set,
6350 * we do not get an outer if node in the generated AST,
6351 * while we do get such an outer if node if the options is not set.
6353 static int test_ast_gen4(isl_ctx *ctx)
6355 const char *str;
6356 isl_set *set;
6357 isl_union_map *schedule;
6358 isl_ast_build *build;
6359 isl_ast_node *tree;
6360 enum isl_ast_node_type type;
6361 int enb;
6363 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
6364 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
6366 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
6368 schedule = isl_union_map_read_from_str(ctx, str);
6369 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6370 build = isl_ast_build_from_context(set);
6371 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6372 isl_ast_build_free(build);
6373 if (!tree)
6374 return -1;
6376 type = isl_ast_node_get_type(tree);
6377 isl_ast_node_free(tree);
6379 if (type == isl_ast_node_if)
6380 isl_die(ctx, isl_error_unknown,
6381 "not expecting if node", return -1);
6383 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
6385 schedule = isl_union_map_read_from_str(ctx, str);
6386 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6387 build = isl_ast_build_from_context(set);
6388 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6389 isl_ast_build_free(build);
6390 if (!tree)
6391 return -1;
6393 type = isl_ast_node_get_type(tree);
6394 isl_ast_node_free(tree);
6396 if (type != isl_ast_node_if)
6397 isl_die(ctx, isl_error_unknown,
6398 "expecting if node", return -1);
6400 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
6402 return 0;
6405 /* This function is called for each leaf in the AST generated
6406 * from test_ast_gen5.
6408 * We finalize the AST generation by extending the outer schedule
6409 * with a zero-dimensional schedule. If this results in any for loops,
6410 * then this means that we did not pass along enough information
6411 * about the outer schedule to the inner AST generation.
6413 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
6414 void *user)
6416 isl_union_map *schedule, *extra;
6417 isl_ast_node *tree;
6419 schedule = isl_ast_build_get_schedule(build);
6420 extra = isl_union_map_copy(schedule);
6421 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
6422 schedule = isl_union_map_range_product(schedule, extra);
6423 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6424 isl_ast_build_free(build);
6426 if (!tree)
6427 return NULL;
6429 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
6430 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
6431 "code should not contain any for loop",
6432 return isl_ast_node_free(tree));
6434 return tree;
6437 /* Check that we do not lose any information when going back and
6438 * forth between internal and external schedule.
6440 * In particular, we create an AST where we unroll the only
6441 * non-constant dimension in the schedule. We therefore do
6442 * not expect any for loops in the AST. However, older versions
6443 * of isl would not pass along enough information about the outer
6444 * schedule when performing an inner code generation from a create_leaf
6445 * callback, resulting in the inner code generation producing a for loop.
6447 static int test_ast_gen5(isl_ctx *ctx)
6449 const char *str;
6450 isl_set *set;
6451 isl_union_map *schedule, *options;
6452 isl_ast_build *build;
6453 isl_ast_node *tree;
6455 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
6456 schedule = isl_union_map_read_from_str(ctx, str);
6458 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
6459 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
6460 options = isl_union_map_read_from_str(ctx, str);
6462 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6463 build = isl_ast_build_from_context(set);
6464 build = isl_ast_build_set_options(build, options);
6465 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
6466 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6467 isl_ast_build_free(build);
6468 isl_ast_node_free(tree);
6469 if (!tree)
6470 return -1;
6472 return 0;
6475 /* Check that the expression
6477 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
6479 * is not combined into
6481 * min(n/2, 0)
6483 * as this would result in n/2 being evaluated in parts of
6484 * the definition domain where n is not a multiple of 2.
6486 static int test_ast_expr(isl_ctx *ctx)
6488 const char *str;
6489 isl_pw_aff *pa;
6490 isl_ast_build *build;
6491 isl_ast_expr *expr;
6492 int min_max;
6493 int is_min;
6495 min_max = isl_options_get_ast_build_detect_min_max(ctx);
6496 isl_options_set_ast_build_detect_min_max(ctx, 1);
6498 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
6499 pa = isl_pw_aff_read_from_str(ctx, str);
6500 build = isl_ast_build_alloc(ctx);
6501 expr = isl_ast_build_expr_from_pw_aff(build, pa);
6502 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
6503 isl_ast_expr_get_op_type(expr) == isl_ast_op_min;
6504 isl_ast_build_free(build);
6505 isl_ast_expr_free(expr);
6507 isl_options_set_ast_build_detect_min_max(ctx, min_max);
6509 if (!expr)
6510 return -1;
6511 if (is_min)
6512 isl_die(ctx, isl_error_unknown,
6513 "expressions should not be combined", return -1);
6515 return 0;
6518 static int test_ast_gen(isl_ctx *ctx)
6520 if (test_ast_gen1(ctx) < 0)
6521 return -1;
6522 if (test_ast_gen2(ctx) < 0)
6523 return -1;
6524 if (test_ast_gen3(ctx) < 0)
6525 return -1;
6526 if (test_ast_gen4(ctx) < 0)
6527 return -1;
6528 if (test_ast_gen5(ctx) < 0)
6529 return -1;
6530 if (test_ast_expr(ctx) < 0)
6531 return -1;
6532 return 0;
6535 /* Check if dropping output dimensions from an isl_pw_multi_aff
6536 * works properly.
6538 static int test_pw_multi_aff(isl_ctx *ctx)
6540 const char *str;
6541 isl_pw_multi_aff *pma1, *pma2;
6542 int equal;
6544 str = "{ [i,j] -> [i+j, 4i-j] }";
6545 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
6546 str = "{ [i,j] -> [4i-j] }";
6547 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6549 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
6551 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6553 isl_pw_multi_aff_free(pma1);
6554 isl_pw_multi_aff_free(pma2);
6555 if (equal < 0)
6556 return -1;
6557 if (!equal)
6558 isl_die(ctx, isl_error_unknown,
6559 "expressions not equal", return -1);
6561 return 0;
6564 /* Check that we can properly parse multi piecewise affine expressions
6565 * where the piecewise affine expressions have different domains.
6567 static int test_multi_pw_aff(isl_ctx *ctx)
6569 const char *str;
6570 isl_set *dom, *dom2;
6571 isl_multi_pw_aff *mpa1, *mpa2;
6572 isl_pw_aff *pa;
6573 int equal;
6574 int equal_domain;
6576 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
6577 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
6578 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
6579 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
6580 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
6581 str = "{ [i] -> [(i : i > 0), 2i] }";
6582 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
6584 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
6586 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
6587 dom = isl_pw_aff_domain(pa);
6588 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
6589 dom2 = isl_pw_aff_domain(pa);
6590 equal_domain = isl_set_is_equal(dom, dom2);
6592 isl_set_free(dom);
6593 isl_set_free(dom2);
6594 isl_multi_pw_aff_free(mpa1);
6595 isl_multi_pw_aff_free(mpa2);
6597 if (equal < 0)
6598 return -1;
6599 if (!equal)
6600 isl_die(ctx, isl_error_unknown,
6601 "expressions not equal", return -1);
6603 if (equal_domain < 0)
6604 return -1;
6605 if (equal_domain)
6606 isl_die(ctx, isl_error_unknown,
6607 "domains unexpectedly equal", return -1);
6609 return 0;
6612 /* This is a regression test for a bug where isl_basic_map_simplify
6613 * would end up in an infinite loop. In particular, we construct
6614 * an empty basic set that is not obviously empty.
6615 * isl_basic_set_is_empty marks the basic set as empty.
6616 * After projecting out i3, the variable can be dropped completely,
6617 * but isl_basic_map_simplify refrains from doing so if the basic set
6618 * is empty and would end up in an infinite loop if it didn't test
6619 * explicitly for empty basic maps in the outer loop.
6621 static int test_simplify_1(isl_ctx *ctx)
6623 const char *str;
6624 isl_basic_set *bset;
6625 int empty;
6627 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
6628 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
6629 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
6630 "i3 >= i2 }";
6631 bset = isl_basic_set_read_from_str(ctx, str);
6632 empty = isl_basic_set_is_empty(bset);
6633 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
6634 isl_basic_set_free(bset);
6635 if (!bset)
6636 return -1;
6637 if (!empty)
6638 isl_die(ctx, isl_error_unknown,
6639 "basic set should be empty", return -1);
6641 return 0;
6644 /* Check that the equality in the set description below
6645 * is simplified away.
6647 static int test_simplify_2(isl_ctx *ctx)
6649 const char *str;
6650 isl_basic_set *bset;
6651 isl_bool universe;
6653 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
6654 bset = isl_basic_set_read_from_str(ctx, str);
6655 universe = isl_basic_set_plain_is_universe(bset);
6656 isl_basic_set_free(bset);
6658 if (universe < 0)
6659 return -1;
6660 if (!universe)
6661 isl_die(ctx, isl_error_unknown,
6662 "equality not simplified away", return -1);
6663 return 0;
6666 /* Some simplification tests.
6668 static int test_simplify(isl_ctx *ctx)
6670 if (test_simplify_1(ctx) < 0)
6671 return -1;
6672 if (test_simplify_2(ctx) < 0)
6673 return -1;
6674 return 0;
6677 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
6678 * with gbr context would fail to disable the use of the shifted tableau
6679 * when transferring equalities for the input to the context, resulting
6680 * in invalid sample values.
6682 static int test_partial_lexmin(isl_ctx *ctx)
6684 const char *str;
6685 isl_basic_set *bset;
6686 isl_basic_map *bmap;
6687 isl_map *map;
6689 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
6690 bmap = isl_basic_map_read_from_str(ctx, str);
6691 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
6692 bset = isl_basic_set_read_from_str(ctx, str);
6693 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
6694 isl_map_free(map);
6696 if (!map)
6697 return -1;
6699 return 0;
6702 /* Check that the variable compression performed on the existentially
6703 * quantified variables inside isl_basic_set_compute_divs is not confused
6704 * by the implicit equalities among the parameters.
6706 static int test_compute_divs(isl_ctx *ctx)
6708 const char *str;
6709 isl_basic_set *bset;
6710 isl_set *set;
6712 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
6713 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
6714 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
6715 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
6716 bset = isl_basic_set_read_from_str(ctx, str);
6717 set = isl_basic_set_compute_divs(bset);
6718 isl_set_free(set);
6719 if (!set)
6720 return -1;
6722 return 0;
6725 /* Check that the reaching domain elements and the prefix schedule
6726 * at a leaf node are the same before and after grouping.
6728 static int test_schedule_tree_group_1(isl_ctx *ctx)
6730 int equal;
6731 const char *str;
6732 isl_id *id;
6733 isl_union_set *uset;
6734 isl_multi_union_pw_aff *mupa;
6735 isl_union_pw_multi_aff *upma1, *upma2;
6736 isl_union_set *domain1, *domain2;
6737 isl_union_map *umap1, *umap2;
6738 isl_schedule_node *node;
6740 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
6741 uset = isl_union_set_read_from_str(ctx, str);
6742 node = isl_schedule_node_from_domain(uset);
6743 node = isl_schedule_node_child(node, 0);
6744 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
6745 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6746 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6747 node = isl_schedule_node_child(node, 0);
6748 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
6749 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6750 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6751 node = isl_schedule_node_child(node, 0);
6752 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
6753 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
6754 domain1 = isl_schedule_node_get_domain(node);
6755 id = isl_id_alloc(ctx, "group", NULL);
6756 node = isl_schedule_node_parent(node);
6757 node = isl_schedule_node_group(node, id);
6758 node = isl_schedule_node_child(node, 0);
6759 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
6760 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
6761 domain2 = isl_schedule_node_get_domain(node);
6762 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
6763 if (equal >= 0 && equal)
6764 equal = isl_union_set_is_equal(domain1, domain2);
6765 if (equal >= 0 && equal)
6766 equal = isl_union_map_is_equal(umap1, umap2);
6767 isl_union_map_free(umap1);
6768 isl_union_map_free(umap2);
6769 isl_union_set_free(domain1);
6770 isl_union_set_free(domain2);
6771 isl_union_pw_multi_aff_free(upma1);
6772 isl_union_pw_multi_aff_free(upma2);
6773 isl_schedule_node_free(node);
6775 if (equal < 0)
6776 return -1;
6777 if (!equal)
6778 isl_die(ctx, isl_error_unknown,
6779 "expressions not equal", return -1);
6781 return 0;
6784 /* Check that we can have nested groupings and that the union map
6785 * schedule representation is the same before and after the grouping.
6786 * Note that after the grouping, the union map representation contains
6787 * the domain constraints from the ranges of the expansion nodes,
6788 * while they are missing from the union map representation of
6789 * the tree without expansion nodes.
6791 * Also check that the global expansion is as expected.
6793 static int test_schedule_tree_group_2(isl_ctx *ctx)
6795 int equal, equal_expansion;
6796 const char *str;
6797 isl_id *id;
6798 isl_union_set *uset;
6799 isl_union_map *umap1, *umap2;
6800 isl_union_map *expansion1, *expansion2;
6801 isl_union_set_list *filters;
6802 isl_multi_union_pw_aff *mupa;
6803 isl_schedule *schedule;
6804 isl_schedule_node *node;
6806 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
6807 "S3[i,j] : 0 <= i,j < 10 }";
6808 uset = isl_union_set_read_from_str(ctx, str);
6809 node = isl_schedule_node_from_domain(uset);
6810 node = isl_schedule_node_child(node, 0);
6811 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
6812 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
6813 node = isl_schedule_node_insert_partial_schedule(node, mupa);
6814 node = isl_schedule_node_child(node, 0);
6815 str = "{ S1[i,j] }";
6816 uset = isl_union_set_read_from_str(ctx, str);
6817 filters = isl_union_set_list_from_union_set(uset);
6818 str = "{ S2[i,j]; S3[i,j] }";
6819 uset = isl_union_set_read_from_str(ctx, str);
6820 filters = isl_union_set_list_add(filters, uset);
6821 node = isl_schedule_node_insert_sequence(node, filters);
6822 node = isl_schedule_node_child(node, 1);
6823 node = isl_schedule_node_child(node, 0);
6824 str = "{ S2[i,j] }";
6825 uset = isl_union_set_read_from_str(ctx, str);
6826 filters = isl_union_set_list_from_union_set(uset);
6827 str = "{ S3[i,j] }";
6828 uset = isl_union_set_read_from_str(ctx, str);
6829 filters = isl_union_set_list_add(filters, uset);
6830 node = isl_schedule_node_insert_sequence(node, filters);
6832 schedule = isl_schedule_node_get_schedule(node);
6833 umap1 = isl_schedule_get_map(schedule);
6834 uset = isl_schedule_get_domain(schedule);
6835 umap1 = isl_union_map_intersect_domain(umap1, uset);
6836 isl_schedule_free(schedule);
6838 node = isl_schedule_node_parent(node);
6839 node = isl_schedule_node_parent(node);
6840 id = isl_id_alloc(ctx, "group1", NULL);
6841 node = isl_schedule_node_group(node, id);
6842 node = isl_schedule_node_child(node, 1);
6843 node = isl_schedule_node_child(node, 0);
6844 id = isl_id_alloc(ctx, "group2", NULL);
6845 node = isl_schedule_node_group(node, id);
6847 schedule = isl_schedule_node_get_schedule(node);
6848 umap2 = isl_schedule_get_map(schedule);
6849 isl_schedule_free(schedule);
6851 node = isl_schedule_node_root(node);
6852 node = isl_schedule_node_child(node, 0);
6853 expansion1 = isl_schedule_node_get_subtree_expansion(node);
6854 isl_schedule_node_free(node);
6856 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
6857 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
6858 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
6860 expansion2 = isl_union_map_read_from_str(ctx, str);
6862 equal = isl_union_map_is_equal(umap1, umap2);
6863 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
6865 isl_union_map_free(umap1);
6866 isl_union_map_free(umap2);
6867 isl_union_map_free(expansion1);
6868 isl_union_map_free(expansion2);
6870 if (equal < 0 || equal_expansion < 0)
6871 return -1;
6872 if (!equal)
6873 isl_die(ctx, isl_error_unknown,
6874 "expressions not equal", return -1);
6875 if (!equal_expansion)
6876 isl_die(ctx, isl_error_unknown,
6877 "unexpected expansion", return -1);
6879 return 0;
6882 /* Some tests for the isl_schedule_node_group function.
6884 static int test_schedule_tree_group(isl_ctx *ctx)
6886 if (test_schedule_tree_group_1(ctx) < 0)
6887 return -1;
6888 if (test_schedule_tree_group_2(ctx) < 0)
6889 return -1;
6890 return 0;
6893 struct {
6894 const char *set;
6895 const char *dual;
6896 } coef_tests[] = {
6897 { "{ rat: [i] : 0 <= i <= 10 }",
6898 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
6899 { "{ rat: [i] : FALSE }",
6900 "{ rat: coefficients[[cst] -> [a]] }" },
6901 { "{ rat: [i] : }",
6902 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
6905 struct {
6906 const char *set;
6907 const char *dual;
6908 } sol_tests[] = {
6909 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
6910 "{ rat: [i] : 0 <= i <= 10 }" },
6911 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
6912 "{ rat: [i] }" },
6913 { "{ rat: coefficients[[cst] -> [a]] }",
6914 "{ rat: [i] : FALSE }" },
6917 /* Test the basic functionality of isl_basic_set_coefficients and
6918 * isl_basic_set_solutions.
6920 static int test_dual(isl_ctx *ctx)
6922 int i;
6924 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
6925 int equal;
6926 isl_basic_set *bset1, *bset2;
6928 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
6929 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
6930 bset1 = isl_basic_set_coefficients(bset1);
6931 equal = isl_basic_set_is_equal(bset1, bset2);
6932 isl_basic_set_free(bset1);
6933 isl_basic_set_free(bset2);
6934 if (equal < 0)
6935 return -1;
6936 if (!equal)
6937 isl_die(ctx, isl_error_unknown,
6938 "incorrect dual", return -1);
6941 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
6942 int equal;
6943 isl_basic_set *bset1, *bset2;
6945 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
6946 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
6947 bset1 = isl_basic_set_solutions(bset1);
6948 equal = isl_basic_set_is_equal(bset1, bset2);
6949 isl_basic_set_free(bset1);
6950 isl_basic_set_free(bset2);
6951 if (equal < 0)
6952 return -1;
6953 if (!equal)
6954 isl_die(ctx, isl_error_unknown,
6955 "incorrect dual", return -1);
6958 return 0;
6961 struct {
6962 int scale_tile;
6963 int shift_point;
6964 const char *domain;
6965 const char *schedule;
6966 const char *sizes;
6967 const char *tile;
6968 const char *point;
6969 } tile_tests[] = {
6970 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6971 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6972 "{ [32,32] }",
6973 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6974 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6976 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
6977 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6978 "{ [32,32] }",
6979 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6980 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6982 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6983 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6984 "{ [32,32] }",
6985 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
6986 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6988 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
6989 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
6990 "{ [32,32] }",
6991 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
6992 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
6996 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
6997 * tile the band and then check if the tile and point bands have the
6998 * expected partial schedule.
7000 static int test_tile(isl_ctx *ctx)
7002 int i;
7003 int scale;
7004 int shift;
7006 scale = isl_options_get_tile_scale_tile_loops(ctx);
7007 shift = isl_options_get_tile_shift_point_loops(ctx);
7009 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
7010 int opt;
7011 int equal;
7012 const char *str;
7013 isl_union_set *domain;
7014 isl_multi_union_pw_aff *mupa, *mupa2;
7015 isl_schedule_node *node;
7016 isl_multi_val *sizes;
7018 opt = tile_tests[i].scale_tile;
7019 isl_options_set_tile_scale_tile_loops(ctx, opt);
7020 opt = tile_tests[i].shift_point;
7021 isl_options_set_tile_shift_point_loops(ctx, opt);
7023 str = tile_tests[i].domain;
7024 domain = isl_union_set_read_from_str(ctx, str);
7025 node = isl_schedule_node_from_domain(domain);
7026 node = isl_schedule_node_child(node, 0);
7027 str = tile_tests[i].schedule;
7028 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7029 node = isl_schedule_node_insert_partial_schedule(node, mupa);
7030 str = tile_tests[i].sizes;
7031 sizes = isl_multi_val_read_from_str(ctx, str);
7032 node = isl_schedule_node_band_tile(node, sizes);
7034 str = tile_tests[i].tile;
7035 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7036 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
7037 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
7038 isl_multi_union_pw_aff_free(mupa);
7039 isl_multi_union_pw_aff_free(mupa2);
7041 node = isl_schedule_node_child(node, 0);
7043 str = tile_tests[i].point;
7044 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7045 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
7046 if (equal >= 0 && equal)
7047 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
7048 mupa2);
7049 isl_multi_union_pw_aff_free(mupa);
7050 isl_multi_union_pw_aff_free(mupa2);
7052 isl_schedule_node_free(node);
7054 if (equal < 0)
7055 return -1;
7056 if (!equal)
7057 isl_die(ctx, isl_error_unknown,
7058 "unexpected result", return -1);
7061 isl_options_set_tile_scale_tile_loops(ctx, scale);
7062 isl_options_set_tile_shift_point_loops(ctx, shift);
7064 return 0;
7067 /* Check that the domain hash of a space is equal to the hash
7068 * of the domain of the space.
7070 static int test_domain_hash(isl_ctx *ctx)
7072 isl_map *map;
7073 isl_space *space;
7074 uint32_t hash1, hash2;
7076 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
7077 space = isl_map_get_space(map);
7078 isl_map_free(map);
7079 hash1 = isl_space_get_domain_hash(space);
7080 space = isl_space_domain(space);
7081 hash2 = isl_space_get_hash(space);
7082 isl_space_free(space);
7084 if (!space)
7085 return -1;
7086 if (hash1 != hash2)
7087 isl_die(ctx, isl_error_unknown,
7088 "domain hash not equal to hash of domain", return -1);
7090 return 0;
7093 /* Check that a universe basic set that is not obviously equal to the universe
7094 * is still recognized as being equal to the universe.
7096 static int test_universe(isl_ctx *ctx)
7098 const char *s;
7099 isl_basic_set *bset;
7100 isl_bool is_univ;
7102 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
7103 bset = isl_basic_set_read_from_str(ctx, s);
7104 is_univ = isl_basic_set_is_universe(bset);
7105 isl_basic_set_free(bset);
7107 if (is_univ < 0)
7108 return -1;
7109 if (!is_univ)
7110 isl_die(ctx, isl_error_unknown,
7111 "not recognized as universe set", return -1);
7113 return 0;
7116 /* Sets for which chambers are computed and checked.
7118 const char *chambers_tests[] = {
7119 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
7120 "z >= 0 and z <= C - y and z <= B - x - y }",
7123 /* Add the domain of "cell" to "cells".
7125 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
7127 isl_basic_set_list **cells = user;
7128 isl_basic_set *dom;
7130 dom = isl_cell_get_domain(cell);
7131 isl_cell_free(cell);
7132 *cells = isl_basic_set_list_add(*cells, dom);
7134 return *cells ? isl_stat_ok : isl_stat_error;
7137 /* Check that the elements of "list" are pairwise disjoint.
7139 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
7141 int i, j, n;
7143 if (!list)
7144 return isl_stat_error;
7146 n = isl_basic_set_list_n_basic_set(list);
7147 for (i = 0; i < n; ++i) {
7148 isl_basic_set *bset_i;
7150 bset_i = isl_basic_set_list_get_basic_set(list, i);
7151 for (j = i + 1; j < n; ++j) {
7152 isl_basic_set *bset_j;
7153 isl_bool disjoint;
7155 bset_j = isl_basic_set_list_get_basic_set(list, j);
7156 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
7157 isl_basic_set_free(bset_j);
7158 if (!disjoint)
7159 isl_die(isl_basic_set_list_get_ctx(list),
7160 isl_error_unknown, "not disjoint",
7161 break);
7162 if (disjoint < 0 || !disjoint)
7163 break;
7165 isl_basic_set_free(bset_i);
7166 if (j < n)
7167 return isl_stat_error;
7170 return isl_stat_ok;
7173 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
7174 * are pairwise disjoint.
7176 static int test_chambers(isl_ctx *ctx)
7178 int i;
7180 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
7181 isl_basic_set *bset;
7182 isl_vertices *vertices;
7183 isl_basic_set_list *cells;
7184 isl_stat ok;
7186 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
7187 vertices = isl_basic_set_compute_vertices(bset);
7188 cells = isl_basic_set_list_alloc(ctx, 0);
7189 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
7190 &cells) < 0)
7191 cells = isl_basic_set_list_free(cells);
7192 ok = check_pairwise_disjoint(cells);
7193 isl_basic_set_list_free(cells);
7194 isl_vertices_free(vertices);
7195 isl_basic_set_free(bset);
7197 if (ok < 0)
7198 return -1;
7201 return 0;
7204 struct {
7205 const char *name;
7206 int (*fn)(isl_ctx *ctx);
7207 } tests [] = {
7208 { "universe", &test_universe },
7209 { "domain hash", &test_domain_hash },
7210 { "dual", &test_dual },
7211 { "dependence analysis", &test_flow },
7212 { "val", &test_val },
7213 { "compute divs", &test_compute_divs },
7214 { "partial lexmin", &test_partial_lexmin },
7215 { "simplify", &test_simplify },
7216 { "curry", &test_curry },
7217 { "piecewise multi affine expressions", &test_pw_multi_aff },
7218 { "multi piecewise affine expressions", &test_multi_pw_aff },
7219 { "conversion", &test_conversion },
7220 { "list", &test_list },
7221 { "align parameters", &test_align_parameters },
7222 { "preimage", &test_preimage },
7223 { "pullback", &test_pullback },
7224 { "AST", &test_ast },
7225 { "AST build", &test_ast_build },
7226 { "AST generation", &test_ast_gen },
7227 { "eliminate", &test_eliminate },
7228 { "residue class", &test_residue_class },
7229 { "div", &test_div },
7230 { "slice", &test_slice },
7231 { "fixed power", &test_fixed_power },
7232 { "sample", &test_sample },
7233 { "output", &test_output },
7234 { "vertices", &test_vertices },
7235 { "chambers", &test_chambers },
7236 { "fixed", &test_fixed },
7237 { "equal", &test_equal },
7238 { "disjoint", &test_disjoint },
7239 { "product", &test_product },
7240 { "dim_max", &test_dim_max },
7241 { "affine", &test_aff },
7242 { "injective", &test_injective },
7243 { "schedule (whole component)", &test_schedule_whole },
7244 { "schedule (incremental)", &test_schedule_incremental },
7245 { "schedule tree grouping", &test_schedule_tree_group },
7246 { "tile", &test_tile },
7247 { "union_pw", &test_union_pw },
7248 { "eval", &test_eval },
7249 { "parse", &test_parse },
7250 { "single-valued", &test_sv },
7251 { "affine hull", &test_affine_hull },
7252 { "simple_hull", &test_simple_hull },
7253 { "coalesce", &test_coalesce },
7254 { "factorize", &test_factorize },
7255 { "subset", &test_subset },
7256 { "subtract", &test_subtract },
7257 { "intersect", &test_intersect },
7258 { "lexmin", &test_lexmin },
7259 { "min", &test_min },
7260 { "gist", &test_gist },
7261 { "piecewise quasi-polynomials", &test_pwqp },
7262 { "lift", &test_lift },
7263 { "bound", &test_bound },
7264 { "union", &test_union },
7265 { "split periods", &test_split_periods },
7266 { "lexicographic order", &test_lex },
7267 { "bijectivity", &test_bijective },
7268 { "dataflow analysis", &test_dep },
7269 { "reading", &test_read },
7270 { "bounded", &test_bounded },
7271 { "construction", &test_construction },
7272 { "dimension manipulation", &test_dim },
7273 { "map application", &test_application },
7274 { "convex hull", &test_convex_hull },
7275 { "transitive closure", &test_closure },
7278 int main(int argc, char **argv)
7280 int i;
7281 struct isl_ctx *ctx;
7282 struct isl_options *options;
7284 options = isl_options_new_with_defaults();
7285 assert(options);
7286 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
7288 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
7289 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
7290 printf("%s\n", tests[i].name);
7291 if (tests[i].fn(ctx) < 0)
7292 goto error;
7294 isl_ctx_free(ctx);
7295 return 0;
7296 error:
7297 isl_ctx_free(ctx);
7298 return -1;