add isl_map_unshifted_simple_hull_from_map_list
[isl.git] / isl_test.c
blob5e9197848bac17913d6909b0fc5209bfacf92915
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 }" },
1172 static int test_gist(struct isl_ctx *ctx)
1174 int i;
1175 const char *str;
1176 isl_basic_set *bset1, *bset2;
1177 isl_map *map1, *map2;
1178 int equal;
1180 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1181 int equal_input;
1182 isl_basic_set *copy;
1184 bset1 = isl_basic_set_read_from_str(ctx, gist_tests[i].set);
1185 bset2 = isl_basic_set_read_from_str(ctx, gist_tests[i].context);
1186 copy = isl_basic_set_copy(bset1);
1187 bset1 = isl_basic_set_gist(bset1, bset2);
1188 bset2 = isl_basic_set_read_from_str(ctx, gist_tests[i].gist);
1189 equal = isl_basic_set_is_equal(bset1, bset2);
1190 isl_basic_set_free(bset1);
1191 isl_basic_set_free(bset2);
1192 bset1 = isl_basic_set_read_from_str(ctx, gist_tests[i].set);
1193 equal_input = isl_basic_set_is_equal(bset1, copy);
1194 isl_basic_set_free(bset1);
1195 isl_basic_set_free(copy);
1196 if (equal < 0 || equal_input < 0)
1197 return -1;
1198 if (!equal)
1199 isl_die(ctx, isl_error_unknown,
1200 "incorrect gist result", return -1);
1201 if (!equal_input)
1202 isl_die(ctx, isl_error_unknown,
1203 "gist modified input", return -1);
1206 test_gist_case(ctx, "gist1");
1208 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1209 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1210 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1211 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1212 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1213 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1214 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1215 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1216 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1217 bset1 = isl_basic_set_read_from_str(ctx, str);
1218 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1219 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1220 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1221 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1222 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1223 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1224 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1225 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1226 bset2 = isl_basic_set_read_from_str(ctx, str);
1227 bset1 = isl_basic_set_gist(bset1, bset2);
1228 assert(bset1 && bset1->n_div == 0);
1229 isl_basic_set_free(bset1);
1231 /* Check that the integer divisions of the second disjunct
1232 * do not spread to the first disjunct.
1234 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1235 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1236 "(exists (e0 = [(-1 + t1)/16], "
1237 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1238 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1239 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1240 "o0 <= 4294967295 and t1 <= -1)) }";
1241 map1 = isl_map_read_from_str(ctx, str);
1242 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1243 map2 = isl_map_read_from_str(ctx, str);
1244 map1 = isl_map_gist(map1, map2);
1245 if (!map1)
1246 return -1;
1247 if (map1->n != 1)
1248 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1249 isl_map_free(map1); return -1);
1250 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1251 isl_die(ctx, isl_error_unknown, "expecting single div",
1252 isl_map_free(map1); return -1);
1253 isl_map_free(map1);
1255 return 0;
1258 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1260 isl_set *set, *set2;
1261 int equal;
1262 int one;
1264 set = isl_set_read_from_str(ctx, str);
1265 set = isl_set_coalesce(set);
1266 set2 = isl_set_read_from_str(ctx, str);
1267 equal = isl_set_is_equal(set, set2);
1268 one = set && set->n == 1;
1269 isl_set_free(set);
1270 isl_set_free(set2);
1272 if (equal < 0)
1273 return -1;
1274 if (!equal)
1275 isl_die(ctx, isl_error_unknown,
1276 "coalesced set not equal to input", return -1);
1277 if (check_one && !one)
1278 isl_die(ctx, isl_error_unknown,
1279 "coalesced set should not be a union", return -1);
1281 return 0;
1284 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1286 int r = 0;
1287 int bounded;
1289 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1290 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1292 if (test_coalesce_set(ctx,
1293 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1294 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1295 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1296 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1297 "-10 <= y <= 0}", 1) < 0)
1298 goto error;
1299 if (test_coalesce_set(ctx,
1300 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
1301 goto error;
1302 if (test_coalesce_set(ctx,
1303 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
1304 goto error;
1306 if (0) {
1307 error:
1308 r = -1;
1311 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1313 return r;
1316 /* Inputs for coalescing tests.
1317 * "str" is a string representation of the input set.
1318 * "single_disjunct" is set if we expect the result to consist of
1319 * a single disjunct.
1321 struct {
1322 int single_disjunct;
1323 const char *str;
1324 } coalesce_tests[] = {
1325 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1326 "y >= x & x >= 2 & 5 >= y }" },
1327 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1328 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1329 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1330 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1331 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1332 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1333 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1334 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1335 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1336 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1337 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1338 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1339 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1340 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1341 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1342 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1343 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1344 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1345 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1346 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1347 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1348 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1349 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1350 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1351 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1352 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1353 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1354 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1355 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1356 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1357 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1358 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1359 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1360 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1361 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1362 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1363 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1364 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1365 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1366 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1367 "[o0, o1, o2, o3, o4, o5, o6]] : "
1368 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1369 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1370 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1371 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1372 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1373 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1374 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1375 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1376 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1377 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1378 "o6 >= i3 + i6 - o3 and M >= 0 and "
1379 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1380 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1381 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1382 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1383 "(o0 = 0 and M >= 2 and N >= 3) or "
1384 "(M = 0 and o0 = 0 and N >= 3) }" },
1385 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1386 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1387 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1388 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1389 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1390 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1391 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1392 "(y = 3 and x = 1) }" },
1393 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1394 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1395 "i1 <= M and i3 <= M and i4 <= M) or "
1396 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1397 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1398 "i4 <= -1 + M) }" },
1399 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1400 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1401 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1402 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1403 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1404 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1405 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1406 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1407 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1408 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1409 { 0, "{ [a, b] : exists e : 2e = a and "
1410 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1411 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1412 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1413 "j >= 1 and j' <= i + j - i' and i >= 1; "
1414 "[1, 1, 1, 1] }" },
1415 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1416 "[i,j] : exists a : j = 3a }" },
1417 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1418 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1419 "a >= 3) or "
1420 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1421 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1422 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1423 "c <= 6 + 8a and a >= 3; "
1424 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1425 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1426 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1427 "[x,0] : 3 <= x <= 4 }" },
1428 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1429 "[x,0] : 4 <= x <= 5 }" },
1430 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1431 "[x,0] : 3 <= x <= 5 }" },
1432 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1433 "[x,0] : 3 <= x <= 4 }" },
1434 { 1 , "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1435 "i1 <= 0; "
1436 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1439 /* Test the functionality of isl_set_coalesce.
1440 * That is, check that the output is always equal to the input
1441 * and in some cases that the result consists of a single disjunct.
1443 static int test_coalesce(struct isl_ctx *ctx)
1445 int i;
1447 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1448 const char *str = coalesce_tests[i].str;
1449 int check_one = coalesce_tests[i].single_disjunct;
1450 if (test_coalesce_set(ctx, str, check_one) < 0)
1451 return -1;
1454 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1455 return -1;
1457 return 0;
1460 void test_closure(struct isl_ctx *ctx)
1462 const char *str;
1463 isl_set *dom;
1464 isl_map *up, *right;
1465 isl_map *map, *map2;
1466 int exact;
1468 /* COCOA example 1 */
1469 map = isl_map_read_from_str(ctx,
1470 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1471 "1 <= i and i < n and 1 <= j and j < n or "
1472 "i2 = i + 1 and j2 = j - 1 and "
1473 "1 <= i and i < n and 2 <= j and j <= n }");
1474 map = isl_map_power(map, &exact);
1475 assert(exact);
1476 isl_map_free(map);
1478 /* COCOA example 1 */
1479 map = isl_map_read_from_str(ctx,
1480 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1481 "1 <= i and i < n and 1 <= j and j < n or "
1482 "i2 = i + 1 and j2 = j - 1 and "
1483 "1 <= i and i < n and 2 <= j and j <= n }");
1484 map = isl_map_transitive_closure(map, &exact);
1485 assert(exact);
1486 map2 = isl_map_read_from_str(ctx,
1487 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1488 "1 <= i and i < n and 1 <= j and j <= n and "
1489 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1490 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1491 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1492 assert(isl_map_is_equal(map, map2));
1493 isl_map_free(map2);
1494 isl_map_free(map);
1496 map = isl_map_read_from_str(ctx,
1497 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1498 " 0 <= y and y <= n }");
1499 map = isl_map_transitive_closure(map, &exact);
1500 map2 = isl_map_read_from_str(ctx,
1501 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1502 " 0 <= y and y <= n }");
1503 assert(isl_map_is_equal(map, map2));
1504 isl_map_free(map2);
1505 isl_map_free(map);
1507 /* COCOA example 2 */
1508 map = isl_map_read_from_str(ctx,
1509 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1510 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1511 "i2 = i + 2 and j2 = j - 2 and "
1512 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1513 map = isl_map_transitive_closure(map, &exact);
1514 assert(exact);
1515 map2 = isl_map_read_from_str(ctx,
1516 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1517 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1518 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1519 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1520 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1521 assert(isl_map_is_equal(map, map2));
1522 isl_map_free(map);
1523 isl_map_free(map2);
1525 /* COCOA Fig.2 left */
1526 map = isl_map_read_from_str(ctx,
1527 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1528 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1529 "j <= n or "
1530 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1531 "j <= 2 i - 3 and j <= n - 2 or "
1532 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1533 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1534 map = isl_map_transitive_closure(map, &exact);
1535 assert(exact);
1536 isl_map_free(map);
1538 /* COCOA Fig.2 right */
1539 map = isl_map_read_from_str(ctx,
1540 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1541 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1542 "j <= n or "
1543 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1544 "j <= 2 i - 4 and j <= n - 3 or "
1545 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1546 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1547 map = isl_map_power(map, &exact);
1548 assert(exact);
1549 isl_map_free(map);
1551 /* COCOA Fig.2 right */
1552 map = isl_map_read_from_str(ctx,
1553 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1554 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1555 "j <= n or "
1556 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1557 "j <= 2 i - 4 and j <= n - 3 or "
1558 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1559 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1560 map = isl_map_transitive_closure(map, &exact);
1561 assert(exact);
1562 map2 = isl_map_read_from_str(ctx,
1563 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1564 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1565 "j <= n and 3 + i + 2 j <= 3 n and "
1566 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1567 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1568 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1569 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1570 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1571 assert(isl_map_is_equal(map, map2));
1572 isl_map_free(map2);
1573 isl_map_free(map);
1575 /* COCOA Fig.1 right */
1576 dom = isl_set_read_from_str(ctx,
1577 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1578 "2 x - 3 y + 3 >= 0 }");
1579 right = isl_map_read_from_str(ctx,
1580 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1581 up = isl_map_read_from_str(ctx,
1582 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1583 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1584 right = isl_map_intersect_range(right, isl_set_copy(dom));
1585 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1586 up = isl_map_intersect_range(up, dom);
1587 map = isl_map_union(up, right);
1588 map = isl_map_transitive_closure(map, &exact);
1589 assert(exact);
1590 map2 = isl_map_read_from_str(ctx,
1591 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1592 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1593 assert(isl_map_is_equal(map, map2));
1594 isl_map_free(map2);
1595 isl_map_free(map);
1597 /* COCOA Theorem 1 counter example */
1598 map = isl_map_read_from_str(ctx,
1599 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1600 "i2 = 1 and j2 = j or "
1601 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1602 map = isl_map_transitive_closure(map, &exact);
1603 assert(exact);
1604 isl_map_free(map);
1606 map = isl_map_read_from_str(ctx,
1607 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1608 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1609 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1610 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1611 map = isl_map_transitive_closure(map, &exact);
1612 assert(exact);
1613 isl_map_free(map);
1615 /* Kelly et al 1996, fig 12 */
1616 map = isl_map_read_from_str(ctx,
1617 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1618 "1 <= i,j,j+1 <= n or "
1619 "j = n and j2 = 1 and i2 = i + 1 and "
1620 "1 <= i,i+1 <= n }");
1621 map = isl_map_transitive_closure(map, &exact);
1622 assert(exact);
1623 map2 = isl_map_read_from_str(ctx,
1624 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1625 "1 <= i <= n and i = i2 or "
1626 "1 <= i < i2 <= n and 1 <= j <= n and "
1627 "1 <= j2 <= n }");
1628 assert(isl_map_is_equal(map, map2));
1629 isl_map_free(map2);
1630 isl_map_free(map);
1632 /* Omega's closure4 */
1633 map = isl_map_read_from_str(ctx,
1634 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1635 "1 <= x,y <= 10 or "
1636 "x2 = x + 1 and y2 = y and "
1637 "1 <= x <= 20 && 5 <= y <= 15 }");
1638 map = isl_map_transitive_closure(map, &exact);
1639 assert(exact);
1640 isl_map_free(map);
1642 map = isl_map_read_from_str(ctx,
1643 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1644 map = isl_map_transitive_closure(map, &exact);
1645 assert(!exact);
1646 map2 = isl_map_read_from_str(ctx,
1647 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1648 assert(isl_map_is_equal(map, map2));
1649 isl_map_free(map);
1650 isl_map_free(map2);
1652 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1653 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1654 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1655 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1656 map = isl_map_read_from_str(ctx, str);
1657 map = isl_map_transitive_closure(map, &exact);
1658 assert(exact);
1659 map2 = isl_map_read_from_str(ctx, str);
1660 assert(isl_map_is_equal(map, map2));
1661 isl_map_free(map);
1662 isl_map_free(map2);
1664 str = "{[0] -> [1]; [2] -> [3]}";
1665 map = isl_map_read_from_str(ctx, str);
1666 map = isl_map_transitive_closure(map, &exact);
1667 assert(exact);
1668 map2 = isl_map_read_from_str(ctx, str);
1669 assert(isl_map_is_equal(map, map2));
1670 isl_map_free(map);
1671 isl_map_free(map2);
1673 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1674 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1675 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1676 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1677 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1678 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1679 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1680 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1681 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1682 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1683 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1684 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1685 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1686 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1687 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1688 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1689 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1690 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1691 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1692 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1693 map = isl_map_read_from_str(ctx, str);
1694 map = isl_map_transitive_closure(map, NULL);
1695 assert(map);
1696 isl_map_free(map);
1699 void test_lex(struct isl_ctx *ctx)
1701 isl_space *dim;
1702 isl_map *map;
1704 dim = isl_space_set_alloc(ctx, 0, 0);
1705 map = isl_map_lex_le(dim);
1706 assert(!isl_map_is_empty(map));
1707 isl_map_free(map);
1710 static int test_lexmin(struct isl_ctx *ctx)
1712 int equal;
1713 const char *str;
1714 isl_basic_map *bmap;
1715 isl_map *map, *map2;
1716 isl_set *set;
1717 isl_set *set2;
1718 isl_pw_multi_aff *pma;
1720 str = "[p0, p1] -> { [] -> [] : "
1721 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1722 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1723 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1724 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1725 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1726 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1727 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1728 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1729 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1730 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1731 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1732 map = isl_map_read_from_str(ctx, str);
1733 map = isl_map_lexmin(map);
1734 isl_map_free(map);
1736 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1737 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1738 set = isl_set_read_from_str(ctx, str);
1739 set = isl_set_lexmax(set);
1740 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1741 set2 = isl_set_read_from_str(ctx, str);
1742 set = isl_set_intersect(set, set2);
1743 assert(!isl_set_is_empty(set));
1744 isl_set_free(set);
1746 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1747 map = isl_map_read_from_str(ctx, str);
1748 map = isl_map_lexmin(map);
1749 str = "{ [x] -> [5] : 6 <= x <= 8; "
1750 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1751 map2 = isl_map_read_from_str(ctx, str);
1752 assert(isl_map_is_equal(map, map2));
1753 isl_map_free(map);
1754 isl_map_free(map2);
1756 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1757 map = isl_map_read_from_str(ctx, str);
1758 map2 = isl_map_copy(map);
1759 map = isl_map_lexmin(map);
1760 assert(isl_map_is_equal(map, map2));
1761 isl_map_free(map);
1762 isl_map_free(map2);
1764 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1765 map = isl_map_read_from_str(ctx, str);
1766 map = isl_map_lexmin(map);
1767 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1768 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1769 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1770 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
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 = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1777 " 8i' <= i and 8i' >= -7 + i }";
1778 bmap = isl_basic_map_read_from_str(ctx, str);
1779 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1780 map2 = isl_map_from_pw_multi_aff(pma);
1781 map = isl_map_from_basic_map(bmap);
1782 assert(isl_map_is_equal(map, map2));
1783 isl_map_free(map);
1784 isl_map_free(map2);
1786 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1787 map = isl_map_read_from_str(ctx, str);
1788 map = isl_map_lexmin(map);
1789 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1790 map2 = isl_map_read_from_str(ctx, str);
1791 assert(isl_map_is_equal(map, map2));
1792 isl_map_free(map);
1793 isl_map_free(map2);
1795 /* Check that empty pieces are properly combined. */
1796 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1797 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1798 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1799 map = isl_map_read_from_str(ctx, str);
1800 map = isl_map_lexmin(map);
1801 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1802 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1803 "x >= 4 }";
1804 map2 = isl_map_read_from_str(ctx, str);
1805 assert(isl_map_is_equal(map, map2));
1806 isl_map_free(map);
1807 isl_map_free(map2);
1809 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1810 " 8i' <= i and 8i' >= -7 + i }";
1811 set = isl_set_read_from_str(ctx, str);
1812 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1813 set2 = isl_set_from_pw_multi_aff(pma);
1814 equal = isl_set_is_equal(set, set2);
1815 isl_set_free(set);
1816 isl_set_free(set2);
1817 if (equal < 0)
1818 return -1;
1819 if (!equal)
1820 isl_die(ctx, isl_error_unknown,
1821 "unexpected difference between set and "
1822 "piecewise affine expression", return -1);
1824 return 0;
1827 /* Check that isl_set_min_val and isl_set_max_val compute the correct
1828 * result on non-convex inputs.
1830 static int test_min(struct isl_ctx *ctx)
1832 isl_set *set;
1833 isl_aff *aff;
1834 isl_val *val;
1835 int min_ok, max_ok;
1837 set = isl_set_read_from_str(ctx, "{ [-1]; [1] }");
1838 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x] }");
1839 val = isl_set_min_val(set, aff);
1840 min_ok = isl_val_is_negone(val);
1841 isl_val_free(val);
1842 val = isl_set_max_val(set, aff);
1843 max_ok = isl_val_is_one(val);
1844 isl_val_free(val);
1845 isl_aff_free(aff);
1846 isl_set_free(set);
1848 if (min_ok < 0 || max_ok < 0)
1849 return -1;
1850 if (!min_ok)
1851 isl_die(ctx, isl_error_unknown,
1852 "unexpected minimum", return -1);
1853 if (!max_ok)
1854 isl_die(ctx, isl_error_unknown,
1855 "unexpected maximum", return -1);
1857 return 0;
1860 struct must_may {
1861 isl_map *must;
1862 isl_map *may;
1865 static int collect_must_may(__isl_take isl_map *dep, int must,
1866 void *dep_user, void *user)
1868 struct must_may *mm = (struct must_may *)user;
1870 if (must)
1871 mm->must = isl_map_union(mm->must, dep);
1872 else
1873 mm->may = isl_map_union(mm->may, dep);
1875 return 0;
1878 static int common_space(void *first, void *second)
1880 int depth = *(int *)first;
1881 return 2 * depth;
1884 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1886 isl_map *map2;
1887 int equal;
1889 if (!map)
1890 return -1;
1892 map2 = isl_map_read_from_str(map->ctx, str);
1893 equal = isl_map_is_equal(map, map2);
1894 isl_map_free(map2);
1896 return equal;
1899 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1901 int equal;
1903 equal = map_is_equal(map, str);
1904 if (equal < 0)
1905 return -1;
1906 if (!equal)
1907 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1908 "result not as expected", return -1);
1909 return 0;
1912 void test_dep(struct isl_ctx *ctx)
1914 const char *str;
1915 isl_space *dim;
1916 isl_map *map;
1917 isl_access_info *ai;
1918 isl_flow *flow;
1919 int depth;
1920 struct must_may mm;
1922 depth = 3;
1924 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1925 map = isl_map_read_from_str(ctx, str);
1926 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1928 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1929 map = isl_map_read_from_str(ctx, str);
1930 ai = isl_access_info_add_source(ai, map, 1, &depth);
1932 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1933 map = isl_map_read_from_str(ctx, str);
1934 ai = isl_access_info_add_source(ai, map, 1, &depth);
1936 flow = isl_access_info_compute_flow(ai);
1937 dim = isl_space_alloc(ctx, 0, 3, 3);
1938 mm.must = isl_map_empty(isl_space_copy(dim));
1939 mm.may = isl_map_empty(dim);
1941 isl_flow_foreach(flow, collect_must_may, &mm);
1943 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1944 " [1,10,0] -> [2,5,0] }";
1945 assert(map_is_equal(mm.must, str));
1946 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1947 assert(map_is_equal(mm.may, str));
1949 isl_map_free(mm.must);
1950 isl_map_free(mm.may);
1951 isl_flow_free(flow);
1954 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1955 map = isl_map_read_from_str(ctx, str);
1956 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1958 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1959 map = isl_map_read_from_str(ctx, str);
1960 ai = isl_access_info_add_source(ai, map, 1, &depth);
1962 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1963 map = isl_map_read_from_str(ctx, str);
1964 ai = isl_access_info_add_source(ai, map, 0, &depth);
1966 flow = isl_access_info_compute_flow(ai);
1967 dim = isl_space_alloc(ctx, 0, 3, 3);
1968 mm.must = isl_map_empty(isl_space_copy(dim));
1969 mm.may = isl_map_empty(dim);
1971 isl_flow_foreach(flow, collect_must_may, &mm);
1973 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1974 assert(map_is_equal(mm.must, str));
1975 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1976 assert(map_is_equal(mm.may, str));
1978 isl_map_free(mm.must);
1979 isl_map_free(mm.may);
1980 isl_flow_free(flow);
1983 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1984 map = isl_map_read_from_str(ctx, str);
1985 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1987 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1988 map = isl_map_read_from_str(ctx, str);
1989 ai = isl_access_info_add_source(ai, map, 0, &depth);
1991 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1992 map = isl_map_read_from_str(ctx, str);
1993 ai = isl_access_info_add_source(ai, map, 0, &depth);
1995 flow = isl_access_info_compute_flow(ai);
1996 dim = isl_space_alloc(ctx, 0, 3, 3);
1997 mm.must = isl_map_empty(isl_space_copy(dim));
1998 mm.may = isl_map_empty(dim);
2000 isl_flow_foreach(flow, collect_must_may, &mm);
2002 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2003 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2004 assert(map_is_equal(mm.may, str));
2005 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2006 assert(map_is_equal(mm.must, str));
2008 isl_map_free(mm.must);
2009 isl_map_free(mm.may);
2010 isl_flow_free(flow);
2013 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2014 map = isl_map_read_from_str(ctx, str);
2015 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2017 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2018 map = isl_map_read_from_str(ctx, str);
2019 ai = isl_access_info_add_source(ai, map, 0, &depth);
2021 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2022 map = isl_map_read_from_str(ctx, str);
2023 ai = isl_access_info_add_source(ai, map, 0, &depth);
2025 flow = isl_access_info_compute_flow(ai);
2026 dim = isl_space_alloc(ctx, 0, 3, 3);
2027 mm.must = isl_map_empty(isl_space_copy(dim));
2028 mm.may = isl_map_empty(dim);
2030 isl_flow_foreach(flow, collect_must_may, &mm);
2032 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2033 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2034 assert(map_is_equal(mm.may, str));
2035 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2036 assert(map_is_equal(mm.must, str));
2038 isl_map_free(mm.must);
2039 isl_map_free(mm.may);
2040 isl_flow_free(flow);
2043 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2044 map = isl_map_read_from_str(ctx, str);
2045 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2047 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2048 map = isl_map_read_from_str(ctx, str);
2049 ai = isl_access_info_add_source(ai, map, 0, &depth);
2051 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2052 map = isl_map_read_from_str(ctx, str);
2053 ai = isl_access_info_add_source(ai, map, 0, &depth);
2055 flow = isl_access_info_compute_flow(ai);
2056 dim = isl_space_alloc(ctx, 0, 3, 3);
2057 mm.must = isl_map_empty(isl_space_copy(dim));
2058 mm.may = isl_map_empty(dim);
2060 isl_flow_foreach(flow, collect_must_may, &mm);
2062 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2063 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2064 assert(map_is_equal(mm.may, str));
2065 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2066 assert(map_is_equal(mm.must, str));
2068 isl_map_free(mm.must);
2069 isl_map_free(mm.may);
2070 isl_flow_free(flow);
2073 depth = 5;
2075 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2076 map = isl_map_read_from_str(ctx, str);
2077 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2079 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2080 map = isl_map_read_from_str(ctx, str);
2081 ai = isl_access_info_add_source(ai, map, 1, &depth);
2083 flow = isl_access_info_compute_flow(ai);
2084 dim = isl_space_alloc(ctx, 0, 5, 5);
2085 mm.must = isl_map_empty(isl_space_copy(dim));
2086 mm.may = isl_map_empty(dim);
2088 isl_flow_foreach(flow, collect_must_may, &mm);
2090 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2091 assert(map_is_equal(mm.must, str));
2092 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2093 assert(map_is_equal(mm.may, str));
2095 isl_map_free(mm.must);
2096 isl_map_free(mm.may);
2097 isl_flow_free(flow);
2100 /* Check that the dependence analysis proceeds without errors.
2101 * Earlier versions of isl would break down during the analysis
2102 * due to the use of the wrong spaces.
2104 static int test_flow(isl_ctx *ctx)
2106 const char *str;
2107 isl_union_map *access, *schedule;
2108 isl_union_map *must_dep, *may_dep;
2109 int r;
2111 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2112 access = isl_union_map_read_from_str(ctx, str);
2113 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2114 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2115 "S2[] -> [1,0,0,0]; "
2116 "S3[] -> [-1,0,0,0] }";
2117 schedule = isl_union_map_read_from_str(ctx, str);
2118 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2119 isl_union_map_copy(access), schedule,
2120 &must_dep, &may_dep, NULL, NULL);
2121 isl_union_map_free(may_dep);
2122 isl_union_map_free(must_dep);
2124 return r;
2127 struct {
2128 const char *map;
2129 int sv;
2130 } sv_tests[] = {
2131 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2132 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2133 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2134 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2135 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2136 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2137 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2138 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2139 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2142 int test_sv(isl_ctx *ctx)
2144 isl_union_map *umap;
2145 int i;
2146 int sv;
2148 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2149 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2150 sv = isl_union_map_is_single_valued(umap);
2151 isl_union_map_free(umap);
2152 if (sv < 0)
2153 return -1;
2154 if (sv_tests[i].sv && !sv)
2155 isl_die(ctx, isl_error_internal,
2156 "map not detected as single valued", return -1);
2157 if (!sv_tests[i].sv && sv)
2158 isl_die(ctx, isl_error_internal,
2159 "map detected as single valued", return -1);
2162 return 0;
2165 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
2167 isl_map *map;
2169 map = isl_map_read_from_str(ctx, str);
2170 if (bijective)
2171 assert(isl_map_is_bijective(map));
2172 else
2173 assert(!isl_map_is_bijective(map));
2174 isl_map_free(map);
2177 void test_bijective(struct isl_ctx *ctx)
2179 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
2180 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
2181 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
2182 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
2183 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
2184 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
2185 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
2186 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
2187 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
2188 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
2189 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
2190 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
2191 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
2194 /* Inputs for isl_pw_qpolynomial_gist tests.
2195 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2197 struct {
2198 const char *pwqp;
2199 const char *set;
2200 const char *gist;
2201 } pwqp_gist_tests[] = {
2202 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2203 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2204 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2205 "{ [i] -> -1/2 + 1/2 * i }" },
2206 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2209 static int test_pwqp(struct isl_ctx *ctx)
2211 int i;
2212 const char *str;
2213 isl_set *set;
2214 isl_pw_qpolynomial *pwqp1, *pwqp2;
2215 int equal;
2217 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2218 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2220 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2221 isl_dim_in, 1, 1);
2223 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2224 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2226 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2228 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2230 isl_pw_qpolynomial_free(pwqp1);
2232 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2233 str = pwqp_gist_tests[i].pwqp;
2234 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2235 str = pwqp_gist_tests[i].set;
2236 set = isl_set_read_from_str(ctx, str);
2237 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2238 str = pwqp_gist_tests[i].gist;
2239 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2240 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2241 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2242 isl_pw_qpolynomial_free(pwqp1);
2244 if (equal < 0)
2245 return -1;
2246 if (!equal)
2247 isl_die(ctx, isl_error_unknown,
2248 "unexpected result", return -1);
2251 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2252 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2253 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2254 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2256 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2258 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2260 isl_pw_qpolynomial_free(pwqp1);
2262 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2263 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2264 str = "{ [x] -> x }";
2265 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2267 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2269 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2271 isl_pw_qpolynomial_free(pwqp1);
2273 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2274 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2275 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2276 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2277 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2278 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2279 isl_pw_qpolynomial_free(pwqp1);
2281 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2282 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2283 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2284 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2285 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2286 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2287 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2288 isl_pw_qpolynomial_free(pwqp1);
2289 isl_pw_qpolynomial_free(pwqp2);
2290 if (equal < 0)
2291 return -1;
2292 if (!equal)
2293 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2295 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2296 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2297 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2298 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2299 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2300 isl_val_one(ctx));
2301 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2302 isl_pw_qpolynomial_free(pwqp1);
2303 isl_pw_qpolynomial_free(pwqp2);
2304 if (equal < 0)
2305 return -1;
2306 if (!equal)
2307 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2309 return 0;
2312 void test_split_periods(isl_ctx *ctx)
2314 const char *str;
2315 isl_pw_qpolynomial *pwqp;
2317 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2318 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2319 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2320 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2322 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2323 assert(pwqp);
2325 isl_pw_qpolynomial_free(pwqp);
2328 void test_union(isl_ctx *ctx)
2330 const char *str;
2331 isl_union_set *uset1, *uset2;
2332 isl_union_map *umap1, *umap2;
2334 str = "{ [i] : 0 <= i <= 1 }";
2335 uset1 = isl_union_set_read_from_str(ctx, str);
2336 str = "{ [1] -> [0] }";
2337 umap1 = isl_union_map_read_from_str(ctx, str);
2339 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2340 assert(isl_union_map_is_equal(umap1, umap2));
2342 isl_union_map_free(umap1);
2343 isl_union_map_free(umap2);
2345 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2346 umap1 = isl_union_map_read_from_str(ctx, str);
2347 str = "{ A[i]; B[i] }";
2348 uset1 = isl_union_set_read_from_str(ctx, str);
2350 uset2 = isl_union_map_domain(umap1);
2352 assert(isl_union_set_is_equal(uset1, uset2));
2354 isl_union_set_free(uset1);
2355 isl_union_set_free(uset2);
2358 void test_bound(isl_ctx *ctx)
2360 const char *str;
2361 isl_pw_qpolynomial *pwqp;
2362 isl_pw_qpolynomial_fold *pwf;
2364 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2365 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2366 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2367 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
2368 isl_pw_qpolynomial_fold_free(pwf);
2370 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2371 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2372 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2373 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
2374 isl_pw_qpolynomial_fold_free(pwf);
2377 void test_lift(isl_ctx *ctx)
2379 const char *str;
2380 isl_basic_map *bmap;
2381 isl_basic_set *bset;
2383 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2384 bset = isl_basic_set_read_from_str(ctx, str);
2385 bset = isl_basic_set_lift(bset);
2386 bmap = isl_basic_map_from_range(bset);
2387 bset = isl_basic_map_domain(bmap);
2388 isl_basic_set_free(bset);
2391 struct {
2392 const char *set1;
2393 const char *set2;
2394 int subset;
2395 } subset_tests[] = {
2396 { "{ [112, 0] }",
2397 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2398 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2399 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2400 { "{ [65] }",
2401 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2402 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2403 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2404 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2405 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2406 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2407 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2408 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2409 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2410 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2411 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2412 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2413 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2414 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
2415 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
2416 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
2417 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
2418 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
2419 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
2420 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
2421 "4e0 <= -57 + i0 + i1)) or "
2422 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
2423 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
2424 "4e0 >= -61 + i0 + i1)) or "
2425 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
2428 static int test_subset(isl_ctx *ctx)
2430 int i;
2431 isl_set *set1, *set2;
2432 int subset;
2434 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2435 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2436 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2437 subset = isl_set_is_subset(set1, set2);
2438 isl_set_free(set1);
2439 isl_set_free(set2);
2440 if (subset < 0)
2441 return -1;
2442 if (subset != subset_tests[i].subset)
2443 isl_die(ctx, isl_error_unknown,
2444 "incorrect subset result", return -1);
2447 return 0;
2450 struct {
2451 const char *minuend;
2452 const char *subtrahend;
2453 const char *difference;
2454 } subtract_domain_tests[] = {
2455 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2456 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2457 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2460 static int test_subtract(isl_ctx *ctx)
2462 int i;
2463 isl_union_map *umap1, *umap2;
2464 isl_union_pw_multi_aff *upma1, *upma2;
2465 isl_union_set *uset;
2466 int equal;
2468 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2469 umap1 = isl_union_map_read_from_str(ctx,
2470 subtract_domain_tests[i].minuend);
2471 uset = isl_union_set_read_from_str(ctx,
2472 subtract_domain_tests[i].subtrahend);
2473 umap2 = isl_union_map_read_from_str(ctx,
2474 subtract_domain_tests[i].difference);
2475 umap1 = isl_union_map_subtract_domain(umap1, uset);
2476 equal = isl_union_map_is_equal(umap1, umap2);
2477 isl_union_map_free(umap1);
2478 isl_union_map_free(umap2);
2479 if (equal < 0)
2480 return -1;
2481 if (!equal)
2482 isl_die(ctx, isl_error_unknown,
2483 "incorrect subtract domain result", return -1);
2486 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2487 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
2488 subtract_domain_tests[i].minuend);
2489 uset = isl_union_set_read_from_str(ctx,
2490 subtract_domain_tests[i].subtrahend);
2491 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
2492 subtract_domain_tests[i].difference);
2493 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
2494 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
2495 isl_union_pw_multi_aff_free(upma1);
2496 isl_union_pw_multi_aff_free(upma2);
2497 if (equal < 0)
2498 return -1;
2499 if (!equal)
2500 isl_die(ctx, isl_error_unknown,
2501 "incorrect subtract domain result", return -1);
2504 return 0;
2507 int test_factorize(isl_ctx *ctx)
2509 const char *str;
2510 isl_basic_set *bset;
2511 isl_factorizer *f;
2513 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2514 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2515 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2516 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2517 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2518 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2519 "3i5 >= -2i0 - i2 + 3i4 }";
2520 bset = isl_basic_set_read_from_str(ctx, str);
2521 f = isl_basic_set_factorizer(bset);
2522 isl_basic_set_free(bset);
2523 isl_factorizer_free(f);
2524 if (!f)
2525 isl_die(ctx, isl_error_unknown,
2526 "failed to construct factorizer", return -1);
2528 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2529 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2530 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2531 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2532 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2533 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2534 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2535 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2536 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2537 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2538 bset = isl_basic_set_read_from_str(ctx, str);
2539 f = isl_basic_set_factorizer(bset);
2540 isl_basic_set_free(bset);
2541 isl_factorizer_free(f);
2542 if (!f)
2543 isl_die(ctx, isl_error_unknown,
2544 "failed to construct factorizer", return -1);
2546 return 0;
2549 static int check_injective(__isl_take isl_map *map, void *user)
2551 int *injective = user;
2553 *injective = isl_map_is_injective(map);
2554 isl_map_free(map);
2556 if (*injective < 0 || !*injective)
2557 return -1;
2559 return 0;
2562 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2563 const char *r, const char *s, int tilable, int parallel)
2565 int i;
2566 isl_union_set *D;
2567 isl_union_map *W, *R, *S;
2568 isl_union_map *empty;
2569 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2570 isl_union_map *validity, *proximity, *coincidence;
2571 isl_union_map *schedule;
2572 isl_union_map *test;
2573 isl_union_set *delta;
2574 isl_union_set *domain;
2575 isl_set *delta_set;
2576 isl_set *slice;
2577 isl_set *origin;
2578 isl_schedule_constraints *sc;
2579 isl_schedule *sched;
2580 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2582 D = isl_union_set_read_from_str(ctx, d);
2583 W = isl_union_map_read_from_str(ctx, w);
2584 R = isl_union_map_read_from_str(ctx, r);
2585 S = isl_union_map_read_from_str(ctx, s);
2587 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2588 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2590 empty = isl_union_map_empty(isl_union_map_get_space(S));
2591 isl_union_map_compute_flow(isl_union_map_copy(R),
2592 isl_union_map_copy(W), empty,
2593 isl_union_map_copy(S),
2594 &dep_raw, NULL, NULL, NULL);
2595 isl_union_map_compute_flow(isl_union_map_copy(W),
2596 isl_union_map_copy(W),
2597 isl_union_map_copy(R),
2598 isl_union_map_copy(S),
2599 &dep_waw, &dep_war, NULL, NULL);
2601 dep = isl_union_map_union(dep_waw, dep_war);
2602 dep = isl_union_map_union(dep, dep_raw);
2603 validity = isl_union_map_copy(dep);
2604 coincidence = isl_union_map_copy(dep);
2605 proximity = isl_union_map_copy(dep);
2607 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
2608 sc = isl_schedule_constraints_set_validity(sc, validity);
2609 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
2610 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2611 sched = isl_schedule_constraints_compute_schedule(sc);
2612 schedule = isl_schedule_get_map(sched);
2613 isl_schedule_free(sched);
2614 isl_union_map_free(W);
2615 isl_union_map_free(R);
2616 isl_union_map_free(S);
2618 is_injection = 1;
2619 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2621 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2622 is_complete = isl_union_set_is_subset(D, domain);
2623 isl_union_set_free(D);
2624 isl_union_set_free(domain);
2626 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2627 test = isl_union_map_apply_range(test, dep);
2628 test = isl_union_map_apply_range(test, schedule);
2630 delta = isl_union_map_deltas(test);
2631 if (isl_union_set_n_set(delta) == 0) {
2632 is_tilable = 1;
2633 is_parallel = 1;
2634 is_nonneg = 1;
2635 isl_union_set_free(delta);
2636 } else {
2637 delta_set = isl_set_from_union_set(delta);
2639 slice = isl_set_universe(isl_set_get_space(delta_set));
2640 for (i = 0; i < tilable; ++i)
2641 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2642 is_tilable = isl_set_is_subset(delta_set, slice);
2643 isl_set_free(slice);
2645 slice = isl_set_universe(isl_set_get_space(delta_set));
2646 for (i = 0; i < parallel; ++i)
2647 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2648 is_parallel = isl_set_is_subset(delta_set, slice);
2649 isl_set_free(slice);
2651 origin = isl_set_universe(isl_set_get_space(delta_set));
2652 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2653 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2655 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2656 delta_set = isl_set_lexmin(delta_set);
2658 is_nonneg = isl_set_is_equal(delta_set, origin);
2660 isl_set_free(origin);
2661 isl_set_free(delta_set);
2664 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2665 is_injection < 0 || is_complete < 0)
2666 return -1;
2667 if (!is_complete)
2668 isl_die(ctx, isl_error_unknown,
2669 "generated schedule incomplete", return -1);
2670 if (!is_injection)
2671 isl_die(ctx, isl_error_unknown,
2672 "generated schedule not injective on each statement",
2673 return -1);
2674 if (!is_nonneg)
2675 isl_die(ctx, isl_error_unknown,
2676 "negative dependences in generated schedule",
2677 return -1);
2678 if (!is_tilable)
2679 isl_die(ctx, isl_error_unknown,
2680 "generated schedule not as tilable as expected",
2681 return -1);
2682 if (!is_parallel)
2683 isl_die(ctx, isl_error_unknown,
2684 "generated schedule not as parallel as expected",
2685 return -1);
2687 return 0;
2690 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2691 const char *domain, const char *validity, const char *proximity)
2693 isl_union_set *dom;
2694 isl_union_map *dep;
2695 isl_union_map *prox;
2696 isl_schedule_constraints *sc;
2697 isl_schedule *schedule;
2698 isl_union_map *sched;
2700 dom = isl_union_set_read_from_str(ctx, domain);
2701 dep = isl_union_map_read_from_str(ctx, validity);
2702 prox = isl_union_map_read_from_str(ctx, proximity);
2703 sc = isl_schedule_constraints_on_domain(dom);
2704 sc = isl_schedule_constraints_set_validity(sc, dep);
2705 sc = isl_schedule_constraints_set_proximity(sc, prox);
2706 schedule = isl_schedule_constraints_compute_schedule(sc);
2707 sched = isl_schedule_get_map(schedule);
2708 isl_schedule_free(schedule);
2710 return sched;
2713 /* Check that a schedule can be constructed on the given domain
2714 * with the given validity and proximity constraints.
2716 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2717 const char *validity, const char *proximity)
2719 isl_union_map *sched;
2721 sched = compute_schedule(ctx, domain, validity, proximity);
2722 if (!sched)
2723 return -1;
2725 isl_union_map_free(sched);
2726 return 0;
2729 int test_special_schedule(isl_ctx *ctx, const char *domain,
2730 const char *validity, const char *proximity, const char *expected_sched)
2732 isl_union_map *sched1, *sched2;
2733 int equal;
2735 sched1 = compute_schedule(ctx, domain, validity, proximity);
2736 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2738 equal = isl_union_map_is_equal(sched1, sched2);
2739 isl_union_map_free(sched1);
2740 isl_union_map_free(sched2);
2742 if (equal < 0)
2743 return -1;
2744 if (!equal)
2745 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2746 return -1);
2748 return 0;
2751 /* Check that the schedule map is properly padded, even after being
2752 * reconstructed from the band forest.
2754 static int test_padded_schedule(isl_ctx *ctx)
2756 const char *str;
2757 isl_union_set *D;
2758 isl_union_map *validity, *proximity;
2759 isl_schedule_constraints *sc;
2760 isl_schedule *sched;
2761 isl_union_map *map1, *map2;
2762 isl_band_list *list;
2763 int equal;
2765 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2766 D = isl_union_set_read_from_str(ctx, str);
2767 validity = isl_union_map_empty(isl_union_set_get_space(D));
2768 proximity = isl_union_map_copy(validity);
2769 sc = isl_schedule_constraints_on_domain(D);
2770 sc = isl_schedule_constraints_set_validity(sc, validity);
2771 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2772 sched = isl_schedule_constraints_compute_schedule(sc);
2773 map1 = isl_schedule_get_map(sched);
2774 list = isl_schedule_get_band_forest(sched);
2775 isl_band_list_free(list);
2776 map2 = isl_schedule_get_map(sched);
2777 isl_schedule_free(sched);
2778 equal = isl_union_map_is_equal(map1, map2);
2779 isl_union_map_free(map1);
2780 isl_union_map_free(map2);
2782 if (equal < 0)
2783 return -1;
2784 if (!equal)
2785 isl_die(ctx, isl_error_unknown,
2786 "reconstructed schedule map not the same as original",
2787 return -1);
2789 return 0;
2792 /* Input for testing of schedule construction based on
2793 * conditional constraints.
2795 * domain is the iteration domain
2796 * flow are the flow dependences, which determine the validity and
2797 * proximity constraints
2798 * condition are the conditions on the conditional validity constraints
2799 * conditional_validity are the conditional validity constraints
2800 * outer_band_n is the expected number of members in the outer band
2802 struct {
2803 const char *domain;
2804 const char *flow;
2805 const char *condition;
2806 const char *conditional_validity;
2807 int outer_band_n;
2808 } live_range_tests[] = {
2809 /* Contrived example that illustrates that we need to keep
2810 * track of tagged condition dependences and
2811 * tagged conditional validity dependences
2812 * in isl_sched_edge separately.
2813 * In particular, the conditional validity constraints on A
2814 * cannot be satisfied,
2815 * but they can be ignored because there are no corresponding
2816 * condition constraints. However, we do have an additional
2817 * conditional validity constraint that maps to the same
2818 * dependence relation
2819 * as the condition constraint on B. If we did not make a distinction
2820 * between tagged condition and tagged conditional validity
2821 * dependences, then we
2822 * could end up treating this shared dependence as an condition
2823 * constraint on A, forcing a localization of the conditions,
2824 * which is impossible.
2826 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
2827 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
2828 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
2829 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2830 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2831 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
2834 /* TACO 2013 Fig. 7 */
2835 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2836 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2837 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2838 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
2839 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
2840 "0 <= i < n and 0 <= j < n - 1 }",
2841 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
2842 "0 <= i < n and 0 <= j < j' < n;"
2843 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
2844 "0 <= i < i' < n and 0 <= j,j' < n;"
2845 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
2846 "0 <= i,j,j' < n and j < j' }",
2849 /* TACO 2013 Fig. 7, without tags */
2850 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2851 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2852 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2853 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2854 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2855 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
2856 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
2857 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
2860 /* TACO 2013 Fig. 12 */
2861 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
2862 "S3[i,3] : 0 <= i <= 1 }",
2863 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
2864 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
2865 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
2866 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
2867 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2868 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
2869 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2870 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
2871 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
2872 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
2873 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
2878 /* Test schedule construction based on conditional constraints.
2879 * In particular, check the number of members in the outer band
2880 * as an indication of whether tiling is possible or not.
2882 static int test_conditional_schedule_constraints(isl_ctx *ctx)
2884 int i;
2885 isl_union_set *domain;
2886 isl_union_map *condition;
2887 isl_union_map *flow;
2888 isl_union_map *validity;
2889 isl_schedule_constraints *sc;
2890 isl_schedule *schedule;
2891 isl_band_list *list;
2892 isl_band *band;
2893 int n_member;
2895 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
2896 domain = isl_union_set_read_from_str(ctx,
2897 live_range_tests[i].domain);
2898 flow = isl_union_map_read_from_str(ctx,
2899 live_range_tests[i].flow);
2900 condition = isl_union_map_read_from_str(ctx,
2901 live_range_tests[i].condition);
2902 validity = isl_union_map_read_from_str(ctx,
2903 live_range_tests[i].conditional_validity);
2904 sc = isl_schedule_constraints_on_domain(domain);
2905 sc = isl_schedule_constraints_set_validity(sc,
2906 isl_union_map_copy(flow));
2907 sc = isl_schedule_constraints_set_proximity(sc, flow);
2908 sc = isl_schedule_constraints_set_conditional_validity(sc,
2909 condition, validity);
2910 schedule = isl_schedule_constraints_compute_schedule(sc);
2911 list = isl_schedule_get_band_forest(schedule);
2912 band = isl_band_list_get_band(list, 0);
2913 n_member = isl_band_n_member(band);
2914 isl_band_free(band);
2915 isl_band_list_free(list);
2916 isl_schedule_free(schedule);
2918 if (!schedule)
2919 return -1;
2920 if (n_member != live_range_tests[i].outer_band_n)
2921 isl_die(ctx, isl_error_unknown,
2922 "unexpected number of members in outer band",
2923 return -1);
2925 return 0;
2928 int test_schedule(isl_ctx *ctx)
2930 const char *D, *W, *R, *V, *P, *S;
2932 /* Handle resulting schedule with zero bands. */
2933 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2934 return -1;
2936 /* Jacobi */
2937 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2938 W = "{ S1[t,i] -> a[t,i] }";
2939 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2940 "S1[t,i] -> a[t-1,i+1] }";
2941 S = "{ S1[t,i] -> [t,i] }";
2942 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2943 return -1;
2945 /* Fig. 5 of CC2008 */
2946 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2947 "j <= -1 + N }";
2948 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2949 "j >= 2 and j <= -1 + N }";
2950 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2951 "j >= 2 and j <= -1 + N; "
2952 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2953 "j >= 2 and j <= -1 + N }";
2954 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2955 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2956 return -1;
2958 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2959 W = "{ S1[i] -> a[i] }";
2960 R = "{ S2[i] -> a[i+1] }";
2961 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2962 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2963 return -1;
2965 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2966 W = "{ S1[i] -> a[i] }";
2967 R = "{ S2[i] -> a[9-i] }";
2968 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2969 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2970 return -1;
2972 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2973 W = "{ S1[i] -> a[i] }";
2974 R = "[N] -> { S2[i] -> a[N-1-i] }";
2975 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2976 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2977 return -1;
2979 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2980 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2981 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2982 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2983 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2984 return -1;
2986 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2987 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2988 R = "{ S2[i,j] -> a[i-1,j] }";
2989 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2990 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2991 return -1;
2993 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2994 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2995 R = "{ S2[i,j] -> a[i,j-1] }";
2996 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2997 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2998 return -1;
3000 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
3001 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
3002 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
3003 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
3004 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
3005 "S_0[] -> [0, 0, 0] }";
3006 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
3007 return -1;
3008 ctx->opt->schedule_parametric = 0;
3009 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3010 return -1;
3011 ctx->opt->schedule_parametric = 1;
3013 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
3014 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
3015 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
3016 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
3017 "S4[i] -> a[i,N] }";
3018 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
3019 "S4[i] -> [4,i,0] }";
3020 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3021 return -1;
3023 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
3024 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3025 "j <= N }";
3026 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3027 "j <= N; "
3028 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
3029 "j <= N }";
3030 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3031 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3032 return -1;
3034 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
3035 " S_2[t] : t >= 0 and t <= -1 + N; "
3036 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
3037 "i <= -1 + N }";
3038 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
3039 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
3040 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
3041 "i >= 0 and i <= -1 + N }";
3042 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
3043 "i >= 0 and i <= -1 + N; "
3044 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
3045 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
3046 " S_0[t] -> [0, t, 0] }";
3048 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3049 return -1;
3050 ctx->opt->schedule_parametric = 0;
3051 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3052 return -1;
3053 ctx->opt->schedule_parametric = 1;
3055 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
3056 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
3057 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
3058 return -1;
3060 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
3061 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
3062 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
3063 "j >= 0 and j <= -1 + N; "
3064 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3065 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
3066 "j >= 0 and j <= -1 + N; "
3067 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3068 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
3069 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3070 return -1;
3072 D = "{ S_0[i] : i >= 0 }";
3073 W = "{ S_0[i] -> a[i] : i >= 0 }";
3074 R = "{ S_0[i] -> a[0] : i >= 0 }";
3075 S = "{ S_0[i] -> [0, i, 0] }";
3076 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3077 return -1;
3079 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
3080 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
3081 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
3082 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
3083 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3084 return -1;
3086 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
3087 "k <= -1 + n and k >= 0 }";
3088 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
3089 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
3090 "k <= -1 + n and k >= 0; "
3091 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
3092 "k <= -1 + n and k >= 0; "
3093 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
3094 "k <= -1 + n and k >= 0 }";
3095 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
3096 ctx->opt->schedule_outer_coincidence = 1;
3097 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3098 return -1;
3099 ctx->opt->schedule_outer_coincidence = 0;
3101 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3102 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3103 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3104 "Stmt_for_body24[i0, i1, 1, 0]:"
3105 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3106 "Stmt_for_body7[i0, i1, i2]:"
3107 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3108 "i2 <= 7 }";
3110 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3111 "Stmt_for_body24[1, i1, i2, i3]:"
3112 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3113 "i2 >= 1;"
3114 "Stmt_for_body24[0, i1, i2, i3] -> "
3115 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3116 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3117 "i3 >= 0;"
3118 "Stmt_for_body24[0, i1, i2, i3] ->"
3119 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3120 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3121 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3122 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3123 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3124 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3125 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3126 "i1 <= 6 and i1 >= 0;"
3127 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3128 "Stmt_for_body7[i0, i1, i2] -> "
3129 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3130 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3131 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3132 "Stmt_for_body7[i0, i1, i2] -> "
3133 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3134 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3135 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3136 P = V;
3137 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3138 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3139 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3141 if (test_special_schedule(ctx, D, V, P, S) < 0)
3142 return -1;
3144 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3145 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3146 "j >= 1 and j <= 7;"
3147 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3148 "j >= 1 and j <= 8 }";
3149 P = "{ }";
3150 S = "{ S_0[i, j] -> [i + j, j] }";
3151 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3152 if (test_special_schedule(ctx, D, V, P, S) < 0)
3153 return -1;
3154 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3156 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3157 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3158 "j >= 0 and j <= -1 + i }";
3159 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3160 "i <= -1 + N and j >= 0;"
3161 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3162 "i <= -2 + N }";
3163 P = "{ }";
3164 S = "{ S_0[i, j] -> [i, j] }";
3165 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3166 if (test_special_schedule(ctx, D, V, P, S) < 0)
3167 return -1;
3168 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3170 /* Test both algorithms on a case with only proximity dependences. */
3171 D = "{ S[i,j] : 0 <= i <= 10 }";
3172 V = "{ }";
3173 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3174 S = "{ S[i, j] -> [j, i] }";
3175 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3176 if (test_special_schedule(ctx, D, V, P, S) < 0)
3177 return -1;
3178 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3179 if (test_special_schedule(ctx, D, V, P, S) < 0)
3180 return -1;
3182 D = "{ A[a]; B[] }";
3183 V = "{}";
3184 P = "{ A[a] -> B[] }";
3185 if (test_has_schedule(ctx, D, V, P) < 0)
3186 return -1;
3188 if (test_padded_schedule(ctx) < 0)
3189 return -1;
3191 /* Check that check for progress is not confused by rational
3192 * solution.
3194 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3195 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3196 "i0 <= -2 + N; "
3197 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3198 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3199 P = "{}";
3200 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3201 if (test_has_schedule(ctx, D, V, P) < 0)
3202 return -1;
3203 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3205 /* Check that we allow schedule rows that are only non-trivial
3206 * on some full-dimensional domains.
3208 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
3209 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
3210 "S1[j] -> S2[1] : 0 <= j <= 1 }";
3211 P = "{}";
3212 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3213 if (test_has_schedule(ctx, D, V, P) < 0)
3214 return -1;
3215 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3217 if (test_conditional_schedule_constraints(ctx) < 0)
3218 return -1;
3220 return 0;
3223 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3225 isl_union_map *umap;
3226 int test;
3228 umap = isl_union_map_read_from_str(ctx, str);
3229 test = isl_union_map_plain_is_injective(umap);
3230 isl_union_map_free(umap);
3231 if (test < 0)
3232 return -1;
3233 if (test == injective)
3234 return 0;
3235 if (injective)
3236 isl_die(ctx, isl_error_unknown,
3237 "map not detected as injective", return -1);
3238 else
3239 isl_die(ctx, isl_error_unknown,
3240 "map detected as injective", return -1);
3243 int test_injective(isl_ctx *ctx)
3245 const char *str;
3247 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
3248 return -1;
3249 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
3250 return -1;
3251 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
3252 return -1;
3253 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
3254 return -1;
3255 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
3256 return -1;
3257 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
3258 return -1;
3259 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
3260 return -1;
3261 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
3262 return -1;
3264 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
3265 if (test_plain_injective(ctx, str, 1))
3266 return -1;
3267 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
3268 if (test_plain_injective(ctx, str, 0))
3269 return -1;
3271 return 0;
3274 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
3276 isl_aff *aff2;
3277 int equal;
3279 if (!aff)
3280 return -1;
3282 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
3283 equal = isl_aff_plain_is_equal(aff, aff2);
3284 isl_aff_free(aff2);
3286 return equal;
3289 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
3291 int equal;
3293 equal = aff_plain_is_equal(aff, str);
3294 if (equal < 0)
3295 return -1;
3296 if (!equal)
3297 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
3298 "result not as expected", return -1);
3299 return 0;
3302 struct {
3303 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3304 __isl_take isl_aff *aff2);
3305 } aff_bin_op[] = {
3306 ['+'] = { &isl_aff_add },
3307 ['-'] = { &isl_aff_sub },
3308 ['*'] = { &isl_aff_mul },
3309 ['/'] = { &isl_aff_div },
3312 struct {
3313 const char *arg1;
3314 unsigned char op;
3315 const char *arg2;
3316 const char *res;
3317 } aff_bin_tests[] = {
3318 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
3319 "{ [i] -> [2i] }" },
3320 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
3321 "{ [i] -> [0] }" },
3322 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
3323 "{ [i] -> [2i] }" },
3324 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
3325 "{ [i] -> [2i] }" },
3326 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
3327 "{ [i] -> [i/2] }" },
3328 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
3329 "{ [i] -> [i] }" },
3330 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
3331 "{ [i] -> [NaN] }" },
3332 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
3333 "{ [i] -> [NaN] }" },
3334 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
3335 "{ [i] -> [NaN] }" },
3336 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
3337 "{ [i] -> [NaN] }" },
3338 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
3339 "{ [i] -> [NaN] }" },
3340 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
3341 "{ [i] -> [NaN] }" },
3342 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
3343 "{ [i] -> [NaN] }" },
3344 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
3345 "{ [i] -> [NaN] }" },
3346 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
3347 "{ [i] -> [NaN] }" },
3348 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
3349 "{ [i] -> [NaN] }" },
3350 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
3351 "{ [i] -> [NaN] }" },
3352 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
3353 "{ [i] -> [NaN] }" },
3356 /* Perform some basic tests of binary operations on isl_aff objects.
3358 static int test_bin_aff(isl_ctx *ctx)
3360 int i;
3361 isl_aff *aff1, *aff2, *res;
3362 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3363 __isl_take isl_aff *aff2);
3364 int ok;
3366 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
3367 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
3368 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
3369 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
3370 fn = aff_bin_op[aff_bin_tests[i].op].fn;
3371 aff1 = fn(aff1, aff2);
3372 if (isl_aff_is_nan(res))
3373 ok = isl_aff_is_nan(aff1);
3374 else
3375 ok = isl_aff_plain_is_equal(aff1, res);
3376 isl_aff_free(aff1);
3377 isl_aff_free(res);
3378 if (ok < 0)
3379 return -1;
3380 if (!ok)
3381 isl_die(ctx, isl_error_unknown,
3382 "unexpected result", return -1);
3385 return 0;
3388 struct {
3389 __isl_give isl_union_pw_multi_aff *(*fn)(
3390 __isl_take isl_union_pw_multi_aff *upma1,
3391 __isl_take isl_union_pw_multi_aff *upma2);
3392 const char *arg1;
3393 const char *arg2;
3394 const char *res;
3395 } upma_bin_tests[] = {
3396 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
3397 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
3398 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
3399 "{ B[x] -> [2] : x >= 0 }",
3400 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
3403 /* Perform some basic tests of binary operations on
3404 * isl_union_pw_multi_aff objects.
3406 static int test_bin_upma(isl_ctx *ctx)
3408 int i;
3409 isl_union_pw_multi_aff *upma1, *upma2, *res;
3410 int ok;
3412 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
3413 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3414 upma_bin_tests[i].arg1);
3415 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3416 upma_bin_tests[i].arg2);
3417 res = isl_union_pw_multi_aff_read_from_str(ctx,
3418 upma_bin_tests[i].res);
3419 upma1 = upma_bin_tests[i].fn(upma1, upma2);
3420 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
3421 isl_union_pw_multi_aff_free(upma1);
3422 isl_union_pw_multi_aff_free(res);
3423 if (ok < 0)
3424 return -1;
3425 if (!ok)
3426 isl_die(ctx, isl_error_unknown,
3427 "unexpected result", return -1);
3430 return 0;
3433 int test_aff(isl_ctx *ctx)
3435 const char *str;
3436 isl_set *set;
3437 isl_space *space;
3438 isl_local_space *ls;
3439 isl_aff *aff;
3440 int zero, equal;
3442 if (test_bin_aff(ctx) < 0)
3443 return -1;
3444 if (test_bin_upma(ctx) < 0)
3445 return -1;
3447 space = isl_space_set_alloc(ctx, 0, 1);
3448 ls = isl_local_space_from_space(space);
3449 aff = isl_aff_zero_on_domain(ls);
3451 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3452 aff = isl_aff_scale_down_ui(aff, 3);
3453 aff = isl_aff_floor(aff);
3454 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3455 aff = isl_aff_scale_down_ui(aff, 2);
3456 aff = isl_aff_floor(aff);
3457 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3459 str = "{ [10] }";
3460 set = isl_set_read_from_str(ctx, str);
3461 aff = isl_aff_gist(aff, set);
3463 aff = isl_aff_add_constant_si(aff, -16);
3464 zero = isl_aff_plain_is_zero(aff);
3465 isl_aff_free(aff);
3467 if (zero < 0)
3468 return -1;
3469 if (!zero)
3470 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3472 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
3473 aff = isl_aff_scale_down_ui(aff, 64);
3474 aff = isl_aff_floor(aff);
3475 equal = aff_check_plain_equal(aff, "{ [-1] }");
3476 isl_aff_free(aff);
3477 if (equal < 0)
3478 return -1;
3480 return 0;
3483 int test_dim_max(isl_ctx *ctx)
3485 int equal;
3486 const char *str;
3487 isl_set *set1, *set2;
3488 isl_set *set;
3489 isl_map *map;
3490 isl_pw_aff *pwaff;
3492 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
3493 set = isl_set_read_from_str(ctx, str);
3494 pwaff = isl_set_dim_max(set, 0);
3495 set1 = isl_set_from_pw_aff(pwaff);
3496 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
3497 set2 = isl_set_read_from_str(ctx, str);
3498 equal = isl_set_is_equal(set1, set2);
3499 isl_set_free(set1);
3500 isl_set_free(set2);
3501 if (equal < 0)
3502 return -1;
3503 if (!equal)
3504 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3506 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
3507 set = isl_set_read_from_str(ctx, str);
3508 pwaff = isl_set_dim_max(set, 0);
3509 set1 = isl_set_from_pw_aff(pwaff);
3510 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3511 set2 = isl_set_read_from_str(ctx, str);
3512 equal = isl_set_is_equal(set1, set2);
3513 isl_set_free(set1);
3514 isl_set_free(set2);
3515 if (equal < 0)
3516 return -1;
3517 if (!equal)
3518 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3520 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
3521 set = isl_set_read_from_str(ctx, str);
3522 pwaff = isl_set_dim_max(set, 0);
3523 set1 = isl_set_from_pw_aff(pwaff);
3524 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3525 set2 = isl_set_read_from_str(ctx, str);
3526 equal = isl_set_is_equal(set1, set2);
3527 isl_set_free(set1);
3528 isl_set_free(set2);
3529 if (equal < 0)
3530 return -1;
3531 if (!equal)
3532 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3534 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
3535 "0 <= i < N and 0 <= j < M }";
3536 map = isl_map_read_from_str(ctx, str);
3537 set = isl_map_range(map);
3539 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
3540 set1 = isl_set_from_pw_aff(pwaff);
3541 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
3542 set2 = isl_set_read_from_str(ctx, str);
3543 equal = isl_set_is_equal(set1, set2);
3544 isl_set_free(set1);
3545 isl_set_free(set2);
3547 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3548 set1 = isl_set_from_pw_aff(pwaff);
3549 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3550 set2 = isl_set_read_from_str(ctx, str);
3551 if (equal >= 0 && equal)
3552 equal = isl_set_is_equal(set1, set2);
3553 isl_set_free(set1);
3554 isl_set_free(set2);
3556 isl_set_free(set);
3558 if (equal < 0)
3559 return -1;
3560 if (!equal)
3561 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3563 /* Check that solutions are properly merged. */
3564 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3565 "c <= -1 + n - 4a - 2b and c >= -2b and "
3566 "4a >= -4 + n and c >= 0 }";
3567 set = isl_set_read_from_str(ctx, str);
3568 pwaff = isl_set_dim_min(set, 2);
3569 set1 = isl_set_from_pw_aff(pwaff);
3570 str = "[n] -> { [(0)] : n >= 1 }";
3571 set2 = isl_set_read_from_str(ctx, str);
3572 equal = isl_set_is_equal(set1, set2);
3573 isl_set_free(set1);
3574 isl_set_free(set2);
3576 if (equal < 0)
3577 return -1;
3578 if (!equal)
3579 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3581 /* Check that empty solution lie in the right space. */
3582 str = "[n] -> { [t,a] : 1 = 0 }";
3583 set = isl_set_read_from_str(ctx, str);
3584 pwaff = isl_set_dim_max(set, 0);
3585 set1 = isl_set_from_pw_aff(pwaff);
3586 str = "[n] -> { [t] : 1 = 0 }";
3587 set2 = isl_set_read_from_str(ctx, str);
3588 equal = isl_set_is_equal(set1, set2);
3589 isl_set_free(set1);
3590 isl_set_free(set2);
3592 if (equal < 0)
3593 return -1;
3594 if (!equal)
3595 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3597 return 0;
3600 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
3602 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
3603 const char *str)
3605 isl_ctx *ctx;
3606 isl_pw_multi_aff *pma2;
3607 int equal;
3609 if (!pma)
3610 return -1;
3612 ctx = isl_pw_multi_aff_get_ctx(pma);
3613 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3614 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
3615 isl_pw_multi_aff_free(pma2);
3617 return equal;
3620 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
3621 * represented by "str".
3623 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
3624 const char *str)
3626 int equal;
3628 equal = pw_multi_aff_plain_is_equal(pma, str);
3629 if (equal < 0)
3630 return -1;
3631 if (!equal)
3632 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
3633 "result not as expected", return -1);
3634 return 0;
3637 /* Basic test for isl_pw_multi_aff_product.
3639 * Check that multiple pieces are properly handled.
3641 static int test_product_pma(isl_ctx *ctx)
3643 int equal;
3644 const char *str;
3645 isl_pw_multi_aff *pma1, *pma2;
3647 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
3648 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3649 str = "{ C[] -> D[] }";
3650 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3651 pma1 = isl_pw_multi_aff_product(pma1, pma2);
3652 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
3653 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
3654 equal = pw_multi_aff_check_plain_equal(pma1, str);
3655 isl_pw_multi_aff_free(pma1);
3656 if (equal < 0)
3657 return -1;
3659 return 0;
3662 int test_product(isl_ctx *ctx)
3664 const char *str;
3665 isl_set *set;
3666 isl_union_set *uset1, *uset2;
3667 int ok;
3669 str = "{ A[i] }";
3670 set = isl_set_read_from_str(ctx, str);
3671 set = isl_set_product(set, isl_set_copy(set));
3672 ok = isl_set_is_wrapping(set);
3673 isl_set_free(set);
3674 if (ok < 0)
3675 return -1;
3676 if (!ok)
3677 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3679 str = "{ [] }";
3680 uset1 = isl_union_set_read_from_str(ctx, str);
3681 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3682 str = "{ [[] -> []] }";
3683 uset2 = isl_union_set_read_from_str(ctx, str);
3684 ok = isl_union_set_is_equal(uset1, uset2);
3685 isl_union_set_free(uset1);
3686 isl_union_set_free(uset2);
3687 if (ok < 0)
3688 return -1;
3689 if (!ok)
3690 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3692 if (test_product_pma(ctx) < 0)
3693 return -1;
3695 return 0;
3698 /* Check that two sets are not considered disjoint just because
3699 * they have a different set of (named) parameters.
3701 static int test_disjoint(isl_ctx *ctx)
3703 const char *str;
3704 isl_set *set, *set2;
3705 int disjoint;
3707 str = "[n] -> { [[]->[]] }";
3708 set = isl_set_read_from_str(ctx, str);
3709 str = "{ [[]->[]] }";
3710 set2 = isl_set_read_from_str(ctx, str);
3711 disjoint = isl_set_is_disjoint(set, set2);
3712 isl_set_free(set);
3713 isl_set_free(set2);
3714 if (disjoint < 0)
3715 return -1;
3716 if (disjoint)
3717 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3719 return 0;
3722 int test_equal(isl_ctx *ctx)
3724 const char *str;
3725 isl_set *set, *set2;
3726 int equal;
3728 str = "{ S_6[i] }";
3729 set = isl_set_read_from_str(ctx, str);
3730 str = "{ S_7[i] }";
3731 set2 = isl_set_read_from_str(ctx, str);
3732 equal = isl_set_is_equal(set, set2);
3733 isl_set_free(set);
3734 isl_set_free(set2);
3735 if (equal < 0)
3736 return -1;
3737 if (equal)
3738 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3740 return 0;
3743 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3744 enum isl_dim_type type, unsigned pos, int fixed)
3746 int test;
3748 test = isl_map_plain_is_fixed(map, type, pos, NULL);
3749 isl_map_free(map);
3750 if (test < 0)
3751 return -1;
3752 if (test == fixed)
3753 return 0;
3754 if (fixed)
3755 isl_die(ctx, isl_error_unknown,
3756 "map not detected as fixed", return -1);
3757 else
3758 isl_die(ctx, isl_error_unknown,
3759 "map detected as fixed", return -1);
3762 int test_fixed(isl_ctx *ctx)
3764 const char *str;
3765 isl_map *map;
3767 str = "{ [i] -> [i] }";
3768 map = isl_map_read_from_str(ctx, str);
3769 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3770 return -1;
3771 str = "{ [i] -> [1] }";
3772 map = isl_map_read_from_str(ctx, str);
3773 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3774 return -1;
3775 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3776 map = isl_map_read_from_str(ctx, str);
3777 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3778 return -1;
3779 map = isl_map_read_from_str(ctx, str);
3780 map = isl_map_neg(map);
3781 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3782 return -1;
3784 return 0;
3787 struct isl_vertices_test_data {
3788 const char *set;
3789 int n;
3790 const char *vertex[2];
3791 } vertices_tests[] = {
3792 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
3793 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
3794 { "{ A[t, i] : t = 14 and i = 1 }",
3795 1, { "{ A[14, 1] }" } },
3798 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
3800 static int find_vertex(__isl_take isl_vertex *vertex, void *user)
3802 struct isl_vertices_test_data *data = user;
3803 isl_ctx *ctx;
3804 isl_multi_aff *ma;
3805 isl_basic_set *bset;
3806 isl_pw_multi_aff *pma;
3807 int i;
3808 int equal;
3810 ctx = isl_vertex_get_ctx(vertex);
3811 bset = isl_vertex_get_domain(vertex);
3812 ma = isl_vertex_get_expr(vertex);
3813 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
3815 for (i = 0; i < data->n; ++i) {
3816 isl_pw_multi_aff *pma_i;
3818 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
3819 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
3820 isl_pw_multi_aff_free(pma_i);
3822 if (equal < 0 || equal)
3823 break;
3826 isl_pw_multi_aff_free(pma);
3827 isl_vertex_free(vertex);
3829 if (equal < 0)
3830 return -1;
3832 return equal ? 0 : - 1;
3835 int test_vertices(isl_ctx *ctx)
3837 int i;
3839 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
3840 isl_basic_set *bset;
3841 isl_vertices *vertices;
3842 int ok = 1;
3843 int n;
3845 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
3846 vertices = isl_basic_set_compute_vertices(bset);
3847 n = isl_vertices_get_n_vertices(vertices);
3848 if (vertices_tests[i].n != n)
3849 ok = 0;
3850 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
3851 &vertices_tests[i]) < 0)
3852 ok = 0;
3853 isl_vertices_free(vertices);
3854 isl_basic_set_free(bset);
3856 if (!vertices)
3857 return -1;
3858 if (!ok)
3859 isl_die(ctx, isl_error_unknown, "unexpected vertices",
3860 return -1);
3863 return 0;
3866 int test_union_pw(isl_ctx *ctx)
3868 int equal;
3869 const char *str;
3870 isl_union_set *uset;
3871 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
3873 str = "{ [x] -> x^2 }";
3874 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
3875 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
3876 uset = isl_union_pw_qpolynomial_domain(upwqp1);
3877 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
3878 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
3879 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
3880 isl_union_pw_qpolynomial_free(upwqp1);
3881 isl_union_pw_qpolynomial_free(upwqp2);
3882 if (equal < 0)
3883 return -1;
3884 if (!equal)
3885 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3887 return 0;
3890 int test_output(isl_ctx *ctx)
3892 char *s;
3893 const char *str;
3894 isl_pw_aff *pa;
3895 isl_printer *p;
3896 int equal;
3898 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
3899 pa = isl_pw_aff_read_from_str(ctx, str);
3901 p = isl_printer_to_str(ctx);
3902 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
3903 p = isl_printer_print_pw_aff(p, pa);
3904 s = isl_printer_get_str(p);
3905 isl_printer_free(p);
3906 isl_pw_aff_free(pa);
3907 if (!s)
3908 equal = -1;
3909 else
3910 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
3911 free(s);
3912 if (equal < 0)
3913 return -1;
3914 if (!equal)
3915 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3917 return 0;
3920 int test_sample(isl_ctx *ctx)
3922 const char *str;
3923 isl_basic_set *bset1, *bset2;
3924 int empty, subset;
3926 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3927 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3928 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3929 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3930 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3931 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3932 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3933 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3934 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3935 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3936 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3937 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3938 "d - 1073741821e and "
3939 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3940 "3j >= 1 - a + b + 2e and "
3941 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3942 "3i <= 4 - a + 4b - e and "
3943 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3944 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3945 "c <= -1 + a and 3i >= -2 - a + 3e and "
3946 "1073741822e <= 1073741823 - a + 1073741822b + c and "
3947 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3948 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3949 "1073741823e >= 1 + 1073741823b - d and "
3950 "3i >= 1073741823b + c - 1073741823e - f and "
3951 "3i >= 1 + 2b + e + 3g }";
3952 bset1 = isl_basic_set_read_from_str(ctx, str);
3953 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3954 empty = isl_basic_set_is_empty(bset2);
3955 subset = isl_basic_set_is_subset(bset2, bset1);
3956 isl_basic_set_free(bset1);
3957 isl_basic_set_free(bset2);
3958 if (empty < 0 || subset < 0)
3959 return -1;
3960 if (empty)
3961 isl_die(ctx, isl_error_unknown, "point not found", return -1);
3962 if (!subset)
3963 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
3965 return 0;
3968 int test_fixed_power(isl_ctx *ctx)
3970 const char *str;
3971 isl_map *map;
3972 isl_int exp;
3973 int equal;
3975 isl_int_init(exp);
3976 str = "{ [i] -> [i + 1] }";
3977 map = isl_map_read_from_str(ctx, str);
3978 isl_int_set_si(exp, 23);
3979 map = isl_map_fixed_power(map, exp);
3980 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
3981 isl_int_clear(exp);
3982 isl_map_free(map);
3983 if (equal < 0)
3984 return -1;
3986 return 0;
3989 int test_slice(isl_ctx *ctx)
3991 const char *str;
3992 isl_map *map;
3993 int equal;
3995 str = "{ [i] -> [j] }";
3996 map = isl_map_read_from_str(ctx, str);
3997 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
3998 equal = map_check_equal(map, "{ [i] -> [i] }");
3999 isl_map_free(map);
4000 if (equal < 0)
4001 return -1;
4003 str = "{ [i] -> [j] }";
4004 map = isl_map_read_from_str(ctx, str);
4005 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
4006 equal = map_check_equal(map, "{ [i] -> [j] }");
4007 isl_map_free(map);
4008 if (equal < 0)
4009 return -1;
4011 str = "{ [i] -> [j] }";
4012 map = isl_map_read_from_str(ctx, str);
4013 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
4014 equal = map_check_equal(map, "{ [i] -> [-i] }");
4015 isl_map_free(map);
4016 if (equal < 0)
4017 return -1;
4019 str = "{ [i] -> [j] }";
4020 map = isl_map_read_from_str(ctx, str);
4021 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
4022 equal = map_check_equal(map, "{ [0] -> [j] }");
4023 isl_map_free(map);
4024 if (equal < 0)
4025 return -1;
4027 str = "{ [i] -> [j] }";
4028 map = isl_map_read_from_str(ctx, str);
4029 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
4030 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
4031 isl_map_free(map);
4032 if (equal < 0)
4033 return -1;
4035 str = "{ [i] -> [j] }";
4036 map = isl_map_read_from_str(ctx, str);
4037 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
4038 equal = map_check_equal(map, "{ [i] -> [j] : false }");
4039 isl_map_free(map);
4040 if (equal < 0)
4041 return -1;
4043 return 0;
4046 int test_eliminate(isl_ctx *ctx)
4048 const char *str;
4049 isl_map *map;
4050 int equal;
4052 str = "{ [i] -> [j] : i = 2j }";
4053 map = isl_map_read_from_str(ctx, str);
4054 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
4055 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
4056 isl_map_free(map);
4057 if (equal < 0)
4058 return -1;
4060 return 0;
4063 /* Check that isl_set_dim_residue_class detects that the values of j
4064 * in the set below are all odd and that it does not detect any spurious
4065 * strides.
4067 static int test_residue_class(isl_ctx *ctx)
4069 const char *str;
4070 isl_set *set;
4071 isl_int m, r;
4072 int res;
4074 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
4075 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
4076 set = isl_set_read_from_str(ctx, str);
4077 isl_int_init(m);
4078 isl_int_init(r);
4079 res = isl_set_dim_residue_class(set, 1, &m, &r);
4080 if (res >= 0 &&
4081 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
4082 isl_die(ctx, isl_error_unknown, "incorrect residue class",
4083 res = -1);
4084 isl_int_clear(r);
4085 isl_int_clear(m);
4086 isl_set_free(set);
4088 return res;
4091 int test_align_parameters(isl_ctx *ctx)
4093 const char *str;
4094 isl_space *space;
4095 isl_multi_aff *ma1, *ma2;
4096 int equal;
4098 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
4099 ma1 = isl_multi_aff_read_from_str(ctx, str);
4101 space = isl_space_params_alloc(ctx, 1);
4102 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
4103 ma1 = isl_multi_aff_align_params(ma1, space);
4105 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
4106 ma2 = isl_multi_aff_read_from_str(ctx, str);
4108 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4110 isl_multi_aff_free(ma1);
4111 isl_multi_aff_free(ma2);
4113 if (equal < 0)
4114 return -1;
4115 if (!equal)
4116 isl_die(ctx, isl_error_unknown,
4117 "result not as expected", return -1);
4119 return 0;
4122 static int test_list(isl_ctx *ctx)
4124 isl_id *a, *b, *c, *d, *id;
4125 isl_id_list *list;
4126 int ok;
4128 a = isl_id_alloc(ctx, "a", NULL);
4129 b = isl_id_alloc(ctx, "b", NULL);
4130 c = isl_id_alloc(ctx, "c", NULL);
4131 d = isl_id_alloc(ctx, "d", NULL);
4133 list = isl_id_list_alloc(ctx, 4);
4134 list = isl_id_list_add(list, a);
4135 list = isl_id_list_add(list, b);
4136 list = isl_id_list_add(list, c);
4137 list = isl_id_list_add(list, d);
4138 list = isl_id_list_drop(list, 1, 1);
4140 if (isl_id_list_n_id(list) != 3) {
4141 isl_id_list_free(list);
4142 isl_die(ctx, isl_error_unknown,
4143 "unexpected number of elements in list", return -1);
4146 id = isl_id_list_get_id(list, 0);
4147 ok = id == a;
4148 isl_id_free(id);
4149 id = isl_id_list_get_id(list, 1);
4150 ok = ok && id == c;
4151 isl_id_free(id);
4152 id = isl_id_list_get_id(list, 2);
4153 ok = ok && id == d;
4154 isl_id_free(id);
4156 isl_id_list_free(list);
4158 if (!ok)
4159 isl_die(ctx, isl_error_unknown,
4160 "unexpected elements in list", return -1);
4162 return 0;
4165 const char *set_conversion_tests[] = {
4166 "[N] -> { [i] : N - 1 <= 2 i <= N }",
4167 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
4168 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4169 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4170 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
4173 /* Check that converting from isl_set to isl_pw_multi_aff and back
4174 * to isl_set produces the original isl_set.
4176 static int test_set_conversion(isl_ctx *ctx)
4178 int i;
4179 const char *str;
4180 isl_set *set1, *set2;
4181 isl_pw_multi_aff *pma;
4182 int equal;
4184 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
4185 str = set_conversion_tests[i];
4186 set1 = isl_set_read_from_str(ctx, str);
4187 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
4188 set2 = isl_set_from_pw_multi_aff(pma);
4189 equal = isl_set_is_equal(set1, set2);
4190 isl_set_free(set1);
4191 isl_set_free(set2);
4193 if (equal < 0)
4194 return -1;
4195 if (!equal)
4196 isl_die(ctx, isl_error_unknown, "bad conversion",
4197 return -1);
4200 return 0;
4203 /* Check that converting from isl_map to isl_pw_multi_aff and back
4204 * to isl_map produces the original isl_map.
4206 static int test_map_conversion(isl_ctx *ctx)
4208 const char *str;
4209 isl_map *map1, *map2;
4210 isl_pw_multi_aff *pma;
4211 int equal;
4213 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
4214 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
4215 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
4216 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
4217 "9e <= -2 - 2a) }";
4218 map1 = isl_map_read_from_str(ctx, str);
4219 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
4220 map2 = isl_map_from_pw_multi_aff(pma);
4221 equal = isl_map_is_equal(map1, map2);
4222 isl_map_free(map1);
4223 isl_map_free(map2);
4225 if (equal < 0)
4226 return -1;
4227 if (!equal)
4228 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
4230 return 0;
4233 static int test_conversion(isl_ctx *ctx)
4235 if (test_set_conversion(ctx) < 0)
4236 return -1;
4237 if (test_map_conversion(ctx) < 0)
4238 return -1;
4239 return 0;
4242 /* Check that isl_basic_map_curry does not modify input.
4244 static int test_curry(isl_ctx *ctx)
4246 const char *str;
4247 isl_basic_map *bmap1, *bmap2;
4248 int equal;
4250 str = "{ [A[] -> B[]] -> C[] }";
4251 bmap1 = isl_basic_map_read_from_str(ctx, str);
4252 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
4253 equal = isl_basic_map_is_equal(bmap1, bmap2);
4254 isl_basic_map_free(bmap1);
4255 isl_basic_map_free(bmap2);
4257 if (equal < 0)
4258 return -1;
4259 if (equal)
4260 isl_die(ctx, isl_error_unknown,
4261 "curried map should not be equal to original",
4262 return -1);
4264 return 0;
4267 struct {
4268 const char *set;
4269 const char *ma;
4270 const char *res;
4271 } preimage_tests[] = {
4272 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
4273 "{ A[j,i] -> B[i,j] }",
4274 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
4275 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4276 "{ A[a,b] -> B[a/2,b/6] }",
4277 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
4278 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4279 "{ A[a,b] -> B[a/2,b/6] }",
4280 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
4281 "exists i,j : a = 2 i and b = 6 j }" },
4282 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
4283 "[n] -> { : 0 <= n <= 100 }" },
4284 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4285 "{ A[a] -> B[2a] }",
4286 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
4287 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4288 "{ A[a] -> B[([a/2])] }",
4289 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
4290 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
4291 "{ A[a] -> B[a,a,a/3] }",
4292 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
4293 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
4294 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
4297 static int test_preimage_basic_set(isl_ctx *ctx)
4299 int i;
4300 isl_basic_set *bset1, *bset2;
4301 isl_multi_aff *ma;
4302 int equal;
4304 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
4305 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
4306 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
4307 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
4308 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
4309 equal = isl_basic_set_is_equal(bset1, bset2);
4310 isl_basic_set_free(bset1);
4311 isl_basic_set_free(bset2);
4312 if (equal < 0)
4313 return -1;
4314 if (!equal)
4315 isl_die(ctx, isl_error_unknown, "bad preimage",
4316 return -1);
4319 return 0;
4322 struct {
4323 const char *map;
4324 const char *ma;
4325 const char *res;
4326 } preimage_domain_tests[] = {
4327 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
4328 "{ A[j,i] -> B[i,j] }",
4329 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
4330 { "{ B[i] -> C[i]; D[i] -> E[i] }",
4331 "{ A[i] -> B[i + 1] }",
4332 "{ A[i] -> C[i + 1] }" },
4333 { "{ B[i] -> C[i]; B[i] -> E[i] }",
4334 "{ A[i] -> B[i + 1] }",
4335 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
4336 { "{ B[i] -> C[([i/2])] }",
4337 "{ A[i] -> B[2i] }",
4338 "{ A[i] -> C[i] }" },
4339 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
4340 "{ A[i] -> B[([i/5]), ([i/7])] }",
4341 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
4342 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
4343 "[N] -> { A[] -> B[([N/5])] }",
4344 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
4345 { "{ B[i] -> C[i] : exists a : i = 5 a }",
4346 "{ A[i] -> B[2i] }",
4347 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
4348 { "{ B[i] -> C[i] : exists a : i = 2 a; "
4349 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
4350 "{ A[i] -> B[2i] }",
4351 "{ A[i] -> C[2i] }" },
4354 static int test_preimage_union_map(isl_ctx *ctx)
4356 int i;
4357 isl_union_map *umap1, *umap2;
4358 isl_multi_aff *ma;
4359 int equal;
4361 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
4362 umap1 = isl_union_map_read_from_str(ctx,
4363 preimage_domain_tests[i].map);
4364 ma = isl_multi_aff_read_from_str(ctx,
4365 preimage_domain_tests[i].ma);
4366 umap2 = isl_union_map_read_from_str(ctx,
4367 preimage_domain_tests[i].res);
4368 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
4369 equal = isl_union_map_is_equal(umap1, umap2);
4370 isl_union_map_free(umap1);
4371 isl_union_map_free(umap2);
4372 if (equal < 0)
4373 return -1;
4374 if (!equal)
4375 isl_die(ctx, isl_error_unknown, "bad preimage",
4376 return -1);
4379 return 0;
4382 static int test_preimage(isl_ctx *ctx)
4384 if (test_preimage_basic_set(ctx) < 0)
4385 return -1;
4386 if (test_preimage_union_map(ctx) < 0)
4387 return -1;
4389 return 0;
4392 struct {
4393 const char *ma1;
4394 const char *ma;
4395 const char *res;
4396 } pullback_tests[] = {
4397 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
4398 "{ A[a,b] -> C[b + 2a] }" },
4399 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
4400 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
4401 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
4402 "{ A[a] -> C[(a)/6] }" },
4403 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
4404 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
4405 "{ A[a] -> C[(2a)/3] }" },
4406 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
4407 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
4408 "{ A[i,j] -> C[i + j, i + j] }"},
4409 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
4410 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
4411 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
4414 static int test_pullback(isl_ctx *ctx)
4416 int i;
4417 isl_multi_aff *ma1, *ma2;
4418 isl_multi_aff *ma;
4419 int equal;
4421 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
4422 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
4423 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
4424 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
4425 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
4426 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4427 isl_multi_aff_free(ma1);
4428 isl_multi_aff_free(ma2);
4429 if (equal < 0)
4430 return -1;
4431 if (!equal)
4432 isl_die(ctx, isl_error_unknown, "bad pullback",
4433 return -1);
4436 return 0;
4439 /* Check that negation is printed correctly.
4441 static int test_ast(isl_ctx *ctx)
4443 isl_ast_expr *expr, *expr1, *expr2, *expr3;
4444 char *str;
4445 int ok;
4447 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4448 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4449 expr = isl_ast_expr_add(expr1, expr2);
4450 expr = isl_ast_expr_neg(expr);
4451 str = isl_ast_expr_to_str(expr);
4452 ok = str ? !strcmp(str, "-(A + B)") : -1;
4453 free(str);
4454 isl_ast_expr_free(expr);
4456 if (ok < 0)
4457 return -1;
4458 if (!ok)
4459 isl_die(ctx, isl_error_unknown,
4460 "isl_ast_expr printed incorrectly", return -1);
4462 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4463 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4464 expr = isl_ast_expr_add(expr1, expr2);
4465 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
4466 expr = isl_ast_expr_sub(expr3, expr);
4467 str = isl_ast_expr_to_str(expr);
4468 ok = str ? !strcmp(str, "C - (A + B)") : -1;
4469 free(str);
4470 isl_ast_expr_free(expr);
4472 if (ok < 0)
4473 return -1;
4474 if (!ok)
4475 isl_die(ctx, isl_error_unknown,
4476 "isl_ast_expr printed incorrectly", return -1);
4478 return 0;
4481 /* Check that isl_ast_build_expr_from_set returns a valid expression
4482 * for an empty set. Note that isl_ast_build_expr_from_set getting
4483 * called on an empty set probably indicates a bug in the caller.
4485 static int test_ast_build(isl_ctx *ctx)
4487 isl_set *set;
4488 isl_ast_build *build;
4489 isl_ast_expr *expr;
4491 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4492 build = isl_ast_build_from_context(set);
4494 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
4495 expr = isl_ast_build_expr_from_set(build, set);
4497 isl_ast_expr_free(expr);
4498 isl_ast_build_free(build);
4500 if (!expr)
4501 return -1;
4503 return 0;
4506 /* Internal data structure for before_for and after_for callbacks.
4508 * depth is the current depth
4509 * before is the number of times before_for has been called
4510 * after is the number of times after_for has been called
4512 struct isl_test_codegen_data {
4513 int depth;
4514 int before;
4515 int after;
4518 /* This function is called before each for loop in the AST generated
4519 * from test_ast_gen1.
4521 * Increment the number of calls and the depth.
4522 * Check that the space returned by isl_ast_build_get_schedule_space
4523 * matches the target space of the schedule returned by
4524 * isl_ast_build_get_schedule.
4525 * Return an isl_id that is checked by the corresponding call
4526 * to after_for.
4528 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
4529 void *user)
4531 struct isl_test_codegen_data *data = user;
4532 isl_ctx *ctx;
4533 isl_space *space;
4534 isl_union_map *schedule;
4535 isl_union_set *uset;
4536 isl_set *set;
4537 int empty;
4538 char name[] = "d0";
4540 ctx = isl_ast_build_get_ctx(build);
4542 if (data->before >= 3)
4543 isl_die(ctx, isl_error_unknown,
4544 "unexpected number of for nodes", return NULL);
4545 if (data->depth >= 2)
4546 isl_die(ctx, isl_error_unknown,
4547 "unexpected depth", return NULL);
4549 snprintf(name, sizeof(name), "d%d", data->depth);
4550 data->before++;
4551 data->depth++;
4553 schedule = isl_ast_build_get_schedule(build);
4554 uset = isl_union_map_range(schedule);
4555 if (!uset)
4556 return NULL;
4557 if (isl_union_set_n_set(uset) != 1) {
4558 isl_union_set_free(uset);
4559 isl_die(ctx, isl_error_unknown,
4560 "expecting single range space", return NULL);
4563 space = isl_ast_build_get_schedule_space(build);
4564 set = isl_union_set_extract_set(uset, space);
4565 isl_union_set_free(uset);
4566 empty = isl_set_is_empty(set);
4567 isl_set_free(set);
4569 if (empty < 0)
4570 return NULL;
4571 if (empty)
4572 isl_die(ctx, isl_error_unknown,
4573 "spaces don't match", return NULL);
4575 return isl_id_alloc(ctx, name, NULL);
4578 /* This function is called after each for loop in the AST generated
4579 * from test_ast_gen1.
4581 * Increment the number of calls and decrement the depth.
4582 * Check that the annotation attached to the node matches
4583 * the isl_id returned by the corresponding call to before_for.
4585 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
4586 __isl_keep isl_ast_build *build, void *user)
4588 struct isl_test_codegen_data *data = user;
4589 isl_id *id;
4590 const char *name;
4591 int valid;
4593 data->after++;
4594 data->depth--;
4596 if (data->after > data->before)
4597 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4598 "mismatch in number of for nodes",
4599 return isl_ast_node_free(node));
4601 id = isl_ast_node_get_annotation(node);
4602 if (!id)
4603 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4604 "missing annotation", return isl_ast_node_free(node));
4606 name = isl_id_get_name(id);
4607 valid = name && atoi(name + 1) == data->depth;
4608 isl_id_free(id);
4610 if (!valid)
4611 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4612 "wrong annotation", return isl_ast_node_free(node));
4614 return node;
4617 /* Check that the before_each_for and after_each_for callbacks
4618 * are called for each for loop in the generated code,
4619 * that they are called in the right order and that the isl_id
4620 * returned from the before_each_for callback is attached to
4621 * the isl_ast_node passed to the corresponding after_each_for call.
4623 static int test_ast_gen1(isl_ctx *ctx)
4625 const char *str;
4626 isl_set *set;
4627 isl_union_map *schedule;
4628 isl_ast_build *build;
4629 isl_ast_node *tree;
4630 struct isl_test_codegen_data data;
4632 str = "[N] -> { : N >= 10 }";
4633 set = isl_set_read_from_str(ctx, str);
4634 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
4635 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
4636 schedule = isl_union_map_read_from_str(ctx, str);
4638 data.before = 0;
4639 data.after = 0;
4640 data.depth = 0;
4641 build = isl_ast_build_from_context(set);
4642 build = isl_ast_build_set_before_each_for(build,
4643 &before_for, &data);
4644 build = isl_ast_build_set_after_each_for(build,
4645 &after_for, &data);
4646 tree = isl_ast_build_ast_from_schedule(build, schedule);
4647 isl_ast_build_free(build);
4648 if (!tree)
4649 return -1;
4651 isl_ast_node_free(tree);
4653 if (data.before != 3 || data.after != 3)
4654 isl_die(ctx, isl_error_unknown,
4655 "unexpected number of for nodes", return -1);
4657 return 0;
4660 /* Check that the AST generator handles domains that are integrally disjoint
4661 * but not ratinoally disjoint.
4663 static int test_ast_gen2(isl_ctx *ctx)
4665 const char *str;
4666 isl_set *set;
4667 isl_union_map *schedule;
4668 isl_union_map *options;
4669 isl_ast_build *build;
4670 isl_ast_node *tree;
4672 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
4673 schedule = isl_union_map_read_from_str(ctx, str);
4674 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4675 build = isl_ast_build_from_context(set);
4677 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
4678 options = isl_union_map_read_from_str(ctx, str);
4679 build = isl_ast_build_set_options(build, options);
4680 tree = isl_ast_build_ast_from_schedule(build, schedule);
4681 isl_ast_build_free(build);
4682 if (!tree)
4683 return -1;
4684 isl_ast_node_free(tree);
4686 return 0;
4689 /* Increment *user on each call.
4691 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
4692 __isl_keep isl_ast_build *build, void *user)
4694 int *n = user;
4696 (*n)++;
4698 return node;
4701 /* Test that unrolling tries to minimize the number of instances.
4702 * In particular, for the schedule given below, make sure it generates
4703 * 3 nodes (rather than 101).
4705 static int test_ast_gen3(isl_ctx *ctx)
4707 const char *str;
4708 isl_set *set;
4709 isl_union_map *schedule;
4710 isl_union_map *options;
4711 isl_ast_build *build;
4712 isl_ast_node *tree;
4713 int n_domain = 0;
4715 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
4716 schedule = isl_union_map_read_from_str(ctx, str);
4717 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4719 str = "{ [i] -> unroll[0] }";
4720 options = isl_union_map_read_from_str(ctx, str);
4722 build = isl_ast_build_from_context(set);
4723 build = isl_ast_build_set_options(build, options);
4724 build = isl_ast_build_set_at_each_domain(build,
4725 &count_domains, &n_domain);
4726 tree = isl_ast_build_ast_from_schedule(build, schedule);
4727 isl_ast_build_free(build);
4728 if (!tree)
4729 return -1;
4731 isl_ast_node_free(tree);
4733 if (n_domain != 3)
4734 isl_die(ctx, isl_error_unknown,
4735 "unexpected number of for nodes", return -1);
4737 return 0;
4740 /* Check that if the ast_build_exploit_nested_bounds options is set,
4741 * we do not get an outer if node in the generated AST,
4742 * while we do get such an outer if node if the options is not set.
4744 static int test_ast_gen4(isl_ctx *ctx)
4746 const char *str;
4747 isl_set *set;
4748 isl_union_map *schedule;
4749 isl_ast_build *build;
4750 isl_ast_node *tree;
4751 enum isl_ast_node_type type;
4752 int enb;
4754 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4755 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4757 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4759 schedule = isl_union_map_read_from_str(ctx, str);
4760 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4761 build = isl_ast_build_from_context(set);
4762 tree = isl_ast_build_ast_from_schedule(build, schedule);
4763 isl_ast_build_free(build);
4764 if (!tree)
4765 return -1;
4767 type = isl_ast_node_get_type(tree);
4768 isl_ast_node_free(tree);
4770 if (type == isl_ast_node_if)
4771 isl_die(ctx, isl_error_unknown,
4772 "not expecting if node", return -1);
4774 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
4776 schedule = isl_union_map_read_from_str(ctx, str);
4777 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4778 build = isl_ast_build_from_context(set);
4779 tree = isl_ast_build_ast_from_schedule(build, schedule);
4780 isl_ast_build_free(build);
4781 if (!tree)
4782 return -1;
4784 type = isl_ast_node_get_type(tree);
4785 isl_ast_node_free(tree);
4787 if (type != isl_ast_node_if)
4788 isl_die(ctx, isl_error_unknown,
4789 "expecting if node", return -1);
4791 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
4793 return 0;
4796 /* This function is called for each leaf in the AST generated
4797 * from test_ast_gen5.
4799 * We finalize the AST generation by extending the outer schedule
4800 * with a zero-dimensional schedule. If this results in any for loops,
4801 * then this means that we did not pass along enough information
4802 * about the outer schedule to the inner AST generation.
4804 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
4805 void *user)
4807 isl_union_map *schedule, *extra;
4808 isl_ast_node *tree;
4810 schedule = isl_ast_build_get_schedule(build);
4811 extra = isl_union_map_copy(schedule);
4812 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
4813 schedule = isl_union_map_range_product(schedule, extra);
4814 tree = isl_ast_build_ast_from_schedule(build, schedule);
4815 isl_ast_build_free(build);
4817 if (!tree)
4818 return NULL;
4820 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
4821 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
4822 "code should not contain any for loop",
4823 return isl_ast_node_free(tree));
4825 return tree;
4828 /* Check that we do not lose any information when going back and
4829 * forth between internal and external schedule.
4831 * In particular, we create an AST where we unroll the only
4832 * non-constant dimension in the schedule. We therefore do
4833 * not expect any for loops in the AST. However, older versions
4834 * of isl would not pass along enough information about the outer
4835 * schedule when performing an inner code generation from a create_leaf
4836 * callback, resulting in the inner code generation producing a for loop.
4838 static int test_ast_gen5(isl_ctx *ctx)
4840 const char *str;
4841 isl_set *set;
4842 isl_union_map *schedule, *options;
4843 isl_ast_build *build;
4844 isl_ast_node *tree;
4846 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
4847 schedule = isl_union_map_read_from_str(ctx, str);
4849 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
4850 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
4851 options = isl_union_map_read_from_str(ctx, str);
4853 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4854 build = isl_ast_build_from_context(set);
4855 build = isl_ast_build_set_options(build, options);
4856 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
4857 tree = isl_ast_build_ast_from_schedule(build, schedule);
4858 isl_ast_build_free(build);
4859 isl_ast_node_free(tree);
4860 if (!tree)
4861 return -1;
4863 return 0;
4866 static int test_ast_gen(isl_ctx *ctx)
4868 if (test_ast_gen1(ctx) < 0)
4869 return -1;
4870 if (test_ast_gen2(ctx) < 0)
4871 return -1;
4872 if (test_ast_gen3(ctx) < 0)
4873 return -1;
4874 if (test_ast_gen4(ctx) < 0)
4875 return -1;
4876 if (test_ast_gen5(ctx) < 0)
4877 return -1;
4878 return 0;
4881 /* Check if dropping output dimensions from an isl_pw_multi_aff
4882 * works properly.
4884 static int test_pw_multi_aff(isl_ctx *ctx)
4886 const char *str;
4887 isl_pw_multi_aff *pma1, *pma2;
4888 int equal;
4890 str = "{ [i,j] -> [i+j, 4i-j] }";
4891 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4892 str = "{ [i,j] -> [4i-j] }";
4893 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4895 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
4897 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
4899 isl_pw_multi_aff_free(pma1);
4900 isl_pw_multi_aff_free(pma2);
4901 if (equal < 0)
4902 return -1;
4903 if (!equal)
4904 isl_die(ctx, isl_error_unknown,
4905 "expressions not equal", return -1);
4907 return 0;
4910 /* Check that we can properly parse multi piecewise affine expressions
4911 * where the piecewise affine expressions have different domains.
4913 static int test_multi_pw_aff(isl_ctx *ctx)
4915 const char *str;
4916 isl_set *dom, *dom2;
4917 isl_multi_pw_aff *mpa1, *mpa2;
4918 isl_pw_aff *pa;
4919 int equal;
4920 int equal_domain;
4922 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
4923 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
4924 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
4925 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
4926 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
4927 str = "{ [i] -> [(i : i > 0), 2i] }";
4928 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
4930 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
4932 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
4933 dom = isl_pw_aff_domain(pa);
4934 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
4935 dom2 = isl_pw_aff_domain(pa);
4936 equal_domain = isl_set_is_equal(dom, dom2);
4938 isl_set_free(dom);
4939 isl_set_free(dom2);
4940 isl_multi_pw_aff_free(mpa1);
4941 isl_multi_pw_aff_free(mpa2);
4943 if (equal < 0)
4944 return -1;
4945 if (!equal)
4946 isl_die(ctx, isl_error_unknown,
4947 "expressions not equal", return -1);
4949 if (equal_domain < 0)
4950 return -1;
4951 if (equal_domain)
4952 isl_die(ctx, isl_error_unknown,
4953 "domains unexpectedly equal", return -1);
4955 return 0;
4958 /* This is a regression test for a bug where isl_basic_map_simplify
4959 * would end up in an infinite loop. In particular, we construct
4960 * an empty basic set that is not obviously empty.
4961 * isl_basic_set_is_empty marks the basic set as empty.
4962 * After projecting out i3, the variable can be dropped completely,
4963 * but isl_basic_map_simplify refrains from doing so if the basic set
4964 * is empty and would end up in an infinite loop if it didn't test
4965 * explicitly for empty basic maps in the outer loop.
4967 static int test_simplify(isl_ctx *ctx)
4969 const char *str;
4970 isl_basic_set *bset;
4971 int empty;
4973 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
4974 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
4975 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
4976 "i3 >= i2 }";
4977 bset = isl_basic_set_read_from_str(ctx, str);
4978 empty = isl_basic_set_is_empty(bset);
4979 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
4980 isl_basic_set_free(bset);
4981 if (!bset)
4982 return -1;
4983 if (!empty)
4984 isl_die(ctx, isl_error_unknown,
4985 "basic set should be empty", return -1);
4987 return 0;
4990 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
4991 * with gbr context would fail to disable the use of the shifted tableau
4992 * when transferring equalities for the input to the context, resulting
4993 * in invalid sample values.
4995 static int test_partial_lexmin(isl_ctx *ctx)
4997 const char *str;
4998 isl_basic_set *bset;
4999 isl_basic_map *bmap;
5000 isl_map *map;
5002 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
5003 bmap = isl_basic_map_read_from_str(ctx, str);
5004 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
5005 bset = isl_basic_set_read_from_str(ctx, str);
5006 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
5007 isl_map_free(map);
5009 if (!map)
5010 return -1;
5012 return 0;
5015 /* Check that the variable compression performed on the existentially
5016 * quantified variables inside isl_basic_set_compute_divs is not confused
5017 * by the implicit equalities among the parameters.
5019 static int test_compute_divs(isl_ctx *ctx)
5021 const char *str;
5022 isl_basic_set *bset;
5023 isl_set *set;
5025 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
5026 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
5027 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
5028 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
5029 bset = isl_basic_set_read_from_str(ctx, str);
5030 set = isl_basic_set_compute_divs(bset);
5031 isl_set_free(set);
5032 if (!set)
5033 return -1;
5035 return 0;
5038 struct {
5039 const char *set;
5040 const char *dual;
5041 } coef_tests[] = {
5042 { "{ rat: [i] : 0 <= i <= 10 }",
5043 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
5044 { "{ rat: [i] : FALSE }",
5045 "{ rat: coefficients[[cst] -> [a]] }" },
5046 { "{ rat: [i] : }",
5047 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
5050 struct {
5051 const char *set;
5052 const char *dual;
5053 } sol_tests[] = {
5054 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
5055 "{ rat: [i] : 0 <= i <= 10 }" },
5056 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
5057 "{ rat: [i] }" },
5058 { "{ rat: coefficients[[cst] -> [a]] }",
5059 "{ rat: [i] : FALSE }" },
5062 /* Test the basic functionality of isl_basic_set_coefficients and
5063 * isl_basic_set_solutions.
5065 static int test_dual(isl_ctx *ctx)
5067 int i;
5069 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
5070 int equal;
5071 isl_basic_set *bset1, *bset2;
5073 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
5074 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
5075 bset1 = isl_basic_set_coefficients(bset1);
5076 equal = isl_basic_set_is_equal(bset1, bset2);
5077 isl_basic_set_free(bset1);
5078 isl_basic_set_free(bset2);
5079 if (equal < 0)
5080 return -1;
5081 if (!equal)
5082 isl_die(ctx, isl_error_unknown,
5083 "incorrect dual", return -1);
5086 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
5087 int equal;
5088 isl_basic_set *bset1, *bset2;
5090 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
5091 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
5092 bset1 = isl_basic_set_solutions(bset1);
5093 equal = isl_basic_set_is_equal(bset1, bset2);
5094 isl_basic_set_free(bset1);
5095 isl_basic_set_free(bset2);
5096 if (equal < 0)
5097 return -1;
5098 if (!equal)
5099 isl_die(ctx, isl_error_unknown,
5100 "incorrect dual", return -1);
5103 return 0;
5106 struct {
5107 const char *name;
5108 int (*fn)(isl_ctx *ctx);
5109 } tests [] = {
5110 { "dual", &test_dual },
5111 { "dependence analysis", &test_flow },
5112 { "val", &test_val },
5113 { "compute divs", &test_compute_divs },
5114 { "partial lexmin", &test_partial_lexmin },
5115 { "simplify", &test_simplify },
5116 { "curry", &test_curry },
5117 { "piecewise multi affine expressions", &test_pw_multi_aff },
5118 { "multi piecewise affine expressions", &test_multi_pw_aff },
5119 { "conversion", &test_conversion },
5120 { "list", &test_list },
5121 { "align parameters", &test_align_parameters },
5122 { "preimage", &test_preimage },
5123 { "pullback", &test_pullback },
5124 { "AST", &test_ast },
5125 { "AST build", &test_ast_build },
5126 { "AST generation", &test_ast_gen },
5127 { "eliminate", &test_eliminate },
5128 { "residue class", &test_residue_class },
5129 { "div", &test_div },
5130 { "slice", &test_slice },
5131 { "fixed power", &test_fixed_power },
5132 { "sample", &test_sample },
5133 { "output", &test_output },
5134 { "vertices", &test_vertices },
5135 { "fixed", &test_fixed },
5136 { "equal", &test_equal },
5137 { "disjoint", &test_disjoint },
5138 { "product", &test_product },
5139 { "dim_max", &test_dim_max },
5140 { "affine", &test_aff },
5141 { "injective", &test_injective },
5142 { "schedule", &test_schedule },
5143 { "union_pw", &test_union_pw },
5144 { "parse", &test_parse },
5145 { "single-valued", &test_sv },
5146 { "affine hull", &test_affine_hull },
5147 { "coalesce", &test_coalesce },
5148 { "factorize", &test_factorize },
5149 { "subset", &test_subset },
5150 { "subtract", &test_subtract },
5151 { "lexmin", &test_lexmin },
5152 { "min", &test_min },
5153 { "gist", &test_gist },
5154 { "piecewise quasi-polynomials", &test_pwqp },
5157 int main(int argc, char **argv)
5159 int i;
5160 struct isl_ctx *ctx;
5161 struct isl_options *options;
5163 srcdir = getenv("srcdir");
5164 assert(srcdir);
5166 options = isl_options_new_with_defaults();
5167 assert(options);
5168 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
5170 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
5171 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
5172 printf("%s\n", tests[i].name);
5173 if (tests[i].fn(ctx) < 0)
5174 goto error;
5176 test_lift(ctx);
5177 test_bound(ctx);
5178 test_union(ctx);
5179 test_split_periods(ctx);
5180 test_lex(ctx);
5181 test_bijective(ctx);
5182 test_dep(ctx);
5183 test_read(ctx);
5184 test_bounded(ctx);
5185 test_construction(ctx);
5186 test_dim(ctx);
5187 test_application(ctx);
5188 test_convex_hull(ctx);
5189 test_closure(ctx);
5190 isl_ctx_free(ctx);
5191 return 0;
5192 error:
5193 isl_ctx_free(ctx);
5194 return -1;