Merge branch 'maint'
[isl.git] / isl_test.c
blob8d393f0b1a90f739a31f6198e084012c2e9049be
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>
33 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
35 static char *srcdir;
37 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
38 char *filename;
39 int length;
40 char *pattern = "%s/test_inputs/%s.%s";
42 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
43 + strlen(suffix) + 1;
44 filename = isl_alloc_array(ctx, char, length);
46 if (!filename)
47 return NULL;
49 sprintf(filename, pattern, srcdir, name, suffix);
51 return filename;
54 void test_parse_map(isl_ctx *ctx, const char *str)
56 isl_map *map;
58 map = isl_map_read_from_str(ctx, str);
59 assert(map);
60 isl_map_free(map);
63 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
65 isl_map *map, *map2;
66 int equal;
68 map = isl_map_read_from_str(ctx, str);
69 map2 = isl_map_read_from_str(ctx, str2);
70 equal = isl_map_is_equal(map, map2);
71 isl_map_free(map);
72 isl_map_free(map2);
74 if (equal < 0)
75 return -1;
76 if (!equal)
77 isl_die(ctx, isl_error_unknown, "maps not equal",
78 return -1);
80 return 0;
83 void test_parse_pwqp(isl_ctx *ctx, const char *str)
85 isl_pw_qpolynomial *pwqp;
87 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
88 assert(pwqp);
89 isl_pw_qpolynomial_free(pwqp);
92 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
94 isl_pw_aff *pwaff;
96 pwaff = isl_pw_aff_read_from_str(ctx, str);
97 assert(pwaff);
98 isl_pw_aff_free(pwaff);
101 int test_parse(struct isl_ctx *ctx)
103 isl_map *map, *map2;
104 const char *str, *str2;
106 str = "{ [i] -> [-i] }";
107 map = isl_map_read_from_str(ctx, str);
108 assert(map);
109 isl_map_free(map);
111 str = "{ A[i] -> L[([i/3])] }";
112 map = isl_map_read_from_str(ctx, str);
113 assert(map);
114 isl_map_free(map);
116 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
117 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
118 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
120 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
121 str2 = "{ [x, y] : 2y >= 6 - x }";
122 if (test_parse_map_equal(ctx, str, str2) < 0)
123 return -1;
125 if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
126 "{ [x,y] : x <= y, 2*y + 3 }") < 0)
127 return -1;
128 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
129 if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
130 str) < 0)
131 return -1;
133 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
134 map = isl_map_read_from_str(ctx, str);
135 str = "{ [new, old] -> [o0, o1] : "
136 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
137 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
138 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
139 map2 = isl_map_read_from_str(ctx, str);
140 assert(isl_map_is_equal(map, map2));
141 isl_map_free(map);
142 isl_map_free(map2);
144 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
145 map = isl_map_read_from_str(ctx, str);
146 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
147 map2 = isl_map_read_from_str(ctx, str);
148 assert(isl_map_is_equal(map, map2));
149 isl_map_free(map);
150 isl_map_free(map2);
152 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
153 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
154 if (test_parse_map_equal(ctx, str, str2) < 0)
155 return -1;
157 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
158 str2 = "{ [i,j] -> [min(i,j)] }";
159 if (test_parse_map_equal(ctx, str, str2) < 0)
160 return -1;
162 str = "{ [i,j] : i != j }";
163 str2 = "{ [i,j] : i < j or i > j }";
164 if (test_parse_map_equal(ctx, str, str2) < 0)
165 return -1;
167 str = "{ [i,j] : (i+1)*2 >= j }";
168 str2 = "{ [i, j] : j <= 2 + 2i }";
169 if (test_parse_map_equal(ctx, str, str2) < 0)
170 return -1;
172 str = "{ [i] -> [i > 0 ? 4 : 5] }";
173 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
174 if (test_parse_map_equal(ctx, str, str2) < 0)
175 return -1;
177 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
178 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
179 if (test_parse_map_equal(ctx, str, str2) < 0)
180 return -1;
182 str = "{ [x] : x >= 0 }";
183 str2 = "{ [x] : x-0 >= 0 }";
184 if (test_parse_map_equal(ctx, str, str2) < 0)
185 return -1;
187 str = "{ [i] : ((i > 10)) }";
188 str2 = "{ [i] : i >= 11 }";
189 if (test_parse_map_equal(ctx, str, str2) < 0)
190 return -1;
192 str = "{ [i] -> [0] }";
193 str2 = "{ [i] -> [0 * i] }";
194 if (test_parse_map_equal(ctx, str, str2) < 0)
195 return -1;
197 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
198 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
199 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
200 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
202 if (test_parse_map_equal(ctx, "{ [a] -> [b] : (not false) }",
203 "{ [a] -> [b] : true }") < 0)
204 return -1;
206 if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
207 "{ [i] : i <= 10 }") < 0)
208 return -1;
210 if (test_parse_map_equal(ctx, "{Sym=[n] [i] : i <= n }",
211 "[n] -> { [i] : i <= n }") < 0)
212 return -1;
214 if (test_parse_map_equal(ctx, "{ [*] }", "{ [a] }") < 0)
215 return -1;
217 if (test_parse_map_equal(ctx, "{ [i] : 2*floor(i/2) = i }",
218 "{ [i] : exists a : i = 2 a }") < 0)
219 return -1;
221 if (test_parse_map_equal(ctx, "{ [a] -> [b] : a = 5 implies b = 5 }",
222 "{ [a] -> [b] : a != 5 or b = 5 }") < 0)
223 return -1;
225 if (test_parse_map_equal(ctx, "{ [a] -> [a - 1 : a > 0] }",
226 "{ [a] -> [a - 1] : a > 0 }") < 0)
227 return -1;
228 if (test_parse_map_equal(ctx,
229 "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
230 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }") < 0)
231 return -1;
232 if (test_parse_map_equal(ctx,
233 "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
234 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
235 return -1;
236 if (test_parse_map_equal(ctx,
237 "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
238 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
239 return -1;
240 if (test_parse_map_equal(ctx,
241 "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
242 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
243 return -1;
244 if (test_parse_map_equal(ctx,
245 "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
246 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
247 return -1;
249 return 0;
252 void test_read(struct isl_ctx *ctx)
254 char *filename;
255 FILE *input;
256 struct isl_basic_set *bset1, *bset2;
257 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
259 filename = get_filename(ctx, "set", "omega");
260 assert(filename);
261 input = fopen(filename, "r");
262 assert(input);
264 bset1 = isl_basic_set_read_from_file(ctx, input);
265 bset2 = isl_basic_set_read_from_str(ctx, str);
267 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
269 isl_basic_set_free(bset1);
270 isl_basic_set_free(bset2);
271 free(filename);
273 fclose(input);
276 void test_bounded(struct isl_ctx *ctx)
278 isl_set *set;
279 int bounded;
281 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
282 bounded = isl_set_is_bounded(set);
283 assert(bounded);
284 isl_set_free(set);
286 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
287 bounded = isl_set_is_bounded(set);
288 assert(!bounded);
289 isl_set_free(set);
291 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
292 bounded = isl_set_is_bounded(set);
293 assert(!bounded);
294 isl_set_free(set);
297 /* Construct the basic set { [i] : 5 <= i <= N } */
298 void test_construction(struct isl_ctx *ctx)
300 isl_int v;
301 isl_space *dim;
302 isl_local_space *ls;
303 struct isl_basic_set *bset;
304 struct isl_constraint *c;
306 isl_int_init(v);
308 dim = isl_space_set_alloc(ctx, 1, 1);
309 bset = isl_basic_set_universe(isl_space_copy(dim));
310 ls = isl_local_space_from_space(dim);
312 c = isl_inequality_alloc(isl_local_space_copy(ls));
313 isl_int_set_si(v, -1);
314 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
315 isl_int_set_si(v, 1);
316 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
317 bset = isl_basic_set_add_constraint(bset, c);
319 c = isl_inequality_alloc(isl_local_space_copy(ls));
320 isl_int_set_si(v, 1);
321 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
322 isl_int_set_si(v, -5);
323 isl_constraint_set_constant(c, v);
324 bset = isl_basic_set_add_constraint(bset, c);
326 isl_local_space_free(ls);
327 isl_basic_set_free(bset);
329 isl_int_clear(v);
332 void test_dim(struct isl_ctx *ctx)
334 const char *str;
335 isl_map *map1, *map2;
337 map1 = isl_map_read_from_str(ctx,
338 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
339 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
340 map2 = isl_map_read_from_str(ctx,
341 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
342 assert(isl_map_is_equal(map1, map2));
343 isl_map_free(map2);
345 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
346 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
347 assert(isl_map_is_equal(map1, map2));
349 isl_map_free(map1);
350 isl_map_free(map2);
352 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
353 map1 = isl_map_read_from_str(ctx, str);
354 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
355 map2 = isl_map_read_from_str(ctx, str);
356 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
357 assert(isl_map_is_equal(map1, map2));
359 isl_map_free(map1);
360 isl_map_free(map2);
363 struct {
364 __isl_give isl_val *(*op)(__isl_take isl_val *v);
365 const char *arg;
366 const char *res;
367 } val_un_tests[] = {
368 { &isl_val_neg, "0", "0" },
369 { &isl_val_abs, "0", "0" },
370 { &isl_val_2exp, "0", "1" },
371 { &isl_val_floor, "0", "0" },
372 { &isl_val_ceil, "0", "0" },
373 { &isl_val_neg, "1", "-1" },
374 { &isl_val_neg, "-1", "1" },
375 { &isl_val_neg, "1/2", "-1/2" },
376 { &isl_val_neg, "-1/2", "1/2" },
377 { &isl_val_neg, "infty", "-infty" },
378 { &isl_val_neg, "-infty", "infty" },
379 { &isl_val_neg, "NaN", "NaN" },
380 { &isl_val_abs, "1", "1" },
381 { &isl_val_abs, "-1", "1" },
382 { &isl_val_abs, "1/2", "1/2" },
383 { &isl_val_abs, "-1/2", "1/2" },
384 { &isl_val_abs, "infty", "infty" },
385 { &isl_val_abs, "-infty", "infty" },
386 { &isl_val_abs, "NaN", "NaN" },
387 { &isl_val_floor, "1", "1" },
388 { &isl_val_floor, "-1", "-1" },
389 { &isl_val_floor, "1/2", "0" },
390 { &isl_val_floor, "-1/2", "-1" },
391 { &isl_val_floor, "infty", "infty" },
392 { &isl_val_floor, "-infty", "-infty" },
393 { &isl_val_floor, "NaN", "NaN" },
394 { &isl_val_ceil, "1", "1" },
395 { &isl_val_ceil, "-1", "-1" },
396 { &isl_val_ceil, "1/2", "1" },
397 { &isl_val_ceil, "-1/2", "0" },
398 { &isl_val_ceil, "infty", "infty" },
399 { &isl_val_ceil, "-infty", "-infty" },
400 { &isl_val_ceil, "NaN", "NaN" },
401 { &isl_val_2exp, "-3", "1/8" },
402 { &isl_val_2exp, "-1", "1/2" },
403 { &isl_val_2exp, "1", "2" },
404 { &isl_val_2exp, "2", "4" },
405 { &isl_val_2exp, "3", "8" },
408 /* Perform some basic tests of unary operations on isl_val objects.
410 static int test_un_val(isl_ctx *ctx)
412 int i;
413 isl_val *v, *res;
414 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
415 int ok;
417 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
418 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
419 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
420 fn = val_un_tests[i].op;
421 v = fn(v);
422 if (isl_val_is_nan(res))
423 ok = isl_val_is_nan(v);
424 else
425 ok = isl_val_eq(v, res);
426 isl_val_free(v);
427 isl_val_free(res);
428 if (ok < 0)
429 return -1;
430 if (!ok)
431 isl_die(ctx, isl_error_unknown,
432 "unexpected result", return -1);
435 return 0;
438 struct {
439 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
440 __isl_take isl_val *v2);
441 } val_bin_op[] = {
442 ['+'] = { &isl_val_add },
443 ['-'] = { &isl_val_sub },
444 ['*'] = { &isl_val_mul },
445 ['/'] = { &isl_val_div },
446 ['g'] = { &isl_val_gcd },
447 ['m'] = { &isl_val_min },
448 ['M'] = { &isl_val_max },
451 struct {
452 const char *arg1;
453 unsigned char op;
454 const char *arg2;
455 const char *res;
456 } val_bin_tests[] = {
457 { "0", '+', "0", "0" },
458 { "1", '+', "0", "1" },
459 { "1", '+', "1", "2" },
460 { "1", '-', "1", "0" },
461 { "1", '*', "1", "1" },
462 { "1", '/', "1", "1" },
463 { "2", '*', "3", "6" },
464 { "2", '*', "1/2", "1" },
465 { "2", '*', "1/3", "2/3" },
466 { "2/3", '*', "3/5", "2/5" },
467 { "2/3", '*', "7/5", "14/15" },
468 { "2", '/', "1/2", "4" },
469 { "-2", '/', "-1/2", "4" },
470 { "-2", '/', "1/2", "-4" },
471 { "2", '/', "-1/2", "-4" },
472 { "2", '/', "2", "1" },
473 { "2", '/', "3", "2/3" },
474 { "2/3", '/', "5/3", "2/5" },
475 { "2/3", '/', "5/7", "14/15" },
476 { "0", '/', "0", "NaN" },
477 { "42", '/', "0", "NaN" },
478 { "-42", '/', "0", "NaN" },
479 { "infty", '/', "0", "NaN" },
480 { "-infty", '/', "0", "NaN" },
481 { "NaN", '/', "0", "NaN" },
482 { "0", '/', "NaN", "NaN" },
483 { "42", '/', "NaN", "NaN" },
484 { "-42", '/', "NaN", "NaN" },
485 { "infty", '/', "NaN", "NaN" },
486 { "-infty", '/', "NaN", "NaN" },
487 { "NaN", '/', "NaN", "NaN" },
488 { "0", '/', "infty", "0" },
489 { "42", '/', "infty", "0" },
490 { "-42", '/', "infty", "0" },
491 { "infty", '/', "infty", "NaN" },
492 { "-infty", '/', "infty", "NaN" },
493 { "NaN", '/', "infty", "NaN" },
494 { "0", '/', "-infty", "0" },
495 { "42", '/', "-infty", "0" },
496 { "-42", '/', "-infty", "0" },
497 { "infty", '/', "-infty", "NaN" },
498 { "-infty", '/', "-infty", "NaN" },
499 { "NaN", '/', "-infty", "NaN" },
500 { "1", '-', "1/3", "2/3" },
501 { "1/3", '+', "1/2", "5/6" },
502 { "1/2", '+', "1/2", "1" },
503 { "3/4", '-', "1/4", "1/2" },
504 { "1/2", '-', "1/3", "1/6" },
505 { "infty", '+', "42", "infty" },
506 { "infty", '+', "infty", "infty" },
507 { "42", '+', "infty", "infty" },
508 { "infty", '-', "infty", "NaN" },
509 { "infty", '*', "infty", "infty" },
510 { "infty", '*', "-infty", "-infty" },
511 { "-infty", '*', "infty", "-infty" },
512 { "-infty", '*', "-infty", "infty" },
513 { "0", '*', "infty", "NaN" },
514 { "1", '*', "infty", "infty" },
515 { "infty", '*', "0", "NaN" },
516 { "infty", '*', "42", "infty" },
517 { "42", '-', "infty", "-infty" },
518 { "infty", '+', "-infty", "NaN" },
519 { "4", 'g', "6", "2" },
520 { "5", 'g', "6", "1" },
521 { "42", 'm', "3", "3" },
522 { "42", 'M', "3", "42" },
523 { "3", 'm', "42", "3" },
524 { "3", 'M', "42", "42" },
525 { "42", 'm', "infty", "42" },
526 { "42", 'M', "infty", "infty" },
527 { "42", 'm', "-infty", "-infty" },
528 { "42", 'M', "-infty", "42" },
529 { "42", 'm', "NaN", "NaN" },
530 { "42", 'M', "NaN", "NaN" },
531 { "infty", 'm', "-infty", "-infty" },
532 { "infty", 'M', "-infty", "infty" },
535 /* Perform some basic tests of binary operations on isl_val objects.
537 static int test_bin_val(isl_ctx *ctx)
539 int i;
540 isl_val *v1, *v2, *res;
541 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
542 __isl_take isl_val *v2);
543 int ok;
545 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
546 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
547 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
548 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
549 fn = val_bin_op[val_bin_tests[i].op].fn;
550 v1 = fn(v1, v2);
551 if (isl_val_is_nan(res))
552 ok = isl_val_is_nan(v1);
553 else
554 ok = isl_val_eq(v1, res);
555 isl_val_free(v1);
556 isl_val_free(res);
557 if (ok < 0)
558 return -1;
559 if (!ok)
560 isl_die(ctx, isl_error_unknown,
561 "unexpected result", return -1);
564 return 0;
567 /* Perform some basic tests on isl_val objects.
569 static int test_val(isl_ctx *ctx)
571 if (test_un_val(ctx) < 0)
572 return -1;
573 if (test_bin_val(ctx) < 0)
574 return -1;
575 return 0;
578 static int test_div(isl_ctx *ctx)
580 unsigned n;
581 const char *str;
582 int empty;
583 isl_int v;
584 isl_space *dim;
585 isl_set *set;
586 isl_local_space *ls;
587 struct isl_basic_set *bset;
588 struct isl_constraint *c;
590 isl_int_init(v);
592 /* test 1 */
593 dim = isl_space_set_alloc(ctx, 0, 3);
594 bset = isl_basic_set_universe(isl_space_copy(dim));
595 ls = isl_local_space_from_space(dim);
597 c = isl_equality_alloc(isl_local_space_copy(ls));
598 isl_int_set_si(v, -1);
599 isl_constraint_set_constant(c, v);
600 isl_int_set_si(v, 1);
601 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
602 isl_int_set_si(v, 3);
603 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
604 bset = isl_basic_set_add_constraint(bset, c);
606 c = isl_equality_alloc(isl_local_space_copy(ls));
607 isl_int_set_si(v, 1);
608 isl_constraint_set_constant(c, v);
609 isl_int_set_si(v, -1);
610 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
611 isl_int_set_si(v, 3);
612 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
613 bset = isl_basic_set_add_constraint(bset, c);
615 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
617 assert(bset && bset->n_div == 1);
618 isl_local_space_free(ls);
619 isl_basic_set_free(bset);
621 /* test 2 */
622 dim = isl_space_set_alloc(ctx, 0, 3);
623 bset = isl_basic_set_universe(isl_space_copy(dim));
624 ls = isl_local_space_from_space(dim);
626 c = isl_equality_alloc(isl_local_space_copy(ls));
627 isl_int_set_si(v, 1);
628 isl_constraint_set_constant(c, v);
629 isl_int_set_si(v, -1);
630 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
631 isl_int_set_si(v, 3);
632 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
633 bset = isl_basic_set_add_constraint(bset, c);
635 c = isl_equality_alloc(isl_local_space_copy(ls));
636 isl_int_set_si(v, -1);
637 isl_constraint_set_constant(c, v);
638 isl_int_set_si(v, 1);
639 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
640 isl_int_set_si(v, 3);
641 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
642 bset = isl_basic_set_add_constraint(bset, c);
644 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
646 assert(bset && bset->n_div == 1);
647 isl_local_space_free(ls);
648 isl_basic_set_free(bset);
650 /* test 3 */
651 dim = isl_space_set_alloc(ctx, 0, 3);
652 bset = isl_basic_set_universe(isl_space_copy(dim));
653 ls = isl_local_space_from_space(dim);
655 c = isl_equality_alloc(isl_local_space_copy(ls));
656 isl_int_set_si(v, 1);
657 isl_constraint_set_constant(c, v);
658 isl_int_set_si(v, -1);
659 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
660 isl_int_set_si(v, 3);
661 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
662 bset = isl_basic_set_add_constraint(bset, c);
664 c = isl_equality_alloc(isl_local_space_copy(ls));
665 isl_int_set_si(v, -3);
666 isl_constraint_set_constant(c, v);
667 isl_int_set_si(v, 1);
668 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
669 isl_int_set_si(v, 4);
670 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
671 bset = isl_basic_set_add_constraint(bset, c);
673 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
675 assert(bset && bset->n_div == 1);
676 isl_local_space_free(ls);
677 isl_basic_set_free(bset);
679 /* test 4 */
680 dim = isl_space_set_alloc(ctx, 0, 3);
681 bset = isl_basic_set_universe(isl_space_copy(dim));
682 ls = isl_local_space_from_space(dim);
684 c = isl_equality_alloc(isl_local_space_copy(ls));
685 isl_int_set_si(v, 2);
686 isl_constraint_set_constant(c, v);
687 isl_int_set_si(v, -1);
688 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
689 isl_int_set_si(v, 3);
690 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
691 bset = isl_basic_set_add_constraint(bset, c);
693 c = isl_equality_alloc(isl_local_space_copy(ls));
694 isl_int_set_si(v, -1);
695 isl_constraint_set_constant(c, v);
696 isl_int_set_si(v, 1);
697 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
698 isl_int_set_si(v, 6);
699 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
700 bset = isl_basic_set_add_constraint(bset, c);
702 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
704 assert(isl_basic_set_is_empty(bset));
705 isl_local_space_free(ls);
706 isl_basic_set_free(bset);
708 /* test 5 */
709 dim = isl_space_set_alloc(ctx, 0, 3);
710 bset = isl_basic_set_universe(isl_space_copy(dim));
711 ls = isl_local_space_from_space(dim);
713 c = isl_equality_alloc(isl_local_space_copy(ls));
714 isl_int_set_si(v, -1);
715 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
716 isl_int_set_si(v, 3);
717 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
718 bset = isl_basic_set_add_constraint(bset, c);
720 c = isl_equality_alloc(isl_local_space_copy(ls));
721 isl_int_set_si(v, 1);
722 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
723 isl_int_set_si(v, -3);
724 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
725 bset = isl_basic_set_add_constraint(bset, c);
727 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
729 assert(bset && bset->n_div == 0);
730 isl_basic_set_free(bset);
731 isl_local_space_free(ls);
733 /* test 6 */
734 dim = isl_space_set_alloc(ctx, 0, 3);
735 bset = isl_basic_set_universe(isl_space_copy(dim));
736 ls = isl_local_space_from_space(dim);
738 c = isl_equality_alloc(isl_local_space_copy(ls));
739 isl_int_set_si(v, -1);
740 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
741 isl_int_set_si(v, 6);
742 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
743 bset = isl_basic_set_add_constraint(bset, c);
745 c = isl_equality_alloc(isl_local_space_copy(ls));
746 isl_int_set_si(v, 1);
747 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
748 isl_int_set_si(v, -3);
749 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
750 bset = isl_basic_set_add_constraint(bset, c);
752 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
754 assert(bset && bset->n_div == 1);
755 isl_basic_set_free(bset);
756 isl_local_space_free(ls);
758 /* test 7 */
759 /* This test is a bit tricky. We set up an equality
760 * a + 3b + 3c = 6 e0
761 * Normalization of divs creates _two_ divs
762 * a = 3 e0
763 * c - b - e0 = 2 e1
764 * Afterwards e0 is removed again because it has coefficient -1
765 * and we end up with the original equality and div again.
766 * Perhaps we can avoid the introduction of this temporary div.
768 dim = isl_space_set_alloc(ctx, 0, 4);
769 bset = isl_basic_set_universe(isl_space_copy(dim));
770 ls = isl_local_space_from_space(dim);
772 c = isl_equality_alloc(isl_local_space_copy(ls));
773 isl_int_set_si(v, -1);
774 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
775 isl_int_set_si(v, -3);
776 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
777 isl_int_set_si(v, -3);
778 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
779 isl_int_set_si(v, 6);
780 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
781 bset = isl_basic_set_add_constraint(bset, c);
783 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
785 /* Test disabled for now */
787 assert(bset && bset->n_div == 1);
789 isl_local_space_free(ls);
790 isl_basic_set_free(bset);
792 /* test 8 */
793 dim = isl_space_set_alloc(ctx, 0, 5);
794 bset = isl_basic_set_universe(isl_space_copy(dim));
795 ls = isl_local_space_from_space(dim);
797 c = isl_equality_alloc(isl_local_space_copy(ls));
798 isl_int_set_si(v, -1);
799 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
800 isl_int_set_si(v, -3);
801 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
802 isl_int_set_si(v, -3);
803 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
804 isl_int_set_si(v, 6);
805 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
806 bset = isl_basic_set_add_constraint(bset, c);
808 c = isl_equality_alloc(isl_local_space_copy(ls));
809 isl_int_set_si(v, -1);
810 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
811 isl_int_set_si(v, 1);
812 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
813 isl_int_set_si(v, 1);
814 isl_constraint_set_constant(c, v);
815 bset = isl_basic_set_add_constraint(bset, c);
817 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
819 /* Test disabled for now */
821 assert(bset && bset->n_div == 1);
823 isl_local_space_free(ls);
824 isl_basic_set_free(bset);
826 /* test 9 */
827 dim = isl_space_set_alloc(ctx, 0, 4);
828 bset = isl_basic_set_universe(isl_space_copy(dim));
829 ls = isl_local_space_from_space(dim);
831 c = isl_equality_alloc(isl_local_space_copy(ls));
832 isl_int_set_si(v, 1);
833 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
834 isl_int_set_si(v, -1);
835 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
836 isl_int_set_si(v, -2);
837 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
838 bset = isl_basic_set_add_constraint(bset, c);
840 c = isl_equality_alloc(isl_local_space_copy(ls));
841 isl_int_set_si(v, -1);
842 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
843 isl_int_set_si(v, 3);
844 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
845 isl_int_set_si(v, 2);
846 isl_constraint_set_constant(c, v);
847 bset = isl_basic_set_add_constraint(bset, c);
849 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
851 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
853 assert(!isl_basic_set_is_empty(bset));
855 isl_local_space_free(ls);
856 isl_basic_set_free(bset);
858 /* test 10 */
859 dim = isl_space_set_alloc(ctx, 0, 3);
860 bset = isl_basic_set_universe(isl_space_copy(dim));
861 ls = isl_local_space_from_space(dim);
863 c = isl_equality_alloc(isl_local_space_copy(ls));
864 isl_int_set_si(v, 1);
865 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
866 isl_int_set_si(v, -2);
867 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
868 bset = isl_basic_set_add_constraint(bset, c);
870 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
872 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
874 isl_local_space_free(ls);
875 isl_basic_set_free(bset);
877 isl_int_clear(v);
879 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
880 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
881 set = isl_set_read_from_str(ctx, str);
882 set = isl_set_compute_divs(set);
883 isl_set_free(set);
884 if (!set)
885 return -1;
887 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
888 bset = isl_basic_set_read_from_str(ctx, str);
889 n = isl_basic_set_dim(bset, isl_dim_div);
890 isl_basic_set_free(bset);
891 if (!bset)
892 return -1;
893 if (n != 0)
894 isl_die(ctx, isl_error_unknown,
895 "expecting no existentials", return -1);
897 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
898 set = isl_set_read_from_str(ctx, str);
899 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
900 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
901 empty = isl_set_is_empty(set);
902 isl_set_free(set);
903 if (empty < 0)
904 return -1;
905 if (!empty)
906 isl_die(ctx, isl_error_unknown,
907 "result not as accurate as expected", return -1);
909 return 0;
912 void test_application_case(struct isl_ctx *ctx, const char *name)
914 char *filename;
915 FILE *input;
916 struct isl_basic_set *bset1, *bset2;
917 struct isl_basic_map *bmap;
919 filename = get_filename(ctx, name, "omega");
920 assert(filename);
921 input = fopen(filename, "r");
922 assert(input);
924 bset1 = isl_basic_set_read_from_file(ctx, input);
925 bmap = isl_basic_map_read_from_file(ctx, input);
927 bset1 = isl_basic_set_apply(bset1, bmap);
929 bset2 = isl_basic_set_read_from_file(ctx, input);
931 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
933 isl_basic_set_free(bset1);
934 isl_basic_set_free(bset2);
935 free(filename);
937 fclose(input);
940 void test_application(struct isl_ctx *ctx)
942 test_application_case(ctx, "application");
943 test_application_case(ctx, "application2");
946 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
948 char *filename;
949 FILE *input;
950 struct isl_basic_set *bset1, *bset2;
952 filename = get_filename(ctx, name, "polylib");
953 assert(filename);
954 input = fopen(filename, "r");
955 assert(input);
957 bset1 = isl_basic_set_read_from_file(ctx, input);
958 bset2 = isl_basic_set_read_from_file(ctx, input);
960 bset1 = isl_basic_set_affine_hull(bset1);
962 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
964 isl_basic_set_free(bset1);
965 isl_basic_set_free(bset2);
966 free(filename);
968 fclose(input);
971 int test_affine_hull(struct isl_ctx *ctx)
973 const char *str;
974 isl_set *set;
975 isl_basic_set *bset, *bset2;
976 int n;
977 int subset;
979 test_affine_hull_case(ctx, "affine2");
980 test_affine_hull_case(ctx, "affine");
981 test_affine_hull_case(ctx, "affine3");
983 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
984 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
985 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
986 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
987 set = isl_set_read_from_str(ctx, str);
988 bset = isl_set_affine_hull(set);
989 n = isl_basic_set_dim(bset, isl_dim_div);
990 isl_basic_set_free(bset);
991 if (n != 0)
992 isl_die(ctx, isl_error_unknown, "not expecting any divs",
993 return -1);
995 /* Check that isl_map_affine_hull is not confused by
996 * the reordering of divs in isl_map_align_divs.
998 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
999 "32e0 = b and 32e1 = c); "
1000 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1001 set = isl_set_read_from_str(ctx, str);
1002 bset = isl_set_affine_hull(set);
1003 isl_basic_set_free(bset);
1004 if (!bset)
1005 return -1;
1007 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1008 "32e2 = 31 + 31e0 }";
1009 set = isl_set_read_from_str(ctx, str);
1010 bset = isl_set_affine_hull(set);
1011 str = "{ [a] : exists e : a = 32 e }";
1012 bset2 = isl_basic_set_read_from_str(ctx, str);
1013 subset = isl_basic_set_is_subset(bset, bset2);
1014 isl_basic_set_free(bset);
1015 isl_basic_set_free(bset2);
1016 if (subset < 0)
1017 return -1;
1018 if (!subset)
1019 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1020 return -1);
1022 return 0;
1025 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1027 char *filename;
1028 FILE *input;
1029 struct isl_basic_set *bset1, *bset2;
1030 struct isl_set *set;
1032 filename = get_filename(ctx, name, "polylib");
1033 assert(filename);
1034 input = fopen(filename, "r");
1035 assert(input);
1037 bset1 = isl_basic_set_read_from_file(ctx, input);
1038 bset2 = isl_basic_set_read_from_file(ctx, input);
1040 set = isl_basic_set_union(bset1, bset2);
1041 bset1 = isl_set_convex_hull(set);
1043 bset2 = isl_basic_set_read_from_file(ctx, input);
1045 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1047 isl_basic_set_free(bset1);
1048 isl_basic_set_free(bset2);
1049 free(filename);
1051 fclose(input);
1054 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
1056 const char *str1, *str2;
1057 isl_set *set1, *set2;
1058 int orig_convex = ctx->opt->convex;
1059 ctx->opt->convex = convex;
1061 test_convex_hull_case(ctx, "convex0");
1062 test_convex_hull_case(ctx, "convex1");
1063 test_convex_hull_case(ctx, "convex2");
1064 test_convex_hull_case(ctx, "convex3");
1065 test_convex_hull_case(ctx, "convex4");
1066 test_convex_hull_case(ctx, "convex5");
1067 test_convex_hull_case(ctx, "convex6");
1068 test_convex_hull_case(ctx, "convex7");
1069 test_convex_hull_case(ctx, "convex8");
1070 test_convex_hull_case(ctx, "convex9");
1071 test_convex_hull_case(ctx, "convex10");
1072 test_convex_hull_case(ctx, "convex11");
1073 test_convex_hull_case(ctx, "convex12");
1074 test_convex_hull_case(ctx, "convex13");
1075 test_convex_hull_case(ctx, "convex14");
1076 test_convex_hull_case(ctx, "convex15");
1078 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1079 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1080 "(i0 = 0 and i1 = 0 and i2 = 0) }";
1081 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
1082 set1 = isl_set_read_from_str(ctx, str1);
1083 set2 = isl_set_read_from_str(ctx, str2);
1084 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1085 assert(isl_set_is_equal(set1, set2));
1086 isl_set_free(set1);
1087 isl_set_free(set2);
1089 ctx->opt->convex = orig_convex;
1092 void test_convex_hull(struct isl_ctx *ctx)
1094 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
1095 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
1098 void test_gist_case(struct isl_ctx *ctx, const char *name)
1100 char *filename;
1101 FILE *input;
1102 struct isl_basic_set *bset1, *bset2;
1104 filename = get_filename(ctx, name, "polylib");
1105 assert(filename);
1106 input = fopen(filename, "r");
1107 assert(input);
1109 bset1 = isl_basic_set_read_from_file(ctx, input);
1110 bset2 = isl_basic_set_read_from_file(ctx, input);
1112 bset1 = isl_basic_set_gist(bset1, bset2);
1114 bset2 = isl_basic_set_read_from_file(ctx, input);
1116 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1118 isl_basic_set_free(bset1);
1119 isl_basic_set_free(bset2);
1120 free(filename);
1122 fclose(input);
1125 static int test_gist(struct isl_ctx *ctx)
1127 const char *str;
1128 isl_basic_set *bset1, *bset2;
1129 isl_map *map1, *map2;
1131 test_gist_case(ctx, "gist1");
1133 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1134 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1135 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1136 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1137 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1138 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1139 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1140 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1141 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1142 bset1 = isl_basic_set_read_from_str(ctx, str);
1143 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1144 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1145 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1146 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1147 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1148 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1149 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1150 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1151 bset2 = isl_basic_set_read_from_str(ctx, str);
1152 bset1 = isl_basic_set_gist(bset1, bset2);
1153 assert(bset1 && bset1->n_div == 0);
1154 isl_basic_set_free(bset1);
1156 /* Check that the integer divisions of the second disjunct
1157 * do not spread to the first disjunct.
1159 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1160 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1161 "(exists (e0 = [(-1 + t1)/16], "
1162 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1163 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1164 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1165 "o0 <= 4294967295 and t1 <= -1)) }";
1166 map1 = isl_map_read_from_str(ctx, str);
1167 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1168 map2 = isl_map_read_from_str(ctx, str);
1169 map1 = isl_map_gist(map1, map2);
1170 if (!map1)
1171 return -1;
1172 if (map1->n != 1)
1173 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1174 isl_map_free(map1); return -1);
1175 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1176 isl_die(ctx, isl_error_unknown, "expecting single div",
1177 isl_map_free(map1); return -1);
1178 isl_map_free(map1);
1180 return 0;
1183 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1185 isl_set *set, *set2;
1186 int equal;
1187 int one;
1189 set = isl_set_read_from_str(ctx, str);
1190 set = isl_set_coalesce(set);
1191 set2 = isl_set_read_from_str(ctx, str);
1192 equal = isl_set_is_equal(set, set2);
1193 one = set && set->n == 1;
1194 isl_set_free(set);
1195 isl_set_free(set2);
1197 if (equal < 0)
1198 return -1;
1199 if (!equal)
1200 isl_die(ctx, isl_error_unknown,
1201 "coalesced set not equal to input", return -1);
1202 if (check_one && !one)
1203 isl_die(ctx, isl_error_unknown,
1204 "coalesced set should not be a union", return -1);
1206 return 0;
1209 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1211 int r = 0;
1212 int bounded;
1214 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1215 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1217 if (test_coalesce_set(ctx,
1218 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1219 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1220 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1221 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1222 "-10 <= y <= 0}", 1) < 0)
1223 goto error;
1224 if (test_coalesce_set(ctx,
1225 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
1226 goto error;
1227 if (test_coalesce_set(ctx,
1228 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
1229 goto error;
1231 if (0) {
1232 error:
1233 r = -1;
1236 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1238 return r;
1241 /* Inputs for coalescing tests.
1242 * "str" is a string representation of the input set.
1243 * "single_disjunct" is set if we expect the result to consist of
1244 * a single disjunct.
1246 struct {
1247 int single_disjunct;
1248 const char *str;
1249 } coalesce_tests[] = {
1250 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1251 "y >= x & x >= 2 & 5 >= y }" },
1252 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1253 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1254 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1255 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1256 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1257 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1258 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1259 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1260 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1261 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1262 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1263 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1264 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1265 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1266 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1267 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1268 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1269 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1270 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1271 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1272 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1273 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1274 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1275 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1276 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1277 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1278 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1279 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1280 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1281 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1282 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1283 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1284 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1285 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1286 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1287 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1288 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1289 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1290 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1291 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1292 "[o0, o1, o2, o3, o4, o5, o6]] : "
1293 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1294 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1295 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1296 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1297 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1298 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1299 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1300 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1301 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1302 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1303 "o6 >= i3 + i6 - o3 and M >= 0 and "
1304 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1305 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1306 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1307 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1308 "(o0 = 0 and M >= 2 and N >= 3) or "
1309 "(M = 0 and o0 = 0 and N >= 3) }" },
1310 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1311 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1312 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1313 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1314 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1315 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1316 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1317 "(y = 3 and x = 1) }" },
1318 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1319 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1320 "i1 <= M and i3 <= M and i4 <= M) or "
1321 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1322 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1323 "i4 <= -1 + M) }" },
1324 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1325 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1326 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1327 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1328 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1329 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1330 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1331 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1332 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1333 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1334 { 0, "{ [a, b] : exists e : 2e = a and "
1335 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1336 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1337 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1338 "j >= 1 and j' <= i + j - i' and i >= 1; "
1339 "[1, 1, 1, 1] }" },
1340 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1341 "[i,j] : exists a : j = 3a }" },
1342 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1343 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1344 "a >= 3) or "
1345 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1346 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1347 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1348 "c <= 6 + 8a and a >= 3; "
1349 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1350 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1351 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1352 "[x,0] : 3 <= x <= 4 }" },
1353 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1354 "[x,0] : 4 <= x <= 5 }" },
1355 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1356 "[x,0] : 3 <= x <= 5 }" },
1357 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1358 "[x,0] : 3 <= x <= 4 }" },
1359 { 1 , "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1360 "i1 <= 0; "
1361 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1364 /* Test the functionality of isl_set_coalesce.
1365 * That is, check that the output is always equal to the input
1366 * and in some cases that the result consists of a single disjunct.
1368 static int test_coalesce(struct isl_ctx *ctx)
1370 int i;
1372 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1373 const char *str = coalesce_tests[i].str;
1374 int check_one = coalesce_tests[i].single_disjunct;
1375 if (test_coalesce_set(ctx, str, check_one) < 0)
1376 return -1;
1379 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1380 return -1;
1382 return 0;
1385 void test_closure(struct isl_ctx *ctx)
1387 const char *str;
1388 isl_set *dom;
1389 isl_map *up, *right;
1390 isl_map *map, *map2;
1391 int exact;
1393 /* COCOA example 1 */
1394 map = isl_map_read_from_str(ctx,
1395 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1396 "1 <= i and i < n and 1 <= j and j < n or "
1397 "i2 = i + 1 and j2 = j - 1 and "
1398 "1 <= i and i < n and 2 <= j and j <= n }");
1399 map = isl_map_power(map, &exact);
1400 assert(exact);
1401 isl_map_free(map);
1403 /* COCOA example 1 */
1404 map = isl_map_read_from_str(ctx,
1405 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1406 "1 <= i and i < n and 1 <= j and j < n or "
1407 "i2 = i + 1 and j2 = j - 1 and "
1408 "1 <= i and i < n and 2 <= j and j <= n }");
1409 map = isl_map_transitive_closure(map, &exact);
1410 assert(exact);
1411 map2 = isl_map_read_from_str(ctx,
1412 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1413 "1 <= i and i < n and 1 <= j and j <= n and "
1414 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1415 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1416 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1417 assert(isl_map_is_equal(map, map2));
1418 isl_map_free(map2);
1419 isl_map_free(map);
1421 map = isl_map_read_from_str(ctx,
1422 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1423 " 0 <= y and y <= n }");
1424 map = isl_map_transitive_closure(map, &exact);
1425 map2 = isl_map_read_from_str(ctx,
1426 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1427 " 0 <= y and y <= n }");
1428 assert(isl_map_is_equal(map, map2));
1429 isl_map_free(map2);
1430 isl_map_free(map);
1432 /* COCOA example 2 */
1433 map = isl_map_read_from_str(ctx,
1434 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1435 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1436 "i2 = i + 2 and j2 = j - 2 and "
1437 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1438 map = isl_map_transitive_closure(map, &exact);
1439 assert(exact);
1440 map2 = isl_map_read_from_str(ctx,
1441 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1442 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1443 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1444 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1445 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1446 assert(isl_map_is_equal(map, map2));
1447 isl_map_free(map);
1448 isl_map_free(map2);
1450 /* COCOA Fig.2 left */
1451 map = isl_map_read_from_str(ctx,
1452 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1453 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1454 "j <= n or "
1455 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1456 "j <= 2 i - 3 and j <= n - 2 or "
1457 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1458 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1459 map = isl_map_transitive_closure(map, &exact);
1460 assert(exact);
1461 isl_map_free(map);
1463 /* COCOA Fig.2 right */
1464 map = isl_map_read_from_str(ctx,
1465 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1466 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1467 "j <= n or "
1468 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1469 "j <= 2 i - 4 and j <= n - 3 or "
1470 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1471 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1472 map = isl_map_power(map, &exact);
1473 assert(exact);
1474 isl_map_free(map);
1476 /* COCOA Fig.2 right */
1477 map = isl_map_read_from_str(ctx,
1478 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1479 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1480 "j <= n or "
1481 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1482 "j <= 2 i - 4 and j <= n - 3 or "
1483 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1484 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1485 map = isl_map_transitive_closure(map, &exact);
1486 assert(exact);
1487 map2 = isl_map_read_from_str(ctx,
1488 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1489 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1490 "j <= n and 3 + i + 2 j <= 3 n and "
1491 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1492 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1493 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1494 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1495 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1496 assert(isl_map_is_equal(map, map2));
1497 isl_map_free(map2);
1498 isl_map_free(map);
1500 /* COCOA Fig.1 right */
1501 dom = isl_set_read_from_str(ctx,
1502 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1503 "2 x - 3 y + 3 >= 0 }");
1504 right = isl_map_read_from_str(ctx,
1505 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1506 up = isl_map_read_from_str(ctx,
1507 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1508 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1509 right = isl_map_intersect_range(right, isl_set_copy(dom));
1510 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1511 up = isl_map_intersect_range(up, dom);
1512 map = isl_map_union(up, right);
1513 map = isl_map_transitive_closure(map, &exact);
1514 assert(exact);
1515 map2 = isl_map_read_from_str(ctx,
1516 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1517 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1518 assert(isl_map_is_equal(map, map2));
1519 isl_map_free(map2);
1520 isl_map_free(map);
1522 /* COCOA Theorem 1 counter example */
1523 map = isl_map_read_from_str(ctx,
1524 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1525 "i2 = 1 and j2 = j or "
1526 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1527 map = isl_map_transitive_closure(map, &exact);
1528 assert(exact);
1529 isl_map_free(map);
1531 map = isl_map_read_from_str(ctx,
1532 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1533 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1534 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1535 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1536 map = isl_map_transitive_closure(map, &exact);
1537 assert(exact);
1538 isl_map_free(map);
1540 /* Kelly et al 1996, fig 12 */
1541 map = isl_map_read_from_str(ctx,
1542 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1543 "1 <= i,j,j+1 <= n or "
1544 "j = n and j2 = 1 and i2 = i + 1 and "
1545 "1 <= i,i+1 <= n }");
1546 map = isl_map_transitive_closure(map, &exact);
1547 assert(exact);
1548 map2 = isl_map_read_from_str(ctx,
1549 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1550 "1 <= i <= n and i = i2 or "
1551 "1 <= i < i2 <= n and 1 <= j <= n and "
1552 "1 <= j2 <= n }");
1553 assert(isl_map_is_equal(map, map2));
1554 isl_map_free(map2);
1555 isl_map_free(map);
1557 /* Omega's closure4 */
1558 map = isl_map_read_from_str(ctx,
1559 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1560 "1 <= x,y <= 10 or "
1561 "x2 = x + 1 and y2 = y and "
1562 "1 <= x <= 20 && 5 <= y <= 15 }");
1563 map = isl_map_transitive_closure(map, &exact);
1564 assert(exact);
1565 isl_map_free(map);
1567 map = isl_map_read_from_str(ctx,
1568 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1569 map = isl_map_transitive_closure(map, &exact);
1570 assert(!exact);
1571 map2 = isl_map_read_from_str(ctx,
1572 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1573 assert(isl_map_is_equal(map, map2));
1574 isl_map_free(map);
1575 isl_map_free(map2);
1577 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1578 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1579 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1580 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1581 map = isl_map_read_from_str(ctx, str);
1582 map = isl_map_transitive_closure(map, &exact);
1583 assert(exact);
1584 map2 = isl_map_read_from_str(ctx, str);
1585 assert(isl_map_is_equal(map, map2));
1586 isl_map_free(map);
1587 isl_map_free(map2);
1589 str = "{[0] -> [1]; [2] -> [3]}";
1590 map = isl_map_read_from_str(ctx, str);
1591 map = isl_map_transitive_closure(map, &exact);
1592 assert(exact);
1593 map2 = isl_map_read_from_str(ctx, str);
1594 assert(isl_map_is_equal(map, map2));
1595 isl_map_free(map);
1596 isl_map_free(map2);
1598 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1599 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1600 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1601 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1602 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1603 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1604 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1605 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1606 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1607 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1608 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1609 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1610 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1611 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1612 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1613 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1614 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1615 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1616 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1617 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1618 map = isl_map_read_from_str(ctx, str);
1619 map = isl_map_transitive_closure(map, NULL);
1620 assert(map);
1621 isl_map_free(map);
1624 void test_lex(struct isl_ctx *ctx)
1626 isl_space *dim;
1627 isl_map *map;
1629 dim = isl_space_set_alloc(ctx, 0, 0);
1630 map = isl_map_lex_le(dim);
1631 assert(!isl_map_is_empty(map));
1632 isl_map_free(map);
1635 static int test_lexmin(struct isl_ctx *ctx)
1637 int equal;
1638 const char *str;
1639 isl_basic_map *bmap;
1640 isl_map *map, *map2;
1641 isl_set *set;
1642 isl_set *set2;
1643 isl_pw_multi_aff *pma;
1645 str = "[p0, p1] -> { [] -> [] : "
1646 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1647 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1648 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1649 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1650 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1651 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1652 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1653 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1654 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1655 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1656 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1657 map = isl_map_read_from_str(ctx, str);
1658 map = isl_map_lexmin(map);
1659 isl_map_free(map);
1661 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1662 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1663 set = isl_set_read_from_str(ctx, str);
1664 set = isl_set_lexmax(set);
1665 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1666 set2 = isl_set_read_from_str(ctx, str);
1667 set = isl_set_intersect(set, set2);
1668 assert(!isl_set_is_empty(set));
1669 isl_set_free(set);
1671 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1672 map = isl_map_read_from_str(ctx, str);
1673 map = isl_map_lexmin(map);
1674 str = "{ [x] -> [5] : 6 <= x <= 8; "
1675 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1676 map2 = isl_map_read_from_str(ctx, str);
1677 assert(isl_map_is_equal(map, map2));
1678 isl_map_free(map);
1679 isl_map_free(map2);
1681 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1682 map = isl_map_read_from_str(ctx, str);
1683 map2 = isl_map_copy(map);
1684 map = isl_map_lexmin(map);
1685 assert(isl_map_is_equal(map, map2));
1686 isl_map_free(map);
1687 isl_map_free(map2);
1689 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1690 map = isl_map_read_from_str(ctx, str);
1691 map = isl_map_lexmin(map);
1692 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1693 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1694 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1695 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1696 map2 = isl_map_read_from_str(ctx, str);
1697 assert(isl_map_is_equal(map, map2));
1698 isl_map_free(map);
1699 isl_map_free(map2);
1701 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1702 " 8i' <= i and 8i' >= -7 + i }";
1703 bmap = isl_basic_map_read_from_str(ctx, str);
1704 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1705 map2 = isl_map_from_pw_multi_aff(pma);
1706 map = isl_map_from_basic_map(bmap);
1707 assert(isl_map_is_equal(map, map2));
1708 isl_map_free(map);
1709 isl_map_free(map2);
1711 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1712 map = isl_map_read_from_str(ctx, str);
1713 map = isl_map_lexmin(map);
1714 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
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 /* Check that empty pieces are properly combined. */
1721 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1722 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1723 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1724 map = isl_map_read_from_str(ctx, str);
1725 map = isl_map_lexmin(map);
1726 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1727 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1728 "x >= 4 }";
1729 map2 = isl_map_read_from_str(ctx, str);
1730 assert(isl_map_is_equal(map, map2));
1731 isl_map_free(map);
1732 isl_map_free(map2);
1734 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1735 " 8i' <= i and 8i' >= -7 + i }";
1736 set = isl_set_read_from_str(ctx, str);
1737 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1738 set2 = isl_set_from_pw_multi_aff(pma);
1739 equal = isl_set_is_equal(set, set2);
1740 isl_set_free(set);
1741 isl_set_free(set2);
1742 if (equal < 0)
1743 return -1;
1744 if (!equal)
1745 isl_die(ctx, isl_error_unknown,
1746 "unexpected difference between set and "
1747 "piecewise affine expression", return -1);
1749 return 0;
1752 struct must_may {
1753 isl_map *must;
1754 isl_map *may;
1757 static int collect_must_may(__isl_take isl_map *dep, int must,
1758 void *dep_user, void *user)
1760 struct must_may *mm = (struct must_may *)user;
1762 if (must)
1763 mm->must = isl_map_union(mm->must, dep);
1764 else
1765 mm->may = isl_map_union(mm->may, dep);
1767 return 0;
1770 static int common_space(void *first, void *second)
1772 int depth = *(int *)first;
1773 return 2 * depth;
1776 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1778 isl_map *map2;
1779 int equal;
1781 if (!map)
1782 return -1;
1784 map2 = isl_map_read_from_str(map->ctx, str);
1785 equal = isl_map_is_equal(map, map2);
1786 isl_map_free(map2);
1788 return equal;
1791 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1793 int equal;
1795 equal = map_is_equal(map, str);
1796 if (equal < 0)
1797 return -1;
1798 if (!equal)
1799 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1800 "result not as expected", return -1);
1801 return 0;
1804 void test_dep(struct isl_ctx *ctx)
1806 const char *str;
1807 isl_space *dim;
1808 isl_map *map;
1809 isl_access_info *ai;
1810 isl_flow *flow;
1811 int depth;
1812 struct must_may mm;
1814 depth = 3;
1816 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1817 map = isl_map_read_from_str(ctx, str);
1818 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1820 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1821 map = isl_map_read_from_str(ctx, str);
1822 ai = isl_access_info_add_source(ai, map, 1, &depth);
1824 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1825 map = isl_map_read_from_str(ctx, str);
1826 ai = isl_access_info_add_source(ai, map, 1, &depth);
1828 flow = isl_access_info_compute_flow(ai);
1829 dim = isl_space_alloc(ctx, 0, 3, 3);
1830 mm.must = isl_map_empty(isl_space_copy(dim));
1831 mm.may = isl_map_empty(dim);
1833 isl_flow_foreach(flow, collect_must_may, &mm);
1835 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1836 " [1,10,0] -> [2,5,0] }";
1837 assert(map_is_equal(mm.must, str));
1838 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1839 assert(map_is_equal(mm.may, str));
1841 isl_map_free(mm.must);
1842 isl_map_free(mm.may);
1843 isl_flow_free(flow);
1846 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1847 map = isl_map_read_from_str(ctx, str);
1848 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1850 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1851 map = isl_map_read_from_str(ctx, str);
1852 ai = isl_access_info_add_source(ai, map, 1, &depth);
1854 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1855 map = isl_map_read_from_str(ctx, str);
1856 ai = isl_access_info_add_source(ai, map, 0, &depth);
1858 flow = isl_access_info_compute_flow(ai);
1859 dim = isl_space_alloc(ctx, 0, 3, 3);
1860 mm.must = isl_map_empty(isl_space_copy(dim));
1861 mm.may = isl_map_empty(dim);
1863 isl_flow_foreach(flow, collect_must_may, &mm);
1865 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1866 assert(map_is_equal(mm.must, str));
1867 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1868 assert(map_is_equal(mm.may, str));
1870 isl_map_free(mm.must);
1871 isl_map_free(mm.may);
1872 isl_flow_free(flow);
1875 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1876 map = isl_map_read_from_str(ctx, str);
1877 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1879 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1880 map = isl_map_read_from_str(ctx, str);
1881 ai = isl_access_info_add_source(ai, map, 0, &depth);
1883 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1884 map = isl_map_read_from_str(ctx, str);
1885 ai = isl_access_info_add_source(ai, map, 0, &depth);
1887 flow = isl_access_info_compute_flow(ai);
1888 dim = isl_space_alloc(ctx, 0, 3, 3);
1889 mm.must = isl_map_empty(isl_space_copy(dim));
1890 mm.may = isl_map_empty(dim);
1892 isl_flow_foreach(flow, collect_must_may, &mm);
1894 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1895 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1896 assert(map_is_equal(mm.may, str));
1897 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1898 assert(map_is_equal(mm.must, str));
1900 isl_map_free(mm.must);
1901 isl_map_free(mm.may);
1902 isl_flow_free(flow);
1905 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1906 map = isl_map_read_from_str(ctx, str);
1907 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1909 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1910 map = isl_map_read_from_str(ctx, str);
1911 ai = isl_access_info_add_source(ai, map, 0, &depth);
1913 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1914 map = isl_map_read_from_str(ctx, str);
1915 ai = isl_access_info_add_source(ai, map, 0, &depth);
1917 flow = isl_access_info_compute_flow(ai);
1918 dim = isl_space_alloc(ctx, 0, 3, 3);
1919 mm.must = isl_map_empty(isl_space_copy(dim));
1920 mm.may = isl_map_empty(dim);
1922 isl_flow_foreach(flow, collect_must_may, &mm);
1924 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1925 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1926 assert(map_is_equal(mm.may, str));
1927 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1928 assert(map_is_equal(mm.must, str));
1930 isl_map_free(mm.must);
1931 isl_map_free(mm.may);
1932 isl_flow_free(flow);
1935 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1936 map = isl_map_read_from_str(ctx, str);
1937 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1939 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1940 map = isl_map_read_from_str(ctx, str);
1941 ai = isl_access_info_add_source(ai, map, 0, &depth);
1943 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1944 map = isl_map_read_from_str(ctx, str);
1945 ai = isl_access_info_add_source(ai, map, 0, &depth);
1947 flow = isl_access_info_compute_flow(ai);
1948 dim = isl_space_alloc(ctx, 0, 3, 3);
1949 mm.must = isl_map_empty(isl_space_copy(dim));
1950 mm.may = isl_map_empty(dim);
1952 isl_flow_foreach(flow, collect_must_may, &mm);
1954 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1955 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1956 assert(map_is_equal(mm.may, str));
1957 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1958 assert(map_is_equal(mm.must, str));
1960 isl_map_free(mm.must);
1961 isl_map_free(mm.may);
1962 isl_flow_free(flow);
1965 depth = 5;
1967 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1968 map = isl_map_read_from_str(ctx, str);
1969 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1971 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1972 map = isl_map_read_from_str(ctx, str);
1973 ai = isl_access_info_add_source(ai, map, 1, &depth);
1975 flow = isl_access_info_compute_flow(ai);
1976 dim = isl_space_alloc(ctx, 0, 5, 5);
1977 mm.must = isl_map_empty(isl_space_copy(dim));
1978 mm.may = isl_map_empty(dim);
1980 isl_flow_foreach(flow, collect_must_may, &mm);
1982 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1983 assert(map_is_equal(mm.must, str));
1984 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1985 assert(map_is_equal(mm.may, str));
1987 isl_map_free(mm.must);
1988 isl_map_free(mm.may);
1989 isl_flow_free(flow);
1992 int test_sv(isl_ctx *ctx)
1994 const char *str;
1995 isl_map *map;
1996 isl_union_map *umap;
1997 int sv;
1999 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
2000 map = isl_map_read_from_str(ctx, str);
2001 sv = isl_map_is_single_valued(map);
2002 isl_map_free(map);
2003 if (sv < 0)
2004 return -1;
2005 if (!sv)
2006 isl_die(ctx, isl_error_internal,
2007 "map not detected as single valued", return -1);
2009 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
2010 map = isl_map_read_from_str(ctx, str);
2011 sv = isl_map_is_single_valued(map);
2012 isl_map_free(map);
2013 if (sv < 0)
2014 return -1;
2015 if (sv)
2016 isl_die(ctx, isl_error_internal,
2017 "map detected as single valued", return -1);
2019 str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
2020 umap = isl_union_map_read_from_str(ctx, str);
2021 sv = isl_union_map_is_single_valued(umap);
2022 isl_union_map_free(umap);
2023 if (sv < 0)
2024 return -1;
2025 if (!sv)
2026 isl_die(ctx, isl_error_internal,
2027 "map not detected as single valued", return -1);
2029 str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
2030 umap = isl_union_map_read_from_str(ctx, str);
2031 sv = isl_union_map_is_single_valued(umap);
2032 isl_union_map_free(umap);
2033 if (sv < 0)
2034 return -1;
2035 if (sv)
2036 isl_die(ctx, isl_error_internal,
2037 "map detected as single valued", return -1);
2039 return 0;
2042 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
2044 isl_map *map;
2046 map = isl_map_read_from_str(ctx, str);
2047 if (bijective)
2048 assert(isl_map_is_bijective(map));
2049 else
2050 assert(!isl_map_is_bijective(map));
2051 isl_map_free(map);
2054 void test_bijective(struct isl_ctx *ctx)
2056 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
2057 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
2058 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
2059 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
2060 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
2061 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
2062 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
2063 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
2064 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
2065 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
2066 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
2067 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
2068 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
2071 static int test_pwqp(struct isl_ctx *ctx)
2073 const char *str;
2074 isl_set *set;
2075 isl_pw_qpolynomial *pwqp1, *pwqp2;
2076 int equal;
2078 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2079 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2081 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2082 isl_dim_in, 1, 1);
2084 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2085 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2087 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2089 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2091 isl_pw_qpolynomial_free(pwqp1);
2093 str = "{ [i] -> i }";
2094 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2095 str = "{ [k] : exists a : k = 2a }";
2096 set = isl_set_read_from_str(ctx, str);
2097 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2098 str = "{ [i] -> i }";
2099 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2101 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2103 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2105 isl_pw_qpolynomial_free(pwqp1);
2107 str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
2108 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2109 str = "{ [10] }";
2110 set = isl_set_read_from_str(ctx, str);
2111 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2112 str = "{ [i] -> 16 }";
2113 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2115 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2117 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2119 isl_pw_qpolynomial_free(pwqp1);
2121 str = "{ [i] -> ([(i)/2]) }";
2122 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2123 str = "{ [k] : exists a : k = 2a+1 }";
2124 set = isl_set_read_from_str(ctx, str);
2125 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2126 str = "{ [i] -> -1/2 + 1/2 * i }";
2127 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2129 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2131 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2133 isl_pw_qpolynomial_free(pwqp1);
2135 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2136 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2137 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2138 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2140 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2142 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2144 isl_pw_qpolynomial_free(pwqp1);
2146 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2147 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2148 str = "{ [x] -> x }";
2149 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2151 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2153 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2155 isl_pw_qpolynomial_free(pwqp1);
2157 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2158 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2159 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2160 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2161 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2162 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2163 isl_pw_qpolynomial_free(pwqp1);
2165 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2166 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2167 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2168 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2169 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2170 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2171 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2172 isl_pw_qpolynomial_free(pwqp1);
2173 isl_pw_qpolynomial_free(pwqp2);
2174 if (equal < 0)
2175 return -1;
2176 if (!equal)
2177 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2179 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2180 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2181 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2182 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2183 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2184 isl_val_one(ctx));
2185 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2186 isl_pw_qpolynomial_free(pwqp1);
2187 isl_pw_qpolynomial_free(pwqp2);
2188 if (equal < 0)
2189 return -1;
2190 if (!equal)
2191 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2193 return 0;
2196 void test_split_periods(isl_ctx *ctx)
2198 const char *str;
2199 isl_pw_qpolynomial *pwqp;
2201 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2202 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2203 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2204 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2206 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2207 assert(pwqp);
2209 isl_pw_qpolynomial_free(pwqp);
2212 void test_union(isl_ctx *ctx)
2214 const char *str;
2215 isl_union_set *uset1, *uset2;
2216 isl_union_map *umap1, *umap2;
2218 str = "{ [i] : 0 <= i <= 1 }";
2219 uset1 = isl_union_set_read_from_str(ctx, str);
2220 str = "{ [1] -> [0] }";
2221 umap1 = isl_union_map_read_from_str(ctx, str);
2223 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2224 assert(isl_union_map_is_equal(umap1, umap2));
2226 isl_union_map_free(umap1);
2227 isl_union_map_free(umap2);
2229 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2230 umap1 = isl_union_map_read_from_str(ctx, str);
2231 str = "{ A[i]; B[i] }";
2232 uset1 = isl_union_set_read_from_str(ctx, str);
2234 uset2 = isl_union_map_domain(umap1);
2236 assert(isl_union_set_is_equal(uset1, uset2));
2238 isl_union_set_free(uset1);
2239 isl_union_set_free(uset2);
2242 void test_bound(isl_ctx *ctx)
2244 const char *str;
2245 isl_pw_qpolynomial *pwqp;
2246 isl_pw_qpolynomial_fold *pwf;
2248 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2249 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2250 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2251 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
2252 isl_pw_qpolynomial_fold_free(pwf);
2254 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2255 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2256 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2257 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
2258 isl_pw_qpolynomial_fold_free(pwf);
2261 void test_lift(isl_ctx *ctx)
2263 const char *str;
2264 isl_basic_map *bmap;
2265 isl_basic_set *bset;
2267 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2268 bset = isl_basic_set_read_from_str(ctx, str);
2269 bset = isl_basic_set_lift(bset);
2270 bmap = isl_basic_map_from_range(bset);
2271 bset = isl_basic_map_domain(bmap);
2272 isl_basic_set_free(bset);
2275 struct {
2276 const char *set1;
2277 const char *set2;
2278 int subset;
2279 } subset_tests[] = {
2280 { "{ [112, 0] }",
2281 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2282 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2283 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2284 { "{ [65] }",
2285 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2286 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2287 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2288 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2289 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2290 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2291 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2292 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2293 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2294 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2295 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2296 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2297 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2300 static int test_subset(isl_ctx *ctx)
2302 int i;
2303 isl_set *set1, *set2;
2304 int subset;
2306 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2307 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2308 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2309 subset = isl_set_is_subset(set1, set2);
2310 isl_set_free(set1);
2311 isl_set_free(set2);
2312 if (subset < 0)
2313 return -1;
2314 if (subset != subset_tests[i].subset)
2315 isl_die(ctx, isl_error_unknown,
2316 "incorrect subset result", return -1);
2319 return 0;
2322 struct {
2323 const char *minuend;
2324 const char *subtrahend;
2325 const char *difference;
2326 } subtract_domain_tests[] = {
2327 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2328 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2329 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2332 static int test_subtract(isl_ctx *ctx)
2334 int i;
2335 isl_union_map *umap1, *umap2;
2336 isl_union_set *uset;
2337 int equal;
2339 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2340 umap1 = isl_union_map_read_from_str(ctx,
2341 subtract_domain_tests[i].minuend);
2342 uset = isl_union_set_read_from_str(ctx,
2343 subtract_domain_tests[i].subtrahend);
2344 umap2 = isl_union_map_read_from_str(ctx,
2345 subtract_domain_tests[i].difference);
2346 umap1 = isl_union_map_subtract_domain(umap1, uset);
2347 equal = isl_union_map_is_equal(umap1, umap2);
2348 isl_union_map_free(umap1);
2349 isl_union_map_free(umap2);
2350 if (equal < 0)
2351 return -1;
2352 if (!equal)
2353 isl_die(ctx, isl_error_unknown,
2354 "incorrect subtract domain result", return -1);
2357 return 0;
2360 int test_factorize(isl_ctx *ctx)
2362 const char *str;
2363 isl_basic_set *bset;
2364 isl_factorizer *f;
2366 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2367 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2368 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2369 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2370 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2371 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2372 "3i5 >= -2i0 - i2 + 3i4 }";
2373 bset = isl_basic_set_read_from_str(ctx, str);
2374 f = isl_basic_set_factorizer(bset);
2375 isl_basic_set_free(bset);
2376 isl_factorizer_free(f);
2377 if (!f)
2378 isl_die(ctx, isl_error_unknown,
2379 "failed to construct factorizer", return -1);
2381 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2382 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2383 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2384 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2385 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2386 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2387 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2388 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2389 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2390 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2391 bset = isl_basic_set_read_from_str(ctx, str);
2392 f = isl_basic_set_factorizer(bset);
2393 isl_basic_set_free(bset);
2394 isl_factorizer_free(f);
2395 if (!f)
2396 isl_die(ctx, isl_error_unknown,
2397 "failed to construct factorizer", return -1);
2399 return 0;
2402 static int check_injective(__isl_take isl_map *map, void *user)
2404 int *injective = user;
2406 *injective = isl_map_is_injective(map);
2407 isl_map_free(map);
2409 if (*injective < 0 || !*injective)
2410 return -1;
2412 return 0;
2415 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2416 const char *r, const char *s, int tilable, int parallel)
2418 int i;
2419 isl_union_set *D;
2420 isl_union_map *W, *R, *S;
2421 isl_union_map *empty;
2422 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2423 isl_union_map *validity, *proximity, *coincidence;
2424 isl_union_map *schedule;
2425 isl_union_map *test;
2426 isl_union_set *delta;
2427 isl_union_set *domain;
2428 isl_set *delta_set;
2429 isl_set *slice;
2430 isl_set *origin;
2431 isl_schedule_constraints *sc;
2432 isl_schedule *sched;
2433 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2435 D = isl_union_set_read_from_str(ctx, d);
2436 W = isl_union_map_read_from_str(ctx, w);
2437 R = isl_union_map_read_from_str(ctx, r);
2438 S = isl_union_map_read_from_str(ctx, s);
2440 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2441 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2443 empty = isl_union_map_empty(isl_union_map_get_space(S));
2444 isl_union_map_compute_flow(isl_union_map_copy(R),
2445 isl_union_map_copy(W), empty,
2446 isl_union_map_copy(S),
2447 &dep_raw, NULL, NULL, NULL);
2448 isl_union_map_compute_flow(isl_union_map_copy(W),
2449 isl_union_map_copy(W),
2450 isl_union_map_copy(R),
2451 isl_union_map_copy(S),
2452 &dep_waw, &dep_war, NULL, NULL);
2454 dep = isl_union_map_union(dep_waw, dep_war);
2455 dep = isl_union_map_union(dep, dep_raw);
2456 validity = isl_union_map_copy(dep);
2457 coincidence = isl_union_map_copy(dep);
2458 proximity = isl_union_map_copy(dep);
2460 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
2461 sc = isl_schedule_constraints_set_validity(sc, validity);
2462 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
2463 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2464 sched = isl_schedule_constraints_compute_schedule(sc);
2465 schedule = isl_schedule_get_map(sched);
2466 isl_schedule_free(sched);
2467 isl_union_map_free(W);
2468 isl_union_map_free(R);
2469 isl_union_map_free(S);
2471 is_injection = 1;
2472 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2474 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2475 is_complete = isl_union_set_is_subset(D, domain);
2476 isl_union_set_free(D);
2477 isl_union_set_free(domain);
2479 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2480 test = isl_union_map_apply_range(test, dep);
2481 test = isl_union_map_apply_range(test, schedule);
2483 delta = isl_union_map_deltas(test);
2484 if (isl_union_set_n_set(delta) == 0) {
2485 is_tilable = 1;
2486 is_parallel = 1;
2487 is_nonneg = 1;
2488 isl_union_set_free(delta);
2489 } else {
2490 delta_set = isl_set_from_union_set(delta);
2492 slice = isl_set_universe(isl_set_get_space(delta_set));
2493 for (i = 0; i < tilable; ++i)
2494 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2495 is_tilable = isl_set_is_subset(delta_set, slice);
2496 isl_set_free(slice);
2498 slice = isl_set_universe(isl_set_get_space(delta_set));
2499 for (i = 0; i < parallel; ++i)
2500 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2501 is_parallel = isl_set_is_subset(delta_set, slice);
2502 isl_set_free(slice);
2504 origin = isl_set_universe(isl_set_get_space(delta_set));
2505 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2506 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2508 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2509 delta_set = isl_set_lexmin(delta_set);
2511 is_nonneg = isl_set_is_equal(delta_set, origin);
2513 isl_set_free(origin);
2514 isl_set_free(delta_set);
2517 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2518 is_injection < 0 || is_complete < 0)
2519 return -1;
2520 if (!is_complete)
2521 isl_die(ctx, isl_error_unknown,
2522 "generated schedule incomplete", return -1);
2523 if (!is_injection)
2524 isl_die(ctx, isl_error_unknown,
2525 "generated schedule not injective on each statement",
2526 return -1);
2527 if (!is_nonneg)
2528 isl_die(ctx, isl_error_unknown,
2529 "negative dependences in generated schedule",
2530 return -1);
2531 if (!is_tilable)
2532 isl_die(ctx, isl_error_unknown,
2533 "generated schedule not as tilable as expected",
2534 return -1);
2535 if (!is_parallel)
2536 isl_die(ctx, isl_error_unknown,
2537 "generated schedule not as parallel as expected",
2538 return -1);
2540 return 0;
2543 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2544 const char *domain, const char *validity, const char *proximity)
2546 isl_union_set *dom;
2547 isl_union_map *dep;
2548 isl_union_map *prox;
2549 isl_schedule_constraints *sc;
2550 isl_schedule *schedule;
2551 isl_union_map *sched;
2553 dom = isl_union_set_read_from_str(ctx, domain);
2554 dep = isl_union_map_read_from_str(ctx, validity);
2555 prox = isl_union_map_read_from_str(ctx, proximity);
2556 sc = isl_schedule_constraints_on_domain(dom);
2557 sc = isl_schedule_constraints_set_validity(sc, dep);
2558 sc = isl_schedule_constraints_set_proximity(sc, prox);
2559 schedule = isl_schedule_constraints_compute_schedule(sc);
2560 sched = isl_schedule_get_map(schedule);
2561 isl_schedule_free(schedule);
2563 return sched;
2566 /* Check that a schedule can be constructed on the given domain
2567 * with the given validity and proximity constraints.
2569 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2570 const char *validity, const char *proximity)
2572 isl_union_map *sched;
2574 sched = compute_schedule(ctx, domain, validity, proximity);
2575 if (!sched)
2576 return -1;
2578 isl_union_map_free(sched);
2579 return 0;
2582 int test_special_schedule(isl_ctx *ctx, const char *domain,
2583 const char *validity, const char *proximity, const char *expected_sched)
2585 isl_union_map *sched1, *sched2;
2586 int equal;
2588 sched1 = compute_schedule(ctx, domain, validity, proximity);
2589 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2591 equal = isl_union_map_is_equal(sched1, sched2);
2592 isl_union_map_free(sched1);
2593 isl_union_map_free(sched2);
2595 if (equal < 0)
2596 return -1;
2597 if (!equal)
2598 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2599 return -1);
2601 return 0;
2604 /* Check that the schedule map is properly padded, even after being
2605 * reconstructed from the band forest.
2607 static int test_padded_schedule(isl_ctx *ctx)
2609 const char *str;
2610 isl_union_set *D;
2611 isl_union_map *validity, *proximity;
2612 isl_schedule_constraints *sc;
2613 isl_schedule *sched;
2614 isl_union_map *map1, *map2;
2615 isl_band_list *list;
2616 int equal;
2618 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2619 D = isl_union_set_read_from_str(ctx, str);
2620 validity = isl_union_map_empty(isl_union_set_get_space(D));
2621 proximity = isl_union_map_copy(validity);
2622 sc = isl_schedule_constraints_on_domain(D);
2623 sc = isl_schedule_constraints_set_validity(sc, validity);
2624 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2625 sched = isl_schedule_constraints_compute_schedule(sc);
2626 map1 = isl_schedule_get_map(sched);
2627 list = isl_schedule_get_band_forest(sched);
2628 isl_band_list_free(list);
2629 map2 = isl_schedule_get_map(sched);
2630 isl_schedule_free(sched);
2631 equal = isl_union_map_is_equal(map1, map2);
2632 isl_union_map_free(map1);
2633 isl_union_map_free(map2);
2635 if (equal < 0)
2636 return -1;
2637 if (!equal)
2638 isl_die(ctx, isl_error_unknown,
2639 "reconstructed schedule map not the same as original",
2640 return -1);
2642 return 0;
2645 /* Input for testing of schedule construction based on
2646 * conditional constraints.
2648 * domain is the iteration domain
2649 * flow are the flow dependences, which determine the validity and
2650 * proximity constraints
2651 * condition are the conditions on the conditional validity constraints
2652 * conditional_validity are the conditional validity constraints
2653 * outer_band_n is the expected number of members in the outer band
2655 struct {
2656 const char *domain;
2657 const char *flow;
2658 const char *condition;
2659 const char *conditional_validity;
2660 int outer_band_n;
2661 } live_range_tests[] = {
2662 /* Contrived example that illustrates that we need to keep
2663 * track of tagged condition dependences and
2664 * tagged conditional validity dependences
2665 * in isl_sched_edge separately.
2666 * In particular, the conditional validity constraints on A
2667 * cannot be satisfied,
2668 * but they can be ignored because there are no corresponding
2669 * condition constraints. However, we do have an additional
2670 * conditional validity constraint that maps to the same
2671 * dependence relation
2672 * as the condition constraint on B. If we did not make a distinction
2673 * between tagged condition and tagged conditional validity
2674 * dependences, then we
2675 * could end up treating this shared dependence as an condition
2676 * constraint on A, forcing a localization of the conditions,
2677 * which is impossible.
2679 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
2680 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
2681 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
2682 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2683 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2684 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
2687 /* TACO 2013 Fig. 7 */
2688 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2689 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2690 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2691 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
2692 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
2693 "0 <= i < n and 0 <= j < n - 1 }",
2694 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
2695 "0 <= i < n and 0 <= j < j' < n;"
2696 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
2697 "0 <= i < i' < n and 0 <= j,j' < n;"
2698 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
2699 "0 <= i,j,j' < n and j < j' }",
2702 /* TACO 2013 Fig. 7, without tags */
2703 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2704 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2705 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2706 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2707 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2708 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
2709 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
2710 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
2713 /* TACO 2013 Fig. 12 */
2714 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
2715 "S3[i,3] : 0 <= i <= 1 }",
2716 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
2717 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
2718 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
2719 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
2720 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2721 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
2722 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2723 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
2724 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
2725 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
2726 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
2731 /* Test schedule construction based on conditional constraints.
2732 * In particular, check the number of members in the outer band
2733 * as an indication of whether tiling is possible or not.
2735 static int test_conditional_schedule_constraints(isl_ctx *ctx)
2737 int i;
2738 isl_union_set *domain;
2739 isl_union_map *condition;
2740 isl_union_map *flow;
2741 isl_union_map *validity;
2742 isl_schedule_constraints *sc;
2743 isl_schedule *schedule;
2744 isl_band_list *list;
2745 isl_band *band;
2746 int n_member;
2748 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
2749 domain = isl_union_set_read_from_str(ctx,
2750 live_range_tests[i].domain);
2751 flow = isl_union_map_read_from_str(ctx,
2752 live_range_tests[i].flow);
2753 condition = isl_union_map_read_from_str(ctx,
2754 live_range_tests[i].condition);
2755 validity = isl_union_map_read_from_str(ctx,
2756 live_range_tests[i].conditional_validity);
2757 sc = isl_schedule_constraints_on_domain(domain);
2758 sc = isl_schedule_constraints_set_validity(sc,
2759 isl_union_map_copy(flow));
2760 sc = isl_schedule_constraints_set_proximity(sc, flow);
2761 sc = isl_schedule_constraints_set_conditional_validity(sc,
2762 condition, validity);
2763 schedule = isl_schedule_constraints_compute_schedule(sc);
2764 list = isl_schedule_get_band_forest(schedule);
2765 band = isl_band_list_get_band(list, 0);
2766 n_member = isl_band_n_member(band);
2767 isl_band_free(band);
2768 isl_band_list_free(list);
2769 isl_schedule_free(schedule);
2771 if (!schedule)
2772 return -1;
2773 if (n_member != live_range_tests[i].outer_band_n)
2774 isl_die(ctx, isl_error_unknown,
2775 "unexpected number of members in outer band",
2776 return -1);
2778 return 0;
2781 int test_schedule(isl_ctx *ctx)
2783 const char *D, *W, *R, *V, *P, *S;
2785 /* Handle resulting schedule with zero bands. */
2786 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2787 return -1;
2789 /* Jacobi */
2790 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2791 W = "{ S1[t,i] -> a[t,i] }";
2792 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2793 "S1[t,i] -> a[t-1,i+1] }";
2794 S = "{ S1[t,i] -> [t,i] }";
2795 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2796 return -1;
2798 /* Fig. 5 of CC2008 */
2799 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2800 "j <= -1 + N }";
2801 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2802 "j >= 2 and j <= -1 + N }";
2803 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2804 "j >= 2 and j <= -1 + N; "
2805 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2806 "j >= 2 and j <= -1 + N }";
2807 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2808 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2809 return -1;
2811 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2812 W = "{ S1[i] -> a[i] }";
2813 R = "{ S2[i] -> a[i+1] }";
2814 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2815 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2816 return -1;
2818 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2819 W = "{ S1[i] -> a[i] }";
2820 R = "{ S2[i] -> a[9-i] }";
2821 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2822 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2823 return -1;
2825 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2826 W = "{ S1[i] -> a[i] }";
2827 R = "[N] -> { S2[i] -> a[N-1-i] }";
2828 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2829 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2830 return -1;
2832 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2833 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2834 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2835 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2836 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2837 return -1;
2839 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2840 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2841 R = "{ S2[i,j] -> a[i-1,j] }";
2842 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2843 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2844 return -1;
2846 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2847 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2848 R = "{ S2[i,j] -> a[i,j-1] }";
2849 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2850 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2851 return -1;
2853 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2854 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2855 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2856 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2857 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2858 "S_0[] -> [0, 0, 0] }";
2859 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2860 return -1;
2861 ctx->opt->schedule_parametric = 0;
2862 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2863 return -1;
2864 ctx->opt->schedule_parametric = 1;
2866 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2867 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2868 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2869 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2870 "S4[i] -> a[i,N] }";
2871 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2872 "S4[i] -> [4,i,0] }";
2873 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2874 return -1;
2876 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2877 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2878 "j <= N }";
2879 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2880 "j <= N; "
2881 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2882 "j <= N }";
2883 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2884 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2885 return -1;
2887 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2888 " S_2[t] : t >= 0 and t <= -1 + N; "
2889 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2890 "i <= -1 + N }";
2891 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2892 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2893 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2894 "i >= 0 and i <= -1 + N }";
2895 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2896 "i >= 0 and i <= -1 + N; "
2897 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2898 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2899 " S_0[t] -> [0, t, 0] }";
2901 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2902 return -1;
2903 ctx->opt->schedule_parametric = 0;
2904 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2905 return -1;
2906 ctx->opt->schedule_parametric = 1;
2908 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2909 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2910 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2911 return -1;
2913 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2914 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2915 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2916 "j >= 0 and j <= -1 + N; "
2917 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2918 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2919 "j >= 0 and j <= -1 + N; "
2920 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2921 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2922 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2923 return -1;
2925 D = "{ S_0[i] : i >= 0 }";
2926 W = "{ S_0[i] -> a[i] : i >= 0 }";
2927 R = "{ S_0[i] -> a[0] : i >= 0 }";
2928 S = "{ S_0[i] -> [0, i, 0] }";
2929 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2930 return -1;
2932 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2933 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2934 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2935 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2936 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2937 return -1;
2939 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2940 "k <= -1 + n and k >= 0 }";
2941 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
2942 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2943 "k <= -1 + n and k >= 0; "
2944 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2945 "k <= -1 + n and k >= 0; "
2946 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2947 "k <= -1 + n and k >= 0 }";
2948 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2949 ctx->opt->schedule_outer_coincidence = 1;
2950 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2951 return -1;
2952 ctx->opt->schedule_outer_coincidence = 0;
2954 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
2955 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
2956 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
2957 "Stmt_for_body24[i0, i1, 1, 0]:"
2958 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
2959 "Stmt_for_body7[i0, i1, i2]:"
2960 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
2961 "i2 <= 7 }";
2963 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
2964 "Stmt_for_body24[1, i1, i2, i3]:"
2965 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
2966 "i2 >= 1;"
2967 "Stmt_for_body24[0, i1, i2, i3] -> "
2968 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
2969 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
2970 "i3 >= 0;"
2971 "Stmt_for_body24[0, i1, i2, i3] ->"
2972 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
2973 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
2974 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
2975 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
2976 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
2977 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
2978 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
2979 "i1 <= 6 and i1 >= 0;"
2980 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
2981 "Stmt_for_body7[i0, i1, i2] -> "
2982 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
2983 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
2984 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
2985 "Stmt_for_body7[i0, i1, i2] -> "
2986 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
2987 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
2988 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
2989 P = V;
2990 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
2991 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
2992 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
2994 if (test_special_schedule(ctx, D, V, P, S) < 0)
2995 return -1;
2997 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
2998 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
2999 "j >= 1 and j <= 7;"
3000 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3001 "j >= 1 and j <= 8 }";
3002 P = "{ }";
3003 S = "{ S_0[i, j] -> [i + j, j] }";
3004 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3005 if (test_special_schedule(ctx, D, V, P, S) < 0)
3006 return -1;
3007 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3009 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3010 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3011 "j >= 0 and j <= -1 + i }";
3012 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3013 "i <= -1 + N and j >= 0;"
3014 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3015 "i <= -2 + N }";
3016 P = "{ }";
3017 S = "{ S_0[i, j] -> [i, j] }";
3018 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3019 if (test_special_schedule(ctx, D, V, P, S) < 0)
3020 return -1;
3021 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3023 /* Test both algorithms on a case with only proximity dependences. */
3024 D = "{ S[i,j] : 0 <= i <= 10 }";
3025 V = "{ }";
3026 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3027 S = "{ S[i, j] -> [j, i] }";
3028 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3029 if (test_special_schedule(ctx, D, V, P, S) < 0)
3030 return -1;
3031 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3032 if (test_special_schedule(ctx, D, V, P, S) < 0)
3033 return -1;
3035 D = "{ A[a]; B[] }";
3036 V = "{}";
3037 P = "{ A[a] -> B[] }";
3038 if (test_has_schedule(ctx, D, V, P) < 0)
3039 return -1;
3041 if (test_padded_schedule(ctx) < 0)
3042 return -1;
3044 /* Check that check for progress is not confused by rational
3045 * solution.
3047 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3048 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3049 "i0 <= -2 + N; "
3050 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3051 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3052 P = "{}";
3053 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3054 if (test_has_schedule(ctx, D, V, P) < 0)
3055 return -1;
3056 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3058 if (test_conditional_schedule_constraints(ctx) < 0)
3059 return -1;
3061 return 0;
3064 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3066 isl_union_map *umap;
3067 int test;
3069 umap = isl_union_map_read_from_str(ctx, str);
3070 test = isl_union_map_plain_is_injective(umap);
3071 isl_union_map_free(umap);
3072 if (test < 0)
3073 return -1;
3074 if (test == injective)
3075 return 0;
3076 if (injective)
3077 isl_die(ctx, isl_error_unknown,
3078 "map not detected as injective", return -1);
3079 else
3080 isl_die(ctx, isl_error_unknown,
3081 "map detected as injective", return -1);
3084 int test_injective(isl_ctx *ctx)
3086 const char *str;
3088 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
3089 return -1;
3090 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
3091 return -1;
3092 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
3093 return -1;
3094 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
3095 return -1;
3096 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
3097 return -1;
3098 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
3099 return -1;
3100 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
3101 return -1;
3102 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
3103 return -1;
3105 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
3106 if (test_plain_injective(ctx, str, 1))
3107 return -1;
3108 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
3109 if (test_plain_injective(ctx, str, 0))
3110 return -1;
3112 return 0;
3115 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
3117 isl_aff *aff2;
3118 int equal;
3120 if (!aff)
3121 return -1;
3123 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
3124 equal = isl_aff_plain_is_equal(aff, aff2);
3125 isl_aff_free(aff2);
3127 return equal;
3130 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
3132 int equal;
3134 equal = aff_plain_is_equal(aff, str);
3135 if (equal < 0)
3136 return -1;
3137 if (!equal)
3138 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
3139 "result not as expected", return -1);
3140 return 0;
3143 int test_aff(isl_ctx *ctx)
3145 const char *str;
3146 isl_set *set;
3147 isl_space *space;
3148 isl_local_space *ls;
3149 isl_aff *aff;
3150 int zero, equal;
3152 space = isl_space_set_alloc(ctx, 0, 1);
3153 ls = isl_local_space_from_space(space);
3154 aff = isl_aff_zero_on_domain(ls);
3156 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3157 aff = isl_aff_scale_down_ui(aff, 3);
3158 aff = isl_aff_floor(aff);
3159 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3160 aff = isl_aff_scale_down_ui(aff, 2);
3161 aff = isl_aff_floor(aff);
3162 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3164 str = "{ [10] }";
3165 set = isl_set_read_from_str(ctx, str);
3166 aff = isl_aff_gist(aff, set);
3168 aff = isl_aff_add_constant_si(aff, -16);
3169 zero = isl_aff_plain_is_zero(aff);
3170 isl_aff_free(aff);
3172 if (zero < 0)
3173 return -1;
3174 if (!zero)
3175 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3177 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
3178 aff = isl_aff_scale_down_ui(aff, 64);
3179 aff = isl_aff_floor(aff);
3180 equal = aff_check_plain_equal(aff, "{ [-1] }");
3181 isl_aff_free(aff);
3182 if (equal < 0)
3183 return -1;
3185 return 0;
3188 int test_dim_max(isl_ctx *ctx)
3190 int equal;
3191 const char *str;
3192 isl_set *set1, *set2;
3193 isl_set *set;
3194 isl_map *map;
3195 isl_pw_aff *pwaff;
3197 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
3198 set = isl_set_read_from_str(ctx, str);
3199 pwaff = isl_set_dim_max(set, 0);
3200 set1 = isl_set_from_pw_aff(pwaff);
3201 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
3202 set2 = isl_set_read_from_str(ctx, str);
3203 equal = isl_set_is_equal(set1, set2);
3204 isl_set_free(set1);
3205 isl_set_free(set2);
3206 if (equal < 0)
3207 return -1;
3208 if (!equal)
3209 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3211 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
3212 set = isl_set_read_from_str(ctx, str);
3213 pwaff = isl_set_dim_max(set, 0);
3214 set1 = isl_set_from_pw_aff(pwaff);
3215 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3216 set2 = isl_set_read_from_str(ctx, str);
3217 equal = isl_set_is_equal(set1, set2);
3218 isl_set_free(set1);
3219 isl_set_free(set2);
3220 if (equal < 0)
3221 return -1;
3222 if (!equal)
3223 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3225 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
3226 set = isl_set_read_from_str(ctx, str);
3227 pwaff = isl_set_dim_max(set, 0);
3228 set1 = isl_set_from_pw_aff(pwaff);
3229 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3230 set2 = isl_set_read_from_str(ctx, str);
3231 equal = isl_set_is_equal(set1, set2);
3232 isl_set_free(set1);
3233 isl_set_free(set2);
3234 if (equal < 0)
3235 return -1;
3236 if (!equal)
3237 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3239 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
3240 "0 <= i < N and 0 <= j < M }";
3241 map = isl_map_read_from_str(ctx, str);
3242 set = isl_map_range(map);
3244 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
3245 set1 = isl_set_from_pw_aff(pwaff);
3246 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
3247 set2 = isl_set_read_from_str(ctx, str);
3248 equal = isl_set_is_equal(set1, set2);
3249 isl_set_free(set1);
3250 isl_set_free(set2);
3252 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3253 set1 = isl_set_from_pw_aff(pwaff);
3254 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3255 set2 = isl_set_read_from_str(ctx, str);
3256 if (equal >= 0 && equal)
3257 equal = isl_set_is_equal(set1, set2);
3258 isl_set_free(set1);
3259 isl_set_free(set2);
3261 isl_set_free(set);
3263 if (equal < 0)
3264 return -1;
3265 if (!equal)
3266 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3268 /* Check that solutions are properly merged. */
3269 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3270 "c <= -1 + n - 4a - 2b and c >= -2b and "
3271 "4a >= -4 + n and c >= 0 }";
3272 set = isl_set_read_from_str(ctx, str);
3273 pwaff = isl_set_dim_min(set, 2);
3274 set1 = isl_set_from_pw_aff(pwaff);
3275 str = "[n] -> { [(0)] : n >= 1 }";
3276 set2 = isl_set_read_from_str(ctx, str);
3277 equal = isl_set_is_equal(set1, set2);
3278 isl_set_free(set1);
3279 isl_set_free(set2);
3281 if (equal < 0)
3282 return -1;
3283 if (!equal)
3284 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3286 return 0;
3289 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
3291 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
3292 const char *str)
3294 isl_ctx *ctx;
3295 isl_pw_multi_aff *pma2;
3296 int equal;
3298 if (!pma)
3299 return -1;
3301 ctx = isl_pw_multi_aff_get_ctx(pma);
3302 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3303 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
3304 isl_pw_multi_aff_free(pma2);
3306 return equal;
3309 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
3310 * represented by "str".
3312 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
3313 const char *str)
3315 int equal;
3317 equal = pw_multi_aff_plain_is_equal(pma, str);
3318 if (equal < 0)
3319 return -1;
3320 if (!equal)
3321 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
3322 "result not as expected", return -1);
3323 return 0;
3326 /* Basic test for isl_pw_multi_aff_product.
3328 * Check that multiple pieces are properly handled.
3330 static int test_product_pma(isl_ctx *ctx)
3332 int equal;
3333 const char *str;
3334 isl_pw_multi_aff *pma1, *pma2;
3336 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
3337 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3338 str = "{ C[] -> D[] }";
3339 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3340 pma1 = isl_pw_multi_aff_product(pma1, pma2);
3341 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
3342 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
3343 equal = pw_multi_aff_check_plain_equal(pma1, str);
3344 isl_pw_multi_aff_free(pma1);
3345 if (equal < 0)
3346 return -1;
3348 return 0;
3351 int test_product(isl_ctx *ctx)
3353 const char *str;
3354 isl_set *set;
3355 isl_union_set *uset1, *uset2;
3356 int ok;
3358 str = "{ A[i] }";
3359 set = isl_set_read_from_str(ctx, str);
3360 set = isl_set_product(set, isl_set_copy(set));
3361 ok = isl_set_is_wrapping(set);
3362 isl_set_free(set);
3363 if (ok < 0)
3364 return -1;
3365 if (!ok)
3366 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3368 str = "{ [] }";
3369 uset1 = isl_union_set_read_from_str(ctx, str);
3370 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3371 str = "{ [[] -> []] }";
3372 uset2 = isl_union_set_read_from_str(ctx, str);
3373 ok = isl_union_set_is_equal(uset1, uset2);
3374 isl_union_set_free(uset1);
3375 isl_union_set_free(uset2);
3376 if (ok < 0)
3377 return -1;
3378 if (!ok)
3379 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3381 if (test_product_pma(ctx) < 0)
3382 return -1;
3384 return 0;
3387 int test_equal(isl_ctx *ctx)
3389 const char *str;
3390 isl_set *set, *set2;
3391 int equal;
3393 str = "{ S_6[i] }";
3394 set = isl_set_read_from_str(ctx, str);
3395 str = "{ S_7[i] }";
3396 set2 = isl_set_read_from_str(ctx, str);
3397 equal = isl_set_is_equal(set, set2);
3398 isl_set_free(set);
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 return 0;
3408 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3409 enum isl_dim_type type, unsigned pos, int fixed)
3411 int test;
3413 test = isl_map_plain_is_fixed(map, type, pos, NULL);
3414 isl_map_free(map);
3415 if (test < 0)
3416 return -1;
3417 if (test == fixed)
3418 return 0;
3419 if (fixed)
3420 isl_die(ctx, isl_error_unknown,
3421 "map not detected as fixed", return -1);
3422 else
3423 isl_die(ctx, isl_error_unknown,
3424 "map detected as fixed", return -1);
3427 int test_fixed(isl_ctx *ctx)
3429 const char *str;
3430 isl_map *map;
3432 str = "{ [i] -> [i] }";
3433 map = isl_map_read_from_str(ctx, str);
3434 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3435 return -1;
3436 str = "{ [i] -> [1] }";
3437 map = isl_map_read_from_str(ctx, str);
3438 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3439 return -1;
3440 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3441 map = isl_map_read_from_str(ctx, str);
3442 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3443 return -1;
3444 map = isl_map_read_from_str(ctx, str);
3445 map = isl_map_neg(map);
3446 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3447 return -1;
3449 return 0;
3452 int test_vertices(isl_ctx *ctx)
3454 const char *str;
3455 isl_basic_set *bset;
3456 isl_vertices *vertices;
3458 str = "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
3459 bset = isl_basic_set_read_from_str(ctx, str);
3460 vertices = isl_basic_set_compute_vertices(bset);
3461 isl_basic_set_free(bset);
3462 isl_vertices_free(vertices);
3463 if (!vertices)
3464 return -1;
3466 str = "{ A[t, i] : t = 14 and i = 1 }";
3467 bset = isl_basic_set_read_from_str(ctx, str);
3468 vertices = isl_basic_set_compute_vertices(bset);
3469 isl_basic_set_free(bset);
3470 isl_vertices_free(vertices);
3471 if (!vertices)
3472 return -1;
3474 return 0;
3477 int test_union_pw(isl_ctx *ctx)
3479 int equal;
3480 const char *str;
3481 isl_union_set *uset;
3482 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
3484 str = "{ [x] -> x^2 }";
3485 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
3486 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
3487 uset = isl_union_pw_qpolynomial_domain(upwqp1);
3488 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
3489 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
3490 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
3491 isl_union_pw_qpolynomial_free(upwqp1);
3492 isl_union_pw_qpolynomial_free(upwqp2);
3493 if (equal < 0)
3494 return -1;
3495 if (!equal)
3496 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3498 return 0;
3501 int test_output(isl_ctx *ctx)
3503 char *s;
3504 const char *str;
3505 isl_pw_aff *pa;
3506 isl_printer *p;
3507 int equal;
3509 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
3510 pa = isl_pw_aff_read_from_str(ctx, str);
3512 p = isl_printer_to_str(ctx);
3513 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
3514 p = isl_printer_print_pw_aff(p, pa);
3515 s = isl_printer_get_str(p);
3516 isl_printer_free(p);
3517 isl_pw_aff_free(pa);
3518 if (!s)
3519 equal = -1;
3520 else
3521 equal = !strcmp(s, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
3522 free(s);
3523 if (equal < 0)
3524 return -1;
3525 if (!equal)
3526 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3528 return 0;
3531 int test_sample(isl_ctx *ctx)
3533 const char *str;
3534 isl_basic_set *bset1, *bset2;
3535 int empty, subset;
3537 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3538 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3539 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3540 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3541 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3542 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3543 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3544 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3545 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3546 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3547 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3548 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3549 "d - 1073741821e and "
3550 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3551 "3j >= 1 - a + b + 2e and "
3552 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3553 "3i <= 4 - a + 4b - e and "
3554 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3555 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3556 "c <= -1 + a and 3i >= -2 - a + 3e and "
3557 "1073741822e <= 1073741823 - a + 1073741822b + c and "
3558 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3559 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3560 "1073741823e >= 1 + 1073741823b - d and "
3561 "3i >= 1073741823b + c - 1073741823e - f and "
3562 "3i >= 1 + 2b + e + 3g }";
3563 bset1 = isl_basic_set_read_from_str(ctx, str);
3564 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3565 empty = isl_basic_set_is_empty(bset2);
3566 subset = isl_basic_set_is_subset(bset2, bset1);
3567 isl_basic_set_free(bset1);
3568 isl_basic_set_free(bset2);
3569 if (empty < 0 || subset < 0)
3570 return -1;
3571 if (empty)
3572 isl_die(ctx, isl_error_unknown, "point not found", return -1);
3573 if (!subset)
3574 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
3576 return 0;
3579 int test_fixed_power(isl_ctx *ctx)
3581 const char *str;
3582 isl_map *map;
3583 isl_int exp;
3584 int equal;
3586 isl_int_init(exp);
3587 str = "{ [i] -> [i + 1] }";
3588 map = isl_map_read_from_str(ctx, str);
3589 isl_int_set_si(exp, 23);
3590 map = isl_map_fixed_power(map, exp);
3591 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
3592 isl_int_clear(exp);
3593 isl_map_free(map);
3594 if (equal < 0)
3595 return -1;
3597 return 0;
3600 int test_slice(isl_ctx *ctx)
3602 const char *str;
3603 isl_map *map;
3604 int equal;
3606 str = "{ [i] -> [j] }";
3607 map = isl_map_read_from_str(ctx, str);
3608 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
3609 equal = map_check_equal(map, "{ [i] -> [i] }");
3610 isl_map_free(map);
3611 if (equal < 0)
3612 return -1;
3614 str = "{ [i] -> [j] }";
3615 map = isl_map_read_from_str(ctx, str);
3616 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
3617 equal = map_check_equal(map, "{ [i] -> [j] }");
3618 isl_map_free(map);
3619 if (equal < 0)
3620 return -1;
3622 str = "{ [i] -> [j] }";
3623 map = isl_map_read_from_str(ctx, str);
3624 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
3625 equal = map_check_equal(map, "{ [i] -> [-i] }");
3626 isl_map_free(map);
3627 if (equal < 0)
3628 return -1;
3630 str = "{ [i] -> [j] }";
3631 map = isl_map_read_from_str(ctx, str);
3632 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
3633 equal = map_check_equal(map, "{ [0] -> [j] }");
3634 isl_map_free(map);
3635 if (equal < 0)
3636 return -1;
3638 str = "{ [i] -> [j] }";
3639 map = isl_map_read_from_str(ctx, str);
3640 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3641 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
3642 isl_map_free(map);
3643 if (equal < 0)
3644 return -1;
3646 str = "{ [i] -> [j] }";
3647 map = isl_map_read_from_str(ctx, str);
3648 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
3649 equal = map_check_equal(map, "{ [i] -> [j] : false }");
3650 isl_map_free(map);
3651 if (equal < 0)
3652 return -1;
3654 return 0;
3657 int test_eliminate(isl_ctx *ctx)
3659 const char *str;
3660 isl_map *map;
3661 int equal;
3663 str = "{ [i] -> [j] : i = 2j }";
3664 map = isl_map_read_from_str(ctx, str);
3665 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
3666 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
3667 isl_map_free(map);
3668 if (equal < 0)
3669 return -1;
3671 return 0;
3674 /* Check that isl_set_dim_residue_class detects that the values of j
3675 * in the set below are all odd and that it does not detect any spurious
3676 * strides.
3678 static int test_residue_class(isl_ctx *ctx)
3680 const char *str;
3681 isl_set *set;
3682 isl_int m, r;
3683 int res;
3685 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3686 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3687 set = isl_set_read_from_str(ctx, str);
3688 isl_int_init(m);
3689 isl_int_init(r);
3690 res = isl_set_dim_residue_class(set, 1, &m, &r);
3691 if (res >= 0 &&
3692 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
3693 isl_die(ctx, isl_error_unknown, "incorrect residue class",
3694 res = -1);
3695 isl_int_clear(r);
3696 isl_int_clear(m);
3697 isl_set_free(set);
3699 return res;
3702 int test_align_parameters(isl_ctx *ctx)
3704 const char *str;
3705 isl_space *space;
3706 isl_multi_aff *ma1, *ma2;
3707 int equal;
3709 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
3710 ma1 = isl_multi_aff_read_from_str(ctx, str);
3712 space = isl_space_params_alloc(ctx, 1);
3713 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
3714 ma1 = isl_multi_aff_align_params(ma1, space);
3716 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
3717 ma2 = isl_multi_aff_read_from_str(ctx, str);
3719 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3721 isl_multi_aff_free(ma1);
3722 isl_multi_aff_free(ma2);
3724 if (equal < 0)
3725 return -1;
3726 if (!equal)
3727 isl_die(ctx, isl_error_unknown,
3728 "result not as expected", return -1);
3730 return 0;
3733 static int test_list(isl_ctx *ctx)
3735 isl_id *a, *b, *c, *d, *id;
3736 isl_id_list *list;
3737 int ok;
3739 a = isl_id_alloc(ctx, "a", NULL);
3740 b = isl_id_alloc(ctx, "b", NULL);
3741 c = isl_id_alloc(ctx, "c", NULL);
3742 d = isl_id_alloc(ctx, "d", NULL);
3744 list = isl_id_list_alloc(ctx, 4);
3745 list = isl_id_list_add(list, a);
3746 list = isl_id_list_add(list, b);
3747 list = isl_id_list_add(list, c);
3748 list = isl_id_list_add(list, d);
3749 list = isl_id_list_drop(list, 1, 1);
3751 if (isl_id_list_n_id(list) != 3) {
3752 isl_id_list_free(list);
3753 isl_die(ctx, isl_error_unknown,
3754 "unexpected number of elements in list", return -1);
3757 id = isl_id_list_get_id(list, 0);
3758 ok = id == a;
3759 isl_id_free(id);
3760 id = isl_id_list_get_id(list, 1);
3761 ok = ok && id == c;
3762 isl_id_free(id);
3763 id = isl_id_list_get_id(list, 2);
3764 ok = ok && id == d;
3765 isl_id_free(id);
3767 isl_id_list_free(list);
3769 if (!ok)
3770 isl_die(ctx, isl_error_unknown,
3771 "unexpected elements in list", return -1);
3773 return 0;
3776 const char *set_conversion_tests[] = {
3777 "[N] -> { [i] : N - 1 <= 2 i <= N }",
3778 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
3779 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3780 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3783 /* Check that converting from isl_set to isl_pw_multi_aff and back
3784 * to isl_set produces the original isl_set.
3786 static int test_set_conversion(isl_ctx *ctx)
3788 int i;
3789 const char *str;
3790 isl_set *set1, *set2;
3791 isl_pw_multi_aff *pma;
3792 int equal;
3794 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
3795 str = set_conversion_tests[i];
3796 set1 = isl_set_read_from_str(ctx, str);
3797 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
3798 set2 = isl_set_from_pw_multi_aff(pma);
3799 equal = isl_set_is_equal(set1, set2);
3800 isl_set_free(set1);
3801 isl_set_free(set2);
3803 if (equal < 0)
3804 return -1;
3805 if (!equal)
3806 isl_die(ctx, isl_error_unknown, "bad conversion",
3807 return -1);
3810 return 0;
3813 /* Check that converting from isl_map to isl_pw_multi_aff and back
3814 * to isl_map produces the original isl_map.
3816 static int test_map_conversion(isl_ctx *ctx)
3818 const char *str;
3819 isl_map *map1, *map2;
3820 isl_pw_multi_aff *pma;
3821 int equal;
3823 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
3824 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
3825 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
3826 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
3827 "9e <= -2 - 2a) }";
3828 map1 = isl_map_read_from_str(ctx, str);
3829 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
3830 map2 = isl_map_from_pw_multi_aff(pma);
3831 equal = isl_map_is_equal(map1, map2);
3832 isl_map_free(map1);
3833 isl_map_free(map2);
3835 if (equal < 0)
3836 return -1;
3837 if (!equal)
3838 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
3840 return 0;
3843 static int test_conversion(isl_ctx *ctx)
3845 if (test_set_conversion(ctx) < 0)
3846 return -1;
3847 if (test_map_conversion(ctx) < 0)
3848 return -1;
3849 return 0;
3852 /* Check that isl_basic_map_curry does not modify input.
3854 static int test_curry(isl_ctx *ctx)
3856 const char *str;
3857 isl_basic_map *bmap1, *bmap2;
3858 int equal;
3860 str = "{ [A[] -> B[]] -> C[] }";
3861 bmap1 = isl_basic_map_read_from_str(ctx, str);
3862 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
3863 equal = isl_basic_map_is_equal(bmap1, bmap2);
3864 isl_basic_map_free(bmap1);
3865 isl_basic_map_free(bmap2);
3867 if (equal < 0)
3868 return -1;
3869 if (equal)
3870 isl_die(ctx, isl_error_unknown,
3871 "curried map should not be equal to original",
3872 return -1);
3874 return 0;
3877 struct {
3878 const char *set;
3879 const char *ma;
3880 const char *res;
3881 } preimage_tests[] = {
3882 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
3883 "{ A[j,i] -> B[i,j] }",
3884 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
3885 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3886 "{ A[a,b] -> B[a/2,b/6] }",
3887 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
3888 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3889 "{ A[a,b] -> B[a/2,b/6] }",
3890 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
3891 "exists i,j : a = 2 i and b = 6 j }" },
3892 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
3893 "[n] -> { : 0 <= n <= 100 }" },
3894 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3895 "{ A[a] -> B[2a] }",
3896 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
3897 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3898 "{ A[a] -> B[([a/2])] }",
3899 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
3900 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
3901 "{ A[a] -> B[a,a,a/3] }",
3902 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
3903 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
3904 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
3907 static int test_preimage_basic_set(isl_ctx *ctx)
3909 int i;
3910 isl_basic_set *bset1, *bset2;
3911 isl_multi_aff *ma;
3912 int equal;
3914 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
3915 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
3916 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
3917 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
3918 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
3919 equal = isl_basic_set_is_equal(bset1, bset2);
3920 isl_basic_set_free(bset1);
3921 isl_basic_set_free(bset2);
3922 if (equal < 0)
3923 return -1;
3924 if (!equal)
3925 isl_die(ctx, isl_error_unknown, "bad preimage",
3926 return -1);
3929 return 0;
3932 struct {
3933 const char *map;
3934 const char *ma;
3935 const char *res;
3936 } preimage_domain_tests[] = {
3937 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
3938 "{ A[j,i] -> B[i,j] }",
3939 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
3940 { "{ B[i] -> C[i]; D[i] -> E[i] }",
3941 "{ A[i] -> B[i + 1] }",
3942 "{ A[i] -> C[i + 1] }" },
3943 { "{ B[i] -> C[i]; B[i] -> E[i] }",
3944 "{ A[i] -> B[i + 1] }",
3945 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
3946 { "{ B[i] -> C[([i/2])] }",
3947 "{ A[i] -> B[2i] }",
3948 "{ A[i] -> C[i] }" },
3949 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
3950 "{ A[i] -> B[([i/5]), ([i/7])] }",
3951 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
3952 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
3953 "[N] -> { A[] -> B[([N/5])] }",
3954 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
3955 { "{ B[i] -> C[i] : exists a : i = 5 a }",
3956 "{ A[i] -> B[2i] }",
3957 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
3958 { "{ B[i] -> C[i] : exists a : i = 2 a; "
3959 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
3960 "{ A[i] -> B[2i] }",
3961 "{ A[i] -> C[2i] }" },
3964 static int test_preimage_union_map(isl_ctx *ctx)
3966 int i;
3967 isl_union_map *umap1, *umap2;
3968 isl_multi_aff *ma;
3969 int equal;
3971 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
3972 umap1 = isl_union_map_read_from_str(ctx,
3973 preimage_domain_tests[i].map);
3974 ma = isl_multi_aff_read_from_str(ctx,
3975 preimage_domain_tests[i].ma);
3976 umap2 = isl_union_map_read_from_str(ctx,
3977 preimage_domain_tests[i].res);
3978 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
3979 equal = isl_union_map_is_equal(umap1, umap2);
3980 isl_union_map_free(umap1);
3981 isl_union_map_free(umap2);
3982 if (equal < 0)
3983 return -1;
3984 if (!equal)
3985 isl_die(ctx, isl_error_unknown, "bad preimage",
3986 return -1);
3989 return 0;
3992 static int test_preimage(isl_ctx *ctx)
3994 if (test_preimage_basic_set(ctx) < 0)
3995 return -1;
3996 if (test_preimage_union_map(ctx) < 0)
3997 return -1;
3999 return 0;
4002 struct {
4003 const char *ma1;
4004 const char *ma;
4005 const char *res;
4006 } pullback_tests[] = {
4007 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
4008 "{ A[a,b] -> C[b + 2a] }" },
4009 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
4010 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
4011 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
4012 "{ A[a] -> C[(a)/6] }" },
4013 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
4014 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
4015 "{ A[a] -> C[(2a)/3] }" },
4016 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
4017 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
4018 "{ A[i,j] -> C[i + j, i + j] }"},
4019 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
4020 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
4021 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
4024 static int test_pullback(isl_ctx *ctx)
4026 int i;
4027 isl_multi_aff *ma1, *ma2;
4028 isl_multi_aff *ma;
4029 int equal;
4031 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
4032 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
4033 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
4034 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
4035 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
4036 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4037 isl_multi_aff_free(ma1);
4038 isl_multi_aff_free(ma2);
4039 if (equal < 0)
4040 return -1;
4041 if (!equal)
4042 isl_die(ctx, isl_error_unknown, "bad pullback",
4043 return -1);
4046 return 0;
4049 /* Check that negation is printed correctly.
4051 static int test_ast(isl_ctx *ctx)
4053 isl_ast_expr *expr, *expr1, *expr2, *expr3;
4054 char *str;
4055 int ok;
4057 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4058 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4059 expr = isl_ast_expr_add(expr1, expr2);
4060 expr = isl_ast_expr_neg(expr);
4061 str = isl_ast_expr_to_str(expr);
4062 ok = str ? !strcmp(str, "-(A + B)") : -1;
4063 free(str);
4064 isl_ast_expr_free(expr);
4066 if (ok < 0)
4067 return -1;
4068 if (!ok)
4069 isl_die(ctx, isl_error_unknown,
4070 "isl_ast_expr printed incorrectly", return -1);
4072 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4073 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4074 expr = isl_ast_expr_add(expr1, expr2);
4075 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
4076 expr = isl_ast_expr_sub(expr3, expr);
4077 str = isl_ast_expr_to_str(expr);
4078 ok = str ? !strcmp(str, "C - (A + B)") : -1;
4079 free(str);
4080 isl_ast_expr_free(expr);
4082 if (ok < 0)
4083 return -1;
4084 if (!ok)
4085 isl_die(ctx, isl_error_unknown,
4086 "isl_ast_expr printed incorrectly", return -1);
4088 return 0;
4091 /* Internal data structure for before_for and after_for callbacks.
4093 * depth is the current depth
4094 * before is the number of times before_for has been called
4095 * after is the number of times after_for has been called
4097 struct isl_test_codegen_data {
4098 int depth;
4099 int before;
4100 int after;
4103 /* This function is called before each for loop in the AST generated
4104 * from test_ast_gen1.
4106 * Increment the number of calls and the depth.
4107 * Check that the space returned by isl_ast_build_get_schedule_space
4108 * matches the target space of the schedule returned by
4109 * isl_ast_build_get_schedule.
4110 * Return an isl_id that is checked by the corresponding call
4111 * to after_for.
4113 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
4114 void *user)
4116 struct isl_test_codegen_data *data = user;
4117 isl_ctx *ctx;
4118 isl_space *space;
4119 isl_union_map *schedule;
4120 isl_union_set *uset;
4121 isl_set *set;
4122 int empty;
4123 char name[] = "d0";
4125 ctx = isl_ast_build_get_ctx(build);
4127 if (data->before >= 3)
4128 isl_die(ctx, isl_error_unknown,
4129 "unexpected number of for nodes", return NULL);
4130 if (data->depth >= 2)
4131 isl_die(ctx, isl_error_unknown,
4132 "unexpected depth", return NULL);
4134 snprintf(name, sizeof(name), "d%d", data->depth);
4135 data->before++;
4136 data->depth++;
4138 schedule = isl_ast_build_get_schedule(build);
4139 uset = isl_union_map_range(schedule);
4140 if (!uset)
4141 return NULL;
4142 if (isl_union_set_n_set(uset) != 1) {
4143 isl_union_set_free(uset);
4144 isl_die(ctx, isl_error_unknown,
4145 "expecting single range space", return NULL);
4148 space = isl_ast_build_get_schedule_space(build);
4149 set = isl_union_set_extract_set(uset, space);
4150 isl_union_set_free(uset);
4151 empty = isl_set_is_empty(set);
4152 isl_set_free(set);
4154 if (empty < 0)
4155 return NULL;
4156 if (empty)
4157 isl_die(ctx, isl_error_unknown,
4158 "spaces don't match", return NULL);
4160 return isl_id_alloc(ctx, name, NULL);
4163 /* This function is called after each for loop in the AST generated
4164 * from test_ast_gen1.
4166 * Increment the number of calls and decrement the depth.
4167 * Check that the annotation attached to the node matches
4168 * the isl_id returned by the corresponding call to before_for.
4170 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
4171 __isl_keep isl_ast_build *build, void *user)
4173 struct isl_test_codegen_data *data = user;
4174 isl_id *id;
4175 const char *name;
4176 int valid;
4178 data->after++;
4179 data->depth--;
4181 if (data->after > data->before)
4182 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4183 "mismatch in number of for nodes",
4184 return isl_ast_node_free(node));
4186 id = isl_ast_node_get_annotation(node);
4187 if (!id)
4188 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4189 "missing annotation", return isl_ast_node_free(node));
4191 name = isl_id_get_name(id);
4192 valid = name && atoi(name + 1) == data->depth;
4193 isl_id_free(id);
4195 if (!valid)
4196 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4197 "wrong annotation", return isl_ast_node_free(node));
4199 return node;
4202 /* Check that the before_each_for and after_each_for callbacks
4203 * are called for each for loop in the generated code,
4204 * that they are called in the right order and that the isl_id
4205 * returned from the before_each_for callback is attached to
4206 * the isl_ast_node passed to the corresponding after_each_for call.
4208 static int test_ast_gen1(isl_ctx *ctx)
4210 const char *str;
4211 isl_set *set;
4212 isl_union_map *schedule;
4213 isl_ast_build *build;
4214 isl_ast_node *tree;
4215 struct isl_test_codegen_data data;
4217 str = "[N] -> { : N >= 10 }";
4218 set = isl_set_read_from_str(ctx, str);
4219 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
4220 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
4221 schedule = isl_union_map_read_from_str(ctx, str);
4223 data.before = 0;
4224 data.after = 0;
4225 data.depth = 0;
4226 build = isl_ast_build_from_context(set);
4227 build = isl_ast_build_set_before_each_for(build,
4228 &before_for, &data);
4229 build = isl_ast_build_set_after_each_for(build,
4230 &after_for, &data);
4231 tree = isl_ast_build_ast_from_schedule(build, schedule);
4232 isl_ast_build_free(build);
4233 if (!tree)
4234 return -1;
4236 isl_ast_node_free(tree);
4238 if (data.before != 3 || data.after != 3)
4239 isl_die(ctx, isl_error_unknown,
4240 "unexpected number of for nodes", return -1);
4242 return 0;
4245 /* Check that the AST generator handles domains that are integrally disjoint
4246 * but not ratinoally disjoint.
4248 static int test_ast_gen2(isl_ctx *ctx)
4250 const char *str;
4251 isl_set *set;
4252 isl_union_map *schedule;
4253 isl_union_map *options;
4254 isl_ast_build *build;
4255 isl_ast_node *tree;
4257 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
4258 schedule = isl_union_map_read_from_str(ctx, str);
4259 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4260 build = isl_ast_build_from_context(set);
4262 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
4263 options = isl_union_map_read_from_str(ctx, str);
4264 build = isl_ast_build_set_options(build, options);
4265 tree = isl_ast_build_ast_from_schedule(build, schedule);
4266 isl_ast_build_free(build);
4267 if (!tree)
4268 return -1;
4269 isl_ast_node_free(tree);
4271 return 0;
4274 /* Increment *user on each call.
4276 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
4277 __isl_keep isl_ast_build *build, void *user)
4279 int *n = user;
4281 (*n)++;
4283 return node;
4286 /* Test that unrolling tries to minimize the number of instances.
4287 * In particular, for the schedule given below, make sure it generates
4288 * 3 nodes (rather than 101).
4290 static int test_ast_gen3(isl_ctx *ctx)
4292 const char *str;
4293 isl_set *set;
4294 isl_union_map *schedule;
4295 isl_union_map *options;
4296 isl_ast_build *build;
4297 isl_ast_node *tree;
4298 int n_domain = 0;
4300 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
4301 schedule = isl_union_map_read_from_str(ctx, str);
4302 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4304 str = "{ [i] -> unroll[0] }";
4305 options = isl_union_map_read_from_str(ctx, str);
4307 build = isl_ast_build_from_context(set);
4308 build = isl_ast_build_set_options(build, options);
4309 build = isl_ast_build_set_at_each_domain(build,
4310 &count_domains, &n_domain);
4311 tree = isl_ast_build_ast_from_schedule(build, schedule);
4312 isl_ast_build_free(build);
4313 if (!tree)
4314 return -1;
4316 isl_ast_node_free(tree);
4318 if (n_domain != 3)
4319 isl_die(ctx, isl_error_unknown,
4320 "unexpected number of for nodes", return -1);
4322 return 0;
4325 /* Check that if the ast_build_exploit_nested_bounds options is set,
4326 * we do not get an outer if node in the generated AST,
4327 * while we do get such an outer if node if the options is not set.
4329 static int test_ast_gen4(isl_ctx *ctx)
4331 const char *str;
4332 isl_set *set;
4333 isl_union_map *schedule;
4334 isl_ast_build *build;
4335 isl_ast_node *tree;
4336 enum isl_ast_node_type type;
4337 int enb;
4339 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4340 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4342 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4344 schedule = isl_union_map_read_from_str(ctx, str);
4345 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4346 build = isl_ast_build_from_context(set);
4347 tree = isl_ast_build_ast_from_schedule(build, schedule);
4348 isl_ast_build_free(build);
4349 if (!tree)
4350 return -1;
4352 type = isl_ast_node_get_type(tree);
4353 isl_ast_node_free(tree);
4355 if (type == isl_ast_node_if)
4356 isl_die(ctx, isl_error_unknown,
4357 "not expecting if node", return -1);
4359 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
4361 schedule = isl_union_map_read_from_str(ctx, str);
4362 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4363 build = isl_ast_build_from_context(set);
4364 tree = isl_ast_build_ast_from_schedule(build, schedule);
4365 isl_ast_build_free(build);
4366 if (!tree)
4367 return -1;
4369 type = isl_ast_node_get_type(tree);
4370 isl_ast_node_free(tree);
4372 if (type != isl_ast_node_if)
4373 isl_die(ctx, isl_error_unknown,
4374 "expecting if node", return -1);
4376 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
4378 return 0;
4381 /* This function is called for each leaf in the AST generated
4382 * from test_ast_gen5.
4384 * We finalize the AST generation by extending the outer schedule
4385 * with a zero-dimensional schedule. If this results in any for loops,
4386 * then this means that we did not pass along enough information
4387 * about the outer schedule to the inner AST generation.
4389 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
4390 void *user)
4392 isl_union_map *schedule, *extra;
4393 isl_ast_node *tree;
4395 schedule = isl_ast_build_get_schedule(build);
4396 extra = isl_union_map_copy(schedule);
4397 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
4398 schedule = isl_union_map_range_product(schedule, extra);
4399 tree = isl_ast_build_ast_from_schedule(build, schedule);
4400 isl_ast_build_free(build);
4402 if (!tree)
4403 return NULL;
4405 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
4406 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
4407 "code should not contain any for loop",
4408 return isl_ast_node_free(tree));
4410 return tree;
4413 /* Check that we do not lose any information when going back and
4414 * forth between internal and external schedule.
4416 * In particular, we create an AST where we unroll the only
4417 * non-constant dimension in the schedule. We therefore do
4418 * not expect any for loops in the AST. However, older versions
4419 * of isl would not pass along enough information about the outer
4420 * schedule when performing an inner code generation from a create_leaf
4421 * callback, resulting in the inner code generation producing a for loop.
4423 static int test_ast_gen5(isl_ctx *ctx)
4425 const char *str;
4426 isl_set *set;
4427 isl_union_map *schedule, *options;
4428 isl_ast_build *build;
4429 isl_ast_node *tree;
4431 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
4432 schedule = isl_union_map_read_from_str(ctx, str);
4434 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
4435 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
4436 options = isl_union_map_read_from_str(ctx, str);
4438 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4439 build = isl_ast_build_from_context(set);
4440 build = isl_ast_build_set_options(build, options);
4441 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
4442 tree = isl_ast_build_ast_from_schedule(build, schedule);
4443 isl_ast_build_free(build);
4444 isl_ast_node_free(tree);
4445 if (!tree)
4446 return -1;
4448 return 0;
4451 static int test_ast_gen(isl_ctx *ctx)
4453 if (test_ast_gen1(ctx) < 0)
4454 return -1;
4455 if (test_ast_gen2(ctx) < 0)
4456 return -1;
4457 if (test_ast_gen3(ctx) < 0)
4458 return -1;
4459 if (test_ast_gen4(ctx) < 0)
4460 return -1;
4461 if (test_ast_gen5(ctx) < 0)
4462 return -1;
4463 return 0;
4466 /* Check if dropping output dimensions from an isl_pw_multi_aff
4467 * works properly.
4469 static int test_pw_multi_aff(isl_ctx *ctx)
4471 const char *str;
4472 isl_pw_multi_aff *pma1, *pma2;
4473 int equal;
4475 str = "{ [i,j] -> [i+j, 4i-j] }";
4476 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4477 str = "{ [i,j] -> [4i-j] }";
4478 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4480 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
4482 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
4484 isl_pw_multi_aff_free(pma1);
4485 isl_pw_multi_aff_free(pma2);
4486 if (equal < 0)
4487 return -1;
4488 if (!equal)
4489 isl_die(ctx, isl_error_unknown,
4490 "expressions not equal", return -1);
4492 return 0;
4495 /* Check that we can properly parse multi piecewise affine expressions
4496 * where the piecewise affine expressions have different domains.
4498 static int test_multi_pw_aff(isl_ctx *ctx)
4500 const char *str;
4501 isl_set *dom, *dom2;
4502 isl_multi_pw_aff *mpa1, *mpa2;
4503 isl_pw_aff *pa;
4504 int equal;
4505 int equal_domain;
4507 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
4508 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
4509 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
4510 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
4511 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
4512 str = "{ [i] -> [(i : i > 0), 2i] }";
4513 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
4515 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
4517 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
4518 dom = isl_pw_aff_domain(pa);
4519 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
4520 dom2 = isl_pw_aff_domain(pa);
4521 equal_domain = isl_set_is_equal(dom, dom2);
4523 isl_set_free(dom);
4524 isl_set_free(dom2);
4525 isl_multi_pw_aff_free(mpa1);
4526 isl_multi_pw_aff_free(mpa2);
4528 if (equal < 0)
4529 return -1;
4530 if (!equal)
4531 isl_die(ctx, isl_error_unknown,
4532 "expressions not equal", return -1);
4534 if (equal_domain < 0)
4535 return -1;
4536 if (equal_domain)
4537 isl_die(ctx, isl_error_unknown,
4538 "domains unexpectedly equal", return -1);
4540 return 0;
4543 /* This is a regression test for a bug where isl_basic_map_simplify
4544 * would end up in an infinite loop. In particular, we construct
4545 * an empty basic set that is not obviously empty.
4546 * isl_basic_set_is_empty marks the basic set as empty.
4547 * After projecting out i3, the variable can be dropped completely,
4548 * but isl_basic_map_simplify refrains from doing so if the basic set
4549 * is empty and would end up in an infinite loop if it didn't test
4550 * explicitly for empty basic maps in the outer loop.
4552 static int test_simplify(isl_ctx *ctx)
4554 const char *str;
4555 isl_basic_set *bset;
4556 int empty;
4558 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
4559 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
4560 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
4561 "i3 >= i2 }";
4562 bset = isl_basic_set_read_from_str(ctx, str);
4563 empty = isl_basic_set_is_empty(bset);
4564 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
4565 isl_basic_set_free(bset);
4566 if (!bset)
4567 return -1;
4568 if (!empty)
4569 isl_die(ctx, isl_error_unknown,
4570 "basic set should be empty", return -1);
4572 return 0;
4575 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
4576 * with gbr context would fail to disable the use of the shifted tableau
4577 * when transferring equalities for the input to the context, resulting
4578 * in invalid sample values.
4580 static int test_partial_lexmin(isl_ctx *ctx)
4582 const char *str;
4583 isl_basic_set *bset;
4584 isl_basic_map *bmap;
4585 isl_map *map;
4587 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
4588 bmap = isl_basic_map_read_from_str(ctx, str);
4589 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
4590 bset = isl_basic_set_read_from_str(ctx, str);
4591 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
4592 isl_map_free(map);
4594 if (!map)
4595 return -1;
4597 return 0;
4600 /* Check that the variable compression performed on the existentially
4601 * quantified variables inside isl_basic_set_compute_divs is not confused
4602 * by the implicit equalities among the parameters.
4604 static int test_compute_divs(isl_ctx *ctx)
4606 const char *str;
4607 isl_basic_set *bset;
4608 isl_set *set;
4610 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
4611 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
4612 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
4613 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
4614 bset = isl_basic_set_read_from_str(ctx, str);
4615 set = isl_basic_set_compute_divs(bset);
4616 isl_set_free(set);
4617 if (!set)
4618 return -1;
4620 return 0;
4623 struct {
4624 const char *name;
4625 int (*fn)(isl_ctx *ctx);
4626 } tests [] = {
4627 { "val", &test_val },
4628 { "compute divs", &test_compute_divs },
4629 { "partial lexmin", &test_partial_lexmin },
4630 { "simplify", &test_simplify },
4631 { "curry", &test_curry },
4632 { "piecewise multi affine expressions", &test_pw_multi_aff },
4633 { "multi piecewise affine expressions", &test_multi_pw_aff },
4634 { "conversion", &test_conversion },
4635 { "list", &test_list },
4636 { "align parameters", &test_align_parameters },
4637 { "preimage", &test_preimage },
4638 { "pullback", &test_pullback },
4639 { "AST", &test_ast },
4640 { "AST generation", &test_ast_gen },
4641 { "eliminate", &test_eliminate },
4642 { "residue class", &test_residue_class },
4643 { "div", &test_div },
4644 { "slice", &test_slice },
4645 { "fixed power", &test_fixed_power },
4646 { "sample", &test_sample },
4647 { "output", &test_output },
4648 { "vertices", &test_vertices },
4649 { "fixed", &test_fixed },
4650 { "equal", &test_equal },
4651 { "product", &test_product },
4652 { "dim_max", &test_dim_max },
4653 { "affine", &test_aff },
4654 { "injective", &test_injective },
4655 { "schedule", &test_schedule },
4656 { "union_pw", &test_union_pw },
4657 { "parse", &test_parse },
4658 { "single-valued", &test_sv },
4659 { "affine hull", &test_affine_hull },
4660 { "coalesce", &test_coalesce },
4661 { "factorize", &test_factorize },
4662 { "subset", &test_subset },
4663 { "subtract", &test_subtract },
4664 { "lexmin", &test_lexmin },
4665 { "gist", &test_gist },
4666 { "piecewise quasi-polynomials", &test_pwqp },
4669 int main()
4671 int i;
4672 struct isl_ctx *ctx;
4674 srcdir = getenv("srcdir");
4675 assert(srcdir);
4677 ctx = isl_ctx_alloc();
4678 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
4679 printf("%s\n", tests[i].name);
4680 if (tests[i].fn(ctx) < 0)
4681 goto error;
4683 test_lift(ctx);
4684 test_bound(ctx);
4685 test_union(ctx);
4686 test_split_periods(ctx);
4687 test_lex(ctx);
4688 test_bijective(ctx);
4689 test_dep(ctx);
4690 test_read(ctx);
4691 test_bounded(ctx);
4692 test_construction(ctx);
4693 test_dim(ctx);
4694 test_application(ctx);
4695 test_convex_hull(ctx);
4696 test_closure(ctx);
4697 isl_ctx_free(ctx);
4698 return 0;
4699 error:
4700 isl_ctx_free(ctx);
4701 return -1;