isl_map_intersect: add missing isl_map_unmark_normalized
[isl.git] / isl_test.c
blobb2ea4c46f11cd76242e7c9fb2b401e213105eab0
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <assert.h>
19 #include <stdio.h>
20 #include <limits.h>
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl_space_private.h>
25 #include <isl/set.h>
26 #include <isl/flow.h>
27 #include <isl_constraint_private.h>
28 #include <isl/polynomial.h>
29 #include <isl/union_set.h>
30 #include <isl/union_map.h>
31 #include <isl_factorization.h>
32 #include <isl/schedule.h>
33 #include <isl/schedule_node.h>
34 #include <isl_options_private.h>
35 #include <isl_vertices_private.h>
36 #include <isl/ast_build.h>
37 #include <isl/val.h>
38 #include <isl/ilp.h>
39 #include <isl_ast_build_expr.h>
40 #include <isl/options.h>
42 #include "isl_srcdir.c"
44 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
46 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
47 char *filename;
48 int length;
49 char *pattern = "%s/test_inputs/%s.%s";
51 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
52 + strlen(suffix) + 1;
53 filename = isl_alloc_array(ctx, char, length);
55 if (!filename)
56 return NULL;
58 sprintf(filename, pattern, srcdir, name, suffix);
60 return filename;
63 void test_parse_map(isl_ctx *ctx, const char *str)
65 isl_map *map;
67 map = isl_map_read_from_str(ctx, str);
68 assert(map);
69 isl_map_free(map);
72 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
74 isl_map *map, *map2;
75 int equal;
77 map = isl_map_read_from_str(ctx, str);
78 map2 = isl_map_read_from_str(ctx, str2);
79 equal = isl_map_is_equal(map, map2);
80 isl_map_free(map);
81 isl_map_free(map2);
83 if (equal < 0)
84 return -1;
85 if (!equal)
86 isl_die(ctx, isl_error_unknown, "maps not equal",
87 return -1);
89 return 0;
92 void test_parse_pwqp(isl_ctx *ctx, const char *str)
94 isl_pw_qpolynomial *pwqp;
96 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
97 assert(pwqp);
98 isl_pw_qpolynomial_free(pwqp);
101 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
103 isl_pw_aff *pwaff;
105 pwaff = isl_pw_aff_read_from_str(ctx, str);
106 assert(pwaff);
107 isl_pw_aff_free(pwaff);
110 /* Check that we can read an isl_multi_val from "str" without errors.
112 static int test_parse_multi_val(isl_ctx *ctx, const char *str)
114 isl_multi_val *mv;
116 mv = isl_multi_val_read_from_str(ctx, str);
117 isl_multi_val_free(mv);
119 return mv ? 0 : -1;
122 /* Pairs of binary relation representations that should represent
123 * the same binary relations.
125 struct {
126 const char *map1;
127 const char *map2;
128 } parse_map_equal_tests[] = {
129 { "{ [x,y] : [([x/2]+y)/3] >= 1 }",
130 "{ [x, y] : 2y >= 6 - x }" },
131 { "{ [x,y] : x <= min(y, 2*y+3) }",
132 "{ [x,y] : x <= y, 2*y + 3 }" },
133 { "{ [x,y] : x >= min(y, 2*y+3) }",
134 "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }" },
135 { "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }",
136 "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }" },
137 { "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }",
138 "{ [i,j] -> [min(i,j)] }" },
139 { "{ [i,j] : i != j }",
140 "{ [i,j] : i < j or i > j }" },
141 { "{ [i,j] : (i+1)*2 >= j }",
142 "{ [i, j] : j <= 2 + 2i }" },
143 { "{ [i] -> [i > 0 ? 4 : 5] }",
144 "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }" },
145 { "[N=2,M] -> { [i=[(M+N)/4]] }",
146 "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }" },
147 { "{ [x] : x >= 0 }",
148 "{ [x] : x-0 >= 0 }" },
149 { "{ [i] : ((i > 10)) }",
150 "{ [i] : i >= 11 }" },
151 { "{ [i] -> [0] }",
152 "{ [i] -> [0 * i] }" },
153 { "{ [a] -> [b] : (not false) }",
154 "{ [a] -> [b] : true }" },
155 { "{ [i] : i/2 <= 5 }",
156 "{ [i] : i <= 10 }" },
157 { "{Sym=[n] [i] : i <= n }",
158 "[n] -> { [i] : i <= n }" },
159 { "{ [*] }",
160 "{ [a] }" },
161 { "{ [i] : 2*floor(i/2) = i }",
162 "{ [i] : exists a : i = 2 a }" },
163 { "{ [a] -> [b] : a = 5 implies b = 5 }",
164 "{ [a] -> [b] : a != 5 or b = 5 }" },
165 { "{ [a] -> [a - 1 : a > 0] }",
166 "{ [a] -> [a - 1] : a > 0 }" },
167 { "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
168 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }" },
169 { "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
170 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
171 { "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
172 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
173 { "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
174 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
175 { "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
176 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
177 { "{ [a,b] -> [i,j] : a,b << i,j }",
178 "{ [a,b] -> [i,j] : a < i or (a = i and b < j) }" },
179 { "{ [a,b] -> [i,j] : a,b <<= i,j }",
180 "{ [a,b] -> [i,j] : a < i or (a = i and b <= j) }" },
181 { "{ [a,b] -> [i,j] : a,b >> i,j }",
182 "{ [a,b] -> [i,j] : a > i or (a = i and b > j) }" },
183 { "{ [a,b] -> [i,j] : a,b >>= i,j }",
184 "{ [a,b] -> [i,j] : a > i or (a = i and b >= j) }" },
185 { "{ [n] -> [i] : exists (a, b, c: 8b <= i - 32a and "
186 "8b >= -7 + i - 32 a and b >= 0 and b <= 3 and "
187 "8c < n - 32a and i < n and c >= 0 and "
188 "c <= 3 and c >= -4a) }",
189 "{ [n] -> [i] : 0 <= i < n }" },
190 { "{ [x] -> [] : exists (a, b: 0 <= a <= 1 and 0 <= b <= 3 and "
191 "2b <= x - 8a and 2b >= -1 + x - 8a) }",
192 "{ [x] -> [] : 0 <= x <= 15 }" },
193 { "{ [x] -> [x] : }",
194 "{ [x] -> [x] }" },
197 int test_parse(struct isl_ctx *ctx)
199 int i;
200 isl_map *map, *map2;
201 const char *str, *str2;
203 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
204 return -1;
205 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
206 return -1;
207 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
208 return -1;
210 str = "{ [i] -> [-i] }";
211 map = isl_map_read_from_str(ctx, str);
212 assert(map);
213 isl_map_free(map);
215 str = "{ A[i] -> L[([i/3])] }";
216 map = isl_map_read_from_str(ctx, str);
217 assert(map);
218 isl_map_free(map);
220 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
221 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
222 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
224 for (i = 0; i < ARRAY_SIZE(parse_map_equal_tests); ++i) {
225 str = parse_map_equal_tests[i].map1;
226 str2 = parse_map_equal_tests[i].map2;
227 if (test_parse_map_equal(ctx, str, str2) < 0)
228 return -1;
231 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
232 map = isl_map_read_from_str(ctx, str);
233 str = "{ [new, old] -> [o0, o1] : "
234 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
235 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
236 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
237 map2 = isl_map_read_from_str(ctx, str);
238 assert(isl_map_is_equal(map, map2));
239 isl_map_free(map);
240 isl_map_free(map2);
242 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
243 map = isl_map_read_from_str(ctx, str);
244 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
245 map2 = isl_map_read_from_str(ctx, str);
246 assert(isl_map_is_equal(map, map2));
247 isl_map_free(map);
248 isl_map_free(map2);
250 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
251 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
252 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
253 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
254 test_parse_pwaff(ctx, "{ [] -> [(100)] }");
256 return 0;
259 static int test_read(isl_ctx *ctx)
261 char *filename;
262 FILE *input;
263 isl_basic_set *bset1, *bset2;
264 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
265 int equal;
267 filename = get_filename(ctx, "set", "omega");
268 assert(filename);
269 input = fopen(filename, "r");
270 assert(input);
272 bset1 = isl_basic_set_read_from_file(ctx, input);
273 bset2 = isl_basic_set_read_from_str(ctx, str);
275 equal = isl_basic_set_is_equal(bset1, bset2);
277 isl_basic_set_free(bset1);
278 isl_basic_set_free(bset2);
279 free(filename);
281 fclose(input);
283 if (equal < 0)
284 return -1;
285 if (!equal)
286 isl_die(ctx, isl_error_unknown,
287 "read sets not equal", return -1);
289 return 0;
292 static int test_bounded(isl_ctx *ctx)
294 isl_set *set;
295 isl_bool bounded;
297 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
298 bounded = isl_set_is_bounded(set);
299 isl_set_free(set);
301 if (bounded < 0)
302 return -1;
303 if (!bounded)
304 isl_die(ctx, isl_error_unknown,
305 "set not considered bounded", return -1);
307 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
308 bounded = isl_set_is_bounded(set);
309 assert(!bounded);
310 isl_set_free(set);
312 if (bounded < 0)
313 return -1;
314 if (bounded)
315 isl_die(ctx, isl_error_unknown,
316 "set considered bounded", return -1);
318 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
319 bounded = isl_set_is_bounded(set);
320 isl_set_free(set);
322 if (bounded < 0)
323 return -1;
324 if (bounded)
325 isl_die(ctx, isl_error_unknown,
326 "set considered bounded", return -1);
328 return 0;
331 /* Construct the basic set { [i] : 5 <= i <= N } */
332 static int test_construction_1(isl_ctx *ctx)
334 isl_space *dim;
335 isl_local_space *ls;
336 isl_basic_set *bset;
337 isl_constraint *c;
339 dim = isl_space_set_alloc(ctx, 1, 1);
340 bset = isl_basic_set_universe(isl_space_copy(dim));
341 ls = isl_local_space_from_space(dim);
343 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
344 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
345 c = isl_constraint_set_coefficient_si(c, isl_dim_param, 0, 1);
346 bset = isl_basic_set_add_constraint(bset, c);
348 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
349 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
350 c = isl_constraint_set_constant_si(c, -5);
351 bset = isl_basic_set_add_constraint(bset, c);
353 isl_local_space_free(ls);
354 isl_basic_set_free(bset);
356 return 0;
359 /* Construct the basic set { [x] : -100 <= x <= 100 }
360 * using isl_basic_set_{lower,upper}_bound_val and
361 * check that it is equal the same basic set parsed from a string.
363 static int test_construction_2(isl_ctx *ctx)
365 isl_bool equal;
366 isl_val *v;
367 isl_space *space;
368 isl_basic_set *bset1, *bset2;
370 v = isl_val_int_from_si(ctx, 100);
371 space = isl_space_set_alloc(ctx, 0, 1);
372 bset1 = isl_basic_set_universe(space);
373 bset1 = isl_basic_set_upper_bound_val(bset1, isl_dim_set, 0,
374 isl_val_copy(v));
375 bset1 = isl_basic_set_lower_bound_val(bset1, isl_dim_set, 0,
376 isl_val_neg(v));
377 bset2 = isl_basic_set_read_from_str(ctx, "{ [x] : -100 <= x <= 100 }");
378 equal = isl_basic_set_is_equal(bset1, bset2);
379 isl_basic_set_free(bset1);
380 isl_basic_set_free(bset2);
382 if (equal < 0)
383 return -1;
384 if (!equal)
385 isl_die(ctx, isl_error_unknown,
386 "failed construction", return -1);
388 return 0;
391 /* Basic tests for constructing basic sets.
393 static int test_construction(isl_ctx *ctx)
395 if (test_construction_1(ctx) < 0)
396 return -1;
397 if (test_construction_2(ctx) < 0)
398 return -1;
399 return 0;
402 static int test_dim(isl_ctx *ctx)
404 const char *str;
405 isl_map *map1, *map2;
406 int equal;
408 map1 = isl_map_read_from_str(ctx,
409 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
410 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
411 map2 = isl_map_read_from_str(ctx,
412 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
413 equal = isl_map_is_equal(map1, map2);
414 isl_map_free(map2);
416 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
417 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
418 if (equal >= 0 && equal)
419 equal = isl_map_is_equal(map1, map2);
421 isl_map_free(map1);
422 isl_map_free(map2);
424 if (equal < 0)
425 return -1;
426 if (!equal)
427 isl_die(ctx, isl_error_unknown,
428 "unexpected result", return -1);
430 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
431 map1 = isl_map_read_from_str(ctx, str);
432 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
433 map2 = isl_map_read_from_str(ctx, str);
434 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
435 equal = isl_map_is_equal(map1, map2);
436 isl_map_free(map1);
437 isl_map_free(map2);
439 if (equal < 0)
440 return -1;
441 if (!equal)
442 isl_die(ctx, isl_error_unknown,
443 "unexpected result", return -1);
445 return 0;
448 struct {
449 __isl_give isl_val *(*op)(__isl_take isl_val *v);
450 const char *arg;
451 const char *res;
452 } val_un_tests[] = {
453 { &isl_val_neg, "0", "0" },
454 { &isl_val_abs, "0", "0" },
455 { &isl_val_2exp, "0", "1" },
456 { &isl_val_floor, "0", "0" },
457 { &isl_val_ceil, "0", "0" },
458 { &isl_val_neg, "1", "-1" },
459 { &isl_val_neg, "-1", "1" },
460 { &isl_val_neg, "1/2", "-1/2" },
461 { &isl_val_neg, "-1/2", "1/2" },
462 { &isl_val_neg, "infty", "-infty" },
463 { &isl_val_neg, "-infty", "infty" },
464 { &isl_val_neg, "NaN", "NaN" },
465 { &isl_val_abs, "1", "1" },
466 { &isl_val_abs, "-1", "1" },
467 { &isl_val_abs, "1/2", "1/2" },
468 { &isl_val_abs, "-1/2", "1/2" },
469 { &isl_val_abs, "infty", "infty" },
470 { &isl_val_abs, "-infty", "infty" },
471 { &isl_val_abs, "NaN", "NaN" },
472 { &isl_val_floor, "1", "1" },
473 { &isl_val_floor, "-1", "-1" },
474 { &isl_val_floor, "1/2", "0" },
475 { &isl_val_floor, "-1/2", "-1" },
476 { &isl_val_floor, "infty", "infty" },
477 { &isl_val_floor, "-infty", "-infty" },
478 { &isl_val_floor, "NaN", "NaN" },
479 { &isl_val_ceil, "1", "1" },
480 { &isl_val_ceil, "-1", "-1" },
481 { &isl_val_ceil, "1/2", "1" },
482 { &isl_val_ceil, "-1/2", "0" },
483 { &isl_val_ceil, "infty", "infty" },
484 { &isl_val_ceil, "-infty", "-infty" },
485 { &isl_val_ceil, "NaN", "NaN" },
486 { &isl_val_2exp, "-3", "1/8" },
487 { &isl_val_2exp, "-1", "1/2" },
488 { &isl_val_2exp, "1", "2" },
489 { &isl_val_2exp, "2", "4" },
490 { &isl_val_2exp, "3", "8" },
491 { &isl_val_inv, "1", "1" },
492 { &isl_val_inv, "2", "1/2" },
493 { &isl_val_inv, "1/2", "2" },
494 { &isl_val_inv, "-2", "-1/2" },
495 { &isl_val_inv, "-1/2", "-2" },
496 { &isl_val_inv, "0", "NaN" },
497 { &isl_val_inv, "NaN", "NaN" },
498 { &isl_val_inv, "infty", "0" },
499 { &isl_val_inv, "-infty", "0" },
502 /* Perform some basic tests of unary operations on isl_val objects.
504 static int test_un_val(isl_ctx *ctx)
506 int i;
507 isl_val *v, *res;
508 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
509 isl_bool ok, is_nan;
511 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
512 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
513 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
514 fn = val_un_tests[i].op;
515 v = fn(v);
516 is_nan = isl_val_is_nan(res);
517 if (is_nan < 0)
518 ok = isl_bool_error;
519 else if (is_nan)
520 ok = isl_val_is_nan(v);
521 else
522 ok = isl_val_eq(v, res);
523 isl_val_free(v);
524 isl_val_free(res);
525 if (ok < 0)
526 return -1;
527 if (!ok)
528 isl_die(ctx, isl_error_unknown,
529 "unexpected result", return -1);
532 return 0;
535 struct {
536 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
537 __isl_take isl_val *v2);
538 } val_bin_op[] = {
539 ['+'] = { &isl_val_add },
540 ['-'] = { &isl_val_sub },
541 ['*'] = { &isl_val_mul },
542 ['/'] = { &isl_val_div },
543 ['g'] = { &isl_val_gcd },
544 ['m'] = { &isl_val_min },
545 ['M'] = { &isl_val_max },
548 struct {
549 const char *arg1;
550 unsigned char op;
551 const char *arg2;
552 const char *res;
553 } val_bin_tests[] = {
554 { "0", '+', "0", "0" },
555 { "1", '+', "0", "1" },
556 { "1", '+', "1", "2" },
557 { "1", '-', "1", "0" },
558 { "1", '*', "1", "1" },
559 { "1", '/', "1", "1" },
560 { "2", '*', "3", "6" },
561 { "2", '*', "1/2", "1" },
562 { "2", '*', "1/3", "2/3" },
563 { "2/3", '*', "3/5", "2/5" },
564 { "2/3", '*', "7/5", "14/15" },
565 { "2", '/', "1/2", "4" },
566 { "-2", '/', "-1/2", "4" },
567 { "-2", '/', "1/2", "-4" },
568 { "2", '/', "-1/2", "-4" },
569 { "2", '/', "2", "1" },
570 { "2", '/', "3", "2/3" },
571 { "2/3", '/', "5/3", "2/5" },
572 { "2/3", '/', "5/7", "14/15" },
573 { "0", '/', "0", "NaN" },
574 { "42", '/', "0", "NaN" },
575 { "-42", '/', "0", "NaN" },
576 { "infty", '/', "0", "NaN" },
577 { "-infty", '/', "0", "NaN" },
578 { "NaN", '/', "0", "NaN" },
579 { "0", '/', "NaN", "NaN" },
580 { "42", '/', "NaN", "NaN" },
581 { "-42", '/', "NaN", "NaN" },
582 { "infty", '/', "NaN", "NaN" },
583 { "-infty", '/', "NaN", "NaN" },
584 { "NaN", '/', "NaN", "NaN" },
585 { "0", '/', "infty", "0" },
586 { "42", '/', "infty", "0" },
587 { "-42", '/', "infty", "0" },
588 { "infty", '/', "infty", "NaN" },
589 { "-infty", '/', "infty", "NaN" },
590 { "NaN", '/', "infty", "NaN" },
591 { "0", '/', "-infty", "0" },
592 { "42", '/', "-infty", "0" },
593 { "-42", '/', "-infty", "0" },
594 { "infty", '/', "-infty", "NaN" },
595 { "-infty", '/', "-infty", "NaN" },
596 { "NaN", '/', "-infty", "NaN" },
597 { "1", '-', "1/3", "2/3" },
598 { "1/3", '+', "1/2", "5/6" },
599 { "1/2", '+', "1/2", "1" },
600 { "3/4", '-', "1/4", "1/2" },
601 { "1/2", '-', "1/3", "1/6" },
602 { "infty", '+', "42", "infty" },
603 { "infty", '+', "infty", "infty" },
604 { "42", '+', "infty", "infty" },
605 { "infty", '-', "infty", "NaN" },
606 { "infty", '*', "infty", "infty" },
607 { "infty", '*', "-infty", "-infty" },
608 { "-infty", '*', "infty", "-infty" },
609 { "-infty", '*', "-infty", "infty" },
610 { "0", '*', "infty", "NaN" },
611 { "1", '*', "infty", "infty" },
612 { "infty", '*', "0", "NaN" },
613 { "infty", '*', "42", "infty" },
614 { "42", '-', "infty", "-infty" },
615 { "infty", '+', "-infty", "NaN" },
616 { "4", 'g', "6", "2" },
617 { "5", 'g', "6", "1" },
618 { "42", 'm', "3", "3" },
619 { "42", 'M', "3", "42" },
620 { "3", 'm', "42", "3" },
621 { "3", 'M', "42", "42" },
622 { "42", 'm', "infty", "42" },
623 { "42", 'M', "infty", "infty" },
624 { "42", 'm', "-infty", "-infty" },
625 { "42", 'M', "-infty", "42" },
626 { "42", 'm', "NaN", "NaN" },
627 { "42", 'M', "NaN", "NaN" },
628 { "infty", 'm', "-infty", "-infty" },
629 { "infty", 'M', "-infty", "infty" },
632 /* Perform some basic tests of binary operations on isl_val objects.
634 static int test_bin_val(isl_ctx *ctx)
636 int i;
637 isl_val *v1, *v2, *res;
638 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
639 __isl_take isl_val *v2);
640 int ok;
642 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
643 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
644 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
645 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
646 fn = val_bin_op[val_bin_tests[i].op].fn;
647 v1 = fn(v1, v2);
648 if (isl_val_is_nan(res))
649 ok = isl_val_is_nan(v1);
650 else
651 ok = isl_val_eq(v1, res);
652 isl_val_free(v1);
653 isl_val_free(res);
654 if (ok < 0)
655 return -1;
656 if (!ok)
657 isl_die(ctx, isl_error_unknown,
658 "unexpected result", return -1);
661 return 0;
664 /* Perform some basic tests on isl_val objects.
666 static int test_val(isl_ctx *ctx)
668 if (test_un_val(ctx) < 0)
669 return -1;
670 if (test_bin_val(ctx) < 0)
671 return -1;
672 return 0;
675 /* Sets described using existentially quantified variables that
676 * can also be described without.
678 static const char *elimination_tests[] = {
679 "{ [i,j] : 2 * [i/2] + 3 * [j/4] <= 10 and 2 i = j }",
680 "{ [m, w] : exists a : w - 2m - 5 <= 3a <= m - 2w }",
681 "{ [m, w] : exists a : w >= 0 and a < m and -1 + w <= a <= 2m - w }",
684 /* Check that redundant existentially quantified variables are
685 * getting removed.
687 static int test_elimination(isl_ctx *ctx)
689 int i;
690 unsigned n;
691 isl_basic_set *bset;
693 for (i = 0; i < ARRAY_SIZE(elimination_tests); ++i) {
694 bset = isl_basic_set_read_from_str(ctx, elimination_tests[i]);
695 n = isl_basic_set_dim(bset, isl_dim_div);
696 isl_basic_set_free(bset);
697 if (!bset)
698 return -1;
699 if (n != 0)
700 isl_die(ctx, isl_error_unknown,
701 "expecting no existentials", return -1);
704 return 0;
707 static int test_div(isl_ctx *ctx)
709 const char *str;
710 int empty;
711 isl_space *dim;
712 isl_set *set;
713 isl_local_space *ls;
714 struct isl_basic_set *bset;
715 struct isl_constraint *c;
717 /* test 1 */
718 dim = isl_space_set_alloc(ctx, 0, 3);
719 bset = isl_basic_set_universe(isl_space_copy(dim));
720 ls = isl_local_space_from_space(dim);
722 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
723 c = isl_constraint_set_constant_si(c, -1);
724 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
725 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
726 bset = isl_basic_set_add_constraint(bset, c);
728 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
729 c = isl_constraint_set_constant_si(c, 1);
730 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
731 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
732 bset = isl_basic_set_add_constraint(bset, c);
734 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
736 assert(bset && bset->n_div == 1);
737 isl_local_space_free(ls);
738 isl_basic_set_free(bset);
740 /* test 2 */
741 dim = isl_space_set_alloc(ctx, 0, 3);
742 bset = isl_basic_set_universe(isl_space_copy(dim));
743 ls = isl_local_space_from_space(dim);
745 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
746 c = isl_constraint_set_constant_si(c, 1);
747 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
748 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
749 bset = isl_basic_set_add_constraint(bset, c);
751 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
752 c = isl_constraint_set_constant_si(c, -1);
753 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
754 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
755 bset = isl_basic_set_add_constraint(bset, c);
757 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
759 assert(bset && bset->n_div == 1);
760 isl_local_space_free(ls);
761 isl_basic_set_free(bset);
763 /* test 3 */
764 dim = isl_space_set_alloc(ctx, 0, 3);
765 bset = isl_basic_set_universe(isl_space_copy(dim));
766 ls = isl_local_space_from_space(dim);
768 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
769 c = isl_constraint_set_constant_si(c, 1);
770 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
771 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
772 bset = isl_basic_set_add_constraint(bset, c);
774 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
775 c = isl_constraint_set_constant_si(c, -3);
776 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
777 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 4);
778 bset = isl_basic_set_add_constraint(bset, c);
780 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
782 assert(bset && bset->n_div == 1);
783 isl_local_space_free(ls);
784 isl_basic_set_free(bset);
786 /* test 4 */
787 dim = isl_space_set_alloc(ctx, 0, 3);
788 bset = isl_basic_set_universe(isl_space_copy(dim));
789 ls = isl_local_space_from_space(dim);
791 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
792 c = isl_constraint_set_constant_si(c, 2);
793 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
794 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
795 bset = isl_basic_set_add_constraint(bset, c);
797 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
798 c = isl_constraint_set_constant_si(c, -1);
799 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
800 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
801 bset = isl_basic_set_add_constraint(bset, c);
803 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
805 assert(isl_basic_set_is_empty(bset));
806 isl_local_space_free(ls);
807 isl_basic_set_free(bset);
809 /* test 5 */
810 dim = isl_space_set_alloc(ctx, 0, 3);
811 bset = isl_basic_set_universe(isl_space_copy(dim));
812 ls = isl_local_space_from_space(dim);
814 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
815 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
816 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
817 bset = isl_basic_set_add_constraint(bset, c);
819 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
820 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
821 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
822 bset = isl_basic_set_add_constraint(bset, c);
824 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
826 assert(bset && bset->n_div == 0);
827 isl_basic_set_free(bset);
828 isl_local_space_free(ls);
830 /* test 6 */
831 dim = isl_space_set_alloc(ctx, 0, 3);
832 bset = isl_basic_set_universe(isl_space_copy(dim));
833 ls = isl_local_space_from_space(dim);
835 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
836 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
837 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
838 bset = isl_basic_set_add_constraint(bset, c);
840 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
841 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
842 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
843 bset = isl_basic_set_add_constraint(bset, c);
845 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
847 assert(bset && bset->n_div == 1);
848 isl_basic_set_free(bset);
849 isl_local_space_free(ls);
851 /* test 7 */
852 /* This test is a bit tricky. We set up an equality
853 * a + 3b + 3c = 6 e0
854 * Normalization of divs creates _two_ divs
855 * a = 3 e0
856 * c - b - e0 = 2 e1
857 * Afterwards e0 is removed again because it has coefficient -1
858 * and we end up with the original equality and div again.
859 * Perhaps we can avoid the introduction of this temporary div.
861 dim = isl_space_set_alloc(ctx, 0, 4);
862 bset = isl_basic_set_universe(isl_space_copy(dim));
863 ls = isl_local_space_from_space(dim);
865 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
866 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
867 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
868 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -3);
869 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 6);
870 bset = isl_basic_set_add_constraint(bset, c);
872 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
874 /* Test disabled for now */
876 assert(bset && bset->n_div == 1);
878 isl_local_space_free(ls);
879 isl_basic_set_free(bset);
881 /* test 8 */
882 dim = isl_space_set_alloc(ctx, 0, 5);
883 bset = isl_basic_set_universe(isl_space_copy(dim));
884 ls = isl_local_space_from_space(dim);
886 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
887 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
888 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
889 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, -3);
890 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 4, 6);
891 bset = isl_basic_set_add_constraint(bset, c);
893 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
894 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
895 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 1);
896 c = isl_constraint_set_constant_si(c, 1);
897 bset = isl_basic_set_add_constraint(bset, c);
899 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
901 /* Test disabled for now */
903 assert(bset && bset->n_div == 1);
905 isl_local_space_free(ls);
906 isl_basic_set_free(bset);
908 /* test 9 */
909 dim = isl_space_set_alloc(ctx, 0, 4);
910 bset = isl_basic_set_universe(isl_space_copy(dim));
911 ls = isl_local_space_from_space(dim);
913 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
914 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
915 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -1);
916 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
917 bset = isl_basic_set_add_constraint(bset, c);
919 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
920 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
921 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 3);
922 c = isl_constraint_set_constant_si(c, 2);
923 bset = isl_basic_set_add_constraint(bset, c);
925 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
927 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
929 assert(!isl_basic_set_is_empty(bset));
931 isl_local_space_free(ls);
932 isl_basic_set_free(bset);
934 /* test 10 */
935 dim = isl_space_set_alloc(ctx, 0, 3);
936 bset = isl_basic_set_universe(isl_space_copy(dim));
937 ls = isl_local_space_from_space(dim);
939 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
940 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
941 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
942 bset = isl_basic_set_add_constraint(bset, c);
944 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
946 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
948 isl_local_space_free(ls);
949 isl_basic_set_free(bset);
951 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
952 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
953 set = isl_set_read_from_str(ctx, str);
954 set = isl_set_compute_divs(set);
955 isl_set_free(set);
956 if (!set)
957 return -1;
959 if (test_elimination(ctx) < 0)
960 return -1;
962 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
963 set = isl_set_read_from_str(ctx, str);
964 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
965 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
966 empty = isl_set_is_empty(set);
967 isl_set_free(set);
968 if (empty < 0)
969 return -1;
970 if (!empty)
971 isl_die(ctx, isl_error_unknown,
972 "result not as accurate as expected", return -1);
974 return 0;
977 void test_application_case(struct isl_ctx *ctx, const char *name)
979 char *filename;
980 FILE *input;
981 struct isl_basic_set *bset1, *bset2;
982 struct isl_basic_map *bmap;
984 filename = get_filename(ctx, name, "omega");
985 assert(filename);
986 input = fopen(filename, "r");
987 assert(input);
989 bset1 = isl_basic_set_read_from_file(ctx, input);
990 bmap = isl_basic_map_read_from_file(ctx, input);
992 bset1 = isl_basic_set_apply(bset1, bmap);
994 bset2 = isl_basic_set_read_from_file(ctx, input);
996 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
998 isl_basic_set_free(bset1);
999 isl_basic_set_free(bset2);
1000 free(filename);
1002 fclose(input);
1005 static int test_application(isl_ctx *ctx)
1007 test_application_case(ctx, "application");
1008 test_application_case(ctx, "application2");
1010 return 0;
1013 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1015 char *filename;
1016 FILE *input;
1017 struct isl_basic_set *bset1, *bset2;
1019 filename = get_filename(ctx, name, "polylib");
1020 assert(filename);
1021 input = fopen(filename, "r");
1022 assert(input);
1024 bset1 = isl_basic_set_read_from_file(ctx, input);
1025 bset2 = isl_basic_set_read_from_file(ctx, input);
1027 bset1 = isl_basic_set_affine_hull(bset1);
1029 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1031 isl_basic_set_free(bset1);
1032 isl_basic_set_free(bset2);
1033 free(filename);
1035 fclose(input);
1038 int test_affine_hull(struct isl_ctx *ctx)
1040 const char *str;
1041 isl_set *set;
1042 isl_basic_set *bset, *bset2;
1043 int n;
1044 int subset;
1046 test_affine_hull_case(ctx, "affine2");
1047 test_affine_hull_case(ctx, "affine");
1048 test_affine_hull_case(ctx, "affine3");
1050 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1051 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1052 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1053 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1054 set = isl_set_read_from_str(ctx, str);
1055 bset = isl_set_affine_hull(set);
1056 n = isl_basic_set_dim(bset, isl_dim_div);
1057 isl_basic_set_free(bset);
1058 if (n != 0)
1059 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1060 return -1);
1062 /* Check that isl_map_affine_hull is not confused by
1063 * the reordering of divs in isl_map_align_divs.
1065 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1066 "32e0 = b and 32e1 = c); "
1067 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1068 set = isl_set_read_from_str(ctx, str);
1069 bset = isl_set_affine_hull(set);
1070 isl_basic_set_free(bset);
1071 if (!bset)
1072 return -1;
1074 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1075 "32e2 = 31 + 31e0 }";
1076 set = isl_set_read_from_str(ctx, str);
1077 bset = isl_set_affine_hull(set);
1078 str = "{ [a] : exists e : a = 32 e }";
1079 bset2 = isl_basic_set_read_from_str(ctx, str);
1080 subset = isl_basic_set_is_subset(bset, bset2);
1081 isl_basic_set_free(bset);
1082 isl_basic_set_free(bset2);
1083 if (subset < 0)
1084 return -1;
1085 if (!subset)
1086 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1087 return -1);
1089 return 0;
1092 /* Pairs of maps and the corresponding expected results of
1093 * isl_map_plain_unshifted_simple_hull.
1095 struct {
1096 const char *map;
1097 const char *hull;
1098 } plain_unshifted_simple_hull_tests[] = {
1099 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1100 "{ [i] -> [j] : i >= 1 }" },
1101 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1102 "(j mod 4 = 2 and k mod 6 = n) }",
1103 "{ [n] -> [i,j,k] : j mod 4 = 2 }" },
1106 /* Basic tests for isl_map_plain_unshifted_simple_hull.
1108 static int test_plain_unshifted_simple_hull(isl_ctx *ctx)
1110 int i;
1111 isl_map *map;
1112 isl_basic_map *hull, *expected;
1113 isl_bool equal;
1115 for (i = 0; i < ARRAY_SIZE(plain_unshifted_simple_hull_tests); ++i) {
1116 const char *str;
1117 str = plain_unshifted_simple_hull_tests[i].map;
1118 map = isl_map_read_from_str(ctx, str);
1119 str = plain_unshifted_simple_hull_tests[i].hull;
1120 expected = isl_basic_map_read_from_str(ctx, str);
1121 hull = isl_map_plain_unshifted_simple_hull(map);
1122 equal = isl_basic_map_is_equal(hull, expected);
1123 isl_basic_map_free(hull);
1124 isl_basic_map_free(expected);
1125 if (equal < 0)
1126 return -1;
1127 if (!equal)
1128 isl_die(ctx, isl_error_unknown, "unexpected hull",
1129 return -1);
1132 return 0;
1135 /* Pairs of sets and the corresponding expected results of
1136 * isl_set_unshifted_simple_hull.
1138 struct {
1139 const char *set;
1140 const char *hull;
1141 } unshifted_simple_hull_tests[] = {
1142 { "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1143 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1146 /* Basic tests for isl_set_unshifted_simple_hull.
1148 static int test_unshifted_simple_hull(isl_ctx *ctx)
1150 int i;
1151 isl_set *set;
1152 isl_basic_set *hull, *expected;
1153 isl_bool equal;
1155 for (i = 0; i < ARRAY_SIZE(unshifted_simple_hull_tests); ++i) {
1156 const char *str;
1157 str = unshifted_simple_hull_tests[i].set;
1158 set = isl_set_read_from_str(ctx, str);
1159 str = unshifted_simple_hull_tests[i].hull;
1160 expected = isl_basic_set_read_from_str(ctx, str);
1161 hull = isl_set_unshifted_simple_hull(set);
1162 equal = isl_basic_set_is_equal(hull, expected);
1163 isl_basic_set_free(hull);
1164 isl_basic_set_free(expected);
1165 if (equal < 0)
1166 return -1;
1167 if (!equal)
1168 isl_die(ctx, isl_error_unknown, "unexpected hull",
1169 return -1);
1172 return 0;
1175 static int test_simple_hull(struct isl_ctx *ctx)
1177 const char *str;
1178 isl_set *set;
1179 isl_basic_set *bset;
1180 isl_bool is_empty;
1182 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1183 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1184 set = isl_set_read_from_str(ctx, str);
1185 bset = isl_set_simple_hull(set);
1186 is_empty = isl_basic_set_is_empty(bset);
1187 isl_basic_set_free(bset);
1189 if (is_empty == isl_bool_error)
1190 return -1;
1192 if (is_empty == isl_bool_false)
1193 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1194 return -1);
1196 if (test_plain_unshifted_simple_hull(ctx) < 0)
1197 return -1;
1198 if (test_unshifted_simple_hull(ctx) < 0)
1199 return -1;
1201 return 0;
1204 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1206 char *filename;
1207 FILE *input;
1208 struct isl_basic_set *bset1, *bset2;
1209 struct isl_set *set;
1211 filename = get_filename(ctx, name, "polylib");
1212 assert(filename);
1213 input = fopen(filename, "r");
1214 assert(input);
1216 bset1 = isl_basic_set_read_from_file(ctx, input);
1217 bset2 = isl_basic_set_read_from_file(ctx, input);
1219 set = isl_basic_set_union(bset1, bset2);
1220 bset1 = isl_set_convex_hull(set);
1222 bset2 = isl_basic_set_read_from_file(ctx, input);
1224 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1226 isl_basic_set_free(bset1);
1227 isl_basic_set_free(bset2);
1228 free(filename);
1230 fclose(input);
1233 struct {
1234 const char *set;
1235 const char *hull;
1236 } convex_hull_tests[] = {
1237 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1238 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1239 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1240 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1241 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1242 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1243 "i2 <= 5 and i2 >= 4; "
1244 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1245 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1246 "i2 <= 5 + i0 and i2 >= i0 }" },
1247 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1248 "{ [x, y] : 1 = 0 }" },
1249 { "{ [x, y, z] : 0 <= x, y, z <= 10; [x, y, 0] : x >= 0 and y > 0; "
1250 "[x, y, 0] : x >= 0 and y < 0 }",
1251 "{ [x, y, z] : x >= 0 and 0 <= z <= 10 }" },
1254 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1256 int i;
1257 int orig_convex = ctx->opt->convex;
1258 ctx->opt->convex = convex;
1260 test_convex_hull_case(ctx, "convex0");
1261 test_convex_hull_case(ctx, "convex1");
1262 test_convex_hull_case(ctx, "convex2");
1263 test_convex_hull_case(ctx, "convex3");
1264 test_convex_hull_case(ctx, "convex4");
1265 test_convex_hull_case(ctx, "convex5");
1266 test_convex_hull_case(ctx, "convex6");
1267 test_convex_hull_case(ctx, "convex7");
1268 test_convex_hull_case(ctx, "convex8");
1269 test_convex_hull_case(ctx, "convex9");
1270 test_convex_hull_case(ctx, "convex10");
1271 test_convex_hull_case(ctx, "convex11");
1272 test_convex_hull_case(ctx, "convex12");
1273 test_convex_hull_case(ctx, "convex13");
1274 test_convex_hull_case(ctx, "convex14");
1275 test_convex_hull_case(ctx, "convex15");
1277 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1278 isl_set *set1, *set2;
1279 int equal;
1281 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1282 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1283 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1284 equal = isl_set_is_equal(set1, set2);
1285 isl_set_free(set1);
1286 isl_set_free(set2);
1288 if (equal < 0)
1289 return -1;
1290 if (!equal)
1291 isl_die(ctx, isl_error_unknown,
1292 "unexpected convex hull", return -1);
1295 ctx->opt->convex = orig_convex;
1297 return 0;
1300 static int test_convex_hull(isl_ctx *ctx)
1302 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1303 return -1;
1304 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1305 return -1;
1306 return 0;
1309 void test_gist_case(struct isl_ctx *ctx, const char *name)
1311 char *filename;
1312 FILE *input;
1313 struct isl_basic_set *bset1, *bset2;
1315 filename = get_filename(ctx, name, "polylib");
1316 assert(filename);
1317 input = fopen(filename, "r");
1318 assert(input);
1320 bset1 = isl_basic_set_read_from_file(ctx, input);
1321 bset2 = isl_basic_set_read_from_file(ctx, input);
1323 bset1 = isl_basic_set_gist(bset1, bset2);
1325 bset2 = isl_basic_set_read_from_file(ctx, input);
1327 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1329 isl_basic_set_free(bset1);
1330 isl_basic_set_free(bset2);
1331 free(filename);
1333 fclose(input);
1336 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1338 struct {
1339 const char *map;
1340 const char *context;
1341 const char *gist;
1342 } plain_gist_tests[] = {
1343 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1344 "{ [i] -> [j] : i >= 1 }",
1345 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1346 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1347 "(j mod 4 = 2 and k mod 6 = n) }",
1348 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1349 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1350 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1351 "{ [i] -> [j] : i > j }",
1352 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1355 /* Basic tests for isl_map_plain_gist_basic_map.
1357 static int test_plain_gist(isl_ctx *ctx)
1359 int i;
1361 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1362 const char *str;
1363 int equal;
1364 isl_map *map, *gist;
1365 isl_basic_map *context;
1367 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1368 str = plain_gist_tests[i].context;
1369 context = isl_basic_map_read_from_str(ctx, str);
1370 map = isl_map_plain_gist_basic_map(map, context);
1371 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1372 equal = isl_map_is_equal(map, gist);
1373 isl_map_free(map);
1374 isl_map_free(gist);
1375 if (equal < 0)
1376 return -1;
1377 if (!equal)
1378 isl_die(ctx, isl_error_unknown,
1379 "incorrect gist result", return -1);
1382 return 0;
1385 struct {
1386 const char *set;
1387 const char *context;
1388 const char *gist;
1389 } gist_tests[] = {
1390 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1391 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1392 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1393 "{ [a, b, c] : a <= 15 }" },
1394 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1395 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1396 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1397 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1398 { "{ [m, n, a, b] : a <= 2147 + n }",
1399 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1400 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1401 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1402 "b >= 0) }",
1403 "{ [m, n, ku, kl] }" },
1404 { "{ [a, a, b] : a >= 10 }",
1405 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1406 "{ [a, a, b] : a >= 10 }" },
1407 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1408 "{ [0, j] : j >= 0 }" },
1409 /* Check that no constraints on i6 are introduced in the gist */
1410 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1411 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1412 "5e0 <= 381 - t1 and i4 <= 1) }",
1413 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1414 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1415 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1416 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1417 "20e0 >= 1511 - 4t1 - 5i4) }" },
1418 /* Check that no constraints on i6 are introduced in the gist */
1419 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1420 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1421 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1422 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1423 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1424 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1425 "20e1 <= 1530 - 4t1 - 5i4 and "
1426 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1427 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1428 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1429 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1430 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1431 "10e0 <= -91 + 5i4 + 4i6 and "
1432 "10e0 >= -105 + 5i4 + 4i6) }",
1433 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1434 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1435 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1436 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1437 "{ [a, b, q, p] : b >= 1 + a }",
1438 "{ [a, b, q, p] : false }" },
1439 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1440 "[n] -> { [x] : x mod 32 = 0 }",
1441 "[n] -> { [x = n] }" },
1442 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1443 "{ [x] : x mod 2 = 0 }" },
1444 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1445 "{ [x] : x mod 128 = 0 }" },
1446 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1447 "{ [x] : x mod 3200 = 0 }" },
1448 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1449 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1450 "{ [a, b, c = a] }" },
1451 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1452 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1453 "{ [a, b, c = a] : a mod 3 = 0 }" },
1454 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1455 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1456 "{ [x] }" },
1457 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1458 "{ [x,y] : 1 <= y <= 3 }",
1459 "{ [x,y] }" },
1462 /* Check that isl_set_gist behaves as expected.
1464 * For the test cases in gist_tests, besides checking that the result
1465 * is as expected, also check that applying the gist operation does
1466 * not modify the input set (an earlier version of isl would do that) and
1467 * that the test case is consistent, i.e., that the gist has the same
1468 * intersection with the context as the input set.
1470 static int test_gist(struct isl_ctx *ctx)
1472 int i;
1473 const char *str;
1474 isl_basic_set *bset1, *bset2;
1475 isl_map *map1, *map2;
1476 int equal;
1478 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1479 int equal_input, equal_intersection;
1480 isl_set *set1, *set2, *copy, *context;
1482 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1483 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1484 copy = isl_set_copy(set1);
1485 set1 = isl_set_gist(set1, isl_set_copy(context));
1486 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1487 equal = isl_set_is_equal(set1, set2);
1488 isl_set_free(set1);
1489 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1490 equal_input = isl_set_is_equal(set1, copy);
1491 isl_set_free(copy);
1492 set1 = isl_set_intersect(set1, isl_set_copy(context));
1493 set2 = isl_set_intersect(set2, context);
1494 equal_intersection = isl_set_is_equal(set1, set2);
1495 isl_set_free(set2);
1496 isl_set_free(set1);
1497 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1498 return -1;
1499 if (!equal)
1500 isl_die(ctx, isl_error_unknown,
1501 "incorrect gist result", return -1);
1502 if (!equal_input)
1503 isl_die(ctx, isl_error_unknown,
1504 "gist modified input", return -1);
1505 if (!equal_input)
1506 isl_die(ctx, isl_error_unknown,
1507 "inconsistent gist test case", return -1);
1510 test_gist_case(ctx, "gist1");
1512 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1513 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1514 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1515 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1516 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1517 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1518 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1519 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1520 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1521 bset1 = isl_basic_set_read_from_str(ctx, str);
1522 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1523 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1524 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1525 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1526 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1527 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1528 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1529 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1530 bset2 = isl_basic_set_read_from_str(ctx, str);
1531 bset1 = isl_basic_set_gist(bset1, bset2);
1532 assert(bset1 && bset1->n_div == 0);
1533 isl_basic_set_free(bset1);
1535 /* Check that the integer divisions of the second disjunct
1536 * do not spread to the first disjunct.
1538 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1539 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1540 "(exists (e0 = [(-1 + t1)/16], "
1541 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1542 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1543 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1544 "o0 <= 4294967295 and t1 <= -1)) }";
1545 map1 = isl_map_read_from_str(ctx, str);
1546 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1547 map2 = isl_map_read_from_str(ctx, str);
1548 map1 = isl_map_gist(map1, map2);
1549 if (!map1)
1550 return -1;
1551 if (map1->n != 1)
1552 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1553 isl_map_free(map1); return -1);
1554 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1555 isl_die(ctx, isl_error_unknown, "expecting single div",
1556 isl_map_free(map1); return -1);
1557 isl_map_free(map1);
1559 if (test_plain_gist(ctx) < 0)
1560 return -1;
1562 return 0;
1565 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1567 isl_set *set, *set2;
1568 int equal;
1569 int one;
1571 set = isl_set_read_from_str(ctx, str);
1572 set = isl_set_coalesce(set);
1573 set2 = isl_set_read_from_str(ctx, str);
1574 equal = isl_set_is_equal(set, set2);
1575 one = set && set->n == 1;
1576 isl_set_free(set);
1577 isl_set_free(set2);
1579 if (equal < 0)
1580 return -1;
1581 if (!equal)
1582 isl_die(ctx, isl_error_unknown,
1583 "coalesced set not equal to input", return -1);
1584 if (check_one && !one)
1585 isl_die(ctx, isl_error_unknown,
1586 "coalesced set should not be a union", return -1);
1588 return 0;
1591 /* Inputs for coalescing tests with unbounded wrapping.
1592 * "str" is a string representation of the input set.
1593 * "single_disjunct" is set if we expect the result to consist of
1594 * a single disjunct.
1596 struct {
1597 int single_disjunct;
1598 const char *str;
1599 } coalesce_unbounded_tests[] = {
1600 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1601 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1602 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1603 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1604 "-10 <= y <= 0}" },
1605 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1606 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1607 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1608 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1611 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1612 * option turned off.
1614 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1616 int i;
1617 int r = 0;
1618 int bounded;
1620 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1621 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1623 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1624 const char *str = coalesce_unbounded_tests[i].str;
1625 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1626 if (test_coalesce_set(ctx, str, check_one) >= 0)
1627 continue;
1628 r = -1;
1629 break;
1632 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1634 return r;
1637 /* Inputs for coalescing tests.
1638 * "str" is a string representation of the input set.
1639 * "single_disjunct" is set if we expect the result to consist of
1640 * a single disjunct.
1642 struct {
1643 int single_disjunct;
1644 const char *str;
1645 } coalesce_tests[] = {
1646 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1647 "y >= x & x >= 2 & 5 >= y }" },
1648 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1649 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1650 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1651 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1652 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1653 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1654 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1655 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1656 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1657 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1658 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1659 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1660 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1661 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1662 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1663 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1664 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1665 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1666 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1667 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1668 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1669 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1670 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1671 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1672 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1673 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1674 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1675 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1676 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1677 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1678 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1679 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1680 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1681 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1682 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1683 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1684 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1685 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1686 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1687 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1688 "[o0, o1, o2, o3, o4, o5, o6]] : "
1689 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1690 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1691 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1692 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1693 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1694 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1695 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1696 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1697 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1698 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1699 "o6 >= i3 + i6 - o3 and M >= 0 and "
1700 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1701 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1702 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1703 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1704 "(o0 = 0 and M >= 2 and N >= 3) or "
1705 "(M = 0 and o0 = 0 and N >= 3) }" },
1706 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1707 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1708 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1709 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1710 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1711 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1712 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1713 "(y = 3 and x = 1) }" },
1714 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1715 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1716 "i1 <= M and i3 <= M and i4 <= M) or "
1717 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1718 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1719 "i4 <= -1 + M) }" },
1720 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1721 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1722 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1723 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1724 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1725 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1726 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1727 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1728 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1729 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1730 { 0, "{ [a, b] : exists e : 2e = a and "
1731 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1732 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1733 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1734 "j >= 1 and j' <= i + j - i' and i >= 1; "
1735 "[1, 1, 1, 1] }" },
1736 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1737 "[i,j] : exists a : j = 3a }" },
1738 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1739 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1740 "a >= 3) or "
1741 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1742 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1743 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1744 "c <= 6 + 8a and a >= 3; "
1745 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1746 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1747 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1748 "[x,0] : 3 <= x <= 4 }" },
1749 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1750 "[x,0] : 4 <= x <= 5 }" },
1751 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1752 "[x,0] : 3 <= x <= 5 }" },
1753 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1754 "[x,0] : 3 <= x <= 4 }" },
1755 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1756 "i1 <= 0; "
1757 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1758 { 1, "{ [0,0]; [1,1] }" },
1759 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1760 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1761 "ii <= k;"
1762 "[k, 0, k] : k <= 6 and k >= 1 }" },
1763 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1764 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1765 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1766 { 1, "[n] -> { [1] : n >= 0;"
1767 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1768 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1769 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1770 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1771 "2e0 <= x and 2e0 <= n);"
1772 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1773 "n >= 0) }" },
1774 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1775 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1776 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1777 "t1 = 1 }" },
1778 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1779 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1780 "[0, 0] }" },
1781 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1782 "t1 >= 13 and t1 <= 16);"
1783 "[t1] : t1 <= 15 and t1 >= 12 }" },
1784 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
1785 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
1786 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
1787 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
1788 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
1789 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
1790 "i <= 4j + 2 }" },
1791 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
1792 "(exists (e0 : 3e0 = -2 + c0)) }" },
1793 { 0, "[n, b0, t0] -> "
1794 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1795 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1796 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1797 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1798 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
1799 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
1800 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
1801 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
1802 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1803 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1804 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1805 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
1806 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
1807 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
1808 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
1809 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
1810 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
1811 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
1812 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
1813 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
1814 { 0, "{ [i0, i1, i2] : "
1815 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
1816 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
1817 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
1818 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
1819 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
1820 "32e0 <= 31 + i0)) or "
1821 "i0 >= 0 }" },
1822 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
1823 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
1824 "2*floor((c)/2) = c and 0 <= a <= 192;"
1825 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
1827 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
1828 "(0 <= a <= b <= n) }" },
1829 { 1, "{ [a, b] : 0 <= a <= 2 and b >= 0 and "
1830 "((0 < b <= 13) or (2*floor((a + b)/2) >= -5 + a + 2b)) }" },
1831 { 1, "{ [a] : (2 <= a <= 5) or (a mod 2 = 1 and 1 <= a <= 5) }" },
1832 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
1833 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1834 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
1835 "b = 3 and 9e0 <= -19 + 2c)) }" },
1836 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
1837 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1838 "(a = 4 and b = 3 and "
1839 "9*floor((-16 + 2c)/9) <= -19 + 2c) }" },
1840 { 0, "{ [a, b, c] : (b <= 2 and b <= -2 + a) or "
1841 "(b = -1 + a and 0 < a <= 3 and "
1842 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
1843 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
1844 "b = 3 and 9e0 <= -19 + 2c)) }" },
1845 { 1, "{ [y, x] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
1846 "[1, 0] }" },
1847 { 1, "{ [x, y] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
1848 "[0, 1] }" },
1849 { 1, "{ [1, y] : -1 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
1850 { 1, "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
1851 { 1, "{ [x, y] : 0 <= x <= 10 and x - 4*floor(x/4) <= 1 and y <= 0; "
1852 "[x, y] : 0 <= x <= 10 and x - 4*floor(x/4) > 1 and y <= 0; "
1853 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) <= 1 and 0 < y; "
1854 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) > 1 and 0 < y }" },
1855 { 1, "{ [x, 0] : 0 <= x <= 10 and x mod 2 = 0; "
1856 "[x, 0] : 0 <= x <= 10 and x mod 2 = 1; "
1857 "[x, y] : 0 <= x <= 10 and 1 <= y <= 10 }" },
1858 { 1, "{ [a] : a <= 8 and "
1859 "(a mod 10 = 7 or a mod 10 = 8 or a mod 10 = 9) }" },
1860 { 1, "{ [x, y] : 2y = -x and x <= 0 or "
1861 "x <= -1 and 2y <= -x - 1 and 2y >= x - 1 }" },
1862 { 0, "{ [x, y] : 2y = -x and x <= 0 or "
1863 "x <= -2 and 2y <= -x - 1 and 2y >= x - 1 }" },
1864 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
1865 "(a < 0 and 3*floor((a)/3) < a) }" },
1866 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
1867 "(a < -1 and 3*floor((a)/3) < a) }" },
1868 { 1, "{ [a, b] : a <= 1024 and b >= 0 and "
1869 "((-31 - a + b <= 32*floor((-1 - a)/32) <= -33 + b and "
1870 "32*floor((-1 - a)/32) <= -16 + b + 16*floor((-1 - a)/16))"
1871 "or (2 <= a <= 15 and b < a)) }" },
1872 { 1, "{ [a] : a > 0 and ((16*floor((a)/16) < a and "
1873 "32*floor((a)/32) < a) or a <= 15) }" },
1874 { 1, "{ [a, b, c, d] : (-a + d) mod 64 = 0 and a <= 8 and b <= 1 and "
1875 "10 - a <= c <= 3 and d >= 5 and 9 - 64b <= d <= 70;"
1876 "[a, b = 1, c, d] : (-a + d) mod 64 = 0 and a <= 8 and c >= 4 and "
1877 "10 - a <= c <= 5 and 5 <= d <= 73 - c }" },
1878 { 1, "[n, m] -> { S_0[i] : (-n + i) mod 3 = 0 and m >= 3 + n and "
1879 "i >= n and 3*floor((2 + n + 2m)/3) <= n + 3m - i; "
1880 "S_0[n] : n <= m <= 2 + n }" },
1881 { 1, "{ [a, b] : exists (e0: 0 <= a <= 1 and b >= 0 and "
1882 "2e0 >= -5 + a + 2b and 2e0 >= -1 + a + b and "
1883 "2e0 <= a + b); "
1884 "[a, b] : exists (e0: 0 <= a <= 1 and 2e0 >= -5 + a + 2b and "
1885 "2e0 >= -1 - a + b and 2e0 <= -a + b and "
1886 "2e0 < -a + 2b) }" },
1887 { 1, "{ [i, j, i - 8j] : 8 <= i <= 63 and -7 + i <= 8j <= i; "
1888 "[i, 0, i] : 0 <= i <= 7 }" },
1889 { 1, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [1, 1] }" },
1890 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [0, 2] }" },
1891 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [-1, 3] }" },
1892 { 1, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [1, 1] }" },
1893 { 0, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [2, 1] }" },
1894 { 0, "{ [a, c] : (2 + a) mod 4 = 0 or "
1895 "(c = 4 + a and 4 * floor((a)/4) = a and a >= 0 and a <= 4) or "
1896 "(c = 3 + a and 4 * floor((-1 + a)/4) = -1 + a and "
1897 "a > 0 and a <= 5) }" },
1900 /* A specialized coalescing test case that would result
1901 * in a segmentation fault or a failed assertion in earlier versions of isl.
1903 static int test_coalesce_special(struct isl_ctx *ctx)
1905 const char *str;
1906 isl_map *map1, *map2;
1908 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1909 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
1910 "(y = 201 and o1 <= 239 and o1 >= 212) or "
1911 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
1912 "o1 <= 239 and o1 >= 212)) or "
1913 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
1914 "o1 <= 241 and o1 >= 240));"
1915 "[S_L220_OUT[] -> T7[]] -> "
1916 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
1917 "(y = 2 and o1 <= 241 and o1 >= 212) or "
1918 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
1919 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
1920 map1 = isl_map_read_from_str(ctx, str);
1921 map1 = isl_map_align_divs_internal(map1);
1922 map1 = isl_map_coalesce(map1);
1923 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1924 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
1925 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
1926 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
1927 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
1928 map2 = isl_map_read_from_str(ctx, str);
1929 map2 = isl_map_union(map2, map1);
1930 map2 = isl_map_align_divs_internal(map2);
1931 map2 = isl_map_coalesce(map2);
1932 isl_map_free(map2);
1933 if (!map2)
1934 return -1;
1936 return 0;
1939 /* A specialized coalescing test case that would result in an assertion
1940 * in an earlier version of isl.
1941 * The explicit call to isl_basic_set_union prevents the implicit
1942 * equality constraints in the first basic map from being detected prior
1943 * to the call to isl_set_coalesce, at least at the point
1944 * where this test case was introduced.
1946 static int test_coalesce_special2(struct isl_ctx *ctx)
1948 const char *str;
1949 isl_basic_set *bset1, *bset2;
1950 isl_set *set;
1952 str = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
1953 bset1 = isl_basic_set_read_from_str(ctx, str);
1954 str = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }" ;
1955 bset2 = isl_basic_set_read_from_str(ctx, str);
1956 set = isl_basic_set_union(bset1, bset2);
1957 set = isl_set_coalesce(set);
1958 isl_set_free(set);
1960 if (!set)
1961 return -1;
1962 return 0;
1965 /* Check that calling isl_set_coalesce does not leave other sets
1966 * that may share some information with the input to isl_set_coalesce
1967 * in an inconsistent state.
1968 * In particular, older versions of isl would modify all copies
1969 * of the basic sets in the isl_set_coalesce input in a way
1970 * that could leave them in an inconsistent state.
1971 * The result of printing any other set containing one of these
1972 * basic sets would then result in an invalid set description.
1974 static int test_coalesce_special3(isl_ctx *ctx)
1976 const char *str;
1977 char *s;
1978 isl_set *set1, *set2;
1979 isl_printer *p;
1981 set1 = isl_set_read_from_str(ctx, "{ [0, 0, 0] }");
1982 str = "{ [a, b, a + b] : a >= 0 and b >= 0 and 0 < a + b }";
1983 set2 = isl_set_read_from_str(ctx, str);
1984 set1 = isl_set_union(set1, isl_set_copy(set2));
1985 set1 = isl_set_coalesce(set1);
1986 isl_set_free(set1);
1988 p = isl_printer_to_str(ctx);
1989 p = isl_printer_print_set(p, set2);
1990 isl_set_free(set2);
1991 s = isl_printer_get_str(p);
1992 isl_printer_free(p);
1993 set1 = isl_set_read_from_str(ctx, s);
1994 free(s);
1995 isl_set_free(set1);
1997 if (!set1)
1998 return -1;
2000 return 0;
2003 /* Test the functionality of isl_set_coalesce.
2004 * That is, check that the output is always equal to the input
2005 * and in some cases that the result consists of a single disjunct.
2007 static int test_coalesce(struct isl_ctx *ctx)
2009 int i;
2011 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
2012 const char *str = coalesce_tests[i].str;
2013 int check_one = coalesce_tests[i].single_disjunct;
2014 if (test_coalesce_set(ctx, str, check_one) < 0)
2015 return -1;
2018 if (test_coalesce_unbounded_wrapping(ctx) < 0)
2019 return -1;
2020 if (test_coalesce_special(ctx) < 0)
2021 return -1;
2022 if (test_coalesce_special2(ctx) < 0)
2023 return -1;
2024 if (test_coalesce_special3(ctx) < 0)
2025 return -1;
2027 return 0;
2030 /* Construct a representation of the graph on the right of Figure 1
2031 * in "Computing the Transitive Closure of a Union of
2032 * Affine Integer Tuple Relations".
2034 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2036 isl_set *dom;
2037 isl_map *up, *right;
2039 dom = isl_set_read_from_str(ctx,
2040 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2041 "2 x - 3 y + 3 >= 0 }");
2042 right = isl_map_read_from_str(ctx,
2043 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2044 up = isl_map_read_from_str(ctx,
2045 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2046 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2047 right = isl_map_intersect_range(right, isl_set_copy(dom));
2048 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2049 up = isl_map_intersect_range(up, dom);
2050 return isl_map_union(up, right);
2053 /* Construct a representation of the power of the graph
2054 * on the right of Figure 1 in "Computing the Transitive Closure of
2055 * a Union of Affine Integer Tuple Relations".
2057 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2059 return isl_map_read_from_str(ctx,
2060 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2061 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2062 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2065 /* Construct a representation of the transitive closure of the graph
2066 * on the right of Figure 1 in "Computing the Transitive Closure of
2067 * a Union of Affine Integer Tuple Relations".
2069 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2071 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2074 static int test_closure(isl_ctx *ctx)
2076 const char *str;
2077 isl_map *map, *map2;
2078 int exact, equal;
2080 /* COCOA example 1 */
2081 map = isl_map_read_from_str(ctx,
2082 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2083 "1 <= i and i < n and 1 <= j and j < n or "
2084 "i2 = i + 1 and j2 = j - 1 and "
2085 "1 <= i and i < n and 2 <= j and j <= n }");
2086 map = isl_map_power(map, &exact);
2087 assert(exact);
2088 isl_map_free(map);
2090 /* COCOA example 1 */
2091 map = isl_map_read_from_str(ctx,
2092 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2093 "1 <= i and i < n and 1 <= j and j < n or "
2094 "i2 = i + 1 and j2 = j - 1 and "
2095 "1 <= i and i < n and 2 <= j and j <= n }");
2096 map = isl_map_transitive_closure(map, &exact);
2097 assert(exact);
2098 map2 = isl_map_read_from_str(ctx,
2099 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2100 "1 <= i and i < n and 1 <= j and j <= n and "
2101 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2102 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2103 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2104 assert(isl_map_is_equal(map, map2));
2105 isl_map_free(map2);
2106 isl_map_free(map);
2108 map = isl_map_read_from_str(ctx,
2109 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2110 " 0 <= y and y <= n }");
2111 map = isl_map_transitive_closure(map, &exact);
2112 map2 = isl_map_read_from_str(ctx,
2113 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2114 " 0 <= y and y <= n }");
2115 assert(isl_map_is_equal(map, map2));
2116 isl_map_free(map2);
2117 isl_map_free(map);
2119 /* COCOA example 2 */
2120 map = isl_map_read_from_str(ctx,
2121 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2122 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2123 "i2 = i + 2 and j2 = j - 2 and "
2124 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2125 map = isl_map_transitive_closure(map, &exact);
2126 assert(exact);
2127 map2 = isl_map_read_from_str(ctx,
2128 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2129 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2130 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2131 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2132 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2133 assert(isl_map_is_equal(map, map2));
2134 isl_map_free(map);
2135 isl_map_free(map2);
2137 /* COCOA Fig.2 left */
2138 map = isl_map_read_from_str(ctx,
2139 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2140 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2141 "j <= n or "
2142 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2143 "j <= 2 i - 3 and j <= n - 2 or "
2144 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2145 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2146 map = isl_map_transitive_closure(map, &exact);
2147 assert(exact);
2148 isl_map_free(map);
2150 /* COCOA Fig.2 right */
2151 map = isl_map_read_from_str(ctx,
2152 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2153 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2154 "j <= n or "
2155 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2156 "j <= 2 i - 4 and j <= n - 3 or "
2157 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2158 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2159 map = isl_map_power(map, &exact);
2160 assert(exact);
2161 isl_map_free(map);
2163 /* COCOA Fig.2 right */
2164 map = isl_map_read_from_str(ctx,
2165 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2166 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2167 "j <= n or "
2168 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2169 "j <= 2 i - 4 and j <= n - 3 or "
2170 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2171 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2172 map = isl_map_transitive_closure(map, &exact);
2173 assert(exact);
2174 map2 = isl_map_read_from_str(ctx,
2175 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2176 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2177 "j <= n and 3 + i + 2 j <= 3 n and "
2178 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2179 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2180 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2181 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2182 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2183 assert(isl_map_is_equal(map, map2));
2184 isl_map_free(map2);
2185 isl_map_free(map);
2187 map = cocoa_fig_1_right_graph(ctx);
2188 map = isl_map_transitive_closure(map, &exact);
2189 assert(exact);
2190 map2 = cocoa_fig_1_right_tc(ctx);
2191 assert(isl_map_is_equal(map, map2));
2192 isl_map_free(map2);
2193 isl_map_free(map);
2195 map = cocoa_fig_1_right_graph(ctx);
2196 map = isl_map_power(map, &exact);
2197 map2 = cocoa_fig_1_right_power(ctx);
2198 equal = isl_map_is_equal(map, map2);
2199 isl_map_free(map2);
2200 isl_map_free(map);
2201 if (equal < 0)
2202 return -1;
2203 if (!exact)
2204 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2205 if (!equal)
2206 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2208 /* COCOA Theorem 1 counter example */
2209 map = isl_map_read_from_str(ctx,
2210 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2211 "i2 = 1 and j2 = j or "
2212 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2213 map = isl_map_transitive_closure(map, &exact);
2214 assert(exact);
2215 isl_map_free(map);
2217 map = isl_map_read_from_str(ctx,
2218 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2219 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2220 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2221 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2222 map = isl_map_transitive_closure(map, &exact);
2223 assert(exact);
2224 isl_map_free(map);
2226 /* Kelly et al 1996, fig 12 */
2227 map = isl_map_read_from_str(ctx,
2228 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2229 "1 <= i,j,j+1 <= n or "
2230 "j = n and j2 = 1 and i2 = i + 1 and "
2231 "1 <= i,i+1 <= n }");
2232 map = isl_map_transitive_closure(map, &exact);
2233 assert(exact);
2234 map2 = isl_map_read_from_str(ctx,
2235 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2236 "1 <= i <= n and i = i2 or "
2237 "1 <= i < i2 <= n and 1 <= j <= n and "
2238 "1 <= j2 <= n }");
2239 assert(isl_map_is_equal(map, map2));
2240 isl_map_free(map2);
2241 isl_map_free(map);
2243 /* Omega's closure4 */
2244 map = isl_map_read_from_str(ctx,
2245 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2246 "1 <= x,y <= 10 or "
2247 "x2 = x + 1 and y2 = y and "
2248 "1 <= x <= 20 && 5 <= y <= 15 }");
2249 map = isl_map_transitive_closure(map, &exact);
2250 assert(exact);
2251 isl_map_free(map);
2253 map = isl_map_read_from_str(ctx,
2254 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2255 map = isl_map_transitive_closure(map, &exact);
2256 assert(!exact);
2257 map2 = isl_map_read_from_str(ctx,
2258 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2259 assert(isl_map_is_equal(map, map2));
2260 isl_map_free(map);
2261 isl_map_free(map2);
2263 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2264 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2265 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2266 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2267 map = isl_map_read_from_str(ctx, str);
2268 map = isl_map_transitive_closure(map, &exact);
2269 assert(exact);
2270 map2 = isl_map_read_from_str(ctx, str);
2271 assert(isl_map_is_equal(map, map2));
2272 isl_map_free(map);
2273 isl_map_free(map2);
2275 str = "{[0] -> [1]; [2] -> [3]}";
2276 map = isl_map_read_from_str(ctx, str);
2277 map = isl_map_transitive_closure(map, &exact);
2278 assert(exact);
2279 map2 = isl_map_read_from_str(ctx, str);
2280 assert(isl_map_is_equal(map, map2));
2281 isl_map_free(map);
2282 isl_map_free(map2);
2284 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2285 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2286 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2287 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2288 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2289 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2290 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2291 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2292 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2293 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2294 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2295 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2296 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2297 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2298 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2299 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2300 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2301 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2302 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2303 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2304 map = isl_map_read_from_str(ctx, str);
2305 map = isl_map_transitive_closure(map, NULL);
2306 assert(map);
2307 isl_map_free(map);
2309 return 0;
2312 static int test_lex(struct isl_ctx *ctx)
2314 isl_space *dim;
2315 isl_map *map;
2316 int empty;
2318 dim = isl_space_set_alloc(ctx, 0, 0);
2319 map = isl_map_lex_le(dim);
2320 empty = isl_map_is_empty(map);
2321 isl_map_free(map);
2323 if (empty < 0)
2324 return -1;
2325 if (empty)
2326 isl_die(ctx, isl_error_unknown,
2327 "expecting non-empty result", return -1);
2329 return 0;
2332 /* Inputs for isl_map_lexmin tests.
2333 * "map" is the input and "lexmin" is the expected result.
2335 struct {
2336 const char *map;
2337 const char *lexmin;
2338 } lexmin_tests [] = {
2339 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
2340 "{ [x] -> [5] : 6 <= x <= 8; "
2341 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
2342 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
2343 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
2344 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
2345 "{ [x] -> [y] : (4y = x and x >= 0) or "
2346 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2347 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2348 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
2349 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
2350 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
2351 /* Check that empty pieces are properly combined. */
2352 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2353 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2354 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
2355 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2356 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2357 "x >= 4 }" },
2358 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
2359 "a <= 255 and c <= 255 and d <= 255 - j and "
2360 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
2361 "247d <= 247 + k - j and 247d <= 247 + k - b and "
2362 "247d <= 247 + i and 248 - b <= 248d <= c and "
2363 "254d >= i - a + b and 254d >= -a + b and "
2364 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
2365 "{ [i, k, j] -> "
2366 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
2367 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
2368 "247j >= 62738 - i and 509j <= 129795 + i and "
2369 "742j >= 188724 - i; "
2370 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
2371 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
2372 "16*floor((8 + b)/16) <= 7 + b; "
2373 "[a] -> [1] }",
2374 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
2375 "[a] -> [b = 0] : 0 < a <= 509 }" },
2376 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
2377 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
2380 static int test_lexmin(struct isl_ctx *ctx)
2382 int i;
2383 int equal;
2384 const char *str;
2385 isl_basic_map *bmap;
2386 isl_map *map, *map2;
2387 isl_set *set;
2388 isl_set *set2;
2389 isl_pw_multi_aff *pma;
2391 str = "[p0, p1] -> { [] -> [] : "
2392 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2393 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2394 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2395 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2396 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2397 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2398 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2399 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2400 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2401 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2402 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2403 map = isl_map_read_from_str(ctx, str);
2404 map = isl_map_lexmin(map);
2405 isl_map_free(map);
2407 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2408 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2409 set = isl_set_read_from_str(ctx, str);
2410 set = isl_set_lexmax(set);
2411 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2412 set2 = isl_set_read_from_str(ctx, str);
2413 set = isl_set_intersect(set, set2);
2414 assert(!isl_set_is_empty(set));
2415 isl_set_free(set);
2417 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
2418 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
2419 map = isl_map_lexmin(map);
2420 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
2421 equal = isl_map_is_equal(map, map2);
2422 isl_map_free(map);
2423 isl_map_free(map2);
2425 if (equal < 0)
2426 return -1;
2427 if (!equal)
2428 isl_die(ctx, isl_error_unknown,
2429 "unexpected result", return -1);
2432 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2433 " 8i' <= i and 8i' >= -7 + i }";
2434 bmap = isl_basic_map_read_from_str(ctx, str);
2435 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2436 map2 = isl_map_from_pw_multi_aff(pma);
2437 map = isl_map_from_basic_map(bmap);
2438 assert(isl_map_is_equal(map, map2));
2439 isl_map_free(map);
2440 isl_map_free(map2);
2442 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2443 " 8i' <= i and 8i' >= -7 + i }";
2444 set = isl_set_read_from_str(ctx, str);
2445 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2446 set2 = isl_set_from_pw_multi_aff(pma);
2447 equal = isl_set_is_equal(set, set2);
2448 isl_set_free(set);
2449 isl_set_free(set2);
2450 if (equal < 0)
2451 return -1;
2452 if (!equal)
2453 isl_die(ctx, isl_error_unknown,
2454 "unexpected difference between set and "
2455 "piecewise affine expression", return -1);
2457 return 0;
2460 /* A specialized isl_set_min_val test case that would return the wrong result
2461 * in earlier versions of isl.
2462 * The explicit call to isl_basic_set_union prevents the second basic set
2463 * from being determined to be empty prior to the call to isl_set_min_val,
2464 * at least at the point where this test case was introduced.
2466 static int test_min_special(isl_ctx *ctx)
2468 const char *str;
2469 isl_basic_set *bset1, *bset2;
2470 isl_set *set;
2471 isl_aff *obj;
2472 isl_val *res;
2473 int ok;
2475 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
2476 bset1 = isl_basic_set_read_from_str(ctx, str);
2477 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
2478 bset2 = isl_basic_set_read_from_str(ctx, str);
2479 set = isl_basic_set_union(bset1, bset2);
2480 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
2482 res = isl_set_min_val(set, obj);
2483 ok = isl_val_cmp_si(res, 5) == 0;
2485 isl_aff_free(obj);
2486 isl_set_free(set);
2487 isl_val_free(res);
2489 if (!res)
2490 return -1;
2491 if (!ok)
2492 isl_die(ctx, isl_error_unknown, "unexpected minimum",
2493 return -1);
2495 return 0;
2498 /* A specialized isl_set_min_val test case that would return an error
2499 * in earlier versions of isl.
2501 static int test_min_special2(isl_ctx *ctx)
2503 const char *str;
2504 isl_basic_set *bset;
2505 isl_aff *obj;
2506 isl_val *res;
2508 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
2509 bset = isl_basic_set_read_from_str(ctx, str);
2511 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
2513 res = isl_basic_set_max_val(bset, obj);
2515 isl_basic_set_free(bset);
2516 isl_aff_free(obj);
2517 isl_val_free(res);
2519 if (!res)
2520 return -1;
2522 return 0;
2525 struct {
2526 const char *set;
2527 const char *obj;
2528 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2529 __isl_keep isl_aff *obj);
2530 const char *res;
2531 } opt_tests[] = {
2532 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2533 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2534 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2535 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2536 &isl_set_max_val, "30" },
2540 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2541 * In particular, check the results on non-convex inputs.
2543 static int test_min(struct isl_ctx *ctx)
2545 int i;
2546 isl_set *set;
2547 isl_aff *obj;
2548 isl_val *val, *res;
2549 isl_bool ok;
2551 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2552 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2553 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2554 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2555 val = opt_tests[i].fn(set, obj);
2556 ok = isl_val_eq(res, val);
2557 isl_val_free(res);
2558 isl_val_free(val);
2559 isl_aff_free(obj);
2560 isl_set_free(set);
2562 if (ok < 0)
2563 return -1;
2564 if (!ok)
2565 isl_die(ctx, isl_error_unknown,
2566 "unexpected optimum", return -1);
2569 if (test_min_special(ctx) < 0)
2570 return -1;
2571 if (test_min_special2(ctx) < 0)
2572 return -1;
2574 return 0;
2577 struct must_may {
2578 isl_map *must;
2579 isl_map *may;
2582 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2583 void *dep_user, void *user)
2585 struct must_may *mm = (struct must_may *)user;
2587 if (must)
2588 mm->must = isl_map_union(mm->must, dep);
2589 else
2590 mm->may = isl_map_union(mm->may, dep);
2592 return isl_stat_ok;
2595 static int common_space(void *first, void *second)
2597 int depth = *(int *)first;
2598 return 2 * depth;
2601 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2603 isl_map *map2;
2604 int equal;
2606 if (!map)
2607 return -1;
2609 map2 = isl_map_read_from_str(map->ctx, str);
2610 equal = isl_map_is_equal(map, map2);
2611 isl_map_free(map2);
2613 return equal;
2616 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2618 int equal;
2620 equal = map_is_equal(map, str);
2621 if (equal < 0)
2622 return -1;
2623 if (!equal)
2624 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2625 "result not as expected", return -1);
2626 return 0;
2629 static int test_dep(struct isl_ctx *ctx)
2631 const char *str;
2632 isl_space *dim;
2633 isl_map *map;
2634 isl_access_info *ai;
2635 isl_flow *flow;
2636 int depth;
2637 struct must_may mm;
2639 depth = 3;
2641 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2642 map = isl_map_read_from_str(ctx, str);
2643 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2645 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2646 map = isl_map_read_from_str(ctx, str);
2647 ai = isl_access_info_add_source(ai, map, 1, &depth);
2649 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2650 map = isl_map_read_from_str(ctx, str);
2651 ai = isl_access_info_add_source(ai, map, 1, &depth);
2653 flow = isl_access_info_compute_flow(ai);
2654 dim = isl_space_alloc(ctx, 0, 3, 3);
2655 mm.must = isl_map_empty(isl_space_copy(dim));
2656 mm.may = isl_map_empty(dim);
2658 isl_flow_foreach(flow, collect_must_may, &mm);
2660 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2661 " [1,10,0] -> [2,5,0] }";
2662 assert(map_is_equal(mm.must, str));
2663 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2664 assert(map_is_equal(mm.may, str));
2666 isl_map_free(mm.must);
2667 isl_map_free(mm.may);
2668 isl_flow_free(flow);
2671 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2672 map = isl_map_read_from_str(ctx, str);
2673 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2675 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2676 map = isl_map_read_from_str(ctx, str);
2677 ai = isl_access_info_add_source(ai, map, 1, &depth);
2679 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2680 map = isl_map_read_from_str(ctx, str);
2681 ai = isl_access_info_add_source(ai, map, 0, &depth);
2683 flow = isl_access_info_compute_flow(ai);
2684 dim = isl_space_alloc(ctx, 0, 3, 3);
2685 mm.must = isl_map_empty(isl_space_copy(dim));
2686 mm.may = isl_map_empty(dim);
2688 isl_flow_foreach(flow, collect_must_may, &mm);
2690 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2691 assert(map_is_equal(mm.must, str));
2692 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2693 assert(map_is_equal(mm.may, str));
2695 isl_map_free(mm.must);
2696 isl_map_free(mm.may);
2697 isl_flow_free(flow);
2700 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2701 map = isl_map_read_from_str(ctx, str);
2702 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2704 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2705 map = isl_map_read_from_str(ctx, str);
2706 ai = isl_access_info_add_source(ai, map, 0, &depth);
2708 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2709 map = isl_map_read_from_str(ctx, str);
2710 ai = isl_access_info_add_source(ai, map, 0, &depth);
2712 flow = isl_access_info_compute_flow(ai);
2713 dim = isl_space_alloc(ctx, 0, 3, 3);
2714 mm.must = isl_map_empty(isl_space_copy(dim));
2715 mm.may = isl_map_empty(dim);
2717 isl_flow_foreach(flow, collect_must_may, &mm);
2719 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2720 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2721 assert(map_is_equal(mm.may, str));
2722 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2723 assert(map_is_equal(mm.must, str));
2725 isl_map_free(mm.must);
2726 isl_map_free(mm.may);
2727 isl_flow_free(flow);
2730 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2731 map = isl_map_read_from_str(ctx, str);
2732 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2734 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2735 map = isl_map_read_from_str(ctx, str);
2736 ai = isl_access_info_add_source(ai, map, 0, &depth);
2738 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2739 map = isl_map_read_from_str(ctx, str);
2740 ai = isl_access_info_add_source(ai, map, 0, &depth);
2742 flow = isl_access_info_compute_flow(ai);
2743 dim = isl_space_alloc(ctx, 0, 3, 3);
2744 mm.must = isl_map_empty(isl_space_copy(dim));
2745 mm.may = isl_map_empty(dim);
2747 isl_flow_foreach(flow, collect_must_may, &mm);
2749 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2750 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2751 assert(map_is_equal(mm.may, str));
2752 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2753 assert(map_is_equal(mm.must, str));
2755 isl_map_free(mm.must);
2756 isl_map_free(mm.may);
2757 isl_flow_free(flow);
2760 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2761 map = isl_map_read_from_str(ctx, str);
2762 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2764 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2765 map = isl_map_read_from_str(ctx, str);
2766 ai = isl_access_info_add_source(ai, map, 0, &depth);
2768 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2769 map = isl_map_read_from_str(ctx, str);
2770 ai = isl_access_info_add_source(ai, map, 0, &depth);
2772 flow = isl_access_info_compute_flow(ai);
2773 dim = isl_space_alloc(ctx, 0, 3, 3);
2774 mm.must = isl_map_empty(isl_space_copy(dim));
2775 mm.may = isl_map_empty(dim);
2777 isl_flow_foreach(flow, collect_must_may, &mm);
2779 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2780 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2781 assert(map_is_equal(mm.may, str));
2782 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2783 assert(map_is_equal(mm.must, str));
2785 isl_map_free(mm.must);
2786 isl_map_free(mm.may);
2787 isl_flow_free(flow);
2790 depth = 5;
2792 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2793 map = isl_map_read_from_str(ctx, str);
2794 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2796 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2797 map = isl_map_read_from_str(ctx, str);
2798 ai = isl_access_info_add_source(ai, map, 1, &depth);
2800 flow = isl_access_info_compute_flow(ai);
2801 dim = isl_space_alloc(ctx, 0, 5, 5);
2802 mm.must = isl_map_empty(isl_space_copy(dim));
2803 mm.may = isl_map_empty(dim);
2805 isl_flow_foreach(flow, collect_must_may, &mm);
2807 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2808 assert(map_is_equal(mm.must, str));
2809 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2810 assert(map_is_equal(mm.may, str));
2812 isl_map_free(mm.must);
2813 isl_map_free(mm.may);
2814 isl_flow_free(flow);
2816 return 0;
2819 /* Check that the dependence analysis proceeds without errors.
2820 * Earlier versions of isl would break down during the analysis
2821 * due to the use of the wrong spaces.
2823 static int test_flow(isl_ctx *ctx)
2825 const char *str;
2826 isl_union_map *access, *schedule;
2827 isl_union_map *must_dep, *may_dep;
2828 int r;
2830 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2831 access = isl_union_map_read_from_str(ctx, str);
2832 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2833 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2834 "S2[] -> [1,0,0,0]; "
2835 "S3[] -> [-1,0,0,0] }";
2836 schedule = isl_union_map_read_from_str(ctx, str);
2837 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2838 isl_union_map_copy(access), schedule,
2839 &must_dep, &may_dep, NULL, NULL);
2840 isl_union_map_free(may_dep);
2841 isl_union_map_free(must_dep);
2843 return r;
2846 struct {
2847 const char *map;
2848 int sv;
2849 } sv_tests[] = {
2850 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2851 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2852 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2853 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2854 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2855 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2856 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2857 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2858 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2861 int test_sv(isl_ctx *ctx)
2863 isl_union_map *umap;
2864 int i;
2865 int sv;
2867 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2868 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2869 sv = isl_union_map_is_single_valued(umap);
2870 isl_union_map_free(umap);
2871 if (sv < 0)
2872 return -1;
2873 if (sv_tests[i].sv && !sv)
2874 isl_die(ctx, isl_error_internal,
2875 "map not detected as single valued", return -1);
2876 if (!sv_tests[i].sv && sv)
2877 isl_die(ctx, isl_error_internal,
2878 "map detected as single valued", return -1);
2881 return 0;
2884 struct {
2885 const char *str;
2886 int bijective;
2887 } bijective_tests[] = {
2888 { "[N,M]->{[i,j] -> [i]}", 0 },
2889 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
2890 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
2891 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
2892 { "[N,M]->{[i,j] -> [j,i]}", 1 },
2893 { "[N,M]->{[i,j] -> [i+j]}", 0 },
2894 { "[N,M]->{[i,j] -> []}", 0 },
2895 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
2896 { "[N,M]->{[i,j] -> [2i]}", 0 },
2897 { "[N,M]->{[i,j] -> [i,i]}", 0 },
2898 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
2899 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
2900 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
2903 static int test_bijective(struct isl_ctx *ctx)
2905 isl_map *map;
2906 int i;
2907 int bijective;
2909 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
2910 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
2911 bijective = isl_map_is_bijective(map);
2912 isl_map_free(map);
2913 if (bijective < 0)
2914 return -1;
2915 if (bijective_tests[i].bijective && !bijective)
2916 isl_die(ctx, isl_error_internal,
2917 "map not detected as bijective", return -1);
2918 if (!bijective_tests[i].bijective && bijective)
2919 isl_die(ctx, isl_error_internal,
2920 "map detected as bijective", return -1);
2923 return 0;
2926 /* Inputs for isl_pw_qpolynomial_gist tests.
2927 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2929 struct {
2930 const char *pwqp;
2931 const char *set;
2932 const char *gist;
2933 } pwqp_gist_tests[] = {
2934 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2935 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2936 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2937 "{ [i] -> -1/2 + 1/2 * i }" },
2938 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2941 static int test_pwqp(struct isl_ctx *ctx)
2943 int i;
2944 const char *str;
2945 isl_set *set;
2946 isl_pw_qpolynomial *pwqp1, *pwqp2;
2947 int equal;
2949 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2950 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2952 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2953 isl_dim_in, 1, 1);
2955 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2956 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2958 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2960 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2962 isl_pw_qpolynomial_free(pwqp1);
2964 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2965 str = pwqp_gist_tests[i].pwqp;
2966 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2967 str = pwqp_gist_tests[i].set;
2968 set = isl_set_read_from_str(ctx, str);
2969 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2970 str = pwqp_gist_tests[i].gist;
2971 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2972 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2973 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2974 isl_pw_qpolynomial_free(pwqp1);
2976 if (equal < 0)
2977 return -1;
2978 if (!equal)
2979 isl_die(ctx, isl_error_unknown,
2980 "unexpected result", return -1);
2983 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2984 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2985 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2986 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2988 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2990 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2992 isl_pw_qpolynomial_free(pwqp1);
2994 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2995 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2996 str = "{ [x] -> x }";
2997 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2999 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3001 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3003 isl_pw_qpolynomial_free(pwqp1);
3005 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3006 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3007 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3008 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3009 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3010 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3011 isl_pw_qpolynomial_free(pwqp1);
3013 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3014 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3015 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3016 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3017 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3018 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3019 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3020 isl_pw_qpolynomial_free(pwqp1);
3021 isl_pw_qpolynomial_free(pwqp2);
3022 if (equal < 0)
3023 return -1;
3024 if (!equal)
3025 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3027 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3028 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3029 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3030 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3031 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3032 isl_val_one(ctx));
3033 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3034 isl_pw_qpolynomial_free(pwqp1);
3035 isl_pw_qpolynomial_free(pwqp2);
3036 if (equal < 0)
3037 return -1;
3038 if (!equal)
3039 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3041 return 0;
3044 static int test_split_periods(isl_ctx *ctx)
3046 const char *str;
3047 isl_pw_qpolynomial *pwqp;
3049 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3050 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3051 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3052 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3054 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3056 isl_pw_qpolynomial_free(pwqp);
3058 if (!pwqp)
3059 return -1;
3061 return 0;
3064 static int test_union(isl_ctx *ctx)
3066 const char *str;
3067 isl_union_set *uset1, *uset2;
3068 isl_union_map *umap1, *umap2;
3069 int equal;
3071 str = "{ [i] : 0 <= i <= 1 }";
3072 uset1 = isl_union_set_read_from_str(ctx, str);
3073 str = "{ [1] -> [0] }";
3074 umap1 = isl_union_map_read_from_str(ctx, str);
3076 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3077 equal = isl_union_map_is_equal(umap1, umap2);
3079 isl_union_map_free(umap1);
3080 isl_union_map_free(umap2);
3082 if (equal < 0)
3083 return -1;
3084 if (!equal)
3085 isl_die(ctx, isl_error_unknown, "union maps not equal",
3086 return -1);
3088 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3089 umap1 = isl_union_map_read_from_str(ctx, str);
3090 str = "{ A[i]; B[i] }";
3091 uset1 = isl_union_set_read_from_str(ctx, str);
3093 uset2 = isl_union_map_domain(umap1);
3095 equal = isl_union_set_is_equal(uset1, uset2);
3097 isl_union_set_free(uset1);
3098 isl_union_set_free(uset2);
3100 if (equal < 0)
3101 return -1;
3102 if (!equal)
3103 isl_die(ctx, isl_error_unknown, "union sets not equal",
3104 return -1);
3106 return 0;
3109 /* Check that computing a bound of a non-zero polynomial over an unbounded
3110 * domain does not produce a rational value.
3111 * In particular, check that the upper bound is infinity.
3113 static int test_bound_unbounded_domain(isl_ctx *ctx)
3115 const char *str;
3116 isl_pw_qpolynomial *pwqp;
3117 isl_pw_qpolynomial_fold *pwf, *pwf2;
3118 isl_bool equal;
3120 str = "{ [m,n] -> -m * n }";
3121 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3122 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3123 str = "{ infty }";
3124 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3125 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3126 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
3127 isl_pw_qpolynomial_fold_free(pwf);
3128 isl_pw_qpolynomial_fold_free(pwf2);
3130 if (equal < 0)
3131 return -1;
3132 if (!equal)
3133 isl_die(ctx, isl_error_unknown,
3134 "expecting infinite polynomial bound", return -1);
3136 return 0;
3139 static int test_bound(isl_ctx *ctx)
3141 const char *str;
3142 unsigned dim;
3143 isl_pw_qpolynomial *pwqp;
3144 isl_pw_qpolynomial_fold *pwf;
3146 if (test_bound_unbounded_domain(ctx) < 0)
3147 return -1;
3149 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
3150 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3151 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3152 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3153 isl_pw_qpolynomial_fold_free(pwf);
3154 if (dim != 4)
3155 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3156 return -1);
3158 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
3159 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3160 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3161 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3162 isl_pw_qpolynomial_fold_free(pwf);
3163 if (dim != 1)
3164 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3165 return -1);
3167 return 0;
3170 static int test_lift(isl_ctx *ctx)
3172 const char *str;
3173 isl_basic_map *bmap;
3174 isl_basic_set *bset;
3176 str = "{ [i0] : exists e0 : i0 = 4e0 }";
3177 bset = isl_basic_set_read_from_str(ctx, str);
3178 bset = isl_basic_set_lift(bset);
3179 bmap = isl_basic_map_from_range(bset);
3180 bset = isl_basic_map_domain(bmap);
3181 isl_basic_set_free(bset);
3183 return 0;
3186 struct {
3187 const char *set1;
3188 const char *set2;
3189 int subset;
3190 } subset_tests[] = {
3191 { "{ [112, 0] }",
3192 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
3193 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
3194 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
3195 { "{ [65] }",
3196 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
3197 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
3198 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
3199 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
3200 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
3201 "256e0 <= 255i and 256e0 >= -255 + 255i and "
3202 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
3203 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
3204 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
3205 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
3206 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
3207 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
3208 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
3209 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
3210 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
3211 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
3212 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
3213 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
3214 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
3215 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
3216 "4e0 <= -57 + i0 + i1)) or "
3217 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
3218 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
3219 "4e0 >= -61 + i0 + i1)) or "
3220 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
3221 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
3224 static int test_subset(isl_ctx *ctx)
3226 int i;
3227 isl_set *set1, *set2;
3228 int subset;
3230 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3231 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3232 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3233 subset = isl_set_is_subset(set1, set2);
3234 isl_set_free(set1);
3235 isl_set_free(set2);
3236 if (subset < 0)
3237 return -1;
3238 if (subset != subset_tests[i].subset)
3239 isl_die(ctx, isl_error_unknown,
3240 "incorrect subset result", return -1);
3243 return 0;
3246 struct {
3247 const char *minuend;
3248 const char *subtrahend;
3249 const char *difference;
3250 } subtract_domain_tests[] = {
3251 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3252 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3253 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3256 static int test_subtract(isl_ctx *ctx)
3258 int i;
3259 isl_union_map *umap1, *umap2;
3260 isl_union_pw_multi_aff *upma1, *upma2;
3261 isl_union_set *uset;
3262 int equal;
3264 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3265 umap1 = isl_union_map_read_from_str(ctx,
3266 subtract_domain_tests[i].minuend);
3267 uset = isl_union_set_read_from_str(ctx,
3268 subtract_domain_tests[i].subtrahend);
3269 umap2 = isl_union_map_read_from_str(ctx,
3270 subtract_domain_tests[i].difference);
3271 umap1 = isl_union_map_subtract_domain(umap1, uset);
3272 equal = isl_union_map_is_equal(umap1, umap2);
3273 isl_union_map_free(umap1);
3274 isl_union_map_free(umap2);
3275 if (equal < 0)
3276 return -1;
3277 if (!equal)
3278 isl_die(ctx, isl_error_unknown,
3279 "incorrect subtract domain result", return -1);
3282 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3283 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3284 subtract_domain_tests[i].minuend);
3285 uset = isl_union_set_read_from_str(ctx,
3286 subtract_domain_tests[i].subtrahend);
3287 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3288 subtract_domain_tests[i].difference);
3289 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3290 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3291 isl_union_pw_multi_aff_free(upma1);
3292 isl_union_pw_multi_aff_free(upma2);
3293 if (equal < 0)
3294 return -1;
3295 if (!equal)
3296 isl_die(ctx, isl_error_unknown,
3297 "incorrect subtract domain result", return -1);
3300 return 0;
3303 /* Check that intersecting the empty basic set with another basic set
3304 * does not increase the number of constraints. In particular,
3305 * the empty basic set should maintain its canonical representation.
3307 static int test_intersect(isl_ctx *ctx)
3309 int n1, n2;
3310 isl_basic_set *bset1, *bset2;
3312 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3313 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3314 n1 = isl_basic_set_n_constraint(bset1);
3315 bset1 = isl_basic_set_intersect(bset1, bset2);
3316 n2 = isl_basic_set_n_constraint(bset1);
3317 isl_basic_set_free(bset1);
3318 if (!bset1)
3319 return -1;
3320 if (n1 != n2)
3321 isl_die(ctx, isl_error_unknown,
3322 "number of constraints of empty set changed",
3323 return -1);
3325 return 0;
3328 int test_factorize(isl_ctx *ctx)
3330 const char *str;
3331 isl_basic_set *bset;
3332 isl_factorizer *f;
3334 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3335 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3336 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3337 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3338 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3339 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3340 "3i5 >= -2i0 - i2 + 3i4 }";
3341 bset = isl_basic_set_read_from_str(ctx, str);
3342 f = isl_basic_set_factorizer(bset);
3343 isl_basic_set_free(bset);
3344 isl_factorizer_free(f);
3345 if (!f)
3346 isl_die(ctx, isl_error_unknown,
3347 "failed to construct factorizer", return -1);
3349 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3350 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3351 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3352 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3353 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3354 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3355 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3356 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3357 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3358 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3359 bset = isl_basic_set_read_from_str(ctx, str);
3360 f = isl_basic_set_factorizer(bset);
3361 isl_basic_set_free(bset);
3362 isl_factorizer_free(f);
3363 if (!f)
3364 isl_die(ctx, isl_error_unknown,
3365 "failed to construct factorizer", return -1);
3367 return 0;
3370 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3372 int *injective = user;
3374 *injective = isl_map_is_injective(map);
3375 isl_map_free(map);
3377 if (*injective < 0 || !*injective)
3378 return isl_stat_error;
3380 return isl_stat_ok;
3383 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3384 const char *r, const char *s, int tilable, int parallel)
3386 int i;
3387 isl_union_set *D;
3388 isl_union_map *W, *R, *S;
3389 isl_union_map *empty;
3390 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3391 isl_union_map *validity, *proximity, *coincidence;
3392 isl_union_map *schedule;
3393 isl_union_map *test;
3394 isl_union_set *delta;
3395 isl_union_set *domain;
3396 isl_set *delta_set;
3397 isl_set *slice;
3398 isl_set *origin;
3399 isl_schedule_constraints *sc;
3400 isl_schedule *sched;
3401 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3403 D = isl_union_set_read_from_str(ctx, d);
3404 W = isl_union_map_read_from_str(ctx, w);
3405 R = isl_union_map_read_from_str(ctx, r);
3406 S = isl_union_map_read_from_str(ctx, s);
3408 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3409 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3411 empty = isl_union_map_empty(isl_union_map_get_space(S));
3412 isl_union_map_compute_flow(isl_union_map_copy(R),
3413 isl_union_map_copy(W), empty,
3414 isl_union_map_copy(S),
3415 &dep_raw, NULL, NULL, NULL);
3416 isl_union_map_compute_flow(isl_union_map_copy(W),
3417 isl_union_map_copy(W),
3418 isl_union_map_copy(R),
3419 isl_union_map_copy(S),
3420 &dep_waw, &dep_war, NULL, NULL);
3422 dep = isl_union_map_union(dep_waw, dep_war);
3423 dep = isl_union_map_union(dep, dep_raw);
3424 validity = isl_union_map_copy(dep);
3425 coincidence = isl_union_map_copy(dep);
3426 proximity = isl_union_map_copy(dep);
3428 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3429 sc = isl_schedule_constraints_set_validity(sc, validity);
3430 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3431 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3432 sched = isl_schedule_constraints_compute_schedule(sc);
3433 schedule = isl_schedule_get_map(sched);
3434 isl_schedule_free(sched);
3435 isl_union_map_free(W);
3436 isl_union_map_free(R);
3437 isl_union_map_free(S);
3439 is_injection = 1;
3440 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3442 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3443 is_complete = isl_union_set_is_subset(D, domain);
3444 isl_union_set_free(D);
3445 isl_union_set_free(domain);
3447 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3448 test = isl_union_map_apply_range(test, dep);
3449 test = isl_union_map_apply_range(test, schedule);
3451 delta = isl_union_map_deltas(test);
3452 if (isl_union_set_n_set(delta) == 0) {
3453 is_tilable = 1;
3454 is_parallel = 1;
3455 is_nonneg = 1;
3456 isl_union_set_free(delta);
3457 } else {
3458 delta_set = isl_set_from_union_set(delta);
3460 slice = isl_set_universe(isl_set_get_space(delta_set));
3461 for (i = 0; i < tilable; ++i)
3462 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3463 is_tilable = isl_set_is_subset(delta_set, slice);
3464 isl_set_free(slice);
3466 slice = isl_set_universe(isl_set_get_space(delta_set));
3467 for (i = 0; i < parallel; ++i)
3468 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3469 is_parallel = isl_set_is_subset(delta_set, slice);
3470 isl_set_free(slice);
3472 origin = isl_set_universe(isl_set_get_space(delta_set));
3473 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
3474 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3476 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3477 delta_set = isl_set_lexmin(delta_set);
3479 is_nonneg = isl_set_is_equal(delta_set, origin);
3481 isl_set_free(origin);
3482 isl_set_free(delta_set);
3485 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3486 is_injection < 0 || is_complete < 0)
3487 return -1;
3488 if (!is_complete)
3489 isl_die(ctx, isl_error_unknown,
3490 "generated schedule incomplete", return -1);
3491 if (!is_injection)
3492 isl_die(ctx, isl_error_unknown,
3493 "generated schedule not injective on each statement",
3494 return -1);
3495 if (!is_nonneg)
3496 isl_die(ctx, isl_error_unknown,
3497 "negative dependences in generated schedule",
3498 return -1);
3499 if (!is_tilable)
3500 isl_die(ctx, isl_error_unknown,
3501 "generated schedule not as tilable as expected",
3502 return -1);
3503 if (!is_parallel)
3504 isl_die(ctx, isl_error_unknown,
3505 "generated schedule not as parallel as expected",
3506 return -1);
3508 return 0;
3511 /* Compute a schedule for the given instance set, validity constraints,
3512 * proximity constraints and context and return a corresponding union map
3513 * representation.
3515 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
3516 const char *domain, const char *validity, const char *proximity,
3517 const char *context)
3519 isl_set *con;
3520 isl_union_set *dom;
3521 isl_union_map *dep;
3522 isl_union_map *prox;
3523 isl_schedule_constraints *sc;
3524 isl_schedule *schedule;
3525 isl_union_map *sched;
3527 con = isl_set_read_from_str(ctx, context);
3528 dom = isl_union_set_read_from_str(ctx, domain);
3529 dep = isl_union_map_read_from_str(ctx, validity);
3530 prox = isl_union_map_read_from_str(ctx, proximity);
3531 sc = isl_schedule_constraints_on_domain(dom);
3532 sc = isl_schedule_constraints_set_context(sc, con);
3533 sc = isl_schedule_constraints_set_validity(sc, dep);
3534 sc = isl_schedule_constraints_set_proximity(sc, prox);
3535 schedule = isl_schedule_constraints_compute_schedule(sc);
3536 sched = isl_schedule_get_map(schedule);
3537 isl_schedule_free(schedule);
3539 return sched;
3542 /* Compute a schedule for the given instance set, validity constraints and
3543 * proximity constraints and return a corresponding union map representation.
3545 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
3546 const char *domain, const char *validity, const char *proximity)
3548 return compute_schedule_with_context(ctx, domain, validity, proximity,
3549 "{ : }");
3552 /* Check that a schedule can be constructed on the given domain
3553 * with the given validity and proximity constraints.
3555 static int test_has_schedule(isl_ctx *ctx, const char *domain,
3556 const char *validity, const char *proximity)
3558 isl_union_map *sched;
3560 sched = compute_schedule(ctx, domain, validity, proximity);
3561 if (!sched)
3562 return -1;
3564 isl_union_map_free(sched);
3565 return 0;
3568 int test_special_schedule(isl_ctx *ctx, const char *domain,
3569 const char *validity, const char *proximity, const char *expected_sched)
3571 isl_union_map *sched1, *sched2;
3572 int equal;
3574 sched1 = compute_schedule(ctx, domain, validity, proximity);
3575 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
3577 equal = isl_union_map_is_equal(sched1, sched2);
3578 isl_union_map_free(sched1);
3579 isl_union_map_free(sched2);
3581 if (equal < 0)
3582 return -1;
3583 if (!equal)
3584 isl_die(ctx, isl_error_unknown, "unexpected schedule",
3585 return -1);
3587 return 0;
3590 /* Check that the schedule map is properly padded, i.e., that the range
3591 * lives in a single space.
3593 static int test_padded_schedule(isl_ctx *ctx)
3595 const char *str;
3596 isl_union_set *D;
3597 isl_union_map *validity, *proximity;
3598 isl_schedule_constraints *sc;
3599 isl_schedule *sched;
3600 isl_union_map *umap;
3601 isl_union_set *range;
3602 isl_set *set;
3604 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
3605 D = isl_union_set_read_from_str(ctx, str);
3606 validity = isl_union_map_empty(isl_union_set_get_space(D));
3607 proximity = isl_union_map_copy(validity);
3608 sc = isl_schedule_constraints_on_domain(D);
3609 sc = isl_schedule_constraints_set_validity(sc, validity);
3610 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3611 sched = isl_schedule_constraints_compute_schedule(sc);
3612 umap = isl_schedule_get_map(sched);
3613 isl_schedule_free(sched);
3614 range = isl_union_map_range(umap);
3615 set = isl_set_from_union_set(range);
3616 isl_set_free(set);
3618 if (!set)
3619 return -1;
3621 return 0;
3624 /* Check that conditional validity constraints are also taken into
3625 * account across bands.
3626 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
3627 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
3628 * and then check that the adjacent order constraint C[2,1]->D[2,0]
3629 * is enforced by the rest of the schedule.
3631 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
3633 const char *str;
3634 isl_union_set *domain;
3635 isl_union_map *validity, *proximity, *condition;
3636 isl_union_map *sink, *source, *dep;
3637 isl_schedule_constraints *sc;
3638 isl_schedule *schedule;
3639 isl_union_access_info *access;
3640 isl_union_flow *flow;
3641 int empty;
3643 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3644 "A[k] : k >= 1 and k <= -1 + n; "
3645 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
3646 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
3647 domain = isl_union_set_read_from_str(ctx, str);
3648 sc = isl_schedule_constraints_on_domain(domain);
3649 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
3650 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3651 "D[k, i] -> C[1 + k, i] : "
3652 "k <= -2 + n and i >= 1 and i <= -1 + k; "
3653 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
3654 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
3655 validity = isl_union_map_read_from_str(ctx, str);
3656 sc = isl_schedule_constraints_set_validity(sc, validity);
3657 str = "[n] -> { C[k, i] -> D[k, i] : "
3658 "0 <= i <= -1 + k and k <= -1 + n }";
3659 proximity = isl_union_map_read_from_str(ctx, str);
3660 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3661 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
3662 "i <= -1 + k and i >= 1 and k <= -2 + n; "
3663 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
3664 "k <= -1 + n and i >= 0 and i <= -2 + k }";
3665 condition = isl_union_map_read_from_str(ctx, str);
3666 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
3667 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3668 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
3669 "i >= 0 and i <= -1 + k and k <= -1 + n and "
3670 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
3671 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
3672 "i >= 0 and i <= -1 + k and k <= -1 + n; "
3673 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
3674 "k <= -1 + n and i >= 0 and i <= -1 + k and "
3675 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
3676 validity = isl_union_map_read_from_str(ctx, str);
3677 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3678 validity);
3679 schedule = isl_schedule_constraints_compute_schedule(sc);
3680 str = "{ D[2,0] -> [] }";
3681 sink = isl_union_map_read_from_str(ctx, str);
3682 access = isl_union_access_info_from_sink(sink);
3683 str = "{ C[2,1] -> [] }";
3684 source = isl_union_map_read_from_str(ctx, str);
3685 access = isl_union_access_info_set_must_source(access, source);
3686 access = isl_union_access_info_set_schedule(access, schedule);
3687 flow = isl_union_access_info_compute_flow(access);
3688 dep = isl_union_flow_get_must_dependence(flow);
3689 isl_union_flow_free(flow);
3690 empty = isl_union_map_is_empty(dep);
3691 isl_union_map_free(dep);
3693 if (empty < 0)
3694 return -1;
3695 if (empty)
3696 isl_die(ctx, isl_error_unknown,
3697 "conditional validity not respected", return -1);
3699 return 0;
3702 /* Check that the test for violated conditional validity constraints
3703 * is not confused by domain compression.
3704 * In particular, earlier versions of isl would apply
3705 * a schedule on the compressed domains to the original domains,
3706 * resulting in a failure to detect that the default schedule
3707 * violates the conditional validity constraints.
3709 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
3711 const char *str;
3712 isl_bool empty;
3713 isl_union_set *domain;
3714 isl_union_map *validity, *condition;
3715 isl_schedule_constraints *sc;
3716 isl_schedule *schedule;
3717 isl_union_map *umap;
3718 isl_map *map, *ge;
3720 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
3721 domain = isl_union_set_read_from_str(ctx, str);
3722 sc = isl_schedule_constraints_on_domain(domain);
3723 str = "{ B[1, i] -> A[0, i + 1] }";
3724 condition = isl_union_map_read_from_str(ctx, str);
3725 str = "{ A[0, i] -> B[1, i - 1] }";
3726 validity = isl_union_map_read_from_str(ctx, str);
3727 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
3728 isl_union_map_copy(validity));
3729 schedule = isl_schedule_constraints_compute_schedule(sc);
3730 umap = isl_schedule_get_map(schedule);
3731 isl_schedule_free(schedule);
3732 validity = isl_union_map_apply_domain(validity,
3733 isl_union_map_copy(umap));
3734 validity = isl_union_map_apply_range(validity, umap);
3735 map = isl_map_from_union_map(validity);
3736 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3737 map = isl_map_intersect(map, ge);
3738 empty = isl_map_is_empty(map);
3739 isl_map_free(map);
3741 if (empty < 0)
3742 return -1;
3743 if (!empty)
3744 isl_die(ctx, isl_error_unknown,
3745 "conditional validity constraints not satisfied",
3746 return -1);
3748 return 0;
3751 /* Input for testing of schedule construction based on
3752 * conditional constraints.
3754 * domain is the iteration domain
3755 * flow are the flow dependences, which determine the validity and
3756 * proximity constraints
3757 * condition are the conditions on the conditional validity constraints
3758 * conditional_validity are the conditional validity constraints
3759 * outer_band_n is the expected number of members in the outer band
3761 struct {
3762 const char *domain;
3763 const char *flow;
3764 const char *condition;
3765 const char *conditional_validity;
3766 int outer_band_n;
3767 } live_range_tests[] = {
3768 /* Contrived example that illustrates that we need to keep
3769 * track of tagged condition dependences and
3770 * tagged conditional validity dependences
3771 * in isl_sched_edge separately.
3772 * In particular, the conditional validity constraints on A
3773 * cannot be satisfied,
3774 * but they can be ignored because there are no corresponding
3775 * condition constraints. However, we do have an additional
3776 * conditional validity constraint that maps to the same
3777 * dependence relation
3778 * as the condition constraint on B. If we did not make a distinction
3779 * between tagged condition and tagged conditional validity
3780 * dependences, then we
3781 * could end up treating this shared dependence as an condition
3782 * constraint on A, forcing a localization of the conditions,
3783 * which is impossible.
3785 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
3786 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
3787 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
3788 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3789 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3790 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
3793 /* TACO 2013 Fig. 7 */
3794 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3795 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3796 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3797 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
3798 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
3799 "0 <= i < n and 0 <= j < n - 1 }",
3800 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
3801 "0 <= i < n and 0 <= j < j' < n;"
3802 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
3803 "0 <= i < i' < n and 0 <= j,j' < n;"
3804 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
3805 "0 <= i,j,j' < n and j < j' }",
3808 /* TACO 2013 Fig. 7, without tags */
3809 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3810 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3811 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3812 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3813 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3814 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
3815 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
3816 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
3819 /* TACO 2013 Fig. 12 */
3820 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
3821 "S3[i,3] : 0 <= i <= 1 }",
3822 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
3823 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
3824 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
3825 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
3826 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3827 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
3828 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3829 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
3830 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
3831 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
3832 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
3837 /* Test schedule construction based on conditional constraints.
3838 * In particular, check the number of members in the outer band node
3839 * as an indication of whether tiling is possible or not.
3841 static int test_conditional_schedule_constraints(isl_ctx *ctx)
3843 int i;
3844 isl_union_set *domain;
3845 isl_union_map *condition;
3846 isl_union_map *flow;
3847 isl_union_map *validity;
3848 isl_schedule_constraints *sc;
3849 isl_schedule *schedule;
3850 isl_schedule_node *node;
3851 int n_member;
3853 if (test_special_conditional_schedule_constraints(ctx) < 0)
3854 return -1;
3855 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
3856 return -1;
3858 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
3859 domain = isl_union_set_read_from_str(ctx,
3860 live_range_tests[i].domain);
3861 flow = isl_union_map_read_from_str(ctx,
3862 live_range_tests[i].flow);
3863 condition = isl_union_map_read_from_str(ctx,
3864 live_range_tests[i].condition);
3865 validity = isl_union_map_read_from_str(ctx,
3866 live_range_tests[i].conditional_validity);
3867 sc = isl_schedule_constraints_on_domain(domain);
3868 sc = isl_schedule_constraints_set_validity(sc,
3869 isl_union_map_copy(flow));
3870 sc = isl_schedule_constraints_set_proximity(sc, flow);
3871 sc = isl_schedule_constraints_set_conditional_validity(sc,
3872 condition, validity);
3873 schedule = isl_schedule_constraints_compute_schedule(sc);
3874 node = isl_schedule_get_root(schedule);
3875 while (node &&
3876 isl_schedule_node_get_type(node) != isl_schedule_node_band)
3877 node = isl_schedule_node_first_child(node);
3878 n_member = isl_schedule_node_band_n_member(node);
3879 isl_schedule_node_free(node);
3880 isl_schedule_free(schedule);
3882 if (!schedule)
3883 return -1;
3884 if (n_member != live_range_tests[i].outer_band_n)
3885 isl_die(ctx, isl_error_unknown,
3886 "unexpected number of members in outer band",
3887 return -1);
3889 return 0;
3892 /* Check that the schedule computed for the given instance set and
3893 * dependence relation strongly satisfies the dependences.
3894 * In particular, check that no instance is scheduled before
3895 * or together with an instance on which it depends.
3896 * Earlier versions of isl would produce a schedule that
3897 * only weakly satisfies the dependences.
3899 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
3901 const char *domain, *dep;
3902 isl_union_map *D, *schedule;
3903 isl_map *map, *ge;
3904 int empty;
3906 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
3907 "A[i0] : 0 <= i0 <= 1 }";
3908 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
3909 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
3910 schedule = compute_schedule(ctx, domain, dep, dep);
3911 D = isl_union_map_read_from_str(ctx, dep);
3912 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
3913 D = isl_union_map_apply_range(D, schedule);
3914 map = isl_map_from_union_map(D);
3915 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
3916 map = isl_map_intersect(map, ge);
3917 empty = isl_map_is_empty(map);
3918 isl_map_free(map);
3920 if (empty < 0)
3921 return -1;
3922 if (!empty)
3923 isl_die(ctx, isl_error_unknown,
3924 "dependences not strongly satisfied", return -1);
3926 return 0;
3929 /* Compute a schedule for input where the instance set constraints
3930 * conflict with the context constraints.
3931 * Earlier versions of isl did not properly handle this situation.
3933 static int test_conflicting_context_schedule(isl_ctx *ctx)
3935 isl_union_map *schedule;
3936 const char *domain, *context;
3938 domain = "[n] -> { A[] : n >= 0 }";
3939 context = "[n] -> { : n < 0 }";
3940 schedule = compute_schedule_with_context(ctx,
3941 domain, "{}", "{}", context);
3942 isl_union_map_free(schedule);
3944 if (!schedule)
3945 return -1;
3947 return 0;
3950 /* Check that the dependence carrying step is not confused by
3951 * a bound on the coefficient size.
3952 * In particular, force the scheduler to move to a dependence carrying
3953 * step by demanding outer coincidence and bound the size of
3954 * the coefficients. Earlier versions of isl would take this
3955 * bound into account while carrying dependences, breaking
3956 * fundamental assumptions.
3957 * On the other hand, the dependence carrying step now tries
3958 * to prevent loop coalescing by default, so check that indeed
3959 * no loop coalescing occurs by comparing the computed schedule
3960 * to the expected non-coalescing schedule.
3962 static int test_bounded_coefficients_schedule(isl_ctx *ctx)
3964 const char *domain, *dep;
3965 isl_union_set *I;
3966 isl_union_map *D;
3967 isl_schedule_constraints *sc;
3968 isl_schedule *schedule;
3969 isl_union_map *sched1, *sched2;
3970 isl_bool equal;
3972 domain = "{ C[i0, i1] : 2 <= i0 <= 3999 and 0 <= i1 <= -1 + i0 }";
3973 dep = "{ C[i0, i1] -> C[i0, 1 + i1] : i0 <= 3999 and i1 >= 0 and "
3974 "i1 <= -2 + i0; "
3975 "C[i0, -1 + i0] -> C[1 + i0, 0] : i0 <= 3998 and i0 >= 1 }";
3976 I = isl_union_set_read_from_str(ctx, domain);
3977 D = isl_union_map_read_from_str(ctx, dep);
3978 sc = isl_schedule_constraints_on_domain(I);
3979 sc = isl_schedule_constraints_set_validity(sc, isl_union_map_copy(D));
3980 sc = isl_schedule_constraints_set_coincidence(sc, D);
3981 isl_options_set_schedule_outer_coincidence(ctx, 1);
3982 isl_options_set_schedule_max_coefficient(ctx, 20);
3983 schedule = isl_schedule_constraints_compute_schedule(sc);
3984 isl_options_set_schedule_max_coefficient(ctx, -1);
3985 isl_options_set_schedule_outer_coincidence(ctx, 0);
3986 sched1 = isl_schedule_get_map(schedule);
3987 isl_schedule_free(schedule);
3989 sched2 = isl_union_map_read_from_str(ctx, "{ C[x,y] -> [x,y] }");
3990 equal = isl_union_map_is_equal(sched1, sched2);
3991 isl_union_map_free(sched1);
3992 isl_union_map_free(sched2);
3994 if (equal < 0)
3995 return -1;
3996 if (!equal)
3997 isl_die(ctx, isl_error_unknown,
3998 "unexpected schedule", return -1);
4000 return 0;
4003 /* Check that the bounds on the coefficients are respected.
4004 * This function checks for a particular output schedule,
4005 * but the exact output is not important, only that it does
4006 * not contain any coefficients greater than 4.
4007 * It is, however, easier to check for a particular output.
4008 * This test is only run for the whole component scheduler
4009 * because the incremental scheduler produces a slightly different schedule.
4011 static int test_bounded_coefficients_schedule_whole(isl_ctx *ctx)
4013 const char *domain, *dep, *str;
4014 isl_union_set *I;
4015 isl_union_map *D;
4016 isl_schedule_constraints *sc;
4017 isl_schedule *schedule;
4018 isl_union_map *sched1, *sched2;
4019 isl_bool equal;
4021 domain = "{ S_4[i, j, k] : 0 <= i < j <= 10 and 0 <= k <= 100; "
4022 "S_2[i, j] : 0 <= i < j <= 10; S_6[i, j] : 0 <= i < j <= 10 }";
4023 dep = "{ S_2[0, j] -> S_4[0, j, 0] : 0 < j <= 10; "
4024 "S_4[0, j, 100] -> S_6[0, j] : 0 < j <= 10 }";
4025 I = isl_union_set_read_from_str(ctx, domain);
4026 D = isl_union_map_read_from_str(ctx, dep);
4027 sc = isl_schedule_constraints_on_domain(I);
4028 sc = isl_schedule_constraints_set_validity(sc, D);
4029 isl_options_set_schedule_max_constant_term(ctx, 10);
4030 isl_options_set_schedule_max_coefficient(ctx, 4);
4031 schedule = isl_schedule_constraints_compute_schedule(sc);
4032 isl_options_set_schedule_max_coefficient(ctx, -1);
4033 isl_options_set_schedule_max_constant_term(ctx, -1);
4034 sched1 = isl_schedule_get_map(schedule);
4035 isl_schedule_free(schedule);
4037 str = "{ S_4[i, j, k] -> [i, j, 10 - k]; "
4038 "S_2[i, j] -> [0, i, j]; S_6[i, j] -> [0, 10 + i, j] }";
4039 sched2 = isl_union_map_read_from_str(ctx, str);
4040 equal = isl_union_map_is_equal(sched1, sched2);
4041 isl_union_map_free(sched1);
4042 isl_union_map_free(sched2);
4044 if (equal < 0)
4045 return -1;
4046 if (!equal)
4047 isl_die(ctx, isl_error_unknown,
4048 "unexpected schedule", return -1);
4050 return 0;
4053 /* Check that a set of schedule constraints that only allow for
4054 * a coalescing schedule still produces a schedule even if the user
4055 * request a non-coalescing schedule. Earlier versions of isl
4056 * would not handle this case correctly.
4058 static int test_coalescing_schedule(isl_ctx *ctx)
4060 const char *domain, *dep;
4061 isl_union_set *I;
4062 isl_union_map *D;
4063 isl_schedule_constraints *sc;
4064 isl_schedule *schedule;
4065 int treat_coalescing;
4067 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
4068 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
4069 I = isl_union_set_read_from_str(ctx, domain);
4070 D = isl_union_map_read_from_str(ctx, dep);
4071 sc = isl_schedule_constraints_on_domain(I);
4072 sc = isl_schedule_constraints_set_validity(sc, D);
4073 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4074 isl_options_set_schedule_treat_coalescing(ctx, 1);
4075 schedule = isl_schedule_constraints_compute_schedule(sc);
4076 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4077 isl_schedule_free(schedule);
4078 if (!schedule)
4079 return -1;
4080 return 0;
4083 /* Check that the scheduler does not perform any needless
4084 * compound skewing. Earlier versions of isl would compute
4085 * schedules in terms of transformed schedule coefficients and
4086 * would not accurately keep track of the sum of the original
4087 * schedule coefficients. It could then produce the schedule
4088 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
4089 * for the input below instead of the schedule below.
4091 static int test_skewing_schedule(isl_ctx *ctx)
4093 const char *D, *V, *P, *S;
4095 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
4096 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
4097 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
4098 "-1 <= a-i + b-j + c-k <= 1 }";
4099 P = "{ }";
4100 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
4102 return test_special_schedule(ctx, D, V, P, S);
4105 int test_schedule(isl_ctx *ctx)
4107 const char *D, *W, *R, *V, *P, *S;
4108 int max_coincidence;
4109 int treat_coalescing;
4111 /* Handle resulting schedule with zero bands. */
4112 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
4113 return -1;
4115 /* Jacobi */
4116 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
4117 W = "{ S1[t,i] -> a[t,i] }";
4118 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
4119 "S1[t,i] -> a[t-1,i+1] }";
4120 S = "{ S1[t,i] -> [t,i] }";
4121 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4122 return -1;
4124 /* Fig. 5 of CC2008 */
4125 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
4126 "j <= -1 + N }";
4127 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
4128 "j >= 2 and j <= -1 + N }";
4129 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
4130 "j >= 2 and j <= -1 + N; "
4131 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
4132 "j >= 2 and j <= -1 + N }";
4133 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4134 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4135 return -1;
4137 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
4138 W = "{ S1[i] -> a[i] }";
4139 R = "{ S2[i] -> a[i+1] }";
4140 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4141 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4142 return -1;
4144 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
4145 W = "{ S1[i] -> a[i] }";
4146 R = "{ S2[i] -> a[9-i] }";
4147 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4148 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4149 return -1;
4151 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
4152 W = "{ S1[i] -> a[i] }";
4153 R = "[N] -> { S2[i] -> a[N-1-i] }";
4154 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4155 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4156 return -1;
4158 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
4159 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
4160 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
4161 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
4162 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4163 return -1;
4165 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4166 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
4167 R = "{ S2[i,j] -> a[i-1,j] }";
4168 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4169 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4170 return -1;
4172 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4173 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
4174 R = "{ S2[i,j] -> a[i,j-1] }";
4175 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4176 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4177 return -1;
4179 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
4180 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
4181 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
4182 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
4183 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
4184 "S_0[] -> [0, 0, 0] }";
4185 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
4186 return -1;
4187 ctx->opt->schedule_parametric = 0;
4188 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4189 return -1;
4190 ctx->opt->schedule_parametric = 1;
4192 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
4193 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
4194 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
4195 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
4196 "S4[i] -> a[i,N] }";
4197 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
4198 "S4[i] -> [4,i,0] }";
4199 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
4200 isl_options_set_schedule_maximize_coincidence(ctx, 0);
4201 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4202 return -1;
4203 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
4205 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
4206 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4207 "j <= N }";
4208 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4209 "j <= N; "
4210 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
4211 "j <= N }";
4212 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4213 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4214 return -1;
4216 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
4217 " S_2[t] : t >= 0 and t <= -1 + N; "
4218 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
4219 "i <= -1 + N }";
4220 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
4221 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
4222 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
4223 "i >= 0 and i <= -1 + N }";
4224 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
4225 "i >= 0 and i <= -1 + N; "
4226 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
4227 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
4228 " S_0[t] -> [0, t, 0] }";
4230 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4231 return -1;
4232 ctx->opt->schedule_parametric = 0;
4233 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4234 return -1;
4235 ctx->opt->schedule_parametric = 1;
4237 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
4238 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
4239 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
4240 return -1;
4242 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
4243 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
4244 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
4245 "j >= 0 and j <= -1 + N; "
4246 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4247 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
4248 "j >= 0 and j <= -1 + N; "
4249 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4250 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
4251 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4252 return -1;
4254 D = "{ S_0[i] : i >= 0 }";
4255 W = "{ S_0[i] -> a[i] : i >= 0 }";
4256 R = "{ S_0[i] -> a[0] : i >= 0 }";
4257 S = "{ S_0[i] -> [0, i, 0] }";
4258 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4259 return -1;
4261 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
4262 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
4263 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
4264 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
4265 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4266 return -1;
4268 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
4269 "k <= -1 + n and k >= 0 }";
4270 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
4271 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
4272 "k <= -1 + n and k >= 0; "
4273 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
4274 "k <= -1 + n and k >= 0; "
4275 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
4276 "k <= -1 + n and k >= 0 }";
4277 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
4278 ctx->opt->schedule_outer_coincidence = 1;
4279 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4280 return -1;
4281 ctx->opt->schedule_outer_coincidence = 0;
4283 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
4284 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
4285 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
4286 "Stmt_for_body24[i0, i1, 1, 0]:"
4287 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
4288 "Stmt_for_body7[i0, i1, i2]:"
4289 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
4290 "i2 <= 7 }";
4292 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
4293 "Stmt_for_body24[1, i1, i2, i3]:"
4294 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
4295 "i2 >= 1;"
4296 "Stmt_for_body24[0, i1, i2, i3] -> "
4297 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
4298 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
4299 "i3 >= 0;"
4300 "Stmt_for_body24[0, i1, i2, i3] ->"
4301 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4302 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4303 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4304 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4305 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4306 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4307 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4308 "i1 <= 6 and i1 >= 0;"
4309 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4310 "Stmt_for_body7[i0, i1, i2] -> "
4311 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4312 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4313 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4314 "Stmt_for_body7[i0, i1, i2] -> "
4315 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4316 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4317 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4318 P = V;
4320 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4321 isl_options_set_schedule_treat_coalescing(ctx, 0);
4322 if (test_has_schedule(ctx, D, V, P) < 0)
4323 return -1;
4324 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4326 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4327 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4328 "j >= 1 and j <= 7;"
4329 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4330 "j >= 1 and j <= 8 }";
4331 P = "{ }";
4332 S = "{ S_0[i, j] -> [i + j, i] }";
4333 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4334 if (test_special_schedule(ctx, D, V, P, S) < 0)
4335 return -1;
4336 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4338 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4339 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4340 "j >= 0 and j <= -1 + i }";
4341 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4342 "i <= -1 + N and j >= 0;"
4343 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4344 "i <= -2 + N }";
4345 P = "{ }";
4346 S = "{ S_0[i, j] -> [i, j] }";
4347 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4348 if (test_special_schedule(ctx, D, V, P, S) < 0)
4349 return -1;
4350 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4352 /* Test both algorithms on a case with only proximity dependences. */
4353 D = "{ S[i,j] : 0 <= i <= 10 }";
4354 V = "{ }";
4355 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4356 S = "{ S[i, j] -> [j, i] }";
4357 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4358 if (test_special_schedule(ctx, D, V, P, S) < 0)
4359 return -1;
4360 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4361 if (test_special_schedule(ctx, D, V, P, S) < 0)
4362 return -1;
4364 D = "{ A[a]; B[] }";
4365 V = "{}";
4366 P = "{ A[a] -> B[] }";
4367 if (test_has_schedule(ctx, D, V, P) < 0)
4368 return -1;
4370 if (test_padded_schedule(ctx) < 0)
4371 return -1;
4373 /* Check that check for progress is not confused by rational
4374 * solution.
4376 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4377 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4378 "i0 <= -2 + N; "
4379 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4380 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4381 P = "{}";
4382 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4383 if (test_has_schedule(ctx, D, V, P) < 0)
4384 return -1;
4385 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4387 /* Check that we allow schedule rows that are only non-trivial
4388 * on some full-dimensional domains.
4390 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4391 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4392 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4393 P = "{}";
4394 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4395 if (test_has_schedule(ctx, D, V, P) < 0)
4396 return -1;
4397 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4399 if (test_conditional_schedule_constraints(ctx) < 0)
4400 return -1;
4402 if (test_strongly_satisfying_schedule(ctx) < 0)
4403 return -1;
4405 if (test_conflicting_context_schedule(ctx) < 0)
4406 return -1;
4408 if (test_bounded_coefficients_schedule(ctx) < 0)
4409 return -1;
4410 if (test_coalescing_schedule(ctx) < 0)
4411 return -1;
4412 if (test_skewing_schedule(ctx) < 0)
4413 return -1;
4415 return 0;
4418 /* Perform scheduling tests using the whole component scheduler.
4420 static int test_schedule_whole(isl_ctx *ctx)
4422 int whole;
4423 int r;
4425 whole = isl_options_get_schedule_whole_component(ctx);
4426 isl_options_set_schedule_whole_component(ctx, 1);
4427 r = test_schedule(ctx);
4428 if (r >= 0)
4429 r = test_bounded_coefficients_schedule_whole(ctx);
4430 isl_options_set_schedule_whole_component(ctx, whole);
4432 return r;
4435 /* Perform scheduling tests using the incremental scheduler.
4437 static int test_schedule_incremental(isl_ctx *ctx)
4439 int whole;
4440 int r;
4442 whole = isl_options_get_schedule_whole_component(ctx);
4443 isl_options_set_schedule_whole_component(ctx, 0);
4444 r = test_schedule(ctx);
4445 isl_options_set_schedule_whole_component(ctx, whole);
4447 return r;
4450 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4452 isl_union_map *umap;
4453 int test;
4455 umap = isl_union_map_read_from_str(ctx, str);
4456 test = isl_union_map_plain_is_injective(umap);
4457 isl_union_map_free(umap);
4458 if (test < 0)
4459 return -1;
4460 if (test == injective)
4461 return 0;
4462 if (injective)
4463 isl_die(ctx, isl_error_unknown,
4464 "map not detected as injective", return -1);
4465 else
4466 isl_die(ctx, isl_error_unknown,
4467 "map detected as injective", return -1);
4470 int test_injective(isl_ctx *ctx)
4472 const char *str;
4474 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4475 return -1;
4476 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4477 return -1;
4478 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4479 return -1;
4480 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4481 return -1;
4482 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4483 return -1;
4484 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4485 return -1;
4486 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4487 return -1;
4488 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4489 return -1;
4491 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4492 if (test_plain_injective(ctx, str, 1))
4493 return -1;
4494 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4495 if (test_plain_injective(ctx, str, 0))
4496 return -1;
4498 return 0;
4501 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4503 isl_aff *aff2;
4504 int equal;
4506 if (!aff)
4507 return -1;
4509 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4510 equal = isl_aff_plain_is_equal(aff, aff2);
4511 isl_aff_free(aff2);
4513 return equal;
4516 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4518 int equal;
4520 equal = aff_plain_is_equal(aff, str);
4521 if (equal < 0)
4522 return -1;
4523 if (!equal)
4524 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4525 "result not as expected", return -1);
4526 return 0;
4529 /* Is "upa" obviously equal to the isl_union_pw_aff represented by "str"?
4531 static isl_bool union_pw_aff_plain_is_equal(__isl_keep isl_union_pw_aff *upa,
4532 const char *str)
4534 isl_ctx *ctx;
4535 isl_union_pw_aff *upa2;
4536 isl_bool equal;
4538 if (!upa)
4539 return isl_bool_error;
4541 ctx = isl_union_pw_aff_get_ctx(upa);
4542 upa2 = isl_union_pw_aff_read_from_str(ctx, str);
4543 equal = isl_union_pw_aff_plain_is_equal(upa, upa2);
4544 isl_union_pw_aff_free(upa2);
4546 return equal;
4549 /* Check that "upa" is obviously equal to the isl_union_pw_aff
4550 * represented by "str".
4552 static isl_stat union_pw_aff_check_plain_equal(__isl_keep isl_union_pw_aff *upa,
4553 const char *str)
4555 isl_bool equal;
4557 equal = union_pw_aff_plain_is_equal(upa, str);
4558 if (equal < 0)
4559 return isl_stat_error;
4560 if (!equal)
4561 isl_die(isl_union_pw_aff_get_ctx(upa), isl_error_unknown,
4562 "result not as expected", return isl_stat_error);
4563 return isl_stat_ok;
4566 /* Basic tests on isl_union_pw_aff.
4568 * In particular, check that isl_union_pw_aff_aff_on_domain
4569 * aligns the parameters of the input objects and
4570 * that isl_union_pw_aff_param_on_domain_id properly
4571 * introduces the parameter.
4573 static int test_upa(isl_ctx *ctx)
4575 const char *str;
4576 isl_id *id;
4577 isl_aff *aff;
4578 isl_union_set *domain;
4579 isl_union_pw_aff *upa;
4580 isl_stat ok;
4582 aff = isl_aff_read_from_str(ctx, "[N] -> { [N] }");
4583 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
4584 domain = isl_union_set_read_from_str(ctx, str);
4585 upa = isl_union_pw_aff_aff_on_domain(domain, aff);
4586 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
4587 ok = union_pw_aff_check_plain_equal(upa, str);
4588 isl_union_pw_aff_free(upa);
4589 if (ok < 0)
4590 return -1;
4592 id = isl_id_alloc(ctx, "N", NULL);
4593 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
4594 domain = isl_union_set_read_from_str(ctx, str);
4595 upa = isl_union_pw_aff_param_on_domain_id(domain, id);
4596 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
4597 ok = union_pw_aff_check_plain_equal(upa, str);
4598 isl_union_pw_aff_free(upa);
4599 if (ok < 0)
4600 return -1;
4602 return 0;
4605 struct {
4606 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4607 __isl_take isl_aff *aff2);
4608 } aff_bin_op[] = {
4609 ['+'] = { &isl_aff_add },
4610 ['-'] = { &isl_aff_sub },
4611 ['*'] = { &isl_aff_mul },
4612 ['/'] = { &isl_aff_div },
4615 struct {
4616 const char *arg1;
4617 unsigned char op;
4618 const char *arg2;
4619 const char *res;
4620 } aff_bin_tests[] = {
4621 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
4622 "{ [i] -> [2i] }" },
4623 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
4624 "{ [i] -> [0] }" },
4625 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
4626 "{ [i] -> [2i] }" },
4627 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
4628 "{ [i] -> [2i] }" },
4629 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
4630 "{ [i] -> [i/2] }" },
4631 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
4632 "{ [i] -> [i] }" },
4633 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
4634 "{ [i] -> [NaN] }" },
4635 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
4636 "{ [i] -> [NaN] }" },
4637 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
4638 "{ [i] -> [NaN] }" },
4639 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
4640 "{ [i] -> [NaN] }" },
4641 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
4642 "{ [i] -> [NaN] }" },
4643 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
4644 "{ [i] -> [NaN] }" },
4645 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
4646 "{ [i] -> [NaN] }" },
4647 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
4648 "{ [i] -> [NaN] }" },
4649 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
4650 "{ [i] -> [NaN] }" },
4651 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
4652 "{ [i] -> [NaN] }" },
4653 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
4654 "{ [i] -> [NaN] }" },
4655 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
4656 "{ [i] -> [NaN] }" },
4659 /* Perform some basic tests of binary operations on isl_aff objects.
4661 static int test_bin_aff(isl_ctx *ctx)
4663 int i;
4664 isl_aff *aff1, *aff2, *res;
4665 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
4666 __isl_take isl_aff *aff2);
4667 int ok;
4669 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
4670 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
4671 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
4672 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
4673 fn = aff_bin_op[aff_bin_tests[i].op].fn;
4674 aff1 = fn(aff1, aff2);
4675 if (isl_aff_is_nan(res))
4676 ok = isl_aff_is_nan(aff1);
4677 else
4678 ok = isl_aff_plain_is_equal(aff1, res);
4679 isl_aff_free(aff1);
4680 isl_aff_free(res);
4681 if (ok < 0)
4682 return -1;
4683 if (!ok)
4684 isl_die(ctx, isl_error_unknown,
4685 "unexpected result", return -1);
4688 return 0;
4691 struct {
4692 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
4693 __isl_take isl_pw_aff *pa2);
4694 } pw_aff_bin_op[] = {
4695 ['m'] = { &isl_pw_aff_min },
4696 ['M'] = { &isl_pw_aff_max },
4699 /* Inputs for binary isl_pw_aff operation tests.
4700 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
4701 * defined by pw_aff_bin_op, and "res" is the expected result.
4703 struct {
4704 const char *arg1;
4705 unsigned char op;
4706 const char *arg2;
4707 const char *res;
4708 } pw_aff_bin_tests[] = {
4709 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
4710 "{ [i] -> [i] }" },
4711 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
4712 "{ [i] -> [i] }" },
4713 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
4714 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
4715 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
4716 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
4717 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
4718 "{ [i] -> [NaN] }" },
4719 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
4720 "{ [i] -> [NaN] }" },
4723 /* Perform some basic tests of binary operations on isl_pw_aff objects.
4725 static int test_bin_pw_aff(isl_ctx *ctx)
4727 int i;
4728 isl_bool ok;
4729 isl_pw_aff *pa1, *pa2, *res;
4731 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
4732 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
4733 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
4734 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
4735 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
4736 if (isl_pw_aff_involves_nan(res))
4737 ok = isl_pw_aff_involves_nan(pa1);
4738 else
4739 ok = isl_pw_aff_plain_is_equal(pa1, res);
4740 isl_pw_aff_free(pa1);
4741 isl_pw_aff_free(res);
4742 if (ok < 0)
4743 return -1;
4744 if (!ok)
4745 isl_die(ctx, isl_error_unknown,
4746 "unexpected result", return -1);
4749 return 0;
4752 struct {
4753 __isl_give isl_union_pw_multi_aff *(*fn)(
4754 __isl_take isl_union_pw_multi_aff *upma1,
4755 __isl_take isl_union_pw_multi_aff *upma2);
4756 const char *arg1;
4757 const char *arg2;
4758 const char *res;
4759 } upma_bin_tests[] = {
4760 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
4761 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
4762 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
4763 "{ B[x] -> [2] : x >= 0 }",
4764 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
4765 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
4766 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
4767 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
4768 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
4769 "D[i] -> B[2] : i >= 5 }" },
4770 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4771 "{ B[x] -> C[2] : x > 0 }",
4772 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
4773 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4774 "{ B[x] -> A[2] : x >= 0 }",
4775 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
4778 /* Perform some basic tests of binary operations on
4779 * isl_union_pw_multi_aff objects.
4781 static int test_bin_upma(isl_ctx *ctx)
4783 int i;
4784 isl_union_pw_multi_aff *upma1, *upma2, *res;
4785 int ok;
4787 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
4788 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4789 upma_bin_tests[i].arg1);
4790 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4791 upma_bin_tests[i].arg2);
4792 res = isl_union_pw_multi_aff_read_from_str(ctx,
4793 upma_bin_tests[i].res);
4794 upma1 = upma_bin_tests[i].fn(upma1, upma2);
4795 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
4796 isl_union_pw_multi_aff_free(upma1);
4797 isl_union_pw_multi_aff_free(res);
4798 if (ok < 0)
4799 return -1;
4800 if (!ok)
4801 isl_die(ctx, isl_error_unknown,
4802 "unexpected result", return -1);
4805 return 0;
4808 struct {
4809 __isl_give isl_union_pw_multi_aff *(*fn)(
4810 __isl_take isl_union_pw_multi_aff *upma1,
4811 __isl_take isl_union_pw_multi_aff *upma2);
4812 const char *arg1;
4813 const char *arg2;
4814 } upma_bin_fail_tests[] = {
4815 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
4816 "{ B[x] -> C[2] : x >= 0 }" },
4819 /* Perform some basic tests of binary operations on
4820 * isl_union_pw_multi_aff objects that are expected to fail.
4822 static int test_bin_upma_fail(isl_ctx *ctx)
4824 int i, n;
4825 isl_union_pw_multi_aff *upma1, *upma2;
4826 int on_error;
4828 on_error = isl_options_get_on_error(ctx);
4829 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
4830 n = ARRAY_SIZE(upma_bin_fail_tests);
4831 for (i = 0; i < n; ++i) {
4832 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
4833 upma_bin_fail_tests[i].arg1);
4834 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
4835 upma_bin_fail_tests[i].arg2);
4836 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
4837 isl_union_pw_multi_aff_free(upma1);
4838 if (upma1)
4839 break;
4841 isl_options_set_on_error(ctx, on_error);
4842 if (i < n)
4843 isl_die(ctx, isl_error_unknown,
4844 "operation not expected to succeed", return -1);
4846 return 0;
4849 /* Inputs for basic tests of binary operations on
4850 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
4851 * "fn" is the function that is tested.
4852 * "arg1" and "arg2" are string descriptions of the inputs.
4853 * "res" is a string description of the expected result.
4855 struct {
4856 __isl_give isl_multi_union_pw_aff *(*fn)(
4857 __isl_take isl_multi_union_pw_aff *mupa,
4858 __isl_take isl_union_set *uset);
4859 const char *arg1;
4860 const char *arg2;
4861 const char *res;
4862 } mupa_uset_tests[] = {
4863 { &isl_multi_union_pw_aff_intersect_domain,
4864 "C[{ B[i,j] -> [i + 2j] }]", "{ B[i,i] }",
4865 "C[{ B[i,i] -> [3i] }]" },
4868 /* Perform some basic tests of binary operations on
4869 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
4871 static int test_mupa_uset(isl_ctx *ctx)
4873 int i;
4874 isl_bool ok;
4875 isl_multi_union_pw_aff *mupa, *res;
4876 isl_union_set *uset;
4878 for (i = 0; i < ARRAY_SIZE(mupa_uset_tests); ++i) {
4879 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
4880 mupa_uset_tests[i].arg1);
4881 uset = isl_union_set_read_from_str(ctx,
4882 mupa_uset_tests[i].arg2);
4883 res = isl_multi_union_pw_aff_read_from_str(ctx,
4884 mupa_uset_tests[i].res);
4885 mupa = mupa_uset_tests[i].fn(mupa, uset);
4886 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
4887 isl_multi_union_pw_aff_free(mupa);
4888 isl_multi_union_pw_aff_free(res);
4889 if (ok < 0)
4890 return -1;
4891 if (!ok)
4892 isl_die(ctx, isl_error_unknown,
4893 "unexpected result", return -1);
4896 return 0;
4899 /* Check that the input tuple of an isl_aff can be set properly.
4901 static isl_stat test_aff_set_tuple_id(isl_ctx *ctx)
4903 isl_id *id;
4904 isl_aff *aff;
4905 int equal;
4907 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x + 1] }");
4908 id = isl_id_alloc(ctx, "A", NULL);
4909 aff = isl_aff_set_tuple_id(aff, isl_dim_in, id);
4910 equal = aff_check_plain_equal(aff, "{ A[x] -> [x + 1] }");
4911 isl_aff_free(aff);
4912 if (equal < 0)
4913 return isl_stat_error;
4915 return isl_stat_ok;
4918 int test_aff(isl_ctx *ctx)
4920 const char *str;
4921 isl_set *set;
4922 isl_space *space;
4923 isl_local_space *ls;
4924 isl_aff *aff;
4925 int zero, equal;
4927 if (test_upa(ctx) < 0)
4928 return -1;
4929 if (test_bin_aff(ctx) < 0)
4930 return -1;
4931 if (test_bin_pw_aff(ctx) < 0)
4932 return -1;
4933 if (test_bin_upma(ctx) < 0)
4934 return -1;
4935 if (test_bin_upma_fail(ctx) < 0)
4936 return -1;
4937 if (test_mupa_uset(ctx) < 0)
4938 return -1;
4940 space = isl_space_set_alloc(ctx, 0, 1);
4941 ls = isl_local_space_from_space(space);
4942 aff = isl_aff_zero_on_domain(ls);
4944 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4945 aff = isl_aff_scale_down_ui(aff, 3);
4946 aff = isl_aff_floor(aff);
4947 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4948 aff = isl_aff_scale_down_ui(aff, 2);
4949 aff = isl_aff_floor(aff);
4950 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
4952 str = "{ [10] }";
4953 set = isl_set_read_from_str(ctx, str);
4954 aff = isl_aff_gist(aff, set);
4956 aff = isl_aff_add_constant_si(aff, -16);
4957 zero = isl_aff_plain_is_zero(aff);
4958 isl_aff_free(aff);
4960 if (zero < 0)
4961 return -1;
4962 if (!zero)
4963 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4965 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
4966 aff = isl_aff_scale_down_ui(aff, 64);
4967 aff = isl_aff_floor(aff);
4968 equal = aff_check_plain_equal(aff, "{ [-1] }");
4969 isl_aff_free(aff);
4970 if (equal < 0)
4971 return -1;
4973 if (test_aff_set_tuple_id(ctx) < 0)
4974 return -1;
4976 return 0;
4979 /* Check that "pa" consists of a single expression.
4981 static int check_single_piece(isl_ctx *ctx, __isl_take isl_pw_aff *pa)
4983 int n;
4985 n = isl_pw_aff_n_piece(pa);
4986 isl_pw_aff_free(pa);
4988 if (!pa)
4989 return -1;
4990 if (n != 1)
4991 isl_die(ctx, isl_error_unknown, "expecting single expression",
4992 return -1);
4994 return 0;
4997 /* Check that the computation below results in a single expression.
4998 * One or two expressions may result depending on which constraint
4999 * ends up being considered as redundant with respect to the other
5000 * constraints after the projection that is performed internally
5001 * by isl_set_dim_min.
5003 static int test_dim_max_1(isl_ctx *ctx)
5005 const char *str;
5006 isl_set *set;
5007 isl_pw_aff *pa;
5009 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
5010 "-4a <= b <= 3 and b < n - 4a }";
5011 set = isl_set_read_from_str(ctx, str);
5012 pa = isl_set_dim_min(set, 0);
5013 return check_single_piece(ctx, pa);
5016 /* Check that the computation below results in a single expression.
5017 * The PIP problem corresponding to these constraints has a row
5018 * that causes a split of the solution domain. The solver should
5019 * first pick rows that split off empty parts such that the actual
5020 * solution domain does not get split.
5021 * Note that the description contains some redundant constraints.
5022 * If these constraints get removed first, then the row mentioned
5023 * above does not appear in the PIP problem.
5025 static int test_dim_max_2(isl_ctx *ctx)
5027 const char *str;
5028 isl_set *set;
5029 isl_pw_aff *pa;
5031 str = "[P, N] -> { [a] : a < N and a >= 0 and N > P and a <= P and "
5032 "N > 0 and P >= 0 }";
5033 set = isl_set_read_from_str(ctx, str);
5034 pa = isl_set_dim_max(set, 0);
5035 return check_single_piece(ctx, pa);
5038 int test_dim_max(isl_ctx *ctx)
5040 int equal;
5041 const char *str;
5042 isl_set *set1, *set2;
5043 isl_set *set;
5044 isl_map *map;
5045 isl_pw_aff *pwaff;
5047 if (test_dim_max_1(ctx) < 0)
5048 return -1;
5049 if (test_dim_max_2(ctx) < 0)
5050 return -1;
5052 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
5053 set = isl_set_read_from_str(ctx, str);
5054 pwaff = isl_set_dim_max(set, 0);
5055 set1 = isl_set_from_pw_aff(pwaff);
5056 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
5057 set2 = isl_set_read_from_str(ctx, str);
5058 equal = isl_set_is_equal(set1, set2);
5059 isl_set_free(set1);
5060 isl_set_free(set2);
5061 if (equal < 0)
5062 return -1;
5063 if (!equal)
5064 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5066 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
5067 set = isl_set_read_from_str(ctx, str);
5068 pwaff = isl_set_dim_max(set, 0);
5069 set1 = isl_set_from_pw_aff(pwaff);
5070 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
5071 set2 = isl_set_read_from_str(ctx, str);
5072 equal = isl_set_is_equal(set1, set2);
5073 isl_set_free(set1);
5074 isl_set_free(set2);
5075 if (equal < 0)
5076 return -1;
5077 if (!equal)
5078 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5080 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
5081 set = isl_set_read_from_str(ctx, str);
5082 pwaff = isl_set_dim_max(set, 0);
5083 set1 = isl_set_from_pw_aff(pwaff);
5084 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
5085 set2 = isl_set_read_from_str(ctx, str);
5086 equal = isl_set_is_equal(set1, set2);
5087 isl_set_free(set1);
5088 isl_set_free(set2);
5089 if (equal < 0)
5090 return -1;
5091 if (!equal)
5092 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5094 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
5095 "0 <= i < N and 0 <= j < M }";
5096 map = isl_map_read_from_str(ctx, str);
5097 set = isl_map_range(map);
5099 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
5100 set1 = isl_set_from_pw_aff(pwaff);
5101 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
5102 set2 = isl_set_read_from_str(ctx, str);
5103 equal = isl_set_is_equal(set1, set2);
5104 isl_set_free(set1);
5105 isl_set_free(set2);
5107 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
5108 set1 = isl_set_from_pw_aff(pwaff);
5109 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
5110 set2 = isl_set_read_from_str(ctx, str);
5111 if (equal >= 0 && equal)
5112 equal = isl_set_is_equal(set1, set2);
5113 isl_set_free(set1);
5114 isl_set_free(set2);
5116 isl_set_free(set);
5118 if (equal < 0)
5119 return -1;
5120 if (!equal)
5121 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5123 /* Check that solutions are properly merged. */
5124 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
5125 "c <= -1 + n - 4a - 2b and c >= -2b and "
5126 "4a >= -4 + n and c >= 0 }";
5127 set = isl_set_read_from_str(ctx, str);
5128 pwaff = isl_set_dim_min(set, 2);
5129 set1 = isl_set_from_pw_aff(pwaff);
5130 str = "[n] -> { [(0)] : n >= 1 }";
5131 set2 = isl_set_read_from_str(ctx, str);
5132 equal = isl_set_is_equal(set1, set2);
5133 isl_set_free(set1);
5134 isl_set_free(set2);
5136 if (equal < 0)
5137 return -1;
5138 if (!equal)
5139 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5141 /* Check that empty solution lie in the right space. */
5142 str = "[n] -> { [t,a] : 1 = 0 }";
5143 set = isl_set_read_from_str(ctx, str);
5144 pwaff = isl_set_dim_max(set, 0);
5145 set1 = isl_set_from_pw_aff(pwaff);
5146 str = "[n] -> { [t] : 1 = 0 }";
5147 set2 = isl_set_read_from_str(ctx, str);
5148 equal = isl_set_is_equal(set1, set2);
5149 isl_set_free(set1);
5150 isl_set_free(set2);
5152 if (equal < 0)
5153 return -1;
5154 if (!equal)
5155 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5157 return 0;
5160 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
5162 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
5163 const char *str)
5165 isl_ctx *ctx;
5166 isl_pw_multi_aff *pma2;
5167 int equal;
5169 if (!pma)
5170 return -1;
5172 ctx = isl_pw_multi_aff_get_ctx(pma);
5173 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5174 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
5175 isl_pw_multi_aff_free(pma2);
5177 return equal;
5180 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
5181 * represented by "str".
5183 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
5184 const char *str)
5186 int equal;
5188 equal = pw_multi_aff_plain_is_equal(pma, str);
5189 if (equal < 0)
5190 return -1;
5191 if (!equal)
5192 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
5193 "result not as expected", return -1);
5194 return 0;
5197 /* Basic test for isl_pw_multi_aff_product.
5199 * Check that multiple pieces are properly handled.
5201 static int test_product_pma(isl_ctx *ctx)
5203 int equal;
5204 const char *str;
5205 isl_pw_multi_aff *pma1, *pma2;
5207 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
5208 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
5209 str = "{ C[] -> D[] }";
5210 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5211 pma1 = isl_pw_multi_aff_product(pma1, pma2);
5212 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
5213 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
5214 equal = pw_multi_aff_check_plain_equal(pma1, str);
5215 isl_pw_multi_aff_free(pma1);
5216 if (equal < 0)
5217 return -1;
5219 return 0;
5222 int test_product(isl_ctx *ctx)
5224 const char *str;
5225 isl_set *set;
5226 isl_union_set *uset1, *uset2;
5227 int ok;
5229 str = "{ A[i] }";
5230 set = isl_set_read_from_str(ctx, str);
5231 set = isl_set_product(set, isl_set_copy(set));
5232 ok = isl_set_is_wrapping(set);
5233 isl_set_free(set);
5234 if (ok < 0)
5235 return -1;
5236 if (!ok)
5237 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5239 str = "{ [] }";
5240 uset1 = isl_union_set_read_from_str(ctx, str);
5241 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
5242 str = "{ [[] -> []] }";
5243 uset2 = isl_union_set_read_from_str(ctx, str);
5244 ok = isl_union_set_is_equal(uset1, uset2);
5245 isl_union_set_free(uset1);
5246 isl_union_set_free(uset2);
5247 if (ok < 0)
5248 return -1;
5249 if (!ok)
5250 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5252 if (test_product_pma(ctx) < 0)
5253 return -1;
5255 return 0;
5258 /* Check that two sets are not considered disjoint just because
5259 * they have a different set of (named) parameters.
5261 static int test_disjoint(isl_ctx *ctx)
5263 const char *str;
5264 isl_set *set, *set2;
5265 int disjoint;
5267 str = "[n] -> { [[]->[]] }";
5268 set = isl_set_read_from_str(ctx, str);
5269 str = "{ [[]->[]] }";
5270 set2 = isl_set_read_from_str(ctx, str);
5271 disjoint = isl_set_is_disjoint(set, set2);
5272 isl_set_free(set);
5273 isl_set_free(set2);
5274 if (disjoint < 0)
5275 return -1;
5276 if (disjoint)
5277 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5279 return 0;
5282 /* Inputs for isl_pw_multi_aff_is_equal tests.
5283 * "f1" and "f2" are the two function that need to be compared.
5284 * "equal" is the expected result.
5286 struct {
5287 int equal;
5288 const char *f1;
5289 const char *f2;
5290 } pma_equal_tests[] = {
5291 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
5292 "[N] -> { [0] : 0 <= N <= 1 }" },
5293 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
5294 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
5295 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
5296 "[N] -> { [0] : 0 <= N <= 1 }" },
5297 { 0, "{ [NaN] }", "{ [NaN] }" },
5300 int test_equal(isl_ctx *ctx)
5302 int i;
5303 const char *str;
5304 isl_set *set, *set2;
5305 int equal;
5307 str = "{ S_6[i] }";
5308 set = isl_set_read_from_str(ctx, str);
5309 str = "{ S_7[i] }";
5310 set2 = isl_set_read_from_str(ctx, str);
5311 equal = isl_set_is_equal(set, set2);
5312 isl_set_free(set);
5313 isl_set_free(set2);
5314 if (equal < 0)
5315 return -1;
5316 if (equal)
5317 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5319 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
5320 int expected = pma_equal_tests[i].equal;
5321 isl_pw_multi_aff *f1, *f2;
5323 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
5324 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
5325 equal = isl_pw_multi_aff_is_equal(f1, f2);
5326 isl_pw_multi_aff_free(f1);
5327 isl_pw_multi_aff_free(f2);
5328 if (equal < 0)
5329 return -1;
5330 if (equal != expected)
5331 isl_die(ctx, isl_error_unknown,
5332 "unexpected equality result", return -1);
5335 return 0;
5338 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
5339 enum isl_dim_type type, unsigned pos, int fixed)
5341 isl_bool test;
5343 test = isl_map_plain_is_fixed(map, type, pos, NULL);
5344 isl_map_free(map);
5345 if (test < 0)
5346 return -1;
5347 if (test == fixed)
5348 return 0;
5349 if (fixed)
5350 isl_die(ctx, isl_error_unknown,
5351 "map not detected as fixed", return -1);
5352 else
5353 isl_die(ctx, isl_error_unknown,
5354 "map detected as fixed", return -1);
5357 int test_fixed(isl_ctx *ctx)
5359 const char *str;
5360 isl_map *map;
5362 str = "{ [i] -> [i] }";
5363 map = isl_map_read_from_str(ctx, str);
5364 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
5365 return -1;
5366 str = "{ [i] -> [1] }";
5367 map = isl_map_read_from_str(ctx, str);
5368 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5369 return -1;
5370 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
5371 map = isl_map_read_from_str(ctx, str);
5372 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5373 return -1;
5374 map = isl_map_read_from_str(ctx, str);
5375 map = isl_map_neg(map);
5376 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
5377 return -1;
5379 return 0;
5382 struct isl_vertices_test_data {
5383 const char *set;
5384 int n;
5385 const char *vertex[6];
5386 } vertices_tests[] = {
5387 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
5388 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
5389 { "{ A[t, i] : t = 14 and i = 1 }",
5390 1, { "{ A[14, 1] }" } },
5391 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
5392 "c <= m and m <= n and m > 0 }",
5393 6, {
5394 "[n, m] -> { [n, m, m] : 0 < m <= n }",
5395 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
5396 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
5397 "[n, m] -> { [m, m, m] : 0 < m <= n }",
5398 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
5399 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
5400 } },
5403 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
5405 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
5407 struct isl_vertices_test_data *data = user;
5408 isl_ctx *ctx;
5409 isl_multi_aff *ma;
5410 isl_basic_set *bset;
5411 isl_pw_multi_aff *pma;
5412 int i;
5413 isl_bool equal;
5415 ctx = isl_vertex_get_ctx(vertex);
5416 bset = isl_vertex_get_domain(vertex);
5417 ma = isl_vertex_get_expr(vertex);
5418 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
5420 for (i = 0; i < data->n; ++i) {
5421 isl_pw_multi_aff *pma_i;
5423 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
5424 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
5425 isl_pw_multi_aff_free(pma_i);
5427 if (equal < 0 || equal)
5428 break;
5431 isl_pw_multi_aff_free(pma);
5432 isl_vertex_free(vertex);
5434 if (equal < 0)
5435 return isl_stat_error;
5437 return equal ? isl_stat_ok : isl_stat_error;
5440 int test_vertices(isl_ctx *ctx)
5442 int i;
5444 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
5445 isl_basic_set *bset;
5446 isl_vertices *vertices;
5447 int ok = 1;
5448 int n;
5450 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
5451 vertices = isl_basic_set_compute_vertices(bset);
5452 n = isl_vertices_get_n_vertices(vertices);
5453 if (vertices_tests[i].n != n)
5454 ok = 0;
5455 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
5456 &vertices_tests[i]) < 0)
5457 ok = 0;
5458 isl_vertices_free(vertices);
5459 isl_basic_set_free(bset);
5461 if (!vertices)
5462 return -1;
5463 if (!ok)
5464 isl_die(ctx, isl_error_unknown, "unexpected vertices",
5465 return -1);
5468 return 0;
5471 int test_union_pw(isl_ctx *ctx)
5473 int equal;
5474 const char *str;
5475 isl_union_set *uset;
5476 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
5478 str = "{ [x] -> x^2 }";
5479 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
5480 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
5481 uset = isl_union_pw_qpolynomial_domain(upwqp1);
5482 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
5483 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
5484 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
5485 isl_union_pw_qpolynomial_free(upwqp1);
5486 isl_union_pw_qpolynomial_free(upwqp2);
5487 if (equal < 0)
5488 return -1;
5489 if (!equal)
5490 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5492 return 0;
5495 /* Test that isl_union_pw_qpolynomial_eval picks up the function
5496 * defined over the correct domain space.
5498 static int test_eval_1(isl_ctx *ctx)
5500 const char *str;
5501 isl_point *pnt;
5502 isl_set *set;
5503 isl_union_pw_qpolynomial *upwqp;
5504 isl_val *v;
5505 int cmp;
5507 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
5508 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
5509 str = "{ A[6] }";
5510 set = isl_set_read_from_str(ctx, str);
5511 pnt = isl_set_sample_point(set);
5512 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
5513 cmp = isl_val_cmp_si(v, 36);
5514 isl_val_free(v);
5516 if (!v)
5517 return -1;
5518 if (cmp != 0)
5519 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
5521 return 0;
5524 /* Check that isl_qpolynomial_eval handles getting called on a void point.
5526 static int test_eval_2(isl_ctx *ctx)
5528 const char *str;
5529 isl_point *pnt;
5530 isl_set *set;
5531 isl_qpolynomial *qp;
5532 isl_val *v;
5533 isl_bool ok;
5535 str = "{ A[x] -> [x] }";
5536 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
5537 str = "{ A[x] : false }";
5538 set = isl_set_read_from_str(ctx, str);
5539 pnt = isl_set_sample_point(set);
5540 v = isl_qpolynomial_eval(qp, pnt);
5541 ok = isl_val_is_nan(v);
5542 isl_val_free(v);
5544 if (ok < 0)
5545 return -1;
5546 if (!ok)
5547 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
5549 return 0;
5552 /* Perform basic polynomial evaluation tests.
5554 static int test_eval(isl_ctx *ctx)
5556 if (test_eval_1(ctx) < 0)
5557 return -1;
5558 if (test_eval_2(ctx) < 0)
5559 return -1;
5560 return 0;
5563 /* Descriptions of sets that are tested for reparsing after printing.
5565 const char *output_tests[] = {
5566 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
5567 "{ [x] : 1 = 0 }",
5568 "{ [x] : false }",
5569 "{ [x] : x mod 2 = 0 }",
5570 "{ [x] : x mod 2 = 1 }",
5571 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) < x }",
5572 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) < x }",
5573 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
5574 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
5575 "[n] -> { [y, x] : 2*((x + 2y) mod 3) = n }",
5576 "{ [x, y] : (2*floor(x/3) + 3*floor(y/4)) mod 5 = x }",
5579 /* Check that printing a set and reparsing a set from the printed output
5580 * results in the same set.
5582 static int test_output_set(isl_ctx *ctx)
5584 int i;
5585 char *str;
5586 isl_set *set1, *set2;
5587 isl_bool equal;
5589 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
5590 set1 = isl_set_read_from_str(ctx, output_tests[i]);
5591 str = isl_set_to_str(set1);
5592 set2 = isl_set_read_from_str(ctx, str);
5593 free(str);
5594 equal = isl_set_is_equal(set1, set2);
5595 isl_set_free(set1);
5596 isl_set_free(set2);
5597 if (equal < 0)
5598 return -1;
5599 if (!equal)
5600 isl_die(ctx, isl_error_unknown,
5601 "parsed output not the same", return -1);
5604 return 0;
5607 /* Check that an isl_multi_aff is printed using a consistent space.
5609 static isl_stat test_output_ma(isl_ctx *ctx)
5611 char *str;
5612 isl_bool equal;
5613 isl_aff *aff;
5614 isl_multi_aff *ma, *ma2;
5616 ma = isl_multi_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
5617 aff = isl_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
5618 ma = isl_multi_aff_set_aff(ma, 0, aff);
5619 str = isl_multi_aff_to_str(ma);
5620 ma2 = isl_multi_aff_read_from_str(ctx, str);
5621 free(str);
5622 equal = isl_multi_aff_plain_is_equal(ma, ma2);
5623 isl_multi_aff_free(ma2);
5624 isl_multi_aff_free(ma);
5626 if (equal < 0)
5627 return isl_stat_error;
5628 if (!equal)
5629 isl_die(ctx, isl_error_unknown, "bad conversion",
5630 return isl_stat_error);
5632 return isl_stat_ok;
5635 /* Check that an isl_multi_pw_aff is printed using a consistent space.
5637 static isl_stat test_output_mpa(isl_ctx *ctx)
5639 char *str;
5640 isl_bool equal;
5641 isl_pw_aff *pa;
5642 isl_multi_pw_aff *mpa, *mpa2;
5644 mpa = isl_multi_pw_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
5645 pa = isl_pw_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
5646 mpa = isl_multi_pw_aff_set_pw_aff(mpa, 0, pa);
5647 str = isl_multi_pw_aff_to_str(mpa);
5648 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
5649 free(str);
5650 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
5651 isl_multi_pw_aff_free(mpa2);
5652 isl_multi_pw_aff_free(mpa);
5654 if (equal < 0)
5655 return isl_stat_error;
5656 if (!equal)
5657 isl_die(ctx, isl_error_unknown, "bad conversion",
5658 return isl_stat_error);
5660 return isl_stat_ok;
5663 int test_output(isl_ctx *ctx)
5665 char *s;
5666 const char *str;
5667 isl_pw_aff *pa;
5668 isl_printer *p;
5669 int equal;
5671 if (test_output_set(ctx) < 0)
5672 return -1;
5673 if (test_output_ma(ctx) < 0)
5674 return -1;
5675 if (test_output_mpa(ctx) < 0)
5676 return -1;
5678 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
5679 pa = isl_pw_aff_read_from_str(ctx, str);
5681 p = isl_printer_to_str(ctx);
5682 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
5683 p = isl_printer_print_pw_aff(p, pa);
5684 s = isl_printer_get_str(p);
5685 isl_printer_free(p);
5686 isl_pw_aff_free(pa);
5687 if (!s)
5688 equal = -1;
5689 else
5690 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
5691 free(s);
5692 if (equal < 0)
5693 return -1;
5694 if (!equal)
5695 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
5697 return 0;
5700 int test_sample(isl_ctx *ctx)
5702 const char *str;
5703 isl_basic_set *bset1, *bset2;
5704 int empty, subset;
5706 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
5707 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
5708 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
5709 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
5710 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
5711 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
5712 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
5713 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
5714 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
5715 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
5716 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
5717 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
5718 "d - 1073741821e and "
5719 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
5720 "3j >= 1 - a + b + 2e and "
5721 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
5722 "3i <= 4 - a + 4b - e and "
5723 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
5724 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
5725 "c <= -1 + a and 3i >= -2 - a + 3e and "
5726 "1073741822e <= 1073741823 - a + 1073741822b + c and "
5727 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
5728 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
5729 "1073741823e >= 1 + 1073741823b - d and "
5730 "3i >= 1073741823b + c - 1073741823e - f and "
5731 "3i >= 1 + 2b + e + 3g }";
5732 bset1 = isl_basic_set_read_from_str(ctx, str);
5733 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
5734 empty = isl_basic_set_is_empty(bset2);
5735 subset = isl_basic_set_is_subset(bset2, bset1);
5736 isl_basic_set_free(bset1);
5737 isl_basic_set_free(bset2);
5738 if (empty < 0 || subset < 0)
5739 return -1;
5740 if (empty)
5741 isl_die(ctx, isl_error_unknown, "point not found", return -1);
5742 if (!subset)
5743 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
5745 return 0;
5748 int test_fixed_power(isl_ctx *ctx)
5750 const char *str;
5751 isl_map *map;
5752 isl_val *exp;
5753 int equal;
5755 str = "{ [i] -> [i + 1] }";
5756 map = isl_map_read_from_str(ctx, str);
5757 exp = isl_val_int_from_si(ctx, 23);
5758 map = isl_map_fixed_power_val(map, exp);
5759 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
5760 isl_map_free(map);
5761 if (equal < 0)
5762 return -1;
5764 return 0;
5767 int test_slice(isl_ctx *ctx)
5769 const char *str;
5770 isl_map *map;
5771 int equal;
5773 str = "{ [i] -> [j] }";
5774 map = isl_map_read_from_str(ctx, str);
5775 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
5776 equal = map_check_equal(map, "{ [i] -> [i] }");
5777 isl_map_free(map);
5778 if (equal < 0)
5779 return -1;
5781 str = "{ [i] -> [j] }";
5782 map = isl_map_read_from_str(ctx, str);
5783 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
5784 equal = map_check_equal(map, "{ [i] -> [j] }");
5785 isl_map_free(map);
5786 if (equal < 0)
5787 return -1;
5789 str = "{ [i] -> [j] }";
5790 map = isl_map_read_from_str(ctx, str);
5791 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
5792 equal = map_check_equal(map, "{ [i] -> [-i] }");
5793 isl_map_free(map);
5794 if (equal < 0)
5795 return -1;
5797 str = "{ [i] -> [j] }";
5798 map = isl_map_read_from_str(ctx, str);
5799 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
5800 equal = map_check_equal(map, "{ [0] -> [j] }");
5801 isl_map_free(map);
5802 if (equal < 0)
5803 return -1;
5805 str = "{ [i] -> [j] }";
5806 map = isl_map_read_from_str(ctx, str);
5807 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
5808 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
5809 isl_map_free(map);
5810 if (equal < 0)
5811 return -1;
5813 str = "{ [i] -> [j] }";
5814 map = isl_map_read_from_str(ctx, str);
5815 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
5816 equal = map_check_equal(map, "{ [i] -> [j] : false }");
5817 isl_map_free(map);
5818 if (equal < 0)
5819 return -1;
5821 return 0;
5824 int test_eliminate(isl_ctx *ctx)
5826 const char *str;
5827 isl_map *map;
5828 int equal;
5830 str = "{ [i] -> [j] : i = 2j }";
5831 map = isl_map_read_from_str(ctx, str);
5832 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
5833 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
5834 isl_map_free(map);
5835 if (equal < 0)
5836 return -1;
5838 return 0;
5841 /* Check that isl_set_dim_residue_class detects that the values of j
5842 * in the set below are all odd and that it does not detect any spurious
5843 * strides.
5845 static int test_residue_class(isl_ctx *ctx)
5847 const char *str;
5848 isl_set *set;
5849 isl_int m, r;
5850 isl_stat res;
5852 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
5853 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
5854 set = isl_set_read_from_str(ctx, str);
5855 isl_int_init(m);
5856 isl_int_init(r);
5857 res = isl_set_dim_residue_class(set, 1, &m, &r);
5858 if (res >= 0 &&
5859 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
5860 isl_die(ctx, isl_error_unknown, "incorrect residue class",
5861 res = isl_stat_error);
5862 isl_int_clear(r);
5863 isl_int_clear(m);
5864 isl_set_free(set);
5866 return res;
5869 static int test_align_parameters_1(isl_ctx *ctx)
5871 const char *str;
5872 isl_space *space;
5873 isl_multi_aff *ma1, *ma2;
5874 int equal;
5876 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
5877 ma1 = isl_multi_aff_read_from_str(ctx, str);
5879 space = isl_space_params_alloc(ctx, 1);
5880 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
5881 ma1 = isl_multi_aff_align_params(ma1, space);
5883 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
5884 ma2 = isl_multi_aff_read_from_str(ctx, str);
5886 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
5888 isl_multi_aff_free(ma1);
5889 isl_multi_aff_free(ma2);
5891 if (equal < 0)
5892 return -1;
5893 if (!equal)
5894 isl_die(ctx, isl_error_unknown,
5895 "result not as expected", return -1);
5897 return 0;
5900 /* Check the isl_multi_*_from_*_list operation in case inputs
5901 * have unaligned parameters.
5902 * In particular, older versions of isl would simply fail
5903 * (without printing any error message).
5905 static isl_stat test_align_parameters_2(isl_ctx *ctx)
5907 isl_space *space;
5908 isl_map *map;
5909 isl_aff *aff;
5910 isl_multi_aff *ma;
5912 map = isl_map_read_from_str(ctx, "{ A[] -> M[x] }");
5913 space = isl_map_get_space(map);
5914 isl_map_free(map);
5916 aff = isl_aff_read_from_str(ctx, "[N] -> { A[] -> [N] }");
5917 ma = isl_multi_aff_from_aff_list(space, isl_aff_list_from_aff(aff));
5918 isl_multi_aff_free(ma);
5920 if (!ma)
5921 return isl_stat_error;
5922 return isl_stat_ok;
5925 /* Perform basic parameter alignment tests.
5927 static int test_align_parameters(isl_ctx *ctx)
5929 if (test_align_parameters_1(ctx) < 0)
5930 return -1;
5931 if (test_align_parameters_2(ctx) < 0)
5932 return -1;
5934 return 0;
5937 static int test_list(isl_ctx *ctx)
5939 isl_id *a, *b, *c, *d, *id;
5940 isl_id_list *list;
5941 int ok;
5943 a = isl_id_alloc(ctx, "a", NULL);
5944 b = isl_id_alloc(ctx, "b", NULL);
5945 c = isl_id_alloc(ctx, "c", NULL);
5946 d = isl_id_alloc(ctx, "d", NULL);
5948 list = isl_id_list_alloc(ctx, 4);
5949 list = isl_id_list_add(list, b);
5950 list = isl_id_list_insert(list, 0, a);
5951 list = isl_id_list_add(list, c);
5952 list = isl_id_list_add(list, d);
5953 list = isl_id_list_drop(list, 1, 1);
5955 if (!list)
5956 return -1;
5957 if (isl_id_list_n_id(list) != 3) {
5958 isl_id_list_free(list);
5959 isl_die(ctx, isl_error_unknown,
5960 "unexpected number of elements in list", return -1);
5963 id = isl_id_list_get_id(list, 0);
5964 ok = id == a;
5965 isl_id_free(id);
5966 id = isl_id_list_get_id(list, 1);
5967 ok = ok && id == c;
5968 isl_id_free(id);
5969 id = isl_id_list_get_id(list, 2);
5970 ok = ok && id == d;
5971 isl_id_free(id);
5973 isl_id_list_free(list);
5975 if (!ok)
5976 isl_die(ctx, isl_error_unknown,
5977 "unexpected elements in list", return -1);
5979 return 0;
5982 const char *set_conversion_tests[] = {
5983 "[N] -> { [i] : N - 1 <= 2 i <= N }",
5984 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
5985 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5986 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
5987 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
5988 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
5989 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
5990 "-3 + c <= d <= 28 + c) }",
5993 /* Check that converting from isl_set to isl_pw_multi_aff and back
5994 * to isl_set produces the original isl_set.
5996 static int test_set_conversion(isl_ctx *ctx)
5998 int i;
5999 const char *str;
6000 isl_set *set1, *set2;
6001 isl_pw_multi_aff *pma;
6002 int equal;
6004 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
6005 str = set_conversion_tests[i];
6006 set1 = isl_set_read_from_str(ctx, str);
6007 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
6008 set2 = isl_set_from_pw_multi_aff(pma);
6009 equal = isl_set_is_equal(set1, set2);
6010 isl_set_free(set1);
6011 isl_set_free(set2);
6013 if (equal < 0)
6014 return -1;
6015 if (!equal)
6016 isl_die(ctx, isl_error_unknown, "bad conversion",
6017 return -1);
6020 return 0;
6023 const char *conversion_tests[] = {
6024 "{ [a, b, c, d] -> s0[a, b, e, f] : "
6025 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
6026 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
6027 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
6028 "9e <= -2 - 2a) }",
6029 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
6030 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
6031 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
6034 /* Check that converting from isl_map to isl_pw_multi_aff and back
6035 * to isl_map produces the original isl_map.
6037 static int test_map_conversion(isl_ctx *ctx)
6039 int i;
6040 isl_map *map1, *map2;
6041 isl_pw_multi_aff *pma;
6042 int equal;
6044 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
6045 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
6046 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
6047 map2 = isl_map_from_pw_multi_aff(pma);
6048 equal = isl_map_is_equal(map1, map2);
6049 isl_map_free(map1);
6050 isl_map_free(map2);
6052 if (equal < 0)
6053 return -1;
6054 if (!equal)
6055 isl_die(ctx, isl_error_unknown, "bad conversion",
6056 return -1);
6059 return 0;
6062 static int test_conversion(isl_ctx *ctx)
6064 if (test_set_conversion(ctx) < 0)
6065 return -1;
6066 if (test_map_conversion(ctx) < 0)
6067 return -1;
6068 return 0;
6071 /* Check that isl_basic_map_curry does not modify input.
6073 static int test_curry(isl_ctx *ctx)
6075 const char *str;
6076 isl_basic_map *bmap1, *bmap2;
6077 int equal;
6079 str = "{ [A[] -> B[]] -> C[] }";
6080 bmap1 = isl_basic_map_read_from_str(ctx, str);
6081 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
6082 equal = isl_basic_map_is_equal(bmap1, bmap2);
6083 isl_basic_map_free(bmap1);
6084 isl_basic_map_free(bmap2);
6086 if (equal < 0)
6087 return -1;
6088 if (equal)
6089 isl_die(ctx, isl_error_unknown,
6090 "curried map should not be equal to original",
6091 return -1);
6093 return 0;
6096 struct {
6097 const char *set;
6098 const char *ma;
6099 const char *res;
6100 } preimage_tests[] = {
6101 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
6102 "{ A[j,i] -> B[i,j] }",
6103 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
6104 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
6105 "{ A[a,b] -> B[a/2,b/6] }",
6106 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
6107 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
6108 "{ A[a,b] -> B[a/2,b/6] }",
6109 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
6110 "exists i,j : a = 2 i and b = 6 j }" },
6111 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
6112 "[n] -> { : 0 <= n <= 100 }" },
6113 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
6114 "{ A[a] -> B[2a] }",
6115 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
6116 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
6117 "{ A[a] -> B[([a/2])] }",
6118 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
6119 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
6120 "{ A[a] -> B[a,a,a/3] }",
6121 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
6122 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
6123 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
6126 static int test_preimage_basic_set(isl_ctx *ctx)
6128 int i;
6129 isl_basic_set *bset1, *bset2;
6130 isl_multi_aff *ma;
6131 int equal;
6133 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
6134 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
6135 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
6136 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
6137 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
6138 equal = isl_basic_set_is_equal(bset1, bset2);
6139 isl_basic_set_free(bset1);
6140 isl_basic_set_free(bset2);
6141 if (equal < 0)
6142 return -1;
6143 if (!equal)
6144 isl_die(ctx, isl_error_unknown, "bad preimage",
6145 return -1);
6148 return 0;
6151 struct {
6152 const char *map;
6153 const char *ma;
6154 const char *res;
6155 } preimage_domain_tests[] = {
6156 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
6157 "{ A[j,i] -> B[i,j] }",
6158 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
6159 { "{ B[i] -> C[i]; D[i] -> E[i] }",
6160 "{ A[i] -> B[i + 1] }",
6161 "{ A[i] -> C[i + 1] }" },
6162 { "{ B[i] -> C[i]; B[i] -> E[i] }",
6163 "{ A[i] -> B[i + 1] }",
6164 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
6165 { "{ B[i] -> C[([i/2])] }",
6166 "{ A[i] -> B[2i] }",
6167 "{ A[i] -> C[i] }" },
6168 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
6169 "{ A[i] -> B[([i/5]), ([i/7])] }",
6170 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
6171 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
6172 "[N] -> { A[] -> B[([N/5])] }",
6173 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
6174 { "{ B[i] -> C[i] : exists a : i = 5 a }",
6175 "{ A[i] -> B[2i] }",
6176 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
6177 { "{ B[i] -> C[i] : exists a : i = 2 a; "
6178 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
6179 "{ A[i] -> B[2i] }",
6180 "{ A[i] -> C[2i] }" },
6181 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
6182 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
6185 static int test_preimage_union_map(isl_ctx *ctx)
6187 int i;
6188 isl_union_map *umap1, *umap2;
6189 isl_multi_aff *ma;
6190 int equal;
6192 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
6193 umap1 = isl_union_map_read_from_str(ctx,
6194 preimage_domain_tests[i].map);
6195 ma = isl_multi_aff_read_from_str(ctx,
6196 preimage_domain_tests[i].ma);
6197 umap2 = isl_union_map_read_from_str(ctx,
6198 preimage_domain_tests[i].res);
6199 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
6200 equal = isl_union_map_is_equal(umap1, umap2);
6201 isl_union_map_free(umap1);
6202 isl_union_map_free(umap2);
6203 if (equal < 0)
6204 return -1;
6205 if (!equal)
6206 isl_die(ctx, isl_error_unknown, "bad preimage",
6207 return -1);
6210 return 0;
6213 static int test_preimage(isl_ctx *ctx)
6215 if (test_preimage_basic_set(ctx) < 0)
6216 return -1;
6217 if (test_preimage_union_map(ctx) < 0)
6218 return -1;
6220 return 0;
6223 struct {
6224 const char *ma1;
6225 const char *ma;
6226 const char *res;
6227 } pullback_tests[] = {
6228 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
6229 "{ A[a,b] -> C[b + 2a] }" },
6230 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
6231 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
6232 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
6233 "{ A[a] -> C[(a)/6] }" },
6234 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
6235 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
6236 "{ A[a] -> C[(2a)/3] }" },
6237 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
6238 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
6239 "{ A[i,j] -> C[i + j, i + j] }"},
6240 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
6241 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
6242 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
6243 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
6244 "{ [i, j] -> [floor((i)/3), j] }",
6245 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
6248 static int test_pullback(isl_ctx *ctx)
6250 int i;
6251 isl_multi_aff *ma1, *ma2;
6252 isl_multi_aff *ma;
6253 int equal;
6255 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
6256 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
6257 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
6258 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
6259 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
6260 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
6261 isl_multi_aff_free(ma1);
6262 isl_multi_aff_free(ma2);
6263 if (equal < 0)
6264 return -1;
6265 if (!equal)
6266 isl_die(ctx, isl_error_unknown, "bad pullback",
6267 return -1);
6270 return 0;
6273 /* Check that negation is printed correctly and that equal expressions
6274 * are correctly identified.
6276 static int test_ast(isl_ctx *ctx)
6278 isl_ast_expr *expr, *expr1, *expr2, *expr3;
6279 char *str;
6280 int ok, equal;
6282 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
6283 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
6284 expr = isl_ast_expr_add(expr1, expr2);
6285 expr2 = isl_ast_expr_copy(expr);
6286 expr = isl_ast_expr_neg(expr);
6287 expr2 = isl_ast_expr_neg(expr2);
6288 equal = isl_ast_expr_is_equal(expr, expr2);
6289 str = isl_ast_expr_to_C_str(expr);
6290 ok = str ? !strcmp(str, "-(A + B)") : -1;
6291 free(str);
6292 isl_ast_expr_free(expr);
6293 isl_ast_expr_free(expr2);
6295 if (ok < 0 || equal < 0)
6296 return -1;
6297 if (!equal)
6298 isl_die(ctx, isl_error_unknown,
6299 "equal expressions not considered equal", return -1);
6300 if (!ok)
6301 isl_die(ctx, isl_error_unknown,
6302 "isl_ast_expr printed incorrectly", return -1);
6304 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
6305 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
6306 expr = isl_ast_expr_add(expr1, expr2);
6307 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
6308 expr = isl_ast_expr_sub(expr3, expr);
6309 str = isl_ast_expr_to_C_str(expr);
6310 ok = str ? !strcmp(str, "C - (A + B)") : -1;
6311 free(str);
6312 isl_ast_expr_free(expr);
6314 if (ok < 0)
6315 return -1;
6316 if (!ok)
6317 isl_die(ctx, isl_error_unknown,
6318 "isl_ast_expr printed incorrectly", return -1);
6320 return 0;
6323 /* Check that isl_ast_build_expr_from_set returns a valid expression
6324 * for an empty set. Note that isl_ast_build_expr_from_set getting
6325 * called on an empty set probably indicates a bug in the caller.
6327 static int test_ast_build(isl_ctx *ctx)
6329 isl_set *set;
6330 isl_ast_build *build;
6331 isl_ast_expr *expr;
6333 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6334 build = isl_ast_build_from_context(set);
6336 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
6337 expr = isl_ast_build_expr_from_set(build, set);
6339 isl_ast_expr_free(expr);
6340 isl_ast_build_free(build);
6342 if (!expr)
6343 return -1;
6345 return 0;
6348 /* Internal data structure for before_for and after_for callbacks.
6350 * depth is the current depth
6351 * before is the number of times before_for has been called
6352 * after is the number of times after_for has been called
6354 struct isl_test_codegen_data {
6355 int depth;
6356 int before;
6357 int after;
6360 /* This function is called before each for loop in the AST generated
6361 * from test_ast_gen1.
6363 * Increment the number of calls and the depth.
6364 * Check that the space returned by isl_ast_build_get_schedule_space
6365 * matches the target space of the schedule returned by
6366 * isl_ast_build_get_schedule.
6367 * Return an isl_id that is checked by the corresponding call
6368 * to after_for.
6370 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
6371 void *user)
6373 struct isl_test_codegen_data *data = user;
6374 isl_ctx *ctx;
6375 isl_space *space;
6376 isl_union_map *schedule;
6377 isl_union_set *uset;
6378 isl_set *set;
6379 int empty;
6380 char name[] = "d0";
6382 ctx = isl_ast_build_get_ctx(build);
6384 if (data->before >= 3)
6385 isl_die(ctx, isl_error_unknown,
6386 "unexpected number of for nodes", return NULL);
6387 if (data->depth >= 2)
6388 isl_die(ctx, isl_error_unknown,
6389 "unexpected depth", return NULL);
6391 snprintf(name, sizeof(name), "d%d", data->depth);
6392 data->before++;
6393 data->depth++;
6395 schedule = isl_ast_build_get_schedule(build);
6396 uset = isl_union_map_range(schedule);
6397 if (!uset)
6398 return NULL;
6399 if (isl_union_set_n_set(uset) != 1) {
6400 isl_union_set_free(uset);
6401 isl_die(ctx, isl_error_unknown,
6402 "expecting single range space", return NULL);
6405 space = isl_ast_build_get_schedule_space(build);
6406 set = isl_union_set_extract_set(uset, space);
6407 isl_union_set_free(uset);
6408 empty = isl_set_is_empty(set);
6409 isl_set_free(set);
6411 if (empty < 0)
6412 return NULL;
6413 if (empty)
6414 isl_die(ctx, isl_error_unknown,
6415 "spaces don't match", return NULL);
6417 return isl_id_alloc(ctx, name, NULL);
6420 /* This function is called after each for loop in the AST generated
6421 * from test_ast_gen1.
6423 * Increment the number of calls and decrement the depth.
6424 * Check that the annotation attached to the node matches
6425 * the isl_id returned by the corresponding call to before_for.
6427 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
6428 __isl_keep isl_ast_build *build, void *user)
6430 struct isl_test_codegen_data *data = user;
6431 isl_id *id;
6432 const char *name;
6433 int valid;
6435 data->after++;
6436 data->depth--;
6438 if (data->after > data->before)
6439 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
6440 "mismatch in number of for nodes",
6441 return isl_ast_node_free(node));
6443 id = isl_ast_node_get_annotation(node);
6444 if (!id)
6445 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
6446 "missing annotation", return isl_ast_node_free(node));
6448 name = isl_id_get_name(id);
6449 valid = name && atoi(name + 1) == data->depth;
6450 isl_id_free(id);
6452 if (!valid)
6453 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
6454 "wrong annotation", return isl_ast_node_free(node));
6456 return node;
6459 /* Check that the before_each_for and after_each_for callbacks
6460 * are called for each for loop in the generated code,
6461 * that they are called in the right order and that the isl_id
6462 * returned from the before_each_for callback is attached to
6463 * the isl_ast_node passed to the corresponding after_each_for call.
6465 static int test_ast_gen1(isl_ctx *ctx)
6467 const char *str;
6468 isl_set *set;
6469 isl_union_map *schedule;
6470 isl_ast_build *build;
6471 isl_ast_node *tree;
6472 struct isl_test_codegen_data data;
6474 str = "[N] -> { : N >= 10 }";
6475 set = isl_set_read_from_str(ctx, str);
6476 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
6477 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
6478 schedule = isl_union_map_read_from_str(ctx, str);
6480 data.before = 0;
6481 data.after = 0;
6482 data.depth = 0;
6483 build = isl_ast_build_from_context(set);
6484 build = isl_ast_build_set_before_each_for(build,
6485 &before_for, &data);
6486 build = isl_ast_build_set_after_each_for(build,
6487 &after_for, &data);
6488 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6489 isl_ast_build_free(build);
6490 if (!tree)
6491 return -1;
6493 isl_ast_node_free(tree);
6495 if (data.before != 3 || data.after != 3)
6496 isl_die(ctx, isl_error_unknown,
6497 "unexpected number of for nodes", return -1);
6499 return 0;
6502 /* Check that the AST generator handles domains that are integrally disjoint
6503 * but not rationally disjoint.
6505 static int test_ast_gen2(isl_ctx *ctx)
6507 const char *str;
6508 isl_set *set;
6509 isl_union_map *schedule;
6510 isl_union_map *options;
6511 isl_ast_build *build;
6512 isl_ast_node *tree;
6514 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
6515 schedule = isl_union_map_read_from_str(ctx, str);
6516 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6517 build = isl_ast_build_from_context(set);
6519 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
6520 options = isl_union_map_read_from_str(ctx, str);
6521 build = isl_ast_build_set_options(build, options);
6522 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6523 isl_ast_build_free(build);
6524 if (!tree)
6525 return -1;
6526 isl_ast_node_free(tree);
6528 return 0;
6531 /* Increment *user on each call.
6533 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
6534 __isl_keep isl_ast_build *build, void *user)
6536 int *n = user;
6538 (*n)++;
6540 return node;
6543 /* Test that unrolling tries to minimize the number of instances.
6544 * In particular, for the schedule given below, make sure it generates
6545 * 3 nodes (rather than 101).
6547 static int test_ast_gen3(isl_ctx *ctx)
6549 const char *str;
6550 isl_set *set;
6551 isl_union_map *schedule;
6552 isl_union_map *options;
6553 isl_ast_build *build;
6554 isl_ast_node *tree;
6555 int n_domain = 0;
6557 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
6558 schedule = isl_union_map_read_from_str(ctx, str);
6559 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6561 str = "{ [i] -> unroll[0] }";
6562 options = isl_union_map_read_from_str(ctx, str);
6564 build = isl_ast_build_from_context(set);
6565 build = isl_ast_build_set_options(build, options);
6566 build = isl_ast_build_set_at_each_domain(build,
6567 &count_domains, &n_domain);
6568 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6569 isl_ast_build_free(build);
6570 if (!tree)
6571 return -1;
6573 isl_ast_node_free(tree);
6575 if (n_domain != 3)
6576 isl_die(ctx, isl_error_unknown,
6577 "unexpected number of for nodes", return -1);
6579 return 0;
6582 /* Check that if the ast_build_exploit_nested_bounds options is set,
6583 * we do not get an outer if node in the generated AST,
6584 * while we do get such an outer if node if the options is not set.
6586 static int test_ast_gen4(isl_ctx *ctx)
6588 const char *str;
6589 isl_set *set;
6590 isl_union_map *schedule;
6591 isl_ast_build *build;
6592 isl_ast_node *tree;
6593 enum isl_ast_node_type type;
6594 int enb;
6596 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
6597 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
6599 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
6601 schedule = isl_union_map_read_from_str(ctx, str);
6602 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6603 build = isl_ast_build_from_context(set);
6604 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6605 isl_ast_build_free(build);
6606 if (!tree)
6607 return -1;
6609 type = isl_ast_node_get_type(tree);
6610 isl_ast_node_free(tree);
6612 if (type == isl_ast_node_if)
6613 isl_die(ctx, isl_error_unknown,
6614 "not expecting if node", return -1);
6616 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
6618 schedule = isl_union_map_read_from_str(ctx, str);
6619 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6620 build = isl_ast_build_from_context(set);
6621 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6622 isl_ast_build_free(build);
6623 if (!tree)
6624 return -1;
6626 type = isl_ast_node_get_type(tree);
6627 isl_ast_node_free(tree);
6629 if (type != isl_ast_node_if)
6630 isl_die(ctx, isl_error_unknown,
6631 "expecting if node", return -1);
6633 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
6635 return 0;
6638 /* This function is called for each leaf in the AST generated
6639 * from test_ast_gen5.
6641 * We finalize the AST generation by extending the outer schedule
6642 * with a zero-dimensional schedule. If this results in any for loops,
6643 * then this means that we did not pass along enough information
6644 * about the outer schedule to the inner AST generation.
6646 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
6647 void *user)
6649 isl_union_map *schedule, *extra;
6650 isl_ast_node *tree;
6652 schedule = isl_ast_build_get_schedule(build);
6653 extra = isl_union_map_copy(schedule);
6654 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
6655 schedule = isl_union_map_range_product(schedule, extra);
6656 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6657 isl_ast_build_free(build);
6659 if (!tree)
6660 return NULL;
6662 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
6663 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
6664 "code should not contain any for loop",
6665 return isl_ast_node_free(tree));
6667 return tree;
6670 /* Check that we do not lose any information when going back and
6671 * forth between internal and external schedule.
6673 * In particular, we create an AST where we unroll the only
6674 * non-constant dimension in the schedule. We therefore do
6675 * not expect any for loops in the AST. However, older versions
6676 * of isl would not pass along enough information about the outer
6677 * schedule when performing an inner code generation from a create_leaf
6678 * callback, resulting in the inner code generation producing a for loop.
6680 static int test_ast_gen5(isl_ctx *ctx)
6682 const char *str;
6683 isl_set *set;
6684 isl_union_map *schedule, *options;
6685 isl_ast_build *build;
6686 isl_ast_node *tree;
6688 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
6689 schedule = isl_union_map_read_from_str(ctx, str);
6691 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
6692 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
6693 options = isl_union_map_read_from_str(ctx, str);
6695 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
6696 build = isl_ast_build_from_context(set);
6697 build = isl_ast_build_set_options(build, options);
6698 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
6699 tree = isl_ast_build_node_from_schedule_map(build, schedule);
6700 isl_ast_build_free(build);
6701 isl_ast_node_free(tree);
6702 if (!tree)
6703 return -1;
6705 return 0;
6708 /* Check that the expression
6710 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
6712 * is not combined into
6714 * min(n/2, 0)
6716 * as this would result in n/2 being evaluated in parts of
6717 * the definition domain where n is not a multiple of 2.
6719 static int test_ast_expr(isl_ctx *ctx)
6721 const char *str;
6722 isl_pw_aff *pa;
6723 isl_ast_build *build;
6724 isl_ast_expr *expr;
6725 int min_max;
6726 int is_min;
6728 min_max = isl_options_get_ast_build_detect_min_max(ctx);
6729 isl_options_set_ast_build_detect_min_max(ctx, 1);
6731 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
6732 pa = isl_pw_aff_read_from_str(ctx, str);
6733 build = isl_ast_build_alloc(ctx);
6734 expr = isl_ast_build_expr_from_pw_aff(build, pa);
6735 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
6736 isl_ast_expr_get_op_type(expr) == isl_ast_op_min;
6737 isl_ast_build_free(build);
6738 isl_ast_expr_free(expr);
6740 isl_options_set_ast_build_detect_min_max(ctx, min_max);
6742 if (!expr)
6743 return -1;
6744 if (is_min)
6745 isl_die(ctx, isl_error_unknown,
6746 "expressions should not be combined", return -1);
6748 return 0;
6751 static int test_ast_gen(isl_ctx *ctx)
6753 if (test_ast_gen1(ctx) < 0)
6754 return -1;
6755 if (test_ast_gen2(ctx) < 0)
6756 return -1;
6757 if (test_ast_gen3(ctx) < 0)
6758 return -1;
6759 if (test_ast_gen4(ctx) < 0)
6760 return -1;
6761 if (test_ast_gen5(ctx) < 0)
6762 return -1;
6763 if (test_ast_expr(ctx) < 0)
6764 return -1;
6765 return 0;
6768 /* Check if dropping output dimensions from an isl_pw_multi_aff
6769 * works properly.
6771 static int test_pw_multi_aff(isl_ctx *ctx)
6773 const char *str;
6774 isl_pw_multi_aff *pma1, *pma2;
6775 int equal;
6777 str = "{ [i,j] -> [i+j, 4i-j] }";
6778 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
6779 str = "{ [i,j] -> [4i-j] }";
6780 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6782 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
6784 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6786 isl_pw_multi_aff_free(pma1);
6787 isl_pw_multi_aff_free(pma2);
6788 if (equal < 0)
6789 return -1;
6790 if (!equal)
6791 isl_die(ctx, isl_error_unknown,
6792 "expressions not equal", return -1);
6794 return 0;
6797 /* Check that we can properly parse multi piecewise affine expressions
6798 * where the piecewise affine expressions have different domains.
6800 static int test_multi_pw_aff(isl_ctx *ctx)
6802 const char *str;
6803 isl_set *dom, *dom2;
6804 isl_multi_pw_aff *mpa1, *mpa2;
6805 isl_pw_aff *pa;
6806 int equal;
6807 int equal_domain;
6809 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
6810 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
6811 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
6812 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
6813 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
6814 str = "{ [i] -> [(i : i > 0), 2i] }";
6815 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
6817 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
6819 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
6820 dom = isl_pw_aff_domain(pa);
6821 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
6822 dom2 = isl_pw_aff_domain(pa);
6823 equal_domain = isl_set_is_equal(dom, dom2);
6825 isl_set_free(dom);
6826 isl_set_free(dom2);
6827 isl_multi_pw_aff_free(mpa1);
6828 isl_multi_pw_aff_free(mpa2);
6830 if (equal < 0)
6831 return -1;
6832 if (!equal)
6833 isl_die(ctx, isl_error_unknown,
6834 "expressions not equal", return -1);
6836 if (equal_domain < 0)
6837 return -1;
6838 if (equal_domain)
6839 isl_die(ctx, isl_error_unknown,
6840 "domains unexpectedly equal", return -1);
6842 return 0;
6845 /* This is a regression test for a bug where isl_basic_map_simplify
6846 * would end up in an infinite loop. In particular, we construct
6847 * an empty basic set that is not obviously empty.
6848 * isl_basic_set_is_empty marks the basic set as empty.
6849 * After projecting out i3, the variable can be dropped completely,
6850 * but isl_basic_map_simplify refrains from doing so if the basic set
6851 * is empty and would end up in an infinite loop if it didn't test
6852 * explicitly for empty basic maps in the outer loop.
6854 static int test_simplify_1(isl_ctx *ctx)
6856 const char *str;
6857 isl_basic_set *bset;
6858 int empty;
6860 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
6861 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
6862 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
6863 "i3 >= i2 }";
6864 bset = isl_basic_set_read_from_str(ctx, str);
6865 empty = isl_basic_set_is_empty(bset);
6866 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
6867 isl_basic_set_free(bset);
6868 if (!bset)
6869 return -1;
6870 if (!empty)
6871 isl_die(ctx, isl_error_unknown,
6872 "basic set should be empty", return -1);
6874 return 0;
6877 /* Check that the equality in the set description below
6878 * is simplified away.
6880 static int test_simplify_2(isl_ctx *ctx)
6882 const char *str;
6883 isl_basic_set *bset;
6884 isl_bool universe;
6886 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
6887 bset = isl_basic_set_read_from_str(ctx, str);
6888 universe = isl_basic_set_plain_is_universe(bset);
6889 isl_basic_set_free(bset);
6891 if (universe < 0)
6892 return -1;
6893 if (!universe)
6894 isl_die(ctx, isl_error_unknown,
6895 "equality not simplified away", return -1);
6896 return 0;
6899 /* Some simplification tests.
6901 static int test_simplify(isl_ctx *ctx)
6903 if (test_simplify_1(ctx) < 0)
6904 return -1;
6905 if (test_simplify_2(ctx) < 0)
6906 return -1;
6907 return 0;
6910 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
6911 * with gbr context would fail to disable the use of the shifted tableau
6912 * when transferring equalities for the input to the context, resulting
6913 * in invalid sample values.
6915 static int test_partial_lexmin(isl_ctx *ctx)
6917 const char *str;
6918 isl_basic_set *bset;
6919 isl_basic_map *bmap;
6920 isl_map *map;
6922 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
6923 bmap = isl_basic_map_read_from_str(ctx, str);
6924 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
6925 bset = isl_basic_set_read_from_str(ctx, str);
6926 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
6927 isl_map_free(map);
6929 if (!map)
6930 return -1;
6932 return 0;
6935 /* Check that the variable compression performed on the existentially
6936 * quantified variables inside isl_basic_set_compute_divs is not confused
6937 * by the implicit equalities among the parameters.
6939 static int test_compute_divs(isl_ctx *ctx)
6941 const char *str;
6942 isl_basic_set *bset;
6943 isl_set *set;
6945 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
6946 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
6947 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
6948 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
6949 bset = isl_basic_set_read_from_str(ctx, str);
6950 set = isl_basic_set_compute_divs(bset);
6951 isl_set_free(set);
6952 if (!set)
6953 return -1;
6955 return 0;
6958 /* Check that isl_schedule_get_map is not confused by a schedule tree
6959 * with divergent filter node parameters, as can result from a call
6960 * to isl_schedule_intersect_domain.
6962 static int test_schedule_tree(isl_ctx *ctx)
6964 const char *str;
6965 isl_union_set *uset;
6966 isl_schedule *sched1, *sched2;
6967 isl_union_map *umap;
6969 uset = isl_union_set_read_from_str(ctx, "{ A[i] }");
6970 sched1 = isl_schedule_from_domain(uset);
6971 uset = isl_union_set_read_from_str(ctx, "{ B[] }");
6972 sched2 = isl_schedule_from_domain(uset);
6974 sched1 = isl_schedule_sequence(sched1, sched2);
6975 str = "[n] -> { A[i] : 0 <= i < n; B[] }";
6976 uset = isl_union_set_read_from_str(ctx, str);
6977 sched1 = isl_schedule_intersect_domain(sched1, uset);
6978 umap = isl_schedule_get_map(sched1);
6979 isl_schedule_free(sched1);
6980 isl_union_map_free(umap);
6981 if (!umap)
6982 return -1;
6984 return 0;
6987 /* Check that the reaching domain elements and the prefix schedule
6988 * at a leaf node are the same before and after grouping.
6990 static int test_schedule_tree_group_1(isl_ctx *ctx)
6992 int equal;
6993 const char *str;
6994 isl_id *id;
6995 isl_union_set *uset;
6996 isl_multi_union_pw_aff *mupa;
6997 isl_union_pw_multi_aff *upma1, *upma2;
6998 isl_union_set *domain1, *domain2;
6999 isl_union_map *umap1, *umap2;
7000 isl_schedule_node *node;
7002 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
7003 uset = isl_union_set_read_from_str(ctx, str);
7004 node = isl_schedule_node_from_domain(uset);
7005 node = isl_schedule_node_child(node, 0);
7006 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
7007 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7008 node = isl_schedule_node_insert_partial_schedule(node, mupa);
7009 node = isl_schedule_node_child(node, 0);
7010 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
7011 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7012 node = isl_schedule_node_insert_partial_schedule(node, mupa);
7013 node = isl_schedule_node_child(node, 0);
7014 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
7015 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
7016 domain1 = isl_schedule_node_get_domain(node);
7017 id = isl_id_alloc(ctx, "group", NULL);
7018 node = isl_schedule_node_parent(node);
7019 node = isl_schedule_node_group(node, id);
7020 node = isl_schedule_node_child(node, 0);
7021 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
7022 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
7023 domain2 = isl_schedule_node_get_domain(node);
7024 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
7025 if (equal >= 0 && equal)
7026 equal = isl_union_set_is_equal(domain1, domain2);
7027 if (equal >= 0 && equal)
7028 equal = isl_union_map_is_equal(umap1, umap2);
7029 isl_union_map_free(umap1);
7030 isl_union_map_free(umap2);
7031 isl_union_set_free(domain1);
7032 isl_union_set_free(domain2);
7033 isl_union_pw_multi_aff_free(upma1);
7034 isl_union_pw_multi_aff_free(upma2);
7035 isl_schedule_node_free(node);
7037 if (equal < 0)
7038 return -1;
7039 if (!equal)
7040 isl_die(ctx, isl_error_unknown,
7041 "expressions not equal", return -1);
7043 return 0;
7046 /* Check that we can have nested groupings and that the union map
7047 * schedule representation is the same before and after the grouping.
7048 * Note that after the grouping, the union map representation contains
7049 * the domain constraints from the ranges of the expansion nodes,
7050 * while they are missing from the union map representation of
7051 * the tree without expansion nodes.
7053 * Also check that the global expansion is as expected.
7055 static int test_schedule_tree_group_2(isl_ctx *ctx)
7057 int equal, equal_expansion;
7058 const char *str;
7059 isl_id *id;
7060 isl_union_set *uset;
7061 isl_union_map *umap1, *umap2;
7062 isl_union_map *expansion1, *expansion2;
7063 isl_union_set_list *filters;
7064 isl_multi_union_pw_aff *mupa;
7065 isl_schedule *schedule;
7066 isl_schedule_node *node;
7068 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
7069 "S3[i,j] : 0 <= i,j < 10 }";
7070 uset = isl_union_set_read_from_str(ctx, str);
7071 node = isl_schedule_node_from_domain(uset);
7072 node = isl_schedule_node_child(node, 0);
7073 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
7074 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7075 node = isl_schedule_node_insert_partial_schedule(node, mupa);
7076 node = isl_schedule_node_child(node, 0);
7077 str = "{ S1[i,j] }";
7078 uset = isl_union_set_read_from_str(ctx, str);
7079 filters = isl_union_set_list_from_union_set(uset);
7080 str = "{ S2[i,j]; S3[i,j] }";
7081 uset = isl_union_set_read_from_str(ctx, str);
7082 filters = isl_union_set_list_add(filters, uset);
7083 node = isl_schedule_node_insert_sequence(node, filters);
7084 node = isl_schedule_node_child(node, 1);
7085 node = isl_schedule_node_child(node, 0);
7086 str = "{ S2[i,j] }";
7087 uset = isl_union_set_read_from_str(ctx, str);
7088 filters = isl_union_set_list_from_union_set(uset);
7089 str = "{ S3[i,j] }";
7090 uset = isl_union_set_read_from_str(ctx, str);
7091 filters = isl_union_set_list_add(filters, uset);
7092 node = isl_schedule_node_insert_sequence(node, filters);
7094 schedule = isl_schedule_node_get_schedule(node);
7095 umap1 = isl_schedule_get_map(schedule);
7096 uset = isl_schedule_get_domain(schedule);
7097 umap1 = isl_union_map_intersect_domain(umap1, uset);
7098 isl_schedule_free(schedule);
7100 node = isl_schedule_node_parent(node);
7101 node = isl_schedule_node_parent(node);
7102 id = isl_id_alloc(ctx, "group1", NULL);
7103 node = isl_schedule_node_group(node, id);
7104 node = isl_schedule_node_child(node, 1);
7105 node = isl_schedule_node_child(node, 0);
7106 id = isl_id_alloc(ctx, "group2", NULL);
7107 node = isl_schedule_node_group(node, id);
7109 schedule = isl_schedule_node_get_schedule(node);
7110 umap2 = isl_schedule_get_map(schedule);
7111 isl_schedule_free(schedule);
7113 node = isl_schedule_node_root(node);
7114 node = isl_schedule_node_child(node, 0);
7115 expansion1 = isl_schedule_node_get_subtree_expansion(node);
7116 isl_schedule_node_free(node);
7118 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
7119 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
7120 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
7122 expansion2 = isl_union_map_read_from_str(ctx, str);
7124 equal = isl_union_map_is_equal(umap1, umap2);
7125 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
7127 isl_union_map_free(umap1);
7128 isl_union_map_free(umap2);
7129 isl_union_map_free(expansion1);
7130 isl_union_map_free(expansion2);
7132 if (equal < 0 || equal_expansion < 0)
7133 return -1;
7134 if (!equal)
7135 isl_die(ctx, isl_error_unknown,
7136 "expressions not equal", return -1);
7137 if (!equal_expansion)
7138 isl_die(ctx, isl_error_unknown,
7139 "unexpected expansion", return -1);
7141 return 0;
7144 /* Some tests for the isl_schedule_node_group function.
7146 static int test_schedule_tree_group(isl_ctx *ctx)
7148 if (test_schedule_tree_group_1(ctx) < 0)
7149 return -1;
7150 if (test_schedule_tree_group_2(ctx) < 0)
7151 return -1;
7152 return 0;
7155 struct {
7156 const char *set;
7157 const char *dual;
7158 } coef_tests[] = {
7159 { "{ rat: [i] : 0 <= i <= 10 }",
7160 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
7161 { "{ rat: [i] : FALSE }",
7162 "{ rat: coefficients[[cst] -> [a]] }" },
7163 { "{ rat: [i] : }",
7164 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
7167 struct {
7168 const char *set;
7169 const char *dual;
7170 } sol_tests[] = {
7171 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
7172 "{ rat: [i] : 0 <= i <= 10 }" },
7173 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
7174 "{ rat: [i] }" },
7175 { "{ rat: coefficients[[cst] -> [a]] }",
7176 "{ rat: [i] : FALSE }" },
7179 /* Test the basic functionality of isl_basic_set_coefficients and
7180 * isl_basic_set_solutions.
7182 static int test_dual(isl_ctx *ctx)
7184 int i;
7186 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
7187 int equal;
7188 isl_basic_set *bset1, *bset2;
7190 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
7191 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
7192 bset1 = isl_basic_set_coefficients(bset1);
7193 equal = isl_basic_set_is_equal(bset1, bset2);
7194 isl_basic_set_free(bset1);
7195 isl_basic_set_free(bset2);
7196 if (equal < 0)
7197 return -1;
7198 if (!equal)
7199 isl_die(ctx, isl_error_unknown,
7200 "incorrect dual", return -1);
7203 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
7204 int equal;
7205 isl_basic_set *bset1, *bset2;
7207 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
7208 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
7209 bset1 = isl_basic_set_solutions(bset1);
7210 equal = isl_basic_set_is_equal(bset1, bset2);
7211 isl_basic_set_free(bset1);
7212 isl_basic_set_free(bset2);
7213 if (equal < 0)
7214 return -1;
7215 if (!equal)
7216 isl_die(ctx, isl_error_unknown,
7217 "incorrect dual", return -1);
7220 return 0;
7223 struct {
7224 int scale_tile;
7225 int shift_point;
7226 const char *domain;
7227 const char *schedule;
7228 const char *sizes;
7229 const char *tile;
7230 const char *point;
7231 } tile_tests[] = {
7232 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
7233 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7234 "{ [32,32] }",
7235 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
7236 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7238 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
7239 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7240 "{ [32,32] }",
7241 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
7242 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7244 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
7245 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7246 "{ [32,32] }",
7247 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
7248 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
7250 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
7251 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
7252 "{ [32,32] }",
7253 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
7254 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
7258 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
7259 * tile the band and then check if the tile and point bands have the
7260 * expected partial schedule.
7262 static int test_tile(isl_ctx *ctx)
7264 int i;
7265 int scale;
7266 int shift;
7268 scale = isl_options_get_tile_scale_tile_loops(ctx);
7269 shift = isl_options_get_tile_shift_point_loops(ctx);
7271 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
7272 int opt;
7273 int equal;
7274 const char *str;
7275 isl_union_set *domain;
7276 isl_multi_union_pw_aff *mupa, *mupa2;
7277 isl_schedule_node *node;
7278 isl_multi_val *sizes;
7280 opt = tile_tests[i].scale_tile;
7281 isl_options_set_tile_scale_tile_loops(ctx, opt);
7282 opt = tile_tests[i].shift_point;
7283 isl_options_set_tile_shift_point_loops(ctx, opt);
7285 str = tile_tests[i].domain;
7286 domain = isl_union_set_read_from_str(ctx, str);
7287 node = isl_schedule_node_from_domain(domain);
7288 node = isl_schedule_node_child(node, 0);
7289 str = tile_tests[i].schedule;
7290 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7291 node = isl_schedule_node_insert_partial_schedule(node, mupa);
7292 str = tile_tests[i].sizes;
7293 sizes = isl_multi_val_read_from_str(ctx, str);
7294 node = isl_schedule_node_band_tile(node, sizes);
7296 str = tile_tests[i].tile;
7297 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7298 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
7299 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
7300 isl_multi_union_pw_aff_free(mupa);
7301 isl_multi_union_pw_aff_free(mupa2);
7303 node = isl_schedule_node_child(node, 0);
7305 str = tile_tests[i].point;
7306 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
7307 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
7308 if (equal >= 0 && equal)
7309 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
7310 mupa2);
7311 isl_multi_union_pw_aff_free(mupa);
7312 isl_multi_union_pw_aff_free(mupa2);
7314 isl_schedule_node_free(node);
7316 if (equal < 0)
7317 return -1;
7318 if (!equal)
7319 isl_die(ctx, isl_error_unknown,
7320 "unexpected result", return -1);
7323 isl_options_set_tile_scale_tile_loops(ctx, scale);
7324 isl_options_set_tile_shift_point_loops(ctx, shift);
7326 return 0;
7329 /* Check that the domain hash of a space is equal to the hash
7330 * of the domain of the space.
7332 static int test_domain_hash(isl_ctx *ctx)
7334 isl_map *map;
7335 isl_space *space;
7336 uint32_t hash1, hash2;
7338 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
7339 space = isl_map_get_space(map);
7340 isl_map_free(map);
7341 hash1 = isl_space_get_domain_hash(space);
7342 space = isl_space_domain(space);
7343 hash2 = isl_space_get_hash(space);
7344 isl_space_free(space);
7346 if (!space)
7347 return -1;
7348 if (hash1 != hash2)
7349 isl_die(ctx, isl_error_unknown,
7350 "domain hash not equal to hash of domain", return -1);
7352 return 0;
7355 /* Check that a universe basic set that is not obviously equal to the universe
7356 * is still recognized as being equal to the universe.
7358 static int test_universe(isl_ctx *ctx)
7360 const char *s;
7361 isl_basic_set *bset;
7362 isl_bool is_univ;
7364 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
7365 bset = isl_basic_set_read_from_str(ctx, s);
7366 is_univ = isl_basic_set_is_universe(bset);
7367 isl_basic_set_free(bset);
7369 if (is_univ < 0)
7370 return -1;
7371 if (!is_univ)
7372 isl_die(ctx, isl_error_unknown,
7373 "not recognized as universe set", return -1);
7375 return 0;
7378 /* Sets for which chambers are computed and checked.
7380 const char *chambers_tests[] = {
7381 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
7382 "z >= 0 and z <= C - y and z <= B - x - y }",
7385 /* Add the domain of "cell" to "cells".
7387 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
7389 isl_basic_set_list **cells = user;
7390 isl_basic_set *dom;
7392 dom = isl_cell_get_domain(cell);
7393 isl_cell_free(cell);
7394 *cells = isl_basic_set_list_add(*cells, dom);
7396 return *cells ? isl_stat_ok : isl_stat_error;
7399 /* Check that the elements of "list" are pairwise disjoint.
7401 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
7403 int i, j, n;
7405 if (!list)
7406 return isl_stat_error;
7408 n = isl_basic_set_list_n_basic_set(list);
7409 for (i = 0; i < n; ++i) {
7410 isl_basic_set *bset_i;
7412 bset_i = isl_basic_set_list_get_basic_set(list, i);
7413 for (j = i + 1; j < n; ++j) {
7414 isl_basic_set *bset_j;
7415 isl_bool disjoint;
7417 bset_j = isl_basic_set_list_get_basic_set(list, j);
7418 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
7419 isl_basic_set_free(bset_j);
7420 if (!disjoint)
7421 isl_die(isl_basic_set_list_get_ctx(list),
7422 isl_error_unknown, "not disjoint",
7423 break);
7424 if (disjoint < 0 || !disjoint)
7425 break;
7427 isl_basic_set_free(bset_i);
7428 if (j < n)
7429 return isl_stat_error;
7432 return isl_stat_ok;
7435 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
7436 * are pairwise disjoint.
7438 static int test_chambers(isl_ctx *ctx)
7440 int i;
7442 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
7443 isl_basic_set *bset;
7444 isl_vertices *vertices;
7445 isl_basic_set_list *cells;
7446 isl_stat ok;
7448 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
7449 vertices = isl_basic_set_compute_vertices(bset);
7450 cells = isl_basic_set_list_alloc(ctx, 0);
7451 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
7452 &cells) < 0)
7453 cells = isl_basic_set_list_free(cells);
7454 ok = check_pairwise_disjoint(cells);
7455 isl_basic_set_list_free(cells);
7456 isl_vertices_free(vertices);
7457 isl_basic_set_free(bset);
7459 if (ok < 0)
7460 return -1;
7463 return 0;
7466 struct {
7467 const char *name;
7468 int (*fn)(isl_ctx *ctx);
7469 } tests [] = {
7470 { "universe", &test_universe },
7471 { "domain hash", &test_domain_hash },
7472 { "dual", &test_dual },
7473 { "dependence analysis", &test_flow },
7474 { "val", &test_val },
7475 { "compute divs", &test_compute_divs },
7476 { "partial lexmin", &test_partial_lexmin },
7477 { "simplify", &test_simplify },
7478 { "curry", &test_curry },
7479 { "piecewise multi affine expressions", &test_pw_multi_aff },
7480 { "multi piecewise affine expressions", &test_multi_pw_aff },
7481 { "conversion", &test_conversion },
7482 { "list", &test_list },
7483 { "align parameters", &test_align_parameters },
7484 { "preimage", &test_preimage },
7485 { "pullback", &test_pullback },
7486 { "AST", &test_ast },
7487 { "AST build", &test_ast_build },
7488 { "AST generation", &test_ast_gen },
7489 { "eliminate", &test_eliminate },
7490 { "residue class", &test_residue_class },
7491 { "div", &test_div },
7492 { "slice", &test_slice },
7493 { "fixed power", &test_fixed_power },
7494 { "sample", &test_sample },
7495 { "output", &test_output },
7496 { "vertices", &test_vertices },
7497 { "chambers", &test_chambers },
7498 { "fixed", &test_fixed },
7499 { "equal", &test_equal },
7500 { "disjoint", &test_disjoint },
7501 { "product", &test_product },
7502 { "dim_max", &test_dim_max },
7503 { "affine", &test_aff },
7504 { "injective", &test_injective },
7505 { "schedule (whole component)", &test_schedule_whole },
7506 { "schedule (incremental)", &test_schedule_incremental },
7507 { "schedule tree", &test_schedule_tree },
7508 { "schedule tree grouping", &test_schedule_tree_group },
7509 { "tile", &test_tile },
7510 { "union_pw", &test_union_pw },
7511 { "eval", &test_eval },
7512 { "parse", &test_parse },
7513 { "single-valued", &test_sv },
7514 { "affine hull", &test_affine_hull },
7515 { "simple_hull", &test_simple_hull },
7516 { "coalesce", &test_coalesce },
7517 { "factorize", &test_factorize },
7518 { "subset", &test_subset },
7519 { "subtract", &test_subtract },
7520 { "intersect", &test_intersect },
7521 { "lexmin", &test_lexmin },
7522 { "min", &test_min },
7523 { "gist", &test_gist },
7524 { "piecewise quasi-polynomials", &test_pwqp },
7525 { "lift", &test_lift },
7526 { "bound", &test_bound },
7527 { "union", &test_union },
7528 { "split periods", &test_split_periods },
7529 { "lexicographic order", &test_lex },
7530 { "bijectivity", &test_bijective },
7531 { "dataflow analysis", &test_dep },
7532 { "reading", &test_read },
7533 { "bounded", &test_bounded },
7534 { "construction", &test_construction },
7535 { "dimension manipulation", &test_dim },
7536 { "map application", &test_application },
7537 { "convex hull", &test_convex_hull },
7538 { "transitive closure", &test_closure },
7541 int main(int argc, char **argv)
7543 int i;
7544 struct isl_ctx *ctx;
7545 struct isl_options *options;
7547 options = isl_options_new_with_defaults();
7548 assert(options);
7549 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
7551 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
7552 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
7553 printf("%s\n", tests[i].name);
7554 if (tests[i].fn(ctx) < 0)
7555 goto error;
7557 isl_ctx_free(ctx);
7558 return 0;
7559 error:
7560 isl_ctx_free(ctx);
7561 return -1;