AX_DETECT_GIT_HEAD: detect git version with newer versions of git
[isl.git] / isl_test.c
blob1b018eb1fa0f92652e91f1fa3e0a4818259021db
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <assert.h>
16 #include <stdio.h>
17 #include <limits.h>
18 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
20 #include <isl_aff_private.h>
21 #include <isl/set.h>
22 #include <isl/flow.h>
23 #include <isl_constraint_private.h>
24 #include <isl/polynomial.h>
25 #include <isl/union_map.h>
26 #include <isl_factorization.h>
27 #include <isl/schedule.h>
28 #include <isl_options_private.h>
29 #include <isl/vertices.h>
30 #include <isl/ast_build.h>
31 #include <isl/val.h>
32 #include <isl/ilp.h>
34 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
36 static char *srcdir;
38 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
39 char *filename;
40 int length;
41 char *pattern = "%s/test_inputs/%s.%s";
43 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
44 + strlen(suffix) + 1;
45 filename = isl_alloc_array(ctx, char, length);
47 if (!filename)
48 return NULL;
50 sprintf(filename, pattern, srcdir, name, suffix);
52 return filename;
55 void test_parse_map(isl_ctx *ctx, const char *str)
57 isl_map *map;
59 map = isl_map_read_from_str(ctx, str);
60 assert(map);
61 isl_map_free(map);
64 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
66 isl_map *map, *map2;
67 int equal;
69 map = isl_map_read_from_str(ctx, str);
70 map2 = isl_map_read_from_str(ctx, str2);
71 equal = isl_map_is_equal(map, map2);
72 isl_map_free(map);
73 isl_map_free(map2);
75 if (equal < 0)
76 return -1;
77 if (!equal)
78 isl_die(ctx, isl_error_unknown, "maps not equal",
79 return -1);
81 return 0;
84 void test_parse_pwqp(isl_ctx *ctx, const char *str)
86 isl_pw_qpolynomial *pwqp;
88 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
89 assert(pwqp);
90 isl_pw_qpolynomial_free(pwqp);
93 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
95 isl_pw_aff *pwaff;
97 pwaff = isl_pw_aff_read_from_str(ctx, str);
98 assert(pwaff);
99 isl_pw_aff_free(pwaff);
102 int test_parse(struct isl_ctx *ctx)
104 isl_map *map, *map2;
105 const char *str, *str2;
107 str = "{ [i] -> [-i] }";
108 map = isl_map_read_from_str(ctx, str);
109 assert(map);
110 isl_map_free(map);
112 str = "{ A[i] -> L[([i/3])] }";
113 map = isl_map_read_from_str(ctx, str);
114 assert(map);
115 isl_map_free(map);
117 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
118 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
119 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
121 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
122 str2 = "{ [x, y] : 2y >= 6 - x }";
123 if (test_parse_map_equal(ctx, str, str2) < 0)
124 return -1;
126 if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
127 "{ [x,y] : x <= y, 2*y + 3 }") < 0)
128 return -1;
129 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
130 if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
131 str) < 0)
132 return -1;
134 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
135 map = isl_map_read_from_str(ctx, str);
136 str = "{ [new, old] -> [o0, o1] : "
137 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
138 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
139 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
140 map2 = isl_map_read_from_str(ctx, str);
141 assert(isl_map_is_equal(map, map2));
142 isl_map_free(map);
143 isl_map_free(map2);
145 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
146 map = isl_map_read_from_str(ctx, str);
147 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
148 map2 = isl_map_read_from_str(ctx, str);
149 assert(isl_map_is_equal(map, map2));
150 isl_map_free(map);
151 isl_map_free(map2);
153 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
154 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
155 if (test_parse_map_equal(ctx, str, str2) < 0)
156 return -1;
158 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
159 str2 = "{ [i,j] -> [min(i,j)] }";
160 if (test_parse_map_equal(ctx, str, str2) < 0)
161 return -1;
163 str = "{ [i,j] : i != j }";
164 str2 = "{ [i,j] : i < j or i > j }";
165 if (test_parse_map_equal(ctx, str, str2) < 0)
166 return -1;
168 str = "{ [i,j] : (i+1)*2 >= j }";
169 str2 = "{ [i, j] : j <= 2 + 2i }";
170 if (test_parse_map_equal(ctx, str, str2) < 0)
171 return -1;
173 str = "{ [i] -> [i > 0 ? 4 : 5] }";
174 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
175 if (test_parse_map_equal(ctx, str, str2) < 0)
176 return -1;
178 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
179 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
180 if (test_parse_map_equal(ctx, str, str2) < 0)
181 return -1;
183 str = "{ [x] : x >= 0 }";
184 str2 = "{ [x] : x-0 >= 0 }";
185 if (test_parse_map_equal(ctx, str, str2) < 0)
186 return -1;
188 str = "{ [i] : ((i > 10)) }";
189 str2 = "{ [i] : i >= 11 }";
190 if (test_parse_map_equal(ctx, str, str2) < 0)
191 return -1;
193 str = "{ [i] -> [0] }";
194 str2 = "{ [i] -> [0 * i] }";
195 if (test_parse_map_equal(ctx, str, str2) < 0)
196 return -1;
198 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
199 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
200 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
201 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
203 if (test_parse_map_equal(ctx, "{ [a] -> [b] : (not false) }",
204 "{ [a] -> [b] : true }") < 0)
205 return -1;
207 if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
208 "{ [i] : i <= 10 }") < 0)
209 return -1;
211 if (test_parse_map_equal(ctx, "{Sym=[n] [i] : i <= n }",
212 "[n] -> { [i] : i <= n }") < 0)
213 return -1;
215 if (test_parse_map_equal(ctx, "{ [*] }", "{ [a] }") < 0)
216 return -1;
218 if (test_parse_map_equal(ctx, "{ [i] : 2*floor(i/2) = i }",
219 "{ [i] : exists a : i = 2 a }") < 0)
220 return -1;
222 if (test_parse_map_equal(ctx, "{ [a] -> [b] : a = 5 implies b = 5 }",
223 "{ [a] -> [b] : a != 5 or b = 5 }") < 0)
224 return -1;
226 if (test_parse_map_equal(ctx, "{ [a] -> [a - 1 : a > 0] }",
227 "{ [a] -> [a - 1] : a > 0 }") < 0)
228 return -1;
229 if (test_parse_map_equal(ctx,
230 "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
231 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }") < 0)
232 return -1;
233 if (test_parse_map_equal(ctx,
234 "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
235 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
236 return -1;
237 if (test_parse_map_equal(ctx,
238 "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
239 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
240 return -1;
241 if (test_parse_map_equal(ctx,
242 "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
243 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
244 return -1;
245 if (test_parse_map_equal(ctx,
246 "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
247 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
248 return -1;
250 return 0;
253 void test_read(struct isl_ctx *ctx)
255 char *filename;
256 FILE *input;
257 struct isl_basic_set *bset1, *bset2;
258 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
260 filename = get_filename(ctx, "set", "omega");
261 assert(filename);
262 input = fopen(filename, "r");
263 assert(input);
265 bset1 = isl_basic_set_read_from_file(ctx, input);
266 bset2 = isl_basic_set_read_from_str(ctx, str);
268 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
270 isl_basic_set_free(bset1);
271 isl_basic_set_free(bset2);
272 free(filename);
274 fclose(input);
277 void test_bounded(struct isl_ctx *ctx)
279 isl_set *set;
280 int bounded;
282 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
283 bounded = isl_set_is_bounded(set);
284 assert(bounded);
285 isl_set_free(set);
287 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
288 bounded = isl_set_is_bounded(set);
289 assert(!bounded);
290 isl_set_free(set);
292 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
293 bounded = isl_set_is_bounded(set);
294 assert(!bounded);
295 isl_set_free(set);
298 /* Construct the basic set { [i] : 5 <= i <= N } */
299 void test_construction(struct isl_ctx *ctx)
301 isl_int v;
302 isl_space *dim;
303 isl_local_space *ls;
304 struct isl_basic_set *bset;
305 struct isl_constraint *c;
307 isl_int_init(v);
309 dim = isl_space_set_alloc(ctx, 1, 1);
310 bset = isl_basic_set_universe(isl_space_copy(dim));
311 ls = isl_local_space_from_space(dim);
313 c = isl_inequality_alloc(isl_local_space_copy(ls));
314 isl_int_set_si(v, -1);
315 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
316 isl_int_set_si(v, 1);
317 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
318 bset = isl_basic_set_add_constraint(bset, c);
320 c = isl_inequality_alloc(isl_local_space_copy(ls));
321 isl_int_set_si(v, 1);
322 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
323 isl_int_set_si(v, -5);
324 isl_constraint_set_constant(c, v);
325 bset = isl_basic_set_add_constraint(bset, c);
327 isl_local_space_free(ls);
328 isl_basic_set_free(bset);
330 isl_int_clear(v);
333 void test_dim(struct isl_ctx *ctx)
335 const char *str;
336 isl_map *map1, *map2;
338 map1 = isl_map_read_from_str(ctx,
339 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
340 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
341 map2 = isl_map_read_from_str(ctx,
342 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
343 assert(isl_map_is_equal(map1, map2));
344 isl_map_free(map2);
346 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
347 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
348 assert(isl_map_is_equal(map1, map2));
350 isl_map_free(map1);
351 isl_map_free(map2);
353 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
354 map1 = isl_map_read_from_str(ctx, str);
355 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
356 map2 = isl_map_read_from_str(ctx, str);
357 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
358 assert(isl_map_is_equal(map1, map2));
360 isl_map_free(map1);
361 isl_map_free(map2);
364 struct {
365 __isl_give isl_val *(*op)(__isl_take isl_val *v);
366 const char *arg;
367 const char *res;
368 } val_un_tests[] = {
369 { &isl_val_neg, "0", "0" },
370 { &isl_val_abs, "0", "0" },
371 { &isl_val_2exp, "0", "1" },
372 { &isl_val_floor, "0", "0" },
373 { &isl_val_ceil, "0", "0" },
374 { &isl_val_neg, "1", "-1" },
375 { &isl_val_neg, "-1", "1" },
376 { &isl_val_neg, "1/2", "-1/2" },
377 { &isl_val_neg, "-1/2", "1/2" },
378 { &isl_val_neg, "infty", "-infty" },
379 { &isl_val_neg, "-infty", "infty" },
380 { &isl_val_neg, "NaN", "NaN" },
381 { &isl_val_abs, "1", "1" },
382 { &isl_val_abs, "-1", "1" },
383 { &isl_val_abs, "1/2", "1/2" },
384 { &isl_val_abs, "-1/2", "1/2" },
385 { &isl_val_abs, "infty", "infty" },
386 { &isl_val_abs, "-infty", "infty" },
387 { &isl_val_abs, "NaN", "NaN" },
388 { &isl_val_floor, "1", "1" },
389 { &isl_val_floor, "-1", "-1" },
390 { &isl_val_floor, "1/2", "0" },
391 { &isl_val_floor, "-1/2", "-1" },
392 { &isl_val_floor, "infty", "infty" },
393 { &isl_val_floor, "-infty", "-infty" },
394 { &isl_val_floor, "NaN", "NaN" },
395 { &isl_val_ceil, "1", "1" },
396 { &isl_val_ceil, "-1", "-1" },
397 { &isl_val_ceil, "1/2", "1" },
398 { &isl_val_ceil, "-1/2", "0" },
399 { &isl_val_ceil, "infty", "infty" },
400 { &isl_val_ceil, "-infty", "-infty" },
401 { &isl_val_ceil, "NaN", "NaN" },
402 { &isl_val_2exp, "-3", "1/8" },
403 { &isl_val_2exp, "-1", "1/2" },
404 { &isl_val_2exp, "1", "2" },
405 { &isl_val_2exp, "2", "4" },
406 { &isl_val_2exp, "3", "8" },
409 /* Perform some basic tests of unary operations on isl_val objects.
411 static int test_un_val(isl_ctx *ctx)
413 int i;
414 isl_val *v, *res;
415 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
416 int ok;
418 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
419 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
420 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
421 fn = val_un_tests[i].op;
422 v = fn(v);
423 if (isl_val_is_nan(res))
424 ok = isl_val_is_nan(v);
425 else
426 ok = isl_val_eq(v, res);
427 isl_val_free(v);
428 isl_val_free(res);
429 if (ok < 0)
430 return -1;
431 if (!ok)
432 isl_die(ctx, isl_error_unknown,
433 "unexpected result", return -1);
436 return 0;
439 struct {
440 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
441 __isl_take isl_val *v2);
442 } val_bin_op[] = {
443 ['+'] = { &isl_val_add },
444 ['-'] = { &isl_val_sub },
445 ['*'] = { &isl_val_mul },
446 ['/'] = { &isl_val_div },
447 ['g'] = { &isl_val_gcd },
448 ['m'] = { &isl_val_min },
449 ['M'] = { &isl_val_max },
452 struct {
453 const char *arg1;
454 unsigned char op;
455 const char *arg2;
456 const char *res;
457 } val_bin_tests[] = {
458 { "0", '+', "0", "0" },
459 { "1", '+', "0", "1" },
460 { "1", '+', "1", "2" },
461 { "1", '-', "1", "0" },
462 { "1", '*', "1", "1" },
463 { "1", '/', "1", "1" },
464 { "2", '*', "3", "6" },
465 { "2", '*', "1/2", "1" },
466 { "2", '*', "1/3", "2/3" },
467 { "2/3", '*', "3/5", "2/5" },
468 { "2/3", '*', "7/5", "14/15" },
469 { "2", '/', "1/2", "4" },
470 { "-2", '/', "-1/2", "4" },
471 { "-2", '/', "1/2", "-4" },
472 { "2", '/', "-1/2", "-4" },
473 { "2", '/', "2", "1" },
474 { "2", '/', "3", "2/3" },
475 { "2/3", '/', "5/3", "2/5" },
476 { "2/3", '/', "5/7", "14/15" },
477 { "0", '/', "0", "NaN" },
478 { "42", '/', "0", "NaN" },
479 { "-42", '/', "0", "NaN" },
480 { "infty", '/', "0", "NaN" },
481 { "-infty", '/', "0", "NaN" },
482 { "NaN", '/', "0", "NaN" },
483 { "0", '/', "NaN", "NaN" },
484 { "42", '/', "NaN", "NaN" },
485 { "-42", '/', "NaN", "NaN" },
486 { "infty", '/', "NaN", "NaN" },
487 { "-infty", '/', "NaN", "NaN" },
488 { "NaN", '/', "NaN", "NaN" },
489 { "0", '/', "infty", "0" },
490 { "42", '/', "infty", "0" },
491 { "-42", '/', "infty", "0" },
492 { "infty", '/', "infty", "NaN" },
493 { "-infty", '/', "infty", "NaN" },
494 { "NaN", '/', "infty", "NaN" },
495 { "0", '/', "-infty", "0" },
496 { "42", '/', "-infty", "0" },
497 { "-42", '/', "-infty", "0" },
498 { "infty", '/', "-infty", "NaN" },
499 { "-infty", '/', "-infty", "NaN" },
500 { "NaN", '/', "-infty", "NaN" },
501 { "1", '-', "1/3", "2/3" },
502 { "1/3", '+', "1/2", "5/6" },
503 { "1/2", '+', "1/2", "1" },
504 { "3/4", '-', "1/4", "1/2" },
505 { "1/2", '-', "1/3", "1/6" },
506 { "infty", '+', "42", "infty" },
507 { "infty", '+', "infty", "infty" },
508 { "42", '+', "infty", "infty" },
509 { "infty", '-', "infty", "NaN" },
510 { "infty", '*', "infty", "infty" },
511 { "infty", '*', "-infty", "-infty" },
512 { "-infty", '*', "infty", "-infty" },
513 { "-infty", '*', "-infty", "infty" },
514 { "0", '*', "infty", "NaN" },
515 { "1", '*', "infty", "infty" },
516 { "infty", '*', "0", "NaN" },
517 { "infty", '*', "42", "infty" },
518 { "42", '-', "infty", "-infty" },
519 { "infty", '+', "-infty", "NaN" },
520 { "4", 'g', "6", "2" },
521 { "5", 'g', "6", "1" },
522 { "42", 'm', "3", "3" },
523 { "42", 'M', "3", "42" },
524 { "3", 'm', "42", "3" },
525 { "3", 'M', "42", "42" },
526 { "42", 'm', "infty", "42" },
527 { "42", 'M', "infty", "infty" },
528 { "42", 'm', "-infty", "-infty" },
529 { "42", 'M', "-infty", "42" },
530 { "42", 'm', "NaN", "NaN" },
531 { "42", 'M', "NaN", "NaN" },
532 { "infty", 'm', "-infty", "-infty" },
533 { "infty", 'M', "-infty", "infty" },
536 /* Perform some basic tests of binary operations on isl_val objects.
538 static int test_bin_val(isl_ctx *ctx)
540 int i;
541 isl_val *v1, *v2, *res;
542 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
543 __isl_take isl_val *v2);
544 int ok;
546 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
547 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
548 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
549 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
550 fn = val_bin_op[val_bin_tests[i].op].fn;
551 v1 = fn(v1, v2);
552 if (isl_val_is_nan(res))
553 ok = isl_val_is_nan(v1);
554 else
555 ok = isl_val_eq(v1, res);
556 isl_val_free(v1);
557 isl_val_free(res);
558 if (ok < 0)
559 return -1;
560 if (!ok)
561 isl_die(ctx, isl_error_unknown,
562 "unexpected result", return -1);
565 return 0;
568 /* Perform some basic tests on isl_val objects.
570 static int test_val(isl_ctx *ctx)
572 if (test_un_val(ctx) < 0)
573 return -1;
574 if (test_bin_val(ctx) < 0)
575 return -1;
576 return 0;
579 static int test_div(isl_ctx *ctx)
581 unsigned n;
582 const char *str;
583 int empty;
584 isl_int v;
585 isl_space *dim;
586 isl_set *set;
587 isl_local_space *ls;
588 struct isl_basic_set *bset;
589 struct isl_constraint *c;
591 isl_int_init(v);
593 /* test 1 */
594 dim = isl_space_set_alloc(ctx, 0, 3);
595 bset = isl_basic_set_universe(isl_space_copy(dim));
596 ls = isl_local_space_from_space(dim);
598 c = isl_equality_alloc(isl_local_space_copy(ls));
599 isl_int_set_si(v, -1);
600 isl_constraint_set_constant(c, v);
601 isl_int_set_si(v, 1);
602 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
603 isl_int_set_si(v, 3);
604 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
605 bset = isl_basic_set_add_constraint(bset, c);
607 c = isl_equality_alloc(isl_local_space_copy(ls));
608 isl_int_set_si(v, 1);
609 isl_constraint_set_constant(c, v);
610 isl_int_set_si(v, -1);
611 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
612 isl_int_set_si(v, 3);
613 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
614 bset = isl_basic_set_add_constraint(bset, c);
616 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
618 assert(bset && bset->n_div == 1);
619 isl_local_space_free(ls);
620 isl_basic_set_free(bset);
622 /* test 2 */
623 dim = isl_space_set_alloc(ctx, 0, 3);
624 bset = isl_basic_set_universe(isl_space_copy(dim));
625 ls = isl_local_space_from_space(dim);
627 c = isl_equality_alloc(isl_local_space_copy(ls));
628 isl_int_set_si(v, 1);
629 isl_constraint_set_constant(c, v);
630 isl_int_set_si(v, -1);
631 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
632 isl_int_set_si(v, 3);
633 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
634 bset = isl_basic_set_add_constraint(bset, c);
636 c = isl_equality_alloc(isl_local_space_copy(ls));
637 isl_int_set_si(v, -1);
638 isl_constraint_set_constant(c, v);
639 isl_int_set_si(v, 1);
640 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
641 isl_int_set_si(v, 3);
642 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
643 bset = isl_basic_set_add_constraint(bset, c);
645 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
647 assert(bset && bset->n_div == 1);
648 isl_local_space_free(ls);
649 isl_basic_set_free(bset);
651 /* test 3 */
652 dim = isl_space_set_alloc(ctx, 0, 3);
653 bset = isl_basic_set_universe(isl_space_copy(dim));
654 ls = isl_local_space_from_space(dim);
656 c = isl_equality_alloc(isl_local_space_copy(ls));
657 isl_int_set_si(v, 1);
658 isl_constraint_set_constant(c, v);
659 isl_int_set_si(v, -1);
660 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
661 isl_int_set_si(v, 3);
662 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
663 bset = isl_basic_set_add_constraint(bset, c);
665 c = isl_equality_alloc(isl_local_space_copy(ls));
666 isl_int_set_si(v, -3);
667 isl_constraint_set_constant(c, v);
668 isl_int_set_si(v, 1);
669 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
670 isl_int_set_si(v, 4);
671 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
672 bset = isl_basic_set_add_constraint(bset, c);
674 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
676 assert(bset && bset->n_div == 1);
677 isl_local_space_free(ls);
678 isl_basic_set_free(bset);
680 /* test 4 */
681 dim = isl_space_set_alloc(ctx, 0, 3);
682 bset = isl_basic_set_universe(isl_space_copy(dim));
683 ls = isl_local_space_from_space(dim);
685 c = isl_equality_alloc(isl_local_space_copy(ls));
686 isl_int_set_si(v, 2);
687 isl_constraint_set_constant(c, v);
688 isl_int_set_si(v, -1);
689 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
690 isl_int_set_si(v, 3);
691 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
692 bset = isl_basic_set_add_constraint(bset, c);
694 c = isl_equality_alloc(isl_local_space_copy(ls));
695 isl_int_set_si(v, -1);
696 isl_constraint_set_constant(c, v);
697 isl_int_set_si(v, 1);
698 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
699 isl_int_set_si(v, 6);
700 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
701 bset = isl_basic_set_add_constraint(bset, c);
703 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
705 assert(isl_basic_set_is_empty(bset));
706 isl_local_space_free(ls);
707 isl_basic_set_free(bset);
709 /* test 5 */
710 dim = isl_space_set_alloc(ctx, 0, 3);
711 bset = isl_basic_set_universe(isl_space_copy(dim));
712 ls = isl_local_space_from_space(dim);
714 c = isl_equality_alloc(isl_local_space_copy(ls));
715 isl_int_set_si(v, -1);
716 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
717 isl_int_set_si(v, 3);
718 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
719 bset = isl_basic_set_add_constraint(bset, c);
721 c = isl_equality_alloc(isl_local_space_copy(ls));
722 isl_int_set_si(v, 1);
723 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
724 isl_int_set_si(v, -3);
725 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
726 bset = isl_basic_set_add_constraint(bset, c);
728 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
730 assert(bset && bset->n_div == 0);
731 isl_basic_set_free(bset);
732 isl_local_space_free(ls);
734 /* test 6 */
735 dim = isl_space_set_alloc(ctx, 0, 3);
736 bset = isl_basic_set_universe(isl_space_copy(dim));
737 ls = isl_local_space_from_space(dim);
739 c = isl_equality_alloc(isl_local_space_copy(ls));
740 isl_int_set_si(v, -1);
741 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
742 isl_int_set_si(v, 6);
743 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
744 bset = isl_basic_set_add_constraint(bset, c);
746 c = isl_equality_alloc(isl_local_space_copy(ls));
747 isl_int_set_si(v, 1);
748 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
749 isl_int_set_si(v, -3);
750 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
751 bset = isl_basic_set_add_constraint(bset, c);
753 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
755 assert(bset && bset->n_div == 1);
756 isl_basic_set_free(bset);
757 isl_local_space_free(ls);
759 /* test 7 */
760 /* This test is a bit tricky. We set up an equality
761 * a + 3b + 3c = 6 e0
762 * Normalization of divs creates _two_ divs
763 * a = 3 e0
764 * c - b - e0 = 2 e1
765 * Afterwards e0 is removed again because it has coefficient -1
766 * and we end up with the original equality and div again.
767 * Perhaps we can avoid the introduction of this temporary div.
769 dim = isl_space_set_alloc(ctx, 0, 4);
770 bset = isl_basic_set_universe(isl_space_copy(dim));
771 ls = isl_local_space_from_space(dim);
773 c = isl_equality_alloc(isl_local_space_copy(ls));
774 isl_int_set_si(v, -1);
775 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
776 isl_int_set_si(v, -3);
777 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
778 isl_int_set_si(v, -3);
779 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
780 isl_int_set_si(v, 6);
781 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
782 bset = isl_basic_set_add_constraint(bset, c);
784 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
786 /* Test disabled for now */
788 assert(bset && bset->n_div == 1);
790 isl_local_space_free(ls);
791 isl_basic_set_free(bset);
793 /* test 8 */
794 dim = isl_space_set_alloc(ctx, 0, 5);
795 bset = isl_basic_set_universe(isl_space_copy(dim));
796 ls = isl_local_space_from_space(dim);
798 c = isl_equality_alloc(isl_local_space_copy(ls));
799 isl_int_set_si(v, -1);
800 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
801 isl_int_set_si(v, -3);
802 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
803 isl_int_set_si(v, -3);
804 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
805 isl_int_set_si(v, 6);
806 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
807 bset = isl_basic_set_add_constraint(bset, c);
809 c = isl_equality_alloc(isl_local_space_copy(ls));
810 isl_int_set_si(v, -1);
811 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
812 isl_int_set_si(v, 1);
813 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
814 isl_int_set_si(v, 1);
815 isl_constraint_set_constant(c, v);
816 bset = isl_basic_set_add_constraint(bset, c);
818 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
820 /* Test disabled for now */
822 assert(bset && bset->n_div == 1);
824 isl_local_space_free(ls);
825 isl_basic_set_free(bset);
827 /* test 9 */
828 dim = isl_space_set_alloc(ctx, 0, 4);
829 bset = isl_basic_set_universe(isl_space_copy(dim));
830 ls = isl_local_space_from_space(dim);
832 c = isl_equality_alloc(isl_local_space_copy(ls));
833 isl_int_set_si(v, 1);
834 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
835 isl_int_set_si(v, -1);
836 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
837 isl_int_set_si(v, -2);
838 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
839 bset = isl_basic_set_add_constraint(bset, c);
841 c = isl_equality_alloc(isl_local_space_copy(ls));
842 isl_int_set_si(v, -1);
843 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
844 isl_int_set_si(v, 3);
845 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
846 isl_int_set_si(v, 2);
847 isl_constraint_set_constant(c, v);
848 bset = isl_basic_set_add_constraint(bset, c);
850 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
852 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
854 assert(!isl_basic_set_is_empty(bset));
856 isl_local_space_free(ls);
857 isl_basic_set_free(bset);
859 /* test 10 */
860 dim = isl_space_set_alloc(ctx, 0, 3);
861 bset = isl_basic_set_universe(isl_space_copy(dim));
862 ls = isl_local_space_from_space(dim);
864 c = isl_equality_alloc(isl_local_space_copy(ls));
865 isl_int_set_si(v, 1);
866 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
867 isl_int_set_si(v, -2);
868 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
869 bset = isl_basic_set_add_constraint(bset, c);
871 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
873 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
875 isl_local_space_free(ls);
876 isl_basic_set_free(bset);
878 isl_int_clear(v);
880 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
881 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
882 set = isl_set_read_from_str(ctx, str);
883 set = isl_set_compute_divs(set);
884 isl_set_free(set);
885 if (!set)
886 return -1;
888 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
889 bset = isl_basic_set_read_from_str(ctx, str);
890 n = isl_basic_set_dim(bset, isl_dim_div);
891 isl_basic_set_free(bset);
892 if (!bset)
893 return -1;
894 if (n != 0)
895 isl_die(ctx, isl_error_unknown,
896 "expecting no existentials", return -1);
898 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
899 set = isl_set_read_from_str(ctx, str);
900 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
901 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
902 empty = isl_set_is_empty(set);
903 isl_set_free(set);
904 if (empty < 0)
905 return -1;
906 if (!empty)
907 isl_die(ctx, isl_error_unknown,
908 "result not as accurate as expected", return -1);
910 return 0;
913 void test_application_case(struct isl_ctx *ctx, const char *name)
915 char *filename;
916 FILE *input;
917 struct isl_basic_set *bset1, *bset2;
918 struct isl_basic_map *bmap;
920 filename = get_filename(ctx, name, "omega");
921 assert(filename);
922 input = fopen(filename, "r");
923 assert(input);
925 bset1 = isl_basic_set_read_from_file(ctx, input);
926 bmap = isl_basic_map_read_from_file(ctx, input);
928 bset1 = isl_basic_set_apply(bset1, bmap);
930 bset2 = isl_basic_set_read_from_file(ctx, input);
932 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
934 isl_basic_set_free(bset1);
935 isl_basic_set_free(bset2);
936 free(filename);
938 fclose(input);
941 void test_application(struct isl_ctx *ctx)
943 test_application_case(ctx, "application");
944 test_application_case(ctx, "application2");
947 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
949 char *filename;
950 FILE *input;
951 struct isl_basic_set *bset1, *bset2;
953 filename = get_filename(ctx, name, "polylib");
954 assert(filename);
955 input = fopen(filename, "r");
956 assert(input);
958 bset1 = isl_basic_set_read_from_file(ctx, input);
959 bset2 = isl_basic_set_read_from_file(ctx, input);
961 bset1 = isl_basic_set_affine_hull(bset1);
963 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
965 isl_basic_set_free(bset1);
966 isl_basic_set_free(bset2);
967 free(filename);
969 fclose(input);
972 int test_affine_hull(struct isl_ctx *ctx)
974 const char *str;
975 isl_set *set;
976 isl_basic_set *bset, *bset2;
977 int n;
978 int subset;
980 test_affine_hull_case(ctx, "affine2");
981 test_affine_hull_case(ctx, "affine");
982 test_affine_hull_case(ctx, "affine3");
984 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
985 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
986 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
987 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
988 set = isl_set_read_from_str(ctx, str);
989 bset = isl_set_affine_hull(set);
990 n = isl_basic_set_dim(bset, isl_dim_div);
991 isl_basic_set_free(bset);
992 if (n != 0)
993 isl_die(ctx, isl_error_unknown, "not expecting any divs",
994 return -1);
996 /* Check that isl_map_affine_hull is not confused by
997 * the reordering of divs in isl_map_align_divs.
999 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1000 "32e0 = b and 32e1 = c); "
1001 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1002 set = isl_set_read_from_str(ctx, str);
1003 bset = isl_set_affine_hull(set);
1004 isl_basic_set_free(bset);
1005 if (!bset)
1006 return -1;
1008 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1009 "32e2 = 31 + 31e0 }";
1010 set = isl_set_read_from_str(ctx, str);
1011 bset = isl_set_affine_hull(set);
1012 str = "{ [a] : exists e : a = 32 e }";
1013 bset2 = isl_basic_set_read_from_str(ctx, str);
1014 subset = isl_basic_set_is_subset(bset, bset2);
1015 isl_basic_set_free(bset);
1016 isl_basic_set_free(bset2);
1017 if (subset < 0)
1018 return -1;
1019 if (!subset)
1020 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1021 return -1);
1023 return 0;
1026 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1028 char *filename;
1029 FILE *input;
1030 struct isl_basic_set *bset1, *bset2;
1031 struct isl_set *set;
1033 filename = get_filename(ctx, name, "polylib");
1034 assert(filename);
1035 input = fopen(filename, "r");
1036 assert(input);
1038 bset1 = isl_basic_set_read_from_file(ctx, input);
1039 bset2 = isl_basic_set_read_from_file(ctx, input);
1041 set = isl_basic_set_union(bset1, bset2);
1042 bset1 = isl_set_convex_hull(set);
1044 bset2 = isl_basic_set_read_from_file(ctx, input);
1046 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1048 isl_basic_set_free(bset1);
1049 isl_basic_set_free(bset2);
1050 free(filename);
1052 fclose(input);
1055 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
1057 const char *str1, *str2;
1058 isl_set *set1, *set2;
1059 int orig_convex = ctx->opt->convex;
1060 ctx->opt->convex = convex;
1062 test_convex_hull_case(ctx, "convex0");
1063 test_convex_hull_case(ctx, "convex1");
1064 test_convex_hull_case(ctx, "convex2");
1065 test_convex_hull_case(ctx, "convex3");
1066 test_convex_hull_case(ctx, "convex4");
1067 test_convex_hull_case(ctx, "convex5");
1068 test_convex_hull_case(ctx, "convex6");
1069 test_convex_hull_case(ctx, "convex7");
1070 test_convex_hull_case(ctx, "convex8");
1071 test_convex_hull_case(ctx, "convex9");
1072 test_convex_hull_case(ctx, "convex10");
1073 test_convex_hull_case(ctx, "convex11");
1074 test_convex_hull_case(ctx, "convex12");
1075 test_convex_hull_case(ctx, "convex13");
1076 test_convex_hull_case(ctx, "convex14");
1077 test_convex_hull_case(ctx, "convex15");
1079 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1080 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1081 "(i0 = 0 and i1 = 0 and i2 = 0) }";
1082 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
1083 set1 = isl_set_read_from_str(ctx, str1);
1084 set2 = isl_set_read_from_str(ctx, str2);
1085 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1086 assert(isl_set_is_equal(set1, set2));
1087 isl_set_free(set1);
1088 isl_set_free(set2);
1090 ctx->opt->convex = orig_convex;
1093 void test_convex_hull(struct isl_ctx *ctx)
1095 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
1096 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
1099 void test_gist_case(struct isl_ctx *ctx, const char *name)
1101 char *filename;
1102 FILE *input;
1103 struct isl_basic_set *bset1, *bset2;
1105 filename = get_filename(ctx, name, "polylib");
1106 assert(filename);
1107 input = fopen(filename, "r");
1108 assert(input);
1110 bset1 = isl_basic_set_read_from_file(ctx, input);
1111 bset2 = isl_basic_set_read_from_file(ctx, input);
1113 bset1 = isl_basic_set_gist(bset1, bset2);
1115 bset2 = isl_basic_set_read_from_file(ctx, input);
1117 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1119 isl_basic_set_free(bset1);
1120 isl_basic_set_free(bset2);
1121 free(filename);
1123 fclose(input);
1126 static int test_gist(struct isl_ctx *ctx)
1128 const char *str;
1129 isl_basic_set *bset1, *bset2;
1130 isl_map *map1, *map2;
1132 test_gist_case(ctx, "gist1");
1134 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1135 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1136 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1137 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1138 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1139 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1140 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1141 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1142 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1143 bset1 = isl_basic_set_read_from_str(ctx, str);
1144 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1145 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1146 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1147 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1148 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1149 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1150 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1151 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1152 bset2 = isl_basic_set_read_from_str(ctx, str);
1153 bset1 = isl_basic_set_gist(bset1, bset2);
1154 assert(bset1 && bset1->n_div == 0);
1155 isl_basic_set_free(bset1);
1157 /* Check that the integer divisions of the second disjunct
1158 * do not spread to the first disjunct.
1160 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1161 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1162 "(exists (e0 = [(-1 + t1)/16], "
1163 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1164 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1165 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1166 "o0 <= 4294967295 and t1 <= -1)) }";
1167 map1 = isl_map_read_from_str(ctx, str);
1168 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1169 map2 = isl_map_read_from_str(ctx, str);
1170 map1 = isl_map_gist(map1, map2);
1171 if (!map1)
1172 return -1;
1173 if (map1->n != 1)
1174 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1175 isl_map_free(map1); return -1);
1176 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1177 isl_die(ctx, isl_error_unknown, "expecting single div",
1178 isl_map_free(map1); return -1);
1179 isl_map_free(map1);
1181 return 0;
1184 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1186 isl_set *set, *set2;
1187 int equal;
1188 int one;
1190 set = isl_set_read_from_str(ctx, str);
1191 set = isl_set_coalesce(set);
1192 set2 = isl_set_read_from_str(ctx, str);
1193 equal = isl_set_is_equal(set, set2);
1194 one = set && set->n == 1;
1195 isl_set_free(set);
1196 isl_set_free(set2);
1198 if (equal < 0)
1199 return -1;
1200 if (!equal)
1201 isl_die(ctx, isl_error_unknown,
1202 "coalesced set not equal to input", return -1);
1203 if (check_one && !one)
1204 isl_die(ctx, isl_error_unknown,
1205 "coalesced set should not be a union", return -1);
1207 return 0;
1210 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1212 int r = 0;
1213 int bounded;
1215 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1216 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1218 if (test_coalesce_set(ctx,
1219 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1220 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1221 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1222 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1223 "-10 <= y <= 0}", 1) < 0)
1224 goto error;
1225 if (test_coalesce_set(ctx,
1226 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
1227 goto error;
1228 if (test_coalesce_set(ctx,
1229 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
1230 goto error;
1232 if (0) {
1233 error:
1234 r = -1;
1237 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1239 return r;
1242 /* Inputs for coalescing tests.
1243 * "str" is a string representation of the input set.
1244 * "single_disjunct" is set if we expect the result to consist of
1245 * a single disjunct.
1247 struct {
1248 int single_disjunct;
1249 const char *str;
1250 } coalesce_tests[] = {
1251 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1252 "y >= x & x >= 2 & 5 >= y }" },
1253 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1254 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1255 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1256 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1257 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1258 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1259 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1260 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1261 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1262 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1263 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1264 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1265 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1266 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1267 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1268 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1269 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1270 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1271 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1272 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1273 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1274 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1275 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1276 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1277 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1278 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1279 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1280 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1281 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1282 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1283 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1284 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1285 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1286 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1287 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1288 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1289 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1290 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1291 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1292 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1293 "[o0, o1, o2, o3, o4, o5, o6]] : "
1294 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1295 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1296 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1297 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1298 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1299 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1300 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1301 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1302 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1303 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1304 "o6 >= i3 + i6 - o3 and M >= 0 and "
1305 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1306 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1307 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1308 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1309 "(o0 = 0 and M >= 2 and N >= 3) or "
1310 "(M = 0 and o0 = 0 and N >= 3) }" },
1311 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1312 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1313 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1314 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1315 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1316 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1317 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1318 "(y = 3 and x = 1) }" },
1319 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1320 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1321 "i1 <= M and i3 <= M and i4 <= M) or "
1322 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1323 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1324 "i4 <= -1 + M) }" },
1325 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1326 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1327 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1328 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1329 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1330 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1331 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1332 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1333 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1334 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1335 { 0, "{ [a, b] : exists e : 2e = a and "
1336 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1337 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1338 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1339 "j >= 1 and j' <= i + j - i' and i >= 1; "
1340 "[1, 1, 1, 1] }" },
1341 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1342 "[i,j] : exists a : j = 3a }" },
1343 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1344 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1345 "a >= 3) or "
1346 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1347 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1348 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1349 "c <= 6 + 8a and a >= 3; "
1350 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1351 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1352 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1353 "[x,0] : 3 <= x <= 4 }" },
1354 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1355 "[x,0] : 4 <= x <= 5 }" },
1356 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1357 "[x,0] : 3 <= x <= 5 }" },
1358 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1359 "[x,0] : 3 <= x <= 4 }" },
1360 { 1 , "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1361 "i1 <= 0; "
1362 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1365 /* Test the functionality of isl_set_coalesce.
1366 * That is, check that the output is always equal to the input
1367 * and in some cases that the result consists of a single disjunct.
1369 static int test_coalesce(struct isl_ctx *ctx)
1371 int i;
1373 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1374 const char *str = coalesce_tests[i].str;
1375 int check_one = coalesce_tests[i].single_disjunct;
1376 if (test_coalesce_set(ctx, str, check_one) < 0)
1377 return -1;
1380 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1381 return -1;
1383 return 0;
1386 void test_closure(struct isl_ctx *ctx)
1388 const char *str;
1389 isl_set *dom;
1390 isl_map *up, *right;
1391 isl_map *map, *map2;
1392 int exact;
1394 /* COCOA example 1 */
1395 map = isl_map_read_from_str(ctx,
1396 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1397 "1 <= i and i < n and 1 <= j and j < n or "
1398 "i2 = i + 1 and j2 = j - 1 and "
1399 "1 <= i and i < n and 2 <= j and j <= n }");
1400 map = isl_map_power(map, &exact);
1401 assert(exact);
1402 isl_map_free(map);
1404 /* COCOA example 1 */
1405 map = isl_map_read_from_str(ctx,
1406 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1407 "1 <= i and i < n and 1 <= j and j < n or "
1408 "i2 = i + 1 and j2 = j - 1 and "
1409 "1 <= i and i < n and 2 <= j and j <= n }");
1410 map = isl_map_transitive_closure(map, &exact);
1411 assert(exact);
1412 map2 = isl_map_read_from_str(ctx,
1413 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1414 "1 <= i and i < n and 1 <= j and j <= n and "
1415 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1416 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1417 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1418 assert(isl_map_is_equal(map, map2));
1419 isl_map_free(map2);
1420 isl_map_free(map);
1422 map = isl_map_read_from_str(ctx,
1423 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1424 " 0 <= y and y <= n }");
1425 map = isl_map_transitive_closure(map, &exact);
1426 map2 = isl_map_read_from_str(ctx,
1427 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1428 " 0 <= y and y <= n }");
1429 assert(isl_map_is_equal(map, map2));
1430 isl_map_free(map2);
1431 isl_map_free(map);
1433 /* COCOA example 2 */
1434 map = isl_map_read_from_str(ctx,
1435 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1436 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1437 "i2 = i + 2 and j2 = j - 2 and "
1438 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1439 map = isl_map_transitive_closure(map, &exact);
1440 assert(exact);
1441 map2 = isl_map_read_from_str(ctx,
1442 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1443 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1444 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1445 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1446 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1447 assert(isl_map_is_equal(map, map2));
1448 isl_map_free(map);
1449 isl_map_free(map2);
1451 /* COCOA Fig.2 left */
1452 map = isl_map_read_from_str(ctx,
1453 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1454 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1455 "j <= n or "
1456 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1457 "j <= 2 i - 3 and j <= n - 2 or "
1458 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1459 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1460 map = isl_map_transitive_closure(map, &exact);
1461 assert(exact);
1462 isl_map_free(map);
1464 /* COCOA Fig.2 right */
1465 map = isl_map_read_from_str(ctx,
1466 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1467 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1468 "j <= n or "
1469 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1470 "j <= 2 i - 4 and j <= n - 3 or "
1471 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1472 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1473 map = isl_map_power(map, &exact);
1474 assert(exact);
1475 isl_map_free(map);
1477 /* COCOA Fig.2 right */
1478 map = isl_map_read_from_str(ctx,
1479 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1480 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1481 "j <= n or "
1482 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1483 "j <= 2 i - 4 and j <= n - 3 or "
1484 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1485 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1486 map = isl_map_transitive_closure(map, &exact);
1487 assert(exact);
1488 map2 = isl_map_read_from_str(ctx,
1489 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1490 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1491 "j <= n and 3 + i + 2 j <= 3 n and "
1492 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1493 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1494 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1495 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1496 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1497 assert(isl_map_is_equal(map, map2));
1498 isl_map_free(map2);
1499 isl_map_free(map);
1501 /* COCOA Fig.1 right */
1502 dom = isl_set_read_from_str(ctx,
1503 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1504 "2 x - 3 y + 3 >= 0 }");
1505 right = isl_map_read_from_str(ctx,
1506 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1507 up = isl_map_read_from_str(ctx,
1508 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1509 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1510 right = isl_map_intersect_range(right, isl_set_copy(dom));
1511 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1512 up = isl_map_intersect_range(up, dom);
1513 map = isl_map_union(up, right);
1514 map = isl_map_transitive_closure(map, &exact);
1515 assert(exact);
1516 map2 = isl_map_read_from_str(ctx,
1517 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1518 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1519 assert(isl_map_is_equal(map, map2));
1520 isl_map_free(map2);
1521 isl_map_free(map);
1523 /* COCOA Theorem 1 counter example */
1524 map = isl_map_read_from_str(ctx,
1525 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1526 "i2 = 1 and j2 = j or "
1527 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1528 map = isl_map_transitive_closure(map, &exact);
1529 assert(exact);
1530 isl_map_free(map);
1532 map = isl_map_read_from_str(ctx,
1533 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1534 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1535 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1536 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1537 map = isl_map_transitive_closure(map, &exact);
1538 assert(exact);
1539 isl_map_free(map);
1541 /* Kelly et al 1996, fig 12 */
1542 map = isl_map_read_from_str(ctx,
1543 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1544 "1 <= i,j,j+1 <= n or "
1545 "j = n and j2 = 1 and i2 = i + 1 and "
1546 "1 <= i,i+1 <= n }");
1547 map = isl_map_transitive_closure(map, &exact);
1548 assert(exact);
1549 map2 = isl_map_read_from_str(ctx,
1550 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1551 "1 <= i <= n and i = i2 or "
1552 "1 <= i < i2 <= n and 1 <= j <= n and "
1553 "1 <= j2 <= n }");
1554 assert(isl_map_is_equal(map, map2));
1555 isl_map_free(map2);
1556 isl_map_free(map);
1558 /* Omega's closure4 */
1559 map = isl_map_read_from_str(ctx,
1560 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1561 "1 <= x,y <= 10 or "
1562 "x2 = x + 1 and y2 = y and "
1563 "1 <= x <= 20 && 5 <= y <= 15 }");
1564 map = isl_map_transitive_closure(map, &exact);
1565 assert(exact);
1566 isl_map_free(map);
1568 map = isl_map_read_from_str(ctx,
1569 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1570 map = isl_map_transitive_closure(map, &exact);
1571 assert(!exact);
1572 map2 = isl_map_read_from_str(ctx,
1573 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1574 assert(isl_map_is_equal(map, map2));
1575 isl_map_free(map);
1576 isl_map_free(map2);
1578 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1579 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1580 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1581 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1582 map = isl_map_read_from_str(ctx, str);
1583 map = isl_map_transitive_closure(map, &exact);
1584 assert(exact);
1585 map2 = isl_map_read_from_str(ctx, str);
1586 assert(isl_map_is_equal(map, map2));
1587 isl_map_free(map);
1588 isl_map_free(map2);
1590 str = "{[0] -> [1]; [2] -> [3]}";
1591 map = isl_map_read_from_str(ctx, str);
1592 map = isl_map_transitive_closure(map, &exact);
1593 assert(exact);
1594 map2 = isl_map_read_from_str(ctx, str);
1595 assert(isl_map_is_equal(map, map2));
1596 isl_map_free(map);
1597 isl_map_free(map2);
1599 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1600 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1601 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1602 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1603 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1604 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1605 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1606 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1607 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1608 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1609 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1610 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1611 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1612 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1613 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1614 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1615 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1616 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1617 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1618 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1619 map = isl_map_read_from_str(ctx, str);
1620 map = isl_map_transitive_closure(map, NULL);
1621 assert(map);
1622 isl_map_free(map);
1625 void test_lex(struct isl_ctx *ctx)
1627 isl_space *dim;
1628 isl_map *map;
1630 dim = isl_space_set_alloc(ctx, 0, 0);
1631 map = isl_map_lex_le(dim);
1632 assert(!isl_map_is_empty(map));
1633 isl_map_free(map);
1636 static int test_lexmin(struct isl_ctx *ctx)
1638 int equal;
1639 const char *str;
1640 isl_basic_map *bmap;
1641 isl_map *map, *map2;
1642 isl_set *set;
1643 isl_set *set2;
1644 isl_pw_multi_aff *pma;
1646 str = "[p0, p1] -> { [] -> [] : "
1647 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1648 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1649 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1650 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1651 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1652 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1653 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1654 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1655 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1656 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1657 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1658 map = isl_map_read_from_str(ctx, str);
1659 map = isl_map_lexmin(map);
1660 isl_map_free(map);
1662 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1663 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1664 set = isl_set_read_from_str(ctx, str);
1665 set = isl_set_lexmax(set);
1666 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1667 set2 = isl_set_read_from_str(ctx, str);
1668 set = isl_set_intersect(set, set2);
1669 assert(!isl_set_is_empty(set));
1670 isl_set_free(set);
1672 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1673 map = isl_map_read_from_str(ctx, str);
1674 map = isl_map_lexmin(map);
1675 str = "{ [x] -> [5] : 6 <= x <= 8; "
1676 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1677 map2 = isl_map_read_from_str(ctx, str);
1678 assert(isl_map_is_equal(map, map2));
1679 isl_map_free(map);
1680 isl_map_free(map2);
1682 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1683 map = isl_map_read_from_str(ctx, str);
1684 map2 = isl_map_copy(map);
1685 map = isl_map_lexmin(map);
1686 assert(isl_map_is_equal(map, map2));
1687 isl_map_free(map);
1688 isl_map_free(map2);
1690 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1691 map = isl_map_read_from_str(ctx, str);
1692 map = isl_map_lexmin(map);
1693 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1694 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1695 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1696 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1697 map2 = isl_map_read_from_str(ctx, str);
1698 assert(isl_map_is_equal(map, map2));
1699 isl_map_free(map);
1700 isl_map_free(map2);
1702 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1703 " 8i' <= i and 8i' >= -7 + i }";
1704 bmap = isl_basic_map_read_from_str(ctx, str);
1705 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1706 map2 = isl_map_from_pw_multi_aff(pma);
1707 map = isl_map_from_basic_map(bmap);
1708 assert(isl_map_is_equal(map, map2));
1709 isl_map_free(map);
1710 isl_map_free(map2);
1712 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1713 map = isl_map_read_from_str(ctx, str);
1714 map = isl_map_lexmin(map);
1715 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1716 map2 = isl_map_read_from_str(ctx, str);
1717 assert(isl_map_is_equal(map, map2));
1718 isl_map_free(map);
1719 isl_map_free(map2);
1721 /* Check that empty pieces are properly combined. */
1722 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1723 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1724 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1725 map = isl_map_read_from_str(ctx, str);
1726 map = isl_map_lexmin(map);
1727 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1728 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1729 "x >= 4 }";
1730 map2 = isl_map_read_from_str(ctx, str);
1731 assert(isl_map_is_equal(map, map2));
1732 isl_map_free(map);
1733 isl_map_free(map2);
1735 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1736 " 8i' <= i and 8i' >= -7 + i }";
1737 set = isl_set_read_from_str(ctx, str);
1738 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1739 set2 = isl_set_from_pw_multi_aff(pma);
1740 equal = isl_set_is_equal(set, set2);
1741 isl_set_free(set);
1742 isl_set_free(set2);
1743 if (equal < 0)
1744 return -1;
1745 if (!equal)
1746 isl_die(ctx, isl_error_unknown,
1747 "unexpected difference between set and "
1748 "piecewise affine expression", return -1);
1750 return 0;
1753 /* Check that isl_set_min_val and isl_set_max_val compute the correct
1754 * result on non-convex inputs.
1756 static int test_min(struct isl_ctx *ctx)
1758 isl_set *set;
1759 isl_aff *aff;
1760 isl_val *val;
1761 int min_ok, max_ok;
1763 set = isl_set_read_from_str(ctx, "{ [-1]; [1] }");
1764 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x] }");
1765 val = isl_set_min_val(set, aff);
1766 min_ok = isl_val_is_negone(val);
1767 isl_val_free(val);
1768 val = isl_set_max_val(set, aff);
1769 max_ok = isl_val_is_one(val);
1770 isl_val_free(val);
1771 isl_aff_free(aff);
1772 isl_set_free(set);
1774 if (min_ok < 0 || max_ok < 0)
1775 return -1;
1776 if (!min_ok)
1777 isl_die(ctx, isl_error_unknown,
1778 "unexpected minimum", return -1);
1779 if (!max_ok)
1780 isl_die(ctx, isl_error_unknown,
1781 "unexpected maximum", return -1);
1783 return 0;
1786 struct must_may {
1787 isl_map *must;
1788 isl_map *may;
1791 static int collect_must_may(__isl_take isl_map *dep, int must,
1792 void *dep_user, void *user)
1794 struct must_may *mm = (struct must_may *)user;
1796 if (must)
1797 mm->must = isl_map_union(mm->must, dep);
1798 else
1799 mm->may = isl_map_union(mm->may, dep);
1801 return 0;
1804 static int common_space(void *first, void *second)
1806 int depth = *(int *)first;
1807 return 2 * depth;
1810 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1812 isl_map *map2;
1813 int equal;
1815 if (!map)
1816 return -1;
1818 map2 = isl_map_read_from_str(map->ctx, str);
1819 equal = isl_map_is_equal(map, map2);
1820 isl_map_free(map2);
1822 return equal;
1825 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1827 int equal;
1829 equal = map_is_equal(map, str);
1830 if (equal < 0)
1831 return -1;
1832 if (!equal)
1833 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1834 "result not as expected", return -1);
1835 return 0;
1838 void test_dep(struct isl_ctx *ctx)
1840 const char *str;
1841 isl_space *dim;
1842 isl_map *map;
1843 isl_access_info *ai;
1844 isl_flow *flow;
1845 int depth;
1846 struct must_may mm;
1848 depth = 3;
1850 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1851 map = isl_map_read_from_str(ctx, str);
1852 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1854 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1855 map = isl_map_read_from_str(ctx, str);
1856 ai = isl_access_info_add_source(ai, map, 1, &depth);
1858 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1859 map = isl_map_read_from_str(ctx, str);
1860 ai = isl_access_info_add_source(ai, map, 1, &depth);
1862 flow = isl_access_info_compute_flow(ai);
1863 dim = isl_space_alloc(ctx, 0, 3, 3);
1864 mm.must = isl_map_empty(isl_space_copy(dim));
1865 mm.may = isl_map_empty(dim);
1867 isl_flow_foreach(flow, collect_must_may, &mm);
1869 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1870 " [1,10,0] -> [2,5,0] }";
1871 assert(map_is_equal(mm.must, str));
1872 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1873 assert(map_is_equal(mm.may, str));
1875 isl_map_free(mm.must);
1876 isl_map_free(mm.may);
1877 isl_flow_free(flow);
1880 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1881 map = isl_map_read_from_str(ctx, str);
1882 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1884 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1885 map = isl_map_read_from_str(ctx, str);
1886 ai = isl_access_info_add_source(ai, map, 1, &depth);
1888 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1889 map = isl_map_read_from_str(ctx, str);
1890 ai = isl_access_info_add_source(ai, map, 0, &depth);
1892 flow = isl_access_info_compute_flow(ai);
1893 dim = isl_space_alloc(ctx, 0, 3, 3);
1894 mm.must = isl_map_empty(isl_space_copy(dim));
1895 mm.may = isl_map_empty(dim);
1897 isl_flow_foreach(flow, collect_must_may, &mm);
1899 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1900 assert(map_is_equal(mm.must, str));
1901 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1902 assert(map_is_equal(mm.may, str));
1904 isl_map_free(mm.must);
1905 isl_map_free(mm.may);
1906 isl_flow_free(flow);
1909 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1910 map = isl_map_read_from_str(ctx, str);
1911 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1913 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1914 map = isl_map_read_from_str(ctx, str);
1915 ai = isl_access_info_add_source(ai, map, 0, &depth);
1917 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1918 map = isl_map_read_from_str(ctx, str);
1919 ai = isl_access_info_add_source(ai, map, 0, &depth);
1921 flow = isl_access_info_compute_flow(ai);
1922 dim = isl_space_alloc(ctx, 0, 3, 3);
1923 mm.must = isl_map_empty(isl_space_copy(dim));
1924 mm.may = isl_map_empty(dim);
1926 isl_flow_foreach(flow, collect_must_may, &mm);
1928 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1929 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1930 assert(map_is_equal(mm.may, str));
1931 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1932 assert(map_is_equal(mm.must, str));
1934 isl_map_free(mm.must);
1935 isl_map_free(mm.may);
1936 isl_flow_free(flow);
1939 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1940 map = isl_map_read_from_str(ctx, str);
1941 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1943 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1944 map = isl_map_read_from_str(ctx, str);
1945 ai = isl_access_info_add_source(ai, map, 0, &depth);
1947 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1948 map = isl_map_read_from_str(ctx, str);
1949 ai = isl_access_info_add_source(ai, map, 0, &depth);
1951 flow = isl_access_info_compute_flow(ai);
1952 dim = isl_space_alloc(ctx, 0, 3, 3);
1953 mm.must = isl_map_empty(isl_space_copy(dim));
1954 mm.may = isl_map_empty(dim);
1956 isl_flow_foreach(flow, collect_must_may, &mm);
1958 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1959 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1960 assert(map_is_equal(mm.may, str));
1961 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1962 assert(map_is_equal(mm.must, str));
1964 isl_map_free(mm.must);
1965 isl_map_free(mm.may);
1966 isl_flow_free(flow);
1969 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1970 map = isl_map_read_from_str(ctx, str);
1971 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1973 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1974 map = isl_map_read_from_str(ctx, str);
1975 ai = isl_access_info_add_source(ai, map, 0, &depth);
1977 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1978 map = isl_map_read_from_str(ctx, str);
1979 ai = isl_access_info_add_source(ai, map, 0, &depth);
1981 flow = isl_access_info_compute_flow(ai);
1982 dim = isl_space_alloc(ctx, 0, 3, 3);
1983 mm.must = isl_map_empty(isl_space_copy(dim));
1984 mm.may = isl_map_empty(dim);
1986 isl_flow_foreach(flow, collect_must_may, &mm);
1988 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1989 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1990 assert(map_is_equal(mm.may, str));
1991 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1992 assert(map_is_equal(mm.must, str));
1994 isl_map_free(mm.must);
1995 isl_map_free(mm.may);
1996 isl_flow_free(flow);
1999 depth = 5;
2001 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2002 map = isl_map_read_from_str(ctx, str);
2003 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2005 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2006 map = isl_map_read_from_str(ctx, str);
2007 ai = isl_access_info_add_source(ai, map, 1, &depth);
2009 flow = isl_access_info_compute_flow(ai);
2010 dim = isl_space_alloc(ctx, 0, 5, 5);
2011 mm.must = isl_map_empty(isl_space_copy(dim));
2012 mm.may = isl_map_empty(dim);
2014 isl_flow_foreach(flow, collect_must_may, &mm);
2016 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2017 assert(map_is_equal(mm.must, str));
2018 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2019 assert(map_is_equal(mm.may, str));
2021 isl_map_free(mm.must);
2022 isl_map_free(mm.may);
2023 isl_flow_free(flow);
2026 int test_sv(isl_ctx *ctx)
2028 const char *str;
2029 isl_map *map;
2030 isl_union_map *umap;
2031 int sv;
2033 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
2034 map = isl_map_read_from_str(ctx, str);
2035 sv = isl_map_is_single_valued(map);
2036 isl_map_free(map);
2037 if (sv < 0)
2038 return -1;
2039 if (!sv)
2040 isl_die(ctx, isl_error_internal,
2041 "map not detected as single valued", return -1);
2043 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
2044 map = isl_map_read_from_str(ctx, str);
2045 sv = isl_map_is_single_valued(map);
2046 isl_map_free(map);
2047 if (sv < 0)
2048 return -1;
2049 if (sv)
2050 isl_die(ctx, isl_error_internal,
2051 "map detected as single valued", return -1);
2053 str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
2054 umap = isl_union_map_read_from_str(ctx, str);
2055 sv = isl_union_map_is_single_valued(umap);
2056 isl_union_map_free(umap);
2057 if (sv < 0)
2058 return -1;
2059 if (!sv)
2060 isl_die(ctx, isl_error_internal,
2061 "map not detected as single valued", return -1);
2063 str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
2064 umap = isl_union_map_read_from_str(ctx, str);
2065 sv = isl_union_map_is_single_valued(umap);
2066 isl_union_map_free(umap);
2067 if (sv < 0)
2068 return -1;
2069 if (sv)
2070 isl_die(ctx, isl_error_internal,
2071 "map detected as single valued", return -1);
2073 return 0;
2076 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
2078 isl_map *map;
2080 map = isl_map_read_from_str(ctx, str);
2081 if (bijective)
2082 assert(isl_map_is_bijective(map));
2083 else
2084 assert(!isl_map_is_bijective(map));
2085 isl_map_free(map);
2088 void test_bijective(struct isl_ctx *ctx)
2090 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
2091 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
2092 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
2093 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
2094 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
2095 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
2096 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
2097 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
2098 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
2099 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
2100 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
2101 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
2102 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
2105 static int test_pwqp(struct isl_ctx *ctx)
2107 const char *str;
2108 isl_set *set;
2109 isl_pw_qpolynomial *pwqp1, *pwqp2;
2110 int equal;
2112 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2113 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2115 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2116 isl_dim_in, 1, 1);
2118 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2119 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2121 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2123 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2125 isl_pw_qpolynomial_free(pwqp1);
2127 str = "{ [i] -> i }";
2128 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2129 str = "{ [k] : exists a : k = 2a }";
2130 set = isl_set_read_from_str(ctx, str);
2131 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2132 str = "{ [i] -> i }";
2133 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2135 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2137 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2139 isl_pw_qpolynomial_free(pwqp1);
2141 str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
2142 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2143 str = "{ [10] }";
2144 set = isl_set_read_from_str(ctx, str);
2145 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2146 str = "{ [i] -> 16 }";
2147 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2149 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2151 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2153 isl_pw_qpolynomial_free(pwqp1);
2155 str = "{ [i] -> ([(i)/2]) }";
2156 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2157 str = "{ [k] : exists a : k = 2a+1 }";
2158 set = isl_set_read_from_str(ctx, str);
2159 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2160 str = "{ [i] -> -1/2 + 1/2 * i }";
2161 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2163 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2165 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2167 isl_pw_qpolynomial_free(pwqp1);
2169 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2170 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2171 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2172 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2174 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2176 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2178 isl_pw_qpolynomial_free(pwqp1);
2180 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2181 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2182 str = "{ [x] -> x }";
2183 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2185 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2187 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2189 isl_pw_qpolynomial_free(pwqp1);
2191 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2192 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2193 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2194 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2195 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2196 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2197 isl_pw_qpolynomial_free(pwqp1);
2199 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2200 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2201 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2202 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2203 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2204 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2205 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2206 isl_pw_qpolynomial_free(pwqp1);
2207 isl_pw_qpolynomial_free(pwqp2);
2208 if (equal < 0)
2209 return -1;
2210 if (!equal)
2211 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2213 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2214 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2215 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2216 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2217 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2218 isl_val_one(ctx));
2219 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2220 isl_pw_qpolynomial_free(pwqp1);
2221 isl_pw_qpolynomial_free(pwqp2);
2222 if (equal < 0)
2223 return -1;
2224 if (!equal)
2225 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2227 return 0;
2230 void test_split_periods(isl_ctx *ctx)
2232 const char *str;
2233 isl_pw_qpolynomial *pwqp;
2235 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2236 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2237 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2238 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2240 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2241 assert(pwqp);
2243 isl_pw_qpolynomial_free(pwqp);
2246 void test_union(isl_ctx *ctx)
2248 const char *str;
2249 isl_union_set *uset1, *uset2;
2250 isl_union_map *umap1, *umap2;
2252 str = "{ [i] : 0 <= i <= 1 }";
2253 uset1 = isl_union_set_read_from_str(ctx, str);
2254 str = "{ [1] -> [0] }";
2255 umap1 = isl_union_map_read_from_str(ctx, str);
2257 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2258 assert(isl_union_map_is_equal(umap1, umap2));
2260 isl_union_map_free(umap1);
2261 isl_union_map_free(umap2);
2263 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2264 umap1 = isl_union_map_read_from_str(ctx, str);
2265 str = "{ A[i]; B[i] }";
2266 uset1 = isl_union_set_read_from_str(ctx, str);
2268 uset2 = isl_union_map_domain(umap1);
2270 assert(isl_union_set_is_equal(uset1, uset2));
2272 isl_union_set_free(uset1);
2273 isl_union_set_free(uset2);
2276 void test_bound(isl_ctx *ctx)
2278 const char *str;
2279 isl_pw_qpolynomial *pwqp;
2280 isl_pw_qpolynomial_fold *pwf;
2282 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2283 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2284 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2285 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
2286 isl_pw_qpolynomial_fold_free(pwf);
2288 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2289 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2290 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2291 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
2292 isl_pw_qpolynomial_fold_free(pwf);
2295 void test_lift(isl_ctx *ctx)
2297 const char *str;
2298 isl_basic_map *bmap;
2299 isl_basic_set *bset;
2301 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2302 bset = isl_basic_set_read_from_str(ctx, str);
2303 bset = isl_basic_set_lift(bset);
2304 bmap = isl_basic_map_from_range(bset);
2305 bset = isl_basic_map_domain(bmap);
2306 isl_basic_set_free(bset);
2309 struct {
2310 const char *set1;
2311 const char *set2;
2312 int subset;
2313 } subset_tests[] = {
2314 { "{ [112, 0] }",
2315 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2316 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2317 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2318 { "{ [65] }",
2319 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2320 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2321 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2322 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2323 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2324 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2325 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2326 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2327 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2328 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2329 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2330 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2331 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2334 static int test_subset(isl_ctx *ctx)
2336 int i;
2337 isl_set *set1, *set2;
2338 int subset;
2340 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2341 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2342 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2343 subset = isl_set_is_subset(set1, set2);
2344 isl_set_free(set1);
2345 isl_set_free(set2);
2346 if (subset < 0)
2347 return -1;
2348 if (subset != subset_tests[i].subset)
2349 isl_die(ctx, isl_error_unknown,
2350 "incorrect subset result", return -1);
2353 return 0;
2356 struct {
2357 const char *minuend;
2358 const char *subtrahend;
2359 const char *difference;
2360 } subtract_domain_tests[] = {
2361 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2362 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2363 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2366 static int test_subtract(isl_ctx *ctx)
2368 int i;
2369 isl_union_map *umap1, *umap2;
2370 isl_union_set *uset;
2371 int equal;
2373 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2374 umap1 = isl_union_map_read_from_str(ctx,
2375 subtract_domain_tests[i].minuend);
2376 uset = isl_union_set_read_from_str(ctx,
2377 subtract_domain_tests[i].subtrahend);
2378 umap2 = isl_union_map_read_from_str(ctx,
2379 subtract_domain_tests[i].difference);
2380 umap1 = isl_union_map_subtract_domain(umap1, uset);
2381 equal = isl_union_map_is_equal(umap1, umap2);
2382 isl_union_map_free(umap1);
2383 isl_union_map_free(umap2);
2384 if (equal < 0)
2385 return -1;
2386 if (!equal)
2387 isl_die(ctx, isl_error_unknown,
2388 "incorrect subtract domain result", return -1);
2391 return 0;
2394 int test_factorize(isl_ctx *ctx)
2396 const char *str;
2397 isl_basic_set *bset;
2398 isl_factorizer *f;
2400 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2401 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2402 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2403 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2404 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2405 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2406 "3i5 >= -2i0 - i2 + 3i4 }";
2407 bset = isl_basic_set_read_from_str(ctx, str);
2408 f = isl_basic_set_factorizer(bset);
2409 isl_basic_set_free(bset);
2410 isl_factorizer_free(f);
2411 if (!f)
2412 isl_die(ctx, isl_error_unknown,
2413 "failed to construct factorizer", return -1);
2415 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2416 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2417 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2418 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2419 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2420 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2421 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2422 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2423 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2424 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2425 bset = isl_basic_set_read_from_str(ctx, str);
2426 f = isl_basic_set_factorizer(bset);
2427 isl_basic_set_free(bset);
2428 isl_factorizer_free(f);
2429 if (!f)
2430 isl_die(ctx, isl_error_unknown,
2431 "failed to construct factorizer", return -1);
2433 return 0;
2436 static int check_injective(__isl_take isl_map *map, void *user)
2438 int *injective = user;
2440 *injective = isl_map_is_injective(map);
2441 isl_map_free(map);
2443 if (*injective < 0 || !*injective)
2444 return -1;
2446 return 0;
2449 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2450 const char *r, const char *s, int tilable, int parallel)
2452 int i;
2453 isl_union_set *D;
2454 isl_union_map *W, *R, *S;
2455 isl_union_map *empty;
2456 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2457 isl_union_map *validity, *proximity, *coincidence;
2458 isl_union_map *schedule;
2459 isl_union_map *test;
2460 isl_union_set *delta;
2461 isl_union_set *domain;
2462 isl_set *delta_set;
2463 isl_set *slice;
2464 isl_set *origin;
2465 isl_schedule_constraints *sc;
2466 isl_schedule *sched;
2467 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2469 D = isl_union_set_read_from_str(ctx, d);
2470 W = isl_union_map_read_from_str(ctx, w);
2471 R = isl_union_map_read_from_str(ctx, r);
2472 S = isl_union_map_read_from_str(ctx, s);
2474 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2475 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2477 empty = isl_union_map_empty(isl_union_map_get_space(S));
2478 isl_union_map_compute_flow(isl_union_map_copy(R),
2479 isl_union_map_copy(W), empty,
2480 isl_union_map_copy(S),
2481 &dep_raw, NULL, NULL, NULL);
2482 isl_union_map_compute_flow(isl_union_map_copy(W),
2483 isl_union_map_copy(W),
2484 isl_union_map_copy(R),
2485 isl_union_map_copy(S),
2486 &dep_waw, &dep_war, NULL, NULL);
2488 dep = isl_union_map_union(dep_waw, dep_war);
2489 dep = isl_union_map_union(dep, dep_raw);
2490 validity = isl_union_map_copy(dep);
2491 coincidence = isl_union_map_copy(dep);
2492 proximity = isl_union_map_copy(dep);
2494 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
2495 sc = isl_schedule_constraints_set_validity(sc, validity);
2496 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
2497 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2498 sched = isl_schedule_constraints_compute_schedule(sc);
2499 schedule = isl_schedule_get_map(sched);
2500 isl_schedule_free(sched);
2501 isl_union_map_free(W);
2502 isl_union_map_free(R);
2503 isl_union_map_free(S);
2505 is_injection = 1;
2506 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2508 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2509 is_complete = isl_union_set_is_subset(D, domain);
2510 isl_union_set_free(D);
2511 isl_union_set_free(domain);
2513 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2514 test = isl_union_map_apply_range(test, dep);
2515 test = isl_union_map_apply_range(test, schedule);
2517 delta = isl_union_map_deltas(test);
2518 if (isl_union_set_n_set(delta) == 0) {
2519 is_tilable = 1;
2520 is_parallel = 1;
2521 is_nonneg = 1;
2522 isl_union_set_free(delta);
2523 } else {
2524 delta_set = isl_set_from_union_set(delta);
2526 slice = isl_set_universe(isl_set_get_space(delta_set));
2527 for (i = 0; i < tilable; ++i)
2528 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2529 is_tilable = isl_set_is_subset(delta_set, slice);
2530 isl_set_free(slice);
2532 slice = isl_set_universe(isl_set_get_space(delta_set));
2533 for (i = 0; i < parallel; ++i)
2534 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2535 is_parallel = isl_set_is_subset(delta_set, slice);
2536 isl_set_free(slice);
2538 origin = isl_set_universe(isl_set_get_space(delta_set));
2539 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2540 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2542 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2543 delta_set = isl_set_lexmin(delta_set);
2545 is_nonneg = isl_set_is_equal(delta_set, origin);
2547 isl_set_free(origin);
2548 isl_set_free(delta_set);
2551 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2552 is_injection < 0 || is_complete < 0)
2553 return -1;
2554 if (!is_complete)
2555 isl_die(ctx, isl_error_unknown,
2556 "generated schedule incomplete", return -1);
2557 if (!is_injection)
2558 isl_die(ctx, isl_error_unknown,
2559 "generated schedule not injective on each statement",
2560 return -1);
2561 if (!is_nonneg)
2562 isl_die(ctx, isl_error_unknown,
2563 "negative dependences in generated schedule",
2564 return -1);
2565 if (!is_tilable)
2566 isl_die(ctx, isl_error_unknown,
2567 "generated schedule not as tilable as expected",
2568 return -1);
2569 if (!is_parallel)
2570 isl_die(ctx, isl_error_unknown,
2571 "generated schedule not as parallel as expected",
2572 return -1);
2574 return 0;
2577 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2578 const char *domain, const char *validity, const char *proximity)
2580 isl_union_set *dom;
2581 isl_union_map *dep;
2582 isl_union_map *prox;
2583 isl_schedule_constraints *sc;
2584 isl_schedule *schedule;
2585 isl_union_map *sched;
2587 dom = isl_union_set_read_from_str(ctx, domain);
2588 dep = isl_union_map_read_from_str(ctx, validity);
2589 prox = isl_union_map_read_from_str(ctx, proximity);
2590 sc = isl_schedule_constraints_on_domain(dom);
2591 sc = isl_schedule_constraints_set_validity(sc, dep);
2592 sc = isl_schedule_constraints_set_proximity(sc, prox);
2593 schedule = isl_schedule_constraints_compute_schedule(sc);
2594 sched = isl_schedule_get_map(schedule);
2595 isl_schedule_free(schedule);
2597 return sched;
2600 /* Check that a schedule can be constructed on the given domain
2601 * with the given validity and proximity constraints.
2603 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2604 const char *validity, const char *proximity)
2606 isl_union_map *sched;
2608 sched = compute_schedule(ctx, domain, validity, proximity);
2609 if (!sched)
2610 return -1;
2612 isl_union_map_free(sched);
2613 return 0;
2616 int test_special_schedule(isl_ctx *ctx, const char *domain,
2617 const char *validity, const char *proximity, const char *expected_sched)
2619 isl_union_map *sched1, *sched2;
2620 int equal;
2622 sched1 = compute_schedule(ctx, domain, validity, proximity);
2623 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2625 equal = isl_union_map_is_equal(sched1, sched2);
2626 isl_union_map_free(sched1);
2627 isl_union_map_free(sched2);
2629 if (equal < 0)
2630 return -1;
2631 if (!equal)
2632 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2633 return -1);
2635 return 0;
2638 /* Check that the schedule map is properly padded, even after being
2639 * reconstructed from the band forest.
2641 static int test_padded_schedule(isl_ctx *ctx)
2643 const char *str;
2644 isl_union_set *D;
2645 isl_union_map *validity, *proximity;
2646 isl_schedule_constraints *sc;
2647 isl_schedule *sched;
2648 isl_union_map *map1, *map2;
2649 isl_band_list *list;
2650 int equal;
2652 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2653 D = isl_union_set_read_from_str(ctx, str);
2654 validity = isl_union_map_empty(isl_union_set_get_space(D));
2655 proximity = isl_union_map_copy(validity);
2656 sc = isl_schedule_constraints_on_domain(D);
2657 sc = isl_schedule_constraints_set_validity(sc, validity);
2658 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2659 sched = isl_schedule_constraints_compute_schedule(sc);
2660 map1 = isl_schedule_get_map(sched);
2661 list = isl_schedule_get_band_forest(sched);
2662 isl_band_list_free(list);
2663 map2 = isl_schedule_get_map(sched);
2664 isl_schedule_free(sched);
2665 equal = isl_union_map_is_equal(map1, map2);
2666 isl_union_map_free(map1);
2667 isl_union_map_free(map2);
2669 if (equal < 0)
2670 return -1;
2671 if (!equal)
2672 isl_die(ctx, isl_error_unknown,
2673 "reconstructed schedule map not the same as original",
2674 return -1);
2676 return 0;
2679 /* Input for testing of schedule construction based on
2680 * conditional constraints.
2682 * domain is the iteration domain
2683 * flow are the flow dependences, which determine the validity and
2684 * proximity constraints
2685 * condition are the conditions on the conditional validity constraints
2686 * conditional_validity are the conditional validity constraints
2687 * outer_band_n is the expected number of members in the outer band
2689 struct {
2690 const char *domain;
2691 const char *flow;
2692 const char *condition;
2693 const char *conditional_validity;
2694 int outer_band_n;
2695 } live_range_tests[] = {
2696 /* Contrived example that illustrates that we need to keep
2697 * track of tagged condition dependences and
2698 * tagged conditional validity dependences
2699 * in isl_sched_edge separately.
2700 * In particular, the conditional validity constraints on A
2701 * cannot be satisfied,
2702 * but they can be ignored because there are no corresponding
2703 * condition constraints. However, we do have an additional
2704 * conditional validity constraint that maps to the same
2705 * dependence relation
2706 * as the condition constraint on B. If we did not make a distinction
2707 * between tagged condition and tagged conditional validity
2708 * dependences, then we
2709 * could end up treating this shared dependence as an condition
2710 * constraint on A, forcing a localization of the conditions,
2711 * which is impossible.
2713 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
2714 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
2715 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
2716 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2717 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2718 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
2721 /* TACO 2013 Fig. 7 */
2722 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2723 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2724 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2725 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
2726 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
2727 "0 <= i < n and 0 <= j < n - 1 }",
2728 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
2729 "0 <= i < n and 0 <= j < j' < n;"
2730 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
2731 "0 <= i < i' < n and 0 <= j,j' < n;"
2732 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
2733 "0 <= i,j,j' < n and j < j' }",
2736 /* TACO 2013 Fig. 7, without tags */
2737 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2738 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2739 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2740 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2741 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2742 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
2743 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
2744 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
2747 /* TACO 2013 Fig. 12 */
2748 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
2749 "S3[i,3] : 0 <= i <= 1 }",
2750 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
2751 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
2752 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
2753 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
2754 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2755 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
2756 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2757 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
2758 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
2759 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
2760 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
2765 /* Test schedule construction based on conditional constraints.
2766 * In particular, check the number of members in the outer band
2767 * as an indication of whether tiling is possible or not.
2769 static int test_conditional_schedule_constraints(isl_ctx *ctx)
2771 int i;
2772 isl_union_set *domain;
2773 isl_union_map *condition;
2774 isl_union_map *flow;
2775 isl_union_map *validity;
2776 isl_schedule_constraints *sc;
2777 isl_schedule *schedule;
2778 isl_band_list *list;
2779 isl_band *band;
2780 int n_member;
2782 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
2783 domain = isl_union_set_read_from_str(ctx,
2784 live_range_tests[i].domain);
2785 flow = isl_union_map_read_from_str(ctx,
2786 live_range_tests[i].flow);
2787 condition = isl_union_map_read_from_str(ctx,
2788 live_range_tests[i].condition);
2789 validity = isl_union_map_read_from_str(ctx,
2790 live_range_tests[i].conditional_validity);
2791 sc = isl_schedule_constraints_on_domain(domain);
2792 sc = isl_schedule_constraints_set_validity(sc,
2793 isl_union_map_copy(flow));
2794 sc = isl_schedule_constraints_set_proximity(sc, flow);
2795 sc = isl_schedule_constraints_set_conditional_validity(sc,
2796 condition, validity);
2797 schedule = isl_schedule_constraints_compute_schedule(sc);
2798 list = isl_schedule_get_band_forest(schedule);
2799 band = isl_band_list_get_band(list, 0);
2800 n_member = isl_band_n_member(band);
2801 isl_band_free(band);
2802 isl_band_list_free(list);
2803 isl_schedule_free(schedule);
2805 if (!schedule)
2806 return -1;
2807 if (n_member != live_range_tests[i].outer_band_n)
2808 isl_die(ctx, isl_error_unknown,
2809 "unexpected number of members in outer band",
2810 return -1);
2812 return 0;
2815 int test_schedule(isl_ctx *ctx)
2817 const char *D, *W, *R, *V, *P, *S;
2819 /* Handle resulting schedule with zero bands. */
2820 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2821 return -1;
2823 /* Jacobi */
2824 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2825 W = "{ S1[t,i] -> a[t,i] }";
2826 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2827 "S1[t,i] -> a[t-1,i+1] }";
2828 S = "{ S1[t,i] -> [t,i] }";
2829 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2830 return -1;
2832 /* Fig. 5 of CC2008 */
2833 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2834 "j <= -1 + N }";
2835 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2836 "j >= 2 and j <= -1 + N }";
2837 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2838 "j >= 2 and j <= -1 + N; "
2839 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2840 "j >= 2 and j <= -1 + N }";
2841 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2842 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2843 return -1;
2845 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2846 W = "{ S1[i] -> a[i] }";
2847 R = "{ S2[i] -> a[i+1] }";
2848 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2849 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2850 return -1;
2852 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2853 W = "{ S1[i] -> a[i] }";
2854 R = "{ S2[i] -> a[9-i] }";
2855 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2856 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2857 return -1;
2859 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2860 W = "{ S1[i] -> a[i] }";
2861 R = "[N] -> { S2[i] -> a[N-1-i] }";
2862 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2863 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2864 return -1;
2866 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2867 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2868 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2869 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2870 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2871 return -1;
2873 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2874 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2875 R = "{ S2[i,j] -> a[i-1,j] }";
2876 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2877 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2878 return -1;
2880 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2881 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2882 R = "{ S2[i,j] -> a[i,j-1] }";
2883 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2884 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2885 return -1;
2887 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2888 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2889 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2890 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2891 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2892 "S_0[] -> [0, 0, 0] }";
2893 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2894 return -1;
2895 ctx->opt->schedule_parametric = 0;
2896 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2897 return -1;
2898 ctx->opt->schedule_parametric = 1;
2900 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2901 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2902 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2903 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2904 "S4[i] -> a[i,N] }";
2905 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2906 "S4[i] -> [4,i,0] }";
2907 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2908 return -1;
2910 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2911 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2912 "j <= N }";
2913 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2914 "j <= N; "
2915 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2916 "j <= N }";
2917 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2918 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2919 return -1;
2921 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2922 " S_2[t] : t >= 0 and t <= -1 + N; "
2923 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2924 "i <= -1 + N }";
2925 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2926 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2927 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2928 "i >= 0 and i <= -1 + N }";
2929 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2930 "i >= 0 and i <= -1 + N; "
2931 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2932 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2933 " S_0[t] -> [0, t, 0] }";
2935 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2936 return -1;
2937 ctx->opt->schedule_parametric = 0;
2938 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2939 return -1;
2940 ctx->opt->schedule_parametric = 1;
2942 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2943 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2944 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2945 return -1;
2947 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2948 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2949 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2950 "j >= 0 and j <= -1 + N; "
2951 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2952 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2953 "j >= 0 and j <= -1 + N; "
2954 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2955 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2956 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2957 return -1;
2959 D = "{ S_0[i] : i >= 0 }";
2960 W = "{ S_0[i] -> a[i] : i >= 0 }";
2961 R = "{ S_0[i] -> a[0] : i >= 0 }";
2962 S = "{ S_0[i] -> [0, i, 0] }";
2963 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2964 return -1;
2966 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2967 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2968 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2969 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2970 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2971 return -1;
2973 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2974 "k <= -1 + n and k >= 0 }";
2975 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
2976 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2977 "k <= -1 + n and k >= 0; "
2978 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2979 "k <= -1 + n and k >= 0; "
2980 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2981 "k <= -1 + n and k >= 0 }";
2982 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2983 ctx->opt->schedule_outer_coincidence = 1;
2984 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2985 return -1;
2986 ctx->opt->schedule_outer_coincidence = 0;
2988 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
2989 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
2990 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
2991 "Stmt_for_body24[i0, i1, 1, 0]:"
2992 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
2993 "Stmt_for_body7[i0, i1, i2]:"
2994 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
2995 "i2 <= 7 }";
2997 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
2998 "Stmt_for_body24[1, i1, i2, i3]:"
2999 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3000 "i2 >= 1;"
3001 "Stmt_for_body24[0, i1, i2, i3] -> "
3002 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3003 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3004 "i3 >= 0;"
3005 "Stmt_for_body24[0, i1, i2, i3] ->"
3006 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3007 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3008 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3009 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3010 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3011 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3012 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3013 "i1 <= 6 and i1 >= 0;"
3014 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3015 "Stmt_for_body7[i0, i1, i2] -> "
3016 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3017 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3018 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3019 "Stmt_for_body7[i0, i1, i2] -> "
3020 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3021 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3022 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3023 P = V;
3024 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3025 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3026 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3028 if (test_special_schedule(ctx, D, V, P, S) < 0)
3029 return -1;
3031 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3032 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3033 "j >= 1 and j <= 7;"
3034 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3035 "j >= 1 and j <= 8 }";
3036 P = "{ }";
3037 S = "{ S_0[i, j] -> [i + j, j] }";
3038 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3039 if (test_special_schedule(ctx, D, V, P, S) < 0)
3040 return -1;
3041 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3043 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3044 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3045 "j >= 0 and j <= -1 + i }";
3046 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3047 "i <= -1 + N and j >= 0;"
3048 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3049 "i <= -2 + N }";
3050 P = "{ }";
3051 S = "{ S_0[i, j] -> [i, j] }";
3052 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3053 if (test_special_schedule(ctx, D, V, P, S) < 0)
3054 return -1;
3055 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3057 /* Test both algorithms on a case with only proximity dependences. */
3058 D = "{ S[i,j] : 0 <= i <= 10 }";
3059 V = "{ }";
3060 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3061 S = "{ S[i, j] -> [j, i] }";
3062 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3063 if (test_special_schedule(ctx, D, V, P, S) < 0)
3064 return -1;
3065 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3066 if (test_special_schedule(ctx, D, V, P, S) < 0)
3067 return -1;
3069 D = "{ A[a]; B[] }";
3070 V = "{}";
3071 P = "{ A[a] -> B[] }";
3072 if (test_has_schedule(ctx, D, V, P) < 0)
3073 return -1;
3075 if (test_padded_schedule(ctx) < 0)
3076 return -1;
3078 /* Check that check for progress is not confused by rational
3079 * solution.
3081 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3082 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3083 "i0 <= -2 + N; "
3084 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3085 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3086 P = "{}";
3087 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3088 if (test_has_schedule(ctx, D, V, P) < 0)
3089 return -1;
3090 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3092 if (test_conditional_schedule_constraints(ctx) < 0)
3093 return -1;
3095 return 0;
3098 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3100 isl_union_map *umap;
3101 int test;
3103 umap = isl_union_map_read_from_str(ctx, str);
3104 test = isl_union_map_plain_is_injective(umap);
3105 isl_union_map_free(umap);
3106 if (test < 0)
3107 return -1;
3108 if (test == injective)
3109 return 0;
3110 if (injective)
3111 isl_die(ctx, isl_error_unknown,
3112 "map not detected as injective", return -1);
3113 else
3114 isl_die(ctx, isl_error_unknown,
3115 "map detected as injective", return -1);
3118 int test_injective(isl_ctx *ctx)
3120 const char *str;
3122 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
3123 return -1;
3124 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
3125 return -1;
3126 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
3127 return -1;
3128 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
3129 return -1;
3130 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
3131 return -1;
3132 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
3133 return -1;
3134 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
3135 return -1;
3136 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
3137 return -1;
3139 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
3140 if (test_plain_injective(ctx, str, 1))
3141 return -1;
3142 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
3143 if (test_plain_injective(ctx, str, 0))
3144 return -1;
3146 return 0;
3149 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
3151 isl_aff *aff2;
3152 int equal;
3154 if (!aff)
3155 return -1;
3157 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
3158 equal = isl_aff_plain_is_equal(aff, aff2);
3159 isl_aff_free(aff2);
3161 return equal;
3164 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
3166 int equal;
3168 equal = aff_plain_is_equal(aff, str);
3169 if (equal < 0)
3170 return -1;
3171 if (!equal)
3172 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
3173 "result not as expected", return -1);
3174 return 0;
3177 int test_aff(isl_ctx *ctx)
3179 const char *str;
3180 isl_set *set;
3181 isl_space *space;
3182 isl_local_space *ls;
3183 isl_aff *aff;
3184 int zero, equal;
3186 space = isl_space_set_alloc(ctx, 0, 1);
3187 ls = isl_local_space_from_space(space);
3188 aff = isl_aff_zero_on_domain(ls);
3190 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3191 aff = isl_aff_scale_down_ui(aff, 3);
3192 aff = isl_aff_floor(aff);
3193 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3194 aff = isl_aff_scale_down_ui(aff, 2);
3195 aff = isl_aff_floor(aff);
3196 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3198 str = "{ [10] }";
3199 set = isl_set_read_from_str(ctx, str);
3200 aff = isl_aff_gist(aff, set);
3202 aff = isl_aff_add_constant_si(aff, -16);
3203 zero = isl_aff_plain_is_zero(aff);
3204 isl_aff_free(aff);
3206 if (zero < 0)
3207 return -1;
3208 if (!zero)
3209 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3211 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
3212 aff = isl_aff_scale_down_ui(aff, 64);
3213 aff = isl_aff_floor(aff);
3214 equal = aff_check_plain_equal(aff, "{ [-1] }");
3215 isl_aff_free(aff);
3216 if (equal < 0)
3217 return -1;
3219 return 0;
3222 int test_dim_max(isl_ctx *ctx)
3224 int equal;
3225 const char *str;
3226 isl_set *set1, *set2;
3227 isl_set *set;
3228 isl_map *map;
3229 isl_pw_aff *pwaff;
3231 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
3232 set = isl_set_read_from_str(ctx, str);
3233 pwaff = isl_set_dim_max(set, 0);
3234 set1 = isl_set_from_pw_aff(pwaff);
3235 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
3236 set2 = isl_set_read_from_str(ctx, str);
3237 equal = isl_set_is_equal(set1, set2);
3238 isl_set_free(set1);
3239 isl_set_free(set2);
3240 if (equal < 0)
3241 return -1;
3242 if (!equal)
3243 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3245 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
3246 set = isl_set_read_from_str(ctx, str);
3247 pwaff = isl_set_dim_max(set, 0);
3248 set1 = isl_set_from_pw_aff(pwaff);
3249 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3250 set2 = isl_set_read_from_str(ctx, str);
3251 equal = isl_set_is_equal(set1, set2);
3252 isl_set_free(set1);
3253 isl_set_free(set2);
3254 if (equal < 0)
3255 return -1;
3256 if (!equal)
3257 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3259 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
3260 set = isl_set_read_from_str(ctx, str);
3261 pwaff = isl_set_dim_max(set, 0);
3262 set1 = isl_set_from_pw_aff(pwaff);
3263 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3264 set2 = isl_set_read_from_str(ctx, str);
3265 equal = isl_set_is_equal(set1, set2);
3266 isl_set_free(set1);
3267 isl_set_free(set2);
3268 if (equal < 0)
3269 return -1;
3270 if (!equal)
3271 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3273 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
3274 "0 <= i < N and 0 <= j < M }";
3275 map = isl_map_read_from_str(ctx, str);
3276 set = isl_map_range(map);
3278 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
3279 set1 = isl_set_from_pw_aff(pwaff);
3280 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
3281 set2 = isl_set_read_from_str(ctx, str);
3282 equal = isl_set_is_equal(set1, set2);
3283 isl_set_free(set1);
3284 isl_set_free(set2);
3286 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3287 set1 = isl_set_from_pw_aff(pwaff);
3288 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3289 set2 = isl_set_read_from_str(ctx, str);
3290 if (equal >= 0 && equal)
3291 equal = isl_set_is_equal(set1, set2);
3292 isl_set_free(set1);
3293 isl_set_free(set2);
3295 isl_set_free(set);
3297 if (equal < 0)
3298 return -1;
3299 if (!equal)
3300 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3302 /* Check that solutions are properly merged. */
3303 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3304 "c <= -1 + n - 4a - 2b and c >= -2b and "
3305 "4a >= -4 + n and c >= 0 }";
3306 set = isl_set_read_from_str(ctx, str);
3307 pwaff = isl_set_dim_min(set, 2);
3308 set1 = isl_set_from_pw_aff(pwaff);
3309 str = "[n] -> { [(0)] : n >= 1 }";
3310 set2 = isl_set_read_from_str(ctx, str);
3311 equal = isl_set_is_equal(set1, set2);
3312 isl_set_free(set1);
3313 isl_set_free(set2);
3315 if (equal < 0)
3316 return -1;
3317 if (!equal)
3318 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3320 return 0;
3323 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
3325 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
3326 const char *str)
3328 isl_ctx *ctx;
3329 isl_pw_multi_aff *pma2;
3330 int equal;
3332 if (!pma)
3333 return -1;
3335 ctx = isl_pw_multi_aff_get_ctx(pma);
3336 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3337 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
3338 isl_pw_multi_aff_free(pma2);
3340 return equal;
3343 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
3344 * represented by "str".
3346 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
3347 const char *str)
3349 int equal;
3351 equal = pw_multi_aff_plain_is_equal(pma, str);
3352 if (equal < 0)
3353 return -1;
3354 if (!equal)
3355 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
3356 "result not as expected", return -1);
3357 return 0;
3360 /* Basic test for isl_pw_multi_aff_product.
3362 * Check that multiple pieces are properly handled.
3364 static int test_product_pma(isl_ctx *ctx)
3366 int equal;
3367 const char *str;
3368 isl_pw_multi_aff *pma1, *pma2;
3370 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
3371 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3372 str = "{ C[] -> D[] }";
3373 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3374 pma1 = isl_pw_multi_aff_product(pma1, pma2);
3375 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
3376 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
3377 equal = pw_multi_aff_check_plain_equal(pma1, str);
3378 isl_pw_multi_aff_free(pma1);
3379 if (equal < 0)
3380 return -1;
3382 return 0;
3385 int test_product(isl_ctx *ctx)
3387 const char *str;
3388 isl_set *set;
3389 isl_union_set *uset1, *uset2;
3390 int ok;
3392 str = "{ A[i] }";
3393 set = isl_set_read_from_str(ctx, str);
3394 set = isl_set_product(set, isl_set_copy(set));
3395 ok = isl_set_is_wrapping(set);
3396 isl_set_free(set);
3397 if (ok < 0)
3398 return -1;
3399 if (!ok)
3400 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3402 str = "{ [] }";
3403 uset1 = isl_union_set_read_from_str(ctx, str);
3404 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3405 str = "{ [[] -> []] }";
3406 uset2 = isl_union_set_read_from_str(ctx, str);
3407 ok = isl_union_set_is_equal(uset1, uset2);
3408 isl_union_set_free(uset1);
3409 isl_union_set_free(uset2);
3410 if (ok < 0)
3411 return -1;
3412 if (!ok)
3413 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3415 if (test_product_pma(ctx) < 0)
3416 return -1;
3418 return 0;
3421 /* Check that two sets are not considered disjoint just because
3422 * they have a different set of (named) parameters.
3424 static int test_disjoint(isl_ctx *ctx)
3426 const char *str;
3427 isl_set *set, *set2;
3428 int disjoint;
3430 str = "[n] -> { [[]->[]] }";
3431 set = isl_set_read_from_str(ctx, str);
3432 str = "{ [[]->[]] }";
3433 set2 = isl_set_read_from_str(ctx, str);
3434 disjoint = isl_set_is_disjoint(set, set2);
3435 isl_set_free(set);
3436 isl_set_free(set2);
3437 if (disjoint < 0)
3438 return -1;
3439 if (disjoint)
3440 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3442 return 0;
3445 int test_equal(isl_ctx *ctx)
3447 const char *str;
3448 isl_set *set, *set2;
3449 int equal;
3451 str = "{ S_6[i] }";
3452 set = isl_set_read_from_str(ctx, str);
3453 str = "{ S_7[i] }";
3454 set2 = isl_set_read_from_str(ctx, str);
3455 equal = isl_set_is_equal(set, set2);
3456 isl_set_free(set);
3457 isl_set_free(set2);
3458 if (equal < 0)
3459 return -1;
3460 if (equal)
3461 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3463 return 0;
3466 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3467 enum isl_dim_type type, unsigned pos, int fixed)
3469 int test;
3471 test = isl_map_plain_is_fixed(map, type, pos, NULL);
3472 isl_map_free(map);
3473 if (test < 0)
3474 return -1;
3475 if (test == fixed)
3476 return 0;
3477 if (fixed)
3478 isl_die(ctx, isl_error_unknown,
3479 "map not detected as fixed", return -1);
3480 else
3481 isl_die(ctx, isl_error_unknown,
3482 "map detected as fixed", return -1);
3485 int test_fixed(isl_ctx *ctx)
3487 const char *str;
3488 isl_map *map;
3490 str = "{ [i] -> [i] }";
3491 map = isl_map_read_from_str(ctx, str);
3492 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3493 return -1;
3494 str = "{ [i] -> [1] }";
3495 map = isl_map_read_from_str(ctx, str);
3496 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3497 return -1;
3498 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3499 map = isl_map_read_from_str(ctx, str);
3500 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3501 return -1;
3502 map = isl_map_read_from_str(ctx, str);
3503 map = isl_map_neg(map);
3504 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3505 return -1;
3507 return 0;
3510 int test_vertices(isl_ctx *ctx)
3512 const char *str;
3513 isl_basic_set *bset;
3514 isl_vertices *vertices;
3516 str = "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
3517 bset = isl_basic_set_read_from_str(ctx, str);
3518 vertices = isl_basic_set_compute_vertices(bset);
3519 isl_basic_set_free(bset);
3520 isl_vertices_free(vertices);
3521 if (!vertices)
3522 return -1;
3524 str = "{ A[t, i] : t = 14 and i = 1 }";
3525 bset = isl_basic_set_read_from_str(ctx, str);
3526 vertices = isl_basic_set_compute_vertices(bset);
3527 isl_basic_set_free(bset);
3528 isl_vertices_free(vertices);
3529 if (!vertices)
3530 return -1;
3532 return 0;
3535 int test_union_pw(isl_ctx *ctx)
3537 int equal;
3538 const char *str;
3539 isl_union_set *uset;
3540 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
3542 str = "{ [x] -> x^2 }";
3543 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
3544 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
3545 uset = isl_union_pw_qpolynomial_domain(upwqp1);
3546 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
3547 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
3548 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
3549 isl_union_pw_qpolynomial_free(upwqp1);
3550 isl_union_pw_qpolynomial_free(upwqp2);
3551 if (equal < 0)
3552 return -1;
3553 if (!equal)
3554 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3556 return 0;
3559 int test_output(isl_ctx *ctx)
3561 char *s;
3562 const char *str;
3563 isl_pw_aff *pa;
3564 isl_printer *p;
3565 int equal;
3567 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
3568 pa = isl_pw_aff_read_from_str(ctx, str);
3570 p = isl_printer_to_str(ctx);
3571 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
3572 p = isl_printer_print_pw_aff(p, pa);
3573 s = isl_printer_get_str(p);
3574 isl_printer_free(p);
3575 isl_pw_aff_free(pa);
3576 if (!s)
3577 equal = -1;
3578 else
3579 equal = !strcmp(s, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
3580 free(s);
3581 if (equal < 0)
3582 return -1;
3583 if (!equal)
3584 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3586 return 0;
3589 int test_sample(isl_ctx *ctx)
3591 const char *str;
3592 isl_basic_set *bset1, *bset2;
3593 int empty, subset;
3595 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3596 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3597 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3598 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3599 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3600 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3601 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3602 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3603 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3604 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3605 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3606 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3607 "d - 1073741821e and "
3608 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3609 "3j >= 1 - a + b + 2e and "
3610 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3611 "3i <= 4 - a + 4b - e and "
3612 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3613 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3614 "c <= -1 + a and 3i >= -2 - a + 3e and "
3615 "1073741822e <= 1073741823 - a + 1073741822b + c and "
3616 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3617 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3618 "1073741823e >= 1 + 1073741823b - d and "
3619 "3i >= 1073741823b + c - 1073741823e - f and "
3620 "3i >= 1 + 2b + e + 3g }";
3621 bset1 = isl_basic_set_read_from_str(ctx, str);
3622 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3623 empty = isl_basic_set_is_empty(bset2);
3624 subset = isl_basic_set_is_subset(bset2, bset1);
3625 isl_basic_set_free(bset1);
3626 isl_basic_set_free(bset2);
3627 if (empty < 0 || subset < 0)
3628 return -1;
3629 if (empty)
3630 isl_die(ctx, isl_error_unknown, "point not found", return -1);
3631 if (!subset)
3632 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
3634 return 0;
3637 int test_fixed_power(isl_ctx *ctx)
3639 const char *str;
3640 isl_map *map;
3641 isl_int exp;
3642 int equal;
3644 isl_int_init(exp);
3645 str = "{ [i] -> [i + 1] }";
3646 map = isl_map_read_from_str(ctx, str);
3647 isl_int_set_si(exp, 23);
3648 map = isl_map_fixed_power(map, exp);
3649 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
3650 isl_int_clear(exp);
3651 isl_map_free(map);
3652 if (equal < 0)
3653 return -1;
3655 return 0;
3658 int test_slice(isl_ctx *ctx)
3660 const char *str;
3661 isl_map *map;
3662 int equal;
3664 str = "{ [i] -> [j] }";
3665 map = isl_map_read_from_str(ctx, str);
3666 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
3667 equal = map_check_equal(map, "{ [i] -> [i] }");
3668 isl_map_free(map);
3669 if (equal < 0)
3670 return -1;
3672 str = "{ [i] -> [j] }";
3673 map = isl_map_read_from_str(ctx, str);
3674 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
3675 equal = map_check_equal(map, "{ [i] -> [j] }");
3676 isl_map_free(map);
3677 if (equal < 0)
3678 return -1;
3680 str = "{ [i] -> [j] }";
3681 map = isl_map_read_from_str(ctx, str);
3682 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
3683 equal = map_check_equal(map, "{ [i] -> [-i] }");
3684 isl_map_free(map);
3685 if (equal < 0)
3686 return -1;
3688 str = "{ [i] -> [j] }";
3689 map = isl_map_read_from_str(ctx, str);
3690 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
3691 equal = map_check_equal(map, "{ [0] -> [j] }");
3692 isl_map_free(map);
3693 if (equal < 0)
3694 return -1;
3696 str = "{ [i] -> [j] }";
3697 map = isl_map_read_from_str(ctx, str);
3698 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3699 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
3700 isl_map_free(map);
3701 if (equal < 0)
3702 return -1;
3704 str = "{ [i] -> [j] }";
3705 map = isl_map_read_from_str(ctx, str);
3706 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
3707 equal = map_check_equal(map, "{ [i] -> [j] : false }");
3708 isl_map_free(map);
3709 if (equal < 0)
3710 return -1;
3712 return 0;
3715 int test_eliminate(isl_ctx *ctx)
3717 const char *str;
3718 isl_map *map;
3719 int equal;
3721 str = "{ [i] -> [j] : i = 2j }";
3722 map = isl_map_read_from_str(ctx, str);
3723 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
3724 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
3725 isl_map_free(map);
3726 if (equal < 0)
3727 return -1;
3729 return 0;
3732 /* Check that isl_set_dim_residue_class detects that the values of j
3733 * in the set below are all odd and that it does not detect any spurious
3734 * strides.
3736 static int test_residue_class(isl_ctx *ctx)
3738 const char *str;
3739 isl_set *set;
3740 isl_int m, r;
3741 int res;
3743 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3744 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3745 set = isl_set_read_from_str(ctx, str);
3746 isl_int_init(m);
3747 isl_int_init(r);
3748 res = isl_set_dim_residue_class(set, 1, &m, &r);
3749 if (res >= 0 &&
3750 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
3751 isl_die(ctx, isl_error_unknown, "incorrect residue class",
3752 res = -1);
3753 isl_int_clear(r);
3754 isl_int_clear(m);
3755 isl_set_free(set);
3757 return res;
3760 int test_align_parameters(isl_ctx *ctx)
3762 const char *str;
3763 isl_space *space;
3764 isl_multi_aff *ma1, *ma2;
3765 int equal;
3767 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
3768 ma1 = isl_multi_aff_read_from_str(ctx, str);
3770 space = isl_space_params_alloc(ctx, 1);
3771 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
3772 ma1 = isl_multi_aff_align_params(ma1, space);
3774 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
3775 ma2 = isl_multi_aff_read_from_str(ctx, str);
3777 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3779 isl_multi_aff_free(ma1);
3780 isl_multi_aff_free(ma2);
3782 if (equal < 0)
3783 return -1;
3784 if (!equal)
3785 isl_die(ctx, isl_error_unknown,
3786 "result not as expected", return -1);
3788 return 0;
3791 static int test_list(isl_ctx *ctx)
3793 isl_id *a, *b, *c, *d, *id;
3794 isl_id_list *list;
3795 int ok;
3797 a = isl_id_alloc(ctx, "a", NULL);
3798 b = isl_id_alloc(ctx, "b", NULL);
3799 c = isl_id_alloc(ctx, "c", NULL);
3800 d = isl_id_alloc(ctx, "d", NULL);
3802 list = isl_id_list_alloc(ctx, 4);
3803 list = isl_id_list_add(list, a);
3804 list = isl_id_list_add(list, b);
3805 list = isl_id_list_add(list, c);
3806 list = isl_id_list_add(list, d);
3807 list = isl_id_list_drop(list, 1, 1);
3809 if (isl_id_list_n_id(list) != 3) {
3810 isl_id_list_free(list);
3811 isl_die(ctx, isl_error_unknown,
3812 "unexpected number of elements in list", return -1);
3815 id = isl_id_list_get_id(list, 0);
3816 ok = id == a;
3817 isl_id_free(id);
3818 id = isl_id_list_get_id(list, 1);
3819 ok = ok && id == c;
3820 isl_id_free(id);
3821 id = isl_id_list_get_id(list, 2);
3822 ok = ok && id == d;
3823 isl_id_free(id);
3825 isl_id_list_free(list);
3827 if (!ok)
3828 isl_die(ctx, isl_error_unknown,
3829 "unexpected elements in list", return -1);
3831 return 0;
3834 const char *set_conversion_tests[] = {
3835 "[N] -> { [i] : N - 1 <= 2 i <= N }",
3836 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
3837 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3838 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3841 /* Check that converting from isl_set to isl_pw_multi_aff and back
3842 * to isl_set produces the original isl_set.
3844 static int test_set_conversion(isl_ctx *ctx)
3846 int i;
3847 const char *str;
3848 isl_set *set1, *set2;
3849 isl_pw_multi_aff *pma;
3850 int equal;
3852 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
3853 str = set_conversion_tests[i];
3854 set1 = isl_set_read_from_str(ctx, str);
3855 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
3856 set2 = isl_set_from_pw_multi_aff(pma);
3857 equal = isl_set_is_equal(set1, set2);
3858 isl_set_free(set1);
3859 isl_set_free(set2);
3861 if (equal < 0)
3862 return -1;
3863 if (!equal)
3864 isl_die(ctx, isl_error_unknown, "bad conversion",
3865 return -1);
3868 return 0;
3871 /* Check that converting from isl_map to isl_pw_multi_aff and back
3872 * to isl_map produces the original isl_map.
3874 static int test_map_conversion(isl_ctx *ctx)
3876 const char *str;
3877 isl_map *map1, *map2;
3878 isl_pw_multi_aff *pma;
3879 int equal;
3881 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
3882 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
3883 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
3884 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
3885 "9e <= -2 - 2a) }";
3886 map1 = isl_map_read_from_str(ctx, str);
3887 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
3888 map2 = isl_map_from_pw_multi_aff(pma);
3889 equal = isl_map_is_equal(map1, map2);
3890 isl_map_free(map1);
3891 isl_map_free(map2);
3893 if (equal < 0)
3894 return -1;
3895 if (!equal)
3896 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
3898 return 0;
3901 static int test_conversion(isl_ctx *ctx)
3903 if (test_set_conversion(ctx) < 0)
3904 return -1;
3905 if (test_map_conversion(ctx) < 0)
3906 return -1;
3907 return 0;
3910 /* Check that isl_basic_map_curry does not modify input.
3912 static int test_curry(isl_ctx *ctx)
3914 const char *str;
3915 isl_basic_map *bmap1, *bmap2;
3916 int equal;
3918 str = "{ [A[] -> B[]] -> C[] }";
3919 bmap1 = isl_basic_map_read_from_str(ctx, str);
3920 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
3921 equal = isl_basic_map_is_equal(bmap1, bmap2);
3922 isl_basic_map_free(bmap1);
3923 isl_basic_map_free(bmap2);
3925 if (equal < 0)
3926 return -1;
3927 if (equal)
3928 isl_die(ctx, isl_error_unknown,
3929 "curried map should not be equal to original",
3930 return -1);
3932 return 0;
3935 struct {
3936 const char *set;
3937 const char *ma;
3938 const char *res;
3939 } preimage_tests[] = {
3940 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
3941 "{ A[j,i] -> B[i,j] }",
3942 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
3943 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3944 "{ A[a,b] -> B[a/2,b/6] }",
3945 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
3946 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3947 "{ A[a,b] -> B[a/2,b/6] }",
3948 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
3949 "exists i,j : a = 2 i and b = 6 j }" },
3950 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
3951 "[n] -> { : 0 <= n <= 100 }" },
3952 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3953 "{ A[a] -> B[2a] }",
3954 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
3955 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3956 "{ A[a] -> B[([a/2])] }",
3957 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
3958 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
3959 "{ A[a] -> B[a,a,a/3] }",
3960 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
3961 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
3962 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
3965 static int test_preimage_basic_set(isl_ctx *ctx)
3967 int i;
3968 isl_basic_set *bset1, *bset2;
3969 isl_multi_aff *ma;
3970 int equal;
3972 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
3973 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
3974 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
3975 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
3976 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
3977 equal = isl_basic_set_is_equal(bset1, bset2);
3978 isl_basic_set_free(bset1);
3979 isl_basic_set_free(bset2);
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 struct {
3991 const char *map;
3992 const char *ma;
3993 const char *res;
3994 } preimage_domain_tests[] = {
3995 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
3996 "{ A[j,i] -> B[i,j] }",
3997 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
3998 { "{ B[i] -> C[i]; D[i] -> E[i] }",
3999 "{ A[i] -> B[i + 1] }",
4000 "{ A[i] -> C[i + 1] }" },
4001 { "{ B[i] -> C[i]; B[i] -> E[i] }",
4002 "{ A[i] -> B[i + 1] }",
4003 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
4004 { "{ B[i] -> C[([i/2])] }",
4005 "{ A[i] -> B[2i] }",
4006 "{ A[i] -> C[i] }" },
4007 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
4008 "{ A[i] -> B[([i/5]), ([i/7])] }",
4009 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
4010 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
4011 "[N] -> { A[] -> B[([N/5])] }",
4012 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
4013 { "{ B[i] -> C[i] : exists a : i = 5 a }",
4014 "{ A[i] -> B[2i] }",
4015 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
4016 { "{ B[i] -> C[i] : exists a : i = 2 a; "
4017 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
4018 "{ A[i] -> B[2i] }",
4019 "{ A[i] -> C[2i] }" },
4022 static int test_preimage_union_map(isl_ctx *ctx)
4024 int i;
4025 isl_union_map *umap1, *umap2;
4026 isl_multi_aff *ma;
4027 int equal;
4029 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
4030 umap1 = isl_union_map_read_from_str(ctx,
4031 preimage_domain_tests[i].map);
4032 ma = isl_multi_aff_read_from_str(ctx,
4033 preimage_domain_tests[i].ma);
4034 umap2 = isl_union_map_read_from_str(ctx,
4035 preimage_domain_tests[i].res);
4036 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
4037 equal = isl_union_map_is_equal(umap1, umap2);
4038 isl_union_map_free(umap1);
4039 isl_union_map_free(umap2);
4040 if (equal < 0)
4041 return -1;
4042 if (!equal)
4043 isl_die(ctx, isl_error_unknown, "bad preimage",
4044 return -1);
4047 return 0;
4050 static int test_preimage(isl_ctx *ctx)
4052 if (test_preimage_basic_set(ctx) < 0)
4053 return -1;
4054 if (test_preimage_union_map(ctx) < 0)
4055 return -1;
4057 return 0;
4060 struct {
4061 const char *ma1;
4062 const char *ma;
4063 const char *res;
4064 } pullback_tests[] = {
4065 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
4066 "{ A[a,b] -> C[b + 2a] }" },
4067 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
4068 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
4069 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
4070 "{ A[a] -> C[(a)/6] }" },
4071 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
4072 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
4073 "{ A[a] -> C[(2a)/3] }" },
4074 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
4075 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
4076 "{ A[i,j] -> C[i + j, i + j] }"},
4077 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
4078 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
4079 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
4082 static int test_pullback(isl_ctx *ctx)
4084 int i;
4085 isl_multi_aff *ma1, *ma2;
4086 isl_multi_aff *ma;
4087 int equal;
4089 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
4090 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
4091 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
4092 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
4093 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
4094 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4095 isl_multi_aff_free(ma1);
4096 isl_multi_aff_free(ma2);
4097 if (equal < 0)
4098 return -1;
4099 if (!equal)
4100 isl_die(ctx, isl_error_unknown, "bad pullback",
4101 return -1);
4104 return 0;
4107 /* Check that negation is printed correctly.
4109 static int test_ast(isl_ctx *ctx)
4111 isl_ast_expr *expr, *expr1, *expr2, *expr3;
4112 char *str;
4113 int ok;
4115 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4116 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4117 expr = isl_ast_expr_add(expr1, expr2);
4118 expr = isl_ast_expr_neg(expr);
4119 str = isl_ast_expr_to_str(expr);
4120 ok = str ? !strcmp(str, "-(A + B)") : -1;
4121 free(str);
4122 isl_ast_expr_free(expr);
4124 if (ok < 0)
4125 return -1;
4126 if (!ok)
4127 isl_die(ctx, isl_error_unknown,
4128 "isl_ast_expr printed incorrectly", return -1);
4130 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4131 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4132 expr = isl_ast_expr_add(expr1, expr2);
4133 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
4134 expr = isl_ast_expr_sub(expr3, expr);
4135 str = isl_ast_expr_to_str(expr);
4136 ok = str ? !strcmp(str, "C - (A + B)") : -1;
4137 free(str);
4138 isl_ast_expr_free(expr);
4140 if (ok < 0)
4141 return -1;
4142 if (!ok)
4143 isl_die(ctx, isl_error_unknown,
4144 "isl_ast_expr printed incorrectly", return -1);
4146 return 0;
4149 /* Internal data structure for before_for and after_for callbacks.
4151 * depth is the current depth
4152 * before is the number of times before_for has been called
4153 * after is the number of times after_for has been called
4155 struct isl_test_codegen_data {
4156 int depth;
4157 int before;
4158 int after;
4161 /* This function is called before each for loop in the AST generated
4162 * from test_ast_gen1.
4164 * Increment the number of calls and the depth.
4165 * Check that the space returned by isl_ast_build_get_schedule_space
4166 * matches the target space of the schedule returned by
4167 * isl_ast_build_get_schedule.
4168 * Return an isl_id that is checked by the corresponding call
4169 * to after_for.
4171 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
4172 void *user)
4174 struct isl_test_codegen_data *data = user;
4175 isl_ctx *ctx;
4176 isl_space *space;
4177 isl_union_map *schedule;
4178 isl_union_set *uset;
4179 isl_set *set;
4180 int empty;
4181 char name[] = "d0";
4183 ctx = isl_ast_build_get_ctx(build);
4185 if (data->before >= 3)
4186 isl_die(ctx, isl_error_unknown,
4187 "unexpected number of for nodes", return NULL);
4188 if (data->depth >= 2)
4189 isl_die(ctx, isl_error_unknown,
4190 "unexpected depth", return NULL);
4192 snprintf(name, sizeof(name), "d%d", data->depth);
4193 data->before++;
4194 data->depth++;
4196 schedule = isl_ast_build_get_schedule(build);
4197 uset = isl_union_map_range(schedule);
4198 if (!uset)
4199 return NULL;
4200 if (isl_union_set_n_set(uset) != 1) {
4201 isl_union_set_free(uset);
4202 isl_die(ctx, isl_error_unknown,
4203 "expecting single range space", return NULL);
4206 space = isl_ast_build_get_schedule_space(build);
4207 set = isl_union_set_extract_set(uset, space);
4208 isl_union_set_free(uset);
4209 empty = isl_set_is_empty(set);
4210 isl_set_free(set);
4212 if (empty < 0)
4213 return NULL;
4214 if (empty)
4215 isl_die(ctx, isl_error_unknown,
4216 "spaces don't match", return NULL);
4218 return isl_id_alloc(ctx, name, NULL);
4221 /* This function is called after each for loop in the AST generated
4222 * from test_ast_gen1.
4224 * Increment the number of calls and decrement the depth.
4225 * Check that the annotation attached to the node matches
4226 * the isl_id returned by the corresponding call to before_for.
4228 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
4229 __isl_keep isl_ast_build *build, void *user)
4231 struct isl_test_codegen_data *data = user;
4232 isl_id *id;
4233 const char *name;
4234 int valid;
4236 data->after++;
4237 data->depth--;
4239 if (data->after > data->before)
4240 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4241 "mismatch in number of for nodes",
4242 return isl_ast_node_free(node));
4244 id = isl_ast_node_get_annotation(node);
4245 if (!id)
4246 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4247 "missing annotation", return isl_ast_node_free(node));
4249 name = isl_id_get_name(id);
4250 valid = name && atoi(name + 1) == data->depth;
4251 isl_id_free(id);
4253 if (!valid)
4254 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4255 "wrong annotation", return isl_ast_node_free(node));
4257 return node;
4260 /* Check that the before_each_for and after_each_for callbacks
4261 * are called for each for loop in the generated code,
4262 * that they are called in the right order and that the isl_id
4263 * returned from the before_each_for callback is attached to
4264 * the isl_ast_node passed to the corresponding after_each_for call.
4266 static int test_ast_gen1(isl_ctx *ctx)
4268 const char *str;
4269 isl_set *set;
4270 isl_union_map *schedule;
4271 isl_ast_build *build;
4272 isl_ast_node *tree;
4273 struct isl_test_codegen_data data;
4275 str = "[N] -> { : N >= 10 }";
4276 set = isl_set_read_from_str(ctx, str);
4277 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
4278 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
4279 schedule = isl_union_map_read_from_str(ctx, str);
4281 data.before = 0;
4282 data.after = 0;
4283 data.depth = 0;
4284 build = isl_ast_build_from_context(set);
4285 build = isl_ast_build_set_before_each_for(build,
4286 &before_for, &data);
4287 build = isl_ast_build_set_after_each_for(build,
4288 &after_for, &data);
4289 tree = isl_ast_build_ast_from_schedule(build, schedule);
4290 isl_ast_build_free(build);
4291 if (!tree)
4292 return -1;
4294 isl_ast_node_free(tree);
4296 if (data.before != 3 || data.after != 3)
4297 isl_die(ctx, isl_error_unknown,
4298 "unexpected number of for nodes", return -1);
4300 return 0;
4303 /* Check that the AST generator handles domains that are integrally disjoint
4304 * but not ratinoally disjoint.
4306 static int test_ast_gen2(isl_ctx *ctx)
4308 const char *str;
4309 isl_set *set;
4310 isl_union_map *schedule;
4311 isl_union_map *options;
4312 isl_ast_build *build;
4313 isl_ast_node *tree;
4315 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
4316 schedule = isl_union_map_read_from_str(ctx, str);
4317 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4318 build = isl_ast_build_from_context(set);
4320 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
4321 options = isl_union_map_read_from_str(ctx, str);
4322 build = isl_ast_build_set_options(build, options);
4323 tree = isl_ast_build_ast_from_schedule(build, schedule);
4324 isl_ast_build_free(build);
4325 if (!tree)
4326 return -1;
4327 isl_ast_node_free(tree);
4329 return 0;
4332 /* Increment *user on each call.
4334 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
4335 __isl_keep isl_ast_build *build, void *user)
4337 int *n = user;
4339 (*n)++;
4341 return node;
4344 /* Test that unrolling tries to minimize the number of instances.
4345 * In particular, for the schedule given below, make sure it generates
4346 * 3 nodes (rather than 101).
4348 static int test_ast_gen3(isl_ctx *ctx)
4350 const char *str;
4351 isl_set *set;
4352 isl_union_map *schedule;
4353 isl_union_map *options;
4354 isl_ast_build *build;
4355 isl_ast_node *tree;
4356 int n_domain = 0;
4358 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
4359 schedule = isl_union_map_read_from_str(ctx, str);
4360 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4362 str = "{ [i] -> unroll[0] }";
4363 options = isl_union_map_read_from_str(ctx, str);
4365 build = isl_ast_build_from_context(set);
4366 build = isl_ast_build_set_options(build, options);
4367 build = isl_ast_build_set_at_each_domain(build,
4368 &count_domains, &n_domain);
4369 tree = isl_ast_build_ast_from_schedule(build, schedule);
4370 isl_ast_build_free(build);
4371 if (!tree)
4372 return -1;
4374 isl_ast_node_free(tree);
4376 if (n_domain != 3)
4377 isl_die(ctx, isl_error_unknown,
4378 "unexpected number of for nodes", return -1);
4380 return 0;
4383 /* Check that if the ast_build_exploit_nested_bounds options is set,
4384 * we do not get an outer if node in the generated AST,
4385 * while we do get such an outer if node if the options is not set.
4387 static int test_ast_gen4(isl_ctx *ctx)
4389 const char *str;
4390 isl_set *set;
4391 isl_union_map *schedule;
4392 isl_ast_build *build;
4393 isl_ast_node *tree;
4394 enum isl_ast_node_type type;
4395 int enb;
4397 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4398 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4400 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4402 schedule = isl_union_map_read_from_str(ctx, str);
4403 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4404 build = isl_ast_build_from_context(set);
4405 tree = isl_ast_build_ast_from_schedule(build, schedule);
4406 isl_ast_build_free(build);
4407 if (!tree)
4408 return -1;
4410 type = isl_ast_node_get_type(tree);
4411 isl_ast_node_free(tree);
4413 if (type == isl_ast_node_if)
4414 isl_die(ctx, isl_error_unknown,
4415 "not expecting if node", return -1);
4417 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
4419 schedule = isl_union_map_read_from_str(ctx, str);
4420 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4421 build = isl_ast_build_from_context(set);
4422 tree = isl_ast_build_ast_from_schedule(build, schedule);
4423 isl_ast_build_free(build);
4424 if (!tree)
4425 return -1;
4427 type = isl_ast_node_get_type(tree);
4428 isl_ast_node_free(tree);
4430 if (type != isl_ast_node_if)
4431 isl_die(ctx, isl_error_unknown,
4432 "expecting if node", return -1);
4434 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
4436 return 0;
4439 /* This function is called for each leaf in the AST generated
4440 * from test_ast_gen5.
4442 * We finalize the AST generation by extending the outer schedule
4443 * with a zero-dimensional schedule. If this results in any for loops,
4444 * then this means that we did not pass along enough information
4445 * about the outer schedule to the inner AST generation.
4447 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
4448 void *user)
4450 isl_union_map *schedule, *extra;
4451 isl_ast_node *tree;
4453 schedule = isl_ast_build_get_schedule(build);
4454 extra = isl_union_map_copy(schedule);
4455 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
4456 schedule = isl_union_map_range_product(schedule, extra);
4457 tree = isl_ast_build_ast_from_schedule(build, schedule);
4458 isl_ast_build_free(build);
4460 if (!tree)
4461 return NULL;
4463 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
4464 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
4465 "code should not contain any for loop",
4466 return isl_ast_node_free(tree));
4468 return tree;
4471 /* Check that we do not lose any information when going back and
4472 * forth between internal and external schedule.
4474 * In particular, we create an AST where we unroll the only
4475 * non-constant dimension in the schedule. We therefore do
4476 * not expect any for loops in the AST. However, older versions
4477 * of isl would not pass along enough information about the outer
4478 * schedule when performing an inner code generation from a create_leaf
4479 * callback, resulting in the inner code generation producing a for loop.
4481 static int test_ast_gen5(isl_ctx *ctx)
4483 const char *str;
4484 isl_set *set;
4485 isl_union_map *schedule, *options;
4486 isl_ast_build *build;
4487 isl_ast_node *tree;
4489 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
4490 schedule = isl_union_map_read_from_str(ctx, str);
4492 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
4493 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
4494 options = isl_union_map_read_from_str(ctx, str);
4496 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4497 build = isl_ast_build_from_context(set);
4498 build = isl_ast_build_set_options(build, options);
4499 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
4500 tree = isl_ast_build_ast_from_schedule(build, schedule);
4501 isl_ast_build_free(build);
4502 isl_ast_node_free(tree);
4503 if (!tree)
4504 return -1;
4506 return 0;
4509 static int test_ast_gen(isl_ctx *ctx)
4511 if (test_ast_gen1(ctx) < 0)
4512 return -1;
4513 if (test_ast_gen2(ctx) < 0)
4514 return -1;
4515 if (test_ast_gen3(ctx) < 0)
4516 return -1;
4517 if (test_ast_gen4(ctx) < 0)
4518 return -1;
4519 if (test_ast_gen5(ctx) < 0)
4520 return -1;
4521 return 0;
4524 /* Check if dropping output dimensions from an isl_pw_multi_aff
4525 * works properly.
4527 static int test_pw_multi_aff(isl_ctx *ctx)
4529 const char *str;
4530 isl_pw_multi_aff *pma1, *pma2;
4531 int equal;
4533 str = "{ [i,j] -> [i+j, 4i-j] }";
4534 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4535 str = "{ [i,j] -> [4i-j] }";
4536 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4538 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
4540 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
4542 isl_pw_multi_aff_free(pma1);
4543 isl_pw_multi_aff_free(pma2);
4544 if (equal < 0)
4545 return -1;
4546 if (!equal)
4547 isl_die(ctx, isl_error_unknown,
4548 "expressions not equal", return -1);
4550 return 0;
4553 /* Check that we can properly parse multi piecewise affine expressions
4554 * where the piecewise affine expressions have different domains.
4556 static int test_multi_pw_aff(isl_ctx *ctx)
4558 const char *str;
4559 isl_set *dom, *dom2;
4560 isl_multi_pw_aff *mpa1, *mpa2;
4561 isl_pw_aff *pa;
4562 int equal;
4563 int equal_domain;
4565 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
4566 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
4567 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
4568 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
4569 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
4570 str = "{ [i] -> [(i : i > 0), 2i] }";
4571 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
4573 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
4575 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
4576 dom = isl_pw_aff_domain(pa);
4577 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
4578 dom2 = isl_pw_aff_domain(pa);
4579 equal_domain = isl_set_is_equal(dom, dom2);
4581 isl_set_free(dom);
4582 isl_set_free(dom2);
4583 isl_multi_pw_aff_free(mpa1);
4584 isl_multi_pw_aff_free(mpa2);
4586 if (equal < 0)
4587 return -1;
4588 if (!equal)
4589 isl_die(ctx, isl_error_unknown,
4590 "expressions not equal", return -1);
4592 if (equal_domain < 0)
4593 return -1;
4594 if (equal_domain)
4595 isl_die(ctx, isl_error_unknown,
4596 "domains unexpectedly equal", return -1);
4598 return 0;
4601 /* This is a regression test for a bug where isl_basic_map_simplify
4602 * would end up in an infinite loop. In particular, we construct
4603 * an empty basic set that is not obviously empty.
4604 * isl_basic_set_is_empty marks the basic set as empty.
4605 * After projecting out i3, the variable can be dropped completely,
4606 * but isl_basic_map_simplify refrains from doing so if the basic set
4607 * is empty and would end up in an infinite loop if it didn't test
4608 * explicitly for empty basic maps in the outer loop.
4610 static int test_simplify(isl_ctx *ctx)
4612 const char *str;
4613 isl_basic_set *bset;
4614 int empty;
4616 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
4617 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
4618 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
4619 "i3 >= i2 }";
4620 bset = isl_basic_set_read_from_str(ctx, str);
4621 empty = isl_basic_set_is_empty(bset);
4622 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
4623 isl_basic_set_free(bset);
4624 if (!bset)
4625 return -1;
4626 if (!empty)
4627 isl_die(ctx, isl_error_unknown,
4628 "basic set should be empty", return -1);
4630 return 0;
4633 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
4634 * with gbr context would fail to disable the use of the shifted tableau
4635 * when transferring equalities for the input to the context, resulting
4636 * in invalid sample values.
4638 static int test_partial_lexmin(isl_ctx *ctx)
4640 const char *str;
4641 isl_basic_set *bset;
4642 isl_basic_map *bmap;
4643 isl_map *map;
4645 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
4646 bmap = isl_basic_map_read_from_str(ctx, str);
4647 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
4648 bset = isl_basic_set_read_from_str(ctx, str);
4649 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
4650 isl_map_free(map);
4652 if (!map)
4653 return -1;
4655 return 0;
4658 /* Check that the variable compression performed on the existentially
4659 * quantified variables inside isl_basic_set_compute_divs is not confused
4660 * by the implicit equalities among the parameters.
4662 static int test_compute_divs(isl_ctx *ctx)
4664 const char *str;
4665 isl_basic_set *bset;
4666 isl_set *set;
4668 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
4669 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
4670 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
4671 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
4672 bset = isl_basic_set_read_from_str(ctx, str);
4673 set = isl_basic_set_compute_divs(bset);
4674 isl_set_free(set);
4675 if (!set)
4676 return -1;
4678 return 0;
4681 struct {
4682 const char *name;
4683 int (*fn)(isl_ctx *ctx);
4684 } tests [] = {
4685 { "val", &test_val },
4686 { "compute divs", &test_compute_divs },
4687 { "partial lexmin", &test_partial_lexmin },
4688 { "simplify", &test_simplify },
4689 { "curry", &test_curry },
4690 { "piecewise multi affine expressions", &test_pw_multi_aff },
4691 { "multi piecewise affine expressions", &test_multi_pw_aff },
4692 { "conversion", &test_conversion },
4693 { "list", &test_list },
4694 { "align parameters", &test_align_parameters },
4695 { "preimage", &test_preimage },
4696 { "pullback", &test_pullback },
4697 { "AST", &test_ast },
4698 { "AST generation", &test_ast_gen },
4699 { "eliminate", &test_eliminate },
4700 { "residue class", &test_residue_class },
4701 { "div", &test_div },
4702 { "slice", &test_slice },
4703 { "fixed power", &test_fixed_power },
4704 { "sample", &test_sample },
4705 { "output", &test_output },
4706 { "vertices", &test_vertices },
4707 { "fixed", &test_fixed },
4708 { "equal", &test_equal },
4709 { "disjoint", &test_disjoint },
4710 { "product", &test_product },
4711 { "dim_max", &test_dim_max },
4712 { "affine", &test_aff },
4713 { "injective", &test_injective },
4714 { "schedule", &test_schedule },
4715 { "union_pw", &test_union_pw },
4716 { "parse", &test_parse },
4717 { "single-valued", &test_sv },
4718 { "affine hull", &test_affine_hull },
4719 { "coalesce", &test_coalesce },
4720 { "factorize", &test_factorize },
4721 { "subset", &test_subset },
4722 { "subtract", &test_subtract },
4723 { "lexmin", &test_lexmin },
4724 { "min", &test_min },
4725 { "gist", &test_gist },
4726 { "piecewise quasi-polynomials", &test_pwqp },
4729 int main()
4731 int i;
4732 struct isl_ctx *ctx;
4734 srcdir = getenv("srcdir");
4735 assert(srcdir);
4737 ctx = isl_ctx_alloc();
4738 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
4739 printf("%s\n", tests[i].name);
4740 if (tests[i].fn(ctx) < 0)
4741 goto error;
4743 test_lift(ctx);
4744 test_bound(ctx);
4745 test_union(ctx);
4746 test_split_periods(ctx);
4747 test_lex(ctx);
4748 test_bijective(ctx);
4749 test_dep(ctx);
4750 test_read(ctx);
4751 test_bounded(ctx);
4752 test_construction(ctx);
4753 test_dim(ctx);
4754 test_application(ctx);
4755 test_convex_hull(ctx);
4756 test_closure(ctx);
4757 isl_ctx_free(ctx);
4758 return 0;
4759 error:
4760 isl_ctx_free(ctx);
4761 return -1;