Revert "scheduler: also (optionally) bound coefficients computed by carry_dependences"
[isl.git] / isl_test.c
blobda9db4b5249de155fd3696396e24e2f49ee13ddf
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 int test_parse(struct isl_ctx *ctx)
109 isl_map *map, *map2;
110 const char *str, *str2;
112 str = "{ [i] -> [-i] }";
113 map = isl_map_read_from_str(ctx, str);
114 assert(map);
115 isl_map_free(map);
117 str = "{ A[i] -> L[([i/3])] }";
118 map = isl_map_read_from_str(ctx, str);
119 assert(map);
120 isl_map_free(map);
122 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
123 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
124 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
126 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
127 str2 = "{ [x, y] : 2y >= 6 - x }";
128 if (test_parse_map_equal(ctx, str, str2) < 0)
129 return -1;
131 if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
132 "{ [x,y] : x <= y, 2*y + 3 }") < 0)
133 return -1;
134 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
135 if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
136 str) < 0)
137 return -1;
139 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
140 map = isl_map_read_from_str(ctx, str);
141 str = "{ [new, old] -> [o0, o1] : "
142 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
143 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
144 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
145 map2 = isl_map_read_from_str(ctx, str);
146 assert(isl_map_is_equal(map, map2));
147 isl_map_free(map);
148 isl_map_free(map2);
150 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
151 map = isl_map_read_from_str(ctx, str);
152 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
153 map2 = isl_map_read_from_str(ctx, str);
154 assert(isl_map_is_equal(map, map2));
155 isl_map_free(map);
156 isl_map_free(map2);
158 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
159 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
160 if (test_parse_map_equal(ctx, str, str2) < 0)
161 return -1;
163 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
164 str2 = "{ [i,j] -> [min(i,j)] }";
165 if (test_parse_map_equal(ctx, str, str2) < 0)
166 return -1;
168 str = "{ [i,j] : i != j }";
169 str2 = "{ [i,j] : i < j or i > j }";
170 if (test_parse_map_equal(ctx, str, str2) < 0)
171 return -1;
173 str = "{ [i,j] : (i+1)*2 >= j }";
174 str2 = "{ [i, j] : j <= 2 + 2i }";
175 if (test_parse_map_equal(ctx, str, str2) < 0)
176 return -1;
178 str = "{ [i] -> [i > 0 ? 4 : 5] }";
179 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
180 if (test_parse_map_equal(ctx, str, str2) < 0)
181 return -1;
183 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
184 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
185 if (test_parse_map_equal(ctx, str, str2) < 0)
186 return -1;
188 str = "{ [x] : x >= 0 }";
189 str2 = "{ [x] : x-0 >= 0 }";
190 if (test_parse_map_equal(ctx, str, str2) < 0)
191 return -1;
193 str = "{ [i] : ((i > 10)) }";
194 str2 = "{ [i] : i >= 11 }";
195 if (test_parse_map_equal(ctx, str, str2) < 0)
196 return -1;
198 str = "{ [i] -> [0] }";
199 str2 = "{ [i] -> [0 * i] }";
200 if (test_parse_map_equal(ctx, str, str2) < 0)
201 return -1;
203 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
204 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
205 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
206 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
208 if (test_parse_map_equal(ctx, "{ [a] -> [b] : (not false) }",
209 "{ [a] -> [b] : true }") < 0)
210 return -1;
212 if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
213 "{ [i] : i <= 10 }") < 0)
214 return -1;
216 if (test_parse_map_equal(ctx, "{Sym=[n] [i] : i <= n }",
217 "[n] -> { [i] : i <= n }") < 0)
218 return -1;
220 if (test_parse_map_equal(ctx, "{ [*] }", "{ [a] }") < 0)
221 return -1;
223 if (test_parse_map_equal(ctx, "{ [i] : 2*floor(i/2) = i }",
224 "{ [i] : exists a : i = 2 a }") < 0)
225 return -1;
227 if (test_parse_map_equal(ctx, "{ [a] -> [b] : a = 5 implies b = 5 }",
228 "{ [a] -> [b] : a != 5 or b = 5 }") < 0)
229 return -1;
231 if (test_parse_map_equal(ctx, "{ [a] -> [a - 1 : a > 0] }",
232 "{ [a] -> [a - 1] : a > 0 }") < 0)
233 return -1;
234 if (test_parse_map_equal(ctx,
235 "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
236 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }") < 0)
237 return -1;
238 if (test_parse_map_equal(ctx,
239 "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
240 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
241 return -1;
242 if (test_parse_map_equal(ctx,
243 "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
244 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
245 return -1;
246 if (test_parse_map_equal(ctx,
247 "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
248 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
249 return -1;
250 if (test_parse_map_equal(ctx,
251 "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
252 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
253 return -1;
255 return 0;
258 void test_read(struct isl_ctx *ctx)
260 char *filename;
261 FILE *input;
262 struct isl_basic_set *bset1, *bset2;
263 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
265 filename = get_filename(ctx, "set", "omega");
266 assert(filename);
267 input = fopen(filename, "r");
268 assert(input);
270 bset1 = isl_basic_set_read_from_file(ctx, input);
271 bset2 = isl_basic_set_read_from_str(ctx, str);
273 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
275 isl_basic_set_free(bset1);
276 isl_basic_set_free(bset2);
277 free(filename);
279 fclose(input);
282 void test_bounded(struct isl_ctx *ctx)
284 isl_set *set;
285 int bounded;
287 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
288 bounded = isl_set_is_bounded(set);
289 assert(bounded);
290 isl_set_free(set);
292 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
293 bounded = isl_set_is_bounded(set);
294 assert(!bounded);
295 isl_set_free(set);
297 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
298 bounded = isl_set_is_bounded(set);
299 assert(!bounded);
300 isl_set_free(set);
303 /* Construct the basic set { [i] : 5 <= i <= N } */
304 void test_construction(struct isl_ctx *ctx)
306 isl_int v;
307 isl_space *dim;
308 isl_local_space *ls;
309 struct isl_basic_set *bset;
310 struct isl_constraint *c;
312 isl_int_init(v);
314 dim = isl_space_set_alloc(ctx, 1, 1);
315 bset = isl_basic_set_universe(isl_space_copy(dim));
316 ls = isl_local_space_from_space(dim);
318 c = isl_inequality_alloc(isl_local_space_copy(ls));
319 isl_int_set_si(v, -1);
320 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
321 isl_int_set_si(v, 1);
322 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
323 bset = isl_basic_set_add_constraint(bset, c);
325 c = isl_inequality_alloc(isl_local_space_copy(ls));
326 isl_int_set_si(v, 1);
327 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
328 isl_int_set_si(v, -5);
329 isl_constraint_set_constant(c, v);
330 bset = isl_basic_set_add_constraint(bset, c);
332 isl_local_space_free(ls);
333 isl_basic_set_free(bset);
335 isl_int_clear(v);
338 void test_dim(struct isl_ctx *ctx)
340 const char *str;
341 isl_map *map1, *map2;
343 map1 = isl_map_read_from_str(ctx,
344 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
345 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
346 map2 = isl_map_read_from_str(ctx,
347 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
348 assert(isl_map_is_equal(map1, map2));
349 isl_map_free(map2);
351 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
352 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
353 assert(isl_map_is_equal(map1, map2));
355 isl_map_free(map1);
356 isl_map_free(map2);
358 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
359 map1 = isl_map_read_from_str(ctx, str);
360 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
361 map2 = isl_map_read_from_str(ctx, str);
362 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
363 assert(isl_map_is_equal(map1, map2));
365 isl_map_free(map1);
366 isl_map_free(map2);
369 struct {
370 __isl_give isl_val *(*op)(__isl_take isl_val *v);
371 const char *arg;
372 const char *res;
373 } val_un_tests[] = {
374 { &isl_val_neg, "0", "0" },
375 { &isl_val_abs, "0", "0" },
376 { &isl_val_2exp, "0", "1" },
377 { &isl_val_floor, "0", "0" },
378 { &isl_val_ceil, "0", "0" },
379 { &isl_val_neg, "1", "-1" },
380 { &isl_val_neg, "-1", "1" },
381 { &isl_val_neg, "1/2", "-1/2" },
382 { &isl_val_neg, "-1/2", "1/2" },
383 { &isl_val_neg, "infty", "-infty" },
384 { &isl_val_neg, "-infty", "infty" },
385 { &isl_val_neg, "NaN", "NaN" },
386 { &isl_val_abs, "1", "1" },
387 { &isl_val_abs, "-1", "1" },
388 { &isl_val_abs, "1/2", "1/2" },
389 { &isl_val_abs, "-1/2", "1/2" },
390 { &isl_val_abs, "infty", "infty" },
391 { &isl_val_abs, "-infty", "infty" },
392 { &isl_val_abs, "NaN", "NaN" },
393 { &isl_val_floor, "1", "1" },
394 { &isl_val_floor, "-1", "-1" },
395 { &isl_val_floor, "1/2", "0" },
396 { &isl_val_floor, "-1/2", "-1" },
397 { &isl_val_floor, "infty", "infty" },
398 { &isl_val_floor, "-infty", "-infty" },
399 { &isl_val_floor, "NaN", "NaN" },
400 { &isl_val_ceil, "1", "1" },
401 { &isl_val_ceil, "-1", "-1" },
402 { &isl_val_ceil, "1/2", "1" },
403 { &isl_val_ceil, "-1/2", "0" },
404 { &isl_val_ceil, "infty", "infty" },
405 { &isl_val_ceil, "-infty", "-infty" },
406 { &isl_val_ceil, "NaN", "NaN" },
407 { &isl_val_2exp, "-3", "1/8" },
408 { &isl_val_2exp, "-1", "1/2" },
409 { &isl_val_2exp, "1", "2" },
410 { &isl_val_2exp, "2", "4" },
411 { &isl_val_2exp, "3", "8" },
414 /* Perform some basic tests of unary operations on isl_val objects.
416 static int test_un_val(isl_ctx *ctx)
418 int i;
419 isl_val *v, *res;
420 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
421 int ok;
423 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
424 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
425 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
426 fn = val_un_tests[i].op;
427 v = fn(v);
428 if (isl_val_is_nan(res))
429 ok = isl_val_is_nan(v);
430 else
431 ok = isl_val_eq(v, res);
432 isl_val_free(v);
433 isl_val_free(res);
434 if (ok < 0)
435 return -1;
436 if (!ok)
437 isl_die(ctx, isl_error_unknown,
438 "unexpected result", return -1);
441 return 0;
444 struct {
445 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
446 __isl_take isl_val *v2);
447 } val_bin_op[] = {
448 ['+'] = { &isl_val_add },
449 ['-'] = { &isl_val_sub },
450 ['*'] = { &isl_val_mul },
451 ['/'] = { &isl_val_div },
452 ['g'] = { &isl_val_gcd },
453 ['m'] = { &isl_val_min },
454 ['M'] = { &isl_val_max },
457 struct {
458 const char *arg1;
459 unsigned char op;
460 const char *arg2;
461 const char *res;
462 } val_bin_tests[] = {
463 { "0", '+', "0", "0" },
464 { "1", '+', "0", "1" },
465 { "1", '+', "1", "2" },
466 { "1", '-', "1", "0" },
467 { "1", '*', "1", "1" },
468 { "1", '/', "1", "1" },
469 { "2", '*', "3", "6" },
470 { "2", '*', "1/2", "1" },
471 { "2", '*', "1/3", "2/3" },
472 { "2/3", '*', "3/5", "2/5" },
473 { "2/3", '*', "7/5", "14/15" },
474 { "2", '/', "1/2", "4" },
475 { "-2", '/', "-1/2", "4" },
476 { "-2", '/', "1/2", "-4" },
477 { "2", '/', "-1/2", "-4" },
478 { "2", '/', "2", "1" },
479 { "2", '/', "3", "2/3" },
480 { "2/3", '/', "5/3", "2/5" },
481 { "2/3", '/', "5/7", "14/15" },
482 { "0", '/', "0", "NaN" },
483 { "42", '/', "0", "NaN" },
484 { "-42", '/', "0", "NaN" },
485 { "infty", '/', "0", "NaN" },
486 { "-infty", '/', "0", "NaN" },
487 { "NaN", '/', "0", "NaN" },
488 { "0", '/', "NaN", "NaN" },
489 { "42", '/', "NaN", "NaN" },
490 { "-42", '/', "NaN", "NaN" },
491 { "infty", '/', "NaN", "NaN" },
492 { "-infty", '/', "NaN", "NaN" },
493 { "NaN", '/', "NaN", "NaN" },
494 { "0", '/', "infty", "0" },
495 { "42", '/', "infty", "0" },
496 { "-42", '/', "infty", "0" },
497 { "infty", '/', "infty", "NaN" },
498 { "-infty", '/', "infty", "NaN" },
499 { "NaN", '/', "infty", "NaN" },
500 { "0", '/', "-infty", "0" },
501 { "42", '/', "-infty", "0" },
502 { "-42", '/', "-infty", "0" },
503 { "infty", '/', "-infty", "NaN" },
504 { "-infty", '/', "-infty", "NaN" },
505 { "NaN", '/', "-infty", "NaN" },
506 { "1", '-', "1/3", "2/3" },
507 { "1/3", '+', "1/2", "5/6" },
508 { "1/2", '+', "1/2", "1" },
509 { "3/4", '-', "1/4", "1/2" },
510 { "1/2", '-', "1/3", "1/6" },
511 { "infty", '+', "42", "infty" },
512 { "infty", '+', "infty", "infty" },
513 { "42", '+', "infty", "infty" },
514 { "infty", '-', "infty", "NaN" },
515 { "infty", '*', "infty", "infty" },
516 { "infty", '*', "-infty", "-infty" },
517 { "-infty", '*', "infty", "-infty" },
518 { "-infty", '*', "-infty", "infty" },
519 { "0", '*', "infty", "NaN" },
520 { "1", '*', "infty", "infty" },
521 { "infty", '*', "0", "NaN" },
522 { "infty", '*', "42", "infty" },
523 { "42", '-', "infty", "-infty" },
524 { "infty", '+', "-infty", "NaN" },
525 { "4", 'g', "6", "2" },
526 { "5", 'g', "6", "1" },
527 { "42", 'm', "3", "3" },
528 { "42", 'M', "3", "42" },
529 { "3", 'm', "42", "3" },
530 { "3", 'M', "42", "42" },
531 { "42", 'm', "infty", "42" },
532 { "42", 'M', "infty", "infty" },
533 { "42", 'm', "-infty", "-infty" },
534 { "42", 'M', "-infty", "42" },
535 { "42", 'm', "NaN", "NaN" },
536 { "42", 'M', "NaN", "NaN" },
537 { "infty", 'm', "-infty", "-infty" },
538 { "infty", 'M', "-infty", "infty" },
541 /* Perform some basic tests of binary operations on isl_val objects.
543 static int test_bin_val(isl_ctx *ctx)
545 int i;
546 isl_val *v1, *v2, *res;
547 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
548 __isl_take isl_val *v2);
549 int ok;
551 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
552 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
553 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
554 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
555 fn = val_bin_op[val_bin_tests[i].op].fn;
556 v1 = fn(v1, v2);
557 if (isl_val_is_nan(res))
558 ok = isl_val_is_nan(v1);
559 else
560 ok = isl_val_eq(v1, res);
561 isl_val_free(v1);
562 isl_val_free(res);
563 if (ok < 0)
564 return -1;
565 if (!ok)
566 isl_die(ctx, isl_error_unknown,
567 "unexpected result", return -1);
570 return 0;
573 /* Perform some basic tests on isl_val objects.
575 static int test_val(isl_ctx *ctx)
577 if (test_un_val(ctx) < 0)
578 return -1;
579 if (test_bin_val(ctx) < 0)
580 return -1;
581 return 0;
584 static int test_div(isl_ctx *ctx)
586 unsigned n;
587 const char *str;
588 int empty;
589 isl_int v;
590 isl_space *dim;
591 isl_set *set;
592 isl_local_space *ls;
593 struct isl_basic_set *bset;
594 struct isl_constraint *c;
596 isl_int_init(v);
598 /* test 1 */
599 dim = isl_space_set_alloc(ctx, 0, 3);
600 bset = isl_basic_set_universe(isl_space_copy(dim));
601 ls = isl_local_space_from_space(dim);
603 c = isl_equality_alloc(isl_local_space_copy(ls));
604 isl_int_set_si(v, -1);
605 isl_constraint_set_constant(c, v);
606 isl_int_set_si(v, 1);
607 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
608 isl_int_set_si(v, 3);
609 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
610 bset = isl_basic_set_add_constraint(bset, c);
612 c = isl_equality_alloc(isl_local_space_copy(ls));
613 isl_int_set_si(v, 1);
614 isl_constraint_set_constant(c, v);
615 isl_int_set_si(v, -1);
616 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
617 isl_int_set_si(v, 3);
618 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
619 bset = isl_basic_set_add_constraint(bset, c);
621 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
623 assert(bset && bset->n_div == 1);
624 isl_local_space_free(ls);
625 isl_basic_set_free(bset);
627 /* test 2 */
628 dim = isl_space_set_alloc(ctx, 0, 3);
629 bset = isl_basic_set_universe(isl_space_copy(dim));
630 ls = isl_local_space_from_space(dim);
632 c = isl_equality_alloc(isl_local_space_copy(ls));
633 isl_int_set_si(v, 1);
634 isl_constraint_set_constant(c, v);
635 isl_int_set_si(v, -1);
636 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
637 isl_int_set_si(v, 3);
638 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
639 bset = isl_basic_set_add_constraint(bset, c);
641 c = isl_equality_alloc(isl_local_space_copy(ls));
642 isl_int_set_si(v, -1);
643 isl_constraint_set_constant(c, v);
644 isl_int_set_si(v, 1);
645 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
646 isl_int_set_si(v, 3);
647 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
648 bset = isl_basic_set_add_constraint(bset, c);
650 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
652 assert(bset && bset->n_div == 1);
653 isl_local_space_free(ls);
654 isl_basic_set_free(bset);
656 /* test 3 */
657 dim = isl_space_set_alloc(ctx, 0, 3);
658 bset = isl_basic_set_universe(isl_space_copy(dim));
659 ls = isl_local_space_from_space(dim);
661 c = isl_equality_alloc(isl_local_space_copy(ls));
662 isl_int_set_si(v, 1);
663 isl_constraint_set_constant(c, v);
664 isl_int_set_si(v, -1);
665 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
666 isl_int_set_si(v, 3);
667 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
668 bset = isl_basic_set_add_constraint(bset, c);
670 c = isl_equality_alloc(isl_local_space_copy(ls));
671 isl_int_set_si(v, -3);
672 isl_constraint_set_constant(c, v);
673 isl_int_set_si(v, 1);
674 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
675 isl_int_set_si(v, 4);
676 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
677 bset = isl_basic_set_add_constraint(bset, c);
679 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
681 assert(bset && bset->n_div == 1);
682 isl_local_space_free(ls);
683 isl_basic_set_free(bset);
685 /* test 4 */
686 dim = isl_space_set_alloc(ctx, 0, 3);
687 bset = isl_basic_set_universe(isl_space_copy(dim));
688 ls = isl_local_space_from_space(dim);
690 c = isl_equality_alloc(isl_local_space_copy(ls));
691 isl_int_set_si(v, 2);
692 isl_constraint_set_constant(c, v);
693 isl_int_set_si(v, -1);
694 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
695 isl_int_set_si(v, 3);
696 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
697 bset = isl_basic_set_add_constraint(bset, c);
699 c = isl_equality_alloc(isl_local_space_copy(ls));
700 isl_int_set_si(v, -1);
701 isl_constraint_set_constant(c, v);
702 isl_int_set_si(v, 1);
703 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
704 isl_int_set_si(v, 6);
705 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
706 bset = isl_basic_set_add_constraint(bset, c);
708 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
710 assert(isl_basic_set_is_empty(bset));
711 isl_local_space_free(ls);
712 isl_basic_set_free(bset);
714 /* test 5 */
715 dim = isl_space_set_alloc(ctx, 0, 3);
716 bset = isl_basic_set_universe(isl_space_copy(dim));
717 ls = isl_local_space_from_space(dim);
719 c = isl_equality_alloc(isl_local_space_copy(ls));
720 isl_int_set_si(v, -1);
721 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
722 isl_int_set_si(v, 3);
723 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
724 bset = isl_basic_set_add_constraint(bset, c);
726 c = isl_equality_alloc(isl_local_space_copy(ls));
727 isl_int_set_si(v, 1);
728 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
729 isl_int_set_si(v, -3);
730 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
731 bset = isl_basic_set_add_constraint(bset, c);
733 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
735 assert(bset && bset->n_div == 0);
736 isl_basic_set_free(bset);
737 isl_local_space_free(ls);
739 /* test 6 */
740 dim = isl_space_set_alloc(ctx, 0, 3);
741 bset = isl_basic_set_universe(isl_space_copy(dim));
742 ls = isl_local_space_from_space(dim);
744 c = isl_equality_alloc(isl_local_space_copy(ls));
745 isl_int_set_si(v, -1);
746 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
747 isl_int_set_si(v, 6);
748 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
749 bset = isl_basic_set_add_constraint(bset, c);
751 c = isl_equality_alloc(isl_local_space_copy(ls));
752 isl_int_set_si(v, 1);
753 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
754 isl_int_set_si(v, -3);
755 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
756 bset = isl_basic_set_add_constraint(bset, c);
758 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
760 assert(bset && bset->n_div == 1);
761 isl_basic_set_free(bset);
762 isl_local_space_free(ls);
764 /* test 7 */
765 /* This test is a bit tricky. We set up an equality
766 * a + 3b + 3c = 6 e0
767 * Normalization of divs creates _two_ divs
768 * a = 3 e0
769 * c - b - e0 = 2 e1
770 * Afterwards e0 is removed again because it has coefficient -1
771 * and we end up with the original equality and div again.
772 * Perhaps we can avoid the introduction of this temporary div.
774 dim = isl_space_set_alloc(ctx, 0, 4);
775 bset = isl_basic_set_universe(isl_space_copy(dim));
776 ls = isl_local_space_from_space(dim);
778 c = isl_equality_alloc(isl_local_space_copy(ls));
779 isl_int_set_si(v, -1);
780 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
781 isl_int_set_si(v, -3);
782 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
783 isl_int_set_si(v, -3);
784 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
785 isl_int_set_si(v, 6);
786 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
787 bset = isl_basic_set_add_constraint(bset, c);
789 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
791 /* Test disabled for now */
793 assert(bset && bset->n_div == 1);
795 isl_local_space_free(ls);
796 isl_basic_set_free(bset);
798 /* test 8 */
799 dim = isl_space_set_alloc(ctx, 0, 5);
800 bset = isl_basic_set_universe(isl_space_copy(dim));
801 ls = isl_local_space_from_space(dim);
803 c = isl_equality_alloc(isl_local_space_copy(ls));
804 isl_int_set_si(v, -1);
805 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
806 isl_int_set_si(v, -3);
807 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
808 isl_int_set_si(v, -3);
809 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
810 isl_int_set_si(v, 6);
811 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
812 bset = isl_basic_set_add_constraint(bset, c);
814 c = isl_equality_alloc(isl_local_space_copy(ls));
815 isl_int_set_si(v, -1);
816 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
817 isl_int_set_si(v, 1);
818 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
819 isl_int_set_si(v, 1);
820 isl_constraint_set_constant(c, v);
821 bset = isl_basic_set_add_constraint(bset, c);
823 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
825 /* Test disabled for now */
827 assert(bset && bset->n_div == 1);
829 isl_local_space_free(ls);
830 isl_basic_set_free(bset);
832 /* test 9 */
833 dim = isl_space_set_alloc(ctx, 0, 4);
834 bset = isl_basic_set_universe(isl_space_copy(dim));
835 ls = isl_local_space_from_space(dim);
837 c = isl_equality_alloc(isl_local_space_copy(ls));
838 isl_int_set_si(v, 1);
839 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
840 isl_int_set_si(v, -1);
841 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
842 isl_int_set_si(v, -2);
843 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
844 bset = isl_basic_set_add_constraint(bset, c);
846 c = isl_equality_alloc(isl_local_space_copy(ls));
847 isl_int_set_si(v, -1);
848 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
849 isl_int_set_si(v, 3);
850 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
851 isl_int_set_si(v, 2);
852 isl_constraint_set_constant(c, v);
853 bset = isl_basic_set_add_constraint(bset, c);
855 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
857 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
859 assert(!isl_basic_set_is_empty(bset));
861 isl_local_space_free(ls);
862 isl_basic_set_free(bset);
864 /* test 10 */
865 dim = isl_space_set_alloc(ctx, 0, 3);
866 bset = isl_basic_set_universe(isl_space_copy(dim));
867 ls = isl_local_space_from_space(dim);
869 c = isl_equality_alloc(isl_local_space_copy(ls));
870 isl_int_set_si(v, 1);
871 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
872 isl_int_set_si(v, -2);
873 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
874 bset = isl_basic_set_add_constraint(bset, c);
876 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
878 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
880 isl_local_space_free(ls);
881 isl_basic_set_free(bset);
883 isl_int_clear(v);
885 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
886 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
887 set = isl_set_read_from_str(ctx, str);
888 set = isl_set_compute_divs(set);
889 isl_set_free(set);
890 if (!set)
891 return -1;
893 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
894 bset = isl_basic_set_read_from_str(ctx, str);
895 n = isl_basic_set_dim(bset, isl_dim_div);
896 isl_basic_set_free(bset);
897 if (!bset)
898 return -1;
899 if (n != 0)
900 isl_die(ctx, isl_error_unknown,
901 "expecting no existentials", return -1);
903 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
904 set = isl_set_read_from_str(ctx, str);
905 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
906 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
907 empty = isl_set_is_empty(set);
908 isl_set_free(set);
909 if (empty < 0)
910 return -1;
911 if (!empty)
912 isl_die(ctx, isl_error_unknown,
913 "result not as accurate as expected", return -1);
915 return 0;
918 void test_application_case(struct isl_ctx *ctx, const char *name)
920 char *filename;
921 FILE *input;
922 struct isl_basic_set *bset1, *bset2;
923 struct isl_basic_map *bmap;
925 filename = get_filename(ctx, name, "omega");
926 assert(filename);
927 input = fopen(filename, "r");
928 assert(input);
930 bset1 = isl_basic_set_read_from_file(ctx, input);
931 bmap = isl_basic_map_read_from_file(ctx, input);
933 bset1 = isl_basic_set_apply(bset1, bmap);
935 bset2 = isl_basic_set_read_from_file(ctx, input);
937 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
939 isl_basic_set_free(bset1);
940 isl_basic_set_free(bset2);
941 free(filename);
943 fclose(input);
946 void test_application(struct isl_ctx *ctx)
948 test_application_case(ctx, "application");
949 test_application_case(ctx, "application2");
952 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
954 char *filename;
955 FILE *input;
956 struct isl_basic_set *bset1, *bset2;
958 filename = get_filename(ctx, name, "polylib");
959 assert(filename);
960 input = fopen(filename, "r");
961 assert(input);
963 bset1 = isl_basic_set_read_from_file(ctx, input);
964 bset2 = isl_basic_set_read_from_file(ctx, input);
966 bset1 = isl_basic_set_affine_hull(bset1);
968 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
970 isl_basic_set_free(bset1);
971 isl_basic_set_free(bset2);
972 free(filename);
974 fclose(input);
977 int test_affine_hull(struct isl_ctx *ctx)
979 const char *str;
980 isl_set *set;
981 isl_basic_set *bset, *bset2;
982 int n;
983 int subset;
985 test_affine_hull_case(ctx, "affine2");
986 test_affine_hull_case(ctx, "affine");
987 test_affine_hull_case(ctx, "affine3");
989 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
990 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
991 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
992 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
993 set = isl_set_read_from_str(ctx, str);
994 bset = isl_set_affine_hull(set);
995 n = isl_basic_set_dim(bset, isl_dim_div);
996 isl_basic_set_free(bset);
997 if (n != 0)
998 isl_die(ctx, isl_error_unknown, "not expecting any divs",
999 return -1);
1001 /* Check that isl_map_affine_hull is not confused by
1002 * the reordering of divs in isl_map_align_divs.
1004 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1005 "32e0 = b and 32e1 = c); "
1006 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1007 set = isl_set_read_from_str(ctx, str);
1008 bset = isl_set_affine_hull(set);
1009 isl_basic_set_free(bset);
1010 if (!bset)
1011 return -1;
1013 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1014 "32e2 = 31 + 31e0 }";
1015 set = isl_set_read_from_str(ctx, str);
1016 bset = isl_set_affine_hull(set);
1017 str = "{ [a] : exists e : a = 32 e }";
1018 bset2 = isl_basic_set_read_from_str(ctx, str);
1019 subset = isl_basic_set_is_subset(bset, bset2);
1020 isl_basic_set_free(bset);
1021 isl_basic_set_free(bset2);
1022 if (subset < 0)
1023 return -1;
1024 if (!subset)
1025 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1026 return -1);
1028 return 0;
1031 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1033 char *filename;
1034 FILE *input;
1035 struct isl_basic_set *bset1, *bset2;
1036 struct isl_set *set;
1038 filename = get_filename(ctx, name, "polylib");
1039 assert(filename);
1040 input = fopen(filename, "r");
1041 assert(input);
1043 bset1 = isl_basic_set_read_from_file(ctx, input);
1044 bset2 = isl_basic_set_read_from_file(ctx, input);
1046 set = isl_basic_set_union(bset1, bset2);
1047 bset1 = isl_set_convex_hull(set);
1049 bset2 = isl_basic_set_read_from_file(ctx, input);
1051 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1053 isl_basic_set_free(bset1);
1054 isl_basic_set_free(bset2);
1055 free(filename);
1057 fclose(input);
1060 struct {
1061 const char *set;
1062 const char *hull;
1063 } convex_hull_tests[] = {
1064 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1065 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1066 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1067 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1068 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1069 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1070 "i2 <= 5 and i2 >= 4; "
1071 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1072 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1073 "i2 <= 5 + i0 and i2 >= i0 }" },
1074 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1075 "{ [x, y] : 1 = 0 }" },
1078 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
1080 int i;
1081 int orig_convex = ctx->opt->convex;
1082 ctx->opt->convex = convex;
1084 test_convex_hull_case(ctx, "convex0");
1085 test_convex_hull_case(ctx, "convex1");
1086 test_convex_hull_case(ctx, "convex2");
1087 test_convex_hull_case(ctx, "convex3");
1088 test_convex_hull_case(ctx, "convex4");
1089 test_convex_hull_case(ctx, "convex5");
1090 test_convex_hull_case(ctx, "convex6");
1091 test_convex_hull_case(ctx, "convex7");
1092 test_convex_hull_case(ctx, "convex8");
1093 test_convex_hull_case(ctx, "convex9");
1094 test_convex_hull_case(ctx, "convex10");
1095 test_convex_hull_case(ctx, "convex11");
1096 test_convex_hull_case(ctx, "convex12");
1097 test_convex_hull_case(ctx, "convex13");
1098 test_convex_hull_case(ctx, "convex14");
1099 test_convex_hull_case(ctx, "convex15");
1101 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1102 isl_set *set1, *set2;
1104 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1105 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1106 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1107 assert(isl_set_is_equal(set1, set2));
1108 isl_set_free(set1);
1109 isl_set_free(set2);
1112 ctx->opt->convex = orig_convex;
1115 void test_convex_hull(struct isl_ctx *ctx)
1117 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
1118 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
1121 void test_gist_case(struct isl_ctx *ctx, const char *name)
1123 char *filename;
1124 FILE *input;
1125 struct isl_basic_set *bset1, *bset2;
1127 filename = get_filename(ctx, name, "polylib");
1128 assert(filename);
1129 input = fopen(filename, "r");
1130 assert(input);
1132 bset1 = isl_basic_set_read_from_file(ctx, input);
1133 bset2 = isl_basic_set_read_from_file(ctx, input);
1135 bset1 = isl_basic_set_gist(bset1, bset2);
1137 bset2 = isl_basic_set_read_from_file(ctx, input);
1139 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1141 isl_basic_set_free(bset1);
1142 isl_basic_set_free(bset2);
1143 free(filename);
1145 fclose(input);
1148 struct {
1149 const char *set;
1150 const char *context;
1151 const char *gist;
1152 } gist_tests[] = {
1153 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1154 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1155 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1156 "{ [a, b, c] : a <= 15 }" },
1157 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1158 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1159 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1160 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1163 static int test_gist(struct isl_ctx *ctx)
1165 int i;
1166 const char *str;
1167 isl_basic_set *bset1, *bset2;
1168 isl_map *map1, *map2;
1169 int equal;
1171 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1172 int equal_input;
1173 isl_basic_set *copy;
1175 bset1 = isl_basic_set_read_from_str(ctx, gist_tests[i].set);
1176 bset2 = isl_basic_set_read_from_str(ctx, gist_tests[i].context);
1177 copy = isl_basic_set_copy(bset1);
1178 bset1 = isl_basic_set_gist(bset1, bset2);
1179 bset2 = isl_basic_set_read_from_str(ctx, gist_tests[i].gist);
1180 equal = isl_basic_set_is_equal(bset1, bset2);
1181 isl_basic_set_free(bset1);
1182 isl_basic_set_free(bset2);
1183 bset1 = isl_basic_set_read_from_str(ctx, gist_tests[i].set);
1184 equal_input = isl_basic_set_is_equal(bset1, copy);
1185 isl_basic_set_free(bset1);
1186 isl_basic_set_free(copy);
1187 if (equal < 0 || equal_input < 0)
1188 return -1;
1189 if (!equal)
1190 isl_die(ctx, isl_error_unknown,
1191 "incorrect gist result", return -1);
1192 if (!equal_input)
1193 isl_die(ctx, isl_error_unknown,
1194 "gist modified input", return -1);
1197 test_gist_case(ctx, "gist1");
1199 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1200 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1201 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1202 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1203 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1204 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1205 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1206 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1207 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1208 bset1 = isl_basic_set_read_from_str(ctx, str);
1209 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1210 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1211 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1212 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1213 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1214 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1215 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1216 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1217 bset2 = isl_basic_set_read_from_str(ctx, str);
1218 bset1 = isl_basic_set_gist(bset1, bset2);
1219 assert(bset1 && bset1->n_div == 0);
1220 isl_basic_set_free(bset1);
1222 /* Check that the integer divisions of the second disjunct
1223 * do not spread to the first disjunct.
1225 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1226 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1227 "(exists (e0 = [(-1 + t1)/16], "
1228 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1229 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1230 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1231 "o0 <= 4294967295 and t1 <= -1)) }";
1232 map1 = isl_map_read_from_str(ctx, str);
1233 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1234 map2 = isl_map_read_from_str(ctx, str);
1235 map1 = isl_map_gist(map1, map2);
1236 if (!map1)
1237 return -1;
1238 if (map1->n != 1)
1239 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1240 isl_map_free(map1); return -1);
1241 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1242 isl_die(ctx, isl_error_unknown, "expecting single div",
1243 isl_map_free(map1); return -1);
1244 isl_map_free(map1);
1246 return 0;
1249 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1251 isl_set *set, *set2;
1252 int equal;
1253 int one;
1255 set = isl_set_read_from_str(ctx, str);
1256 set = isl_set_coalesce(set);
1257 set2 = isl_set_read_from_str(ctx, str);
1258 equal = isl_set_is_equal(set, set2);
1259 one = set && set->n == 1;
1260 isl_set_free(set);
1261 isl_set_free(set2);
1263 if (equal < 0)
1264 return -1;
1265 if (!equal)
1266 isl_die(ctx, isl_error_unknown,
1267 "coalesced set not equal to input", return -1);
1268 if (check_one && !one)
1269 isl_die(ctx, isl_error_unknown,
1270 "coalesced set should not be a union", return -1);
1272 return 0;
1275 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1277 int r = 0;
1278 int bounded;
1280 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1281 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1283 if (test_coalesce_set(ctx,
1284 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1285 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1286 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1287 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1288 "-10 <= y <= 0}", 1) < 0)
1289 goto error;
1290 if (test_coalesce_set(ctx,
1291 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
1292 goto error;
1293 if (test_coalesce_set(ctx,
1294 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
1295 goto error;
1297 if (0) {
1298 error:
1299 r = -1;
1302 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1304 return r;
1307 /* Inputs for coalescing tests.
1308 * "str" is a string representation of the input set.
1309 * "single_disjunct" is set if we expect the result to consist of
1310 * a single disjunct.
1312 struct {
1313 int single_disjunct;
1314 const char *str;
1315 } coalesce_tests[] = {
1316 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1317 "y >= x & x >= 2 & 5 >= y }" },
1318 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1319 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1320 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1321 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1322 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1323 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1324 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1325 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1326 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1327 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1328 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1329 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1330 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1331 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1332 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1333 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1334 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1335 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1336 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1337 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1338 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1339 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1340 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1341 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1342 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1343 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1344 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1345 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1346 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1347 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1348 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1349 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1350 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1351 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1352 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1353 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1354 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1355 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1356 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1357 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1358 "[o0, o1, o2, o3, o4, o5, o6]] : "
1359 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1360 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1361 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1362 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1363 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1364 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1365 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1366 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1367 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1368 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1369 "o6 >= i3 + i6 - o3 and M >= 0 and "
1370 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1371 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1372 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1373 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1374 "(o0 = 0 and M >= 2 and N >= 3) or "
1375 "(M = 0 and o0 = 0 and N >= 3) }" },
1376 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1377 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1378 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1379 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1380 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1381 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1382 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1383 "(y = 3 and x = 1) }" },
1384 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1385 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1386 "i1 <= M and i3 <= M and i4 <= M) or "
1387 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1388 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1389 "i4 <= -1 + M) }" },
1390 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1391 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1392 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1393 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1394 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1395 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1396 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1397 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1398 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1399 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1400 { 0, "{ [a, b] : exists e : 2e = a and "
1401 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1402 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1403 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1404 "j >= 1 and j' <= i + j - i' and i >= 1; "
1405 "[1, 1, 1, 1] }" },
1406 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1407 "[i,j] : exists a : j = 3a }" },
1408 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1409 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1410 "a >= 3) or "
1411 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1412 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1413 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1414 "c <= 6 + 8a and a >= 3; "
1415 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1416 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1417 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1418 "[x,0] : 3 <= x <= 4 }" },
1419 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1420 "[x,0] : 4 <= x <= 5 }" },
1421 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1422 "[x,0] : 3 <= x <= 5 }" },
1423 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1424 "[x,0] : 3 <= x <= 4 }" },
1425 { 1 , "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1426 "i1 <= 0; "
1427 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1430 /* Test the functionality of isl_set_coalesce.
1431 * That is, check that the output is always equal to the input
1432 * and in some cases that the result consists of a single disjunct.
1434 static int test_coalesce(struct isl_ctx *ctx)
1436 int i;
1438 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1439 const char *str = coalesce_tests[i].str;
1440 int check_one = coalesce_tests[i].single_disjunct;
1441 if (test_coalesce_set(ctx, str, check_one) < 0)
1442 return -1;
1445 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1446 return -1;
1448 return 0;
1451 void test_closure(struct isl_ctx *ctx)
1453 const char *str;
1454 isl_set *dom;
1455 isl_map *up, *right;
1456 isl_map *map, *map2;
1457 int exact;
1459 /* COCOA example 1 */
1460 map = isl_map_read_from_str(ctx,
1461 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1462 "1 <= i and i < n and 1 <= j and j < n or "
1463 "i2 = i + 1 and j2 = j - 1 and "
1464 "1 <= i and i < n and 2 <= j and j <= n }");
1465 map = isl_map_power(map, &exact);
1466 assert(exact);
1467 isl_map_free(map);
1469 /* COCOA example 1 */
1470 map = isl_map_read_from_str(ctx,
1471 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1472 "1 <= i and i < n and 1 <= j and j < n or "
1473 "i2 = i + 1 and j2 = j - 1 and "
1474 "1 <= i and i < n and 2 <= j and j <= n }");
1475 map = isl_map_transitive_closure(map, &exact);
1476 assert(exact);
1477 map2 = isl_map_read_from_str(ctx,
1478 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1479 "1 <= i and i < n and 1 <= j and j <= n and "
1480 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1481 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1482 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1483 assert(isl_map_is_equal(map, map2));
1484 isl_map_free(map2);
1485 isl_map_free(map);
1487 map = isl_map_read_from_str(ctx,
1488 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1489 " 0 <= y and y <= n }");
1490 map = isl_map_transitive_closure(map, &exact);
1491 map2 = isl_map_read_from_str(ctx,
1492 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1493 " 0 <= y and y <= n }");
1494 assert(isl_map_is_equal(map, map2));
1495 isl_map_free(map2);
1496 isl_map_free(map);
1498 /* COCOA example 2 */
1499 map = isl_map_read_from_str(ctx,
1500 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1501 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1502 "i2 = i + 2 and j2 = j - 2 and "
1503 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1504 map = isl_map_transitive_closure(map, &exact);
1505 assert(exact);
1506 map2 = isl_map_read_from_str(ctx,
1507 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1508 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1509 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1510 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1511 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1512 assert(isl_map_is_equal(map, map2));
1513 isl_map_free(map);
1514 isl_map_free(map2);
1516 /* COCOA Fig.2 left */
1517 map = isl_map_read_from_str(ctx,
1518 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1519 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1520 "j <= n or "
1521 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1522 "j <= 2 i - 3 and j <= n - 2 or "
1523 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1524 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1525 map = isl_map_transitive_closure(map, &exact);
1526 assert(exact);
1527 isl_map_free(map);
1529 /* COCOA Fig.2 right */
1530 map = isl_map_read_from_str(ctx,
1531 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1532 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1533 "j <= n or "
1534 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1535 "j <= 2 i - 4 and j <= n - 3 or "
1536 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1537 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1538 map = isl_map_power(map, &exact);
1539 assert(exact);
1540 isl_map_free(map);
1542 /* COCOA Fig.2 right */
1543 map = isl_map_read_from_str(ctx,
1544 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1545 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1546 "j <= n or "
1547 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1548 "j <= 2 i - 4 and j <= n - 3 or "
1549 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1550 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1551 map = isl_map_transitive_closure(map, &exact);
1552 assert(exact);
1553 map2 = isl_map_read_from_str(ctx,
1554 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1555 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1556 "j <= n and 3 + i + 2 j <= 3 n and "
1557 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1558 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1559 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1560 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1561 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1562 assert(isl_map_is_equal(map, map2));
1563 isl_map_free(map2);
1564 isl_map_free(map);
1566 /* COCOA Fig.1 right */
1567 dom = isl_set_read_from_str(ctx,
1568 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1569 "2 x - 3 y + 3 >= 0 }");
1570 right = isl_map_read_from_str(ctx,
1571 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1572 up = isl_map_read_from_str(ctx,
1573 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1574 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1575 right = isl_map_intersect_range(right, isl_set_copy(dom));
1576 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1577 up = isl_map_intersect_range(up, dom);
1578 map = isl_map_union(up, right);
1579 map = isl_map_transitive_closure(map, &exact);
1580 assert(exact);
1581 map2 = isl_map_read_from_str(ctx,
1582 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1583 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1584 assert(isl_map_is_equal(map, map2));
1585 isl_map_free(map2);
1586 isl_map_free(map);
1588 /* COCOA Theorem 1 counter example */
1589 map = isl_map_read_from_str(ctx,
1590 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1591 "i2 = 1 and j2 = j or "
1592 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1593 map = isl_map_transitive_closure(map, &exact);
1594 assert(exact);
1595 isl_map_free(map);
1597 map = isl_map_read_from_str(ctx,
1598 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1599 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1600 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1601 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1602 map = isl_map_transitive_closure(map, &exact);
1603 assert(exact);
1604 isl_map_free(map);
1606 /* Kelly et al 1996, fig 12 */
1607 map = isl_map_read_from_str(ctx,
1608 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1609 "1 <= i,j,j+1 <= n or "
1610 "j = n and j2 = 1 and i2 = i + 1 and "
1611 "1 <= i,i+1 <= n }");
1612 map = isl_map_transitive_closure(map, &exact);
1613 assert(exact);
1614 map2 = isl_map_read_from_str(ctx,
1615 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1616 "1 <= i <= n and i = i2 or "
1617 "1 <= i < i2 <= n and 1 <= j <= n and "
1618 "1 <= j2 <= n }");
1619 assert(isl_map_is_equal(map, map2));
1620 isl_map_free(map2);
1621 isl_map_free(map);
1623 /* Omega's closure4 */
1624 map = isl_map_read_from_str(ctx,
1625 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1626 "1 <= x,y <= 10 or "
1627 "x2 = x + 1 and y2 = y and "
1628 "1 <= x <= 20 && 5 <= y <= 15 }");
1629 map = isl_map_transitive_closure(map, &exact);
1630 assert(exact);
1631 isl_map_free(map);
1633 map = isl_map_read_from_str(ctx,
1634 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1635 map = isl_map_transitive_closure(map, &exact);
1636 assert(!exact);
1637 map2 = isl_map_read_from_str(ctx,
1638 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1639 assert(isl_map_is_equal(map, map2));
1640 isl_map_free(map);
1641 isl_map_free(map2);
1643 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1644 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1645 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1646 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1647 map = isl_map_read_from_str(ctx, str);
1648 map = isl_map_transitive_closure(map, &exact);
1649 assert(exact);
1650 map2 = isl_map_read_from_str(ctx, str);
1651 assert(isl_map_is_equal(map, map2));
1652 isl_map_free(map);
1653 isl_map_free(map2);
1655 str = "{[0] -> [1]; [2] -> [3]}";
1656 map = isl_map_read_from_str(ctx, str);
1657 map = isl_map_transitive_closure(map, &exact);
1658 assert(exact);
1659 map2 = isl_map_read_from_str(ctx, str);
1660 assert(isl_map_is_equal(map, map2));
1661 isl_map_free(map);
1662 isl_map_free(map2);
1664 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1665 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1666 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1667 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1668 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1669 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1670 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1671 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1672 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1673 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1674 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1675 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1676 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1677 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1678 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1679 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1680 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1681 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1682 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1683 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1684 map = isl_map_read_from_str(ctx, str);
1685 map = isl_map_transitive_closure(map, NULL);
1686 assert(map);
1687 isl_map_free(map);
1690 void test_lex(struct isl_ctx *ctx)
1692 isl_space *dim;
1693 isl_map *map;
1695 dim = isl_space_set_alloc(ctx, 0, 0);
1696 map = isl_map_lex_le(dim);
1697 assert(!isl_map_is_empty(map));
1698 isl_map_free(map);
1701 static int test_lexmin(struct isl_ctx *ctx)
1703 int equal;
1704 const char *str;
1705 isl_basic_map *bmap;
1706 isl_map *map, *map2;
1707 isl_set *set;
1708 isl_set *set2;
1709 isl_pw_multi_aff *pma;
1711 str = "[p0, p1] -> { [] -> [] : "
1712 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1713 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1714 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1715 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1716 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1717 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1718 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1719 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1720 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1721 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1722 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1723 map = isl_map_read_from_str(ctx, str);
1724 map = isl_map_lexmin(map);
1725 isl_map_free(map);
1727 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1728 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1729 set = isl_set_read_from_str(ctx, str);
1730 set = isl_set_lexmax(set);
1731 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1732 set2 = isl_set_read_from_str(ctx, str);
1733 set = isl_set_intersect(set, set2);
1734 assert(!isl_set_is_empty(set));
1735 isl_set_free(set);
1737 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1738 map = isl_map_read_from_str(ctx, str);
1739 map = isl_map_lexmin(map);
1740 str = "{ [x] -> [5] : 6 <= x <= 8; "
1741 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1742 map2 = isl_map_read_from_str(ctx, str);
1743 assert(isl_map_is_equal(map, map2));
1744 isl_map_free(map);
1745 isl_map_free(map2);
1747 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1748 map = isl_map_read_from_str(ctx, str);
1749 map2 = isl_map_copy(map);
1750 map = isl_map_lexmin(map);
1751 assert(isl_map_is_equal(map, map2));
1752 isl_map_free(map);
1753 isl_map_free(map2);
1755 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1756 map = isl_map_read_from_str(ctx, str);
1757 map = isl_map_lexmin(map);
1758 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1759 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1760 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1761 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1762 map2 = isl_map_read_from_str(ctx, str);
1763 assert(isl_map_is_equal(map, map2));
1764 isl_map_free(map);
1765 isl_map_free(map2);
1767 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1768 " 8i' <= i and 8i' >= -7 + i }";
1769 bmap = isl_basic_map_read_from_str(ctx, str);
1770 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1771 map2 = isl_map_from_pw_multi_aff(pma);
1772 map = isl_map_from_basic_map(bmap);
1773 assert(isl_map_is_equal(map, map2));
1774 isl_map_free(map);
1775 isl_map_free(map2);
1777 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1778 map = isl_map_read_from_str(ctx, str);
1779 map = isl_map_lexmin(map);
1780 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1781 map2 = isl_map_read_from_str(ctx, str);
1782 assert(isl_map_is_equal(map, map2));
1783 isl_map_free(map);
1784 isl_map_free(map2);
1786 /* Check that empty pieces are properly combined. */
1787 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1788 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1789 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1790 map = isl_map_read_from_str(ctx, str);
1791 map = isl_map_lexmin(map);
1792 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1793 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1794 "x >= 4 }";
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 = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1801 " 8i' <= i and 8i' >= -7 + i }";
1802 set = isl_set_read_from_str(ctx, str);
1803 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1804 set2 = isl_set_from_pw_multi_aff(pma);
1805 equal = isl_set_is_equal(set, set2);
1806 isl_set_free(set);
1807 isl_set_free(set2);
1808 if (equal < 0)
1809 return -1;
1810 if (!equal)
1811 isl_die(ctx, isl_error_unknown,
1812 "unexpected difference between set and "
1813 "piecewise affine expression", return -1);
1815 return 0;
1818 /* Check that isl_set_min_val and isl_set_max_val compute the correct
1819 * result on non-convex inputs.
1821 static int test_min(struct isl_ctx *ctx)
1823 isl_set *set;
1824 isl_aff *aff;
1825 isl_val *val;
1826 int min_ok, max_ok;
1828 set = isl_set_read_from_str(ctx, "{ [-1]; [1] }");
1829 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x] }");
1830 val = isl_set_min_val(set, aff);
1831 min_ok = isl_val_is_negone(val);
1832 isl_val_free(val);
1833 val = isl_set_max_val(set, aff);
1834 max_ok = isl_val_is_one(val);
1835 isl_val_free(val);
1836 isl_aff_free(aff);
1837 isl_set_free(set);
1839 if (min_ok < 0 || max_ok < 0)
1840 return -1;
1841 if (!min_ok)
1842 isl_die(ctx, isl_error_unknown,
1843 "unexpected minimum", return -1);
1844 if (!max_ok)
1845 isl_die(ctx, isl_error_unknown,
1846 "unexpected maximum", return -1);
1848 return 0;
1851 struct must_may {
1852 isl_map *must;
1853 isl_map *may;
1856 static int collect_must_may(__isl_take isl_map *dep, int must,
1857 void *dep_user, void *user)
1859 struct must_may *mm = (struct must_may *)user;
1861 if (must)
1862 mm->must = isl_map_union(mm->must, dep);
1863 else
1864 mm->may = isl_map_union(mm->may, dep);
1866 return 0;
1869 static int common_space(void *first, void *second)
1871 int depth = *(int *)first;
1872 return 2 * depth;
1875 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1877 isl_map *map2;
1878 int equal;
1880 if (!map)
1881 return -1;
1883 map2 = isl_map_read_from_str(map->ctx, str);
1884 equal = isl_map_is_equal(map, map2);
1885 isl_map_free(map2);
1887 return equal;
1890 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1892 int equal;
1894 equal = map_is_equal(map, str);
1895 if (equal < 0)
1896 return -1;
1897 if (!equal)
1898 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1899 "result not as expected", return -1);
1900 return 0;
1903 void test_dep(struct isl_ctx *ctx)
1905 const char *str;
1906 isl_space *dim;
1907 isl_map *map;
1908 isl_access_info *ai;
1909 isl_flow *flow;
1910 int depth;
1911 struct must_may mm;
1913 depth = 3;
1915 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1916 map = isl_map_read_from_str(ctx, str);
1917 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1919 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1920 map = isl_map_read_from_str(ctx, str);
1921 ai = isl_access_info_add_source(ai, map, 1, &depth);
1923 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1924 map = isl_map_read_from_str(ctx, str);
1925 ai = isl_access_info_add_source(ai, map, 1, &depth);
1927 flow = isl_access_info_compute_flow(ai);
1928 dim = isl_space_alloc(ctx, 0, 3, 3);
1929 mm.must = isl_map_empty(isl_space_copy(dim));
1930 mm.may = isl_map_empty(dim);
1932 isl_flow_foreach(flow, collect_must_may, &mm);
1934 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1935 " [1,10,0] -> [2,5,0] }";
1936 assert(map_is_equal(mm.must, str));
1937 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1938 assert(map_is_equal(mm.may, str));
1940 isl_map_free(mm.must);
1941 isl_map_free(mm.may);
1942 isl_flow_free(flow);
1945 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1946 map = isl_map_read_from_str(ctx, str);
1947 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1949 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1950 map = isl_map_read_from_str(ctx, str);
1951 ai = isl_access_info_add_source(ai, map, 1, &depth);
1953 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1954 map = isl_map_read_from_str(ctx, str);
1955 ai = isl_access_info_add_source(ai, map, 0, &depth);
1957 flow = isl_access_info_compute_flow(ai);
1958 dim = isl_space_alloc(ctx, 0, 3, 3);
1959 mm.must = isl_map_empty(isl_space_copy(dim));
1960 mm.may = isl_map_empty(dim);
1962 isl_flow_foreach(flow, collect_must_may, &mm);
1964 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1965 assert(map_is_equal(mm.must, str));
1966 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1967 assert(map_is_equal(mm.may, str));
1969 isl_map_free(mm.must);
1970 isl_map_free(mm.may);
1971 isl_flow_free(flow);
1974 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1975 map = isl_map_read_from_str(ctx, str);
1976 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1978 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1979 map = isl_map_read_from_str(ctx, str);
1980 ai = isl_access_info_add_source(ai, map, 0, &depth);
1982 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1983 map = isl_map_read_from_str(ctx, str);
1984 ai = isl_access_info_add_source(ai, map, 0, &depth);
1986 flow = isl_access_info_compute_flow(ai);
1987 dim = isl_space_alloc(ctx, 0, 3, 3);
1988 mm.must = isl_map_empty(isl_space_copy(dim));
1989 mm.may = isl_map_empty(dim);
1991 isl_flow_foreach(flow, collect_must_may, &mm);
1993 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1994 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1995 assert(map_is_equal(mm.may, str));
1996 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1997 assert(map_is_equal(mm.must, str));
1999 isl_map_free(mm.must);
2000 isl_map_free(mm.may);
2001 isl_flow_free(flow);
2004 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2005 map = isl_map_read_from_str(ctx, str);
2006 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2008 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2009 map = isl_map_read_from_str(ctx, str);
2010 ai = isl_access_info_add_source(ai, map, 0, &depth);
2012 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2013 map = isl_map_read_from_str(ctx, str);
2014 ai = isl_access_info_add_source(ai, map, 0, &depth);
2016 flow = isl_access_info_compute_flow(ai);
2017 dim = isl_space_alloc(ctx, 0, 3, 3);
2018 mm.must = isl_map_empty(isl_space_copy(dim));
2019 mm.may = isl_map_empty(dim);
2021 isl_flow_foreach(flow, collect_must_may, &mm);
2023 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2024 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2025 assert(map_is_equal(mm.may, str));
2026 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2027 assert(map_is_equal(mm.must, str));
2029 isl_map_free(mm.must);
2030 isl_map_free(mm.may);
2031 isl_flow_free(flow);
2034 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2035 map = isl_map_read_from_str(ctx, str);
2036 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2038 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2039 map = isl_map_read_from_str(ctx, str);
2040 ai = isl_access_info_add_source(ai, map, 0, &depth);
2042 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2043 map = isl_map_read_from_str(ctx, str);
2044 ai = isl_access_info_add_source(ai, map, 0, &depth);
2046 flow = isl_access_info_compute_flow(ai);
2047 dim = isl_space_alloc(ctx, 0, 3, 3);
2048 mm.must = isl_map_empty(isl_space_copy(dim));
2049 mm.may = isl_map_empty(dim);
2051 isl_flow_foreach(flow, collect_must_may, &mm);
2053 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2054 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2055 assert(map_is_equal(mm.may, str));
2056 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2057 assert(map_is_equal(mm.must, str));
2059 isl_map_free(mm.must);
2060 isl_map_free(mm.may);
2061 isl_flow_free(flow);
2064 depth = 5;
2066 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2067 map = isl_map_read_from_str(ctx, str);
2068 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2070 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2071 map = isl_map_read_from_str(ctx, str);
2072 ai = isl_access_info_add_source(ai, map, 1, &depth);
2074 flow = isl_access_info_compute_flow(ai);
2075 dim = isl_space_alloc(ctx, 0, 5, 5);
2076 mm.must = isl_map_empty(isl_space_copy(dim));
2077 mm.may = isl_map_empty(dim);
2079 isl_flow_foreach(flow, collect_must_may, &mm);
2081 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2082 assert(map_is_equal(mm.must, str));
2083 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2084 assert(map_is_equal(mm.may, str));
2086 isl_map_free(mm.must);
2087 isl_map_free(mm.may);
2088 isl_flow_free(flow);
2091 /* Check that the dependence analysis proceeds without errors.
2092 * Earlier versions of isl would break down during the analysis
2093 * due to the use of the wrong spaces.
2095 static int test_flow(isl_ctx *ctx)
2097 const char *str;
2098 isl_union_map *access, *schedule;
2099 isl_union_map *must_dep, *may_dep;
2100 int r;
2102 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2103 access = isl_union_map_read_from_str(ctx, str);
2104 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2105 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2106 "S2[] -> [1,0,0,0]; "
2107 "S3[] -> [-1,0,0,0] }";
2108 schedule = isl_union_map_read_from_str(ctx, str);
2109 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2110 isl_union_map_copy(access), schedule,
2111 &must_dep, &may_dep, NULL, NULL);
2112 isl_union_map_free(may_dep);
2113 isl_union_map_free(must_dep);
2115 return r;
2118 struct {
2119 const char *map;
2120 int sv;
2121 } sv_tests[] = {
2122 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2123 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2124 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2125 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2126 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2127 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2128 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2129 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2130 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2133 int test_sv(isl_ctx *ctx)
2135 isl_union_map *umap;
2136 int i;
2137 int sv;
2139 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2140 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2141 sv = isl_union_map_is_single_valued(umap);
2142 isl_union_map_free(umap);
2143 if (sv < 0)
2144 return -1;
2145 if (sv_tests[i].sv && !sv)
2146 isl_die(ctx, isl_error_internal,
2147 "map not detected as single valued", return -1);
2148 if (!sv_tests[i].sv && sv)
2149 isl_die(ctx, isl_error_internal,
2150 "map detected as single valued", return -1);
2153 return 0;
2156 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
2158 isl_map *map;
2160 map = isl_map_read_from_str(ctx, str);
2161 if (bijective)
2162 assert(isl_map_is_bijective(map));
2163 else
2164 assert(!isl_map_is_bijective(map));
2165 isl_map_free(map);
2168 void test_bijective(struct isl_ctx *ctx)
2170 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
2171 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
2172 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
2173 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
2174 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
2175 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
2176 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
2177 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
2178 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
2179 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
2180 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
2181 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
2182 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
2185 /* Inputs for isl_pw_qpolynomial_gist tests.
2186 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2188 struct {
2189 const char *pwqp;
2190 const char *set;
2191 const char *gist;
2192 } pwqp_gist_tests[] = {
2193 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2194 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2195 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2196 "{ [i] -> -1/2 + 1/2 * i }" },
2197 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2200 static int test_pwqp(struct isl_ctx *ctx)
2202 int i;
2203 const char *str;
2204 isl_set *set;
2205 isl_pw_qpolynomial *pwqp1, *pwqp2;
2206 int equal;
2208 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2209 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2211 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2212 isl_dim_in, 1, 1);
2214 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2215 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2217 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2219 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2221 isl_pw_qpolynomial_free(pwqp1);
2223 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2224 str = pwqp_gist_tests[i].pwqp;
2225 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2226 str = pwqp_gist_tests[i].set;
2227 set = isl_set_read_from_str(ctx, str);
2228 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2229 str = pwqp_gist_tests[i].gist;
2230 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2231 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2232 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2233 isl_pw_qpolynomial_free(pwqp1);
2235 if (equal < 0)
2236 return -1;
2237 if (!equal)
2238 isl_die(ctx, isl_error_unknown,
2239 "unexpected result", return -1);
2242 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2243 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2244 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2245 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2247 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2249 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2251 isl_pw_qpolynomial_free(pwqp1);
2253 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2254 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2255 str = "{ [x] -> x }";
2256 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2258 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2260 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2262 isl_pw_qpolynomial_free(pwqp1);
2264 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2265 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2266 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2267 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2268 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2269 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2270 isl_pw_qpolynomial_free(pwqp1);
2272 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2273 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2274 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2275 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2276 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2277 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2278 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2279 isl_pw_qpolynomial_free(pwqp1);
2280 isl_pw_qpolynomial_free(pwqp2);
2281 if (equal < 0)
2282 return -1;
2283 if (!equal)
2284 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2286 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2287 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2288 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2289 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2290 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2291 isl_val_one(ctx));
2292 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2293 isl_pw_qpolynomial_free(pwqp1);
2294 isl_pw_qpolynomial_free(pwqp2);
2295 if (equal < 0)
2296 return -1;
2297 if (!equal)
2298 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2300 return 0;
2303 void test_split_periods(isl_ctx *ctx)
2305 const char *str;
2306 isl_pw_qpolynomial *pwqp;
2308 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2309 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2310 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2311 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2313 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2314 assert(pwqp);
2316 isl_pw_qpolynomial_free(pwqp);
2319 void test_union(isl_ctx *ctx)
2321 const char *str;
2322 isl_union_set *uset1, *uset2;
2323 isl_union_map *umap1, *umap2;
2325 str = "{ [i] : 0 <= i <= 1 }";
2326 uset1 = isl_union_set_read_from_str(ctx, str);
2327 str = "{ [1] -> [0] }";
2328 umap1 = isl_union_map_read_from_str(ctx, str);
2330 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2331 assert(isl_union_map_is_equal(umap1, umap2));
2333 isl_union_map_free(umap1);
2334 isl_union_map_free(umap2);
2336 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2337 umap1 = isl_union_map_read_from_str(ctx, str);
2338 str = "{ A[i]; B[i] }";
2339 uset1 = isl_union_set_read_from_str(ctx, str);
2341 uset2 = isl_union_map_domain(umap1);
2343 assert(isl_union_set_is_equal(uset1, uset2));
2345 isl_union_set_free(uset1);
2346 isl_union_set_free(uset2);
2349 /* Check that computing a bound of a non-zero polynomial over an unbounded
2350 * domain does not produce a rational value.
2351 * Ideally, we want the value to be infinity, but we accept NaN for now.
2352 * We certainly do not want to obtain the value zero.
2354 static int test_bound_unbounded_domain(isl_ctx *ctx)
2356 const char *str;
2357 isl_set *dom;
2358 isl_point *pnt;
2359 isl_pw_qpolynomial *pwqp;
2360 isl_pw_qpolynomial_fold *pwf;
2361 isl_val *v;
2362 int is_rat;
2364 str = "{ [m,n] -> -m * n }";
2365 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2366 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2367 dom = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf));
2368 pnt = isl_set_sample_point(dom);
2369 v = isl_pw_qpolynomial_fold_eval(pwf, pnt);
2370 is_rat = isl_val_is_rat(v);
2371 isl_val_free(v);
2373 if (is_rat < 0)
2374 return -1;
2375 if (is_rat)
2376 isl_die(ctx, isl_error_unknown,
2377 "unexpected rational value", return -1);
2379 return 0;
2382 static int test_bound(isl_ctx *ctx)
2384 const char *str;
2385 isl_pw_qpolynomial *pwqp;
2386 isl_pw_qpolynomial_fold *pwf;
2388 if (test_bound_unbounded_domain(ctx) < 0)
2389 return -1;
2391 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2392 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2393 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2394 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
2395 isl_pw_qpolynomial_fold_free(pwf);
2397 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2398 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2399 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2400 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
2401 isl_pw_qpolynomial_fold_free(pwf);
2403 return 0;
2406 void test_lift(isl_ctx *ctx)
2408 const char *str;
2409 isl_basic_map *bmap;
2410 isl_basic_set *bset;
2412 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2413 bset = isl_basic_set_read_from_str(ctx, str);
2414 bset = isl_basic_set_lift(bset);
2415 bmap = isl_basic_map_from_range(bset);
2416 bset = isl_basic_map_domain(bmap);
2417 isl_basic_set_free(bset);
2420 struct {
2421 const char *set1;
2422 const char *set2;
2423 int subset;
2424 } subset_tests[] = {
2425 { "{ [112, 0] }",
2426 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2427 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2428 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2429 { "{ [65] }",
2430 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2431 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2432 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2433 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2434 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2435 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2436 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2437 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2438 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2439 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2440 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2441 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2442 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2443 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
2444 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
2445 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
2446 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
2447 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
2448 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
2449 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
2450 "4e0 <= -57 + i0 + i1)) or "
2451 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
2452 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
2453 "4e0 >= -61 + i0 + i1)) or "
2454 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
2457 static int test_subset(isl_ctx *ctx)
2459 int i;
2460 isl_set *set1, *set2;
2461 int subset;
2463 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2464 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2465 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2466 subset = isl_set_is_subset(set1, set2);
2467 isl_set_free(set1);
2468 isl_set_free(set2);
2469 if (subset < 0)
2470 return -1;
2471 if (subset != subset_tests[i].subset)
2472 isl_die(ctx, isl_error_unknown,
2473 "incorrect subset result", return -1);
2476 return 0;
2479 struct {
2480 const char *minuend;
2481 const char *subtrahend;
2482 const char *difference;
2483 } subtract_domain_tests[] = {
2484 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2485 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2486 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2489 static int test_subtract(isl_ctx *ctx)
2491 int i;
2492 isl_union_map *umap1, *umap2;
2493 isl_union_set *uset;
2494 int equal;
2496 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2497 umap1 = isl_union_map_read_from_str(ctx,
2498 subtract_domain_tests[i].minuend);
2499 uset = isl_union_set_read_from_str(ctx,
2500 subtract_domain_tests[i].subtrahend);
2501 umap2 = isl_union_map_read_from_str(ctx,
2502 subtract_domain_tests[i].difference);
2503 umap1 = isl_union_map_subtract_domain(umap1, uset);
2504 equal = isl_union_map_is_equal(umap1, umap2);
2505 isl_union_map_free(umap1);
2506 isl_union_map_free(umap2);
2507 if (equal < 0)
2508 return -1;
2509 if (!equal)
2510 isl_die(ctx, isl_error_unknown,
2511 "incorrect subtract domain result", return -1);
2514 return 0;
2517 int test_factorize(isl_ctx *ctx)
2519 const char *str;
2520 isl_basic_set *bset;
2521 isl_factorizer *f;
2523 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2524 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2525 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2526 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2527 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2528 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2529 "3i5 >= -2i0 - i2 + 3i4 }";
2530 bset = isl_basic_set_read_from_str(ctx, str);
2531 f = isl_basic_set_factorizer(bset);
2532 isl_basic_set_free(bset);
2533 isl_factorizer_free(f);
2534 if (!f)
2535 isl_die(ctx, isl_error_unknown,
2536 "failed to construct factorizer", return -1);
2538 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2539 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2540 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2541 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2542 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2543 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2544 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2545 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2546 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2547 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2548 bset = isl_basic_set_read_from_str(ctx, str);
2549 f = isl_basic_set_factorizer(bset);
2550 isl_basic_set_free(bset);
2551 isl_factorizer_free(f);
2552 if (!f)
2553 isl_die(ctx, isl_error_unknown,
2554 "failed to construct factorizer", return -1);
2556 return 0;
2559 static int check_injective(__isl_take isl_map *map, void *user)
2561 int *injective = user;
2563 *injective = isl_map_is_injective(map);
2564 isl_map_free(map);
2566 if (*injective < 0 || !*injective)
2567 return -1;
2569 return 0;
2572 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2573 const char *r, const char *s, int tilable, int parallel)
2575 int i;
2576 isl_union_set *D;
2577 isl_union_map *W, *R, *S;
2578 isl_union_map *empty;
2579 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2580 isl_union_map *validity, *proximity, *coincidence;
2581 isl_union_map *schedule;
2582 isl_union_map *test;
2583 isl_union_set *delta;
2584 isl_union_set *domain;
2585 isl_set *delta_set;
2586 isl_set *slice;
2587 isl_set *origin;
2588 isl_schedule_constraints *sc;
2589 isl_schedule *sched;
2590 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2592 D = isl_union_set_read_from_str(ctx, d);
2593 W = isl_union_map_read_from_str(ctx, w);
2594 R = isl_union_map_read_from_str(ctx, r);
2595 S = isl_union_map_read_from_str(ctx, s);
2597 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2598 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2600 empty = isl_union_map_empty(isl_union_map_get_space(S));
2601 isl_union_map_compute_flow(isl_union_map_copy(R),
2602 isl_union_map_copy(W), empty,
2603 isl_union_map_copy(S),
2604 &dep_raw, NULL, NULL, NULL);
2605 isl_union_map_compute_flow(isl_union_map_copy(W),
2606 isl_union_map_copy(W),
2607 isl_union_map_copy(R),
2608 isl_union_map_copy(S),
2609 &dep_waw, &dep_war, NULL, NULL);
2611 dep = isl_union_map_union(dep_waw, dep_war);
2612 dep = isl_union_map_union(dep, dep_raw);
2613 validity = isl_union_map_copy(dep);
2614 coincidence = isl_union_map_copy(dep);
2615 proximity = isl_union_map_copy(dep);
2617 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
2618 sc = isl_schedule_constraints_set_validity(sc, validity);
2619 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
2620 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2621 sched = isl_schedule_constraints_compute_schedule(sc);
2622 schedule = isl_schedule_get_map(sched);
2623 isl_schedule_free(sched);
2624 isl_union_map_free(W);
2625 isl_union_map_free(R);
2626 isl_union_map_free(S);
2628 is_injection = 1;
2629 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2631 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2632 is_complete = isl_union_set_is_subset(D, domain);
2633 isl_union_set_free(D);
2634 isl_union_set_free(domain);
2636 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2637 test = isl_union_map_apply_range(test, dep);
2638 test = isl_union_map_apply_range(test, schedule);
2640 delta = isl_union_map_deltas(test);
2641 if (isl_union_set_n_set(delta) == 0) {
2642 is_tilable = 1;
2643 is_parallel = 1;
2644 is_nonneg = 1;
2645 isl_union_set_free(delta);
2646 } else {
2647 delta_set = isl_set_from_union_set(delta);
2649 slice = isl_set_universe(isl_set_get_space(delta_set));
2650 for (i = 0; i < tilable; ++i)
2651 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2652 is_tilable = isl_set_is_subset(delta_set, slice);
2653 isl_set_free(slice);
2655 slice = isl_set_universe(isl_set_get_space(delta_set));
2656 for (i = 0; i < parallel; ++i)
2657 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2658 is_parallel = isl_set_is_subset(delta_set, slice);
2659 isl_set_free(slice);
2661 origin = isl_set_universe(isl_set_get_space(delta_set));
2662 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2663 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2665 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2666 delta_set = isl_set_lexmin(delta_set);
2668 is_nonneg = isl_set_is_equal(delta_set, origin);
2670 isl_set_free(origin);
2671 isl_set_free(delta_set);
2674 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2675 is_injection < 0 || is_complete < 0)
2676 return -1;
2677 if (!is_complete)
2678 isl_die(ctx, isl_error_unknown,
2679 "generated schedule incomplete", return -1);
2680 if (!is_injection)
2681 isl_die(ctx, isl_error_unknown,
2682 "generated schedule not injective on each statement",
2683 return -1);
2684 if (!is_nonneg)
2685 isl_die(ctx, isl_error_unknown,
2686 "negative dependences in generated schedule",
2687 return -1);
2688 if (!is_tilable)
2689 isl_die(ctx, isl_error_unknown,
2690 "generated schedule not as tilable as expected",
2691 return -1);
2692 if (!is_parallel)
2693 isl_die(ctx, isl_error_unknown,
2694 "generated schedule not as parallel as expected",
2695 return -1);
2697 return 0;
2700 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2701 const char *domain, const char *validity, const char *proximity)
2703 isl_union_set *dom;
2704 isl_union_map *dep;
2705 isl_union_map *prox;
2706 isl_schedule_constraints *sc;
2707 isl_schedule *schedule;
2708 isl_union_map *sched;
2710 dom = isl_union_set_read_from_str(ctx, domain);
2711 dep = isl_union_map_read_from_str(ctx, validity);
2712 prox = isl_union_map_read_from_str(ctx, proximity);
2713 sc = isl_schedule_constraints_on_domain(dom);
2714 sc = isl_schedule_constraints_set_validity(sc, dep);
2715 sc = isl_schedule_constraints_set_proximity(sc, prox);
2716 schedule = isl_schedule_constraints_compute_schedule(sc);
2717 sched = isl_schedule_get_map(schedule);
2718 isl_schedule_free(schedule);
2720 return sched;
2723 /* Check that a schedule can be constructed on the given domain
2724 * with the given validity and proximity constraints.
2726 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2727 const char *validity, const char *proximity)
2729 isl_union_map *sched;
2731 sched = compute_schedule(ctx, domain, validity, proximity);
2732 if (!sched)
2733 return -1;
2735 isl_union_map_free(sched);
2736 return 0;
2739 int test_special_schedule(isl_ctx *ctx, const char *domain,
2740 const char *validity, const char *proximity, const char *expected_sched)
2742 isl_union_map *sched1, *sched2;
2743 int equal;
2745 sched1 = compute_schedule(ctx, domain, validity, proximity);
2746 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2748 equal = isl_union_map_is_equal(sched1, sched2);
2749 isl_union_map_free(sched1);
2750 isl_union_map_free(sched2);
2752 if (equal < 0)
2753 return -1;
2754 if (!equal)
2755 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2756 return -1);
2758 return 0;
2761 /* Check that the schedule map is properly padded, even after being
2762 * reconstructed from the band forest.
2764 static int test_padded_schedule(isl_ctx *ctx)
2766 const char *str;
2767 isl_union_set *D;
2768 isl_union_map *validity, *proximity;
2769 isl_schedule_constraints *sc;
2770 isl_schedule *sched;
2771 isl_union_map *map1, *map2;
2772 isl_band_list *list;
2773 int equal;
2775 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2776 D = isl_union_set_read_from_str(ctx, str);
2777 validity = isl_union_map_empty(isl_union_set_get_space(D));
2778 proximity = isl_union_map_copy(validity);
2779 sc = isl_schedule_constraints_on_domain(D);
2780 sc = isl_schedule_constraints_set_validity(sc, validity);
2781 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2782 sched = isl_schedule_constraints_compute_schedule(sc);
2783 map1 = isl_schedule_get_map(sched);
2784 list = isl_schedule_get_band_forest(sched);
2785 isl_band_list_free(list);
2786 map2 = isl_schedule_get_map(sched);
2787 isl_schedule_free(sched);
2788 equal = isl_union_map_is_equal(map1, map2);
2789 isl_union_map_free(map1);
2790 isl_union_map_free(map2);
2792 if (equal < 0)
2793 return -1;
2794 if (!equal)
2795 isl_die(ctx, isl_error_unknown,
2796 "reconstructed schedule map not the same as original",
2797 return -1);
2799 return 0;
2802 /* Input for testing of schedule construction based on
2803 * conditional constraints.
2805 * domain is the iteration domain
2806 * flow are the flow dependences, which determine the validity and
2807 * proximity constraints
2808 * condition are the conditions on the conditional validity constraints
2809 * conditional_validity are the conditional validity constraints
2810 * outer_band_n is the expected number of members in the outer band
2812 struct {
2813 const char *domain;
2814 const char *flow;
2815 const char *condition;
2816 const char *conditional_validity;
2817 int outer_band_n;
2818 } live_range_tests[] = {
2819 /* Contrived example that illustrates that we need to keep
2820 * track of tagged condition dependences and
2821 * tagged conditional validity dependences
2822 * in isl_sched_edge separately.
2823 * In particular, the conditional validity constraints on A
2824 * cannot be satisfied,
2825 * but they can be ignored because there are no corresponding
2826 * condition constraints. However, we do have an additional
2827 * conditional validity constraint that maps to the same
2828 * dependence relation
2829 * as the condition constraint on B. If we did not make a distinction
2830 * between tagged condition and tagged conditional validity
2831 * dependences, then we
2832 * could end up treating this shared dependence as an condition
2833 * constraint on A, forcing a localization of the conditions,
2834 * which is impossible.
2836 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
2837 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
2838 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
2839 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2840 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2841 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
2844 /* TACO 2013 Fig. 7 */
2845 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2846 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2847 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2848 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
2849 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
2850 "0 <= i < n and 0 <= j < n - 1 }",
2851 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
2852 "0 <= i < n and 0 <= j < j' < n;"
2853 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
2854 "0 <= i < i' < n and 0 <= j,j' < n;"
2855 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
2856 "0 <= i,j,j' < n and j < j' }",
2859 /* TACO 2013 Fig. 7, without tags */
2860 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2861 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2862 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2863 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2864 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2865 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
2866 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
2867 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
2870 /* TACO 2013 Fig. 12 */
2871 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
2872 "S3[i,3] : 0 <= i <= 1 }",
2873 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
2874 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
2875 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
2876 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
2877 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2878 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
2879 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2880 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
2881 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
2882 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
2883 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
2888 /* Test schedule construction based on conditional constraints.
2889 * In particular, check the number of members in the outer band
2890 * as an indication of whether tiling is possible or not.
2892 static int test_conditional_schedule_constraints(isl_ctx *ctx)
2894 int i;
2895 isl_union_set *domain;
2896 isl_union_map *condition;
2897 isl_union_map *flow;
2898 isl_union_map *validity;
2899 isl_schedule_constraints *sc;
2900 isl_schedule *schedule;
2901 isl_band_list *list;
2902 isl_band *band;
2903 int n_member;
2905 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
2906 domain = isl_union_set_read_from_str(ctx,
2907 live_range_tests[i].domain);
2908 flow = isl_union_map_read_from_str(ctx,
2909 live_range_tests[i].flow);
2910 condition = isl_union_map_read_from_str(ctx,
2911 live_range_tests[i].condition);
2912 validity = isl_union_map_read_from_str(ctx,
2913 live_range_tests[i].conditional_validity);
2914 sc = isl_schedule_constraints_on_domain(domain);
2915 sc = isl_schedule_constraints_set_validity(sc,
2916 isl_union_map_copy(flow));
2917 sc = isl_schedule_constraints_set_proximity(sc, flow);
2918 sc = isl_schedule_constraints_set_conditional_validity(sc,
2919 condition, validity);
2920 schedule = isl_schedule_constraints_compute_schedule(sc);
2921 list = isl_schedule_get_band_forest(schedule);
2922 band = isl_band_list_get_band(list, 0);
2923 n_member = isl_band_n_member(band);
2924 isl_band_free(band);
2925 isl_band_list_free(list);
2926 isl_schedule_free(schedule);
2928 if (!schedule)
2929 return -1;
2930 if (n_member != live_range_tests[i].outer_band_n)
2931 isl_die(ctx, isl_error_unknown,
2932 "unexpected number of members in outer band",
2933 return -1);
2935 return 0;
2938 /* Check that the schedule computed for the given instance set and
2939 * dependence relation strongly satisfies the dependences.
2940 * In particular, check that no instance is scheduled before
2941 * or together with an instance on which it depends.
2942 * Earlier versions of isl would produce a schedule that
2943 * only weakly satisfies the dependences.
2945 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
2947 const char *domain, *dep;
2948 isl_union_map *D, *schedule;
2949 isl_map *map, *ge;
2950 int empty;
2952 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
2953 "A[i0] : 0 <= i0 <= 1 }";
2954 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
2955 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
2956 schedule = compute_schedule(ctx, domain, dep, dep);
2957 D = isl_union_map_read_from_str(ctx, dep);
2958 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
2959 D = isl_union_map_apply_range(D, schedule);
2960 map = isl_map_from_union_map(D);
2961 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
2962 map = isl_map_intersect(map, ge);
2963 empty = isl_map_is_empty(map);
2964 isl_map_free(map);
2966 if (empty < 0)
2967 return -1;
2968 if (!empty)
2969 isl_die(ctx, isl_error_unknown,
2970 "dependences not strongly satisfied", return -1);
2972 return 0;
2975 /* Check that the dependence carrying step is not confused by
2976 * a bound on the coefficient size.
2977 * In particular, force the scheduler to move to a dependence carrying
2978 * step by demanding outer coincidence and bound the size of
2979 * the coefficients. Earlier versions of isl would take this
2980 * bound into account while carrying dependences, breaking
2981 * fundamental assumptions.
2983 static int test_bounded_coefficients_schedule(isl_ctx *ctx)
2985 const char *domain, *dep;
2986 isl_union_set *I;
2987 isl_union_map *D;
2988 isl_schedule_constraints *sc;
2989 isl_schedule *schedule;
2991 domain = "{ C[i0, i1] : 2 <= i0 <= 3999 and 0 <= i1 <= -1 + i0 }";
2992 dep = "{ C[i0, i1] -> C[i0, 1 + i1] : i0 <= 3999 and i1 >= 0 and "
2993 "i1 <= -2 + i0; "
2994 "C[i0, -1 + i0] -> C[1 + i0, 0] : i0 <= 3998 and i0 >= 1 }";
2995 I = isl_union_set_read_from_str(ctx, domain);
2996 D = isl_union_map_read_from_str(ctx, dep);
2997 sc = isl_schedule_constraints_on_domain(I);
2998 sc = isl_schedule_constraints_set_validity(sc, isl_union_map_copy(D));
2999 sc = isl_schedule_constraints_set_coincidence(sc, D);
3000 isl_options_set_schedule_outer_coincidence(ctx, 1);
3001 isl_options_set_schedule_max_coefficient(ctx, 20);
3002 schedule = isl_schedule_constraints_compute_schedule(sc);
3003 isl_options_set_schedule_max_coefficient(ctx, -1);
3004 isl_options_set_schedule_outer_coincidence(ctx, 0);
3005 isl_schedule_free(schedule);
3007 if (!schedule)
3008 return -1;
3010 return 0;
3013 int test_schedule(isl_ctx *ctx)
3015 const char *D, *W, *R, *V, *P, *S;
3017 /* Handle resulting schedule with zero bands. */
3018 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
3019 return -1;
3021 /* Jacobi */
3022 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
3023 W = "{ S1[t,i] -> a[t,i] }";
3024 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
3025 "S1[t,i] -> a[t-1,i+1] }";
3026 S = "{ S1[t,i] -> [t,i] }";
3027 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3028 return -1;
3030 /* Fig. 5 of CC2008 */
3031 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
3032 "j <= -1 + N }";
3033 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
3034 "j >= 2 and j <= -1 + N }";
3035 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
3036 "j >= 2 and j <= -1 + N; "
3037 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
3038 "j >= 2 and j <= -1 + N }";
3039 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3040 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3041 return -1;
3043 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
3044 W = "{ S1[i] -> a[i] }";
3045 R = "{ S2[i] -> a[i+1] }";
3046 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3047 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3048 return -1;
3050 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
3051 W = "{ S1[i] -> a[i] }";
3052 R = "{ S2[i] -> a[9-i] }";
3053 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3054 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3055 return -1;
3057 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
3058 W = "{ S1[i] -> a[i] }";
3059 R = "[N] -> { S2[i] -> a[N-1-i] }";
3060 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
3061 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
3062 return -1;
3064 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
3065 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
3066 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
3067 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
3068 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3069 return -1;
3071 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3072 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
3073 R = "{ S2[i,j] -> a[i-1,j] }";
3074 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3075 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3076 return -1;
3078 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3079 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
3080 R = "{ S2[i,j] -> a[i,j-1] }";
3081 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3082 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3083 return -1;
3085 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
3086 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
3087 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
3088 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
3089 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
3090 "S_0[] -> [0, 0, 0] }";
3091 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
3092 return -1;
3093 ctx->opt->schedule_parametric = 0;
3094 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3095 return -1;
3096 ctx->opt->schedule_parametric = 1;
3098 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
3099 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
3100 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
3101 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
3102 "S4[i] -> a[i,N] }";
3103 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
3104 "S4[i] -> [4,i,0] }";
3105 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3106 return -1;
3108 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
3109 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3110 "j <= N }";
3111 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3112 "j <= N; "
3113 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
3114 "j <= N }";
3115 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3116 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3117 return -1;
3119 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
3120 " S_2[t] : t >= 0 and t <= -1 + N; "
3121 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
3122 "i <= -1 + N }";
3123 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
3124 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
3125 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
3126 "i >= 0 and i <= -1 + N }";
3127 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
3128 "i >= 0 and i <= -1 + N; "
3129 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
3130 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
3131 " S_0[t] -> [0, t, 0] }";
3133 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3134 return -1;
3135 ctx->opt->schedule_parametric = 0;
3136 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3137 return -1;
3138 ctx->opt->schedule_parametric = 1;
3140 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
3141 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
3142 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
3143 return -1;
3145 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
3146 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
3147 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
3148 "j >= 0 and j <= -1 + N; "
3149 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3150 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
3151 "j >= 0 and j <= -1 + N; "
3152 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3153 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
3154 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3155 return -1;
3157 D = "{ S_0[i] : i >= 0 }";
3158 W = "{ S_0[i] -> a[i] : i >= 0 }";
3159 R = "{ S_0[i] -> a[0] : i >= 0 }";
3160 S = "{ S_0[i] -> [0, i, 0] }";
3161 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3162 return -1;
3164 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
3165 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
3166 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
3167 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
3168 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3169 return -1;
3171 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
3172 "k <= -1 + n and k >= 0 }";
3173 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
3174 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
3175 "k <= -1 + n and k >= 0; "
3176 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
3177 "k <= -1 + n and k >= 0; "
3178 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
3179 "k <= -1 + n and k >= 0 }";
3180 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
3181 ctx->opt->schedule_outer_coincidence = 1;
3182 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3183 return -1;
3184 ctx->opt->schedule_outer_coincidence = 0;
3186 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3187 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3188 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3189 "Stmt_for_body24[i0, i1, 1, 0]:"
3190 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3191 "Stmt_for_body7[i0, i1, i2]:"
3192 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3193 "i2 <= 7 }";
3195 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3196 "Stmt_for_body24[1, i1, i2, i3]:"
3197 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3198 "i2 >= 1;"
3199 "Stmt_for_body24[0, i1, i2, i3] -> "
3200 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3201 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3202 "i3 >= 0;"
3203 "Stmt_for_body24[0, i1, i2, i3] ->"
3204 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3205 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3206 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3207 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3208 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3209 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3210 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3211 "i1 <= 6 and i1 >= 0;"
3212 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3213 "Stmt_for_body7[i0, i1, i2] -> "
3214 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3215 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3216 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3217 "Stmt_for_body7[i0, i1, i2] -> "
3218 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3219 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3220 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3221 P = V;
3222 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3223 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3224 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3226 if (test_special_schedule(ctx, D, V, P, S) < 0)
3227 return -1;
3229 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3230 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3231 "j >= 1 and j <= 7;"
3232 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3233 "j >= 1 and j <= 8 }";
3234 P = "{ }";
3235 S = "{ S_0[i, j] -> [i + j, j] }";
3236 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3237 if (test_special_schedule(ctx, D, V, P, S) < 0)
3238 return -1;
3239 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3241 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3242 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3243 "j >= 0 and j <= -1 + i }";
3244 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3245 "i <= -1 + N and j >= 0;"
3246 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3247 "i <= -2 + N }";
3248 P = "{ }";
3249 S = "{ S_0[i, j] -> [i, j] }";
3250 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3251 if (test_special_schedule(ctx, D, V, P, S) < 0)
3252 return -1;
3253 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3255 /* Test both algorithms on a case with only proximity dependences. */
3256 D = "{ S[i,j] : 0 <= i <= 10 }";
3257 V = "{ }";
3258 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3259 S = "{ S[i, j] -> [j, i] }";
3260 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3261 if (test_special_schedule(ctx, D, V, P, S) < 0)
3262 return -1;
3263 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3264 if (test_special_schedule(ctx, D, V, P, S) < 0)
3265 return -1;
3267 D = "{ A[a]; B[] }";
3268 V = "{}";
3269 P = "{ A[a] -> B[] }";
3270 if (test_has_schedule(ctx, D, V, P) < 0)
3271 return -1;
3273 if (test_padded_schedule(ctx) < 0)
3274 return -1;
3276 /* Check that check for progress is not confused by rational
3277 * solution.
3279 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3280 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3281 "i0 <= -2 + N; "
3282 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3283 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3284 P = "{}";
3285 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3286 if (test_has_schedule(ctx, D, V, P) < 0)
3287 return -1;
3288 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3290 /* Check that we allow schedule rows that are only non-trivial
3291 * on some full-dimensional domains.
3293 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
3294 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
3295 "S1[j] -> S2[1] : 0 <= j <= 1 }";
3296 P = "{}";
3297 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3298 if (test_has_schedule(ctx, D, V, P) < 0)
3299 return -1;
3300 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3302 if (test_conditional_schedule_constraints(ctx) < 0)
3303 return -1;
3305 if (test_strongly_satisfying_schedule(ctx) < 0)
3306 return -1;
3308 if (test_bounded_coefficients_schedule(ctx) < 0)
3309 return -1;
3311 return 0;
3314 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3316 isl_union_map *umap;
3317 int test;
3319 umap = isl_union_map_read_from_str(ctx, str);
3320 test = isl_union_map_plain_is_injective(umap);
3321 isl_union_map_free(umap);
3322 if (test < 0)
3323 return -1;
3324 if (test == injective)
3325 return 0;
3326 if (injective)
3327 isl_die(ctx, isl_error_unknown,
3328 "map not detected as injective", return -1);
3329 else
3330 isl_die(ctx, isl_error_unknown,
3331 "map detected as injective", return -1);
3334 int test_injective(isl_ctx *ctx)
3336 const char *str;
3338 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
3339 return -1;
3340 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
3341 return -1;
3342 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
3343 return -1;
3344 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
3345 return -1;
3346 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
3347 return -1;
3348 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
3349 return -1;
3350 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
3351 return -1;
3352 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
3353 return -1;
3355 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
3356 if (test_plain_injective(ctx, str, 1))
3357 return -1;
3358 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
3359 if (test_plain_injective(ctx, str, 0))
3360 return -1;
3362 return 0;
3365 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
3367 isl_aff *aff2;
3368 int equal;
3370 if (!aff)
3371 return -1;
3373 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
3374 equal = isl_aff_plain_is_equal(aff, aff2);
3375 isl_aff_free(aff2);
3377 return equal;
3380 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
3382 int equal;
3384 equal = aff_plain_is_equal(aff, str);
3385 if (equal < 0)
3386 return -1;
3387 if (!equal)
3388 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
3389 "result not as expected", return -1);
3390 return 0;
3393 struct {
3394 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3395 __isl_take isl_aff *aff2);
3396 } aff_bin_op[] = {
3397 ['+'] = { &isl_aff_add },
3398 ['-'] = { &isl_aff_sub },
3399 ['*'] = { &isl_aff_mul },
3400 ['/'] = { &isl_aff_div },
3403 struct {
3404 const char *arg1;
3405 unsigned char op;
3406 const char *arg2;
3407 const char *res;
3408 } aff_bin_tests[] = {
3409 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
3410 "{ [i] -> [2i] }" },
3411 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
3412 "{ [i] -> [0] }" },
3413 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
3414 "{ [i] -> [2i] }" },
3415 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
3416 "{ [i] -> [2i] }" },
3417 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
3418 "{ [i] -> [i/2] }" },
3419 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
3420 "{ [i] -> [i] }" },
3421 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
3422 "{ [i] -> [NaN] }" },
3423 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
3424 "{ [i] -> [NaN] }" },
3425 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
3426 "{ [i] -> [NaN] }" },
3427 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
3428 "{ [i] -> [NaN] }" },
3429 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
3430 "{ [i] -> [NaN] }" },
3431 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
3432 "{ [i] -> [NaN] }" },
3433 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
3434 "{ [i] -> [NaN] }" },
3435 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
3436 "{ [i] -> [NaN] }" },
3437 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
3438 "{ [i] -> [NaN] }" },
3439 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
3440 "{ [i] -> [NaN] }" },
3441 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
3442 "{ [i] -> [NaN] }" },
3443 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
3444 "{ [i] -> [NaN] }" },
3447 /* Perform some basic tests of binary operations on isl_aff objects.
3449 static int test_bin_aff(isl_ctx *ctx)
3451 int i;
3452 isl_aff *aff1, *aff2, *res;
3453 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3454 __isl_take isl_aff *aff2);
3455 int ok;
3457 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
3458 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
3459 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
3460 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
3461 fn = aff_bin_op[aff_bin_tests[i].op].fn;
3462 aff1 = fn(aff1, aff2);
3463 if (isl_aff_is_nan(res))
3464 ok = isl_aff_is_nan(aff1);
3465 else
3466 ok = isl_aff_plain_is_equal(aff1, res);
3467 isl_aff_free(aff1);
3468 isl_aff_free(res);
3469 if (ok < 0)
3470 return -1;
3471 if (!ok)
3472 isl_die(ctx, isl_error_unknown,
3473 "unexpected result", return -1);
3476 return 0;
3479 int test_aff(isl_ctx *ctx)
3481 const char *str;
3482 isl_set *set;
3483 isl_space *space;
3484 isl_local_space *ls;
3485 isl_aff *aff;
3486 int zero, equal;
3488 if (test_bin_aff(ctx) < 0)
3489 return -1;
3491 space = isl_space_set_alloc(ctx, 0, 1);
3492 ls = isl_local_space_from_space(space);
3493 aff = isl_aff_zero_on_domain(ls);
3495 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3496 aff = isl_aff_scale_down_ui(aff, 3);
3497 aff = isl_aff_floor(aff);
3498 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3499 aff = isl_aff_scale_down_ui(aff, 2);
3500 aff = isl_aff_floor(aff);
3501 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3503 str = "{ [10] }";
3504 set = isl_set_read_from_str(ctx, str);
3505 aff = isl_aff_gist(aff, set);
3507 aff = isl_aff_add_constant_si(aff, -16);
3508 zero = isl_aff_plain_is_zero(aff);
3509 isl_aff_free(aff);
3511 if (zero < 0)
3512 return -1;
3513 if (!zero)
3514 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3516 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
3517 aff = isl_aff_scale_down_ui(aff, 64);
3518 aff = isl_aff_floor(aff);
3519 equal = aff_check_plain_equal(aff, "{ [-1] }");
3520 isl_aff_free(aff);
3521 if (equal < 0)
3522 return -1;
3524 return 0;
3527 int test_dim_max(isl_ctx *ctx)
3529 int equal;
3530 const char *str;
3531 isl_set *set1, *set2;
3532 isl_set *set;
3533 isl_map *map;
3534 isl_pw_aff *pwaff;
3536 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
3537 set = isl_set_read_from_str(ctx, str);
3538 pwaff = isl_set_dim_max(set, 0);
3539 set1 = isl_set_from_pw_aff(pwaff);
3540 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
3541 set2 = isl_set_read_from_str(ctx, str);
3542 equal = isl_set_is_equal(set1, set2);
3543 isl_set_free(set1);
3544 isl_set_free(set2);
3545 if (equal < 0)
3546 return -1;
3547 if (!equal)
3548 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3550 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
3551 set = isl_set_read_from_str(ctx, str);
3552 pwaff = isl_set_dim_max(set, 0);
3553 set1 = isl_set_from_pw_aff(pwaff);
3554 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3555 set2 = isl_set_read_from_str(ctx, str);
3556 equal = isl_set_is_equal(set1, set2);
3557 isl_set_free(set1);
3558 isl_set_free(set2);
3559 if (equal < 0)
3560 return -1;
3561 if (!equal)
3562 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3564 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
3565 set = isl_set_read_from_str(ctx, str);
3566 pwaff = isl_set_dim_max(set, 0);
3567 set1 = isl_set_from_pw_aff(pwaff);
3568 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3569 set2 = isl_set_read_from_str(ctx, str);
3570 equal = isl_set_is_equal(set1, set2);
3571 isl_set_free(set1);
3572 isl_set_free(set2);
3573 if (equal < 0)
3574 return -1;
3575 if (!equal)
3576 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3578 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
3579 "0 <= i < N and 0 <= j < M }";
3580 map = isl_map_read_from_str(ctx, str);
3581 set = isl_map_range(map);
3583 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
3584 set1 = isl_set_from_pw_aff(pwaff);
3585 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
3586 set2 = isl_set_read_from_str(ctx, str);
3587 equal = isl_set_is_equal(set1, set2);
3588 isl_set_free(set1);
3589 isl_set_free(set2);
3591 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3592 set1 = isl_set_from_pw_aff(pwaff);
3593 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3594 set2 = isl_set_read_from_str(ctx, str);
3595 if (equal >= 0 && equal)
3596 equal = isl_set_is_equal(set1, set2);
3597 isl_set_free(set1);
3598 isl_set_free(set2);
3600 isl_set_free(set);
3602 if (equal < 0)
3603 return -1;
3604 if (!equal)
3605 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3607 /* Check that solutions are properly merged. */
3608 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3609 "c <= -1 + n - 4a - 2b and c >= -2b and "
3610 "4a >= -4 + n and c >= 0 }";
3611 set = isl_set_read_from_str(ctx, str);
3612 pwaff = isl_set_dim_min(set, 2);
3613 set1 = isl_set_from_pw_aff(pwaff);
3614 str = "[n] -> { [(0)] : n >= 1 }";
3615 set2 = isl_set_read_from_str(ctx, str);
3616 equal = isl_set_is_equal(set1, set2);
3617 isl_set_free(set1);
3618 isl_set_free(set2);
3620 if (equal < 0)
3621 return -1;
3622 if (!equal)
3623 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3625 /* Check that empty solution lie in the right space. */
3626 str = "[n] -> { [t,a] : 1 = 0 }";
3627 set = isl_set_read_from_str(ctx, str);
3628 pwaff = isl_set_dim_max(set, 0);
3629 set1 = isl_set_from_pw_aff(pwaff);
3630 str = "[n] -> { [t] : 1 = 0 }";
3631 set2 = isl_set_read_from_str(ctx, str);
3632 equal = isl_set_is_equal(set1, set2);
3633 isl_set_free(set1);
3634 isl_set_free(set2);
3636 if (equal < 0)
3637 return -1;
3638 if (!equal)
3639 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3641 return 0;
3644 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
3646 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
3647 const char *str)
3649 isl_ctx *ctx;
3650 isl_pw_multi_aff *pma2;
3651 int equal;
3653 if (!pma)
3654 return -1;
3656 ctx = isl_pw_multi_aff_get_ctx(pma);
3657 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3658 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
3659 isl_pw_multi_aff_free(pma2);
3661 return equal;
3664 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
3665 * represented by "str".
3667 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
3668 const char *str)
3670 int equal;
3672 equal = pw_multi_aff_plain_is_equal(pma, str);
3673 if (equal < 0)
3674 return -1;
3675 if (!equal)
3676 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
3677 "result not as expected", return -1);
3678 return 0;
3681 /* Basic test for isl_pw_multi_aff_product.
3683 * Check that multiple pieces are properly handled.
3685 static int test_product_pma(isl_ctx *ctx)
3687 int equal;
3688 const char *str;
3689 isl_pw_multi_aff *pma1, *pma2;
3691 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
3692 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3693 str = "{ C[] -> D[] }";
3694 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3695 pma1 = isl_pw_multi_aff_product(pma1, pma2);
3696 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
3697 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
3698 equal = pw_multi_aff_check_plain_equal(pma1, str);
3699 isl_pw_multi_aff_free(pma1);
3700 if (equal < 0)
3701 return -1;
3703 return 0;
3706 int test_product(isl_ctx *ctx)
3708 const char *str;
3709 isl_set *set;
3710 isl_union_set *uset1, *uset2;
3711 int ok;
3713 str = "{ A[i] }";
3714 set = isl_set_read_from_str(ctx, str);
3715 set = isl_set_product(set, isl_set_copy(set));
3716 ok = isl_set_is_wrapping(set);
3717 isl_set_free(set);
3718 if (ok < 0)
3719 return -1;
3720 if (!ok)
3721 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3723 str = "{ [] }";
3724 uset1 = isl_union_set_read_from_str(ctx, str);
3725 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3726 str = "{ [[] -> []] }";
3727 uset2 = isl_union_set_read_from_str(ctx, str);
3728 ok = isl_union_set_is_equal(uset1, uset2);
3729 isl_union_set_free(uset1);
3730 isl_union_set_free(uset2);
3731 if (ok < 0)
3732 return -1;
3733 if (!ok)
3734 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3736 if (test_product_pma(ctx) < 0)
3737 return -1;
3739 return 0;
3742 /* Check that two sets are not considered disjoint just because
3743 * they have a different set of (named) parameters.
3745 static int test_disjoint(isl_ctx *ctx)
3747 const char *str;
3748 isl_set *set, *set2;
3749 int disjoint;
3751 str = "[n] -> { [[]->[]] }";
3752 set = isl_set_read_from_str(ctx, str);
3753 str = "{ [[]->[]] }";
3754 set2 = isl_set_read_from_str(ctx, str);
3755 disjoint = isl_set_is_disjoint(set, set2);
3756 isl_set_free(set);
3757 isl_set_free(set2);
3758 if (disjoint < 0)
3759 return -1;
3760 if (disjoint)
3761 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3763 return 0;
3766 int test_equal(isl_ctx *ctx)
3768 const char *str;
3769 isl_set *set, *set2;
3770 int equal;
3772 str = "{ S_6[i] }";
3773 set = isl_set_read_from_str(ctx, str);
3774 str = "{ S_7[i] }";
3775 set2 = isl_set_read_from_str(ctx, str);
3776 equal = isl_set_is_equal(set, set2);
3777 isl_set_free(set);
3778 isl_set_free(set2);
3779 if (equal < 0)
3780 return -1;
3781 if (equal)
3782 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3784 return 0;
3787 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3788 enum isl_dim_type type, unsigned pos, int fixed)
3790 int test;
3792 test = isl_map_plain_is_fixed(map, type, pos, NULL);
3793 isl_map_free(map);
3794 if (test < 0)
3795 return -1;
3796 if (test == fixed)
3797 return 0;
3798 if (fixed)
3799 isl_die(ctx, isl_error_unknown,
3800 "map not detected as fixed", return -1);
3801 else
3802 isl_die(ctx, isl_error_unknown,
3803 "map detected as fixed", return -1);
3806 int test_fixed(isl_ctx *ctx)
3808 const char *str;
3809 isl_map *map;
3811 str = "{ [i] -> [i] }";
3812 map = isl_map_read_from_str(ctx, str);
3813 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3814 return -1;
3815 str = "{ [i] -> [1] }";
3816 map = isl_map_read_from_str(ctx, str);
3817 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3818 return -1;
3819 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3820 map = isl_map_read_from_str(ctx, str);
3821 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3822 return -1;
3823 map = isl_map_read_from_str(ctx, str);
3824 map = isl_map_neg(map);
3825 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3826 return -1;
3828 return 0;
3831 struct isl_vertices_test_data {
3832 const char *set;
3833 int n;
3834 const char *vertex[2];
3835 } vertices_tests[] = {
3836 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
3837 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
3838 { "{ A[t, i] : t = 14 and i = 1 }",
3839 1, { "{ A[14, 1] }" } },
3842 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
3844 static int find_vertex(__isl_take isl_vertex *vertex, void *user)
3846 struct isl_vertices_test_data *data = user;
3847 isl_ctx *ctx;
3848 isl_multi_aff *ma;
3849 isl_basic_set *bset;
3850 isl_pw_multi_aff *pma;
3851 int i;
3852 int equal;
3854 ctx = isl_vertex_get_ctx(vertex);
3855 bset = isl_vertex_get_domain(vertex);
3856 ma = isl_vertex_get_expr(vertex);
3857 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
3859 for (i = 0; i < data->n; ++i) {
3860 isl_pw_multi_aff *pma_i;
3862 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
3863 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
3864 isl_pw_multi_aff_free(pma_i);
3866 if (equal < 0 || equal)
3867 break;
3870 isl_pw_multi_aff_free(pma);
3871 isl_vertex_free(vertex);
3873 if (equal < 0)
3874 return -1;
3876 return equal ? 0 : - 1;
3879 int test_vertices(isl_ctx *ctx)
3881 int i;
3883 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
3884 isl_basic_set *bset;
3885 isl_vertices *vertices;
3886 int ok = 1;
3887 int n;
3889 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
3890 vertices = isl_basic_set_compute_vertices(bset);
3891 n = isl_vertices_get_n_vertices(vertices);
3892 if (vertices_tests[i].n != n)
3893 ok = 0;
3894 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
3895 &vertices_tests[i]) < 0)
3896 ok = 0;
3897 isl_vertices_free(vertices);
3898 isl_basic_set_free(bset);
3900 if (!vertices)
3901 return -1;
3902 if (!ok)
3903 isl_die(ctx, isl_error_unknown, "unexpected vertices",
3904 return -1);
3907 return 0;
3910 int test_union_pw(isl_ctx *ctx)
3912 int equal;
3913 const char *str;
3914 isl_union_set *uset;
3915 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
3917 str = "{ [x] -> x^2 }";
3918 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
3919 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
3920 uset = isl_union_pw_qpolynomial_domain(upwqp1);
3921 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
3922 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
3923 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
3924 isl_union_pw_qpolynomial_free(upwqp1);
3925 isl_union_pw_qpolynomial_free(upwqp2);
3926 if (equal < 0)
3927 return -1;
3928 if (!equal)
3929 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3931 return 0;
3934 int test_output(isl_ctx *ctx)
3936 char *s;
3937 const char *str;
3938 isl_pw_aff *pa;
3939 isl_printer *p;
3940 int equal;
3942 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
3943 pa = isl_pw_aff_read_from_str(ctx, str);
3945 p = isl_printer_to_str(ctx);
3946 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
3947 p = isl_printer_print_pw_aff(p, pa);
3948 s = isl_printer_get_str(p);
3949 isl_printer_free(p);
3950 isl_pw_aff_free(pa);
3951 if (!s)
3952 equal = -1;
3953 else
3954 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
3955 free(s);
3956 if (equal < 0)
3957 return -1;
3958 if (!equal)
3959 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3961 return 0;
3964 int test_sample(isl_ctx *ctx)
3966 const char *str;
3967 isl_basic_set *bset1, *bset2;
3968 int empty, subset;
3970 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3971 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3972 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3973 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3974 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3975 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3976 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3977 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3978 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3979 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3980 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3981 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3982 "d - 1073741821e and "
3983 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3984 "3j >= 1 - a + b + 2e and "
3985 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3986 "3i <= 4 - a + 4b - e and "
3987 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3988 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3989 "c <= -1 + a and 3i >= -2 - a + 3e and "
3990 "1073741822e <= 1073741823 - a + 1073741822b + c and "
3991 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3992 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3993 "1073741823e >= 1 + 1073741823b - d and "
3994 "3i >= 1073741823b + c - 1073741823e - f and "
3995 "3i >= 1 + 2b + e + 3g }";
3996 bset1 = isl_basic_set_read_from_str(ctx, str);
3997 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3998 empty = isl_basic_set_is_empty(bset2);
3999 subset = isl_basic_set_is_subset(bset2, bset1);
4000 isl_basic_set_free(bset1);
4001 isl_basic_set_free(bset2);
4002 if (empty < 0 || subset < 0)
4003 return -1;
4004 if (empty)
4005 isl_die(ctx, isl_error_unknown, "point not found", return -1);
4006 if (!subset)
4007 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
4009 return 0;
4012 int test_fixed_power(isl_ctx *ctx)
4014 const char *str;
4015 isl_map *map;
4016 isl_int exp;
4017 int equal;
4019 isl_int_init(exp);
4020 str = "{ [i] -> [i + 1] }";
4021 map = isl_map_read_from_str(ctx, str);
4022 isl_int_set_si(exp, 23);
4023 map = isl_map_fixed_power(map, exp);
4024 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
4025 isl_int_clear(exp);
4026 isl_map_free(map);
4027 if (equal < 0)
4028 return -1;
4030 return 0;
4033 int test_slice(isl_ctx *ctx)
4035 const char *str;
4036 isl_map *map;
4037 int equal;
4039 str = "{ [i] -> [j] }";
4040 map = isl_map_read_from_str(ctx, str);
4041 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
4042 equal = map_check_equal(map, "{ [i] -> [i] }");
4043 isl_map_free(map);
4044 if (equal < 0)
4045 return -1;
4047 str = "{ [i] -> [j] }";
4048 map = isl_map_read_from_str(ctx, str);
4049 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
4050 equal = map_check_equal(map, "{ [i] -> [j] }");
4051 isl_map_free(map);
4052 if (equal < 0)
4053 return -1;
4055 str = "{ [i] -> [j] }";
4056 map = isl_map_read_from_str(ctx, str);
4057 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
4058 equal = map_check_equal(map, "{ [i] -> [-i] }");
4059 isl_map_free(map);
4060 if (equal < 0)
4061 return -1;
4063 str = "{ [i] -> [j] }";
4064 map = isl_map_read_from_str(ctx, str);
4065 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
4066 equal = map_check_equal(map, "{ [0] -> [j] }");
4067 isl_map_free(map);
4068 if (equal < 0)
4069 return -1;
4071 str = "{ [i] -> [j] }";
4072 map = isl_map_read_from_str(ctx, str);
4073 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
4074 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
4075 isl_map_free(map);
4076 if (equal < 0)
4077 return -1;
4079 str = "{ [i] -> [j] }";
4080 map = isl_map_read_from_str(ctx, str);
4081 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
4082 equal = map_check_equal(map, "{ [i] -> [j] : false }");
4083 isl_map_free(map);
4084 if (equal < 0)
4085 return -1;
4087 return 0;
4090 int test_eliminate(isl_ctx *ctx)
4092 const char *str;
4093 isl_map *map;
4094 int equal;
4096 str = "{ [i] -> [j] : i = 2j }";
4097 map = isl_map_read_from_str(ctx, str);
4098 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
4099 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
4100 isl_map_free(map);
4101 if (equal < 0)
4102 return -1;
4104 return 0;
4107 /* Check that isl_set_dim_residue_class detects that the values of j
4108 * in the set below are all odd and that it does not detect any spurious
4109 * strides.
4111 static int test_residue_class(isl_ctx *ctx)
4113 const char *str;
4114 isl_set *set;
4115 isl_int m, r;
4116 int res;
4118 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
4119 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
4120 set = isl_set_read_from_str(ctx, str);
4121 isl_int_init(m);
4122 isl_int_init(r);
4123 res = isl_set_dim_residue_class(set, 1, &m, &r);
4124 if (res >= 0 &&
4125 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
4126 isl_die(ctx, isl_error_unknown, "incorrect residue class",
4127 res = -1);
4128 isl_int_clear(r);
4129 isl_int_clear(m);
4130 isl_set_free(set);
4132 return res;
4135 int test_align_parameters(isl_ctx *ctx)
4137 const char *str;
4138 isl_space *space;
4139 isl_multi_aff *ma1, *ma2;
4140 int equal;
4142 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
4143 ma1 = isl_multi_aff_read_from_str(ctx, str);
4145 space = isl_space_params_alloc(ctx, 1);
4146 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
4147 ma1 = isl_multi_aff_align_params(ma1, space);
4149 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
4150 ma2 = isl_multi_aff_read_from_str(ctx, str);
4152 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4154 isl_multi_aff_free(ma1);
4155 isl_multi_aff_free(ma2);
4157 if (equal < 0)
4158 return -1;
4159 if (!equal)
4160 isl_die(ctx, isl_error_unknown,
4161 "result not as expected", return -1);
4163 return 0;
4166 static int test_list(isl_ctx *ctx)
4168 isl_id *a, *b, *c, *d, *id;
4169 isl_id_list *list;
4170 int ok;
4172 a = isl_id_alloc(ctx, "a", NULL);
4173 b = isl_id_alloc(ctx, "b", NULL);
4174 c = isl_id_alloc(ctx, "c", NULL);
4175 d = isl_id_alloc(ctx, "d", NULL);
4177 list = isl_id_list_alloc(ctx, 4);
4178 list = isl_id_list_add(list, a);
4179 list = isl_id_list_add(list, b);
4180 list = isl_id_list_add(list, c);
4181 list = isl_id_list_add(list, d);
4182 list = isl_id_list_drop(list, 1, 1);
4184 if (isl_id_list_n_id(list) != 3) {
4185 isl_id_list_free(list);
4186 isl_die(ctx, isl_error_unknown,
4187 "unexpected number of elements in list", return -1);
4190 id = isl_id_list_get_id(list, 0);
4191 ok = id == a;
4192 isl_id_free(id);
4193 id = isl_id_list_get_id(list, 1);
4194 ok = ok && id == c;
4195 isl_id_free(id);
4196 id = isl_id_list_get_id(list, 2);
4197 ok = ok && id == d;
4198 isl_id_free(id);
4200 isl_id_list_free(list);
4202 if (!ok)
4203 isl_die(ctx, isl_error_unknown,
4204 "unexpected elements in list", return -1);
4206 return 0;
4209 const char *set_conversion_tests[] = {
4210 "[N] -> { [i] : N - 1 <= 2 i <= N }",
4211 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
4212 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4213 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4214 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
4217 /* Check that converting from isl_set to isl_pw_multi_aff and back
4218 * to isl_set produces the original isl_set.
4220 static int test_set_conversion(isl_ctx *ctx)
4222 int i;
4223 const char *str;
4224 isl_set *set1, *set2;
4225 isl_pw_multi_aff *pma;
4226 int equal;
4228 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
4229 str = set_conversion_tests[i];
4230 set1 = isl_set_read_from_str(ctx, str);
4231 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
4232 set2 = isl_set_from_pw_multi_aff(pma);
4233 equal = isl_set_is_equal(set1, set2);
4234 isl_set_free(set1);
4235 isl_set_free(set2);
4237 if (equal < 0)
4238 return -1;
4239 if (!equal)
4240 isl_die(ctx, isl_error_unknown, "bad conversion",
4241 return -1);
4244 return 0;
4247 /* Check that converting from isl_map to isl_pw_multi_aff and back
4248 * to isl_map produces the original isl_map.
4250 static int test_map_conversion(isl_ctx *ctx)
4252 const char *str;
4253 isl_map *map1, *map2;
4254 isl_pw_multi_aff *pma;
4255 int equal;
4257 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
4258 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
4259 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
4260 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
4261 "9e <= -2 - 2a) }";
4262 map1 = isl_map_read_from_str(ctx, str);
4263 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
4264 map2 = isl_map_from_pw_multi_aff(pma);
4265 equal = isl_map_is_equal(map1, map2);
4266 isl_map_free(map1);
4267 isl_map_free(map2);
4269 if (equal < 0)
4270 return -1;
4271 if (!equal)
4272 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
4274 return 0;
4277 static int test_conversion(isl_ctx *ctx)
4279 if (test_set_conversion(ctx) < 0)
4280 return -1;
4281 if (test_map_conversion(ctx) < 0)
4282 return -1;
4283 return 0;
4286 /* Check that isl_basic_map_curry does not modify input.
4288 static int test_curry(isl_ctx *ctx)
4290 const char *str;
4291 isl_basic_map *bmap1, *bmap2;
4292 int equal;
4294 str = "{ [A[] -> B[]] -> C[] }";
4295 bmap1 = isl_basic_map_read_from_str(ctx, str);
4296 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
4297 equal = isl_basic_map_is_equal(bmap1, bmap2);
4298 isl_basic_map_free(bmap1);
4299 isl_basic_map_free(bmap2);
4301 if (equal < 0)
4302 return -1;
4303 if (equal)
4304 isl_die(ctx, isl_error_unknown,
4305 "curried map should not be equal to original",
4306 return -1);
4308 return 0;
4311 struct {
4312 const char *set;
4313 const char *ma;
4314 const char *res;
4315 } preimage_tests[] = {
4316 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
4317 "{ A[j,i] -> B[i,j] }",
4318 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
4319 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4320 "{ A[a,b] -> B[a/2,b/6] }",
4321 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
4322 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4323 "{ A[a,b] -> B[a/2,b/6] }",
4324 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
4325 "exists i,j : a = 2 i and b = 6 j }" },
4326 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
4327 "[n] -> { : 0 <= n <= 100 }" },
4328 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4329 "{ A[a] -> B[2a] }",
4330 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
4331 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4332 "{ A[a] -> B[([a/2])] }",
4333 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
4334 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
4335 "{ A[a] -> B[a,a,a/3] }",
4336 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
4337 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
4338 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
4341 static int test_preimage_basic_set(isl_ctx *ctx)
4343 int i;
4344 isl_basic_set *bset1, *bset2;
4345 isl_multi_aff *ma;
4346 int equal;
4348 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
4349 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
4350 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
4351 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
4352 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
4353 equal = isl_basic_set_is_equal(bset1, bset2);
4354 isl_basic_set_free(bset1);
4355 isl_basic_set_free(bset2);
4356 if (equal < 0)
4357 return -1;
4358 if (!equal)
4359 isl_die(ctx, isl_error_unknown, "bad preimage",
4360 return -1);
4363 return 0;
4366 struct {
4367 const char *map;
4368 const char *ma;
4369 const char *res;
4370 } preimage_domain_tests[] = {
4371 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
4372 "{ A[j,i] -> B[i,j] }",
4373 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
4374 { "{ B[i] -> C[i]; D[i] -> E[i] }",
4375 "{ A[i] -> B[i + 1] }",
4376 "{ A[i] -> C[i + 1] }" },
4377 { "{ B[i] -> C[i]; B[i] -> E[i] }",
4378 "{ A[i] -> B[i + 1] }",
4379 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
4380 { "{ B[i] -> C[([i/2])] }",
4381 "{ A[i] -> B[2i] }",
4382 "{ A[i] -> C[i] }" },
4383 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
4384 "{ A[i] -> B[([i/5]), ([i/7])] }",
4385 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
4386 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
4387 "[N] -> { A[] -> B[([N/5])] }",
4388 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
4389 { "{ B[i] -> C[i] : exists a : i = 5 a }",
4390 "{ A[i] -> B[2i] }",
4391 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
4392 { "{ B[i] -> C[i] : exists a : i = 2 a; "
4393 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
4394 "{ A[i] -> B[2i] }",
4395 "{ A[i] -> C[2i] }" },
4398 static int test_preimage_union_map(isl_ctx *ctx)
4400 int i;
4401 isl_union_map *umap1, *umap2;
4402 isl_multi_aff *ma;
4403 int equal;
4405 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
4406 umap1 = isl_union_map_read_from_str(ctx,
4407 preimage_domain_tests[i].map);
4408 ma = isl_multi_aff_read_from_str(ctx,
4409 preimage_domain_tests[i].ma);
4410 umap2 = isl_union_map_read_from_str(ctx,
4411 preimage_domain_tests[i].res);
4412 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
4413 equal = isl_union_map_is_equal(umap1, umap2);
4414 isl_union_map_free(umap1);
4415 isl_union_map_free(umap2);
4416 if (equal < 0)
4417 return -1;
4418 if (!equal)
4419 isl_die(ctx, isl_error_unknown, "bad preimage",
4420 return -1);
4423 return 0;
4426 static int test_preimage(isl_ctx *ctx)
4428 if (test_preimage_basic_set(ctx) < 0)
4429 return -1;
4430 if (test_preimage_union_map(ctx) < 0)
4431 return -1;
4433 return 0;
4436 struct {
4437 const char *ma1;
4438 const char *ma;
4439 const char *res;
4440 } pullback_tests[] = {
4441 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
4442 "{ A[a,b] -> C[b + 2a] }" },
4443 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
4444 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
4445 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
4446 "{ A[a] -> C[(a)/6] }" },
4447 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
4448 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
4449 "{ A[a] -> C[(2a)/3] }" },
4450 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
4451 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
4452 "{ A[i,j] -> C[i + j, i + j] }"},
4453 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
4454 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
4455 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
4456 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
4457 "{ [i, j] -> [floor((i)/3), j] }",
4458 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
4461 static int test_pullback(isl_ctx *ctx)
4463 int i;
4464 isl_multi_aff *ma1, *ma2;
4465 isl_multi_aff *ma;
4466 int equal;
4468 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
4469 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
4470 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
4471 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
4472 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
4473 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4474 isl_multi_aff_free(ma1);
4475 isl_multi_aff_free(ma2);
4476 if (equal < 0)
4477 return -1;
4478 if (!equal)
4479 isl_die(ctx, isl_error_unknown, "bad pullback",
4480 return -1);
4483 return 0;
4486 /* Check that negation is printed correctly and that equal expressions
4487 * are correctly identified.
4489 static int test_ast(isl_ctx *ctx)
4491 isl_ast_expr *expr, *expr1, *expr2, *expr3;
4492 char *str;
4493 int ok, equal;
4495 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4496 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4497 expr = isl_ast_expr_add(expr1, expr2);
4498 expr2 = isl_ast_expr_copy(expr);
4499 expr = isl_ast_expr_neg(expr);
4500 expr2 = isl_ast_expr_neg(expr2);
4501 equal = isl_ast_expr_is_equal(expr, expr2);
4502 str = isl_ast_expr_to_str(expr);
4503 ok = str ? !strcmp(str, "-(A + B)") : -1;
4504 free(str);
4505 isl_ast_expr_free(expr);
4506 isl_ast_expr_free(expr2);
4508 if (ok < 0 || equal < 0)
4509 return -1;
4510 if (!equal)
4511 isl_die(ctx, isl_error_unknown,
4512 "equal expressions not considered equal", return -1);
4513 if (!ok)
4514 isl_die(ctx, isl_error_unknown,
4515 "isl_ast_expr printed incorrectly", return -1);
4517 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4518 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4519 expr = isl_ast_expr_add(expr1, expr2);
4520 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
4521 expr = isl_ast_expr_sub(expr3, expr);
4522 str = isl_ast_expr_to_str(expr);
4523 ok = str ? !strcmp(str, "C - (A + B)") : -1;
4524 free(str);
4525 isl_ast_expr_free(expr);
4527 if (ok < 0)
4528 return -1;
4529 if (!ok)
4530 isl_die(ctx, isl_error_unknown,
4531 "isl_ast_expr printed incorrectly", return -1);
4533 return 0;
4536 /* Check that isl_ast_build_expr_from_set returns a valid expression
4537 * for an empty set. Note that isl_ast_build_expr_from_set getting
4538 * called on an empty set probably indicates a bug in the caller.
4540 static int test_ast_build(isl_ctx *ctx)
4542 isl_set *set;
4543 isl_ast_build *build;
4544 isl_ast_expr *expr;
4546 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4547 build = isl_ast_build_from_context(set);
4549 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
4550 expr = isl_ast_build_expr_from_set(build, set);
4552 isl_ast_expr_free(expr);
4553 isl_ast_build_free(build);
4555 if (!expr)
4556 return -1;
4558 return 0;
4561 /* Internal data structure for before_for and after_for callbacks.
4563 * depth is the current depth
4564 * before is the number of times before_for has been called
4565 * after is the number of times after_for has been called
4567 struct isl_test_codegen_data {
4568 int depth;
4569 int before;
4570 int after;
4573 /* This function is called before each for loop in the AST generated
4574 * from test_ast_gen1.
4576 * Increment the number of calls and the depth.
4577 * Check that the space returned by isl_ast_build_get_schedule_space
4578 * matches the target space of the schedule returned by
4579 * isl_ast_build_get_schedule.
4580 * Return an isl_id that is checked by the corresponding call
4581 * to after_for.
4583 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
4584 void *user)
4586 struct isl_test_codegen_data *data = user;
4587 isl_ctx *ctx;
4588 isl_space *space;
4589 isl_union_map *schedule;
4590 isl_union_set *uset;
4591 isl_set *set;
4592 int empty;
4593 char name[] = "d0";
4595 ctx = isl_ast_build_get_ctx(build);
4597 if (data->before >= 3)
4598 isl_die(ctx, isl_error_unknown,
4599 "unexpected number of for nodes", return NULL);
4600 if (data->depth >= 2)
4601 isl_die(ctx, isl_error_unknown,
4602 "unexpected depth", return NULL);
4604 snprintf(name, sizeof(name), "d%d", data->depth);
4605 data->before++;
4606 data->depth++;
4608 schedule = isl_ast_build_get_schedule(build);
4609 uset = isl_union_map_range(schedule);
4610 if (!uset)
4611 return NULL;
4612 if (isl_union_set_n_set(uset) != 1) {
4613 isl_union_set_free(uset);
4614 isl_die(ctx, isl_error_unknown,
4615 "expecting single range space", return NULL);
4618 space = isl_ast_build_get_schedule_space(build);
4619 set = isl_union_set_extract_set(uset, space);
4620 isl_union_set_free(uset);
4621 empty = isl_set_is_empty(set);
4622 isl_set_free(set);
4624 if (empty < 0)
4625 return NULL;
4626 if (empty)
4627 isl_die(ctx, isl_error_unknown,
4628 "spaces don't match", return NULL);
4630 return isl_id_alloc(ctx, name, NULL);
4633 /* This function is called after each for loop in the AST generated
4634 * from test_ast_gen1.
4636 * Increment the number of calls and decrement the depth.
4637 * Check that the annotation attached to the node matches
4638 * the isl_id returned by the corresponding call to before_for.
4640 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
4641 __isl_keep isl_ast_build *build, void *user)
4643 struct isl_test_codegen_data *data = user;
4644 isl_id *id;
4645 const char *name;
4646 int valid;
4648 data->after++;
4649 data->depth--;
4651 if (data->after > data->before)
4652 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4653 "mismatch in number of for nodes",
4654 return isl_ast_node_free(node));
4656 id = isl_ast_node_get_annotation(node);
4657 if (!id)
4658 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4659 "missing annotation", return isl_ast_node_free(node));
4661 name = isl_id_get_name(id);
4662 valid = name && atoi(name + 1) == data->depth;
4663 isl_id_free(id);
4665 if (!valid)
4666 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4667 "wrong annotation", return isl_ast_node_free(node));
4669 return node;
4672 /* Check that the before_each_for and after_each_for callbacks
4673 * are called for each for loop in the generated code,
4674 * that they are called in the right order and that the isl_id
4675 * returned from the before_each_for callback is attached to
4676 * the isl_ast_node passed to the corresponding after_each_for call.
4678 static int test_ast_gen1(isl_ctx *ctx)
4680 const char *str;
4681 isl_set *set;
4682 isl_union_map *schedule;
4683 isl_ast_build *build;
4684 isl_ast_node *tree;
4685 struct isl_test_codegen_data data;
4687 str = "[N] -> { : N >= 10 }";
4688 set = isl_set_read_from_str(ctx, str);
4689 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
4690 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
4691 schedule = isl_union_map_read_from_str(ctx, str);
4693 data.before = 0;
4694 data.after = 0;
4695 data.depth = 0;
4696 build = isl_ast_build_from_context(set);
4697 build = isl_ast_build_set_before_each_for(build,
4698 &before_for, &data);
4699 build = isl_ast_build_set_after_each_for(build,
4700 &after_for, &data);
4701 tree = isl_ast_build_ast_from_schedule(build, schedule);
4702 isl_ast_build_free(build);
4703 if (!tree)
4704 return -1;
4706 isl_ast_node_free(tree);
4708 if (data.before != 3 || data.after != 3)
4709 isl_die(ctx, isl_error_unknown,
4710 "unexpected number of for nodes", return -1);
4712 return 0;
4715 /* Check that the AST generator handles domains that are integrally disjoint
4716 * but not ratinoally disjoint.
4718 static int test_ast_gen2(isl_ctx *ctx)
4720 const char *str;
4721 isl_set *set;
4722 isl_union_map *schedule;
4723 isl_union_map *options;
4724 isl_ast_build *build;
4725 isl_ast_node *tree;
4727 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
4728 schedule = isl_union_map_read_from_str(ctx, str);
4729 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4730 build = isl_ast_build_from_context(set);
4732 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
4733 options = isl_union_map_read_from_str(ctx, str);
4734 build = isl_ast_build_set_options(build, options);
4735 tree = isl_ast_build_ast_from_schedule(build, schedule);
4736 isl_ast_build_free(build);
4737 if (!tree)
4738 return -1;
4739 isl_ast_node_free(tree);
4741 return 0;
4744 /* Increment *user on each call.
4746 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
4747 __isl_keep isl_ast_build *build, void *user)
4749 int *n = user;
4751 (*n)++;
4753 return node;
4756 /* Test that unrolling tries to minimize the number of instances.
4757 * In particular, for the schedule given below, make sure it generates
4758 * 3 nodes (rather than 101).
4760 static int test_ast_gen3(isl_ctx *ctx)
4762 const char *str;
4763 isl_set *set;
4764 isl_union_map *schedule;
4765 isl_union_map *options;
4766 isl_ast_build *build;
4767 isl_ast_node *tree;
4768 int n_domain = 0;
4770 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
4771 schedule = isl_union_map_read_from_str(ctx, str);
4772 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4774 str = "{ [i] -> unroll[0] }";
4775 options = isl_union_map_read_from_str(ctx, str);
4777 build = isl_ast_build_from_context(set);
4778 build = isl_ast_build_set_options(build, options);
4779 build = isl_ast_build_set_at_each_domain(build,
4780 &count_domains, &n_domain);
4781 tree = isl_ast_build_ast_from_schedule(build, schedule);
4782 isl_ast_build_free(build);
4783 if (!tree)
4784 return -1;
4786 isl_ast_node_free(tree);
4788 if (n_domain != 3)
4789 isl_die(ctx, isl_error_unknown,
4790 "unexpected number of for nodes", return -1);
4792 return 0;
4795 /* Check that if the ast_build_exploit_nested_bounds options is set,
4796 * we do not get an outer if node in the generated AST,
4797 * while we do get such an outer if node if the options is not set.
4799 static int test_ast_gen4(isl_ctx *ctx)
4801 const char *str;
4802 isl_set *set;
4803 isl_union_map *schedule;
4804 isl_ast_build *build;
4805 isl_ast_node *tree;
4806 enum isl_ast_node_type type;
4807 int enb;
4809 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4810 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4812 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4814 schedule = isl_union_map_read_from_str(ctx, str);
4815 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4816 build = isl_ast_build_from_context(set);
4817 tree = isl_ast_build_ast_from_schedule(build, schedule);
4818 isl_ast_build_free(build);
4819 if (!tree)
4820 return -1;
4822 type = isl_ast_node_get_type(tree);
4823 isl_ast_node_free(tree);
4825 if (type == isl_ast_node_if)
4826 isl_die(ctx, isl_error_unknown,
4827 "not expecting if node", return -1);
4829 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
4831 schedule = isl_union_map_read_from_str(ctx, str);
4832 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4833 build = isl_ast_build_from_context(set);
4834 tree = isl_ast_build_ast_from_schedule(build, schedule);
4835 isl_ast_build_free(build);
4836 if (!tree)
4837 return -1;
4839 type = isl_ast_node_get_type(tree);
4840 isl_ast_node_free(tree);
4842 if (type != isl_ast_node_if)
4843 isl_die(ctx, isl_error_unknown,
4844 "expecting if node", return -1);
4846 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
4848 return 0;
4851 /* This function is called for each leaf in the AST generated
4852 * from test_ast_gen5.
4854 * We finalize the AST generation by extending the outer schedule
4855 * with a zero-dimensional schedule. If this results in any for loops,
4856 * then this means that we did not pass along enough information
4857 * about the outer schedule to the inner AST generation.
4859 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
4860 void *user)
4862 isl_union_map *schedule, *extra;
4863 isl_ast_node *tree;
4865 schedule = isl_ast_build_get_schedule(build);
4866 extra = isl_union_map_copy(schedule);
4867 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
4868 schedule = isl_union_map_range_product(schedule, extra);
4869 tree = isl_ast_build_ast_from_schedule(build, schedule);
4870 isl_ast_build_free(build);
4872 if (!tree)
4873 return NULL;
4875 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
4876 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
4877 "code should not contain any for loop",
4878 return isl_ast_node_free(tree));
4880 return tree;
4883 /* Check that we do not lose any information when going back and
4884 * forth between internal and external schedule.
4886 * In particular, we create an AST where we unroll the only
4887 * non-constant dimension in the schedule. We therefore do
4888 * not expect any for loops in the AST. However, older versions
4889 * of isl would not pass along enough information about the outer
4890 * schedule when performing an inner code generation from a create_leaf
4891 * callback, resulting in the inner code generation producing a for loop.
4893 static int test_ast_gen5(isl_ctx *ctx)
4895 const char *str;
4896 isl_set *set;
4897 isl_union_map *schedule, *options;
4898 isl_ast_build *build;
4899 isl_ast_node *tree;
4901 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
4902 schedule = isl_union_map_read_from_str(ctx, str);
4904 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
4905 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
4906 options = isl_union_map_read_from_str(ctx, str);
4908 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4909 build = isl_ast_build_from_context(set);
4910 build = isl_ast_build_set_options(build, options);
4911 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
4912 tree = isl_ast_build_ast_from_schedule(build, schedule);
4913 isl_ast_build_free(build);
4914 isl_ast_node_free(tree);
4915 if (!tree)
4916 return -1;
4918 return 0;
4921 static int test_ast_gen(isl_ctx *ctx)
4923 if (test_ast_gen1(ctx) < 0)
4924 return -1;
4925 if (test_ast_gen2(ctx) < 0)
4926 return -1;
4927 if (test_ast_gen3(ctx) < 0)
4928 return -1;
4929 if (test_ast_gen4(ctx) < 0)
4930 return -1;
4931 if (test_ast_gen5(ctx) < 0)
4932 return -1;
4933 return 0;
4936 /* Check if dropping output dimensions from an isl_pw_multi_aff
4937 * works properly.
4939 static int test_pw_multi_aff(isl_ctx *ctx)
4941 const char *str;
4942 isl_pw_multi_aff *pma1, *pma2;
4943 int equal;
4945 str = "{ [i,j] -> [i+j, 4i-j] }";
4946 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4947 str = "{ [i,j] -> [4i-j] }";
4948 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4950 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
4952 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
4954 isl_pw_multi_aff_free(pma1);
4955 isl_pw_multi_aff_free(pma2);
4956 if (equal < 0)
4957 return -1;
4958 if (!equal)
4959 isl_die(ctx, isl_error_unknown,
4960 "expressions not equal", return -1);
4962 return 0;
4965 /* Check that we can properly parse multi piecewise affine expressions
4966 * where the piecewise affine expressions have different domains.
4968 static int test_multi_pw_aff(isl_ctx *ctx)
4970 const char *str;
4971 isl_set *dom, *dom2;
4972 isl_multi_pw_aff *mpa1, *mpa2;
4973 isl_pw_aff *pa;
4974 int equal;
4975 int equal_domain;
4977 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
4978 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
4979 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
4980 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
4981 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
4982 str = "{ [i] -> [(i : i > 0), 2i] }";
4983 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
4985 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
4987 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
4988 dom = isl_pw_aff_domain(pa);
4989 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
4990 dom2 = isl_pw_aff_domain(pa);
4991 equal_domain = isl_set_is_equal(dom, dom2);
4993 isl_set_free(dom);
4994 isl_set_free(dom2);
4995 isl_multi_pw_aff_free(mpa1);
4996 isl_multi_pw_aff_free(mpa2);
4998 if (equal < 0)
4999 return -1;
5000 if (!equal)
5001 isl_die(ctx, isl_error_unknown,
5002 "expressions not equal", return -1);
5004 if (equal_domain < 0)
5005 return -1;
5006 if (equal_domain)
5007 isl_die(ctx, isl_error_unknown,
5008 "domains unexpectedly equal", return -1);
5010 return 0;
5013 /* This is a regression test for a bug where isl_basic_map_simplify
5014 * would end up in an infinite loop. In particular, we construct
5015 * an empty basic set that is not obviously empty.
5016 * isl_basic_set_is_empty marks the basic set as empty.
5017 * After projecting out i3, the variable can be dropped completely,
5018 * but isl_basic_map_simplify refrains from doing so if the basic set
5019 * is empty and would end up in an infinite loop if it didn't test
5020 * explicitly for empty basic maps in the outer loop.
5022 static int test_simplify(isl_ctx *ctx)
5024 const char *str;
5025 isl_basic_set *bset;
5026 int empty;
5028 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
5029 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
5030 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
5031 "i3 >= i2 }";
5032 bset = isl_basic_set_read_from_str(ctx, str);
5033 empty = isl_basic_set_is_empty(bset);
5034 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
5035 isl_basic_set_free(bset);
5036 if (!bset)
5037 return -1;
5038 if (!empty)
5039 isl_die(ctx, isl_error_unknown,
5040 "basic set should be empty", return -1);
5042 return 0;
5045 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
5046 * with gbr context would fail to disable the use of the shifted tableau
5047 * when transferring equalities for the input to the context, resulting
5048 * in invalid sample values.
5050 static int test_partial_lexmin(isl_ctx *ctx)
5052 const char *str;
5053 isl_basic_set *bset;
5054 isl_basic_map *bmap;
5055 isl_map *map;
5057 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
5058 bmap = isl_basic_map_read_from_str(ctx, str);
5059 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
5060 bset = isl_basic_set_read_from_str(ctx, str);
5061 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
5062 isl_map_free(map);
5064 if (!map)
5065 return -1;
5067 return 0;
5070 /* Check that the variable compression performed on the existentially
5071 * quantified variables inside isl_basic_set_compute_divs is not confused
5072 * by the implicit equalities among the parameters.
5074 static int test_compute_divs(isl_ctx *ctx)
5076 const char *str;
5077 isl_basic_set *bset;
5078 isl_set *set;
5080 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
5081 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
5082 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
5083 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
5084 bset = isl_basic_set_read_from_str(ctx, str);
5085 set = isl_basic_set_compute_divs(bset);
5086 isl_set_free(set);
5087 if (!set)
5088 return -1;
5090 return 0;
5093 struct {
5094 const char *set;
5095 const char *dual;
5096 } coef_tests[] = {
5097 { "{ rat: [i] : 0 <= i <= 10 }",
5098 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
5099 { "{ rat: [i] : FALSE }",
5100 "{ rat: coefficients[[cst] -> [a]] }" },
5101 { "{ rat: [i] : }",
5102 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
5105 struct {
5106 const char *set;
5107 const char *dual;
5108 } sol_tests[] = {
5109 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
5110 "{ rat: [i] : 0 <= i <= 10 }" },
5111 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
5112 "{ rat: [i] }" },
5113 { "{ rat: coefficients[[cst] -> [a]] }",
5114 "{ rat: [i] : FALSE }" },
5117 /* Test the basic functionality of isl_basic_set_coefficients and
5118 * isl_basic_set_solutions.
5120 static int test_dual(isl_ctx *ctx)
5122 int i;
5124 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
5125 int equal;
5126 isl_basic_set *bset1, *bset2;
5128 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
5129 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
5130 bset1 = isl_basic_set_coefficients(bset1);
5131 equal = isl_basic_set_is_equal(bset1, bset2);
5132 isl_basic_set_free(bset1);
5133 isl_basic_set_free(bset2);
5134 if (equal < 0)
5135 return -1;
5136 if (!equal)
5137 isl_die(ctx, isl_error_unknown,
5138 "incorrect dual", return -1);
5141 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
5142 int equal;
5143 isl_basic_set *bset1, *bset2;
5145 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
5146 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
5147 bset1 = isl_basic_set_solutions(bset1);
5148 equal = isl_basic_set_is_equal(bset1, bset2);
5149 isl_basic_set_free(bset1);
5150 isl_basic_set_free(bset2);
5151 if (equal < 0)
5152 return -1;
5153 if (!equal)
5154 isl_die(ctx, isl_error_unknown,
5155 "incorrect dual", return -1);
5158 return 0;
5161 struct {
5162 const char *name;
5163 int (*fn)(isl_ctx *ctx);
5164 } tests [] = {
5165 { "dual", &test_dual },
5166 { "dependence analysis", &test_flow },
5167 { "val", &test_val },
5168 { "compute divs", &test_compute_divs },
5169 { "partial lexmin", &test_partial_lexmin },
5170 { "simplify", &test_simplify },
5171 { "curry", &test_curry },
5172 { "piecewise multi affine expressions", &test_pw_multi_aff },
5173 { "multi piecewise affine expressions", &test_multi_pw_aff },
5174 { "conversion", &test_conversion },
5175 { "list", &test_list },
5176 { "align parameters", &test_align_parameters },
5177 { "preimage", &test_preimage },
5178 { "pullback", &test_pullback },
5179 { "AST", &test_ast },
5180 { "AST build", &test_ast_build },
5181 { "AST generation", &test_ast_gen },
5182 { "eliminate", &test_eliminate },
5183 { "residue class", &test_residue_class },
5184 { "div", &test_div },
5185 { "slice", &test_slice },
5186 { "fixed power", &test_fixed_power },
5187 { "sample", &test_sample },
5188 { "output", &test_output },
5189 { "vertices", &test_vertices },
5190 { "fixed", &test_fixed },
5191 { "equal", &test_equal },
5192 { "disjoint", &test_disjoint },
5193 { "product", &test_product },
5194 { "dim_max", &test_dim_max },
5195 { "affine", &test_aff },
5196 { "injective", &test_injective },
5197 { "schedule", &test_schedule },
5198 { "union_pw", &test_union_pw },
5199 { "parse", &test_parse },
5200 { "single-valued", &test_sv },
5201 { "affine hull", &test_affine_hull },
5202 { "coalesce", &test_coalesce },
5203 { "factorize", &test_factorize },
5204 { "subset", &test_subset },
5205 { "subtract", &test_subtract },
5206 { "lexmin", &test_lexmin },
5207 { "min", &test_min },
5208 { "gist", &test_gist },
5209 { "piecewise quasi-polynomials", &test_pwqp },
5212 int main(int argc, char **argv)
5214 int i;
5215 struct isl_ctx *ctx;
5216 struct isl_options *options;
5218 srcdir = getenv("srcdir");
5219 assert(srcdir);
5221 options = isl_options_new_with_defaults();
5222 assert(options);
5223 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
5225 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
5226 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
5227 printf("%s\n", tests[i].name);
5228 if (tests[i].fn(ctx) < 0)
5229 goto error;
5231 test_lift(ctx);
5232 test_bound(ctx);
5233 test_union(ctx);
5234 test_split_periods(ctx);
5235 test_lex(ctx);
5236 test_bijective(ctx);
5237 test_dep(ctx);
5238 test_read(ctx);
5239 test_bounded(ctx);
5240 test_construction(ctx);
5241 test_dim(ctx);
5242 test_application(ctx);
5243 test_convex_hull(ctx);
5244 test_closure(ctx);
5245 isl_ctx_free(ctx);
5246 return 0;
5247 error:
5248 isl_ctx_free(ctx);
5249 return -1;