isl_union_map_is_single_valued: add a couple of extra tests
[isl.git] / isl_test.c
blob780c56834efc0cd56305d964c8d85946c56d571a
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <assert.h>
19 #include <stdio.h>
20 #include <limits.h>
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl/set.h>
25 #include <isl/flow.h>
26 #include <isl_constraint_private.h>
27 #include <isl/polynomial.h>
28 #include <isl/union_map.h>
29 #include <isl_factorization.h>
30 #include <isl/schedule.h>
31 #include <isl_options_private.h>
32 #include <isl/vertices.h>
33 #include <isl/ast_build.h>
34 #include <isl/val.h>
35 #include <isl/ilp.h>
36 #include <isl_ast_build_expr.h>
38 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
40 static char *srcdir;
42 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
43 char *filename;
44 int length;
45 char *pattern = "%s/test_inputs/%s.%s";
47 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
48 + strlen(suffix) + 1;
49 filename = isl_alloc_array(ctx, char, length);
51 if (!filename)
52 return NULL;
54 sprintf(filename, pattern, srcdir, name, suffix);
56 return filename;
59 void test_parse_map(isl_ctx *ctx, const char *str)
61 isl_map *map;
63 map = isl_map_read_from_str(ctx, str);
64 assert(map);
65 isl_map_free(map);
68 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
70 isl_map *map, *map2;
71 int equal;
73 map = isl_map_read_from_str(ctx, str);
74 map2 = isl_map_read_from_str(ctx, str2);
75 equal = isl_map_is_equal(map, map2);
76 isl_map_free(map);
77 isl_map_free(map2);
79 if (equal < 0)
80 return -1;
81 if (!equal)
82 isl_die(ctx, isl_error_unknown, "maps not equal",
83 return -1);
85 return 0;
88 void test_parse_pwqp(isl_ctx *ctx, const char *str)
90 isl_pw_qpolynomial *pwqp;
92 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
93 assert(pwqp);
94 isl_pw_qpolynomial_free(pwqp);
97 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
99 isl_pw_aff *pwaff;
101 pwaff = isl_pw_aff_read_from_str(ctx, str);
102 assert(pwaff);
103 isl_pw_aff_free(pwaff);
106 int test_parse(struct isl_ctx *ctx)
108 isl_map *map, *map2;
109 const char *str, *str2;
111 str = "{ [i] -> [-i] }";
112 map = isl_map_read_from_str(ctx, str);
113 assert(map);
114 isl_map_free(map);
116 str = "{ A[i] -> L[([i/3])] }";
117 map = isl_map_read_from_str(ctx, str);
118 assert(map);
119 isl_map_free(map);
121 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
122 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
123 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
125 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
126 str2 = "{ [x, y] : 2y >= 6 - x }";
127 if (test_parse_map_equal(ctx, str, str2) < 0)
128 return -1;
130 if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
131 "{ [x,y] : x <= y, 2*y + 3 }") < 0)
132 return -1;
133 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
134 if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
135 str) < 0)
136 return -1;
138 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
139 map = isl_map_read_from_str(ctx, str);
140 str = "{ [new, old] -> [o0, o1] : "
141 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
142 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
143 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
144 map2 = isl_map_read_from_str(ctx, str);
145 assert(isl_map_is_equal(map, map2));
146 isl_map_free(map);
147 isl_map_free(map2);
149 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
150 map = isl_map_read_from_str(ctx, str);
151 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
152 map2 = isl_map_read_from_str(ctx, str);
153 assert(isl_map_is_equal(map, map2));
154 isl_map_free(map);
155 isl_map_free(map2);
157 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
158 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
159 if (test_parse_map_equal(ctx, str, str2) < 0)
160 return -1;
162 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
163 str2 = "{ [i,j] -> [min(i,j)] }";
164 if (test_parse_map_equal(ctx, str, str2) < 0)
165 return -1;
167 str = "{ [i,j] : i != j }";
168 str2 = "{ [i,j] : i < j or i > j }";
169 if (test_parse_map_equal(ctx, str, str2) < 0)
170 return -1;
172 str = "{ [i,j] : (i+1)*2 >= j }";
173 str2 = "{ [i, j] : j <= 2 + 2i }";
174 if (test_parse_map_equal(ctx, str, str2) < 0)
175 return -1;
177 str = "{ [i] -> [i > 0 ? 4 : 5] }";
178 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
179 if (test_parse_map_equal(ctx, str, str2) < 0)
180 return -1;
182 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
183 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
184 if (test_parse_map_equal(ctx, str, str2) < 0)
185 return -1;
187 str = "{ [x] : x >= 0 }";
188 str2 = "{ [x] : x-0 >= 0 }";
189 if (test_parse_map_equal(ctx, str, str2) < 0)
190 return -1;
192 str = "{ [i] : ((i > 10)) }";
193 str2 = "{ [i] : i >= 11 }";
194 if (test_parse_map_equal(ctx, str, str2) < 0)
195 return -1;
197 str = "{ [i] -> [0] }";
198 str2 = "{ [i] -> [0 * i] }";
199 if (test_parse_map_equal(ctx, str, str2) < 0)
200 return -1;
202 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
203 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
204 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
205 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
207 if (test_parse_map_equal(ctx, "{ [a] -> [b] : (not false) }",
208 "{ [a] -> [b] : true }") < 0)
209 return -1;
211 if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
212 "{ [i] : i <= 10 }") < 0)
213 return -1;
215 if (test_parse_map_equal(ctx, "{Sym=[n] [i] : i <= n }",
216 "[n] -> { [i] : i <= n }") < 0)
217 return -1;
219 if (test_parse_map_equal(ctx, "{ [*] }", "{ [a] }") < 0)
220 return -1;
222 if (test_parse_map_equal(ctx, "{ [i] : 2*floor(i/2) = i }",
223 "{ [i] : exists a : i = 2 a }") < 0)
224 return -1;
226 if (test_parse_map_equal(ctx, "{ [a] -> [b] : a = 5 implies b = 5 }",
227 "{ [a] -> [b] : a != 5 or b = 5 }") < 0)
228 return -1;
230 if (test_parse_map_equal(ctx, "{ [a] -> [a - 1 : a > 0] }",
231 "{ [a] -> [a - 1] : a > 0 }") < 0)
232 return -1;
233 if (test_parse_map_equal(ctx,
234 "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
235 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : 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;
249 if (test_parse_map_equal(ctx,
250 "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
251 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
252 return -1;
254 return 0;
257 void test_read(struct isl_ctx *ctx)
259 char *filename;
260 FILE *input;
261 struct isl_basic_set *bset1, *bset2;
262 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
264 filename = get_filename(ctx, "set", "omega");
265 assert(filename);
266 input = fopen(filename, "r");
267 assert(input);
269 bset1 = isl_basic_set_read_from_file(ctx, input);
270 bset2 = isl_basic_set_read_from_str(ctx, str);
272 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
274 isl_basic_set_free(bset1);
275 isl_basic_set_free(bset2);
276 free(filename);
278 fclose(input);
281 void test_bounded(struct isl_ctx *ctx)
283 isl_set *set;
284 int bounded;
286 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
287 bounded = isl_set_is_bounded(set);
288 assert(bounded);
289 isl_set_free(set);
291 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
292 bounded = isl_set_is_bounded(set);
293 assert(!bounded);
294 isl_set_free(set);
296 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
297 bounded = isl_set_is_bounded(set);
298 assert(!bounded);
299 isl_set_free(set);
302 /* Construct the basic set { [i] : 5 <= i <= N } */
303 void test_construction(struct isl_ctx *ctx)
305 isl_int v;
306 isl_space *dim;
307 isl_local_space *ls;
308 struct isl_basic_set *bset;
309 struct isl_constraint *c;
311 isl_int_init(v);
313 dim = isl_space_set_alloc(ctx, 1, 1);
314 bset = isl_basic_set_universe(isl_space_copy(dim));
315 ls = isl_local_space_from_space(dim);
317 c = isl_inequality_alloc(isl_local_space_copy(ls));
318 isl_int_set_si(v, -1);
319 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
320 isl_int_set_si(v, 1);
321 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
322 bset = isl_basic_set_add_constraint(bset, c);
324 c = isl_inequality_alloc(isl_local_space_copy(ls));
325 isl_int_set_si(v, 1);
326 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
327 isl_int_set_si(v, -5);
328 isl_constraint_set_constant(c, v);
329 bset = isl_basic_set_add_constraint(bset, c);
331 isl_local_space_free(ls);
332 isl_basic_set_free(bset);
334 isl_int_clear(v);
337 void test_dim(struct isl_ctx *ctx)
339 const char *str;
340 isl_map *map1, *map2;
342 map1 = isl_map_read_from_str(ctx,
343 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
344 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
345 map2 = isl_map_read_from_str(ctx,
346 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
347 assert(isl_map_is_equal(map1, map2));
348 isl_map_free(map2);
350 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
351 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
352 assert(isl_map_is_equal(map1, map2));
354 isl_map_free(map1);
355 isl_map_free(map2);
357 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
358 map1 = isl_map_read_from_str(ctx, str);
359 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
360 map2 = isl_map_read_from_str(ctx, str);
361 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
362 assert(isl_map_is_equal(map1, map2));
364 isl_map_free(map1);
365 isl_map_free(map2);
368 struct {
369 __isl_give isl_val *(*op)(__isl_take isl_val *v);
370 const char *arg;
371 const char *res;
372 } val_un_tests[] = {
373 { &isl_val_neg, "0", "0" },
374 { &isl_val_abs, "0", "0" },
375 { &isl_val_2exp, "0", "1" },
376 { &isl_val_floor, "0", "0" },
377 { &isl_val_ceil, "0", "0" },
378 { &isl_val_neg, "1", "-1" },
379 { &isl_val_neg, "-1", "1" },
380 { &isl_val_neg, "1/2", "-1/2" },
381 { &isl_val_neg, "-1/2", "1/2" },
382 { &isl_val_neg, "infty", "-infty" },
383 { &isl_val_neg, "-infty", "infty" },
384 { &isl_val_neg, "NaN", "NaN" },
385 { &isl_val_abs, "1", "1" },
386 { &isl_val_abs, "-1", "1" },
387 { &isl_val_abs, "1/2", "1/2" },
388 { &isl_val_abs, "-1/2", "1/2" },
389 { &isl_val_abs, "infty", "infty" },
390 { &isl_val_abs, "-infty", "infty" },
391 { &isl_val_abs, "NaN", "NaN" },
392 { &isl_val_floor, "1", "1" },
393 { &isl_val_floor, "-1", "-1" },
394 { &isl_val_floor, "1/2", "0" },
395 { &isl_val_floor, "-1/2", "-1" },
396 { &isl_val_floor, "infty", "infty" },
397 { &isl_val_floor, "-infty", "-infty" },
398 { &isl_val_floor, "NaN", "NaN" },
399 { &isl_val_ceil, "1", "1" },
400 { &isl_val_ceil, "-1", "-1" },
401 { &isl_val_ceil, "1/2", "1" },
402 { &isl_val_ceil, "-1/2", "0" },
403 { &isl_val_ceil, "infty", "infty" },
404 { &isl_val_ceil, "-infty", "-infty" },
405 { &isl_val_ceil, "NaN", "NaN" },
406 { &isl_val_2exp, "-3", "1/8" },
407 { &isl_val_2exp, "-1", "1/2" },
408 { &isl_val_2exp, "1", "2" },
409 { &isl_val_2exp, "2", "4" },
410 { &isl_val_2exp, "3", "8" },
413 /* Perform some basic tests of unary operations on isl_val objects.
415 static int test_un_val(isl_ctx *ctx)
417 int i;
418 isl_val *v, *res;
419 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
420 int ok;
422 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
423 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
424 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
425 fn = val_un_tests[i].op;
426 v = fn(v);
427 if (isl_val_is_nan(res))
428 ok = isl_val_is_nan(v);
429 else
430 ok = isl_val_eq(v, res);
431 isl_val_free(v);
432 isl_val_free(res);
433 if (ok < 0)
434 return -1;
435 if (!ok)
436 isl_die(ctx, isl_error_unknown,
437 "unexpected result", return -1);
440 return 0;
443 struct {
444 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
445 __isl_take isl_val *v2);
446 } val_bin_op[] = {
447 ['+'] = { &isl_val_add },
448 ['-'] = { &isl_val_sub },
449 ['*'] = { &isl_val_mul },
450 ['/'] = { &isl_val_div },
451 ['g'] = { &isl_val_gcd },
452 ['m'] = { &isl_val_min },
453 ['M'] = { &isl_val_max },
456 struct {
457 const char *arg1;
458 unsigned char op;
459 const char *arg2;
460 const char *res;
461 } val_bin_tests[] = {
462 { "0", '+', "0", "0" },
463 { "1", '+', "0", "1" },
464 { "1", '+', "1", "2" },
465 { "1", '-', "1", "0" },
466 { "1", '*', "1", "1" },
467 { "1", '/', "1", "1" },
468 { "2", '*', "3", "6" },
469 { "2", '*', "1/2", "1" },
470 { "2", '*', "1/3", "2/3" },
471 { "2/3", '*', "3/5", "2/5" },
472 { "2/3", '*', "7/5", "14/15" },
473 { "2", '/', "1/2", "4" },
474 { "-2", '/', "-1/2", "4" },
475 { "-2", '/', "1/2", "-4" },
476 { "2", '/', "-1/2", "-4" },
477 { "2", '/', "2", "1" },
478 { "2", '/', "3", "2/3" },
479 { "2/3", '/', "5/3", "2/5" },
480 { "2/3", '/', "5/7", "14/15" },
481 { "0", '/', "0", "NaN" },
482 { "42", '/', "0", "NaN" },
483 { "-42", '/', "0", "NaN" },
484 { "infty", '/', "0", "NaN" },
485 { "-infty", '/', "0", "NaN" },
486 { "NaN", '/', "0", "NaN" },
487 { "0", '/', "NaN", "NaN" },
488 { "42", '/', "NaN", "NaN" },
489 { "-42", '/', "NaN", "NaN" },
490 { "infty", '/', "NaN", "NaN" },
491 { "-infty", '/', "NaN", "NaN" },
492 { "NaN", '/', "NaN", "NaN" },
493 { "0", '/', "infty", "0" },
494 { "42", '/', "infty", "0" },
495 { "-42", '/', "infty", "0" },
496 { "infty", '/', "infty", "NaN" },
497 { "-infty", '/', "infty", "NaN" },
498 { "NaN", '/', "infty", "NaN" },
499 { "0", '/', "-infty", "0" },
500 { "42", '/', "-infty", "0" },
501 { "-42", '/', "-infty", "0" },
502 { "infty", '/', "-infty", "NaN" },
503 { "-infty", '/', "-infty", "NaN" },
504 { "NaN", '/', "-infty", "NaN" },
505 { "1", '-', "1/3", "2/3" },
506 { "1/3", '+', "1/2", "5/6" },
507 { "1/2", '+', "1/2", "1" },
508 { "3/4", '-', "1/4", "1/2" },
509 { "1/2", '-', "1/3", "1/6" },
510 { "infty", '+', "42", "infty" },
511 { "infty", '+', "infty", "infty" },
512 { "42", '+', "infty", "infty" },
513 { "infty", '-', "infty", "NaN" },
514 { "infty", '*', "infty", "infty" },
515 { "infty", '*', "-infty", "-infty" },
516 { "-infty", '*', "infty", "-infty" },
517 { "-infty", '*', "-infty", "infty" },
518 { "0", '*', "infty", "NaN" },
519 { "1", '*', "infty", "infty" },
520 { "infty", '*', "0", "NaN" },
521 { "infty", '*', "42", "infty" },
522 { "42", '-', "infty", "-infty" },
523 { "infty", '+', "-infty", "NaN" },
524 { "4", 'g', "6", "2" },
525 { "5", 'g', "6", "1" },
526 { "42", 'm', "3", "3" },
527 { "42", 'M', "3", "42" },
528 { "3", 'm', "42", "3" },
529 { "3", 'M', "42", "42" },
530 { "42", 'm', "infty", "42" },
531 { "42", 'M', "infty", "infty" },
532 { "42", 'm', "-infty", "-infty" },
533 { "42", 'M', "-infty", "42" },
534 { "42", 'm', "NaN", "NaN" },
535 { "42", 'M', "NaN", "NaN" },
536 { "infty", 'm', "-infty", "-infty" },
537 { "infty", 'M', "-infty", "infty" },
540 /* Perform some basic tests of binary operations on isl_val objects.
542 static int test_bin_val(isl_ctx *ctx)
544 int i;
545 isl_val *v1, *v2, *res;
546 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
547 __isl_take isl_val *v2);
548 int ok;
550 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
551 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
552 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
553 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
554 fn = val_bin_op[val_bin_tests[i].op].fn;
555 v1 = fn(v1, v2);
556 if (isl_val_is_nan(res))
557 ok = isl_val_is_nan(v1);
558 else
559 ok = isl_val_eq(v1, res);
560 isl_val_free(v1);
561 isl_val_free(res);
562 if (ok < 0)
563 return -1;
564 if (!ok)
565 isl_die(ctx, isl_error_unknown,
566 "unexpected result", return -1);
569 return 0;
572 /* Perform some basic tests on isl_val objects.
574 static int test_val(isl_ctx *ctx)
576 if (test_un_val(ctx) < 0)
577 return -1;
578 if (test_bin_val(ctx) < 0)
579 return -1;
580 return 0;
583 static int test_div(isl_ctx *ctx)
585 unsigned n;
586 const char *str;
587 int empty;
588 isl_int v;
589 isl_space *dim;
590 isl_set *set;
591 isl_local_space *ls;
592 struct isl_basic_set *bset;
593 struct isl_constraint *c;
595 isl_int_init(v);
597 /* test 1 */
598 dim = isl_space_set_alloc(ctx, 0, 3);
599 bset = isl_basic_set_universe(isl_space_copy(dim));
600 ls = isl_local_space_from_space(dim);
602 c = isl_equality_alloc(isl_local_space_copy(ls));
603 isl_int_set_si(v, -1);
604 isl_constraint_set_constant(c, v);
605 isl_int_set_si(v, 1);
606 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
607 isl_int_set_si(v, 3);
608 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
609 bset = isl_basic_set_add_constraint(bset, c);
611 c = isl_equality_alloc(isl_local_space_copy(ls));
612 isl_int_set_si(v, 1);
613 isl_constraint_set_constant(c, v);
614 isl_int_set_si(v, -1);
615 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
616 isl_int_set_si(v, 3);
617 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
618 bset = isl_basic_set_add_constraint(bset, c);
620 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
622 assert(bset && bset->n_div == 1);
623 isl_local_space_free(ls);
624 isl_basic_set_free(bset);
626 /* test 2 */
627 dim = isl_space_set_alloc(ctx, 0, 3);
628 bset = isl_basic_set_universe(isl_space_copy(dim));
629 ls = isl_local_space_from_space(dim);
631 c = isl_equality_alloc(isl_local_space_copy(ls));
632 isl_int_set_si(v, 1);
633 isl_constraint_set_constant(c, v);
634 isl_int_set_si(v, -1);
635 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
636 isl_int_set_si(v, 3);
637 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
638 bset = isl_basic_set_add_constraint(bset, c);
640 c = isl_equality_alloc(isl_local_space_copy(ls));
641 isl_int_set_si(v, -1);
642 isl_constraint_set_constant(c, v);
643 isl_int_set_si(v, 1);
644 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
645 isl_int_set_si(v, 3);
646 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
647 bset = isl_basic_set_add_constraint(bset, c);
649 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
651 assert(bset && bset->n_div == 1);
652 isl_local_space_free(ls);
653 isl_basic_set_free(bset);
655 /* test 3 */
656 dim = isl_space_set_alloc(ctx, 0, 3);
657 bset = isl_basic_set_universe(isl_space_copy(dim));
658 ls = isl_local_space_from_space(dim);
660 c = isl_equality_alloc(isl_local_space_copy(ls));
661 isl_int_set_si(v, 1);
662 isl_constraint_set_constant(c, v);
663 isl_int_set_si(v, -1);
664 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
665 isl_int_set_si(v, 3);
666 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
667 bset = isl_basic_set_add_constraint(bset, c);
669 c = isl_equality_alloc(isl_local_space_copy(ls));
670 isl_int_set_si(v, -3);
671 isl_constraint_set_constant(c, v);
672 isl_int_set_si(v, 1);
673 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
674 isl_int_set_si(v, 4);
675 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
676 bset = isl_basic_set_add_constraint(bset, c);
678 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
680 assert(bset && bset->n_div == 1);
681 isl_local_space_free(ls);
682 isl_basic_set_free(bset);
684 /* test 4 */
685 dim = isl_space_set_alloc(ctx, 0, 3);
686 bset = isl_basic_set_universe(isl_space_copy(dim));
687 ls = isl_local_space_from_space(dim);
689 c = isl_equality_alloc(isl_local_space_copy(ls));
690 isl_int_set_si(v, 2);
691 isl_constraint_set_constant(c, v);
692 isl_int_set_si(v, -1);
693 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
694 isl_int_set_si(v, 3);
695 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
696 bset = isl_basic_set_add_constraint(bset, c);
698 c = isl_equality_alloc(isl_local_space_copy(ls));
699 isl_int_set_si(v, -1);
700 isl_constraint_set_constant(c, v);
701 isl_int_set_si(v, 1);
702 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
703 isl_int_set_si(v, 6);
704 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
705 bset = isl_basic_set_add_constraint(bset, c);
707 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
709 assert(isl_basic_set_is_empty(bset));
710 isl_local_space_free(ls);
711 isl_basic_set_free(bset);
713 /* test 5 */
714 dim = isl_space_set_alloc(ctx, 0, 3);
715 bset = isl_basic_set_universe(isl_space_copy(dim));
716 ls = isl_local_space_from_space(dim);
718 c = isl_equality_alloc(isl_local_space_copy(ls));
719 isl_int_set_si(v, -1);
720 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
721 isl_int_set_si(v, 3);
722 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
723 bset = isl_basic_set_add_constraint(bset, c);
725 c = isl_equality_alloc(isl_local_space_copy(ls));
726 isl_int_set_si(v, 1);
727 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
728 isl_int_set_si(v, -3);
729 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
730 bset = isl_basic_set_add_constraint(bset, c);
732 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
734 assert(bset && bset->n_div == 0);
735 isl_basic_set_free(bset);
736 isl_local_space_free(ls);
738 /* test 6 */
739 dim = isl_space_set_alloc(ctx, 0, 3);
740 bset = isl_basic_set_universe(isl_space_copy(dim));
741 ls = isl_local_space_from_space(dim);
743 c = isl_equality_alloc(isl_local_space_copy(ls));
744 isl_int_set_si(v, -1);
745 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
746 isl_int_set_si(v, 6);
747 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
748 bset = isl_basic_set_add_constraint(bset, c);
750 c = isl_equality_alloc(isl_local_space_copy(ls));
751 isl_int_set_si(v, 1);
752 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
753 isl_int_set_si(v, -3);
754 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
755 bset = isl_basic_set_add_constraint(bset, c);
757 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
759 assert(bset && bset->n_div == 1);
760 isl_basic_set_free(bset);
761 isl_local_space_free(ls);
763 /* test 7 */
764 /* This test is a bit tricky. We set up an equality
765 * a + 3b + 3c = 6 e0
766 * Normalization of divs creates _two_ divs
767 * a = 3 e0
768 * c - b - e0 = 2 e1
769 * Afterwards e0 is removed again because it has coefficient -1
770 * and we end up with the original equality and div again.
771 * Perhaps we can avoid the introduction of this temporary div.
773 dim = isl_space_set_alloc(ctx, 0, 4);
774 bset = isl_basic_set_universe(isl_space_copy(dim));
775 ls = isl_local_space_from_space(dim);
777 c = isl_equality_alloc(isl_local_space_copy(ls));
778 isl_int_set_si(v, -1);
779 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
780 isl_int_set_si(v, -3);
781 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
782 isl_int_set_si(v, -3);
783 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
784 isl_int_set_si(v, 6);
785 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
786 bset = isl_basic_set_add_constraint(bset, c);
788 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
790 /* Test disabled for now */
792 assert(bset && bset->n_div == 1);
794 isl_local_space_free(ls);
795 isl_basic_set_free(bset);
797 /* test 8 */
798 dim = isl_space_set_alloc(ctx, 0, 5);
799 bset = isl_basic_set_universe(isl_space_copy(dim));
800 ls = isl_local_space_from_space(dim);
802 c = isl_equality_alloc(isl_local_space_copy(ls));
803 isl_int_set_si(v, -1);
804 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
805 isl_int_set_si(v, -3);
806 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
807 isl_int_set_si(v, -3);
808 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
809 isl_int_set_si(v, 6);
810 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
811 bset = isl_basic_set_add_constraint(bset, c);
813 c = isl_equality_alloc(isl_local_space_copy(ls));
814 isl_int_set_si(v, -1);
815 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
816 isl_int_set_si(v, 1);
817 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
818 isl_int_set_si(v, 1);
819 isl_constraint_set_constant(c, v);
820 bset = isl_basic_set_add_constraint(bset, c);
822 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
824 /* Test disabled for now */
826 assert(bset && bset->n_div == 1);
828 isl_local_space_free(ls);
829 isl_basic_set_free(bset);
831 /* test 9 */
832 dim = isl_space_set_alloc(ctx, 0, 4);
833 bset = isl_basic_set_universe(isl_space_copy(dim));
834 ls = isl_local_space_from_space(dim);
836 c = isl_equality_alloc(isl_local_space_copy(ls));
837 isl_int_set_si(v, 1);
838 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
839 isl_int_set_si(v, -1);
840 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
841 isl_int_set_si(v, -2);
842 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
843 bset = isl_basic_set_add_constraint(bset, c);
845 c = isl_equality_alloc(isl_local_space_copy(ls));
846 isl_int_set_si(v, -1);
847 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
848 isl_int_set_si(v, 3);
849 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
850 isl_int_set_si(v, 2);
851 isl_constraint_set_constant(c, v);
852 bset = isl_basic_set_add_constraint(bset, c);
854 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
856 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
858 assert(!isl_basic_set_is_empty(bset));
860 isl_local_space_free(ls);
861 isl_basic_set_free(bset);
863 /* test 10 */
864 dim = isl_space_set_alloc(ctx, 0, 3);
865 bset = isl_basic_set_universe(isl_space_copy(dim));
866 ls = isl_local_space_from_space(dim);
868 c = isl_equality_alloc(isl_local_space_copy(ls));
869 isl_int_set_si(v, 1);
870 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
871 isl_int_set_si(v, -2);
872 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
873 bset = isl_basic_set_add_constraint(bset, c);
875 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
877 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
879 isl_local_space_free(ls);
880 isl_basic_set_free(bset);
882 isl_int_clear(v);
884 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
885 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
886 set = isl_set_read_from_str(ctx, str);
887 set = isl_set_compute_divs(set);
888 isl_set_free(set);
889 if (!set)
890 return -1;
892 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
893 bset = isl_basic_set_read_from_str(ctx, str);
894 n = isl_basic_set_dim(bset, isl_dim_div);
895 isl_basic_set_free(bset);
896 if (!bset)
897 return -1;
898 if (n != 0)
899 isl_die(ctx, isl_error_unknown,
900 "expecting no existentials", return -1);
902 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
903 set = isl_set_read_from_str(ctx, str);
904 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
905 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
906 empty = isl_set_is_empty(set);
907 isl_set_free(set);
908 if (empty < 0)
909 return -1;
910 if (!empty)
911 isl_die(ctx, isl_error_unknown,
912 "result not as accurate as expected", return -1);
914 return 0;
917 void test_application_case(struct isl_ctx *ctx, const char *name)
919 char *filename;
920 FILE *input;
921 struct isl_basic_set *bset1, *bset2;
922 struct isl_basic_map *bmap;
924 filename = get_filename(ctx, name, "omega");
925 assert(filename);
926 input = fopen(filename, "r");
927 assert(input);
929 bset1 = isl_basic_set_read_from_file(ctx, input);
930 bmap = isl_basic_map_read_from_file(ctx, input);
932 bset1 = isl_basic_set_apply(bset1, bmap);
934 bset2 = isl_basic_set_read_from_file(ctx, input);
936 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
938 isl_basic_set_free(bset1);
939 isl_basic_set_free(bset2);
940 free(filename);
942 fclose(input);
945 void test_application(struct isl_ctx *ctx)
947 test_application_case(ctx, "application");
948 test_application_case(ctx, "application2");
951 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
953 char *filename;
954 FILE *input;
955 struct isl_basic_set *bset1, *bset2;
957 filename = get_filename(ctx, name, "polylib");
958 assert(filename);
959 input = fopen(filename, "r");
960 assert(input);
962 bset1 = isl_basic_set_read_from_file(ctx, input);
963 bset2 = isl_basic_set_read_from_file(ctx, input);
965 bset1 = isl_basic_set_affine_hull(bset1);
967 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
969 isl_basic_set_free(bset1);
970 isl_basic_set_free(bset2);
971 free(filename);
973 fclose(input);
976 int test_affine_hull(struct isl_ctx *ctx)
978 const char *str;
979 isl_set *set;
980 isl_basic_set *bset, *bset2;
981 int n;
982 int subset;
984 test_affine_hull_case(ctx, "affine2");
985 test_affine_hull_case(ctx, "affine");
986 test_affine_hull_case(ctx, "affine3");
988 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
989 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
990 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
991 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
992 set = isl_set_read_from_str(ctx, str);
993 bset = isl_set_affine_hull(set);
994 n = isl_basic_set_dim(bset, isl_dim_div);
995 isl_basic_set_free(bset);
996 if (n != 0)
997 isl_die(ctx, isl_error_unknown, "not expecting any divs",
998 return -1);
1000 /* Check that isl_map_affine_hull is not confused by
1001 * the reordering of divs in isl_map_align_divs.
1003 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1004 "32e0 = b and 32e1 = c); "
1005 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1006 set = isl_set_read_from_str(ctx, str);
1007 bset = isl_set_affine_hull(set);
1008 isl_basic_set_free(bset);
1009 if (!bset)
1010 return -1;
1012 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1013 "32e2 = 31 + 31e0 }";
1014 set = isl_set_read_from_str(ctx, str);
1015 bset = isl_set_affine_hull(set);
1016 str = "{ [a] : exists e : a = 32 e }";
1017 bset2 = isl_basic_set_read_from_str(ctx, str);
1018 subset = isl_basic_set_is_subset(bset, bset2);
1019 isl_basic_set_free(bset);
1020 isl_basic_set_free(bset2);
1021 if (subset < 0)
1022 return -1;
1023 if (!subset)
1024 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1025 return -1);
1027 return 0;
1030 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1032 char *filename;
1033 FILE *input;
1034 struct isl_basic_set *bset1, *bset2;
1035 struct isl_set *set;
1037 filename = get_filename(ctx, name, "polylib");
1038 assert(filename);
1039 input = fopen(filename, "r");
1040 assert(input);
1042 bset1 = isl_basic_set_read_from_file(ctx, input);
1043 bset2 = isl_basic_set_read_from_file(ctx, input);
1045 set = isl_basic_set_union(bset1, bset2);
1046 bset1 = isl_set_convex_hull(set);
1048 bset2 = isl_basic_set_read_from_file(ctx, input);
1050 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1052 isl_basic_set_free(bset1);
1053 isl_basic_set_free(bset2);
1054 free(filename);
1056 fclose(input);
1059 struct {
1060 const char *set;
1061 const char *hull;
1062 } convex_hull_tests[] = {
1063 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1064 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1065 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1066 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1067 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1068 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1069 "i2 <= 5 and i2 >= 4; "
1070 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1071 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1072 "i2 <= 5 + i0 and i2 >= i0 }" },
1075 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
1077 int i;
1078 int orig_convex = ctx->opt->convex;
1079 ctx->opt->convex = convex;
1081 test_convex_hull_case(ctx, "convex0");
1082 test_convex_hull_case(ctx, "convex1");
1083 test_convex_hull_case(ctx, "convex2");
1084 test_convex_hull_case(ctx, "convex3");
1085 test_convex_hull_case(ctx, "convex4");
1086 test_convex_hull_case(ctx, "convex5");
1087 test_convex_hull_case(ctx, "convex6");
1088 test_convex_hull_case(ctx, "convex7");
1089 test_convex_hull_case(ctx, "convex8");
1090 test_convex_hull_case(ctx, "convex9");
1091 test_convex_hull_case(ctx, "convex10");
1092 test_convex_hull_case(ctx, "convex11");
1093 test_convex_hull_case(ctx, "convex12");
1094 test_convex_hull_case(ctx, "convex13");
1095 test_convex_hull_case(ctx, "convex14");
1096 test_convex_hull_case(ctx, "convex15");
1098 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1099 isl_set *set1, *set2;
1101 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1102 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1103 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1104 assert(isl_set_is_equal(set1, set2));
1105 isl_set_free(set1);
1106 isl_set_free(set2);
1109 ctx->opt->convex = orig_convex;
1112 void test_convex_hull(struct isl_ctx *ctx)
1114 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
1115 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
1118 void test_gist_case(struct isl_ctx *ctx, const char *name)
1120 char *filename;
1121 FILE *input;
1122 struct isl_basic_set *bset1, *bset2;
1124 filename = get_filename(ctx, name, "polylib");
1125 assert(filename);
1126 input = fopen(filename, "r");
1127 assert(input);
1129 bset1 = isl_basic_set_read_from_file(ctx, input);
1130 bset2 = isl_basic_set_read_from_file(ctx, input);
1132 bset1 = isl_basic_set_gist(bset1, bset2);
1134 bset2 = isl_basic_set_read_from_file(ctx, input);
1136 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1138 isl_basic_set_free(bset1);
1139 isl_basic_set_free(bset2);
1140 free(filename);
1142 fclose(input);
1145 struct {
1146 const char *set;
1147 const char *context;
1148 const char *gist;
1149 } gist_tests[] = {
1150 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1151 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1152 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1153 "{ [a, b, c] : a <= 15 }" },
1154 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1155 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1158 static int test_gist(struct isl_ctx *ctx)
1160 int i;
1161 const char *str;
1162 isl_basic_set *bset1, *bset2;
1163 isl_map *map1, *map2;
1164 int equal;
1166 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1167 bset1 = isl_basic_set_read_from_str(ctx, gist_tests[i].set);
1168 bset2 = isl_basic_set_read_from_str(ctx, gist_tests[i].context);
1169 bset1 = isl_basic_set_gist(bset1, bset2);
1170 bset2 = isl_basic_set_read_from_str(ctx, gist_tests[i].gist);
1171 equal = isl_basic_set_is_equal(bset1, bset2);
1172 isl_basic_set_free(bset1);
1173 isl_basic_set_free(bset2);
1174 if (equal < 0)
1175 return -1;
1176 if (!equal)
1177 isl_die(ctx, isl_error_unknown,
1178 "incorrect gist result", return -1);
1181 test_gist_case(ctx, "gist1");
1183 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1184 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1185 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1186 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1187 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1188 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1189 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1190 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1191 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1192 bset1 = isl_basic_set_read_from_str(ctx, str);
1193 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1194 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1195 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1196 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1197 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1198 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1199 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1200 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1201 bset2 = isl_basic_set_read_from_str(ctx, str);
1202 bset1 = isl_basic_set_gist(bset1, bset2);
1203 assert(bset1 && bset1->n_div == 0);
1204 isl_basic_set_free(bset1);
1206 /* Check that the integer divisions of the second disjunct
1207 * do not spread to the first disjunct.
1209 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1210 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1211 "(exists (e0 = [(-1 + t1)/16], "
1212 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1213 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1214 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1215 "o0 <= 4294967295 and t1 <= -1)) }";
1216 map1 = isl_map_read_from_str(ctx, str);
1217 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1218 map2 = isl_map_read_from_str(ctx, str);
1219 map1 = isl_map_gist(map1, map2);
1220 if (!map1)
1221 return -1;
1222 if (map1->n != 1)
1223 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1224 isl_map_free(map1); return -1);
1225 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1226 isl_die(ctx, isl_error_unknown, "expecting single div",
1227 isl_map_free(map1); return -1);
1228 isl_map_free(map1);
1230 return 0;
1233 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1235 isl_set *set, *set2;
1236 int equal;
1237 int one;
1239 set = isl_set_read_from_str(ctx, str);
1240 set = isl_set_coalesce(set);
1241 set2 = isl_set_read_from_str(ctx, str);
1242 equal = isl_set_is_equal(set, set2);
1243 one = set && set->n == 1;
1244 isl_set_free(set);
1245 isl_set_free(set2);
1247 if (equal < 0)
1248 return -1;
1249 if (!equal)
1250 isl_die(ctx, isl_error_unknown,
1251 "coalesced set not equal to input", return -1);
1252 if (check_one && !one)
1253 isl_die(ctx, isl_error_unknown,
1254 "coalesced set should not be a union", return -1);
1256 return 0;
1259 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1261 int r = 0;
1262 int bounded;
1264 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1265 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1267 if (test_coalesce_set(ctx,
1268 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1269 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1270 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1271 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1272 "-10 <= y <= 0}", 1) < 0)
1273 goto error;
1274 if (test_coalesce_set(ctx,
1275 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
1276 goto error;
1277 if (test_coalesce_set(ctx,
1278 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
1279 goto error;
1281 if (0) {
1282 error:
1283 r = -1;
1286 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1288 return r;
1291 /* Inputs for coalescing tests.
1292 * "str" is a string representation of the input set.
1293 * "single_disjunct" is set if we expect the result to consist of
1294 * a single disjunct.
1296 struct {
1297 int single_disjunct;
1298 const char *str;
1299 } coalesce_tests[] = {
1300 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1301 "y >= x & x >= 2 & 5 >= y }" },
1302 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1303 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1304 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1305 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1306 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1307 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1308 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1309 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1310 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1311 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1312 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1313 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1314 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1315 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1316 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1317 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1318 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1319 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1320 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1321 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1322 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1323 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1324 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1325 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1326 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1327 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1328 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1329 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1330 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1331 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1332 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1333 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1334 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1335 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1336 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1337 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1338 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1339 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1340 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1341 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1342 "[o0, o1, o2, o3, o4, o5, o6]] : "
1343 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1344 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1345 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1346 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1347 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1348 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1349 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1350 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1351 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1352 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1353 "o6 >= i3 + i6 - o3 and M >= 0 and "
1354 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1355 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1356 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1357 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1358 "(o0 = 0 and M >= 2 and N >= 3) or "
1359 "(M = 0 and o0 = 0 and N >= 3) }" },
1360 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1361 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1362 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1363 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1364 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1365 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1366 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1367 "(y = 3 and x = 1) }" },
1368 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1369 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1370 "i1 <= M and i3 <= M and i4 <= M) or "
1371 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1372 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1373 "i4 <= -1 + M) }" },
1374 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1375 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1376 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1377 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1378 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1379 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1380 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1381 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1382 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1383 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1384 { 0, "{ [a, b] : exists e : 2e = a and "
1385 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1386 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1387 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1388 "j >= 1 and j' <= i + j - i' and i >= 1; "
1389 "[1, 1, 1, 1] }" },
1390 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1391 "[i,j] : exists a : j = 3a }" },
1392 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1393 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1394 "a >= 3) or "
1395 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1396 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1397 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1398 "c <= 6 + 8a and a >= 3; "
1399 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1400 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1401 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1402 "[x,0] : 3 <= x <= 4 }" },
1403 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1404 "[x,0] : 4 <= x <= 5 }" },
1405 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1406 "[x,0] : 3 <= x <= 5 }" },
1407 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1408 "[x,0] : 3 <= x <= 4 }" },
1409 { 1 , "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1410 "i1 <= 0; "
1411 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1414 /* Test the functionality of isl_set_coalesce.
1415 * That is, check that the output is always equal to the input
1416 * and in some cases that the result consists of a single disjunct.
1418 static int test_coalesce(struct isl_ctx *ctx)
1420 int i;
1422 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1423 const char *str = coalesce_tests[i].str;
1424 int check_one = coalesce_tests[i].single_disjunct;
1425 if (test_coalesce_set(ctx, str, check_one) < 0)
1426 return -1;
1429 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1430 return -1;
1432 return 0;
1435 void test_closure(struct isl_ctx *ctx)
1437 const char *str;
1438 isl_set *dom;
1439 isl_map *up, *right;
1440 isl_map *map, *map2;
1441 int exact;
1443 /* COCOA example 1 */
1444 map = isl_map_read_from_str(ctx,
1445 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1446 "1 <= i and i < n and 1 <= j and j < n or "
1447 "i2 = i + 1 and j2 = j - 1 and "
1448 "1 <= i and i < n and 2 <= j and j <= n }");
1449 map = isl_map_power(map, &exact);
1450 assert(exact);
1451 isl_map_free(map);
1453 /* COCOA example 1 */
1454 map = isl_map_read_from_str(ctx,
1455 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1456 "1 <= i and i < n and 1 <= j and j < n or "
1457 "i2 = i + 1 and j2 = j - 1 and "
1458 "1 <= i and i < n and 2 <= j and j <= n }");
1459 map = isl_map_transitive_closure(map, &exact);
1460 assert(exact);
1461 map2 = isl_map_read_from_str(ctx,
1462 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1463 "1 <= i and i < n and 1 <= j and j <= n and "
1464 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1465 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1466 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1467 assert(isl_map_is_equal(map, map2));
1468 isl_map_free(map2);
1469 isl_map_free(map);
1471 map = isl_map_read_from_str(ctx,
1472 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1473 " 0 <= y and y <= n }");
1474 map = isl_map_transitive_closure(map, &exact);
1475 map2 = isl_map_read_from_str(ctx,
1476 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1477 " 0 <= y and y <= n }");
1478 assert(isl_map_is_equal(map, map2));
1479 isl_map_free(map2);
1480 isl_map_free(map);
1482 /* COCOA example 2 */
1483 map = isl_map_read_from_str(ctx,
1484 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1485 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1486 "i2 = i + 2 and j2 = j - 2 and "
1487 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1488 map = isl_map_transitive_closure(map, &exact);
1489 assert(exact);
1490 map2 = isl_map_read_from_str(ctx,
1491 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1492 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1493 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1494 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1495 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1496 assert(isl_map_is_equal(map, map2));
1497 isl_map_free(map);
1498 isl_map_free(map2);
1500 /* COCOA Fig.2 left */
1501 map = isl_map_read_from_str(ctx,
1502 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1503 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1504 "j <= n or "
1505 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1506 "j <= 2 i - 3 and j <= n - 2 or "
1507 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1508 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1509 map = isl_map_transitive_closure(map, &exact);
1510 assert(exact);
1511 isl_map_free(map);
1513 /* COCOA Fig.2 right */
1514 map = isl_map_read_from_str(ctx,
1515 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1516 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1517 "j <= n or "
1518 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1519 "j <= 2 i - 4 and j <= n - 3 or "
1520 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1521 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1522 map = isl_map_power(map, &exact);
1523 assert(exact);
1524 isl_map_free(map);
1526 /* COCOA Fig.2 right */
1527 map = isl_map_read_from_str(ctx,
1528 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1529 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1530 "j <= n or "
1531 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1532 "j <= 2 i - 4 and j <= n - 3 or "
1533 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1534 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1535 map = isl_map_transitive_closure(map, &exact);
1536 assert(exact);
1537 map2 = isl_map_read_from_str(ctx,
1538 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1539 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1540 "j <= n and 3 + i + 2 j <= 3 n and "
1541 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1542 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1543 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1544 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1545 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1546 assert(isl_map_is_equal(map, map2));
1547 isl_map_free(map2);
1548 isl_map_free(map);
1550 /* COCOA Fig.1 right */
1551 dom = isl_set_read_from_str(ctx,
1552 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1553 "2 x - 3 y + 3 >= 0 }");
1554 right = isl_map_read_from_str(ctx,
1555 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1556 up = isl_map_read_from_str(ctx,
1557 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1558 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1559 right = isl_map_intersect_range(right, isl_set_copy(dom));
1560 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1561 up = isl_map_intersect_range(up, dom);
1562 map = isl_map_union(up, right);
1563 map = isl_map_transitive_closure(map, &exact);
1564 assert(exact);
1565 map2 = isl_map_read_from_str(ctx,
1566 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1567 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1568 assert(isl_map_is_equal(map, map2));
1569 isl_map_free(map2);
1570 isl_map_free(map);
1572 /* COCOA Theorem 1 counter example */
1573 map = isl_map_read_from_str(ctx,
1574 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1575 "i2 = 1 and j2 = j or "
1576 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1577 map = isl_map_transitive_closure(map, &exact);
1578 assert(exact);
1579 isl_map_free(map);
1581 map = isl_map_read_from_str(ctx,
1582 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1583 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1584 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1585 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1586 map = isl_map_transitive_closure(map, &exact);
1587 assert(exact);
1588 isl_map_free(map);
1590 /* Kelly et al 1996, fig 12 */
1591 map = isl_map_read_from_str(ctx,
1592 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1593 "1 <= i,j,j+1 <= n or "
1594 "j = n and j2 = 1 and i2 = i + 1 and "
1595 "1 <= i,i+1 <= n }");
1596 map = isl_map_transitive_closure(map, &exact);
1597 assert(exact);
1598 map2 = isl_map_read_from_str(ctx,
1599 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1600 "1 <= i <= n and i = i2 or "
1601 "1 <= i < i2 <= n and 1 <= j <= n and "
1602 "1 <= j2 <= n }");
1603 assert(isl_map_is_equal(map, map2));
1604 isl_map_free(map2);
1605 isl_map_free(map);
1607 /* Omega's closure4 */
1608 map = isl_map_read_from_str(ctx,
1609 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1610 "1 <= x,y <= 10 or "
1611 "x2 = x + 1 and y2 = y and "
1612 "1 <= x <= 20 && 5 <= y <= 15 }");
1613 map = isl_map_transitive_closure(map, &exact);
1614 assert(exact);
1615 isl_map_free(map);
1617 map = isl_map_read_from_str(ctx,
1618 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1619 map = isl_map_transitive_closure(map, &exact);
1620 assert(!exact);
1621 map2 = isl_map_read_from_str(ctx,
1622 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1623 assert(isl_map_is_equal(map, map2));
1624 isl_map_free(map);
1625 isl_map_free(map2);
1627 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1628 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1629 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1630 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1631 map = isl_map_read_from_str(ctx, str);
1632 map = isl_map_transitive_closure(map, &exact);
1633 assert(exact);
1634 map2 = isl_map_read_from_str(ctx, str);
1635 assert(isl_map_is_equal(map, map2));
1636 isl_map_free(map);
1637 isl_map_free(map2);
1639 str = "{[0] -> [1]; [2] -> [3]}";
1640 map = isl_map_read_from_str(ctx, str);
1641 map = isl_map_transitive_closure(map, &exact);
1642 assert(exact);
1643 map2 = isl_map_read_from_str(ctx, str);
1644 assert(isl_map_is_equal(map, map2));
1645 isl_map_free(map);
1646 isl_map_free(map2);
1648 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1649 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1650 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1651 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1652 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1653 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1654 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1655 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1656 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1657 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1658 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1659 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1660 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1661 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1662 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1663 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1664 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1665 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1666 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1667 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1668 map = isl_map_read_from_str(ctx, str);
1669 map = isl_map_transitive_closure(map, NULL);
1670 assert(map);
1671 isl_map_free(map);
1674 void test_lex(struct isl_ctx *ctx)
1676 isl_space *dim;
1677 isl_map *map;
1679 dim = isl_space_set_alloc(ctx, 0, 0);
1680 map = isl_map_lex_le(dim);
1681 assert(!isl_map_is_empty(map));
1682 isl_map_free(map);
1685 static int test_lexmin(struct isl_ctx *ctx)
1687 int equal;
1688 const char *str;
1689 isl_basic_map *bmap;
1690 isl_map *map, *map2;
1691 isl_set *set;
1692 isl_set *set2;
1693 isl_pw_multi_aff *pma;
1695 str = "[p0, p1] -> { [] -> [] : "
1696 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1697 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1698 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1699 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1700 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1701 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1702 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1703 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1704 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1705 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1706 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1707 map = isl_map_read_from_str(ctx, str);
1708 map = isl_map_lexmin(map);
1709 isl_map_free(map);
1711 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1712 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1713 set = isl_set_read_from_str(ctx, str);
1714 set = isl_set_lexmax(set);
1715 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1716 set2 = isl_set_read_from_str(ctx, str);
1717 set = isl_set_intersect(set, set2);
1718 assert(!isl_set_is_empty(set));
1719 isl_set_free(set);
1721 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1722 map = isl_map_read_from_str(ctx, str);
1723 map = isl_map_lexmin(map);
1724 str = "{ [x] -> [5] : 6 <= x <= 8; "
1725 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1726 map2 = isl_map_read_from_str(ctx, str);
1727 assert(isl_map_is_equal(map, map2));
1728 isl_map_free(map);
1729 isl_map_free(map2);
1731 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1732 map = isl_map_read_from_str(ctx, str);
1733 map2 = isl_map_copy(map);
1734 map = isl_map_lexmin(map);
1735 assert(isl_map_is_equal(map, map2));
1736 isl_map_free(map);
1737 isl_map_free(map2);
1739 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1740 map = isl_map_read_from_str(ctx, str);
1741 map = isl_map_lexmin(map);
1742 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1743 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1744 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1745 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1746 map2 = isl_map_read_from_str(ctx, str);
1747 assert(isl_map_is_equal(map, map2));
1748 isl_map_free(map);
1749 isl_map_free(map2);
1751 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1752 " 8i' <= i and 8i' >= -7 + i }";
1753 bmap = isl_basic_map_read_from_str(ctx, str);
1754 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1755 map2 = isl_map_from_pw_multi_aff(pma);
1756 map = isl_map_from_basic_map(bmap);
1757 assert(isl_map_is_equal(map, map2));
1758 isl_map_free(map);
1759 isl_map_free(map2);
1761 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1762 map = isl_map_read_from_str(ctx, str);
1763 map = isl_map_lexmin(map);
1764 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1765 map2 = isl_map_read_from_str(ctx, str);
1766 assert(isl_map_is_equal(map, map2));
1767 isl_map_free(map);
1768 isl_map_free(map2);
1770 /* Check that empty pieces are properly combined. */
1771 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1772 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1773 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1774 map = isl_map_read_from_str(ctx, str);
1775 map = isl_map_lexmin(map);
1776 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1777 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1778 "x >= 4 }";
1779 map2 = isl_map_read_from_str(ctx, str);
1780 assert(isl_map_is_equal(map, map2));
1781 isl_map_free(map);
1782 isl_map_free(map2);
1784 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1785 " 8i' <= i and 8i' >= -7 + i }";
1786 set = isl_set_read_from_str(ctx, str);
1787 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1788 set2 = isl_set_from_pw_multi_aff(pma);
1789 equal = isl_set_is_equal(set, set2);
1790 isl_set_free(set);
1791 isl_set_free(set2);
1792 if (equal < 0)
1793 return -1;
1794 if (!equal)
1795 isl_die(ctx, isl_error_unknown,
1796 "unexpected difference between set and "
1797 "piecewise affine expression", return -1);
1799 return 0;
1802 /* Check that isl_set_min_val and isl_set_max_val compute the correct
1803 * result on non-convex inputs.
1805 static int test_min(struct isl_ctx *ctx)
1807 isl_set *set;
1808 isl_aff *aff;
1809 isl_val *val;
1810 int min_ok, max_ok;
1812 set = isl_set_read_from_str(ctx, "{ [-1]; [1] }");
1813 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x] }");
1814 val = isl_set_min_val(set, aff);
1815 min_ok = isl_val_is_negone(val);
1816 isl_val_free(val);
1817 val = isl_set_max_val(set, aff);
1818 max_ok = isl_val_is_one(val);
1819 isl_val_free(val);
1820 isl_aff_free(aff);
1821 isl_set_free(set);
1823 if (min_ok < 0 || max_ok < 0)
1824 return -1;
1825 if (!min_ok)
1826 isl_die(ctx, isl_error_unknown,
1827 "unexpected minimum", return -1);
1828 if (!max_ok)
1829 isl_die(ctx, isl_error_unknown,
1830 "unexpected maximum", return -1);
1832 return 0;
1835 struct must_may {
1836 isl_map *must;
1837 isl_map *may;
1840 static int collect_must_may(__isl_take isl_map *dep, int must,
1841 void *dep_user, void *user)
1843 struct must_may *mm = (struct must_may *)user;
1845 if (must)
1846 mm->must = isl_map_union(mm->must, dep);
1847 else
1848 mm->may = isl_map_union(mm->may, dep);
1850 return 0;
1853 static int common_space(void *first, void *second)
1855 int depth = *(int *)first;
1856 return 2 * depth;
1859 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1861 isl_map *map2;
1862 int equal;
1864 if (!map)
1865 return -1;
1867 map2 = isl_map_read_from_str(map->ctx, str);
1868 equal = isl_map_is_equal(map, map2);
1869 isl_map_free(map2);
1871 return equal;
1874 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1876 int equal;
1878 equal = map_is_equal(map, str);
1879 if (equal < 0)
1880 return -1;
1881 if (!equal)
1882 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1883 "result not as expected", return -1);
1884 return 0;
1887 void test_dep(struct isl_ctx *ctx)
1889 const char *str;
1890 isl_space *dim;
1891 isl_map *map;
1892 isl_access_info *ai;
1893 isl_flow *flow;
1894 int depth;
1895 struct must_may mm;
1897 depth = 3;
1899 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1900 map = isl_map_read_from_str(ctx, str);
1901 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1903 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1904 map = isl_map_read_from_str(ctx, str);
1905 ai = isl_access_info_add_source(ai, map, 1, &depth);
1907 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1908 map = isl_map_read_from_str(ctx, str);
1909 ai = isl_access_info_add_source(ai, map, 1, &depth);
1911 flow = isl_access_info_compute_flow(ai);
1912 dim = isl_space_alloc(ctx, 0, 3, 3);
1913 mm.must = isl_map_empty(isl_space_copy(dim));
1914 mm.may = isl_map_empty(dim);
1916 isl_flow_foreach(flow, collect_must_may, &mm);
1918 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1919 " [1,10,0] -> [2,5,0] }";
1920 assert(map_is_equal(mm.must, str));
1921 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1922 assert(map_is_equal(mm.may, str));
1924 isl_map_free(mm.must);
1925 isl_map_free(mm.may);
1926 isl_flow_free(flow);
1929 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1930 map = isl_map_read_from_str(ctx, str);
1931 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1933 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1934 map = isl_map_read_from_str(ctx, str);
1935 ai = isl_access_info_add_source(ai, map, 1, &depth);
1937 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1938 map = isl_map_read_from_str(ctx, str);
1939 ai = isl_access_info_add_source(ai, map, 0, &depth);
1941 flow = isl_access_info_compute_flow(ai);
1942 dim = isl_space_alloc(ctx, 0, 3, 3);
1943 mm.must = isl_map_empty(isl_space_copy(dim));
1944 mm.may = isl_map_empty(dim);
1946 isl_flow_foreach(flow, collect_must_may, &mm);
1948 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1949 assert(map_is_equal(mm.must, str));
1950 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1951 assert(map_is_equal(mm.may, str));
1953 isl_map_free(mm.must);
1954 isl_map_free(mm.may);
1955 isl_flow_free(flow);
1958 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1959 map = isl_map_read_from_str(ctx, str);
1960 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1962 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1963 map = isl_map_read_from_str(ctx, str);
1964 ai = isl_access_info_add_source(ai, map, 0, &depth);
1966 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1967 map = isl_map_read_from_str(ctx, str);
1968 ai = isl_access_info_add_source(ai, map, 0, &depth);
1970 flow = isl_access_info_compute_flow(ai);
1971 dim = isl_space_alloc(ctx, 0, 3, 3);
1972 mm.must = isl_map_empty(isl_space_copy(dim));
1973 mm.may = isl_map_empty(dim);
1975 isl_flow_foreach(flow, collect_must_may, &mm);
1977 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1978 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1979 assert(map_is_equal(mm.may, str));
1980 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1981 assert(map_is_equal(mm.must, str));
1983 isl_map_free(mm.must);
1984 isl_map_free(mm.may);
1985 isl_flow_free(flow);
1988 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1989 map = isl_map_read_from_str(ctx, str);
1990 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1992 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1993 map = isl_map_read_from_str(ctx, str);
1994 ai = isl_access_info_add_source(ai, map, 0, &depth);
1996 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1997 map = isl_map_read_from_str(ctx, str);
1998 ai = isl_access_info_add_source(ai, map, 0, &depth);
2000 flow = isl_access_info_compute_flow(ai);
2001 dim = isl_space_alloc(ctx, 0, 3, 3);
2002 mm.must = isl_map_empty(isl_space_copy(dim));
2003 mm.may = isl_map_empty(dim);
2005 isl_flow_foreach(flow, collect_must_may, &mm);
2007 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2008 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2009 assert(map_is_equal(mm.may, str));
2010 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2011 assert(map_is_equal(mm.must, str));
2013 isl_map_free(mm.must);
2014 isl_map_free(mm.may);
2015 isl_flow_free(flow);
2018 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2019 map = isl_map_read_from_str(ctx, str);
2020 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2022 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2023 map = isl_map_read_from_str(ctx, str);
2024 ai = isl_access_info_add_source(ai, map, 0, &depth);
2026 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2027 map = isl_map_read_from_str(ctx, str);
2028 ai = isl_access_info_add_source(ai, map, 0, &depth);
2030 flow = isl_access_info_compute_flow(ai);
2031 dim = isl_space_alloc(ctx, 0, 3, 3);
2032 mm.must = isl_map_empty(isl_space_copy(dim));
2033 mm.may = isl_map_empty(dim);
2035 isl_flow_foreach(flow, collect_must_may, &mm);
2037 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2038 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2039 assert(map_is_equal(mm.may, str));
2040 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2041 assert(map_is_equal(mm.must, str));
2043 isl_map_free(mm.must);
2044 isl_map_free(mm.may);
2045 isl_flow_free(flow);
2048 depth = 5;
2050 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2051 map = isl_map_read_from_str(ctx, str);
2052 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2054 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2055 map = isl_map_read_from_str(ctx, str);
2056 ai = isl_access_info_add_source(ai, map, 1, &depth);
2058 flow = isl_access_info_compute_flow(ai);
2059 dim = isl_space_alloc(ctx, 0, 5, 5);
2060 mm.must = isl_map_empty(isl_space_copy(dim));
2061 mm.may = isl_map_empty(dim);
2063 isl_flow_foreach(flow, collect_must_may, &mm);
2065 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2066 assert(map_is_equal(mm.must, str));
2067 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2068 assert(map_is_equal(mm.may, str));
2070 isl_map_free(mm.must);
2071 isl_map_free(mm.may);
2072 isl_flow_free(flow);
2075 /* Check that the dependence analysis proceeds without errors.
2076 * Earlier versions of isl would break down during the analysis
2077 * due to the use of the wrong spaces.
2079 static int test_flow(isl_ctx *ctx)
2081 const char *str;
2082 isl_union_map *access, *schedule;
2083 isl_union_map *must_dep, *may_dep;
2084 int r;
2086 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2087 access = isl_union_map_read_from_str(ctx, str);
2088 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2089 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2090 "S2[] -> [1,0,0,0]; "
2091 "S3[] -> [-1,0,0,0] }";
2092 schedule = isl_union_map_read_from_str(ctx, str);
2093 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2094 isl_union_map_copy(access), schedule,
2095 &must_dep, &may_dep, NULL, NULL);
2096 isl_union_map_free(may_dep);
2097 isl_union_map_free(must_dep);
2099 return r;
2102 struct {
2103 const char *map;
2104 int sv;
2105 } sv_tests[] = {
2106 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2107 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2108 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2109 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2110 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2111 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2112 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2113 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2114 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2117 int test_sv(isl_ctx *ctx)
2119 isl_union_map *umap;
2120 int i;
2121 int sv;
2123 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2124 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2125 sv = isl_union_map_is_single_valued(umap);
2126 isl_union_map_free(umap);
2127 if (sv < 0)
2128 return -1;
2129 if (sv_tests[i].sv && !sv)
2130 isl_die(ctx, isl_error_internal,
2131 "map not detected as single valued", return -1);
2132 if (!sv_tests[i].sv && sv)
2133 isl_die(ctx, isl_error_internal,
2134 "map detected as single valued", return -1);
2137 return 0;
2140 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
2142 isl_map *map;
2144 map = isl_map_read_from_str(ctx, str);
2145 if (bijective)
2146 assert(isl_map_is_bijective(map));
2147 else
2148 assert(!isl_map_is_bijective(map));
2149 isl_map_free(map);
2152 void test_bijective(struct isl_ctx *ctx)
2154 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
2155 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
2156 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
2157 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
2158 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
2159 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
2160 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
2161 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
2162 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
2163 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
2164 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
2165 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
2166 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
2169 /* Inputs for isl_pw_qpolynomial_gist tests.
2170 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2172 struct {
2173 const char *pwqp;
2174 const char *set;
2175 const char *gist;
2176 } pwqp_gist_tests[] = {
2177 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2178 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2179 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2180 "{ [i] -> -1/2 + 1/2 * i }" },
2181 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2184 static int test_pwqp(struct isl_ctx *ctx)
2186 int i;
2187 const char *str;
2188 isl_set *set;
2189 isl_pw_qpolynomial *pwqp1, *pwqp2;
2190 int equal;
2192 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2193 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2195 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2196 isl_dim_in, 1, 1);
2198 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2199 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2201 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2203 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2205 isl_pw_qpolynomial_free(pwqp1);
2207 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2208 str = pwqp_gist_tests[i].pwqp;
2209 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2210 str = pwqp_gist_tests[i].set;
2211 set = isl_set_read_from_str(ctx, str);
2212 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2213 str = pwqp_gist_tests[i].gist;
2214 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2215 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2216 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2217 isl_pw_qpolynomial_free(pwqp1);
2219 if (equal < 0)
2220 return -1;
2221 if (!equal)
2222 isl_die(ctx, isl_error_unknown,
2223 "unexpected result", return -1);
2226 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2227 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2228 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2229 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2231 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2233 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2235 isl_pw_qpolynomial_free(pwqp1);
2237 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2238 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2239 str = "{ [x] -> x }";
2240 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2242 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2244 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2246 isl_pw_qpolynomial_free(pwqp1);
2248 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2249 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2250 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2251 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2252 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2253 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2254 isl_pw_qpolynomial_free(pwqp1);
2256 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2257 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2258 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2259 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2260 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2261 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2262 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2263 isl_pw_qpolynomial_free(pwqp1);
2264 isl_pw_qpolynomial_free(pwqp2);
2265 if (equal < 0)
2266 return -1;
2267 if (!equal)
2268 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2270 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2271 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2272 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2273 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2274 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2275 isl_val_one(ctx));
2276 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2277 isl_pw_qpolynomial_free(pwqp1);
2278 isl_pw_qpolynomial_free(pwqp2);
2279 if (equal < 0)
2280 return -1;
2281 if (!equal)
2282 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2284 return 0;
2287 void test_split_periods(isl_ctx *ctx)
2289 const char *str;
2290 isl_pw_qpolynomial *pwqp;
2292 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2293 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2294 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2295 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2297 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2298 assert(pwqp);
2300 isl_pw_qpolynomial_free(pwqp);
2303 void test_union(isl_ctx *ctx)
2305 const char *str;
2306 isl_union_set *uset1, *uset2;
2307 isl_union_map *umap1, *umap2;
2309 str = "{ [i] : 0 <= i <= 1 }";
2310 uset1 = isl_union_set_read_from_str(ctx, str);
2311 str = "{ [1] -> [0] }";
2312 umap1 = isl_union_map_read_from_str(ctx, str);
2314 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2315 assert(isl_union_map_is_equal(umap1, umap2));
2317 isl_union_map_free(umap1);
2318 isl_union_map_free(umap2);
2320 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2321 umap1 = isl_union_map_read_from_str(ctx, str);
2322 str = "{ A[i]; B[i] }";
2323 uset1 = isl_union_set_read_from_str(ctx, str);
2325 uset2 = isl_union_map_domain(umap1);
2327 assert(isl_union_set_is_equal(uset1, uset2));
2329 isl_union_set_free(uset1);
2330 isl_union_set_free(uset2);
2333 void test_bound(isl_ctx *ctx)
2335 const char *str;
2336 isl_pw_qpolynomial *pwqp;
2337 isl_pw_qpolynomial_fold *pwf;
2339 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2340 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2341 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2342 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
2343 isl_pw_qpolynomial_fold_free(pwf);
2345 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2346 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2347 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2348 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
2349 isl_pw_qpolynomial_fold_free(pwf);
2352 void test_lift(isl_ctx *ctx)
2354 const char *str;
2355 isl_basic_map *bmap;
2356 isl_basic_set *bset;
2358 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2359 bset = isl_basic_set_read_from_str(ctx, str);
2360 bset = isl_basic_set_lift(bset);
2361 bmap = isl_basic_map_from_range(bset);
2362 bset = isl_basic_map_domain(bmap);
2363 isl_basic_set_free(bset);
2366 struct {
2367 const char *set1;
2368 const char *set2;
2369 int subset;
2370 } subset_tests[] = {
2371 { "{ [112, 0] }",
2372 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2373 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2374 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2375 { "{ [65] }",
2376 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2377 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2378 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2379 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2380 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2381 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2382 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2383 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2384 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2385 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2386 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2387 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2388 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2389 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
2390 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
2391 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
2392 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
2393 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
2394 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
2395 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
2396 "4e0 <= -57 + i0 + i1)) or "
2397 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
2398 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
2399 "4e0 >= -61 + i0 + i1)) or "
2400 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
2403 static int test_subset(isl_ctx *ctx)
2405 int i;
2406 isl_set *set1, *set2;
2407 int subset;
2409 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2410 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2411 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2412 subset = isl_set_is_subset(set1, set2);
2413 isl_set_free(set1);
2414 isl_set_free(set2);
2415 if (subset < 0)
2416 return -1;
2417 if (subset != subset_tests[i].subset)
2418 isl_die(ctx, isl_error_unknown,
2419 "incorrect subset result", return -1);
2422 return 0;
2425 struct {
2426 const char *minuend;
2427 const char *subtrahend;
2428 const char *difference;
2429 } subtract_domain_tests[] = {
2430 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2431 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2432 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2435 static int test_subtract(isl_ctx *ctx)
2437 int i;
2438 isl_union_map *umap1, *umap2;
2439 isl_union_set *uset;
2440 int equal;
2442 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2443 umap1 = isl_union_map_read_from_str(ctx,
2444 subtract_domain_tests[i].minuend);
2445 uset = isl_union_set_read_from_str(ctx,
2446 subtract_domain_tests[i].subtrahend);
2447 umap2 = isl_union_map_read_from_str(ctx,
2448 subtract_domain_tests[i].difference);
2449 umap1 = isl_union_map_subtract_domain(umap1, uset);
2450 equal = isl_union_map_is_equal(umap1, umap2);
2451 isl_union_map_free(umap1);
2452 isl_union_map_free(umap2);
2453 if (equal < 0)
2454 return -1;
2455 if (!equal)
2456 isl_die(ctx, isl_error_unknown,
2457 "incorrect subtract domain result", return -1);
2460 return 0;
2463 int test_factorize(isl_ctx *ctx)
2465 const char *str;
2466 isl_basic_set *bset;
2467 isl_factorizer *f;
2469 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2470 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2471 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2472 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2473 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2474 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2475 "3i5 >= -2i0 - i2 + 3i4 }";
2476 bset = isl_basic_set_read_from_str(ctx, str);
2477 f = isl_basic_set_factorizer(bset);
2478 isl_basic_set_free(bset);
2479 isl_factorizer_free(f);
2480 if (!f)
2481 isl_die(ctx, isl_error_unknown,
2482 "failed to construct factorizer", return -1);
2484 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2485 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2486 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2487 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2488 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2489 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2490 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2491 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2492 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2493 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2494 bset = isl_basic_set_read_from_str(ctx, str);
2495 f = isl_basic_set_factorizer(bset);
2496 isl_basic_set_free(bset);
2497 isl_factorizer_free(f);
2498 if (!f)
2499 isl_die(ctx, isl_error_unknown,
2500 "failed to construct factorizer", return -1);
2502 return 0;
2505 static int check_injective(__isl_take isl_map *map, void *user)
2507 int *injective = user;
2509 *injective = isl_map_is_injective(map);
2510 isl_map_free(map);
2512 if (*injective < 0 || !*injective)
2513 return -1;
2515 return 0;
2518 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2519 const char *r, const char *s, int tilable, int parallel)
2521 int i;
2522 isl_union_set *D;
2523 isl_union_map *W, *R, *S;
2524 isl_union_map *empty;
2525 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2526 isl_union_map *validity, *proximity, *coincidence;
2527 isl_union_map *schedule;
2528 isl_union_map *test;
2529 isl_union_set *delta;
2530 isl_union_set *domain;
2531 isl_set *delta_set;
2532 isl_set *slice;
2533 isl_set *origin;
2534 isl_schedule_constraints *sc;
2535 isl_schedule *sched;
2536 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2538 D = isl_union_set_read_from_str(ctx, d);
2539 W = isl_union_map_read_from_str(ctx, w);
2540 R = isl_union_map_read_from_str(ctx, r);
2541 S = isl_union_map_read_from_str(ctx, s);
2543 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2544 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2546 empty = isl_union_map_empty(isl_union_map_get_space(S));
2547 isl_union_map_compute_flow(isl_union_map_copy(R),
2548 isl_union_map_copy(W), empty,
2549 isl_union_map_copy(S),
2550 &dep_raw, NULL, NULL, NULL);
2551 isl_union_map_compute_flow(isl_union_map_copy(W),
2552 isl_union_map_copy(W),
2553 isl_union_map_copy(R),
2554 isl_union_map_copy(S),
2555 &dep_waw, &dep_war, NULL, NULL);
2557 dep = isl_union_map_union(dep_waw, dep_war);
2558 dep = isl_union_map_union(dep, dep_raw);
2559 validity = isl_union_map_copy(dep);
2560 coincidence = isl_union_map_copy(dep);
2561 proximity = isl_union_map_copy(dep);
2563 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
2564 sc = isl_schedule_constraints_set_validity(sc, validity);
2565 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
2566 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2567 sched = isl_schedule_constraints_compute_schedule(sc);
2568 schedule = isl_schedule_get_map(sched);
2569 isl_schedule_free(sched);
2570 isl_union_map_free(W);
2571 isl_union_map_free(R);
2572 isl_union_map_free(S);
2574 is_injection = 1;
2575 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2577 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2578 is_complete = isl_union_set_is_subset(D, domain);
2579 isl_union_set_free(D);
2580 isl_union_set_free(domain);
2582 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2583 test = isl_union_map_apply_range(test, dep);
2584 test = isl_union_map_apply_range(test, schedule);
2586 delta = isl_union_map_deltas(test);
2587 if (isl_union_set_n_set(delta) == 0) {
2588 is_tilable = 1;
2589 is_parallel = 1;
2590 is_nonneg = 1;
2591 isl_union_set_free(delta);
2592 } else {
2593 delta_set = isl_set_from_union_set(delta);
2595 slice = isl_set_universe(isl_set_get_space(delta_set));
2596 for (i = 0; i < tilable; ++i)
2597 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2598 is_tilable = isl_set_is_subset(delta_set, slice);
2599 isl_set_free(slice);
2601 slice = isl_set_universe(isl_set_get_space(delta_set));
2602 for (i = 0; i < parallel; ++i)
2603 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2604 is_parallel = isl_set_is_subset(delta_set, slice);
2605 isl_set_free(slice);
2607 origin = isl_set_universe(isl_set_get_space(delta_set));
2608 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2609 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2611 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2612 delta_set = isl_set_lexmin(delta_set);
2614 is_nonneg = isl_set_is_equal(delta_set, origin);
2616 isl_set_free(origin);
2617 isl_set_free(delta_set);
2620 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2621 is_injection < 0 || is_complete < 0)
2622 return -1;
2623 if (!is_complete)
2624 isl_die(ctx, isl_error_unknown,
2625 "generated schedule incomplete", return -1);
2626 if (!is_injection)
2627 isl_die(ctx, isl_error_unknown,
2628 "generated schedule not injective on each statement",
2629 return -1);
2630 if (!is_nonneg)
2631 isl_die(ctx, isl_error_unknown,
2632 "negative dependences in generated schedule",
2633 return -1);
2634 if (!is_tilable)
2635 isl_die(ctx, isl_error_unknown,
2636 "generated schedule not as tilable as expected",
2637 return -1);
2638 if (!is_parallel)
2639 isl_die(ctx, isl_error_unknown,
2640 "generated schedule not as parallel as expected",
2641 return -1);
2643 return 0;
2646 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2647 const char *domain, const char *validity, const char *proximity)
2649 isl_union_set *dom;
2650 isl_union_map *dep;
2651 isl_union_map *prox;
2652 isl_schedule_constraints *sc;
2653 isl_schedule *schedule;
2654 isl_union_map *sched;
2656 dom = isl_union_set_read_from_str(ctx, domain);
2657 dep = isl_union_map_read_from_str(ctx, validity);
2658 prox = isl_union_map_read_from_str(ctx, proximity);
2659 sc = isl_schedule_constraints_on_domain(dom);
2660 sc = isl_schedule_constraints_set_validity(sc, dep);
2661 sc = isl_schedule_constraints_set_proximity(sc, prox);
2662 schedule = isl_schedule_constraints_compute_schedule(sc);
2663 sched = isl_schedule_get_map(schedule);
2664 isl_schedule_free(schedule);
2666 return sched;
2669 /* Check that a schedule can be constructed on the given domain
2670 * with the given validity and proximity constraints.
2672 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2673 const char *validity, const char *proximity)
2675 isl_union_map *sched;
2677 sched = compute_schedule(ctx, domain, validity, proximity);
2678 if (!sched)
2679 return -1;
2681 isl_union_map_free(sched);
2682 return 0;
2685 int test_special_schedule(isl_ctx *ctx, const char *domain,
2686 const char *validity, const char *proximity, const char *expected_sched)
2688 isl_union_map *sched1, *sched2;
2689 int equal;
2691 sched1 = compute_schedule(ctx, domain, validity, proximity);
2692 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2694 equal = isl_union_map_is_equal(sched1, sched2);
2695 isl_union_map_free(sched1);
2696 isl_union_map_free(sched2);
2698 if (equal < 0)
2699 return -1;
2700 if (!equal)
2701 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2702 return -1);
2704 return 0;
2707 /* Check that the schedule map is properly padded, even after being
2708 * reconstructed from the band forest.
2710 static int test_padded_schedule(isl_ctx *ctx)
2712 const char *str;
2713 isl_union_set *D;
2714 isl_union_map *validity, *proximity;
2715 isl_schedule_constraints *sc;
2716 isl_schedule *sched;
2717 isl_union_map *map1, *map2;
2718 isl_band_list *list;
2719 int equal;
2721 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2722 D = isl_union_set_read_from_str(ctx, str);
2723 validity = isl_union_map_empty(isl_union_set_get_space(D));
2724 proximity = isl_union_map_copy(validity);
2725 sc = isl_schedule_constraints_on_domain(D);
2726 sc = isl_schedule_constraints_set_validity(sc, validity);
2727 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2728 sched = isl_schedule_constraints_compute_schedule(sc);
2729 map1 = isl_schedule_get_map(sched);
2730 list = isl_schedule_get_band_forest(sched);
2731 isl_band_list_free(list);
2732 map2 = isl_schedule_get_map(sched);
2733 isl_schedule_free(sched);
2734 equal = isl_union_map_is_equal(map1, map2);
2735 isl_union_map_free(map1);
2736 isl_union_map_free(map2);
2738 if (equal < 0)
2739 return -1;
2740 if (!equal)
2741 isl_die(ctx, isl_error_unknown,
2742 "reconstructed schedule map not the same as original",
2743 return -1);
2745 return 0;
2748 /* Input for testing of schedule construction based on
2749 * conditional constraints.
2751 * domain is the iteration domain
2752 * flow are the flow dependences, which determine the validity and
2753 * proximity constraints
2754 * condition are the conditions on the conditional validity constraints
2755 * conditional_validity are the conditional validity constraints
2756 * outer_band_n is the expected number of members in the outer band
2758 struct {
2759 const char *domain;
2760 const char *flow;
2761 const char *condition;
2762 const char *conditional_validity;
2763 int outer_band_n;
2764 } live_range_tests[] = {
2765 /* Contrived example that illustrates that we need to keep
2766 * track of tagged condition dependences and
2767 * tagged conditional validity dependences
2768 * in isl_sched_edge separately.
2769 * In particular, the conditional validity constraints on A
2770 * cannot be satisfied,
2771 * but they can be ignored because there are no corresponding
2772 * condition constraints. However, we do have an additional
2773 * conditional validity constraint that maps to the same
2774 * dependence relation
2775 * as the condition constraint on B. If we did not make a distinction
2776 * between tagged condition and tagged conditional validity
2777 * dependences, then we
2778 * could end up treating this shared dependence as an condition
2779 * constraint on A, forcing a localization of the conditions,
2780 * which is impossible.
2782 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
2783 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
2784 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
2785 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2786 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2787 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
2790 /* TACO 2013 Fig. 7 */
2791 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2792 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2793 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2794 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
2795 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
2796 "0 <= i < n and 0 <= j < n - 1 }",
2797 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
2798 "0 <= i < n and 0 <= j < j' < n;"
2799 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
2800 "0 <= i < i' < n and 0 <= j,j' < n;"
2801 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
2802 "0 <= i,j,j' < n and j < j' }",
2805 /* TACO 2013 Fig. 7, without tags */
2806 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2807 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2808 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2809 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2810 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2811 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
2812 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
2813 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
2816 /* TACO 2013 Fig. 12 */
2817 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
2818 "S3[i,3] : 0 <= i <= 1 }",
2819 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
2820 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
2821 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
2822 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
2823 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2824 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
2825 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2826 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
2827 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
2828 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
2829 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
2834 /* Test schedule construction based on conditional constraints.
2835 * In particular, check the number of members in the outer band
2836 * as an indication of whether tiling is possible or not.
2838 static int test_conditional_schedule_constraints(isl_ctx *ctx)
2840 int i;
2841 isl_union_set *domain;
2842 isl_union_map *condition;
2843 isl_union_map *flow;
2844 isl_union_map *validity;
2845 isl_schedule_constraints *sc;
2846 isl_schedule *schedule;
2847 isl_band_list *list;
2848 isl_band *band;
2849 int n_member;
2851 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
2852 domain = isl_union_set_read_from_str(ctx,
2853 live_range_tests[i].domain);
2854 flow = isl_union_map_read_from_str(ctx,
2855 live_range_tests[i].flow);
2856 condition = isl_union_map_read_from_str(ctx,
2857 live_range_tests[i].condition);
2858 validity = isl_union_map_read_from_str(ctx,
2859 live_range_tests[i].conditional_validity);
2860 sc = isl_schedule_constraints_on_domain(domain);
2861 sc = isl_schedule_constraints_set_validity(sc,
2862 isl_union_map_copy(flow));
2863 sc = isl_schedule_constraints_set_proximity(sc, flow);
2864 sc = isl_schedule_constraints_set_conditional_validity(sc,
2865 condition, validity);
2866 schedule = isl_schedule_constraints_compute_schedule(sc);
2867 list = isl_schedule_get_band_forest(schedule);
2868 band = isl_band_list_get_band(list, 0);
2869 n_member = isl_band_n_member(band);
2870 isl_band_free(band);
2871 isl_band_list_free(list);
2872 isl_schedule_free(schedule);
2874 if (!schedule)
2875 return -1;
2876 if (n_member != live_range_tests[i].outer_band_n)
2877 isl_die(ctx, isl_error_unknown,
2878 "unexpected number of members in outer band",
2879 return -1);
2881 return 0;
2884 int test_schedule(isl_ctx *ctx)
2886 const char *D, *W, *R, *V, *P, *S;
2888 /* Handle resulting schedule with zero bands. */
2889 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2890 return -1;
2892 /* Jacobi */
2893 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2894 W = "{ S1[t,i] -> a[t,i] }";
2895 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2896 "S1[t,i] -> a[t-1,i+1] }";
2897 S = "{ S1[t,i] -> [t,i] }";
2898 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2899 return -1;
2901 /* Fig. 5 of CC2008 */
2902 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2903 "j <= -1 + N }";
2904 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2905 "j >= 2 and j <= -1 + N }";
2906 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2907 "j >= 2 and j <= -1 + N; "
2908 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2909 "j >= 2 and j <= -1 + N }";
2910 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2911 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2912 return -1;
2914 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2915 W = "{ S1[i] -> a[i] }";
2916 R = "{ S2[i] -> a[i+1] }";
2917 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2918 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2919 return -1;
2921 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2922 W = "{ S1[i] -> a[i] }";
2923 R = "{ S2[i] -> a[9-i] }";
2924 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2925 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2926 return -1;
2928 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2929 W = "{ S1[i] -> a[i] }";
2930 R = "[N] -> { S2[i] -> a[N-1-i] }";
2931 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2932 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2933 return -1;
2935 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2936 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2937 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2938 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2939 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2940 return -1;
2942 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2943 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2944 R = "{ S2[i,j] -> a[i-1,j] }";
2945 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2946 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2947 return -1;
2949 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2950 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2951 R = "{ S2[i,j] -> a[i,j-1] }";
2952 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2953 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2954 return -1;
2956 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2957 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2958 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2959 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2960 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2961 "S_0[] -> [0, 0, 0] }";
2962 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2963 return -1;
2964 ctx->opt->schedule_parametric = 0;
2965 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2966 return -1;
2967 ctx->opt->schedule_parametric = 1;
2969 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2970 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2971 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2972 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2973 "S4[i] -> a[i,N] }";
2974 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2975 "S4[i] -> [4,i,0] }";
2976 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2977 return -1;
2979 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2980 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2981 "j <= N }";
2982 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2983 "j <= N; "
2984 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2985 "j <= N }";
2986 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2987 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2988 return -1;
2990 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2991 " S_2[t] : t >= 0 and t <= -1 + N; "
2992 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2993 "i <= -1 + N }";
2994 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2995 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2996 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2997 "i >= 0 and i <= -1 + N }";
2998 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2999 "i >= 0 and i <= -1 + N; "
3000 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
3001 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
3002 " S_0[t] -> [0, t, 0] }";
3004 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3005 return -1;
3006 ctx->opt->schedule_parametric = 0;
3007 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3008 return -1;
3009 ctx->opt->schedule_parametric = 1;
3011 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
3012 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
3013 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
3014 return -1;
3016 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
3017 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
3018 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
3019 "j >= 0 and j <= -1 + N; "
3020 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3021 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
3022 "j >= 0 and j <= -1 + N; "
3023 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3024 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
3025 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3026 return -1;
3028 D = "{ S_0[i] : i >= 0 }";
3029 W = "{ S_0[i] -> a[i] : i >= 0 }";
3030 R = "{ S_0[i] -> a[0] : i >= 0 }";
3031 S = "{ S_0[i] -> [0, i, 0] }";
3032 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3033 return -1;
3035 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
3036 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
3037 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
3038 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
3039 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3040 return -1;
3042 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
3043 "k <= -1 + n and k >= 0 }";
3044 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
3045 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
3046 "k <= -1 + n and k >= 0; "
3047 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
3048 "k <= -1 + n and k >= 0; "
3049 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
3050 "k <= -1 + n and k >= 0 }";
3051 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
3052 ctx->opt->schedule_outer_coincidence = 1;
3053 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3054 return -1;
3055 ctx->opt->schedule_outer_coincidence = 0;
3057 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3058 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3059 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3060 "Stmt_for_body24[i0, i1, 1, 0]:"
3061 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3062 "Stmt_for_body7[i0, i1, i2]:"
3063 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3064 "i2 <= 7 }";
3066 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3067 "Stmt_for_body24[1, i1, i2, i3]:"
3068 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3069 "i2 >= 1;"
3070 "Stmt_for_body24[0, i1, i2, i3] -> "
3071 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3072 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3073 "i3 >= 0;"
3074 "Stmt_for_body24[0, i1, i2, i3] ->"
3075 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3076 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3077 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3078 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3079 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3080 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3081 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3082 "i1 <= 6 and i1 >= 0;"
3083 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3084 "Stmt_for_body7[i0, i1, i2] -> "
3085 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3086 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3087 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3088 "Stmt_for_body7[i0, i1, i2] -> "
3089 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3090 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3091 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3092 P = V;
3093 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3094 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3095 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3097 if (test_special_schedule(ctx, D, V, P, S) < 0)
3098 return -1;
3100 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3101 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3102 "j >= 1 and j <= 7;"
3103 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3104 "j >= 1 and j <= 8 }";
3105 P = "{ }";
3106 S = "{ S_0[i, j] -> [i + j, j] }";
3107 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3108 if (test_special_schedule(ctx, D, V, P, S) < 0)
3109 return -1;
3110 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3112 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3113 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3114 "j >= 0 and j <= -1 + i }";
3115 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3116 "i <= -1 + N and j >= 0;"
3117 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3118 "i <= -2 + N }";
3119 P = "{ }";
3120 S = "{ S_0[i, j] -> [i, j] }";
3121 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3122 if (test_special_schedule(ctx, D, V, P, S) < 0)
3123 return -1;
3124 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3126 /* Test both algorithms on a case with only proximity dependences. */
3127 D = "{ S[i,j] : 0 <= i <= 10 }";
3128 V = "{ }";
3129 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3130 S = "{ S[i, j] -> [j, i] }";
3131 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3132 if (test_special_schedule(ctx, D, V, P, S) < 0)
3133 return -1;
3134 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3135 if (test_special_schedule(ctx, D, V, P, S) < 0)
3136 return -1;
3138 D = "{ A[a]; B[] }";
3139 V = "{}";
3140 P = "{ A[a] -> B[] }";
3141 if (test_has_schedule(ctx, D, V, P) < 0)
3142 return -1;
3144 if (test_padded_schedule(ctx) < 0)
3145 return -1;
3147 /* Check that check for progress is not confused by rational
3148 * solution.
3150 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3151 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3152 "i0 <= -2 + N; "
3153 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3154 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3155 P = "{}";
3156 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3157 if (test_has_schedule(ctx, D, V, P) < 0)
3158 return -1;
3159 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3161 if (test_conditional_schedule_constraints(ctx) < 0)
3162 return -1;
3164 return 0;
3167 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3169 isl_union_map *umap;
3170 int test;
3172 umap = isl_union_map_read_from_str(ctx, str);
3173 test = isl_union_map_plain_is_injective(umap);
3174 isl_union_map_free(umap);
3175 if (test < 0)
3176 return -1;
3177 if (test == injective)
3178 return 0;
3179 if (injective)
3180 isl_die(ctx, isl_error_unknown,
3181 "map not detected as injective", return -1);
3182 else
3183 isl_die(ctx, isl_error_unknown,
3184 "map detected as injective", return -1);
3187 int test_injective(isl_ctx *ctx)
3189 const char *str;
3191 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
3192 return -1;
3193 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
3194 return -1;
3195 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
3196 return -1;
3197 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
3198 return -1;
3199 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
3200 return -1;
3201 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
3202 return -1;
3203 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
3204 return -1;
3205 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
3206 return -1;
3208 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
3209 if (test_plain_injective(ctx, str, 1))
3210 return -1;
3211 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
3212 if (test_plain_injective(ctx, str, 0))
3213 return -1;
3215 return 0;
3218 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
3220 isl_aff *aff2;
3221 int equal;
3223 if (!aff)
3224 return -1;
3226 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
3227 equal = isl_aff_plain_is_equal(aff, aff2);
3228 isl_aff_free(aff2);
3230 return equal;
3233 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
3235 int equal;
3237 equal = aff_plain_is_equal(aff, str);
3238 if (equal < 0)
3239 return -1;
3240 if (!equal)
3241 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
3242 "result not as expected", return -1);
3243 return 0;
3246 struct {
3247 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3248 __isl_take isl_aff *aff2);
3249 } aff_bin_op[] = {
3250 ['+'] = { &isl_aff_add },
3251 ['-'] = { &isl_aff_sub },
3252 ['*'] = { &isl_aff_mul },
3253 ['/'] = { &isl_aff_div },
3256 struct {
3257 const char *arg1;
3258 unsigned char op;
3259 const char *arg2;
3260 const char *res;
3261 } aff_bin_tests[] = {
3262 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
3263 "{ [i] -> [2i] }" },
3264 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
3265 "{ [i] -> [0] }" },
3266 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
3267 "{ [i] -> [2i] }" },
3268 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
3269 "{ [i] -> [2i] }" },
3270 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
3271 "{ [i] -> [i/2] }" },
3272 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
3273 "{ [i] -> [i] }" },
3274 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
3275 "{ [i] -> [NaN] }" },
3276 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
3277 "{ [i] -> [NaN] }" },
3278 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
3279 "{ [i] -> [NaN] }" },
3280 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
3281 "{ [i] -> [NaN] }" },
3282 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
3283 "{ [i] -> [NaN] }" },
3284 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
3285 "{ [i] -> [NaN] }" },
3286 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
3287 "{ [i] -> [NaN] }" },
3288 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
3289 "{ [i] -> [NaN] }" },
3290 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
3291 "{ [i] -> [NaN] }" },
3292 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
3293 "{ [i] -> [NaN] }" },
3294 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
3295 "{ [i] -> [NaN] }" },
3296 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
3297 "{ [i] -> [NaN] }" },
3300 /* Perform some basic tests of binary operations on isl_aff objects.
3302 static int test_bin_aff(isl_ctx *ctx)
3304 int i;
3305 isl_aff *aff1, *aff2, *res;
3306 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3307 __isl_take isl_aff *aff2);
3308 int ok;
3310 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
3311 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
3312 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
3313 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
3314 fn = aff_bin_op[aff_bin_tests[i].op].fn;
3315 aff1 = fn(aff1, aff2);
3316 if (isl_aff_is_nan(res))
3317 ok = isl_aff_is_nan(aff1);
3318 else
3319 ok = isl_aff_plain_is_equal(aff1, res);
3320 isl_aff_free(aff1);
3321 isl_aff_free(res);
3322 if (ok < 0)
3323 return -1;
3324 if (!ok)
3325 isl_die(ctx, isl_error_unknown,
3326 "unexpected result", return -1);
3329 return 0;
3332 int test_aff(isl_ctx *ctx)
3334 const char *str;
3335 isl_set *set;
3336 isl_space *space;
3337 isl_local_space *ls;
3338 isl_aff *aff;
3339 int zero, equal;
3341 if (test_bin_aff(ctx) < 0)
3342 return -1;
3344 space = isl_space_set_alloc(ctx, 0, 1);
3345 ls = isl_local_space_from_space(space);
3346 aff = isl_aff_zero_on_domain(ls);
3348 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3349 aff = isl_aff_scale_down_ui(aff, 3);
3350 aff = isl_aff_floor(aff);
3351 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3352 aff = isl_aff_scale_down_ui(aff, 2);
3353 aff = isl_aff_floor(aff);
3354 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3356 str = "{ [10] }";
3357 set = isl_set_read_from_str(ctx, str);
3358 aff = isl_aff_gist(aff, set);
3360 aff = isl_aff_add_constant_si(aff, -16);
3361 zero = isl_aff_plain_is_zero(aff);
3362 isl_aff_free(aff);
3364 if (zero < 0)
3365 return -1;
3366 if (!zero)
3367 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3369 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
3370 aff = isl_aff_scale_down_ui(aff, 64);
3371 aff = isl_aff_floor(aff);
3372 equal = aff_check_plain_equal(aff, "{ [-1] }");
3373 isl_aff_free(aff);
3374 if (equal < 0)
3375 return -1;
3377 return 0;
3380 int test_dim_max(isl_ctx *ctx)
3382 int equal;
3383 const char *str;
3384 isl_set *set1, *set2;
3385 isl_set *set;
3386 isl_map *map;
3387 isl_pw_aff *pwaff;
3389 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
3390 set = isl_set_read_from_str(ctx, str);
3391 pwaff = isl_set_dim_max(set, 0);
3392 set1 = isl_set_from_pw_aff(pwaff);
3393 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
3394 set2 = isl_set_read_from_str(ctx, str);
3395 equal = isl_set_is_equal(set1, set2);
3396 isl_set_free(set1);
3397 isl_set_free(set2);
3398 if (equal < 0)
3399 return -1;
3400 if (!equal)
3401 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3403 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
3404 set = isl_set_read_from_str(ctx, str);
3405 pwaff = isl_set_dim_max(set, 0);
3406 set1 = isl_set_from_pw_aff(pwaff);
3407 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3408 set2 = isl_set_read_from_str(ctx, str);
3409 equal = isl_set_is_equal(set1, set2);
3410 isl_set_free(set1);
3411 isl_set_free(set2);
3412 if (equal < 0)
3413 return -1;
3414 if (!equal)
3415 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3417 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
3418 set = isl_set_read_from_str(ctx, str);
3419 pwaff = isl_set_dim_max(set, 0);
3420 set1 = isl_set_from_pw_aff(pwaff);
3421 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3422 set2 = isl_set_read_from_str(ctx, str);
3423 equal = isl_set_is_equal(set1, set2);
3424 isl_set_free(set1);
3425 isl_set_free(set2);
3426 if (equal < 0)
3427 return -1;
3428 if (!equal)
3429 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3431 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
3432 "0 <= i < N and 0 <= j < M }";
3433 map = isl_map_read_from_str(ctx, str);
3434 set = isl_map_range(map);
3436 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
3437 set1 = isl_set_from_pw_aff(pwaff);
3438 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
3439 set2 = isl_set_read_from_str(ctx, str);
3440 equal = isl_set_is_equal(set1, set2);
3441 isl_set_free(set1);
3442 isl_set_free(set2);
3444 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3445 set1 = isl_set_from_pw_aff(pwaff);
3446 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3447 set2 = isl_set_read_from_str(ctx, str);
3448 if (equal >= 0 && equal)
3449 equal = isl_set_is_equal(set1, set2);
3450 isl_set_free(set1);
3451 isl_set_free(set2);
3453 isl_set_free(set);
3455 if (equal < 0)
3456 return -1;
3457 if (!equal)
3458 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3460 /* Check that solutions are properly merged. */
3461 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3462 "c <= -1 + n - 4a - 2b and c >= -2b and "
3463 "4a >= -4 + n and c >= 0 }";
3464 set = isl_set_read_from_str(ctx, str);
3465 pwaff = isl_set_dim_min(set, 2);
3466 set1 = isl_set_from_pw_aff(pwaff);
3467 str = "[n] -> { [(0)] : n >= 1 }";
3468 set2 = isl_set_read_from_str(ctx, str);
3469 equal = isl_set_is_equal(set1, set2);
3470 isl_set_free(set1);
3471 isl_set_free(set2);
3473 if (equal < 0)
3474 return -1;
3475 if (!equal)
3476 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3478 /* Check that empty solution lie in the right space. */
3479 str = "[n] -> { [t,a] : 1 = 0 }";
3480 set = isl_set_read_from_str(ctx, str);
3481 pwaff = isl_set_dim_max(set, 0);
3482 set1 = isl_set_from_pw_aff(pwaff);
3483 str = "[n] -> { [t] : 1 = 0 }";
3484 set2 = isl_set_read_from_str(ctx, str);
3485 equal = isl_set_is_equal(set1, set2);
3486 isl_set_free(set1);
3487 isl_set_free(set2);
3489 if (equal < 0)
3490 return -1;
3491 if (!equal)
3492 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3494 return 0;
3497 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
3499 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
3500 const char *str)
3502 isl_ctx *ctx;
3503 isl_pw_multi_aff *pma2;
3504 int equal;
3506 if (!pma)
3507 return -1;
3509 ctx = isl_pw_multi_aff_get_ctx(pma);
3510 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3511 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
3512 isl_pw_multi_aff_free(pma2);
3514 return equal;
3517 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
3518 * represented by "str".
3520 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
3521 const char *str)
3523 int equal;
3525 equal = pw_multi_aff_plain_is_equal(pma, str);
3526 if (equal < 0)
3527 return -1;
3528 if (!equal)
3529 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
3530 "result not as expected", return -1);
3531 return 0;
3534 /* Basic test for isl_pw_multi_aff_product.
3536 * Check that multiple pieces are properly handled.
3538 static int test_product_pma(isl_ctx *ctx)
3540 int equal;
3541 const char *str;
3542 isl_pw_multi_aff *pma1, *pma2;
3544 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
3545 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3546 str = "{ C[] -> D[] }";
3547 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3548 pma1 = isl_pw_multi_aff_product(pma1, pma2);
3549 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
3550 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
3551 equal = pw_multi_aff_check_plain_equal(pma1, str);
3552 isl_pw_multi_aff_free(pma1);
3553 if (equal < 0)
3554 return -1;
3556 return 0;
3559 int test_product(isl_ctx *ctx)
3561 const char *str;
3562 isl_set *set;
3563 isl_union_set *uset1, *uset2;
3564 int ok;
3566 str = "{ A[i] }";
3567 set = isl_set_read_from_str(ctx, str);
3568 set = isl_set_product(set, isl_set_copy(set));
3569 ok = isl_set_is_wrapping(set);
3570 isl_set_free(set);
3571 if (ok < 0)
3572 return -1;
3573 if (!ok)
3574 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3576 str = "{ [] }";
3577 uset1 = isl_union_set_read_from_str(ctx, str);
3578 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3579 str = "{ [[] -> []] }";
3580 uset2 = isl_union_set_read_from_str(ctx, str);
3581 ok = isl_union_set_is_equal(uset1, uset2);
3582 isl_union_set_free(uset1);
3583 isl_union_set_free(uset2);
3584 if (ok < 0)
3585 return -1;
3586 if (!ok)
3587 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3589 if (test_product_pma(ctx) < 0)
3590 return -1;
3592 return 0;
3595 /* Check that two sets are not considered disjoint just because
3596 * they have a different set of (named) parameters.
3598 static int test_disjoint(isl_ctx *ctx)
3600 const char *str;
3601 isl_set *set, *set2;
3602 int disjoint;
3604 str = "[n] -> { [[]->[]] }";
3605 set = isl_set_read_from_str(ctx, str);
3606 str = "{ [[]->[]] }";
3607 set2 = isl_set_read_from_str(ctx, str);
3608 disjoint = isl_set_is_disjoint(set, set2);
3609 isl_set_free(set);
3610 isl_set_free(set2);
3611 if (disjoint < 0)
3612 return -1;
3613 if (disjoint)
3614 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3616 return 0;
3619 int test_equal(isl_ctx *ctx)
3621 const char *str;
3622 isl_set *set, *set2;
3623 int equal;
3625 str = "{ S_6[i] }";
3626 set = isl_set_read_from_str(ctx, str);
3627 str = "{ S_7[i] }";
3628 set2 = isl_set_read_from_str(ctx, str);
3629 equal = isl_set_is_equal(set, set2);
3630 isl_set_free(set);
3631 isl_set_free(set2);
3632 if (equal < 0)
3633 return -1;
3634 if (equal)
3635 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3637 return 0;
3640 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3641 enum isl_dim_type type, unsigned pos, int fixed)
3643 int test;
3645 test = isl_map_plain_is_fixed(map, type, pos, NULL);
3646 isl_map_free(map);
3647 if (test < 0)
3648 return -1;
3649 if (test == fixed)
3650 return 0;
3651 if (fixed)
3652 isl_die(ctx, isl_error_unknown,
3653 "map not detected as fixed", return -1);
3654 else
3655 isl_die(ctx, isl_error_unknown,
3656 "map detected as fixed", return -1);
3659 int test_fixed(isl_ctx *ctx)
3661 const char *str;
3662 isl_map *map;
3664 str = "{ [i] -> [i] }";
3665 map = isl_map_read_from_str(ctx, str);
3666 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3667 return -1;
3668 str = "{ [i] -> [1] }";
3669 map = isl_map_read_from_str(ctx, str);
3670 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3671 return -1;
3672 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3673 map = isl_map_read_from_str(ctx, str);
3674 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3675 return -1;
3676 map = isl_map_read_from_str(ctx, str);
3677 map = isl_map_neg(map);
3678 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3679 return -1;
3681 return 0;
3684 struct isl_vertices_test_data {
3685 const char *set;
3686 int n;
3687 const char *vertex[2];
3688 } vertices_tests[] = {
3689 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
3690 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
3691 { "{ A[t, i] : t = 14 and i = 1 }",
3692 1, { "{ A[14, 1] }" } },
3695 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
3697 static int find_vertex(__isl_take isl_vertex *vertex, void *user)
3699 struct isl_vertices_test_data *data = user;
3700 isl_ctx *ctx;
3701 isl_multi_aff *ma;
3702 isl_basic_set *bset;
3703 isl_pw_multi_aff *pma;
3704 int i;
3705 int equal;
3707 ctx = isl_vertex_get_ctx(vertex);
3708 bset = isl_vertex_get_domain(vertex);
3709 ma = isl_vertex_get_expr(vertex);
3710 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
3712 for (i = 0; i < data->n; ++i) {
3713 isl_pw_multi_aff *pma_i;
3715 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
3716 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
3717 isl_pw_multi_aff_free(pma_i);
3719 if (equal < 0 || equal)
3720 break;
3723 isl_pw_multi_aff_free(pma);
3724 isl_vertex_free(vertex);
3726 if (equal < 0)
3727 return -1;
3729 return equal ? 0 : - 1;
3732 int test_vertices(isl_ctx *ctx)
3734 int i;
3736 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
3737 isl_basic_set *bset;
3738 isl_vertices *vertices;
3739 int ok = 1;
3740 int n;
3742 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
3743 vertices = isl_basic_set_compute_vertices(bset);
3744 n = isl_vertices_get_n_vertices(vertices);
3745 if (vertices_tests[i].n != n)
3746 ok = 0;
3747 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
3748 &vertices_tests[i]) < 0)
3749 ok = 0;
3750 isl_vertices_free(vertices);
3751 isl_basic_set_free(bset);
3753 if (!vertices)
3754 return -1;
3755 if (!ok)
3756 isl_die(ctx, isl_error_unknown, "unexpected vertices",
3757 return -1);
3760 return 0;
3763 int test_union_pw(isl_ctx *ctx)
3765 int equal;
3766 const char *str;
3767 isl_union_set *uset;
3768 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
3770 str = "{ [x] -> x^2 }";
3771 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
3772 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
3773 uset = isl_union_pw_qpolynomial_domain(upwqp1);
3774 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
3775 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
3776 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
3777 isl_union_pw_qpolynomial_free(upwqp1);
3778 isl_union_pw_qpolynomial_free(upwqp2);
3779 if (equal < 0)
3780 return -1;
3781 if (!equal)
3782 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3784 return 0;
3787 int test_output(isl_ctx *ctx)
3789 char *s;
3790 const char *str;
3791 isl_pw_aff *pa;
3792 isl_printer *p;
3793 int equal;
3795 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
3796 pa = isl_pw_aff_read_from_str(ctx, str);
3798 p = isl_printer_to_str(ctx);
3799 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
3800 p = isl_printer_print_pw_aff(p, pa);
3801 s = isl_printer_get_str(p);
3802 isl_printer_free(p);
3803 isl_pw_aff_free(pa);
3804 if (!s)
3805 equal = -1;
3806 else
3807 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
3808 free(s);
3809 if (equal < 0)
3810 return -1;
3811 if (!equal)
3812 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3814 return 0;
3817 int test_sample(isl_ctx *ctx)
3819 const char *str;
3820 isl_basic_set *bset1, *bset2;
3821 int empty, subset;
3823 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3824 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3825 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3826 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3827 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3828 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3829 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3830 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3831 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3832 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3833 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3834 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3835 "d - 1073741821e and "
3836 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3837 "3j >= 1 - a + b + 2e and "
3838 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3839 "3i <= 4 - a + 4b - e and "
3840 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3841 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3842 "c <= -1 + a and 3i >= -2 - a + 3e and "
3843 "1073741822e <= 1073741823 - a + 1073741822b + c and "
3844 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3845 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3846 "1073741823e >= 1 + 1073741823b - d and "
3847 "3i >= 1073741823b + c - 1073741823e - f and "
3848 "3i >= 1 + 2b + e + 3g }";
3849 bset1 = isl_basic_set_read_from_str(ctx, str);
3850 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3851 empty = isl_basic_set_is_empty(bset2);
3852 subset = isl_basic_set_is_subset(bset2, bset1);
3853 isl_basic_set_free(bset1);
3854 isl_basic_set_free(bset2);
3855 if (empty < 0 || subset < 0)
3856 return -1;
3857 if (empty)
3858 isl_die(ctx, isl_error_unknown, "point not found", return -1);
3859 if (!subset)
3860 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
3862 return 0;
3865 int test_fixed_power(isl_ctx *ctx)
3867 const char *str;
3868 isl_map *map;
3869 isl_int exp;
3870 int equal;
3872 isl_int_init(exp);
3873 str = "{ [i] -> [i + 1] }";
3874 map = isl_map_read_from_str(ctx, str);
3875 isl_int_set_si(exp, 23);
3876 map = isl_map_fixed_power(map, exp);
3877 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
3878 isl_int_clear(exp);
3879 isl_map_free(map);
3880 if (equal < 0)
3881 return -1;
3883 return 0;
3886 int test_slice(isl_ctx *ctx)
3888 const char *str;
3889 isl_map *map;
3890 int equal;
3892 str = "{ [i] -> [j] }";
3893 map = isl_map_read_from_str(ctx, str);
3894 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
3895 equal = map_check_equal(map, "{ [i] -> [i] }");
3896 isl_map_free(map);
3897 if (equal < 0)
3898 return -1;
3900 str = "{ [i] -> [j] }";
3901 map = isl_map_read_from_str(ctx, str);
3902 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
3903 equal = map_check_equal(map, "{ [i] -> [j] }");
3904 isl_map_free(map);
3905 if (equal < 0)
3906 return -1;
3908 str = "{ [i] -> [j] }";
3909 map = isl_map_read_from_str(ctx, str);
3910 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
3911 equal = map_check_equal(map, "{ [i] -> [-i] }");
3912 isl_map_free(map);
3913 if (equal < 0)
3914 return -1;
3916 str = "{ [i] -> [j] }";
3917 map = isl_map_read_from_str(ctx, str);
3918 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
3919 equal = map_check_equal(map, "{ [0] -> [j] }");
3920 isl_map_free(map);
3921 if (equal < 0)
3922 return -1;
3924 str = "{ [i] -> [j] }";
3925 map = isl_map_read_from_str(ctx, str);
3926 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3927 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
3928 isl_map_free(map);
3929 if (equal < 0)
3930 return -1;
3932 str = "{ [i] -> [j] }";
3933 map = isl_map_read_from_str(ctx, str);
3934 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
3935 equal = map_check_equal(map, "{ [i] -> [j] : false }");
3936 isl_map_free(map);
3937 if (equal < 0)
3938 return -1;
3940 return 0;
3943 int test_eliminate(isl_ctx *ctx)
3945 const char *str;
3946 isl_map *map;
3947 int equal;
3949 str = "{ [i] -> [j] : i = 2j }";
3950 map = isl_map_read_from_str(ctx, str);
3951 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
3952 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
3953 isl_map_free(map);
3954 if (equal < 0)
3955 return -1;
3957 return 0;
3960 /* Check that isl_set_dim_residue_class detects that the values of j
3961 * in the set below are all odd and that it does not detect any spurious
3962 * strides.
3964 static int test_residue_class(isl_ctx *ctx)
3966 const char *str;
3967 isl_set *set;
3968 isl_int m, r;
3969 int res;
3971 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3972 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3973 set = isl_set_read_from_str(ctx, str);
3974 isl_int_init(m);
3975 isl_int_init(r);
3976 res = isl_set_dim_residue_class(set, 1, &m, &r);
3977 if (res >= 0 &&
3978 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
3979 isl_die(ctx, isl_error_unknown, "incorrect residue class",
3980 res = -1);
3981 isl_int_clear(r);
3982 isl_int_clear(m);
3983 isl_set_free(set);
3985 return res;
3988 int test_align_parameters(isl_ctx *ctx)
3990 const char *str;
3991 isl_space *space;
3992 isl_multi_aff *ma1, *ma2;
3993 int equal;
3995 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
3996 ma1 = isl_multi_aff_read_from_str(ctx, str);
3998 space = isl_space_params_alloc(ctx, 1);
3999 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
4000 ma1 = isl_multi_aff_align_params(ma1, space);
4002 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
4003 ma2 = isl_multi_aff_read_from_str(ctx, str);
4005 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4007 isl_multi_aff_free(ma1);
4008 isl_multi_aff_free(ma2);
4010 if (equal < 0)
4011 return -1;
4012 if (!equal)
4013 isl_die(ctx, isl_error_unknown,
4014 "result not as expected", return -1);
4016 return 0;
4019 static int test_list(isl_ctx *ctx)
4021 isl_id *a, *b, *c, *d, *id;
4022 isl_id_list *list;
4023 int ok;
4025 a = isl_id_alloc(ctx, "a", NULL);
4026 b = isl_id_alloc(ctx, "b", NULL);
4027 c = isl_id_alloc(ctx, "c", NULL);
4028 d = isl_id_alloc(ctx, "d", NULL);
4030 list = isl_id_list_alloc(ctx, 4);
4031 list = isl_id_list_add(list, a);
4032 list = isl_id_list_add(list, b);
4033 list = isl_id_list_add(list, c);
4034 list = isl_id_list_add(list, d);
4035 list = isl_id_list_drop(list, 1, 1);
4037 if (isl_id_list_n_id(list) != 3) {
4038 isl_id_list_free(list);
4039 isl_die(ctx, isl_error_unknown,
4040 "unexpected number of elements in list", return -1);
4043 id = isl_id_list_get_id(list, 0);
4044 ok = id == a;
4045 isl_id_free(id);
4046 id = isl_id_list_get_id(list, 1);
4047 ok = ok && id == c;
4048 isl_id_free(id);
4049 id = isl_id_list_get_id(list, 2);
4050 ok = ok && id == d;
4051 isl_id_free(id);
4053 isl_id_list_free(list);
4055 if (!ok)
4056 isl_die(ctx, isl_error_unknown,
4057 "unexpected elements in list", return -1);
4059 return 0;
4062 const char *set_conversion_tests[] = {
4063 "[N] -> { [i] : N - 1 <= 2 i <= N }",
4064 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
4065 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4066 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4067 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
4070 /* Check that converting from isl_set to isl_pw_multi_aff and back
4071 * to isl_set produces the original isl_set.
4073 static int test_set_conversion(isl_ctx *ctx)
4075 int i;
4076 const char *str;
4077 isl_set *set1, *set2;
4078 isl_pw_multi_aff *pma;
4079 int equal;
4081 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
4082 str = set_conversion_tests[i];
4083 set1 = isl_set_read_from_str(ctx, str);
4084 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
4085 set2 = isl_set_from_pw_multi_aff(pma);
4086 equal = isl_set_is_equal(set1, set2);
4087 isl_set_free(set1);
4088 isl_set_free(set2);
4090 if (equal < 0)
4091 return -1;
4092 if (!equal)
4093 isl_die(ctx, isl_error_unknown, "bad conversion",
4094 return -1);
4097 return 0;
4100 /* Check that converting from isl_map to isl_pw_multi_aff and back
4101 * to isl_map produces the original isl_map.
4103 static int test_map_conversion(isl_ctx *ctx)
4105 const char *str;
4106 isl_map *map1, *map2;
4107 isl_pw_multi_aff *pma;
4108 int equal;
4110 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
4111 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
4112 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
4113 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
4114 "9e <= -2 - 2a) }";
4115 map1 = isl_map_read_from_str(ctx, str);
4116 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
4117 map2 = isl_map_from_pw_multi_aff(pma);
4118 equal = isl_map_is_equal(map1, map2);
4119 isl_map_free(map1);
4120 isl_map_free(map2);
4122 if (equal < 0)
4123 return -1;
4124 if (!equal)
4125 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
4127 return 0;
4130 static int test_conversion(isl_ctx *ctx)
4132 if (test_set_conversion(ctx) < 0)
4133 return -1;
4134 if (test_map_conversion(ctx) < 0)
4135 return -1;
4136 return 0;
4139 /* Check that isl_basic_map_curry does not modify input.
4141 static int test_curry(isl_ctx *ctx)
4143 const char *str;
4144 isl_basic_map *bmap1, *bmap2;
4145 int equal;
4147 str = "{ [A[] -> B[]] -> C[] }";
4148 bmap1 = isl_basic_map_read_from_str(ctx, str);
4149 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
4150 equal = isl_basic_map_is_equal(bmap1, bmap2);
4151 isl_basic_map_free(bmap1);
4152 isl_basic_map_free(bmap2);
4154 if (equal < 0)
4155 return -1;
4156 if (equal)
4157 isl_die(ctx, isl_error_unknown,
4158 "curried map should not be equal to original",
4159 return -1);
4161 return 0;
4164 struct {
4165 const char *set;
4166 const char *ma;
4167 const char *res;
4168 } preimage_tests[] = {
4169 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
4170 "{ A[j,i] -> B[i,j] }",
4171 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
4172 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4173 "{ A[a,b] -> B[a/2,b/6] }",
4174 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
4175 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4176 "{ A[a,b] -> B[a/2,b/6] }",
4177 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
4178 "exists i,j : a = 2 i and b = 6 j }" },
4179 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
4180 "[n] -> { : 0 <= n <= 100 }" },
4181 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4182 "{ A[a] -> B[2a] }",
4183 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
4184 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4185 "{ A[a] -> B[([a/2])] }",
4186 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
4187 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
4188 "{ A[a] -> B[a,a,a/3] }",
4189 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
4190 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
4191 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
4194 static int test_preimage_basic_set(isl_ctx *ctx)
4196 int i;
4197 isl_basic_set *bset1, *bset2;
4198 isl_multi_aff *ma;
4199 int equal;
4201 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
4202 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
4203 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
4204 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
4205 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
4206 equal = isl_basic_set_is_equal(bset1, bset2);
4207 isl_basic_set_free(bset1);
4208 isl_basic_set_free(bset2);
4209 if (equal < 0)
4210 return -1;
4211 if (!equal)
4212 isl_die(ctx, isl_error_unknown, "bad preimage",
4213 return -1);
4216 return 0;
4219 struct {
4220 const char *map;
4221 const char *ma;
4222 const char *res;
4223 } preimage_domain_tests[] = {
4224 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
4225 "{ A[j,i] -> B[i,j] }",
4226 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
4227 { "{ B[i] -> C[i]; D[i] -> E[i] }",
4228 "{ A[i] -> B[i + 1] }",
4229 "{ A[i] -> C[i + 1] }" },
4230 { "{ B[i] -> C[i]; B[i] -> E[i] }",
4231 "{ A[i] -> B[i + 1] }",
4232 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
4233 { "{ B[i] -> C[([i/2])] }",
4234 "{ A[i] -> B[2i] }",
4235 "{ A[i] -> C[i] }" },
4236 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
4237 "{ A[i] -> B[([i/5]), ([i/7])] }",
4238 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
4239 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
4240 "[N] -> { A[] -> B[([N/5])] }",
4241 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
4242 { "{ B[i] -> C[i] : exists a : i = 5 a }",
4243 "{ A[i] -> B[2i] }",
4244 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
4245 { "{ B[i] -> C[i] : exists a : i = 2 a; "
4246 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
4247 "{ A[i] -> B[2i] }",
4248 "{ A[i] -> C[2i] }" },
4251 static int test_preimage_union_map(isl_ctx *ctx)
4253 int i;
4254 isl_union_map *umap1, *umap2;
4255 isl_multi_aff *ma;
4256 int equal;
4258 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
4259 umap1 = isl_union_map_read_from_str(ctx,
4260 preimage_domain_tests[i].map);
4261 ma = isl_multi_aff_read_from_str(ctx,
4262 preimage_domain_tests[i].ma);
4263 umap2 = isl_union_map_read_from_str(ctx,
4264 preimage_domain_tests[i].res);
4265 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
4266 equal = isl_union_map_is_equal(umap1, umap2);
4267 isl_union_map_free(umap1);
4268 isl_union_map_free(umap2);
4269 if (equal < 0)
4270 return -1;
4271 if (!equal)
4272 isl_die(ctx, isl_error_unknown, "bad preimage",
4273 return -1);
4276 return 0;
4279 static int test_preimage(isl_ctx *ctx)
4281 if (test_preimage_basic_set(ctx) < 0)
4282 return -1;
4283 if (test_preimage_union_map(ctx) < 0)
4284 return -1;
4286 return 0;
4289 struct {
4290 const char *ma1;
4291 const char *ma;
4292 const char *res;
4293 } pullback_tests[] = {
4294 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
4295 "{ A[a,b] -> C[b + 2a] }" },
4296 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
4297 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
4298 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
4299 "{ A[a] -> C[(a)/6] }" },
4300 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
4301 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
4302 "{ A[a] -> C[(2a)/3] }" },
4303 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
4304 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
4305 "{ A[i,j] -> C[i + j, i + j] }"},
4306 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
4307 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
4308 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
4311 static int test_pullback(isl_ctx *ctx)
4313 int i;
4314 isl_multi_aff *ma1, *ma2;
4315 isl_multi_aff *ma;
4316 int equal;
4318 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
4319 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
4320 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
4321 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
4322 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
4323 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4324 isl_multi_aff_free(ma1);
4325 isl_multi_aff_free(ma2);
4326 if (equal < 0)
4327 return -1;
4328 if (!equal)
4329 isl_die(ctx, isl_error_unknown, "bad pullback",
4330 return -1);
4333 return 0;
4336 /* Check that negation is printed correctly.
4338 static int test_ast(isl_ctx *ctx)
4340 isl_ast_expr *expr, *expr1, *expr2, *expr3;
4341 char *str;
4342 int ok;
4344 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4345 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4346 expr = isl_ast_expr_add(expr1, expr2);
4347 expr = isl_ast_expr_neg(expr);
4348 str = isl_ast_expr_to_str(expr);
4349 ok = str ? !strcmp(str, "-(A + B)") : -1;
4350 free(str);
4351 isl_ast_expr_free(expr);
4353 if (ok < 0)
4354 return -1;
4355 if (!ok)
4356 isl_die(ctx, isl_error_unknown,
4357 "isl_ast_expr printed incorrectly", return -1);
4359 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4360 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4361 expr = isl_ast_expr_add(expr1, expr2);
4362 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
4363 expr = isl_ast_expr_sub(expr3, expr);
4364 str = isl_ast_expr_to_str(expr);
4365 ok = str ? !strcmp(str, "C - (A + B)") : -1;
4366 free(str);
4367 isl_ast_expr_free(expr);
4369 if (ok < 0)
4370 return -1;
4371 if (!ok)
4372 isl_die(ctx, isl_error_unknown,
4373 "isl_ast_expr printed incorrectly", return -1);
4375 return 0;
4378 /* Check that isl_ast_build_expr_from_set returns a valid expression
4379 * for an empty set. Note that isl_ast_build_expr_from_set getting
4380 * called on an empty set probably indicates a bug in the caller.
4382 static int test_ast_build(isl_ctx *ctx)
4384 isl_set *set;
4385 isl_ast_build *build;
4386 isl_ast_expr *expr;
4388 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4389 build = isl_ast_build_from_context(set);
4391 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
4392 expr = isl_ast_build_expr_from_set(build, set);
4394 isl_ast_expr_free(expr);
4395 isl_ast_build_free(build);
4397 if (!expr)
4398 return -1;
4400 return 0;
4403 /* Internal data structure for before_for and after_for callbacks.
4405 * depth is the current depth
4406 * before is the number of times before_for has been called
4407 * after is the number of times after_for has been called
4409 struct isl_test_codegen_data {
4410 int depth;
4411 int before;
4412 int after;
4415 /* This function is called before each for loop in the AST generated
4416 * from test_ast_gen1.
4418 * Increment the number of calls and the depth.
4419 * Check that the space returned by isl_ast_build_get_schedule_space
4420 * matches the target space of the schedule returned by
4421 * isl_ast_build_get_schedule.
4422 * Return an isl_id that is checked by the corresponding call
4423 * to after_for.
4425 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
4426 void *user)
4428 struct isl_test_codegen_data *data = user;
4429 isl_ctx *ctx;
4430 isl_space *space;
4431 isl_union_map *schedule;
4432 isl_union_set *uset;
4433 isl_set *set;
4434 int empty;
4435 char name[] = "d0";
4437 ctx = isl_ast_build_get_ctx(build);
4439 if (data->before >= 3)
4440 isl_die(ctx, isl_error_unknown,
4441 "unexpected number of for nodes", return NULL);
4442 if (data->depth >= 2)
4443 isl_die(ctx, isl_error_unknown,
4444 "unexpected depth", return NULL);
4446 snprintf(name, sizeof(name), "d%d", data->depth);
4447 data->before++;
4448 data->depth++;
4450 schedule = isl_ast_build_get_schedule(build);
4451 uset = isl_union_map_range(schedule);
4452 if (!uset)
4453 return NULL;
4454 if (isl_union_set_n_set(uset) != 1) {
4455 isl_union_set_free(uset);
4456 isl_die(ctx, isl_error_unknown,
4457 "expecting single range space", return NULL);
4460 space = isl_ast_build_get_schedule_space(build);
4461 set = isl_union_set_extract_set(uset, space);
4462 isl_union_set_free(uset);
4463 empty = isl_set_is_empty(set);
4464 isl_set_free(set);
4466 if (empty < 0)
4467 return NULL;
4468 if (empty)
4469 isl_die(ctx, isl_error_unknown,
4470 "spaces don't match", return NULL);
4472 return isl_id_alloc(ctx, name, NULL);
4475 /* This function is called after each for loop in the AST generated
4476 * from test_ast_gen1.
4478 * Increment the number of calls and decrement the depth.
4479 * Check that the annotation attached to the node matches
4480 * the isl_id returned by the corresponding call to before_for.
4482 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
4483 __isl_keep isl_ast_build *build, void *user)
4485 struct isl_test_codegen_data *data = user;
4486 isl_id *id;
4487 const char *name;
4488 int valid;
4490 data->after++;
4491 data->depth--;
4493 if (data->after > data->before)
4494 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4495 "mismatch in number of for nodes",
4496 return isl_ast_node_free(node));
4498 id = isl_ast_node_get_annotation(node);
4499 if (!id)
4500 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4501 "missing annotation", return isl_ast_node_free(node));
4503 name = isl_id_get_name(id);
4504 valid = name && atoi(name + 1) == data->depth;
4505 isl_id_free(id);
4507 if (!valid)
4508 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4509 "wrong annotation", return isl_ast_node_free(node));
4511 return node;
4514 /* Check that the before_each_for and after_each_for callbacks
4515 * are called for each for loop in the generated code,
4516 * that they are called in the right order and that the isl_id
4517 * returned from the before_each_for callback is attached to
4518 * the isl_ast_node passed to the corresponding after_each_for call.
4520 static int test_ast_gen1(isl_ctx *ctx)
4522 const char *str;
4523 isl_set *set;
4524 isl_union_map *schedule;
4525 isl_ast_build *build;
4526 isl_ast_node *tree;
4527 struct isl_test_codegen_data data;
4529 str = "[N] -> { : N >= 10 }";
4530 set = isl_set_read_from_str(ctx, str);
4531 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
4532 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
4533 schedule = isl_union_map_read_from_str(ctx, str);
4535 data.before = 0;
4536 data.after = 0;
4537 data.depth = 0;
4538 build = isl_ast_build_from_context(set);
4539 build = isl_ast_build_set_before_each_for(build,
4540 &before_for, &data);
4541 build = isl_ast_build_set_after_each_for(build,
4542 &after_for, &data);
4543 tree = isl_ast_build_ast_from_schedule(build, schedule);
4544 isl_ast_build_free(build);
4545 if (!tree)
4546 return -1;
4548 isl_ast_node_free(tree);
4550 if (data.before != 3 || data.after != 3)
4551 isl_die(ctx, isl_error_unknown,
4552 "unexpected number of for nodes", return -1);
4554 return 0;
4557 /* Check that the AST generator handles domains that are integrally disjoint
4558 * but not ratinoally disjoint.
4560 static int test_ast_gen2(isl_ctx *ctx)
4562 const char *str;
4563 isl_set *set;
4564 isl_union_map *schedule;
4565 isl_union_map *options;
4566 isl_ast_build *build;
4567 isl_ast_node *tree;
4569 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
4570 schedule = isl_union_map_read_from_str(ctx, str);
4571 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4572 build = isl_ast_build_from_context(set);
4574 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
4575 options = isl_union_map_read_from_str(ctx, str);
4576 build = isl_ast_build_set_options(build, options);
4577 tree = isl_ast_build_ast_from_schedule(build, schedule);
4578 isl_ast_build_free(build);
4579 if (!tree)
4580 return -1;
4581 isl_ast_node_free(tree);
4583 return 0;
4586 /* Increment *user on each call.
4588 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
4589 __isl_keep isl_ast_build *build, void *user)
4591 int *n = user;
4593 (*n)++;
4595 return node;
4598 /* Test that unrolling tries to minimize the number of instances.
4599 * In particular, for the schedule given below, make sure it generates
4600 * 3 nodes (rather than 101).
4602 static int test_ast_gen3(isl_ctx *ctx)
4604 const char *str;
4605 isl_set *set;
4606 isl_union_map *schedule;
4607 isl_union_map *options;
4608 isl_ast_build *build;
4609 isl_ast_node *tree;
4610 int n_domain = 0;
4612 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
4613 schedule = isl_union_map_read_from_str(ctx, str);
4614 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4616 str = "{ [i] -> unroll[0] }";
4617 options = isl_union_map_read_from_str(ctx, str);
4619 build = isl_ast_build_from_context(set);
4620 build = isl_ast_build_set_options(build, options);
4621 build = isl_ast_build_set_at_each_domain(build,
4622 &count_domains, &n_domain);
4623 tree = isl_ast_build_ast_from_schedule(build, schedule);
4624 isl_ast_build_free(build);
4625 if (!tree)
4626 return -1;
4628 isl_ast_node_free(tree);
4630 if (n_domain != 3)
4631 isl_die(ctx, isl_error_unknown,
4632 "unexpected number of for nodes", return -1);
4634 return 0;
4637 /* Check that if the ast_build_exploit_nested_bounds options is set,
4638 * we do not get an outer if node in the generated AST,
4639 * while we do get such an outer if node if the options is not set.
4641 static int test_ast_gen4(isl_ctx *ctx)
4643 const char *str;
4644 isl_set *set;
4645 isl_union_map *schedule;
4646 isl_ast_build *build;
4647 isl_ast_node *tree;
4648 enum isl_ast_node_type type;
4649 int enb;
4651 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4652 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4654 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4656 schedule = isl_union_map_read_from_str(ctx, str);
4657 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4658 build = isl_ast_build_from_context(set);
4659 tree = isl_ast_build_ast_from_schedule(build, schedule);
4660 isl_ast_build_free(build);
4661 if (!tree)
4662 return -1;
4664 type = isl_ast_node_get_type(tree);
4665 isl_ast_node_free(tree);
4667 if (type == isl_ast_node_if)
4668 isl_die(ctx, isl_error_unknown,
4669 "not expecting if node", return -1);
4671 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
4673 schedule = isl_union_map_read_from_str(ctx, str);
4674 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4675 build = isl_ast_build_from_context(set);
4676 tree = isl_ast_build_ast_from_schedule(build, schedule);
4677 isl_ast_build_free(build);
4678 if (!tree)
4679 return -1;
4681 type = isl_ast_node_get_type(tree);
4682 isl_ast_node_free(tree);
4684 if (type != isl_ast_node_if)
4685 isl_die(ctx, isl_error_unknown,
4686 "expecting if node", return -1);
4688 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
4690 return 0;
4693 /* This function is called for each leaf in the AST generated
4694 * from test_ast_gen5.
4696 * We finalize the AST generation by extending the outer schedule
4697 * with a zero-dimensional schedule. If this results in any for loops,
4698 * then this means that we did not pass along enough information
4699 * about the outer schedule to the inner AST generation.
4701 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
4702 void *user)
4704 isl_union_map *schedule, *extra;
4705 isl_ast_node *tree;
4707 schedule = isl_ast_build_get_schedule(build);
4708 extra = isl_union_map_copy(schedule);
4709 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
4710 schedule = isl_union_map_range_product(schedule, extra);
4711 tree = isl_ast_build_ast_from_schedule(build, schedule);
4712 isl_ast_build_free(build);
4714 if (!tree)
4715 return NULL;
4717 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
4718 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
4719 "code should not contain any for loop",
4720 return isl_ast_node_free(tree));
4722 return tree;
4725 /* Check that we do not lose any information when going back and
4726 * forth between internal and external schedule.
4728 * In particular, we create an AST where we unroll the only
4729 * non-constant dimension in the schedule. We therefore do
4730 * not expect any for loops in the AST. However, older versions
4731 * of isl would not pass along enough information about the outer
4732 * schedule when performing an inner code generation from a create_leaf
4733 * callback, resulting in the inner code generation producing a for loop.
4735 static int test_ast_gen5(isl_ctx *ctx)
4737 const char *str;
4738 isl_set *set;
4739 isl_union_map *schedule, *options;
4740 isl_ast_build *build;
4741 isl_ast_node *tree;
4743 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
4744 schedule = isl_union_map_read_from_str(ctx, str);
4746 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
4747 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
4748 options = isl_union_map_read_from_str(ctx, str);
4750 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4751 build = isl_ast_build_from_context(set);
4752 build = isl_ast_build_set_options(build, options);
4753 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
4754 tree = isl_ast_build_ast_from_schedule(build, schedule);
4755 isl_ast_build_free(build);
4756 isl_ast_node_free(tree);
4757 if (!tree)
4758 return -1;
4760 return 0;
4763 static int test_ast_gen(isl_ctx *ctx)
4765 if (test_ast_gen1(ctx) < 0)
4766 return -1;
4767 if (test_ast_gen2(ctx) < 0)
4768 return -1;
4769 if (test_ast_gen3(ctx) < 0)
4770 return -1;
4771 if (test_ast_gen4(ctx) < 0)
4772 return -1;
4773 if (test_ast_gen5(ctx) < 0)
4774 return -1;
4775 return 0;
4778 /* Check if dropping output dimensions from an isl_pw_multi_aff
4779 * works properly.
4781 static int test_pw_multi_aff(isl_ctx *ctx)
4783 const char *str;
4784 isl_pw_multi_aff *pma1, *pma2;
4785 int equal;
4787 str = "{ [i,j] -> [i+j, 4i-j] }";
4788 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4789 str = "{ [i,j] -> [4i-j] }";
4790 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4792 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
4794 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
4796 isl_pw_multi_aff_free(pma1);
4797 isl_pw_multi_aff_free(pma2);
4798 if (equal < 0)
4799 return -1;
4800 if (!equal)
4801 isl_die(ctx, isl_error_unknown,
4802 "expressions not equal", return -1);
4804 return 0;
4807 /* Check that we can properly parse multi piecewise affine expressions
4808 * where the piecewise affine expressions have different domains.
4810 static int test_multi_pw_aff(isl_ctx *ctx)
4812 const char *str;
4813 isl_set *dom, *dom2;
4814 isl_multi_pw_aff *mpa1, *mpa2;
4815 isl_pw_aff *pa;
4816 int equal;
4817 int equal_domain;
4819 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
4820 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
4821 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
4822 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
4823 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
4824 str = "{ [i] -> [(i : i > 0), 2i] }";
4825 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
4827 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
4829 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
4830 dom = isl_pw_aff_domain(pa);
4831 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
4832 dom2 = isl_pw_aff_domain(pa);
4833 equal_domain = isl_set_is_equal(dom, dom2);
4835 isl_set_free(dom);
4836 isl_set_free(dom2);
4837 isl_multi_pw_aff_free(mpa1);
4838 isl_multi_pw_aff_free(mpa2);
4840 if (equal < 0)
4841 return -1;
4842 if (!equal)
4843 isl_die(ctx, isl_error_unknown,
4844 "expressions not equal", return -1);
4846 if (equal_domain < 0)
4847 return -1;
4848 if (equal_domain)
4849 isl_die(ctx, isl_error_unknown,
4850 "domains unexpectedly equal", return -1);
4852 return 0;
4855 /* This is a regression test for a bug where isl_basic_map_simplify
4856 * would end up in an infinite loop. In particular, we construct
4857 * an empty basic set that is not obviously empty.
4858 * isl_basic_set_is_empty marks the basic set as empty.
4859 * After projecting out i3, the variable can be dropped completely,
4860 * but isl_basic_map_simplify refrains from doing so if the basic set
4861 * is empty and would end up in an infinite loop if it didn't test
4862 * explicitly for empty basic maps in the outer loop.
4864 static int test_simplify(isl_ctx *ctx)
4866 const char *str;
4867 isl_basic_set *bset;
4868 int empty;
4870 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
4871 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
4872 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
4873 "i3 >= i2 }";
4874 bset = isl_basic_set_read_from_str(ctx, str);
4875 empty = isl_basic_set_is_empty(bset);
4876 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
4877 isl_basic_set_free(bset);
4878 if (!bset)
4879 return -1;
4880 if (!empty)
4881 isl_die(ctx, isl_error_unknown,
4882 "basic set should be empty", return -1);
4884 return 0;
4887 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
4888 * with gbr context would fail to disable the use of the shifted tableau
4889 * when transferring equalities for the input to the context, resulting
4890 * in invalid sample values.
4892 static int test_partial_lexmin(isl_ctx *ctx)
4894 const char *str;
4895 isl_basic_set *bset;
4896 isl_basic_map *bmap;
4897 isl_map *map;
4899 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
4900 bmap = isl_basic_map_read_from_str(ctx, str);
4901 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
4902 bset = isl_basic_set_read_from_str(ctx, str);
4903 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
4904 isl_map_free(map);
4906 if (!map)
4907 return -1;
4909 return 0;
4912 /* Check that the variable compression performed on the existentially
4913 * quantified variables inside isl_basic_set_compute_divs is not confused
4914 * by the implicit equalities among the parameters.
4916 static int test_compute_divs(isl_ctx *ctx)
4918 const char *str;
4919 isl_basic_set *bset;
4920 isl_set *set;
4922 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
4923 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
4924 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
4925 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
4926 bset = isl_basic_set_read_from_str(ctx, str);
4927 set = isl_basic_set_compute_divs(bset);
4928 isl_set_free(set);
4929 if (!set)
4930 return -1;
4932 return 0;
4935 struct {
4936 const char *set;
4937 const char *dual;
4938 } coef_tests[] = {
4939 { "{ rat: [i] : 0 <= i <= 10 }",
4940 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
4941 { "{ rat: [i] : FALSE }",
4942 "{ rat: coefficients[[cst] -> [a]] }" },
4943 { "{ rat: [i] : }",
4944 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
4947 struct {
4948 const char *set;
4949 const char *dual;
4950 } sol_tests[] = {
4951 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
4952 "{ rat: [i] : 0 <= i <= 10 }" },
4953 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
4954 "{ rat: [i] }" },
4955 { "{ rat: coefficients[[cst] -> [a]] }",
4956 "{ rat: [i] : FALSE }" },
4959 /* Test the basic functionality of isl_basic_set_coefficients and
4960 * isl_basic_set_solutions.
4962 static int test_dual(isl_ctx *ctx)
4964 int i;
4966 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
4967 int equal;
4968 isl_basic_set *bset1, *bset2;
4970 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
4971 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
4972 bset1 = isl_basic_set_coefficients(bset1);
4973 equal = isl_basic_set_is_equal(bset1, bset2);
4974 isl_basic_set_free(bset1);
4975 isl_basic_set_free(bset2);
4976 if (equal < 0)
4977 return -1;
4978 if (!equal)
4979 isl_die(ctx, isl_error_unknown,
4980 "incorrect dual", return -1);
4983 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
4984 int equal;
4985 isl_basic_set *bset1, *bset2;
4987 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
4988 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
4989 bset1 = isl_basic_set_solutions(bset1);
4990 equal = isl_basic_set_is_equal(bset1, bset2);
4991 isl_basic_set_free(bset1);
4992 isl_basic_set_free(bset2);
4993 if (equal < 0)
4994 return -1;
4995 if (!equal)
4996 isl_die(ctx, isl_error_unknown,
4997 "incorrect dual", return -1);
5000 return 0;
5003 struct {
5004 const char *name;
5005 int (*fn)(isl_ctx *ctx);
5006 } tests [] = {
5007 { "dual", &test_dual },
5008 { "dependence analysis", &test_flow },
5009 { "val", &test_val },
5010 { "compute divs", &test_compute_divs },
5011 { "partial lexmin", &test_partial_lexmin },
5012 { "simplify", &test_simplify },
5013 { "curry", &test_curry },
5014 { "piecewise multi affine expressions", &test_pw_multi_aff },
5015 { "multi piecewise affine expressions", &test_multi_pw_aff },
5016 { "conversion", &test_conversion },
5017 { "list", &test_list },
5018 { "align parameters", &test_align_parameters },
5019 { "preimage", &test_preimage },
5020 { "pullback", &test_pullback },
5021 { "AST", &test_ast },
5022 { "AST build", &test_ast_build },
5023 { "AST generation", &test_ast_gen },
5024 { "eliminate", &test_eliminate },
5025 { "residue class", &test_residue_class },
5026 { "div", &test_div },
5027 { "slice", &test_slice },
5028 { "fixed power", &test_fixed_power },
5029 { "sample", &test_sample },
5030 { "output", &test_output },
5031 { "vertices", &test_vertices },
5032 { "fixed", &test_fixed },
5033 { "equal", &test_equal },
5034 { "disjoint", &test_disjoint },
5035 { "product", &test_product },
5036 { "dim_max", &test_dim_max },
5037 { "affine", &test_aff },
5038 { "injective", &test_injective },
5039 { "schedule", &test_schedule },
5040 { "union_pw", &test_union_pw },
5041 { "parse", &test_parse },
5042 { "single-valued", &test_sv },
5043 { "affine hull", &test_affine_hull },
5044 { "coalesce", &test_coalesce },
5045 { "factorize", &test_factorize },
5046 { "subset", &test_subset },
5047 { "subtract", &test_subtract },
5048 { "lexmin", &test_lexmin },
5049 { "min", &test_min },
5050 { "gist", &test_gist },
5051 { "piecewise quasi-polynomials", &test_pwqp },
5054 int main()
5056 int i;
5057 struct isl_ctx *ctx;
5059 srcdir = getenv("srcdir");
5060 assert(srcdir);
5062 ctx = isl_ctx_alloc();
5063 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
5064 printf("%s\n", tests[i].name);
5065 if (tests[i].fn(ctx) < 0)
5066 goto error;
5068 test_lift(ctx);
5069 test_bound(ctx);
5070 test_union(ctx);
5071 test_split_periods(ctx);
5072 test_lex(ctx);
5073 test_bijective(ctx);
5074 test_dep(ctx);
5075 test_read(ctx);
5076 test_bounded(ctx);
5077 test_construction(ctx);
5078 test_dim(ctx);
5079 test_application(ctx);
5080 test_convex_hull(ctx);
5081 test_closure(ctx);
5082 isl_ctx_free(ctx);
5083 return 0;
5084 error:
5085 isl_ctx_free(ctx);
5086 return -1;