isl_map_convex_hull: detect equalities before checking number of basic maps
[isl.git] / isl_test.c
blob32102835393805685fc14cfc773e754d15202902
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <assert.h>
16 #include <stdio.h>
17 #include <limits.h>
18 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
20 #include <isl_aff_private.h>
21 #include <isl/set.h>
22 #include <isl/flow.h>
23 #include <isl_constraint_private.h>
24 #include <isl/polynomial.h>
25 #include <isl/union_map.h>
26 #include <isl_factorization.h>
27 #include <isl/schedule.h>
28 #include <isl_options_private.h>
29 #include <isl/vertices.h>
30 #include <isl/ast_build.h>
31 #include <isl/val.h>
32 #include <isl/ilp.h>
33 #include <isl_ast_build_expr.h>
35 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
37 static char *srcdir;
39 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
40 char *filename;
41 int length;
42 char *pattern = "%s/test_inputs/%s.%s";
44 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
45 + strlen(suffix) + 1;
46 filename = isl_alloc_array(ctx, char, length);
48 if (!filename)
49 return NULL;
51 sprintf(filename, pattern, srcdir, name, suffix);
53 return filename;
56 void test_parse_map(isl_ctx *ctx, const char *str)
58 isl_map *map;
60 map = isl_map_read_from_str(ctx, str);
61 assert(map);
62 isl_map_free(map);
65 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
67 isl_map *map, *map2;
68 int equal;
70 map = isl_map_read_from_str(ctx, str);
71 map2 = isl_map_read_from_str(ctx, str2);
72 equal = isl_map_is_equal(map, map2);
73 isl_map_free(map);
74 isl_map_free(map2);
76 if (equal < 0)
77 return -1;
78 if (!equal)
79 isl_die(ctx, isl_error_unknown, "maps not equal",
80 return -1);
82 return 0;
85 void test_parse_pwqp(isl_ctx *ctx, const char *str)
87 isl_pw_qpolynomial *pwqp;
89 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
90 assert(pwqp);
91 isl_pw_qpolynomial_free(pwqp);
94 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
96 isl_pw_aff *pwaff;
98 pwaff = isl_pw_aff_read_from_str(ctx, str);
99 assert(pwaff);
100 isl_pw_aff_free(pwaff);
103 int test_parse(struct isl_ctx *ctx)
105 isl_map *map, *map2;
106 const char *str, *str2;
108 str = "{ [i] -> [-i] }";
109 map = isl_map_read_from_str(ctx, str);
110 assert(map);
111 isl_map_free(map);
113 str = "{ A[i] -> L[([i/3])] }";
114 map = isl_map_read_from_str(ctx, str);
115 assert(map);
116 isl_map_free(map);
118 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
119 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
120 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
122 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
123 str2 = "{ [x, y] : 2y >= 6 - x }";
124 if (test_parse_map_equal(ctx, str, str2) < 0)
125 return -1;
127 if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
128 "{ [x,y] : x <= y, 2*y + 3 }") < 0)
129 return -1;
130 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
131 if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
132 str) < 0)
133 return -1;
135 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
136 map = isl_map_read_from_str(ctx, str);
137 str = "{ [new, old] -> [o0, o1] : "
138 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
139 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
140 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
141 map2 = isl_map_read_from_str(ctx, str);
142 assert(isl_map_is_equal(map, map2));
143 isl_map_free(map);
144 isl_map_free(map2);
146 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
147 map = isl_map_read_from_str(ctx, str);
148 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
149 map2 = isl_map_read_from_str(ctx, str);
150 assert(isl_map_is_equal(map, map2));
151 isl_map_free(map);
152 isl_map_free(map2);
154 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
155 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
156 if (test_parse_map_equal(ctx, str, str2) < 0)
157 return -1;
159 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
160 str2 = "{ [i,j] -> [min(i,j)] }";
161 if (test_parse_map_equal(ctx, str, str2) < 0)
162 return -1;
164 str = "{ [i,j] : i != j }";
165 str2 = "{ [i,j] : i < j or i > j }";
166 if (test_parse_map_equal(ctx, str, str2) < 0)
167 return -1;
169 str = "{ [i,j] : (i+1)*2 >= j }";
170 str2 = "{ [i, j] : j <= 2 + 2i }";
171 if (test_parse_map_equal(ctx, str, str2) < 0)
172 return -1;
174 str = "{ [i] -> [i > 0 ? 4 : 5] }";
175 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
176 if (test_parse_map_equal(ctx, str, str2) < 0)
177 return -1;
179 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
180 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
181 if (test_parse_map_equal(ctx, str, str2) < 0)
182 return -1;
184 str = "{ [x] : x >= 0 }";
185 str2 = "{ [x] : x-0 >= 0 }";
186 if (test_parse_map_equal(ctx, str, str2) < 0)
187 return -1;
189 str = "{ [i] : ((i > 10)) }";
190 str2 = "{ [i] : i >= 11 }";
191 if (test_parse_map_equal(ctx, str, str2) < 0)
192 return -1;
194 str = "{ [i] -> [0] }";
195 str2 = "{ [i] -> [0 * i] }";
196 if (test_parse_map_equal(ctx, str, str2) < 0)
197 return -1;
199 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
200 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
201 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
202 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
204 if (test_parse_map_equal(ctx, "{ [a] -> [b] : (not false) }",
205 "{ [a] -> [b] : true }") < 0)
206 return -1;
208 if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
209 "{ [i] : i <= 10 }") < 0)
210 return -1;
212 if (test_parse_map_equal(ctx, "{Sym=[n] [i] : i <= n }",
213 "[n] -> { [i] : i <= n }") < 0)
214 return -1;
216 if (test_parse_map_equal(ctx, "{ [*] }", "{ [a] }") < 0)
217 return -1;
219 if (test_parse_map_equal(ctx, "{ [i] : 2*floor(i/2) = i }",
220 "{ [i] : exists a : i = 2 a }") < 0)
221 return -1;
223 if (test_parse_map_equal(ctx, "{ [a] -> [b] : a = 5 implies b = 5 }",
224 "{ [a] -> [b] : a != 5 or b = 5 }") < 0)
225 return -1;
227 if (test_parse_map_equal(ctx, "{ [a] -> [a - 1 : a > 0] }",
228 "{ [a] -> [a - 1] : a > 0 }") < 0)
229 return -1;
230 if (test_parse_map_equal(ctx,
231 "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
232 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }") < 0)
233 return -1;
234 if (test_parse_map_equal(ctx,
235 "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
236 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
237 return -1;
238 if (test_parse_map_equal(ctx,
239 "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
240 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
241 return -1;
242 if (test_parse_map_equal(ctx,
243 "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
244 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
245 return -1;
246 if (test_parse_map_equal(ctx,
247 "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
248 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
249 return -1;
251 return 0;
254 void test_read(struct isl_ctx *ctx)
256 char *filename;
257 FILE *input;
258 struct isl_basic_set *bset1, *bset2;
259 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
261 filename = get_filename(ctx, "set", "omega");
262 assert(filename);
263 input = fopen(filename, "r");
264 assert(input);
266 bset1 = isl_basic_set_read_from_file(ctx, input);
267 bset2 = isl_basic_set_read_from_str(ctx, str);
269 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
271 isl_basic_set_free(bset1);
272 isl_basic_set_free(bset2);
273 free(filename);
275 fclose(input);
278 void test_bounded(struct isl_ctx *ctx)
280 isl_set *set;
281 int bounded;
283 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
284 bounded = isl_set_is_bounded(set);
285 assert(bounded);
286 isl_set_free(set);
288 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
289 bounded = isl_set_is_bounded(set);
290 assert(!bounded);
291 isl_set_free(set);
293 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
294 bounded = isl_set_is_bounded(set);
295 assert(!bounded);
296 isl_set_free(set);
299 /* Construct the basic set { [i] : 5 <= i <= N } */
300 void test_construction(struct isl_ctx *ctx)
302 isl_int v;
303 isl_space *dim;
304 isl_local_space *ls;
305 struct isl_basic_set *bset;
306 struct isl_constraint *c;
308 isl_int_init(v);
310 dim = isl_space_set_alloc(ctx, 1, 1);
311 bset = isl_basic_set_universe(isl_space_copy(dim));
312 ls = isl_local_space_from_space(dim);
314 c = isl_inequality_alloc(isl_local_space_copy(ls));
315 isl_int_set_si(v, -1);
316 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
317 isl_int_set_si(v, 1);
318 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
319 bset = isl_basic_set_add_constraint(bset, c);
321 c = isl_inequality_alloc(isl_local_space_copy(ls));
322 isl_int_set_si(v, 1);
323 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
324 isl_int_set_si(v, -5);
325 isl_constraint_set_constant(c, v);
326 bset = isl_basic_set_add_constraint(bset, c);
328 isl_local_space_free(ls);
329 isl_basic_set_free(bset);
331 isl_int_clear(v);
334 void test_dim(struct isl_ctx *ctx)
336 const char *str;
337 isl_map *map1, *map2;
339 map1 = isl_map_read_from_str(ctx,
340 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
341 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
342 map2 = isl_map_read_from_str(ctx,
343 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
344 assert(isl_map_is_equal(map1, map2));
345 isl_map_free(map2);
347 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
348 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
349 assert(isl_map_is_equal(map1, map2));
351 isl_map_free(map1);
352 isl_map_free(map2);
354 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
355 map1 = isl_map_read_from_str(ctx, str);
356 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
357 map2 = isl_map_read_from_str(ctx, str);
358 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
359 assert(isl_map_is_equal(map1, map2));
361 isl_map_free(map1);
362 isl_map_free(map2);
365 struct {
366 __isl_give isl_val *(*op)(__isl_take isl_val *v);
367 const char *arg;
368 const char *res;
369 } val_un_tests[] = {
370 { &isl_val_neg, "0", "0" },
371 { &isl_val_abs, "0", "0" },
372 { &isl_val_2exp, "0", "1" },
373 { &isl_val_floor, "0", "0" },
374 { &isl_val_ceil, "0", "0" },
375 { &isl_val_neg, "1", "-1" },
376 { &isl_val_neg, "-1", "1" },
377 { &isl_val_neg, "1/2", "-1/2" },
378 { &isl_val_neg, "-1/2", "1/2" },
379 { &isl_val_neg, "infty", "-infty" },
380 { &isl_val_neg, "-infty", "infty" },
381 { &isl_val_neg, "NaN", "NaN" },
382 { &isl_val_abs, "1", "1" },
383 { &isl_val_abs, "-1", "1" },
384 { &isl_val_abs, "1/2", "1/2" },
385 { &isl_val_abs, "-1/2", "1/2" },
386 { &isl_val_abs, "infty", "infty" },
387 { &isl_val_abs, "-infty", "infty" },
388 { &isl_val_abs, "NaN", "NaN" },
389 { &isl_val_floor, "1", "1" },
390 { &isl_val_floor, "-1", "-1" },
391 { &isl_val_floor, "1/2", "0" },
392 { &isl_val_floor, "-1/2", "-1" },
393 { &isl_val_floor, "infty", "infty" },
394 { &isl_val_floor, "-infty", "-infty" },
395 { &isl_val_floor, "NaN", "NaN" },
396 { &isl_val_ceil, "1", "1" },
397 { &isl_val_ceil, "-1", "-1" },
398 { &isl_val_ceil, "1/2", "1" },
399 { &isl_val_ceil, "-1/2", "0" },
400 { &isl_val_ceil, "infty", "infty" },
401 { &isl_val_ceil, "-infty", "-infty" },
402 { &isl_val_ceil, "NaN", "NaN" },
403 { &isl_val_2exp, "-3", "1/8" },
404 { &isl_val_2exp, "-1", "1/2" },
405 { &isl_val_2exp, "1", "2" },
406 { &isl_val_2exp, "2", "4" },
407 { &isl_val_2exp, "3", "8" },
410 /* Perform some basic tests of unary operations on isl_val objects.
412 static int test_un_val(isl_ctx *ctx)
414 int i;
415 isl_val *v, *res;
416 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
417 int ok;
419 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
420 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
421 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
422 fn = val_un_tests[i].op;
423 v = fn(v);
424 if (isl_val_is_nan(res))
425 ok = isl_val_is_nan(v);
426 else
427 ok = isl_val_eq(v, res);
428 isl_val_free(v);
429 isl_val_free(res);
430 if (ok < 0)
431 return -1;
432 if (!ok)
433 isl_die(ctx, isl_error_unknown,
434 "unexpected result", return -1);
437 return 0;
440 struct {
441 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
442 __isl_take isl_val *v2);
443 } val_bin_op[] = {
444 ['+'] = { &isl_val_add },
445 ['-'] = { &isl_val_sub },
446 ['*'] = { &isl_val_mul },
447 ['/'] = { &isl_val_div },
448 ['g'] = { &isl_val_gcd },
449 ['m'] = { &isl_val_min },
450 ['M'] = { &isl_val_max },
453 struct {
454 const char *arg1;
455 unsigned char op;
456 const char *arg2;
457 const char *res;
458 } val_bin_tests[] = {
459 { "0", '+', "0", "0" },
460 { "1", '+', "0", "1" },
461 { "1", '+', "1", "2" },
462 { "1", '-', "1", "0" },
463 { "1", '*', "1", "1" },
464 { "1", '/', "1", "1" },
465 { "2", '*', "3", "6" },
466 { "2", '*', "1/2", "1" },
467 { "2", '*', "1/3", "2/3" },
468 { "2/3", '*', "3/5", "2/5" },
469 { "2/3", '*', "7/5", "14/15" },
470 { "2", '/', "1/2", "4" },
471 { "-2", '/', "-1/2", "4" },
472 { "-2", '/', "1/2", "-4" },
473 { "2", '/', "-1/2", "-4" },
474 { "2", '/', "2", "1" },
475 { "2", '/', "3", "2/3" },
476 { "2/3", '/', "5/3", "2/5" },
477 { "2/3", '/', "5/7", "14/15" },
478 { "0", '/', "0", "NaN" },
479 { "42", '/', "0", "NaN" },
480 { "-42", '/', "0", "NaN" },
481 { "infty", '/', "0", "NaN" },
482 { "-infty", '/', "0", "NaN" },
483 { "NaN", '/', "0", "NaN" },
484 { "0", '/', "NaN", "NaN" },
485 { "42", '/', "NaN", "NaN" },
486 { "-42", '/', "NaN", "NaN" },
487 { "infty", '/', "NaN", "NaN" },
488 { "-infty", '/', "NaN", "NaN" },
489 { "NaN", '/', "NaN", "NaN" },
490 { "0", '/', "infty", "0" },
491 { "42", '/', "infty", "0" },
492 { "-42", '/', "infty", "0" },
493 { "infty", '/', "infty", "NaN" },
494 { "-infty", '/', "infty", "NaN" },
495 { "NaN", '/', "infty", "NaN" },
496 { "0", '/', "-infty", "0" },
497 { "42", '/', "-infty", "0" },
498 { "-42", '/', "-infty", "0" },
499 { "infty", '/', "-infty", "NaN" },
500 { "-infty", '/', "-infty", "NaN" },
501 { "NaN", '/', "-infty", "NaN" },
502 { "1", '-', "1/3", "2/3" },
503 { "1/3", '+', "1/2", "5/6" },
504 { "1/2", '+', "1/2", "1" },
505 { "3/4", '-', "1/4", "1/2" },
506 { "1/2", '-', "1/3", "1/6" },
507 { "infty", '+', "42", "infty" },
508 { "infty", '+', "infty", "infty" },
509 { "42", '+', "infty", "infty" },
510 { "infty", '-', "infty", "NaN" },
511 { "infty", '*', "infty", "infty" },
512 { "infty", '*', "-infty", "-infty" },
513 { "-infty", '*', "infty", "-infty" },
514 { "-infty", '*', "-infty", "infty" },
515 { "0", '*', "infty", "NaN" },
516 { "1", '*', "infty", "infty" },
517 { "infty", '*', "0", "NaN" },
518 { "infty", '*', "42", "infty" },
519 { "42", '-', "infty", "-infty" },
520 { "infty", '+', "-infty", "NaN" },
521 { "4", 'g', "6", "2" },
522 { "5", 'g', "6", "1" },
523 { "42", 'm', "3", "3" },
524 { "42", 'M', "3", "42" },
525 { "3", 'm', "42", "3" },
526 { "3", 'M', "42", "42" },
527 { "42", 'm', "infty", "42" },
528 { "42", 'M', "infty", "infty" },
529 { "42", 'm', "-infty", "-infty" },
530 { "42", 'M', "-infty", "42" },
531 { "42", 'm', "NaN", "NaN" },
532 { "42", 'M', "NaN", "NaN" },
533 { "infty", 'm', "-infty", "-infty" },
534 { "infty", 'M', "-infty", "infty" },
537 /* Perform some basic tests of binary operations on isl_val objects.
539 static int test_bin_val(isl_ctx *ctx)
541 int i;
542 isl_val *v1, *v2, *res;
543 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
544 __isl_take isl_val *v2);
545 int ok;
547 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
548 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
549 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
550 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
551 fn = val_bin_op[val_bin_tests[i].op].fn;
552 v1 = fn(v1, v2);
553 if (isl_val_is_nan(res))
554 ok = isl_val_is_nan(v1);
555 else
556 ok = isl_val_eq(v1, res);
557 isl_val_free(v1);
558 isl_val_free(res);
559 if (ok < 0)
560 return -1;
561 if (!ok)
562 isl_die(ctx, isl_error_unknown,
563 "unexpected result", return -1);
566 return 0;
569 /* Perform some basic tests on isl_val objects.
571 static int test_val(isl_ctx *ctx)
573 if (test_un_val(ctx) < 0)
574 return -1;
575 if (test_bin_val(ctx) < 0)
576 return -1;
577 return 0;
580 static int test_div(isl_ctx *ctx)
582 unsigned n;
583 const char *str;
584 int empty;
585 isl_int v;
586 isl_space *dim;
587 isl_set *set;
588 isl_local_space *ls;
589 struct isl_basic_set *bset;
590 struct isl_constraint *c;
592 isl_int_init(v);
594 /* test 1 */
595 dim = isl_space_set_alloc(ctx, 0, 3);
596 bset = isl_basic_set_universe(isl_space_copy(dim));
597 ls = isl_local_space_from_space(dim);
599 c = isl_equality_alloc(isl_local_space_copy(ls));
600 isl_int_set_si(v, -1);
601 isl_constraint_set_constant(c, v);
602 isl_int_set_si(v, 1);
603 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
604 isl_int_set_si(v, 3);
605 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
606 bset = isl_basic_set_add_constraint(bset, c);
608 c = isl_equality_alloc(isl_local_space_copy(ls));
609 isl_int_set_si(v, 1);
610 isl_constraint_set_constant(c, v);
611 isl_int_set_si(v, -1);
612 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
613 isl_int_set_si(v, 3);
614 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
615 bset = isl_basic_set_add_constraint(bset, c);
617 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
619 assert(bset && bset->n_div == 1);
620 isl_local_space_free(ls);
621 isl_basic_set_free(bset);
623 /* test 2 */
624 dim = isl_space_set_alloc(ctx, 0, 3);
625 bset = isl_basic_set_universe(isl_space_copy(dim));
626 ls = isl_local_space_from_space(dim);
628 c = isl_equality_alloc(isl_local_space_copy(ls));
629 isl_int_set_si(v, 1);
630 isl_constraint_set_constant(c, v);
631 isl_int_set_si(v, -1);
632 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
633 isl_int_set_si(v, 3);
634 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
635 bset = isl_basic_set_add_constraint(bset, c);
637 c = isl_equality_alloc(isl_local_space_copy(ls));
638 isl_int_set_si(v, -1);
639 isl_constraint_set_constant(c, v);
640 isl_int_set_si(v, 1);
641 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
642 isl_int_set_si(v, 3);
643 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
644 bset = isl_basic_set_add_constraint(bset, c);
646 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
648 assert(bset && bset->n_div == 1);
649 isl_local_space_free(ls);
650 isl_basic_set_free(bset);
652 /* test 3 */
653 dim = isl_space_set_alloc(ctx, 0, 3);
654 bset = isl_basic_set_universe(isl_space_copy(dim));
655 ls = isl_local_space_from_space(dim);
657 c = isl_equality_alloc(isl_local_space_copy(ls));
658 isl_int_set_si(v, 1);
659 isl_constraint_set_constant(c, v);
660 isl_int_set_si(v, -1);
661 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
662 isl_int_set_si(v, 3);
663 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
664 bset = isl_basic_set_add_constraint(bset, c);
666 c = isl_equality_alloc(isl_local_space_copy(ls));
667 isl_int_set_si(v, -3);
668 isl_constraint_set_constant(c, v);
669 isl_int_set_si(v, 1);
670 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
671 isl_int_set_si(v, 4);
672 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
673 bset = isl_basic_set_add_constraint(bset, c);
675 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
677 assert(bset && bset->n_div == 1);
678 isl_local_space_free(ls);
679 isl_basic_set_free(bset);
681 /* test 4 */
682 dim = isl_space_set_alloc(ctx, 0, 3);
683 bset = isl_basic_set_universe(isl_space_copy(dim));
684 ls = isl_local_space_from_space(dim);
686 c = isl_equality_alloc(isl_local_space_copy(ls));
687 isl_int_set_si(v, 2);
688 isl_constraint_set_constant(c, v);
689 isl_int_set_si(v, -1);
690 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
691 isl_int_set_si(v, 3);
692 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
693 bset = isl_basic_set_add_constraint(bset, c);
695 c = isl_equality_alloc(isl_local_space_copy(ls));
696 isl_int_set_si(v, -1);
697 isl_constraint_set_constant(c, v);
698 isl_int_set_si(v, 1);
699 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
700 isl_int_set_si(v, 6);
701 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
702 bset = isl_basic_set_add_constraint(bset, c);
704 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
706 assert(isl_basic_set_is_empty(bset));
707 isl_local_space_free(ls);
708 isl_basic_set_free(bset);
710 /* test 5 */
711 dim = isl_space_set_alloc(ctx, 0, 3);
712 bset = isl_basic_set_universe(isl_space_copy(dim));
713 ls = isl_local_space_from_space(dim);
715 c = isl_equality_alloc(isl_local_space_copy(ls));
716 isl_int_set_si(v, -1);
717 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
718 isl_int_set_si(v, 3);
719 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
720 bset = isl_basic_set_add_constraint(bset, c);
722 c = isl_equality_alloc(isl_local_space_copy(ls));
723 isl_int_set_si(v, 1);
724 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
725 isl_int_set_si(v, -3);
726 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
727 bset = isl_basic_set_add_constraint(bset, c);
729 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
731 assert(bset && bset->n_div == 0);
732 isl_basic_set_free(bset);
733 isl_local_space_free(ls);
735 /* test 6 */
736 dim = isl_space_set_alloc(ctx, 0, 3);
737 bset = isl_basic_set_universe(isl_space_copy(dim));
738 ls = isl_local_space_from_space(dim);
740 c = isl_equality_alloc(isl_local_space_copy(ls));
741 isl_int_set_si(v, -1);
742 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
743 isl_int_set_si(v, 6);
744 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
745 bset = isl_basic_set_add_constraint(bset, c);
747 c = isl_equality_alloc(isl_local_space_copy(ls));
748 isl_int_set_si(v, 1);
749 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
750 isl_int_set_si(v, -3);
751 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
752 bset = isl_basic_set_add_constraint(bset, c);
754 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
756 assert(bset && bset->n_div == 1);
757 isl_basic_set_free(bset);
758 isl_local_space_free(ls);
760 /* test 7 */
761 /* This test is a bit tricky. We set up an equality
762 * a + 3b + 3c = 6 e0
763 * Normalization of divs creates _two_ divs
764 * a = 3 e0
765 * c - b - e0 = 2 e1
766 * Afterwards e0 is removed again because it has coefficient -1
767 * and we end up with the original equality and div again.
768 * Perhaps we can avoid the introduction of this temporary div.
770 dim = isl_space_set_alloc(ctx, 0, 4);
771 bset = isl_basic_set_universe(isl_space_copy(dim));
772 ls = isl_local_space_from_space(dim);
774 c = isl_equality_alloc(isl_local_space_copy(ls));
775 isl_int_set_si(v, -1);
776 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
777 isl_int_set_si(v, -3);
778 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
779 isl_int_set_si(v, -3);
780 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
781 isl_int_set_si(v, 6);
782 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
783 bset = isl_basic_set_add_constraint(bset, c);
785 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
787 /* Test disabled for now */
789 assert(bset && bset->n_div == 1);
791 isl_local_space_free(ls);
792 isl_basic_set_free(bset);
794 /* test 8 */
795 dim = isl_space_set_alloc(ctx, 0, 5);
796 bset = isl_basic_set_universe(isl_space_copy(dim));
797 ls = isl_local_space_from_space(dim);
799 c = isl_equality_alloc(isl_local_space_copy(ls));
800 isl_int_set_si(v, -1);
801 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
802 isl_int_set_si(v, -3);
803 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
804 isl_int_set_si(v, -3);
805 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
806 isl_int_set_si(v, 6);
807 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
808 bset = isl_basic_set_add_constraint(bset, c);
810 c = isl_equality_alloc(isl_local_space_copy(ls));
811 isl_int_set_si(v, -1);
812 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
813 isl_int_set_si(v, 1);
814 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
815 isl_int_set_si(v, 1);
816 isl_constraint_set_constant(c, v);
817 bset = isl_basic_set_add_constraint(bset, c);
819 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
821 /* Test disabled for now */
823 assert(bset && bset->n_div == 1);
825 isl_local_space_free(ls);
826 isl_basic_set_free(bset);
828 /* test 9 */
829 dim = isl_space_set_alloc(ctx, 0, 4);
830 bset = isl_basic_set_universe(isl_space_copy(dim));
831 ls = isl_local_space_from_space(dim);
833 c = isl_equality_alloc(isl_local_space_copy(ls));
834 isl_int_set_si(v, 1);
835 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
836 isl_int_set_si(v, -1);
837 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
838 isl_int_set_si(v, -2);
839 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
840 bset = isl_basic_set_add_constraint(bset, c);
842 c = isl_equality_alloc(isl_local_space_copy(ls));
843 isl_int_set_si(v, -1);
844 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
845 isl_int_set_si(v, 3);
846 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
847 isl_int_set_si(v, 2);
848 isl_constraint_set_constant(c, v);
849 bset = isl_basic_set_add_constraint(bset, c);
851 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
853 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
855 assert(!isl_basic_set_is_empty(bset));
857 isl_local_space_free(ls);
858 isl_basic_set_free(bset);
860 /* test 10 */
861 dim = isl_space_set_alloc(ctx, 0, 3);
862 bset = isl_basic_set_universe(isl_space_copy(dim));
863 ls = isl_local_space_from_space(dim);
865 c = isl_equality_alloc(isl_local_space_copy(ls));
866 isl_int_set_si(v, 1);
867 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
868 isl_int_set_si(v, -2);
869 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
870 bset = isl_basic_set_add_constraint(bset, c);
872 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
874 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
876 isl_local_space_free(ls);
877 isl_basic_set_free(bset);
879 isl_int_clear(v);
881 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
882 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
883 set = isl_set_read_from_str(ctx, str);
884 set = isl_set_compute_divs(set);
885 isl_set_free(set);
886 if (!set)
887 return -1;
889 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
890 bset = isl_basic_set_read_from_str(ctx, str);
891 n = isl_basic_set_dim(bset, isl_dim_div);
892 isl_basic_set_free(bset);
893 if (!bset)
894 return -1;
895 if (n != 0)
896 isl_die(ctx, isl_error_unknown,
897 "expecting no existentials", return -1);
899 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
900 set = isl_set_read_from_str(ctx, str);
901 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
902 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
903 empty = isl_set_is_empty(set);
904 isl_set_free(set);
905 if (empty < 0)
906 return -1;
907 if (!empty)
908 isl_die(ctx, isl_error_unknown,
909 "result not as accurate as expected", return -1);
911 return 0;
914 void test_application_case(struct isl_ctx *ctx, const char *name)
916 char *filename;
917 FILE *input;
918 struct isl_basic_set *bset1, *bset2;
919 struct isl_basic_map *bmap;
921 filename = get_filename(ctx, name, "omega");
922 assert(filename);
923 input = fopen(filename, "r");
924 assert(input);
926 bset1 = isl_basic_set_read_from_file(ctx, input);
927 bmap = isl_basic_map_read_from_file(ctx, input);
929 bset1 = isl_basic_set_apply(bset1, bmap);
931 bset2 = isl_basic_set_read_from_file(ctx, input);
933 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
935 isl_basic_set_free(bset1);
936 isl_basic_set_free(bset2);
937 free(filename);
939 fclose(input);
942 void test_application(struct isl_ctx *ctx)
944 test_application_case(ctx, "application");
945 test_application_case(ctx, "application2");
948 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
950 char *filename;
951 FILE *input;
952 struct isl_basic_set *bset1, *bset2;
954 filename = get_filename(ctx, name, "polylib");
955 assert(filename);
956 input = fopen(filename, "r");
957 assert(input);
959 bset1 = isl_basic_set_read_from_file(ctx, input);
960 bset2 = isl_basic_set_read_from_file(ctx, input);
962 bset1 = isl_basic_set_affine_hull(bset1);
964 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
966 isl_basic_set_free(bset1);
967 isl_basic_set_free(bset2);
968 free(filename);
970 fclose(input);
973 int test_affine_hull(struct isl_ctx *ctx)
975 const char *str;
976 isl_set *set;
977 isl_basic_set *bset, *bset2;
978 int n;
979 int subset;
981 test_affine_hull_case(ctx, "affine2");
982 test_affine_hull_case(ctx, "affine");
983 test_affine_hull_case(ctx, "affine3");
985 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
986 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
987 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
988 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
989 set = isl_set_read_from_str(ctx, str);
990 bset = isl_set_affine_hull(set);
991 n = isl_basic_set_dim(bset, isl_dim_div);
992 isl_basic_set_free(bset);
993 if (n != 0)
994 isl_die(ctx, isl_error_unknown, "not expecting any divs",
995 return -1);
997 /* Check that isl_map_affine_hull is not confused by
998 * the reordering of divs in isl_map_align_divs.
1000 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1001 "32e0 = b and 32e1 = c); "
1002 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1003 set = isl_set_read_from_str(ctx, str);
1004 bset = isl_set_affine_hull(set);
1005 isl_basic_set_free(bset);
1006 if (!bset)
1007 return -1;
1009 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1010 "32e2 = 31 + 31e0 }";
1011 set = isl_set_read_from_str(ctx, str);
1012 bset = isl_set_affine_hull(set);
1013 str = "{ [a] : exists e : a = 32 e }";
1014 bset2 = isl_basic_set_read_from_str(ctx, str);
1015 subset = isl_basic_set_is_subset(bset, bset2);
1016 isl_basic_set_free(bset);
1017 isl_basic_set_free(bset2);
1018 if (subset < 0)
1019 return -1;
1020 if (!subset)
1021 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1022 return -1);
1024 return 0;
1027 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1029 char *filename;
1030 FILE *input;
1031 struct isl_basic_set *bset1, *bset2;
1032 struct isl_set *set;
1034 filename = get_filename(ctx, name, "polylib");
1035 assert(filename);
1036 input = fopen(filename, "r");
1037 assert(input);
1039 bset1 = isl_basic_set_read_from_file(ctx, input);
1040 bset2 = isl_basic_set_read_from_file(ctx, input);
1042 set = isl_basic_set_union(bset1, bset2);
1043 bset1 = isl_set_convex_hull(set);
1045 bset2 = isl_basic_set_read_from_file(ctx, input);
1047 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1049 isl_basic_set_free(bset1);
1050 isl_basic_set_free(bset2);
1051 free(filename);
1053 fclose(input);
1056 struct {
1057 const char *set;
1058 const char *hull;
1059 } convex_hull_tests[] = {
1060 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1061 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1062 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1063 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1064 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1065 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1066 "i2 <= 5 and i2 >= 4; "
1067 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1068 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1069 "i2 <= 5 + i0 and i2 >= i0 }" },
1070 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1071 "{ [x, y] : 1 = 0 }" },
1074 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
1076 int i;
1077 int orig_convex = ctx->opt->convex;
1078 ctx->opt->convex = convex;
1080 test_convex_hull_case(ctx, "convex0");
1081 test_convex_hull_case(ctx, "convex1");
1082 test_convex_hull_case(ctx, "convex2");
1083 test_convex_hull_case(ctx, "convex3");
1084 test_convex_hull_case(ctx, "convex4");
1085 test_convex_hull_case(ctx, "convex5");
1086 test_convex_hull_case(ctx, "convex6");
1087 test_convex_hull_case(ctx, "convex7");
1088 test_convex_hull_case(ctx, "convex8");
1089 test_convex_hull_case(ctx, "convex9");
1090 test_convex_hull_case(ctx, "convex10");
1091 test_convex_hull_case(ctx, "convex11");
1092 test_convex_hull_case(ctx, "convex12");
1093 test_convex_hull_case(ctx, "convex13");
1094 test_convex_hull_case(ctx, "convex14");
1095 test_convex_hull_case(ctx, "convex15");
1097 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1098 isl_set *set1, *set2;
1100 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1101 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1102 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1103 assert(isl_set_is_equal(set1, set2));
1104 isl_set_free(set1);
1105 isl_set_free(set2);
1108 ctx->opt->convex = orig_convex;
1111 void test_convex_hull(struct isl_ctx *ctx)
1113 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
1114 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
1117 void test_gist_case(struct isl_ctx *ctx, const char *name)
1119 char *filename;
1120 FILE *input;
1121 struct isl_basic_set *bset1, *bset2;
1123 filename = get_filename(ctx, name, "polylib");
1124 assert(filename);
1125 input = fopen(filename, "r");
1126 assert(input);
1128 bset1 = isl_basic_set_read_from_file(ctx, input);
1129 bset2 = isl_basic_set_read_from_file(ctx, input);
1131 bset1 = isl_basic_set_gist(bset1, bset2);
1133 bset2 = isl_basic_set_read_from_file(ctx, input);
1135 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1137 isl_basic_set_free(bset1);
1138 isl_basic_set_free(bset2);
1139 free(filename);
1141 fclose(input);
1144 static int test_gist(struct isl_ctx *ctx)
1146 const char *str;
1147 isl_basic_set *bset1, *bset2;
1148 isl_map *map1, *map2;
1150 test_gist_case(ctx, "gist1");
1152 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1153 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1154 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1155 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1156 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1157 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1158 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1159 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1160 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1161 bset1 = isl_basic_set_read_from_str(ctx, str);
1162 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1163 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1164 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1165 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1166 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1167 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1168 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1169 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1170 bset2 = isl_basic_set_read_from_str(ctx, str);
1171 bset1 = isl_basic_set_gist(bset1, bset2);
1172 assert(bset1 && bset1->n_div == 0);
1173 isl_basic_set_free(bset1);
1175 /* Check that the integer divisions of the second disjunct
1176 * do not spread to the first disjunct.
1178 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1179 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1180 "(exists (e0 = [(-1 + t1)/16], "
1181 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1182 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1183 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1184 "o0 <= 4294967295 and t1 <= -1)) }";
1185 map1 = isl_map_read_from_str(ctx, str);
1186 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1187 map2 = isl_map_read_from_str(ctx, str);
1188 map1 = isl_map_gist(map1, map2);
1189 if (!map1)
1190 return -1;
1191 if (map1->n != 1)
1192 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1193 isl_map_free(map1); return -1);
1194 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1195 isl_die(ctx, isl_error_unknown, "expecting single div",
1196 isl_map_free(map1); return -1);
1197 isl_map_free(map1);
1199 return 0;
1202 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1204 isl_set *set, *set2;
1205 int equal;
1206 int one;
1208 set = isl_set_read_from_str(ctx, str);
1209 set = isl_set_coalesce(set);
1210 set2 = isl_set_read_from_str(ctx, str);
1211 equal = isl_set_is_equal(set, set2);
1212 one = set && set->n == 1;
1213 isl_set_free(set);
1214 isl_set_free(set2);
1216 if (equal < 0)
1217 return -1;
1218 if (!equal)
1219 isl_die(ctx, isl_error_unknown,
1220 "coalesced set not equal to input", return -1);
1221 if (check_one && !one)
1222 isl_die(ctx, isl_error_unknown,
1223 "coalesced set should not be a union", return -1);
1225 return 0;
1228 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1230 int r = 0;
1231 int bounded;
1233 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1234 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1236 if (test_coalesce_set(ctx,
1237 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1238 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1239 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1240 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1241 "-10 <= y <= 0}", 1) < 0)
1242 goto error;
1243 if (test_coalesce_set(ctx,
1244 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
1245 goto error;
1246 if (test_coalesce_set(ctx,
1247 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
1248 goto error;
1250 if (0) {
1251 error:
1252 r = -1;
1255 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1257 return r;
1260 /* Inputs for coalescing tests.
1261 * "str" is a string representation of the input set.
1262 * "single_disjunct" is set if we expect the result to consist of
1263 * a single disjunct.
1265 struct {
1266 int single_disjunct;
1267 const char *str;
1268 } coalesce_tests[] = {
1269 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1270 "y >= x & x >= 2 & 5 >= y }" },
1271 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1272 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1273 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1274 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1275 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1276 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1277 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1278 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1279 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1280 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1281 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1282 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1283 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1284 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1285 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1286 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1287 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1288 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1289 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1290 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1291 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1292 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1293 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1294 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1295 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1296 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1297 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1298 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1299 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1300 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1301 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1302 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1303 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1304 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1305 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1306 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1307 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1308 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1309 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1310 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1311 "[o0, o1, o2, o3, o4, o5, o6]] : "
1312 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1313 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1314 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1315 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1316 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1317 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1318 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1319 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1320 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1321 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1322 "o6 >= i3 + i6 - o3 and M >= 0 and "
1323 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1324 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1325 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1326 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1327 "(o0 = 0 and M >= 2 and N >= 3) or "
1328 "(M = 0 and o0 = 0 and N >= 3) }" },
1329 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1330 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1331 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1332 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1333 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1334 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1335 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1336 "(y = 3 and x = 1) }" },
1337 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1338 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1339 "i1 <= M and i3 <= M and i4 <= M) or "
1340 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1341 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1342 "i4 <= -1 + M) }" },
1343 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1344 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1345 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1346 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1347 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1348 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1349 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1350 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1351 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1352 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1353 { 0, "{ [a, b] : exists e : 2e = a and "
1354 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1355 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1356 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1357 "j >= 1 and j' <= i + j - i' and i >= 1; "
1358 "[1, 1, 1, 1] }" },
1359 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1360 "[i,j] : exists a : j = 3a }" },
1361 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1362 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1363 "a >= 3) or "
1364 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1365 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1366 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1367 "c <= 6 + 8a and a >= 3; "
1368 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1369 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1370 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1371 "[x,0] : 3 <= x <= 4 }" },
1372 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1373 "[x,0] : 4 <= x <= 5 }" },
1374 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1375 "[x,0] : 3 <= x <= 5 }" },
1376 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1377 "[x,0] : 3 <= x <= 4 }" },
1378 { 1 , "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1379 "i1 <= 0; "
1380 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1383 /* Test the functionality of isl_set_coalesce.
1384 * That is, check that the output is always equal to the input
1385 * and in some cases that the result consists of a single disjunct.
1387 static int test_coalesce(struct isl_ctx *ctx)
1389 int i;
1391 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1392 const char *str = coalesce_tests[i].str;
1393 int check_one = coalesce_tests[i].single_disjunct;
1394 if (test_coalesce_set(ctx, str, check_one) < 0)
1395 return -1;
1398 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1399 return -1;
1401 return 0;
1404 void test_closure(struct isl_ctx *ctx)
1406 const char *str;
1407 isl_set *dom;
1408 isl_map *up, *right;
1409 isl_map *map, *map2;
1410 int exact;
1412 /* COCOA example 1 */
1413 map = isl_map_read_from_str(ctx,
1414 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1415 "1 <= i and i < n and 1 <= j and j < n or "
1416 "i2 = i + 1 and j2 = j - 1 and "
1417 "1 <= i and i < n and 2 <= j and j <= n }");
1418 map = isl_map_power(map, &exact);
1419 assert(exact);
1420 isl_map_free(map);
1422 /* COCOA example 1 */
1423 map = isl_map_read_from_str(ctx,
1424 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1425 "1 <= i and i < n and 1 <= j and j < n or "
1426 "i2 = i + 1 and j2 = j - 1 and "
1427 "1 <= i and i < n and 2 <= j and j <= n }");
1428 map = isl_map_transitive_closure(map, &exact);
1429 assert(exact);
1430 map2 = isl_map_read_from_str(ctx,
1431 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1432 "1 <= i and i < n and 1 <= j and j <= n and "
1433 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1434 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1435 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1436 assert(isl_map_is_equal(map, map2));
1437 isl_map_free(map2);
1438 isl_map_free(map);
1440 map = isl_map_read_from_str(ctx,
1441 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1442 " 0 <= y and y <= n }");
1443 map = isl_map_transitive_closure(map, &exact);
1444 map2 = isl_map_read_from_str(ctx,
1445 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1446 " 0 <= y and y <= n }");
1447 assert(isl_map_is_equal(map, map2));
1448 isl_map_free(map2);
1449 isl_map_free(map);
1451 /* COCOA example 2 */
1452 map = isl_map_read_from_str(ctx,
1453 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1454 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1455 "i2 = i + 2 and j2 = j - 2 and "
1456 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1457 map = isl_map_transitive_closure(map, &exact);
1458 assert(exact);
1459 map2 = isl_map_read_from_str(ctx,
1460 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1461 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1462 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1463 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1464 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1465 assert(isl_map_is_equal(map, map2));
1466 isl_map_free(map);
1467 isl_map_free(map2);
1469 /* COCOA Fig.2 left */
1470 map = isl_map_read_from_str(ctx,
1471 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1472 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1473 "j <= n or "
1474 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1475 "j <= 2 i - 3 and j <= n - 2 or "
1476 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1477 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1478 map = isl_map_transitive_closure(map, &exact);
1479 assert(exact);
1480 isl_map_free(map);
1482 /* COCOA Fig.2 right */
1483 map = isl_map_read_from_str(ctx,
1484 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1485 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1486 "j <= n or "
1487 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1488 "j <= 2 i - 4 and j <= n - 3 or "
1489 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1490 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1491 map = isl_map_power(map, &exact);
1492 assert(exact);
1493 isl_map_free(map);
1495 /* COCOA Fig.2 right */
1496 map = isl_map_read_from_str(ctx,
1497 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1498 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1499 "j <= n or "
1500 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1501 "j <= 2 i - 4 and j <= n - 3 or "
1502 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1503 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1504 map = isl_map_transitive_closure(map, &exact);
1505 assert(exact);
1506 map2 = isl_map_read_from_str(ctx,
1507 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1508 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1509 "j <= n and 3 + i + 2 j <= 3 n and "
1510 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1511 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1512 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1513 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1514 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1515 assert(isl_map_is_equal(map, map2));
1516 isl_map_free(map2);
1517 isl_map_free(map);
1519 /* COCOA Fig.1 right */
1520 dom = isl_set_read_from_str(ctx,
1521 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1522 "2 x - 3 y + 3 >= 0 }");
1523 right = isl_map_read_from_str(ctx,
1524 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1525 up = isl_map_read_from_str(ctx,
1526 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1527 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1528 right = isl_map_intersect_range(right, isl_set_copy(dom));
1529 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1530 up = isl_map_intersect_range(up, dom);
1531 map = isl_map_union(up, right);
1532 map = isl_map_transitive_closure(map, &exact);
1533 assert(exact);
1534 map2 = isl_map_read_from_str(ctx,
1535 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1536 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1537 assert(isl_map_is_equal(map, map2));
1538 isl_map_free(map2);
1539 isl_map_free(map);
1541 /* COCOA Theorem 1 counter example */
1542 map = isl_map_read_from_str(ctx,
1543 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1544 "i2 = 1 and j2 = j or "
1545 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1546 map = isl_map_transitive_closure(map, &exact);
1547 assert(exact);
1548 isl_map_free(map);
1550 map = isl_map_read_from_str(ctx,
1551 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1552 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1553 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1554 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1555 map = isl_map_transitive_closure(map, &exact);
1556 assert(exact);
1557 isl_map_free(map);
1559 /* Kelly et al 1996, fig 12 */
1560 map = isl_map_read_from_str(ctx,
1561 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1562 "1 <= i,j,j+1 <= n or "
1563 "j = n and j2 = 1 and i2 = i + 1 and "
1564 "1 <= i,i+1 <= n }");
1565 map = isl_map_transitive_closure(map, &exact);
1566 assert(exact);
1567 map2 = isl_map_read_from_str(ctx,
1568 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1569 "1 <= i <= n and i = i2 or "
1570 "1 <= i < i2 <= n and 1 <= j <= n and "
1571 "1 <= j2 <= n }");
1572 assert(isl_map_is_equal(map, map2));
1573 isl_map_free(map2);
1574 isl_map_free(map);
1576 /* Omega's closure4 */
1577 map = isl_map_read_from_str(ctx,
1578 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1579 "1 <= x,y <= 10 or "
1580 "x2 = x + 1 and y2 = y and "
1581 "1 <= x <= 20 && 5 <= y <= 15 }");
1582 map = isl_map_transitive_closure(map, &exact);
1583 assert(exact);
1584 isl_map_free(map);
1586 map = isl_map_read_from_str(ctx,
1587 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1588 map = isl_map_transitive_closure(map, &exact);
1589 assert(!exact);
1590 map2 = isl_map_read_from_str(ctx,
1591 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1592 assert(isl_map_is_equal(map, map2));
1593 isl_map_free(map);
1594 isl_map_free(map2);
1596 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1597 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1598 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1599 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1600 map = isl_map_read_from_str(ctx, str);
1601 map = isl_map_transitive_closure(map, &exact);
1602 assert(exact);
1603 map2 = isl_map_read_from_str(ctx, str);
1604 assert(isl_map_is_equal(map, map2));
1605 isl_map_free(map);
1606 isl_map_free(map2);
1608 str = "{[0] -> [1]; [2] -> [3]}";
1609 map = isl_map_read_from_str(ctx, str);
1610 map = isl_map_transitive_closure(map, &exact);
1611 assert(exact);
1612 map2 = isl_map_read_from_str(ctx, str);
1613 assert(isl_map_is_equal(map, map2));
1614 isl_map_free(map);
1615 isl_map_free(map2);
1617 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1618 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1619 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1620 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1621 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1622 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1623 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1624 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1625 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1626 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1627 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1628 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1629 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1630 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1631 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1632 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1633 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1634 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1635 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1636 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1637 map = isl_map_read_from_str(ctx, str);
1638 map = isl_map_transitive_closure(map, NULL);
1639 assert(map);
1640 isl_map_free(map);
1643 void test_lex(struct isl_ctx *ctx)
1645 isl_space *dim;
1646 isl_map *map;
1648 dim = isl_space_set_alloc(ctx, 0, 0);
1649 map = isl_map_lex_le(dim);
1650 assert(!isl_map_is_empty(map));
1651 isl_map_free(map);
1654 static int test_lexmin(struct isl_ctx *ctx)
1656 int equal;
1657 const char *str;
1658 isl_basic_map *bmap;
1659 isl_map *map, *map2;
1660 isl_set *set;
1661 isl_set *set2;
1662 isl_pw_multi_aff *pma;
1664 str = "[p0, p1] -> { [] -> [] : "
1665 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1666 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1667 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1668 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1669 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1670 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1671 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1672 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1673 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1674 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1675 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1676 map = isl_map_read_from_str(ctx, str);
1677 map = isl_map_lexmin(map);
1678 isl_map_free(map);
1680 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1681 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1682 set = isl_set_read_from_str(ctx, str);
1683 set = isl_set_lexmax(set);
1684 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1685 set2 = isl_set_read_from_str(ctx, str);
1686 set = isl_set_intersect(set, set2);
1687 assert(!isl_set_is_empty(set));
1688 isl_set_free(set);
1690 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1691 map = isl_map_read_from_str(ctx, str);
1692 map = isl_map_lexmin(map);
1693 str = "{ [x] -> [5] : 6 <= x <= 8; "
1694 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1695 map2 = isl_map_read_from_str(ctx, str);
1696 assert(isl_map_is_equal(map, map2));
1697 isl_map_free(map);
1698 isl_map_free(map2);
1700 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1701 map = isl_map_read_from_str(ctx, str);
1702 map2 = isl_map_copy(map);
1703 map = isl_map_lexmin(map);
1704 assert(isl_map_is_equal(map, map2));
1705 isl_map_free(map);
1706 isl_map_free(map2);
1708 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1709 map = isl_map_read_from_str(ctx, str);
1710 map = isl_map_lexmin(map);
1711 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1712 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1713 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1714 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1715 map2 = isl_map_read_from_str(ctx, str);
1716 assert(isl_map_is_equal(map, map2));
1717 isl_map_free(map);
1718 isl_map_free(map2);
1720 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1721 " 8i' <= i and 8i' >= -7 + i }";
1722 bmap = isl_basic_map_read_from_str(ctx, str);
1723 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1724 map2 = isl_map_from_pw_multi_aff(pma);
1725 map = isl_map_from_basic_map(bmap);
1726 assert(isl_map_is_equal(map, map2));
1727 isl_map_free(map);
1728 isl_map_free(map2);
1730 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1731 map = isl_map_read_from_str(ctx, str);
1732 map = isl_map_lexmin(map);
1733 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1734 map2 = isl_map_read_from_str(ctx, str);
1735 assert(isl_map_is_equal(map, map2));
1736 isl_map_free(map);
1737 isl_map_free(map2);
1739 /* Check that empty pieces are properly combined. */
1740 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1741 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1742 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1743 map = isl_map_read_from_str(ctx, str);
1744 map = isl_map_lexmin(map);
1745 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1746 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1747 "x >= 4 }";
1748 map2 = isl_map_read_from_str(ctx, str);
1749 assert(isl_map_is_equal(map, map2));
1750 isl_map_free(map);
1751 isl_map_free(map2);
1753 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1754 " 8i' <= i and 8i' >= -7 + i }";
1755 set = isl_set_read_from_str(ctx, str);
1756 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1757 set2 = isl_set_from_pw_multi_aff(pma);
1758 equal = isl_set_is_equal(set, set2);
1759 isl_set_free(set);
1760 isl_set_free(set2);
1761 if (equal < 0)
1762 return -1;
1763 if (!equal)
1764 isl_die(ctx, isl_error_unknown,
1765 "unexpected difference between set and "
1766 "piecewise affine expression", return -1);
1768 return 0;
1771 /* Check that isl_set_min_val and isl_set_max_val compute the correct
1772 * result on non-convex inputs.
1774 static int test_min(struct isl_ctx *ctx)
1776 isl_set *set;
1777 isl_aff *aff;
1778 isl_val *val;
1779 int min_ok, max_ok;
1781 set = isl_set_read_from_str(ctx, "{ [-1]; [1] }");
1782 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x] }");
1783 val = isl_set_min_val(set, aff);
1784 min_ok = isl_val_is_negone(val);
1785 isl_val_free(val);
1786 val = isl_set_max_val(set, aff);
1787 max_ok = isl_val_is_one(val);
1788 isl_val_free(val);
1789 isl_aff_free(aff);
1790 isl_set_free(set);
1792 if (min_ok < 0 || max_ok < 0)
1793 return -1;
1794 if (!min_ok)
1795 isl_die(ctx, isl_error_unknown,
1796 "unexpected minimum", return -1);
1797 if (!max_ok)
1798 isl_die(ctx, isl_error_unknown,
1799 "unexpected maximum", return -1);
1801 return 0;
1804 struct must_may {
1805 isl_map *must;
1806 isl_map *may;
1809 static int collect_must_may(__isl_take isl_map *dep, int must,
1810 void *dep_user, void *user)
1812 struct must_may *mm = (struct must_may *)user;
1814 if (must)
1815 mm->must = isl_map_union(mm->must, dep);
1816 else
1817 mm->may = isl_map_union(mm->may, dep);
1819 return 0;
1822 static int common_space(void *first, void *second)
1824 int depth = *(int *)first;
1825 return 2 * depth;
1828 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1830 isl_map *map2;
1831 int equal;
1833 if (!map)
1834 return -1;
1836 map2 = isl_map_read_from_str(map->ctx, str);
1837 equal = isl_map_is_equal(map, map2);
1838 isl_map_free(map2);
1840 return equal;
1843 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1845 int equal;
1847 equal = map_is_equal(map, str);
1848 if (equal < 0)
1849 return -1;
1850 if (!equal)
1851 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1852 "result not as expected", return -1);
1853 return 0;
1856 void test_dep(struct isl_ctx *ctx)
1858 const char *str;
1859 isl_space *dim;
1860 isl_map *map;
1861 isl_access_info *ai;
1862 isl_flow *flow;
1863 int depth;
1864 struct must_may mm;
1866 depth = 3;
1868 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1869 map = isl_map_read_from_str(ctx, str);
1870 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1872 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1873 map = isl_map_read_from_str(ctx, str);
1874 ai = isl_access_info_add_source(ai, map, 1, &depth);
1876 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1877 map = isl_map_read_from_str(ctx, str);
1878 ai = isl_access_info_add_source(ai, map, 1, &depth);
1880 flow = isl_access_info_compute_flow(ai);
1881 dim = isl_space_alloc(ctx, 0, 3, 3);
1882 mm.must = isl_map_empty(isl_space_copy(dim));
1883 mm.may = isl_map_empty(dim);
1885 isl_flow_foreach(flow, collect_must_may, &mm);
1887 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1888 " [1,10,0] -> [2,5,0] }";
1889 assert(map_is_equal(mm.must, str));
1890 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1891 assert(map_is_equal(mm.may, str));
1893 isl_map_free(mm.must);
1894 isl_map_free(mm.may);
1895 isl_flow_free(flow);
1898 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1899 map = isl_map_read_from_str(ctx, str);
1900 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1902 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1903 map = isl_map_read_from_str(ctx, str);
1904 ai = isl_access_info_add_source(ai, map, 1, &depth);
1906 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1907 map = isl_map_read_from_str(ctx, str);
1908 ai = isl_access_info_add_source(ai, map, 0, &depth);
1910 flow = isl_access_info_compute_flow(ai);
1911 dim = isl_space_alloc(ctx, 0, 3, 3);
1912 mm.must = isl_map_empty(isl_space_copy(dim));
1913 mm.may = isl_map_empty(dim);
1915 isl_flow_foreach(flow, collect_must_may, &mm);
1917 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1918 assert(map_is_equal(mm.must, str));
1919 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1920 assert(map_is_equal(mm.may, str));
1922 isl_map_free(mm.must);
1923 isl_map_free(mm.may);
1924 isl_flow_free(flow);
1927 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1928 map = isl_map_read_from_str(ctx, str);
1929 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1931 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1932 map = isl_map_read_from_str(ctx, str);
1933 ai = isl_access_info_add_source(ai, map, 0, &depth);
1935 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1936 map = isl_map_read_from_str(ctx, str);
1937 ai = isl_access_info_add_source(ai, map, 0, &depth);
1939 flow = isl_access_info_compute_flow(ai);
1940 dim = isl_space_alloc(ctx, 0, 3, 3);
1941 mm.must = isl_map_empty(isl_space_copy(dim));
1942 mm.may = isl_map_empty(dim);
1944 isl_flow_foreach(flow, collect_must_may, &mm);
1946 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1947 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1948 assert(map_is_equal(mm.may, str));
1949 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1950 assert(map_is_equal(mm.must, str));
1952 isl_map_free(mm.must);
1953 isl_map_free(mm.may);
1954 isl_flow_free(flow);
1957 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1958 map = isl_map_read_from_str(ctx, str);
1959 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1961 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1962 map = isl_map_read_from_str(ctx, str);
1963 ai = isl_access_info_add_source(ai, map, 0, &depth);
1965 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1966 map = isl_map_read_from_str(ctx, str);
1967 ai = isl_access_info_add_source(ai, map, 0, &depth);
1969 flow = isl_access_info_compute_flow(ai);
1970 dim = isl_space_alloc(ctx, 0, 3, 3);
1971 mm.must = isl_map_empty(isl_space_copy(dim));
1972 mm.may = isl_map_empty(dim);
1974 isl_flow_foreach(flow, collect_must_may, &mm);
1976 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1977 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1978 assert(map_is_equal(mm.may, str));
1979 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1980 assert(map_is_equal(mm.must, str));
1982 isl_map_free(mm.must);
1983 isl_map_free(mm.may);
1984 isl_flow_free(flow);
1987 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1988 map = isl_map_read_from_str(ctx, str);
1989 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1991 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1992 map = isl_map_read_from_str(ctx, str);
1993 ai = isl_access_info_add_source(ai, map, 0, &depth);
1995 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1996 map = isl_map_read_from_str(ctx, str);
1997 ai = isl_access_info_add_source(ai, map, 0, &depth);
1999 flow = isl_access_info_compute_flow(ai);
2000 dim = isl_space_alloc(ctx, 0, 3, 3);
2001 mm.must = isl_map_empty(isl_space_copy(dim));
2002 mm.may = isl_map_empty(dim);
2004 isl_flow_foreach(flow, collect_must_may, &mm);
2006 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2007 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2008 assert(map_is_equal(mm.may, str));
2009 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2010 assert(map_is_equal(mm.must, str));
2012 isl_map_free(mm.must);
2013 isl_map_free(mm.may);
2014 isl_flow_free(flow);
2017 depth = 5;
2019 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2020 map = isl_map_read_from_str(ctx, str);
2021 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2023 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2024 map = isl_map_read_from_str(ctx, str);
2025 ai = isl_access_info_add_source(ai, map, 1, &depth);
2027 flow = isl_access_info_compute_flow(ai);
2028 dim = isl_space_alloc(ctx, 0, 5, 5);
2029 mm.must = isl_map_empty(isl_space_copy(dim));
2030 mm.may = isl_map_empty(dim);
2032 isl_flow_foreach(flow, collect_must_may, &mm);
2034 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2035 assert(map_is_equal(mm.must, str));
2036 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2037 assert(map_is_equal(mm.may, str));
2039 isl_map_free(mm.must);
2040 isl_map_free(mm.may);
2041 isl_flow_free(flow);
2044 /* Check that the dependence analysis proceeds without errors.
2045 * Earlier versions of isl would break down during the analysis
2046 * due to the use of the wrong spaces.
2048 static int test_flow(isl_ctx *ctx)
2050 const char *str;
2051 isl_union_map *access, *schedule;
2052 isl_union_map *must_dep, *may_dep;
2053 int r;
2055 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2056 access = isl_union_map_read_from_str(ctx, str);
2057 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2058 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2059 "S2[] -> [1,0,0,0]; "
2060 "S3[] -> [-1,0,0,0] }";
2061 schedule = isl_union_map_read_from_str(ctx, str);
2062 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2063 isl_union_map_copy(access), schedule,
2064 &must_dep, &may_dep, NULL, NULL);
2065 isl_union_map_free(may_dep);
2066 isl_union_map_free(must_dep);
2068 return r;
2071 int test_sv(isl_ctx *ctx)
2073 const char *str;
2074 isl_map *map;
2075 isl_union_map *umap;
2076 int sv;
2078 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
2079 map = isl_map_read_from_str(ctx, str);
2080 sv = isl_map_is_single_valued(map);
2081 isl_map_free(map);
2082 if (sv < 0)
2083 return -1;
2084 if (!sv)
2085 isl_die(ctx, isl_error_internal,
2086 "map not detected as single valued", return -1);
2088 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
2089 map = isl_map_read_from_str(ctx, str);
2090 sv = isl_map_is_single_valued(map);
2091 isl_map_free(map);
2092 if (sv < 0)
2093 return -1;
2094 if (sv)
2095 isl_die(ctx, isl_error_internal,
2096 "map detected as single valued", return -1);
2098 str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
2099 umap = isl_union_map_read_from_str(ctx, str);
2100 sv = isl_union_map_is_single_valued(umap);
2101 isl_union_map_free(umap);
2102 if (sv < 0)
2103 return -1;
2104 if (!sv)
2105 isl_die(ctx, isl_error_internal,
2106 "map not detected as single valued", return -1);
2108 str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
2109 umap = isl_union_map_read_from_str(ctx, str);
2110 sv = isl_union_map_is_single_valued(umap);
2111 isl_union_map_free(umap);
2112 if (sv < 0)
2113 return -1;
2114 if (sv)
2115 isl_die(ctx, isl_error_internal,
2116 "map detected as single valued", return -1);
2118 return 0;
2121 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
2123 isl_map *map;
2125 map = isl_map_read_from_str(ctx, str);
2126 if (bijective)
2127 assert(isl_map_is_bijective(map));
2128 else
2129 assert(!isl_map_is_bijective(map));
2130 isl_map_free(map);
2133 void test_bijective(struct isl_ctx *ctx)
2135 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
2136 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
2137 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
2138 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
2139 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
2140 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
2141 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
2142 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
2143 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
2144 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
2145 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
2146 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
2147 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
2150 static int test_pwqp(struct isl_ctx *ctx)
2152 const char *str;
2153 isl_set *set;
2154 isl_pw_qpolynomial *pwqp1, *pwqp2;
2155 int equal;
2157 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2158 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2160 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2161 isl_dim_in, 1, 1);
2163 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2164 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2166 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2168 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2170 isl_pw_qpolynomial_free(pwqp1);
2172 str = "{ [i] -> i }";
2173 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2174 str = "{ [k] : exists a : k = 2a }";
2175 set = isl_set_read_from_str(ctx, str);
2176 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2177 str = "{ [i] -> i }";
2178 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2180 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2182 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2184 isl_pw_qpolynomial_free(pwqp1);
2186 str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
2187 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2188 str = "{ [10] }";
2189 set = isl_set_read_from_str(ctx, str);
2190 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2191 str = "{ [i] -> 16 }";
2192 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2194 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2196 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2198 isl_pw_qpolynomial_free(pwqp1);
2200 str = "{ [i] -> ([(i)/2]) }";
2201 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2202 str = "{ [k] : exists a : k = 2a+1 }";
2203 set = isl_set_read_from_str(ctx, str);
2204 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2205 str = "{ [i] -> -1/2 + 1/2 * i }";
2206 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2208 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2210 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2212 isl_pw_qpolynomial_free(pwqp1);
2214 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2215 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2216 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2217 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2219 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2221 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2223 isl_pw_qpolynomial_free(pwqp1);
2225 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2226 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2227 str = "{ [x] -> x }";
2228 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2230 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2232 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2234 isl_pw_qpolynomial_free(pwqp1);
2236 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2237 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2238 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2239 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2240 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2241 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2242 isl_pw_qpolynomial_free(pwqp1);
2244 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2245 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2246 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2247 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2248 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2249 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2250 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2251 isl_pw_qpolynomial_free(pwqp1);
2252 isl_pw_qpolynomial_free(pwqp2);
2253 if (equal < 0)
2254 return -1;
2255 if (!equal)
2256 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2258 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2259 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2260 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2261 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2262 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2263 isl_val_one(ctx));
2264 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2265 isl_pw_qpolynomial_free(pwqp1);
2266 isl_pw_qpolynomial_free(pwqp2);
2267 if (equal < 0)
2268 return -1;
2269 if (!equal)
2270 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2272 return 0;
2275 void test_split_periods(isl_ctx *ctx)
2277 const char *str;
2278 isl_pw_qpolynomial *pwqp;
2280 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2281 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2282 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2283 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2285 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2286 assert(pwqp);
2288 isl_pw_qpolynomial_free(pwqp);
2291 void test_union(isl_ctx *ctx)
2293 const char *str;
2294 isl_union_set *uset1, *uset2;
2295 isl_union_map *umap1, *umap2;
2297 str = "{ [i] : 0 <= i <= 1 }";
2298 uset1 = isl_union_set_read_from_str(ctx, str);
2299 str = "{ [1] -> [0] }";
2300 umap1 = isl_union_map_read_from_str(ctx, str);
2302 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2303 assert(isl_union_map_is_equal(umap1, umap2));
2305 isl_union_map_free(umap1);
2306 isl_union_map_free(umap2);
2308 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2309 umap1 = isl_union_map_read_from_str(ctx, str);
2310 str = "{ A[i]; B[i] }";
2311 uset1 = isl_union_set_read_from_str(ctx, str);
2313 uset2 = isl_union_map_domain(umap1);
2315 assert(isl_union_set_is_equal(uset1, uset2));
2317 isl_union_set_free(uset1);
2318 isl_union_set_free(uset2);
2321 void test_bound(isl_ctx *ctx)
2323 const char *str;
2324 isl_pw_qpolynomial *pwqp;
2325 isl_pw_qpolynomial_fold *pwf;
2327 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2328 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2329 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2330 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
2331 isl_pw_qpolynomial_fold_free(pwf);
2333 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2334 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2335 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2336 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
2337 isl_pw_qpolynomial_fold_free(pwf);
2340 void test_lift(isl_ctx *ctx)
2342 const char *str;
2343 isl_basic_map *bmap;
2344 isl_basic_set *bset;
2346 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2347 bset = isl_basic_set_read_from_str(ctx, str);
2348 bset = isl_basic_set_lift(bset);
2349 bmap = isl_basic_map_from_range(bset);
2350 bset = isl_basic_map_domain(bmap);
2351 isl_basic_set_free(bset);
2354 struct {
2355 const char *set1;
2356 const char *set2;
2357 int subset;
2358 } subset_tests[] = {
2359 { "{ [112, 0] }",
2360 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2361 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2362 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2363 { "{ [65] }",
2364 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2365 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2366 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2367 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2368 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2369 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2370 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2371 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2372 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2373 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2374 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2375 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2376 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2377 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
2378 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
2379 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
2380 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
2381 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
2382 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
2383 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
2384 "4e0 <= -57 + i0 + i1)) or "
2385 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
2386 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
2387 "4e0 >= -61 + i0 + i1)) or "
2388 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
2391 static int test_subset(isl_ctx *ctx)
2393 int i;
2394 isl_set *set1, *set2;
2395 int subset;
2397 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2398 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2399 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2400 subset = isl_set_is_subset(set1, set2);
2401 isl_set_free(set1);
2402 isl_set_free(set2);
2403 if (subset < 0)
2404 return -1;
2405 if (subset != subset_tests[i].subset)
2406 isl_die(ctx, isl_error_unknown,
2407 "incorrect subset result", return -1);
2410 return 0;
2413 struct {
2414 const char *minuend;
2415 const char *subtrahend;
2416 const char *difference;
2417 } subtract_domain_tests[] = {
2418 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2419 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2420 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2423 static int test_subtract(isl_ctx *ctx)
2425 int i;
2426 isl_union_map *umap1, *umap2;
2427 isl_union_set *uset;
2428 int equal;
2430 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2431 umap1 = isl_union_map_read_from_str(ctx,
2432 subtract_domain_tests[i].minuend);
2433 uset = isl_union_set_read_from_str(ctx,
2434 subtract_domain_tests[i].subtrahend);
2435 umap2 = isl_union_map_read_from_str(ctx,
2436 subtract_domain_tests[i].difference);
2437 umap1 = isl_union_map_subtract_domain(umap1, uset);
2438 equal = isl_union_map_is_equal(umap1, umap2);
2439 isl_union_map_free(umap1);
2440 isl_union_map_free(umap2);
2441 if (equal < 0)
2442 return -1;
2443 if (!equal)
2444 isl_die(ctx, isl_error_unknown,
2445 "incorrect subtract domain result", return -1);
2448 return 0;
2451 int test_factorize(isl_ctx *ctx)
2453 const char *str;
2454 isl_basic_set *bset;
2455 isl_factorizer *f;
2457 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2458 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2459 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2460 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2461 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2462 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2463 "3i5 >= -2i0 - i2 + 3i4 }";
2464 bset = isl_basic_set_read_from_str(ctx, str);
2465 f = isl_basic_set_factorizer(bset);
2466 isl_basic_set_free(bset);
2467 isl_factorizer_free(f);
2468 if (!f)
2469 isl_die(ctx, isl_error_unknown,
2470 "failed to construct factorizer", return -1);
2472 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2473 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2474 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2475 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2476 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2477 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2478 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2479 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2480 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2481 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2482 bset = isl_basic_set_read_from_str(ctx, str);
2483 f = isl_basic_set_factorizer(bset);
2484 isl_basic_set_free(bset);
2485 isl_factorizer_free(f);
2486 if (!f)
2487 isl_die(ctx, isl_error_unknown,
2488 "failed to construct factorizer", return -1);
2490 return 0;
2493 static int check_injective(__isl_take isl_map *map, void *user)
2495 int *injective = user;
2497 *injective = isl_map_is_injective(map);
2498 isl_map_free(map);
2500 if (*injective < 0 || !*injective)
2501 return -1;
2503 return 0;
2506 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2507 const char *r, const char *s, int tilable, int parallel)
2509 int i;
2510 isl_union_set *D;
2511 isl_union_map *W, *R, *S;
2512 isl_union_map *empty;
2513 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2514 isl_union_map *validity, *proximity, *coincidence;
2515 isl_union_map *schedule;
2516 isl_union_map *test;
2517 isl_union_set *delta;
2518 isl_union_set *domain;
2519 isl_set *delta_set;
2520 isl_set *slice;
2521 isl_set *origin;
2522 isl_schedule_constraints *sc;
2523 isl_schedule *sched;
2524 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2526 D = isl_union_set_read_from_str(ctx, d);
2527 W = isl_union_map_read_from_str(ctx, w);
2528 R = isl_union_map_read_from_str(ctx, r);
2529 S = isl_union_map_read_from_str(ctx, s);
2531 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2532 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2534 empty = isl_union_map_empty(isl_union_map_get_space(S));
2535 isl_union_map_compute_flow(isl_union_map_copy(R),
2536 isl_union_map_copy(W), empty,
2537 isl_union_map_copy(S),
2538 &dep_raw, NULL, NULL, NULL);
2539 isl_union_map_compute_flow(isl_union_map_copy(W),
2540 isl_union_map_copy(W),
2541 isl_union_map_copy(R),
2542 isl_union_map_copy(S),
2543 &dep_waw, &dep_war, NULL, NULL);
2545 dep = isl_union_map_union(dep_waw, dep_war);
2546 dep = isl_union_map_union(dep, dep_raw);
2547 validity = isl_union_map_copy(dep);
2548 coincidence = isl_union_map_copy(dep);
2549 proximity = isl_union_map_copy(dep);
2551 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
2552 sc = isl_schedule_constraints_set_validity(sc, validity);
2553 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
2554 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2555 sched = isl_schedule_constraints_compute_schedule(sc);
2556 schedule = isl_schedule_get_map(sched);
2557 isl_schedule_free(sched);
2558 isl_union_map_free(W);
2559 isl_union_map_free(R);
2560 isl_union_map_free(S);
2562 is_injection = 1;
2563 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2565 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2566 is_complete = isl_union_set_is_subset(D, domain);
2567 isl_union_set_free(D);
2568 isl_union_set_free(domain);
2570 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2571 test = isl_union_map_apply_range(test, dep);
2572 test = isl_union_map_apply_range(test, schedule);
2574 delta = isl_union_map_deltas(test);
2575 if (isl_union_set_n_set(delta) == 0) {
2576 is_tilable = 1;
2577 is_parallel = 1;
2578 is_nonneg = 1;
2579 isl_union_set_free(delta);
2580 } else {
2581 delta_set = isl_set_from_union_set(delta);
2583 slice = isl_set_universe(isl_set_get_space(delta_set));
2584 for (i = 0; i < tilable; ++i)
2585 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2586 is_tilable = isl_set_is_subset(delta_set, slice);
2587 isl_set_free(slice);
2589 slice = isl_set_universe(isl_set_get_space(delta_set));
2590 for (i = 0; i < parallel; ++i)
2591 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2592 is_parallel = isl_set_is_subset(delta_set, slice);
2593 isl_set_free(slice);
2595 origin = isl_set_universe(isl_set_get_space(delta_set));
2596 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2597 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2599 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2600 delta_set = isl_set_lexmin(delta_set);
2602 is_nonneg = isl_set_is_equal(delta_set, origin);
2604 isl_set_free(origin);
2605 isl_set_free(delta_set);
2608 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2609 is_injection < 0 || is_complete < 0)
2610 return -1;
2611 if (!is_complete)
2612 isl_die(ctx, isl_error_unknown,
2613 "generated schedule incomplete", return -1);
2614 if (!is_injection)
2615 isl_die(ctx, isl_error_unknown,
2616 "generated schedule not injective on each statement",
2617 return -1);
2618 if (!is_nonneg)
2619 isl_die(ctx, isl_error_unknown,
2620 "negative dependences in generated schedule",
2621 return -1);
2622 if (!is_tilable)
2623 isl_die(ctx, isl_error_unknown,
2624 "generated schedule not as tilable as expected",
2625 return -1);
2626 if (!is_parallel)
2627 isl_die(ctx, isl_error_unknown,
2628 "generated schedule not as parallel as expected",
2629 return -1);
2631 return 0;
2634 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2635 const char *domain, const char *validity, const char *proximity)
2637 isl_union_set *dom;
2638 isl_union_map *dep;
2639 isl_union_map *prox;
2640 isl_schedule_constraints *sc;
2641 isl_schedule *schedule;
2642 isl_union_map *sched;
2644 dom = isl_union_set_read_from_str(ctx, domain);
2645 dep = isl_union_map_read_from_str(ctx, validity);
2646 prox = isl_union_map_read_from_str(ctx, proximity);
2647 sc = isl_schedule_constraints_on_domain(dom);
2648 sc = isl_schedule_constraints_set_validity(sc, dep);
2649 sc = isl_schedule_constraints_set_proximity(sc, prox);
2650 schedule = isl_schedule_constraints_compute_schedule(sc);
2651 sched = isl_schedule_get_map(schedule);
2652 isl_schedule_free(schedule);
2654 return sched;
2657 /* Check that a schedule can be constructed on the given domain
2658 * with the given validity and proximity constraints.
2660 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2661 const char *validity, const char *proximity)
2663 isl_union_map *sched;
2665 sched = compute_schedule(ctx, domain, validity, proximity);
2666 if (!sched)
2667 return -1;
2669 isl_union_map_free(sched);
2670 return 0;
2673 int test_special_schedule(isl_ctx *ctx, const char *domain,
2674 const char *validity, const char *proximity, const char *expected_sched)
2676 isl_union_map *sched1, *sched2;
2677 int equal;
2679 sched1 = compute_schedule(ctx, domain, validity, proximity);
2680 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2682 equal = isl_union_map_is_equal(sched1, sched2);
2683 isl_union_map_free(sched1);
2684 isl_union_map_free(sched2);
2686 if (equal < 0)
2687 return -1;
2688 if (!equal)
2689 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2690 return -1);
2692 return 0;
2695 /* Check that the schedule map is properly padded, even after being
2696 * reconstructed from the band forest.
2698 static int test_padded_schedule(isl_ctx *ctx)
2700 const char *str;
2701 isl_union_set *D;
2702 isl_union_map *validity, *proximity;
2703 isl_schedule_constraints *sc;
2704 isl_schedule *sched;
2705 isl_union_map *map1, *map2;
2706 isl_band_list *list;
2707 int equal;
2709 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2710 D = isl_union_set_read_from_str(ctx, str);
2711 validity = isl_union_map_empty(isl_union_set_get_space(D));
2712 proximity = isl_union_map_copy(validity);
2713 sc = isl_schedule_constraints_on_domain(D);
2714 sc = isl_schedule_constraints_set_validity(sc, validity);
2715 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2716 sched = isl_schedule_constraints_compute_schedule(sc);
2717 map1 = isl_schedule_get_map(sched);
2718 list = isl_schedule_get_band_forest(sched);
2719 isl_band_list_free(list);
2720 map2 = isl_schedule_get_map(sched);
2721 isl_schedule_free(sched);
2722 equal = isl_union_map_is_equal(map1, map2);
2723 isl_union_map_free(map1);
2724 isl_union_map_free(map2);
2726 if (equal < 0)
2727 return -1;
2728 if (!equal)
2729 isl_die(ctx, isl_error_unknown,
2730 "reconstructed schedule map not the same as original",
2731 return -1);
2733 return 0;
2736 /* Input for testing of schedule construction based on
2737 * conditional constraints.
2739 * domain is the iteration domain
2740 * flow are the flow dependences, which determine the validity and
2741 * proximity constraints
2742 * condition are the conditions on the conditional validity constraints
2743 * conditional_validity are the conditional validity constraints
2744 * outer_band_n is the expected number of members in the outer band
2746 struct {
2747 const char *domain;
2748 const char *flow;
2749 const char *condition;
2750 const char *conditional_validity;
2751 int outer_band_n;
2752 } live_range_tests[] = {
2753 /* Contrived example that illustrates that we need to keep
2754 * track of tagged condition dependences and
2755 * tagged conditional validity dependences
2756 * in isl_sched_edge separately.
2757 * In particular, the conditional validity constraints on A
2758 * cannot be satisfied,
2759 * but they can be ignored because there are no corresponding
2760 * condition constraints. However, we do have an additional
2761 * conditional validity constraint that maps to the same
2762 * dependence relation
2763 * as the condition constraint on B. If we did not make a distinction
2764 * between tagged condition and tagged conditional validity
2765 * dependences, then we
2766 * could end up treating this shared dependence as an condition
2767 * constraint on A, forcing a localization of the conditions,
2768 * which is impossible.
2770 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
2771 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
2772 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
2773 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2774 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2775 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
2778 /* TACO 2013 Fig. 7 */
2779 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2780 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2781 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2782 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
2783 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
2784 "0 <= i < n and 0 <= j < n - 1 }",
2785 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
2786 "0 <= i < n and 0 <= j < j' < n;"
2787 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
2788 "0 <= i < i' < n and 0 <= j,j' < n;"
2789 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
2790 "0 <= i,j,j' < n and j < j' }",
2793 /* TACO 2013 Fig. 7, without tags */
2794 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2795 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2796 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2797 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2798 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2799 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
2800 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
2801 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
2804 /* TACO 2013 Fig. 12 */
2805 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
2806 "S3[i,3] : 0 <= i <= 1 }",
2807 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
2808 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
2809 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
2810 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
2811 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2812 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
2813 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2814 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
2815 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
2816 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
2817 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
2822 /* Test schedule construction based on conditional constraints.
2823 * In particular, check the number of members in the outer band
2824 * as an indication of whether tiling is possible or not.
2826 static int test_conditional_schedule_constraints(isl_ctx *ctx)
2828 int i;
2829 isl_union_set *domain;
2830 isl_union_map *condition;
2831 isl_union_map *flow;
2832 isl_union_map *validity;
2833 isl_schedule_constraints *sc;
2834 isl_schedule *schedule;
2835 isl_band_list *list;
2836 isl_band *band;
2837 int n_member;
2839 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
2840 domain = isl_union_set_read_from_str(ctx,
2841 live_range_tests[i].domain);
2842 flow = isl_union_map_read_from_str(ctx,
2843 live_range_tests[i].flow);
2844 condition = isl_union_map_read_from_str(ctx,
2845 live_range_tests[i].condition);
2846 validity = isl_union_map_read_from_str(ctx,
2847 live_range_tests[i].conditional_validity);
2848 sc = isl_schedule_constraints_on_domain(domain);
2849 sc = isl_schedule_constraints_set_validity(sc,
2850 isl_union_map_copy(flow));
2851 sc = isl_schedule_constraints_set_proximity(sc, flow);
2852 sc = isl_schedule_constraints_set_conditional_validity(sc,
2853 condition, validity);
2854 schedule = isl_schedule_constraints_compute_schedule(sc);
2855 list = isl_schedule_get_band_forest(schedule);
2856 band = isl_band_list_get_band(list, 0);
2857 n_member = isl_band_n_member(band);
2858 isl_band_free(band);
2859 isl_band_list_free(list);
2860 isl_schedule_free(schedule);
2862 if (!schedule)
2863 return -1;
2864 if (n_member != live_range_tests[i].outer_band_n)
2865 isl_die(ctx, isl_error_unknown,
2866 "unexpected number of members in outer band",
2867 return -1);
2869 return 0;
2872 int test_schedule(isl_ctx *ctx)
2874 const char *D, *W, *R, *V, *P, *S;
2876 /* Handle resulting schedule with zero bands. */
2877 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2878 return -1;
2880 /* Jacobi */
2881 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2882 W = "{ S1[t,i] -> a[t,i] }";
2883 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2884 "S1[t,i] -> a[t-1,i+1] }";
2885 S = "{ S1[t,i] -> [t,i] }";
2886 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2887 return -1;
2889 /* Fig. 5 of CC2008 */
2890 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2891 "j <= -1 + N }";
2892 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2893 "j >= 2 and j <= -1 + N }";
2894 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2895 "j >= 2 and j <= -1 + N; "
2896 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2897 "j >= 2 and j <= -1 + N }";
2898 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2899 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2900 return -1;
2902 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2903 W = "{ S1[i] -> a[i] }";
2904 R = "{ S2[i] -> a[i+1] }";
2905 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2906 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2907 return -1;
2909 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2910 W = "{ S1[i] -> a[i] }";
2911 R = "{ S2[i] -> a[9-i] }";
2912 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2913 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2914 return -1;
2916 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2917 W = "{ S1[i] -> a[i] }";
2918 R = "[N] -> { S2[i] -> a[N-1-i] }";
2919 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2920 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2921 return -1;
2923 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2924 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2925 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2926 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2927 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2928 return -1;
2930 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2931 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2932 R = "{ S2[i,j] -> a[i-1,j] }";
2933 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2934 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2935 return -1;
2937 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2938 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2939 R = "{ S2[i,j] -> a[i,j-1] }";
2940 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2941 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2942 return -1;
2944 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2945 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2946 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2947 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2948 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2949 "S_0[] -> [0, 0, 0] }";
2950 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2951 return -1;
2952 ctx->opt->schedule_parametric = 0;
2953 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2954 return -1;
2955 ctx->opt->schedule_parametric = 1;
2957 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2958 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2959 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2960 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2961 "S4[i] -> a[i,N] }";
2962 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2963 "S4[i] -> [4,i,0] }";
2964 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2965 return -1;
2967 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2968 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2969 "j <= N }";
2970 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2971 "j <= N; "
2972 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2973 "j <= N }";
2974 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2975 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2976 return -1;
2978 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2979 " S_2[t] : t >= 0 and t <= -1 + N; "
2980 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2981 "i <= -1 + N }";
2982 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2983 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2984 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2985 "i >= 0 and i <= -1 + N }";
2986 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2987 "i >= 0 and i <= -1 + N; "
2988 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2989 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2990 " S_0[t] -> [0, t, 0] }";
2992 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2993 return -1;
2994 ctx->opt->schedule_parametric = 0;
2995 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2996 return -1;
2997 ctx->opt->schedule_parametric = 1;
2999 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
3000 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
3001 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
3002 return -1;
3004 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
3005 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
3006 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
3007 "j >= 0 and j <= -1 + N; "
3008 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3009 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
3010 "j >= 0 and j <= -1 + N; "
3011 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3012 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
3013 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3014 return -1;
3016 D = "{ S_0[i] : i >= 0 }";
3017 W = "{ S_0[i] -> a[i] : i >= 0 }";
3018 R = "{ S_0[i] -> a[0] : i >= 0 }";
3019 S = "{ S_0[i] -> [0, i, 0] }";
3020 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3021 return -1;
3023 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
3024 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
3025 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
3026 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
3027 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3028 return -1;
3030 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
3031 "k <= -1 + n and k >= 0 }";
3032 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
3033 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
3034 "k <= -1 + n and k >= 0; "
3035 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
3036 "k <= -1 + n and k >= 0; "
3037 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
3038 "k <= -1 + n and k >= 0 }";
3039 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
3040 ctx->opt->schedule_outer_coincidence = 1;
3041 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3042 return -1;
3043 ctx->opt->schedule_outer_coincidence = 0;
3045 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3046 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3047 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3048 "Stmt_for_body24[i0, i1, 1, 0]:"
3049 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3050 "Stmt_for_body7[i0, i1, i2]:"
3051 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3052 "i2 <= 7 }";
3054 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3055 "Stmt_for_body24[1, i1, i2, i3]:"
3056 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3057 "i2 >= 1;"
3058 "Stmt_for_body24[0, i1, i2, i3] -> "
3059 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3060 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3061 "i3 >= 0;"
3062 "Stmt_for_body24[0, i1, i2, i3] ->"
3063 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3064 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3065 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3066 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3067 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3068 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3069 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3070 "i1 <= 6 and i1 >= 0;"
3071 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3072 "Stmt_for_body7[i0, i1, i2] -> "
3073 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3074 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3075 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3076 "Stmt_for_body7[i0, i1, i2] -> "
3077 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3078 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3079 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3080 P = V;
3081 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3082 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3083 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3085 if (test_special_schedule(ctx, D, V, P, S) < 0)
3086 return -1;
3088 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3089 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3090 "j >= 1 and j <= 7;"
3091 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3092 "j >= 1 and j <= 8 }";
3093 P = "{ }";
3094 S = "{ S_0[i, j] -> [i + j, j] }";
3095 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3096 if (test_special_schedule(ctx, D, V, P, S) < 0)
3097 return -1;
3098 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3100 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3101 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3102 "j >= 0 and j <= -1 + i }";
3103 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3104 "i <= -1 + N and j >= 0;"
3105 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3106 "i <= -2 + N }";
3107 P = "{ }";
3108 S = "{ S_0[i, j] -> [i, j] }";
3109 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3110 if (test_special_schedule(ctx, D, V, P, S) < 0)
3111 return -1;
3112 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3114 /* Test both algorithms on a case with only proximity dependences. */
3115 D = "{ S[i,j] : 0 <= i <= 10 }";
3116 V = "{ }";
3117 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3118 S = "{ S[i, j] -> [j, i] }";
3119 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3120 if (test_special_schedule(ctx, D, V, P, S) < 0)
3121 return -1;
3122 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3123 if (test_special_schedule(ctx, D, V, P, S) < 0)
3124 return -1;
3126 D = "{ A[a]; B[] }";
3127 V = "{}";
3128 P = "{ A[a] -> B[] }";
3129 if (test_has_schedule(ctx, D, V, P) < 0)
3130 return -1;
3132 if (test_padded_schedule(ctx) < 0)
3133 return -1;
3135 /* Check that check for progress is not confused by rational
3136 * solution.
3138 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3139 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3140 "i0 <= -2 + N; "
3141 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3142 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3143 P = "{}";
3144 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3145 if (test_has_schedule(ctx, D, V, P) < 0)
3146 return -1;
3147 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3149 if (test_conditional_schedule_constraints(ctx) < 0)
3150 return -1;
3152 return 0;
3155 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3157 isl_union_map *umap;
3158 int test;
3160 umap = isl_union_map_read_from_str(ctx, str);
3161 test = isl_union_map_plain_is_injective(umap);
3162 isl_union_map_free(umap);
3163 if (test < 0)
3164 return -1;
3165 if (test == injective)
3166 return 0;
3167 if (injective)
3168 isl_die(ctx, isl_error_unknown,
3169 "map not detected as injective", return -1);
3170 else
3171 isl_die(ctx, isl_error_unknown,
3172 "map detected as injective", return -1);
3175 int test_injective(isl_ctx *ctx)
3177 const char *str;
3179 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
3180 return -1;
3181 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
3182 return -1;
3183 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
3184 return -1;
3185 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
3186 return -1;
3187 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
3188 return -1;
3189 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
3190 return -1;
3191 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
3192 return -1;
3193 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
3194 return -1;
3196 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
3197 if (test_plain_injective(ctx, str, 1))
3198 return -1;
3199 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
3200 if (test_plain_injective(ctx, str, 0))
3201 return -1;
3203 return 0;
3206 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
3208 isl_aff *aff2;
3209 int equal;
3211 if (!aff)
3212 return -1;
3214 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
3215 equal = isl_aff_plain_is_equal(aff, aff2);
3216 isl_aff_free(aff2);
3218 return equal;
3221 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
3223 int equal;
3225 equal = aff_plain_is_equal(aff, str);
3226 if (equal < 0)
3227 return -1;
3228 if (!equal)
3229 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
3230 "result not as expected", return -1);
3231 return 0;
3234 struct {
3235 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3236 __isl_take isl_aff *aff2);
3237 } aff_bin_op[] = {
3238 ['+'] = { &isl_aff_add },
3239 ['-'] = { &isl_aff_sub },
3240 ['*'] = { &isl_aff_mul },
3241 ['/'] = { &isl_aff_div },
3244 struct {
3245 const char *arg1;
3246 unsigned char op;
3247 const char *arg2;
3248 const char *res;
3249 } aff_bin_tests[] = {
3250 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
3251 "{ [i] -> [2i] }" },
3252 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
3253 "{ [i] -> [0] }" },
3254 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
3255 "{ [i] -> [2i] }" },
3256 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
3257 "{ [i] -> [2i] }" },
3258 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
3259 "{ [i] -> [i/2] }" },
3260 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
3261 "{ [i] -> [i] }" },
3262 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
3263 "{ [i] -> [NaN] }" },
3264 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
3265 "{ [i] -> [NaN] }" },
3266 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
3267 "{ [i] -> [NaN] }" },
3268 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
3269 "{ [i] -> [NaN] }" },
3270 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
3271 "{ [i] -> [NaN] }" },
3272 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
3273 "{ [i] -> [NaN] }" },
3274 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
3275 "{ [i] -> [NaN] }" },
3276 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
3277 "{ [i] -> [NaN] }" },
3278 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
3279 "{ [i] -> [NaN] }" },
3280 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
3281 "{ [i] -> [NaN] }" },
3282 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
3283 "{ [i] -> [NaN] }" },
3284 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
3285 "{ [i] -> [NaN] }" },
3288 /* Perform some basic tests of binary operations on isl_aff objects.
3290 static int test_bin_aff(isl_ctx *ctx)
3292 int i;
3293 isl_aff *aff1, *aff2, *res;
3294 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3295 __isl_take isl_aff *aff2);
3296 int ok;
3298 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
3299 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
3300 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
3301 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
3302 fn = aff_bin_op[aff_bin_tests[i].op].fn;
3303 aff1 = fn(aff1, aff2);
3304 if (isl_aff_is_nan(res))
3305 ok = isl_aff_is_nan(aff1);
3306 else
3307 ok = isl_aff_plain_is_equal(aff1, res);
3308 isl_aff_free(aff1);
3309 isl_aff_free(res);
3310 if (ok < 0)
3311 return -1;
3312 if (!ok)
3313 isl_die(ctx, isl_error_unknown,
3314 "unexpected result", return -1);
3317 return 0;
3320 int test_aff(isl_ctx *ctx)
3322 const char *str;
3323 isl_set *set;
3324 isl_space *space;
3325 isl_local_space *ls;
3326 isl_aff *aff;
3327 int zero, equal;
3329 if (test_bin_aff(ctx) < 0)
3330 return -1;
3332 space = isl_space_set_alloc(ctx, 0, 1);
3333 ls = isl_local_space_from_space(space);
3334 aff = isl_aff_zero_on_domain(ls);
3336 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3337 aff = isl_aff_scale_down_ui(aff, 3);
3338 aff = isl_aff_floor(aff);
3339 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3340 aff = isl_aff_scale_down_ui(aff, 2);
3341 aff = isl_aff_floor(aff);
3342 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3344 str = "{ [10] }";
3345 set = isl_set_read_from_str(ctx, str);
3346 aff = isl_aff_gist(aff, set);
3348 aff = isl_aff_add_constant_si(aff, -16);
3349 zero = isl_aff_plain_is_zero(aff);
3350 isl_aff_free(aff);
3352 if (zero < 0)
3353 return -1;
3354 if (!zero)
3355 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3357 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
3358 aff = isl_aff_scale_down_ui(aff, 64);
3359 aff = isl_aff_floor(aff);
3360 equal = aff_check_plain_equal(aff, "{ [-1] }");
3361 isl_aff_free(aff);
3362 if (equal < 0)
3363 return -1;
3365 return 0;
3368 int test_dim_max(isl_ctx *ctx)
3370 int equal;
3371 const char *str;
3372 isl_set *set1, *set2;
3373 isl_set *set;
3374 isl_map *map;
3375 isl_pw_aff *pwaff;
3377 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
3378 set = isl_set_read_from_str(ctx, str);
3379 pwaff = isl_set_dim_max(set, 0);
3380 set1 = isl_set_from_pw_aff(pwaff);
3381 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
3382 set2 = isl_set_read_from_str(ctx, str);
3383 equal = isl_set_is_equal(set1, set2);
3384 isl_set_free(set1);
3385 isl_set_free(set2);
3386 if (equal < 0)
3387 return -1;
3388 if (!equal)
3389 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3391 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
3392 set = isl_set_read_from_str(ctx, str);
3393 pwaff = isl_set_dim_max(set, 0);
3394 set1 = isl_set_from_pw_aff(pwaff);
3395 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3396 set2 = isl_set_read_from_str(ctx, str);
3397 equal = isl_set_is_equal(set1, set2);
3398 isl_set_free(set1);
3399 isl_set_free(set2);
3400 if (equal < 0)
3401 return -1;
3402 if (!equal)
3403 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3405 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
3406 set = isl_set_read_from_str(ctx, str);
3407 pwaff = isl_set_dim_max(set, 0);
3408 set1 = isl_set_from_pw_aff(pwaff);
3409 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3410 set2 = isl_set_read_from_str(ctx, str);
3411 equal = isl_set_is_equal(set1, set2);
3412 isl_set_free(set1);
3413 isl_set_free(set2);
3414 if (equal < 0)
3415 return -1;
3416 if (!equal)
3417 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3419 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
3420 "0 <= i < N and 0 <= j < M }";
3421 map = isl_map_read_from_str(ctx, str);
3422 set = isl_map_range(map);
3424 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
3425 set1 = isl_set_from_pw_aff(pwaff);
3426 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
3427 set2 = isl_set_read_from_str(ctx, str);
3428 equal = isl_set_is_equal(set1, set2);
3429 isl_set_free(set1);
3430 isl_set_free(set2);
3432 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3433 set1 = isl_set_from_pw_aff(pwaff);
3434 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3435 set2 = isl_set_read_from_str(ctx, str);
3436 if (equal >= 0 && equal)
3437 equal = isl_set_is_equal(set1, set2);
3438 isl_set_free(set1);
3439 isl_set_free(set2);
3441 isl_set_free(set);
3443 if (equal < 0)
3444 return -1;
3445 if (!equal)
3446 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3448 /* Check that solutions are properly merged. */
3449 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3450 "c <= -1 + n - 4a - 2b and c >= -2b and "
3451 "4a >= -4 + n and c >= 0 }";
3452 set = isl_set_read_from_str(ctx, str);
3453 pwaff = isl_set_dim_min(set, 2);
3454 set1 = isl_set_from_pw_aff(pwaff);
3455 str = "[n] -> { [(0)] : n >= 1 }";
3456 set2 = isl_set_read_from_str(ctx, str);
3457 equal = isl_set_is_equal(set1, set2);
3458 isl_set_free(set1);
3459 isl_set_free(set2);
3461 if (equal < 0)
3462 return -1;
3463 if (!equal)
3464 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3466 /* Check that empty solution lie in the right space. */
3467 str = "[n] -> { [t,a] : 1 = 0 }";
3468 set = isl_set_read_from_str(ctx, str);
3469 pwaff = isl_set_dim_max(set, 0);
3470 set1 = isl_set_from_pw_aff(pwaff);
3471 str = "[n] -> { [t] : 1 = 0 }";
3472 set2 = isl_set_read_from_str(ctx, str);
3473 equal = isl_set_is_equal(set1, set2);
3474 isl_set_free(set1);
3475 isl_set_free(set2);
3477 if (equal < 0)
3478 return -1;
3479 if (!equal)
3480 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3482 return 0;
3485 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
3487 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
3488 const char *str)
3490 isl_ctx *ctx;
3491 isl_pw_multi_aff *pma2;
3492 int equal;
3494 if (!pma)
3495 return -1;
3497 ctx = isl_pw_multi_aff_get_ctx(pma);
3498 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3499 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
3500 isl_pw_multi_aff_free(pma2);
3502 return equal;
3505 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
3506 * represented by "str".
3508 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
3509 const char *str)
3511 int equal;
3513 equal = pw_multi_aff_plain_is_equal(pma, str);
3514 if (equal < 0)
3515 return -1;
3516 if (!equal)
3517 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
3518 "result not as expected", return -1);
3519 return 0;
3522 /* Basic test for isl_pw_multi_aff_product.
3524 * Check that multiple pieces are properly handled.
3526 static int test_product_pma(isl_ctx *ctx)
3528 int equal;
3529 const char *str;
3530 isl_pw_multi_aff *pma1, *pma2;
3532 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
3533 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3534 str = "{ C[] -> D[] }";
3535 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3536 pma1 = isl_pw_multi_aff_product(pma1, pma2);
3537 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
3538 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
3539 equal = pw_multi_aff_check_plain_equal(pma1, str);
3540 isl_pw_multi_aff_free(pma1);
3541 if (equal < 0)
3542 return -1;
3544 return 0;
3547 int test_product(isl_ctx *ctx)
3549 const char *str;
3550 isl_set *set;
3551 isl_union_set *uset1, *uset2;
3552 int ok;
3554 str = "{ A[i] }";
3555 set = isl_set_read_from_str(ctx, str);
3556 set = isl_set_product(set, isl_set_copy(set));
3557 ok = isl_set_is_wrapping(set);
3558 isl_set_free(set);
3559 if (ok < 0)
3560 return -1;
3561 if (!ok)
3562 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3564 str = "{ [] }";
3565 uset1 = isl_union_set_read_from_str(ctx, str);
3566 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3567 str = "{ [[] -> []] }";
3568 uset2 = isl_union_set_read_from_str(ctx, str);
3569 ok = isl_union_set_is_equal(uset1, uset2);
3570 isl_union_set_free(uset1);
3571 isl_union_set_free(uset2);
3572 if (ok < 0)
3573 return -1;
3574 if (!ok)
3575 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3577 if (test_product_pma(ctx) < 0)
3578 return -1;
3580 return 0;
3583 /* Check that two sets are not considered disjoint just because
3584 * they have a different set of (named) parameters.
3586 static int test_disjoint(isl_ctx *ctx)
3588 const char *str;
3589 isl_set *set, *set2;
3590 int disjoint;
3592 str = "[n] -> { [[]->[]] }";
3593 set = isl_set_read_from_str(ctx, str);
3594 str = "{ [[]->[]] }";
3595 set2 = isl_set_read_from_str(ctx, str);
3596 disjoint = isl_set_is_disjoint(set, set2);
3597 isl_set_free(set);
3598 isl_set_free(set2);
3599 if (disjoint < 0)
3600 return -1;
3601 if (disjoint)
3602 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3604 return 0;
3607 int test_equal(isl_ctx *ctx)
3609 const char *str;
3610 isl_set *set, *set2;
3611 int equal;
3613 str = "{ S_6[i] }";
3614 set = isl_set_read_from_str(ctx, str);
3615 str = "{ S_7[i] }";
3616 set2 = isl_set_read_from_str(ctx, str);
3617 equal = isl_set_is_equal(set, set2);
3618 isl_set_free(set);
3619 isl_set_free(set2);
3620 if (equal < 0)
3621 return -1;
3622 if (equal)
3623 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3625 return 0;
3628 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3629 enum isl_dim_type type, unsigned pos, int fixed)
3631 int test;
3633 test = isl_map_plain_is_fixed(map, type, pos, NULL);
3634 isl_map_free(map);
3635 if (test < 0)
3636 return -1;
3637 if (test == fixed)
3638 return 0;
3639 if (fixed)
3640 isl_die(ctx, isl_error_unknown,
3641 "map not detected as fixed", return -1);
3642 else
3643 isl_die(ctx, isl_error_unknown,
3644 "map detected as fixed", return -1);
3647 int test_fixed(isl_ctx *ctx)
3649 const char *str;
3650 isl_map *map;
3652 str = "{ [i] -> [i] }";
3653 map = isl_map_read_from_str(ctx, str);
3654 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3655 return -1;
3656 str = "{ [i] -> [1] }";
3657 map = isl_map_read_from_str(ctx, str);
3658 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3659 return -1;
3660 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3661 map = isl_map_read_from_str(ctx, str);
3662 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3663 return -1;
3664 map = isl_map_read_from_str(ctx, str);
3665 map = isl_map_neg(map);
3666 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3667 return -1;
3669 return 0;
3672 struct isl_vertices_test_data {
3673 const char *set;
3674 int n;
3675 const char *vertex[2];
3676 } vertices_tests[] = {
3677 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
3678 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
3679 { "{ A[t, i] : t = 14 and i = 1 }",
3680 1, { "{ A[14, 1] }" } },
3683 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
3685 static int find_vertex(__isl_take isl_vertex *vertex, void *user)
3687 struct isl_vertices_test_data *data = user;
3688 isl_ctx *ctx;
3689 isl_multi_aff *ma;
3690 isl_basic_set *bset;
3691 isl_pw_multi_aff *pma;
3692 int i;
3693 int equal;
3695 ctx = isl_vertex_get_ctx(vertex);
3696 bset = isl_vertex_get_domain(vertex);
3697 ma = isl_vertex_get_expr(vertex);
3698 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
3700 for (i = 0; i < data->n; ++i) {
3701 isl_pw_multi_aff *pma_i;
3703 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
3704 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
3705 isl_pw_multi_aff_free(pma_i);
3707 if (equal < 0 || equal)
3708 break;
3711 isl_pw_multi_aff_free(pma);
3712 isl_vertex_free(vertex);
3714 if (equal < 0)
3715 return -1;
3717 return equal ? 0 : - 1;
3720 int test_vertices(isl_ctx *ctx)
3722 int i;
3724 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
3725 isl_basic_set *bset;
3726 isl_vertices *vertices;
3727 int ok = 1;
3728 int n;
3730 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
3731 vertices = isl_basic_set_compute_vertices(bset);
3732 n = isl_vertices_get_n_vertices(vertices);
3733 if (vertices_tests[i].n != n)
3734 ok = 0;
3735 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
3736 &vertices_tests[i]) < 0)
3737 ok = 0;
3738 isl_vertices_free(vertices);
3739 isl_basic_set_free(bset);
3741 if (!vertices)
3742 return -1;
3743 if (!ok)
3744 isl_die(ctx, isl_error_unknown, "unexpected vertices",
3745 return -1);
3748 return 0;
3751 int test_union_pw(isl_ctx *ctx)
3753 int equal;
3754 const char *str;
3755 isl_union_set *uset;
3756 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
3758 str = "{ [x] -> x^2 }";
3759 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
3760 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
3761 uset = isl_union_pw_qpolynomial_domain(upwqp1);
3762 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
3763 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
3764 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
3765 isl_union_pw_qpolynomial_free(upwqp1);
3766 isl_union_pw_qpolynomial_free(upwqp2);
3767 if (equal < 0)
3768 return -1;
3769 if (!equal)
3770 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3772 return 0;
3775 int test_output(isl_ctx *ctx)
3777 char *s;
3778 const char *str;
3779 isl_pw_aff *pa;
3780 isl_printer *p;
3781 int equal;
3783 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
3784 pa = isl_pw_aff_read_from_str(ctx, str);
3786 p = isl_printer_to_str(ctx);
3787 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
3788 p = isl_printer_print_pw_aff(p, pa);
3789 s = isl_printer_get_str(p);
3790 isl_printer_free(p);
3791 isl_pw_aff_free(pa);
3792 if (!s)
3793 equal = -1;
3794 else
3795 equal = !strcmp(s, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
3796 free(s);
3797 if (equal < 0)
3798 return -1;
3799 if (!equal)
3800 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3802 return 0;
3805 int test_sample(isl_ctx *ctx)
3807 const char *str;
3808 isl_basic_set *bset1, *bset2;
3809 int empty, subset;
3811 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3812 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3813 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3814 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3815 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3816 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3817 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3818 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3819 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3820 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3821 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3822 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3823 "d - 1073741821e and "
3824 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3825 "3j >= 1 - a + b + 2e and "
3826 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3827 "3i <= 4 - a + 4b - e and "
3828 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3829 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3830 "c <= -1 + a and 3i >= -2 - a + 3e and "
3831 "1073741822e <= 1073741823 - a + 1073741822b + c and "
3832 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3833 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3834 "1073741823e >= 1 + 1073741823b - d and "
3835 "3i >= 1073741823b + c - 1073741823e - f and "
3836 "3i >= 1 + 2b + e + 3g }";
3837 bset1 = isl_basic_set_read_from_str(ctx, str);
3838 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3839 empty = isl_basic_set_is_empty(bset2);
3840 subset = isl_basic_set_is_subset(bset2, bset1);
3841 isl_basic_set_free(bset1);
3842 isl_basic_set_free(bset2);
3843 if (empty < 0 || subset < 0)
3844 return -1;
3845 if (empty)
3846 isl_die(ctx, isl_error_unknown, "point not found", return -1);
3847 if (!subset)
3848 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
3850 return 0;
3853 int test_fixed_power(isl_ctx *ctx)
3855 const char *str;
3856 isl_map *map;
3857 isl_int exp;
3858 int equal;
3860 isl_int_init(exp);
3861 str = "{ [i] -> [i + 1] }";
3862 map = isl_map_read_from_str(ctx, str);
3863 isl_int_set_si(exp, 23);
3864 map = isl_map_fixed_power(map, exp);
3865 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
3866 isl_int_clear(exp);
3867 isl_map_free(map);
3868 if (equal < 0)
3869 return -1;
3871 return 0;
3874 int test_slice(isl_ctx *ctx)
3876 const char *str;
3877 isl_map *map;
3878 int equal;
3880 str = "{ [i] -> [j] }";
3881 map = isl_map_read_from_str(ctx, str);
3882 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
3883 equal = map_check_equal(map, "{ [i] -> [i] }");
3884 isl_map_free(map);
3885 if (equal < 0)
3886 return -1;
3888 str = "{ [i] -> [j] }";
3889 map = isl_map_read_from_str(ctx, str);
3890 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
3891 equal = map_check_equal(map, "{ [i] -> [j] }");
3892 isl_map_free(map);
3893 if (equal < 0)
3894 return -1;
3896 str = "{ [i] -> [j] }";
3897 map = isl_map_read_from_str(ctx, str);
3898 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
3899 equal = map_check_equal(map, "{ [i] -> [-i] }");
3900 isl_map_free(map);
3901 if (equal < 0)
3902 return -1;
3904 str = "{ [i] -> [j] }";
3905 map = isl_map_read_from_str(ctx, str);
3906 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
3907 equal = map_check_equal(map, "{ [0] -> [j] }");
3908 isl_map_free(map);
3909 if (equal < 0)
3910 return -1;
3912 str = "{ [i] -> [j] }";
3913 map = isl_map_read_from_str(ctx, str);
3914 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3915 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
3916 isl_map_free(map);
3917 if (equal < 0)
3918 return -1;
3920 str = "{ [i] -> [j] }";
3921 map = isl_map_read_from_str(ctx, str);
3922 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
3923 equal = map_check_equal(map, "{ [i] -> [j] : false }");
3924 isl_map_free(map);
3925 if (equal < 0)
3926 return -1;
3928 return 0;
3931 int test_eliminate(isl_ctx *ctx)
3933 const char *str;
3934 isl_map *map;
3935 int equal;
3937 str = "{ [i] -> [j] : i = 2j }";
3938 map = isl_map_read_from_str(ctx, str);
3939 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
3940 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
3941 isl_map_free(map);
3942 if (equal < 0)
3943 return -1;
3945 return 0;
3948 /* Check that isl_set_dim_residue_class detects that the values of j
3949 * in the set below are all odd and that it does not detect any spurious
3950 * strides.
3952 static int test_residue_class(isl_ctx *ctx)
3954 const char *str;
3955 isl_set *set;
3956 isl_int m, r;
3957 int res;
3959 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3960 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3961 set = isl_set_read_from_str(ctx, str);
3962 isl_int_init(m);
3963 isl_int_init(r);
3964 res = isl_set_dim_residue_class(set, 1, &m, &r);
3965 if (res >= 0 &&
3966 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
3967 isl_die(ctx, isl_error_unknown, "incorrect residue class",
3968 res = -1);
3969 isl_int_clear(r);
3970 isl_int_clear(m);
3971 isl_set_free(set);
3973 return res;
3976 int test_align_parameters(isl_ctx *ctx)
3978 const char *str;
3979 isl_space *space;
3980 isl_multi_aff *ma1, *ma2;
3981 int equal;
3983 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
3984 ma1 = isl_multi_aff_read_from_str(ctx, str);
3986 space = isl_space_params_alloc(ctx, 1);
3987 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
3988 ma1 = isl_multi_aff_align_params(ma1, space);
3990 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
3991 ma2 = isl_multi_aff_read_from_str(ctx, str);
3993 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3995 isl_multi_aff_free(ma1);
3996 isl_multi_aff_free(ma2);
3998 if (equal < 0)
3999 return -1;
4000 if (!equal)
4001 isl_die(ctx, isl_error_unknown,
4002 "result not as expected", return -1);
4004 return 0;
4007 static int test_list(isl_ctx *ctx)
4009 isl_id *a, *b, *c, *d, *id;
4010 isl_id_list *list;
4011 int ok;
4013 a = isl_id_alloc(ctx, "a", NULL);
4014 b = isl_id_alloc(ctx, "b", NULL);
4015 c = isl_id_alloc(ctx, "c", NULL);
4016 d = isl_id_alloc(ctx, "d", NULL);
4018 list = isl_id_list_alloc(ctx, 4);
4019 list = isl_id_list_add(list, a);
4020 list = isl_id_list_add(list, b);
4021 list = isl_id_list_add(list, c);
4022 list = isl_id_list_add(list, d);
4023 list = isl_id_list_drop(list, 1, 1);
4025 if (isl_id_list_n_id(list) != 3) {
4026 isl_id_list_free(list);
4027 isl_die(ctx, isl_error_unknown,
4028 "unexpected number of elements in list", return -1);
4031 id = isl_id_list_get_id(list, 0);
4032 ok = id == a;
4033 isl_id_free(id);
4034 id = isl_id_list_get_id(list, 1);
4035 ok = ok && id == c;
4036 isl_id_free(id);
4037 id = isl_id_list_get_id(list, 2);
4038 ok = ok && id == d;
4039 isl_id_free(id);
4041 isl_id_list_free(list);
4043 if (!ok)
4044 isl_die(ctx, isl_error_unknown,
4045 "unexpected elements in list", return -1);
4047 return 0;
4050 const char *set_conversion_tests[] = {
4051 "[N] -> { [i] : N - 1 <= 2 i <= N }",
4052 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
4053 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4054 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4057 /* Check that converting from isl_set to isl_pw_multi_aff and back
4058 * to isl_set produces the original isl_set.
4060 static int test_set_conversion(isl_ctx *ctx)
4062 int i;
4063 const char *str;
4064 isl_set *set1, *set2;
4065 isl_pw_multi_aff *pma;
4066 int equal;
4068 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
4069 str = set_conversion_tests[i];
4070 set1 = isl_set_read_from_str(ctx, str);
4071 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
4072 set2 = isl_set_from_pw_multi_aff(pma);
4073 equal = isl_set_is_equal(set1, set2);
4074 isl_set_free(set1);
4075 isl_set_free(set2);
4077 if (equal < 0)
4078 return -1;
4079 if (!equal)
4080 isl_die(ctx, isl_error_unknown, "bad conversion",
4081 return -1);
4084 return 0;
4087 /* Check that converting from isl_map to isl_pw_multi_aff and back
4088 * to isl_map produces the original isl_map.
4090 static int test_map_conversion(isl_ctx *ctx)
4092 const char *str;
4093 isl_map *map1, *map2;
4094 isl_pw_multi_aff *pma;
4095 int equal;
4097 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
4098 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
4099 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
4100 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
4101 "9e <= -2 - 2a) }";
4102 map1 = isl_map_read_from_str(ctx, str);
4103 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
4104 map2 = isl_map_from_pw_multi_aff(pma);
4105 equal = isl_map_is_equal(map1, map2);
4106 isl_map_free(map1);
4107 isl_map_free(map2);
4109 if (equal < 0)
4110 return -1;
4111 if (!equal)
4112 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
4114 return 0;
4117 static int test_conversion(isl_ctx *ctx)
4119 if (test_set_conversion(ctx) < 0)
4120 return -1;
4121 if (test_map_conversion(ctx) < 0)
4122 return -1;
4123 return 0;
4126 /* Check that isl_basic_map_curry does not modify input.
4128 static int test_curry(isl_ctx *ctx)
4130 const char *str;
4131 isl_basic_map *bmap1, *bmap2;
4132 int equal;
4134 str = "{ [A[] -> B[]] -> C[] }";
4135 bmap1 = isl_basic_map_read_from_str(ctx, str);
4136 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
4137 equal = isl_basic_map_is_equal(bmap1, bmap2);
4138 isl_basic_map_free(bmap1);
4139 isl_basic_map_free(bmap2);
4141 if (equal < 0)
4142 return -1;
4143 if (equal)
4144 isl_die(ctx, isl_error_unknown,
4145 "curried map should not be equal to original",
4146 return -1);
4148 return 0;
4151 struct {
4152 const char *set;
4153 const char *ma;
4154 const char *res;
4155 } preimage_tests[] = {
4156 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
4157 "{ A[j,i] -> B[i,j] }",
4158 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
4159 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4160 "{ A[a,b] -> B[a/2,b/6] }",
4161 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
4162 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4163 "{ A[a,b] -> B[a/2,b/6] }",
4164 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
4165 "exists i,j : a = 2 i and b = 6 j }" },
4166 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
4167 "[n] -> { : 0 <= n <= 100 }" },
4168 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4169 "{ A[a] -> B[2a] }",
4170 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
4171 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4172 "{ A[a] -> B[([a/2])] }",
4173 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
4174 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
4175 "{ A[a] -> B[a,a,a/3] }",
4176 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
4177 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
4178 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
4181 static int test_preimage_basic_set(isl_ctx *ctx)
4183 int i;
4184 isl_basic_set *bset1, *bset2;
4185 isl_multi_aff *ma;
4186 int equal;
4188 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
4189 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
4190 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
4191 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
4192 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
4193 equal = isl_basic_set_is_equal(bset1, bset2);
4194 isl_basic_set_free(bset1);
4195 isl_basic_set_free(bset2);
4196 if (equal < 0)
4197 return -1;
4198 if (!equal)
4199 isl_die(ctx, isl_error_unknown, "bad preimage",
4200 return -1);
4203 return 0;
4206 struct {
4207 const char *map;
4208 const char *ma;
4209 const char *res;
4210 } preimage_domain_tests[] = {
4211 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
4212 "{ A[j,i] -> B[i,j] }",
4213 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
4214 { "{ B[i] -> C[i]; D[i] -> E[i] }",
4215 "{ A[i] -> B[i + 1] }",
4216 "{ A[i] -> C[i + 1] }" },
4217 { "{ B[i] -> C[i]; B[i] -> E[i] }",
4218 "{ A[i] -> B[i + 1] }",
4219 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
4220 { "{ B[i] -> C[([i/2])] }",
4221 "{ A[i] -> B[2i] }",
4222 "{ A[i] -> C[i] }" },
4223 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
4224 "{ A[i] -> B[([i/5]), ([i/7])] }",
4225 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
4226 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
4227 "[N] -> { A[] -> B[([N/5])] }",
4228 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
4229 { "{ B[i] -> C[i] : exists a : i = 5 a }",
4230 "{ A[i] -> B[2i] }",
4231 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
4232 { "{ B[i] -> C[i] : exists a : i = 2 a; "
4233 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
4234 "{ A[i] -> B[2i] }",
4235 "{ A[i] -> C[2i] }" },
4238 static int test_preimage_union_map(isl_ctx *ctx)
4240 int i;
4241 isl_union_map *umap1, *umap2;
4242 isl_multi_aff *ma;
4243 int equal;
4245 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
4246 umap1 = isl_union_map_read_from_str(ctx,
4247 preimage_domain_tests[i].map);
4248 ma = isl_multi_aff_read_from_str(ctx,
4249 preimage_domain_tests[i].ma);
4250 umap2 = isl_union_map_read_from_str(ctx,
4251 preimage_domain_tests[i].res);
4252 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
4253 equal = isl_union_map_is_equal(umap1, umap2);
4254 isl_union_map_free(umap1);
4255 isl_union_map_free(umap2);
4256 if (equal < 0)
4257 return -1;
4258 if (!equal)
4259 isl_die(ctx, isl_error_unknown, "bad preimage",
4260 return -1);
4263 return 0;
4266 static int test_preimage(isl_ctx *ctx)
4268 if (test_preimage_basic_set(ctx) < 0)
4269 return -1;
4270 if (test_preimage_union_map(ctx) < 0)
4271 return -1;
4273 return 0;
4276 struct {
4277 const char *ma1;
4278 const char *ma;
4279 const char *res;
4280 } pullback_tests[] = {
4281 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
4282 "{ A[a,b] -> C[b + 2a] }" },
4283 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
4284 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
4285 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
4286 "{ A[a] -> C[(a)/6] }" },
4287 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
4288 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
4289 "{ A[a] -> C[(2a)/3] }" },
4290 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
4291 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
4292 "{ A[i,j] -> C[i + j, i + j] }"},
4293 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
4294 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
4295 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
4298 static int test_pullback(isl_ctx *ctx)
4300 int i;
4301 isl_multi_aff *ma1, *ma2;
4302 isl_multi_aff *ma;
4303 int equal;
4305 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
4306 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
4307 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
4308 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
4309 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
4310 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4311 isl_multi_aff_free(ma1);
4312 isl_multi_aff_free(ma2);
4313 if (equal < 0)
4314 return -1;
4315 if (!equal)
4316 isl_die(ctx, isl_error_unknown, "bad pullback",
4317 return -1);
4320 return 0;
4323 /* Check that negation is printed correctly.
4325 static int test_ast(isl_ctx *ctx)
4327 isl_ast_expr *expr, *expr1, *expr2, *expr3;
4328 char *str;
4329 int ok;
4331 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4332 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4333 expr = isl_ast_expr_add(expr1, expr2);
4334 expr = isl_ast_expr_neg(expr);
4335 str = isl_ast_expr_to_str(expr);
4336 ok = str ? !strcmp(str, "-(A + B)") : -1;
4337 free(str);
4338 isl_ast_expr_free(expr);
4340 if (ok < 0)
4341 return -1;
4342 if (!ok)
4343 isl_die(ctx, isl_error_unknown,
4344 "isl_ast_expr printed incorrectly", return -1);
4346 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4347 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4348 expr = isl_ast_expr_add(expr1, expr2);
4349 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
4350 expr = isl_ast_expr_sub(expr3, expr);
4351 str = isl_ast_expr_to_str(expr);
4352 ok = str ? !strcmp(str, "C - (A + B)") : -1;
4353 free(str);
4354 isl_ast_expr_free(expr);
4356 if (ok < 0)
4357 return -1;
4358 if (!ok)
4359 isl_die(ctx, isl_error_unknown,
4360 "isl_ast_expr printed incorrectly", return -1);
4362 return 0;
4365 /* Check that isl_ast_build_expr_from_set returns a valid expression
4366 * for an empty set. Note that isl_ast_build_expr_from_set getting
4367 * called on an empty set probably indicates a bug in the caller.
4369 static int test_ast_build(isl_ctx *ctx)
4371 isl_set *set;
4372 isl_ast_build *build;
4373 isl_ast_expr *expr;
4375 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4376 build = isl_ast_build_from_context(set);
4378 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
4379 expr = isl_ast_build_expr_from_set(build, set);
4381 isl_ast_expr_free(expr);
4382 isl_ast_build_free(build);
4384 if (!expr)
4385 return -1;
4387 return 0;
4390 /* Internal data structure for before_for and after_for callbacks.
4392 * depth is the current depth
4393 * before is the number of times before_for has been called
4394 * after is the number of times after_for has been called
4396 struct isl_test_codegen_data {
4397 int depth;
4398 int before;
4399 int after;
4402 /* This function is called before each for loop in the AST generated
4403 * from test_ast_gen1.
4405 * Increment the number of calls and the depth.
4406 * Check that the space returned by isl_ast_build_get_schedule_space
4407 * matches the target space of the schedule returned by
4408 * isl_ast_build_get_schedule.
4409 * Return an isl_id that is checked by the corresponding call
4410 * to after_for.
4412 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
4413 void *user)
4415 struct isl_test_codegen_data *data = user;
4416 isl_ctx *ctx;
4417 isl_space *space;
4418 isl_union_map *schedule;
4419 isl_union_set *uset;
4420 isl_set *set;
4421 int empty;
4422 char name[] = "d0";
4424 ctx = isl_ast_build_get_ctx(build);
4426 if (data->before >= 3)
4427 isl_die(ctx, isl_error_unknown,
4428 "unexpected number of for nodes", return NULL);
4429 if (data->depth >= 2)
4430 isl_die(ctx, isl_error_unknown,
4431 "unexpected depth", return NULL);
4433 snprintf(name, sizeof(name), "d%d", data->depth);
4434 data->before++;
4435 data->depth++;
4437 schedule = isl_ast_build_get_schedule(build);
4438 uset = isl_union_map_range(schedule);
4439 if (!uset)
4440 return NULL;
4441 if (isl_union_set_n_set(uset) != 1) {
4442 isl_union_set_free(uset);
4443 isl_die(ctx, isl_error_unknown,
4444 "expecting single range space", return NULL);
4447 space = isl_ast_build_get_schedule_space(build);
4448 set = isl_union_set_extract_set(uset, space);
4449 isl_union_set_free(uset);
4450 empty = isl_set_is_empty(set);
4451 isl_set_free(set);
4453 if (empty < 0)
4454 return NULL;
4455 if (empty)
4456 isl_die(ctx, isl_error_unknown,
4457 "spaces don't match", return NULL);
4459 return isl_id_alloc(ctx, name, NULL);
4462 /* This function is called after each for loop in the AST generated
4463 * from test_ast_gen1.
4465 * Increment the number of calls and decrement the depth.
4466 * Check that the annotation attached to the node matches
4467 * the isl_id returned by the corresponding call to before_for.
4469 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
4470 __isl_keep isl_ast_build *build, void *user)
4472 struct isl_test_codegen_data *data = user;
4473 isl_id *id;
4474 const char *name;
4475 int valid;
4477 data->after++;
4478 data->depth--;
4480 if (data->after > data->before)
4481 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4482 "mismatch in number of for nodes",
4483 return isl_ast_node_free(node));
4485 id = isl_ast_node_get_annotation(node);
4486 if (!id)
4487 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4488 "missing annotation", return isl_ast_node_free(node));
4490 name = isl_id_get_name(id);
4491 valid = name && atoi(name + 1) == data->depth;
4492 isl_id_free(id);
4494 if (!valid)
4495 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4496 "wrong annotation", return isl_ast_node_free(node));
4498 return node;
4501 /* Check that the before_each_for and after_each_for callbacks
4502 * are called for each for loop in the generated code,
4503 * that they are called in the right order and that the isl_id
4504 * returned from the before_each_for callback is attached to
4505 * the isl_ast_node passed to the corresponding after_each_for call.
4507 static int test_ast_gen1(isl_ctx *ctx)
4509 const char *str;
4510 isl_set *set;
4511 isl_union_map *schedule;
4512 isl_ast_build *build;
4513 isl_ast_node *tree;
4514 struct isl_test_codegen_data data;
4516 str = "[N] -> { : N >= 10 }";
4517 set = isl_set_read_from_str(ctx, str);
4518 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
4519 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
4520 schedule = isl_union_map_read_from_str(ctx, str);
4522 data.before = 0;
4523 data.after = 0;
4524 data.depth = 0;
4525 build = isl_ast_build_from_context(set);
4526 build = isl_ast_build_set_before_each_for(build,
4527 &before_for, &data);
4528 build = isl_ast_build_set_after_each_for(build,
4529 &after_for, &data);
4530 tree = isl_ast_build_ast_from_schedule(build, schedule);
4531 isl_ast_build_free(build);
4532 if (!tree)
4533 return -1;
4535 isl_ast_node_free(tree);
4537 if (data.before != 3 || data.after != 3)
4538 isl_die(ctx, isl_error_unknown,
4539 "unexpected number of for nodes", return -1);
4541 return 0;
4544 /* Check that the AST generator handles domains that are integrally disjoint
4545 * but not ratinoally disjoint.
4547 static int test_ast_gen2(isl_ctx *ctx)
4549 const char *str;
4550 isl_set *set;
4551 isl_union_map *schedule;
4552 isl_union_map *options;
4553 isl_ast_build *build;
4554 isl_ast_node *tree;
4556 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
4557 schedule = isl_union_map_read_from_str(ctx, str);
4558 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4559 build = isl_ast_build_from_context(set);
4561 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
4562 options = isl_union_map_read_from_str(ctx, str);
4563 build = isl_ast_build_set_options(build, options);
4564 tree = isl_ast_build_ast_from_schedule(build, schedule);
4565 isl_ast_build_free(build);
4566 if (!tree)
4567 return -1;
4568 isl_ast_node_free(tree);
4570 return 0;
4573 /* Increment *user on each call.
4575 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
4576 __isl_keep isl_ast_build *build, void *user)
4578 int *n = user;
4580 (*n)++;
4582 return node;
4585 /* Test that unrolling tries to minimize the number of instances.
4586 * In particular, for the schedule given below, make sure it generates
4587 * 3 nodes (rather than 101).
4589 static int test_ast_gen3(isl_ctx *ctx)
4591 const char *str;
4592 isl_set *set;
4593 isl_union_map *schedule;
4594 isl_union_map *options;
4595 isl_ast_build *build;
4596 isl_ast_node *tree;
4597 int n_domain = 0;
4599 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
4600 schedule = isl_union_map_read_from_str(ctx, str);
4601 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4603 str = "{ [i] -> unroll[0] }";
4604 options = isl_union_map_read_from_str(ctx, str);
4606 build = isl_ast_build_from_context(set);
4607 build = isl_ast_build_set_options(build, options);
4608 build = isl_ast_build_set_at_each_domain(build,
4609 &count_domains, &n_domain);
4610 tree = isl_ast_build_ast_from_schedule(build, schedule);
4611 isl_ast_build_free(build);
4612 if (!tree)
4613 return -1;
4615 isl_ast_node_free(tree);
4617 if (n_domain != 3)
4618 isl_die(ctx, isl_error_unknown,
4619 "unexpected number of for nodes", return -1);
4621 return 0;
4624 /* Check that if the ast_build_exploit_nested_bounds options is set,
4625 * we do not get an outer if node in the generated AST,
4626 * while we do get such an outer if node if the options is not set.
4628 static int test_ast_gen4(isl_ctx *ctx)
4630 const char *str;
4631 isl_set *set;
4632 isl_union_map *schedule;
4633 isl_ast_build *build;
4634 isl_ast_node *tree;
4635 enum isl_ast_node_type type;
4636 int enb;
4638 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4639 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4641 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4643 schedule = isl_union_map_read_from_str(ctx, str);
4644 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4645 build = isl_ast_build_from_context(set);
4646 tree = isl_ast_build_ast_from_schedule(build, schedule);
4647 isl_ast_build_free(build);
4648 if (!tree)
4649 return -1;
4651 type = isl_ast_node_get_type(tree);
4652 isl_ast_node_free(tree);
4654 if (type == isl_ast_node_if)
4655 isl_die(ctx, isl_error_unknown,
4656 "not expecting if node", return -1);
4658 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
4660 schedule = isl_union_map_read_from_str(ctx, str);
4661 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4662 build = isl_ast_build_from_context(set);
4663 tree = isl_ast_build_ast_from_schedule(build, schedule);
4664 isl_ast_build_free(build);
4665 if (!tree)
4666 return -1;
4668 type = isl_ast_node_get_type(tree);
4669 isl_ast_node_free(tree);
4671 if (type != isl_ast_node_if)
4672 isl_die(ctx, isl_error_unknown,
4673 "expecting if node", return -1);
4675 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
4677 return 0;
4680 /* This function is called for each leaf in the AST generated
4681 * from test_ast_gen5.
4683 * We finalize the AST generation by extending the outer schedule
4684 * with a zero-dimensional schedule. If this results in any for loops,
4685 * then this means that we did not pass along enough information
4686 * about the outer schedule to the inner AST generation.
4688 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
4689 void *user)
4691 isl_union_map *schedule, *extra;
4692 isl_ast_node *tree;
4694 schedule = isl_ast_build_get_schedule(build);
4695 extra = isl_union_map_copy(schedule);
4696 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
4697 schedule = isl_union_map_range_product(schedule, extra);
4698 tree = isl_ast_build_ast_from_schedule(build, schedule);
4699 isl_ast_build_free(build);
4701 if (!tree)
4702 return NULL;
4704 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
4705 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
4706 "code should not contain any for loop",
4707 return isl_ast_node_free(tree));
4709 return tree;
4712 /* Check that we do not lose any information when going back and
4713 * forth between internal and external schedule.
4715 * In particular, we create an AST where we unroll the only
4716 * non-constant dimension in the schedule. We therefore do
4717 * not expect any for loops in the AST. However, older versions
4718 * of isl would not pass along enough information about the outer
4719 * schedule when performing an inner code generation from a create_leaf
4720 * callback, resulting in the inner code generation producing a for loop.
4722 static int test_ast_gen5(isl_ctx *ctx)
4724 const char *str;
4725 isl_set *set;
4726 isl_union_map *schedule, *options;
4727 isl_ast_build *build;
4728 isl_ast_node *tree;
4730 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
4731 schedule = isl_union_map_read_from_str(ctx, str);
4733 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
4734 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
4735 options = isl_union_map_read_from_str(ctx, str);
4737 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4738 build = isl_ast_build_from_context(set);
4739 build = isl_ast_build_set_options(build, options);
4740 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
4741 tree = isl_ast_build_ast_from_schedule(build, schedule);
4742 isl_ast_build_free(build);
4743 isl_ast_node_free(tree);
4744 if (!tree)
4745 return -1;
4747 return 0;
4750 static int test_ast_gen(isl_ctx *ctx)
4752 if (test_ast_gen1(ctx) < 0)
4753 return -1;
4754 if (test_ast_gen2(ctx) < 0)
4755 return -1;
4756 if (test_ast_gen3(ctx) < 0)
4757 return -1;
4758 if (test_ast_gen4(ctx) < 0)
4759 return -1;
4760 if (test_ast_gen5(ctx) < 0)
4761 return -1;
4762 return 0;
4765 /* Check if dropping output dimensions from an isl_pw_multi_aff
4766 * works properly.
4768 static int test_pw_multi_aff(isl_ctx *ctx)
4770 const char *str;
4771 isl_pw_multi_aff *pma1, *pma2;
4772 int equal;
4774 str = "{ [i,j] -> [i+j, 4i-j] }";
4775 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4776 str = "{ [i,j] -> [4i-j] }";
4777 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4779 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
4781 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
4783 isl_pw_multi_aff_free(pma1);
4784 isl_pw_multi_aff_free(pma2);
4785 if (equal < 0)
4786 return -1;
4787 if (!equal)
4788 isl_die(ctx, isl_error_unknown,
4789 "expressions not equal", return -1);
4791 return 0;
4794 /* Check that we can properly parse multi piecewise affine expressions
4795 * where the piecewise affine expressions have different domains.
4797 static int test_multi_pw_aff(isl_ctx *ctx)
4799 const char *str;
4800 isl_set *dom, *dom2;
4801 isl_multi_pw_aff *mpa1, *mpa2;
4802 isl_pw_aff *pa;
4803 int equal;
4804 int equal_domain;
4806 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
4807 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
4808 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
4809 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
4810 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
4811 str = "{ [i] -> [(i : i > 0), 2i] }";
4812 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
4814 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
4816 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
4817 dom = isl_pw_aff_domain(pa);
4818 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
4819 dom2 = isl_pw_aff_domain(pa);
4820 equal_domain = isl_set_is_equal(dom, dom2);
4822 isl_set_free(dom);
4823 isl_set_free(dom2);
4824 isl_multi_pw_aff_free(mpa1);
4825 isl_multi_pw_aff_free(mpa2);
4827 if (equal < 0)
4828 return -1;
4829 if (!equal)
4830 isl_die(ctx, isl_error_unknown,
4831 "expressions not equal", return -1);
4833 if (equal_domain < 0)
4834 return -1;
4835 if (equal_domain)
4836 isl_die(ctx, isl_error_unknown,
4837 "domains unexpectedly equal", return -1);
4839 return 0;
4842 /* This is a regression test for a bug where isl_basic_map_simplify
4843 * would end up in an infinite loop. In particular, we construct
4844 * an empty basic set that is not obviously empty.
4845 * isl_basic_set_is_empty marks the basic set as empty.
4846 * After projecting out i3, the variable can be dropped completely,
4847 * but isl_basic_map_simplify refrains from doing so if the basic set
4848 * is empty and would end up in an infinite loop if it didn't test
4849 * explicitly for empty basic maps in the outer loop.
4851 static int test_simplify(isl_ctx *ctx)
4853 const char *str;
4854 isl_basic_set *bset;
4855 int empty;
4857 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
4858 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
4859 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
4860 "i3 >= i2 }";
4861 bset = isl_basic_set_read_from_str(ctx, str);
4862 empty = isl_basic_set_is_empty(bset);
4863 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
4864 isl_basic_set_free(bset);
4865 if (!bset)
4866 return -1;
4867 if (!empty)
4868 isl_die(ctx, isl_error_unknown,
4869 "basic set should be empty", return -1);
4871 return 0;
4874 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
4875 * with gbr context would fail to disable the use of the shifted tableau
4876 * when transferring equalities for the input to the context, resulting
4877 * in invalid sample values.
4879 static int test_partial_lexmin(isl_ctx *ctx)
4881 const char *str;
4882 isl_basic_set *bset;
4883 isl_basic_map *bmap;
4884 isl_map *map;
4886 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
4887 bmap = isl_basic_map_read_from_str(ctx, str);
4888 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
4889 bset = isl_basic_set_read_from_str(ctx, str);
4890 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
4891 isl_map_free(map);
4893 if (!map)
4894 return -1;
4896 return 0;
4899 /* Check that the variable compression performed on the existentially
4900 * quantified variables inside isl_basic_set_compute_divs is not confused
4901 * by the implicit equalities among the parameters.
4903 static int test_compute_divs(isl_ctx *ctx)
4905 const char *str;
4906 isl_basic_set *bset;
4907 isl_set *set;
4909 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
4910 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
4911 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
4912 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
4913 bset = isl_basic_set_read_from_str(ctx, str);
4914 set = isl_basic_set_compute_divs(bset);
4915 isl_set_free(set);
4916 if (!set)
4917 return -1;
4919 return 0;
4922 struct {
4923 const char *set;
4924 const char *dual;
4925 } coef_tests[] = {
4926 { "{ rat: [i] : 0 <= i <= 10 }",
4927 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
4928 { "{ rat: [i] : FALSE }",
4929 "{ rat: coefficients[[cst] -> [a]] }" },
4930 { "{ rat: [i] : }",
4931 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
4934 struct {
4935 const char *set;
4936 const char *dual;
4937 } sol_tests[] = {
4938 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
4939 "{ rat: [i] : 0 <= i <= 10 }" },
4940 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
4941 "{ rat: [i] }" },
4942 { "{ rat: coefficients[[cst] -> [a]] }",
4943 "{ rat: [i] : FALSE }" },
4946 /* Test the basic functionality of isl_basic_set_coefficients and
4947 * isl_basic_set_solutions.
4949 static int test_dual(isl_ctx *ctx)
4951 int i;
4953 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
4954 int equal;
4955 isl_basic_set *bset1, *bset2;
4957 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
4958 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
4959 bset1 = isl_basic_set_coefficients(bset1);
4960 equal = isl_basic_set_is_equal(bset1, bset2);
4961 isl_basic_set_free(bset1);
4962 isl_basic_set_free(bset2);
4963 if (equal < 0)
4964 return -1;
4965 if (!equal)
4966 isl_die(ctx, isl_error_unknown,
4967 "incorrect dual", return -1);
4970 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
4971 int equal;
4972 isl_basic_set *bset1, *bset2;
4974 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
4975 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
4976 bset1 = isl_basic_set_solutions(bset1);
4977 equal = isl_basic_set_is_equal(bset1, bset2);
4978 isl_basic_set_free(bset1);
4979 isl_basic_set_free(bset2);
4980 if (equal < 0)
4981 return -1;
4982 if (!equal)
4983 isl_die(ctx, isl_error_unknown,
4984 "incorrect dual", return -1);
4987 return 0;
4990 struct {
4991 const char *name;
4992 int (*fn)(isl_ctx *ctx);
4993 } tests [] = {
4994 { "dual", &test_dual },
4995 { "dependence analysis", &test_flow },
4996 { "val", &test_val },
4997 { "compute divs", &test_compute_divs },
4998 { "partial lexmin", &test_partial_lexmin },
4999 { "simplify", &test_simplify },
5000 { "curry", &test_curry },
5001 { "piecewise multi affine expressions", &test_pw_multi_aff },
5002 { "multi piecewise affine expressions", &test_multi_pw_aff },
5003 { "conversion", &test_conversion },
5004 { "list", &test_list },
5005 { "align parameters", &test_align_parameters },
5006 { "preimage", &test_preimage },
5007 { "pullback", &test_pullback },
5008 { "AST", &test_ast },
5009 { "AST build", &test_ast_build },
5010 { "AST generation", &test_ast_gen },
5011 { "eliminate", &test_eliminate },
5012 { "residue class", &test_residue_class },
5013 { "div", &test_div },
5014 { "slice", &test_slice },
5015 { "fixed power", &test_fixed_power },
5016 { "sample", &test_sample },
5017 { "output", &test_output },
5018 { "vertices", &test_vertices },
5019 { "fixed", &test_fixed },
5020 { "equal", &test_equal },
5021 { "disjoint", &test_disjoint },
5022 { "product", &test_product },
5023 { "dim_max", &test_dim_max },
5024 { "affine", &test_aff },
5025 { "injective", &test_injective },
5026 { "schedule", &test_schedule },
5027 { "union_pw", &test_union_pw },
5028 { "parse", &test_parse },
5029 { "single-valued", &test_sv },
5030 { "affine hull", &test_affine_hull },
5031 { "coalesce", &test_coalesce },
5032 { "factorize", &test_factorize },
5033 { "subset", &test_subset },
5034 { "subtract", &test_subtract },
5035 { "lexmin", &test_lexmin },
5036 { "min", &test_min },
5037 { "gist", &test_gist },
5038 { "piecewise quasi-polynomials", &test_pwqp },
5041 int main()
5043 int i;
5044 struct isl_ctx *ctx;
5046 srcdir = getenv("srcdir");
5047 assert(srcdir);
5049 ctx = isl_ctx_alloc();
5050 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
5051 printf("%s\n", tests[i].name);
5052 if (tests[i].fn(ctx) < 0)
5053 goto error;
5055 test_lift(ctx);
5056 test_bound(ctx);
5057 test_union(ctx);
5058 test_split_periods(ctx);
5059 test_lex(ctx);
5060 test_bijective(ctx);
5061 test_dep(ctx);
5062 test_read(ctx);
5063 test_bounded(ctx);
5064 test_construction(ctx);
5065 test_dim(ctx);
5066 test_application(ctx);
5067 test_convex_hull(ctx);
5068 test_closure(ctx);
5069 isl_ctx_free(ctx);
5070 return 0;
5071 error:
5072 isl_ctx_free(ctx);
5073 return -1;