isl_schedule_constraints: add support for conditional validity constraints
[isl.git] / isl_test.c
blob2e2755c25043f3f81f700384426d02d89ff45be8
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;
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 proximity = isl_union_map_copy(dep);
2459 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
2460 sc = isl_schedule_constraints_set_validity(sc, validity);
2461 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2462 sched = isl_schedule_constraints_compute_schedule(sc);
2463 schedule = isl_schedule_get_map(sched);
2464 isl_schedule_free(sched);
2465 isl_union_map_free(W);
2466 isl_union_map_free(R);
2467 isl_union_map_free(S);
2469 is_injection = 1;
2470 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2472 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2473 is_complete = isl_union_set_is_subset(D, domain);
2474 isl_union_set_free(D);
2475 isl_union_set_free(domain);
2477 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2478 test = isl_union_map_apply_range(test, dep);
2479 test = isl_union_map_apply_range(test, schedule);
2481 delta = isl_union_map_deltas(test);
2482 if (isl_union_set_n_set(delta) == 0) {
2483 is_tilable = 1;
2484 is_parallel = 1;
2485 is_nonneg = 1;
2486 isl_union_set_free(delta);
2487 } else {
2488 delta_set = isl_set_from_union_set(delta);
2490 slice = isl_set_universe(isl_set_get_space(delta_set));
2491 for (i = 0; i < tilable; ++i)
2492 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2493 is_tilable = isl_set_is_subset(delta_set, slice);
2494 isl_set_free(slice);
2496 slice = isl_set_universe(isl_set_get_space(delta_set));
2497 for (i = 0; i < parallel; ++i)
2498 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2499 is_parallel = isl_set_is_subset(delta_set, slice);
2500 isl_set_free(slice);
2502 origin = isl_set_universe(isl_set_get_space(delta_set));
2503 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2504 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2506 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2507 delta_set = isl_set_lexmin(delta_set);
2509 is_nonneg = isl_set_is_equal(delta_set, origin);
2511 isl_set_free(origin);
2512 isl_set_free(delta_set);
2515 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2516 is_injection < 0 || is_complete < 0)
2517 return -1;
2518 if (!is_complete)
2519 isl_die(ctx, isl_error_unknown,
2520 "generated schedule incomplete", return -1);
2521 if (!is_injection)
2522 isl_die(ctx, isl_error_unknown,
2523 "generated schedule not injective on each statement",
2524 return -1);
2525 if (!is_nonneg)
2526 isl_die(ctx, isl_error_unknown,
2527 "negative dependences in generated schedule",
2528 return -1);
2529 if (!is_tilable)
2530 isl_die(ctx, isl_error_unknown,
2531 "generated schedule not as tilable as expected",
2532 return -1);
2533 if (!is_parallel)
2534 isl_die(ctx, isl_error_unknown,
2535 "generated schedule not as parallel as expected",
2536 return -1);
2538 return 0;
2541 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2542 const char *domain, const char *validity, const char *proximity)
2544 isl_union_set *dom;
2545 isl_union_map *dep;
2546 isl_union_map *prox;
2547 isl_schedule_constraints *sc;
2548 isl_schedule *schedule;
2549 isl_union_map *sched;
2551 dom = isl_union_set_read_from_str(ctx, domain);
2552 dep = isl_union_map_read_from_str(ctx, validity);
2553 prox = isl_union_map_read_from_str(ctx, proximity);
2554 sc = isl_schedule_constraints_on_domain(dom);
2555 sc = isl_schedule_constraints_set_validity(sc, dep);
2556 sc = isl_schedule_constraints_set_proximity(sc, prox);
2557 schedule = isl_schedule_constraints_compute_schedule(sc);
2558 sched = isl_schedule_get_map(schedule);
2559 isl_schedule_free(schedule);
2561 return sched;
2564 /* Check that a schedule can be constructed on the given domain
2565 * with the given validity and proximity constraints.
2567 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2568 const char *validity, const char *proximity)
2570 isl_union_map *sched;
2572 sched = compute_schedule(ctx, domain, validity, proximity);
2573 if (!sched)
2574 return -1;
2576 isl_union_map_free(sched);
2577 return 0;
2580 int test_special_schedule(isl_ctx *ctx, const char *domain,
2581 const char *validity, const char *proximity, const char *expected_sched)
2583 isl_union_map *sched1, *sched2;
2584 int equal;
2586 sched1 = compute_schedule(ctx, domain, validity, proximity);
2587 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2589 equal = isl_union_map_is_equal(sched1, sched2);
2590 isl_union_map_free(sched1);
2591 isl_union_map_free(sched2);
2593 if (equal < 0)
2594 return -1;
2595 if (!equal)
2596 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2597 return -1);
2599 return 0;
2602 /* Check that the schedule map is properly padded, even after being
2603 * reconstructed from the band forest.
2605 static int test_padded_schedule(isl_ctx *ctx)
2607 const char *str;
2608 isl_union_set *D;
2609 isl_union_map *validity, *proximity;
2610 isl_schedule_constraints *sc;
2611 isl_schedule *sched;
2612 isl_union_map *map1, *map2;
2613 isl_band_list *list;
2614 int equal;
2616 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2617 D = isl_union_set_read_from_str(ctx, str);
2618 validity = isl_union_map_empty(isl_union_set_get_space(D));
2619 proximity = isl_union_map_copy(validity);
2620 sc = isl_schedule_constraints_on_domain(D);
2621 sc = isl_schedule_constraints_set_validity(sc, validity);
2622 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2623 sched = isl_schedule_constraints_compute_schedule(sc);
2624 map1 = isl_schedule_get_map(sched);
2625 list = isl_schedule_get_band_forest(sched);
2626 isl_band_list_free(list);
2627 map2 = isl_schedule_get_map(sched);
2628 isl_schedule_free(sched);
2629 equal = isl_union_map_is_equal(map1, map2);
2630 isl_union_map_free(map1);
2631 isl_union_map_free(map2);
2633 if (equal < 0)
2634 return -1;
2635 if (!equal)
2636 isl_die(ctx, isl_error_unknown,
2637 "reconstructed schedule map not the same as original",
2638 return -1);
2640 return 0;
2643 /* Input for testing of schedule construction based on
2644 * conditional constraints.
2646 * domain is the iteration domain
2647 * flow are the flow dependences, which determine the validity and
2648 * proximity constraints
2649 * condition are the conditions on the conditional validity constraints
2650 * conditional_validity are the conditional validity constraints
2651 * outer_band_n is the expected number of members in the outer band
2653 struct {
2654 const char *domain;
2655 const char *flow;
2656 const char *condition;
2657 const char *conditional_validity;
2658 int outer_band_n;
2659 } live_range_tests[] = {
2660 /* Contrived example that illustrates that we need to keep
2661 * track of tagged condition dependences and
2662 * tagged conditional validity dependences
2663 * in isl_sched_edge separately.
2664 * In particular, the conditional validity constraints on A
2665 * cannot be satisfied,
2666 * but they can be ignored because there are no corresponding
2667 * condition constraints. However, we do have an additional
2668 * conditional validity constraint that maps to the same
2669 * dependence relation
2670 * as the condition constraint on B. If we did not make a distinction
2671 * between tagged condition and tagged conditional validity
2672 * dependences, then we
2673 * could end up treating this shared dependence as an condition
2674 * constraint on A, forcing a localization of the conditions,
2675 * which is impossible.
2677 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
2678 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
2679 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
2680 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2681 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2682 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
2685 /* TACO 2013 Fig. 7 */
2686 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2687 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2688 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2689 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
2690 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
2691 "0 <= i < n and 0 <= j < n - 1 }",
2692 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
2693 "0 <= i < n and 0 <= j < j' < n;"
2694 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
2695 "0 <= i < i' < n and 0 <= j,j' < n;"
2696 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
2697 "0 <= i,j,j' < n and j < j' }",
2700 /* TACO 2013 Fig. 7, without tags */
2701 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2702 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2703 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
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] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
2707 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
2708 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
2711 /* TACO 2013 Fig. 12 */
2712 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
2713 "S3[i,3] : 0 <= i <= 1 }",
2714 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
2715 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
2716 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
2717 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
2718 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2719 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
2720 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2721 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
2722 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
2723 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
2724 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
2729 /* Test schedule construction based on conditional constraints.
2730 * In particular, check the number of members in the outer band
2731 * as an indication of whether tiling is possible or not.
2733 static int test_conditional_schedule_constraints(isl_ctx *ctx)
2735 int i;
2736 isl_union_set *domain;
2737 isl_union_map *condition;
2738 isl_union_map *flow;
2739 isl_union_map *validity;
2740 isl_schedule_constraints *sc;
2741 isl_schedule *schedule;
2742 isl_band_list *list;
2743 isl_band *band;
2744 int n_member;
2746 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
2747 domain = isl_union_set_read_from_str(ctx,
2748 live_range_tests[i].domain);
2749 flow = isl_union_map_read_from_str(ctx,
2750 live_range_tests[i].flow);
2751 condition = isl_union_map_read_from_str(ctx,
2752 live_range_tests[i].condition);
2753 validity = isl_union_map_read_from_str(ctx,
2754 live_range_tests[i].conditional_validity);
2755 sc = isl_schedule_constraints_on_domain(domain);
2756 sc = isl_schedule_constraints_set_validity(sc,
2757 isl_union_map_copy(flow));
2758 sc = isl_schedule_constraints_set_proximity(sc, flow);
2759 sc = isl_schedule_constraints_set_conditional_validity(sc,
2760 condition, validity);
2761 schedule = isl_schedule_constraints_compute_schedule(sc);
2762 list = isl_schedule_get_band_forest(schedule);
2763 band = isl_band_list_get_band(list, 0);
2764 n_member = isl_band_n_member(band);
2765 isl_band_free(band);
2766 isl_band_list_free(list);
2767 isl_schedule_free(schedule);
2769 if (!schedule)
2770 return -1;
2771 if (n_member != live_range_tests[i].outer_band_n)
2772 isl_die(ctx, isl_error_unknown,
2773 "unexpected number of members in outer band",
2774 return -1);
2776 return 0;
2779 int test_schedule(isl_ctx *ctx)
2781 const char *D, *W, *R, *V, *P, *S;
2783 /* Handle resulting schedule with zero bands. */
2784 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2785 return -1;
2787 /* Jacobi */
2788 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2789 W = "{ S1[t,i] -> a[t,i] }";
2790 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2791 "S1[t,i] -> a[t-1,i+1] }";
2792 S = "{ S1[t,i] -> [t,i] }";
2793 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2794 return -1;
2796 /* Fig. 5 of CC2008 */
2797 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2798 "j <= -1 + N }";
2799 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2800 "j >= 2 and j <= -1 + N }";
2801 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2802 "j >= 2 and j <= -1 + N; "
2803 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2804 "j >= 2 and j <= -1 + N }";
2805 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2806 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2807 return -1;
2809 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2810 W = "{ S1[i] -> a[i] }";
2811 R = "{ S2[i] -> a[i+1] }";
2812 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2813 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2814 return -1;
2816 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2817 W = "{ S1[i] -> a[i] }";
2818 R = "{ S2[i] -> a[9-i] }";
2819 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2820 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2821 return -1;
2823 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2824 W = "{ S1[i] -> a[i] }";
2825 R = "[N] -> { S2[i] -> a[N-1-i] }";
2826 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2827 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2828 return -1;
2830 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2831 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2832 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2833 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2834 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2835 return -1;
2837 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2838 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2839 R = "{ S2[i,j] -> a[i-1,j] }";
2840 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2841 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2842 return -1;
2844 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2845 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2846 R = "{ S2[i,j] -> a[i,j-1] }";
2847 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2848 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2849 return -1;
2851 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2852 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2853 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2854 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2855 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2856 "S_0[] -> [0, 0, 0] }";
2857 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2858 return -1;
2859 ctx->opt->schedule_parametric = 0;
2860 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2861 return -1;
2862 ctx->opt->schedule_parametric = 1;
2864 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2865 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2866 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2867 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2868 "S4[i] -> a[i,N] }";
2869 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2870 "S4[i] -> [4,i,0] }";
2871 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2872 return -1;
2874 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2875 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2876 "j <= N }";
2877 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2878 "j <= N; "
2879 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2880 "j <= N }";
2881 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2882 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2883 return -1;
2885 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2886 " S_2[t] : t >= 0 and t <= -1 + N; "
2887 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2888 "i <= -1 + N }";
2889 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2890 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2891 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2892 "i >= 0 and i <= -1 + N }";
2893 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2894 "i >= 0 and i <= -1 + N; "
2895 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2896 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2897 " S_0[t] -> [0, t, 0] }";
2899 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2900 return -1;
2901 ctx->opt->schedule_parametric = 0;
2902 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2903 return -1;
2904 ctx->opt->schedule_parametric = 1;
2906 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2907 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2908 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2909 return -1;
2911 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2912 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2913 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2914 "j >= 0 and j <= -1 + N; "
2915 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2916 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2917 "j >= 0 and j <= -1 + N; "
2918 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2919 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2920 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2921 return -1;
2923 D = "{ S_0[i] : i >= 0 }";
2924 W = "{ S_0[i] -> a[i] : i >= 0 }";
2925 R = "{ S_0[i] -> a[0] : i >= 0 }";
2926 S = "{ S_0[i] -> [0, i, 0] }";
2927 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2928 return -1;
2930 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2931 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2932 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2933 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2934 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2935 return -1;
2937 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2938 "k <= -1 + n and k >= 0 }";
2939 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
2940 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2941 "k <= -1 + n and k >= 0; "
2942 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2943 "k <= -1 + n and k >= 0; "
2944 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2945 "k <= -1 + n and k >= 0 }";
2946 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2947 ctx->opt->schedule_outer_zero_distance = 1;
2948 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2949 return -1;
2950 ctx->opt->schedule_outer_zero_distance = 0;
2952 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
2953 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
2954 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
2955 "Stmt_for_body24[i0, i1, 1, 0]:"
2956 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
2957 "Stmt_for_body7[i0, i1, i2]:"
2958 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
2959 "i2 <= 7 }";
2961 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
2962 "Stmt_for_body24[1, i1, i2, i3]:"
2963 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
2964 "i2 >= 1;"
2965 "Stmt_for_body24[0, i1, i2, i3] -> "
2966 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
2967 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
2968 "i3 >= 0;"
2969 "Stmt_for_body24[0, i1, i2, i3] ->"
2970 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
2971 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
2972 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
2973 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
2974 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
2975 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
2976 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
2977 "i1 <= 6 and i1 >= 0;"
2978 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
2979 "Stmt_for_body7[i0, i1, i2] -> "
2980 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
2981 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
2982 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
2983 "Stmt_for_body7[i0, i1, i2] -> "
2984 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
2985 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
2986 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
2987 P = V;
2988 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
2989 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
2990 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
2992 if (test_special_schedule(ctx, D, V, P, S) < 0)
2993 return -1;
2995 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
2996 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
2997 "j >= 1 and j <= 7;"
2998 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
2999 "j >= 1 and j <= 8 }";
3000 P = "{ }";
3001 S = "{ S_0[i, j] -> [i + j, j] }";
3002 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3003 if (test_special_schedule(ctx, D, V, P, S) < 0)
3004 return -1;
3005 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3007 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3008 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3009 "j >= 0 and j <= -1 + i }";
3010 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3011 "i <= -1 + N and j >= 0;"
3012 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3013 "i <= -2 + N }";
3014 P = "{ }";
3015 S = "{ S_0[i, j] -> [i, j] }";
3016 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3017 if (test_special_schedule(ctx, D, V, P, S) < 0)
3018 return -1;
3019 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3021 /* Test both algorithms on a case with only proximity dependences. */
3022 D = "{ S[i,j] : 0 <= i <= 10 }";
3023 V = "{ }";
3024 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3025 S = "{ S[i, j] -> [j, i] }";
3026 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3027 if (test_special_schedule(ctx, D, V, P, S) < 0)
3028 return -1;
3029 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3030 if (test_special_schedule(ctx, D, V, P, S) < 0)
3031 return -1;
3033 D = "{ A[a]; B[] }";
3034 V = "{}";
3035 P = "{ A[a] -> B[] }";
3036 if (test_has_schedule(ctx, D, V, P) < 0)
3037 return -1;
3039 if (test_padded_schedule(ctx) < 0)
3040 return -1;
3042 /* Check that check for progress is not confused by rational
3043 * solution.
3045 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3046 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3047 "i0 <= -2 + N; "
3048 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3049 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3050 P = "{}";
3051 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3052 if (test_has_schedule(ctx, D, V, P) < 0)
3053 return -1;
3054 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3056 if (test_conditional_schedule_constraints(ctx) < 0)
3057 return -1;
3059 return 0;
3062 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3064 isl_union_map *umap;
3065 int test;
3067 umap = isl_union_map_read_from_str(ctx, str);
3068 test = isl_union_map_plain_is_injective(umap);
3069 isl_union_map_free(umap);
3070 if (test < 0)
3071 return -1;
3072 if (test == injective)
3073 return 0;
3074 if (injective)
3075 isl_die(ctx, isl_error_unknown,
3076 "map not detected as injective", return -1);
3077 else
3078 isl_die(ctx, isl_error_unknown,
3079 "map detected as injective", return -1);
3082 int test_injective(isl_ctx *ctx)
3084 const char *str;
3086 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
3087 return -1;
3088 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
3089 return -1;
3090 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
3091 return -1;
3092 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
3093 return -1;
3094 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
3095 return -1;
3096 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
3097 return -1;
3098 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
3099 return -1;
3100 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
3101 return -1;
3103 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
3104 if (test_plain_injective(ctx, str, 1))
3105 return -1;
3106 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
3107 if (test_plain_injective(ctx, str, 0))
3108 return -1;
3110 return 0;
3113 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
3115 isl_aff *aff2;
3116 int equal;
3118 if (!aff)
3119 return -1;
3121 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
3122 equal = isl_aff_plain_is_equal(aff, aff2);
3123 isl_aff_free(aff2);
3125 return equal;
3128 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
3130 int equal;
3132 equal = aff_plain_is_equal(aff, str);
3133 if (equal < 0)
3134 return -1;
3135 if (!equal)
3136 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
3137 "result not as expected", return -1);
3138 return 0;
3141 int test_aff(isl_ctx *ctx)
3143 const char *str;
3144 isl_set *set;
3145 isl_space *space;
3146 isl_local_space *ls;
3147 isl_aff *aff;
3148 int zero, equal;
3150 space = isl_space_set_alloc(ctx, 0, 1);
3151 ls = isl_local_space_from_space(space);
3152 aff = isl_aff_zero_on_domain(ls);
3154 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3155 aff = isl_aff_scale_down_ui(aff, 3);
3156 aff = isl_aff_floor(aff);
3157 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3158 aff = isl_aff_scale_down_ui(aff, 2);
3159 aff = isl_aff_floor(aff);
3160 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3162 str = "{ [10] }";
3163 set = isl_set_read_from_str(ctx, str);
3164 aff = isl_aff_gist(aff, set);
3166 aff = isl_aff_add_constant_si(aff, -16);
3167 zero = isl_aff_plain_is_zero(aff);
3168 isl_aff_free(aff);
3170 if (zero < 0)
3171 return -1;
3172 if (!zero)
3173 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3175 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
3176 aff = isl_aff_scale_down_ui(aff, 64);
3177 aff = isl_aff_floor(aff);
3178 equal = aff_check_plain_equal(aff, "{ [-1] }");
3179 isl_aff_free(aff);
3180 if (equal < 0)
3181 return -1;
3183 return 0;
3186 int test_dim_max(isl_ctx *ctx)
3188 int equal;
3189 const char *str;
3190 isl_set *set1, *set2;
3191 isl_set *set;
3192 isl_map *map;
3193 isl_pw_aff *pwaff;
3195 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
3196 set = isl_set_read_from_str(ctx, str);
3197 pwaff = isl_set_dim_max(set, 0);
3198 set1 = isl_set_from_pw_aff(pwaff);
3199 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
3200 set2 = isl_set_read_from_str(ctx, str);
3201 equal = isl_set_is_equal(set1, set2);
3202 isl_set_free(set1);
3203 isl_set_free(set2);
3204 if (equal < 0)
3205 return -1;
3206 if (!equal)
3207 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3209 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
3210 set = isl_set_read_from_str(ctx, str);
3211 pwaff = isl_set_dim_max(set, 0);
3212 set1 = isl_set_from_pw_aff(pwaff);
3213 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3214 set2 = isl_set_read_from_str(ctx, str);
3215 equal = isl_set_is_equal(set1, set2);
3216 isl_set_free(set1);
3217 isl_set_free(set2);
3218 if (equal < 0)
3219 return -1;
3220 if (!equal)
3221 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3223 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
3224 set = isl_set_read_from_str(ctx, str);
3225 pwaff = isl_set_dim_max(set, 0);
3226 set1 = isl_set_from_pw_aff(pwaff);
3227 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3228 set2 = isl_set_read_from_str(ctx, str);
3229 equal = isl_set_is_equal(set1, set2);
3230 isl_set_free(set1);
3231 isl_set_free(set2);
3232 if (equal < 0)
3233 return -1;
3234 if (!equal)
3235 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3237 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
3238 "0 <= i < N and 0 <= j < M }";
3239 map = isl_map_read_from_str(ctx, str);
3240 set = isl_map_range(map);
3242 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
3243 set1 = isl_set_from_pw_aff(pwaff);
3244 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
3245 set2 = isl_set_read_from_str(ctx, str);
3246 equal = isl_set_is_equal(set1, set2);
3247 isl_set_free(set1);
3248 isl_set_free(set2);
3250 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3251 set1 = isl_set_from_pw_aff(pwaff);
3252 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3253 set2 = isl_set_read_from_str(ctx, str);
3254 if (equal >= 0 && equal)
3255 equal = isl_set_is_equal(set1, set2);
3256 isl_set_free(set1);
3257 isl_set_free(set2);
3259 isl_set_free(set);
3261 if (equal < 0)
3262 return -1;
3263 if (!equal)
3264 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3266 /* Check that solutions are properly merged. */
3267 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3268 "c <= -1 + n - 4a - 2b and c >= -2b and "
3269 "4a >= -4 + n and c >= 0 }";
3270 set = isl_set_read_from_str(ctx, str);
3271 pwaff = isl_set_dim_min(set, 2);
3272 set1 = isl_set_from_pw_aff(pwaff);
3273 str = "[n] -> { [(0)] : n >= 1 }";
3274 set2 = isl_set_read_from_str(ctx, str);
3275 equal = isl_set_is_equal(set1, set2);
3276 isl_set_free(set1);
3277 isl_set_free(set2);
3279 if (equal < 0)
3280 return -1;
3281 if (!equal)
3282 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3284 return 0;
3287 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
3289 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
3290 const char *str)
3292 isl_ctx *ctx;
3293 isl_pw_multi_aff *pma2;
3294 int equal;
3296 if (!pma)
3297 return -1;
3299 ctx = isl_pw_multi_aff_get_ctx(pma);
3300 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3301 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
3302 isl_pw_multi_aff_free(pma2);
3304 return equal;
3307 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
3308 * represented by "str".
3310 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
3311 const char *str)
3313 int equal;
3315 equal = pw_multi_aff_plain_is_equal(pma, str);
3316 if (equal < 0)
3317 return -1;
3318 if (!equal)
3319 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
3320 "result not as expected", return -1);
3321 return 0;
3324 /* Basic test for isl_pw_multi_aff_product.
3326 * Check that multiple pieces are properly handled.
3328 static int test_product_pma(isl_ctx *ctx)
3330 int equal;
3331 const char *str;
3332 isl_pw_multi_aff *pma1, *pma2;
3334 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
3335 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3336 str = "{ C[] -> D[] }";
3337 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3338 pma1 = isl_pw_multi_aff_product(pma1, pma2);
3339 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
3340 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
3341 equal = pw_multi_aff_check_plain_equal(pma1, str);
3342 isl_pw_multi_aff_free(pma1);
3343 if (equal < 0)
3344 return -1;
3346 return 0;
3349 int test_product(isl_ctx *ctx)
3351 const char *str;
3352 isl_set *set;
3353 isl_union_set *uset1, *uset2;
3354 int ok;
3356 str = "{ A[i] }";
3357 set = isl_set_read_from_str(ctx, str);
3358 set = isl_set_product(set, isl_set_copy(set));
3359 ok = isl_set_is_wrapping(set);
3360 isl_set_free(set);
3361 if (ok < 0)
3362 return -1;
3363 if (!ok)
3364 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3366 str = "{ [] }";
3367 uset1 = isl_union_set_read_from_str(ctx, str);
3368 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3369 str = "{ [[] -> []] }";
3370 uset2 = isl_union_set_read_from_str(ctx, str);
3371 ok = isl_union_set_is_equal(uset1, uset2);
3372 isl_union_set_free(uset1);
3373 isl_union_set_free(uset2);
3374 if (ok < 0)
3375 return -1;
3376 if (!ok)
3377 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3379 if (test_product_pma(ctx) < 0)
3380 return -1;
3382 return 0;
3385 int test_equal(isl_ctx *ctx)
3387 const char *str;
3388 isl_set *set, *set2;
3389 int equal;
3391 str = "{ S_6[i] }";
3392 set = isl_set_read_from_str(ctx, str);
3393 str = "{ S_7[i] }";
3394 set2 = isl_set_read_from_str(ctx, str);
3395 equal = isl_set_is_equal(set, set2);
3396 isl_set_free(set);
3397 isl_set_free(set2);
3398 if (equal < 0)
3399 return -1;
3400 if (equal)
3401 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3403 return 0;
3406 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3407 enum isl_dim_type type, unsigned pos, int fixed)
3409 int test;
3411 test = isl_map_plain_is_fixed(map, type, pos, NULL);
3412 isl_map_free(map);
3413 if (test < 0)
3414 return -1;
3415 if (test == fixed)
3416 return 0;
3417 if (fixed)
3418 isl_die(ctx, isl_error_unknown,
3419 "map not detected as fixed", return -1);
3420 else
3421 isl_die(ctx, isl_error_unknown,
3422 "map detected as fixed", return -1);
3425 int test_fixed(isl_ctx *ctx)
3427 const char *str;
3428 isl_map *map;
3430 str = "{ [i] -> [i] }";
3431 map = isl_map_read_from_str(ctx, str);
3432 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3433 return -1;
3434 str = "{ [i] -> [1] }";
3435 map = isl_map_read_from_str(ctx, str);
3436 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3437 return -1;
3438 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3439 map = isl_map_read_from_str(ctx, str);
3440 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3441 return -1;
3442 map = isl_map_read_from_str(ctx, str);
3443 map = isl_map_neg(map);
3444 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3445 return -1;
3447 return 0;
3450 int test_vertices(isl_ctx *ctx)
3452 const char *str;
3453 isl_basic_set *bset;
3454 isl_vertices *vertices;
3456 str = "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
3457 bset = isl_basic_set_read_from_str(ctx, str);
3458 vertices = isl_basic_set_compute_vertices(bset);
3459 isl_basic_set_free(bset);
3460 isl_vertices_free(vertices);
3461 if (!vertices)
3462 return -1;
3464 str = "{ A[t, i] : t = 14 and i = 1 }";
3465 bset = isl_basic_set_read_from_str(ctx, str);
3466 vertices = isl_basic_set_compute_vertices(bset);
3467 isl_basic_set_free(bset);
3468 isl_vertices_free(vertices);
3469 if (!vertices)
3470 return -1;
3472 return 0;
3475 int test_union_pw(isl_ctx *ctx)
3477 int equal;
3478 const char *str;
3479 isl_union_set *uset;
3480 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
3482 str = "{ [x] -> x^2 }";
3483 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
3484 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
3485 uset = isl_union_pw_qpolynomial_domain(upwqp1);
3486 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
3487 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
3488 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
3489 isl_union_pw_qpolynomial_free(upwqp1);
3490 isl_union_pw_qpolynomial_free(upwqp2);
3491 if (equal < 0)
3492 return -1;
3493 if (!equal)
3494 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3496 return 0;
3499 int test_output(isl_ctx *ctx)
3501 char *s;
3502 const char *str;
3503 isl_pw_aff *pa;
3504 isl_printer *p;
3505 int equal;
3507 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
3508 pa = isl_pw_aff_read_from_str(ctx, str);
3510 p = isl_printer_to_str(ctx);
3511 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
3512 p = isl_printer_print_pw_aff(p, pa);
3513 s = isl_printer_get_str(p);
3514 isl_printer_free(p);
3515 isl_pw_aff_free(pa);
3516 if (!s)
3517 equal = -1;
3518 else
3519 equal = !strcmp(s, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
3520 free(s);
3521 if (equal < 0)
3522 return -1;
3523 if (!equal)
3524 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3526 return 0;
3529 int test_sample(isl_ctx *ctx)
3531 const char *str;
3532 isl_basic_set *bset1, *bset2;
3533 int empty, subset;
3535 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3536 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3537 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3538 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3539 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3540 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3541 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3542 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3543 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3544 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3545 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3546 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3547 "d - 1073741821e and "
3548 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3549 "3j >= 1 - a + b + 2e and "
3550 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3551 "3i <= 4 - a + 4b - e and "
3552 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3553 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3554 "c <= -1 + a and 3i >= -2 - a + 3e and "
3555 "1073741822e <= 1073741823 - a + 1073741822b + c and "
3556 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3557 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3558 "1073741823e >= 1 + 1073741823b - d and "
3559 "3i >= 1073741823b + c - 1073741823e - f and "
3560 "3i >= 1 + 2b + e + 3g }";
3561 bset1 = isl_basic_set_read_from_str(ctx, str);
3562 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3563 empty = isl_basic_set_is_empty(bset2);
3564 subset = isl_basic_set_is_subset(bset2, bset1);
3565 isl_basic_set_free(bset1);
3566 isl_basic_set_free(bset2);
3567 if (empty < 0 || subset < 0)
3568 return -1;
3569 if (empty)
3570 isl_die(ctx, isl_error_unknown, "point not found", return -1);
3571 if (!subset)
3572 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
3574 return 0;
3577 int test_fixed_power(isl_ctx *ctx)
3579 const char *str;
3580 isl_map *map;
3581 isl_int exp;
3582 int equal;
3584 isl_int_init(exp);
3585 str = "{ [i] -> [i + 1] }";
3586 map = isl_map_read_from_str(ctx, str);
3587 isl_int_set_si(exp, 23);
3588 map = isl_map_fixed_power(map, exp);
3589 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
3590 isl_int_clear(exp);
3591 isl_map_free(map);
3592 if (equal < 0)
3593 return -1;
3595 return 0;
3598 int test_slice(isl_ctx *ctx)
3600 const char *str;
3601 isl_map *map;
3602 int equal;
3604 str = "{ [i] -> [j] }";
3605 map = isl_map_read_from_str(ctx, str);
3606 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
3607 equal = map_check_equal(map, "{ [i] -> [i] }");
3608 isl_map_free(map);
3609 if (equal < 0)
3610 return -1;
3612 str = "{ [i] -> [j] }";
3613 map = isl_map_read_from_str(ctx, str);
3614 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
3615 equal = map_check_equal(map, "{ [i] -> [j] }");
3616 isl_map_free(map);
3617 if (equal < 0)
3618 return -1;
3620 str = "{ [i] -> [j] }";
3621 map = isl_map_read_from_str(ctx, str);
3622 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
3623 equal = map_check_equal(map, "{ [i] -> [-i] }");
3624 isl_map_free(map);
3625 if (equal < 0)
3626 return -1;
3628 str = "{ [i] -> [j] }";
3629 map = isl_map_read_from_str(ctx, str);
3630 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
3631 equal = map_check_equal(map, "{ [0] -> [j] }");
3632 isl_map_free(map);
3633 if (equal < 0)
3634 return -1;
3636 str = "{ [i] -> [j] }";
3637 map = isl_map_read_from_str(ctx, str);
3638 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3639 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
3640 isl_map_free(map);
3641 if (equal < 0)
3642 return -1;
3644 str = "{ [i] -> [j] }";
3645 map = isl_map_read_from_str(ctx, str);
3646 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
3647 equal = map_check_equal(map, "{ [i] -> [j] : false }");
3648 isl_map_free(map);
3649 if (equal < 0)
3650 return -1;
3652 return 0;
3655 int test_eliminate(isl_ctx *ctx)
3657 const char *str;
3658 isl_map *map;
3659 int equal;
3661 str = "{ [i] -> [j] : i = 2j }";
3662 map = isl_map_read_from_str(ctx, str);
3663 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
3664 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
3665 isl_map_free(map);
3666 if (equal < 0)
3667 return -1;
3669 return 0;
3672 /* Check that isl_set_dim_residue_class detects that the values of j
3673 * in the set below are all odd and that it does not detect any spurious
3674 * strides.
3676 static int test_residue_class(isl_ctx *ctx)
3678 const char *str;
3679 isl_set *set;
3680 isl_int m, r;
3681 int res;
3683 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3684 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3685 set = isl_set_read_from_str(ctx, str);
3686 isl_int_init(m);
3687 isl_int_init(r);
3688 res = isl_set_dim_residue_class(set, 1, &m, &r);
3689 if (res >= 0 &&
3690 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
3691 isl_die(ctx, isl_error_unknown, "incorrect residue class",
3692 res = -1);
3693 isl_int_clear(r);
3694 isl_int_clear(m);
3695 isl_set_free(set);
3697 return res;
3700 int test_align_parameters(isl_ctx *ctx)
3702 const char *str;
3703 isl_space *space;
3704 isl_multi_aff *ma1, *ma2;
3705 int equal;
3707 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
3708 ma1 = isl_multi_aff_read_from_str(ctx, str);
3710 space = isl_space_params_alloc(ctx, 1);
3711 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
3712 ma1 = isl_multi_aff_align_params(ma1, space);
3714 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
3715 ma2 = isl_multi_aff_read_from_str(ctx, str);
3717 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3719 isl_multi_aff_free(ma1);
3720 isl_multi_aff_free(ma2);
3722 if (equal < 0)
3723 return -1;
3724 if (!equal)
3725 isl_die(ctx, isl_error_unknown,
3726 "result not as expected", return -1);
3728 return 0;
3731 static int test_list(isl_ctx *ctx)
3733 isl_id *a, *b, *c, *d, *id;
3734 isl_id_list *list;
3735 int ok;
3737 a = isl_id_alloc(ctx, "a", NULL);
3738 b = isl_id_alloc(ctx, "b", NULL);
3739 c = isl_id_alloc(ctx, "c", NULL);
3740 d = isl_id_alloc(ctx, "d", NULL);
3742 list = isl_id_list_alloc(ctx, 4);
3743 list = isl_id_list_add(list, a);
3744 list = isl_id_list_add(list, b);
3745 list = isl_id_list_add(list, c);
3746 list = isl_id_list_add(list, d);
3747 list = isl_id_list_drop(list, 1, 1);
3749 if (isl_id_list_n_id(list) != 3) {
3750 isl_id_list_free(list);
3751 isl_die(ctx, isl_error_unknown,
3752 "unexpected number of elements in list", return -1);
3755 id = isl_id_list_get_id(list, 0);
3756 ok = id == a;
3757 isl_id_free(id);
3758 id = isl_id_list_get_id(list, 1);
3759 ok = ok && id == c;
3760 isl_id_free(id);
3761 id = isl_id_list_get_id(list, 2);
3762 ok = ok && id == d;
3763 isl_id_free(id);
3765 isl_id_list_free(list);
3767 if (!ok)
3768 isl_die(ctx, isl_error_unknown,
3769 "unexpected elements in list", return -1);
3771 return 0;
3774 const char *set_conversion_tests[] = {
3775 "[N] -> { [i] : N - 1 <= 2 i <= N }",
3776 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
3777 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3778 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3781 /* Check that converting from isl_set to isl_pw_multi_aff and back
3782 * to isl_set produces the original isl_set.
3784 static int test_set_conversion(isl_ctx *ctx)
3786 int i;
3787 const char *str;
3788 isl_set *set1, *set2;
3789 isl_pw_multi_aff *pma;
3790 int equal;
3792 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
3793 str = set_conversion_tests[i];
3794 set1 = isl_set_read_from_str(ctx, str);
3795 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
3796 set2 = isl_set_from_pw_multi_aff(pma);
3797 equal = isl_set_is_equal(set1, set2);
3798 isl_set_free(set1);
3799 isl_set_free(set2);
3801 if (equal < 0)
3802 return -1;
3803 if (!equal)
3804 isl_die(ctx, isl_error_unknown, "bad conversion",
3805 return -1);
3808 return 0;
3811 /* Check that converting from isl_map to isl_pw_multi_aff and back
3812 * to isl_map produces the original isl_map.
3814 static int test_map_conversion(isl_ctx *ctx)
3816 const char *str;
3817 isl_map *map1, *map2;
3818 isl_pw_multi_aff *pma;
3819 int equal;
3821 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
3822 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
3823 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
3824 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
3825 "9e <= -2 - 2a) }";
3826 map1 = isl_map_read_from_str(ctx, str);
3827 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
3828 map2 = isl_map_from_pw_multi_aff(pma);
3829 equal = isl_map_is_equal(map1, map2);
3830 isl_map_free(map1);
3831 isl_map_free(map2);
3833 if (equal < 0)
3834 return -1;
3835 if (!equal)
3836 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
3838 return 0;
3841 static int test_conversion(isl_ctx *ctx)
3843 if (test_set_conversion(ctx) < 0)
3844 return -1;
3845 if (test_map_conversion(ctx) < 0)
3846 return -1;
3847 return 0;
3850 /* Check that isl_basic_map_curry does not modify input.
3852 static int test_curry(isl_ctx *ctx)
3854 const char *str;
3855 isl_basic_map *bmap1, *bmap2;
3856 int equal;
3858 str = "{ [A[] -> B[]] -> C[] }";
3859 bmap1 = isl_basic_map_read_from_str(ctx, str);
3860 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
3861 equal = isl_basic_map_is_equal(bmap1, bmap2);
3862 isl_basic_map_free(bmap1);
3863 isl_basic_map_free(bmap2);
3865 if (equal < 0)
3866 return -1;
3867 if (equal)
3868 isl_die(ctx, isl_error_unknown,
3869 "curried map should not be equal to original",
3870 return -1);
3872 return 0;
3875 struct {
3876 const char *set;
3877 const char *ma;
3878 const char *res;
3879 } preimage_tests[] = {
3880 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
3881 "{ A[j,i] -> B[i,j] }",
3882 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
3883 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3884 "{ A[a,b] -> B[a/2,b/6] }",
3885 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
3886 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3887 "{ A[a,b] -> B[a/2,b/6] }",
3888 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
3889 "exists i,j : a = 2 i and b = 6 j }" },
3890 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
3891 "[n] -> { : 0 <= n <= 100 }" },
3892 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3893 "{ A[a] -> B[2a] }",
3894 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
3895 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3896 "{ A[a] -> B[([a/2])] }",
3897 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
3898 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
3899 "{ A[a] -> B[a,a,a/3] }",
3900 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
3901 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
3902 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
3905 static int test_preimage_basic_set(isl_ctx *ctx)
3907 int i;
3908 isl_basic_set *bset1, *bset2;
3909 isl_multi_aff *ma;
3910 int equal;
3912 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
3913 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
3914 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
3915 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
3916 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
3917 equal = isl_basic_set_is_equal(bset1, bset2);
3918 isl_basic_set_free(bset1);
3919 isl_basic_set_free(bset2);
3920 if (equal < 0)
3921 return -1;
3922 if (!equal)
3923 isl_die(ctx, isl_error_unknown, "bad preimage",
3924 return -1);
3927 return 0;
3930 struct {
3931 const char *map;
3932 const char *ma;
3933 const char *res;
3934 } preimage_domain_tests[] = {
3935 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
3936 "{ A[j,i] -> B[i,j] }",
3937 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
3938 { "{ B[i] -> C[i]; D[i] -> E[i] }",
3939 "{ A[i] -> B[i + 1] }",
3940 "{ A[i] -> C[i + 1] }" },
3941 { "{ B[i] -> C[i]; B[i] -> E[i] }",
3942 "{ A[i] -> B[i + 1] }",
3943 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
3944 { "{ B[i] -> C[([i/2])] }",
3945 "{ A[i] -> B[2i] }",
3946 "{ A[i] -> C[i] }" },
3947 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
3948 "{ A[i] -> B[([i/5]), ([i/7])] }",
3949 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
3950 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
3951 "[N] -> { A[] -> B[([N/5])] }",
3952 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
3953 { "{ B[i] -> C[i] : exists a : i = 5 a }",
3954 "{ A[i] -> B[2i] }",
3955 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
3956 { "{ B[i] -> C[i] : exists a : i = 2 a; "
3957 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
3958 "{ A[i] -> B[2i] }",
3959 "{ A[i] -> C[2i] }" },
3962 static int test_preimage_union_map(isl_ctx *ctx)
3964 int i;
3965 isl_union_map *umap1, *umap2;
3966 isl_multi_aff *ma;
3967 int equal;
3969 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
3970 umap1 = isl_union_map_read_from_str(ctx,
3971 preimage_domain_tests[i].map);
3972 ma = isl_multi_aff_read_from_str(ctx,
3973 preimage_domain_tests[i].ma);
3974 umap2 = isl_union_map_read_from_str(ctx,
3975 preimage_domain_tests[i].res);
3976 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
3977 equal = isl_union_map_is_equal(umap1, umap2);
3978 isl_union_map_free(umap1);
3979 isl_union_map_free(umap2);
3980 if (equal < 0)
3981 return -1;
3982 if (!equal)
3983 isl_die(ctx, isl_error_unknown, "bad preimage",
3984 return -1);
3987 return 0;
3990 static int test_preimage(isl_ctx *ctx)
3992 if (test_preimage_basic_set(ctx) < 0)
3993 return -1;
3994 if (test_preimage_union_map(ctx) < 0)
3995 return -1;
3997 return 0;
4000 struct {
4001 const char *ma1;
4002 const char *ma;
4003 const char *res;
4004 } pullback_tests[] = {
4005 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
4006 "{ A[a,b] -> C[b + 2a] }" },
4007 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
4008 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
4009 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
4010 "{ A[a] -> C[(a)/6] }" },
4011 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
4012 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
4013 "{ A[a] -> C[(2a)/3] }" },
4014 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
4015 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
4016 "{ A[i,j] -> C[i + j, i + j] }"},
4017 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
4018 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
4019 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
4022 static int test_pullback(isl_ctx *ctx)
4024 int i;
4025 isl_multi_aff *ma1, *ma2;
4026 isl_multi_aff *ma;
4027 int equal;
4029 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
4030 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
4031 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
4032 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
4033 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
4034 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4035 isl_multi_aff_free(ma1);
4036 isl_multi_aff_free(ma2);
4037 if (equal < 0)
4038 return -1;
4039 if (!equal)
4040 isl_die(ctx, isl_error_unknown, "bad pullback",
4041 return -1);
4044 return 0;
4047 /* Check that negation is printed correctly.
4049 static int test_ast(isl_ctx *ctx)
4051 isl_ast_expr *expr, *expr1, *expr2, *expr3;
4052 char *str;
4053 int ok;
4055 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4056 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4057 expr = isl_ast_expr_add(expr1, expr2);
4058 expr = isl_ast_expr_neg(expr);
4059 str = isl_ast_expr_to_str(expr);
4060 ok = str ? !strcmp(str, "-(A + B)") : -1;
4061 free(str);
4062 isl_ast_expr_free(expr);
4064 if (ok < 0)
4065 return -1;
4066 if (!ok)
4067 isl_die(ctx, isl_error_unknown,
4068 "isl_ast_expr printed incorrectly", return -1);
4070 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4071 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4072 expr = isl_ast_expr_add(expr1, expr2);
4073 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
4074 expr = isl_ast_expr_sub(expr3, expr);
4075 str = isl_ast_expr_to_str(expr);
4076 ok = str ? !strcmp(str, "C - (A + B)") : -1;
4077 free(str);
4078 isl_ast_expr_free(expr);
4080 if (ok < 0)
4081 return -1;
4082 if (!ok)
4083 isl_die(ctx, isl_error_unknown,
4084 "isl_ast_expr printed incorrectly", return -1);
4086 return 0;
4089 /* Internal data structure for before_for and after_for callbacks.
4091 * depth is the current depth
4092 * before is the number of times before_for has been called
4093 * after is the number of times after_for has been called
4095 struct isl_test_codegen_data {
4096 int depth;
4097 int before;
4098 int after;
4101 /* This function is called before each for loop in the AST generated
4102 * from test_ast_gen1.
4104 * Increment the number of calls and the depth.
4105 * Check that the space returned by isl_ast_build_get_schedule_space
4106 * matches the target space of the schedule returned by
4107 * isl_ast_build_get_schedule.
4108 * Return an isl_id that is checked by the corresponding call
4109 * to after_for.
4111 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
4112 void *user)
4114 struct isl_test_codegen_data *data = user;
4115 isl_ctx *ctx;
4116 isl_space *space;
4117 isl_union_map *schedule;
4118 isl_union_set *uset;
4119 isl_set *set;
4120 int empty;
4121 char name[] = "d0";
4123 ctx = isl_ast_build_get_ctx(build);
4125 if (data->before >= 3)
4126 isl_die(ctx, isl_error_unknown,
4127 "unexpected number of for nodes", return NULL);
4128 if (data->depth >= 2)
4129 isl_die(ctx, isl_error_unknown,
4130 "unexpected depth", return NULL);
4132 snprintf(name, sizeof(name), "d%d", data->depth);
4133 data->before++;
4134 data->depth++;
4136 schedule = isl_ast_build_get_schedule(build);
4137 uset = isl_union_map_range(schedule);
4138 if (!uset)
4139 return NULL;
4140 if (isl_union_set_n_set(uset) != 1) {
4141 isl_union_set_free(uset);
4142 isl_die(ctx, isl_error_unknown,
4143 "expecting single range space", return NULL);
4146 space = isl_ast_build_get_schedule_space(build);
4147 set = isl_union_set_extract_set(uset, space);
4148 isl_union_set_free(uset);
4149 empty = isl_set_is_empty(set);
4150 isl_set_free(set);
4152 if (empty < 0)
4153 return NULL;
4154 if (empty)
4155 isl_die(ctx, isl_error_unknown,
4156 "spaces don't match", return NULL);
4158 return isl_id_alloc(ctx, name, NULL);
4161 /* This function is called after each for loop in the AST generated
4162 * from test_ast_gen1.
4164 * Increment the number of calls and decrement the depth.
4165 * Check that the annotation attached to the node matches
4166 * the isl_id returned by the corresponding call to before_for.
4168 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
4169 __isl_keep isl_ast_build *build, void *user)
4171 struct isl_test_codegen_data *data = user;
4172 isl_id *id;
4173 const char *name;
4174 int valid;
4176 data->after++;
4177 data->depth--;
4179 if (data->after > data->before)
4180 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4181 "mismatch in number of for nodes",
4182 return isl_ast_node_free(node));
4184 id = isl_ast_node_get_annotation(node);
4185 if (!id)
4186 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4187 "missing annotation", return isl_ast_node_free(node));
4189 name = isl_id_get_name(id);
4190 valid = name && atoi(name + 1) == data->depth;
4191 isl_id_free(id);
4193 if (!valid)
4194 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4195 "wrong annotation", return isl_ast_node_free(node));
4197 return node;
4200 /* Check that the before_each_for and after_each_for callbacks
4201 * are called for each for loop in the generated code,
4202 * that they are called in the right order and that the isl_id
4203 * returned from the before_each_for callback is attached to
4204 * the isl_ast_node passed to the corresponding after_each_for call.
4206 static int test_ast_gen1(isl_ctx *ctx)
4208 const char *str;
4209 isl_set *set;
4210 isl_union_map *schedule;
4211 isl_ast_build *build;
4212 isl_ast_node *tree;
4213 struct isl_test_codegen_data data;
4215 str = "[N] -> { : N >= 10 }";
4216 set = isl_set_read_from_str(ctx, str);
4217 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
4218 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
4219 schedule = isl_union_map_read_from_str(ctx, str);
4221 data.before = 0;
4222 data.after = 0;
4223 data.depth = 0;
4224 build = isl_ast_build_from_context(set);
4225 build = isl_ast_build_set_before_each_for(build,
4226 &before_for, &data);
4227 build = isl_ast_build_set_after_each_for(build,
4228 &after_for, &data);
4229 tree = isl_ast_build_ast_from_schedule(build, schedule);
4230 isl_ast_build_free(build);
4231 if (!tree)
4232 return -1;
4234 isl_ast_node_free(tree);
4236 if (data.before != 3 || data.after != 3)
4237 isl_die(ctx, isl_error_unknown,
4238 "unexpected number of for nodes", return -1);
4240 return 0;
4243 /* Check that the AST generator handles domains that are integrally disjoint
4244 * but not ratinoally disjoint.
4246 static int test_ast_gen2(isl_ctx *ctx)
4248 const char *str;
4249 isl_set *set;
4250 isl_union_map *schedule;
4251 isl_union_map *options;
4252 isl_ast_build *build;
4253 isl_ast_node *tree;
4255 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
4256 schedule = isl_union_map_read_from_str(ctx, str);
4257 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4258 build = isl_ast_build_from_context(set);
4260 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
4261 options = isl_union_map_read_from_str(ctx, str);
4262 build = isl_ast_build_set_options(build, options);
4263 tree = isl_ast_build_ast_from_schedule(build, schedule);
4264 isl_ast_build_free(build);
4265 if (!tree)
4266 return -1;
4267 isl_ast_node_free(tree);
4269 return 0;
4272 /* Increment *user on each call.
4274 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
4275 __isl_keep isl_ast_build *build, void *user)
4277 int *n = user;
4279 (*n)++;
4281 return node;
4284 /* Test that unrolling tries to minimize the number of instances.
4285 * In particular, for the schedule given below, make sure it generates
4286 * 3 nodes (rather than 101).
4288 static int test_ast_gen3(isl_ctx *ctx)
4290 const char *str;
4291 isl_set *set;
4292 isl_union_map *schedule;
4293 isl_union_map *options;
4294 isl_ast_build *build;
4295 isl_ast_node *tree;
4296 int n_domain = 0;
4298 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
4299 schedule = isl_union_map_read_from_str(ctx, str);
4300 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4302 str = "{ [i] -> unroll[0] }";
4303 options = isl_union_map_read_from_str(ctx, str);
4305 build = isl_ast_build_from_context(set);
4306 build = isl_ast_build_set_options(build, options);
4307 build = isl_ast_build_set_at_each_domain(build,
4308 &count_domains, &n_domain);
4309 tree = isl_ast_build_ast_from_schedule(build, schedule);
4310 isl_ast_build_free(build);
4311 if (!tree)
4312 return -1;
4314 isl_ast_node_free(tree);
4316 if (n_domain != 3)
4317 isl_die(ctx, isl_error_unknown,
4318 "unexpected number of for nodes", return -1);
4320 return 0;
4323 /* Check that if the ast_build_exploit_nested_bounds options is set,
4324 * we do not get an outer if node in the generated AST,
4325 * while we do get such an outer if node if the options is not set.
4327 static int test_ast_gen4(isl_ctx *ctx)
4329 const char *str;
4330 isl_set *set;
4331 isl_union_map *schedule;
4332 isl_ast_build *build;
4333 isl_ast_node *tree;
4334 enum isl_ast_node_type type;
4335 int enb;
4337 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4338 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4340 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4342 schedule = isl_union_map_read_from_str(ctx, str);
4343 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4344 build = isl_ast_build_from_context(set);
4345 tree = isl_ast_build_ast_from_schedule(build, schedule);
4346 isl_ast_build_free(build);
4347 if (!tree)
4348 return -1;
4350 type = isl_ast_node_get_type(tree);
4351 isl_ast_node_free(tree);
4353 if (type == isl_ast_node_if)
4354 isl_die(ctx, isl_error_unknown,
4355 "not expecting if node", return -1);
4357 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
4359 schedule = isl_union_map_read_from_str(ctx, str);
4360 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4361 build = isl_ast_build_from_context(set);
4362 tree = isl_ast_build_ast_from_schedule(build, schedule);
4363 isl_ast_build_free(build);
4364 if (!tree)
4365 return -1;
4367 type = isl_ast_node_get_type(tree);
4368 isl_ast_node_free(tree);
4370 if (type != isl_ast_node_if)
4371 isl_die(ctx, isl_error_unknown,
4372 "expecting if node", return -1);
4374 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
4376 return 0;
4379 /* This function is called for each leaf in the AST generated
4380 * from test_ast_gen5.
4382 * We finalize the AST generation by extending the outer schedule
4383 * with a zero-dimensional schedule. If this results in any for loops,
4384 * then this means that we did not pass along enough information
4385 * about the outer schedule to the inner AST generation.
4387 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
4388 void *user)
4390 isl_union_map *schedule, *extra;
4391 isl_ast_node *tree;
4393 schedule = isl_ast_build_get_schedule(build);
4394 extra = isl_union_map_copy(schedule);
4395 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
4396 schedule = isl_union_map_range_product(schedule, extra);
4397 tree = isl_ast_build_ast_from_schedule(build, schedule);
4398 isl_ast_build_free(build);
4400 if (!tree)
4401 return NULL;
4403 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
4404 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
4405 "code should not contain any for loop",
4406 return isl_ast_node_free(tree));
4408 return tree;
4411 /* Check that we do not lose any information when going back and
4412 * forth between internal and external schedule.
4414 * In particular, we create an AST where we unroll the only
4415 * non-constant dimension in the schedule. We therefore do
4416 * not expect any for loops in the AST. However, older versions
4417 * of isl would not pass along enough information about the outer
4418 * schedule when performing an inner code generation from a create_leaf
4419 * callback, resulting in the inner code generation producing a for loop.
4421 static int test_ast_gen5(isl_ctx *ctx)
4423 const char *str;
4424 isl_set *set;
4425 isl_union_map *schedule, *options;
4426 isl_ast_build *build;
4427 isl_ast_node *tree;
4429 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
4430 schedule = isl_union_map_read_from_str(ctx, str);
4432 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
4433 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
4434 options = isl_union_map_read_from_str(ctx, str);
4436 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4437 build = isl_ast_build_from_context(set);
4438 build = isl_ast_build_set_options(build, options);
4439 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
4440 tree = isl_ast_build_ast_from_schedule(build, schedule);
4441 isl_ast_build_free(build);
4442 isl_ast_node_free(tree);
4443 if (!tree)
4444 return -1;
4446 return 0;
4449 static int test_ast_gen(isl_ctx *ctx)
4451 if (test_ast_gen1(ctx) < 0)
4452 return -1;
4453 if (test_ast_gen2(ctx) < 0)
4454 return -1;
4455 if (test_ast_gen3(ctx) < 0)
4456 return -1;
4457 if (test_ast_gen4(ctx) < 0)
4458 return -1;
4459 if (test_ast_gen5(ctx) < 0)
4460 return -1;
4461 return 0;
4464 /* Check if dropping output dimensions from an isl_pw_multi_aff
4465 * works properly.
4467 static int test_pw_multi_aff(isl_ctx *ctx)
4469 const char *str;
4470 isl_pw_multi_aff *pma1, *pma2;
4471 int equal;
4473 str = "{ [i,j] -> [i+j, 4i-j] }";
4474 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4475 str = "{ [i,j] -> [4i-j] }";
4476 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4478 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
4480 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
4482 isl_pw_multi_aff_free(pma1);
4483 isl_pw_multi_aff_free(pma2);
4484 if (equal < 0)
4485 return -1;
4486 if (!equal)
4487 isl_die(ctx, isl_error_unknown,
4488 "expressions not equal", return -1);
4490 return 0;
4493 /* Check that we can properly parse multi piecewise affine expressions
4494 * where the piecewise affine expressions have different domains.
4496 static int test_multi_pw_aff(isl_ctx *ctx)
4498 const char *str;
4499 isl_set *dom, *dom2;
4500 isl_multi_pw_aff *mpa1, *mpa2;
4501 isl_pw_aff *pa;
4502 int equal;
4503 int equal_domain;
4505 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
4506 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
4507 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
4508 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
4509 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
4510 str = "{ [i] -> [(i : i > 0), 2i] }";
4511 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
4513 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
4515 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
4516 dom = isl_pw_aff_domain(pa);
4517 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
4518 dom2 = isl_pw_aff_domain(pa);
4519 equal_domain = isl_set_is_equal(dom, dom2);
4521 isl_set_free(dom);
4522 isl_set_free(dom2);
4523 isl_multi_pw_aff_free(mpa1);
4524 isl_multi_pw_aff_free(mpa2);
4526 if (equal < 0)
4527 return -1;
4528 if (!equal)
4529 isl_die(ctx, isl_error_unknown,
4530 "expressions not equal", return -1);
4532 if (equal_domain < 0)
4533 return -1;
4534 if (equal_domain)
4535 isl_die(ctx, isl_error_unknown,
4536 "domains unexpectedly equal", return -1);
4538 return 0;
4541 /* This is a regression test for a bug where isl_basic_map_simplify
4542 * would end up in an infinite loop. In particular, we construct
4543 * an empty basic set that is not obviously empty.
4544 * isl_basic_set_is_empty marks the basic set as empty.
4545 * After projecting out i3, the variable can be dropped completely,
4546 * but isl_basic_map_simplify refrains from doing so if the basic set
4547 * is empty and would end up in an infinite loop if it didn't test
4548 * explicitly for empty basic maps in the outer loop.
4550 static int test_simplify(isl_ctx *ctx)
4552 const char *str;
4553 isl_basic_set *bset;
4554 int empty;
4556 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
4557 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
4558 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
4559 "i3 >= i2 }";
4560 bset = isl_basic_set_read_from_str(ctx, str);
4561 empty = isl_basic_set_is_empty(bset);
4562 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
4563 isl_basic_set_free(bset);
4564 if (!bset)
4565 return -1;
4566 if (!empty)
4567 isl_die(ctx, isl_error_unknown,
4568 "basic set should be empty", return -1);
4570 return 0;
4573 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
4574 * with gbr context would fail to disable the use of the shifted tableau
4575 * when transferring equalities for the input to the context, resulting
4576 * in invalid sample values.
4578 static int test_partial_lexmin(isl_ctx *ctx)
4580 const char *str;
4581 isl_basic_set *bset;
4582 isl_basic_map *bmap;
4583 isl_map *map;
4585 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
4586 bmap = isl_basic_map_read_from_str(ctx, str);
4587 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
4588 bset = isl_basic_set_read_from_str(ctx, str);
4589 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
4590 isl_map_free(map);
4592 if (!map)
4593 return -1;
4595 return 0;
4598 /* Check that the variable compression performed on the existentially
4599 * quantified variables inside isl_basic_set_compute_divs is not confused
4600 * by the implicit equalities among the parameters.
4602 static int test_compute_divs(isl_ctx *ctx)
4604 const char *str;
4605 isl_basic_set *bset;
4606 isl_set *set;
4608 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
4609 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
4610 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
4611 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
4612 bset = isl_basic_set_read_from_str(ctx, str);
4613 set = isl_basic_set_compute_divs(bset);
4614 isl_set_free(set);
4615 if (!set)
4616 return -1;
4618 return 0;
4621 struct {
4622 const char *name;
4623 int (*fn)(isl_ctx *ctx);
4624 } tests [] = {
4625 { "val", &test_val },
4626 { "compute divs", &test_compute_divs },
4627 { "partial lexmin", &test_partial_lexmin },
4628 { "simplify", &test_simplify },
4629 { "curry", &test_curry },
4630 { "piecewise multi affine expressions", &test_pw_multi_aff },
4631 { "multi piecewise affine expressions", &test_multi_pw_aff },
4632 { "conversion", &test_conversion },
4633 { "list", &test_list },
4634 { "align parameters", &test_align_parameters },
4635 { "preimage", &test_preimage },
4636 { "pullback", &test_pullback },
4637 { "AST", &test_ast },
4638 { "AST generation", &test_ast_gen },
4639 { "eliminate", &test_eliminate },
4640 { "residue class", &test_residue_class },
4641 { "div", &test_div },
4642 { "slice", &test_slice },
4643 { "fixed power", &test_fixed_power },
4644 { "sample", &test_sample },
4645 { "output", &test_output },
4646 { "vertices", &test_vertices },
4647 { "fixed", &test_fixed },
4648 { "equal", &test_equal },
4649 { "product", &test_product },
4650 { "dim_max", &test_dim_max },
4651 { "affine", &test_aff },
4652 { "injective", &test_injective },
4653 { "schedule", &test_schedule },
4654 { "union_pw", &test_union_pw },
4655 { "parse", &test_parse },
4656 { "single-valued", &test_sv },
4657 { "affine hull", &test_affine_hull },
4658 { "coalesce", &test_coalesce },
4659 { "factorize", &test_factorize },
4660 { "subset", &test_subset },
4661 { "subtract", &test_subtract },
4662 { "lexmin", &test_lexmin },
4663 { "gist", &test_gist },
4664 { "piecewise quasi-polynomials", &test_pwqp },
4667 int main()
4669 int i;
4670 struct isl_ctx *ctx;
4672 srcdir = getenv("srcdir");
4673 assert(srcdir);
4675 ctx = isl_ctx_alloc();
4676 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
4677 printf("%s\n", tests[i].name);
4678 if (tests[i].fn(ctx) < 0)
4679 goto error;
4681 test_lift(ctx);
4682 test_bound(ctx);
4683 test_union(ctx);
4684 test_split_periods(ctx);
4685 test_lex(ctx);
4686 test_bijective(ctx);
4687 test_dep(ctx);
4688 test_read(ctx);
4689 test_bounded(ctx);
4690 test_construction(ctx);
4691 test_dim(ctx);
4692 test_application(ctx);
4693 test_convex_hull(ctx);
4694 test_closure(ctx);
4695 isl_ctx_free(ctx);
4696 return 0;
4697 error:
4698 isl_ctx_free(ctx);
4699 return -1;