add isl_union_pw_aff_val_on_domain
[isl.git] / isl_test.c
blob0a3e079f9025e413f3890ffde5a3161c427f7d01
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>
37 #include <isl/options.h>
39 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
41 static char *srcdir;
43 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
44 char *filename;
45 int length;
46 char *pattern = "%s/test_inputs/%s.%s";
48 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
49 + strlen(suffix) + 1;
50 filename = isl_alloc_array(ctx, char, length);
52 if (!filename)
53 return NULL;
55 sprintf(filename, pattern, srcdir, name, suffix);
57 return filename;
60 void test_parse_map(isl_ctx *ctx, const char *str)
62 isl_map *map;
64 map = isl_map_read_from_str(ctx, str);
65 assert(map);
66 isl_map_free(map);
69 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
71 isl_map *map, *map2;
72 int equal;
74 map = isl_map_read_from_str(ctx, str);
75 map2 = isl_map_read_from_str(ctx, str2);
76 equal = isl_map_is_equal(map, map2);
77 isl_map_free(map);
78 isl_map_free(map2);
80 if (equal < 0)
81 return -1;
82 if (!equal)
83 isl_die(ctx, isl_error_unknown, "maps not equal",
84 return -1);
86 return 0;
89 void test_parse_pwqp(isl_ctx *ctx, const char *str)
91 isl_pw_qpolynomial *pwqp;
93 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
94 assert(pwqp);
95 isl_pw_qpolynomial_free(pwqp);
98 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
100 isl_pw_aff *pwaff;
102 pwaff = isl_pw_aff_read_from_str(ctx, str);
103 assert(pwaff);
104 isl_pw_aff_free(pwaff);
107 /* Check that we can read an isl_multi_val from "str" without errors.
109 static int test_parse_multi_val(isl_ctx *ctx, const char *str)
111 isl_multi_val *mv;
113 mv = isl_multi_val_read_from_str(ctx, str);
114 isl_multi_val_free(mv);
116 return mv ? 0 : -1;
119 int test_parse(struct isl_ctx *ctx)
121 isl_map *map, *map2;
122 const char *str, *str2;
124 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
125 return -1;
126 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
127 return -1;
128 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
129 return -1;
131 str = "{ [i] -> [-i] }";
132 map = isl_map_read_from_str(ctx, str);
133 assert(map);
134 isl_map_free(map);
136 str = "{ A[i] -> L[([i/3])] }";
137 map = isl_map_read_from_str(ctx, str);
138 assert(map);
139 isl_map_free(map);
141 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
142 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
143 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
145 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
146 str2 = "{ [x, y] : 2y >= 6 - x }";
147 if (test_parse_map_equal(ctx, str, str2) < 0)
148 return -1;
150 if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
151 "{ [x,y] : x <= y, 2*y + 3 }") < 0)
152 return -1;
153 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
154 if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
155 str) < 0)
156 return -1;
158 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
159 map = isl_map_read_from_str(ctx, str);
160 str = "{ [new, old] -> [o0, o1] : "
161 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
162 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
163 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
164 map2 = isl_map_read_from_str(ctx, str);
165 assert(isl_map_is_equal(map, map2));
166 isl_map_free(map);
167 isl_map_free(map2);
169 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
170 map = isl_map_read_from_str(ctx, str);
171 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
172 map2 = isl_map_read_from_str(ctx, str);
173 assert(isl_map_is_equal(map, map2));
174 isl_map_free(map);
175 isl_map_free(map2);
177 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
178 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
179 if (test_parse_map_equal(ctx, str, str2) < 0)
180 return -1;
182 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
183 str2 = "{ [i,j] -> [min(i,j)] }";
184 if (test_parse_map_equal(ctx, str, str2) < 0)
185 return -1;
187 str = "{ [i,j] : i != j }";
188 str2 = "{ [i,j] : i < j or i > j }";
189 if (test_parse_map_equal(ctx, str, str2) < 0)
190 return -1;
192 str = "{ [i,j] : (i+1)*2 >= j }";
193 str2 = "{ [i, j] : j <= 2 + 2i }";
194 if (test_parse_map_equal(ctx, str, str2) < 0)
195 return -1;
197 str = "{ [i] -> [i > 0 ? 4 : 5] }";
198 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
199 if (test_parse_map_equal(ctx, str, str2) < 0)
200 return -1;
202 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
203 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
204 if (test_parse_map_equal(ctx, str, str2) < 0)
205 return -1;
207 str = "{ [x] : x >= 0 }";
208 str2 = "{ [x] : x-0 >= 0 }";
209 if (test_parse_map_equal(ctx, str, str2) < 0)
210 return -1;
212 str = "{ [i] : ((i > 10)) }";
213 str2 = "{ [i] : i >= 11 }";
214 if (test_parse_map_equal(ctx, str, str2) < 0)
215 return -1;
217 str = "{ [i] -> [0] }";
218 str2 = "{ [i] -> [0 * i] }";
219 if (test_parse_map_equal(ctx, str, str2) < 0)
220 return -1;
222 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
223 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
224 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
225 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
227 if (test_parse_map_equal(ctx, "{ [a] -> [b] : (not false) }",
228 "{ [a] -> [b] : true }") < 0)
229 return -1;
231 if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
232 "{ [i] : i <= 10 }") < 0)
233 return -1;
235 if (test_parse_map_equal(ctx, "{Sym=[n] [i] : i <= n }",
236 "[n] -> { [i] : i <= n }") < 0)
237 return -1;
239 if (test_parse_map_equal(ctx, "{ [*] }", "{ [a] }") < 0)
240 return -1;
242 if (test_parse_map_equal(ctx, "{ [i] : 2*floor(i/2) = i }",
243 "{ [i] : exists a : i = 2 a }") < 0)
244 return -1;
246 if (test_parse_map_equal(ctx, "{ [a] -> [b] : a = 5 implies b = 5 }",
247 "{ [a] -> [b] : a != 5 or b = 5 }") < 0)
248 return -1;
250 if (test_parse_map_equal(ctx, "{ [a] -> [a - 1 : a > 0] }",
251 "{ [a] -> [a - 1] : a > 0 }") < 0)
252 return -1;
253 if (test_parse_map_equal(ctx,
254 "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
255 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }") < 0)
256 return -1;
257 if (test_parse_map_equal(ctx,
258 "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
259 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
260 return -1;
261 if (test_parse_map_equal(ctx,
262 "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
263 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
264 return -1;
265 if (test_parse_map_equal(ctx,
266 "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
267 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
268 return -1;
269 if (test_parse_map_equal(ctx,
270 "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
271 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
272 return -1;
274 return 0;
277 void test_read(struct isl_ctx *ctx)
279 char *filename;
280 FILE *input;
281 struct isl_basic_set *bset1, *bset2;
282 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
284 filename = get_filename(ctx, "set", "omega");
285 assert(filename);
286 input = fopen(filename, "r");
287 assert(input);
289 bset1 = isl_basic_set_read_from_file(ctx, input);
290 bset2 = isl_basic_set_read_from_str(ctx, str);
292 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
294 isl_basic_set_free(bset1);
295 isl_basic_set_free(bset2);
296 free(filename);
298 fclose(input);
301 void test_bounded(struct isl_ctx *ctx)
303 isl_set *set;
304 int bounded;
306 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
307 bounded = isl_set_is_bounded(set);
308 assert(bounded);
309 isl_set_free(set);
311 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
312 bounded = isl_set_is_bounded(set);
313 assert(!bounded);
314 isl_set_free(set);
316 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
317 bounded = isl_set_is_bounded(set);
318 assert(!bounded);
319 isl_set_free(set);
322 /* Construct the basic set { [i] : 5 <= i <= N } */
323 void test_construction(struct isl_ctx *ctx)
325 isl_int v;
326 isl_space *dim;
327 isl_local_space *ls;
328 struct isl_basic_set *bset;
329 struct isl_constraint *c;
331 isl_int_init(v);
333 dim = isl_space_set_alloc(ctx, 1, 1);
334 bset = isl_basic_set_universe(isl_space_copy(dim));
335 ls = isl_local_space_from_space(dim);
337 c = isl_inequality_alloc(isl_local_space_copy(ls));
338 isl_int_set_si(v, -1);
339 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
340 isl_int_set_si(v, 1);
341 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
342 bset = isl_basic_set_add_constraint(bset, c);
344 c = isl_inequality_alloc(isl_local_space_copy(ls));
345 isl_int_set_si(v, 1);
346 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
347 isl_int_set_si(v, -5);
348 isl_constraint_set_constant(c, v);
349 bset = isl_basic_set_add_constraint(bset, c);
351 isl_local_space_free(ls);
352 isl_basic_set_free(bset);
354 isl_int_clear(v);
357 void test_dim(struct isl_ctx *ctx)
359 const char *str;
360 isl_map *map1, *map2;
362 map1 = isl_map_read_from_str(ctx,
363 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
364 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
365 map2 = isl_map_read_from_str(ctx,
366 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
367 assert(isl_map_is_equal(map1, map2));
368 isl_map_free(map2);
370 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
371 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
372 assert(isl_map_is_equal(map1, map2));
374 isl_map_free(map1);
375 isl_map_free(map2);
377 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
378 map1 = isl_map_read_from_str(ctx, str);
379 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
380 map2 = isl_map_read_from_str(ctx, str);
381 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
382 assert(isl_map_is_equal(map1, map2));
384 isl_map_free(map1);
385 isl_map_free(map2);
388 struct {
389 __isl_give isl_val *(*op)(__isl_take isl_val *v);
390 const char *arg;
391 const char *res;
392 } val_un_tests[] = {
393 { &isl_val_neg, "0", "0" },
394 { &isl_val_abs, "0", "0" },
395 { &isl_val_2exp, "0", "1" },
396 { &isl_val_floor, "0", "0" },
397 { &isl_val_ceil, "0", "0" },
398 { &isl_val_neg, "1", "-1" },
399 { &isl_val_neg, "-1", "1" },
400 { &isl_val_neg, "1/2", "-1/2" },
401 { &isl_val_neg, "-1/2", "1/2" },
402 { &isl_val_neg, "infty", "-infty" },
403 { &isl_val_neg, "-infty", "infty" },
404 { &isl_val_neg, "NaN", "NaN" },
405 { &isl_val_abs, "1", "1" },
406 { &isl_val_abs, "-1", "1" },
407 { &isl_val_abs, "1/2", "1/2" },
408 { &isl_val_abs, "-1/2", "1/2" },
409 { &isl_val_abs, "infty", "infty" },
410 { &isl_val_abs, "-infty", "infty" },
411 { &isl_val_abs, "NaN", "NaN" },
412 { &isl_val_floor, "1", "1" },
413 { &isl_val_floor, "-1", "-1" },
414 { &isl_val_floor, "1/2", "0" },
415 { &isl_val_floor, "-1/2", "-1" },
416 { &isl_val_floor, "infty", "infty" },
417 { &isl_val_floor, "-infty", "-infty" },
418 { &isl_val_floor, "NaN", "NaN" },
419 { &isl_val_ceil, "1", "1" },
420 { &isl_val_ceil, "-1", "-1" },
421 { &isl_val_ceil, "1/2", "1" },
422 { &isl_val_ceil, "-1/2", "0" },
423 { &isl_val_ceil, "infty", "infty" },
424 { &isl_val_ceil, "-infty", "-infty" },
425 { &isl_val_ceil, "NaN", "NaN" },
426 { &isl_val_2exp, "-3", "1/8" },
427 { &isl_val_2exp, "-1", "1/2" },
428 { &isl_val_2exp, "1", "2" },
429 { &isl_val_2exp, "2", "4" },
430 { &isl_val_2exp, "3", "8" },
431 { &isl_val_inv, "1", "1" },
432 { &isl_val_inv, "2", "1/2" },
433 { &isl_val_inv, "1/2", "2" },
434 { &isl_val_inv, "-2", "-1/2" },
435 { &isl_val_inv, "-1/2", "-2" },
436 { &isl_val_inv, "0", "NaN" },
437 { &isl_val_inv, "NaN", "NaN" },
438 { &isl_val_inv, "infty", "0" },
439 { &isl_val_inv, "-infty", "0" },
442 /* Perform some basic tests of unary operations on isl_val objects.
444 static int test_un_val(isl_ctx *ctx)
446 int i;
447 isl_val *v, *res;
448 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
449 int ok;
451 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
452 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
453 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
454 fn = val_un_tests[i].op;
455 v = fn(v);
456 if (isl_val_is_nan(res))
457 ok = isl_val_is_nan(v);
458 else
459 ok = isl_val_eq(v, res);
460 isl_val_free(v);
461 isl_val_free(res);
462 if (ok < 0)
463 return -1;
464 if (!ok)
465 isl_die(ctx, isl_error_unknown,
466 "unexpected result", return -1);
469 return 0;
472 struct {
473 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
474 __isl_take isl_val *v2);
475 } val_bin_op[] = {
476 ['+'] = { &isl_val_add },
477 ['-'] = { &isl_val_sub },
478 ['*'] = { &isl_val_mul },
479 ['/'] = { &isl_val_div },
480 ['g'] = { &isl_val_gcd },
481 ['m'] = { &isl_val_min },
482 ['M'] = { &isl_val_max },
485 struct {
486 const char *arg1;
487 unsigned char op;
488 const char *arg2;
489 const char *res;
490 } val_bin_tests[] = {
491 { "0", '+', "0", "0" },
492 { "1", '+', "0", "1" },
493 { "1", '+', "1", "2" },
494 { "1", '-', "1", "0" },
495 { "1", '*', "1", "1" },
496 { "1", '/', "1", "1" },
497 { "2", '*', "3", "6" },
498 { "2", '*', "1/2", "1" },
499 { "2", '*', "1/3", "2/3" },
500 { "2/3", '*', "3/5", "2/5" },
501 { "2/3", '*', "7/5", "14/15" },
502 { "2", '/', "1/2", "4" },
503 { "-2", '/', "-1/2", "4" },
504 { "-2", '/', "1/2", "-4" },
505 { "2", '/', "-1/2", "-4" },
506 { "2", '/', "2", "1" },
507 { "2", '/', "3", "2/3" },
508 { "2/3", '/', "5/3", "2/5" },
509 { "2/3", '/', "5/7", "14/15" },
510 { "0", '/', "0", "NaN" },
511 { "42", '/', "0", "NaN" },
512 { "-42", '/', "0", "NaN" },
513 { "infty", '/', "0", "NaN" },
514 { "-infty", '/', "0", "NaN" },
515 { "NaN", '/', "0", "NaN" },
516 { "0", '/', "NaN", "NaN" },
517 { "42", '/', "NaN", "NaN" },
518 { "-42", '/', "NaN", "NaN" },
519 { "infty", '/', "NaN", "NaN" },
520 { "-infty", '/', "NaN", "NaN" },
521 { "NaN", '/', "NaN", "NaN" },
522 { "0", '/', "infty", "0" },
523 { "42", '/', "infty", "0" },
524 { "-42", '/', "infty", "0" },
525 { "infty", '/', "infty", "NaN" },
526 { "-infty", '/', "infty", "NaN" },
527 { "NaN", '/', "infty", "NaN" },
528 { "0", '/', "-infty", "0" },
529 { "42", '/', "-infty", "0" },
530 { "-42", '/', "-infty", "0" },
531 { "infty", '/', "-infty", "NaN" },
532 { "-infty", '/', "-infty", "NaN" },
533 { "NaN", '/', "-infty", "NaN" },
534 { "1", '-', "1/3", "2/3" },
535 { "1/3", '+', "1/2", "5/6" },
536 { "1/2", '+', "1/2", "1" },
537 { "3/4", '-', "1/4", "1/2" },
538 { "1/2", '-', "1/3", "1/6" },
539 { "infty", '+', "42", "infty" },
540 { "infty", '+', "infty", "infty" },
541 { "42", '+', "infty", "infty" },
542 { "infty", '-', "infty", "NaN" },
543 { "infty", '*', "infty", "infty" },
544 { "infty", '*', "-infty", "-infty" },
545 { "-infty", '*', "infty", "-infty" },
546 { "-infty", '*', "-infty", "infty" },
547 { "0", '*', "infty", "NaN" },
548 { "1", '*', "infty", "infty" },
549 { "infty", '*', "0", "NaN" },
550 { "infty", '*', "42", "infty" },
551 { "42", '-', "infty", "-infty" },
552 { "infty", '+', "-infty", "NaN" },
553 { "4", 'g', "6", "2" },
554 { "5", 'g', "6", "1" },
555 { "42", 'm', "3", "3" },
556 { "42", 'M', "3", "42" },
557 { "3", 'm', "42", "3" },
558 { "3", 'M', "42", "42" },
559 { "42", 'm', "infty", "42" },
560 { "42", 'M', "infty", "infty" },
561 { "42", 'm', "-infty", "-infty" },
562 { "42", 'M', "-infty", "42" },
563 { "42", 'm', "NaN", "NaN" },
564 { "42", 'M', "NaN", "NaN" },
565 { "infty", 'm', "-infty", "-infty" },
566 { "infty", 'M', "-infty", "infty" },
569 /* Perform some basic tests of binary operations on isl_val objects.
571 static int test_bin_val(isl_ctx *ctx)
573 int i;
574 isl_val *v1, *v2, *res;
575 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
576 __isl_take isl_val *v2);
577 int ok;
579 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
580 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
581 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
582 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
583 fn = val_bin_op[val_bin_tests[i].op].fn;
584 v1 = fn(v1, v2);
585 if (isl_val_is_nan(res))
586 ok = isl_val_is_nan(v1);
587 else
588 ok = isl_val_eq(v1, res);
589 isl_val_free(v1);
590 isl_val_free(res);
591 if (ok < 0)
592 return -1;
593 if (!ok)
594 isl_die(ctx, isl_error_unknown,
595 "unexpected result", return -1);
598 return 0;
601 /* Perform some basic tests on isl_val objects.
603 static int test_val(isl_ctx *ctx)
605 if (test_un_val(ctx) < 0)
606 return -1;
607 if (test_bin_val(ctx) < 0)
608 return -1;
609 return 0;
612 static int test_div(isl_ctx *ctx)
614 unsigned n;
615 const char *str;
616 int empty;
617 isl_int v;
618 isl_space *dim;
619 isl_set *set;
620 isl_local_space *ls;
621 struct isl_basic_set *bset;
622 struct isl_constraint *c;
624 isl_int_init(v);
626 /* test 1 */
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 2 */
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, -1);
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, 3);
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 3 */
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, 1);
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, -3);
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, 4);
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(bset && bset->n_div == 1);
710 isl_local_space_free(ls);
711 isl_basic_set_free(bset);
713 /* test 4 */
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, 2);
720 isl_constraint_set_constant(c, v);
721 isl_int_set_si(v, -1);
722 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
723 isl_int_set_si(v, 3);
724 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
725 bset = isl_basic_set_add_constraint(bset, c);
727 c = isl_equality_alloc(isl_local_space_copy(ls));
728 isl_int_set_si(v, -1);
729 isl_constraint_set_constant(c, v);
730 isl_int_set_si(v, 1);
731 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
732 isl_int_set_si(v, 6);
733 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
734 bset = isl_basic_set_add_constraint(bset, c);
736 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
738 assert(isl_basic_set_is_empty(bset));
739 isl_local_space_free(ls);
740 isl_basic_set_free(bset);
742 /* test 5 */
743 dim = isl_space_set_alloc(ctx, 0, 3);
744 bset = isl_basic_set_universe(isl_space_copy(dim));
745 ls = isl_local_space_from_space(dim);
747 c = isl_equality_alloc(isl_local_space_copy(ls));
748 isl_int_set_si(v, -1);
749 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
750 isl_int_set_si(v, 3);
751 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
752 bset = isl_basic_set_add_constraint(bset, c);
754 c = isl_equality_alloc(isl_local_space_copy(ls));
755 isl_int_set_si(v, 1);
756 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
757 isl_int_set_si(v, -3);
758 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
759 bset = isl_basic_set_add_constraint(bset, c);
761 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
763 assert(bset && bset->n_div == 0);
764 isl_basic_set_free(bset);
765 isl_local_space_free(ls);
767 /* test 6 */
768 dim = isl_space_set_alloc(ctx, 0, 3);
769 bset = isl_basic_set_universe(isl_space_copy(dim));
770 ls = isl_local_space_from_space(dim);
772 c = isl_equality_alloc(isl_local_space_copy(ls));
773 isl_int_set_si(v, -1);
774 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
775 isl_int_set_si(v, 6);
776 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
777 bset = isl_basic_set_add_constraint(bset, c);
779 c = isl_equality_alloc(isl_local_space_copy(ls));
780 isl_int_set_si(v, 1);
781 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
782 isl_int_set_si(v, -3);
783 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
784 bset = isl_basic_set_add_constraint(bset, c);
786 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
788 assert(bset && bset->n_div == 1);
789 isl_basic_set_free(bset);
790 isl_local_space_free(ls);
792 /* test 7 */
793 /* This test is a bit tricky. We set up an equality
794 * a + 3b + 3c = 6 e0
795 * Normalization of divs creates _two_ divs
796 * a = 3 e0
797 * c - b - e0 = 2 e1
798 * Afterwards e0 is removed again because it has coefficient -1
799 * and we end up with the original equality and div again.
800 * Perhaps we can avoid the introduction of this temporary div.
802 dim = isl_space_set_alloc(ctx, 0, 4);
803 bset = isl_basic_set_universe(isl_space_copy(dim));
804 ls = isl_local_space_from_space(dim);
806 c = isl_equality_alloc(isl_local_space_copy(ls));
807 isl_int_set_si(v, -1);
808 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
809 isl_int_set_si(v, -3);
810 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
811 isl_int_set_si(v, -3);
812 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
813 isl_int_set_si(v, 6);
814 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
815 bset = isl_basic_set_add_constraint(bset, c);
817 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
819 /* Test disabled for now */
821 assert(bset && bset->n_div == 1);
823 isl_local_space_free(ls);
824 isl_basic_set_free(bset);
826 /* test 8 */
827 dim = isl_space_set_alloc(ctx, 0, 5);
828 bset = isl_basic_set_universe(isl_space_copy(dim));
829 ls = isl_local_space_from_space(dim);
831 c = isl_equality_alloc(isl_local_space_copy(ls));
832 isl_int_set_si(v, -1);
833 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
834 isl_int_set_si(v, -3);
835 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
836 isl_int_set_si(v, -3);
837 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
838 isl_int_set_si(v, 6);
839 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
840 bset = isl_basic_set_add_constraint(bset, c);
842 c = isl_equality_alloc(isl_local_space_copy(ls));
843 isl_int_set_si(v, -1);
844 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
845 isl_int_set_si(v, 1);
846 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
847 isl_int_set_si(v, 1);
848 isl_constraint_set_constant(c, v);
849 bset = isl_basic_set_add_constraint(bset, c);
851 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
853 /* Test disabled for now */
855 assert(bset && bset->n_div == 1);
857 isl_local_space_free(ls);
858 isl_basic_set_free(bset);
860 /* test 9 */
861 dim = isl_space_set_alloc(ctx, 0, 4);
862 bset = isl_basic_set_universe(isl_space_copy(dim));
863 ls = isl_local_space_from_space(dim);
865 c = isl_equality_alloc(isl_local_space_copy(ls));
866 isl_int_set_si(v, 1);
867 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
868 isl_int_set_si(v, -1);
869 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
870 isl_int_set_si(v, -2);
871 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
872 bset = isl_basic_set_add_constraint(bset, c);
874 c = isl_equality_alloc(isl_local_space_copy(ls));
875 isl_int_set_si(v, -1);
876 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
877 isl_int_set_si(v, 3);
878 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
879 isl_int_set_si(v, 2);
880 isl_constraint_set_constant(c, v);
881 bset = isl_basic_set_add_constraint(bset, c);
883 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
885 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
887 assert(!isl_basic_set_is_empty(bset));
889 isl_local_space_free(ls);
890 isl_basic_set_free(bset);
892 /* test 10 */
893 dim = isl_space_set_alloc(ctx, 0, 3);
894 bset = isl_basic_set_universe(isl_space_copy(dim));
895 ls = isl_local_space_from_space(dim);
897 c = isl_equality_alloc(isl_local_space_copy(ls));
898 isl_int_set_si(v, 1);
899 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
900 isl_int_set_si(v, -2);
901 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
902 bset = isl_basic_set_add_constraint(bset, c);
904 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
906 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
908 isl_local_space_free(ls);
909 isl_basic_set_free(bset);
911 isl_int_clear(v);
913 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
914 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
915 set = isl_set_read_from_str(ctx, str);
916 set = isl_set_compute_divs(set);
917 isl_set_free(set);
918 if (!set)
919 return -1;
921 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
922 bset = isl_basic_set_read_from_str(ctx, str);
923 n = isl_basic_set_dim(bset, isl_dim_div);
924 isl_basic_set_free(bset);
925 if (!bset)
926 return -1;
927 if (n != 0)
928 isl_die(ctx, isl_error_unknown,
929 "expecting no existentials", return -1);
931 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
932 set = isl_set_read_from_str(ctx, str);
933 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
934 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
935 empty = isl_set_is_empty(set);
936 isl_set_free(set);
937 if (empty < 0)
938 return -1;
939 if (!empty)
940 isl_die(ctx, isl_error_unknown,
941 "result not as accurate as expected", return -1);
943 return 0;
946 void test_application_case(struct isl_ctx *ctx, const char *name)
948 char *filename;
949 FILE *input;
950 struct isl_basic_set *bset1, *bset2;
951 struct isl_basic_map *bmap;
953 filename = get_filename(ctx, name, "omega");
954 assert(filename);
955 input = fopen(filename, "r");
956 assert(input);
958 bset1 = isl_basic_set_read_from_file(ctx, input);
959 bmap = isl_basic_map_read_from_file(ctx, input);
961 bset1 = isl_basic_set_apply(bset1, bmap);
963 bset2 = isl_basic_set_read_from_file(ctx, input);
965 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
967 isl_basic_set_free(bset1);
968 isl_basic_set_free(bset2);
969 free(filename);
971 fclose(input);
974 void test_application(struct isl_ctx *ctx)
976 test_application_case(ctx, "application");
977 test_application_case(ctx, "application2");
980 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
982 char *filename;
983 FILE *input;
984 struct isl_basic_set *bset1, *bset2;
986 filename = get_filename(ctx, name, "polylib");
987 assert(filename);
988 input = fopen(filename, "r");
989 assert(input);
991 bset1 = isl_basic_set_read_from_file(ctx, input);
992 bset2 = isl_basic_set_read_from_file(ctx, input);
994 bset1 = isl_basic_set_affine_hull(bset1);
996 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
998 isl_basic_set_free(bset1);
999 isl_basic_set_free(bset2);
1000 free(filename);
1002 fclose(input);
1005 int test_affine_hull(struct isl_ctx *ctx)
1007 const char *str;
1008 isl_set *set;
1009 isl_basic_set *bset, *bset2;
1010 int n;
1011 int subset;
1013 test_affine_hull_case(ctx, "affine2");
1014 test_affine_hull_case(ctx, "affine");
1015 test_affine_hull_case(ctx, "affine3");
1017 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1018 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1019 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1020 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1021 set = isl_set_read_from_str(ctx, str);
1022 bset = isl_set_affine_hull(set);
1023 n = isl_basic_set_dim(bset, isl_dim_div);
1024 isl_basic_set_free(bset);
1025 if (n != 0)
1026 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1027 return -1);
1029 /* Check that isl_map_affine_hull is not confused by
1030 * the reordering of divs in isl_map_align_divs.
1032 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1033 "32e0 = b and 32e1 = c); "
1034 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1035 set = isl_set_read_from_str(ctx, str);
1036 bset = isl_set_affine_hull(set);
1037 isl_basic_set_free(bset);
1038 if (!bset)
1039 return -1;
1041 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1042 "32e2 = 31 + 31e0 }";
1043 set = isl_set_read_from_str(ctx, str);
1044 bset = isl_set_affine_hull(set);
1045 str = "{ [a] : exists e : a = 32 e }";
1046 bset2 = isl_basic_set_read_from_str(ctx, str);
1047 subset = isl_basic_set_is_subset(bset, bset2);
1048 isl_basic_set_free(bset);
1049 isl_basic_set_free(bset2);
1050 if (subset < 0)
1051 return -1;
1052 if (!subset)
1053 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1054 return -1);
1056 return 0;
1059 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1061 char *filename;
1062 FILE *input;
1063 struct isl_basic_set *bset1, *bset2;
1064 struct isl_set *set;
1066 filename = get_filename(ctx, name, "polylib");
1067 assert(filename);
1068 input = fopen(filename, "r");
1069 assert(input);
1071 bset1 = isl_basic_set_read_from_file(ctx, input);
1072 bset2 = isl_basic_set_read_from_file(ctx, input);
1074 set = isl_basic_set_union(bset1, bset2);
1075 bset1 = isl_set_convex_hull(set);
1077 bset2 = isl_basic_set_read_from_file(ctx, input);
1079 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1081 isl_basic_set_free(bset1);
1082 isl_basic_set_free(bset2);
1083 free(filename);
1085 fclose(input);
1088 struct {
1089 const char *set;
1090 const char *hull;
1091 } convex_hull_tests[] = {
1092 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1093 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1094 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1095 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1096 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1097 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1098 "i2 <= 5 and i2 >= 4; "
1099 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1100 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1101 "i2 <= 5 + i0 and i2 >= i0 }" },
1102 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1103 "{ [x, y] : 1 = 0 }" },
1106 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
1108 int i;
1109 int orig_convex = ctx->opt->convex;
1110 ctx->opt->convex = convex;
1112 test_convex_hull_case(ctx, "convex0");
1113 test_convex_hull_case(ctx, "convex1");
1114 test_convex_hull_case(ctx, "convex2");
1115 test_convex_hull_case(ctx, "convex3");
1116 test_convex_hull_case(ctx, "convex4");
1117 test_convex_hull_case(ctx, "convex5");
1118 test_convex_hull_case(ctx, "convex6");
1119 test_convex_hull_case(ctx, "convex7");
1120 test_convex_hull_case(ctx, "convex8");
1121 test_convex_hull_case(ctx, "convex9");
1122 test_convex_hull_case(ctx, "convex10");
1123 test_convex_hull_case(ctx, "convex11");
1124 test_convex_hull_case(ctx, "convex12");
1125 test_convex_hull_case(ctx, "convex13");
1126 test_convex_hull_case(ctx, "convex14");
1127 test_convex_hull_case(ctx, "convex15");
1129 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1130 isl_set *set1, *set2;
1132 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1133 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1134 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1135 assert(isl_set_is_equal(set1, set2));
1136 isl_set_free(set1);
1137 isl_set_free(set2);
1140 ctx->opt->convex = orig_convex;
1143 void test_convex_hull(struct isl_ctx *ctx)
1145 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
1146 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
1149 void test_gist_case(struct isl_ctx *ctx, const char *name)
1151 char *filename;
1152 FILE *input;
1153 struct isl_basic_set *bset1, *bset2;
1155 filename = get_filename(ctx, name, "polylib");
1156 assert(filename);
1157 input = fopen(filename, "r");
1158 assert(input);
1160 bset1 = isl_basic_set_read_from_file(ctx, input);
1161 bset2 = isl_basic_set_read_from_file(ctx, input);
1163 bset1 = isl_basic_set_gist(bset1, bset2);
1165 bset2 = isl_basic_set_read_from_file(ctx, input);
1167 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1169 isl_basic_set_free(bset1);
1170 isl_basic_set_free(bset2);
1171 free(filename);
1173 fclose(input);
1176 struct {
1177 const char *set;
1178 const char *context;
1179 const char *gist;
1180 } gist_tests[] = {
1181 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1182 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1183 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1184 "{ [a, b, c] : a <= 15 }" },
1185 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1186 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1187 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1188 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1189 { "{ [m, n, a, b] : a <= 2147 + n }",
1190 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1191 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1192 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1193 "b >= 0) }",
1194 "{ [m, n, ku, kl] }" },
1197 static int test_gist(struct isl_ctx *ctx)
1199 int i;
1200 const char *str;
1201 isl_basic_set *bset1, *bset2;
1202 isl_map *map1, *map2;
1203 int equal;
1205 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1206 int equal_input;
1207 isl_set *set1, *set2, *copy;
1209 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1210 set2 = isl_set_read_from_str(ctx, gist_tests[i].context);
1211 copy = isl_set_copy(set1);
1212 set1 = isl_set_gist(set1, set2);
1213 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1214 equal = isl_set_is_equal(set1, set2);
1215 isl_set_free(set1);
1216 isl_set_free(set2);
1217 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1218 equal_input = isl_set_is_equal(set1, copy);
1219 isl_set_free(set1);
1220 isl_set_free(copy);
1221 if (equal < 0 || equal_input < 0)
1222 return -1;
1223 if (!equal)
1224 isl_die(ctx, isl_error_unknown,
1225 "incorrect gist result", return -1);
1226 if (!equal_input)
1227 isl_die(ctx, isl_error_unknown,
1228 "gist modified input", return -1);
1231 test_gist_case(ctx, "gist1");
1233 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1234 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1235 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1236 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1237 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1238 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1239 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1240 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1241 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1242 bset1 = isl_basic_set_read_from_str(ctx, str);
1243 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1244 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1245 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1246 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1247 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1248 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1249 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1250 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1251 bset2 = isl_basic_set_read_from_str(ctx, str);
1252 bset1 = isl_basic_set_gist(bset1, bset2);
1253 assert(bset1 && bset1->n_div == 0);
1254 isl_basic_set_free(bset1);
1256 /* Check that the integer divisions of the second disjunct
1257 * do not spread to the first disjunct.
1259 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1260 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1261 "(exists (e0 = [(-1 + t1)/16], "
1262 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1263 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1264 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1265 "o0 <= 4294967295 and t1 <= -1)) }";
1266 map1 = isl_map_read_from_str(ctx, str);
1267 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1268 map2 = isl_map_read_from_str(ctx, str);
1269 map1 = isl_map_gist(map1, map2);
1270 if (!map1)
1271 return -1;
1272 if (map1->n != 1)
1273 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1274 isl_map_free(map1); return -1);
1275 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1276 isl_die(ctx, isl_error_unknown, "expecting single div",
1277 isl_map_free(map1); return -1);
1278 isl_map_free(map1);
1280 return 0;
1283 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1285 isl_set *set, *set2;
1286 int equal;
1287 int one;
1289 set = isl_set_read_from_str(ctx, str);
1290 set = isl_set_coalesce(set);
1291 set2 = isl_set_read_from_str(ctx, str);
1292 equal = isl_set_is_equal(set, set2);
1293 one = set && set->n == 1;
1294 isl_set_free(set);
1295 isl_set_free(set2);
1297 if (equal < 0)
1298 return -1;
1299 if (!equal)
1300 isl_die(ctx, isl_error_unknown,
1301 "coalesced set not equal to input", return -1);
1302 if (check_one && !one)
1303 isl_die(ctx, isl_error_unknown,
1304 "coalesced set should not be a union", return -1);
1306 return 0;
1309 /* Inputs for coalescing tests with unbounded wrapping.
1310 * "str" is a string representation of the input set.
1311 * "single_disjunct" is set if we expect the result to consist of
1312 * a single disjunct.
1314 struct {
1315 int single_disjunct;
1316 const char *str;
1317 } coalesce_unbounded_tests[] = {
1318 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1319 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1320 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1321 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1322 "-10 <= y <= 0}" },
1323 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1324 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1325 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1326 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1329 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1330 * option turned off.
1332 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1334 int i;
1335 int r = 0;
1336 int bounded;
1338 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1339 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1341 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1342 const char *str = coalesce_unbounded_tests[i].str;
1343 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1344 if (test_coalesce_set(ctx, str, check_one) >= 0)
1345 continue;
1346 r = -1;
1347 break;
1350 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1352 return r;
1355 /* Inputs for coalescing tests.
1356 * "str" is a string representation of the input set.
1357 * "single_disjunct" is set if we expect the result to consist of
1358 * a single disjunct.
1360 struct {
1361 int single_disjunct;
1362 const char *str;
1363 } coalesce_tests[] = {
1364 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1365 "y >= x & x >= 2 & 5 >= y }" },
1366 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1367 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1368 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1369 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1370 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1371 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1372 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1373 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1374 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1375 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1376 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1377 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1378 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1379 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1380 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1381 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1382 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1383 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1384 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1385 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1386 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1387 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1388 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1389 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1390 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1391 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1392 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1393 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1394 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1395 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1396 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1397 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1398 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1399 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1400 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1401 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1402 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1403 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1404 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1405 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1406 "[o0, o1, o2, o3, o4, o5, o6]] : "
1407 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1408 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1409 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1410 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1411 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1412 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1413 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1414 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1415 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1416 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1417 "o6 >= i3 + i6 - o3 and M >= 0 and "
1418 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1419 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1420 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1421 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1422 "(o0 = 0 and M >= 2 and N >= 3) or "
1423 "(M = 0 and o0 = 0 and N >= 3) }" },
1424 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1425 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1426 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1427 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1428 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1429 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1430 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1431 "(y = 3 and x = 1) }" },
1432 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1433 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1434 "i1 <= M and i3 <= M and i4 <= M) or "
1435 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1436 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1437 "i4 <= -1 + M) }" },
1438 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1439 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1440 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1441 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1442 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1443 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1444 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1445 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1446 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1447 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1448 { 0, "{ [a, b] : exists e : 2e = a and "
1449 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1450 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1451 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1452 "j >= 1 and j' <= i + j - i' and i >= 1; "
1453 "[1, 1, 1, 1] }" },
1454 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1455 "[i,j] : exists a : j = 3a }" },
1456 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1457 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1458 "a >= 3) or "
1459 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1460 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1461 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1462 "c <= 6 + 8a and a >= 3; "
1463 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1464 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1465 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1466 "[x,0] : 3 <= x <= 4 }" },
1467 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1468 "[x,0] : 4 <= x <= 5 }" },
1469 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1470 "[x,0] : 3 <= x <= 5 }" },
1471 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1472 "[x,0] : 3 <= x <= 4 }" },
1473 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1474 "i1 <= 0; "
1475 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1476 { 1, "{ [0,0]; [1,1] }" },
1477 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1478 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1479 "ii <= k;"
1480 "[k, 0, k] : k <= 6 and k >= 1 }" },
1481 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1482 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1483 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1484 { 1, "[n] -> { [1] : n >= 0;"
1485 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1486 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1487 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1488 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1489 "2e0 <= x and 2e0 <= n);"
1490 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1491 "n >= 0) }" },
1492 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1493 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1494 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1495 "t1 = 1 }" },
1496 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1497 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1498 "[0, 0] }" },
1499 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1500 "t1 >= 13 and t1 <= 16);"
1501 "[t1] : t1 <= 15 and t1 >= 12 }" },
1502 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
1503 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
1504 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
1505 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
1506 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
1507 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
1508 "i <= 4j + 2 }" },
1509 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
1510 "(exists (e0 : 3e0 = -2 + c0)) }" },
1513 /* Test the functionality of isl_set_coalesce.
1514 * That is, check that the output is always equal to the input
1515 * and in some cases that the result consists of a single disjunct.
1517 static int test_coalesce(struct isl_ctx *ctx)
1519 int i;
1521 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1522 const char *str = coalesce_tests[i].str;
1523 int check_one = coalesce_tests[i].single_disjunct;
1524 if (test_coalesce_set(ctx, str, check_one) < 0)
1525 return -1;
1528 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1529 return -1;
1531 return 0;
1534 void test_closure(struct isl_ctx *ctx)
1536 const char *str;
1537 isl_set *dom;
1538 isl_map *up, *right;
1539 isl_map *map, *map2;
1540 int exact;
1542 /* COCOA example 1 */
1543 map = isl_map_read_from_str(ctx,
1544 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1545 "1 <= i and i < n and 1 <= j and j < n or "
1546 "i2 = i + 1 and j2 = j - 1 and "
1547 "1 <= i and i < n and 2 <= j and j <= n }");
1548 map = isl_map_power(map, &exact);
1549 assert(exact);
1550 isl_map_free(map);
1552 /* COCOA example 1 */
1553 map = isl_map_read_from_str(ctx,
1554 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1555 "1 <= i and i < n and 1 <= j and j < n or "
1556 "i2 = i + 1 and j2 = j - 1 and "
1557 "1 <= i and i < n and 2 <= j and j <= n }");
1558 map = isl_map_transitive_closure(map, &exact);
1559 assert(exact);
1560 map2 = isl_map_read_from_str(ctx,
1561 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1562 "1 <= i and i < n and 1 <= j and j <= n and "
1563 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1564 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1565 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1566 assert(isl_map_is_equal(map, map2));
1567 isl_map_free(map2);
1568 isl_map_free(map);
1570 map = isl_map_read_from_str(ctx,
1571 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1572 " 0 <= y and y <= n }");
1573 map = isl_map_transitive_closure(map, &exact);
1574 map2 = isl_map_read_from_str(ctx,
1575 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1576 " 0 <= y and y <= n }");
1577 assert(isl_map_is_equal(map, map2));
1578 isl_map_free(map2);
1579 isl_map_free(map);
1581 /* COCOA example 2 */
1582 map = isl_map_read_from_str(ctx,
1583 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1584 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1585 "i2 = i + 2 and j2 = j - 2 and "
1586 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1587 map = isl_map_transitive_closure(map, &exact);
1588 assert(exact);
1589 map2 = isl_map_read_from_str(ctx,
1590 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1591 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1592 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1593 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1594 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1595 assert(isl_map_is_equal(map, map2));
1596 isl_map_free(map);
1597 isl_map_free(map2);
1599 /* COCOA Fig.2 left */
1600 map = isl_map_read_from_str(ctx,
1601 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1602 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1603 "j <= n or "
1604 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1605 "j <= 2 i - 3 and j <= n - 2 or "
1606 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1607 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1608 map = isl_map_transitive_closure(map, &exact);
1609 assert(exact);
1610 isl_map_free(map);
1612 /* COCOA Fig.2 right */
1613 map = isl_map_read_from_str(ctx,
1614 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1615 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1616 "j <= n or "
1617 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1618 "j <= 2 i - 4 and j <= n - 3 or "
1619 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1620 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1621 map = isl_map_power(map, &exact);
1622 assert(exact);
1623 isl_map_free(map);
1625 /* COCOA Fig.2 right */
1626 map = isl_map_read_from_str(ctx,
1627 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1628 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1629 "j <= n or "
1630 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1631 "j <= 2 i - 4 and j <= n - 3 or "
1632 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1633 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1634 map = isl_map_transitive_closure(map, &exact);
1635 assert(exact);
1636 map2 = isl_map_read_from_str(ctx,
1637 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1638 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1639 "j <= n and 3 + i + 2 j <= 3 n and "
1640 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1641 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1642 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1643 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1644 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1645 assert(isl_map_is_equal(map, map2));
1646 isl_map_free(map2);
1647 isl_map_free(map);
1649 /* COCOA Fig.1 right */
1650 dom = isl_set_read_from_str(ctx,
1651 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1652 "2 x - 3 y + 3 >= 0 }");
1653 right = isl_map_read_from_str(ctx,
1654 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1655 up = isl_map_read_from_str(ctx,
1656 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1657 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1658 right = isl_map_intersect_range(right, isl_set_copy(dom));
1659 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1660 up = isl_map_intersect_range(up, dom);
1661 map = isl_map_union(up, right);
1662 map = isl_map_transitive_closure(map, &exact);
1663 assert(exact);
1664 map2 = isl_map_read_from_str(ctx,
1665 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1666 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1667 assert(isl_map_is_equal(map, map2));
1668 isl_map_free(map2);
1669 isl_map_free(map);
1671 /* COCOA Theorem 1 counter example */
1672 map = isl_map_read_from_str(ctx,
1673 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1674 "i2 = 1 and j2 = j or "
1675 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1676 map = isl_map_transitive_closure(map, &exact);
1677 assert(exact);
1678 isl_map_free(map);
1680 map = isl_map_read_from_str(ctx,
1681 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1682 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1683 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1684 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1685 map = isl_map_transitive_closure(map, &exact);
1686 assert(exact);
1687 isl_map_free(map);
1689 /* Kelly et al 1996, fig 12 */
1690 map = isl_map_read_from_str(ctx,
1691 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1692 "1 <= i,j,j+1 <= n or "
1693 "j = n and j2 = 1 and i2 = i + 1 and "
1694 "1 <= i,i+1 <= n }");
1695 map = isl_map_transitive_closure(map, &exact);
1696 assert(exact);
1697 map2 = isl_map_read_from_str(ctx,
1698 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1699 "1 <= i <= n and i = i2 or "
1700 "1 <= i < i2 <= n and 1 <= j <= n and "
1701 "1 <= j2 <= n }");
1702 assert(isl_map_is_equal(map, map2));
1703 isl_map_free(map2);
1704 isl_map_free(map);
1706 /* Omega's closure4 */
1707 map = isl_map_read_from_str(ctx,
1708 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1709 "1 <= x,y <= 10 or "
1710 "x2 = x + 1 and y2 = y and "
1711 "1 <= x <= 20 && 5 <= y <= 15 }");
1712 map = isl_map_transitive_closure(map, &exact);
1713 assert(exact);
1714 isl_map_free(map);
1716 map = isl_map_read_from_str(ctx,
1717 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1718 map = isl_map_transitive_closure(map, &exact);
1719 assert(!exact);
1720 map2 = isl_map_read_from_str(ctx,
1721 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1722 assert(isl_map_is_equal(map, map2));
1723 isl_map_free(map);
1724 isl_map_free(map2);
1726 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1727 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1728 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1729 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1730 map = isl_map_read_from_str(ctx, str);
1731 map = isl_map_transitive_closure(map, &exact);
1732 assert(exact);
1733 map2 = isl_map_read_from_str(ctx, str);
1734 assert(isl_map_is_equal(map, map2));
1735 isl_map_free(map);
1736 isl_map_free(map2);
1738 str = "{[0] -> [1]; [2] -> [3]}";
1739 map = isl_map_read_from_str(ctx, str);
1740 map = isl_map_transitive_closure(map, &exact);
1741 assert(exact);
1742 map2 = isl_map_read_from_str(ctx, str);
1743 assert(isl_map_is_equal(map, map2));
1744 isl_map_free(map);
1745 isl_map_free(map2);
1747 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1748 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1749 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1750 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1751 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1752 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1753 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1754 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1755 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1756 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1757 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1758 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1759 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1760 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1761 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1762 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1763 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1764 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1765 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1766 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1767 map = isl_map_read_from_str(ctx, str);
1768 map = isl_map_transitive_closure(map, NULL);
1769 assert(map);
1770 isl_map_free(map);
1773 void test_lex(struct isl_ctx *ctx)
1775 isl_space *dim;
1776 isl_map *map;
1778 dim = isl_space_set_alloc(ctx, 0, 0);
1779 map = isl_map_lex_le(dim);
1780 assert(!isl_map_is_empty(map));
1781 isl_map_free(map);
1784 static int test_lexmin(struct isl_ctx *ctx)
1786 int equal;
1787 const char *str;
1788 isl_basic_map *bmap;
1789 isl_map *map, *map2;
1790 isl_set *set;
1791 isl_set *set2;
1792 isl_pw_multi_aff *pma;
1794 str = "[p0, p1] -> { [] -> [] : "
1795 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1796 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1797 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1798 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1799 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1800 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1801 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1802 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1803 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1804 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1805 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1806 map = isl_map_read_from_str(ctx, str);
1807 map = isl_map_lexmin(map);
1808 isl_map_free(map);
1810 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1811 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1812 set = isl_set_read_from_str(ctx, str);
1813 set = isl_set_lexmax(set);
1814 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1815 set2 = isl_set_read_from_str(ctx, str);
1816 set = isl_set_intersect(set, set2);
1817 assert(!isl_set_is_empty(set));
1818 isl_set_free(set);
1820 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1821 map = isl_map_read_from_str(ctx, str);
1822 map = isl_map_lexmin(map);
1823 str = "{ [x] -> [5] : 6 <= x <= 8; "
1824 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1825 map2 = isl_map_read_from_str(ctx, str);
1826 assert(isl_map_is_equal(map, map2));
1827 isl_map_free(map);
1828 isl_map_free(map2);
1830 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1831 map = isl_map_read_from_str(ctx, str);
1832 map2 = isl_map_copy(map);
1833 map = isl_map_lexmin(map);
1834 assert(isl_map_is_equal(map, map2));
1835 isl_map_free(map);
1836 isl_map_free(map2);
1838 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1839 map = isl_map_read_from_str(ctx, str);
1840 map = isl_map_lexmin(map);
1841 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1842 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1843 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1844 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1845 map2 = isl_map_read_from_str(ctx, str);
1846 assert(isl_map_is_equal(map, map2));
1847 isl_map_free(map);
1848 isl_map_free(map2);
1850 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1851 " 8i' <= i and 8i' >= -7 + i }";
1852 bmap = isl_basic_map_read_from_str(ctx, str);
1853 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1854 map2 = isl_map_from_pw_multi_aff(pma);
1855 map = isl_map_from_basic_map(bmap);
1856 assert(isl_map_is_equal(map, map2));
1857 isl_map_free(map);
1858 isl_map_free(map2);
1860 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1861 map = isl_map_read_from_str(ctx, str);
1862 map = isl_map_lexmin(map);
1863 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1864 map2 = isl_map_read_from_str(ctx, str);
1865 assert(isl_map_is_equal(map, map2));
1866 isl_map_free(map);
1867 isl_map_free(map2);
1869 /* Check that empty pieces are properly combined. */
1870 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1871 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1872 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1873 map = isl_map_read_from_str(ctx, str);
1874 map = isl_map_lexmin(map);
1875 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1876 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1877 "x >= 4 }";
1878 map2 = isl_map_read_from_str(ctx, str);
1879 assert(isl_map_is_equal(map, map2));
1880 isl_map_free(map);
1881 isl_map_free(map2);
1883 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1884 " 8i' <= i and 8i' >= -7 + i }";
1885 set = isl_set_read_from_str(ctx, str);
1886 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1887 set2 = isl_set_from_pw_multi_aff(pma);
1888 equal = isl_set_is_equal(set, set2);
1889 isl_set_free(set);
1890 isl_set_free(set2);
1891 if (equal < 0)
1892 return -1;
1893 if (!equal)
1894 isl_die(ctx, isl_error_unknown,
1895 "unexpected difference between set and "
1896 "piecewise affine expression", return -1);
1898 return 0;
1901 /* Check that isl_set_min_val and isl_set_max_val compute the correct
1902 * result on non-convex inputs.
1904 static int test_min(struct isl_ctx *ctx)
1906 isl_set *set;
1907 isl_aff *aff;
1908 isl_val *val;
1909 int min_ok, max_ok;
1911 set = isl_set_read_from_str(ctx, "{ [-1]; [1] }");
1912 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x] }");
1913 val = isl_set_min_val(set, aff);
1914 min_ok = isl_val_is_negone(val);
1915 isl_val_free(val);
1916 val = isl_set_max_val(set, aff);
1917 max_ok = isl_val_is_one(val);
1918 isl_val_free(val);
1919 isl_aff_free(aff);
1920 isl_set_free(set);
1922 if (min_ok < 0 || max_ok < 0)
1923 return -1;
1924 if (!min_ok)
1925 isl_die(ctx, isl_error_unknown,
1926 "unexpected minimum", return -1);
1927 if (!max_ok)
1928 isl_die(ctx, isl_error_unknown,
1929 "unexpected maximum", return -1);
1931 return 0;
1934 struct must_may {
1935 isl_map *must;
1936 isl_map *may;
1939 static int collect_must_may(__isl_take isl_map *dep, int must,
1940 void *dep_user, void *user)
1942 struct must_may *mm = (struct must_may *)user;
1944 if (must)
1945 mm->must = isl_map_union(mm->must, dep);
1946 else
1947 mm->may = isl_map_union(mm->may, dep);
1949 return 0;
1952 static int common_space(void *first, void *second)
1954 int depth = *(int *)first;
1955 return 2 * depth;
1958 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1960 isl_map *map2;
1961 int equal;
1963 if (!map)
1964 return -1;
1966 map2 = isl_map_read_from_str(map->ctx, str);
1967 equal = isl_map_is_equal(map, map2);
1968 isl_map_free(map2);
1970 return equal;
1973 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1975 int equal;
1977 equal = map_is_equal(map, str);
1978 if (equal < 0)
1979 return -1;
1980 if (!equal)
1981 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1982 "result not as expected", return -1);
1983 return 0;
1986 void test_dep(struct isl_ctx *ctx)
1988 const char *str;
1989 isl_space *dim;
1990 isl_map *map;
1991 isl_access_info *ai;
1992 isl_flow *flow;
1993 int depth;
1994 struct must_may mm;
1996 depth = 3;
1998 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1999 map = isl_map_read_from_str(ctx, str);
2000 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2002 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2003 map = isl_map_read_from_str(ctx, str);
2004 ai = isl_access_info_add_source(ai, map, 1, &depth);
2006 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2007 map = isl_map_read_from_str(ctx, str);
2008 ai = isl_access_info_add_source(ai, map, 1, &depth);
2010 flow = isl_access_info_compute_flow(ai);
2011 dim = isl_space_alloc(ctx, 0, 3, 3);
2012 mm.must = isl_map_empty(isl_space_copy(dim));
2013 mm.may = isl_map_empty(dim);
2015 isl_flow_foreach(flow, collect_must_may, &mm);
2017 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2018 " [1,10,0] -> [2,5,0] }";
2019 assert(map_is_equal(mm.must, str));
2020 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2021 assert(map_is_equal(mm.may, str));
2023 isl_map_free(mm.must);
2024 isl_map_free(mm.may);
2025 isl_flow_free(flow);
2028 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2029 map = isl_map_read_from_str(ctx, str);
2030 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2032 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2033 map = isl_map_read_from_str(ctx, str);
2034 ai = isl_access_info_add_source(ai, map, 1, &depth);
2036 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2037 map = isl_map_read_from_str(ctx, str);
2038 ai = isl_access_info_add_source(ai, map, 0, &depth);
2040 flow = isl_access_info_compute_flow(ai);
2041 dim = isl_space_alloc(ctx, 0, 3, 3);
2042 mm.must = isl_map_empty(isl_space_copy(dim));
2043 mm.may = isl_map_empty(dim);
2045 isl_flow_foreach(flow, collect_must_may, &mm);
2047 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2048 assert(map_is_equal(mm.must, str));
2049 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2050 assert(map_is_equal(mm.may, str));
2052 isl_map_free(mm.must);
2053 isl_map_free(mm.may);
2054 isl_flow_free(flow);
2057 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2058 map = isl_map_read_from_str(ctx, str);
2059 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2061 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2062 map = isl_map_read_from_str(ctx, str);
2063 ai = isl_access_info_add_source(ai, map, 0, &depth);
2065 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2066 map = isl_map_read_from_str(ctx, str);
2067 ai = isl_access_info_add_source(ai, map, 0, &depth);
2069 flow = isl_access_info_compute_flow(ai);
2070 dim = isl_space_alloc(ctx, 0, 3, 3);
2071 mm.must = isl_map_empty(isl_space_copy(dim));
2072 mm.may = isl_map_empty(dim);
2074 isl_flow_foreach(flow, collect_must_may, &mm);
2076 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2077 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2078 assert(map_is_equal(mm.may, str));
2079 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2080 assert(map_is_equal(mm.must, str));
2082 isl_map_free(mm.must);
2083 isl_map_free(mm.may);
2084 isl_flow_free(flow);
2087 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2088 map = isl_map_read_from_str(ctx, str);
2089 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2091 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2092 map = isl_map_read_from_str(ctx, str);
2093 ai = isl_access_info_add_source(ai, map, 0, &depth);
2095 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2096 map = isl_map_read_from_str(ctx, str);
2097 ai = isl_access_info_add_source(ai, map, 0, &depth);
2099 flow = isl_access_info_compute_flow(ai);
2100 dim = isl_space_alloc(ctx, 0, 3, 3);
2101 mm.must = isl_map_empty(isl_space_copy(dim));
2102 mm.may = isl_map_empty(dim);
2104 isl_flow_foreach(flow, collect_must_may, &mm);
2106 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2107 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2108 assert(map_is_equal(mm.may, str));
2109 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2110 assert(map_is_equal(mm.must, str));
2112 isl_map_free(mm.must);
2113 isl_map_free(mm.may);
2114 isl_flow_free(flow);
2117 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2118 map = isl_map_read_from_str(ctx, str);
2119 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2121 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2122 map = isl_map_read_from_str(ctx, str);
2123 ai = isl_access_info_add_source(ai, map, 0, &depth);
2125 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2126 map = isl_map_read_from_str(ctx, str);
2127 ai = isl_access_info_add_source(ai, map, 0, &depth);
2129 flow = isl_access_info_compute_flow(ai);
2130 dim = isl_space_alloc(ctx, 0, 3, 3);
2131 mm.must = isl_map_empty(isl_space_copy(dim));
2132 mm.may = isl_map_empty(dim);
2134 isl_flow_foreach(flow, collect_must_may, &mm);
2136 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2137 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2138 assert(map_is_equal(mm.may, str));
2139 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2140 assert(map_is_equal(mm.must, str));
2142 isl_map_free(mm.must);
2143 isl_map_free(mm.may);
2144 isl_flow_free(flow);
2147 depth = 5;
2149 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2150 map = isl_map_read_from_str(ctx, str);
2151 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2153 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2154 map = isl_map_read_from_str(ctx, str);
2155 ai = isl_access_info_add_source(ai, map, 1, &depth);
2157 flow = isl_access_info_compute_flow(ai);
2158 dim = isl_space_alloc(ctx, 0, 5, 5);
2159 mm.must = isl_map_empty(isl_space_copy(dim));
2160 mm.may = isl_map_empty(dim);
2162 isl_flow_foreach(flow, collect_must_may, &mm);
2164 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2165 assert(map_is_equal(mm.must, str));
2166 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2167 assert(map_is_equal(mm.may, str));
2169 isl_map_free(mm.must);
2170 isl_map_free(mm.may);
2171 isl_flow_free(flow);
2174 /* Check that the dependence analysis proceeds without errors.
2175 * Earlier versions of isl would break down during the analysis
2176 * due to the use of the wrong spaces.
2178 static int test_flow(isl_ctx *ctx)
2180 const char *str;
2181 isl_union_map *access, *schedule;
2182 isl_union_map *must_dep, *may_dep;
2183 int r;
2185 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2186 access = isl_union_map_read_from_str(ctx, str);
2187 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2188 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2189 "S2[] -> [1,0,0,0]; "
2190 "S3[] -> [-1,0,0,0] }";
2191 schedule = isl_union_map_read_from_str(ctx, str);
2192 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2193 isl_union_map_copy(access), schedule,
2194 &must_dep, &may_dep, NULL, NULL);
2195 isl_union_map_free(may_dep);
2196 isl_union_map_free(must_dep);
2198 return r;
2201 struct {
2202 const char *map;
2203 int sv;
2204 } sv_tests[] = {
2205 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2206 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2207 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2208 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2209 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2210 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2211 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2212 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2213 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2216 int test_sv(isl_ctx *ctx)
2218 isl_union_map *umap;
2219 int i;
2220 int sv;
2222 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2223 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2224 sv = isl_union_map_is_single_valued(umap);
2225 isl_union_map_free(umap);
2226 if (sv < 0)
2227 return -1;
2228 if (sv_tests[i].sv && !sv)
2229 isl_die(ctx, isl_error_internal,
2230 "map not detected as single valued", return -1);
2231 if (!sv_tests[i].sv && sv)
2232 isl_die(ctx, isl_error_internal,
2233 "map detected as single valued", return -1);
2236 return 0;
2239 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
2241 isl_map *map;
2243 map = isl_map_read_from_str(ctx, str);
2244 if (bijective)
2245 assert(isl_map_is_bijective(map));
2246 else
2247 assert(!isl_map_is_bijective(map));
2248 isl_map_free(map);
2251 void test_bijective(struct isl_ctx *ctx)
2253 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
2254 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
2255 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
2256 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
2257 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
2258 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
2259 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
2260 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
2261 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
2262 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
2263 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
2264 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
2265 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
2268 /* Inputs for isl_pw_qpolynomial_gist tests.
2269 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2271 struct {
2272 const char *pwqp;
2273 const char *set;
2274 const char *gist;
2275 } pwqp_gist_tests[] = {
2276 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2277 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2278 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2279 "{ [i] -> -1/2 + 1/2 * i }" },
2280 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2283 static int test_pwqp(struct isl_ctx *ctx)
2285 int i;
2286 const char *str;
2287 isl_set *set;
2288 isl_pw_qpolynomial *pwqp1, *pwqp2;
2289 int equal;
2291 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2292 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2294 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2295 isl_dim_in, 1, 1);
2297 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2298 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2300 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2302 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2304 isl_pw_qpolynomial_free(pwqp1);
2306 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2307 str = pwqp_gist_tests[i].pwqp;
2308 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2309 str = pwqp_gist_tests[i].set;
2310 set = isl_set_read_from_str(ctx, str);
2311 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2312 str = pwqp_gist_tests[i].gist;
2313 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2314 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2315 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2316 isl_pw_qpolynomial_free(pwqp1);
2318 if (equal < 0)
2319 return -1;
2320 if (!equal)
2321 isl_die(ctx, isl_error_unknown,
2322 "unexpected result", return -1);
2325 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2326 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2327 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2328 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2330 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2332 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2334 isl_pw_qpolynomial_free(pwqp1);
2336 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2337 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2338 str = "{ [x] -> x }";
2339 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2341 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2343 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2345 isl_pw_qpolynomial_free(pwqp1);
2347 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2348 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2349 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2350 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2351 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2352 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2353 isl_pw_qpolynomial_free(pwqp1);
2355 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2356 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2357 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2358 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2359 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2360 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2361 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2362 isl_pw_qpolynomial_free(pwqp1);
2363 isl_pw_qpolynomial_free(pwqp2);
2364 if (equal < 0)
2365 return -1;
2366 if (!equal)
2367 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2369 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2370 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2371 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2372 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2373 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2374 isl_val_one(ctx));
2375 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2376 isl_pw_qpolynomial_free(pwqp1);
2377 isl_pw_qpolynomial_free(pwqp2);
2378 if (equal < 0)
2379 return -1;
2380 if (!equal)
2381 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2383 return 0;
2386 void test_split_periods(isl_ctx *ctx)
2388 const char *str;
2389 isl_pw_qpolynomial *pwqp;
2391 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2392 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2393 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2394 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2396 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2397 assert(pwqp);
2399 isl_pw_qpolynomial_free(pwqp);
2402 void test_union(isl_ctx *ctx)
2404 const char *str;
2405 isl_union_set *uset1, *uset2;
2406 isl_union_map *umap1, *umap2;
2408 str = "{ [i] : 0 <= i <= 1 }";
2409 uset1 = isl_union_set_read_from_str(ctx, str);
2410 str = "{ [1] -> [0] }";
2411 umap1 = isl_union_map_read_from_str(ctx, str);
2413 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2414 assert(isl_union_map_is_equal(umap1, umap2));
2416 isl_union_map_free(umap1);
2417 isl_union_map_free(umap2);
2419 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2420 umap1 = isl_union_map_read_from_str(ctx, str);
2421 str = "{ A[i]; B[i] }";
2422 uset1 = isl_union_set_read_from_str(ctx, str);
2424 uset2 = isl_union_map_domain(umap1);
2426 assert(isl_union_set_is_equal(uset1, uset2));
2428 isl_union_set_free(uset1);
2429 isl_union_set_free(uset2);
2432 void test_bound(isl_ctx *ctx)
2434 const char *str;
2435 isl_pw_qpolynomial *pwqp;
2436 isl_pw_qpolynomial_fold *pwf;
2438 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2439 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2440 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2441 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
2442 isl_pw_qpolynomial_fold_free(pwf);
2444 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2445 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2446 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2447 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
2448 isl_pw_qpolynomial_fold_free(pwf);
2451 void test_lift(isl_ctx *ctx)
2453 const char *str;
2454 isl_basic_map *bmap;
2455 isl_basic_set *bset;
2457 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2458 bset = isl_basic_set_read_from_str(ctx, str);
2459 bset = isl_basic_set_lift(bset);
2460 bmap = isl_basic_map_from_range(bset);
2461 bset = isl_basic_map_domain(bmap);
2462 isl_basic_set_free(bset);
2465 struct {
2466 const char *set1;
2467 const char *set2;
2468 int subset;
2469 } subset_tests[] = {
2470 { "{ [112, 0] }",
2471 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2472 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2473 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2474 { "{ [65] }",
2475 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2476 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2477 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2478 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2479 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2480 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2481 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2482 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2483 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2484 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2485 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2486 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2487 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2488 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
2489 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
2490 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
2491 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
2492 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
2493 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
2494 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
2495 "4e0 <= -57 + i0 + i1)) or "
2496 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
2497 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
2498 "4e0 >= -61 + i0 + i1)) or "
2499 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
2502 static int test_subset(isl_ctx *ctx)
2504 int i;
2505 isl_set *set1, *set2;
2506 int subset;
2508 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2509 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2510 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2511 subset = isl_set_is_subset(set1, set2);
2512 isl_set_free(set1);
2513 isl_set_free(set2);
2514 if (subset < 0)
2515 return -1;
2516 if (subset != subset_tests[i].subset)
2517 isl_die(ctx, isl_error_unknown,
2518 "incorrect subset result", return -1);
2521 return 0;
2524 struct {
2525 const char *minuend;
2526 const char *subtrahend;
2527 const char *difference;
2528 } subtract_domain_tests[] = {
2529 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2530 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2531 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2534 static int test_subtract(isl_ctx *ctx)
2536 int i;
2537 isl_union_map *umap1, *umap2;
2538 isl_union_pw_multi_aff *upma1, *upma2;
2539 isl_union_set *uset;
2540 int equal;
2542 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2543 umap1 = isl_union_map_read_from_str(ctx,
2544 subtract_domain_tests[i].minuend);
2545 uset = isl_union_set_read_from_str(ctx,
2546 subtract_domain_tests[i].subtrahend);
2547 umap2 = isl_union_map_read_from_str(ctx,
2548 subtract_domain_tests[i].difference);
2549 umap1 = isl_union_map_subtract_domain(umap1, uset);
2550 equal = isl_union_map_is_equal(umap1, umap2);
2551 isl_union_map_free(umap1);
2552 isl_union_map_free(umap2);
2553 if (equal < 0)
2554 return -1;
2555 if (!equal)
2556 isl_die(ctx, isl_error_unknown,
2557 "incorrect subtract domain result", return -1);
2560 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2561 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
2562 subtract_domain_tests[i].minuend);
2563 uset = isl_union_set_read_from_str(ctx,
2564 subtract_domain_tests[i].subtrahend);
2565 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
2566 subtract_domain_tests[i].difference);
2567 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
2568 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
2569 isl_union_pw_multi_aff_free(upma1);
2570 isl_union_pw_multi_aff_free(upma2);
2571 if (equal < 0)
2572 return -1;
2573 if (!equal)
2574 isl_die(ctx, isl_error_unknown,
2575 "incorrect subtract domain result", return -1);
2578 return 0;
2581 int test_factorize(isl_ctx *ctx)
2583 const char *str;
2584 isl_basic_set *bset;
2585 isl_factorizer *f;
2587 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2588 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2589 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2590 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2591 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2592 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2593 "3i5 >= -2i0 - i2 + 3i4 }";
2594 bset = isl_basic_set_read_from_str(ctx, str);
2595 f = isl_basic_set_factorizer(bset);
2596 isl_basic_set_free(bset);
2597 isl_factorizer_free(f);
2598 if (!f)
2599 isl_die(ctx, isl_error_unknown,
2600 "failed to construct factorizer", return -1);
2602 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2603 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2604 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2605 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2606 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2607 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2608 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2609 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2610 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2611 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2612 bset = isl_basic_set_read_from_str(ctx, str);
2613 f = isl_basic_set_factorizer(bset);
2614 isl_basic_set_free(bset);
2615 isl_factorizer_free(f);
2616 if (!f)
2617 isl_die(ctx, isl_error_unknown,
2618 "failed to construct factorizer", return -1);
2620 return 0;
2623 static int check_injective(__isl_take isl_map *map, void *user)
2625 int *injective = user;
2627 *injective = isl_map_is_injective(map);
2628 isl_map_free(map);
2630 if (*injective < 0 || !*injective)
2631 return -1;
2633 return 0;
2636 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2637 const char *r, const char *s, int tilable, int parallel)
2639 int i;
2640 isl_union_set *D;
2641 isl_union_map *W, *R, *S;
2642 isl_union_map *empty;
2643 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2644 isl_union_map *validity, *proximity, *coincidence;
2645 isl_union_map *schedule;
2646 isl_union_map *test;
2647 isl_union_set *delta;
2648 isl_union_set *domain;
2649 isl_set *delta_set;
2650 isl_set *slice;
2651 isl_set *origin;
2652 isl_schedule_constraints *sc;
2653 isl_schedule *sched;
2654 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2656 D = isl_union_set_read_from_str(ctx, d);
2657 W = isl_union_map_read_from_str(ctx, w);
2658 R = isl_union_map_read_from_str(ctx, r);
2659 S = isl_union_map_read_from_str(ctx, s);
2661 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2662 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2664 empty = isl_union_map_empty(isl_union_map_get_space(S));
2665 isl_union_map_compute_flow(isl_union_map_copy(R),
2666 isl_union_map_copy(W), empty,
2667 isl_union_map_copy(S),
2668 &dep_raw, NULL, NULL, NULL);
2669 isl_union_map_compute_flow(isl_union_map_copy(W),
2670 isl_union_map_copy(W),
2671 isl_union_map_copy(R),
2672 isl_union_map_copy(S),
2673 &dep_waw, &dep_war, NULL, NULL);
2675 dep = isl_union_map_union(dep_waw, dep_war);
2676 dep = isl_union_map_union(dep, dep_raw);
2677 validity = isl_union_map_copy(dep);
2678 coincidence = isl_union_map_copy(dep);
2679 proximity = isl_union_map_copy(dep);
2681 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
2682 sc = isl_schedule_constraints_set_validity(sc, validity);
2683 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
2684 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2685 sched = isl_schedule_constraints_compute_schedule(sc);
2686 schedule = isl_schedule_get_map(sched);
2687 isl_schedule_free(sched);
2688 isl_union_map_free(W);
2689 isl_union_map_free(R);
2690 isl_union_map_free(S);
2692 is_injection = 1;
2693 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2695 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2696 is_complete = isl_union_set_is_subset(D, domain);
2697 isl_union_set_free(D);
2698 isl_union_set_free(domain);
2700 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2701 test = isl_union_map_apply_range(test, dep);
2702 test = isl_union_map_apply_range(test, schedule);
2704 delta = isl_union_map_deltas(test);
2705 if (isl_union_set_n_set(delta) == 0) {
2706 is_tilable = 1;
2707 is_parallel = 1;
2708 is_nonneg = 1;
2709 isl_union_set_free(delta);
2710 } else {
2711 delta_set = isl_set_from_union_set(delta);
2713 slice = isl_set_universe(isl_set_get_space(delta_set));
2714 for (i = 0; i < tilable; ++i)
2715 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2716 is_tilable = isl_set_is_subset(delta_set, slice);
2717 isl_set_free(slice);
2719 slice = isl_set_universe(isl_set_get_space(delta_set));
2720 for (i = 0; i < parallel; ++i)
2721 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2722 is_parallel = isl_set_is_subset(delta_set, slice);
2723 isl_set_free(slice);
2725 origin = isl_set_universe(isl_set_get_space(delta_set));
2726 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2727 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2729 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2730 delta_set = isl_set_lexmin(delta_set);
2732 is_nonneg = isl_set_is_equal(delta_set, origin);
2734 isl_set_free(origin);
2735 isl_set_free(delta_set);
2738 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2739 is_injection < 0 || is_complete < 0)
2740 return -1;
2741 if (!is_complete)
2742 isl_die(ctx, isl_error_unknown,
2743 "generated schedule incomplete", return -1);
2744 if (!is_injection)
2745 isl_die(ctx, isl_error_unknown,
2746 "generated schedule not injective on each statement",
2747 return -1);
2748 if (!is_nonneg)
2749 isl_die(ctx, isl_error_unknown,
2750 "negative dependences in generated schedule",
2751 return -1);
2752 if (!is_tilable)
2753 isl_die(ctx, isl_error_unknown,
2754 "generated schedule not as tilable as expected",
2755 return -1);
2756 if (!is_parallel)
2757 isl_die(ctx, isl_error_unknown,
2758 "generated schedule not as parallel as expected",
2759 return -1);
2761 return 0;
2764 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2765 const char *domain, const char *validity, const char *proximity)
2767 isl_union_set *dom;
2768 isl_union_map *dep;
2769 isl_union_map *prox;
2770 isl_schedule_constraints *sc;
2771 isl_schedule *schedule;
2772 isl_union_map *sched;
2774 dom = isl_union_set_read_from_str(ctx, domain);
2775 dep = isl_union_map_read_from_str(ctx, validity);
2776 prox = isl_union_map_read_from_str(ctx, proximity);
2777 sc = isl_schedule_constraints_on_domain(dom);
2778 sc = isl_schedule_constraints_set_validity(sc, dep);
2779 sc = isl_schedule_constraints_set_proximity(sc, prox);
2780 schedule = isl_schedule_constraints_compute_schedule(sc);
2781 sched = isl_schedule_get_map(schedule);
2782 isl_schedule_free(schedule);
2784 return sched;
2787 /* Check that a schedule can be constructed on the given domain
2788 * with the given validity and proximity constraints.
2790 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2791 const char *validity, const char *proximity)
2793 isl_union_map *sched;
2795 sched = compute_schedule(ctx, domain, validity, proximity);
2796 if (!sched)
2797 return -1;
2799 isl_union_map_free(sched);
2800 return 0;
2803 int test_special_schedule(isl_ctx *ctx, const char *domain,
2804 const char *validity, const char *proximity, const char *expected_sched)
2806 isl_union_map *sched1, *sched2;
2807 int equal;
2809 sched1 = compute_schedule(ctx, domain, validity, proximity);
2810 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2812 equal = isl_union_map_is_equal(sched1, sched2);
2813 isl_union_map_free(sched1);
2814 isl_union_map_free(sched2);
2816 if (equal < 0)
2817 return -1;
2818 if (!equal)
2819 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2820 return -1);
2822 return 0;
2825 /* Check that the schedule map is properly padded, even after being
2826 * reconstructed from the band forest.
2828 static int test_padded_schedule(isl_ctx *ctx)
2830 const char *str;
2831 isl_union_set *D;
2832 isl_union_map *validity, *proximity;
2833 isl_schedule_constraints *sc;
2834 isl_schedule *sched;
2835 isl_union_map *map1, *map2;
2836 isl_band_list *list;
2837 int equal;
2839 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2840 D = isl_union_set_read_from_str(ctx, str);
2841 validity = isl_union_map_empty(isl_union_set_get_space(D));
2842 proximity = isl_union_map_copy(validity);
2843 sc = isl_schedule_constraints_on_domain(D);
2844 sc = isl_schedule_constraints_set_validity(sc, validity);
2845 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2846 sched = isl_schedule_constraints_compute_schedule(sc);
2847 map1 = isl_schedule_get_map(sched);
2848 list = isl_schedule_get_band_forest(sched);
2849 isl_band_list_free(list);
2850 map2 = isl_schedule_get_map(sched);
2851 isl_schedule_free(sched);
2852 equal = isl_union_map_is_equal(map1, map2);
2853 isl_union_map_free(map1);
2854 isl_union_map_free(map2);
2856 if (equal < 0)
2857 return -1;
2858 if (!equal)
2859 isl_die(ctx, isl_error_unknown,
2860 "reconstructed schedule map not the same as original",
2861 return -1);
2863 return 0;
2866 /* Input for testing of schedule construction based on
2867 * conditional constraints.
2869 * domain is the iteration domain
2870 * flow are the flow dependences, which determine the validity and
2871 * proximity constraints
2872 * condition are the conditions on the conditional validity constraints
2873 * conditional_validity are the conditional validity constraints
2874 * outer_band_n is the expected number of members in the outer band
2876 struct {
2877 const char *domain;
2878 const char *flow;
2879 const char *condition;
2880 const char *conditional_validity;
2881 int outer_band_n;
2882 } live_range_tests[] = {
2883 /* Contrived example that illustrates that we need to keep
2884 * track of tagged condition dependences and
2885 * tagged conditional validity dependences
2886 * in isl_sched_edge separately.
2887 * In particular, the conditional validity constraints on A
2888 * cannot be satisfied,
2889 * but they can be ignored because there are no corresponding
2890 * condition constraints. However, we do have an additional
2891 * conditional validity constraint that maps to the same
2892 * dependence relation
2893 * as the condition constraint on B. If we did not make a distinction
2894 * between tagged condition and tagged conditional validity
2895 * dependences, then we
2896 * could end up treating this shared dependence as an condition
2897 * constraint on A, forcing a localization of the conditions,
2898 * which is impossible.
2900 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
2901 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
2902 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
2903 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2904 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2905 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
2908 /* TACO 2013 Fig. 7 */
2909 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2910 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2911 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2912 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
2913 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
2914 "0 <= i < n and 0 <= j < n - 1 }",
2915 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
2916 "0 <= i < n and 0 <= j < j' < n;"
2917 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
2918 "0 <= i < i' < n and 0 <= j,j' < n;"
2919 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
2920 "0 <= i,j,j' < n and j < j' }",
2923 /* TACO 2013 Fig. 7, without tags */
2924 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2925 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2926 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2927 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2928 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2929 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
2930 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
2931 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
2934 /* TACO 2013 Fig. 12 */
2935 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
2936 "S3[i,3] : 0 <= i <= 1 }",
2937 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
2938 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
2939 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
2940 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
2941 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2942 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
2943 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2944 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
2945 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
2946 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
2947 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
2952 /* Test schedule construction based on conditional constraints.
2953 * In particular, check the number of members in the outer band
2954 * as an indication of whether tiling is possible or not.
2956 static int test_conditional_schedule_constraints(isl_ctx *ctx)
2958 int i;
2959 isl_union_set *domain;
2960 isl_union_map *condition;
2961 isl_union_map *flow;
2962 isl_union_map *validity;
2963 isl_schedule_constraints *sc;
2964 isl_schedule *schedule;
2965 isl_band_list *list;
2966 isl_band *band;
2967 int n_member;
2969 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
2970 domain = isl_union_set_read_from_str(ctx,
2971 live_range_tests[i].domain);
2972 flow = isl_union_map_read_from_str(ctx,
2973 live_range_tests[i].flow);
2974 condition = isl_union_map_read_from_str(ctx,
2975 live_range_tests[i].condition);
2976 validity = isl_union_map_read_from_str(ctx,
2977 live_range_tests[i].conditional_validity);
2978 sc = isl_schedule_constraints_on_domain(domain);
2979 sc = isl_schedule_constraints_set_validity(sc,
2980 isl_union_map_copy(flow));
2981 sc = isl_schedule_constraints_set_proximity(sc, flow);
2982 sc = isl_schedule_constraints_set_conditional_validity(sc,
2983 condition, validity);
2984 schedule = isl_schedule_constraints_compute_schedule(sc);
2985 list = isl_schedule_get_band_forest(schedule);
2986 band = isl_band_list_get_band(list, 0);
2987 n_member = isl_band_n_member(band);
2988 isl_band_free(band);
2989 isl_band_list_free(list);
2990 isl_schedule_free(schedule);
2992 if (!schedule)
2993 return -1;
2994 if (n_member != live_range_tests[i].outer_band_n)
2995 isl_die(ctx, isl_error_unknown,
2996 "unexpected number of members in outer band",
2997 return -1);
2999 return 0;
3002 int test_schedule(isl_ctx *ctx)
3004 const char *D, *W, *R, *V, *P, *S;
3006 /* Handle resulting schedule with zero bands. */
3007 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
3008 return -1;
3010 /* Jacobi */
3011 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
3012 W = "{ S1[t,i] -> a[t,i] }";
3013 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
3014 "S1[t,i] -> a[t-1,i+1] }";
3015 S = "{ S1[t,i] -> [t,i] }";
3016 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3017 return -1;
3019 /* Fig. 5 of CC2008 */
3020 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
3021 "j <= -1 + N }";
3022 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
3023 "j >= 2 and j <= -1 + N }";
3024 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
3025 "j >= 2 and j <= -1 + N; "
3026 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
3027 "j >= 2 and j <= -1 + N }";
3028 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3029 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3030 return -1;
3032 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
3033 W = "{ S1[i] -> a[i] }";
3034 R = "{ S2[i] -> a[i+1] }";
3035 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3036 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3037 return -1;
3039 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
3040 W = "{ S1[i] -> a[i] }";
3041 R = "{ S2[i] -> a[9-i] }";
3042 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3043 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3044 return -1;
3046 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
3047 W = "{ S1[i] -> a[i] }";
3048 R = "[N] -> { S2[i] -> a[N-1-i] }";
3049 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3050 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3051 return -1;
3053 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
3054 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
3055 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
3056 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
3057 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3058 return -1;
3060 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3061 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
3062 R = "{ S2[i,j] -> a[i-1,j] }";
3063 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3064 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3065 return -1;
3067 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3068 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
3069 R = "{ S2[i,j] -> a[i,j-1] }";
3070 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3071 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3072 return -1;
3074 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
3075 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
3076 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
3077 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
3078 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
3079 "S_0[] -> [0, 0, 0] }";
3080 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
3081 return -1;
3082 ctx->opt->schedule_parametric = 0;
3083 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3084 return -1;
3085 ctx->opt->schedule_parametric = 1;
3087 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
3088 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
3089 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
3090 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
3091 "S4[i] -> a[i,N] }";
3092 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
3093 "S4[i] -> [4,i,0] }";
3094 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3095 return -1;
3097 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
3098 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3099 "j <= N }";
3100 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3101 "j <= N; "
3102 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
3103 "j <= N }";
3104 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3105 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3106 return -1;
3108 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
3109 " S_2[t] : t >= 0 and t <= -1 + N; "
3110 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
3111 "i <= -1 + N }";
3112 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
3113 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
3114 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
3115 "i >= 0 and i <= -1 + N }";
3116 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
3117 "i >= 0 and i <= -1 + N; "
3118 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
3119 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
3120 " S_0[t] -> [0, t, 0] }";
3122 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3123 return -1;
3124 ctx->opt->schedule_parametric = 0;
3125 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3126 return -1;
3127 ctx->opt->schedule_parametric = 1;
3129 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
3130 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
3131 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
3132 return -1;
3134 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
3135 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
3136 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
3137 "j >= 0 and j <= -1 + N; "
3138 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3139 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
3140 "j >= 0 and j <= -1 + N; "
3141 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3142 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
3143 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3144 return -1;
3146 D = "{ S_0[i] : i >= 0 }";
3147 W = "{ S_0[i] -> a[i] : i >= 0 }";
3148 R = "{ S_0[i] -> a[0] : i >= 0 }";
3149 S = "{ S_0[i] -> [0, i, 0] }";
3150 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3151 return -1;
3153 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
3154 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
3155 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
3156 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
3157 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3158 return -1;
3160 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
3161 "k <= -1 + n and k >= 0 }";
3162 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
3163 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
3164 "k <= -1 + n and k >= 0; "
3165 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
3166 "k <= -1 + n and k >= 0; "
3167 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
3168 "k <= -1 + n and k >= 0 }";
3169 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
3170 ctx->opt->schedule_outer_coincidence = 1;
3171 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3172 return -1;
3173 ctx->opt->schedule_outer_coincidence = 0;
3175 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3176 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3177 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3178 "Stmt_for_body24[i0, i1, 1, 0]:"
3179 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3180 "Stmt_for_body7[i0, i1, i2]:"
3181 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3182 "i2 <= 7 }";
3184 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3185 "Stmt_for_body24[1, i1, i2, i3]:"
3186 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3187 "i2 >= 1;"
3188 "Stmt_for_body24[0, i1, i2, i3] -> "
3189 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3190 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3191 "i3 >= 0;"
3192 "Stmt_for_body24[0, i1, i2, i3] ->"
3193 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3194 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3195 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3196 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3197 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3198 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3199 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3200 "i1 <= 6 and i1 >= 0;"
3201 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3202 "Stmt_for_body7[i0, i1, i2] -> "
3203 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3204 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3205 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3206 "Stmt_for_body7[i0, i1, i2] -> "
3207 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3208 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3209 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3210 P = V;
3211 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3212 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3213 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3215 if (test_special_schedule(ctx, D, V, P, S) < 0)
3216 return -1;
3218 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3219 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3220 "j >= 1 and j <= 7;"
3221 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3222 "j >= 1 and j <= 8 }";
3223 P = "{ }";
3224 S = "{ S_0[i, j] -> [i + j, j] }";
3225 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3226 if (test_special_schedule(ctx, D, V, P, S) < 0)
3227 return -1;
3228 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3230 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3231 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3232 "j >= 0 and j <= -1 + i }";
3233 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3234 "i <= -1 + N and j >= 0;"
3235 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3236 "i <= -2 + N }";
3237 P = "{ }";
3238 S = "{ S_0[i, j] -> [i, j] }";
3239 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3240 if (test_special_schedule(ctx, D, V, P, S) < 0)
3241 return -1;
3242 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3244 /* Test both algorithms on a case with only proximity dependences. */
3245 D = "{ S[i,j] : 0 <= i <= 10 }";
3246 V = "{ }";
3247 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3248 S = "{ S[i, j] -> [j, i] }";
3249 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3250 if (test_special_schedule(ctx, D, V, P, S) < 0)
3251 return -1;
3252 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3253 if (test_special_schedule(ctx, D, V, P, S) < 0)
3254 return -1;
3256 D = "{ A[a]; B[] }";
3257 V = "{}";
3258 P = "{ A[a] -> B[] }";
3259 if (test_has_schedule(ctx, D, V, P) < 0)
3260 return -1;
3262 if (test_padded_schedule(ctx) < 0)
3263 return -1;
3265 /* Check that check for progress is not confused by rational
3266 * solution.
3268 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3269 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3270 "i0 <= -2 + N; "
3271 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3272 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3273 P = "{}";
3274 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3275 if (test_has_schedule(ctx, D, V, P) < 0)
3276 return -1;
3277 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3279 /* Check that we allow schedule rows that are only non-trivial
3280 * on some full-dimensional domains.
3282 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
3283 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
3284 "S1[j] -> S2[1] : 0 <= j <= 1 }";
3285 P = "{}";
3286 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3287 if (test_has_schedule(ctx, D, V, P) < 0)
3288 return -1;
3289 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3291 if (test_conditional_schedule_constraints(ctx) < 0)
3292 return -1;
3294 return 0;
3297 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3299 isl_union_map *umap;
3300 int test;
3302 umap = isl_union_map_read_from_str(ctx, str);
3303 test = isl_union_map_plain_is_injective(umap);
3304 isl_union_map_free(umap);
3305 if (test < 0)
3306 return -1;
3307 if (test == injective)
3308 return 0;
3309 if (injective)
3310 isl_die(ctx, isl_error_unknown,
3311 "map not detected as injective", return -1);
3312 else
3313 isl_die(ctx, isl_error_unknown,
3314 "map detected as injective", return -1);
3317 int test_injective(isl_ctx *ctx)
3319 const char *str;
3321 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
3322 return -1;
3323 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
3324 return -1;
3325 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
3326 return -1;
3327 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
3328 return -1;
3329 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
3330 return -1;
3331 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
3332 return -1;
3333 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
3334 return -1;
3335 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
3336 return -1;
3338 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
3339 if (test_plain_injective(ctx, str, 1))
3340 return -1;
3341 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
3342 if (test_plain_injective(ctx, str, 0))
3343 return -1;
3345 return 0;
3348 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
3350 isl_aff *aff2;
3351 int equal;
3353 if (!aff)
3354 return -1;
3356 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
3357 equal = isl_aff_plain_is_equal(aff, aff2);
3358 isl_aff_free(aff2);
3360 return equal;
3363 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
3365 int equal;
3367 equal = aff_plain_is_equal(aff, str);
3368 if (equal < 0)
3369 return -1;
3370 if (!equal)
3371 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
3372 "result not as expected", return -1);
3373 return 0;
3376 struct {
3377 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3378 __isl_take isl_aff *aff2);
3379 } aff_bin_op[] = {
3380 ['+'] = { &isl_aff_add },
3381 ['-'] = { &isl_aff_sub },
3382 ['*'] = { &isl_aff_mul },
3383 ['/'] = { &isl_aff_div },
3386 struct {
3387 const char *arg1;
3388 unsigned char op;
3389 const char *arg2;
3390 const char *res;
3391 } aff_bin_tests[] = {
3392 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
3393 "{ [i] -> [2i] }" },
3394 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
3395 "{ [i] -> [0] }" },
3396 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
3397 "{ [i] -> [2i] }" },
3398 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
3399 "{ [i] -> [2i] }" },
3400 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
3401 "{ [i] -> [i/2] }" },
3402 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
3403 "{ [i] -> [i] }" },
3404 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
3405 "{ [i] -> [NaN] }" },
3406 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
3407 "{ [i] -> [NaN] }" },
3408 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
3409 "{ [i] -> [NaN] }" },
3410 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
3411 "{ [i] -> [NaN] }" },
3412 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
3413 "{ [i] -> [NaN] }" },
3414 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
3415 "{ [i] -> [NaN] }" },
3416 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
3417 "{ [i] -> [NaN] }" },
3418 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
3419 "{ [i] -> [NaN] }" },
3420 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
3421 "{ [i] -> [NaN] }" },
3422 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
3423 "{ [i] -> [NaN] }" },
3424 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
3425 "{ [i] -> [NaN] }" },
3426 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
3427 "{ [i] -> [NaN] }" },
3430 /* Perform some basic tests of binary operations on isl_aff objects.
3432 static int test_bin_aff(isl_ctx *ctx)
3434 int i;
3435 isl_aff *aff1, *aff2, *res;
3436 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3437 __isl_take isl_aff *aff2);
3438 int ok;
3440 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
3441 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
3442 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
3443 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
3444 fn = aff_bin_op[aff_bin_tests[i].op].fn;
3445 aff1 = fn(aff1, aff2);
3446 if (isl_aff_is_nan(res))
3447 ok = isl_aff_is_nan(aff1);
3448 else
3449 ok = isl_aff_plain_is_equal(aff1, res);
3450 isl_aff_free(aff1);
3451 isl_aff_free(res);
3452 if (ok < 0)
3453 return -1;
3454 if (!ok)
3455 isl_die(ctx, isl_error_unknown,
3456 "unexpected result", return -1);
3459 return 0;
3462 struct {
3463 __isl_give isl_union_pw_multi_aff *(*fn)(
3464 __isl_take isl_union_pw_multi_aff *upma1,
3465 __isl_take isl_union_pw_multi_aff *upma2);
3466 const char *arg1;
3467 const char *arg2;
3468 const char *res;
3469 } upma_bin_tests[] = {
3470 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
3471 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
3472 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
3473 "{ B[x] -> [2] : x >= 0 }",
3474 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
3477 /* Perform some basic tests of binary operations on
3478 * isl_union_pw_multi_aff objects.
3480 static int test_bin_upma(isl_ctx *ctx)
3482 int i;
3483 isl_union_pw_multi_aff *upma1, *upma2, *res;
3484 int ok;
3486 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
3487 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3488 upma_bin_tests[i].arg1);
3489 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3490 upma_bin_tests[i].arg2);
3491 res = isl_union_pw_multi_aff_read_from_str(ctx,
3492 upma_bin_tests[i].res);
3493 upma1 = upma_bin_tests[i].fn(upma1, upma2);
3494 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
3495 isl_union_pw_multi_aff_free(upma1);
3496 isl_union_pw_multi_aff_free(res);
3497 if (ok < 0)
3498 return -1;
3499 if (!ok)
3500 isl_die(ctx, isl_error_unknown,
3501 "unexpected result", return -1);
3504 return 0;
3507 int test_aff(isl_ctx *ctx)
3509 const char *str;
3510 isl_set *set;
3511 isl_space *space;
3512 isl_local_space *ls;
3513 isl_aff *aff;
3514 int zero, equal;
3516 if (test_bin_aff(ctx) < 0)
3517 return -1;
3518 if (test_bin_upma(ctx) < 0)
3519 return -1;
3521 space = isl_space_set_alloc(ctx, 0, 1);
3522 ls = isl_local_space_from_space(space);
3523 aff = isl_aff_zero_on_domain(ls);
3525 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3526 aff = isl_aff_scale_down_ui(aff, 3);
3527 aff = isl_aff_floor(aff);
3528 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3529 aff = isl_aff_scale_down_ui(aff, 2);
3530 aff = isl_aff_floor(aff);
3531 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3533 str = "{ [10] }";
3534 set = isl_set_read_from_str(ctx, str);
3535 aff = isl_aff_gist(aff, set);
3537 aff = isl_aff_add_constant_si(aff, -16);
3538 zero = isl_aff_plain_is_zero(aff);
3539 isl_aff_free(aff);
3541 if (zero < 0)
3542 return -1;
3543 if (!zero)
3544 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3546 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
3547 aff = isl_aff_scale_down_ui(aff, 64);
3548 aff = isl_aff_floor(aff);
3549 equal = aff_check_plain_equal(aff, "{ [-1] }");
3550 isl_aff_free(aff);
3551 if (equal < 0)
3552 return -1;
3554 return 0;
3557 int test_dim_max(isl_ctx *ctx)
3559 int equal;
3560 const char *str;
3561 isl_set *set1, *set2;
3562 isl_set *set;
3563 isl_map *map;
3564 isl_pw_aff *pwaff;
3566 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
3567 set = isl_set_read_from_str(ctx, str);
3568 pwaff = isl_set_dim_max(set, 0);
3569 set1 = isl_set_from_pw_aff(pwaff);
3570 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
3571 set2 = isl_set_read_from_str(ctx, str);
3572 equal = isl_set_is_equal(set1, set2);
3573 isl_set_free(set1);
3574 isl_set_free(set2);
3575 if (equal < 0)
3576 return -1;
3577 if (!equal)
3578 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3580 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
3581 set = isl_set_read_from_str(ctx, str);
3582 pwaff = isl_set_dim_max(set, 0);
3583 set1 = isl_set_from_pw_aff(pwaff);
3584 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3585 set2 = isl_set_read_from_str(ctx, str);
3586 equal = isl_set_is_equal(set1, set2);
3587 isl_set_free(set1);
3588 isl_set_free(set2);
3589 if (equal < 0)
3590 return -1;
3591 if (!equal)
3592 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3594 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
3595 set = isl_set_read_from_str(ctx, str);
3596 pwaff = isl_set_dim_max(set, 0);
3597 set1 = isl_set_from_pw_aff(pwaff);
3598 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3599 set2 = isl_set_read_from_str(ctx, str);
3600 equal = isl_set_is_equal(set1, set2);
3601 isl_set_free(set1);
3602 isl_set_free(set2);
3603 if (equal < 0)
3604 return -1;
3605 if (!equal)
3606 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3608 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
3609 "0 <= i < N and 0 <= j < M }";
3610 map = isl_map_read_from_str(ctx, str);
3611 set = isl_map_range(map);
3613 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
3614 set1 = isl_set_from_pw_aff(pwaff);
3615 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
3616 set2 = isl_set_read_from_str(ctx, str);
3617 equal = isl_set_is_equal(set1, set2);
3618 isl_set_free(set1);
3619 isl_set_free(set2);
3621 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3622 set1 = isl_set_from_pw_aff(pwaff);
3623 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3624 set2 = isl_set_read_from_str(ctx, str);
3625 if (equal >= 0 && equal)
3626 equal = isl_set_is_equal(set1, set2);
3627 isl_set_free(set1);
3628 isl_set_free(set2);
3630 isl_set_free(set);
3632 if (equal < 0)
3633 return -1;
3634 if (!equal)
3635 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3637 /* Check that solutions are properly merged. */
3638 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3639 "c <= -1 + n - 4a - 2b and c >= -2b and "
3640 "4a >= -4 + n and c >= 0 }";
3641 set = isl_set_read_from_str(ctx, str);
3642 pwaff = isl_set_dim_min(set, 2);
3643 set1 = isl_set_from_pw_aff(pwaff);
3644 str = "[n] -> { [(0)] : n >= 1 }";
3645 set2 = isl_set_read_from_str(ctx, str);
3646 equal = isl_set_is_equal(set1, set2);
3647 isl_set_free(set1);
3648 isl_set_free(set2);
3650 if (equal < 0)
3651 return -1;
3652 if (!equal)
3653 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3655 /* Check that empty solution lie in the right space. */
3656 str = "[n] -> { [t,a] : 1 = 0 }";
3657 set = isl_set_read_from_str(ctx, str);
3658 pwaff = isl_set_dim_max(set, 0);
3659 set1 = isl_set_from_pw_aff(pwaff);
3660 str = "[n] -> { [t] : 1 = 0 }";
3661 set2 = isl_set_read_from_str(ctx, str);
3662 equal = isl_set_is_equal(set1, set2);
3663 isl_set_free(set1);
3664 isl_set_free(set2);
3666 if (equal < 0)
3667 return -1;
3668 if (!equal)
3669 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3671 return 0;
3674 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
3676 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
3677 const char *str)
3679 isl_ctx *ctx;
3680 isl_pw_multi_aff *pma2;
3681 int equal;
3683 if (!pma)
3684 return -1;
3686 ctx = isl_pw_multi_aff_get_ctx(pma);
3687 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3688 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
3689 isl_pw_multi_aff_free(pma2);
3691 return equal;
3694 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
3695 * represented by "str".
3697 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
3698 const char *str)
3700 int equal;
3702 equal = pw_multi_aff_plain_is_equal(pma, str);
3703 if (equal < 0)
3704 return -1;
3705 if (!equal)
3706 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
3707 "result not as expected", return -1);
3708 return 0;
3711 /* Basic test for isl_pw_multi_aff_product.
3713 * Check that multiple pieces are properly handled.
3715 static int test_product_pma(isl_ctx *ctx)
3717 int equal;
3718 const char *str;
3719 isl_pw_multi_aff *pma1, *pma2;
3721 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
3722 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3723 str = "{ C[] -> D[] }";
3724 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3725 pma1 = isl_pw_multi_aff_product(pma1, pma2);
3726 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
3727 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
3728 equal = pw_multi_aff_check_plain_equal(pma1, str);
3729 isl_pw_multi_aff_free(pma1);
3730 if (equal < 0)
3731 return -1;
3733 return 0;
3736 int test_product(isl_ctx *ctx)
3738 const char *str;
3739 isl_set *set;
3740 isl_union_set *uset1, *uset2;
3741 int ok;
3743 str = "{ A[i] }";
3744 set = isl_set_read_from_str(ctx, str);
3745 set = isl_set_product(set, isl_set_copy(set));
3746 ok = isl_set_is_wrapping(set);
3747 isl_set_free(set);
3748 if (ok < 0)
3749 return -1;
3750 if (!ok)
3751 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3753 str = "{ [] }";
3754 uset1 = isl_union_set_read_from_str(ctx, str);
3755 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3756 str = "{ [[] -> []] }";
3757 uset2 = isl_union_set_read_from_str(ctx, str);
3758 ok = isl_union_set_is_equal(uset1, uset2);
3759 isl_union_set_free(uset1);
3760 isl_union_set_free(uset2);
3761 if (ok < 0)
3762 return -1;
3763 if (!ok)
3764 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3766 if (test_product_pma(ctx) < 0)
3767 return -1;
3769 return 0;
3772 /* Check that two sets are not considered disjoint just because
3773 * they have a different set of (named) parameters.
3775 static int test_disjoint(isl_ctx *ctx)
3777 const char *str;
3778 isl_set *set, *set2;
3779 int disjoint;
3781 str = "[n] -> { [[]->[]] }";
3782 set = isl_set_read_from_str(ctx, str);
3783 str = "{ [[]->[]] }";
3784 set2 = isl_set_read_from_str(ctx, str);
3785 disjoint = isl_set_is_disjoint(set, set2);
3786 isl_set_free(set);
3787 isl_set_free(set2);
3788 if (disjoint < 0)
3789 return -1;
3790 if (disjoint)
3791 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3793 return 0;
3796 int test_equal(isl_ctx *ctx)
3798 const char *str;
3799 isl_set *set, *set2;
3800 int equal;
3802 str = "{ S_6[i] }";
3803 set = isl_set_read_from_str(ctx, str);
3804 str = "{ S_7[i] }";
3805 set2 = isl_set_read_from_str(ctx, str);
3806 equal = isl_set_is_equal(set, set2);
3807 isl_set_free(set);
3808 isl_set_free(set2);
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 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3818 enum isl_dim_type type, unsigned pos, int fixed)
3820 int test;
3822 test = isl_map_plain_is_fixed(map, type, pos, NULL);
3823 isl_map_free(map);
3824 if (test < 0)
3825 return -1;
3826 if (test == fixed)
3827 return 0;
3828 if (fixed)
3829 isl_die(ctx, isl_error_unknown,
3830 "map not detected as fixed", return -1);
3831 else
3832 isl_die(ctx, isl_error_unknown,
3833 "map detected as fixed", return -1);
3836 int test_fixed(isl_ctx *ctx)
3838 const char *str;
3839 isl_map *map;
3841 str = "{ [i] -> [i] }";
3842 map = isl_map_read_from_str(ctx, str);
3843 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3844 return -1;
3845 str = "{ [i] -> [1] }";
3846 map = isl_map_read_from_str(ctx, str);
3847 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3848 return -1;
3849 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3850 map = isl_map_read_from_str(ctx, str);
3851 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3852 return -1;
3853 map = isl_map_read_from_str(ctx, str);
3854 map = isl_map_neg(map);
3855 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3856 return -1;
3858 return 0;
3861 struct isl_vertices_test_data {
3862 const char *set;
3863 int n;
3864 const char *vertex[2];
3865 } vertices_tests[] = {
3866 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
3867 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
3868 { "{ A[t, i] : t = 14 and i = 1 }",
3869 1, { "{ A[14, 1] }" } },
3872 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
3874 static int find_vertex(__isl_take isl_vertex *vertex, void *user)
3876 struct isl_vertices_test_data *data = user;
3877 isl_ctx *ctx;
3878 isl_multi_aff *ma;
3879 isl_basic_set *bset;
3880 isl_pw_multi_aff *pma;
3881 int i;
3882 int equal;
3884 ctx = isl_vertex_get_ctx(vertex);
3885 bset = isl_vertex_get_domain(vertex);
3886 ma = isl_vertex_get_expr(vertex);
3887 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
3889 for (i = 0; i < data->n; ++i) {
3890 isl_pw_multi_aff *pma_i;
3892 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
3893 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
3894 isl_pw_multi_aff_free(pma_i);
3896 if (equal < 0 || equal)
3897 break;
3900 isl_pw_multi_aff_free(pma);
3901 isl_vertex_free(vertex);
3903 if (equal < 0)
3904 return -1;
3906 return equal ? 0 : - 1;
3909 int test_vertices(isl_ctx *ctx)
3911 int i;
3913 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
3914 isl_basic_set *bset;
3915 isl_vertices *vertices;
3916 int ok = 1;
3917 int n;
3919 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
3920 vertices = isl_basic_set_compute_vertices(bset);
3921 n = isl_vertices_get_n_vertices(vertices);
3922 if (vertices_tests[i].n != n)
3923 ok = 0;
3924 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
3925 &vertices_tests[i]) < 0)
3926 ok = 0;
3927 isl_vertices_free(vertices);
3928 isl_basic_set_free(bset);
3930 if (!vertices)
3931 return -1;
3932 if (!ok)
3933 isl_die(ctx, isl_error_unknown, "unexpected vertices",
3934 return -1);
3937 return 0;
3940 int test_union_pw(isl_ctx *ctx)
3942 int equal;
3943 const char *str;
3944 isl_union_set *uset;
3945 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
3947 str = "{ [x] -> x^2 }";
3948 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
3949 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
3950 uset = isl_union_pw_qpolynomial_domain(upwqp1);
3951 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
3952 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
3953 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
3954 isl_union_pw_qpolynomial_free(upwqp1);
3955 isl_union_pw_qpolynomial_free(upwqp2);
3956 if (equal < 0)
3957 return -1;
3958 if (!equal)
3959 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3961 return 0;
3964 int test_output(isl_ctx *ctx)
3966 char *s;
3967 const char *str;
3968 isl_pw_aff *pa;
3969 isl_printer *p;
3970 int equal;
3972 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
3973 pa = isl_pw_aff_read_from_str(ctx, str);
3975 p = isl_printer_to_str(ctx);
3976 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
3977 p = isl_printer_print_pw_aff(p, pa);
3978 s = isl_printer_get_str(p);
3979 isl_printer_free(p);
3980 isl_pw_aff_free(pa);
3981 if (!s)
3982 equal = -1;
3983 else
3984 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
3985 free(s);
3986 if (equal < 0)
3987 return -1;
3988 if (!equal)
3989 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3991 return 0;
3994 int test_sample(isl_ctx *ctx)
3996 const char *str;
3997 isl_basic_set *bset1, *bset2;
3998 int empty, subset;
4000 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
4001 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
4002 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
4003 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
4004 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
4005 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
4006 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
4007 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
4008 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
4009 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
4010 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
4011 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
4012 "d - 1073741821e and "
4013 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
4014 "3j >= 1 - a + b + 2e and "
4015 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
4016 "3i <= 4 - a + 4b - e and "
4017 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
4018 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
4019 "c <= -1 + a and 3i >= -2 - a + 3e and "
4020 "1073741822e <= 1073741823 - a + 1073741822b + c and "
4021 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
4022 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
4023 "1073741823e >= 1 + 1073741823b - d and "
4024 "3i >= 1073741823b + c - 1073741823e - f and "
4025 "3i >= 1 + 2b + e + 3g }";
4026 bset1 = isl_basic_set_read_from_str(ctx, str);
4027 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
4028 empty = isl_basic_set_is_empty(bset2);
4029 subset = isl_basic_set_is_subset(bset2, bset1);
4030 isl_basic_set_free(bset1);
4031 isl_basic_set_free(bset2);
4032 if (empty < 0 || subset < 0)
4033 return -1;
4034 if (empty)
4035 isl_die(ctx, isl_error_unknown, "point not found", return -1);
4036 if (!subset)
4037 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
4039 return 0;
4042 int test_fixed_power(isl_ctx *ctx)
4044 const char *str;
4045 isl_map *map;
4046 isl_int exp;
4047 int equal;
4049 isl_int_init(exp);
4050 str = "{ [i] -> [i + 1] }";
4051 map = isl_map_read_from_str(ctx, str);
4052 isl_int_set_si(exp, 23);
4053 map = isl_map_fixed_power(map, exp);
4054 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
4055 isl_int_clear(exp);
4056 isl_map_free(map);
4057 if (equal < 0)
4058 return -1;
4060 return 0;
4063 int test_slice(isl_ctx *ctx)
4065 const char *str;
4066 isl_map *map;
4067 int equal;
4069 str = "{ [i] -> [j] }";
4070 map = isl_map_read_from_str(ctx, str);
4071 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
4072 equal = map_check_equal(map, "{ [i] -> [i] }");
4073 isl_map_free(map);
4074 if (equal < 0)
4075 return -1;
4077 str = "{ [i] -> [j] }";
4078 map = isl_map_read_from_str(ctx, str);
4079 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
4080 equal = map_check_equal(map, "{ [i] -> [j] }");
4081 isl_map_free(map);
4082 if (equal < 0)
4083 return -1;
4085 str = "{ [i] -> [j] }";
4086 map = isl_map_read_from_str(ctx, str);
4087 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
4088 equal = map_check_equal(map, "{ [i] -> [-i] }");
4089 isl_map_free(map);
4090 if (equal < 0)
4091 return -1;
4093 str = "{ [i] -> [j] }";
4094 map = isl_map_read_from_str(ctx, str);
4095 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
4096 equal = map_check_equal(map, "{ [0] -> [j] }");
4097 isl_map_free(map);
4098 if (equal < 0)
4099 return -1;
4101 str = "{ [i] -> [j] }";
4102 map = isl_map_read_from_str(ctx, str);
4103 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
4104 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
4105 isl_map_free(map);
4106 if (equal < 0)
4107 return -1;
4109 str = "{ [i] -> [j] }";
4110 map = isl_map_read_from_str(ctx, str);
4111 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
4112 equal = map_check_equal(map, "{ [i] -> [j] : false }");
4113 isl_map_free(map);
4114 if (equal < 0)
4115 return -1;
4117 return 0;
4120 int test_eliminate(isl_ctx *ctx)
4122 const char *str;
4123 isl_map *map;
4124 int equal;
4126 str = "{ [i] -> [j] : i = 2j }";
4127 map = isl_map_read_from_str(ctx, str);
4128 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
4129 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
4130 isl_map_free(map);
4131 if (equal < 0)
4132 return -1;
4134 return 0;
4137 /* Check that isl_set_dim_residue_class detects that the values of j
4138 * in the set below are all odd and that it does not detect any spurious
4139 * strides.
4141 static int test_residue_class(isl_ctx *ctx)
4143 const char *str;
4144 isl_set *set;
4145 isl_int m, r;
4146 int res;
4148 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
4149 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
4150 set = isl_set_read_from_str(ctx, str);
4151 isl_int_init(m);
4152 isl_int_init(r);
4153 res = isl_set_dim_residue_class(set, 1, &m, &r);
4154 if (res >= 0 &&
4155 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
4156 isl_die(ctx, isl_error_unknown, "incorrect residue class",
4157 res = -1);
4158 isl_int_clear(r);
4159 isl_int_clear(m);
4160 isl_set_free(set);
4162 return res;
4165 int test_align_parameters(isl_ctx *ctx)
4167 const char *str;
4168 isl_space *space;
4169 isl_multi_aff *ma1, *ma2;
4170 int equal;
4172 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
4173 ma1 = isl_multi_aff_read_from_str(ctx, str);
4175 space = isl_space_params_alloc(ctx, 1);
4176 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
4177 ma1 = isl_multi_aff_align_params(ma1, space);
4179 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
4180 ma2 = isl_multi_aff_read_from_str(ctx, str);
4182 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4184 isl_multi_aff_free(ma1);
4185 isl_multi_aff_free(ma2);
4187 if (equal < 0)
4188 return -1;
4189 if (!equal)
4190 isl_die(ctx, isl_error_unknown,
4191 "result not as expected", return -1);
4193 return 0;
4196 static int test_list(isl_ctx *ctx)
4198 isl_id *a, *b, *c, *d, *id;
4199 isl_id_list *list;
4200 int ok;
4202 a = isl_id_alloc(ctx, "a", NULL);
4203 b = isl_id_alloc(ctx, "b", NULL);
4204 c = isl_id_alloc(ctx, "c", NULL);
4205 d = isl_id_alloc(ctx, "d", NULL);
4207 list = isl_id_list_alloc(ctx, 4);
4208 list = isl_id_list_add(list, a);
4209 list = isl_id_list_add(list, b);
4210 list = isl_id_list_add(list, c);
4211 list = isl_id_list_add(list, d);
4212 list = isl_id_list_drop(list, 1, 1);
4214 if (isl_id_list_n_id(list) != 3) {
4215 isl_id_list_free(list);
4216 isl_die(ctx, isl_error_unknown,
4217 "unexpected number of elements in list", return -1);
4220 id = isl_id_list_get_id(list, 0);
4221 ok = id == a;
4222 isl_id_free(id);
4223 id = isl_id_list_get_id(list, 1);
4224 ok = ok && id == c;
4225 isl_id_free(id);
4226 id = isl_id_list_get_id(list, 2);
4227 ok = ok && id == d;
4228 isl_id_free(id);
4230 isl_id_list_free(list);
4232 if (!ok)
4233 isl_die(ctx, isl_error_unknown,
4234 "unexpected elements in list", return -1);
4236 return 0;
4239 const char *set_conversion_tests[] = {
4240 "[N] -> { [i] : N - 1 <= 2 i <= N }",
4241 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
4242 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4243 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4244 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
4247 /* Check that converting from isl_set to isl_pw_multi_aff and back
4248 * to isl_set produces the original isl_set.
4250 static int test_set_conversion(isl_ctx *ctx)
4252 int i;
4253 const char *str;
4254 isl_set *set1, *set2;
4255 isl_pw_multi_aff *pma;
4256 int equal;
4258 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
4259 str = set_conversion_tests[i];
4260 set1 = isl_set_read_from_str(ctx, str);
4261 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
4262 set2 = isl_set_from_pw_multi_aff(pma);
4263 equal = isl_set_is_equal(set1, set2);
4264 isl_set_free(set1);
4265 isl_set_free(set2);
4267 if (equal < 0)
4268 return -1;
4269 if (!equal)
4270 isl_die(ctx, isl_error_unknown, "bad conversion",
4271 return -1);
4274 return 0;
4277 /* Check that converting from isl_map to isl_pw_multi_aff and back
4278 * to isl_map produces the original isl_map.
4280 static int test_map_conversion(isl_ctx *ctx)
4282 const char *str;
4283 isl_map *map1, *map2;
4284 isl_pw_multi_aff *pma;
4285 int equal;
4287 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
4288 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
4289 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
4290 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
4291 "9e <= -2 - 2a) }";
4292 map1 = isl_map_read_from_str(ctx, str);
4293 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
4294 map2 = isl_map_from_pw_multi_aff(pma);
4295 equal = isl_map_is_equal(map1, map2);
4296 isl_map_free(map1);
4297 isl_map_free(map2);
4299 if (equal < 0)
4300 return -1;
4301 if (!equal)
4302 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
4304 return 0;
4307 static int test_conversion(isl_ctx *ctx)
4309 if (test_set_conversion(ctx) < 0)
4310 return -1;
4311 if (test_map_conversion(ctx) < 0)
4312 return -1;
4313 return 0;
4316 /* Check that isl_basic_map_curry does not modify input.
4318 static int test_curry(isl_ctx *ctx)
4320 const char *str;
4321 isl_basic_map *bmap1, *bmap2;
4322 int equal;
4324 str = "{ [A[] -> B[]] -> C[] }";
4325 bmap1 = isl_basic_map_read_from_str(ctx, str);
4326 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
4327 equal = isl_basic_map_is_equal(bmap1, bmap2);
4328 isl_basic_map_free(bmap1);
4329 isl_basic_map_free(bmap2);
4331 if (equal < 0)
4332 return -1;
4333 if (equal)
4334 isl_die(ctx, isl_error_unknown,
4335 "curried map should not be equal to original",
4336 return -1);
4338 return 0;
4341 struct {
4342 const char *set;
4343 const char *ma;
4344 const char *res;
4345 } preimage_tests[] = {
4346 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
4347 "{ A[j,i] -> B[i,j] }",
4348 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
4349 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4350 "{ A[a,b] -> B[a/2,b/6] }",
4351 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
4352 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4353 "{ A[a,b] -> B[a/2,b/6] }",
4354 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
4355 "exists i,j : a = 2 i and b = 6 j }" },
4356 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
4357 "[n] -> { : 0 <= n <= 100 }" },
4358 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4359 "{ A[a] -> B[2a] }",
4360 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
4361 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4362 "{ A[a] -> B[([a/2])] }",
4363 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
4364 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
4365 "{ A[a] -> B[a,a,a/3] }",
4366 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
4367 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
4368 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
4371 static int test_preimage_basic_set(isl_ctx *ctx)
4373 int i;
4374 isl_basic_set *bset1, *bset2;
4375 isl_multi_aff *ma;
4376 int equal;
4378 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
4379 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
4380 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
4381 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
4382 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
4383 equal = isl_basic_set_is_equal(bset1, bset2);
4384 isl_basic_set_free(bset1);
4385 isl_basic_set_free(bset2);
4386 if (equal < 0)
4387 return -1;
4388 if (!equal)
4389 isl_die(ctx, isl_error_unknown, "bad preimage",
4390 return -1);
4393 return 0;
4396 struct {
4397 const char *map;
4398 const char *ma;
4399 const char *res;
4400 } preimage_domain_tests[] = {
4401 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
4402 "{ A[j,i] -> B[i,j] }",
4403 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
4404 { "{ B[i] -> C[i]; D[i] -> E[i] }",
4405 "{ A[i] -> B[i + 1] }",
4406 "{ A[i] -> C[i + 1] }" },
4407 { "{ B[i] -> C[i]; B[i] -> E[i] }",
4408 "{ A[i] -> B[i + 1] }",
4409 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
4410 { "{ B[i] -> C[([i/2])] }",
4411 "{ A[i] -> B[2i] }",
4412 "{ A[i] -> C[i] }" },
4413 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
4414 "{ A[i] -> B[([i/5]), ([i/7])] }",
4415 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
4416 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
4417 "[N] -> { A[] -> B[([N/5])] }",
4418 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
4419 { "{ B[i] -> C[i] : exists a : i = 5 a }",
4420 "{ A[i] -> B[2i] }",
4421 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
4422 { "{ B[i] -> C[i] : exists a : i = 2 a; "
4423 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
4424 "{ A[i] -> B[2i] }",
4425 "{ A[i] -> C[2i] }" },
4428 static int test_preimage_union_map(isl_ctx *ctx)
4430 int i;
4431 isl_union_map *umap1, *umap2;
4432 isl_multi_aff *ma;
4433 int equal;
4435 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
4436 umap1 = isl_union_map_read_from_str(ctx,
4437 preimage_domain_tests[i].map);
4438 ma = isl_multi_aff_read_from_str(ctx,
4439 preimage_domain_tests[i].ma);
4440 umap2 = isl_union_map_read_from_str(ctx,
4441 preimage_domain_tests[i].res);
4442 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
4443 equal = isl_union_map_is_equal(umap1, umap2);
4444 isl_union_map_free(umap1);
4445 isl_union_map_free(umap2);
4446 if (equal < 0)
4447 return -1;
4448 if (!equal)
4449 isl_die(ctx, isl_error_unknown, "bad preimage",
4450 return -1);
4453 return 0;
4456 static int test_preimage(isl_ctx *ctx)
4458 if (test_preimage_basic_set(ctx) < 0)
4459 return -1;
4460 if (test_preimage_union_map(ctx) < 0)
4461 return -1;
4463 return 0;
4466 struct {
4467 const char *ma1;
4468 const char *ma;
4469 const char *res;
4470 } pullback_tests[] = {
4471 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
4472 "{ A[a,b] -> C[b + 2a] }" },
4473 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
4474 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
4475 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
4476 "{ A[a] -> C[(a)/6] }" },
4477 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
4478 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
4479 "{ A[a] -> C[(2a)/3] }" },
4480 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
4481 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
4482 "{ A[i,j] -> C[i + j, i + j] }"},
4483 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
4484 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
4485 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
4488 static int test_pullback(isl_ctx *ctx)
4490 int i;
4491 isl_multi_aff *ma1, *ma2;
4492 isl_multi_aff *ma;
4493 int equal;
4495 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
4496 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
4497 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
4498 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
4499 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
4500 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4501 isl_multi_aff_free(ma1);
4502 isl_multi_aff_free(ma2);
4503 if (equal < 0)
4504 return -1;
4505 if (!equal)
4506 isl_die(ctx, isl_error_unknown, "bad pullback",
4507 return -1);
4510 return 0;
4513 /* Check that negation is printed correctly.
4515 static int test_ast(isl_ctx *ctx)
4517 isl_ast_expr *expr, *expr1, *expr2, *expr3;
4518 char *str;
4519 int ok;
4521 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4522 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4523 expr = isl_ast_expr_add(expr1, expr2);
4524 expr = isl_ast_expr_neg(expr);
4525 str = isl_ast_expr_to_str(expr);
4526 ok = str ? !strcmp(str, "-(A + B)") : -1;
4527 free(str);
4528 isl_ast_expr_free(expr);
4530 if (ok < 0)
4531 return -1;
4532 if (!ok)
4533 isl_die(ctx, isl_error_unknown,
4534 "isl_ast_expr printed incorrectly", return -1);
4536 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4537 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4538 expr = isl_ast_expr_add(expr1, expr2);
4539 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
4540 expr = isl_ast_expr_sub(expr3, expr);
4541 str = isl_ast_expr_to_str(expr);
4542 ok = str ? !strcmp(str, "C - (A + B)") : -1;
4543 free(str);
4544 isl_ast_expr_free(expr);
4546 if (ok < 0)
4547 return -1;
4548 if (!ok)
4549 isl_die(ctx, isl_error_unknown,
4550 "isl_ast_expr printed incorrectly", return -1);
4552 return 0;
4555 /* Check that isl_ast_build_expr_from_set returns a valid expression
4556 * for an empty set. Note that isl_ast_build_expr_from_set getting
4557 * called on an empty set probably indicates a bug in the caller.
4559 static int test_ast_build(isl_ctx *ctx)
4561 isl_set *set;
4562 isl_ast_build *build;
4563 isl_ast_expr *expr;
4565 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4566 build = isl_ast_build_from_context(set);
4568 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
4569 expr = isl_ast_build_expr_from_set(build, set);
4571 isl_ast_expr_free(expr);
4572 isl_ast_build_free(build);
4574 if (!expr)
4575 return -1;
4577 return 0;
4580 /* Internal data structure for before_for and after_for callbacks.
4582 * depth is the current depth
4583 * before is the number of times before_for has been called
4584 * after is the number of times after_for has been called
4586 struct isl_test_codegen_data {
4587 int depth;
4588 int before;
4589 int after;
4592 /* This function is called before each for loop in the AST generated
4593 * from test_ast_gen1.
4595 * Increment the number of calls and the depth.
4596 * Check that the space returned by isl_ast_build_get_schedule_space
4597 * matches the target space of the schedule returned by
4598 * isl_ast_build_get_schedule.
4599 * Return an isl_id that is checked by the corresponding call
4600 * to after_for.
4602 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
4603 void *user)
4605 struct isl_test_codegen_data *data = user;
4606 isl_ctx *ctx;
4607 isl_space *space;
4608 isl_union_map *schedule;
4609 isl_union_set *uset;
4610 isl_set *set;
4611 int empty;
4612 char name[] = "d0";
4614 ctx = isl_ast_build_get_ctx(build);
4616 if (data->before >= 3)
4617 isl_die(ctx, isl_error_unknown,
4618 "unexpected number of for nodes", return NULL);
4619 if (data->depth >= 2)
4620 isl_die(ctx, isl_error_unknown,
4621 "unexpected depth", return NULL);
4623 snprintf(name, sizeof(name), "d%d", data->depth);
4624 data->before++;
4625 data->depth++;
4627 schedule = isl_ast_build_get_schedule(build);
4628 uset = isl_union_map_range(schedule);
4629 if (!uset)
4630 return NULL;
4631 if (isl_union_set_n_set(uset) != 1) {
4632 isl_union_set_free(uset);
4633 isl_die(ctx, isl_error_unknown,
4634 "expecting single range space", return NULL);
4637 space = isl_ast_build_get_schedule_space(build);
4638 set = isl_union_set_extract_set(uset, space);
4639 isl_union_set_free(uset);
4640 empty = isl_set_is_empty(set);
4641 isl_set_free(set);
4643 if (empty < 0)
4644 return NULL;
4645 if (empty)
4646 isl_die(ctx, isl_error_unknown,
4647 "spaces don't match", return NULL);
4649 return isl_id_alloc(ctx, name, NULL);
4652 /* This function is called after each for loop in the AST generated
4653 * from test_ast_gen1.
4655 * Increment the number of calls and decrement the depth.
4656 * Check that the annotation attached to the node matches
4657 * the isl_id returned by the corresponding call to before_for.
4659 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
4660 __isl_keep isl_ast_build *build, void *user)
4662 struct isl_test_codegen_data *data = user;
4663 isl_id *id;
4664 const char *name;
4665 int valid;
4667 data->after++;
4668 data->depth--;
4670 if (data->after > data->before)
4671 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4672 "mismatch in number of for nodes",
4673 return isl_ast_node_free(node));
4675 id = isl_ast_node_get_annotation(node);
4676 if (!id)
4677 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4678 "missing annotation", return isl_ast_node_free(node));
4680 name = isl_id_get_name(id);
4681 valid = name && atoi(name + 1) == data->depth;
4682 isl_id_free(id);
4684 if (!valid)
4685 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4686 "wrong annotation", return isl_ast_node_free(node));
4688 return node;
4691 /* Check that the before_each_for and after_each_for callbacks
4692 * are called for each for loop in the generated code,
4693 * that they are called in the right order and that the isl_id
4694 * returned from the before_each_for callback is attached to
4695 * the isl_ast_node passed to the corresponding after_each_for call.
4697 static int test_ast_gen1(isl_ctx *ctx)
4699 const char *str;
4700 isl_set *set;
4701 isl_union_map *schedule;
4702 isl_ast_build *build;
4703 isl_ast_node *tree;
4704 struct isl_test_codegen_data data;
4706 str = "[N] -> { : N >= 10 }";
4707 set = isl_set_read_from_str(ctx, str);
4708 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
4709 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
4710 schedule = isl_union_map_read_from_str(ctx, str);
4712 data.before = 0;
4713 data.after = 0;
4714 data.depth = 0;
4715 build = isl_ast_build_from_context(set);
4716 build = isl_ast_build_set_before_each_for(build,
4717 &before_for, &data);
4718 build = isl_ast_build_set_after_each_for(build,
4719 &after_for, &data);
4720 tree = isl_ast_build_ast_from_schedule(build, schedule);
4721 isl_ast_build_free(build);
4722 if (!tree)
4723 return -1;
4725 isl_ast_node_free(tree);
4727 if (data.before != 3 || data.after != 3)
4728 isl_die(ctx, isl_error_unknown,
4729 "unexpected number of for nodes", return -1);
4731 return 0;
4734 /* Check that the AST generator handles domains that are integrally disjoint
4735 * but not ratinoally disjoint.
4737 static int test_ast_gen2(isl_ctx *ctx)
4739 const char *str;
4740 isl_set *set;
4741 isl_union_map *schedule;
4742 isl_union_map *options;
4743 isl_ast_build *build;
4744 isl_ast_node *tree;
4746 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
4747 schedule = isl_union_map_read_from_str(ctx, str);
4748 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4749 build = isl_ast_build_from_context(set);
4751 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
4752 options = isl_union_map_read_from_str(ctx, str);
4753 build = isl_ast_build_set_options(build, options);
4754 tree = isl_ast_build_ast_from_schedule(build, schedule);
4755 isl_ast_build_free(build);
4756 if (!tree)
4757 return -1;
4758 isl_ast_node_free(tree);
4760 return 0;
4763 /* Increment *user on each call.
4765 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
4766 __isl_keep isl_ast_build *build, void *user)
4768 int *n = user;
4770 (*n)++;
4772 return node;
4775 /* Test that unrolling tries to minimize the number of instances.
4776 * In particular, for the schedule given below, make sure it generates
4777 * 3 nodes (rather than 101).
4779 static int test_ast_gen3(isl_ctx *ctx)
4781 const char *str;
4782 isl_set *set;
4783 isl_union_map *schedule;
4784 isl_union_map *options;
4785 isl_ast_build *build;
4786 isl_ast_node *tree;
4787 int n_domain = 0;
4789 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
4790 schedule = isl_union_map_read_from_str(ctx, str);
4791 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4793 str = "{ [i] -> unroll[0] }";
4794 options = isl_union_map_read_from_str(ctx, str);
4796 build = isl_ast_build_from_context(set);
4797 build = isl_ast_build_set_options(build, options);
4798 build = isl_ast_build_set_at_each_domain(build,
4799 &count_domains, &n_domain);
4800 tree = isl_ast_build_ast_from_schedule(build, schedule);
4801 isl_ast_build_free(build);
4802 if (!tree)
4803 return -1;
4805 isl_ast_node_free(tree);
4807 if (n_domain != 3)
4808 isl_die(ctx, isl_error_unknown,
4809 "unexpected number of for nodes", return -1);
4811 return 0;
4814 /* Check that if the ast_build_exploit_nested_bounds options is set,
4815 * we do not get an outer if node in the generated AST,
4816 * while we do get such an outer if node if the options is not set.
4818 static int test_ast_gen4(isl_ctx *ctx)
4820 const char *str;
4821 isl_set *set;
4822 isl_union_map *schedule;
4823 isl_ast_build *build;
4824 isl_ast_node *tree;
4825 enum isl_ast_node_type type;
4826 int enb;
4828 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4829 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4831 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4833 schedule = isl_union_map_read_from_str(ctx, str);
4834 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4835 build = isl_ast_build_from_context(set);
4836 tree = isl_ast_build_ast_from_schedule(build, schedule);
4837 isl_ast_build_free(build);
4838 if (!tree)
4839 return -1;
4841 type = isl_ast_node_get_type(tree);
4842 isl_ast_node_free(tree);
4844 if (type == isl_ast_node_if)
4845 isl_die(ctx, isl_error_unknown,
4846 "not expecting if node", return -1);
4848 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
4850 schedule = isl_union_map_read_from_str(ctx, str);
4851 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4852 build = isl_ast_build_from_context(set);
4853 tree = isl_ast_build_ast_from_schedule(build, schedule);
4854 isl_ast_build_free(build);
4855 if (!tree)
4856 return -1;
4858 type = isl_ast_node_get_type(tree);
4859 isl_ast_node_free(tree);
4861 if (type != isl_ast_node_if)
4862 isl_die(ctx, isl_error_unknown,
4863 "expecting if node", return -1);
4865 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
4867 return 0;
4870 /* This function is called for each leaf in the AST generated
4871 * from test_ast_gen5.
4873 * We finalize the AST generation by extending the outer schedule
4874 * with a zero-dimensional schedule. If this results in any for loops,
4875 * then this means that we did not pass along enough information
4876 * about the outer schedule to the inner AST generation.
4878 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
4879 void *user)
4881 isl_union_map *schedule, *extra;
4882 isl_ast_node *tree;
4884 schedule = isl_ast_build_get_schedule(build);
4885 extra = isl_union_map_copy(schedule);
4886 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
4887 schedule = isl_union_map_range_product(schedule, extra);
4888 tree = isl_ast_build_ast_from_schedule(build, schedule);
4889 isl_ast_build_free(build);
4891 if (!tree)
4892 return NULL;
4894 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
4895 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
4896 "code should not contain any for loop",
4897 return isl_ast_node_free(tree));
4899 return tree;
4902 /* Check that we do not lose any information when going back and
4903 * forth between internal and external schedule.
4905 * In particular, we create an AST where we unroll the only
4906 * non-constant dimension in the schedule. We therefore do
4907 * not expect any for loops in the AST. However, older versions
4908 * of isl would not pass along enough information about the outer
4909 * schedule when performing an inner code generation from a create_leaf
4910 * callback, resulting in the inner code generation producing a for loop.
4912 static int test_ast_gen5(isl_ctx *ctx)
4914 const char *str;
4915 isl_set *set;
4916 isl_union_map *schedule, *options;
4917 isl_ast_build *build;
4918 isl_ast_node *tree;
4920 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
4921 schedule = isl_union_map_read_from_str(ctx, str);
4923 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
4924 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
4925 options = isl_union_map_read_from_str(ctx, str);
4927 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4928 build = isl_ast_build_from_context(set);
4929 build = isl_ast_build_set_options(build, options);
4930 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
4931 tree = isl_ast_build_ast_from_schedule(build, schedule);
4932 isl_ast_build_free(build);
4933 isl_ast_node_free(tree);
4934 if (!tree)
4935 return -1;
4937 return 0;
4940 static int test_ast_gen(isl_ctx *ctx)
4942 if (test_ast_gen1(ctx) < 0)
4943 return -1;
4944 if (test_ast_gen2(ctx) < 0)
4945 return -1;
4946 if (test_ast_gen3(ctx) < 0)
4947 return -1;
4948 if (test_ast_gen4(ctx) < 0)
4949 return -1;
4950 if (test_ast_gen5(ctx) < 0)
4951 return -1;
4952 return 0;
4955 /* Check if dropping output dimensions from an isl_pw_multi_aff
4956 * works properly.
4958 static int test_pw_multi_aff(isl_ctx *ctx)
4960 const char *str;
4961 isl_pw_multi_aff *pma1, *pma2;
4962 int equal;
4964 str = "{ [i,j] -> [i+j, 4i-j] }";
4965 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4966 str = "{ [i,j] -> [4i-j] }";
4967 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4969 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
4971 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
4973 isl_pw_multi_aff_free(pma1);
4974 isl_pw_multi_aff_free(pma2);
4975 if (equal < 0)
4976 return -1;
4977 if (!equal)
4978 isl_die(ctx, isl_error_unknown,
4979 "expressions not equal", return -1);
4981 return 0;
4984 /* Check that we can properly parse multi piecewise affine expressions
4985 * where the piecewise affine expressions have different domains.
4987 static int test_multi_pw_aff(isl_ctx *ctx)
4989 const char *str;
4990 isl_set *dom, *dom2;
4991 isl_multi_pw_aff *mpa1, *mpa2;
4992 isl_pw_aff *pa;
4993 int equal;
4994 int equal_domain;
4996 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
4997 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
4998 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
4999 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
5000 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
5001 str = "{ [i] -> [(i : i > 0), 2i] }";
5002 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
5004 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
5006 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
5007 dom = isl_pw_aff_domain(pa);
5008 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
5009 dom2 = isl_pw_aff_domain(pa);
5010 equal_domain = isl_set_is_equal(dom, dom2);
5012 isl_set_free(dom);
5013 isl_set_free(dom2);
5014 isl_multi_pw_aff_free(mpa1);
5015 isl_multi_pw_aff_free(mpa2);
5017 if (equal < 0)
5018 return -1;
5019 if (!equal)
5020 isl_die(ctx, isl_error_unknown,
5021 "expressions not equal", return -1);
5023 if (equal_domain < 0)
5024 return -1;
5025 if (equal_domain)
5026 isl_die(ctx, isl_error_unknown,
5027 "domains unexpectedly equal", return -1);
5029 return 0;
5032 /* This is a regression test for a bug where isl_basic_map_simplify
5033 * would end up in an infinite loop. In particular, we construct
5034 * an empty basic set that is not obviously empty.
5035 * isl_basic_set_is_empty marks the basic set as empty.
5036 * After projecting out i3, the variable can be dropped completely,
5037 * but isl_basic_map_simplify refrains from doing so if the basic set
5038 * is empty and would end up in an infinite loop if it didn't test
5039 * explicitly for empty basic maps in the outer loop.
5041 static int test_simplify(isl_ctx *ctx)
5043 const char *str;
5044 isl_basic_set *bset;
5045 int empty;
5047 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
5048 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
5049 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
5050 "i3 >= i2 }";
5051 bset = isl_basic_set_read_from_str(ctx, str);
5052 empty = isl_basic_set_is_empty(bset);
5053 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
5054 isl_basic_set_free(bset);
5055 if (!bset)
5056 return -1;
5057 if (!empty)
5058 isl_die(ctx, isl_error_unknown,
5059 "basic set should be empty", return -1);
5061 return 0;
5064 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
5065 * with gbr context would fail to disable the use of the shifted tableau
5066 * when transferring equalities for the input to the context, resulting
5067 * in invalid sample values.
5069 static int test_partial_lexmin(isl_ctx *ctx)
5071 const char *str;
5072 isl_basic_set *bset;
5073 isl_basic_map *bmap;
5074 isl_map *map;
5076 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
5077 bmap = isl_basic_map_read_from_str(ctx, str);
5078 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
5079 bset = isl_basic_set_read_from_str(ctx, str);
5080 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
5081 isl_map_free(map);
5083 if (!map)
5084 return -1;
5086 return 0;
5089 /* Check that the variable compression performed on the existentially
5090 * quantified variables inside isl_basic_set_compute_divs is not confused
5091 * by the implicit equalities among the parameters.
5093 static int test_compute_divs(isl_ctx *ctx)
5095 const char *str;
5096 isl_basic_set *bset;
5097 isl_set *set;
5099 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
5100 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
5101 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
5102 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
5103 bset = isl_basic_set_read_from_str(ctx, str);
5104 set = isl_basic_set_compute_divs(bset);
5105 isl_set_free(set);
5106 if (!set)
5107 return -1;
5109 return 0;
5112 struct {
5113 const char *set;
5114 const char *dual;
5115 } coef_tests[] = {
5116 { "{ rat: [i] : 0 <= i <= 10 }",
5117 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
5118 { "{ rat: [i] : FALSE }",
5119 "{ rat: coefficients[[cst] -> [a]] }" },
5120 { "{ rat: [i] : }",
5121 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
5124 struct {
5125 const char *set;
5126 const char *dual;
5127 } sol_tests[] = {
5128 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
5129 "{ rat: [i] : 0 <= i <= 10 }" },
5130 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
5131 "{ rat: [i] }" },
5132 { "{ rat: coefficients[[cst] -> [a]] }",
5133 "{ rat: [i] : FALSE }" },
5136 /* Test the basic functionality of isl_basic_set_coefficients and
5137 * isl_basic_set_solutions.
5139 static int test_dual(isl_ctx *ctx)
5141 int i;
5143 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
5144 int equal;
5145 isl_basic_set *bset1, *bset2;
5147 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
5148 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
5149 bset1 = isl_basic_set_coefficients(bset1);
5150 equal = isl_basic_set_is_equal(bset1, bset2);
5151 isl_basic_set_free(bset1);
5152 isl_basic_set_free(bset2);
5153 if (equal < 0)
5154 return -1;
5155 if (!equal)
5156 isl_die(ctx, isl_error_unknown,
5157 "incorrect dual", return -1);
5160 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
5161 int equal;
5162 isl_basic_set *bset1, *bset2;
5164 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
5165 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
5166 bset1 = isl_basic_set_solutions(bset1);
5167 equal = isl_basic_set_is_equal(bset1, bset2);
5168 isl_basic_set_free(bset1);
5169 isl_basic_set_free(bset2);
5170 if (equal < 0)
5171 return -1;
5172 if (!equal)
5173 isl_die(ctx, isl_error_unknown,
5174 "incorrect dual", return -1);
5177 return 0;
5180 struct {
5181 const char *name;
5182 int (*fn)(isl_ctx *ctx);
5183 } tests [] = {
5184 { "dual", &test_dual },
5185 { "dependence analysis", &test_flow },
5186 { "val", &test_val },
5187 { "compute divs", &test_compute_divs },
5188 { "partial lexmin", &test_partial_lexmin },
5189 { "simplify", &test_simplify },
5190 { "curry", &test_curry },
5191 { "piecewise multi affine expressions", &test_pw_multi_aff },
5192 { "multi piecewise affine expressions", &test_multi_pw_aff },
5193 { "conversion", &test_conversion },
5194 { "list", &test_list },
5195 { "align parameters", &test_align_parameters },
5196 { "preimage", &test_preimage },
5197 { "pullback", &test_pullback },
5198 { "AST", &test_ast },
5199 { "AST build", &test_ast_build },
5200 { "AST generation", &test_ast_gen },
5201 { "eliminate", &test_eliminate },
5202 { "residue class", &test_residue_class },
5203 { "div", &test_div },
5204 { "slice", &test_slice },
5205 { "fixed power", &test_fixed_power },
5206 { "sample", &test_sample },
5207 { "output", &test_output },
5208 { "vertices", &test_vertices },
5209 { "fixed", &test_fixed },
5210 { "equal", &test_equal },
5211 { "disjoint", &test_disjoint },
5212 { "product", &test_product },
5213 { "dim_max", &test_dim_max },
5214 { "affine", &test_aff },
5215 { "injective", &test_injective },
5216 { "schedule", &test_schedule },
5217 { "union_pw", &test_union_pw },
5218 { "parse", &test_parse },
5219 { "single-valued", &test_sv },
5220 { "affine hull", &test_affine_hull },
5221 { "coalesce", &test_coalesce },
5222 { "factorize", &test_factorize },
5223 { "subset", &test_subset },
5224 { "subtract", &test_subtract },
5225 { "lexmin", &test_lexmin },
5226 { "min", &test_min },
5227 { "gist", &test_gist },
5228 { "piecewise quasi-polynomials", &test_pwqp },
5231 int main(int argc, char **argv)
5233 int i;
5234 struct isl_ctx *ctx;
5235 struct isl_options *options;
5237 srcdir = getenv("srcdir");
5238 assert(srcdir);
5240 options = isl_options_new_with_defaults();
5241 assert(options);
5242 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
5244 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
5245 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
5246 printf("%s\n", tests[i].name);
5247 if (tests[i].fn(ctx) < 0)
5248 goto error;
5250 test_lift(ctx);
5251 test_bound(ctx);
5252 test_union(ctx);
5253 test_split_periods(ctx);
5254 test_lex(ctx);
5255 test_bijective(ctx);
5256 test_dep(ctx);
5257 test_read(ctx);
5258 test_bounded(ctx);
5259 test_construction(ctx);
5260 test_dim(ctx);
5261 test_application(ctx);
5262 test_convex_hull(ctx);
5263 test_closure(ctx);
5264 isl_ctx_free(ctx);
5265 return 0;
5266 error:
5267 isl_ctx_free(ctx);
5268 return -1;