isl_coalesce.c: check_coalesce_into_eq: avoid double free on error path
[isl.git] / isl_test.c
blob25e9341a338d842da1f6f78ad662608bcb659b00
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/schedule_node.h>
32 #include <isl_options_private.h>
33 #include <isl/vertices.h>
34 #include <isl/ast_build.h>
35 #include <isl/val.h>
36 #include <isl/ilp.h>
37 #include <isl_ast_build_expr.h>
38 #include <isl/options.h>
40 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
42 static char *srcdir;
44 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
45 char *filename;
46 int length;
47 char *pattern = "%s/test_inputs/%s.%s";
49 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
50 + strlen(suffix) + 1;
51 filename = isl_alloc_array(ctx, char, length);
53 if (!filename)
54 return NULL;
56 sprintf(filename, pattern, srcdir, name, suffix);
58 return filename;
61 void test_parse_map(isl_ctx *ctx, const char *str)
63 isl_map *map;
65 map = isl_map_read_from_str(ctx, str);
66 assert(map);
67 isl_map_free(map);
70 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
72 isl_map *map, *map2;
73 int equal;
75 map = isl_map_read_from_str(ctx, str);
76 map2 = isl_map_read_from_str(ctx, str2);
77 equal = isl_map_is_equal(map, map2);
78 isl_map_free(map);
79 isl_map_free(map2);
81 if (equal < 0)
82 return -1;
83 if (!equal)
84 isl_die(ctx, isl_error_unknown, "maps not equal",
85 return -1);
87 return 0;
90 void test_parse_pwqp(isl_ctx *ctx, const char *str)
92 isl_pw_qpolynomial *pwqp;
94 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
95 assert(pwqp);
96 isl_pw_qpolynomial_free(pwqp);
99 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
101 isl_pw_aff *pwaff;
103 pwaff = isl_pw_aff_read_from_str(ctx, str);
104 assert(pwaff);
105 isl_pw_aff_free(pwaff);
108 /* Check that we can read an isl_multi_val from "str" without errors.
110 static int test_parse_multi_val(isl_ctx *ctx, const char *str)
112 isl_multi_val *mv;
114 mv = isl_multi_val_read_from_str(ctx, str);
115 isl_multi_val_free(mv);
117 return mv ? 0 : -1;
120 int test_parse(struct isl_ctx *ctx)
122 isl_map *map, *map2;
123 const char *str, *str2;
125 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
126 return -1;
127 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
128 return -1;
129 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
130 return -1;
132 str = "{ [i] -> [-i] }";
133 map = isl_map_read_from_str(ctx, str);
134 assert(map);
135 isl_map_free(map);
137 str = "{ A[i] -> L[([i/3])] }";
138 map = isl_map_read_from_str(ctx, str);
139 assert(map);
140 isl_map_free(map);
142 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
143 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
144 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
146 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
147 str2 = "{ [x, y] : 2y >= 6 - x }";
148 if (test_parse_map_equal(ctx, str, str2) < 0)
149 return -1;
151 if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
152 "{ [x,y] : x <= y, 2*y + 3 }") < 0)
153 return -1;
154 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
155 if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
156 str) < 0)
157 return -1;
159 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
160 map = isl_map_read_from_str(ctx, str);
161 str = "{ [new, old] -> [o0, o1] : "
162 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
163 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
164 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
165 map2 = isl_map_read_from_str(ctx, str);
166 assert(isl_map_is_equal(map, map2));
167 isl_map_free(map);
168 isl_map_free(map2);
170 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
171 map = isl_map_read_from_str(ctx, str);
172 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
173 map2 = isl_map_read_from_str(ctx, str);
174 assert(isl_map_is_equal(map, map2));
175 isl_map_free(map);
176 isl_map_free(map2);
178 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
179 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
180 if (test_parse_map_equal(ctx, str, str2) < 0)
181 return -1;
183 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
184 str2 = "{ [i,j] -> [min(i,j)] }";
185 if (test_parse_map_equal(ctx, str, str2) < 0)
186 return -1;
188 str = "{ [i,j] : i != j }";
189 str2 = "{ [i,j] : i < j or i > j }";
190 if (test_parse_map_equal(ctx, str, str2) < 0)
191 return -1;
193 str = "{ [i,j] : (i+1)*2 >= j }";
194 str2 = "{ [i, j] : j <= 2 + 2i }";
195 if (test_parse_map_equal(ctx, str, str2) < 0)
196 return -1;
198 str = "{ [i] -> [i > 0 ? 4 : 5] }";
199 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
200 if (test_parse_map_equal(ctx, str, str2) < 0)
201 return -1;
203 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
204 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
205 if (test_parse_map_equal(ctx, str, str2) < 0)
206 return -1;
208 str = "{ [x] : x >= 0 }";
209 str2 = "{ [x] : x-0 >= 0 }";
210 if (test_parse_map_equal(ctx, str, str2) < 0)
211 return -1;
213 str = "{ [i] : ((i > 10)) }";
214 str2 = "{ [i] : i >= 11 }";
215 if (test_parse_map_equal(ctx, str, str2) < 0)
216 return -1;
218 str = "{ [i] -> [0] }";
219 str2 = "{ [i] -> [0 * i] }";
220 if (test_parse_map_equal(ctx, str, str2) < 0)
221 return -1;
223 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
224 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
225 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
226 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
228 if (test_parse_map_equal(ctx, "{ [a] -> [b] : (not false) }",
229 "{ [a] -> [b] : true }") < 0)
230 return -1;
232 if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
233 "{ [i] : i <= 10 }") < 0)
234 return -1;
236 if (test_parse_map_equal(ctx, "{Sym=[n] [i] : i <= n }",
237 "[n] -> { [i] : i <= n }") < 0)
238 return -1;
240 if (test_parse_map_equal(ctx, "{ [*] }", "{ [a] }") < 0)
241 return -1;
243 if (test_parse_map_equal(ctx, "{ [i] : 2*floor(i/2) = i }",
244 "{ [i] : exists a : i = 2 a }") < 0)
245 return -1;
247 if (test_parse_map_equal(ctx, "{ [a] -> [b] : a = 5 implies b = 5 }",
248 "{ [a] -> [b] : a != 5 or b = 5 }") < 0)
249 return -1;
251 if (test_parse_map_equal(ctx, "{ [a] -> [a - 1 : a > 0] }",
252 "{ [a] -> [a - 1] : a > 0 }") < 0)
253 return -1;
254 if (test_parse_map_equal(ctx,
255 "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
256 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }") < 0)
257 return -1;
258 if (test_parse_map_equal(ctx,
259 "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
260 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
261 return -1;
262 if (test_parse_map_equal(ctx,
263 "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
264 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
265 return -1;
266 if (test_parse_map_equal(ctx,
267 "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
268 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
269 return -1;
270 if (test_parse_map_equal(ctx,
271 "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
272 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
273 return -1;
275 return 0;
278 void test_read(struct isl_ctx *ctx)
280 char *filename;
281 FILE *input;
282 struct isl_basic_set *bset1, *bset2;
283 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
285 filename = get_filename(ctx, "set", "omega");
286 assert(filename);
287 input = fopen(filename, "r");
288 assert(input);
290 bset1 = isl_basic_set_read_from_file(ctx, input);
291 bset2 = isl_basic_set_read_from_str(ctx, str);
293 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
295 isl_basic_set_free(bset1);
296 isl_basic_set_free(bset2);
297 free(filename);
299 fclose(input);
302 void test_bounded(struct isl_ctx *ctx)
304 isl_set *set;
305 int bounded;
307 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
308 bounded = isl_set_is_bounded(set);
309 assert(bounded);
310 isl_set_free(set);
312 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
313 bounded = isl_set_is_bounded(set);
314 assert(!bounded);
315 isl_set_free(set);
317 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
318 bounded = isl_set_is_bounded(set);
319 assert(!bounded);
320 isl_set_free(set);
323 /* Construct the basic set { [i] : 5 <= i <= N } */
324 void test_construction(struct isl_ctx *ctx)
326 isl_int v;
327 isl_space *dim;
328 isl_local_space *ls;
329 struct isl_basic_set *bset;
330 struct isl_constraint *c;
332 isl_int_init(v);
334 dim = isl_space_set_alloc(ctx, 1, 1);
335 bset = isl_basic_set_universe(isl_space_copy(dim));
336 ls = isl_local_space_from_space(dim);
338 c = isl_inequality_alloc(isl_local_space_copy(ls));
339 isl_int_set_si(v, -1);
340 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
341 isl_int_set_si(v, 1);
342 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
343 bset = isl_basic_set_add_constraint(bset, c);
345 c = isl_inequality_alloc(isl_local_space_copy(ls));
346 isl_int_set_si(v, 1);
347 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
348 isl_int_set_si(v, -5);
349 isl_constraint_set_constant(c, v);
350 bset = isl_basic_set_add_constraint(bset, c);
352 isl_local_space_free(ls);
353 isl_basic_set_free(bset);
355 isl_int_clear(v);
358 void test_dim(struct isl_ctx *ctx)
360 const char *str;
361 isl_map *map1, *map2;
363 map1 = isl_map_read_from_str(ctx,
364 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
365 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
366 map2 = isl_map_read_from_str(ctx,
367 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
368 assert(isl_map_is_equal(map1, map2));
369 isl_map_free(map2);
371 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
372 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
373 assert(isl_map_is_equal(map1, map2));
375 isl_map_free(map1);
376 isl_map_free(map2);
378 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
379 map1 = isl_map_read_from_str(ctx, str);
380 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
381 map2 = isl_map_read_from_str(ctx, str);
382 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
383 assert(isl_map_is_equal(map1, map2));
385 isl_map_free(map1);
386 isl_map_free(map2);
389 struct {
390 __isl_give isl_val *(*op)(__isl_take isl_val *v);
391 const char *arg;
392 const char *res;
393 } val_un_tests[] = {
394 { &isl_val_neg, "0", "0" },
395 { &isl_val_abs, "0", "0" },
396 { &isl_val_2exp, "0", "1" },
397 { &isl_val_floor, "0", "0" },
398 { &isl_val_ceil, "0", "0" },
399 { &isl_val_neg, "1", "-1" },
400 { &isl_val_neg, "-1", "1" },
401 { &isl_val_neg, "1/2", "-1/2" },
402 { &isl_val_neg, "-1/2", "1/2" },
403 { &isl_val_neg, "infty", "-infty" },
404 { &isl_val_neg, "-infty", "infty" },
405 { &isl_val_neg, "NaN", "NaN" },
406 { &isl_val_abs, "1", "1" },
407 { &isl_val_abs, "-1", "1" },
408 { &isl_val_abs, "1/2", "1/2" },
409 { &isl_val_abs, "-1/2", "1/2" },
410 { &isl_val_abs, "infty", "infty" },
411 { &isl_val_abs, "-infty", "infty" },
412 { &isl_val_abs, "NaN", "NaN" },
413 { &isl_val_floor, "1", "1" },
414 { &isl_val_floor, "-1", "-1" },
415 { &isl_val_floor, "1/2", "0" },
416 { &isl_val_floor, "-1/2", "-1" },
417 { &isl_val_floor, "infty", "infty" },
418 { &isl_val_floor, "-infty", "-infty" },
419 { &isl_val_floor, "NaN", "NaN" },
420 { &isl_val_ceil, "1", "1" },
421 { &isl_val_ceil, "-1", "-1" },
422 { &isl_val_ceil, "1/2", "1" },
423 { &isl_val_ceil, "-1/2", "0" },
424 { &isl_val_ceil, "infty", "infty" },
425 { &isl_val_ceil, "-infty", "-infty" },
426 { &isl_val_ceil, "NaN", "NaN" },
427 { &isl_val_2exp, "-3", "1/8" },
428 { &isl_val_2exp, "-1", "1/2" },
429 { &isl_val_2exp, "1", "2" },
430 { &isl_val_2exp, "2", "4" },
431 { &isl_val_2exp, "3", "8" },
432 { &isl_val_inv, "1", "1" },
433 { &isl_val_inv, "2", "1/2" },
434 { &isl_val_inv, "1/2", "2" },
435 { &isl_val_inv, "-2", "-1/2" },
436 { &isl_val_inv, "-1/2", "-2" },
437 { &isl_val_inv, "0", "NaN" },
438 { &isl_val_inv, "NaN", "NaN" },
439 { &isl_val_inv, "infty", "0" },
440 { &isl_val_inv, "-infty", "0" },
443 /* Perform some basic tests of unary operations on isl_val objects.
445 static int test_un_val(isl_ctx *ctx)
447 int i;
448 isl_val *v, *res;
449 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
450 int ok;
452 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
453 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
454 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
455 fn = val_un_tests[i].op;
456 v = fn(v);
457 if (isl_val_is_nan(res))
458 ok = isl_val_is_nan(v);
459 else
460 ok = isl_val_eq(v, res);
461 isl_val_free(v);
462 isl_val_free(res);
463 if (ok < 0)
464 return -1;
465 if (!ok)
466 isl_die(ctx, isl_error_unknown,
467 "unexpected result", return -1);
470 return 0;
473 struct {
474 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
475 __isl_take isl_val *v2);
476 } val_bin_op[] = {
477 ['+'] = { &isl_val_add },
478 ['-'] = { &isl_val_sub },
479 ['*'] = { &isl_val_mul },
480 ['/'] = { &isl_val_div },
481 ['g'] = { &isl_val_gcd },
482 ['m'] = { &isl_val_min },
483 ['M'] = { &isl_val_max },
486 struct {
487 const char *arg1;
488 unsigned char op;
489 const char *arg2;
490 const char *res;
491 } val_bin_tests[] = {
492 { "0", '+', "0", "0" },
493 { "1", '+', "0", "1" },
494 { "1", '+', "1", "2" },
495 { "1", '-', "1", "0" },
496 { "1", '*', "1", "1" },
497 { "1", '/', "1", "1" },
498 { "2", '*', "3", "6" },
499 { "2", '*', "1/2", "1" },
500 { "2", '*', "1/3", "2/3" },
501 { "2/3", '*', "3/5", "2/5" },
502 { "2/3", '*', "7/5", "14/15" },
503 { "2", '/', "1/2", "4" },
504 { "-2", '/', "-1/2", "4" },
505 { "-2", '/', "1/2", "-4" },
506 { "2", '/', "-1/2", "-4" },
507 { "2", '/', "2", "1" },
508 { "2", '/', "3", "2/3" },
509 { "2/3", '/', "5/3", "2/5" },
510 { "2/3", '/', "5/7", "14/15" },
511 { "0", '/', "0", "NaN" },
512 { "42", '/', "0", "NaN" },
513 { "-42", '/', "0", "NaN" },
514 { "infty", '/', "0", "NaN" },
515 { "-infty", '/', "0", "NaN" },
516 { "NaN", '/', "0", "NaN" },
517 { "0", '/', "NaN", "NaN" },
518 { "42", '/', "NaN", "NaN" },
519 { "-42", '/', "NaN", "NaN" },
520 { "infty", '/', "NaN", "NaN" },
521 { "-infty", '/', "NaN", "NaN" },
522 { "NaN", '/', "NaN", "NaN" },
523 { "0", '/', "infty", "0" },
524 { "42", '/', "infty", "0" },
525 { "-42", '/', "infty", "0" },
526 { "infty", '/', "infty", "NaN" },
527 { "-infty", '/', "infty", "NaN" },
528 { "NaN", '/', "infty", "NaN" },
529 { "0", '/', "-infty", "0" },
530 { "42", '/', "-infty", "0" },
531 { "-42", '/', "-infty", "0" },
532 { "infty", '/', "-infty", "NaN" },
533 { "-infty", '/', "-infty", "NaN" },
534 { "NaN", '/', "-infty", "NaN" },
535 { "1", '-', "1/3", "2/3" },
536 { "1/3", '+', "1/2", "5/6" },
537 { "1/2", '+', "1/2", "1" },
538 { "3/4", '-', "1/4", "1/2" },
539 { "1/2", '-', "1/3", "1/6" },
540 { "infty", '+', "42", "infty" },
541 { "infty", '+', "infty", "infty" },
542 { "42", '+', "infty", "infty" },
543 { "infty", '-', "infty", "NaN" },
544 { "infty", '*', "infty", "infty" },
545 { "infty", '*', "-infty", "-infty" },
546 { "-infty", '*', "infty", "-infty" },
547 { "-infty", '*', "-infty", "infty" },
548 { "0", '*', "infty", "NaN" },
549 { "1", '*', "infty", "infty" },
550 { "infty", '*', "0", "NaN" },
551 { "infty", '*', "42", "infty" },
552 { "42", '-', "infty", "-infty" },
553 { "infty", '+', "-infty", "NaN" },
554 { "4", 'g', "6", "2" },
555 { "5", 'g', "6", "1" },
556 { "42", 'm', "3", "3" },
557 { "42", 'M', "3", "42" },
558 { "3", 'm', "42", "3" },
559 { "3", 'M', "42", "42" },
560 { "42", 'm', "infty", "42" },
561 { "42", 'M', "infty", "infty" },
562 { "42", 'm', "-infty", "-infty" },
563 { "42", 'M', "-infty", "42" },
564 { "42", 'm', "NaN", "NaN" },
565 { "42", 'M', "NaN", "NaN" },
566 { "infty", 'm', "-infty", "-infty" },
567 { "infty", 'M', "-infty", "infty" },
570 /* Perform some basic tests of binary operations on isl_val objects.
572 static int test_bin_val(isl_ctx *ctx)
574 int i;
575 isl_val *v1, *v2, *res;
576 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
577 __isl_take isl_val *v2);
578 int ok;
580 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
581 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
582 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
583 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
584 fn = val_bin_op[val_bin_tests[i].op].fn;
585 v1 = fn(v1, v2);
586 if (isl_val_is_nan(res))
587 ok = isl_val_is_nan(v1);
588 else
589 ok = isl_val_eq(v1, res);
590 isl_val_free(v1);
591 isl_val_free(res);
592 if (ok < 0)
593 return -1;
594 if (!ok)
595 isl_die(ctx, isl_error_unknown,
596 "unexpected result", return -1);
599 return 0;
602 /* Perform some basic tests on isl_val objects.
604 static int test_val(isl_ctx *ctx)
606 if (test_un_val(ctx) < 0)
607 return -1;
608 if (test_bin_val(ctx) < 0)
609 return -1;
610 return 0;
613 static int test_div(isl_ctx *ctx)
615 unsigned n;
616 const char *str;
617 int empty;
618 isl_int v;
619 isl_space *dim;
620 isl_set *set;
621 isl_local_space *ls;
622 struct isl_basic_set *bset;
623 struct isl_constraint *c;
625 isl_int_init(v);
627 /* test 1 */
628 dim = isl_space_set_alloc(ctx, 0, 3);
629 bset = isl_basic_set_universe(isl_space_copy(dim));
630 ls = isl_local_space_from_space(dim);
632 c = isl_equality_alloc(isl_local_space_copy(ls));
633 isl_int_set_si(v, -1);
634 isl_constraint_set_constant(c, v);
635 isl_int_set_si(v, 1);
636 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
637 isl_int_set_si(v, 3);
638 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
639 bset = isl_basic_set_add_constraint(bset, c);
641 c = isl_equality_alloc(isl_local_space_copy(ls));
642 isl_int_set_si(v, 1);
643 isl_constraint_set_constant(c, v);
644 isl_int_set_si(v, -1);
645 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
646 isl_int_set_si(v, 3);
647 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
648 bset = isl_basic_set_add_constraint(bset, c);
650 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
652 assert(bset && bset->n_div == 1);
653 isl_local_space_free(ls);
654 isl_basic_set_free(bset);
656 /* test 2 */
657 dim = isl_space_set_alloc(ctx, 0, 3);
658 bset = isl_basic_set_universe(isl_space_copy(dim));
659 ls = isl_local_space_from_space(dim);
661 c = isl_equality_alloc(isl_local_space_copy(ls));
662 isl_int_set_si(v, 1);
663 isl_constraint_set_constant(c, v);
664 isl_int_set_si(v, -1);
665 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
666 isl_int_set_si(v, 3);
667 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
668 bset = isl_basic_set_add_constraint(bset, c);
670 c = isl_equality_alloc(isl_local_space_copy(ls));
671 isl_int_set_si(v, -1);
672 isl_constraint_set_constant(c, v);
673 isl_int_set_si(v, 1);
674 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
675 isl_int_set_si(v, 3);
676 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
677 bset = isl_basic_set_add_constraint(bset, c);
679 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
681 assert(bset && bset->n_div == 1);
682 isl_local_space_free(ls);
683 isl_basic_set_free(bset);
685 /* test 3 */
686 dim = isl_space_set_alloc(ctx, 0, 3);
687 bset = isl_basic_set_universe(isl_space_copy(dim));
688 ls = isl_local_space_from_space(dim);
690 c = isl_equality_alloc(isl_local_space_copy(ls));
691 isl_int_set_si(v, 1);
692 isl_constraint_set_constant(c, v);
693 isl_int_set_si(v, -1);
694 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
695 isl_int_set_si(v, 3);
696 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
697 bset = isl_basic_set_add_constraint(bset, c);
699 c = isl_equality_alloc(isl_local_space_copy(ls));
700 isl_int_set_si(v, -3);
701 isl_constraint_set_constant(c, v);
702 isl_int_set_si(v, 1);
703 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
704 isl_int_set_si(v, 4);
705 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
706 bset = isl_basic_set_add_constraint(bset, c);
708 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
710 assert(bset && bset->n_div == 1);
711 isl_local_space_free(ls);
712 isl_basic_set_free(bset);
714 /* test 4 */
715 dim = isl_space_set_alloc(ctx, 0, 3);
716 bset = isl_basic_set_universe(isl_space_copy(dim));
717 ls = isl_local_space_from_space(dim);
719 c = isl_equality_alloc(isl_local_space_copy(ls));
720 isl_int_set_si(v, 2);
721 isl_constraint_set_constant(c, v);
722 isl_int_set_si(v, -1);
723 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
724 isl_int_set_si(v, 3);
725 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
726 bset = isl_basic_set_add_constraint(bset, c);
728 c = isl_equality_alloc(isl_local_space_copy(ls));
729 isl_int_set_si(v, -1);
730 isl_constraint_set_constant(c, v);
731 isl_int_set_si(v, 1);
732 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
733 isl_int_set_si(v, 6);
734 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
735 bset = isl_basic_set_add_constraint(bset, c);
737 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
739 assert(isl_basic_set_is_empty(bset));
740 isl_local_space_free(ls);
741 isl_basic_set_free(bset);
743 /* test 5 */
744 dim = isl_space_set_alloc(ctx, 0, 3);
745 bset = isl_basic_set_universe(isl_space_copy(dim));
746 ls = isl_local_space_from_space(dim);
748 c = isl_equality_alloc(isl_local_space_copy(ls));
749 isl_int_set_si(v, -1);
750 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
751 isl_int_set_si(v, 3);
752 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
753 bset = isl_basic_set_add_constraint(bset, c);
755 c = isl_equality_alloc(isl_local_space_copy(ls));
756 isl_int_set_si(v, 1);
757 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
758 isl_int_set_si(v, -3);
759 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
760 bset = isl_basic_set_add_constraint(bset, c);
762 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
764 assert(bset && bset->n_div == 0);
765 isl_basic_set_free(bset);
766 isl_local_space_free(ls);
768 /* test 6 */
769 dim = isl_space_set_alloc(ctx, 0, 3);
770 bset = isl_basic_set_universe(isl_space_copy(dim));
771 ls = isl_local_space_from_space(dim);
773 c = isl_equality_alloc(isl_local_space_copy(ls));
774 isl_int_set_si(v, -1);
775 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
776 isl_int_set_si(v, 6);
777 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
778 bset = isl_basic_set_add_constraint(bset, c);
780 c = isl_equality_alloc(isl_local_space_copy(ls));
781 isl_int_set_si(v, 1);
782 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
783 isl_int_set_si(v, -3);
784 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
785 bset = isl_basic_set_add_constraint(bset, c);
787 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
789 assert(bset && bset->n_div == 1);
790 isl_basic_set_free(bset);
791 isl_local_space_free(ls);
793 /* test 7 */
794 /* This test is a bit tricky. We set up an equality
795 * a + 3b + 3c = 6 e0
796 * Normalization of divs creates _two_ divs
797 * a = 3 e0
798 * c - b - e0 = 2 e1
799 * Afterwards e0 is removed again because it has coefficient -1
800 * and we end up with the original equality and div again.
801 * Perhaps we can avoid the introduction of this temporary div.
803 dim = isl_space_set_alloc(ctx, 0, 4);
804 bset = isl_basic_set_universe(isl_space_copy(dim));
805 ls = isl_local_space_from_space(dim);
807 c = isl_equality_alloc(isl_local_space_copy(ls));
808 isl_int_set_si(v, -1);
809 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
810 isl_int_set_si(v, -3);
811 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
812 isl_int_set_si(v, -3);
813 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
814 isl_int_set_si(v, 6);
815 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
816 bset = isl_basic_set_add_constraint(bset, c);
818 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
820 /* Test disabled for now */
822 assert(bset && bset->n_div == 1);
824 isl_local_space_free(ls);
825 isl_basic_set_free(bset);
827 /* test 8 */
828 dim = isl_space_set_alloc(ctx, 0, 5);
829 bset = isl_basic_set_universe(isl_space_copy(dim));
830 ls = isl_local_space_from_space(dim);
832 c = isl_equality_alloc(isl_local_space_copy(ls));
833 isl_int_set_si(v, -1);
834 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
835 isl_int_set_si(v, -3);
836 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
837 isl_int_set_si(v, -3);
838 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
839 isl_int_set_si(v, 6);
840 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
841 bset = isl_basic_set_add_constraint(bset, c);
843 c = isl_equality_alloc(isl_local_space_copy(ls));
844 isl_int_set_si(v, -1);
845 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
846 isl_int_set_si(v, 1);
847 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
848 isl_int_set_si(v, 1);
849 isl_constraint_set_constant(c, v);
850 bset = isl_basic_set_add_constraint(bset, c);
852 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
854 /* Test disabled for now */
856 assert(bset && bset->n_div == 1);
858 isl_local_space_free(ls);
859 isl_basic_set_free(bset);
861 /* test 9 */
862 dim = isl_space_set_alloc(ctx, 0, 4);
863 bset = isl_basic_set_universe(isl_space_copy(dim));
864 ls = isl_local_space_from_space(dim);
866 c = isl_equality_alloc(isl_local_space_copy(ls));
867 isl_int_set_si(v, 1);
868 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
869 isl_int_set_si(v, -1);
870 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
871 isl_int_set_si(v, -2);
872 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
873 bset = isl_basic_set_add_constraint(bset, c);
875 c = isl_equality_alloc(isl_local_space_copy(ls));
876 isl_int_set_si(v, -1);
877 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
878 isl_int_set_si(v, 3);
879 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
880 isl_int_set_si(v, 2);
881 isl_constraint_set_constant(c, v);
882 bset = isl_basic_set_add_constraint(bset, c);
884 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
886 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
888 assert(!isl_basic_set_is_empty(bset));
890 isl_local_space_free(ls);
891 isl_basic_set_free(bset);
893 /* test 10 */
894 dim = isl_space_set_alloc(ctx, 0, 3);
895 bset = isl_basic_set_universe(isl_space_copy(dim));
896 ls = isl_local_space_from_space(dim);
898 c = isl_equality_alloc(isl_local_space_copy(ls));
899 isl_int_set_si(v, 1);
900 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
901 isl_int_set_si(v, -2);
902 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
903 bset = isl_basic_set_add_constraint(bset, c);
905 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
907 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
909 isl_local_space_free(ls);
910 isl_basic_set_free(bset);
912 isl_int_clear(v);
914 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
915 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
916 set = isl_set_read_from_str(ctx, str);
917 set = isl_set_compute_divs(set);
918 isl_set_free(set);
919 if (!set)
920 return -1;
922 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
923 bset = isl_basic_set_read_from_str(ctx, str);
924 n = isl_basic_set_dim(bset, isl_dim_div);
925 isl_basic_set_free(bset);
926 if (!bset)
927 return -1;
928 if (n != 0)
929 isl_die(ctx, isl_error_unknown,
930 "expecting no existentials", return -1);
932 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
933 set = isl_set_read_from_str(ctx, str);
934 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
935 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
936 empty = isl_set_is_empty(set);
937 isl_set_free(set);
938 if (empty < 0)
939 return -1;
940 if (!empty)
941 isl_die(ctx, isl_error_unknown,
942 "result not as accurate as expected", return -1);
944 return 0;
947 void test_application_case(struct isl_ctx *ctx, const char *name)
949 char *filename;
950 FILE *input;
951 struct isl_basic_set *bset1, *bset2;
952 struct isl_basic_map *bmap;
954 filename = get_filename(ctx, name, "omega");
955 assert(filename);
956 input = fopen(filename, "r");
957 assert(input);
959 bset1 = isl_basic_set_read_from_file(ctx, input);
960 bmap = isl_basic_map_read_from_file(ctx, input);
962 bset1 = isl_basic_set_apply(bset1, bmap);
964 bset2 = isl_basic_set_read_from_file(ctx, input);
966 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
968 isl_basic_set_free(bset1);
969 isl_basic_set_free(bset2);
970 free(filename);
972 fclose(input);
975 void test_application(struct isl_ctx *ctx)
977 test_application_case(ctx, "application");
978 test_application_case(ctx, "application2");
981 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
983 char *filename;
984 FILE *input;
985 struct isl_basic_set *bset1, *bset2;
987 filename = get_filename(ctx, name, "polylib");
988 assert(filename);
989 input = fopen(filename, "r");
990 assert(input);
992 bset1 = isl_basic_set_read_from_file(ctx, input);
993 bset2 = isl_basic_set_read_from_file(ctx, input);
995 bset1 = isl_basic_set_affine_hull(bset1);
997 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
999 isl_basic_set_free(bset1);
1000 isl_basic_set_free(bset2);
1001 free(filename);
1003 fclose(input);
1006 int test_affine_hull(struct isl_ctx *ctx)
1008 const char *str;
1009 isl_set *set;
1010 isl_basic_set *bset, *bset2;
1011 int n;
1012 int subset;
1014 test_affine_hull_case(ctx, "affine2");
1015 test_affine_hull_case(ctx, "affine");
1016 test_affine_hull_case(ctx, "affine3");
1018 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1019 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1020 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1021 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1022 set = isl_set_read_from_str(ctx, str);
1023 bset = isl_set_affine_hull(set);
1024 n = isl_basic_set_dim(bset, isl_dim_div);
1025 isl_basic_set_free(bset);
1026 if (n != 0)
1027 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1028 return -1);
1030 /* Check that isl_map_affine_hull is not confused by
1031 * the reordering of divs in isl_map_align_divs.
1033 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1034 "32e0 = b and 32e1 = c); "
1035 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1036 set = isl_set_read_from_str(ctx, str);
1037 bset = isl_set_affine_hull(set);
1038 isl_basic_set_free(bset);
1039 if (!bset)
1040 return -1;
1042 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1043 "32e2 = 31 + 31e0 }";
1044 set = isl_set_read_from_str(ctx, str);
1045 bset = isl_set_affine_hull(set);
1046 str = "{ [a] : exists e : a = 32 e }";
1047 bset2 = isl_basic_set_read_from_str(ctx, str);
1048 subset = isl_basic_set_is_subset(bset, bset2);
1049 isl_basic_set_free(bset);
1050 isl_basic_set_free(bset2);
1051 if (subset < 0)
1052 return -1;
1053 if (!subset)
1054 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1055 return -1);
1057 return 0;
1060 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1062 char *filename;
1063 FILE *input;
1064 struct isl_basic_set *bset1, *bset2;
1065 struct isl_set *set;
1067 filename = get_filename(ctx, name, "polylib");
1068 assert(filename);
1069 input = fopen(filename, "r");
1070 assert(input);
1072 bset1 = isl_basic_set_read_from_file(ctx, input);
1073 bset2 = isl_basic_set_read_from_file(ctx, input);
1075 set = isl_basic_set_union(bset1, bset2);
1076 bset1 = isl_set_convex_hull(set);
1078 bset2 = isl_basic_set_read_from_file(ctx, input);
1080 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1082 isl_basic_set_free(bset1);
1083 isl_basic_set_free(bset2);
1084 free(filename);
1086 fclose(input);
1089 struct {
1090 const char *set;
1091 const char *hull;
1092 } convex_hull_tests[] = {
1093 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1094 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1095 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1096 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1097 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1098 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1099 "i2 <= 5 and i2 >= 4; "
1100 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1101 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1102 "i2 <= 5 + i0 and i2 >= i0 }" },
1103 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1104 "{ [x, y] : 1 = 0 }" },
1107 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
1109 int i;
1110 int orig_convex = ctx->opt->convex;
1111 ctx->opt->convex = convex;
1113 test_convex_hull_case(ctx, "convex0");
1114 test_convex_hull_case(ctx, "convex1");
1115 test_convex_hull_case(ctx, "convex2");
1116 test_convex_hull_case(ctx, "convex3");
1117 test_convex_hull_case(ctx, "convex4");
1118 test_convex_hull_case(ctx, "convex5");
1119 test_convex_hull_case(ctx, "convex6");
1120 test_convex_hull_case(ctx, "convex7");
1121 test_convex_hull_case(ctx, "convex8");
1122 test_convex_hull_case(ctx, "convex9");
1123 test_convex_hull_case(ctx, "convex10");
1124 test_convex_hull_case(ctx, "convex11");
1125 test_convex_hull_case(ctx, "convex12");
1126 test_convex_hull_case(ctx, "convex13");
1127 test_convex_hull_case(ctx, "convex14");
1128 test_convex_hull_case(ctx, "convex15");
1130 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1131 isl_set *set1, *set2;
1133 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1134 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1135 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1136 assert(isl_set_is_equal(set1, set2));
1137 isl_set_free(set1);
1138 isl_set_free(set2);
1141 ctx->opt->convex = orig_convex;
1144 void test_convex_hull(struct isl_ctx *ctx)
1146 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
1147 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
1150 void test_gist_case(struct isl_ctx *ctx, const char *name)
1152 char *filename;
1153 FILE *input;
1154 struct isl_basic_set *bset1, *bset2;
1156 filename = get_filename(ctx, name, "polylib");
1157 assert(filename);
1158 input = fopen(filename, "r");
1159 assert(input);
1161 bset1 = isl_basic_set_read_from_file(ctx, input);
1162 bset2 = isl_basic_set_read_from_file(ctx, input);
1164 bset1 = isl_basic_set_gist(bset1, bset2);
1166 bset2 = isl_basic_set_read_from_file(ctx, input);
1168 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1170 isl_basic_set_free(bset1);
1171 isl_basic_set_free(bset2);
1172 free(filename);
1174 fclose(input);
1177 struct {
1178 const char *set;
1179 const char *context;
1180 const char *gist;
1181 } gist_tests[] = {
1182 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1183 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1184 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1185 "{ [a, b, c] : a <= 15 }" },
1186 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1187 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1188 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1189 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1190 { "{ [m, n, a, b] : a <= 2147 + n }",
1191 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1192 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1193 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1194 "b >= 0) }",
1195 "{ [m, n, ku, kl] }" },
1198 static int test_gist(struct isl_ctx *ctx)
1200 int i;
1201 const char *str;
1202 isl_basic_set *bset1, *bset2;
1203 isl_map *map1, *map2;
1204 int equal;
1206 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1207 int equal_input;
1208 isl_set *set1, *set2, *copy;
1210 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1211 set2 = isl_set_read_from_str(ctx, gist_tests[i].context);
1212 copy = isl_set_copy(set1);
1213 set1 = isl_set_gist(set1, set2);
1214 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1215 equal = isl_set_is_equal(set1, set2);
1216 isl_set_free(set1);
1217 isl_set_free(set2);
1218 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1219 equal_input = isl_set_is_equal(set1, copy);
1220 isl_set_free(set1);
1221 isl_set_free(copy);
1222 if (equal < 0 || equal_input < 0)
1223 return -1;
1224 if (!equal)
1225 isl_die(ctx, isl_error_unknown,
1226 "incorrect gist result", return -1);
1227 if (!equal_input)
1228 isl_die(ctx, isl_error_unknown,
1229 "gist modified input", return -1);
1232 test_gist_case(ctx, "gist1");
1234 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1235 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1236 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1237 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1238 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1239 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1240 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1241 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1242 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1243 bset1 = isl_basic_set_read_from_str(ctx, str);
1244 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1245 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1246 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1247 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1248 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1249 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1250 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1251 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1252 bset2 = isl_basic_set_read_from_str(ctx, str);
1253 bset1 = isl_basic_set_gist(bset1, bset2);
1254 assert(bset1 && bset1->n_div == 0);
1255 isl_basic_set_free(bset1);
1257 /* Check that the integer divisions of the second disjunct
1258 * do not spread to the first disjunct.
1260 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1261 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1262 "(exists (e0 = [(-1 + t1)/16], "
1263 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1264 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1265 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1266 "o0 <= 4294967295 and t1 <= -1)) }";
1267 map1 = isl_map_read_from_str(ctx, str);
1268 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1269 map2 = isl_map_read_from_str(ctx, str);
1270 map1 = isl_map_gist(map1, map2);
1271 if (!map1)
1272 return -1;
1273 if (map1->n != 1)
1274 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1275 isl_map_free(map1); return -1);
1276 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1277 isl_die(ctx, isl_error_unknown, "expecting single div",
1278 isl_map_free(map1); return -1);
1279 isl_map_free(map1);
1281 return 0;
1284 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1286 isl_set *set, *set2;
1287 int equal;
1288 int one;
1290 set = isl_set_read_from_str(ctx, str);
1291 set = isl_set_coalesce(set);
1292 set2 = isl_set_read_from_str(ctx, str);
1293 equal = isl_set_is_equal(set, set2);
1294 one = set && set->n == 1;
1295 isl_set_free(set);
1296 isl_set_free(set2);
1298 if (equal < 0)
1299 return -1;
1300 if (!equal)
1301 isl_die(ctx, isl_error_unknown,
1302 "coalesced set not equal to input", return -1);
1303 if (check_one && !one)
1304 isl_die(ctx, isl_error_unknown,
1305 "coalesced set should not be a union", return -1);
1307 return 0;
1310 /* Inputs for coalescing tests with unbounded wrapping.
1311 * "str" is a string representation of the input set.
1312 * "single_disjunct" is set if we expect the result to consist of
1313 * a single disjunct.
1315 struct {
1316 int single_disjunct;
1317 const char *str;
1318 } coalesce_unbounded_tests[] = {
1319 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1320 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1321 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1322 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1323 "-10 <= y <= 0}" },
1324 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1325 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1326 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1327 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1330 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1331 * option turned off.
1333 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1335 int i;
1336 int r = 0;
1337 int bounded;
1339 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1340 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1342 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1343 const char *str = coalesce_unbounded_tests[i].str;
1344 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1345 if (test_coalesce_set(ctx, str, check_one) >= 0)
1346 continue;
1347 r = -1;
1348 break;
1351 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1353 return r;
1356 /* Inputs for coalescing tests.
1357 * "str" is a string representation of the input set.
1358 * "single_disjunct" is set if we expect the result to consist of
1359 * a single disjunct.
1361 struct {
1362 int single_disjunct;
1363 const char *str;
1364 } coalesce_tests[] = {
1365 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1366 "y >= x & x >= 2 & 5 >= y }" },
1367 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1368 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1369 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1370 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1371 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1372 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1373 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1374 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1375 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1376 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1377 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1378 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1379 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1380 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1381 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1382 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1383 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1384 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1385 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1386 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1387 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1388 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1389 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1390 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1391 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1392 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1393 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1394 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1395 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1396 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1397 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1398 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1399 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1400 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1401 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1402 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1403 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1404 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1405 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1406 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1407 "[o0, o1, o2, o3, o4, o5, o6]] : "
1408 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1409 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1410 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1411 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1412 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1413 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1414 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1415 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1416 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1417 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1418 "o6 >= i3 + i6 - o3 and M >= 0 and "
1419 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1420 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1421 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1422 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1423 "(o0 = 0 and M >= 2 and N >= 3) or "
1424 "(M = 0 and o0 = 0 and N >= 3) }" },
1425 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1426 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1427 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1428 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1429 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1430 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1431 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1432 "(y = 3 and x = 1) }" },
1433 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1434 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1435 "i1 <= M and i3 <= M and i4 <= M) or "
1436 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1437 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1438 "i4 <= -1 + M) }" },
1439 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1440 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1441 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1442 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1443 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1444 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1445 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1446 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1447 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1448 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1449 { 0, "{ [a, b] : exists e : 2e = a and "
1450 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1451 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1452 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1453 "j >= 1 and j' <= i + j - i' and i >= 1; "
1454 "[1, 1, 1, 1] }" },
1455 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1456 "[i,j] : exists a : j = 3a }" },
1457 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1458 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1459 "a >= 3) or "
1460 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1461 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1462 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1463 "c <= 6 + 8a and a >= 3; "
1464 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1465 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1466 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1467 "[x,0] : 3 <= x <= 4 }" },
1468 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1469 "[x,0] : 4 <= x <= 5 }" },
1470 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1471 "[x,0] : 3 <= x <= 5 }" },
1472 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1473 "[x,0] : 3 <= x <= 4 }" },
1474 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1475 "i1 <= 0; "
1476 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1477 { 1, "{ [0,0]; [1,1] }" },
1478 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1479 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1480 "ii <= k;"
1481 "[k, 0, k] : k <= 6 and k >= 1 }" },
1482 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1483 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1484 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1485 { 1, "[n] -> { [1] : n >= 0;"
1486 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1487 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1488 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1489 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1490 "2e0 <= x and 2e0 <= n);"
1491 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1492 "n >= 0) }" },
1493 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1494 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1495 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1496 "t1 = 1 }" },
1497 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1498 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1499 "[0, 0] }" },
1500 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1501 "t1 >= 13 and t1 <= 16);"
1502 "[t1] : t1 <= 15 and t1 >= 12 }" },
1503 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
1504 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
1505 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
1506 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
1507 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
1508 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
1509 "i <= 4j + 2 }" },
1510 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
1511 "(exists (e0 : 3e0 = -2 + c0)) }" },
1512 { 0, "[n, b0, t0] -> "
1513 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1514 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1515 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1516 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1517 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
1518 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
1519 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
1520 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
1521 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1522 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1523 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1524 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
1525 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
1526 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
1527 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
1528 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
1529 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
1530 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
1531 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
1532 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
1535 /* A specialized coalescing test case that would result
1536 * in a segmentation fault or a failed assertion in earlier versions of isl.
1538 static int test_coalesce_special(struct isl_ctx *ctx)
1540 const char *str;
1541 isl_map *map1, *map2;
1543 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1544 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
1545 "(y = 201 and o1 <= 239 and o1 >= 212) or "
1546 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
1547 "o1 <= 239 and o1 >= 212)) or "
1548 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
1549 "o1 <= 241 and o1 >= 240));"
1550 "[S_L220_OUT[] -> T7[]] -> "
1551 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
1552 "(y = 2 and o1 <= 241 and o1 >= 212) or "
1553 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
1554 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
1555 map1 = isl_map_read_from_str(ctx, str);
1556 map1 = isl_map_align_divs(map1);
1557 map1 = isl_map_coalesce(map1);
1558 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1559 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
1560 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
1561 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
1562 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
1563 map2 = isl_map_read_from_str(ctx, str);
1564 map2 = isl_map_union(map2, map1);
1565 map2 = isl_map_align_divs(map2);
1566 map2 = isl_map_coalesce(map2);
1567 isl_map_free(map2);
1568 if (!map2)
1569 return -1;
1571 return 0;
1574 /* Test the functionality of isl_set_coalesce.
1575 * That is, check that the output is always equal to the input
1576 * and in some cases that the result consists of a single disjunct.
1578 static int test_coalesce(struct isl_ctx *ctx)
1580 int i;
1582 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1583 const char *str = coalesce_tests[i].str;
1584 int check_one = coalesce_tests[i].single_disjunct;
1585 if (test_coalesce_set(ctx, str, check_one) < 0)
1586 return -1;
1589 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1590 return -1;
1591 if (test_coalesce_special(ctx) < 0)
1592 return -1;
1594 return 0;
1597 void test_closure(struct isl_ctx *ctx)
1599 const char *str;
1600 isl_set *dom;
1601 isl_map *up, *right;
1602 isl_map *map, *map2;
1603 int exact;
1605 /* COCOA example 1 */
1606 map = isl_map_read_from_str(ctx,
1607 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1608 "1 <= i and i < n and 1 <= j and j < n or "
1609 "i2 = i + 1 and j2 = j - 1 and "
1610 "1 <= i and i < n and 2 <= j and j <= n }");
1611 map = isl_map_power(map, &exact);
1612 assert(exact);
1613 isl_map_free(map);
1615 /* COCOA example 1 */
1616 map = isl_map_read_from_str(ctx,
1617 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1618 "1 <= i and i < n and 1 <= j and j < n or "
1619 "i2 = i + 1 and j2 = j - 1 and "
1620 "1 <= i and i < n and 2 <= j and j <= n }");
1621 map = isl_map_transitive_closure(map, &exact);
1622 assert(exact);
1623 map2 = isl_map_read_from_str(ctx,
1624 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1625 "1 <= i and i < n and 1 <= j and j <= n and "
1626 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1627 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1628 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1629 assert(isl_map_is_equal(map, map2));
1630 isl_map_free(map2);
1631 isl_map_free(map);
1633 map = isl_map_read_from_str(ctx,
1634 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1635 " 0 <= y and y <= n }");
1636 map = isl_map_transitive_closure(map, &exact);
1637 map2 = isl_map_read_from_str(ctx,
1638 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1639 " 0 <= y and y <= n }");
1640 assert(isl_map_is_equal(map, map2));
1641 isl_map_free(map2);
1642 isl_map_free(map);
1644 /* COCOA example 2 */
1645 map = isl_map_read_from_str(ctx,
1646 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1647 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1648 "i2 = i + 2 and j2 = j - 2 and "
1649 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1650 map = isl_map_transitive_closure(map, &exact);
1651 assert(exact);
1652 map2 = isl_map_read_from_str(ctx,
1653 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1654 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1655 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1656 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1657 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1658 assert(isl_map_is_equal(map, map2));
1659 isl_map_free(map);
1660 isl_map_free(map2);
1662 /* COCOA Fig.2 left */
1663 map = isl_map_read_from_str(ctx,
1664 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1665 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1666 "j <= n or "
1667 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1668 "j <= 2 i - 3 and j <= n - 2 or "
1669 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1670 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1671 map = isl_map_transitive_closure(map, &exact);
1672 assert(exact);
1673 isl_map_free(map);
1675 /* COCOA Fig.2 right */
1676 map = isl_map_read_from_str(ctx,
1677 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1678 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1679 "j <= n or "
1680 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1681 "j <= 2 i - 4 and j <= n - 3 or "
1682 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1683 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1684 map = isl_map_power(map, &exact);
1685 assert(exact);
1686 isl_map_free(map);
1688 /* COCOA Fig.2 right */
1689 map = isl_map_read_from_str(ctx,
1690 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1691 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1692 "j <= n or "
1693 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1694 "j <= 2 i - 4 and j <= n - 3 or "
1695 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1696 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1697 map = isl_map_transitive_closure(map, &exact);
1698 assert(exact);
1699 map2 = isl_map_read_from_str(ctx,
1700 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1701 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1702 "j <= n and 3 + i + 2 j <= 3 n and "
1703 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1704 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1705 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1706 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1707 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1708 assert(isl_map_is_equal(map, map2));
1709 isl_map_free(map2);
1710 isl_map_free(map);
1712 /* COCOA Fig.1 right */
1713 dom = isl_set_read_from_str(ctx,
1714 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1715 "2 x - 3 y + 3 >= 0 }");
1716 right = isl_map_read_from_str(ctx,
1717 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1718 up = isl_map_read_from_str(ctx,
1719 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1720 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1721 right = isl_map_intersect_range(right, isl_set_copy(dom));
1722 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1723 up = isl_map_intersect_range(up, dom);
1724 map = isl_map_union(up, right);
1725 map = isl_map_transitive_closure(map, &exact);
1726 assert(exact);
1727 map2 = isl_map_read_from_str(ctx,
1728 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1729 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1730 assert(isl_map_is_equal(map, map2));
1731 isl_map_free(map2);
1732 isl_map_free(map);
1734 /* COCOA Theorem 1 counter example */
1735 map = isl_map_read_from_str(ctx,
1736 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1737 "i2 = 1 and j2 = j or "
1738 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1739 map = isl_map_transitive_closure(map, &exact);
1740 assert(exact);
1741 isl_map_free(map);
1743 map = isl_map_read_from_str(ctx,
1744 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1745 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1746 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1747 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1748 map = isl_map_transitive_closure(map, &exact);
1749 assert(exact);
1750 isl_map_free(map);
1752 /* Kelly et al 1996, fig 12 */
1753 map = isl_map_read_from_str(ctx,
1754 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1755 "1 <= i,j,j+1 <= n or "
1756 "j = n and j2 = 1 and i2 = i + 1 and "
1757 "1 <= i,i+1 <= n }");
1758 map = isl_map_transitive_closure(map, &exact);
1759 assert(exact);
1760 map2 = isl_map_read_from_str(ctx,
1761 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1762 "1 <= i <= n and i = i2 or "
1763 "1 <= i < i2 <= n and 1 <= j <= n and "
1764 "1 <= j2 <= n }");
1765 assert(isl_map_is_equal(map, map2));
1766 isl_map_free(map2);
1767 isl_map_free(map);
1769 /* Omega's closure4 */
1770 map = isl_map_read_from_str(ctx,
1771 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1772 "1 <= x,y <= 10 or "
1773 "x2 = x + 1 and y2 = y and "
1774 "1 <= x <= 20 && 5 <= y <= 15 }");
1775 map = isl_map_transitive_closure(map, &exact);
1776 assert(exact);
1777 isl_map_free(map);
1779 map = isl_map_read_from_str(ctx,
1780 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1781 map = isl_map_transitive_closure(map, &exact);
1782 assert(!exact);
1783 map2 = isl_map_read_from_str(ctx,
1784 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1785 assert(isl_map_is_equal(map, map2));
1786 isl_map_free(map);
1787 isl_map_free(map2);
1789 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1790 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1791 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1792 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1793 map = isl_map_read_from_str(ctx, str);
1794 map = isl_map_transitive_closure(map, &exact);
1795 assert(exact);
1796 map2 = isl_map_read_from_str(ctx, str);
1797 assert(isl_map_is_equal(map, map2));
1798 isl_map_free(map);
1799 isl_map_free(map2);
1801 str = "{[0] -> [1]; [2] -> [3]}";
1802 map = isl_map_read_from_str(ctx, str);
1803 map = isl_map_transitive_closure(map, &exact);
1804 assert(exact);
1805 map2 = isl_map_read_from_str(ctx, str);
1806 assert(isl_map_is_equal(map, map2));
1807 isl_map_free(map);
1808 isl_map_free(map2);
1810 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1811 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1812 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1813 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1814 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1815 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1816 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1817 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1818 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1819 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1820 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1821 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1822 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1823 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1824 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1825 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1826 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1827 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1828 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1829 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1830 map = isl_map_read_from_str(ctx, str);
1831 map = isl_map_transitive_closure(map, NULL);
1832 assert(map);
1833 isl_map_free(map);
1836 void test_lex(struct isl_ctx *ctx)
1838 isl_space *dim;
1839 isl_map *map;
1841 dim = isl_space_set_alloc(ctx, 0, 0);
1842 map = isl_map_lex_le(dim);
1843 assert(!isl_map_is_empty(map));
1844 isl_map_free(map);
1847 static int test_lexmin(struct isl_ctx *ctx)
1849 int equal;
1850 const char *str;
1851 isl_basic_map *bmap;
1852 isl_map *map, *map2;
1853 isl_set *set;
1854 isl_set *set2;
1855 isl_pw_multi_aff *pma;
1857 str = "[p0, p1] -> { [] -> [] : "
1858 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1859 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1860 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1861 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1862 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1863 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1864 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1865 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1866 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1867 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1868 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1869 map = isl_map_read_from_str(ctx, str);
1870 map = isl_map_lexmin(map);
1871 isl_map_free(map);
1873 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1874 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1875 set = isl_set_read_from_str(ctx, str);
1876 set = isl_set_lexmax(set);
1877 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1878 set2 = isl_set_read_from_str(ctx, str);
1879 set = isl_set_intersect(set, set2);
1880 assert(!isl_set_is_empty(set));
1881 isl_set_free(set);
1883 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1884 map = isl_map_read_from_str(ctx, str);
1885 map = isl_map_lexmin(map);
1886 str = "{ [x] -> [5] : 6 <= x <= 8; "
1887 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1888 map2 = isl_map_read_from_str(ctx, str);
1889 assert(isl_map_is_equal(map, map2));
1890 isl_map_free(map);
1891 isl_map_free(map2);
1893 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1894 map = isl_map_read_from_str(ctx, str);
1895 map2 = isl_map_copy(map);
1896 map = isl_map_lexmin(map);
1897 assert(isl_map_is_equal(map, map2));
1898 isl_map_free(map);
1899 isl_map_free(map2);
1901 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1902 map = isl_map_read_from_str(ctx, str);
1903 map = isl_map_lexmin(map);
1904 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1905 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1906 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1907 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1908 map2 = isl_map_read_from_str(ctx, str);
1909 assert(isl_map_is_equal(map, map2));
1910 isl_map_free(map);
1911 isl_map_free(map2);
1913 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1914 " 8i' <= i and 8i' >= -7 + i }";
1915 bmap = isl_basic_map_read_from_str(ctx, str);
1916 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1917 map2 = isl_map_from_pw_multi_aff(pma);
1918 map = isl_map_from_basic_map(bmap);
1919 assert(isl_map_is_equal(map, map2));
1920 isl_map_free(map);
1921 isl_map_free(map2);
1923 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1924 map = isl_map_read_from_str(ctx, str);
1925 map = isl_map_lexmin(map);
1926 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1927 map2 = isl_map_read_from_str(ctx, str);
1928 assert(isl_map_is_equal(map, map2));
1929 isl_map_free(map);
1930 isl_map_free(map2);
1932 /* Check that empty pieces are properly combined. */
1933 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1934 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1935 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1936 map = isl_map_read_from_str(ctx, str);
1937 map = isl_map_lexmin(map);
1938 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1939 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1940 "x >= 4 }";
1941 map2 = isl_map_read_from_str(ctx, str);
1942 assert(isl_map_is_equal(map, map2));
1943 isl_map_free(map);
1944 isl_map_free(map2);
1946 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1947 " 8i' <= i and 8i' >= -7 + i }";
1948 set = isl_set_read_from_str(ctx, str);
1949 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1950 set2 = isl_set_from_pw_multi_aff(pma);
1951 equal = isl_set_is_equal(set, set2);
1952 isl_set_free(set);
1953 isl_set_free(set2);
1954 if (equal < 0)
1955 return -1;
1956 if (!equal)
1957 isl_die(ctx, isl_error_unknown,
1958 "unexpected difference between set and "
1959 "piecewise affine expression", return -1);
1961 return 0;
1964 /* Check that isl_set_min_val and isl_set_max_val compute the correct
1965 * result on non-convex inputs.
1967 static int test_min(struct isl_ctx *ctx)
1969 isl_set *set;
1970 isl_aff *aff;
1971 isl_val *val;
1972 int min_ok, max_ok;
1974 set = isl_set_read_from_str(ctx, "{ [-1]; [1] }");
1975 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x] }");
1976 val = isl_set_min_val(set, aff);
1977 min_ok = isl_val_is_negone(val);
1978 isl_val_free(val);
1979 val = isl_set_max_val(set, aff);
1980 max_ok = isl_val_is_one(val);
1981 isl_val_free(val);
1982 isl_aff_free(aff);
1983 isl_set_free(set);
1985 if (min_ok < 0 || max_ok < 0)
1986 return -1;
1987 if (!min_ok)
1988 isl_die(ctx, isl_error_unknown,
1989 "unexpected minimum", return -1);
1990 if (!max_ok)
1991 isl_die(ctx, isl_error_unknown,
1992 "unexpected maximum", return -1);
1994 return 0;
1997 struct must_may {
1998 isl_map *must;
1999 isl_map *may;
2002 static int collect_must_may(__isl_take isl_map *dep, int must,
2003 void *dep_user, void *user)
2005 struct must_may *mm = (struct must_may *)user;
2007 if (must)
2008 mm->must = isl_map_union(mm->must, dep);
2009 else
2010 mm->may = isl_map_union(mm->may, dep);
2012 return 0;
2015 static int common_space(void *first, void *second)
2017 int depth = *(int *)first;
2018 return 2 * depth;
2021 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2023 isl_map *map2;
2024 int equal;
2026 if (!map)
2027 return -1;
2029 map2 = isl_map_read_from_str(map->ctx, str);
2030 equal = isl_map_is_equal(map, map2);
2031 isl_map_free(map2);
2033 return equal;
2036 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2038 int equal;
2040 equal = map_is_equal(map, str);
2041 if (equal < 0)
2042 return -1;
2043 if (!equal)
2044 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2045 "result not as expected", return -1);
2046 return 0;
2049 void test_dep(struct isl_ctx *ctx)
2051 const char *str;
2052 isl_space *dim;
2053 isl_map *map;
2054 isl_access_info *ai;
2055 isl_flow *flow;
2056 int depth;
2057 struct must_may mm;
2059 depth = 3;
2061 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2062 map = isl_map_read_from_str(ctx, str);
2063 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2065 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2066 map = isl_map_read_from_str(ctx, str);
2067 ai = isl_access_info_add_source(ai, map, 1, &depth);
2069 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2070 map = isl_map_read_from_str(ctx, str);
2071 ai = isl_access_info_add_source(ai, map, 1, &depth);
2073 flow = isl_access_info_compute_flow(ai);
2074 dim = isl_space_alloc(ctx, 0, 3, 3);
2075 mm.must = isl_map_empty(isl_space_copy(dim));
2076 mm.may = isl_map_empty(dim);
2078 isl_flow_foreach(flow, collect_must_may, &mm);
2080 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2081 " [1,10,0] -> [2,5,0] }";
2082 assert(map_is_equal(mm.must, str));
2083 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2084 assert(map_is_equal(mm.may, str));
2086 isl_map_free(mm.must);
2087 isl_map_free(mm.may);
2088 isl_flow_free(flow);
2091 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2092 map = isl_map_read_from_str(ctx, str);
2093 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2095 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2096 map = isl_map_read_from_str(ctx, str);
2097 ai = isl_access_info_add_source(ai, map, 1, &depth);
2099 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2100 map = isl_map_read_from_str(ctx, str);
2101 ai = isl_access_info_add_source(ai, map, 0, &depth);
2103 flow = isl_access_info_compute_flow(ai);
2104 dim = isl_space_alloc(ctx, 0, 3, 3);
2105 mm.must = isl_map_empty(isl_space_copy(dim));
2106 mm.may = isl_map_empty(dim);
2108 isl_flow_foreach(flow, collect_must_may, &mm);
2110 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2111 assert(map_is_equal(mm.must, str));
2112 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2113 assert(map_is_equal(mm.may, str));
2115 isl_map_free(mm.must);
2116 isl_map_free(mm.may);
2117 isl_flow_free(flow);
2120 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2121 map = isl_map_read_from_str(ctx, str);
2122 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2124 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2125 map = isl_map_read_from_str(ctx, str);
2126 ai = isl_access_info_add_source(ai, map, 0, &depth);
2128 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2129 map = isl_map_read_from_str(ctx, str);
2130 ai = isl_access_info_add_source(ai, map, 0, &depth);
2132 flow = isl_access_info_compute_flow(ai);
2133 dim = isl_space_alloc(ctx, 0, 3, 3);
2134 mm.must = isl_map_empty(isl_space_copy(dim));
2135 mm.may = isl_map_empty(dim);
2137 isl_flow_foreach(flow, collect_must_may, &mm);
2139 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2140 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2141 assert(map_is_equal(mm.may, str));
2142 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2143 assert(map_is_equal(mm.must, str));
2145 isl_map_free(mm.must);
2146 isl_map_free(mm.may);
2147 isl_flow_free(flow);
2150 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2151 map = isl_map_read_from_str(ctx, str);
2152 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2154 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2155 map = isl_map_read_from_str(ctx, str);
2156 ai = isl_access_info_add_source(ai, map, 0, &depth);
2158 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2159 map = isl_map_read_from_str(ctx, str);
2160 ai = isl_access_info_add_source(ai, map, 0, &depth);
2162 flow = isl_access_info_compute_flow(ai);
2163 dim = isl_space_alloc(ctx, 0, 3, 3);
2164 mm.must = isl_map_empty(isl_space_copy(dim));
2165 mm.may = isl_map_empty(dim);
2167 isl_flow_foreach(flow, collect_must_may, &mm);
2169 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2170 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2171 assert(map_is_equal(mm.may, str));
2172 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2173 assert(map_is_equal(mm.must, str));
2175 isl_map_free(mm.must);
2176 isl_map_free(mm.may);
2177 isl_flow_free(flow);
2180 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2181 map = isl_map_read_from_str(ctx, str);
2182 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2184 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2185 map = isl_map_read_from_str(ctx, str);
2186 ai = isl_access_info_add_source(ai, map, 0, &depth);
2188 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2189 map = isl_map_read_from_str(ctx, str);
2190 ai = isl_access_info_add_source(ai, map, 0, &depth);
2192 flow = isl_access_info_compute_flow(ai);
2193 dim = isl_space_alloc(ctx, 0, 3, 3);
2194 mm.must = isl_map_empty(isl_space_copy(dim));
2195 mm.may = isl_map_empty(dim);
2197 isl_flow_foreach(flow, collect_must_may, &mm);
2199 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2200 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2201 assert(map_is_equal(mm.may, str));
2202 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2203 assert(map_is_equal(mm.must, str));
2205 isl_map_free(mm.must);
2206 isl_map_free(mm.may);
2207 isl_flow_free(flow);
2210 depth = 5;
2212 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2213 map = isl_map_read_from_str(ctx, str);
2214 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2216 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2217 map = isl_map_read_from_str(ctx, str);
2218 ai = isl_access_info_add_source(ai, map, 1, &depth);
2220 flow = isl_access_info_compute_flow(ai);
2221 dim = isl_space_alloc(ctx, 0, 5, 5);
2222 mm.must = isl_map_empty(isl_space_copy(dim));
2223 mm.may = isl_map_empty(dim);
2225 isl_flow_foreach(flow, collect_must_may, &mm);
2227 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2228 assert(map_is_equal(mm.must, str));
2229 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2230 assert(map_is_equal(mm.may, str));
2232 isl_map_free(mm.must);
2233 isl_map_free(mm.may);
2234 isl_flow_free(flow);
2237 /* Check that the dependence analysis proceeds without errors.
2238 * Earlier versions of isl would break down during the analysis
2239 * due to the use of the wrong spaces.
2241 static int test_flow(isl_ctx *ctx)
2243 const char *str;
2244 isl_union_map *access, *schedule;
2245 isl_union_map *must_dep, *may_dep;
2246 int r;
2248 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2249 access = isl_union_map_read_from_str(ctx, str);
2250 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2251 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2252 "S2[] -> [1,0,0,0]; "
2253 "S3[] -> [-1,0,0,0] }";
2254 schedule = isl_union_map_read_from_str(ctx, str);
2255 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2256 isl_union_map_copy(access), schedule,
2257 &must_dep, &may_dep, NULL, NULL);
2258 isl_union_map_free(may_dep);
2259 isl_union_map_free(must_dep);
2261 return r;
2264 struct {
2265 const char *map;
2266 int sv;
2267 } sv_tests[] = {
2268 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2269 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2270 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2271 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2272 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2273 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2274 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2275 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2276 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2279 int test_sv(isl_ctx *ctx)
2281 isl_union_map *umap;
2282 int i;
2283 int sv;
2285 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2286 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2287 sv = isl_union_map_is_single_valued(umap);
2288 isl_union_map_free(umap);
2289 if (sv < 0)
2290 return -1;
2291 if (sv_tests[i].sv && !sv)
2292 isl_die(ctx, isl_error_internal,
2293 "map not detected as single valued", return -1);
2294 if (!sv_tests[i].sv && sv)
2295 isl_die(ctx, isl_error_internal,
2296 "map detected as single valued", return -1);
2299 return 0;
2302 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
2304 isl_map *map;
2306 map = isl_map_read_from_str(ctx, str);
2307 if (bijective)
2308 assert(isl_map_is_bijective(map));
2309 else
2310 assert(!isl_map_is_bijective(map));
2311 isl_map_free(map);
2314 void test_bijective(struct isl_ctx *ctx)
2316 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
2317 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
2318 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
2319 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
2320 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
2321 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
2322 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
2323 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
2324 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
2325 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
2326 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
2327 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
2328 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
2331 /* Inputs for isl_pw_qpolynomial_gist tests.
2332 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2334 struct {
2335 const char *pwqp;
2336 const char *set;
2337 const char *gist;
2338 } pwqp_gist_tests[] = {
2339 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2340 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2341 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2342 "{ [i] -> -1/2 + 1/2 * i }" },
2343 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2346 static int test_pwqp(struct isl_ctx *ctx)
2348 int i;
2349 const char *str;
2350 isl_set *set;
2351 isl_pw_qpolynomial *pwqp1, *pwqp2;
2352 int equal;
2354 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2355 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2357 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2358 isl_dim_in, 1, 1);
2360 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2361 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2363 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2365 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2367 isl_pw_qpolynomial_free(pwqp1);
2369 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2370 str = pwqp_gist_tests[i].pwqp;
2371 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2372 str = pwqp_gist_tests[i].set;
2373 set = isl_set_read_from_str(ctx, str);
2374 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2375 str = pwqp_gist_tests[i].gist;
2376 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2377 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2378 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2379 isl_pw_qpolynomial_free(pwqp1);
2381 if (equal < 0)
2382 return -1;
2383 if (!equal)
2384 isl_die(ctx, isl_error_unknown,
2385 "unexpected result", return -1);
2388 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2389 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2390 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2391 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2393 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2395 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2397 isl_pw_qpolynomial_free(pwqp1);
2399 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2400 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2401 str = "{ [x] -> x }";
2402 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2404 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2406 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2408 isl_pw_qpolynomial_free(pwqp1);
2410 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2411 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2412 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2413 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2414 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2415 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2416 isl_pw_qpolynomial_free(pwqp1);
2418 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2419 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2420 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2421 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2422 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2423 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2424 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2425 isl_pw_qpolynomial_free(pwqp1);
2426 isl_pw_qpolynomial_free(pwqp2);
2427 if (equal < 0)
2428 return -1;
2429 if (!equal)
2430 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2432 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2433 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2434 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2435 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2436 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2437 isl_val_one(ctx));
2438 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2439 isl_pw_qpolynomial_free(pwqp1);
2440 isl_pw_qpolynomial_free(pwqp2);
2441 if (equal < 0)
2442 return -1;
2443 if (!equal)
2444 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2446 return 0;
2449 void test_split_periods(isl_ctx *ctx)
2451 const char *str;
2452 isl_pw_qpolynomial *pwqp;
2454 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2455 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2456 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2457 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2459 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2460 assert(pwqp);
2462 isl_pw_qpolynomial_free(pwqp);
2465 void test_union(isl_ctx *ctx)
2467 const char *str;
2468 isl_union_set *uset1, *uset2;
2469 isl_union_map *umap1, *umap2;
2471 str = "{ [i] : 0 <= i <= 1 }";
2472 uset1 = isl_union_set_read_from_str(ctx, str);
2473 str = "{ [1] -> [0] }";
2474 umap1 = isl_union_map_read_from_str(ctx, str);
2476 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2477 assert(isl_union_map_is_equal(umap1, umap2));
2479 isl_union_map_free(umap1);
2480 isl_union_map_free(umap2);
2482 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2483 umap1 = isl_union_map_read_from_str(ctx, str);
2484 str = "{ A[i]; B[i] }";
2485 uset1 = isl_union_set_read_from_str(ctx, str);
2487 uset2 = isl_union_map_domain(umap1);
2489 assert(isl_union_set_is_equal(uset1, uset2));
2491 isl_union_set_free(uset1);
2492 isl_union_set_free(uset2);
2495 void test_bound(isl_ctx *ctx)
2497 const char *str;
2498 isl_pw_qpolynomial *pwqp;
2499 isl_pw_qpolynomial_fold *pwf;
2501 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2502 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2503 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2504 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
2505 isl_pw_qpolynomial_fold_free(pwf);
2507 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2508 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2509 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2510 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
2511 isl_pw_qpolynomial_fold_free(pwf);
2514 void test_lift(isl_ctx *ctx)
2516 const char *str;
2517 isl_basic_map *bmap;
2518 isl_basic_set *bset;
2520 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2521 bset = isl_basic_set_read_from_str(ctx, str);
2522 bset = isl_basic_set_lift(bset);
2523 bmap = isl_basic_map_from_range(bset);
2524 bset = isl_basic_map_domain(bmap);
2525 isl_basic_set_free(bset);
2528 struct {
2529 const char *set1;
2530 const char *set2;
2531 int subset;
2532 } subset_tests[] = {
2533 { "{ [112, 0] }",
2534 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2535 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2536 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2537 { "{ [65] }",
2538 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2539 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2540 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2541 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2542 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2543 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2544 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2545 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2546 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2547 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2548 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2549 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2550 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2551 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
2552 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
2553 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
2554 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
2555 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
2556 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
2557 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
2558 "4e0 <= -57 + i0 + i1)) or "
2559 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
2560 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
2561 "4e0 >= -61 + i0 + i1)) or "
2562 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
2565 static int test_subset(isl_ctx *ctx)
2567 int i;
2568 isl_set *set1, *set2;
2569 int subset;
2571 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2572 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2573 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2574 subset = isl_set_is_subset(set1, set2);
2575 isl_set_free(set1);
2576 isl_set_free(set2);
2577 if (subset < 0)
2578 return -1;
2579 if (subset != subset_tests[i].subset)
2580 isl_die(ctx, isl_error_unknown,
2581 "incorrect subset result", return -1);
2584 return 0;
2587 struct {
2588 const char *minuend;
2589 const char *subtrahend;
2590 const char *difference;
2591 } subtract_domain_tests[] = {
2592 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2593 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2594 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2597 static int test_subtract(isl_ctx *ctx)
2599 int i;
2600 isl_union_map *umap1, *umap2;
2601 isl_union_pw_multi_aff *upma1, *upma2;
2602 isl_union_set *uset;
2603 int equal;
2605 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2606 umap1 = isl_union_map_read_from_str(ctx,
2607 subtract_domain_tests[i].minuend);
2608 uset = isl_union_set_read_from_str(ctx,
2609 subtract_domain_tests[i].subtrahend);
2610 umap2 = isl_union_map_read_from_str(ctx,
2611 subtract_domain_tests[i].difference);
2612 umap1 = isl_union_map_subtract_domain(umap1, uset);
2613 equal = isl_union_map_is_equal(umap1, umap2);
2614 isl_union_map_free(umap1);
2615 isl_union_map_free(umap2);
2616 if (equal < 0)
2617 return -1;
2618 if (!equal)
2619 isl_die(ctx, isl_error_unknown,
2620 "incorrect subtract domain result", return -1);
2623 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2624 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
2625 subtract_domain_tests[i].minuend);
2626 uset = isl_union_set_read_from_str(ctx,
2627 subtract_domain_tests[i].subtrahend);
2628 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
2629 subtract_domain_tests[i].difference);
2630 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
2631 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
2632 isl_union_pw_multi_aff_free(upma1);
2633 isl_union_pw_multi_aff_free(upma2);
2634 if (equal < 0)
2635 return -1;
2636 if (!equal)
2637 isl_die(ctx, isl_error_unknown,
2638 "incorrect subtract domain result", return -1);
2641 return 0;
2644 int test_factorize(isl_ctx *ctx)
2646 const char *str;
2647 isl_basic_set *bset;
2648 isl_factorizer *f;
2650 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2651 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2652 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2653 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2654 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2655 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2656 "3i5 >= -2i0 - i2 + 3i4 }";
2657 bset = isl_basic_set_read_from_str(ctx, str);
2658 f = isl_basic_set_factorizer(bset);
2659 isl_basic_set_free(bset);
2660 isl_factorizer_free(f);
2661 if (!f)
2662 isl_die(ctx, isl_error_unknown,
2663 "failed to construct factorizer", return -1);
2665 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2666 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2667 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2668 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2669 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2670 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2671 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2672 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2673 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2674 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2675 bset = isl_basic_set_read_from_str(ctx, str);
2676 f = isl_basic_set_factorizer(bset);
2677 isl_basic_set_free(bset);
2678 isl_factorizer_free(f);
2679 if (!f)
2680 isl_die(ctx, isl_error_unknown,
2681 "failed to construct factorizer", return -1);
2683 return 0;
2686 static int check_injective(__isl_take isl_map *map, void *user)
2688 int *injective = user;
2690 *injective = isl_map_is_injective(map);
2691 isl_map_free(map);
2693 if (*injective < 0 || !*injective)
2694 return -1;
2696 return 0;
2699 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2700 const char *r, const char *s, int tilable, int parallel)
2702 int i;
2703 isl_union_set *D;
2704 isl_union_map *W, *R, *S;
2705 isl_union_map *empty;
2706 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2707 isl_union_map *validity, *proximity, *coincidence;
2708 isl_union_map *schedule;
2709 isl_union_map *test;
2710 isl_union_set *delta;
2711 isl_union_set *domain;
2712 isl_set *delta_set;
2713 isl_set *slice;
2714 isl_set *origin;
2715 isl_schedule_constraints *sc;
2716 isl_schedule *sched;
2717 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2719 D = isl_union_set_read_from_str(ctx, d);
2720 W = isl_union_map_read_from_str(ctx, w);
2721 R = isl_union_map_read_from_str(ctx, r);
2722 S = isl_union_map_read_from_str(ctx, s);
2724 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2725 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2727 empty = isl_union_map_empty(isl_union_map_get_space(S));
2728 isl_union_map_compute_flow(isl_union_map_copy(R),
2729 isl_union_map_copy(W), empty,
2730 isl_union_map_copy(S),
2731 &dep_raw, NULL, NULL, NULL);
2732 isl_union_map_compute_flow(isl_union_map_copy(W),
2733 isl_union_map_copy(W),
2734 isl_union_map_copy(R),
2735 isl_union_map_copy(S),
2736 &dep_waw, &dep_war, NULL, NULL);
2738 dep = isl_union_map_union(dep_waw, dep_war);
2739 dep = isl_union_map_union(dep, dep_raw);
2740 validity = isl_union_map_copy(dep);
2741 coincidence = isl_union_map_copy(dep);
2742 proximity = isl_union_map_copy(dep);
2744 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
2745 sc = isl_schedule_constraints_set_validity(sc, validity);
2746 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
2747 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2748 sched = isl_schedule_constraints_compute_schedule(sc);
2749 schedule = isl_schedule_get_map(sched);
2750 isl_schedule_free(sched);
2751 isl_union_map_free(W);
2752 isl_union_map_free(R);
2753 isl_union_map_free(S);
2755 is_injection = 1;
2756 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2758 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2759 is_complete = isl_union_set_is_subset(D, domain);
2760 isl_union_set_free(D);
2761 isl_union_set_free(domain);
2763 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2764 test = isl_union_map_apply_range(test, dep);
2765 test = isl_union_map_apply_range(test, schedule);
2767 delta = isl_union_map_deltas(test);
2768 if (isl_union_set_n_set(delta) == 0) {
2769 is_tilable = 1;
2770 is_parallel = 1;
2771 is_nonneg = 1;
2772 isl_union_set_free(delta);
2773 } else {
2774 delta_set = isl_set_from_union_set(delta);
2776 slice = isl_set_universe(isl_set_get_space(delta_set));
2777 for (i = 0; i < tilable; ++i)
2778 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2779 is_tilable = isl_set_is_subset(delta_set, slice);
2780 isl_set_free(slice);
2782 slice = isl_set_universe(isl_set_get_space(delta_set));
2783 for (i = 0; i < parallel; ++i)
2784 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2785 is_parallel = isl_set_is_subset(delta_set, slice);
2786 isl_set_free(slice);
2788 origin = isl_set_universe(isl_set_get_space(delta_set));
2789 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2790 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2792 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2793 delta_set = isl_set_lexmin(delta_set);
2795 is_nonneg = isl_set_is_equal(delta_set, origin);
2797 isl_set_free(origin);
2798 isl_set_free(delta_set);
2801 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2802 is_injection < 0 || is_complete < 0)
2803 return -1;
2804 if (!is_complete)
2805 isl_die(ctx, isl_error_unknown,
2806 "generated schedule incomplete", return -1);
2807 if (!is_injection)
2808 isl_die(ctx, isl_error_unknown,
2809 "generated schedule not injective on each statement",
2810 return -1);
2811 if (!is_nonneg)
2812 isl_die(ctx, isl_error_unknown,
2813 "negative dependences in generated schedule",
2814 return -1);
2815 if (!is_tilable)
2816 isl_die(ctx, isl_error_unknown,
2817 "generated schedule not as tilable as expected",
2818 return -1);
2819 if (!is_parallel)
2820 isl_die(ctx, isl_error_unknown,
2821 "generated schedule not as parallel as expected",
2822 return -1);
2824 return 0;
2827 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2828 const char *domain, const char *validity, const char *proximity)
2830 isl_union_set *dom;
2831 isl_union_map *dep;
2832 isl_union_map *prox;
2833 isl_schedule_constraints *sc;
2834 isl_schedule *schedule;
2835 isl_union_map *sched;
2837 dom = isl_union_set_read_from_str(ctx, domain);
2838 dep = isl_union_map_read_from_str(ctx, validity);
2839 prox = isl_union_map_read_from_str(ctx, proximity);
2840 sc = isl_schedule_constraints_on_domain(dom);
2841 sc = isl_schedule_constraints_set_validity(sc, dep);
2842 sc = isl_schedule_constraints_set_proximity(sc, prox);
2843 schedule = isl_schedule_constraints_compute_schedule(sc);
2844 sched = isl_schedule_get_map(schedule);
2845 isl_schedule_free(schedule);
2847 return sched;
2850 /* Check that a schedule can be constructed on the given domain
2851 * with the given validity and proximity constraints.
2853 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2854 const char *validity, const char *proximity)
2856 isl_union_map *sched;
2858 sched = compute_schedule(ctx, domain, validity, proximity);
2859 if (!sched)
2860 return -1;
2862 isl_union_map_free(sched);
2863 return 0;
2866 int test_special_schedule(isl_ctx *ctx, const char *domain,
2867 const char *validity, const char *proximity, const char *expected_sched)
2869 isl_union_map *sched1, *sched2;
2870 int equal;
2872 sched1 = compute_schedule(ctx, domain, validity, proximity);
2873 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2875 equal = isl_union_map_is_equal(sched1, sched2);
2876 isl_union_map_free(sched1);
2877 isl_union_map_free(sched2);
2879 if (equal < 0)
2880 return -1;
2881 if (!equal)
2882 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2883 return -1);
2885 return 0;
2888 /* Check that the schedule map is properly padded, even after being
2889 * reconstructed from the band forest.
2891 static int test_padded_schedule(isl_ctx *ctx)
2893 const char *str;
2894 isl_union_set *D;
2895 isl_union_map *validity, *proximity;
2896 isl_schedule_constraints *sc;
2897 isl_schedule *sched;
2898 isl_union_map *map1, *map2;
2899 isl_band_list *list;
2900 int equal;
2902 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2903 D = isl_union_set_read_from_str(ctx, str);
2904 validity = isl_union_map_empty(isl_union_set_get_space(D));
2905 proximity = isl_union_map_copy(validity);
2906 sc = isl_schedule_constraints_on_domain(D);
2907 sc = isl_schedule_constraints_set_validity(sc, validity);
2908 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2909 sched = isl_schedule_constraints_compute_schedule(sc);
2910 map1 = isl_schedule_get_map(sched);
2911 list = isl_schedule_get_band_forest(sched);
2912 isl_band_list_free(list);
2913 map2 = isl_schedule_get_map(sched);
2914 isl_schedule_free(sched);
2915 equal = isl_union_map_is_equal(map1, map2);
2916 isl_union_map_free(map1);
2917 isl_union_map_free(map2);
2919 if (equal < 0)
2920 return -1;
2921 if (!equal)
2922 isl_die(ctx, isl_error_unknown,
2923 "reconstructed schedule map not the same as original",
2924 return -1);
2926 return 0;
2929 /* Check that conditional validity constraints are also taken into
2930 * account across bands.
2931 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
2932 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
2933 * and then check that the adjacent order constraint C[2,1]->D[2,0]
2934 * is enforced by the rest of the schedule.
2936 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
2938 const char *str;
2939 isl_union_set *domain;
2940 isl_union_map *validity, *proximity, *condition;
2941 isl_union_map *sink, *source, *dep;
2942 isl_schedule_constraints *sc;
2943 isl_schedule *schedule;
2944 isl_union_access_info *access;
2945 isl_union_flow *flow;
2946 int empty;
2948 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
2949 "A[k] : k >= 1 and k <= -1 + n; "
2950 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
2951 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
2952 domain = isl_union_set_read_from_str(ctx, str);
2953 sc = isl_schedule_constraints_on_domain(domain);
2954 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
2955 "k <= -2 + n and i >= 1 and i <= -1 + k; "
2956 "D[k, i] -> C[1 + k, i] : "
2957 "k <= -2 + n and i >= 1 and i <= -1 + k; "
2958 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
2959 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
2960 validity = isl_union_map_read_from_str(ctx, str);
2961 sc = isl_schedule_constraints_set_validity(sc, validity);
2962 str = "[n] -> { C[k, i] -> D[k, i] : "
2963 "0 <= i <= -1 + k and k <= -1 + n }";
2964 proximity = isl_union_map_read_from_str(ctx, str);
2965 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2966 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
2967 "i <= -1 + k and i >= 1 and k <= -2 + n; "
2968 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
2969 "k <= -1 + n and i >= 0 and i <= -2 + k }";
2970 condition = isl_union_map_read_from_str(ctx, str);
2971 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
2972 "i >= 0 and i <= -1 + k and k <= -1 + n; "
2973 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
2974 "i >= 0 and i <= -1 + k and k <= -1 + n and "
2975 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
2976 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
2977 "i >= 0 and i <= -1 + k and k <= -1 + n; "
2978 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
2979 "k <= -1 + n and i >= 0 and i <= -1 + k and "
2980 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
2981 validity = isl_union_map_read_from_str(ctx, str);
2982 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
2983 validity);
2984 schedule = isl_schedule_constraints_compute_schedule(sc);
2985 str = "{ D[2,0] -> [] }";
2986 sink = isl_union_map_read_from_str(ctx, str);
2987 access = isl_union_access_info_from_sink(sink);
2988 str = "{ C[2,1] -> [] }";
2989 source = isl_union_map_read_from_str(ctx, str);
2990 access = isl_union_access_info_set_must_source(access, source);
2991 access = isl_union_access_info_set_schedule(access, schedule);
2992 flow = isl_union_access_info_compute_flow(access);
2993 dep = isl_union_flow_get_must_dependence(flow);
2994 isl_union_flow_free(flow);
2995 empty = isl_union_map_is_empty(dep);
2996 isl_union_map_free(dep);
2998 if (empty < 0)
2999 return -1;
3000 if (empty)
3001 isl_die(ctx, isl_error_unknown,
3002 "conditional validity not respected", return -1);
3004 return 0;
3007 /* Input for testing of schedule construction based on
3008 * conditional constraints.
3010 * domain is the iteration domain
3011 * flow are the flow dependences, which determine the validity and
3012 * proximity constraints
3013 * condition are the conditions on the conditional validity constraints
3014 * conditional_validity are the conditional validity constraints
3015 * outer_band_n is the expected number of members in the outer band
3017 struct {
3018 const char *domain;
3019 const char *flow;
3020 const char *condition;
3021 const char *conditional_validity;
3022 int outer_band_n;
3023 } live_range_tests[] = {
3024 /* Contrived example that illustrates that we need to keep
3025 * track of tagged condition dependences and
3026 * tagged conditional validity dependences
3027 * in isl_sched_edge separately.
3028 * In particular, the conditional validity constraints on A
3029 * cannot be satisfied,
3030 * but they can be ignored because there are no corresponding
3031 * condition constraints. However, we do have an additional
3032 * conditional validity constraint that maps to the same
3033 * dependence relation
3034 * as the condition constraint on B. If we did not make a distinction
3035 * between tagged condition and tagged conditional validity
3036 * dependences, then we
3037 * could end up treating this shared dependence as an condition
3038 * constraint on A, forcing a localization of the conditions,
3039 * which is impossible.
3041 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
3042 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
3043 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
3044 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3045 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
3046 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
3049 /* TACO 2013 Fig. 7 */
3050 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3051 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3052 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3053 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
3054 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
3055 "0 <= i < n and 0 <= j < n - 1 }",
3056 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
3057 "0 <= i < n and 0 <= j < j' < n;"
3058 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
3059 "0 <= i < i' < n and 0 <= j,j' < n;"
3060 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
3061 "0 <= i,j,j' < n and j < j' }",
3064 /* TACO 2013 Fig. 7, without tags */
3065 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
3066 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3067 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3068 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
3069 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
3070 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
3071 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
3072 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
3075 /* TACO 2013 Fig. 12 */
3076 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
3077 "S3[i,3] : 0 <= i <= 1 }",
3078 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
3079 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
3080 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
3081 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
3082 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3083 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
3084 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3085 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
3086 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
3087 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
3088 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
3093 /* Test schedule construction based on conditional constraints.
3094 * In particular, check the number of members in the outer band node
3095 * as an indication of whether tiling is possible or not.
3097 static int test_conditional_schedule_constraints(isl_ctx *ctx)
3099 int i;
3100 isl_union_set *domain;
3101 isl_union_map *condition;
3102 isl_union_map *flow;
3103 isl_union_map *validity;
3104 isl_schedule_constraints *sc;
3105 isl_schedule *schedule;
3106 isl_schedule_node *node;
3107 int n_member;
3109 if (test_special_conditional_schedule_constraints(ctx) < 0)
3110 return -1;
3112 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
3113 domain = isl_union_set_read_from_str(ctx,
3114 live_range_tests[i].domain);
3115 flow = isl_union_map_read_from_str(ctx,
3116 live_range_tests[i].flow);
3117 condition = isl_union_map_read_from_str(ctx,
3118 live_range_tests[i].condition);
3119 validity = isl_union_map_read_from_str(ctx,
3120 live_range_tests[i].conditional_validity);
3121 sc = isl_schedule_constraints_on_domain(domain);
3122 sc = isl_schedule_constraints_set_validity(sc,
3123 isl_union_map_copy(flow));
3124 sc = isl_schedule_constraints_set_proximity(sc, flow);
3125 sc = isl_schedule_constraints_set_conditional_validity(sc,
3126 condition, validity);
3127 schedule = isl_schedule_constraints_compute_schedule(sc);
3128 node = isl_schedule_get_root(schedule);
3129 while (node &&
3130 isl_schedule_node_get_type(node) != isl_schedule_node_band)
3131 node = isl_schedule_node_first_child(node);
3132 n_member = isl_schedule_node_band_n_member(node);
3133 isl_schedule_node_free(node);
3134 isl_schedule_free(schedule);
3136 if (!schedule)
3137 return -1;
3138 if (n_member != live_range_tests[i].outer_band_n)
3139 isl_die(ctx, isl_error_unknown,
3140 "unexpected number of members in outer band",
3141 return -1);
3143 return 0;
3146 int test_schedule(isl_ctx *ctx)
3148 const char *D, *W, *R, *V, *P, *S;
3150 /* Handle resulting schedule with zero bands. */
3151 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
3152 return -1;
3154 /* Jacobi */
3155 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
3156 W = "{ S1[t,i] -> a[t,i] }";
3157 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
3158 "S1[t,i] -> a[t-1,i+1] }";
3159 S = "{ S1[t,i] -> [t,i] }";
3160 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3161 return -1;
3163 /* Fig. 5 of CC2008 */
3164 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
3165 "j <= -1 + N }";
3166 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
3167 "j >= 2 and j <= -1 + N }";
3168 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
3169 "j >= 2 and j <= -1 + N; "
3170 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
3171 "j >= 2 and j <= -1 + N }";
3172 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3173 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3174 return -1;
3176 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
3177 W = "{ S1[i] -> a[i] }";
3178 R = "{ S2[i] -> a[i+1] }";
3179 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3180 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3181 return -1;
3183 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
3184 W = "{ S1[i] -> a[i] }";
3185 R = "{ S2[i] -> a[9-i] }";
3186 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3187 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3188 return -1;
3190 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
3191 W = "{ S1[i] -> a[i] }";
3192 R = "[N] -> { S2[i] -> a[N-1-i] }";
3193 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3194 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3195 return -1;
3197 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
3198 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
3199 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
3200 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
3201 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3202 return -1;
3204 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3205 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
3206 R = "{ S2[i,j] -> a[i-1,j] }";
3207 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3208 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3209 return -1;
3211 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3212 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
3213 R = "{ S2[i,j] -> a[i,j-1] }";
3214 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3215 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3216 return -1;
3218 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
3219 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
3220 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
3221 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
3222 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
3223 "S_0[] -> [0, 0, 0] }";
3224 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
3225 return -1;
3226 ctx->opt->schedule_parametric = 0;
3227 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3228 return -1;
3229 ctx->opt->schedule_parametric = 1;
3231 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
3232 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
3233 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
3234 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
3235 "S4[i] -> a[i,N] }";
3236 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
3237 "S4[i] -> [4,i,0] }";
3238 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3239 return -1;
3241 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
3242 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3243 "j <= N }";
3244 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3245 "j <= N; "
3246 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
3247 "j <= N }";
3248 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3249 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3250 return -1;
3252 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
3253 " S_2[t] : t >= 0 and t <= -1 + N; "
3254 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
3255 "i <= -1 + N }";
3256 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
3257 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
3258 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
3259 "i >= 0 and i <= -1 + N }";
3260 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
3261 "i >= 0 and i <= -1 + N; "
3262 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
3263 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
3264 " S_0[t] -> [0, t, 0] }";
3266 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3267 return -1;
3268 ctx->opt->schedule_parametric = 0;
3269 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3270 return -1;
3271 ctx->opt->schedule_parametric = 1;
3273 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
3274 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
3275 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
3276 return -1;
3278 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
3279 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
3280 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
3281 "j >= 0 and j <= -1 + N; "
3282 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3283 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
3284 "j >= 0 and j <= -1 + N; "
3285 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3286 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
3287 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3288 return -1;
3290 D = "{ S_0[i] : i >= 0 }";
3291 W = "{ S_0[i] -> a[i] : i >= 0 }";
3292 R = "{ S_0[i] -> a[0] : i >= 0 }";
3293 S = "{ S_0[i] -> [0, i, 0] }";
3294 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3295 return -1;
3297 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
3298 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
3299 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
3300 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
3301 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3302 return -1;
3304 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
3305 "k <= -1 + n and k >= 0 }";
3306 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
3307 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
3308 "k <= -1 + n and k >= 0; "
3309 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
3310 "k <= -1 + n and k >= 0; "
3311 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
3312 "k <= -1 + n and k >= 0 }";
3313 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
3314 ctx->opt->schedule_outer_coincidence = 1;
3315 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3316 return -1;
3317 ctx->opt->schedule_outer_coincidence = 0;
3319 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3320 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3321 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3322 "Stmt_for_body24[i0, i1, 1, 0]:"
3323 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3324 "Stmt_for_body7[i0, i1, i2]:"
3325 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3326 "i2 <= 7 }";
3328 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3329 "Stmt_for_body24[1, i1, i2, i3]:"
3330 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3331 "i2 >= 1;"
3332 "Stmt_for_body24[0, i1, i2, i3] -> "
3333 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3334 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3335 "i3 >= 0;"
3336 "Stmt_for_body24[0, i1, i2, i3] ->"
3337 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3338 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3339 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3340 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3341 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3342 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3343 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3344 "i1 <= 6 and i1 >= 0;"
3345 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3346 "Stmt_for_body7[i0, i1, i2] -> "
3347 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3348 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3349 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3350 "Stmt_for_body7[i0, i1, i2] -> "
3351 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3352 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3353 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3354 P = V;
3355 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3356 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3357 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3359 if (test_special_schedule(ctx, D, V, P, S) < 0)
3360 return -1;
3362 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3363 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3364 "j >= 1 and j <= 7;"
3365 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3366 "j >= 1 and j <= 8 }";
3367 P = "{ }";
3368 S = "{ S_0[i, j] -> [i + j, j] }";
3369 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3370 if (test_special_schedule(ctx, D, V, P, S) < 0)
3371 return -1;
3372 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3374 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3375 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3376 "j >= 0 and j <= -1 + i }";
3377 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3378 "i <= -1 + N and j >= 0;"
3379 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3380 "i <= -2 + N }";
3381 P = "{ }";
3382 S = "{ S_0[i, j] -> [i, j] }";
3383 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3384 if (test_special_schedule(ctx, D, V, P, S) < 0)
3385 return -1;
3386 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3388 /* Test both algorithms on a case with only proximity dependences. */
3389 D = "{ S[i,j] : 0 <= i <= 10 }";
3390 V = "{ }";
3391 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3392 S = "{ S[i, j] -> [j, i] }";
3393 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3394 if (test_special_schedule(ctx, D, V, P, S) < 0)
3395 return -1;
3396 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3397 if (test_special_schedule(ctx, D, V, P, S) < 0)
3398 return -1;
3400 D = "{ A[a]; B[] }";
3401 V = "{}";
3402 P = "{ A[a] -> B[] }";
3403 if (test_has_schedule(ctx, D, V, P) < 0)
3404 return -1;
3406 if (test_padded_schedule(ctx) < 0)
3407 return -1;
3409 /* Check that check for progress is not confused by rational
3410 * solution.
3412 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3413 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3414 "i0 <= -2 + N; "
3415 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3416 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3417 P = "{}";
3418 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3419 if (test_has_schedule(ctx, D, V, P) < 0)
3420 return -1;
3421 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3423 /* Check that we allow schedule rows that are only non-trivial
3424 * on some full-dimensional domains.
3426 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
3427 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
3428 "S1[j] -> S2[1] : 0 <= j <= 1 }";
3429 P = "{}";
3430 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3431 if (test_has_schedule(ctx, D, V, P) < 0)
3432 return -1;
3433 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3435 if (test_conditional_schedule_constraints(ctx) < 0)
3436 return -1;
3438 return 0;
3441 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3443 isl_union_map *umap;
3444 int test;
3446 umap = isl_union_map_read_from_str(ctx, str);
3447 test = isl_union_map_plain_is_injective(umap);
3448 isl_union_map_free(umap);
3449 if (test < 0)
3450 return -1;
3451 if (test == injective)
3452 return 0;
3453 if (injective)
3454 isl_die(ctx, isl_error_unknown,
3455 "map not detected as injective", return -1);
3456 else
3457 isl_die(ctx, isl_error_unknown,
3458 "map detected as injective", return -1);
3461 int test_injective(isl_ctx *ctx)
3463 const char *str;
3465 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
3466 return -1;
3467 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
3468 return -1;
3469 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
3470 return -1;
3471 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
3472 return -1;
3473 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
3474 return -1;
3475 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
3476 return -1;
3477 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
3478 return -1;
3479 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
3480 return -1;
3482 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
3483 if (test_plain_injective(ctx, str, 1))
3484 return -1;
3485 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
3486 if (test_plain_injective(ctx, str, 0))
3487 return -1;
3489 return 0;
3492 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
3494 isl_aff *aff2;
3495 int equal;
3497 if (!aff)
3498 return -1;
3500 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
3501 equal = isl_aff_plain_is_equal(aff, aff2);
3502 isl_aff_free(aff2);
3504 return equal;
3507 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
3509 int equal;
3511 equal = aff_plain_is_equal(aff, str);
3512 if (equal < 0)
3513 return -1;
3514 if (!equal)
3515 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
3516 "result not as expected", return -1);
3517 return 0;
3520 struct {
3521 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3522 __isl_take isl_aff *aff2);
3523 } aff_bin_op[] = {
3524 ['+'] = { &isl_aff_add },
3525 ['-'] = { &isl_aff_sub },
3526 ['*'] = { &isl_aff_mul },
3527 ['/'] = { &isl_aff_div },
3530 struct {
3531 const char *arg1;
3532 unsigned char op;
3533 const char *arg2;
3534 const char *res;
3535 } aff_bin_tests[] = {
3536 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
3537 "{ [i] -> [2i] }" },
3538 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
3539 "{ [i] -> [0] }" },
3540 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
3541 "{ [i] -> [2i] }" },
3542 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
3543 "{ [i] -> [2i] }" },
3544 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
3545 "{ [i] -> [i/2] }" },
3546 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
3547 "{ [i] -> [i] }" },
3548 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
3549 "{ [i] -> [NaN] }" },
3550 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
3551 "{ [i] -> [NaN] }" },
3552 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
3553 "{ [i] -> [NaN] }" },
3554 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
3555 "{ [i] -> [NaN] }" },
3556 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
3557 "{ [i] -> [NaN] }" },
3558 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
3559 "{ [i] -> [NaN] }" },
3560 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
3561 "{ [i] -> [NaN] }" },
3562 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
3563 "{ [i] -> [NaN] }" },
3564 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
3565 "{ [i] -> [NaN] }" },
3566 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
3567 "{ [i] -> [NaN] }" },
3568 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
3569 "{ [i] -> [NaN] }" },
3570 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
3571 "{ [i] -> [NaN] }" },
3574 /* Perform some basic tests of binary operations on isl_aff objects.
3576 static int test_bin_aff(isl_ctx *ctx)
3578 int i;
3579 isl_aff *aff1, *aff2, *res;
3580 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3581 __isl_take isl_aff *aff2);
3582 int ok;
3584 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
3585 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
3586 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
3587 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
3588 fn = aff_bin_op[aff_bin_tests[i].op].fn;
3589 aff1 = fn(aff1, aff2);
3590 if (isl_aff_is_nan(res))
3591 ok = isl_aff_is_nan(aff1);
3592 else
3593 ok = isl_aff_plain_is_equal(aff1, res);
3594 isl_aff_free(aff1);
3595 isl_aff_free(res);
3596 if (ok < 0)
3597 return -1;
3598 if (!ok)
3599 isl_die(ctx, isl_error_unknown,
3600 "unexpected result", return -1);
3603 return 0;
3606 struct {
3607 __isl_give isl_union_pw_multi_aff *(*fn)(
3608 __isl_take isl_union_pw_multi_aff *upma1,
3609 __isl_take isl_union_pw_multi_aff *upma2);
3610 const char *arg1;
3611 const char *arg2;
3612 const char *res;
3613 } upma_bin_tests[] = {
3614 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
3615 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
3616 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
3617 "{ B[x] -> [2] : x >= 0 }",
3618 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
3621 /* Perform some basic tests of binary operations on
3622 * isl_union_pw_multi_aff objects.
3624 static int test_bin_upma(isl_ctx *ctx)
3626 int i;
3627 isl_union_pw_multi_aff *upma1, *upma2, *res;
3628 int ok;
3630 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
3631 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3632 upma_bin_tests[i].arg1);
3633 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3634 upma_bin_tests[i].arg2);
3635 res = isl_union_pw_multi_aff_read_from_str(ctx,
3636 upma_bin_tests[i].res);
3637 upma1 = upma_bin_tests[i].fn(upma1, upma2);
3638 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
3639 isl_union_pw_multi_aff_free(upma1);
3640 isl_union_pw_multi_aff_free(res);
3641 if (ok < 0)
3642 return -1;
3643 if (!ok)
3644 isl_die(ctx, isl_error_unknown,
3645 "unexpected result", return -1);
3648 return 0;
3651 int test_aff(isl_ctx *ctx)
3653 const char *str;
3654 isl_set *set;
3655 isl_space *space;
3656 isl_local_space *ls;
3657 isl_aff *aff;
3658 int zero, equal;
3660 if (test_bin_aff(ctx) < 0)
3661 return -1;
3662 if (test_bin_upma(ctx) < 0)
3663 return -1;
3665 space = isl_space_set_alloc(ctx, 0, 1);
3666 ls = isl_local_space_from_space(space);
3667 aff = isl_aff_zero_on_domain(ls);
3669 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3670 aff = isl_aff_scale_down_ui(aff, 3);
3671 aff = isl_aff_floor(aff);
3672 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3673 aff = isl_aff_scale_down_ui(aff, 2);
3674 aff = isl_aff_floor(aff);
3675 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3677 str = "{ [10] }";
3678 set = isl_set_read_from_str(ctx, str);
3679 aff = isl_aff_gist(aff, set);
3681 aff = isl_aff_add_constant_si(aff, -16);
3682 zero = isl_aff_plain_is_zero(aff);
3683 isl_aff_free(aff);
3685 if (zero < 0)
3686 return -1;
3687 if (!zero)
3688 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3690 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
3691 aff = isl_aff_scale_down_ui(aff, 64);
3692 aff = isl_aff_floor(aff);
3693 equal = aff_check_plain_equal(aff, "{ [-1] }");
3694 isl_aff_free(aff);
3695 if (equal < 0)
3696 return -1;
3698 return 0;
3701 int test_dim_max(isl_ctx *ctx)
3703 int equal;
3704 const char *str;
3705 isl_set *set1, *set2;
3706 isl_set *set;
3707 isl_map *map;
3708 isl_pw_aff *pwaff;
3710 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
3711 set = isl_set_read_from_str(ctx, str);
3712 pwaff = isl_set_dim_max(set, 0);
3713 set1 = isl_set_from_pw_aff(pwaff);
3714 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
3715 set2 = isl_set_read_from_str(ctx, str);
3716 equal = isl_set_is_equal(set1, set2);
3717 isl_set_free(set1);
3718 isl_set_free(set2);
3719 if (equal < 0)
3720 return -1;
3721 if (!equal)
3722 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3724 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
3725 set = isl_set_read_from_str(ctx, str);
3726 pwaff = isl_set_dim_max(set, 0);
3727 set1 = isl_set_from_pw_aff(pwaff);
3728 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3729 set2 = isl_set_read_from_str(ctx, str);
3730 equal = isl_set_is_equal(set1, set2);
3731 isl_set_free(set1);
3732 isl_set_free(set2);
3733 if (equal < 0)
3734 return -1;
3735 if (!equal)
3736 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3738 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
3739 set = isl_set_read_from_str(ctx, str);
3740 pwaff = isl_set_dim_max(set, 0);
3741 set1 = isl_set_from_pw_aff(pwaff);
3742 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3743 set2 = isl_set_read_from_str(ctx, str);
3744 equal = isl_set_is_equal(set1, set2);
3745 isl_set_free(set1);
3746 isl_set_free(set2);
3747 if (equal < 0)
3748 return -1;
3749 if (!equal)
3750 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3752 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
3753 "0 <= i < N and 0 <= j < M }";
3754 map = isl_map_read_from_str(ctx, str);
3755 set = isl_map_range(map);
3757 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
3758 set1 = isl_set_from_pw_aff(pwaff);
3759 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
3760 set2 = isl_set_read_from_str(ctx, str);
3761 equal = isl_set_is_equal(set1, set2);
3762 isl_set_free(set1);
3763 isl_set_free(set2);
3765 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3766 set1 = isl_set_from_pw_aff(pwaff);
3767 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3768 set2 = isl_set_read_from_str(ctx, str);
3769 if (equal >= 0 && equal)
3770 equal = isl_set_is_equal(set1, set2);
3771 isl_set_free(set1);
3772 isl_set_free(set2);
3774 isl_set_free(set);
3776 if (equal < 0)
3777 return -1;
3778 if (!equal)
3779 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3781 /* Check that solutions are properly merged. */
3782 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3783 "c <= -1 + n - 4a - 2b and c >= -2b and "
3784 "4a >= -4 + n and c >= 0 }";
3785 set = isl_set_read_from_str(ctx, str);
3786 pwaff = isl_set_dim_min(set, 2);
3787 set1 = isl_set_from_pw_aff(pwaff);
3788 str = "[n] -> { [(0)] : n >= 1 }";
3789 set2 = isl_set_read_from_str(ctx, str);
3790 equal = isl_set_is_equal(set1, set2);
3791 isl_set_free(set1);
3792 isl_set_free(set2);
3794 if (equal < 0)
3795 return -1;
3796 if (!equal)
3797 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3799 /* Check that empty solution lie in the right space. */
3800 str = "[n] -> { [t,a] : 1 = 0 }";
3801 set = isl_set_read_from_str(ctx, str);
3802 pwaff = isl_set_dim_max(set, 0);
3803 set1 = isl_set_from_pw_aff(pwaff);
3804 str = "[n] -> { [t] : 1 = 0 }";
3805 set2 = isl_set_read_from_str(ctx, str);
3806 equal = isl_set_is_equal(set1, set2);
3807 isl_set_free(set1);
3808 isl_set_free(set2);
3810 if (equal < 0)
3811 return -1;
3812 if (!equal)
3813 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3815 return 0;
3818 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
3820 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
3821 const char *str)
3823 isl_ctx *ctx;
3824 isl_pw_multi_aff *pma2;
3825 int equal;
3827 if (!pma)
3828 return -1;
3830 ctx = isl_pw_multi_aff_get_ctx(pma);
3831 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3832 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
3833 isl_pw_multi_aff_free(pma2);
3835 return equal;
3838 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
3839 * represented by "str".
3841 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
3842 const char *str)
3844 int equal;
3846 equal = pw_multi_aff_plain_is_equal(pma, str);
3847 if (equal < 0)
3848 return -1;
3849 if (!equal)
3850 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
3851 "result not as expected", return -1);
3852 return 0;
3855 /* Basic test for isl_pw_multi_aff_product.
3857 * Check that multiple pieces are properly handled.
3859 static int test_product_pma(isl_ctx *ctx)
3861 int equal;
3862 const char *str;
3863 isl_pw_multi_aff *pma1, *pma2;
3865 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
3866 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3867 str = "{ C[] -> D[] }";
3868 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3869 pma1 = isl_pw_multi_aff_product(pma1, pma2);
3870 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
3871 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
3872 equal = pw_multi_aff_check_plain_equal(pma1, str);
3873 isl_pw_multi_aff_free(pma1);
3874 if (equal < 0)
3875 return -1;
3877 return 0;
3880 int test_product(isl_ctx *ctx)
3882 const char *str;
3883 isl_set *set;
3884 isl_union_set *uset1, *uset2;
3885 int ok;
3887 str = "{ A[i] }";
3888 set = isl_set_read_from_str(ctx, str);
3889 set = isl_set_product(set, isl_set_copy(set));
3890 ok = isl_set_is_wrapping(set);
3891 isl_set_free(set);
3892 if (ok < 0)
3893 return -1;
3894 if (!ok)
3895 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3897 str = "{ [] }";
3898 uset1 = isl_union_set_read_from_str(ctx, str);
3899 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3900 str = "{ [[] -> []] }";
3901 uset2 = isl_union_set_read_from_str(ctx, str);
3902 ok = isl_union_set_is_equal(uset1, uset2);
3903 isl_union_set_free(uset1);
3904 isl_union_set_free(uset2);
3905 if (ok < 0)
3906 return -1;
3907 if (!ok)
3908 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3910 if (test_product_pma(ctx) < 0)
3911 return -1;
3913 return 0;
3916 /* Check that two sets are not considered disjoint just because
3917 * they have a different set of (named) parameters.
3919 static int test_disjoint(isl_ctx *ctx)
3921 const char *str;
3922 isl_set *set, *set2;
3923 int disjoint;
3925 str = "[n] -> { [[]->[]] }";
3926 set = isl_set_read_from_str(ctx, str);
3927 str = "{ [[]->[]] }";
3928 set2 = isl_set_read_from_str(ctx, str);
3929 disjoint = isl_set_is_disjoint(set, set2);
3930 isl_set_free(set);
3931 isl_set_free(set2);
3932 if (disjoint < 0)
3933 return -1;
3934 if (disjoint)
3935 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3937 return 0;
3940 int test_equal(isl_ctx *ctx)
3942 const char *str;
3943 isl_set *set, *set2;
3944 int equal;
3946 str = "{ S_6[i] }";
3947 set = isl_set_read_from_str(ctx, str);
3948 str = "{ S_7[i] }";
3949 set2 = isl_set_read_from_str(ctx, str);
3950 equal = isl_set_is_equal(set, set2);
3951 isl_set_free(set);
3952 isl_set_free(set2);
3953 if (equal < 0)
3954 return -1;
3955 if (equal)
3956 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3958 return 0;
3961 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3962 enum isl_dim_type type, unsigned pos, int fixed)
3964 int test;
3966 test = isl_map_plain_is_fixed(map, type, pos, NULL);
3967 isl_map_free(map);
3968 if (test < 0)
3969 return -1;
3970 if (test == fixed)
3971 return 0;
3972 if (fixed)
3973 isl_die(ctx, isl_error_unknown,
3974 "map not detected as fixed", return -1);
3975 else
3976 isl_die(ctx, isl_error_unknown,
3977 "map detected as fixed", return -1);
3980 int test_fixed(isl_ctx *ctx)
3982 const char *str;
3983 isl_map *map;
3985 str = "{ [i] -> [i] }";
3986 map = isl_map_read_from_str(ctx, str);
3987 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3988 return -1;
3989 str = "{ [i] -> [1] }";
3990 map = isl_map_read_from_str(ctx, str);
3991 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3992 return -1;
3993 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3994 map = isl_map_read_from_str(ctx, str);
3995 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3996 return -1;
3997 map = isl_map_read_from_str(ctx, str);
3998 map = isl_map_neg(map);
3999 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
4000 return -1;
4002 return 0;
4005 struct isl_vertices_test_data {
4006 const char *set;
4007 int n;
4008 const char *vertex[2];
4009 } vertices_tests[] = {
4010 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
4011 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
4012 { "{ A[t, i] : t = 14 and i = 1 }",
4013 1, { "{ A[14, 1] }" } },
4016 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
4018 static int find_vertex(__isl_take isl_vertex *vertex, void *user)
4020 struct isl_vertices_test_data *data = user;
4021 isl_ctx *ctx;
4022 isl_multi_aff *ma;
4023 isl_basic_set *bset;
4024 isl_pw_multi_aff *pma;
4025 int i;
4026 int equal;
4028 ctx = isl_vertex_get_ctx(vertex);
4029 bset = isl_vertex_get_domain(vertex);
4030 ma = isl_vertex_get_expr(vertex);
4031 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
4033 for (i = 0; i < data->n; ++i) {
4034 isl_pw_multi_aff *pma_i;
4036 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
4037 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
4038 isl_pw_multi_aff_free(pma_i);
4040 if (equal < 0 || equal)
4041 break;
4044 isl_pw_multi_aff_free(pma);
4045 isl_vertex_free(vertex);
4047 if (equal < 0)
4048 return -1;
4050 return equal ? 0 : - 1;
4053 int test_vertices(isl_ctx *ctx)
4055 int i;
4057 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
4058 isl_basic_set *bset;
4059 isl_vertices *vertices;
4060 int ok = 1;
4061 int n;
4063 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
4064 vertices = isl_basic_set_compute_vertices(bset);
4065 n = isl_vertices_get_n_vertices(vertices);
4066 if (vertices_tests[i].n != n)
4067 ok = 0;
4068 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
4069 &vertices_tests[i]) < 0)
4070 ok = 0;
4071 isl_vertices_free(vertices);
4072 isl_basic_set_free(bset);
4074 if (!vertices)
4075 return -1;
4076 if (!ok)
4077 isl_die(ctx, isl_error_unknown, "unexpected vertices",
4078 return -1);
4081 return 0;
4084 int test_union_pw(isl_ctx *ctx)
4086 int equal;
4087 const char *str;
4088 isl_union_set *uset;
4089 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
4091 str = "{ [x] -> x^2 }";
4092 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
4093 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
4094 uset = isl_union_pw_qpolynomial_domain(upwqp1);
4095 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
4096 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
4097 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
4098 isl_union_pw_qpolynomial_free(upwqp1);
4099 isl_union_pw_qpolynomial_free(upwqp2);
4100 if (equal < 0)
4101 return -1;
4102 if (!equal)
4103 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4105 return 0;
4108 int test_output(isl_ctx *ctx)
4110 char *s;
4111 const char *str;
4112 isl_pw_aff *pa;
4113 isl_printer *p;
4114 int equal;
4116 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
4117 pa = isl_pw_aff_read_from_str(ctx, str);
4119 p = isl_printer_to_str(ctx);
4120 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
4121 p = isl_printer_print_pw_aff(p, pa);
4122 s = isl_printer_get_str(p);
4123 isl_printer_free(p);
4124 isl_pw_aff_free(pa);
4125 if (!s)
4126 equal = -1;
4127 else
4128 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
4129 free(s);
4130 if (equal < 0)
4131 return -1;
4132 if (!equal)
4133 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4135 return 0;
4138 int test_sample(isl_ctx *ctx)
4140 const char *str;
4141 isl_basic_set *bset1, *bset2;
4142 int empty, subset;
4144 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
4145 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
4146 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
4147 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
4148 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
4149 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
4150 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
4151 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
4152 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
4153 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
4154 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
4155 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
4156 "d - 1073741821e and "
4157 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
4158 "3j >= 1 - a + b + 2e and "
4159 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
4160 "3i <= 4 - a + 4b - e and "
4161 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
4162 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
4163 "c <= -1 + a and 3i >= -2 - a + 3e and "
4164 "1073741822e <= 1073741823 - a + 1073741822b + c and "
4165 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
4166 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
4167 "1073741823e >= 1 + 1073741823b - d and "
4168 "3i >= 1073741823b + c - 1073741823e - f and "
4169 "3i >= 1 + 2b + e + 3g }";
4170 bset1 = isl_basic_set_read_from_str(ctx, str);
4171 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
4172 empty = isl_basic_set_is_empty(bset2);
4173 subset = isl_basic_set_is_subset(bset2, bset1);
4174 isl_basic_set_free(bset1);
4175 isl_basic_set_free(bset2);
4176 if (empty < 0 || subset < 0)
4177 return -1;
4178 if (empty)
4179 isl_die(ctx, isl_error_unknown, "point not found", return -1);
4180 if (!subset)
4181 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
4183 return 0;
4186 int test_fixed_power(isl_ctx *ctx)
4188 const char *str;
4189 isl_map *map;
4190 isl_int exp;
4191 int equal;
4193 isl_int_init(exp);
4194 str = "{ [i] -> [i + 1] }";
4195 map = isl_map_read_from_str(ctx, str);
4196 isl_int_set_si(exp, 23);
4197 map = isl_map_fixed_power(map, exp);
4198 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
4199 isl_int_clear(exp);
4200 isl_map_free(map);
4201 if (equal < 0)
4202 return -1;
4204 return 0;
4207 int test_slice(isl_ctx *ctx)
4209 const char *str;
4210 isl_map *map;
4211 int equal;
4213 str = "{ [i] -> [j] }";
4214 map = isl_map_read_from_str(ctx, str);
4215 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
4216 equal = map_check_equal(map, "{ [i] -> [i] }");
4217 isl_map_free(map);
4218 if (equal < 0)
4219 return -1;
4221 str = "{ [i] -> [j] }";
4222 map = isl_map_read_from_str(ctx, str);
4223 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
4224 equal = map_check_equal(map, "{ [i] -> [j] }");
4225 isl_map_free(map);
4226 if (equal < 0)
4227 return -1;
4229 str = "{ [i] -> [j] }";
4230 map = isl_map_read_from_str(ctx, str);
4231 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
4232 equal = map_check_equal(map, "{ [i] -> [-i] }");
4233 isl_map_free(map);
4234 if (equal < 0)
4235 return -1;
4237 str = "{ [i] -> [j] }";
4238 map = isl_map_read_from_str(ctx, str);
4239 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
4240 equal = map_check_equal(map, "{ [0] -> [j] }");
4241 isl_map_free(map);
4242 if (equal < 0)
4243 return -1;
4245 str = "{ [i] -> [j] }";
4246 map = isl_map_read_from_str(ctx, str);
4247 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
4248 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
4249 isl_map_free(map);
4250 if (equal < 0)
4251 return -1;
4253 str = "{ [i] -> [j] }";
4254 map = isl_map_read_from_str(ctx, str);
4255 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
4256 equal = map_check_equal(map, "{ [i] -> [j] : false }");
4257 isl_map_free(map);
4258 if (equal < 0)
4259 return -1;
4261 return 0;
4264 int test_eliminate(isl_ctx *ctx)
4266 const char *str;
4267 isl_map *map;
4268 int equal;
4270 str = "{ [i] -> [j] : i = 2j }";
4271 map = isl_map_read_from_str(ctx, str);
4272 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
4273 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
4274 isl_map_free(map);
4275 if (equal < 0)
4276 return -1;
4278 return 0;
4281 /* Check that isl_set_dim_residue_class detects that the values of j
4282 * in the set below are all odd and that it does not detect any spurious
4283 * strides.
4285 static int test_residue_class(isl_ctx *ctx)
4287 const char *str;
4288 isl_set *set;
4289 isl_int m, r;
4290 int res;
4292 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
4293 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
4294 set = isl_set_read_from_str(ctx, str);
4295 isl_int_init(m);
4296 isl_int_init(r);
4297 res = isl_set_dim_residue_class(set, 1, &m, &r);
4298 if (res >= 0 &&
4299 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
4300 isl_die(ctx, isl_error_unknown, "incorrect residue class",
4301 res = -1);
4302 isl_int_clear(r);
4303 isl_int_clear(m);
4304 isl_set_free(set);
4306 return res;
4309 int test_align_parameters(isl_ctx *ctx)
4311 const char *str;
4312 isl_space *space;
4313 isl_multi_aff *ma1, *ma2;
4314 int equal;
4316 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
4317 ma1 = isl_multi_aff_read_from_str(ctx, str);
4319 space = isl_space_params_alloc(ctx, 1);
4320 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
4321 ma1 = isl_multi_aff_align_params(ma1, space);
4323 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
4324 ma2 = isl_multi_aff_read_from_str(ctx, str);
4326 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4328 isl_multi_aff_free(ma1);
4329 isl_multi_aff_free(ma2);
4331 if (equal < 0)
4332 return -1;
4333 if (!equal)
4334 isl_die(ctx, isl_error_unknown,
4335 "result not as expected", return -1);
4337 return 0;
4340 static int test_list(isl_ctx *ctx)
4342 isl_id *a, *b, *c, *d, *id;
4343 isl_id_list *list;
4344 int ok;
4346 a = isl_id_alloc(ctx, "a", NULL);
4347 b = isl_id_alloc(ctx, "b", NULL);
4348 c = isl_id_alloc(ctx, "c", NULL);
4349 d = isl_id_alloc(ctx, "d", NULL);
4351 list = isl_id_list_alloc(ctx, 4);
4352 list = isl_id_list_add(list, a);
4353 list = isl_id_list_add(list, b);
4354 list = isl_id_list_add(list, c);
4355 list = isl_id_list_add(list, d);
4356 list = isl_id_list_drop(list, 1, 1);
4358 if (isl_id_list_n_id(list) != 3) {
4359 isl_id_list_free(list);
4360 isl_die(ctx, isl_error_unknown,
4361 "unexpected number of elements in list", return -1);
4364 id = isl_id_list_get_id(list, 0);
4365 ok = id == a;
4366 isl_id_free(id);
4367 id = isl_id_list_get_id(list, 1);
4368 ok = ok && id == c;
4369 isl_id_free(id);
4370 id = isl_id_list_get_id(list, 2);
4371 ok = ok && id == d;
4372 isl_id_free(id);
4374 isl_id_list_free(list);
4376 if (!ok)
4377 isl_die(ctx, isl_error_unknown,
4378 "unexpected elements in list", return -1);
4380 return 0;
4383 const char *set_conversion_tests[] = {
4384 "[N] -> { [i] : N - 1 <= 2 i <= N }",
4385 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
4386 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4387 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4388 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
4391 /* Check that converting from isl_set to isl_pw_multi_aff and back
4392 * to isl_set produces the original isl_set.
4394 static int test_set_conversion(isl_ctx *ctx)
4396 int i;
4397 const char *str;
4398 isl_set *set1, *set2;
4399 isl_pw_multi_aff *pma;
4400 int equal;
4402 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
4403 str = set_conversion_tests[i];
4404 set1 = isl_set_read_from_str(ctx, str);
4405 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
4406 set2 = isl_set_from_pw_multi_aff(pma);
4407 equal = isl_set_is_equal(set1, set2);
4408 isl_set_free(set1);
4409 isl_set_free(set2);
4411 if (equal < 0)
4412 return -1;
4413 if (!equal)
4414 isl_die(ctx, isl_error_unknown, "bad conversion",
4415 return -1);
4418 return 0;
4421 /* Check that converting from isl_map to isl_pw_multi_aff and back
4422 * to isl_map produces the original isl_map.
4424 static int test_map_conversion(isl_ctx *ctx)
4426 const char *str;
4427 isl_map *map1, *map2;
4428 isl_pw_multi_aff *pma;
4429 int equal;
4431 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
4432 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
4433 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
4434 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
4435 "9e <= -2 - 2a) }";
4436 map1 = isl_map_read_from_str(ctx, str);
4437 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
4438 map2 = isl_map_from_pw_multi_aff(pma);
4439 equal = isl_map_is_equal(map1, map2);
4440 isl_map_free(map1);
4441 isl_map_free(map2);
4443 if (equal < 0)
4444 return -1;
4445 if (!equal)
4446 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
4448 return 0;
4451 static int test_conversion(isl_ctx *ctx)
4453 if (test_set_conversion(ctx) < 0)
4454 return -1;
4455 if (test_map_conversion(ctx) < 0)
4456 return -1;
4457 return 0;
4460 /* Check that isl_basic_map_curry does not modify input.
4462 static int test_curry(isl_ctx *ctx)
4464 const char *str;
4465 isl_basic_map *bmap1, *bmap2;
4466 int equal;
4468 str = "{ [A[] -> B[]] -> C[] }";
4469 bmap1 = isl_basic_map_read_from_str(ctx, str);
4470 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
4471 equal = isl_basic_map_is_equal(bmap1, bmap2);
4472 isl_basic_map_free(bmap1);
4473 isl_basic_map_free(bmap2);
4475 if (equal < 0)
4476 return -1;
4477 if (equal)
4478 isl_die(ctx, isl_error_unknown,
4479 "curried map should not be equal to original",
4480 return -1);
4482 return 0;
4485 struct {
4486 const char *set;
4487 const char *ma;
4488 const char *res;
4489 } preimage_tests[] = {
4490 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
4491 "{ A[j,i] -> B[i,j] }",
4492 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
4493 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4494 "{ A[a,b] -> B[a/2,b/6] }",
4495 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
4496 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4497 "{ A[a,b] -> B[a/2,b/6] }",
4498 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
4499 "exists i,j : a = 2 i and b = 6 j }" },
4500 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
4501 "[n] -> { : 0 <= n <= 100 }" },
4502 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4503 "{ A[a] -> B[2a] }",
4504 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
4505 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4506 "{ A[a] -> B[([a/2])] }",
4507 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
4508 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
4509 "{ A[a] -> B[a,a,a/3] }",
4510 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
4511 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
4512 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
4515 static int test_preimage_basic_set(isl_ctx *ctx)
4517 int i;
4518 isl_basic_set *bset1, *bset2;
4519 isl_multi_aff *ma;
4520 int equal;
4522 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
4523 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
4524 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
4525 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
4526 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
4527 equal = isl_basic_set_is_equal(bset1, bset2);
4528 isl_basic_set_free(bset1);
4529 isl_basic_set_free(bset2);
4530 if (equal < 0)
4531 return -1;
4532 if (!equal)
4533 isl_die(ctx, isl_error_unknown, "bad preimage",
4534 return -1);
4537 return 0;
4540 struct {
4541 const char *map;
4542 const char *ma;
4543 const char *res;
4544 } preimage_domain_tests[] = {
4545 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
4546 "{ A[j,i] -> B[i,j] }",
4547 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
4548 { "{ B[i] -> C[i]; D[i] -> E[i] }",
4549 "{ A[i] -> B[i + 1] }",
4550 "{ A[i] -> C[i + 1] }" },
4551 { "{ B[i] -> C[i]; B[i] -> E[i] }",
4552 "{ A[i] -> B[i + 1] }",
4553 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
4554 { "{ B[i] -> C[([i/2])] }",
4555 "{ A[i] -> B[2i] }",
4556 "{ A[i] -> C[i] }" },
4557 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
4558 "{ A[i] -> B[([i/5]), ([i/7])] }",
4559 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
4560 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
4561 "[N] -> { A[] -> B[([N/5])] }",
4562 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
4563 { "{ B[i] -> C[i] : exists a : i = 5 a }",
4564 "{ A[i] -> B[2i] }",
4565 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
4566 { "{ B[i] -> C[i] : exists a : i = 2 a; "
4567 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
4568 "{ A[i] -> B[2i] }",
4569 "{ A[i] -> C[2i] }" },
4572 static int test_preimage_union_map(isl_ctx *ctx)
4574 int i;
4575 isl_union_map *umap1, *umap2;
4576 isl_multi_aff *ma;
4577 int equal;
4579 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
4580 umap1 = isl_union_map_read_from_str(ctx,
4581 preimage_domain_tests[i].map);
4582 ma = isl_multi_aff_read_from_str(ctx,
4583 preimage_domain_tests[i].ma);
4584 umap2 = isl_union_map_read_from_str(ctx,
4585 preimage_domain_tests[i].res);
4586 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
4587 equal = isl_union_map_is_equal(umap1, umap2);
4588 isl_union_map_free(umap1);
4589 isl_union_map_free(umap2);
4590 if (equal < 0)
4591 return -1;
4592 if (!equal)
4593 isl_die(ctx, isl_error_unknown, "bad preimage",
4594 return -1);
4597 return 0;
4600 static int test_preimage(isl_ctx *ctx)
4602 if (test_preimage_basic_set(ctx) < 0)
4603 return -1;
4604 if (test_preimage_union_map(ctx) < 0)
4605 return -1;
4607 return 0;
4610 struct {
4611 const char *ma1;
4612 const char *ma;
4613 const char *res;
4614 } pullback_tests[] = {
4615 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
4616 "{ A[a,b] -> C[b + 2a] }" },
4617 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
4618 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
4619 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
4620 "{ A[a] -> C[(a)/6] }" },
4621 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
4622 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
4623 "{ A[a] -> C[(2a)/3] }" },
4624 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
4625 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
4626 "{ A[i,j] -> C[i + j, i + j] }"},
4627 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
4628 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
4629 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
4630 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
4631 "{ [i, j] -> [floor((i)/3), j] }",
4632 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
4635 static int test_pullback(isl_ctx *ctx)
4637 int i;
4638 isl_multi_aff *ma1, *ma2;
4639 isl_multi_aff *ma;
4640 int equal;
4642 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
4643 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
4644 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
4645 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
4646 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
4647 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4648 isl_multi_aff_free(ma1);
4649 isl_multi_aff_free(ma2);
4650 if (equal < 0)
4651 return -1;
4652 if (!equal)
4653 isl_die(ctx, isl_error_unknown, "bad pullback",
4654 return -1);
4657 return 0;
4660 /* Check that negation is printed correctly and that equal expressions
4661 * are correctly identified.
4663 static int test_ast(isl_ctx *ctx)
4665 isl_ast_expr *expr, *expr1, *expr2, *expr3;
4666 char *str;
4667 int ok, equal;
4669 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4670 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4671 expr = isl_ast_expr_add(expr1, expr2);
4672 expr2 = isl_ast_expr_copy(expr);
4673 expr = isl_ast_expr_neg(expr);
4674 expr2 = isl_ast_expr_neg(expr2);
4675 equal = isl_ast_expr_is_equal(expr, expr2);
4676 str = isl_ast_expr_to_str(expr);
4677 ok = str ? !strcmp(str, "-(A + B)") : -1;
4678 free(str);
4679 isl_ast_expr_free(expr);
4680 isl_ast_expr_free(expr2);
4682 if (ok < 0 || equal < 0)
4683 return -1;
4684 if (!equal)
4685 isl_die(ctx, isl_error_unknown,
4686 "equal expressions not considered equal", return -1);
4687 if (!ok)
4688 isl_die(ctx, isl_error_unknown,
4689 "isl_ast_expr printed incorrectly", return -1);
4691 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4692 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4693 expr = isl_ast_expr_add(expr1, expr2);
4694 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
4695 expr = isl_ast_expr_sub(expr3, expr);
4696 str = isl_ast_expr_to_str(expr);
4697 ok = str ? !strcmp(str, "C - (A + B)") : -1;
4698 free(str);
4699 isl_ast_expr_free(expr);
4701 if (ok < 0)
4702 return -1;
4703 if (!ok)
4704 isl_die(ctx, isl_error_unknown,
4705 "isl_ast_expr printed incorrectly", return -1);
4707 return 0;
4710 /* Check that isl_ast_build_expr_from_set returns a valid expression
4711 * for an empty set. Note that isl_ast_build_expr_from_set getting
4712 * called on an empty set probably indicates a bug in the caller.
4714 static int test_ast_build(isl_ctx *ctx)
4716 isl_set *set;
4717 isl_ast_build *build;
4718 isl_ast_expr *expr;
4720 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4721 build = isl_ast_build_from_context(set);
4723 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
4724 expr = isl_ast_build_expr_from_set(build, set);
4726 isl_ast_expr_free(expr);
4727 isl_ast_build_free(build);
4729 if (!expr)
4730 return -1;
4732 return 0;
4735 /* Internal data structure for before_for and after_for callbacks.
4737 * depth is the current depth
4738 * before is the number of times before_for has been called
4739 * after is the number of times after_for has been called
4741 struct isl_test_codegen_data {
4742 int depth;
4743 int before;
4744 int after;
4747 /* This function is called before each for loop in the AST generated
4748 * from test_ast_gen1.
4750 * Increment the number of calls and the depth.
4751 * Check that the space returned by isl_ast_build_get_schedule_space
4752 * matches the target space of the schedule returned by
4753 * isl_ast_build_get_schedule.
4754 * Return an isl_id that is checked by the corresponding call
4755 * to after_for.
4757 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
4758 void *user)
4760 struct isl_test_codegen_data *data = user;
4761 isl_ctx *ctx;
4762 isl_space *space;
4763 isl_union_map *schedule;
4764 isl_union_set *uset;
4765 isl_set *set;
4766 int empty;
4767 char name[] = "d0";
4769 ctx = isl_ast_build_get_ctx(build);
4771 if (data->before >= 3)
4772 isl_die(ctx, isl_error_unknown,
4773 "unexpected number of for nodes", return NULL);
4774 if (data->depth >= 2)
4775 isl_die(ctx, isl_error_unknown,
4776 "unexpected depth", return NULL);
4778 snprintf(name, sizeof(name), "d%d", data->depth);
4779 data->before++;
4780 data->depth++;
4782 schedule = isl_ast_build_get_schedule(build);
4783 uset = isl_union_map_range(schedule);
4784 if (!uset)
4785 return NULL;
4786 if (isl_union_set_n_set(uset) != 1) {
4787 isl_union_set_free(uset);
4788 isl_die(ctx, isl_error_unknown,
4789 "expecting single range space", return NULL);
4792 space = isl_ast_build_get_schedule_space(build);
4793 set = isl_union_set_extract_set(uset, space);
4794 isl_union_set_free(uset);
4795 empty = isl_set_is_empty(set);
4796 isl_set_free(set);
4798 if (empty < 0)
4799 return NULL;
4800 if (empty)
4801 isl_die(ctx, isl_error_unknown,
4802 "spaces don't match", return NULL);
4804 return isl_id_alloc(ctx, name, NULL);
4807 /* This function is called after each for loop in the AST generated
4808 * from test_ast_gen1.
4810 * Increment the number of calls and decrement the depth.
4811 * Check that the annotation attached to the node matches
4812 * the isl_id returned by the corresponding call to before_for.
4814 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
4815 __isl_keep isl_ast_build *build, void *user)
4817 struct isl_test_codegen_data *data = user;
4818 isl_id *id;
4819 const char *name;
4820 int valid;
4822 data->after++;
4823 data->depth--;
4825 if (data->after > data->before)
4826 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4827 "mismatch in number of for nodes",
4828 return isl_ast_node_free(node));
4830 id = isl_ast_node_get_annotation(node);
4831 if (!id)
4832 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4833 "missing annotation", return isl_ast_node_free(node));
4835 name = isl_id_get_name(id);
4836 valid = name && atoi(name + 1) == data->depth;
4837 isl_id_free(id);
4839 if (!valid)
4840 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4841 "wrong annotation", return isl_ast_node_free(node));
4843 return node;
4846 /* Check that the before_each_for and after_each_for callbacks
4847 * are called for each for loop in the generated code,
4848 * that they are called in the right order and that the isl_id
4849 * returned from the before_each_for callback is attached to
4850 * the isl_ast_node passed to the corresponding after_each_for call.
4852 static int test_ast_gen1(isl_ctx *ctx)
4854 const char *str;
4855 isl_set *set;
4856 isl_union_map *schedule;
4857 isl_ast_build *build;
4858 isl_ast_node *tree;
4859 struct isl_test_codegen_data data;
4861 str = "[N] -> { : N >= 10 }";
4862 set = isl_set_read_from_str(ctx, str);
4863 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
4864 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
4865 schedule = isl_union_map_read_from_str(ctx, str);
4867 data.before = 0;
4868 data.after = 0;
4869 data.depth = 0;
4870 build = isl_ast_build_from_context(set);
4871 build = isl_ast_build_set_before_each_for(build,
4872 &before_for, &data);
4873 build = isl_ast_build_set_after_each_for(build,
4874 &after_for, &data);
4875 tree = isl_ast_build_node_from_schedule_map(build, schedule);
4876 isl_ast_build_free(build);
4877 if (!tree)
4878 return -1;
4880 isl_ast_node_free(tree);
4882 if (data.before != 3 || data.after != 3)
4883 isl_die(ctx, isl_error_unknown,
4884 "unexpected number of for nodes", return -1);
4886 return 0;
4889 /* Check that the AST generator handles domains that are integrally disjoint
4890 * but not ratinoally disjoint.
4892 static int test_ast_gen2(isl_ctx *ctx)
4894 const char *str;
4895 isl_set *set;
4896 isl_union_map *schedule;
4897 isl_union_map *options;
4898 isl_ast_build *build;
4899 isl_ast_node *tree;
4901 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
4902 schedule = isl_union_map_read_from_str(ctx, str);
4903 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4904 build = isl_ast_build_from_context(set);
4906 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
4907 options = isl_union_map_read_from_str(ctx, str);
4908 build = isl_ast_build_set_options(build, options);
4909 tree = isl_ast_build_node_from_schedule_map(build, schedule);
4910 isl_ast_build_free(build);
4911 if (!tree)
4912 return -1;
4913 isl_ast_node_free(tree);
4915 return 0;
4918 /* Increment *user on each call.
4920 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
4921 __isl_keep isl_ast_build *build, void *user)
4923 int *n = user;
4925 (*n)++;
4927 return node;
4930 /* Test that unrolling tries to minimize the number of instances.
4931 * In particular, for the schedule given below, make sure it generates
4932 * 3 nodes (rather than 101).
4934 static int test_ast_gen3(isl_ctx *ctx)
4936 const char *str;
4937 isl_set *set;
4938 isl_union_map *schedule;
4939 isl_union_map *options;
4940 isl_ast_build *build;
4941 isl_ast_node *tree;
4942 int n_domain = 0;
4944 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
4945 schedule = isl_union_map_read_from_str(ctx, str);
4946 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4948 str = "{ [i] -> unroll[0] }";
4949 options = isl_union_map_read_from_str(ctx, str);
4951 build = isl_ast_build_from_context(set);
4952 build = isl_ast_build_set_options(build, options);
4953 build = isl_ast_build_set_at_each_domain(build,
4954 &count_domains, &n_domain);
4955 tree = isl_ast_build_node_from_schedule_map(build, schedule);
4956 isl_ast_build_free(build);
4957 if (!tree)
4958 return -1;
4960 isl_ast_node_free(tree);
4962 if (n_domain != 3)
4963 isl_die(ctx, isl_error_unknown,
4964 "unexpected number of for nodes", return -1);
4966 return 0;
4969 /* Check that if the ast_build_exploit_nested_bounds options is set,
4970 * we do not get an outer if node in the generated AST,
4971 * while we do get such an outer if node if the options is not set.
4973 static int test_ast_gen4(isl_ctx *ctx)
4975 const char *str;
4976 isl_set *set;
4977 isl_union_map *schedule;
4978 isl_ast_build *build;
4979 isl_ast_node *tree;
4980 enum isl_ast_node_type type;
4981 int enb;
4983 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4984 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4986 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4988 schedule = isl_union_map_read_from_str(ctx, str);
4989 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4990 build = isl_ast_build_from_context(set);
4991 tree = isl_ast_build_node_from_schedule_map(build, schedule);
4992 isl_ast_build_free(build);
4993 if (!tree)
4994 return -1;
4996 type = isl_ast_node_get_type(tree);
4997 isl_ast_node_free(tree);
4999 if (type == isl_ast_node_if)
5000 isl_die(ctx, isl_error_unknown,
5001 "not expecting if node", return -1);
5003 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
5005 schedule = isl_union_map_read_from_str(ctx, str);
5006 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5007 build = isl_ast_build_from_context(set);
5008 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5009 isl_ast_build_free(build);
5010 if (!tree)
5011 return -1;
5013 type = isl_ast_node_get_type(tree);
5014 isl_ast_node_free(tree);
5016 if (type != isl_ast_node_if)
5017 isl_die(ctx, isl_error_unknown,
5018 "expecting if node", return -1);
5020 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
5022 return 0;
5025 /* This function is called for each leaf in the AST generated
5026 * from test_ast_gen5.
5028 * We finalize the AST generation by extending the outer schedule
5029 * with a zero-dimensional schedule. If this results in any for loops,
5030 * then this means that we did not pass along enough information
5031 * about the outer schedule to the inner AST generation.
5033 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
5034 void *user)
5036 isl_union_map *schedule, *extra;
5037 isl_ast_node *tree;
5039 schedule = isl_ast_build_get_schedule(build);
5040 extra = isl_union_map_copy(schedule);
5041 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
5042 schedule = isl_union_map_range_product(schedule, extra);
5043 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5044 isl_ast_build_free(build);
5046 if (!tree)
5047 return NULL;
5049 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
5050 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
5051 "code should not contain any for loop",
5052 return isl_ast_node_free(tree));
5054 return tree;
5057 /* Check that we do not lose any information when going back and
5058 * forth between internal and external schedule.
5060 * In particular, we create an AST where we unroll the only
5061 * non-constant dimension in the schedule. We therefore do
5062 * not expect any for loops in the AST. However, older versions
5063 * of isl would not pass along enough information about the outer
5064 * schedule when performing an inner code generation from a create_leaf
5065 * callback, resulting in the inner code generation producing a for loop.
5067 static int test_ast_gen5(isl_ctx *ctx)
5069 const char *str;
5070 isl_set *set;
5071 isl_union_map *schedule, *options;
5072 isl_ast_build *build;
5073 isl_ast_node *tree;
5075 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
5076 schedule = isl_union_map_read_from_str(ctx, str);
5078 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
5079 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
5080 options = isl_union_map_read_from_str(ctx, str);
5082 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
5083 build = isl_ast_build_from_context(set);
5084 build = isl_ast_build_set_options(build, options);
5085 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
5086 tree = isl_ast_build_node_from_schedule_map(build, schedule);
5087 isl_ast_build_free(build);
5088 isl_ast_node_free(tree);
5089 if (!tree)
5090 return -1;
5092 return 0;
5095 static int test_ast_gen(isl_ctx *ctx)
5097 if (test_ast_gen1(ctx) < 0)
5098 return -1;
5099 if (test_ast_gen2(ctx) < 0)
5100 return -1;
5101 if (test_ast_gen3(ctx) < 0)
5102 return -1;
5103 if (test_ast_gen4(ctx) < 0)
5104 return -1;
5105 if (test_ast_gen5(ctx) < 0)
5106 return -1;
5107 return 0;
5110 /* Check if dropping output dimensions from an isl_pw_multi_aff
5111 * works properly.
5113 static int test_pw_multi_aff(isl_ctx *ctx)
5115 const char *str;
5116 isl_pw_multi_aff *pma1, *pma2;
5117 int equal;
5119 str = "{ [i,j] -> [i+j, 4i-j] }";
5120 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
5121 str = "{ [i,j] -> [4i-j] }";
5122 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5124 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
5126 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
5128 isl_pw_multi_aff_free(pma1);
5129 isl_pw_multi_aff_free(pma2);
5130 if (equal < 0)
5131 return -1;
5132 if (!equal)
5133 isl_die(ctx, isl_error_unknown,
5134 "expressions not equal", return -1);
5136 return 0;
5139 /* Check that we can properly parse multi piecewise affine expressions
5140 * where the piecewise affine expressions have different domains.
5142 static int test_multi_pw_aff(isl_ctx *ctx)
5144 const char *str;
5145 isl_set *dom, *dom2;
5146 isl_multi_pw_aff *mpa1, *mpa2;
5147 isl_pw_aff *pa;
5148 int equal;
5149 int equal_domain;
5151 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
5152 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
5153 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
5154 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
5155 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
5156 str = "{ [i] -> [(i : i > 0), 2i] }";
5157 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
5159 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
5161 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
5162 dom = isl_pw_aff_domain(pa);
5163 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
5164 dom2 = isl_pw_aff_domain(pa);
5165 equal_domain = isl_set_is_equal(dom, dom2);
5167 isl_set_free(dom);
5168 isl_set_free(dom2);
5169 isl_multi_pw_aff_free(mpa1);
5170 isl_multi_pw_aff_free(mpa2);
5172 if (equal < 0)
5173 return -1;
5174 if (!equal)
5175 isl_die(ctx, isl_error_unknown,
5176 "expressions not equal", return -1);
5178 if (equal_domain < 0)
5179 return -1;
5180 if (equal_domain)
5181 isl_die(ctx, isl_error_unknown,
5182 "domains unexpectedly equal", return -1);
5184 return 0;
5187 /* This is a regression test for a bug where isl_basic_map_simplify
5188 * would end up in an infinite loop. In particular, we construct
5189 * an empty basic set that is not obviously empty.
5190 * isl_basic_set_is_empty marks the basic set as empty.
5191 * After projecting out i3, the variable can be dropped completely,
5192 * but isl_basic_map_simplify refrains from doing so if the basic set
5193 * is empty and would end up in an infinite loop if it didn't test
5194 * explicitly for empty basic maps in the outer loop.
5196 static int test_simplify(isl_ctx *ctx)
5198 const char *str;
5199 isl_basic_set *bset;
5200 int empty;
5202 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
5203 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
5204 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
5205 "i3 >= i2 }";
5206 bset = isl_basic_set_read_from_str(ctx, str);
5207 empty = isl_basic_set_is_empty(bset);
5208 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
5209 isl_basic_set_free(bset);
5210 if (!bset)
5211 return -1;
5212 if (!empty)
5213 isl_die(ctx, isl_error_unknown,
5214 "basic set should be empty", return -1);
5216 return 0;
5219 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
5220 * with gbr context would fail to disable the use of the shifted tableau
5221 * when transferring equalities for the input to the context, resulting
5222 * in invalid sample values.
5224 static int test_partial_lexmin(isl_ctx *ctx)
5226 const char *str;
5227 isl_basic_set *bset;
5228 isl_basic_map *bmap;
5229 isl_map *map;
5231 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
5232 bmap = isl_basic_map_read_from_str(ctx, str);
5233 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
5234 bset = isl_basic_set_read_from_str(ctx, str);
5235 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
5236 isl_map_free(map);
5238 if (!map)
5239 return -1;
5241 return 0;
5244 /* Check that the variable compression performed on the existentially
5245 * quantified variables inside isl_basic_set_compute_divs is not confused
5246 * by the implicit equalities among the parameters.
5248 static int test_compute_divs(isl_ctx *ctx)
5250 const char *str;
5251 isl_basic_set *bset;
5252 isl_set *set;
5254 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
5255 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
5256 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
5257 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
5258 bset = isl_basic_set_read_from_str(ctx, str);
5259 set = isl_basic_set_compute_divs(bset);
5260 isl_set_free(set);
5261 if (!set)
5262 return -1;
5264 return 0;
5267 /* Check that the reaching domain elements and the prefix schedule
5268 * at a leaf node are the same before and after grouping.
5270 static int test_schedule_tree_group_1(isl_ctx *ctx)
5272 int equal;
5273 const char *str;
5274 isl_id *id;
5275 isl_union_set *uset;
5276 isl_multi_union_pw_aff *mupa;
5277 isl_union_pw_multi_aff *upma1, *upma2;
5278 isl_union_set *domain1, *domain2;
5279 isl_union_map *umap1, *umap2;
5280 isl_schedule_node *node;
5282 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
5283 uset = isl_union_set_read_from_str(ctx, str);
5284 node = isl_schedule_node_from_domain(uset);
5285 node = isl_schedule_node_child(node, 0);
5286 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
5287 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5288 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5289 node = isl_schedule_node_child(node, 0);
5290 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
5291 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5292 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5293 node = isl_schedule_node_child(node, 0);
5294 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
5295 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
5296 domain1 = isl_schedule_node_get_domain(node);
5297 id = isl_id_alloc(ctx, "group", NULL);
5298 node = isl_schedule_node_parent(node);
5299 node = isl_schedule_node_group(node, id);
5300 node = isl_schedule_node_child(node, 0);
5301 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
5302 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
5303 domain2 = isl_schedule_node_get_domain(node);
5304 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
5305 if (equal >= 0 && equal)
5306 equal = isl_union_set_is_equal(domain1, domain2);
5307 if (equal >= 0 && equal)
5308 equal = isl_union_map_is_equal(umap1, umap2);
5309 isl_union_map_free(umap1);
5310 isl_union_map_free(umap2);
5311 isl_union_set_free(domain1);
5312 isl_union_set_free(domain2);
5313 isl_union_pw_multi_aff_free(upma1);
5314 isl_union_pw_multi_aff_free(upma2);
5315 isl_schedule_node_free(node);
5317 if (equal < 0)
5318 return -1;
5319 if (!equal)
5320 isl_die(ctx, isl_error_unknown,
5321 "expressions not equal", return -1);
5323 return 0;
5326 /* Check that we can have nested groupings and that the union map
5327 * schedule representation is the same before and after the grouping.
5328 * Note that after the grouping, the union map representation contains
5329 * the domain constraints from the ranges of the expansion nodes,
5330 * while they are missing from the union map representation of
5331 * the tree without expansion nodes.
5333 * Also check that the global expansion is as expected.
5335 static int test_schedule_tree_group_2(isl_ctx *ctx)
5337 int equal, equal_expansion;
5338 const char *str;
5339 isl_id *id;
5340 isl_union_set *uset;
5341 isl_union_map *umap1, *umap2;
5342 isl_union_map *expansion1, *expansion2;
5343 isl_union_set_list *filters;
5344 isl_multi_union_pw_aff *mupa;
5345 isl_schedule *schedule;
5346 isl_schedule_node *node;
5348 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
5349 "S3[i,j] : 0 <= i,j < 10 }";
5350 uset = isl_union_set_read_from_str(ctx, str);
5351 node = isl_schedule_node_from_domain(uset);
5352 node = isl_schedule_node_child(node, 0);
5353 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
5354 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5355 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5356 node = isl_schedule_node_child(node, 0);
5357 str = "{ S1[i,j] }";
5358 uset = isl_union_set_read_from_str(ctx, str);
5359 filters = isl_union_set_list_from_union_set(uset);
5360 str = "{ S2[i,j]; S3[i,j] }";
5361 uset = isl_union_set_read_from_str(ctx, str);
5362 filters = isl_union_set_list_add(filters, uset);
5363 node = isl_schedule_node_insert_sequence(node, filters);
5364 node = isl_schedule_node_child(node, 1);
5365 node = isl_schedule_node_child(node, 0);
5366 str = "{ S2[i,j] }";
5367 uset = isl_union_set_read_from_str(ctx, str);
5368 filters = isl_union_set_list_from_union_set(uset);
5369 str = "{ S3[i,j] }";
5370 uset = isl_union_set_read_from_str(ctx, str);
5371 filters = isl_union_set_list_add(filters, uset);
5372 node = isl_schedule_node_insert_sequence(node, filters);
5374 schedule = isl_schedule_node_get_schedule(node);
5375 umap1 = isl_schedule_get_map(schedule);
5376 uset = isl_schedule_get_domain(schedule);
5377 umap1 = isl_union_map_intersect_domain(umap1, uset);
5378 isl_schedule_free(schedule);
5380 node = isl_schedule_node_parent(node);
5381 node = isl_schedule_node_parent(node);
5382 id = isl_id_alloc(ctx, "group1", NULL);
5383 node = isl_schedule_node_group(node, id);
5384 node = isl_schedule_node_child(node, 1);
5385 node = isl_schedule_node_child(node, 0);
5386 id = isl_id_alloc(ctx, "group2", NULL);
5387 node = isl_schedule_node_group(node, id);
5389 schedule = isl_schedule_node_get_schedule(node);
5390 umap2 = isl_schedule_get_map(schedule);
5391 isl_schedule_free(schedule);
5393 node = isl_schedule_node_root(node);
5394 node = isl_schedule_node_child(node, 0);
5395 expansion1 = isl_schedule_node_get_subtree_expansion(node);
5396 isl_schedule_node_free(node);
5398 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
5399 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
5400 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
5402 expansion2 = isl_union_map_read_from_str(ctx, str);
5404 equal = isl_union_map_is_equal(umap1, umap2);
5405 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
5407 isl_union_map_free(umap1);
5408 isl_union_map_free(umap2);
5409 isl_union_map_free(expansion1);
5410 isl_union_map_free(expansion2);
5412 if (equal < 0 || equal_expansion < 0)
5413 return -1;
5414 if (!equal)
5415 isl_die(ctx, isl_error_unknown,
5416 "expressions not equal", return -1);
5417 if (!equal_expansion)
5418 isl_die(ctx, isl_error_unknown,
5419 "unexpected expansion", return -1);
5421 return 0;
5424 /* Some tests for the isl_schedule_node_group function.
5426 static int test_schedule_tree_group(isl_ctx *ctx)
5428 if (test_schedule_tree_group_1(ctx) < 0)
5429 return -1;
5430 if (test_schedule_tree_group_2(ctx) < 0)
5431 return -1;
5432 return 0;
5435 struct {
5436 const char *set;
5437 const char *dual;
5438 } coef_tests[] = {
5439 { "{ rat: [i] : 0 <= i <= 10 }",
5440 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
5441 { "{ rat: [i] : FALSE }",
5442 "{ rat: coefficients[[cst] -> [a]] }" },
5443 { "{ rat: [i] : }",
5444 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
5447 struct {
5448 const char *set;
5449 const char *dual;
5450 } sol_tests[] = {
5451 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
5452 "{ rat: [i] : 0 <= i <= 10 }" },
5453 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
5454 "{ rat: [i] }" },
5455 { "{ rat: coefficients[[cst] -> [a]] }",
5456 "{ rat: [i] : FALSE }" },
5459 /* Test the basic functionality of isl_basic_set_coefficients and
5460 * isl_basic_set_solutions.
5462 static int test_dual(isl_ctx *ctx)
5464 int i;
5466 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
5467 int equal;
5468 isl_basic_set *bset1, *bset2;
5470 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
5471 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
5472 bset1 = isl_basic_set_coefficients(bset1);
5473 equal = isl_basic_set_is_equal(bset1, bset2);
5474 isl_basic_set_free(bset1);
5475 isl_basic_set_free(bset2);
5476 if (equal < 0)
5477 return -1;
5478 if (!equal)
5479 isl_die(ctx, isl_error_unknown,
5480 "incorrect dual", return -1);
5483 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
5484 int equal;
5485 isl_basic_set *bset1, *bset2;
5487 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
5488 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
5489 bset1 = isl_basic_set_solutions(bset1);
5490 equal = isl_basic_set_is_equal(bset1, bset2);
5491 isl_basic_set_free(bset1);
5492 isl_basic_set_free(bset2);
5493 if (equal < 0)
5494 return -1;
5495 if (!equal)
5496 isl_die(ctx, isl_error_unknown,
5497 "incorrect dual", return -1);
5500 return 0;
5503 struct {
5504 int scale_tile;
5505 int shift_point;
5506 const char *domain;
5507 const char *schedule;
5508 const char *sizes;
5509 const char *tile;
5510 const char *point;
5511 } tile_tests[] = {
5512 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
5513 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5514 "{ [32,32] }",
5515 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
5516 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5518 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
5519 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5520 "{ [32,32] }",
5521 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
5522 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5524 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
5525 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5526 "{ [32,32] }",
5527 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
5528 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
5530 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
5531 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
5532 "{ [32,32] }",
5533 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
5534 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
5538 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
5539 * tile the band and then check if the tile and point bands have the
5540 * expected partial schedule.
5542 static int test_tile(isl_ctx *ctx)
5544 int i;
5545 int scale;
5546 int shift;
5548 scale = isl_options_get_tile_scale_tile_loops(ctx);
5549 shift = isl_options_get_tile_shift_point_loops(ctx);
5551 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
5552 int opt;
5553 int equal;
5554 const char *str;
5555 isl_union_set *domain;
5556 isl_multi_union_pw_aff *mupa, *mupa2;
5557 isl_schedule_node *node;
5558 isl_multi_val *sizes;
5560 opt = tile_tests[i].scale_tile;
5561 isl_options_set_tile_scale_tile_loops(ctx, opt);
5562 opt = tile_tests[i].shift_point;
5563 isl_options_set_tile_shift_point_loops(ctx, opt);
5565 str = tile_tests[i].domain;
5566 domain = isl_union_set_read_from_str(ctx, str);
5567 node = isl_schedule_node_from_domain(domain);
5568 node = isl_schedule_node_child(node, 0);
5569 str = tile_tests[i].schedule;
5570 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5571 node = isl_schedule_node_insert_partial_schedule(node, mupa);
5572 str = tile_tests[i].sizes;
5573 sizes = isl_multi_val_read_from_str(ctx, str);
5574 node = isl_schedule_node_band_tile(node, sizes);
5576 str = tile_tests[i].tile;
5577 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5578 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
5579 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
5580 isl_multi_union_pw_aff_free(mupa);
5581 isl_multi_union_pw_aff_free(mupa2);
5583 node = isl_schedule_node_child(node, 0);
5585 str = tile_tests[i].point;
5586 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
5587 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
5588 if (equal >= 0 && equal)
5589 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
5590 mupa2);
5591 isl_multi_union_pw_aff_free(mupa);
5592 isl_multi_union_pw_aff_free(mupa2);
5594 isl_schedule_node_free(node);
5596 if (equal < 0)
5597 return -1;
5598 if (!equal)
5599 isl_die(ctx, isl_error_unknown,
5600 "unexpected result", return -1);
5603 isl_options_set_tile_scale_tile_loops(ctx, scale);
5604 isl_options_set_tile_shift_point_loops(ctx, shift);
5606 return 0;
5609 struct {
5610 const char *name;
5611 int (*fn)(isl_ctx *ctx);
5612 } tests [] = {
5613 { "dual", &test_dual },
5614 { "dependence analysis", &test_flow },
5615 { "val", &test_val },
5616 { "compute divs", &test_compute_divs },
5617 { "partial lexmin", &test_partial_lexmin },
5618 { "simplify", &test_simplify },
5619 { "curry", &test_curry },
5620 { "piecewise multi affine expressions", &test_pw_multi_aff },
5621 { "multi piecewise affine expressions", &test_multi_pw_aff },
5622 { "conversion", &test_conversion },
5623 { "list", &test_list },
5624 { "align parameters", &test_align_parameters },
5625 { "preimage", &test_preimage },
5626 { "pullback", &test_pullback },
5627 { "AST", &test_ast },
5628 { "AST build", &test_ast_build },
5629 { "AST generation", &test_ast_gen },
5630 { "eliminate", &test_eliminate },
5631 { "residue class", &test_residue_class },
5632 { "div", &test_div },
5633 { "slice", &test_slice },
5634 { "fixed power", &test_fixed_power },
5635 { "sample", &test_sample },
5636 { "output", &test_output },
5637 { "vertices", &test_vertices },
5638 { "fixed", &test_fixed },
5639 { "equal", &test_equal },
5640 { "disjoint", &test_disjoint },
5641 { "product", &test_product },
5642 { "dim_max", &test_dim_max },
5643 { "affine", &test_aff },
5644 { "injective", &test_injective },
5645 { "schedule", &test_schedule },
5646 { "schedule tree grouping", &test_schedule_tree_group },
5647 { "tile", &test_tile },
5648 { "union_pw", &test_union_pw },
5649 { "parse", &test_parse },
5650 { "single-valued", &test_sv },
5651 { "affine hull", &test_affine_hull },
5652 { "coalesce", &test_coalesce },
5653 { "factorize", &test_factorize },
5654 { "subset", &test_subset },
5655 { "subtract", &test_subtract },
5656 { "lexmin", &test_lexmin },
5657 { "min", &test_min },
5658 { "gist", &test_gist },
5659 { "piecewise quasi-polynomials", &test_pwqp },
5662 int main(int argc, char **argv)
5664 int i;
5665 struct isl_ctx *ctx;
5666 struct isl_options *options;
5668 srcdir = getenv("srcdir");
5669 assert(srcdir);
5671 options = isl_options_new_with_defaults();
5672 assert(options);
5673 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
5675 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
5676 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
5677 printf("%s\n", tests[i].name);
5678 if (tests[i].fn(ctx) < 0)
5679 goto error;
5681 test_lift(ctx);
5682 test_bound(ctx);
5683 test_union(ctx);
5684 test_split_periods(ctx);
5685 test_lex(ctx);
5686 test_bijective(ctx);
5687 test_dep(ctx);
5688 test_read(ctx);
5689 test_bounded(ctx);
5690 test_construction(ctx);
5691 test_dim(ctx);
5692 test_application(ctx);
5693 test_convex_hull(ctx);
5694 test_closure(ctx);
5695 isl_ctx_free(ctx);
5696 return 0;
5697 error:
5698 isl_ctx_free(ctx);
5699 return -1;