interface/extract_interface.cc: restore compatibility for clang 3.5 and 3.6
[isl.git] / isl_test.c
blob62eb1507f32152e22cb5d1c42a887c65fb323bef
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <assert.h>
19 #include <stdio.h>
20 #include <limits.h>
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl/set.h>
25 #include <isl/flow.h>
26 #include <isl_constraint_private.h>
27 #include <isl/polynomial.h>
28 #include <isl/union_map.h>
29 #include <isl_factorization.h>
30 #include <isl/schedule.h>
31 #include <isl_options_private.h>
32 #include <isl/vertices.h>
33 #include <isl/ast_build.h>
34 #include <isl/val.h>
35 #include <isl/ilp.h>
36 #include <isl_ast_build_expr.h>
37 #include <isl/options.h>
39 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
41 static char *srcdir;
43 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
44 char *filename;
45 int length;
46 char *pattern = "%s/test_inputs/%s.%s";
48 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
49 + strlen(suffix) + 1;
50 filename = isl_alloc_array(ctx, char, length);
52 if (!filename)
53 return NULL;
55 sprintf(filename, pattern, srcdir, name, suffix);
57 return filename;
60 void test_parse_map(isl_ctx *ctx, const char *str)
62 isl_map *map;
64 map = isl_map_read_from_str(ctx, str);
65 assert(map);
66 isl_map_free(map);
69 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
71 isl_map *map, *map2;
72 int equal;
74 map = isl_map_read_from_str(ctx, str);
75 map2 = isl_map_read_from_str(ctx, str2);
76 equal = isl_map_is_equal(map, map2);
77 isl_map_free(map);
78 isl_map_free(map2);
80 if (equal < 0)
81 return -1;
82 if (!equal)
83 isl_die(ctx, isl_error_unknown, "maps not equal",
84 return -1);
86 return 0;
89 void test_parse_pwqp(isl_ctx *ctx, const char *str)
91 isl_pw_qpolynomial *pwqp;
93 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
94 assert(pwqp);
95 isl_pw_qpolynomial_free(pwqp);
98 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
100 isl_pw_aff *pwaff;
102 pwaff = isl_pw_aff_read_from_str(ctx, str);
103 assert(pwaff);
104 isl_pw_aff_free(pwaff);
107 /* Check that we can read an isl_multi_val from "str" without errors.
109 static int test_parse_multi_val(isl_ctx *ctx, const char *str)
111 isl_multi_val *mv;
113 mv = isl_multi_val_read_from_str(ctx, str);
114 isl_multi_val_free(mv);
116 return mv ? 0 : -1;
119 int test_parse(struct isl_ctx *ctx)
121 isl_map *map, *map2;
122 const char *str, *str2;
124 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
125 return -1;
126 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
127 return -1;
128 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
129 return -1;
131 str = "{ [i] -> [-i] }";
132 map = isl_map_read_from_str(ctx, str);
133 assert(map);
134 isl_map_free(map);
136 str = "{ A[i] -> L[([i/3])] }";
137 map = isl_map_read_from_str(ctx, str);
138 assert(map);
139 isl_map_free(map);
141 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
142 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
143 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
145 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
146 str2 = "{ [x, y] : 2y >= 6 - x }";
147 if (test_parse_map_equal(ctx, str, str2) < 0)
148 return -1;
150 if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
151 "{ [x,y] : x <= y, 2*y + 3 }") < 0)
152 return -1;
153 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
154 if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
155 str) < 0)
156 return -1;
158 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
159 map = isl_map_read_from_str(ctx, str);
160 str = "{ [new, old] -> [o0, o1] : "
161 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
162 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
163 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
164 map2 = isl_map_read_from_str(ctx, str);
165 assert(isl_map_is_equal(map, map2));
166 isl_map_free(map);
167 isl_map_free(map2);
169 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
170 map = isl_map_read_from_str(ctx, str);
171 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
172 map2 = isl_map_read_from_str(ctx, str);
173 assert(isl_map_is_equal(map, map2));
174 isl_map_free(map);
175 isl_map_free(map2);
177 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
178 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
179 if (test_parse_map_equal(ctx, str, str2) < 0)
180 return -1;
182 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
183 str2 = "{ [i,j] -> [min(i,j)] }";
184 if (test_parse_map_equal(ctx, str, str2) < 0)
185 return -1;
187 str = "{ [i,j] : i != j }";
188 str2 = "{ [i,j] : i < j or i > j }";
189 if (test_parse_map_equal(ctx, str, str2) < 0)
190 return -1;
192 str = "{ [i,j] : (i+1)*2 >= j }";
193 str2 = "{ [i, j] : j <= 2 + 2i }";
194 if (test_parse_map_equal(ctx, str, str2) < 0)
195 return -1;
197 str = "{ [i] -> [i > 0 ? 4 : 5] }";
198 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
199 if (test_parse_map_equal(ctx, str, str2) < 0)
200 return -1;
202 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
203 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
204 if (test_parse_map_equal(ctx, str, str2) < 0)
205 return -1;
207 str = "{ [x] : x >= 0 }";
208 str2 = "{ [x] : x-0 >= 0 }";
209 if (test_parse_map_equal(ctx, str, str2) < 0)
210 return -1;
212 str = "{ [i] : ((i > 10)) }";
213 str2 = "{ [i] : i >= 11 }";
214 if (test_parse_map_equal(ctx, str, str2) < 0)
215 return -1;
217 str = "{ [i] -> [0] }";
218 str2 = "{ [i] -> [0 * i] }";
219 if (test_parse_map_equal(ctx, str, str2) < 0)
220 return -1;
222 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
223 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
224 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
225 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
227 if (test_parse_map_equal(ctx, "{ [a] -> [b] : (not false) }",
228 "{ [a] -> [b] : true }") < 0)
229 return -1;
231 if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
232 "{ [i] : i <= 10 }") < 0)
233 return -1;
235 if (test_parse_map_equal(ctx, "{Sym=[n] [i] : i <= n }",
236 "[n] -> { [i] : i <= n }") < 0)
237 return -1;
239 if (test_parse_map_equal(ctx, "{ [*] }", "{ [a] }") < 0)
240 return -1;
242 if (test_parse_map_equal(ctx, "{ [i] : 2*floor(i/2) = i }",
243 "{ [i] : exists a : i = 2 a }") < 0)
244 return -1;
246 if (test_parse_map_equal(ctx, "{ [a] -> [b] : a = 5 implies b = 5 }",
247 "{ [a] -> [b] : a != 5 or b = 5 }") < 0)
248 return -1;
250 if (test_parse_map_equal(ctx, "{ [a] -> [a - 1 : a > 0] }",
251 "{ [a] -> [a - 1] : a > 0 }") < 0)
252 return -1;
253 if (test_parse_map_equal(ctx,
254 "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
255 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }") < 0)
256 return -1;
257 if (test_parse_map_equal(ctx,
258 "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
259 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
260 return -1;
261 if (test_parse_map_equal(ctx,
262 "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
263 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
264 return -1;
265 if (test_parse_map_equal(ctx,
266 "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
267 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
268 return -1;
269 if (test_parse_map_equal(ctx,
270 "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
271 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
272 return -1;
274 return 0;
277 void test_read(struct isl_ctx *ctx)
279 char *filename;
280 FILE *input;
281 struct isl_basic_set *bset1, *bset2;
282 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
284 filename = get_filename(ctx, "set", "omega");
285 assert(filename);
286 input = fopen(filename, "r");
287 assert(input);
289 bset1 = isl_basic_set_read_from_file(ctx, input);
290 bset2 = isl_basic_set_read_from_str(ctx, str);
292 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
294 isl_basic_set_free(bset1);
295 isl_basic_set_free(bset2);
296 free(filename);
298 fclose(input);
301 void test_bounded(struct isl_ctx *ctx)
303 isl_set *set;
304 int bounded;
306 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
307 bounded = isl_set_is_bounded(set);
308 assert(bounded);
309 isl_set_free(set);
311 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
312 bounded = isl_set_is_bounded(set);
313 assert(!bounded);
314 isl_set_free(set);
316 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
317 bounded = isl_set_is_bounded(set);
318 assert(!bounded);
319 isl_set_free(set);
322 /* Construct the basic set { [i] : 5 <= i <= N } */
323 void test_construction(struct isl_ctx *ctx)
325 isl_int v;
326 isl_space *dim;
327 isl_local_space *ls;
328 struct isl_basic_set *bset;
329 struct isl_constraint *c;
331 isl_int_init(v);
333 dim = isl_space_set_alloc(ctx, 1, 1);
334 bset = isl_basic_set_universe(isl_space_copy(dim));
335 ls = isl_local_space_from_space(dim);
337 c = isl_inequality_alloc(isl_local_space_copy(ls));
338 isl_int_set_si(v, -1);
339 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
340 isl_int_set_si(v, 1);
341 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
342 bset = isl_basic_set_add_constraint(bset, c);
344 c = isl_inequality_alloc(isl_local_space_copy(ls));
345 isl_int_set_si(v, 1);
346 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
347 isl_int_set_si(v, -5);
348 isl_constraint_set_constant(c, v);
349 bset = isl_basic_set_add_constraint(bset, c);
351 isl_local_space_free(ls);
352 isl_basic_set_free(bset);
354 isl_int_clear(v);
357 void test_dim(struct isl_ctx *ctx)
359 const char *str;
360 isl_map *map1, *map2;
362 map1 = isl_map_read_from_str(ctx,
363 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
364 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
365 map2 = isl_map_read_from_str(ctx,
366 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
367 assert(isl_map_is_equal(map1, map2));
368 isl_map_free(map2);
370 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
371 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
372 assert(isl_map_is_equal(map1, map2));
374 isl_map_free(map1);
375 isl_map_free(map2);
377 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
378 map1 = isl_map_read_from_str(ctx, str);
379 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
380 map2 = isl_map_read_from_str(ctx, str);
381 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
382 assert(isl_map_is_equal(map1, map2));
384 isl_map_free(map1);
385 isl_map_free(map2);
388 struct {
389 __isl_give isl_val *(*op)(__isl_take isl_val *v);
390 const char *arg;
391 const char *res;
392 } val_un_tests[] = {
393 { &isl_val_neg, "0", "0" },
394 { &isl_val_abs, "0", "0" },
395 { &isl_val_2exp, "0", "1" },
396 { &isl_val_floor, "0", "0" },
397 { &isl_val_ceil, "0", "0" },
398 { &isl_val_neg, "1", "-1" },
399 { &isl_val_neg, "-1", "1" },
400 { &isl_val_neg, "1/2", "-1/2" },
401 { &isl_val_neg, "-1/2", "1/2" },
402 { &isl_val_neg, "infty", "-infty" },
403 { &isl_val_neg, "-infty", "infty" },
404 { &isl_val_neg, "NaN", "NaN" },
405 { &isl_val_abs, "1", "1" },
406 { &isl_val_abs, "-1", "1" },
407 { &isl_val_abs, "1/2", "1/2" },
408 { &isl_val_abs, "-1/2", "1/2" },
409 { &isl_val_abs, "infty", "infty" },
410 { &isl_val_abs, "-infty", "infty" },
411 { &isl_val_abs, "NaN", "NaN" },
412 { &isl_val_floor, "1", "1" },
413 { &isl_val_floor, "-1", "-1" },
414 { &isl_val_floor, "1/2", "0" },
415 { &isl_val_floor, "-1/2", "-1" },
416 { &isl_val_floor, "infty", "infty" },
417 { &isl_val_floor, "-infty", "-infty" },
418 { &isl_val_floor, "NaN", "NaN" },
419 { &isl_val_ceil, "1", "1" },
420 { &isl_val_ceil, "-1", "-1" },
421 { &isl_val_ceil, "1/2", "1" },
422 { &isl_val_ceil, "-1/2", "0" },
423 { &isl_val_ceil, "infty", "infty" },
424 { &isl_val_ceil, "-infty", "-infty" },
425 { &isl_val_ceil, "NaN", "NaN" },
426 { &isl_val_2exp, "-3", "1/8" },
427 { &isl_val_2exp, "-1", "1/2" },
428 { &isl_val_2exp, "1", "2" },
429 { &isl_val_2exp, "2", "4" },
430 { &isl_val_2exp, "3", "8" },
431 { &isl_val_inv, "1", "1" },
432 { &isl_val_inv, "2", "1/2" },
433 { &isl_val_inv, "1/2", "2" },
434 { &isl_val_inv, "-2", "-1/2" },
435 { &isl_val_inv, "-1/2", "-2" },
436 { &isl_val_inv, "0", "NaN" },
437 { &isl_val_inv, "NaN", "NaN" },
438 { &isl_val_inv, "infty", "0" },
439 { &isl_val_inv, "-infty", "0" },
442 /* Perform some basic tests of unary operations on isl_val objects.
444 static int test_un_val(isl_ctx *ctx)
446 int i;
447 isl_val *v, *res;
448 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
449 int ok;
451 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
452 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
453 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
454 fn = val_un_tests[i].op;
455 v = fn(v);
456 if (isl_val_is_nan(res))
457 ok = isl_val_is_nan(v);
458 else
459 ok = isl_val_eq(v, res);
460 isl_val_free(v);
461 isl_val_free(res);
462 if (ok < 0)
463 return -1;
464 if (!ok)
465 isl_die(ctx, isl_error_unknown,
466 "unexpected result", return -1);
469 return 0;
472 struct {
473 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
474 __isl_take isl_val *v2);
475 } val_bin_op[] = {
476 ['+'] = { &isl_val_add },
477 ['-'] = { &isl_val_sub },
478 ['*'] = { &isl_val_mul },
479 ['/'] = { &isl_val_div },
480 ['g'] = { &isl_val_gcd },
481 ['m'] = { &isl_val_min },
482 ['M'] = { &isl_val_max },
485 struct {
486 const char *arg1;
487 unsigned char op;
488 const char *arg2;
489 const char *res;
490 } val_bin_tests[] = {
491 { "0", '+', "0", "0" },
492 { "1", '+', "0", "1" },
493 { "1", '+', "1", "2" },
494 { "1", '-', "1", "0" },
495 { "1", '*', "1", "1" },
496 { "1", '/', "1", "1" },
497 { "2", '*', "3", "6" },
498 { "2", '*', "1/2", "1" },
499 { "2", '*', "1/3", "2/3" },
500 { "2/3", '*', "3/5", "2/5" },
501 { "2/3", '*', "7/5", "14/15" },
502 { "2", '/', "1/2", "4" },
503 { "-2", '/', "-1/2", "4" },
504 { "-2", '/', "1/2", "-4" },
505 { "2", '/', "-1/2", "-4" },
506 { "2", '/', "2", "1" },
507 { "2", '/', "3", "2/3" },
508 { "2/3", '/', "5/3", "2/5" },
509 { "2/3", '/', "5/7", "14/15" },
510 { "0", '/', "0", "NaN" },
511 { "42", '/', "0", "NaN" },
512 { "-42", '/', "0", "NaN" },
513 { "infty", '/', "0", "NaN" },
514 { "-infty", '/', "0", "NaN" },
515 { "NaN", '/', "0", "NaN" },
516 { "0", '/', "NaN", "NaN" },
517 { "42", '/', "NaN", "NaN" },
518 { "-42", '/', "NaN", "NaN" },
519 { "infty", '/', "NaN", "NaN" },
520 { "-infty", '/', "NaN", "NaN" },
521 { "NaN", '/', "NaN", "NaN" },
522 { "0", '/', "infty", "0" },
523 { "42", '/', "infty", "0" },
524 { "-42", '/', "infty", "0" },
525 { "infty", '/', "infty", "NaN" },
526 { "-infty", '/', "infty", "NaN" },
527 { "NaN", '/', "infty", "NaN" },
528 { "0", '/', "-infty", "0" },
529 { "42", '/', "-infty", "0" },
530 { "-42", '/', "-infty", "0" },
531 { "infty", '/', "-infty", "NaN" },
532 { "-infty", '/', "-infty", "NaN" },
533 { "NaN", '/', "-infty", "NaN" },
534 { "1", '-', "1/3", "2/3" },
535 { "1/3", '+', "1/2", "5/6" },
536 { "1/2", '+', "1/2", "1" },
537 { "3/4", '-', "1/4", "1/2" },
538 { "1/2", '-', "1/3", "1/6" },
539 { "infty", '+', "42", "infty" },
540 { "infty", '+', "infty", "infty" },
541 { "42", '+', "infty", "infty" },
542 { "infty", '-', "infty", "NaN" },
543 { "infty", '*', "infty", "infty" },
544 { "infty", '*', "-infty", "-infty" },
545 { "-infty", '*', "infty", "-infty" },
546 { "-infty", '*', "-infty", "infty" },
547 { "0", '*', "infty", "NaN" },
548 { "1", '*', "infty", "infty" },
549 { "infty", '*', "0", "NaN" },
550 { "infty", '*', "42", "infty" },
551 { "42", '-', "infty", "-infty" },
552 { "infty", '+', "-infty", "NaN" },
553 { "4", 'g', "6", "2" },
554 { "5", 'g', "6", "1" },
555 { "42", 'm', "3", "3" },
556 { "42", 'M', "3", "42" },
557 { "3", 'm', "42", "3" },
558 { "3", 'M', "42", "42" },
559 { "42", 'm', "infty", "42" },
560 { "42", 'M', "infty", "infty" },
561 { "42", 'm', "-infty", "-infty" },
562 { "42", 'M', "-infty", "42" },
563 { "42", 'm', "NaN", "NaN" },
564 { "42", 'M', "NaN", "NaN" },
565 { "infty", 'm', "-infty", "-infty" },
566 { "infty", 'M', "-infty", "infty" },
569 /* Perform some basic tests of binary operations on isl_val objects.
571 static int test_bin_val(isl_ctx *ctx)
573 int i;
574 isl_val *v1, *v2, *res;
575 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
576 __isl_take isl_val *v2);
577 int ok;
579 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
580 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
581 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
582 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
583 fn = val_bin_op[val_bin_tests[i].op].fn;
584 v1 = fn(v1, v2);
585 if (isl_val_is_nan(res))
586 ok = isl_val_is_nan(v1);
587 else
588 ok = isl_val_eq(v1, res);
589 isl_val_free(v1);
590 isl_val_free(res);
591 if (ok < 0)
592 return -1;
593 if (!ok)
594 isl_die(ctx, isl_error_unknown,
595 "unexpected result", return -1);
598 return 0;
601 /* Perform some basic tests on isl_val objects.
603 static int test_val(isl_ctx *ctx)
605 if (test_un_val(ctx) < 0)
606 return -1;
607 if (test_bin_val(ctx) < 0)
608 return -1;
609 return 0;
612 static int test_div(isl_ctx *ctx)
614 unsigned n;
615 const char *str;
616 int empty;
617 isl_int v;
618 isl_space *dim;
619 isl_set *set;
620 isl_local_space *ls;
621 struct isl_basic_set *bset;
622 struct isl_constraint *c;
624 isl_int_init(v);
626 /* test 1 */
627 dim = isl_space_set_alloc(ctx, 0, 3);
628 bset = isl_basic_set_universe(isl_space_copy(dim));
629 ls = isl_local_space_from_space(dim);
631 c = isl_equality_alloc(isl_local_space_copy(ls));
632 isl_int_set_si(v, -1);
633 isl_constraint_set_constant(c, v);
634 isl_int_set_si(v, 1);
635 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
636 isl_int_set_si(v, 3);
637 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
638 bset = isl_basic_set_add_constraint(bset, c);
640 c = isl_equality_alloc(isl_local_space_copy(ls));
641 isl_int_set_si(v, 1);
642 isl_constraint_set_constant(c, v);
643 isl_int_set_si(v, -1);
644 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
645 isl_int_set_si(v, 3);
646 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
647 bset = isl_basic_set_add_constraint(bset, c);
649 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
651 assert(bset && bset->n_div == 1);
652 isl_local_space_free(ls);
653 isl_basic_set_free(bset);
655 /* test 2 */
656 dim = isl_space_set_alloc(ctx, 0, 3);
657 bset = isl_basic_set_universe(isl_space_copy(dim));
658 ls = isl_local_space_from_space(dim);
660 c = isl_equality_alloc(isl_local_space_copy(ls));
661 isl_int_set_si(v, 1);
662 isl_constraint_set_constant(c, v);
663 isl_int_set_si(v, -1);
664 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
665 isl_int_set_si(v, 3);
666 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
667 bset = isl_basic_set_add_constraint(bset, c);
669 c = isl_equality_alloc(isl_local_space_copy(ls));
670 isl_int_set_si(v, -1);
671 isl_constraint_set_constant(c, v);
672 isl_int_set_si(v, 1);
673 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
674 isl_int_set_si(v, 3);
675 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
676 bset = isl_basic_set_add_constraint(bset, c);
678 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
680 assert(bset && bset->n_div == 1);
681 isl_local_space_free(ls);
682 isl_basic_set_free(bset);
684 /* test 3 */
685 dim = isl_space_set_alloc(ctx, 0, 3);
686 bset = isl_basic_set_universe(isl_space_copy(dim));
687 ls = isl_local_space_from_space(dim);
689 c = isl_equality_alloc(isl_local_space_copy(ls));
690 isl_int_set_si(v, 1);
691 isl_constraint_set_constant(c, v);
692 isl_int_set_si(v, -1);
693 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
694 isl_int_set_si(v, 3);
695 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
696 bset = isl_basic_set_add_constraint(bset, c);
698 c = isl_equality_alloc(isl_local_space_copy(ls));
699 isl_int_set_si(v, -3);
700 isl_constraint_set_constant(c, v);
701 isl_int_set_si(v, 1);
702 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
703 isl_int_set_si(v, 4);
704 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
705 bset = isl_basic_set_add_constraint(bset, c);
707 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
709 assert(bset && bset->n_div == 1);
710 isl_local_space_free(ls);
711 isl_basic_set_free(bset);
713 /* test 4 */
714 dim = isl_space_set_alloc(ctx, 0, 3);
715 bset = isl_basic_set_universe(isl_space_copy(dim));
716 ls = isl_local_space_from_space(dim);
718 c = isl_equality_alloc(isl_local_space_copy(ls));
719 isl_int_set_si(v, 2);
720 isl_constraint_set_constant(c, v);
721 isl_int_set_si(v, -1);
722 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
723 isl_int_set_si(v, 3);
724 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
725 bset = isl_basic_set_add_constraint(bset, c);
727 c = isl_equality_alloc(isl_local_space_copy(ls));
728 isl_int_set_si(v, -1);
729 isl_constraint_set_constant(c, v);
730 isl_int_set_si(v, 1);
731 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
732 isl_int_set_si(v, 6);
733 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
734 bset = isl_basic_set_add_constraint(bset, c);
736 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
738 assert(isl_basic_set_is_empty(bset));
739 isl_local_space_free(ls);
740 isl_basic_set_free(bset);
742 /* test 5 */
743 dim = isl_space_set_alloc(ctx, 0, 3);
744 bset = isl_basic_set_universe(isl_space_copy(dim));
745 ls = isl_local_space_from_space(dim);
747 c = isl_equality_alloc(isl_local_space_copy(ls));
748 isl_int_set_si(v, -1);
749 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
750 isl_int_set_si(v, 3);
751 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
752 bset = isl_basic_set_add_constraint(bset, c);
754 c = isl_equality_alloc(isl_local_space_copy(ls));
755 isl_int_set_si(v, 1);
756 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
757 isl_int_set_si(v, -3);
758 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
759 bset = isl_basic_set_add_constraint(bset, c);
761 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
763 assert(bset && bset->n_div == 0);
764 isl_basic_set_free(bset);
765 isl_local_space_free(ls);
767 /* test 6 */
768 dim = isl_space_set_alloc(ctx, 0, 3);
769 bset = isl_basic_set_universe(isl_space_copy(dim));
770 ls = isl_local_space_from_space(dim);
772 c = isl_equality_alloc(isl_local_space_copy(ls));
773 isl_int_set_si(v, -1);
774 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
775 isl_int_set_si(v, 6);
776 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
777 bset = isl_basic_set_add_constraint(bset, c);
779 c = isl_equality_alloc(isl_local_space_copy(ls));
780 isl_int_set_si(v, 1);
781 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
782 isl_int_set_si(v, -3);
783 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
784 bset = isl_basic_set_add_constraint(bset, c);
786 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
788 assert(bset && bset->n_div == 1);
789 isl_basic_set_free(bset);
790 isl_local_space_free(ls);
792 /* test 7 */
793 /* This test is a bit tricky. We set up an equality
794 * a + 3b + 3c = 6 e0
795 * Normalization of divs creates _two_ divs
796 * a = 3 e0
797 * c - b - e0 = 2 e1
798 * Afterwards e0 is removed again because it has coefficient -1
799 * and we end up with the original equality and div again.
800 * Perhaps we can avoid the introduction of this temporary div.
802 dim = isl_space_set_alloc(ctx, 0, 4);
803 bset = isl_basic_set_universe(isl_space_copy(dim));
804 ls = isl_local_space_from_space(dim);
806 c = isl_equality_alloc(isl_local_space_copy(ls));
807 isl_int_set_si(v, -1);
808 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
809 isl_int_set_si(v, -3);
810 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
811 isl_int_set_si(v, -3);
812 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
813 isl_int_set_si(v, 6);
814 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
815 bset = isl_basic_set_add_constraint(bset, c);
817 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
819 /* Test disabled for now */
821 assert(bset && bset->n_div == 1);
823 isl_local_space_free(ls);
824 isl_basic_set_free(bset);
826 /* test 8 */
827 dim = isl_space_set_alloc(ctx, 0, 5);
828 bset = isl_basic_set_universe(isl_space_copy(dim));
829 ls = isl_local_space_from_space(dim);
831 c = isl_equality_alloc(isl_local_space_copy(ls));
832 isl_int_set_si(v, -1);
833 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
834 isl_int_set_si(v, -3);
835 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
836 isl_int_set_si(v, -3);
837 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
838 isl_int_set_si(v, 6);
839 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
840 bset = isl_basic_set_add_constraint(bset, c);
842 c = isl_equality_alloc(isl_local_space_copy(ls));
843 isl_int_set_si(v, -1);
844 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
845 isl_int_set_si(v, 1);
846 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
847 isl_int_set_si(v, 1);
848 isl_constraint_set_constant(c, v);
849 bset = isl_basic_set_add_constraint(bset, c);
851 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
853 /* Test disabled for now */
855 assert(bset && bset->n_div == 1);
857 isl_local_space_free(ls);
858 isl_basic_set_free(bset);
860 /* test 9 */
861 dim = isl_space_set_alloc(ctx, 0, 4);
862 bset = isl_basic_set_universe(isl_space_copy(dim));
863 ls = isl_local_space_from_space(dim);
865 c = isl_equality_alloc(isl_local_space_copy(ls));
866 isl_int_set_si(v, 1);
867 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
868 isl_int_set_si(v, -1);
869 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
870 isl_int_set_si(v, -2);
871 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
872 bset = isl_basic_set_add_constraint(bset, c);
874 c = isl_equality_alloc(isl_local_space_copy(ls));
875 isl_int_set_si(v, -1);
876 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
877 isl_int_set_si(v, 3);
878 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
879 isl_int_set_si(v, 2);
880 isl_constraint_set_constant(c, v);
881 bset = isl_basic_set_add_constraint(bset, c);
883 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
885 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
887 assert(!isl_basic_set_is_empty(bset));
889 isl_local_space_free(ls);
890 isl_basic_set_free(bset);
892 /* test 10 */
893 dim = isl_space_set_alloc(ctx, 0, 3);
894 bset = isl_basic_set_universe(isl_space_copy(dim));
895 ls = isl_local_space_from_space(dim);
897 c = isl_equality_alloc(isl_local_space_copy(ls));
898 isl_int_set_si(v, 1);
899 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
900 isl_int_set_si(v, -2);
901 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
902 bset = isl_basic_set_add_constraint(bset, c);
904 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
906 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
908 isl_local_space_free(ls);
909 isl_basic_set_free(bset);
911 isl_int_clear(v);
913 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
914 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
915 set = isl_set_read_from_str(ctx, str);
916 set = isl_set_compute_divs(set);
917 isl_set_free(set);
918 if (!set)
919 return -1;
921 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
922 bset = isl_basic_set_read_from_str(ctx, str);
923 n = isl_basic_set_dim(bset, isl_dim_div);
924 isl_basic_set_free(bset);
925 if (!bset)
926 return -1;
927 if (n != 0)
928 isl_die(ctx, isl_error_unknown,
929 "expecting no existentials", return -1);
931 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
932 set = isl_set_read_from_str(ctx, str);
933 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
934 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
935 empty = isl_set_is_empty(set);
936 isl_set_free(set);
937 if (empty < 0)
938 return -1;
939 if (!empty)
940 isl_die(ctx, isl_error_unknown,
941 "result not as accurate as expected", return -1);
943 return 0;
946 void test_application_case(struct isl_ctx *ctx, const char *name)
948 char *filename;
949 FILE *input;
950 struct isl_basic_set *bset1, *bset2;
951 struct isl_basic_map *bmap;
953 filename = get_filename(ctx, name, "omega");
954 assert(filename);
955 input = fopen(filename, "r");
956 assert(input);
958 bset1 = isl_basic_set_read_from_file(ctx, input);
959 bmap = isl_basic_map_read_from_file(ctx, input);
961 bset1 = isl_basic_set_apply(bset1, bmap);
963 bset2 = isl_basic_set_read_from_file(ctx, input);
965 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
967 isl_basic_set_free(bset1);
968 isl_basic_set_free(bset2);
969 free(filename);
971 fclose(input);
974 void test_application(struct isl_ctx *ctx)
976 test_application_case(ctx, "application");
977 test_application_case(ctx, "application2");
980 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
982 char *filename;
983 FILE *input;
984 struct isl_basic_set *bset1, *bset2;
986 filename = get_filename(ctx, name, "polylib");
987 assert(filename);
988 input = fopen(filename, "r");
989 assert(input);
991 bset1 = isl_basic_set_read_from_file(ctx, input);
992 bset2 = isl_basic_set_read_from_file(ctx, input);
994 bset1 = isl_basic_set_affine_hull(bset1);
996 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
998 isl_basic_set_free(bset1);
999 isl_basic_set_free(bset2);
1000 free(filename);
1002 fclose(input);
1005 int test_affine_hull(struct isl_ctx *ctx)
1007 const char *str;
1008 isl_set *set;
1009 isl_basic_set *bset, *bset2;
1010 int n;
1011 int subset;
1013 test_affine_hull_case(ctx, "affine2");
1014 test_affine_hull_case(ctx, "affine");
1015 test_affine_hull_case(ctx, "affine3");
1017 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1018 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1019 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1020 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1021 set = isl_set_read_from_str(ctx, str);
1022 bset = isl_set_affine_hull(set);
1023 n = isl_basic_set_dim(bset, isl_dim_div);
1024 isl_basic_set_free(bset);
1025 if (n != 0)
1026 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1027 return -1);
1029 /* Check that isl_map_affine_hull is not confused by
1030 * the reordering of divs in isl_map_align_divs.
1032 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1033 "32e0 = b and 32e1 = c); "
1034 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1035 set = isl_set_read_from_str(ctx, str);
1036 bset = isl_set_affine_hull(set);
1037 isl_basic_set_free(bset);
1038 if (!bset)
1039 return -1;
1041 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1042 "32e2 = 31 + 31e0 }";
1043 set = isl_set_read_from_str(ctx, str);
1044 bset = isl_set_affine_hull(set);
1045 str = "{ [a] : exists e : a = 32 e }";
1046 bset2 = isl_basic_set_read_from_str(ctx, str);
1047 subset = isl_basic_set_is_subset(bset, bset2);
1048 isl_basic_set_free(bset);
1049 isl_basic_set_free(bset2);
1050 if (subset < 0)
1051 return -1;
1052 if (!subset)
1053 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1054 return -1);
1056 return 0;
1059 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1061 char *filename;
1062 FILE *input;
1063 struct isl_basic_set *bset1, *bset2;
1064 struct isl_set *set;
1066 filename = get_filename(ctx, name, "polylib");
1067 assert(filename);
1068 input = fopen(filename, "r");
1069 assert(input);
1071 bset1 = isl_basic_set_read_from_file(ctx, input);
1072 bset2 = isl_basic_set_read_from_file(ctx, input);
1074 set = isl_basic_set_union(bset1, bset2);
1075 bset1 = isl_set_convex_hull(set);
1077 bset2 = isl_basic_set_read_from_file(ctx, input);
1079 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1081 isl_basic_set_free(bset1);
1082 isl_basic_set_free(bset2);
1083 free(filename);
1085 fclose(input);
1088 struct {
1089 const char *set;
1090 const char *hull;
1091 } convex_hull_tests[] = {
1092 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1093 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1094 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1095 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1096 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1097 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1098 "i2 <= 5 and i2 >= 4; "
1099 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1100 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1101 "i2 <= 5 + i0 and i2 >= i0 }" },
1102 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1103 "{ [x, y] : 1 = 0 }" },
1106 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
1108 int i;
1109 int orig_convex = ctx->opt->convex;
1110 ctx->opt->convex = convex;
1112 test_convex_hull_case(ctx, "convex0");
1113 test_convex_hull_case(ctx, "convex1");
1114 test_convex_hull_case(ctx, "convex2");
1115 test_convex_hull_case(ctx, "convex3");
1116 test_convex_hull_case(ctx, "convex4");
1117 test_convex_hull_case(ctx, "convex5");
1118 test_convex_hull_case(ctx, "convex6");
1119 test_convex_hull_case(ctx, "convex7");
1120 test_convex_hull_case(ctx, "convex8");
1121 test_convex_hull_case(ctx, "convex9");
1122 test_convex_hull_case(ctx, "convex10");
1123 test_convex_hull_case(ctx, "convex11");
1124 test_convex_hull_case(ctx, "convex12");
1125 test_convex_hull_case(ctx, "convex13");
1126 test_convex_hull_case(ctx, "convex14");
1127 test_convex_hull_case(ctx, "convex15");
1129 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1130 isl_set *set1, *set2;
1132 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1133 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1134 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1135 assert(isl_set_is_equal(set1, set2));
1136 isl_set_free(set1);
1137 isl_set_free(set2);
1140 ctx->opt->convex = orig_convex;
1143 void test_convex_hull(struct isl_ctx *ctx)
1145 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
1146 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
1149 void test_gist_case(struct isl_ctx *ctx, const char *name)
1151 char *filename;
1152 FILE *input;
1153 struct isl_basic_set *bset1, *bset2;
1155 filename = get_filename(ctx, name, "polylib");
1156 assert(filename);
1157 input = fopen(filename, "r");
1158 assert(input);
1160 bset1 = isl_basic_set_read_from_file(ctx, input);
1161 bset2 = isl_basic_set_read_from_file(ctx, input);
1163 bset1 = isl_basic_set_gist(bset1, bset2);
1165 bset2 = isl_basic_set_read_from_file(ctx, input);
1167 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1169 isl_basic_set_free(bset1);
1170 isl_basic_set_free(bset2);
1171 free(filename);
1173 fclose(input);
1176 struct {
1177 const char *set;
1178 const char *context;
1179 const char *gist;
1180 } gist_tests[] = {
1181 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1182 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1183 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1184 "{ [a, b, c] : a <= 15 }" },
1185 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1186 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1187 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1188 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1189 { "{ [m, n, a, b] : a <= 2147 + n }",
1190 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1191 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1192 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1193 "b >= 0) }",
1194 "{ [m, n, ku, kl] }" },
1197 static int test_gist(struct isl_ctx *ctx)
1199 int i;
1200 const char *str;
1201 isl_basic_set *bset1, *bset2;
1202 isl_map *map1, *map2;
1203 int equal;
1205 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1206 int equal_input;
1207 isl_set *set1, *set2, *copy;
1209 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1210 set2 = isl_set_read_from_str(ctx, gist_tests[i].context);
1211 copy = isl_set_copy(set1);
1212 set1 = isl_set_gist(set1, set2);
1213 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1214 equal = isl_set_is_equal(set1, set2);
1215 isl_set_free(set1);
1216 isl_set_free(set2);
1217 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1218 equal_input = isl_set_is_equal(set1, copy);
1219 isl_set_free(set1);
1220 isl_set_free(copy);
1221 if (equal < 0 || equal_input < 0)
1222 return -1;
1223 if (!equal)
1224 isl_die(ctx, isl_error_unknown,
1225 "incorrect gist result", return -1);
1226 if (!equal_input)
1227 isl_die(ctx, isl_error_unknown,
1228 "gist modified input", return -1);
1231 test_gist_case(ctx, "gist1");
1233 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1234 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1235 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1236 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1237 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1238 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1239 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1240 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1241 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1242 bset1 = isl_basic_set_read_from_str(ctx, str);
1243 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1244 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1245 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1246 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1247 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1248 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1249 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1250 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1251 bset2 = isl_basic_set_read_from_str(ctx, str);
1252 bset1 = isl_basic_set_gist(bset1, bset2);
1253 assert(bset1 && bset1->n_div == 0);
1254 isl_basic_set_free(bset1);
1256 /* Check that the integer divisions of the second disjunct
1257 * do not spread to the first disjunct.
1259 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1260 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1261 "(exists (e0 = [(-1 + t1)/16], "
1262 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1263 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1264 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1265 "o0 <= 4294967295 and t1 <= -1)) }";
1266 map1 = isl_map_read_from_str(ctx, str);
1267 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1268 map2 = isl_map_read_from_str(ctx, str);
1269 map1 = isl_map_gist(map1, map2);
1270 if (!map1)
1271 return -1;
1272 if (map1->n != 1)
1273 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1274 isl_map_free(map1); return -1);
1275 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1276 isl_die(ctx, isl_error_unknown, "expecting single div",
1277 isl_map_free(map1); return -1);
1278 isl_map_free(map1);
1280 return 0;
1283 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1285 isl_set *set, *set2;
1286 int equal;
1287 int one;
1289 set = isl_set_read_from_str(ctx, str);
1290 set = isl_set_coalesce(set);
1291 set2 = isl_set_read_from_str(ctx, str);
1292 equal = isl_set_is_equal(set, set2);
1293 one = set && set->n == 1;
1294 isl_set_free(set);
1295 isl_set_free(set2);
1297 if (equal < 0)
1298 return -1;
1299 if (!equal)
1300 isl_die(ctx, isl_error_unknown,
1301 "coalesced set not equal to input", return -1);
1302 if (check_one && !one)
1303 isl_die(ctx, isl_error_unknown,
1304 "coalesced set should not be a union", return -1);
1306 return 0;
1309 /* Inputs for coalescing tests with unbounded wrapping.
1310 * "str" is a string representation of the input set.
1311 * "single_disjunct" is set if we expect the result to consist of
1312 * a single disjunct.
1314 struct {
1315 int single_disjunct;
1316 const char *str;
1317 } coalesce_unbounded_tests[] = {
1318 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1319 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1320 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1321 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1322 "-10 <= y <= 0}" },
1323 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1324 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1325 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1326 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1329 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1330 * option turned off.
1332 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1334 int i;
1335 int r = 0;
1336 int bounded;
1338 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1339 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1341 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1342 const char *str = coalesce_unbounded_tests[i].str;
1343 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1344 if (test_coalesce_set(ctx, str, check_one) >= 0)
1345 continue;
1346 r = -1;
1347 break;
1350 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1352 return r;
1355 /* Inputs for coalescing tests.
1356 * "str" is a string representation of the input set.
1357 * "single_disjunct" is set if we expect the result to consist of
1358 * a single disjunct.
1360 struct {
1361 int single_disjunct;
1362 const char *str;
1363 } coalesce_tests[] = {
1364 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1365 "y >= x & x >= 2 & 5 >= y }" },
1366 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1367 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1368 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1369 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1370 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1371 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1372 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1373 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1374 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1375 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1376 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1377 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1378 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1379 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1380 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1381 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1382 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1383 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1384 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1385 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1386 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1387 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1388 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1389 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1390 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1391 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1392 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1393 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1394 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1395 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1396 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1397 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1398 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1399 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1400 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1401 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1402 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1403 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1404 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1405 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1406 "[o0, o1, o2, o3, o4, o5, o6]] : "
1407 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1408 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1409 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1410 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1411 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1412 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1413 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1414 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1415 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1416 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1417 "o6 >= i3 + i6 - o3 and M >= 0 and "
1418 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1419 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1420 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1421 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1422 "(o0 = 0 and M >= 2 and N >= 3) or "
1423 "(M = 0 and o0 = 0 and N >= 3) }" },
1424 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1425 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1426 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1427 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1428 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1429 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1430 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1431 "(y = 3 and x = 1) }" },
1432 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1433 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1434 "i1 <= M and i3 <= M and i4 <= M) or "
1435 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1436 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1437 "i4 <= -1 + M) }" },
1438 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1439 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1440 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1441 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1442 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1443 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1444 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1445 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1446 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1447 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1448 { 0, "{ [a, b] : exists e : 2e = a and "
1449 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1450 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1451 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1452 "j >= 1 and j' <= i + j - i' and i >= 1; "
1453 "[1, 1, 1, 1] }" },
1454 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1455 "[i,j] : exists a : j = 3a }" },
1456 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1457 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1458 "a >= 3) or "
1459 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1460 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1461 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1462 "c <= 6 + 8a and a >= 3; "
1463 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1464 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1465 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1466 "[x,0] : 3 <= x <= 4 }" },
1467 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1468 "[x,0] : 4 <= x <= 5 }" },
1469 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1470 "[x,0] : 3 <= x <= 5 }" },
1471 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1472 "[x,0] : 3 <= x <= 4 }" },
1473 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1474 "i1 <= 0; "
1475 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1476 { 1, "{ [0,0]; [1,1] }" },
1477 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1478 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1479 "ii <= k;"
1480 "[k, 0, k] : k <= 6 and k >= 1 }" },
1481 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1482 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1483 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1484 { 1, "[n] -> { [1] : n >= 0;"
1485 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1486 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1487 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1488 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1489 "2e0 <= x and 2e0 <= n);"
1490 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1491 "n >= 0) }" },
1492 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1493 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1494 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1495 "t1 = 1 }" },
1496 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1497 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1498 "[0, 0] }" },
1499 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1500 "t1 >= 13 and t1 <= 16);"
1501 "[t1] : t1 <= 15 and t1 >= 12 }" },
1502 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
1503 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
1504 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
1505 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
1506 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
1507 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
1508 "i <= 4j + 2 }" },
1509 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
1510 "(exists (e0 : 3e0 = -2 + c0)) }" },
1511 { 0, "[n, b0, t0] -> "
1512 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1513 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1514 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1515 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1516 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
1517 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
1518 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
1519 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
1520 "(exists (e0 = floor((-32b0 + i4)/1048576), "
1521 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
1522 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
1523 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
1524 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
1525 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
1526 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
1527 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
1528 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
1529 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
1530 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
1531 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
1534 /* A specialized coalescing test case that would result
1535 * in a segmentation fault or a failed assertion in earlier versions of isl.
1537 static int test_coalesce_special(struct isl_ctx *ctx)
1539 const char *str;
1540 isl_map *map1, *map2;
1542 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1543 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
1544 "(y = 201 and o1 <= 239 and o1 >= 212) or "
1545 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
1546 "o1 <= 239 and o1 >= 212)) or "
1547 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
1548 "o1 <= 241 and o1 >= 240));"
1549 "[S_L220_OUT[] -> T7[]] -> "
1550 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
1551 "(y = 2 and o1 <= 241 and o1 >= 212) or "
1552 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
1553 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
1554 map1 = isl_map_read_from_str(ctx, str);
1555 map1 = isl_map_align_divs(map1);
1556 map1 = isl_map_coalesce(map1);
1557 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
1558 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
1559 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
1560 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
1561 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
1562 map2 = isl_map_read_from_str(ctx, str);
1563 map2 = isl_map_union(map2, map1);
1564 map2 = isl_map_align_divs(map2);
1565 map2 = isl_map_coalesce(map2);
1566 isl_map_free(map2);
1567 if (!map2)
1568 return -1;
1570 return 0;
1573 /* Test the functionality of isl_set_coalesce.
1574 * That is, check that the output is always equal to the input
1575 * and in some cases that the result consists of a single disjunct.
1577 static int test_coalesce(struct isl_ctx *ctx)
1579 int i;
1581 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1582 const char *str = coalesce_tests[i].str;
1583 int check_one = coalesce_tests[i].single_disjunct;
1584 if (test_coalesce_set(ctx, str, check_one) < 0)
1585 return -1;
1588 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1589 return -1;
1590 if (test_coalesce_special(ctx) < 0)
1591 return -1;
1593 return 0;
1596 void test_closure(struct isl_ctx *ctx)
1598 const char *str;
1599 isl_set *dom;
1600 isl_map *up, *right;
1601 isl_map *map, *map2;
1602 int exact;
1604 /* COCOA example 1 */
1605 map = isl_map_read_from_str(ctx,
1606 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1607 "1 <= i and i < n and 1 <= j and j < n or "
1608 "i2 = i + 1 and j2 = j - 1 and "
1609 "1 <= i and i < n and 2 <= j and j <= n }");
1610 map = isl_map_power(map, &exact);
1611 assert(exact);
1612 isl_map_free(map);
1614 /* COCOA example 1 */
1615 map = isl_map_read_from_str(ctx,
1616 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1617 "1 <= i and i < n and 1 <= j and j < n or "
1618 "i2 = i + 1 and j2 = j - 1 and "
1619 "1 <= i and i < n and 2 <= j and j <= n }");
1620 map = isl_map_transitive_closure(map, &exact);
1621 assert(exact);
1622 map2 = isl_map_read_from_str(ctx,
1623 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1624 "1 <= i and i < n and 1 <= j and j <= n and "
1625 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1626 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1627 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1628 assert(isl_map_is_equal(map, map2));
1629 isl_map_free(map2);
1630 isl_map_free(map);
1632 map = isl_map_read_from_str(ctx,
1633 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1634 " 0 <= y and y <= n }");
1635 map = isl_map_transitive_closure(map, &exact);
1636 map2 = isl_map_read_from_str(ctx,
1637 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1638 " 0 <= y and y <= n }");
1639 assert(isl_map_is_equal(map, map2));
1640 isl_map_free(map2);
1641 isl_map_free(map);
1643 /* COCOA example 2 */
1644 map = isl_map_read_from_str(ctx,
1645 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1646 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1647 "i2 = i + 2 and j2 = j - 2 and "
1648 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1649 map = isl_map_transitive_closure(map, &exact);
1650 assert(exact);
1651 map2 = isl_map_read_from_str(ctx,
1652 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1653 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1654 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1655 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1656 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1657 assert(isl_map_is_equal(map, map2));
1658 isl_map_free(map);
1659 isl_map_free(map2);
1661 /* COCOA Fig.2 left */
1662 map = isl_map_read_from_str(ctx,
1663 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1664 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1665 "j <= n or "
1666 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1667 "j <= 2 i - 3 and j <= n - 2 or "
1668 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1669 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1670 map = isl_map_transitive_closure(map, &exact);
1671 assert(exact);
1672 isl_map_free(map);
1674 /* COCOA Fig.2 right */
1675 map = isl_map_read_from_str(ctx,
1676 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1677 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1678 "j <= n or "
1679 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1680 "j <= 2 i - 4 and j <= n - 3 or "
1681 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1682 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1683 map = isl_map_power(map, &exact);
1684 assert(exact);
1685 isl_map_free(map);
1687 /* COCOA Fig.2 right */
1688 map = isl_map_read_from_str(ctx,
1689 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1690 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1691 "j <= n or "
1692 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1693 "j <= 2 i - 4 and j <= n - 3 or "
1694 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1695 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1696 map = isl_map_transitive_closure(map, &exact);
1697 assert(exact);
1698 map2 = isl_map_read_from_str(ctx,
1699 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1700 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1701 "j <= n and 3 + i + 2 j <= 3 n and "
1702 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1703 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1704 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1705 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1706 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1707 assert(isl_map_is_equal(map, map2));
1708 isl_map_free(map2);
1709 isl_map_free(map);
1711 /* COCOA Fig.1 right */
1712 dom = isl_set_read_from_str(ctx,
1713 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1714 "2 x - 3 y + 3 >= 0 }");
1715 right = isl_map_read_from_str(ctx,
1716 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1717 up = isl_map_read_from_str(ctx,
1718 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1719 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1720 right = isl_map_intersect_range(right, isl_set_copy(dom));
1721 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1722 up = isl_map_intersect_range(up, dom);
1723 map = isl_map_union(up, right);
1724 map = isl_map_transitive_closure(map, &exact);
1725 assert(exact);
1726 map2 = isl_map_read_from_str(ctx,
1727 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1728 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1729 assert(isl_map_is_equal(map, map2));
1730 isl_map_free(map2);
1731 isl_map_free(map);
1733 /* COCOA Theorem 1 counter example */
1734 map = isl_map_read_from_str(ctx,
1735 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1736 "i2 = 1 and j2 = j or "
1737 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1738 map = isl_map_transitive_closure(map, &exact);
1739 assert(exact);
1740 isl_map_free(map);
1742 map = isl_map_read_from_str(ctx,
1743 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1744 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1745 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1746 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1747 map = isl_map_transitive_closure(map, &exact);
1748 assert(exact);
1749 isl_map_free(map);
1751 /* Kelly et al 1996, fig 12 */
1752 map = isl_map_read_from_str(ctx,
1753 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1754 "1 <= i,j,j+1 <= n or "
1755 "j = n and j2 = 1 and i2 = i + 1 and "
1756 "1 <= i,i+1 <= n }");
1757 map = isl_map_transitive_closure(map, &exact);
1758 assert(exact);
1759 map2 = isl_map_read_from_str(ctx,
1760 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1761 "1 <= i <= n and i = i2 or "
1762 "1 <= i < i2 <= n and 1 <= j <= n and "
1763 "1 <= j2 <= n }");
1764 assert(isl_map_is_equal(map, map2));
1765 isl_map_free(map2);
1766 isl_map_free(map);
1768 /* Omega's closure4 */
1769 map = isl_map_read_from_str(ctx,
1770 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1771 "1 <= x,y <= 10 or "
1772 "x2 = x + 1 and y2 = y and "
1773 "1 <= x <= 20 && 5 <= y <= 15 }");
1774 map = isl_map_transitive_closure(map, &exact);
1775 assert(exact);
1776 isl_map_free(map);
1778 map = isl_map_read_from_str(ctx,
1779 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1780 map = isl_map_transitive_closure(map, &exact);
1781 assert(!exact);
1782 map2 = isl_map_read_from_str(ctx,
1783 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1784 assert(isl_map_is_equal(map, map2));
1785 isl_map_free(map);
1786 isl_map_free(map2);
1788 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1789 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1790 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1791 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1792 map = isl_map_read_from_str(ctx, str);
1793 map = isl_map_transitive_closure(map, &exact);
1794 assert(exact);
1795 map2 = isl_map_read_from_str(ctx, str);
1796 assert(isl_map_is_equal(map, map2));
1797 isl_map_free(map);
1798 isl_map_free(map2);
1800 str = "{[0] -> [1]; [2] -> [3]}";
1801 map = isl_map_read_from_str(ctx, str);
1802 map = isl_map_transitive_closure(map, &exact);
1803 assert(exact);
1804 map2 = isl_map_read_from_str(ctx, str);
1805 assert(isl_map_is_equal(map, map2));
1806 isl_map_free(map);
1807 isl_map_free(map2);
1809 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1810 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1811 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1812 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1813 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1814 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1815 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1816 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1817 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1818 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1819 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1820 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1821 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1822 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1823 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1824 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1825 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1826 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1827 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1828 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1829 map = isl_map_read_from_str(ctx, str);
1830 map = isl_map_transitive_closure(map, NULL);
1831 assert(map);
1832 isl_map_free(map);
1835 void test_lex(struct isl_ctx *ctx)
1837 isl_space *dim;
1838 isl_map *map;
1840 dim = isl_space_set_alloc(ctx, 0, 0);
1841 map = isl_map_lex_le(dim);
1842 assert(!isl_map_is_empty(map));
1843 isl_map_free(map);
1846 static int test_lexmin(struct isl_ctx *ctx)
1848 int equal;
1849 const char *str;
1850 isl_basic_map *bmap;
1851 isl_map *map, *map2;
1852 isl_set *set;
1853 isl_set *set2;
1854 isl_pw_multi_aff *pma;
1856 str = "[p0, p1] -> { [] -> [] : "
1857 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1858 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1859 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1860 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1861 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1862 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1863 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1864 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1865 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1866 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1867 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1868 map = isl_map_read_from_str(ctx, str);
1869 map = isl_map_lexmin(map);
1870 isl_map_free(map);
1872 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1873 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1874 set = isl_set_read_from_str(ctx, str);
1875 set = isl_set_lexmax(set);
1876 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1877 set2 = isl_set_read_from_str(ctx, str);
1878 set = isl_set_intersect(set, set2);
1879 assert(!isl_set_is_empty(set));
1880 isl_set_free(set);
1882 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1883 map = isl_map_read_from_str(ctx, str);
1884 map = isl_map_lexmin(map);
1885 str = "{ [x] -> [5] : 6 <= x <= 8; "
1886 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1887 map2 = isl_map_read_from_str(ctx, str);
1888 assert(isl_map_is_equal(map, map2));
1889 isl_map_free(map);
1890 isl_map_free(map2);
1892 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1893 map = isl_map_read_from_str(ctx, str);
1894 map2 = isl_map_copy(map);
1895 map = isl_map_lexmin(map);
1896 assert(isl_map_is_equal(map, map2));
1897 isl_map_free(map);
1898 isl_map_free(map2);
1900 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1901 map = isl_map_read_from_str(ctx, str);
1902 map = isl_map_lexmin(map);
1903 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1904 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1905 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1906 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1907 map2 = isl_map_read_from_str(ctx, str);
1908 assert(isl_map_is_equal(map, map2));
1909 isl_map_free(map);
1910 isl_map_free(map2);
1912 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1913 " 8i' <= i and 8i' >= -7 + i }";
1914 bmap = isl_basic_map_read_from_str(ctx, str);
1915 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1916 map2 = isl_map_from_pw_multi_aff(pma);
1917 map = isl_map_from_basic_map(bmap);
1918 assert(isl_map_is_equal(map, map2));
1919 isl_map_free(map);
1920 isl_map_free(map2);
1922 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1923 map = isl_map_read_from_str(ctx, str);
1924 map = isl_map_lexmin(map);
1925 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1926 map2 = isl_map_read_from_str(ctx, str);
1927 assert(isl_map_is_equal(map, map2));
1928 isl_map_free(map);
1929 isl_map_free(map2);
1931 /* Check that empty pieces are properly combined. */
1932 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1933 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1934 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1935 map = isl_map_read_from_str(ctx, str);
1936 map = isl_map_lexmin(map);
1937 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1938 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1939 "x >= 4 }";
1940 map2 = isl_map_read_from_str(ctx, str);
1941 assert(isl_map_is_equal(map, map2));
1942 isl_map_free(map);
1943 isl_map_free(map2);
1945 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1946 " 8i' <= i and 8i' >= -7 + i }";
1947 set = isl_set_read_from_str(ctx, str);
1948 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1949 set2 = isl_set_from_pw_multi_aff(pma);
1950 equal = isl_set_is_equal(set, set2);
1951 isl_set_free(set);
1952 isl_set_free(set2);
1953 if (equal < 0)
1954 return -1;
1955 if (!equal)
1956 isl_die(ctx, isl_error_unknown,
1957 "unexpected difference between set and "
1958 "piecewise affine expression", return -1);
1960 return 0;
1963 /* Check that isl_set_min_val and isl_set_max_val compute the correct
1964 * result on non-convex inputs.
1966 static int test_min(struct isl_ctx *ctx)
1968 isl_set *set;
1969 isl_aff *aff;
1970 isl_val *val;
1971 int min_ok, max_ok;
1973 set = isl_set_read_from_str(ctx, "{ [-1]; [1] }");
1974 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x] }");
1975 val = isl_set_min_val(set, aff);
1976 min_ok = isl_val_is_negone(val);
1977 isl_val_free(val);
1978 val = isl_set_max_val(set, aff);
1979 max_ok = isl_val_is_one(val);
1980 isl_val_free(val);
1981 isl_aff_free(aff);
1982 isl_set_free(set);
1984 if (min_ok < 0 || max_ok < 0)
1985 return -1;
1986 if (!min_ok)
1987 isl_die(ctx, isl_error_unknown,
1988 "unexpected minimum", return -1);
1989 if (!max_ok)
1990 isl_die(ctx, isl_error_unknown,
1991 "unexpected maximum", return -1);
1993 return 0;
1996 struct must_may {
1997 isl_map *must;
1998 isl_map *may;
2001 static int collect_must_may(__isl_take isl_map *dep, int must,
2002 void *dep_user, void *user)
2004 struct must_may *mm = (struct must_may *)user;
2006 if (must)
2007 mm->must = isl_map_union(mm->must, dep);
2008 else
2009 mm->may = isl_map_union(mm->may, dep);
2011 return 0;
2014 static int common_space(void *first, void *second)
2016 int depth = *(int *)first;
2017 return 2 * depth;
2020 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2022 isl_map *map2;
2023 int equal;
2025 if (!map)
2026 return -1;
2028 map2 = isl_map_read_from_str(map->ctx, str);
2029 equal = isl_map_is_equal(map, map2);
2030 isl_map_free(map2);
2032 return equal;
2035 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2037 int equal;
2039 equal = map_is_equal(map, str);
2040 if (equal < 0)
2041 return -1;
2042 if (!equal)
2043 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2044 "result not as expected", return -1);
2045 return 0;
2048 void test_dep(struct isl_ctx *ctx)
2050 const char *str;
2051 isl_space *dim;
2052 isl_map *map;
2053 isl_access_info *ai;
2054 isl_flow *flow;
2055 int depth;
2056 struct must_may mm;
2058 depth = 3;
2060 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2061 map = isl_map_read_from_str(ctx, str);
2062 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2064 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2065 map = isl_map_read_from_str(ctx, str);
2066 ai = isl_access_info_add_source(ai, map, 1, &depth);
2068 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2069 map = isl_map_read_from_str(ctx, str);
2070 ai = isl_access_info_add_source(ai, map, 1, &depth);
2072 flow = isl_access_info_compute_flow(ai);
2073 dim = isl_space_alloc(ctx, 0, 3, 3);
2074 mm.must = isl_map_empty(isl_space_copy(dim));
2075 mm.may = isl_map_empty(dim);
2077 isl_flow_foreach(flow, collect_must_may, &mm);
2079 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2080 " [1,10,0] -> [2,5,0] }";
2081 assert(map_is_equal(mm.must, str));
2082 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2083 assert(map_is_equal(mm.may, str));
2085 isl_map_free(mm.must);
2086 isl_map_free(mm.may);
2087 isl_flow_free(flow);
2090 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2091 map = isl_map_read_from_str(ctx, str);
2092 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2094 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2095 map = isl_map_read_from_str(ctx, str);
2096 ai = isl_access_info_add_source(ai, map, 1, &depth);
2098 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2099 map = isl_map_read_from_str(ctx, str);
2100 ai = isl_access_info_add_source(ai, map, 0, &depth);
2102 flow = isl_access_info_compute_flow(ai);
2103 dim = isl_space_alloc(ctx, 0, 3, 3);
2104 mm.must = isl_map_empty(isl_space_copy(dim));
2105 mm.may = isl_map_empty(dim);
2107 isl_flow_foreach(flow, collect_must_may, &mm);
2109 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2110 assert(map_is_equal(mm.must, str));
2111 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2112 assert(map_is_equal(mm.may, str));
2114 isl_map_free(mm.must);
2115 isl_map_free(mm.may);
2116 isl_flow_free(flow);
2119 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2120 map = isl_map_read_from_str(ctx, str);
2121 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2123 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2124 map = isl_map_read_from_str(ctx, str);
2125 ai = isl_access_info_add_source(ai, map, 0, &depth);
2127 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2128 map = isl_map_read_from_str(ctx, str);
2129 ai = isl_access_info_add_source(ai, map, 0, &depth);
2131 flow = isl_access_info_compute_flow(ai);
2132 dim = isl_space_alloc(ctx, 0, 3, 3);
2133 mm.must = isl_map_empty(isl_space_copy(dim));
2134 mm.may = isl_map_empty(dim);
2136 isl_flow_foreach(flow, collect_must_may, &mm);
2138 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2139 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2140 assert(map_is_equal(mm.may, str));
2141 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2142 assert(map_is_equal(mm.must, str));
2144 isl_map_free(mm.must);
2145 isl_map_free(mm.may);
2146 isl_flow_free(flow);
2149 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2150 map = isl_map_read_from_str(ctx, str);
2151 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2153 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2154 map = isl_map_read_from_str(ctx, str);
2155 ai = isl_access_info_add_source(ai, map, 0, &depth);
2157 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2158 map = isl_map_read_from_str(ctx, str);
2159 ai = isl_access_info_add_source(ai, map, 0, &depth);
2161 flow = isl_access_info_compute_flow(ai);
2162 dim = isl_space_alloc(ctx, 0, 3, 3);
2163 mm.must = isl_map_empty(isl_space_copy(dim));
2164 mm.may = isl_map_empty(dim);
2166 isl_flow_foreach(flow, collect_must_may, &mm);
2168 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2169 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2170 assert(map_is_equal(mm.may, str));
2171 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2172 assert(map_is_equal(mm.must, str));
2174 isl_map_free(mm.must);
2175 isl_map_free(mm.may);
2176 isl_flow_free(flow);
2179 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2180 map = isl_map_read_from_str(ctx, str);
2181 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2183 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2184 map = isl_map_read_from_str(ctx, str);
2185 ai = isl_access_info_add_source(ai, map, 0, &depth);
2187 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2188 map = isl_map_read_from_str(ctx, str);
2189 ai = isl_access_info_add_source(ai, map, 0, &depth);
2191 flow = isl_access_info_compute_flow(ai);
2192 dim = isl_space_alloc(ctx, 0, 3, 3);
2193 mm.must = isl_map_empty(isl_space_copy(dim));
2194 mm.may = isl_map_empty(dim);
2196 isl_flow_foreach(flow, collect_must_may, &mm);
2198 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2199 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2200 assert(map_is_equal(mm.may, str));
2201 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2202 assert(map_is_equal(mm.must, str));
2204 isl_map_free(mm.must);
2205 isl_map_free(mm.may);
2206 isl_flow_free(flow);
2209 depth = 5;
2211 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2212 map = isl_map_read_from_str(ctx, str);
2213 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2215 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2216 map = isl_map_read_from_str(ctx, str);
2217 ai = isl_access_info_add_source(ai, map, 1, &depth);
2219 flow = isl_access_info_compute_flow(ai);
2220 dim = isl_space_alloc(ctx, 0, 5, 5);
2221 mm.must = isl_map_empty(isl_space_copy(dim));
2222 mm.may = isl_map_empty(dim);
2224 isl_flow_foreach(flow, collect_must_may, &mm);
2226 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2227 assert(map_is_equal(mm.must, str));
2228 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2229 assert(map_is_equal(mm.may, str));
2231 isl_map_free(mm.must);
2232 isl_map_free(mm.may);
2233 isl_flow_free(flow);
2236 /* Check that the dependence analysis proceeds without errors.
2237 * Earlier versions of isl would break down during the analysis
2238 * due to the use of the wrong spaces.
2240 static int test_flow(isl_ctx *ctx)
2242 const char *str;
2243 isl_union_map *access, *schedule;
2244 isl_union_map *must_dep, *may_dep;
2245 int r;
2247 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2248 access = isl_union_map_read_from_str(ctx, str);
2249 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2250 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2251 "S2[] -> [1,0,0,0]; "
2252 "S3[] -> [-1,0,0,0] }";
2253 schedule = isl_union_map_read_from_str(ctx, str);
2254 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2255 isl_union_map_copy(access), schedule,
2256 &must_dep, &may_dep, NULL, NULL);
2257 isl_union_map_free(may_dep);
2258 isl_union_map_free(must_dep);
2260 return r;
2263 struct {
2264 const char *map;
2265 int sv;
2266 } sv_tests[] = {
2267 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2268 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2269 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2270 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2271 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2272 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2273 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2274 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2275 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2278 int test_sv(isl_ctx *ctx)
2280 isl_union_map *umap;
2281 int i;
2282 int sv;
2284 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2285 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2286 sv = isl_union_map_is_single_valued(umap);
2287 isl_union_map_free(umap);
2288 if (sv < 0)
2289 return -1;
2290 if (sv_tests[i].sv && !sv)
2291 isl_die(ctx, isl_error_internal,
2292 "map not detected as single valued", return -1);
2293 if (!sv_tests[i].sv && sv)
2294 isl_die(ctx, isl_error_internal,
2295 "map detected as single valued", return -1);
2298 return 0;
2301 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
2303 isl_map *map;
2305 map = isl_map_read_from_str(ctx, str);
2306 if (bijective)
2307 assert(isl_map_is_bijective(map));
2308 else
2309 assert(!isl_map_is_bijective(map));
2310 isl_map_free(map);
2313 void test_bijective(struct isl_ctx *ctx)
2315 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
2316 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
2317 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
2318 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
2319 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
2320 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
2321 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
2322 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
2323 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
2324 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
2325 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
2326 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
2327 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
2330 /* Inputs for isl_pw_qpolynomial_gist tests.
2331 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2333 struct {
2334 const char *pwqp;
2335 const char *set;
2336 const char *gist;
2337 } pwqp_gist_tests[] = {
2338 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2339 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2340 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2341 "{ [i] -> -1/2 + 1/2 * i }" },
2342 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2345 static int test_pwqp(struct isl_ctx *ctx)
2347 int i;
2348 const char *str;
2349 isl_set *set;
2350 isl_pw_qpolynomial *pwqp1, *pwqp2;
2351 int equal;
2353 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2354 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2356 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2357 isl_dim_in, 1, 1);
2359 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2360 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2362 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2364 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2366 isl_pw_qpolynomial_free(pwqp1);
2368 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2369 str = pwqp_gist_tests[i].pwqp;
2370 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2371 str = pwqp_gist_tests[i].set;
2372 set = isl_set_read_from_str(ctx, str);
2373 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2374 str = pwqp_gist_tests[i].gist;
2375 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2376 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2377 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2378 isl_pw_qpolynomial_free(pwqp1);
2380 if (equal < 0)
2381 return -1;
2382 if (!equal)
2383 isl_die(ctx, isl_error_unknown,
2384 "unexpected result", return -1);
2387 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2388 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2389 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2390 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2392 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2394 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2396 isl_pw_qpolynomial_free(pwqp1);
2398 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2399 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2400 str = "{ [x] -> x }";
2401 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2403 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2405 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2407 isl_pw_qpolynomial_free(pwqp1);
2409 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2410 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2411 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2412 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2413 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2414 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2415 isl_pw_qpolynomial_free(pwqp1);
2417 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2418 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2419 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2420 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2421 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2422 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2423 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2424 isl_pw_qpolynomial_free(pwqp1);
2425 isl_pw_qpolynomial_free(pwqp2);
2426 if (equal < 0)
2427 return -1;
2428 if (!equal)
2429 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2431 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2432 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2433 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2434 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2435 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2436 isl_val_one(ctx));
2437 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2438 isl_pw_qpolynomial_free(pwqp1);
2439 isl_pw_qpolynomial_free(pwqp2);
2440 if (equal < 0)
2441 return -1;
2442 if (!equal)
2443 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2445 return 0;
2448 void test_split_periods(isl_ctx *ctx)
2450 const char *str;
2451 isl_pw_qpolynomial *pwqp;
2453 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2454 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2455 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2456 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2458 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2459 assert(pwqp);
2461 isl_pw_qpolynomial_free(pwqp);
2464 void test_union(isl_ctx *ctx)
2466 const char *str;
2467 isl_union_set *uset1, *uset2;
2468 isl_union_map *umap1, *umap2;
2470 str = "{ [i] : 0 <= i <= 1 }";
2471 uset1 = isl_union_set_read_from_str(ctx, str);
2472 str = "{ [1] -> [0] }";
2473 umap1 = isl_union_map_read_from_str(ctx, str);
2475 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2476 assert(isl_union_map_is_equal(umap1, umap2));
2478 isl_union_map_free(umap1);
2479 isl_union_map_free(umap2);
2481 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2482 umap1 = isl_union_map_read_from_str(ctx, str);
2483 str = "{ A[i]; B[i] }";
2484 uset1 = isl_union_set_read_from_str(ctx, str);
2486 uset2 = isl_union_map_domain(umap1);
2488 assert(isl_union_set_is_equal(uset1, uset2));
2490 isl_union_set_free(uset1);
2491 isl_union_set_free(uset2);
2494 void test_bound(isl_ctx *ctx)
2496 const char *str;
2497 isl_pw_qpolynomial *pwqp;
2498 isl_pw_qpolynomial_fold *pwf;
2500 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2501 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2502 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2503 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
2504 isl_pw_qpolynomial_fold_free(pwf);
2506 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2507 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2508 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2509 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
2510 isl_pw_qpolynomial_fold_free(pwf);
2513 void test_lift(isl_ctx *ctx)
2515 const char *str;
2516 isl_basic_map *bmap;
2517 isl_basic_set *bset;
2519 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2520 bset = isl_basic_set_read_from_str(ctx, str);
2521 bset = isl_basic_set_lift(bset);
2522 bmap = isl_basic_map_from_range(bset);
2523 bset = isl_basic_map_domain(bmap);
2524 isl_basic_set_free(bset);
2527 struct {
2528 const char *set1;
2529 const char *set2;
2530 int subset;
2531 } subset_tests[] = {
2532 { "{ [112, 0] }",
2533 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2534 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2535 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2536 { "{ [65] }",
2537 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2538 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2539 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2540 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2541 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2542 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2543 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2544 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2545 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2546 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2547 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2548 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2549 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2550 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
2551 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
2552 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
2553 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
2554 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
2555 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
2556 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
2557 "4e0 <= -57 + i0 + i1)) or "
2558 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
2559 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
2560 "4e0 >= -61 + i0 + i1)) or "
2561 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
2564 static int test_subset(isl_ctx *ctx)
2566 int i;
2567 isl_set *set1, *set2;
2568 int subset;
2570 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2571 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2572 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2573 subset = isl_set_is_subset(set1, set2);
2574 isl_set_free(set1);
2575 isl_set_free(set2);
2576 if (subset < 0)
2577 return -1;
2578 if (subset != subset_tests[i].subset)
2579 isl_die(ctx, isl_error_unknown,
2580 "incorrect subset result", return -1);
2583 return 0;
2586 struct {
2587 const char *minuend;
2588 const char *subtrahend;
2589 const char *difference;
2590 } subtract_domain_tests[] = {
2591 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2592 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2593 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2596 static int test_subtract(isl_ctx *ctx)
2598 int i;
2599 isl_union_map *umap1, *umap2;
2600 isl_union_pw_multi_aff *upma1, *upma2;
2601 isl_union_set *uset;
2602 int equal;
2604 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2605 umap1 = isl_union_map_read_from_str(ctx,
2606 subtract_domain_tests[i].minuend);
2607 uset = isl_union_set_read_from_str(ctx,
2608 subtract_domain_tests[i].subtrahend);
2609 umap2 = isl_union_map_read_from_str(ctx,
2610 subtract_domain_tests[i].difference);
2611 umap1 = isl_union_map_subtract_domain(umap1, uset);
2612 equal = isl_union_map_is_equal(umap1, umap2);
2613 isl_union_map_free(umap1);
2614 isl_union_map_free(umap2);
2615 if (equal < 0)
2616 return -1;
2617 if (!equal)
2618 isl_die(ctx, isl_error_unknown,
2619 "incorrect subtract domain result", return -1);
2622 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2623 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
2624 subtract_domain_tests[i].minuend);
2625 uset = isl_union_set_read_from_str(ctx,
2626 subtract_domain_tests[i].subtrahend);
2627 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
2628 subtract_domain_tests[i].difference);
2629 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
2630 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
2631 isl_union_pw_multi_aff_free(upma1);
2632 isl_union_pw_multi_aff_free(upma2);
2633 if (equal < 0)
2634 return -1;
2635 if (!equal)
2636 isl_die(ctx, isl_error_unknown,
2637 "incorrect subtract domain result", return -1);
2640 return 0;
2643 int test_factorize(isl_ctx *ctx)
2645 const char *str;
2646 isl_basic_set *bset;
2647 isl_factorizer *f;
2649 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2650 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2651 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2652 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2653 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2654 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2655 "3i5 >= -2i0 - i2 + 3i4 }";
2656 bset = isl_basic_set_read_from_str(ctx, str);
2657 f = isl_basic_set_factorizer(bset);
2658 isl_basic_set_free(bset);
2659 isl_factorizer_free(f);
2660 if (!f)
2661 isl_die(ctx, isl_error_unknown,
2662 "failed to construct factorizer", return -1);
2664 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2665 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2666 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2667 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2668 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2669 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2670 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2671 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2672 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2673 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2674 bset = isl_basic_set_read_from_str(ctx, str);
2675 f = isl_basic_set_factorizer(bset);
2676 isl_basic_set_free(bset);
2677 isl_factorizer_free(f);
2678 if (!f)
2679 isl_die(ctx, isl_error_unknown,
2680 "failed to construct factorizer", return -1);
2682 return 0;
2685 static int check_injective(__isl_take isl_map *map, void *user)
2687 int *injective = user;
2689 *injective = isl_map_is_injective(map);
2690 isl_map_free(map);
2692 if (*injective < 0 || !*injective)
2693 return -1;
2695 return 0;
2698 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2699 const char *r, const char *s, int tilable, int parallel)
2701 int i;
2702 isl_union_set *D;
2703 isl_union_map *W, *R, *S;
2704 isl_union_map *empty;
2705 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2706 isl_union_map *validity, *proximity, *coincidence;
2707 isl_union_map *schedule;
2708 isl_union_map *test;
2709 isl_union_set *delta;
2710 isl_union_set *domain;
2711 isl_set *delta_set;
2712 isl_set *slice;
2713 isl_set *origin;
2714 isl_schedule_constraints *sc;
2715 isl_schedule *sched;
2716 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2718 D = isl_union_set_read_from_str(ctx, d);
2719 W = isl_union_map_read_from_str(ctx, w);
2720 R = isl_union_map_read_from_str(ctx, r);
2721 S = isl_union_map_read_from_str(ctx, s);
2723 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2724 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2726 empty = isl_union_map_empty(isl_union_map_get_space(S));
2727 isl_union_map_compute_flow(isl_union_map_copy(R),
2728 isl_union_map_copy(W), empty,
2729 isl_union_map_copy(S),
2730 &dep_raw, NULL, NULL, NULL);
2731 isl_union_map_compute_flow(isl_union_map_copy(W),
2732 isl_union_map_copy(W),
2733 isl_union_map_copy(R),
2734 isl_union_map_copy(S),
2735 &dep_waw, &dep_war, NULL, NULL);
2737 dep = isl_union_map_union(dep_waw, dep_war);
2738 dep = isl_union_map_union(dep, dep_raw);
2739 validity = isl_union_map_copy(dep);
2740 coincidence = isl_union_map_copy(dep);
2741 proximity = isl_union_map_copy(dep);
2743 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
2744 sc = isl_schedule_constraints_set_validity(sc, validity);
2745 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
2746 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2747 sched = isl_schedule_constraints_compute_schedule(sc);
2748 schedule = isl_schedule_get_map(sched);
2749 isl_schedule_free(sched);
2750 isl_union_map_free(W);
2751 isl_union_map_free(R);
2752 isl_union_map_free(S);
2754 is_injection = 1;
2755 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2757 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2758 is_complete = isl_union_set_is_subset(D, domain);
2759 isl_union_set_free(D);
2760 isl_union_set_free(domain);
2762 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2763 test = isl_union_map_apply_range(test, dep);
2764 test = isl_union_map_apply_range(test, schedule);
2766 delta = isl_union_map_deltas(test);
2767 if (isl_union_set_n_set(delta) == 0) {
2768 is_tilable = 1;
2769 is_parallel = 1;
2770 is_nonneg = 1;
2771 isl_union_set_free(delta);
2772 } else {
2773 delta_set = isl_set_from_union_set(delta);
2775 slice = isl_set_universe(isl_set_get_space(delta_set));
2776 for (i = 0; i < tilable; ++i)
2777 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2778 is_tilable = isl_set_is_subset(delta_set, slice);
2779 isl_set_free(slice);
2781 slice = isl_set_universe(isl_set_get_space(delta_set));
2782 for (i = 0; i < parallel; ++i)
2783 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2784 is_parallel = isl_set_is_subset(delta_set, slice);
2785 isl_set_free(slice);
2787 origin = isl_set_universe(isl_set_get_space(delta_set));
2788 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2789 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2791 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2792 delta_set = isl_set_lexmin(delta_set);
2794 is_nonneg = isl_set_is_equal(delta_set, origin);
2796 isl_set_free(origin);
2797 isl_set_free(delta_set);
2800 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2801 is_injection < 0 || is_complete < 0)
2802 return -1;
2803 if (!is_complete)
2804 isl_die(ctx, isl_error_unknown,
2805 "generated schedule incomplete", return -1);
2806 if (!is_injection)
2807 isl_die(ctx, isl_error_unknown,
2808 "generated schedule not injective on each statement",
2809 return -1);
2810 if (!is_nonneg)
2811 isl_die(ctx, isl_error_unknown,
2812 "negative dependences in generated schedule",
2813 return -1);
2814 if (!is_tilable)
2815 isl_die(ctx, isl_error_unknown,
2816 "generated schedule not as tilable as expected",
2817 return -1);
2818 if (!is_parallel)
2819 isl_die(ctx, isl_error_unknown,
2820 "generated schedule not as parallel as expected",
2821 return -1);
2823 return 0;
2826 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2827 const char *domain, const char *validity, const char *proximity)
2829 isl_union_set *dom;
2830 isl_union_map *dep;
2831 isl_union_map *prox;
2832 isl_schedule_constraints *sc;
2833 isl_schedule *schedule;
2834 isl_union_map *sched;
2836 dom = isl_union_set_read_from_str(ctx, domain);
2837 dep = isl_union_map_read_from_str(ctx, validity);
2838 prox = isl_union_map_read_from_str(ctx, proximity);
2839 sc = isl_schedule_constraints_on_domain(dom);
2840 sc = isl_schedule_constraints_set_validity(sc, dep);
2841 sc = isl_schedule_constraints_set_proximity(sc, prox);
2842 schedule = isl_schedule_constraints_compute_schedule(sc);
2843 sched = isl_schedule_get_map(schedule);
2844 isl_schedule_free(schedule);
2846 return sched;
2849 /* Check that a schedule can be constructed on the given domain
2850 * with the given validity and proximity constraints.
2852 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2853 const char *validity, const char *proximity)
2855 isl_union_map *sched;
2857 sched = compute_schedule(ctx, domain, validity, proximity);
2858 if (!sched)
2859 return -1;
2861 isl_union_map_free(sched);
2862 return 0;
2865 int test_special_schedule(isl_ctx *ctx, const char *domain,
2866 const char *validity, const char *proximity, const char *expected_sched)
2868 isl_union_map *sched1, *sched2;
2869 int equal;
2871 sched1 = compute_schedule(ctx, domain, validity, proximity);
2872 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2874 equal = isl_union_map_is_equal(sched1, sched2);
2875 isl_union_map_free(sched1);
2876 isl_union_map_free(sched2);
2878 if (equal < 0)
2879 return -1;
2880 if (!equal)
2881 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2882 return -1);
2884 return 0;
2887 /* Check that the schedule map is properly padded, even after being
2888 * reconstructed from the band forest.
2890 static int test_padded_schedule(isl_ctx *ctx)
2892 const char *str;
2893 isl_union_set *D;
2894 isl_union_map *validity, *proximity;
2895 isl_schedule_constraints *sc;
2896 isl_schedule *sched;
2897 isl_union_map *map1, *map2;
2898 isl_band_list *list;
2899 int equal;
2901 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2902 D = isl_union_set_read_from_str(ctx, str);
2903 validity = isl_union_map_empty(isl_union_set_get_space(D));
2904 proximity = isl_union_map_copy(validity);
2905 sc = isl_schedule_constraints_on_domain(D);
2906 sc = isl_schedule_constraints_set_validity(sc, validity);
2907 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2908 sched = isl_schedule_constraints_compute_schedule(sc);
2909 map1 = isl_schedule_get_map(sched);
2910 list = isl_schedule_get_band_forest(sched);
2911 isl_band_list_free(list);
2912 map2 = isl_schedule_get_map(sched);
2913 isl_schedule_free(sched);
2914 equal = isl_union_map_is_equal(map1, map2);
2915 isl_union_map_free(map1);
2916 isl_union_map_free(map2);
2918 if (equal < 0)
2919 return -1;
2920 if (!equal)
2921 isl_die(ctx, isl_error_unknown,
2922 "reconstructed schedule map not the same as original",
2923 return -1);
2925 return 0;
2928 /* Input for testing of schedule construction based on
2929 * conditional constraints.
2931 * domain is the iteration domain
2932 * flow are the flow dependences, which determine the validity and
2933 * proximity constraints
2934 * condition are the conditions on the conditional validity constraints
2935 * conditional_validity are the conditional validity constraints
2936 * outer_band_n is the expected number of members in the outer band
2938 struct {
2939 const char *domain;
2940 const char *flow;
2941 const char *condition;
2942 const char *conditional_validity;
2943 int outer_band_n;
2944 } live_range_tests[] = {
2945 /* Contrived example that illustrates that we need to keep
2946 * track of tagged condition dependences and
2947 * tagged conditional validity dependences
2948 * in isl_sched_edge separately.
2949 * In particular, the conditional validity constraints on A
2950 * cannot be satisfied,
2951 * but they can be ignored because there are no corresponding
2952 * condition constraints. However, we do have an additional
2953 * conditional validity constraint that maps to the same
2954 * dependence relation
2955 * as the condition constraint on B. If we did not make a distinction
2956 * between tagged condition and tagged conditional validity
2957 * dependences, then we
2958 * could end up treating this shared dependence as an condition
2959 * constraint on A, forcing a localization of the conditions,
2960 * which is impossible.
2962 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
2963 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
2964 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
2965 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2966 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2967 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
2970 /* TACO 2013 Fig. 7 */
2971 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2972 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2973 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2974 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
2975 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
2976 "0 <= i < n and 0 <= j < n - 1 }",
2977 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
2978 "0 <= i < n and 0 <= j < j' < n;"
2979 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
2980 "0 <= i < i' < n and 0 <= j,j' < n;"
2981 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
2982 "0 <= i,j,j' < n and j < j' }",
2985 /* TACO 2013 Fig. 7, without tags */
2986 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2987 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2988 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2989 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2990 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2991 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
2992 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
2993 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
2996 /* TACO 2013 Fig. 12 */
2997 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
2998 "S3[i,3] : 0 <= i <= 1 }",
2999 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
3000 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
3001 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
3002 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
3003 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3004 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
3005 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
3006 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
3007 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
3008 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
3009 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
3014 /* Test schedule construction based on conditional constraints.
3015 * In particular, check the number of members in the outer band
3016 * as an indication of whether tiling is possible or not.
3018 static int test_conditional_schedule_constraints(isl_ctx *ctx)
3020 int i;
3021 isl_union_set *domain;
3022 isl_union_map *condition;
3023 isl_union_map *flow;
3024 isl_union_map *validity;
3025 isl_schedule_constraints *sc;
3026 isl_schedule *schedule;
3027 isl_band_list *list;
3028 isl_band *band;
3029 int n_member;
3031 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
3032 domain = isl_union_set_read_from_str(ctx,
3033 live_range_tests[i].domain);
3034 flow = isl_union_map_read_from_str(ctx,
3035 live_range_tests[i].flow);
3036 condition = isl_union_map_read_from_str(ctx,
3037 live_range_tests[i].condition);
3038 validity = isl_union_map_read_from_str(ctx,
3039 live_range_tests[i].conditional_validity);
3040 sc = isl_schedule_constraints_on_domain(domain);
3041 sc = isl_schedule_constraints_set_validity(sc,
3042 isl_union_map_copy(flow));
3043 sc = isl_schedule_constraints_set_proximity(sc, flow);
3044 sc = isl_schedule_constraints_set_conditional_validity(sc,
3045 condition, validity);
3046 schedule = isl_schedule_constraints_compute_schedule(sc);
3047 list = isl_schedule_get_band_forest(schedule);
3048 band = isl_band_list_get_band(list, 0);
3049 n_member = isl_band_n_member(band);
3050 isl_band_free(band);
3051 isl_band_list_free(list);
3052 isl_schedule_free(schedule);
3054 if (!schedule)
3055 return -1;
3056 if (n_member != live_range_tests[i].outer_band_n)
3057 isl_die(ctx, isl_error_unknown,
3058 "unexpected number of members in outer band",
3059 return -1);
3061 return 0;
3064 int test_schedule(isl_ctx *ctx)
3066 const char *D, *W, *R, *V, *P, *S;
3068 /* Handle resulting schedule with zero bands. */
3069 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
3070 return -1;
3072 /* Jacobi */
3073 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
3074 W = "{ S1[t,i] -> a[t,i] }";
3075 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
3076 "S1[t,i] -> a[t-1,i+1] }";
3077 S = "{ S1[t,i] -> [t,i] }";
3078 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3079 return -1;
3081 /* Fig. 5 of CC2008 */
3082 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
3083 "j <= -1 + N }";
3084 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
3085 "j >= 2 and j <= -1 + N }";
3086 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
3087 "j >= 2 and j <= -1 + N; "
3088 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
3089 "j >= 2 and j <= -1 + N }";
3090 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3091 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3092 return -1;
3094 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
3095 W = "{ S1[i] -> a[i] }";
3096 R = "{ S2[i] -> a[i+1] }";
3097 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3098 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3099 return -1;
3101 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
3102 W = "{ S1[i] -> a[i] }";
3103 R = "{ S2[i] -> a[9-i] }";
3104 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3105 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3106 return -1;
3108 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
3109 W = "{ S1[i] -> a[i] }";
3110 R = "[N] -> { S2[i] -> a[N-1-i] }";
3111 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3112 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3113 return -1;
3115 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
3116 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
3117 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
3118 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
3119 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3120 return -1;
3122 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3123 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
3124 R = "{ S2[i,j] -> a[i-1,j] }";
3125 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3126 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3127 return -1;
3129 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3130 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
3131 R = "{ S2[i,j] -> a[i,j-1] }";
3132 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3133 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3134 return -1;
3136 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
3137 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
3138 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
3139 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
3140 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
3141 "S_0[] -> [0, 0, 0] }";
3142 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
3143 return -1;
3144 ctx->opt->schedule_parametric = 0;
3145 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3146 return -1;
3147 ctx->opt->schedule_parametric = 1;
3149 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
3150 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
3151 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
3152 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
3153 "S4[i] -> a[i,N] }";
3154 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
3155 "S4[i] -> [4,i,0] }";
3156 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3157 return -1;
3159 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
3160 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3161 "j <= N }";
3162 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3163 "j <= N; "
3164 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
3165 "j <= N }";
3166 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3167 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3168 return -1;
3170 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
3171 " S_2[t] : t >= 0 and t <= -1 + N; "
3172 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
3173 "i <= -1 + N }";
3174 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
3175 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
3176 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
3177 "i >= 0 and i <= -1 + N }";
3178 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
3179 "i >= 0 and i <= -1 + N; "
3180 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
3181 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
3182 " S_0[t] -> [0, t, 0] }";
3184 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3185 return -1;
3186 ctx->opt->schedule_parametric = 0;
3187 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3188 return -1;
3189 ctx->opt->schedule_parametric = 1;
3191 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
3192 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
3193 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
3194 return -1;
3196 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
3197 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
3198 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
3199 "j >= 0 and j <= -1 + N; "
3200 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3201 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
3202 "j >= 0 and j <= -1 + N; "
3203 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3204 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
3205 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3206 return -1;
3208 D = "{ S_0[i] : i >= 0 }";
3209 W = "{ S_0[i] -> a[i] : i >= 0 }";
3210 R = "{ S_0[i] -> a[0] : i >= 0 }";
3211 S = "{ S_0[i] -> [0, i, 0] }";
3212 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3213 return -1;
3215 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
3216 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
3217 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
3218 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
3219 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3220 return -1;
3222 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
3223 "k <= -1 + n and k >= 0 }";
3224 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
3225 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
3226 "k <= -1 + n and k >= 0; "
3227 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
3228 "k <= -1 + n and k >= 0; "
3229 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
3230 "k <= -1 + n and k >= 0 }";
3231 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
3232 ctx->opt->schedule_outer_coincidence = 1;
3233 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3234 return -1;
3235 ctx->opt->schedule_outer_coincidence = 0;
3237 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3238 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3239 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3240 "Stmt_for_body24[i0, i1, 1, 0]:"
3241 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3242 "Stmt_for_body7[i0, i1, i2]:"
3243 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3244 "i2 <= 7 }";
3246 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3247 "Stmt_for_body24[1, i1, i2, i3]:"
3248 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3249 "i2 >= 1;"
3250 "Stmt_for_body24[0, i1, i2, i3] -> "
3251 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3252 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3253 "i3 >= 0;"
3254 "Stmt_for_body24[0, i1, i2, i3] ->"
3255 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3256 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3257 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3258 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3259 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3260 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3261 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3262 "i1 <= 6 and i1 >= 0;"
3263 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3264 "Stmt_for_body7[i0, i1, i2] -> "
3265 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3266 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3267 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3268 "Stmt_for_body7[i0, i1, i2] -> "
3269 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3270 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3271 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3272 P = V;
3273 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3274 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3275 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3277 if (test_special_schedule(ctx, D, V, P, S) < 0)
3278 return -1;
3280 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3281 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3282 "j >= 1 and j <= 7;"
3283 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3284 "j >= 1 and j <= 8 }";
3285 P = "{ }";
3286 S = "{ S_0[i, j] -> [i + j, j] }";
3287 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3288 if (test_special_schedule(ctx, D, V, P, S) < 0)
3289 return -1;
3290 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3292 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3293 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3294 "j >= 0 and j <= -1 + i }";
3295 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3296 "i <= -1 + N and j >= 0;"
3297 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3298 "i <= -2 + N }";
3299 P = "{ }";
3300 S = "{ S_0[i, j] -> [i, j] }";
3301 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3302 if (test_special_schedule(ctx, D, V, P, S) < 0)
3303 return -1;
3304 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3306 /* Test both algorithms on a case with only proximity dependences. */
3307 D = "{ S[i,j] : 0 <= i <= 10 }";
3308 V = "{ }";
3309 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3310 S = "{ S[i, j] -> [j, i] }";
3311 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3312 if (test_special_schedule(ctx, D, V, P, S) < 0)
3313 return -1;
3314 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3315 if (test_special_schedule(ctx, D, V, P, S) < 0)
3316 return -1;
3318 D = "{ A[a]; B[] }";
3319 V = "{}";
3320 P = "{ A[a] -> B[] }";
3321 if (test_has_schedule(ctx, D, V, P) < 0)
3322 return -1;
3324 if (test_padded_schedule(ctx) < 0)
3325 return -1;
3327 /* Check that check for progress is not confused by rational
3328 * solution.
3330 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3331 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3332 "i0 <= -2 + N; "
3333 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3334 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3335 P = "{}";
3336 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3337 if (test_has_schedule(ctx, D, V, P) < 0)
3338 return -1;
3339 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3341 /* Check that we allow schedule rows that are only non-trivial
3342 * on some full-dimensional domains.
3344 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
3345 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
3346 "S1[j] -> S2[1] : 0 <= j <= 1 }";
3347 P = "{}";
3348 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3349 if (test_has_schedule(ctx, D, V, P) < 0)
3350 return -1;
3351 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3353 if (test_conditional_schedule_constraints(ctx) < 0)
3354 return -1;
3356 return 0;
3359 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3361 isl_union_map *umap;
3362 int test;
3364 umap = isl_union_map_read_from_str(ctx, str);
3365 test = isl_union_map_plain_is_injective(umap);
3366 isl_union_map_free(umap);
3367 if (test < 0)
3368 return -1;
3369 if (test == injective)
3370 return 0;
3371 if (injective)
3372 isl_die(ctx, isl_error_unknown,
3373 "map not detected as injective", return -1);
3374 else
3375 isl_die(ctx, isl_error_unknown,
3376 "map detected as injective", return -1);
3379 int test_injective(isl_ctx *ctx)
3381 const char *str;
3383 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
3384 return -1;
3385 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
3386 return -1;
3387 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
3388 return -1;
3389 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
3390 return -1;
3391 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
3392 return -1;
3393 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
3394 return -1;
3395 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
3396 return -1;
3397 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
3398 return -1;
3400 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
3401 if (test_plain_injective(ctx, str, 1))
3402 return -1;
3403 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
3404 if (test_plain_injective(ctx, str, 0))
3405 return -1;
3407 return 0;
3410 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
3412 isl_aff *aff2;
3413 int equal;
3415 if (!aff)
3416 return -1;
3418 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
3419 equal = isl_aff_plain_is_equal(aff, aff2);
3420 isl_aff_free(aff2);
3422 return equal;
3425 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
3427 int equal;
3429 equal = aff_plain_is_equal(aff, str);
3430 if (equal < 0)
3431 return -1;
3432 if (!equal)
3433 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
3434 "result not as expected", return -1);
3435 return 0;
3438 struct {
3439 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3440 __isl_take isl_aff *aff2);
3441 } aff_bin_op[] = {
3442 ['+'] = { &isl_aff_add },
3443 ['-'] = { &isl_aff_sub },
3444 ['*'] = { &isl_aff_mul },
3445 ['/'] = { &isl_aff_div },
3448 struct {
3449 const char *arg1;
3450 unsigned char op;
3451 const char *arg2;
3452 const char *res;
3453 } aff_bin_tests[] = {
3454 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
3455 "{ [i] -> [2i] }" },
3456 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
3457 "{ [i] -> [0] }" },
3458 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
3459 "{ [i] -> [2i] }" },
3460 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
3461 "{ [i] -> [2i] }" },
3462 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
3463 "{ [i] -> [i/2] }" },
3464 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
3465 "{ [i] -> [i] }" },
3466 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
3467 "{ [i] -> [NaN] }" },
3468 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
3469 "{ [i] -> [NaN] }" },
3470 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
3471 "{ [i] -> [NaN] }" },
3472 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
3473 "{ [i] -> [NaN] }" },
3474 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
3475 "{ [i] -> [NaN] }" },
3476 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
3477 "{ [i] -> [NaN] }" },
3478 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
3479 "{ [i] -> [NaN] }" },
3480 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
3481 "{ [i] -> [NaN] }" },
3482 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
3483 "{ [i] -> [NaN] }" },
3484 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
3485 "{ [i] -> [NaN] }" },
3486 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
3487 "{ [i] -> [NaN] }" },
3488 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
3489 "{ [i] -> [NaN] }" },
3492 /* Perform some basic tests of binary operations on isl_aff objects.
3494 static int test_bin_aff(isl_ctx *ctx)
3496 int i;
3497 isl_aff *aff1, *aff2, *res;
3498 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3499 __isl_take isl_aff *aff2);
3500 int ok;
3502 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
3503 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
3504 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
3505 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
3506 fn = aff_bin_op[aff_bin_tests[i].op].fn;
3507 aff1 = fn(aff1, aff2);
3508 if (isl_aff_is_nan(res))
3509 ok = isl_aff_is_nan(aff1);
3510 else
3511 ok = isl_aff_plain_is_equal(aff1, res);
3512 isl_aff_free(aff1);
3513 isl_aff_free(res);
3514 if (ok < 0)
3515 return -1;
3516 if (!ok)
3517 isl_die(ctx, isl_error_unknown,
3518 "unexpected result", return -1);
3521 return 0;
3524 struct {
3525 __isl_give isl_union_pw_multi_aff *(*fn)(
3526 __isl_take isl_union_pw_multi_aff *upma1,
3527 __isl_take isl_union_pw_multi_aff *upma2);
3528 const char *arg1;
3529 const char *arg2;
3530 const char *res;
3531 } upma_bin_tests[] = {
3532 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
3533 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
3534 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
3535 "{ B[x] -> [2] : x >= 0 }",
3536 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
3539 /* Perform some basic tests of binary operations on
3540 * isl_union_pw_multi_aff objects.
3542 static int test_bin_upma(isl_ctx *ctx)
3544 int i;
3545 isl_union_pw_multi_aff *upma1, *upma2, *res;
3546 int ok;
3548 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
3549 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3550 upma_bin_tests[i].arg1);
3551 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3552 upma_bin_tests[i].arg2);
3553 res = isl_union_pw_multi_aff_read_from_str(ctx,
3554 upma_bin_tests[i].res);
3555 upma1 = upma_bin_tests[i].fn(upma1, upma2);
3556 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
3557 isl_union_pw_multi_aff_free(upma1);
3558 isl_union_pw_multi_aff_free(res);
3559 if (ok < 0)
3560 return -1;
3561 if (!ok)
3562 isl_die(ctx, isl_error_unknown,
3563 "unexpected result", return -1);
3566 return 0;
3569 int test_aff(isl_ctx *ctx)
3571 const char *str;
3572 isl_set *set;
3573 isl_space *space;
3574 isl_local_space *ls;
3575 isl_aff *aff;
3576 int zero, equal;
3578 if (test_bin_aff(ctx) < 0)
3579 return -1;
3580 if (test_bin_upma(ctx) < 0)
3581 return -1;
3583 space = isl_space_set_alloc(ctx, 0, 1);
3584 ls = isl_local_space_from_space(space);
3585 aff = isl_aff_zero_on_domain(ls);
3587 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3588 aff = isl_aff_scale_down_ui(aff, 3);
3589 aff = isl_aff_floor(aff);
3590 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3591 aff = isl_aff_scale_down_ui(aff, 2);
3592 aff = isl_aff_floor(aff);
3593 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3595 str = "{ [10] }";
3596 set = isl_set_read_from_str(ctx, str);
3597 aff = isl_aff_gist(aff, set);
3599 aff = isl_aff_add_constant_si(aff, -16);
3600 zero = isl_aff_plain_is_zero(aff);
3601 isl_aff_free(aff);
3603 if (zero < 0)
3604 return -1;
3605 if (!zero)
3606 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3608 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
3609 aff = isl_aff_scale_down_ui(aff, 64);
3610 aff = isl_aff_floor(aff);
3611 equal = aff_check_plain_equal(aff, "{ [-1] }");
3612 isl_aff_free(aff);
3613 if (equal < 0)
3614 return -1;
3616 return 0;
3619 int test_dim_max(isl_ctx *ctx)
3621 int equal;
3622 const char *str;
3623 isl_set *set1, *set2;
3624 isl_set *set;
3625 isl_map *map;
3626 isl_pw_aff *pwaff;
3628 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
3629 set = isl_set_read_from_str(ctx, str);
3630 pwaff = isl_set_dim_max(set, 0);
3631 set1 = isl_set_from_pw_aff(pwaff);
3632 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
3633 set2 = isl_set_read_from_str(ctx, str);
3634 equal = isl_set_is_equal(set1, set2);
3635 isl_set_free(set1);
3636 isl_set_free(set2);
3637 if (equal < 0)
3638 return -1;
3639 if (!equal)
3640 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3642 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
3643 set = isl_set_read_from_str(ctx, str);
3644 pwaff = isl_set_dim_max(set, 0);
3645 set1 = isl_set_from_pw_aff(pwaff);
3646 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3647 set2 = isl_set_read_from_str(ctx, str);
3648 equal = isl_set_is_equal(set1, set2);
3649 isl_set_free(set1);
3650 isl_set_free(set2);
3651 if (equal < 0)
3652 return -1;
3653 if (!equal)
3654 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3656 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
3657 set = isl_set_read_from_str(ctx, str);
3658 pwaff = isl_set_dim_max(set, 0);
3659 set1 = isl_set_from_pw_aff(pwaff);
3660 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3661 set2 = isl_set_read_from_str(ctx, str);
3662 equal = isl_set_is_equal(set1, set2);
3663 isl_set_free(set1);
3664 isl_set_free(set2);
3665 if (equal < 0)
3666 return -1;
3667 if (!equal)
3668 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3670 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
3671 "0 <= i < N and 0 <= j < M }";
3672 map = isl_map_read_from_str(ctx, str);
3673 set = isl_map_range(map);
3675 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
3676 set1 = isl_set_from_pw_aff(pwaff);
3677 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
3678 set2 = isl_set_read_from_str(ctx, str);
3679 equal = isl_set_is_equal(set1, set2);
3680 isl_set_free(set1);
3681 isl_set_free(set2);
3683 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3684 set1 = isl_set_from_pw_aff(pwaff);
3685 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3686 set2 = isl_set_read_from_str(ctx, str);
3687 if (equal >= 0 && equal)
3688 equal = isl_set_is_equal(set1, set2);
3689 isl_set_free(set1);
3690 isl_set_free(set2);
3692 isl_set_free(set);
3694 if (equal < 0)
3695 return -1;
3696 if (!equal)
3697 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3699 /* Check that solutions are properly merged. */
3700 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3701 "c <= -1 + n - 4a - 2b and c >= -2b and "
3702 "4a >= -4 + n and c >= 0 }";
3703 set = isl_set_read_from_str(ctx, str);
3704 pwaff = isl_set_dim_min(set, 2);
3705 set1 = isl_set_from_pw_aff(pwaff);
3706 str = "[n] -> { [(0)] : n >= 1 }";
3707 set2 = isl_set_read_from_str(ctx, str);
3708 equal = isl_set_is_equal(set1, set2);
3709 isl_set_free(set1);
3710 isl_set_free(set2);
3712 if (equal < 0)
3713 return -1;
3714 if (!equal)
3715 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3717 /* Check that empty solution lie in the right space. */
3718 str = "[n] -> { [t,a] : 1 = 0 }";
3719 set = isl_set_read_from_str(ctx, str);
3720 pwaff = isl_set_dim_max(set, 0);
3721 set1 = isl_set_from_pw_aff(pwaff);
3722 str = "[n] -> { [t] : 1 = 0 }";
3723 set2 = isl_set_read_from_str(ctx, str);
3724 equal = isl_set_is_equal(set1, set2);
3725 isl_set_free(set1);
3726 isl_set_free(set2);
3728 if (equal < 0)
3729 return -1;
3730 if (!equal)
3731 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3733 return 0;
3736 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
3738 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
3739 const char *str)
3741 isl_ctx *ctx;
3742 isl_pw_multi_aff *pma2;
3743 int equal;
3745 if (!pma)
3746 return -1;
3748 ctx = isl_pw_multi_aff_get_ctx(pma);
3749 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3750 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
3751 isl_pw_multi_aff_free(pma2);
3753 return equal;
3756 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
3757 * represented by "str".
3759 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
3760 const char *str)
3762 int equal;
3764 equal = pw_multi_aff_plain_is_equal(pma, str);
3765 if (equal < 0)
3766 return -1;
3767 if (!equal)
3768 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
3769 "result not as expected", return -1);
3770 return 0;
3773 /* Basic test for isl_pw_multi_aff_product.
3775 * Check that multiple pieces are properly handled.
3777 static int test_product_pma(isl_ctx *ctx)
3779 int equal;
3780 const char *str;
3781 isl_pw_multi_aff *pma1, *pma2;
3783 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
3784 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3785 str = "{ C[] -> D[] }";
3786 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3787 pma1 = isl_pw_multi_aff_product(pma1, pma2);
3788 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
3789 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
3790 equal = pw_multi_aff_check_plain_equal(pma1, str);
3791 isl_pw_multi_aff_free(pma1);
3792 if (equal < 0)
3793 return -1;
3795 return 0;
3798 int test_product(isl_ctx *ctx)
3800 const char *str;
3801 isl_set *set;
3802 isl_union_set *uset1, *uset2;
3803 int ok;
3805 str = "{ A[i] }";
3806 set = isl_set_read_from_str(ctx, str);
3807 set = isl_set_product(set, isl_set_copy(set));
3808 ok = isl_set_is_wrapping(set);
3809 isl_set_free(set);
3810 if (ok < 0)
3811 return -1;
3812 if (!ok)
3813 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3815 str = "{ [] }";
3816 uset1 = isl_union_set_read_from_str(ctx, str);
3817 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3818 str = "{ [[] -> []] }";
3819 uset2 = isl_union_set_read_from_str(ctx, str);
3820 ok = isl_union_set_is_equal(uset1, uset2);
3821 isl_union_set_free(uset1);
3822 isl_union_set_free(uset2);
3823 if (ok < 0)
3824 return -1;
3825 if (!ok)
3826 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3828 if (test_product_pma(ctx) < 0)
3829 return -1;
3831 return 0;
3834 /* Check that two sets are not considered disjoint just because
3835 * they have a different set of (named) parameters.
3837 static int test_disjoint(isl_ctx *ctx)
3839 const char *str;
3840 isl_set *set, *set2;
3841 int disjoint;
3843 str = "[n] -> { [[]->[]] }";
3844 set = isl_set_read_from_str(ctx, str);
3845 str = "{ [[]->[]] }";
3846 set2 = isl_set_read_from_str(ctx, str);
3847 disjoint = isl_set_is_disjoint(set, set2);
3848 isl_set_free(set);
3849 isl_set_free(set2);
3850 if (disjoint < 0)
3851 return -1;
3852 if (disjoint)
3853 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3855 return 0;
3858 int test_equal(isl_ctx *ctx)
3860 const char *str;
3861 isl_set *set, *set2;
3862 int equal;
3864 str = "{ S_6[i] }";
3865 set = isl_set_read_from_str(ctx, str);
3866 str = "{ S_7[i] }";
3867 set2 = isl_set_read_from_str(ctx, str);
3868 equal = isl_set_is_equal(set, set2);
3869 isl_set_free(set);
3870 isl_set_free(set2);
3871 if (equal < 0)
3872 return -1;
3873 if (equal)
3874 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3876 return 0;
3879 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3880 enum isl_dim_type type, unsigned pos, int fixed)
3882 int test;
3884 test = isl_map_plain_is_fixed(map, type, pos, NULL);
3885 isl_map_free(map);
3886 if (test < 0)
3887 return -1;
3888 if (test == fixed)
3889 return 0;
3890 if (fixed)
3891 isl_die(ctx, isl_error_unknown,
3892 "map not detected as fixed", return -1);
3893 else
3894 isl_die(ctx, isl_error_unknown,
3895 "map detected as fixed", return -1);
3898 int test_fixed(isl_ctx *ctx)
3900 const char *str;
3901 isl_map *map;
3903 str = "{ [i] -> [i] }";
3904 map = isl_map_read_from_str(ctx, str);
3905 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3906 return -1;
3907 str = "{ [i] -> [1] }";
3908 map = isl_map_read_from_str(ctx, str);
3909 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3910 return -1;
3911 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3912 map = isl_map_read_from_str(ctx, str);
3913 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3914 return -1;
3915 map = isl_map_read_from_str(ctx, str);
3916 map = isl_map_neg(map);
3917 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3918 return -1;
3920 return 0;
3923 struct isl_vertices_test_data {
3924 const char *set;
3925 int n;
3926 const char *vertex[2];
3927 } vertices_tests[] = {
3928 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
3929 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
3930 { "{ A[t, i] : t = 14 and i = 1 }",
3931 1, { "{ A[14, 1] }" } },
3934 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
3936 static int find_vertex(__isl_take isl_vertex *vertex, void *user)
3938 struct isl_vertices_test_data *data = user;
3939 isl_ctx *ctx;
3940 isl_multi_aff *ma;
3941 isl_basic_set *bset;
3942 isl_pw_multi_aff *pma;
3943 int i;
3944 int equal;
3946 ctx = isl_vertex_get_ctx(vertex);
3947 bset = isl_vertex_get_domain(vertex);
3948 ma = isl_vertex_get_expr(vertex);
3949 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
3951 for (i = 0; i < data->n; ++i) {
3952 isl_pw_multi_aff *pma_i;
3954 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
3955 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
3956 isl_pw_multi_aff_free(pma_i);
3958 if (equal < 0 || equal)
3959 break;
3962 isl_pw_multi_aff_free(pma);
3963 isl_vertex_free(vertex);
3965 if (equal < 0)
3966 return -1;
3968 return equal ? 0 : - 1;
3971 int test_vertices(isl_ctx *ctx)
3973 int i;
3975 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
3976 isl_basic_set *bset;
3977 isl_vertices *vertices;
3978 int ok = 1;
3979 int n;
3981 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
3982 vertices = isl_basic_set_compute_vertices(bset);
3983 n = isl_vertices_get_n_vertices(vertices);
3984 if (vertices_tests[i].n != n)
3985 ok = 0;
3986 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
3987 &vertices_tests[i]) < 0)
3988 ok = 0;
3989 isl_vertices_free(vertices);
3990 isl_basic_set_free(bset);
3992 if (!vertices)
3993 return -1;
3994 if (!ok)
3995 isl_die(ctx, isl_error_unknown, "unexpected vertices",
3996 return -1);
3999 return 0;
4002 int test_union_pw(isl_ctx *ctx)
4004 int equal;
4005 const char *str;
4006 isl_union_set *uset;
4007 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
4009 str = "{ [x] -> x^2 }";
4010 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
4011 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
4012 uset = isl_union_pw_qpolynomial_domain(upwqp1);
4013 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
4014 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
4015 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
4016 isl_union_pw_qpolynomial_free(upwqp1);
4017 isl_union_pw_qpolynomial_free(upwqp2);
4018 if (equal < 0)
4019 return -1;
4020 if (!equal)
4021 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4023 return 0;
4026 int test_output(isl_ctx *ctx)
4028 char *s;
4029 const char *str;
4030 isl_pw_aff *pa;
4031 isl_printer *p;
4032 int equal;
4034 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
4035 pa = isl_pw_aff_read_from_str(ctx, str);
4037 p = isl_printer_to_str(ctx);
4038 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
4039 p = isl_printer_print_pw_aff(p, pa);
4040 s = isl_printer_get_str(p);
4041 isl_printer_free(p);
4042 isl_pw_aff_free(pa);
4043 if (!s)
4044 equal = -1;
4045 else
4046 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
4047 free(s);
4048 if (equal < 0)
4049 return -1;
4050 if (!equal)
4051 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
4053 return 0;
4056 int test_sample(isl_ctx *ctx)
4058 const char *str;
4059 isl_basic_set *bset1, *bset2;
4060 int empty, subset;
4062 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
4063 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
4064 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
4065 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
4066 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
4067 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
4068 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
4069 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
4070 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
4071 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
4072 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
4073 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
4074 "d - 1073741821e and "
4075 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
4076 "3j >= 1 - a + b + 2e and "
4077 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
4078 "3i <= 4 - a + 4b - e and "
4079 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
4080 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
4081 "c <= -1 + a and 3i >= -2 - a + 3e and "
4082 "1073741822e <= 1073741823 - a + 1073741822b + c and "
4083 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
4084 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
4085 "1073741823e >= 1 + 1073741823b - d and "
4086 "3i >= 1073741823b + c - 1073741823e - f and "
4087 "3i >= 1 + 2b + e + 3g }";
4088 bset1 = isl_basic_set_read_from_str(ctx, str);
4089 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
4090 empty = isl_basic_set_is_empty(bset2);
4091 subset = isl_basic_set_is_subset(bset2, bset1);
4092 isl_basic_set_free(bset1);
4093 isl_basic_set_free(bset2);
4094 if (empty < 0 || subset < 0)
4095 return -1;
4096 if (empty)
4097 isl_die(ctx, isl_error_unknown, "point not found", return -1);
4098 if (!subset)
4099 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
4101 return 0;
4104 int test_fixed_power(isl_ctx *ctx)
4106 const char *str;
4107 isl_map *map;
4108 isl_int exp;
4109 int equal;
4111 isl_int_init(exp);
4112 str = "{ [i] -> [i + 1] }";
4113 map = isl_map_read_from_str(ctx, str);
4114 isl_int_set_si(exp, 23);
4115 map = isl_map_fixed_power(map, exp);
4116 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
4117 isl_int_clear(exp);
4118 isl_map_free(map);
4119 if (equal < 0)
4120 return -1;
4122 return 0;
4125 int test_slice(isl_ctx *ctx)
4127 const char *str;
4128 isl_map *map;
4129 int equal;
4131 str = "{ [i] -> [j] }";
4132 map = isl_map_read_from_str(ctx, str);
4133 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
4134 equal = map_check_equal(map, "{ [i] -> [i] }");
4135 isl_map_free(map);
4136 if (equal < 0)
4137 return -1;
4139 str = "{ [i] -> [j] }";
4140 map = isl_map_read_from_str(ctx, str);
4141 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
4142 equal = map_check_equal(map, "{ [i] -> [j] }");
4143 isl_map_free(map);
4144 if (equal < 0)
4145 return -1;
4147 str = "{ [i] -> [j] }";
4148 map = isl_map_read_from_str(ctx, str);
4149 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
4150 equal = map_check_equal(map, "{ [i] -> [-i] }");
4151 isl_map_free(map);
4152 if (equal < 0)
4153 return -1;
4155 str = "{ [i] -> [j] }";
4156 map = isl_map_read_from_str(ctx, str);
4157 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
4158 equal = map_check_equal(map, "{ [0] -> [j] }");
4159 isl_map_free(map);
4160 if (equal < 0)
4161 return -1;
4163 str = "{ [i] -> [j] }";
4164 map = isl_map_read_from_str(ctx, str);
4165 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
4166 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
4167 isl_map_free(map);
4168 if (equal < 0)
4169 return -1;
4171 str = "{ [i] -> [j] }";
4172 map = isl_map_read_from_str(ctx, str);
4173 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
4174 equal = map_check_equal(map, "{ [i] -> [j] : false }");
4175 isl_map_free(map);
4176 if (equal < 0)
4177 return -1;
4179 return 0;
4182 int test_eliminate(isl_ctx *ctx)
4184 const char *str;
4185 isl_map *map;
4186 int equal;
4188 str = "{ [i] -> [j] : i = 2j }";
4189 map = isl_map_read_from_str(ctx, str);
4190 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
4191 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
4192 isl_map_free(map);
4193 if (equal < 0)
4194 return -1;
4196 return 0;
4199 /* Check that isl_set_dim_residue_class detects that the values of j
4200 * in the set below are all odd and that it does not detect any spurious
4201 * strides.
4203 static int test_residue_class(isl_ctx *ctx)
4205 const char *str;
4206 isl_set *set;
4207 isl_int m, r;
4208 int res;
4210 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
4211 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
4212 set = isl_set_read_from_str(ctx, str);
4213 isl_int_init(m);
4214 isl_int_init(r);
4215 res = isl_set_dim_residue_class(set, 1, &m, &r);
4216 if (res >= 0 &&
4217 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
4218 isl_die(ctx, isl_error_unknown, "incorrect residue class",
4219 res = -1);
4220 isl_int_clear(r);
4221 isl_int_clear(m);
4222 isl_set_free(set);
4224 return res;
4227 int test_align_parameters(isl_ctx *ctx)
4229 const char *str;
4230 isl_space *space;
4231 isl_multi_aff *ma1, *ma2;
4232 int equal;
4234 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
4235 ma1 = isl_multi_aff_read_from_str(ctx, str);
4237 space = isl_space_params_alloc(ctx, 1);
4238 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
4239 ma1 = isl_multi_aff_align_params(ma1, space);
4241 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
4242 ma2 = isl_multi_aff_read_from_str(ctx, str);
4244 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4246 isl_multi_aff_free(ma1);
4247 isl_multi_aff_free(ma2);
4249 if (equal < 0)
4250 return -1;
4251 if (!equal)
4252 isl_die(ctx, isl_error_unknown,
4253 "result not as expected", return -1);
4255 return 0;
4258 static int test_list(isl_ctx *ctx)
4260 isl_id *a, *b, *c, *d, *id;
4261 isl_id_list *list;
4262 int ok;
4264 a = isl_id_alloc(ctx, "a", NULL);
4265 b = isl_id_alloc(ctx, "b", NULL);
4266 c = isl_id_alloc(ctx, "c", NULL);
4267 d = isl_id_alloc(ctx, "d", NULL);
4269 list = isl_id_list_alloc(ctx, 4);
4270 list = isl_id_list_add(list, a);
4271 list = isl_id_list_add(list, b);
4272 list = isl_id_list_add(list, c);
4273 list = isl_id_list_add(list, d);
4274 list = isl_id_list_drop(list, 1, 1);
4276 if (isl_id_list_n_id(list) != 3) {
4277 isl_id_list_free(list);
4278 isl_die(ctx, isl_error_unknown,
4279 "unexpected number of elements in list", return -1);
4282 id = isl_id_list_get_id(list, 0);
4283 ok = id == a;
4284 isl_id_free(id);
4285 id = isl_id_list_get_id(list, 1);
4286 ok = ok && id == c;
4287 isl_id_free(id);
4288 id = isl_id_list_get_id(list, 2);
4289 ok = ok && id == d;
4290 isl_id_free(id);
4292 isl_id_list_free(list);
4294 if (!ok)
4295 isl_die(ctx, isl_error_unknown,
4296 "unexpected elements in list", return -1);
4298 return 0;
4301 const char *set_conversion_tests[] = {
4302 "[N] -> { [i] : N - 1 <= 2 i <= N }",
4303 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
4304 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4305 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4306 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
4309 /* Check that converting from isl_set to isl_pw_multi_aff and back
4310 * to isl_set produces the original isl_set.
4312 static int test_set_conversion(isl_ctx *ctx)
4314 int i;
4315 const char *str;
4316 isl_set *set1, *set2;
4317 isl_pw_multi_aff *pma;
4318 int equal;
4320 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
4321 str = set_conversion_tests[i];
4322 set1 = isl_set_read_from_str(ctx, str);
4323 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
4324 set2 = isl_set_from_pw_multi_aff(pma);
4325 equal = isl_set_is_equal(set1, set2);
4326 isl_set_free(set1);
4327 isl_set_free(set2);
4329 if (equal < 0)
4330 return -1;
4331 if (!equal)
4332 isl_die(ctx, isl_error_unknown, "bad conversion",
4333 return -1);
4336 return 0;
4339 /* Check that converting from isl_map to isl_pw_multi_aff and back
4340 * to isl_map produces the original isl_map.
4342 static int test_map_conversion(isl_ctx *ctx)
4344 const char *str;
4345 isl_map *map1, *map2;
4346 isl_pw_multi_aff *pma;
4347 int equal;
4349 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
4350 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
4351 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
4352 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
4353 "9e <= -2 - 2a) }";
4354 map1 = isl_map_read_from_str(ctx, str);
4355 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
4356 map2 = isl_map_from_pw_multi_aff(pma);
4357 equal = isl_map_is_equal(map1, map2);
4358 isl_map_free(map1);
4359 isl_map_free(map2);
4361 if (equal < 0)
4362 return -1;
4363 if (!equal)
4364 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
4366 return 0;
4369 static int test_conversion(isl_ctx *ctx)
4371 if (test_set_conversion(ctx) < 0)
4372 return -1;
4373 if (test_map_conversion(ctx) < 0)
4374 return -1;
4375 return 0;
4378 /* Check that isl_basic_map_curry does not modify input.
4380 static int test_curry(isl_ctx *ctx)
4382 const char *str;
4383 isl_basic_map *bmap1, *bmap2;
4384 int equal;
4386 str = "{ [A[] -> B[]] -> C[] }";
4387 bmap1 = isl_basic_map_read_from_str(ctx, str);
4388 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
4389 equal = isl_basic_map_is_equal(bmap1, bmap2);
4390 isl_basic_map_free(bmap1);
4391 isl_basic_map_free(bmap2);
4393 if (equal < 0)
4394 return -1;
4395 if (equal)
4396 isl_die(ctx, isl_error_unknown,
4397 "curried map should not be equal to original",
4398 return -1);
4400 return 0;
4403 struct {
4404 const char *set;
4405 const char *ma;
4406 const char *res;
4407 } preimage_tests[] = {
4408 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
4409 "{ A[j,i] -> B[i,j] }",
4410 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
4411 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4412 "{ A[a,b] -> B[a/2,b/6] }",
4413 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
4414 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4415 "{ A[a,b] -> B[a/2,b/6] }",
4416 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
4417 "exists i,j : a = 2 i and b = 6 j }" },
4418 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
4419 "[n] -> { : 0 <= n <= 100 }" },
4420 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4421 "{ A[a] -> B[2a] }",
4422 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
4423 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4424 "{ A[a] -> B[([a/2])] }",
4425 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
4426 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
4427 "{ A[a] -> B[a,a,a/3] }",
4428 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
4429 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
4430 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
4433 static int test_preimage_basic_set(isl_ctx *ctx)
4435 int i;
4436 isl_basic_set *bset1, *bset2;
4437 isl_multi_aff *ma;
4438 int equal;
4440 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
4441 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
4442 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
4443 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
4444 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
4445 equal = isl_basic_set_is_equal(bset1, bset2);
4446 isl_basic_set_free(bset1);
4447 isl_basic_set_free(bset2);
4448 if (equal < 0)
4449 return -1;
4450 if (!equal)
4451 isl_die(ctx, isl_error_unknown, "bad preimage",
4452 return -1);
4455 return 0;
4458 struct {
4459 const char *map;
4460 const char *ma;
4461 const char *res;
4462 } preimage_domain_tests[] = {
4463 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
4464 "{ A[j,i] -> B[i,j] }",
4465 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
4466 { "{ B[i] -> C[i]; D[i] -> E[i] }",
4467 "{ A[i] -> B[i + 1] }",
4468 "{ A[i] -> C[i + 1] }" },
4469 { "{ B[i] -> C[i]; B[i] -> E[i] }",
4470 "{ A[i] -> B[i + 1] }",
4471 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
4472 { "{ B[i] -> C[([i/2])] }",
4473 "{ A[i] -> B[2i] }",
4474 "{ A[i] -> C[i] }" },
4475 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
4476 "{ A[i] -> B[([i/5]), ([i/7])] }",
4477 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
4478 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
4479 "[N] -> { A[] -> B[([N/5])] }",
4480 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
4481 { "{ B[i] -> C[i] : exists a : i = 5 a }",
4482 "{ A[i] -> B[2i] }",
4483 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
4484 { "{ B[i] -> C[i] : exists a : i = 2 a; "
4485 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
4486 "{ A[i] -> B[2i] }",
4487 "{ A[i] -> C[2i] }" },
4490 static int test_preimage_union_map(isl_ctx *ctx)
4492 int i;
4493 isl_union_map *umap1, *umap2;
4494 isl_multi_aff *ma;
4495 int equal;
4497 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
4498 umap1 = isl_union_map_read_from_str(ctx,
4499 preimage_domain_tests[i].map);
4500 ma = isl_multi_aff_read_from_str(ctx,
4501 preimage_domain_tests[i].ma);
4502 umap2 = isl_union_map_read_from_str(ctx,
4503 preimage_domain_tests[i].res);
4504 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
4505 equal = isl_union_map_is_equal(umap1, umap2);
4506 isl_union_map_free(umap1);
4507 isl_union_map_free(umap2);
4508 if (equal < 0)
4509 return -1;
4510 if (!equal)
4511 isl_die(ctx, isl_error_unknown, "bad preimage",
4512 return -1);
4515 return 0;
4518 static int test_preimage(isl_ctx *ctx)
4520 if (test_preimage_basic_set(ctx) < 0)
4521 return -1;
4522 if (test_preimage_union_map(ctx) < 0)
4523 return -1;
4525 return 0;
4528 struct {
4529 const char *ma1;
4530 const char *ma;
4531 const char *res;
4532 } pullback_tests[] = {
4533 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
4534 "{ A[a,b] -> C[b + 2a] }" },
4535 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
4536 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
4537 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
4538 "{ A[a] -> C[(a)/6] }" },
4539 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
4540 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
4541 "{ A[a] -> C[(2a)/3] }" },
4542 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
4543 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
4544 "{ A[i,j] -> C[i + j, i + j] }"},
4545 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
4546 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
4547 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
4550 static int test_pullback(isl_ctx *ctx)
4552 int i;
4553 isl_multi_aff *ma1, *ma2;
4554 isl_multi_aff *ma;
4555 int equal;
4557 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
4558 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
4559 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
4560 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
4561 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
4562 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4563 isl_multi_aff_free(ma1);
4564 isl_multi_aff_free(ma2);
4565 if (equal < 0)
4566 return -1;
4567 if (!equal)
4568 isl_die(ctx, isl_error_unknown, "bad pullback",
4569 return -1);
4572 return 0;
4575 /* Check that negation is printed correctly.
4577 static int test_ast(isl_ctx *ctx)
4579 isl_ast_expr *expr, *expr1, *expr2, *expr3;
4580 char *str;
4581 int ok;
4583 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4584 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4585 expr = isl_ast_expr_add(expr1, expr2);
4586 expr = isl_ast_expr_neg(expr);
4587 str = isl_ast_expr_to_str(expr);
4588 ok = str ? !strcmp(str, "-(A + B)") : -1;
4589 free(str);
4590 isl_ast_expr_free(expr);
4592 if (ok < 0)
4593 return -1;
4594 if (!ok)
4595 isl_die(ctx, isl_error_unknown,
4596 "isl_ast_expr printed incorrectly", return -1);
4598 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4599 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4600 expr = isl_ast_expr_add(expr1, expr2);
4601 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
4602 expr = isl_ast_expr_sub(expr3, expr);
4603 str = isl_ast_expr_to_str(expr);
4604 ok = str ? !strcmp(str, "C - (A + B)") : -1;
4605 free(str);
4606 isl_ast_expr_free(expr);
4608 if (ok < 0)
4609 return -1;
4610 if (!ok)
4611 isl_die(ctx, isl_error_unknown,
4612 "isl_ast_expr printed incorrectly", return -1);
4614 return 0;
4617 /* Check that isl_ast_build_expr_from_set returns a valid expression
4618 * for an empty set. Note that isl_ast_build_expr_from_set getting
4619 * called on an empty set probably indicates a bug in the caller.
4621 static int test_ast_build(isl_ctx *ctx)
4623 isl_set *set;
4624 isl_ast_build *build;
4625 isl_ast_expr *expr;
4627 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4628 build = isl_ast_build_from_context(set);
4630 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
4631 expr = isl_ast_build_expr_from_set(build, set);
4633 isl_ast_expr_free(expr);
4634 isl_ast_build_free(build);
4636 if (!expr)
4637 return -1;
4639 return 0;
4642 /* Internal data structure for before_for and after_for callbacks.
4644 * depth is the current depth
4645 * before is the number of times before_for has been called
4646 * after is the number of times after_for has been called
4648 struct isl_test_codegen_data {
4649 int depth;
4650 int before;
4651 int after;
4654 /* This function is called before each for loop in the AST generated
4655 * from test_ast_gen1.
4657 * Increment the number of calls and the depth.
4658 * Check that the space returned by isl_ast_build_get_schedule_space
4659 * matches the target space of the schedule returned by
4660 * isl_ast_build_get_schedule.
4661 * Return an isl_id that is checked by the corresponding call
4662 * to after_for.
4664 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
4665 void *user)
4667 struct isl_test_codegen_data *data = user;
4668 isl_ctx *ctx;
4669 isl_space *space;
4670 isl_union_map *schedule;
4671 isl_union_set *uset;
4672 isl_set *set;
4673 int empty;
4674 char name[] = "d0";
4676 ctx = isl_ast_build_get_ctx(build);
4678 if (data->before >= 3)
4679 isl_die(ctx, isl_error_unknown,
4680 "unexpected number of for nodes", return NULL);
4681 if (data->depth >= 2)
4682 isl_die(ctx, isl_error_unknown,
4683 "unexpected depth", return NULL);
4685 snprintf(name, sizeof(name), "d%d", data->depth);
4686 data->before++;
4687 data->depth++;
4689 schedule = isl_ast_build_get_schedule(build);
4690 uset = isl_union_map_range(schedule);
4691 if (!uset)
4692 return NULL;
4693 if (isl_union_set_n_set(uset) != 1) {
4694 isl_union_set_free(uset);
4695 isl_die(ctx, isl_error_unknown,
4696 "expecting single range space", return NULL);
4699 space = isl_ast_build_get_schedule_space(build);
4700 set = isl_union_set_extract_set(uset, space);
4701 isl_union_set_free(uset);
4702 empty = isl_set_is_empty(set);
4703 isl_set_free(set);
4705 if (empty < 0)
4706 return NULL;
4707 if (empty)
4708 isl_die(ctx, isl_error_unknown,
4709 "spaces don't match", return NULL);
4711 return isl_id_alloc(ctx, name, NULL);
4714 /* This function is called after each for loop in the AST generated
4715 * from test_ast_gen1.
4717 * Increment the number of calls and decrement the depth.
4718 * Check that the annotation attached to the node matches
4719 * the isl_id returned by the corresponding call to before_for.
4721 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
4722 __isl_keep isl_ast_build *build, void *user)
4724 struct isl_test_codegen_data *data = user;
4725 isl_id *id;
4726 const char *name;
4727 int valid;
4729 data->after++;
4730 data->depth--;
4732 if (data->after > data->before)
4733 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4734 "mismatch in number of for nodes",
4735 return isl_ast_node_free(node));
4737 id = isl_ast_node_get_annotation(node);
4738 if (!id)
4739 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4740 "missing annotation", return isl_ast_node_free(node));
4742 name = isl_id_get_name(id);
4743 valid = name && atoi(name + 1) == data->depth;
4744 isl_id_free(id);
4746 if (!valid)
4747 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4748 "wrong annotation", return isl_ast_node_free(node));
4750 return node;
4753 /* Check that the before_each_for and after_each_for callbacks
4754 * are called for each for loop in the generated code,
4755 * that they are called in the right order and that the isl_id
4756 * returned from the before_each_for callback is attached to
4757 * the isl_ast_node passed to the corresponding after_each_for call.
4759 static int test_ast_gen1(isl_ctx *ctx)
4761 const char *str;
4762 isl_set *set;
4763 isl_union_map *schedule;
4764 isl_ast_build *build;
4765 isl_ast_node *tree;
4766 struct isl_test_codegen_data data;
4768 str = "[N] -> { : N >= 10 }";
4769 set = isl_set_read_from_str(ctx, str);
4770 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
4771 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
4772 schedule = isl_union_map_read_from_str(ctx, str);
4774 data.before = 0;
4775 data.after = 0;
4776 data.depth = 0;
4777 build = isl_ast_build_from_context(set);
4778 build = isl_ast_build_set_before_each_for(build,
4779 &before_for, &data);
4780 build = isl_ast_build_set_after_each_for(build,
4781 &after_for, &data);
4782 tree = isl_ast_build_ast_from_schedule(build, schedule);
4783 isl_ast_build_free(build);
4784 if (!tree)
4785 return -1;
4787 isl_ast_node_free(tree);
4789 if (data.before != 3 || data.after != 3)
4790 isl_die(ctx, isl_error_unknown,
4791 "unexpected number of for nodes", return -1);
4793 return 0;
4796 /* Check that the AST generator handles domains that are integrally disjoint
4797 * but not ratinoally disjoint.
4799 static int test_ast_gen2(isl_ctx *ctx)
4801 const char *str;
4802 isl_set *set;
4803 isl_union_map *schedule;
4804 isl_union_map *options;
4805 isl_ast_build *build;
4806 isl_ast_node *tree;
4808 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
4809 schedule = isl_union_map_read_from_str(ctx, str);
4810 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4811 build = isl_ast_build_from_context(set);
4813 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
4814 options = isl_union_map_read_from_str(ctx, str);
4815 build = isl_ast_build_set_options(build, options);
4816 tree = isl_ast_build_ast_from_schedule(build, schedule);
4817 isl_ast_build_free(build);
4818 if (!tree)
4819 return -1;
4820 isl_ast_node_free(tree);
4822 return 0;
4825 /* Increment *user on each call.
4827 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
4828 __isl_keep isl_ast_build *build, void *user)
4830 int *n = user;
4832 (*n)++;
4834 return node;
4837 /* Test that unrolling tries to minimize the number of instances.
4838 * In particular, for the schedule given below, make sure it generates
4839 * 3 nodes (rather than 101).
4841 static int test_ast_gen3(isl_ctx *ctx)
4843 const char *str;
4844 isl_set *set;
4845 isl_union_map *schedule;
4846 isl_union_map *options;
4847 isl_ast_build *build;
4848 isl_ast_node *tree;
4849 int n_domain = 0;
4851 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
4852 schedule = isl_union_map_read_from_str(ctx, str);
4853 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4855 str = "{ [i] -> unroll[0] }";
4856 options = isl_union_map_read_from_str(ctx, str);
4858 build = isl_ast_build_from_context(set);
4859 build = isl_ast_build_set_options(build, options);
4860 build = isl_ast_build_set_at_each_domain(build,
4861 &count_domains, &n_domain);
4862 tree = isl_ast_build_ast_from_schedule(build, schedule);
4863 isl_ast_build_free(build);
4864 if (!tree)
4865 return -1;
4867 isl_ast_node_free(tree);
4869 if (n_domain != 3)
4870 isl_die(ctx, isl_error_unknown,
4871 "unexpected number of for nodes", return -1);
4873 return 0;
4876 /* Check that if the ast_build_exploit_nested_bounds options is set,
4877 * we do not get an outer if node in the generated AST,
4878 * while we do get such an outer if node if the options is not set.
4880 static int test_ast_gen4(isl_ctx *ctx)
4882 const char *str;
4883 isl_set *set;
4884 isl_union_map *schedule;
4885 isl_ast_build *build;
4886 isl_ast_node *tree;
4887 enum isl_ast_node_type type;
4888 int enb;
4890 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4891 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4893 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4895 schedule = isl_union_map_read_from_str(ctx, str);
4896 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4897 build = isl_ast_build_from_context(set);
4898 tree = isl_ast_build_ast_from_schedule(build, schedule);
4899 isl_ast_build_free(build);
4900 if (!tree)
4901 return -1;
4903 type = isl_ast_node_get_type(tree);
4904 isl_ast_node_free(tree);
4906 if (type == isl_ast_node_if)
4907 isl_die(ctx, isl_error_unknown,
4908 "not expecting if node", return -1);
4910 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
4912 schedule = isl_union_map_read_from_str(ctx, str);
4913 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4914 build = isl_ast_build_from_context(set);
4915 tree = isl_ast_build_ast_from_schedule(build, schedule);
4916 isl_ast_build_free(build);
4917 if (!tree)
4918 return -1;
4920 type = isl_ast_node_get_type(tree);
4921 isl_ast_node_free(tree);
4923 if (type != isl_ast_node_if)
4924 isl_die(ctx, isl_error_unknown,
4925 "expecting if node", return -1);
4927 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
4929 return 0;
4932 /* This function is called for each leaf in the AST generated
4933 * from test_ast_gen5.
4935 * We finalize the AST generation by extending the outer schedule
4936 * with a zero-dimensional schedule. If this results in any for loops,
4937 * then this means that we did not pass along enough information
4938 * about the outer schedule to the inner AST generation.
4940 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
4941 void *user)
4943 isl_union_map *schedule, *extra;
4944 isl_ast_node *tree;
4946 schedule = isl_ast_build_get_schedule(build);
4947 extra = isl_union_map_copy(schedule);
4948 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
4949 schedule = isl_union_map_range_product(schedule, extra);
4950 tree = isl_ast_build_ast_from_schedule(build, schedule);
4951 isl_ast_build_free(build);
4953 if (!tree)
4954 return NULL;
4956 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
4957 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
4958 "code should not contain any for loop",
4959 return isl_ast_node_free(tree));
4961 return tree;
4964 /* Check that we do not lose any information when going back and
4965 * forth between internal and external schedule.
4967 * In particular, we create an AST where we unroll the only
4968 * non-constant dimension in the schedule. We therefore do
4969 * not expect any for loops in the AST. However, older versions
4970 * of isl would not pass along enough information about the outer
4971 * schedule when performing an inner code generation from a create_leaf
4972 * callback, resulting in the inner code generation producing a for loop.
4974 static int test_ast_gen5(isl_ctx *ctx)
4976 const char *str;
4977 isl_set *set;
4978 isl_union_map *schedule, *options;
4979 isl_ast_build *build;
4980 isl_ast_node *tree;
4982 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
4983 schedule = isl_union_map_read_from_str(ctx, str);
4985 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
4986 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
4987 options = 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 build = isl_ast_build_set_options(build, options);
4992 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
4993 tree = isl_ast_build_ast_from_schedule(build, schedule);
4994 isl_ast_build_free(build);
4995 isl_ast_node_free(tree);
4996 if (!tree)
4997 return -1;
4999 return 0;
5002 static int test_ast_gen(isl_ctx *ctx)
5004 if (test_ast_gen1(ctx) < 0)
5005 return -1;
5006 if (test_ast_gen2(ctx) < 0)
5007 return -1;
5008 if (test_ast_gen3(ctx) < 0)
5009 return -1;
5010 if (test_ast_gen4(ctx) < 0)
5011 return -1;
5012 if (test_ast_gen5(ctx) < 0)
5013 return -1;
5014 return 0;
5017 /* Check if dropping output dimensions from an isl_pw_multi_aff
5018 * works properly.
5020 static int test_pw_multi_aff(isl_ctx *ctx)
5022 const char *str;
5023 isl_pw_multi_aff *pma1, *pma2;
5024 int equal;
5026 str = "{ [i,j] -> [i+j, 4i-j] }";
5027 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
5028 str = "{ [i,j] -> [4i-j] }";
5029 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
5031 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
5033 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
5035 isl_pw_multi_aff_free(pma1);
5036 isl_pw_multi_aff_free(pma2);
5037 if (equal < 0)
5038 return -1;
5039 if (!equal)
5040 isl_die(ctx, isl_error_unknown,
5041 "expressions not equal", return -1);
5043 return 0;
5046 /* Check that we can properly parse multi piecewise affine expressions
5047 * where the piecewise affine expressions have different domains.
5049 static int test_multi_pw_aff(isl_ctx *ctx)
5051 const char *str;
5052 isl_set *dom, *dom2;
5053 isl_multi_pw_aff *mpa1, *mpa2;
5054 isl_pw_aff *pa;
5055 int equal;
5056 int equal_domain;
5058 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
5059 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
5060 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
5061 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
5062 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
5063 str = "{ [i] -> [(i : i > 0), 2i] }";
5064 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
5066 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
5068 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
5069 dom = isl_pw_aff_domain(pa);
5070 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
5071 dom2 = isl_pw_aff_domain(pa);
5072 equal_domain = isl_set_is_equal(dom, dom2);
5074 isl_set_free(dom);
5075 isl_set_free(dom2);
5076 isl_multi_pw_aff_free(mpa1);
5077 isl_multi_pw_aff_free(mpa2);
5079 if (equal < 0)
5080 return -1;
5081 if (!equal)
5082 isl_die(ctx, isl_error_unknown,
5083 "expressions not equal", return -1);
5085 if (equal_domain < 0)
5086 return -1;
5087 if (equal_domain)
5088 isl_die(ctx, isl_error_unknown,
5089 "domains unexpectedly equal", return -1);
5091 return 0;
5094 /* This is a regression test for a bug where isl_basic_map_simplify
5095 * would end up in an infinite loop. In particular, we construct
5096 * an empty basic set that is not obviously empty.
5097 * isl_basic_set_is_empty marks the basic set as empty.
5098 * After projecting out i3, the variable can be dropped completely,
5099 * but isl_basic_map_simplify refrains from doing so if the basic set
5100 * is empty and would end up in an infinite loop if it didn't test
5101 * explicitly for empty basic maps in the outer loop.
5103 static int test_simplify(isl_ctx *ctx)
5105 const char *str;
5106 isl_basic_set *bset;
5107 int empty;
5109 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
5110 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
5111 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
5112 "i3 >= i2 }";
5113 bset = isl_basic_set_read_from_str(ctx, str);
5114 empty = isl_basic_set_is_empty(bset);
5115 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
5116 isl_basic_set_free(bset);
5117 if (!bset)
5118 return -1;
5119 if (!empty)
5120 isl_die(ctx, isl_error_unknown,
5121 "basic set should be empty", return -1);
5123 return 0;
5126 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
5127 * with gbr context would fail to disable the use of the shifted tableau
5128 * when transferring equalities for the input to the context, resulting
5129 * in invalid sample values.
5131 static int test_partial_lexmin(isl_ctx *ctx)
5133 const char *str;
5134 isl_basic_set *bset;
5135 isl_basic_map *bmap;
5136 isl_map *map;
5138 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
5139 bmap = isl_basic_map_read_from_str(ctx, str);
5140 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
5141 bset = isl_basic_set_read_from_str(ctx, str);
5142 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
5143 isl_map_free(map);
5145 if (!map)
5146 return -1;
5148 return 0;
5151 /* Check that the variable compression performed on the existentially
5152 * quantified variables inside isl_basic_set_compute_divs is not confused
5153 * by the implicit equalities among the parameters.
5155 static int test_compute_divs(isl_ctx *ctx)
5157 const char *str;
5158 isl_basic_set *bset;
5159 isl_set *set;
5161 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
5162 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
5163 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
5164 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
5165 bset = isl_basic_set_read_from_str(ctx, str);
5166 set = isl_basic_set_compute_divs(bset);
5167 isl_set_free(set);
5168 if (!set)
5169 return -1;
5171 return 0;
5174 struct {
5175 const char *set;
5176 const char *dual;
5177 } coef_tests[] = {
5178 { "{ rat: [i] : 0 <= i <= 10 }",
5179 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
5180 { "{ rat: [i] : FALSE }",
5181 "{ rat: coefficients[[cst] -> [a]] }" },
5182 { "{ rat: [i] : }",
5183 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
5186 struct {
5187 const char *set;
5188 const char *dual;
5189 } sol_tests[] = {
5190 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
5191 "{ rat: [i] : 0 <= i <= 10 }" },
5192 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
5193 "{ rat: [i] }" },
5194 { "{ rat: coefficients[[cst] -> [a]] }",
5195 "{ rat: [i] : FALSE }" },
5198 /* Test the basic functionality of isl_basic_set_coefficients and
5199 * isl_basic_set_solutions.
5201 static int test_dual(isl_ctx *ctx)
5203 int i;
5205 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
5206 int equal;
5207 isl_basic_set *bset1, *bset2;
5209 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
5210 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
5211 bset1 = isl_basic_set_coefficients(bset1);
5212 equal = isl_basic_set_is_equal(bset1, bset2);
5213 isl_basic_set_free(bset1);
5214 isl_basic_set_free(bset2);
5215 if (equal < 0)
5216 return -1;
5217 if (!equal)
5218 isl_die(ctx, isl_error_unknown,
5219 "incorrect dual", return -1);
5222 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
5223 int equal;
5224 isl_basic_set *bset1, *bset2;
5226 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
5227 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
5228 bset1 = isl_basic_set_solutions(bset1);
5229 equal = isl_basic_set_is_equal(bset1, bset2);
5230 isl_basic_set_free(bset1);
5231 isl_basic_set_free(bset2);
5232 if (equal < 0)
5233 return -1;
5234 if (!equal)
5235 isl_die(ctx, isl_error_unknown,
5236 "incorrect dual", return -1);
5239 return 0;
5242 struct {
5243 const char *name;
5244 int (*fn)(isl_ctx *ctx);
5245 } tests [] = {
5246 { "dual", &test_dual },
5247 { "dependence analysis", &test_flow },
5248 { "val", &test_val },
5249 { "compute divs", &test_compute_divs },
5250 { "partial lexmin", &test_partial_lexmin },
5251 { "simplify", &test_simplify },
5252 { "curry", &test_curry },
5253 { "piecewise multi affine expressions", &test_pw_multi_aff },
5254 { "multi piecewise affine expressions", &test_multi_pw_aff },
5255 { "conversion", &test_conversion },
5256 { "list", &test_list },
5257 { "align parameters", &test_align_parameters },
5258 { "preimage", &test_preimage },
5259 { "pullback", &test_pullback },
5260 { "AST", &test_ast },
5261 { "AST build", &test_ast_build },
5262 { "AST generation", &test_ast_gen },
5263 { "eliminate", &test_eliminate },
5264 { "residue class", &test_residue_class },
5265 { "div", &test_div },
5266 { "slice", &test_slice },
5267 { "fixed power", &test_fixed_power },
5268 { "sample", &test_sample },
5269 { "output", &test_output },
5270 { "vertices", &test_vertices },
5271 { "fixed", &test_fixed },
5272 { "equal", &test_equal },
5273 { "disjoint", &test_disjoint },
5274 { "product", &test_product },
5275 { "dim_max", &test_dim_max },
5276 { "affine", &test_aff },
5277 { "injective", &test_injective },
5278 { "schedule", &test_schedule },
5279 { "union_pw", &test_union_pw },
5280 { "parse", &test_parse },
5281 { "single-valued", &test_sv },
5282 { "affine hull", &test_affine_hull },
5283 { "coalesce", &test_coalesce },
5284 { "factorize", &test_factorize },
5285 { "subset", &test_subset },
5286 { "subtract", &test_subtract },
5287 { "lexmin", &test_lexmin },
5288 { "min", &test_min },
5289 { "gist", &test_gist },
5290 { "piecewise quasi-polynomials", &test_pwqp },
5293 int main(int argc, char **argv)
5295 int i;
5296 struct isl_ctx *ctx;
5297 struct isl_options *options;
5299 srcdir = getenv("srcdir");
5300 assert(srcdir);
5302 options = isl_options_new_with_defaults();
5303 assert(options);
5304 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
5306 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
5307 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
5308 printf("%s\n", tests[i].name);
5309 if (tests[i].fn(ctx) < 0)
5310 goto error;
5312 test_lift(ctx);
5313 test_bound(ctx);
5314 test_union(ctx);
5315 test_split_periods(ctx);
5316 test_lex(ctx);
5317 test_bijective(ctx);
5318 test_dep(ctx);
5319 test_read(ctx);
5320 test_bounded(ctx);
5321 test_construction(ctx);
5322 test_dim(ctx);
5323 test_application(ctx);
5324 test_convex_hull(ctx);
5325 test_closure(ctx);
5326 isl_ctx_free(ctx);
5327 return 0;
5328 error:
5329 isl_ctx_free(ctx);
5330 return -1;