isl_coalesce.c: add_wraps: extract out common add_wrap
[isl.git] / isl_test.c
bloba000afea8e16a83e6f70be788823477d072d9386
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" },
412 { &isl_val_inv, "1", "1" },
413 { &isl_val_inv, "2", "1/2" },
414 { &isl_val_inv, "1/2", "2" },
415 { &isl_val_inv, "-2", "-1/2" },
416 { &isl_val_inv, "-1/2", "-2" },
417 { &isl_val_inv, "0", "NaN" },
418 { &isl_val_inv, "NaN", "NaN" },
419 { &isl_val_inv, "infty", "0" },
420 { &isl_val_inv, "-infty", "0" },
423 /* Perform some basic tests of unary operations on isl_val objects.
425 static int test_un_val(isl_ctx *ctx)
427 int i;
428 isl_val *v, *res;
429 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
430 int ok;
432 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
433 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
434 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
435 fn = val_un_tests[i].op;
436 v = fn(v);
437 if (isl_val_is_nan(res))
438 ok = isl_val_is_nan(v);
439 else
440 ok = isl_val_eq(v, res);
441 isl_val_free(v);
442 isl_val_free(res);
443 if (ok < 0)
444 return -1;
445 if (!ok)
446 isl_die(ctx, isl_error_unknown,
447 "unexpected result", return -1);
450 return 0;
453 struct {
454 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
455 __isl_take isl_val *v2);
456 } val_bin_op[] = {
457 ['+'] = { &isl_val_add },
458 ['-'] = { &isl_val_sub },
459 ['*'] = { &isl_val_mul },
460 ['/'] = { &isl_val_div },
461 ['g'] = { &isl_val_gcd },
462 ['m'] = { &isl_val_min },
463 ['M'] = { &isl_val_max },
466 struct {
467 const char *arg1;
468 unsigned char op;
469 const char *arg2;
470 const char *res;
471 } val_bin_tests[] = {
472 { "0", '+', "0", "0" },
473 { "1", '+', "0", "1" },
474 { "1", '+', "1", "2" },
475 { "1", '-', "1", "0" },
476 { "1", '*', "1", "1" },
477 { "1", '/', "1", "1" },
478 { "2", '*', "3", "6" },
479 { "2", '*', "1/2", "1" },
480 { "2", '*', "1/3", "2/3" },
481 { "2/3", '*', "3/5", "2/5" },
482 { "2/3", '*', "7/5", "14/15" },
483 { "2", '/', "1/2", "4" },
484 { "-2", '/', "-1/2", "4" },
485 { "-2", '/', "1/2", "-4" },
486 { "2", '/', "-1/2", "-4" },
487 { "2", '/', "2", "1" },
488 { "2", '/', "3", "2/3" },
489 { "2/3", '/', "5/3", "2/5" },
490 { "2/3", '/', "5/7", "14/15" },
491 { "0", '/', "0", "NaN" },
492 { "42", '/', "0", "NaN" },
493 { "-42", '/', "0", "NaN" },
494 { "infty", '/', "0", "NaN" },
495 { "-infty", '/', "0", "NaN" },
496 { "NaN", '/', "0", "NaN" },
497 { "0", '/', "NaN", "NaN" },
498 { "42", '/', "NaN", "NaN" },
499 { "-42", '/', "NaN", "NaN" },
500 { "infty", '/', "NaN", "NaN" },
501 { "-infty", '/', "NaN", "NaN" },
502 { "NaN", '/', "NaN", "NaN" },
503 { "0", '/', "infty", "0" },
504 { "42", '/', "infty", "0" },
505 { "-42", '/', "infty", "0" },
506 { "infty", '/', "infty", "NaN" },
507 { "-infty", '/', "infty", "NaN" },
508 { "NaN", '/', "infty", "NaN" },
509 { "0", '/', "-infty", "0" },
510 { "42", '/', "-infty", "0" },
511 { "-42", '/', "-infty", "0" },
512 { "infty", '/', "-infty", "NaN" },
513 { "-infty", '/', "-infty", "NaN" },
514 { "NaN", '/', "-infty", "NaN" },
515 { "1", '-', "1/3", "2/3" },
516 { "1/3", '+', "1/2", "5/6" },
517 { "1/2", '+', "1/2", "1" },
518 { "3/4", '-', "1/4", "1/2" },
519 { "1/2", '-', "1/3", "1/6" },
520 { "infty", '+', "42", "infty" },
521 { "infty", '+', "infty", "infty" },
522 { "42", '+', "infty", "infty" },
523 { "infty", '-', "infty", "NaN" },
524 { "infty", '*', "infty", "infty" },
525 { "infty", '*', "-infty", "-infty" },
526 { "-infty", '*', "infty", "-infty" },
527 { "-infty", '*', "-infty", "infty" },
528 { "0", '*', "infty", "NaN" },
529 { "1", '*', "infty", "infty" },
530 { "infty", '*', "0", "NaN" },
531 { "infty", '*', "42", "infty" },
532 { "42", '-', "infty", "-infty" },
533 { "infty", '+', "-infty", "NaN" },
534 { "4", 'g', "6", "2" },
535 { "5", 'g', "6", "1" },
536 { "42", 'm', "3", "3" },
537 { "42", 'M', "3", "42" },
538 { "3", 'm', "42", "3" },
539 { "3", 'M', "42", "42" },
540 { "42", 'm', "infty", "42" },
541 { "42", 'M', "infty", "infty" },
542 { "42", 'm', "-infty", "-infty" },
543 { "42", 'M', "-infty", "42" },
544 { "42", 'm', "NaN", "NaN" },
545 { "42", 'M', "NaN", "NaN" },
546 { "infty", 'm', "-infty", "-infty" },
547 { "infty", 'M', "-infty", "infty" },
550 /* Perform some basic tests of binary operations on isl_val objects.
552 static int test_bin_val(isl_ctx *ctx)
554 int i;
555 isl_val *v1, *v2, *res;
556 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
557 __isl_take isl_val *v2);
558 int ok;
560 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
561 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
562 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
563 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
564 fn = val_bin_op[val_bin_tests[i].op].fn;
565 v1 = fn(v1, v2);
566 if (isl_val_is_nan(res))
567 ok = isl_val_is_nan(v1);
568 else
569 ok = isl_val_eq(v1, res);
570 isl_val_free(v1);
571 isl_val_free(res);
572 if (ok < 0)
573 return -1;
574 if (!ok)
575 isl_die(ctx, isl_error_unknown,
576 "unexpected result", return -1);
579 return 0;
582 /* Perform some basic tests on isl_val objects.
584 static int test_val(isl_ctx *ctx)
586 if (test_un_val(ctx) < 0)
587 return -1;
588 if (test_bin_val(ctx) < 0)
589 return -1;
590 return 0;
593 static int test_div(isl_ctx *ctx)
595 unsigned n;
596 const char *str;
597 int empty;
598 isl_int v;
599 isl_space *dim;
600 isl_set *set;
601 isl_local_space *ls;
602 struct isl_basic_set *bset;
603 struct isl_constraint *c;
605 isl_int_init(v);
607 /* test 1 */
608 dim = isl_space_set_alloc(ctx, 0, 3);
609 bset = isl_basic_set_universe(isl_space_copy(dim));
610 ls = isl_local_space_from_space(dim);
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, 1, v);
619 bset = isl_basic_set_add_constraint(bset, c);
621 c = isl_equality_alloc(isl_local_space_copy(ls));
622 isl_int_set_si(v, 1);
623 isl_constraint_set_constant(c, v);
624 isl_int_set_si(v, -1);
625 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
626 isl_int_set_si(v, 3);
627 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
628 bset = isl_basic_set_add_constraint(bset, c);
630 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
632 assert(bset && bset->n_div == 1);
633 isl_local_space_free(ls);
634 isl_basic_set_free(bset);
636 /* test 2 */
637 dim = isl_space_set_alloc(ctx, 0, 3);
638 bset = isl_basic_set_universe(isl_space_copy(dim));
639 ls = isl_local_space_from_space(dim);
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, 1, v);
648 bset = isl_basic_set_add_constraint(bset, c);
650 c = isl_equality_alloc(isl_local_space_copy(ls));
651 isl_int_set_si(v, -1);
652 isl_constraint_set_constant(c, v);
653 isl_int_set_si(v, 1);
654 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
655 isl_int_set_si(v, 3);
656 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
657 bset = isl_basic_set_add_constraint(bset, c);
659 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
661 assert(bset && bset->n_div == 1);
662 isl_local_space_free(ls);
663 isl_basic_set_free(bset);
665 /* test 3 */
666 dim = isl_space_set_alloc(ctx, 0, 3);
667 bset = isl_basic_set_universe(isl_space_copy(dim));
668 ls = isl_local_space_from_space(dim);
670 c = isl_equality_alloc(isl_local_space_copy(ls));
671 isl_int_set_si(v, 1);
672 isl_constraint_set_constant(c, v);
673 isl_int_set_si(v, -1);
674 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
675 isl_int_set_si(v, 3);
676 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
677 bset = isl_basic_set_add_constraint(bset, c);
679 c = isl_equality_alloc(isl_local_space_copy(ls));
680 isl_int_set_si(v, -3);
681 isl_constraint_set_constant(c, v);
682 isl_int_set_si(v, 1);
683 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
684 isl_int_set_si(v, 4);
685 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
686 bset = isl_basic_set_add_constraint(bset, c);
688 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
690 assert(bset && bset->n_div == 1);
691 isl_local_space_free(ls);
692 isl_basic_set_free(bset);
694 /* test 4 */
695 dim = isl_space_set_alloc(ctx, 0, 3);
696 bset = isl_basic_set_universe(isl_space_copy(dim));
697 ls = isl_local_space_from_space(dim);
699 c = isl_equality_alloc(isl_local_space_copy(ls));
700 isl_int_set_si(v, 2);
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, 3);
705 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
706 bset = isl_basic_set_add_constraint(bset, c);
708 c = isl_equality_alloc(isl_local_space_copy(ls));
709 isl_int_set_si(v, -1);
710 isl_constraint_set_constant(c, v);
711 isl_int_set_si(v, 1);
712 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
713 isl_int_set_si(v, 6);
714 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
715 bset = isl_basic_set_add_constraint(bset, c);
717 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
719 assert(isl_basic_set_is_empty(bset));
720 isl_local_space_free(ls);
721 isl_basic_set_free(bset);
723 /* test 5 */
724 dim = isl_space_set_alloc(ctx, 0, 3);
725 bset = isl_basic_set_universe(isl_space_copy(dim));
726 ls = isl_local_space_from_space(dim);
728 c = isl_equality_alloc(isl_local_space_copy(ls));
729 isl_int_set_si(v, -1);
730 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
731 isl_int_set_si(v, 3);
732 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
733 bset = isl_basic_set_add_constraint(bset, c);
735 c = isl_equality_alloc(isl_local_space_copy(ls));
736 isl_int_set_si(v, 1);
737 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
738 isl_int_set_si(v, -3);
739 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
740 bset = isl_basic_set_add_constraint(bset, c);
742 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
744 assert(bset && bset->n_div == 0);
745 isl_basic_set_free(bset);
746 isl_local_space_free(ls);
748 /* test 6 */
749 dim = isl_space_set_alloc(ctx, 0, 3);
750 bset = isl_basic_set_universe(isl_space_copy(dim));
751 ls = isl_local_space_from_space(dim);
753 c = isl_equality_alloc(isl_local_space_copy(ls));
754 isl_int_set_si(v, -1);
755 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
756 isl_int_set_si(v, 6);
757 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
758 bset = isl_basic_set_add_constraint(bset, c);
760 c = isl_equality_alloc(isl_local_space_copy(ls));
761 isl_int_set_si(v, 1);
762 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
763 isl_int_set_si(v, -3);
764 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
765 bset = isl_basic_set_add_constraint(bset, c);
767 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
769 assert(bset && bset->n_div == 1);
770 isl_basic_set_free(bset);
771 isl_local_space_free(ls);
773 /* test 7 */
774 /* This test is a bit tricky. We set up an equality
775 * a + 3b + 3c = 6 e0
776 * Normalization of divs creates _two_ divs
777 * a = 3 e0
778 * c - b - e0 = 2 e1
779 * Afterwards e0 is removed again because it has coefficient -1
780 * and we end up with the original equality and div again.
781 * Perhaps we can avoid the introduction of this temporary div.
783 dim = isl_space_set_alloc(ctx, 0, 4);
784 bset = isl_basic_set_universe(isl_space_copy(dim));
785 ls = isl_local_space_from_space(dim);
787 c = isl_equality_alloc(isl_local_space_copy(ls));
788 isl_int_set_si(v, -1);
789 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
790 isl_int_set_si(v, -3);
791 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
792 isl_int_set_si(v, -3);
793 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
794 isl_int_set_si(v, 6);
795 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
796 bset = isl_basic_set_add_constraint(bset, c);
798 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
800 /* Test disabled for now */
802 assert(bset && bset->n_div == 1);
804 isl_local_space_free(ls);
805 isl_basic_set_free(bset);
807 /* test 8 */
808 dim = isl_space_set_alloc(ctx, 0, 5);
809 bset = isl_basic_set_universe(isl_space_copy(dim));
810 ls = isl_local_space_from_space(dim);
812 c = isl_equality_alloc(isl_local_space_copy(ls));
813 isl_int_set_si(v, -1);
814 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
815 isl_int_set_si(v, -3);
816 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
817 isl_int_set_si(v, -3);
818 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
819 isl_int_set_si(v, 6);
820 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
821 bset = isl_basic_set_add_constraint(bset, c);
823 c = isl_equality_alloc(isl_local_space_copy(ls));
824 isl_int_set_si(v, -1);
825 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
826 isl_int_set_si(v, 1);
827 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
828 isl_int_set_si(v, 1);
829 isl_constraint_set_constant(c, v);
830 bset = isl_basic_set_add_constraint(bset, c);
832 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
834 /* Test disabled for now */
836 assert(bset && bset->n_div == 1);
838 isl_local_space_free(ls);
839 isl_basic_set_free(bset);
841 /* test 9 */
842 dim = isl_space_set_alloc(ctx, 0, 4);
843 bset = isl_basic_set_universe(isl_space_copy(dim));
844 ls = isl_local_space_from_space(dim);
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, -1);
850 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
851 isl_int_set_si(v, -2);
852 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
853 bset = isl_basic_set_add_constraint(bset, c);
855 c = isl_equality_alloc(isl_local_space_copy(ls));
856 isl_int_set_si(v, -1);
857 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
858 isl_int_set_si(v, 3);
859 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
860 isl_int_set_si(v, 2);
861 isl_constraint_set_constant(c, v);
862 bset = isl_basic_set_add_constraint(bset, c);
864 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
866 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
868 assert(!isl_basic_set_is_empty(bset));
870 isl_local_space_free(ls);
871 isl_basic_set_free(bset);
873 /* test 10 */
874 dim = isl_space_set_alloc(ctx, 0, 3);
875 bset = isl_basic_set_universe(isl_space_copy(dim));
876 ls = isl_local_space_from_space(dim);
878 c = isl_equality_alloc(isl_local_space_copy(ls));
879 isl_int_set_si(v, 1);
880 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
881 isl_int_set_si(v, -2);
882 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
883 bset = isl_basic_set_add_constraint(bset, c);
885 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
887 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
889 isl_local_space_free(ls);
890 isl_basic_set_free(bset);
892 isl_int_clear(v);
894 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
895 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
896 set = isl_set_read_from_str(ctx, str);
897 set = isl_set_compute_divs(set);
898 isl_set_free(set);
899 if (!set)
900 return -1;
902 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
903 bset = isl_basic_set_read_from_str(ctx, str);
904 n = isl_basic_set_dim(bset, isl_dim_div);
905 isl_basic_set_free(bset);
906 if (!bset)
907 return -1;
908 if (n != 0)
909 isl_die(ctx, isl_error_unknown,
910 "expecting no existentials", return -1);
912 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
913 set = isl_set_read_from_str(ctx, str);
914 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
915 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
916 empty = isl_set_is_empty(set);
917 isl_set_free(set);
918 if (empty < 0)
919 return -1;
920 if (!empty)
921 isl_die(ctx, isl_error_unknown,
922 "result not as accurate as expected", return -1);
924 return 0;
927 void test_application_case(struct isl_ctx *ctx, const char *name)
929 char *filename;
930 FILE *input;
931 struct isl_basic_set *bset1, *bset2;
932 struct isl_basic_map *bmap;
934 filename = get_filename(ctx, name, "omega");
935 assert(filename);
936 input = fopen(filename, "r");
937 assert(input);
939 bset1 = isl_basic_set_read_from_file(ctx, input);
940 bmap = isl_basic_map_read_from_file(ctx, input);
942 bset1 = isl_basic_set_apply(bset1, bmap);
944 bset2 = isl_basic_set_read_from_file(ctx, input);
946 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
948 isl_basic_set_free(bset1);
949 isl_basic_set_free(bset2);
950 free(filename);
952 fclose(input);
955 void test_application(struct isl_ctx *ctx)
957 test_application_case(ctx, "application");
958 test_application_case(ctx, "application2");
961 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
963 char *filename;
964 FILE *input;
965 struct isl_basic_set *bset1, *bset2;
967 filename = get_filename(ctx, name, "polylib");
968 assert(filename);
969 input = fopen(filename, "r");
970 assert(input);
972 bset1 = isl_basic_set_read_from_file(ctx, input);
973 bset2 = isl_basic_set_read_from_file(ctx, input);
975 bset1 = isl_basic_set_affine_hull(bset1);
977 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
979 isl_basic_set_free(bset1);
980 isl_basic_set_free(bset2);
981 free(filename);
983 fclose(input);
986 int test_affine_hull(struct isl_ctx *ctx)
988 const char *str;
989 isl_set *set;
990 isl_basic_set *bset, *bset2;
991 int n;
992 int subset;
994 test_affine_hull_case(ctx, "affine2");
995 test_affine_hull_case(ctx, "affine");
996 test_affine_hull_case(ctx, "affine3");
998 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
999 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1000 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1001 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1002 set = isl_set_read_from_str(ctx, str);
1003 bset = isl_set_affine_hull(set);
1004 n = isl_basic_set_dim(bset, isl_dim_div);
1005 isl_basic_set_free(bset);
1006 if (n != 0)
1007 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1008 return -1);
1010 /* Check that isl_map_affine_hull is not confused by
1011 * the reordering of divs in isl_map_align_divs.
1013 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1014 "32e0 = b and 32e1 = c); "
1015 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1016 set = isl_set_read_from_str(ctx, str);
1017 bset = isl_set_affine_hull(set);
1018 isl_basic_set_free(bset);
1019 if (!bset)
1020 return -1;
1022 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1023 "32e2 = 31 + 31e0 }";
1024 set = isl_set_read_from_str(ctx, str);
1025 bset = isl_set_affine_hull(set);
1026 str = "{ [a] : exists e : a = 32 e }";
1027 bset2 = isl_basic_set_read_from_str(ctx, str);
1028 subset = isl_basic_set_is_subset(bset, bset2);
1029 isl_basic_set_free(bset);
1030 isl_basic_set_free(bset2);
1031 if (subset < 0)
1032 return -1;
1033 if (!subset)
1034 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1035 return -1);
1037 return 0;
1040 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1042 char *filename;
1043 FILE *input;
1044 struct isl_basic_set *bset1, *bset2;
1045 struct isl_set *set;
1047 filename = get_filename(ctx, name, "polylib");
1048 assert(filename);
1049 input = fopen(filename, "r");
1050 assert(input);
1052 bset1 = isl_basic_set_read_from_file(ctx, input);
1053 bset2 = isl_basic_set_read_from_file(ctx, input);
1055 set = isl_basic_set_union(bset1, bset2);
1056 bset1 = isl_set_convex_hull(set);
1058 bset2 = isl_basic_set_read_from_file(ctx, input);
1060 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1062 isl_basic_set_free(bset1);
1063 isl_basic_set_free(bset2);
1064 free(filename);
1066 fclose(input);
1069 struct {
1070 const char *set;
1071 const char *hull;
1072 } convex_hull_tests[] = {
1073 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1074 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1075 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1076 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1077 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1078 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1079 "i2 <= 5 and i2 >= 4; "
1080 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1081 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1082 "i2 <= 5 + i0 and i2 >= i0 }" },
1083 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1084 "{ [x, y] : 1 = 0 }" },
1087 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
1089 int i;
1090 int orig_convex = ctx->opt->convex;
1091 ctx->opt->convex = convex;
1093 test_convex_hull_case(ctx, "convex0");
1094 test_convex_hull_case(ctx, "convex1");
1095 test_convex_hull_case(ctx, "convex2");
1096 test_convex_hull_case(ctx, "convex3");
1097 test_convex_hull_case(ctx, "convex4");
1098 test_convex_hull_case(ctx, "convex5");
1099 test_convex_hull_case(ctx, "convex6");
1100 test_convex_hull_case(ctx, "convex7");
1101 test_convex_hull_case(ctx, "convex8");
1102 test_convex_hull_case(ctx, "convex9");
1103 test_convex_hull_case(ctx, "convex10");
1104 test_convex_hull_case(ctx, "convex11");
1105 test_convex_hull_case(ctx, "convex12");
1106 test_convex_hull_case(ctx, "convex13");
1107 test_convex_hull_case(ctx, "convex14");
1108 test_convex_hull_case(ctx, "convex15");
1110 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1111 isl_set *set1, *set2;
1113 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1114 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1115 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1116 assert(isl_set_is_equal(set1, set2));
1117 isl_set_free(set1);
1118 isl_set_free(set2);
1121 ctx->opt->convex = orig_convex;
1124 void test_convex_hull(struct isl_ctx *ctx)
1126 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
1127 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
1130 void test_gist_case(struct isl_ctx *ctx, const char *name)
1132 char *filename;
1133 FILE *input;
1134 struct isl_basic_set *bset1, *bset2;
1136 filename = get_filename(ctx, name, "polylib");
1137 assert(filename);
1138 input = fopen(filename, "r");
1139 assert(input);
1141 bset1 = isl_basic_set_read_from_file(ctx, input);
1142 bset2 = isl_basic_set_read_from_file(ctx, input);
1144 bset1 = isl_basic_set_gist(bset1, bset2);
1146 bset2 = isl_basic_set_read_from_file(ctx, input);
1148 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1150 isl_basic_set_free(bset1);
1151 isl_basic_set_free(bset2);
1152 free(filename);
1154 fclose(input);
1157 struct {
1158 const char *set;
1159 const char *context;
1160 const char *gist;
1161 } gist_tests[] = {
1162 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1163 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1164 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1165 "{ [a, b, c] : a <= 15 }" },
1166 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1167 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1168 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1169 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1170 { "{ [m, n, a, b] : a <= 2147 + n }",
1171 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1172 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1173 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1174 "b >= 0) }",
1175 "{ [m, n, ku, kl] }" },
1178 static int test_gist(struct isl_ctx *ctx)
1180 int i;
1181 const char *str;
1182 isl_basic_set *bset1, *bset2;
1183 isl_map *map1, *map2;
1184 int equal;
1186 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1187 int equal_input;
1188 isl_set *set1, *set2, *copy;
1190 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1191 set2 = isl_set_read_from_str(ctx, gist_tests[i].context);
1192 copy = isl_set_copy(set1);
1193 set1 = isl_set_gist(set1, set2);
1194 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1195 equal = isl_set_is_equal(set1, set2);
1196 isl_set_free(set1);
1197 isl_set_free(set2);
1198 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1199 equal_input = isl_set_is_equal(set1, copy);
1200 isl_set_free(set1);
1201 isl_set_free(copy);
1202 if (equal < 0 || equal_input < 0)
1203 return -1;
1204 if (!equal)
1205 isl_die(ctx, isl_error_unknown,
1206 "incorrect gist result", return -1);
1207 if (!equal_input)
1208 isl_die(ctx, isl_error_unknown,
1209 "gist modified input", return -1);
1212 test_gist_case(ctx, "gist1");
1214 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1215 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1216 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1217 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1218 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1219 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1220 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1221 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1222 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1223 bset1 = isl_basic_set_read_from_str(ctx, str);
1224 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1225 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1226 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1227 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1228 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1229 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1230 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1231 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1232 bset2 = isl_basic_set_read_from_str(ctx, str);
1233 bset1 = isl_basic_set_gist(bset1, bset2);
1234 assert(bset1 && bset1->n_div == 0);
1235 isl_basic_set_free(bset1);
1237 /* Check that the integer divisions of the second disjunct
1238 * do not spread to the first disjunct.
1240 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1241 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1242 "(exists (e0 = [(-1 + t1)/16], "
1243 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1244 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1245 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1246 "o0 <= 4294967295 and t1 <= -1)) }";
1247 map1 = isl_map_read_from_str(ctx, str);
1248 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1249 map2 = isl_map_read_from_str(ctx, str);
1250 map1 = isl_map_gist(map1, map2);
1251 if (!map1)
1252 return -1;
1253 if (map1->n != 1)
1254 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1255 isl_map_free(map1); return -1);
1256 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1257 isl_die(ctx, isl_error_unknown, "expecting single div",
1258 isl_map_free(map1); return -1);
1259 isl_map_free(map1);
1261 return 0;
1264 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1266 isl_set *set, *set2;
1267 int equal;
1268 int one;
1270 set = isl_set_read_from_str(ctx, str);
1271 set = isl_set_coalesce(set);
1272 set2 = isl_set_read_from_str(ctx, str);
1273 equal = isl_set_is_equal(set, set2);
1274 one = set && set->n == 1;
1275 isl_set_free(set);
1276 isl_set_free(set2);
1278 if (equal < 0)
1279 return -1;
1280 if (!equal)
1281 isl_die(ctx, isl_error_unknown,
1282 "coalesced set not equal to input", return -1);
1283 if (check_one && !one)
1284 isl_die(ctx, isl_error_unknown,
1285 "coalesced set should not be a union", return -1);
1287 return 0;
1290 /* Inputs for coalescing tests with unbounded wrapping.
1291 * "str" is a string representation of the input set.
1292 * "single_disjunct" is set if we expect the result to consist of
1293 * a single disjunct.
1295 struct {
1296 int single_disjunct;
1297 const char *str;
1298 } coalesce_unbounded_tests[] = {
1299 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1300 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1301 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1302 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1303 "-10 <= y <= 0}" },
1304 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1305 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1306 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1309 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1310 * option turned off.
1312 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1314 int i;
1315 int r = 0;
1316 int bounded;
1318 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1319 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1321 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1322 const char *str = coalesce_unbounded_tests[i].str;
1323 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1324 if (test_coalesce_set(ctx, str, check_one) >= 0)
1325 continue;
1326 r = -1;
1327 break;
1330 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1332 return r;
1335 /* Inputs for coalescing tests.
1336 * "str" is a string representation of the input set.
1337 * "single_disjunct" is set if we expect the result to consist of
1338 * a single disjunct.
1340 struct {
1341 int single_disjunct;
1342 const char *str;
1343 } coalesce_tests[] = {
1344 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1345 "y >= x & x >= 2 & 5 >= y }" },
1346 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1347 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1348 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1349 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1350 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1351 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1352 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1353 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1354 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1355 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1356 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1357 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1358 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1359 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1360 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1361 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1362 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1363 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1364 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1365 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1366 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1367 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1368 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1369 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1370 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1371 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1372 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1373 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1374 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1375 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1376 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1377 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1378 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1379 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1380 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1381 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1382 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1383 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1384 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1385 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1386 "[o0, o1, o2, o3, o4, o5, o6]] : "
1387 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1388 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1389 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1390 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1391 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1392 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1393 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1394 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1395 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1396 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1397 "o6 >= i3 + i6 - o3 and M >= 0 and "
1398 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1399 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1400 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1401 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1402 "(o0 = 0 and M >= 2 and N >= 3) or "
1403 "(M = 0 and o0 = 0 and N >= 3) }" },
1404 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1405 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1406 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1407 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1408 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1409 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1410 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1411 "(y = 3 and x = 1) }" },
1412 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1413 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1414 "i1 <= M and i3 <= M and i4 <= M) or "
1415 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1416 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1417 "i4 <= -1 + M) }" },
1418 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1419 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1420 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1421 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1422 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1423 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1424 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1425 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1426 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1427 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1428 { 0, "{ [a, b] : exists e : 2e = a and "
1429 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1430 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1431 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1432 "j >= 1 and j' <= i + j - i' and i >= 1; "
1433 "[1, 1, 1, 1] }" },
1434 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1435 "[i,j] : exists a : j = 3a }" },
1436 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1437 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1438 "a >= 3) or "
1439 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1440 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1441 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1442 "c <= 6 + 8a and a >= 3; "
1443 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1444 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1445 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1446 "[x,0] : 3 <= x <= 4 }" },
1447 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1448 "[x,0] : 4 <= x <= 5 }" },
1449 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1450 "[x,0] : 3 <= x <= 5 }" },
1451 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1452 "[x,0] : 3 <= x <= 4 }" },
1453 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1454 "i1 <= 0; "
1455 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1456 { 1, "{ [0,0]; [1,1] }" },
1459 /* Test the functionality of isl_set_coalesce.
1460 * That is, check that the output is always equal to the input
1461 * and in some cases that the result consists of a single disjunct.
1463 static int test_coalesce(struct isl_ctx *ctx)
1465 int i;
1467 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1468 const char *str = coalesce_tests[i].str;
1469 int check_one = coalesce_tests[i].single_disjunct;
1470 if (test_coalesce_set(ctx, str, check_one) < 0)
1471 return -1;
1474 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1475 return -1;
1477 return 0;
1480 void test_closure(struct isl_ctx *ctx)
1482 const char *str;
1483 isl_set *dom;
1484 isl_map *up, *right;
1485 isl_map *map, *map2;
1486 int exact;
1488 /* COCOA example 1 */
1489 map = isl_map_read_from_str(ctx,
1490 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1491 "1 <= i and i < n and 1 <= j and j < n or "
1492 "i2 = i + 1 and j2 = j - 1 and "
1493 "1 <= i and i < n and 2 <= j and j <= n }");
1494 map = isl_map_power(map, &exact);
1495 assert(exact);
1496 isl_map_free(map);
1498 /* COCOA example 1 */
1499 map = isl_map_read_from_str(ctx,
1500 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1501 "1 <= i and i < n and 1 <= j and j < n or "
1502 "i2 = i + 1 and j2 = j - 1 and "
1503 "1 <= i and i < n and 2 <= 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 and 1 <= j and j <= n and "
1509 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1510 "i2 = i + k1 + k2 and j2 = j + k1 - 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(map2);
1514 isl_map_free(map);
1516 map = isl_map_read_from_str(ctx,
1517 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1518 " 0 <= y and y <= n }");
1519 map = isl_map_transitive_closure(map, &exact);
1520 map2 = isl_map_read_from_str(ctx,
1521 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1522 " 0 <= y and y <= n }");
1523 assert(isl_map_is_equal(map, map2));
1524 isl_map_free(map2);
1525 isl_map_free(map);
1527 /* COCOA example 2 */
1528 map = isl_map_read_from_str(ctx,
1529 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1530 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1531 "i2 = i + 2 and j2 = j - 2 and "
1532 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1533 map = isl_map_transitive_closure(map, &exact);
1534 assert(exact);
1535 map2 = isl_map_read_from_str(ctx,
1536 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1537 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1538 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1539 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1540 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1541 assert(isl_map_is_equal(map, map2));
1542 isl_map_free(map);
1543 isl_map_free(map2);
1545 /* COCOA Fig.2 left */
1546 map = isl_map_read_from_str(ctx,
1547 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1548 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1549 "j <= n or "
1550 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1551 "j <= 2 i - 3 and j <= n - 2 or "
1552 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1553 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1554 map = isl_map_transitive_closure(map, &exact);
1555 assert(exact);
1556 isl_map_free(map);
1558 /* COCOA Fig.2 right */
1559 map = isl_map_read_from_str(ctx,
1560 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1561 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1562 "j <= n or "
1563 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1564 "j <= 2 i - 4 and j <= n - 3 or "
1565 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1566 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1567 map = isl_map_power(map, &exact);
1568 assert(exact);
1569 isl_map_free(map);
1571 /* COCOA Fig.2 right */
1572 map = isl_map_read_from_str(ctx,
1573 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1574 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1575 "j <= n or "
1576 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1577 "j <= 2 i - 4 and j <= n - 3 or "
1578 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1579 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1580 map = isl_map_transitive_closure(map, &exact);
1581 assert(exact);
1582 map2 = isl_map_read_from_str(ctx,
1583 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1584 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1585 "j <= n and 3 + i + 2 j <= 3 n and "
1586 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1587 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1588 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1589 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1590 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1591 assert(isl_map_is_equal(map, map2));
1592 isl_map_free(map2);
1593 isl_map_free(map);
1595 /* COCOA Fig.1 right */
1596 dom = isl_set_read_from_str(ctx,
1597 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1598 "2 x - 3 y + 3 >= 0 }");
1599 right = isl_map_read_from_str(ctx,
1600 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1601 up = isl_map_read_from_str(ctx,
1602 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1603 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1604 right = isl_map_intersect_range(right, isl_set_copy(dom));
1605 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1606 up = isl_map_intersect_range(up, dom);
1607 map = isl_map_union(up, right);
1608 map = isl_map_transitive_closure(map, &exact);
1609 assert(exact);
1610 map2 = isl_map_read_from_str(ctx,
1611 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1612 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1613 assert(isl_map_is_equal(map, map2));
1614 isl_map_free(map2);
1615 isl_map_free(map);
1617 /* COCOA Theorem 1 counter example */
1618 map = isl_map_read_from_str(ctx,
1619 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1620 "i2 = 1 and j2 = j or "
1621 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1622 map = isl_map_transitive_closure(map, &exact);
1623 assert(exact);
1624 isl_map_free(map);
1626 map = isl_map_read_from_str(ctx,
1627 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1628 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1629 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1630 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1631 map = isl_map_transitive_closure(map, &exact);
1632 assert(exact);
1633 isl_map_free(map);
1635 /* Kelly et al 1996, fig 12 */
1636 map = isl_map_read_from_str(ctx,
1637 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1638 "1 <= i,j,j+1 <= n or "
1639 "j = n and j2 = 1 and i2 = i + 1 and "
1640 "1 <= i,i+1 <= n }");
1641 map = isl_map_transitive_closure(map, &exact);
1642 assert(exact);
1643 map2 = isl_map_read_from_str(ctx,
1644 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1645 "1 <= i <= n and i = i2 or "
1646 "1 <= i < i2 <= n and 1 <= j <= n and "
1647 "1 <= j2 <= n }");
1648 assert(isl_map_is_equal(map, map2));
1649 isl_map_free(map2);
1650 isl_map_free(map);
1652 /* Omega's closure4 */
1653 map = isl_map_read_from_str(ctx,
1654 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1655 "1 <= x,y <= 10 or "
1656 "x2 = x + 1 and y2 = y and "
1657 "1 <= x <= 20 && 5 <= y <= 15 }");
1658 map = isl_map_transitive_closure(map, &exact);
1659 assert(exact);
1660 isl_map_free(map);
1662 map = isl_map_read_from_str(ctx,
1663 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1664 map = isl_map_transitive_closure(map, &exact);
1665 assert(!exact);
1666 map2 = isl_map_read_from_str(ctx,
1667 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1668 assert(isl_map_is_equal(map, map2));
1669 isl_map_free(map);
1670 isl_map_free(map2);
1672 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1673 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1674 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1675 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1676 map = isl_map_read_from_str(ctx, str);
1677 map = isl_map_transitive_closure(map, &exact);
1678 assert(exact);
1679 map2 = isl_map_read_from_str(ctx, str);
1680 assert(isl_map_is_equal(map, map2));
1681 isl_map_free(map);
1682 isl_map_free(map2);
1684 str = "{[0] -> [1]; [2] -> [3]}";
1685 map = isl_map_read_from_str(ctx, str);
1686 map = isl_map_transitive_closure(map, &exact);
1687 assert(exact);
1688 map2 = isl_map_read_from_str(ctx, str);
1689 assert(isl_map_is_equal(map, map2));
1690 isl_map_free(map);
1691 isl_map_free(map2);
1693 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1694 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1695 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1696 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1697 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1698 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1699 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1700 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1701 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1702 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1703 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1704 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1705 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1706 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1707 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1708 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1709 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1710 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1711 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1712 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1713 map = isl_map_read_from_str(ctx, str);
1714 map = isl_map_transitive_closure(map, NULL);
1715 assert(map);
1716 isl_map_free(map);
1719 void test_lex(struct isl_ctx *ctx)
1721 isl_space *dim;
1722 isl_map *map;
1724 dim = isl_space_set_alloc(ctx, 0, 0);
1725 map = isl_map_lex_le(dim);
1726 assert(!isl_map_is_empty(map));
1727 isl_map_free(map);
1730 static int test_lexmin(struct isl_ctx *ctx)
1732 int equal;
1733 const char *str;
1734 isl_basic_map *bmap;
1735 isl_map *map, *map2;
1736 isl_set *set;
1737 isl_set *set2;
1738 isl_pw_multi_aff *pma;
1740 str = "[p0, p1] -> { [] -> [] : "
1741 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1742 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1743 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1744 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1745 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1746 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1747 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1748 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1749 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1750 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1751 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1752 map = isl_map_read_from_str(ctx, str);
1753 map = isl_map_lexmin(map);
1754 isl_map_free(map);
1756 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1757 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1758 set = isl_set_read_from_str(ctx, str);
1759 set = isl_set_lexmax(set);
1760 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1761 set2 = isl_set_read_from_str(ctx, str);
1762 set = isl_set_intersect(set, set2);
1763 assert(!isl_set_is_empty(set));
1764 isl_set_free(set);
1766 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1767 map = isl_map_read_from_str(ctx, str);
1768 map = isl_map_lexmin(map);
1769 str = "{ [x] -> [5] : 6 <= x <= 8; "
1770 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1771 map2 = isl_map_read_from_str(ctx, str);
1772 assert(isl_map_is_equal(map, map2));
1773 isl_map_free(map);
1774 isl_map_free(map2);
1776 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1777 map = isl_map_read_from_str(ctx, str);
1778 map2 = isl_map_copy(map);
1779 map = isl_map_lexmin(map);
1780 assert(isl_map_is_equal(map, map2));
1781 isl_map_free(map);
1782 isl_map_free(map2);
1784 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1785 map = isl_map_read_from_str(ctx, str);
1786 map = isl_map_lexmin(map);
1787 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1788 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1789 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1790 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1791 map2 = isl_map_read_from_str(ctx, str);
1792 assert(isl_map_is_equal(map, map2));
1793 isl_map_free(map);
1794 isl_map_free(map2);
1796 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1797 " 8i' <= i and 8i' >= -7 + i }";
1798 bmap = isl_basic_map_read_from_str(ctx, str);
1799 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1800 map2 = isl_map_from_pw_multi_aff(pma);
1801 map = isl_map_from_basic_map(bmap);
1802 assert(isl_map_is_equal(map, map2));
1803 isl_map_free(map);
1804 isl_map_free(map2);
1806 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1807 map = isl_map_read_from_str(ctx, str);
1808 map = isl_map_lexmin(map);
1809 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1810 map2 = isl_map_read_from_str(ctx, str);
1811 assert(isl_map_is_equal(map, map2));
1812 isl_map_free(map);
1813 isl_map_free(map2);
1815 /* Check that empty pieces are properly combined. */
1816 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1817 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1818 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1819 map = isl_map_read_from_str(ctx, str);
1820 map = isl_map_lexmin(map);
1821 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1822 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1823 "x >= 4 }";
1824 map2 = isl_map_read_from_str(ctx, str);
1825 assert(isl_map_is_equal(map, map2));
1826 isl_map_free(map);
1827 isl_map_free(map2);
1829 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1830 " 8i' <= i and 8i' >= -7 + i }";
1831 set = isl_set_read_from_str(ctx, str);
1832 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1833 set2 = isl_set_from_pw_multi_aff(pma);
1834 equal = isl_set_is_equal(set, set2);
1835 isl_set_free(set);
1836 isl_set_free(set2);
1837 if (equal < 0)
1838 return -1;
1839 if (!equal)
1840 isl_die(ctx, isl_error_unknown,
1841 "unexpected difference between set and "
1842 "piecewise affine expression", return -1);
1844 return 0;
1847 /* Check that isl_set_min_val and isl_set_max_val compute the correct
1848 * result on non-convex inputs.
1850 static int test_min(struct isl_ctx *ctx)
1852 isl_set *set;
1853 isl_aff *aff;
1854 isl_val *val;
1855 int min_ok, max_ok;
1857 set = isl_set_read_from_str(ctx, "{ [-1]; [1] }");
1858 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x] }");
1859 val = isl_set_min_val(set, aff);
1860 min_ok = isl_val_is_negone(val);
1861 isl_val_free(val);
1862 val = isl_set_max_val(set, aff);
1863 max_ok = isl_val_is_one(val);
1864 isl_val_free(val);
1865 isl_aff_free(aff);
1866 isl_set_free(set);
1868 if (min_ok < 0 || max_ok < 0)
1869 return -1;
1870 if (!min_ok)
1871 isl_die(ctx, isl_error_unknown,
1872 "unexpected minimum", return -1);
1873 if (!max_ok)
1874 isl_die(ctx, isl_error_unknown,
1875 "unexpected maximum", return -1);
1877 return 0;
1880 struct must_may {
1881 isl_map *must;
1882 isl_map *may;
1885 static int collect_must_may(__isl_take isl_map *dep, int must,
1886 void *dep_user, void *user)
1888 struct must_may *mm = (struct must_may *)user;
1890 if (must)
1891 mm->must = isl_map_union(mm->must, dep);
1892 else
1893 mm->may = isl_map_union(mm->may, dep);
1895 return 0;
1898 static int common_space(void *first, void *second)
1900 int depth = *(int *)first;
1901 return 2 * depth;
1904 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1906 isl_map *map2;
1907 int equal;
1909 if (!map)
1910 return -1;
1912 map2 = isl_map_read_from_str(map->ctx, str);
1913 equal = isl_map_is_equal(map, map2);
1914 isl_map_free(map2);
1916 return equal;
1919 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1921 int equal;
1923 equal = map_is_equal(map, str);
1924 if (equal < 0)
1925 return -1;
1926 if (!equal)
1927 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1928 "result not as expected", return -1);
1929 return 0;
1932 void test_dep(struct isl_ctx *ctx)
1934 const char *str;
1935 isl_space *dim;
1936 isl_map *map;
1937 isl_access_info *ai;
1938 isl_flow *flow;
1939 int depth;
1940 struct must_may mm;
1942 depth = 3;
1944 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1945 map = isl_map_read_from_str(ctx, str);
1946 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1948 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1949 map = isl_map_read_from_str(ctx, str);
1950 ai = isl_access_info_add_source(ai, map, 1, &depth);
1952 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1953 map = isl_map_read_from_str(ctx, str);
1954 ai = isl_access_info_add_source(ai, map, 1, &depth);
1956 flow = isl_access_info_compute_flow(ai);
1957 dim = isl_space_alloc(ctx, 0, 3, 3);
1958 mm.must = isl_map_empty(isl_space_copy(dim));
1959 mm.may = isl_map_empty(dim);
1961 isl_flow_foreach(flow, collect_must_may, &mm);
1963 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1964 " [1,10,0] -> [2,5,0] }";
1965 assert(map_is_equal(mm.must, str));
1966 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
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, 1, &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 <= 4) or (6 <= i <= 10) }";
1994 assert(map_is_equal(mm.must, str));
1995 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1996 assert(map_is_equal(mm.may, str));
1998 isl_map_free(mm.must);
1999 isl_map_free(mm.may);
2000 isl_flow_free(flow);
2003 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2004 map = isl_map_read_from_str(ctx, str);
2005 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2007 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2008 map = isl_map_read_from_str(ctx, str);
2009 ai = isl_access_info_add_source(ai, map, 0, &depth);
2011 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2012 map = isl_map_read_from_str(ctx, str);
2013 ai = isl_access_info_add_source(ai, map, 0, &depth);
2015 flow = isl_access_info_compute_flow(ai);
2016 dim = isl_space_alloc(ctx, 0, 3, 3);
2017 mm.must = isl_map_empty(isl_space_copy(dim));
2018 mm.may = isl_map_empty(dim);
2020 isl_flow_foreach(flow, collect_must_may, &mm);
2022 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2023 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2024 assert(map_is_equal(mm.may, str));
2025 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2026 assert(map_is_equal(mm.must, str));
2028 isl_map_free(mm.must);
2029 isl_map_free(mm.may);
2030 isl_flow_free(flow);
2033 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2034 map = isl_map_read_from_str(ctx, str);
2035 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2037 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2038 map = isl_map_read_from_str(ctx, str);
2039 ai = isl_access_info_add_source(ai, map, 0, &depth);
2041 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2042 map = isl_map_read_from_str(ctx, str);
2043 ai = isl_access_info_add_source(ai, map, 0, &depth);
2045 flow = isl_access_info_compute_flow(ai);
2046 dim = isl_space_alloc(ctx, 0, 3, 3);
2047 mm.must = isl_map_empty(isl_space_copy(dim));
2048 mm.may = isl_map_empty(dim);
2050 isl_flow_foreach(flow, collect_must_may, &mm);
2052 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2053 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2054 assert(map_is_equal(mm.may, str));
2055 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2056 assert(map_is_equal(mm.must, str));
2058 isl_map_free(mm.must);
2059 isl_map_free(mm.may);
2060 isl_flow_free(flow);
2063 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2064 map = isl_map_read_from_str(ctx, str);
2065 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2067 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2068 map = isl_map_read_from_str(ctx, str);
2069 ai = isl_access_info_add_source(ai, map, 0, &depth);
2071 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2072 map = isl_map_read_from_str(ctx, str);
2073 ai = isl_access_info_add_source(ai, map, 0, &depth);
2075 flow = isl_access_info_compute_flow(ai);
2076 dim = isl_space_alloc(ctx, 0, 3, 3);
2077 mm.must = isl_map_empty(isl_space_copy(dim));
2078 mm.may = isl_map_empty(dim);
2080 isl_flow_foreach(flow, collect_must_may, &mm);
2082 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2083 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2084 assert(map_is_equal(mm.may, str));
2085 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2086 assert(map_is_equal(mm.must, str));
2088 isl_map_free(mm.must);
2089 isl_map_free(mm.may);
2090 isl_flow_free(flow);
2093 depth = 5;
2095 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2096 map = isl_map_read_from_str(ctx, str);
2097 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2099 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2100 map = isl_map_read_from_str(ctx, str);
2101 ai = isl_access_info_add_source(ai, map, 1, &depth);
2103 flow = isl_access_info_compute_flow(ai);
2104 dim = isl_space_alloc(ctx, 0, 5, 5);
2105 mm.must = isl_map_empty(isl_space_copy(dim));
2106 mm.may = isl_map_empty(dim);
2108 isl_flow_foreach(flow, collect_must_may, &mm);
2110 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2111 assert(map_is_equal(mm.must, str));
2112 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2113 assert(map_is_equal(mm.may, str));
2115 isl_map_free(mm.must);
2116 isl_map_free(mm.may);
2117 isl_flow_free(flow);
2120 /* Check that the dependence analysis proceeds without errors.
2121 * Earlier versions of isl would break down during the analysis
2122 * due to the use of the wrong spaces.
2124 static int test_flow(isl_ctx *ctx)
2126 const char *str;
2127 isl_union_map *access, *schedule;
2128 isl_union_map *must_dep, *may_dep;
2129 int r;
2131 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2132 access = isl_union_map_read_from_str(ctx, str);
2133 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2134 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2135 "S2[] -> [1,0,0,0]; "
2136 "S3[] -> [-1,0,0,0] }";
2137 schedule = isl_union_map_read_from_str(ctx, str);
2138 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2139 isl_union_map_copy(access), schedule,
2140 &must_dep, &may_dep, NULL, NULL);
2141 isl_union_map_free(may_dep);
2142 isl_union_map_free(must_dep);
2144 return r;
2147 struct {
2148 const char *map;
2149 int sv;
2150 } sv_tests[] = {
2151 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2152 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2153 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2154 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2155 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2156 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2157 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2158 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2159 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2162 int test_sv(isl_ctx *ctx)
2164 isl_union_map *umap;
2165 int i;
2166 int sv;
2168 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2169 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2170 sv = isl_union_map_is_single_valued(umap);
2171 isl_union_map_free(umap);
2172 if (sv < 0)
2173 return -1;
2174 if (sv_tests[i].sv && !sv)
2175 isl_die(ctx, isl_error_internal,
2176 "map not detected as single valued", return -1);
2177 if (!sv_tests[i].sv && sv)
2178 isl_die(ctx, isl_error_internal,
2179 "map detected as single valued", return -1);
2182 return 0;
2185 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
2187 isl_map *map;
2189 map = isl_map_read_from_str(ctx, str);
2190 if (bijective)
2191 assert(isl_map_is_bijective(map));
2192 else
2193 assert(!isl_map_is_bijective(map));
2194 isl_map_free(map);
2197 void test_bijective(struct isl_ctx *ctx)
2199 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
2200 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
2201 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
2202 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
2203 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
2204 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
2205 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
2206 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
2207 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
2208 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
2209 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
2210 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
2211 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
2214 /* Inputs for isl_pw_qpolynomial_gist tests.
2215 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2217 struct {
2218 const char *pwqp;
2219 const char *set;
2220 const char *gist;
2221 } pwqp_gist_tests[] = {
2222 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2223 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2224 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2225 "{ [i] -> -1/2 + 1/2 * i }" },
2226 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2229 static int test_pwqp(struct isl_ctx *ctx)
2231 int i;
2232 const char *str;
2233 isl_set *set;
2234 isl_pw_qpolynomial *pwqp1, *pwqp2;
2235 int equal;
2237 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2238 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2240 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2241 isl_dim_in, 1, 1);
2243 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2244 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2246 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2248 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2250 isl_pw_qpolynomial_free(pwqp1);
2252 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2253 str = pwqp_gist_tests[i].pwqp;
2254 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2255 str = pwqp_gist_tests[i].set;
2256 set = isl_set_read_from_str(ctx, str);
2257 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2258 str = pwqp_gist_tests[i].gist;
2259 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2260 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2261 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2262 isl_pw_qpolynomial_free(pwqp1);
2264 if (equal < 0)
2265 return -1;
2266 if (!equal)
2267 isl_die(ctx, isl_error_unknown,
2268 "unexpected result", return -1);
2271 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2272 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2273 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2274 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2276 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2278 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2280 isl_pw_qpolynomial_free(pwqp1);
2282 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2283 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2284 str = "{ [x] -> x }";
2285 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2287 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2289 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2291 isl_pw_qpolynomial_free(pwqp1);
2293 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2294 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2295 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2296 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2297 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2298 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2299 isl_pw_qpolynomial_free(pwqp1);
2301 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2302 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2303 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2304 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2305 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2306 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2307 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2308 isl_pw_qpolynomial_free(pwqp1);
2309 isl_pw_qpolynomial_free(pwqp2);
2310 if (equal < 0)
2311 return -1;
2312 if (!equal)
2313 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2315 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2316 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2317 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2318 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2319 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2320 isl_val_one(ctx));
2321 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2322 isl_pw_qpolynomial_free(pwqp1);
2323 isl_pw_qpolynomial_free(pwqp2);
2324 if (equal < 0)
2325 return -1;
2326 if (!equal)
2327 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2329 return 0;
2332 void test_split_periods(isl_ctx *ctx)
2334 const char *str;
2335 isl_pw_qpolynomial *pwqp;
2337 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2338 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2339 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2340 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2342 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2343 assert(pwqp);
2345 isl_pw_qpolynomial_free(pwqp);
2348 void test_union(isl_ctx *ctx)
2350 const char *str;
2351 isl_union_set *uset1, *uset2;
2352 isl_union_map *umap1, *umap2;
2354 str = "{ [i] : 0 <= i <= 1 }";
2355 uset1 = isl_union_set_read_from_str(ctx, str);
2356 str = "{ [1] -> [0] }";
2357 umap1 = isl_union_map_read_from_str(ctx, str);
2359 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2360 assert(isl_union_map_is_equal(umap1, umap2));
2362 isl_union_map_free(umap1);
2363 isl_union_map_free(umap2);
2365 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2366 umap1 = isl_union_map_read_from_str(ctx, str);
2367 str = "{ A[i]; B[i] }";
2368 uset1 = isl_union_set_read_from_str(ctx, str);
2370 uset2 = isl_union_map_domain(umap1);
2372 assert(isl_union_set_is_equal(uset1, uset2));
2374 isl_union_set_free(uset1);
2375 isl_union_set_free(uset2);
2378 void test_bound(isl_ctx *ctx)
2380 const char *str;
2381 isl_pw_qpolynomial *pwqp;
2382 isl_pw_qpolynomial_fold *pwf;
2384 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2385 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2386 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2387 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
2388 isl_pw_qpolynomial_fold_free(pwf);
2390 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2391 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2392 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2393 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
2394 isl_pw_qpolynomial_fold_free(pwf);
2397 void test_lift(isl_ctx *ctx)
2399 const char *str;
2400 isl_basic_map *bmap;
2401 isl_basic_set *bset;
2403 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2404 bset = isl_basic_set_read_from_str(ctx, str);
2405 bset = isl_basic_set_lift(bset);
2406 bmap = isl_basic_map_from_range(bset);
2407 bset = isl_basic_map_domain(bmap);
2408 isl_basic_set_free(bset);
2411 struct {
2412 const char *set1;
2413 const char *set2;
2414 int subset;
2415 } subset_tests[] = {
2416 { "{ [112, 0] }",
2417 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2418 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2419 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2420 { "{ [65] }",
2421 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2422 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2423 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2424 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2425 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2426 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2427 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2428 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2429 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2430 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2431 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2432 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2433 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2434 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
2435 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
2436 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
2437 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
2438 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
2439 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
2440 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
2441 "4e0 <= -57 + i0 + i1)) or "
2442 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
2443 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
2444 "4e0 >= -61 + i0 + i1)) or "
2445 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
2448 static int test_subset(isl_ctx *ctx)
2450 int i;
2451 isl_set *set1, *set2;
2452 int subset;
2454 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2455 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2456 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2457 subset = isl_set_is_subset(set1, set2);
2458 isl_set_free(set1);
2459 isl_set_free(set2);
2460 if (subset < 0)
2461 return -1;
2462 if (subset != subset_tests[i].subset)
2463 isl_die(ctx, isl_error_unknown,
2464 "incorrect subset result", return -1);
2467 return 0;
2470 struct {
2471 const char *minuend;
2472 const char *subtrahend;
2473 const char *difference;
2474 } subtract_domain_tests[] = {
2475 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2476 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2477 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2480 static int test_subtract(isl_ctx *ctx)
2482 int i;
2483 isl_union_map *umap1, *umap2;
2484 isl_union_pw_multi_aff *upma1, *upma2;
2485 isl_union_set *uset;
2486 int equal;
2488 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2489 umap1 = isl_union_map_read_from_str(ctx,
2490 subtract_domain_tests[i].minuend);
2491 uset = isl_union_set_read_from_str(ctx,
2492 subtract_domain_tests[i].subtrahend);
2493 umap2 = isl_union_map_read_from_str(ctx,
2494 subtract_domain_tests[i].difference);
2495 umap1 = isl_union_map_subtract_domain(umap1, uset);
2496 equal = isl_union_map_is_equal(umap1, umap2);
2497 isl_union_map_free(umap1);
2498 isl_union_map_free(umap2);
2499 if (equal < 0)
2500 return -1;
2501 if (!equal)
2502 isl_die(ctx, isl_error_unknown,
2503 "incorrect subtract domain result", return -1);
2506 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2507 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
2508 subtract_domain_tests[i].minuend);
2509 uset = isl_union_set_read_from_str(ctx,
2510 subtract_domain_tests[i].subtrahend);
2511 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
2512 subtract_domain_tests[i].difference);
2513 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
2514 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
2515 isl_union_pw_multi_aff_free(upma1);
2516 isl_union_pw_multi_aff_free(upma2);
2517 if (equal < 0)
2518 return -1;
2519 if (!equal)
2520 isl_die(ctx, isl_error_unknown,
2521 "incorrect subtract domain result", return -1);
2524 return 0;
2527 int test_factorize(isl_ctx *ctx)
2529 const char *str;
2530 isl_basic_set *bset;
2531 isl_factorizer *f;
2533 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2534 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2535 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2536 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2537 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2538 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2539 "3i5 >= -2i0 - i2 + 3i4 }";
2540 bset = isl_basic_set_read_from_str(ctx, str);
2541 f = isl_basic_set_factorizer(bset);
2542 isl_basic_set_free(bset);
2543 isl_factorizer_free(f);
2544 if (!f)
2545 isl_die(ctx, isl_error_unknown,
2546 "failed to construct factorizer", return -1);
2548 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2549 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2550 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2551 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2552 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2553 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2554 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2555 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2556 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2557 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2558 bset = isl_basic_set_read_from_str(ctx, str);
2559 f = isl_basic_set_factorizer(bset);
2560 isl_basic_set_free(bset);
2561 isl_factorizer_free(f);
2562 if (!f)
2563 isl_die(ctx, isl_error_unknown,
2564 "failed to construct factorizer", return -1);
2566 return 0;
2569 static int check_injective(__isl_take isl_map *map, void *user)
2571 int *injective = user;
2573 *injective = isl_map_is_injective(map);
2574 isl_map_free(map);
2576 if (*injective < 0 || !*injective)
2577 return -1;
2579 return 0;
2582 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2583 const char *r, const char *s, int tilable, int parallel)
2585 int i;
2586 isl_union_set *D;
2587 isl_union_map *W, *R, *S;
2588 isl_union_map *empty;
2589 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2590 isl_union_map *validity, *proximity, *coincidence;
2591 isl_union_map *schedule;
2592 isl_union_map *test;
2593 isl_union_set *delta;
2594 isl_union_set *domain;
2595 isl_set *delta_set;
2596 isl_set *slice;
2597 isl_set *origin;
2598 isl_schedule_constraints *sc;
2599 isl_schedule *sched;
2600 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2602 D = isl_union_set_read_from_str(ctx, d);
2603 W = isl_union_map_read_from_str(ctx, w);
2604 R = isl_union_map_read_from_str(ctx, r);
2605 S = isl_union_map_read_from_str(ctx, s);
2607 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2608 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2610 empty = isl_union_map_empty(isl_union_map_get_space(S));
2611 isl_union_map_compute_flow(isl_union_map_copy(R),
2612 isl_union_map_copy(W), empty,
2613 isl_union_map_copy(S),
2614 &dep_raw, NULL, NULL, NULL);
2615 isl_union_map_compute_flow(isl_union_map_copy(W),
2616 isl_union_map_copy(W),
2617 isl_union_map_copy(R),
2618 isl_union_map_copy(S),
2619 &dep_waw, &dep_war, NULL, NULL);
2621 dep = isl_union_map_union(dep_waw, dep_war);
2622 dep = isl_union_map_union(dep, dep_raw);
2623 validity = isl_union_map_copy(dep);
2624 coincidence = isl_union_map_copy(dep);
2625 proximity = isl_union_map_copy(dep);
2627 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
2628 sc = isl_schedule_constraints_set_validity(sc, validity);
2629 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
2630 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2631 sched = isl_schedule_constraints_compute_schedule(sc);
2632 schedule = isl_schedule_get_map(sched);
2633 isl_schedule_free(sched);
2634 isl_union_map_free(W);
2635 isl_union_map_free(R);
2636 isl_union_map_free(S);
2638 is_injection = 1;
2639 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2641 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2642 is_complete = isl_union_set_is_subset(D, domain);
2643 isl_union_set_free(D);
2644 isl_union_set_free(domain);
2646 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2647 test = isl_union_map_apply_range(test, dep);
2648 test = isl_union_map_apply_range(test, schedule);
2650 delta = isl_union_map_deltas(test);
2651 if (isl_union_set_n_set(delta) == 0) {
2652 is_tilable = 1;
2653 is_parallel = 1;
2654 is_nonneg = 1;
2655 isl_union_set_free(delta);
2656 } else {
2657 delta_set = isl_set_from_union_set(delta);
2659 slice = isl_set_universe(isl_set_get_space(delta_set));
2660 for (i = 0; i < tilable; ++i)
2661 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2662 is_tilable = isl_set_is_subset(delta_set, slice);
2663 isl_set_free(slice);
2665 slice = isl_set_universe(isl_set_get_space(delta_set));
2666 for (i = 0; i < parallel; ++i)
2667 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2668 is_parallel = isl_set_is_subset(delta_set, slice);
2669 isl_set_free(slice);
2671 origin = isl_set_universe(isl_set_get_space(delta_set));
2672 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2673 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2675 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2676 delta_set = isl_set_lexmin(delta_set);
2678 is_nonneg = isl_set_is_equal(delta_set, origin);
2680 isl_set_free(origin);
2681 isl_set_free(delta_set);
2684 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2685 is_injection < 0 || is_complete < 0)
2686 return -1;
2687 if (!is_complete)
2688 isl_die(ctx, isl_error_unknown,
2689 "generated schedule incomplete", return -1);
2690 if (!is_injection)
2691 isl_die(ctx, isl_error_unknown,
2692 "generated schedule not injective on each statement",
2693 return -1);
2694 if (!is_nonneg)
2695 isl_die(ctx, isl_error_unknown,
2696 "negative dependences in generated schedule",
2697 return -1);
2698 if (!is_tilable)
2699 isl_die(ctx, isl_error_unknown,
2700 "generated schedule not as tilable as expected",
2701 return -1);
2702 if (!is_parallel)
2703 isl_die(ctx, isl_error_unknown,
2704 "generated schedule not as parallel as expected",
2705 return -1);
2707 return 0;
2710 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2711 const char *domain, const char *validity, const char *proximity)
2713 isl_union_set *dom;
2714 isl_union_map *dep;
2715 isl_union_map *prox;
2716 isl_schedule_constraints *sc;
2717 isl_schedule *schedule;
2718 isl_union_map *sched;
2720 dom = isl_union_set_read_from_str(ctx, domain);
2721 dep = isl_union_map_read_from_str(ctx, validity);
2722 prox = isl_union_map_read_from_str(ctx, proximity);
2723 sc = isl_schedule_constraints_on_domain(dom);
2724 sc = isl_schedule_constraints_set_validity(sc, dep);
2725 sc = isl_schedule_constraints_set_proximity(sc, prox);
2726 schedule = isl_schedule_constraints_compute_schedule(sc);
2727 sched = isl_schedule_get_map(schedule);
2728 isl_schedule_free(schedule);
2730 return sched;
2733 /* Check that a schedule can be constructed on the given domain
2734 * with the given validity and proximity constraints.
2736 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2737 const char *validity, const char *proximity)
2739 isl_union_map *sched;
2741 sched = compute_schedule(ctx, domain, validity, proximity);
2742 if (!sched)
2743 return -1;
2745 isl_union_map_free(sched);
2746 return 0;
2749 int test_special_schedule(isl_ctx *ctx, const char *domain,
2750 const char *validity, const char *proximity, const char *expected_sched)
2752 isl_union_map *sched1, *sched2;
2753 int equal;
2755 sched1 = compute_schedule(ctx, domain, validity, proximity);
2756 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2758 equal = isl_union_map_is_equal(sched1, sched2);
2759 isl_union_map_free(sched1);
2760 isl_union_map_free(sched2);
2762 if (equal < 0)
2763 return -1;
2764 if (!equal)
2765 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2766 return -1);
2768 return 0;
2771 /* Check that the schedule map is properly padded, even after being
2772 * reconstructed from the band forest.
2774 static int test_padded_schedule(isl_ctx *ctx)
2776 const char *str;
2777 isl_union_set *D;
2778 isl_union_map *validity, *proximity;
2779 isl_schedule_constraints *sc;
2780 isl_schedule *sched;
2781 isl_union_map *map1, *map2;
2782 isl_band_list *list;
2783 int equal;
2785 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2786 D = isl_union_set_read_from_str(ctx, str);
2787 validity = isl_union_map_empty(isl_union_set_get_space(D));
2788 proximity = isl_union_map_copy(validity);
2789 sc = isl_schedule_constraints_on_domain(D);
2790 sc = isl_schedule_constraints_set_validity(sc, validity);
2791 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2792 sched = isl_schedule_constraints_compute_schedule(sc);
2793 map1 = isl_schedule_get_map(sched);
2794 list = isl_schedule_get_band_forest(sched);
2795 isl_band_list_free(list);
2796 map2 = isl_schedule_get_map(sched);
2797 isl_schedule_free(sched);
2798 equal = isl_union_map_is_equal(map1, map2);
2799 isl_union_map_free(map1);
2800 isl_union_map_free(map2);
2802 if (equal < 0)
2803 return -1;
2804 if (!equal)
2805 isl_die(ctx, isl_error_unknown,
2806 "reconstructed schedule map not the same as original",
2807 return -1);
2809 return 0;
2812 /* Input for testing of schedule construction based on
2813 * conditional constraints.
2815 * domain is the iteration domain
2816 * flow are the flow dependences, which determine the validity and
2817 * proximity constraints
2818 * condition are the conditions on the conditional validity constraints
2819 * conditional_validity are the conditional validity constraints
2820 * outer_band_n is the expected number of members in the outer band
2822 struct {
2823 const char *domain;
2824 const char *flow;
2825 const char *condition;
2826 const char *conditional_validity;
2827 int outer_band_n;
2828 } live_range_tests[] = {
2829 /* Contrived example that illustrates that we need to keep
2830 * track of tagged condition dependences and
2831 * tagged conditional validity dependences
2832 * in isl_sched_edge separately.
2833 * In particular, the conditional validity constraints on A
2834 * cannot be satisfied,
2835 * but they can be ignored because there are no corresponding
2836 * condition constraints. However, we do have an additional
2837 * conditional validity constraint that maps to the same
2838 * dependence relation
2839 * as the condition constraint on B. If we did not make a distinction
2840 * between tagged condition and tagged conditional validity
2841 * dependences, then we
2842 * could end up treating this shared dependence as an condition
2843 * constraint on A, forcing a localization of the conditions,
2844 * which is impossible.
2846 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
2847 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
2848 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
2849 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2850 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2851 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
2854 /* TACO 2013 Fig. 7 */
2855 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2856 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2857 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2858 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
2859 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
2860 "0 <= i < n and 0 <= j < n - 1 }",
2861 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
2862 "0 <= i < n and 0 <= j < j' < n;"
2863 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
2864 "0 <= i < i' < n and 0 <= j,j' < n;"
2865 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
2866 "0 <= i,j,j' < n and j < j' }",
2869 /* TACO 2013 Fig. 7, without tags */
2870 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2871 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2872 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2873 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2874 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2875 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
2876 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
2877 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
2880 /* TACO 2013 Fig. 12 */
2881 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
2882 "S3[i,3] : 0 <= i <= 1 }",
2883 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
2884 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
2885 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
2886 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
2887 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2888 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
2889 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2890 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
2891 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
2892 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
2893 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
2898 /* Test schedule construction based on conditional constraints.
2899 * In particular, check the number of members in the outer band
2900 * as an indication of whether tiling is possible or not.
2902 static int test_conditional_schedule_constraints(isl_ctx *ctx)
2904 int i;
2905 isl_union_set *domain;
2906 isl_union_map *condition;
2907 isl_union_map *flow;
2908 isl_union_map *validity;
2909 isl_schedule_constraints *sc;
2910 isl_schedule *schedule;
2911 isl_band_list *list;
2912 isl_band *band;
2913 int n_member;
2915 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
2916 domain = isl_union_set_read_from_str(ctx,
2917 live_range_tests[i].domain);
2918 flow = isl_union_map_read_from_str(ctx,
2919 live_range_tests[i].flow);
2920 condition = isl_union_map_read_from_str(ctx,
2921 live_range_tests[i].condition);
2922 validity = isl_union_map_read_from_str(ctx,
2923 live_range_tests[i].conditional_validity);
2924 sc = isl_schedule_constraints_on_domain(domain);
2925 sc = isl_schedule_constraints_set_validity(sc,
2926 isl_union_map_copy(flow));
2927 sc = isl_schedule_constraints_set_proximity(sc, flow);
2928 sc = isl_schedule_constraints_set_conditional_validity(sc,
2929 condition, validity);
2930 schedule = isl_schedule_constraints_compute_schedule(sc);
2931 list = isl_schedule_get_band_forest(schedule);
2932 band = isl_band_list_get_band(list, 0);
2933 n_member = isl_band_n_member(band);
2934 isl_band_free(band);
2935 isl_band_list_free(list);
2936 isl_schedule_free(schedule);
2938 if (!schedule)
2939 return -1;
2940 if (n_member != live_range_tests[i].outer_band_n)
2941 isl_die(ctx, isl_error_unknown,
2942 "unexpected number of members in outer band",
2943 return -1);
2945 return 0;
2948 int test_schedule(isl_ctx *ctx)
2950 const char *D, *W, *R, *V, *P, *S;
2952 /* Handle resulting schedule with zero bands. */
2953 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2954 return -1;
2956 /* Jacobi */
2957 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2958 W = "{ S1[t,i] -> a[t,i] }";
2959 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2960 "S1[t,i] -> a[t-1,i+1] }";
2961 S = "{ S1[t,i] -> [t,i] }";
2962 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2963 return -1;
2965 /* Fig. 5 of CC2008 */
2966 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2967 "j <= -1 + N }";
2968 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2969 "j >= 2 and j <= -1 + N }";
2970 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2971 "j >= 2 and j <= -1 + N; "
2972 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2973 "j >= 2 and j <= -1 + N }";
2974 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2975 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2976 return -1;
2978 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2979 W = "{ S1[i] -> a[i] }";
2980 R = "{ S2[i] -> a[i+1] }";
2981 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2982 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2983 return -1;
2985 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2986 W = "{ S1[i] -> a[i] }";
2987 R = "{ S2[i] -> a[9-i] }";
2988 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2989 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2990 return -1;
2992 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2993 W = "{ S1[i] -> a[i] }";
2994 R = "[N] -> { S2[i] -> a[N-1-i] }";
2995 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2996 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2997 return -1;
2999 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
3000 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
3001 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
3002 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
3003 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3004 return -1;
3006 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3007 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
3008 R = "{ S2[i,j] -> a[i-1,j] }";
3009 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3010 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3011 return -1;
3013 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3014 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
3015 R = "{ S2[i,j] -> a[i,j-1] }";
3016 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3017 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3018 return -1;
3020 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
3021 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
3022 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
3023 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
3024 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
3025 "S_0[] -> [0, 0, 0] }";
3026 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
3027 return -1;
3028 ctx->opt->schedule_parametric = 0;
3029 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3030 return -1;
3031 ctx->opt->schedule_parametric = 1;
3033 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
3034 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
3035 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
3036 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
3037 "S4[i] -> a[i,N] }";
3038 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
3039 "S4[i] -> [4,i,0] }";
3040 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3041 return -1;
3043 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
3044 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3045 "j <= N }";
3046 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3047 "j <= N; "
3048 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
3049 "j <= N }";
3050 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3051 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3052 return -1;
3054 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
3055 " S_2[t] : t >= 0 and t <= -1 + N; "
3056 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
3057 "i <= -1 + N }";
3058 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
3059 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
3060 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
3061 "i >= 0 and i <= -1 + N }";
3062 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
3063 "i >= 0 and i <= -1 + N; "
3064 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
3065 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
3066 " S_0[t] -> [0, t, 0] }";
3068 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3069 return -1;
3070 ctx->opt->schedule_parametric = 0;
3071 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3072 return -1;
3073 ctx->opt->schedule_parametric = 1;
3075 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
3076 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
3077 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
3078 return -1;
3080 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
3081 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
3082 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
3083 "j >= 0 and j <= -1 + N; "
3084 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3085 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
3086 "j >= 0 and j <= -1 + N; "
3087 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3088 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
3089 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3090 return -1;
3092 D = "{ S_0[i] : i >= 0 }";
3093 W = "{ S_0[i] -> a[i] : i >= 0 }";
3094 R = "{ S_0[i] -> a[0] : i >= 0 }";
3095 S = "{ S_0[i] -> [0, i, 0] }";
3096 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3097 return -1;
3099 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
3100 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
3101 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
3102 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
3103 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3104 return -1;
3106 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
3107 "k <= -1 + n and k >= 0 }";
3108 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
3109 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
3110 "k <= -1 + n and k >= 0; "
3111 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
3112 "k <= -1 + n and k >= 0; "
3113 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
3114 "k <= -1 + n and k >= 0 }";
3115 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
3116 ctx->opt->schedule_outer_coincidence = 1;
3117 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3118 return -1;
3119 ctx->opt->schedule_outer_coincidence = 0;
3121 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3122 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3123 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3124 "Stmt_for_body24[i0, i1, 1, 0]:"
3125 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3126 "Stmt_for_body7[i0, i1, i2]:"
3127 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3128 "i2 <= 7 }";
3130 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3131 "Stmt_for_body24[1, i1, i2, i3]:"
3132 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3133 "i2 >= 1;"
3134 "Stmt_for_body24[0, i1, i2, i3] -> "
3135 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3136 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3137 "i3 >= 0;"
3138 "Stmt_for_body24[0, i1, i2, i3] ->"
3139 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3140 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3141 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3142 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3143 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3144 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3145 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3146 "i1 <= 6 and i1 >= 0;"
3147 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3148 "Stmt_for_body7[i0, i1, i2] -> "
3149 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3150 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3151 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3152 "Stmt_for_body7[i0, i1, i2] -> "
3153 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3154 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3155 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3156 P = V;
3157 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3158 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3159 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3161 if (test_special_schedule(ctx, D, V, P, S) < 0)
3162 return -1;
3164 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3165 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3166 "j >= 1 and j <= 7;"
3167 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3168 "j >= 1 and j <= 8 }";
3169 P = "{ }";
3170 S = "{ S_0[i, j] -> [i + j, j] }";
3171 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3172 if (test_special_schedule(ctx, D, V, P, S) < 0)
3173 return -1;
3174 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3176 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3177 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3178 "j >= 0 and j <= -1 + i }";
3179 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3180 "i <= -1 + N and j >= 0;"
3181 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3182 "i <= -2 + N }";
3183 P = "{ }";
3184 S = "{ S_0[i, j] -> [i, j] }";
3185 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3186 if (test_special_schedule(ctx, D, V, P, S) < 0)
3187 return -1;
3188 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3190 /* Test both algorithms on a case with only proximity dependences. */
3191 D = "{ S[i,j] : 0 <= i <= 10 }";
3192 V = "{ }";
3193 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3194 S = "{ S[i, j] -> [j, i] }";
3195 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3196 if (test_special_schedule(ctx, D, V, P, S) < 0)
3197 return -1;
3198 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3199 if (test_special_schedule(ctx, D, V, P, S) < 0)
3200 return -1;
3202 D = "{ A[a]; B[] }";
3203 V = "{}";
3204 P = "{ A[a] -> B[] }";
3205 if (test_has_schedule(ctx, D, V, P) < 0)
3206 return -1;
3208 if (test_padded_schedule(ctx) < 0)
3209 return -1;
3211 /* Check that check for progress is not confused by rational
3212 * solution.
3214 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3215 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3216 "i0 <= -2 + N; "
3217 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3218 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3219 P = "{}";
3220 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3221 if (test_has_schedule(ctx, D, V, P) < 0)
3222 return -1;
3223 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3225 /* Check that we allow schedule rows that are only non-trivial
3226 * on some full-dimensional domains.
3228 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
3229 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
3230 "S1[j] -> S2[1] : 0 <= j <= 1 }";
3231 P = "{}";
3232 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3233 if (test_has_schedule(ctx, D, V, P) < 0)
3234 return -1;
3235 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3237 if (test_conditional_schedule_constraints(ctx) < 0)
3238 return -1;
3240 return 0;
3243 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3245 isl_union_map *umap;
3246 int test;
3248 umap = isl_union_map_read_from_str(ctx, str);
3249 test = isl_union_map_plain_is_injective(umap);
3250 isl_union_map_free(umap);
3251 if (test < 0)
3252 return -1;
3253 if (test == injective)
3254 return 0;
3255 if (injective)
3256 isl_die(ctx, isl_error_unknown,
3257 "map not detected as injective", return -1);
3258 else
3259 isl_die(ctx, isl_error_unknown,
3260 "map detected as injective", return -1);
3263 int test_injective(isl_ctx *ctx)
3265 const char *str;
3267 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
3268 return -1;
3269 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
3270 return -1;
3271 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
3272 return -1;
3273 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
3274 return -1;
3275 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
3276 return -1;
3277 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
3278 return -1;
3279 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
3280 return -1;
3281 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
3282 return -1;
3284 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
3285 if (test_plain_injective(ctx, str, 1))
3286 return -1;
3287 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
3288 if (test_plain_injective(ctx, str, 0))
3289 return -1;
3291 return 0;
3294 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
3296 isl_aff *aff2;
3297 int equal;
3299 if (!aff)
3300 return -1;
3302 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
3303 equal = isl_aff_plain_is_equal(aff, aff2);
3304 isl_aff_free(aff2);
3306 return equal;
3309 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
3311 int equal;
3313 equal = aff_plain_is_equal(aff, str);
3314 if (equal < 0)
3315 return -1;
3316 if (!equal)
3317 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
3318 "result not as expected", return -1);
3319 return 0;
3322 struct {
3323 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3324 __isl_take isl_aff *aff2);
3325 } aff_bin_op[] = {
3326 ['+'] = { &isl_aff_add },
3327 ['-'] = { &isl_aff_sub },
3328 ['*'] = { &isl_aff_mul },
3329 ['/'] = { &isl_aff_div },
3332 struct {
3333 const char *arg1;
3334 unsigned char op;
3335 const char *arg2;
3336 const char *res;
3337 } aff_bin_tests[] = {
3338 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
3339 "{ [i] -> [2i] }" },
3340 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
3341 "{ [i] -> [0] }" },
3342 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
3343 "{ [i] -> [2i] }" },
3344 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
3345 "{ [i] -> [2i] }" },
3346 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
3347 "{ [i] -> [i/2] }" },
3348 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
3349 "{ [i] -> [i] }" },
3350 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
3351 "{ [i] -> [NaN] }" },
3352 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
3353 "{ [i] -> [NaN] }" },
3354 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
3355 "{ [i] -> [NaN] }" },
3356 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
3357 "{ [i] -> [NaN] }" },
3358 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
3359 "{ [i] -> [NaN] }" },
3360 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
3361 "{ [i] -> [NaN] }" },
3362 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
3363 "{ [i] -> [NaN] }" },
3364 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
3365 "{ [i] -> [NaN] }" },
3366 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
3367 "{ [i] -> [NaN] }" },
3368 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
3369 "{ [i] -> [NaN] }" },
3370 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
3371 "{ [i] -> [NaN] }" },
3372 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
3373 "{ [i] -> [NaN] }" },
3376 /* Perform some basic tests of binary operations on isl_aff objects.
3378 static int test_bin_aff(isl_ctx *ctx)
3380 int i;
3381 isl_aff *aff1, *aff2, *res;
3382 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3383 __isl_take isl_aff *aff2);
3384 int ok;
3386 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
3387 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
3388 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
3389 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
3390 fn = aff_bin_op[aff_bin_tests[i].op].fn;
3391 aff1 = fn(aff1, aff2);
3392 if (isl_aff_is_nan(res))
3393 ok = isl_aff_is_nan(aff1);
3394 else
3395 ok = isl_aff_plain_is_equal(aff1, res);
3396 isl_aff_free(aff1);
3397 isl_aff_free(res);
3398 if (ok < 0)
3399 return -1;
3400 if (!ok)
3401 isl_die(ctx, isl_error_unknown,
3402 "unexpected result", return -1);
3405 return 0;
3408 struct {
3409 __isl_give isl_union_pw_multi_aff *(*fn)(
3410 __isl_take isl_union_pw_multi_aff *upma1,
3411 __isl_take isl_union_pw_multi_aff *upma2);
3412 const char *arg1;
3413 const char *arg2;
3414 const char *res;
3415 } upma_bin_tests[] = {
3416 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
3417 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
3418 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
3419 "{ B[x] -> [2] : x >= 0 }",
3420 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
3423 /* Perform some basic tests of binary operations on
3424 * isl_union_pw_multi_aff objects.
3426 static int test_bin_upma(isl_ctx *ctx)
3428 int i;
3429 isl_union_pw_multi_aff *upma1, *upma2, *res;
3430 int ok;
3432 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
3433 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3434 upma_bin_tests[i].arg1);
3435 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3436 upma_bin_tests[i].arg2);
3437 res = isl_union_pw_multi_aff_read_from_str(ctx,
3438 upma_bin_tests[i].res);
3439 upma1 = upma_bin_tests[i].fn(upma1, upma2);
3440 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
3441 isl_union_pw_multi_aff_free(upma1);
3442 isl_union_pw_multi_aff_free(res);
3443 if (ok < 0)
3444 return -1;
3445 if (!ok)
3446 isl_die(ctx, isl_error_unknown,
3447 "unexpected result", return -1);
3450 return 0;
3453 int test_aff(isl_ctx *ctx)
3455 const char *str;
3456 isl_set *set;
3457 isl_space *space;
3458 isl_local_space *ls;
3459 isl_aff *aff;
3460 int zero, equal;
3462 if (test_bin_aff(ctx) < 0)
3463 return -1;
3464 if (test_bin_upma(ctx) < 0)
3465 return -1;
3467 space = isl_space_set_alloc(ctx, 0, 1);
3468 ls = isl_local_space_from_space(space);
3469 aff = isl_aff_zero_on_domain(ls);
3471 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3472 aff = isl_aff_scale_down_ui(aff, 3);
3473 aff = isl_aff_floor(aff);
3474 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3475 aff = isl_aff_scale_down_ui(aff, 2);
3476 aff = isl_aff_floor(aff);
3477 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3479 str = "{ [10] }";
3480 set = isl_set_read_from_str(ctx, str);
3481 aff = isl_aff_gist(aff, set);
3483 aff = isl_aff_add_constant_si(aff, -16);
3484 zero = isl_aff_plain_is_zero(aff);
3485 isl_aff_free(aff);
3487 if (zero < 0)
3488 return -1;
3489 if (!zero)
3490 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3492 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
3493 aff = isl_aff_scale_down_ui(aff, 64);
3494 aff = isl_aff_floor(aff);
3495 equal = aff_check_plain_equal(aff, "{ [-1] }");
3496 isl_aff_free(aff);
3497 if (equal < 0)
3498 return -1;
3500 return 0;
3503 int test_dim_max(isl_ctx *ctx)
3505 int equal;
3506 const char *str;
3507 isl_set *set1, *set2;
3508 isl_set *set;
3509 isl_map *map;
3510 isl_pw_aff *pwaff;
3512 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
3513 set = isl_set_read_from_str(ctx, str);
3514 pwaff = isl_set_dim_max(set, 0);
3515 set1 = isl_set_from_pw_aff(pwaff);
3516 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
3517 set2 = isl_set_read_from_str(ctx, str);
3518 equal = isl_set_is_equal(set1, set2);
3519 isl_set_free(set1);
3520 isl_set_free(set2);
3521 if (equal < 0)
3522 return -1;
3523 if (!equal)
3524 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3526 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
3527 set = isl_set_read_from_str(ctx, str);
3528 pwaff = isl_set_dim_max(set, 0);
3529 set1 = isl_set_from_pw_aff(pwaff);
3530 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3531 set2 = isl_set_read_from_str(ctx, str);
3532 equal = isl_set_is_equal(set1, set2);
3533 isl_set_free(set1);
3534 isl_set_free(set2);
3535 if (equal < 0)
3536 return -1;
3537 if (!equal)
3538 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3540 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
3541 set = isl_set_read_from_str(ctx, str);
3542 pwaff = isl_set_dim_max(set, 0);
3543 set1 = isl_set_from_pw_aff(pwaff);
3544 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3545 set2 = isl_set_read_from_str(ctx, str);
3546 equal = isl_set_is_equal(set1, set2);
3547 isl_set_free(set1);
3548 isl_set_free(set2);
3549 if (equal < 0)
3550 return -1;
3551 if (!equal)
3552 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3554 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
3555 "0 <= i < N and 0 <= j < M }";
3556 map = isl_map_read_from_str(ctx, str);
3557 set = isl_map_range(map);
3559 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
3560 set1 = isl_set_from_pw_aff(pwaff);
3561 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
3562 set2 = isl_set_read_from_str(ctx, str);
3563 equal = isl_set_is_equal(set1, set2);
3564 isl_set_free(set1);
3565 isl_set_free(set2);
3567 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3568 set1 = isl_set_from_pw_aff(pwaff);
3569 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3570 set2 = isl_set_read_from_str(ctx, str);
3571 if (equal >= 0 && equal)
3572 equal = isl_set_is_equal(set1, set2);
3573 isl_set_free(set1);
3574 isl_set_free(set2);
3576 isl_set_free(set);
3578 if (equal < 0)
3579 return -1;
3580 if (!equal)
3581 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3583 /* Check that solutions are properly merged. */
3584 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3585 "c <= -1 + n - 4a - 2b and c >= -2b and "
3586 "4a >= -4 + n and c >= 0 }";
3587 set = isl_set_read_from_str(ctx, str);
3588 pwaff = isl_set_dim_min(set, 2);
3589 set1 = isl_set_from_pw_aff(pwaff);
3590 str = "[n] -> { [(0)] : n >= 1 }";
3591 set2 = isl_set_read_from_str(ctx, str);
3592 equal = isl_set_is_equal(set1, set2);
3593 isl_set_free(set1);
3594 isl_set_free(set2);
3596 if (equal < 0)
3597 return -1;
3598 if (!equal)
3599 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3601 /* Check that empty solution lie in the right space. */
3602 str = "[n] -> { [t,a] : 1 = 0 }";
3603 set = isl_set_read_from_str(ctx, str);
3604 pwaff = isl_set_dim_max(set, 0);
3605 set1 = isl_set_from_pw_aff(pwaff);
3606 str = "[n] -> { [t] : 1 = 0 }";
3607 set2 = isl_set_read_from_str(ctx, str);
3608 equal = isl_set_is_equal(set1, set2);
3609 isl_set_free(set1);
3610 isl_set_free(set2);
3612 if (equal < 0)
3613 return -1;
3614 if (!equal)
3615 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3617 return 0;
3620 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
3622 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
3623 const char *str)
3625 isl_ctx *ctx;
3626 isl_pw_multi_aff *pma2;
3627 int equal;
3629 if (!pma)
3630 return -1;
3632 ctx = isl_pw_multi_aff_get_ctx(pma);
3633 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3634 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
3635 isl_pw_multi_aff_free(pma2);
3637 return equal;
3640 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
3641 * represented by "str".
3643 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
3644 const char *str)
3646 int equal;
3648 equal = pw_multi_aff_plain_is_equal(pma, str);
3649 if (equal < 0)
3650 return -1;
3651 if (!equal)
3652 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
3653 "result not as expected", return -1);
3654 return 0;
3657 /* Basic test for isl_pw_multi_aff_product.
3659 * Check that multiple pieces are properly handled.
3661 static int test_product_pma(isl_ctx *ctx)
3663 int equal;
3664 const char *str;
3665 isl_pw_multi_aff *pma1, *pma2;
3667 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
3668 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3669 str = "{ C[] -> D[] }";
3670 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3671 pma1 = isl_pw_multi_aff_product(pma1, pma2);
3672 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
3673 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
3674 equal = pw_multi_aff_check_plain_equal(pma1, str);
3675 isl_pw_multi_aff_free(pma1);
3676 if (equal < 0)
3677 return -1;
3679 return 0;
3682 int test_product(isl_ctx *ctx)
3684 const char *str;
3685 isl_set *set;
3686 isl_union_set *uset1, *uset2;
3687 int ok;
3689 str = "{ A[i] }";
3690 set = isl_set_read_from_str(ctx, str);
3691 set = isl_set_product(set, isl_set_copy(set));
3692 ok = isl_set_is_wrapping(set);
3693 isl_set_free(set);
3694 if (ok < 0)
3695 return -1;
3696 if (!ok)
3697 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3699 str = "{ [] }";
3700 uset1 = isl_union_set_read_from_str(ctx, str);
3701 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3702 str = "{ [[] -> []] }";
3703 uset2 = isl_union_set_read_from_str(ctx, str);
3704 ok = isl_union_set_is_equal(uset1, uset2);
3705 isl_union_set_free(uset1);
3706 isl_union_set_free(uset2);
3707 if (ok < 0)
3708 return -1;
3709 if (!ok)
3710 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3712 if (test_product_pma(ctx) < 0)
3713 return -1;
3715 return 0;
3718 /* Check that two sets are not considered disjoint just because
3719 * they have a different set of (named) parameters.
3721 static int test_disjoint(isl_ctx *ctx)
3723 const char *str;
3724 isl_set *set, *set2;
3725 int disjoint;
3727 str = "[n] -> { [[]->[]] }";
3728 set = isl_set_read_from_str(ctx, str);
3729 str = "{ [[]->[]] }";
3730 set2 = isl_set_read_from_str(ctx, str);
3731 disjoint = isl_set_is_disjoint(set, set2);
3732 isl_set_free(set);
3733 isl_set_free(set2);
3734 if (disjoint < 0)
3735 return -1;
3736 if (disjoint)
3737 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3739 return 0;
3742 int test_equal(isl_ctx *ctx)
3744 const char *str;
3745 isl_set *set, *set2;
3746 int equal;
3748 str = "{ S_6[i] }";
3749 set = isl_set_read_from_str(ctx, str);
3750 str = "{ S_7[i] }";
3751 set2 = isl_set_read_from_str(ctx, str);
3752 equal = isl_set_is_equal(set, set2);
3753 isl_set_free(set);
3754 isl_set_free(set2);
3755 if (equal < 0)
3756 return -1;
3757 if (equal)
3758 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3760 return 0;
3763 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3764 enum isl_dim_type type, unsigned pos, int fixed)
3766 int test;
3768 test = isl_map_plain_is_fixed(map, type, pos, NULL);
3769 isl_map_free(map);
3770 if (test < 0)
3771 return -1;
3772 if (test == fixed)
3773 return 0;
3774 if (fixed)
3775 isl_die(ctx, isl_error_unknown,
3776 "map not detected as fixed", return -1);
3777 else
3778 isl_die(ctx, isl_error_unknown,
3779 "map detected as fixed", return -1);
3782 int test_fixed(isl_ctx *ctx)
3784 const char *str;
3785 isl_map *map;
3787 str = "{ [i] -> [i] }";
3788 map = isl_map_read_from_str(ctx, str);
3789 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3790 return -1;
3791 str = "{ [i] -> [1] }";
3792 map = isl_map_read_from_str(ctx, str);
3793 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3794 return -1;
3795 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3796 map = isl_map_read_from_str(ctx, str);
3797 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3798 return -1;
3799 map = isl_map_read_from_str(ctx, str);
3800 map = isl_map_neg(map);
3801 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3802 return -1;
3804 return 0;
3807 struct isl_vertices_test_data {
3808 const char *set;
3809 int n;
3810 const char *vertex[2];
3811 } vertices_tests[] = {
3812 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
3813 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
3814 { "{ A[t, i] : t = 14 and i = 1 }",
3815 1, { "{ A[14, 1] }" } },
3818 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
3820 static int find_vertex(__isl_take isl_vertex *vertex, void *user)
3822 struct isl_vertices_test_data *data = user;
3823 isl_ctx *ctx;
3824 isl_multi_aff *ma;
3825 isl_basic_set *bset;
3826 isl_pw_multi_aff *pma;
3827 int i;
3828 int equal;
3830 ctx = isl_vertex_get_ctx(vertex);
3831 bset = isl_vertex_get_domain(vertex);
3832 ma = isl_vertex_get_expr(vertex);
3833 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
3835 for (i = 0; i < data->n; ++i) {
3836 isl_pw_multi_aff *pma_i;
3838 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
3839 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
3840 isl_pw_multi_aff_free(pma_i);
3842 if (equal < 0 || equal)
3843 break;
3846 isl_pw_multi_aff_free(pma);
3847 isl_vertex_free(vertex);
3849 if (equal < 0)
3850 return -1;
3852 return equal ? 0 : - 1;
3855 int test_vertices(isl_ctx *ctx)
3857 int i;
3859 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
3860 isl_basic_set *bset;
3861 isl_vertices *vertices;
3862 int ok = 1;
3863 int n;
3865 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
3866 vertices = isl_basic_set_compute_vertices(bset);
3867 n = isl_vertices_get_n_vertices(vertices);
3868 if (vertices_tests[i].n != n)
3869 ok = 0;
3870 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
3871 &vertices_tests[i]) < 0)
3872 ok = 0;
3873 isl_vertices_free(vertices);
3874 isl_basic_set_free(bset);
3876 if (!vertices)
3877 return -1;
3878 if (!ok)
3879 isl_die(ctx, isl_error_unknown, "unexpected vertices",
3880 return -1);
3883 return 0;
3886 int test_union_pw(isl_ctx *ctx)
3888 int equal;
3889 const char *str;
3890 isl_union_set *uset;
3891 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
3893 str = "{ [x] -> x^2 }";
3894 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
3895 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
3896 uset = isl_union_pw_qpolynomial_domain(upwqp1);
3897 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
3898 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
3899 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
3900 isl_union_pw_qpolynomial_free(upwqp1);
3901 isl_union_pw_qpolynomial_free(upwqp2);
3902 if (equal < 0)
3903 return -1;
3904 if (!equal)
3905 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3907 return 0;
3910 int test_output(isl_ctx *ctx)
3912 char *s;
3913 const char *str;
3914 isl_pw_aff *pa;
3915 isl_printer *p;
3916 int equal;
3918 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
3919 pa = isl_pw_aff_read_from_str(ctx, str);
3921 p = isl_printer_to_str(ctx);
3922 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
3923 p = isl_printer_print_pw_aff(p, pa);
3924 s = isl_printer_get_str(p);
3925 isl_printer_free(p);
3926 isl_pw_aff_free(pa);
3927 if (!s)
3928 equal = -1;
3929 else
3930 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
3931 free(s);
3932 if (equal < 0)
3933 return -1;
3934 if (!equal)
3935 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3937 return 0;
3940 int test_sample(isl_ctx *ctx)
3942 const char *str;
3943 isl_basic_set *bset1, *bset2;
3944 int empty, subset;
3946 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3947 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3948 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3949 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3950 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3951 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3952 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3953 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3954 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3955 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3956 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3957 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3958 "d - 1073741821e and "
3959 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3960 "3j >= 1 - a + b + 2e and "
3961 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3962 "3i <= 4 - a + 4b - e and "
3963 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3964 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3965 "c <= -1 + a and 3i >= -2 - a + 3e and "
3966 "1073741822e <= 1073741823 - a + 1073741822b + c and "
3967 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3968 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3969 "1073741823e >= 1 + 1073741823b - d and "
3970 "3i >= 1073741823b + c - 1073741823e - f and "
3971 "3i >= 1 + 2b + e + 3g }";
3972 bset1 = isl_basic_set_read_from_str(ctx, str);
3973 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3974 empty = isl_basic_set_is_empty(bset2);
3975 subset = isl_basic_set_is_subset(bset2, bset1);
3976 isl_basic_set_free(bset1);
3977 isl_basic_set_free(bset2);
3978 if (empty < 0 || subset < 0)
3979 return -1;
3980 if (empty)
3981 isl_die(ctx, isl_error_unknown, "point not found", return -1);
3982 if (!subset)
3983 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
3985 return 0;
3988 int test_fixed_power(isl_ctx *ctx)
3990 const char *str;
3991 isl_map *map;
3992 isl_int exp;
3993 int equal;
3995 isl_int_init(exp);
3996 str = "{ [i] -> [i + 1] }";
3997 map = isl_map_read_from_str(ctx, str);
3998 isl_int_set_si(exp, 23);
3999 map = isl_map_fixed_power(map, exp);
4000 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
4001 isl_int_clear(exp);
4002 isl_map_free(map);
4003 if (equal < 0)
4004 return -1;
4006 return 0;
4009 int test_slice(isl_ctx *ctx)
4011 const char *str;
4012 isl_map *map;
4013 int equal;
4015 str = "{ [i] -> [j] }";
4016 map = isl_map_read_from_str(ctx, str);
4017 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
4018 equal = map_check_equal(map, "{ [i] -> [i] }");
4019 isl_map_free(map);
4020 if (equal < 0)
4021 return -1;
4023 str = "{ [i] -> [j] }";
4024 map = isl_map_read_from_str(ctx, str);
4025 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
4026 equal = map_check_equal(map, "{ [i] -> [j] }");
4027 isl_map_free(map);
4028 if (equal < 0)
4029 return -1;
4031 str = "{ [i] -> [j] }";
4032 map = isl_map_read_from_str(ctx, str);
4033 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
4034 equal = map_check_equal(map, "{ [i] -> [-i] }");
4035 isl_map_free(map);
4036 if (equal < 0)
4037 return -1;
4039 str = "{ [i] -> [j] }";
4040 map = isl_map_read_from_str(ctx, str);
4041 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
4042 equal = map_check_equal(map, "{ [0] -> [j] }");
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_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
4050 equal = map_check_equal(map, "{ [i] -> [j] : 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_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
4058 equal = map_check_equal(map, "{ [i] -> [j] : false }");
4059 isl_map_free(map);
4060 if (equal < 0)
4061 return -1;
4063 return 0;
4066 int test_eliminate(isl_ctx *ctx)
4068 const char *str;
4069 isl_map *map;
4070 int equal;
4072 str = "{ [i] -> [j] : i = 2j }";
4073 map = isl_map_read_from_str(ctx, str);
4074 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
4075 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
4076 isl_map_free(map);
4077 if (equal < 0)
4078 return -1;
4080 return 0;
4083 /* Check that isl_set_dim_residue_class detects that the values of j
4084 * in the set below are all odd and that it does not detect any spurious
4085 * strides.
4087 static int test_residue_class(isl_ctx *ctx)
4089 const char *str;
4090 isl_set *set;
4091 isl_int m, r;
4092 int res;
4094 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
4095 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
4096 set = isl_set_read_from_str(ctx, str);
4097 isl_int_init(m);
4098 isl_int_init(r);
4099 res = isl_set_dim_residue_class(set, 1, &m, &r);
4100 if (res >= 0 &&
4101 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
4102 isl_die(ctx, isl_error_unknown, "incorrect residue class",
4103 res = -1);
4104 isl_int_clear(r);
4105 isl_int_clear(m);
4106 isl_set_free(set);
4108 return res;
4111 int test_align_parameters(isl_ctx *ctx)
4113 const char *str;
4114 isl_space *space;
4115 isl_multi_aff *ma1, *ma2;
4116 int equal;
4118 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
4119 ma1 = isl_multi_aff_read_from_str(ctx, str);
4121 space = isl_space_params_alloc(ctx, 1);
4122 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
4123 ma1 = isl_multi_aff_align_params(ma1, space);
4125 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
4126 ma2 = isl_multi_aff_read_from_str(ctx, str);
4128 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4130 isl_multi_aff_free(ma1);
4131 isl_multi_aff_free(ma2);
4133 if (equal < 0)
4134 return -1;
4135 if (!equal)
4136 isl_die(ctx, isl_error_unknown,
4137 "result not as expected", return -1);
4139 return 0;
4142 static int test_list(isl_ctx *ctx)
4144 isl_id *a, *b, *c, *d, *id;
4145 isl_id_list *list;
4146 int ok;
4148 a = isl_id_alloc(ctx, "a", NULL);
4149 b = isl_id_alloc(ctx, "b", NULL);
4150 c = isl_id_alloc(ctx, "c", NULL);
4151 d = isl_id_alloc(ctx, "d", NULL);
4153 list = isl_id_list_alloc(ctx, 4);
4154 list = isl_id_list_add(list, a);
4155 list = isl_id_list_add(list, b);
4156 list = isl_id_list_add(list, c);
4157 list = isl_id_list_add(list, d);
4158 list = isl_id_list_drop(list, 1, 1);
4160 if (isl_id_list_n_id(list) != 3) {
4161 isl_id_list_free(list);
4162 isl_die(ctx, isl_error_unknown,
4163 "unexpected number of elements in list", return -1);
4166 id = isl_id_list_get_id(list, 0);
4167 ok = id == a;
4168 isl_id_free(id);
4169 id = isl_id_list_get_id(list, 1);
4170 ok = ok && id == c;
4171 isl_id_free(id);
4172 id = isl_id_list_get_id(list, 2);
4173 ok = ok && id == d;
4174 isl_id_free(id);
4176 isl_id_list_free(list);
4178 if (!ok)
4179 isl_die(ctx, isl_error_unknown,
4180 "unexpected elements in list", return -1);
4182 return 0;
4185 const char *set_conversion_tests[] = {
4186 "[N] -> { [i] : N - 1 <= 2 i <= N }",
4187 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
4188 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4189 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4190 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
4193 /* Check that converting from isl_set to isl_pw_multi_aff and back
4194 * to isl_set produces the original isl_set.
4196 static int test_set_conversion(isl_ctx *ctx)
4198 int i;
4199 const char *str;
4200 isl_set *set1, *set2;
4201 isl_pw_multi_aff *pma;
4202 int equal;
4204 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
4205 str = set_conversion_tests[i];
4206 set1 = isl_set_read_from_str(ctx, str);
4207 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
4208 set2 = isl_set_from_pw_multi_aff(pma);
4209 equal = isl_set_is_equal(set1, set2);
4210 isl_set_free(set1);
4211 isl_set_free(set2);
4213 if (equal < 0)
4214 return -1;
4215 if (!equal)
4216 isl_die(ctx, isl_error_unknown, "bad conversion",
4217 return -1);
4220 return 0;
4223 /* Check that converting from isl_map to isl_pw_multi_aff and back
4224 * to isl_map produces the original isl_map.
4226 static int test_map_conversion(isl_ctx *ctx)
4228 const char *str;
4229 isl_map *map1, *map2;
4230 isl_pw_multi_aff *pma;
4231 int equal;
4233 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
4234 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
4235 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
4236 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
4237 "9e <= -2 - 2a) }";
4238 map1 = isl_map_read_from_str(ctx, str);
4239 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
4240 map2 = isl_map_from_pw_multi_aff(pma);
4241 equal = isl_map_is_equal(map1, map2);
4242 isl_map_free(map1);
4243 isl_map_free(map2);
4245 if (equal < 0)
4246 return -1;
4247 if (!equal)
4248 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
4250 return 0;
4253 static int test_conversion(isl_ctx *ctx)
4255 if (test_set_conversion(ctx) < 0)
4256 return -1;
4257 if (test_map_conversion(ctx) < 0)
4258 return -1;
4259 return 0;
4262 /* Check that isl_basic_map_curry does not modify input.
4264 static int test_curry(isl_ctx *ctx)
4266 const char *str;
4267 isl_basic_map *bmap1, *bmap2;
4268 int equal;
4270 str = "{ [A[] -> B[]] -> C[] }";
4271 bmap1 = isl_basic_map_read_from_str(ctx, str);
4272 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
4273 equal = isl_basic_map_is_equal(bmap1, bmap2);
4274 isl_basic_map_free(bmap1);
4275 isl_basic_map_free(bmap2);
4277 if (equal < 0)
4278 return -1;
4279 if (equal)
4280 isl_die(ctx, isl_error_unknown,
4281 "curried map should not be equal to original",
4282 return -1);
4284 return 0;
4287 struct {
4288 const char *set;
4289 const char *ma;
4290 const char *res;
4291 } preimage_tests[] = {
4292 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
4293 "{ A[j,i] -> B[i,j] }",
4294 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
4295 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4296 "{ A[a,b] -> B[a/2,b/6] }",
4297 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
4298 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4299 "{ A[a,b] -> B[a/2,b/6] }",
4300 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
4301 "exists i,j : a = 2 i and b = 6 j }" },
4302 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
4303 "[n] -> { : 0 <= n <= 100 }" },
4304 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4305 "{ A[a] -> B[2a] }",
4306 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
4307 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4308 "{ A[a] -> B[([a/2])] }",
4309 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
4310 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
4311 "{ A[a] -> B[a,a,a/3] }",
4312 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
4313 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
4314 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
4317 static int test_preimage_basic_set(isl_ctx *ctx)
4319 int i;
4320 isl_basic_set *bset1, *bset2;
4321 isl_multi_aff *ma;
4322 int equal;
4324 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
4325 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
4326 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
4327 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
4328 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
4329 equal = isl_basic_set_is_equal(bset1, bset2);
4330 isl_basic_set_free(bset1);
4331 isl_basic_set_free(bset2);
4332 if (equal < 0)
4333 return -1;
4334 if (!equal)
4335 isl_die(ctx, isl_error_unknown, "bad preimage",
4336 return -1);
4339 return 0;
4342 struct {
4343 const char *map;
4344 const char *ma;
4345 const char *res;
4346 } preimage_domain_tests[] = {
4347 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
4348 "{ A[j,i] -> B[i,j] }",
4349 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
4350 { "{ B[i] -> C[i]; D[i] -> E[i] }",
4351 "{ A[i] -> B[i + 1] }",
4352 "{ A[i] -> C[i + 1] }" },
4353 { "{ B[i] -> C[i]; B[i] -> E[i] }",
4354 "{ A[i] -> B[i + 1] }",
4355 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
4356 { "{ B[i] -> C[([i/2])] }",
4357 "{ A[i] -> B[2i] }",
4358 "{ A[i] -> C[i] }" },
4359 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
4360 "{ A[i] -> B[([i/5]), ([i/7])] }",
4361 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
4362 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
4363 "[N] -> { A[] -> B[([N/5])] }",
4364 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
4365 { "{ B[i] -> C[i] : exists a : i = 5 a }",
4366 "{ A[i] -> B[2i] }",
4367 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
4368 { "{ B[i] -> C[i] : exists a : i = 2 a; "
4369 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
4370 "{ A[i] -> B[2i] }",
4371 "{ A[i] -> C[2i] }" },
4374 static int test_preimage_union_map(isl_ctx *ctx)
4376 int i;
4377 isl_union_map *umap1, *umap2;
4378 isl_multi_aff *ma;
4379 int equal;
4381 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
4382 umap1 = isl_union_map_read_from_str(ctx,
4383 preimage_domain_tests[i].map);
4384 ma = isl_multi_aff_read_from_str(ctx,
4385 preimage_domain_tests[i].ma);
4386 umap2 = isl_union_map_read_from_str(ctx,
4387 preimage_domain_tests[i].res);
4388 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
4389 equal = isl_union_map_is_equal(umap1, umap2);
4390 isl_union_map_free(umap1);
4391 isl_union_map_free(umap2);
4392 if (equal < 0)
4393 return -1;
4394 if (!equal)
4395 isl_die(ctx, isl_error_unknown, "bad preimage",
4396 return -1);
4399 return 0;
4402 static int test_preimage(isl_ctx *ctx)
4404 if (test_preimage_basic_set(ctx) < 0)
4405 return -1;
4406 if (test_preimage_union_map(ctx) < 0)
4407 return -1;
4409 return 0;
4412 struct {
4413 const char *ma1;
4414 const char *ma;
4415 const char *res;
4416 } pullback_tests[] = {
4417 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
4418 "{ A[a,b] -> C[b + 2a] }" },
4419 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
4420 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
4421 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
4422 "{ A[a] -> C[(a)/6] }" },
4423 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
4424 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
4425 "{ A[a] -> C[(2a)/3] }" },
4426 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
4427 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
4428 "{ A[i,j] -> C[i + j, i + j] }"},
4429 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
4430 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
4431 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
4434 static int test_pullback(isl_ctx *ctx)
4436 int i;
4437 isl_multi_aff *ma1, *ma2;
4438 isl_multi_aff *ma;
4439 int equal;
4441 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
4442 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
4443 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
4444 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
4445 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
4446 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4447 isl_multi_aff_free(ma1);
4448 isl_multi_aff_free(ma2);
4449 if (equal < 0)
4450 return -1;
4451 if (!equal)
4452 isl_die(ctx, isl_error_unknown, "bad pullback",
4453 return -1);
4456 return 0;
4459 /* Check that negation is printed correctly.
4461 static int test_ast(isl_ctx *ctx)
4463 isl_ast_expr *expr, *expr1, *expr2, *expr3;
4464 char *str;
4465 int ok;
4467 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4468 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4469 expr = isl_ast_expr_add(expr1, expr2);
4470 expr = isl_ast_expr_neg(expr);
4471 str = isl_ast_expr_to_str(expr);
4472 ok = str ? !strcmp(str, "-(A + B)") : -1;
4473 free(str);
4474 isl_ast_expr_free(expr);
4476 if (ok < 0)
4477 return -1;
4478 if (!ok)
4479 isl_die(ctx, isl_error_unknown,
4480 "isl_ast_expr printed incorrectly", return -1);
4482 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4483 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4484 expr = isl_ast_expr_add(expr1, expr2);
4485 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
4486 expr = isl_ast_expr_sub(expr3, expr);
4487 str = isl_ast_expr_to_str(expr);
4488 ok = str ? !strcmp(str, "C - (A + B)") : -1;
4489 free(str);
4490 isl_ast_expr_free(expr);
4492 if (ok < 0)
4493 return -1;
4494 if (!ok)
4495 isl_die(ctx, isl_error_unknown,
4496 "isl_ast_expr printed incorrectly", return -1);
4498 return 0;
4501 /* Check that isl_ast_build_expr_from_set returns a valid expression
4502 * for an empty set. Note that isl_ast_build_expr_from_set getting
4503 * called on an empty set probably indicates a bug in the caller.
4505 static int test_ast_build(isl_ctx *ctx)
4507 isl_set *set;
4508 isl_ast_build *build;
4509 isl_ast_expr *expr;
4511 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4512 build = isl_ast_build_from_context(set);
4514 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
4515 expr = isl_ast_build_expr_from_set(build, set);
4517 isl_ast_expr_free(expr);
4518 isl_ast_build_free(build);
4520 if (!expr)
4521 return -1;
4523 return 0;
4526 /* Internal data structure for before_for and after_for callbacks.
4528 * depth is the current depth
4529 * before is the number of times before_for has been called
4530 * after is the number of times after_for has been called
4532 struct isl_test_codegen_data {
4533 int depth;
4534 int before;
4535 int after;
4538 /* This function is called before each for loop in the AST generated
4539 * from test_ast_gen1.
4541 * Increment the number of calls and the depth.
4542 * Check that the space returned by isl_ast_build_get_schedule_space
4543 * matches the target space of the schedule returned by
4544 * isl_ast_build_get_schedule.
4545 * Return an isl_id that is checked by the corresponding call
4546 * to after_for.
4548 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
4549 void *user)
4551 struct isl_test_codegen_data *data = user;
4552 isl_ctx *ctx;
4553 isl_space *space;
4554 isl_union_map *schedule;
4555 isl_union_set *uset;
4556 isl_set *set;
4557 int empty;
4558 char name[] = "d0";
4560 ctx = isl_ast_build_get_ctx(build);
4562 if (data->before >= 3)
4563 isl_die(ctx, isl_error_unknown,
4564 "unexpected number of for nodes", return NULL);
4565 if (data->depth >= 2)
4566 isl_die(ctx, isl_error_unknown,
4567 "unexpected depth", return NULL);
4569 snprintf(name, sizeof(name), "d%d", data->depth);
4570 data->before++;
4571 data->depth++;
4573 schedule = isl_ast_build_get_schedule(build);
4574 uset = isl_union_map_range(schedule);
4575 if (!uset)
4576 return NULL;
4577 if (isl_union_set_n_set(uset) != 1) {
4578 isl_union_set_free(uset);
4579 isl_die(ctx, isl_error_unknown,
4580 "expecting single range space", return NULL);
4583 space = isl_ast_build_get_schedule_space(build);
4584 set = isl_union_set_extract_set(uset, space);
4585 isl_union_set_free(uset);
4586 empty = isl_set_is_empty(set);
4587 isl_set_free(set);
4589 if (empty < 0)
4590 return NULL;
4591 if (empty)
4592 isl_die(ctx, isl_error_unknown,
4593 "spaces don't match", return NULL);
4595 return isl_id_alloc(ctx, name, NULL);
4598 /* This function is called after each for loop in the AST generated
4599 * from test_ast_gen1.
4601 * Increment the number of calls and decrement the depth.
4602 * Check that the annotation attached to the node matches
4603 * the isl_id returned by the corresponding call to before_for.
4605 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
4606 __isl_keep isl_ast_build *build, void *user)
4608 struct isl_test_codegen_data *data = user;
4609 isl_id *id;
4610 const char *name;
4611 int valid;
4613 data->after++;
4614 data->depth--;
4616 if (data->after > data->before)
4617 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4618 "mismatch in number of for nodes",
4619 return isl_ast_node_free(node));
4621 id = isl_ast_node_get_annotation(node);
4622 if (!id)
4623 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4624 "missing annotation", return isl_ast_node_free(node));
4626 name = isl_id_get_name(id);
4627 valid = name && atoi(name + 1) == data->depth;
4628 isl_id_free(id);
4630 if (!valid)
4631 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4632 "wrong annotation", return isl_ast_node_free(node));
4634 return node;
4637 /* Check that the before_each_for and after_each_for callbacks
4638 * are called for each for loop in the generated code,
4639 * that they are called in the right order and that the isl_id
4640 * returned from the before_each_for callback is attached to
4641 * the isl_ast_node passed to the corresponding after_each_for call.
4643 static int test_ast_gen1(isl_ctx *ctx)
4645 const char *str;
4646 isl_set *set;
4647 isl_union_map *schedule;
4648 isl_ast_build *build;
4649 isl_ast_node *tree;
4650 struct isl_test_codegen_data data;
4652 str = "[N] -> { : N >= 10 }";
4653 set = isl_set_read_from_str(ctx, str);
4654 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
4655 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
4656 schedule = isl_union_map_read_from_str(ctx, str);
4658 data.before = 0;
4659 data.after = 0;
4660 data.depth = 0;
4661 build = isl_ast_build_from_context(set);
4662 build = isl_ast_build_set_before_each_for(build,
4663 &before_for, &data);
4664 build = isl_ast_build_set_after_each_for(build,
4665 &after_for, &data);
4666 tree = isl_ast_build_ast_from_schedule(build, schedule);
4667 isl_ast_build_free(build);
4668 if (!tree)
4669 return -1;
4671 isl_ast_node_free(tree);
4673 if (data.before != 3 || data.after != 3)
4674 isl_die(ctx, isl_error_unknown,
4675 "unexpected number of for nodes", return -1);
4677 return 0;
4680 /* Check that the AST generator handles domains that are integrally disjoint
4681 * but not ratinoally disjoint.
4683 static int test_ast_gen2(isl_ctx *ctx)
4685 const char *str;
4686 isl_set *set;
4687 isl_union_map *schedule;
4688 isl_union_map *options;
4689 isl_ast_build *build;
4690 isl_ast_node *tree;
4692 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
4693 schedule = isl_union_map_read_from_str(ctx, str);
4694 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4695 build = isl_ast_build_from_context(set);
4697 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
4698 options = isl_union_map_read_from_str(ctx, str);
4699 build = isl_ast_build_set_options(build, options);
4700 tree = isl_ast_build_ast_from_schedule(build, schedule);
4701 isl_ast_build_free(build);
4702 if (!tree)
4703 return -1;
4704 isl_ast_node_free(tree);
4706 return 0;
4709 /* Increment *user on each call.
4711 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
4712 __isl_keep isl_ast_build *build, void *user)
4714 int *n = user;
4716 (*n)++;
4718 return node;
4721 /* Test that unrolling tries to minimize the number of instances.
4722 * In particular, for the schedule given below, make sure it generates
4723 * 3 nodes (rather than 101).
4725 static int test_ast_gen3(isl_ctx *ctx)
4727 const char *str;
4728 isl_set *set;
4729 isl_union_map *schedule;
4730 isl_union_map *options;
4731 isl_ast_build *build;
4732 isl_ast_node *tree;
4733 int n_domain = 0;
4735 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
4736 schedule = isl_union_map_read_from_str(ctx, str);
4737 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4739 str = "{ [i] -> unroll[0] }";
4740 options = isl_union_map_read_from_str(ctx, str);
4742 build = isl_ast_build_from_context(set);
4743 build = isl_ast_build_set_options(build, options);
4744 build = isl_ast_build_set_at_each_domain(build,
4745 &count_domains, &n_domain);
4746 tree = isl_ast_build_ast_from_schedule(build, schedule);
4747 isl_ast_build_free(build);
4748 if (!tree)
4749 return -1;
4751 isl_ast_node_free(tree);
4753 if (n_domain != 3)
4754 isl_die(ctx, isl_error_unknown,
4755 "unexpected number of for nodes", return -1);
4757 return 0;
4760 /* Check that if the ast_build_exploit_nested_bounds options is set,
4761 * we do not get an outer if node in the generated AST,
4762 * while we do get such an outer if node if the options is not set.
4764 static int test_ast_gen4(isl_ctx *ctx)
4766 const char *str;
4767 isl_set *set;
4768 isl_union_map *schedule;
4769 isl_ast_build *build;
4770 isl_ast_node *tree;
4771 enum isl_ast_node_type type;
4772 int enb;
4774 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4775 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4777 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4779 schedule = isl_union_map_read_from_str(ctx, str);
4780 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4781 build = isl_ast_build_from_context(set);
4782 tree = isl_ast_build_ast_from_schedule(build, schedule);
4783 isl_ast_build_free(build);
4784 if (!tree)
4785 return -1;
4787 type = isl_ast_node_get_type(tree);
4788 isl_ast_node_free(tree);
4790 if (type == isl_ast_node_if)
4791 isl_die(ctx, isl_error_unknown,
4792 "not expecting if node", return -1);
4794 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
4796 schedule = isl_union_map_read_from_str(ctx, str);
4797 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4798 build = isl_ast_build_from_context(set);
4799 tree = isl_ast_build_ast_from_schedule(build, schedule);
4800 isl_ast_build_free(build);
4801 if (!tree)
4802 return -1;
4804 type = isl_ast_node_get_type(tree);
4805 isl_ast_node_free(tree);
4807 if (type != isl_ast_node_if)
4808 isl_die(ctx, isl_error_unknown,
4809 "expecting if node", return -1);
4811 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
4813 return 0;
4816 /* This function is called for each leaf in the AST generated
4817 * from test_ast_gen5.
4819 * We finalize the AST generation by extending the outer schedule
4820 * with a zero-dimensional schedule. If this results in any for loops,
4821 * then this means that we did not pass along enough information
4822 * about the outer schedule to the inner AST generation.
4824 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
4825 void *user)
4827 isl_union_map *schedule, *extra;
4828 isl_ast_node *tree;
4830 schedule = isl_ast_build_get_schedule(build);
4831 extra = isl_union_map_copy(schedule);
4832 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
4833 schedule = isl_union_map_range_product(schedule, extra);
4834 tree = isl_ast_build_ast_from_schedule(build, schedule);
4835 isl_ast_build_free(build);
4837 if (!tree)
4838 return NULL;
4840 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
4841 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
4842 "code should not contain any for loop",
4843 return isl_ast_node_free(tree));
4845 return tree;
4848 /* Check that we do not lose any information when going back and
4849 * forth between internal and external schedule.
4851 * In particular, we create an AST where we unroll the only
4852 * non-constant dimension in the schedule. We therefore do
4853 * not expect any for loops in the AST. However, older versions
4854 * of isl would not pass along enough information about the outer
4855 * schedule when performing an inner code generation from a create_leaf
4856 * callback, resulting in the inner code generation producing a for loop.
4858 static int test_ast_gen5(isl_ctx *ctx)
4860 const char *str;
4861 isl_set *set;
4862 isl_union_map *schedule, *options;
4863 isl_ast_build *build;
4864 isl_ast_node *tree;
4866 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
4867 schedule = isl_union_map_read_from_str(ctx, str);
4869 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
4870 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
4871 options = isl_union_map_read_from_str(ctx, str);
4873 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4874 build = isl_ast_build_from_context(set);
4875 build = isl_ast_build_set_options(build, options);
4876 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
4877 tree = isl_ast_build_ast_from_schedule(build, schedule);
4878 isl_ast_build_free(build);
4879 isl_ast_node_free(tree);
4880 if (!tree)
4881 return -1;
4883 return 0;
4886 static int test_ast_gen(isl_ctx *ctx)
4888 if (test_ast_gen1(ctx) < 0)
4889 return -1;
4890 if (test_ast_gen2(ctx) < 0)
4891 return -1;
4892 if (test_ast_gen3(ctx) < 0)
4893 return -1;
4894 if (test_ast_gen4(ctx) < 0)
4895 return -1;
4896 if (test_ast_gen5(ctx) < 0)
4897 return -1;
4898 return 0;
4901 /* Check if dropping output dimensions from an isl_pw_multi_aff
4902 * works properly.
4904 static int test_pw_multi_aff(isl_ctx *ctx)
4906 const char *str;
4907 isl_pw_multi_aff *pma1, *pma2;
4908 int equal;
4910 str = "{ [i,j] -> [i+j, 4i-j] }";
4911 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4912 str = "{ [i,j] -> [4i-j] }";
4913 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4915 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
4917 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
4919 isl_pw_multi_aff_free(pma1);
4920 isl_pw_multi_aff_free(pma2);
4921 if (equal < 0)
4922 return -1;
4923 if (!equal)
4924 isl_die(ctx, isl_error_unknown,
4925 "expressions not equal", return -1);
4927 return 0;
4930 /* Check that we can properly parse multi piecewise affine expressions
4931 * where the piecewise affine expressions have different domains.
4933 static int test_multi_pw_aff(isl_ctx *ctx)
4935 const char *str;
4936 isl_set *dom, *dom2;
4937 isl_multi_pw_aff *mpa1, *mpa2;
4938 isl_pw_aff *pa;
4939 int equal;
4940 int equal_domain;
4942 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
4943 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
4944 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
4945 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
4946 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
4947 str = "{ [i] -> [(i : i > 0), 2i] }";
4948 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
4950 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
4952 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
4953 dom = isl_pw_aff_domain(pa);
4954 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
4955 dom2 = isl_pw_aff_domain(pa);
4956 equal_domain = isl_set_is_equal(dom, dom2);
4958 isl_set_free(dom);
4959 isl_set_free(dom2);
4960 isl_multi_pw_aff_free(mpa1);
4961 isl_multi_pw_aff_free(mpa2);
4963 if (equal < 0)
4964 return -1;
4965 if (!equal)
4966 isl_die(ctx, isl_error_unknown,
4967 "expressions not equal", return -1);
4969 if (equal_domain < 0)
4970 return -1;
4971 if (equal_domain)
4972 isl_die(ctx, isl_error_unknown,
4973 "domains unexpectedly equal", return -1);
4975 return 0;
4978 /* This is a regression test for a bug where isl_basic_map_simplify
4979 * would end up in an infinite loop. In particular, we construct
4980 * an empty basic set that is not obviously empty.
4981 * isl_basic_set_is_empty marks the basic set as empty.
4982 * After projecting out i3, the variable can be dropped completely,
4983 * but isl_basic_map_simplify refrains from doing so if the basic set
4984 * is empty and would end up in an infinite loop if it didn't test
4985 * explicitly for empty basic maps in the outer loop.
4987 static int test_simplify(isl_ctx *ctx)
4989 const char *str;
4990 isl_basic_set *bset;
4991 int empty;
4993 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
4994 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
4995 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
4996 "i3 >= i2 }";
4997 bset = isl_basic_set_read_from_str(ctx, str);
4998 empty = isl_basic_set_is_empty(bset);
4999 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
5000 isl_basic_set_free(bset);
5001 if (!bset)
5002 return -1;
5003 if (!empty)
5004 isl_die(ctx, isl_error_unknown,
5005 "basic set should be empty", return -1);
5007 return 0;
5010 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
5011 * with gbr context would fail to disable the use of the shifted tableau
5012 * when transferring equalities for the input to the context, resulting
5013 * in invalid sample values.
5015 static int test_partial_lexmin(isl_ctx *ctx)
5017 const char *str;
5018 isl_basic_set *bset;
5019 isl_basic_map *bmap;
5020 isl_map *map;
5022 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
5023 bmap = isl_basic_map_read_from_str(ctx, str);
5024 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
5025 bset = isl_basic_set_read_from_str(ctx, str);
5026 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
5027 isl_map_free(map);
5029 if (!map)
5030 return -1;
5032 return 0;
5035 /* Check that the variable compression performed on the existentially
5036 * quantified variables inside isl_basic_set_compute_divs is not confused
5037 * by the implicit equalities among the parameters.
5039 static int test_compute_divs(isl_ctx *ctx)
5041 const char *str;
5042 isl_basic_set *bset;
5043 isl_set *set;
5045 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
5046 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
5047 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
5048 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
5049 bset = isl_basic_set_read_from_str(ctx, str);
5050 set = isl_basic_set_compute_divs(bset);
5051 isl_set_free(set);
5052 if (!set)
5053 return -1;
5055 return 0;
5058 struct {
5059 const char *set;
5060 const char *dual;
5061 } coef_tests[] = {
5062 { "{ rat: [i] : 0 <= i <= 10 }",
5063 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
5064 { "{ rat: [i] : FALSE }",
5065 "{ rat: coefficients[[cst] -> [a]] }" },
5066 { "{ rat: [i] : }",
5067 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
5070 struct {
5071 const char *set;
5072 const char *dual;
5073 } sol_tests[] = {
5074 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
5075 "{ rat: [i] : 0 <= i <= 10 }" },
5076 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
5077 "{ rat: [i] }" },
5078 { "{ rat: coefficients[[cst] -> [a]] }",
5079 "{ rat: [i] : FALSE }" },
5082 /* Test the basic functionality of isl_basic_set_coefficients and
5083 * isl_basic_set_solutions.
5085 static int test_dual(isl_ctx *ctx)
5087 int i;
5089 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
5090 int equal;
5091 isl_basic_set *bset1, *bset2;
5093 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
5094 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
5095 bset1 = isl_basic_set_coefficients(bset1);
5096 equal = isl_basic_set_is_equal(bset1, bset2);
5097 isl_basic_set_free(bset1);
5098 isl_basic_set_free(bset2);
5099 if (equal < 0)
5100 return -1;
5101 if (!equal)
5102 isl_die(ctx, isl_error_unknown,
5103 "incorrect dual", return -1);
5106 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
5107 int equal;
5108 isl_basic_set *bset1, *bset2;
5110 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
5111 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
5112 bset1 = isl_basic_set_solutions(bset1);
5113 equal = isl_basic_set_is_equal(bset1, bset2);
5114 isl_basic_set_free(bset1);
5115 isl_basic_set_free(bset2);
5116 if (equal < 0)
5117 return -1;
5118 if (!equal)
5119 isl_die(ctx, isl_error_unknown,
5120 "incorrect dual", return -1);
5123 return 0;
5126 struct {
5127 const char *name;
5128 int (*fn)(isl_ctx *ctx);
5129 } tests [] = {
5130 { "dual", &test_dual },
5131 { "dependence analysis", &test_flow },
5132 { "val", &test_val },
5133 { "compute divs", &test_compute_divs },
5134 { "partial lexmin", &test_partial_lexmin },
5135 { "simplify", &test_simplify },
5136 { "curry", &test_curry },
5137 { "piecewise multi affine expressions", &test_pw_multi_aff },
5138 { "multi piecewise affine expressions", &test_multi_pw_aff },
5139 { "conversion", &test_conversion },
5140 { "list", &test_list },
5141 { "align parameters", &test_align_parameters },
5142 { "preimage", &test_preimage },
5143 { "pullback", &test_pullback },
5144 { "AST", &test_ast },
5145 { "AST build", &test_ast_build },
5146 { "AST generation", &test_ast_gen },
5147 { "eliminate", &test_eliminate },
5148 { "residue class", &test_residue_class },
5149 { "div", &test_div },
5150 { "slice", &test_slice },
5151 { "fixed power", &test_fixed_power },
5152 { "sample", &test_sample },
5153 { "output", &test_output },
5154 { "vertices", &test_vertices },
5155 { "fixed", &test_fixed },
5156 { "equal", &test_equal },
5157 { "disjoint", &test_disjoint },
5158 { "product", &test_product },
5159 { "dim_max", &test_dim_max },
5160 { "affine", &test_aff },
5161 { "injective", &test_injective },
5162 { "schedule", &test_schedule },
5163 { "union_pw", &test_union_pw },
5164 { "parse", &test_parse },
5165 { "single-valued", &test_sv },
5166 { "affine hull", &test_affine_hull },
5167 { "coalesce", &test_coalesce },
5168 { "factorize", &test_factorize },
5169 { "subset", &test_subset },
5170 { "subtract", &test_subtract },
5171 { "lexmin", &test_lexmin },
5172 { "min", &test_min },
5173 { "gist", &test_gist },
5174 { "piecewise quasi-polynomials", &test_pwqp },
5177 int main(int argc, char **argv)
5179 int i;
5180 struct isl_ctx *ctx;
5181 struct isl_options *options;
5183 srcdir = getenv("srcdir");
5184 assert(srcdir);
5186 options = isl_options_new_with_defaults();
5187 assert(options);
5188 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
5190 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
5191 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
5192 printf("%s\n", tests[i].name);
5193 if (tests[i].fn(ctx) < 0)
5194 goto error;
5196 test_lift(ctx);
5197 test_bound(ctx);
5198 test_union(ctx);
5199 test_split_periods(ctx);
5200 test_lex(ctx);
5201 test_bijective(ctx);
5202 test_dep(ctx);
5203 test_read(ctx);
5204 test_bounded(ctx);
5205 test_construction(ctx);
5206 test_dim(ctx);
5207 test_application(ctx);
5208 test_convex_hull(ctx);
5209 test_closure(ctx);
5210 isl_ctx_free(ctx);
5211 return 0;
5212 error:
5213 isl_ctx_free(ctx);
5214 return -1;