2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_aff_private.h>
18 #include <isl/constraint.h>
19 #include <isl/polynomial.h>
20 #include <isl/union_map.h>
21 #include <isl_factorization.h>
22 #include <isl/schedule.h>
23 #include <isl_options_private.h>
24 #include <isl/vertices.h>
25 #include <isl/ast_build.h>
27 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
31 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
34 char *pattern = "%s/test_inputs/%s.%s";
36 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
38 filename = isl_alloc_array(ctx, char, length);
43 sprintf(filename, pattern, srcdir, name, suffix);
48 void test_parse_map(isl_ctx *ctx, const char *str)
52 map = isl_map_read_from_str(ctx, str);
57 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
62 map = isl_map_read_from_str(ctx, str);
63 map2 = isl_map_read_from_str(ctx, str2);
64 equal = isl_map_is_equal(map, map2);
71 isl_die(ctx, isl_error_unknown, "maps not equal",
77 void test_parse_pwqp(isl_ctx *ctx, const char *str)
79 isl_pw_qpolynomial *pwqp;
81 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
83 isl_pw_qpolynomial_free(pwqp);
86 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
90 pwaff = isl_pw_aff_read_from_str(ctx, str);
92 isl_pw_aff_free(pwaff);
95 int test_parse(struct isl_ctx *ctx)
98 const char *str, *str2;
100 str = "{ [i] -> [-i] }";
101 map = isl_map_read_from_str(ctx, str);
105 str = "{ A[i] -> L[([i/3])] }";
106 map = isl_map_read_from_str(ctx, str);
110 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
111 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
112 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
114 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
115 str2 = "{ [x, y] : 2y >= 6 - x }";
116 if (test_parse_map_equal(ctx, str, str2) < 0)
119 if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
120 "{ [x,y] : x <= y, 2*y + 3 }") < 0)
122 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
123 if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
127 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
128 map = isl_map_read_from_str(ctx, str);
129 str = "{ [new, old] -> [o0, o1] : "
130 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
131 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
132 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
133 map2 = isl_map_read_from_str(ctx, str);
134 assert(isl_map_is_equal(map, map2));
138 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
139 map = isl_map_read_from_str(ctx, str);
140 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
141 map2 = isl_map_read_from_str(ctx, str);
142 assert(isl_map_is_equal(map, map2));
146 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
147 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
148 if (test_parse_map_equal(ctx, str, str2) < 0)
151 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
152 str2 = "{ [i,j] -> [min(i,j)] }";
153 if (test_parse_map_equal(ctx, str, str2) < 0)
156 str = "{ [i,j] : i != j }";
157 str2 = "{ [i,j] : i < j or i > j }";
158 if (test_parse_map_equal(ctx, str, str2) < 0)
161 str = "{ [i,j] : (i+1)*2 >= j }";
162 str2 = "{ [i, j] : j <= 2 + 2i }";
163 if (test_parse_map_equal(ctx, str, str2) < 0)
166 str = "{ [i] -> [i > 0 ? 4 : 5] }";
167 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
168 if (test_parse_map_equal(ctx, str, str2) < 0)
171 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
172 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
173 if (test_parse_map_equal(ctx, str, str2) < 0)
176 str = "{ [x] : x >= 0 }";
177 str2 = "{ [x] : x-0 >= 0 }";
178 if (test_parse_map_equal(ctx, str, str2) < 0)
181 str = "{ [i] : ((i > 10)) }";
182 str2 = "{ [i] : i >= 11 }";
183 if (test_parse_map_equal(ctx, str, str2) < 0)
186 str = "{ [i] -> [0] }";
187 str2 = "{ [i] -> [0 * i] }";
188 if (test_parse_map_equal(ctx, str, str2) < 0)
191 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
192 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
193 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
194 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
196 if (test_parse_map_equal(ctx, "{ [a] -> [b] : (not false) }",
197 "{ [a] -> [b] : true }") < 0)
200 if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
201 "{ [i] : i <= 10 }") < 0)
204 if (test_parse_map_equal(ctx, "{Sym=[n] [i] : i <= n }",
205 "[n] -> { [i] : i <= n }") < 0)
208 if (test_parse_map_equal(ctx, "{ [*] }", "{ [a] }") < 0)
214 void test_read(struct isl_ctx *ctx)
218 struct isl_basic_set *bset1, *bset2;
219 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
221 filename = get_filename(ctx, "set", "omega");
223 input = fopen(filename, "r");
226 bset1 = isl_basic_set_read_from_file(ctx, input);
227 bset2 = isl_basic_set_read_from_str(ctx, str);
229 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
231 isl_basic_set_free(bset1);
232 isl_basic_set_free(bset2);
238 void test_bounded(struct isl_ctx *ctx)
243 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
244 bounded = isl_set_is_bounded(set);
248 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
249 bounded = isl_set_is_bounded(set);
253 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
254 bounded = isl_set_is_bounded(set);
259 /* Construct the basic set { [i] : 5 <= i <= N } */
260 void test_construction(struct isl_ctx *ctx)
265 struct isl_basic_set *bset;
266 struct isl_constraint *c;
270 dim = isl_space_set_alloc(ctx, 1, 1);
271 bset = isl_basic_set_universe(isl_space_copy(dim));
272 ls = isl_local_space_from_space(dim);
274 c = isl_inequality_alloc(isl_local_space_copy(ls));
275 isl_int_set_si(v, -1);
276 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
277 isl_int_set_si(v, 1);
278 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
279 bset = isl_basic_set_add_constraint(bset, c);
281 c = isl_inequality_alloc(isl_local_space_copy(ls));
282 isl_int_set_si(v, 1);
283 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
284 isl_int_set_si(v, -5);
285 isl_constraint_set_constant(c, v);
286 bset = isl_basic_set_add_constraint(bset, c);
288 isl_local_space_free(ls);
289 isl_basic_set_free(bset);
294 void test_dim(struct isl_ctx *ctx)
297 isl_map *map1, *map2;
299 map1 = isl_map_read_from_str(ctx,
300 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
301 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
302 map2 = isl_map_read_from_str(ctx,
303 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
304 assert(isl_map_is_equal(map1, map2));
307 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
308 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
309 assert(isl_map_is_equal(map1, map2));
314 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
315 map1 = isl_map_read_from_str(ctx, str);
316 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
317 map2 = isl_map_read_from_str(ctx, str);
318 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
319 assert(isl_map_is_equal(map1, map2));
325 static int test_div(isl_ctx *ctx)
334 struct isl_basic_set *bset;
335 struct isl_constraint *c;
340 dim = isl_space_set_alloc(ctx, 0, 3);
341 bset = isl_basic_set_universe(isl_space_copy(dim));
342 ls = isl_local_space_from_space(dim);
344 c = isl_equality_alloc(isl_local_space_copy(ls));
345 isl_int_set_si(v, -1);
346 isl_constraint_set_constant(c, v);
347 isl_int_set_si(v, 1);
348 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
349 isl_int_set_si(v, 3);
350 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
351 bset = isl_basic_set_add_constraint(bset, c);
353 c = isl_equality_alloc(isl_local_space_copy(ls));
354 isl_int_set_si(v, 1);
355 isl_constraint_set_constant(c, v);
356 isl_int_set_si(v, -1);
357 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
358 isl_int_set_si(v, 3);
359 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
360 bset = isl_basic_set_add_constraint(bset, c);
362 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
364 assert(bset && bset->n_div == 1);
365 isl_local_space_free(ls);
366 isl_basic_set_free(bset);
369 dim = isl_space_set_alloc(ctx, 0, 3);
370 bset = isl_basic_set_universe(isl_space_copy(dim));
371 ls = isl_local_space_from_space(dim);
373 c = isl_equality_alloc(isl_local_space_copy(ls));
374 isl_int_set_si(v, 1);
375 isl_constraint_set_constant(c, v);
376 isl_int_set_si(v, -1);
377 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
378 isl_int_set_si(v, 3);
379 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
380 bset = isl_basic_set_add_constraint(bset, c);
382 c = isl_equality_alloc(isl_local_space_copy(ls));
383 isl_int_set_si(v, -1);
384 isl_constraint_set_constant(c, v);
385 isl_int_set_si(v, 1);
386 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
387 isl_int_set_si(v, 3);
388 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
389 bset = isl_basic_set_add_constraint(bset, c);
391 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
393 assert(bset && bset->n_div == 1);
394 isl_local_space_free(ls);
395 isl_basic_set_free(bset);
398 dim = isl_space_set_alloc(ctx, 0, 3);
399 bset = isl_basic_set_universe(isl_space_copy(dim));
400 ls = isl_local_space_from_space(dim);
402 c = isl_equality_alloc(isl_local_space_copy(ls));
403 isl_int_set_si(v, 1);
404 isl_constraint_set_constant(c, v);
405 isl_int_set_si(v, -1);
406 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
407 isl_int_set_si(v, 3);
408 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
409 bset = isl_basic_set_add_constraint(bset, c);
411 c = isl_equality_alloc(isl_local_space_copy(ls));
412 isl_int_set_si(v, -3);
413 isl_constraint_set_constant(c, v);
414 isl_int_set_si(v, 1);
415 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
416 isl_int_set_si(v, 4);
417 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
418 bset = isl_basic_set_add_constraint(bset, c);
420 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
422 assert(bset && bset->n_div == 1);
423 isl_local_space_free(ls);
424 isl_basic_set_free(bset);
427 dim = isl_space_set_alloc(ctx, 0, 3);
428 bset = isl_basic_set_universe(isl_space_copy(dim));
429 ls = isl_local_space_from_space(dim);
431 c = isl_equality_alloc(isl_local_space_copy(ls));
432 isl_int_set_si(v, 2);
433 isl_constraint_set_constant(c, v);
434 isl_int_set_si(v, -1);
435 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
436 isl_int_set_si(v, 3);
437 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
438 bset = isl_basic_set_add_constraint(bset, c);
440 c = isl_equality_alloc(isl_local_space_copy(ls));
441 isl_int_set_si(v, -1);
442 isl_constraint_set_constant(c, v);
443 isl_int_set_si(v, 1);
444 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
445 isl_int_set_si(v, 6);
446 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
447 bset = isl_basic_set_add_constraint(bset, c);
449 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
451 assert(isl_basic_set_is_empty(bset));
452 isl_local_space_free(ls);
453 isl_basic_set_free(bset);
456 dim = isl_space_set_alloc(ctx, 0, 3);
457 bset = isl_basic_set_universe(isl_space_copy(dim));
458 ls = isl_local_space_from_space(dim);
460 c = isl_equality_alloc(isl_local_space_copy(ls));
461 isl_int_set_si(v, -1);
462 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
463 isl_int_set_si(v, 3);
464 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
465 bset = isl_basic_set_add_constraint(bset, c);
467 c = isl_equality_alloc(isl_local_space_copy(ls));
468 isl_int_set_si(v, 1);
469 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
470 isl_int_set_si(v, -3);
471 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
472 bset = isl_basic_set_add_constraint(bset, c);
474 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
476 assert(bset && bset->n_div == 0);
477 isl_basic_set_free(bset);
478 isl_local_space_free(ls);
481 dim = isl_space_set_alloc(ctx, 0, 3);
482 bset = isl_basic_set_universe(isl_space_copy(dim));
483 ls = isl_local_space_from_space(dim);
485 c = isl_equality_alloc(isl_local_space_copy(ls));
486 isl_int_set_si(v, -1);
487 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
488 isl_int_set_si(v, 6);
489 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
490 bset = isl_basic_set_add_constraint(bset, c);
492 c = isl_equality_alloc(isl_local_space_copy(ls));
493 isl_int_set_si(v, 1);
494 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
495 isl_int_set_si(v, -3);
496 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
497 bset = isl_basic_set_add_constraint(bset, c);
499 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
501 assert(bset && bset->n_div == 1);
502 isl_basic_set_free(bset);
503 isl_local_space_free(ls);
506 /* This test is a bit tricky. We set up an equality
508 * Normalization of divs creates _two_ divs
511 * Afterwards e0 is removed again because it has coefficient -1
512 * and we end up with the original equality and div again.
513 * Perhaps we can avoid the introduction of this temporary div.
515 dim = isl_space_set_alloc(ctx, 0, 4);
516 bset = isl_basic_set_universe(isl_space_copy(dim));
517 ls = isl_local_space_from_space(dim);
519 c = isl_equality_alloc(isl_local_space_copy(ls));
520 isl_int_set_si(v, -1);
521 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
522 isl_int_set_si(v, -3);
523 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
524 isl_int_set_si(v, -3);
525 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
526 isl_int_set_si(v, 6);
527 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
528 bset = isl_basic_set_add_constraint(bset, c);
530 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
532 /* Test disabled for now */
534 assert(bset && bset->n_div == 1);
536 isl_local_space_free(ls);
537 isl_basic_set_free(bset);
540 dim = isl_space_set_alloc(ctx, 0, 5);
541 bset = isl_basic_set_universe(isl_space_copy(dim));
542 ls = isl_local_space_from_space(dim);
544 c = isl_equality_alloc(isl_local_space_copy(ls));
545 isl_int_set_si(v, -1);
546 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
547 isl_int_set_si(v, -3);
548 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
549 isl_int_set_si(v, -3);
550 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
551 isl_int_set_si(v, 6);
552 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
553 bset = isl_basic_set_add_constraint(bset, c);
555 c = isl_equality_alloc(isl_local_space_copy(ls));
556 isl_int_set_si(v, -1);
557 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
558 isl_int_set_si(v, 1);
559 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
560 isl_int_set_si(v, 1);
561 isl_constraint_set_constant(c, v);
562 bset = isl_basic_set_add_constraint(bset, c);
564 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
566 /* Test disabled for now */
568 assert(bset && bset->n_div == 1);
570 isl_local_space_free(ls);
571 isl_basic_set_free(bset);
574 dim = isl_space_set_alloc(ctx, 0, 4);
575 bset = isl_basic_set_universe(isl_space_copy(dim));
576 ls = isl_local_space_from_space(dim);
578 c = isl_equality_alloc(isl_local_space_copy(ls));
579 isl_int_set_si(v, 1);
580 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
581 isl_int_set_si(v, -1);
582 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
583 isl_int_set_si(v, -2);
584 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
585 bset = isl_basic_set_add_constraint(bset, c);
587 c = isl_equality_alloc(isl_local_space_copy(ls));
588 isl_int_set_si(v, -1);
589 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
590 isl_int_set_si(v, 3);
591 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
592 isl_int_set_si(v, 2);
593 isl_constraint_set_constant(c, v);
594 bset = isl_basic_set_add_constraint(bset, c);
596 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
598 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
600 assert(!isl_basic_set_is_empty(bset));
602 isl_local_space_free(ls);
603 isl_basic_set_free(bset);
606 dim = isl_space_set_alloc(ctx, 0, 3);
607 bset = isl_basic_set_universe(isl_space_copy(dim));
608 ls = isl_local_space_from_space(dim);
610 c = isl_equality_alloc(isl_local_space_copy(ls));
611 isl_int_set_si(v, 1);
612 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
613 isl_int_set_si(v, -2);
614 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
615 bset = isl_basic_set_add_constraint(bset, c);
617 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
619 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
621 isl_local_space_free(ls);
622 isl_basic_set_free(bset);
626 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
627 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
628 set = isl_set_read_from_str(ctx, str);
629 set = isl_set_compute_divs(set);
634 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
635 bset = isl_basic_set_read_from_str(ctx, str);
636 n = isl_basic_set_dim(bset, isl_dim_div);
637 isl_basic_set_free(bset);
641 isl_die(ctx, isl_error_unknown,
642 "expecting no existentials", return -1);
644 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
645 set = isl_set_read_from_str(ctx, str);
646 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
647 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
648 empty = isl_set_is_empty(set);
653 isl_die(ctx, isl_error_unknown,
654 "result not as accurate as expected", return -1);
659 void test_application_case(struct isl_ctx *ctx, const char *name)
663 struct isl_basic_set *bset1, *bset2;
664 struct isl_basic_map *bmap;
666 filename = get_filename(ctx, name, "omega");
668 input = fopen(filename, "r");
671 bset1 = isl_basic_set_read_from_file(ctx, input);
672 bmap = isl_basic_map_read_from_file(ctx, input);
674 bset1 = isl_basic_set_apply(bset1, bmap);
676 bset2 = isl_basic_set_read_from_file(ctx, input);
678 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
680 isl_basic_set_free(bset1);
681 isl_basic_set_free(bset2);
687 void test_application(struct isl_ctx *ctx)
689 test_application_case(ctx, "application");
690 test_application_case(ctx, "application2");
693 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
697 struct isl_basic_set *bset1, *bset2;
699 filename = get_filename(ctx, name, "polylib");
701 input = fopen(filename, "r");
704 bset1 = isl_basic_set_read_from_file(ctx, input);
705 bset2 = isl_basic_set_read_from_file(ctx, input);
707 bset1 = isl_basic_set_affine_hull(bset1);
709 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
711 isl_basic_set_free(bset1);
712 isl_basic_set_free(bset2);
718 int test_affine_hull(struct isl_ctx *ctx)
722 isl_basic_set *bset, *bset2;
726 test_affine_hull_case(ctx, "affine2");
727 test_affine_hull_case(ctx, "affine");
728 test_affine_hull_case(ctx, "affine3");
730 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
731 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
732 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
733 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
734 set = isl_set_read_from_str(ctx, str);
735 bset = isl_set_affine_hull(set);
736 n = isl_basic_set_dim(bset, isl_dim_div);
737 isl_basic_set_free(bset);
739 isl_die(ctx, isl_error_unknown, "not expecting any divs",
742 /* Check that isl_map_affine_hull is not confused by
743 * the reordering of divs in isl_map_align_divs.
745 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
746 "32e0 = b and 32e1 = c); "
747 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
748 set = isl_set_read_from_str(ctx, str);
749 bset = isl_set_affine_hull(set);
750 isl_basic_set_free(bset);
754 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
755 "32e2 = 31 + 31e0 }";
756 set = isl_set_read_from_str(ctx, str);
757 bset = isl_set_affine_hull(set);
758 str = "{ [a] : exists e : a = 32 e }";
759 bset2 = isl_basic_set_read_from_str(ctx, str);
760 subset = isl_basic_set_is_subset(bset, bset2);
761 isl_basic_set_free(bset);
762 isl_basic_set_free(bset2);
766 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
772 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
776 struct isl_basic_set *bset1, *bset2;
779 filename = get_filename(ctx, name, "polylib");
781 input = fopen(filename, "r");
784 bset1 = isl_basic_set_read_from_file(ctx, input);
785 bset2 = isl_basic_set_read_from_file(ctx, input);
787 set = isl_basic_set_union(bset1, bset2);
788 bset1 = isl_set_convex_hull(set);
790 bset2 = isl_basic_set_read_from_file(ctx, input);
792 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
794 isl_basic_set_free(bset1);
795 isl_basic_set_free(bset2);
801 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
803 const char *str1, *str2;
804 isl_set *set1, *set2;
805 int orig_convex = ctx->opt->convex;
806 ctx->opt->convex = convex;
808 test_convex_hull_case(ctx, "convex0");
809 test_convex_hull_case(ctx, "convex1");
810 test_convex_hull_case(ctx, "convex2");
811 test_convex_hull_case(ctx, "convex3");
812 test_convex_hull_case(ctx, "convex4");
813 test_convex_hull_case(ctx, "convex5");
814 test_convex_hull_case(ctx, "convex6");
815 test_convex_hull_case(ctx, "convex7");
816 test_convex_hull_case(ctx, "convex8");
817 test_convex_hull_case(ctx, "convex9");
818 test_convex_hull_case(ctx, "convex10");
819 test_convex_hull_case(ctx, "convex11");
820 test_convex_hull_case(ctx, "convex12");
821 test_convex_hull_case(ctx, "convex13");
822 test_convex_hull_case(ctx, "convex14");
823 test_convex_hull_case(ctx, "convex15");
825 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
826 "(i0 = 1 and i1 = 0 and i2 = 1) or "
827 "(i0 = 0 and i1 = 0 and i2 = 0) }";
828 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
829 set1 = isl_set_read_from_str(ctx, str1);
830 set2 = isl_set_read_from_str(ctx, str2);
831 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
832 assert(isl_set_is_equal(set1, set2));
836 ctx->opt->convex = orig_convex;
839 void test_convex_hull(struct isl_ctx *ctx)
841 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
842 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
845 void test_gist_case(struct isl_ctx *ctx, const char *name)
849 struct isl_basic_set *bset1, *bset2;
851 filename = get_filename(ctx, name, "polylib");
853 input = fopen(filename, "r");
856 bset1 = isl_basic_set_read_from_file(ctx, input);
857 bset2 = isl_basic_set_read_from_file(ctx, input);
859 bset1 = isl_basic_set_gist(bset1, bset2);
861 bset2 = isl_basic_set_read_from_file(ctx, input);
863 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
865 isl_basic_set_free(bset1);
866 isl_basic_set_free(bset2);
872 void test_gist(struct isl_ctx *ctx)
875 isl_basic_set *bset1, *bset2;
877 test_gist_case(ctx, "gist1");
879 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
880 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
881 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
882 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
883 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
884 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
885 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
886 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
887 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
888 bset1 = isl_basic_set_read_from_str(ctx, str);
889 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
890 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
891 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
892 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
893 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
894 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
895 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
896 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
897 bset2 = isl_basic_set_read_from_str(ctx, str);
898 bset1 = isl_basic_set_gist(bset1, bset2);
899 assert(bset1 && bset1->n_div == 0);
900 isl_basic_set_free(bset1);
903 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
909 set = isl_set_read_from_str(ctx, str);
910 set = isl_set_coalesce(set);
911 set2 = isl_set_read_from_str(ctx, str);
912 equal = isl_set_is_equal(set, set2);
913 one = set && set->n == 1;
920 isl_die(ctx, isl_error_unknown,
921 "coalesced set not equal to input", return -1);
922 if (check_one && !one)
923 isl_die(ctx, isl_error_unknown,
924 "coalesced set should not be a union", return -1);
929 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
934 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
935 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
937 if (test_coalesce_set(ctx,
938 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
939 "-x - y + 1 >= 0 and -3 <= z <= 3;"
940 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
941 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
942 "-10 <= y <= 0}", 1) < 0)
944 if (test_coalesce_set(ctx,
945 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
947 if (test_coalesce_set(ctx,
948 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
956 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
961 /* Inputs for coalescing tests.
962 * "str" is a string representation of the input set.
963 * "single_disjunct" is set if we expect the result to consist of
969 } coalesce_tests[] = {
970 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
971 "y >= x & x >= 2 & 5 >= y }" },
972 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
973 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
974 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
975 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
976 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
977 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
978 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
979 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
980 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
981 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
982 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
983 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
984 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
985 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
986 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
987 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
988 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
989 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
990 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
991 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
992 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
993 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
994 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
995 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
996 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
997 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
998 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
999 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1000 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1001 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1002 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1003 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1004 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1005 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1006 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1007 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1008 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1009 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1010 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1011 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1012 "[o0, o1, o2, o3, o4, o5, o6]] : "
1013 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1014 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1015 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1016 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1017 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1018 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1019 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1020 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1021 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1022 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1023 "o6 >= i3 + i6 - o3 and M >= 0 and "
1024 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1025 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1026 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1027 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1028 "(o0 = 0 and M >= 2 and N >= 3) or "
1029 "(M = 0 and o0 = 0 and N >= 3) }" },
1030 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1031 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1032 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1033 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1034 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1035 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1036 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1037 "(y = 3 and x = 1) }" },
1038 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1039 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1040 "i1 <= M and i3 <= M and i4 <= M) or "
1041 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1042 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1043 "i4 <= -1 + M) }" },
1044 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1045 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1046 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1047 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1048 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1049 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1050 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1051 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1052 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1053 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1054 { 0, "{ [a, b] : exists e : 2e = a and "
1055 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1056 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1057 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1058 "j >= 1 and j' <= i + j - i' and i >= 1; "
1060 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1061 "[i,j] : exists a : j = 3a }" },
1062 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1063 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1065 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1066 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1067 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1068 "c <= 6 + 8a and a >= 3; "
1069 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1070 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1071 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1072 "[x,0] : 3 <= x <= 4 }" },
1073 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1074 "[x,0] : 4 <= x <= 5 }" },
1075 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1076 "[x,0] : 3 <= x <= 5 }" },
1077 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1078 "[x,0] : 3 <= x <= 4 }" },
1079 { 1 , "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1081 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1084 /* Test the functionality of isl_set_coalesce.
1085 * That is, check that the output is always equal to the input
1086 * and in some cases that the result consists of a single disjunct.
1088 static int test_coalesce(struct isl_ctx *ctx)
1092 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1093 const char *str = coalesce_tests[i].str;
1094 int check_one = coalesce_tests[i].single_disjunct;
1095 if (test_coalesce_set(ctx, str, check_one) < 0)
1099 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1105 void test_closure(struct isl_ctx *ctx)
1109 isl_map *up, *right;
1110 isl_map *map, *map2;
1113 /* COCOA example 1 */
1114 map = isl_map_read_from_str(ctx,
1115 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1116 "1 <= i and i < n and 1 <= j and j < n or "
1117 "i2 = i + 1 and j2 = j - 1 and "
1118 "1 <= i and i < n and 2 <= j and j <= n }");
1119 map = isl_map_power(map, &exact);
1123 /* COCOA example 1 */
1124 map = isl_map_read_from_str(ctx,
1125 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1126 "1 <= i and i < n and 1 <= j and j < n or "
1127 "i2 = i + 1 and j2 = j - 1 and "
1128 "1 <= i and i < n and 2 <= j and j <= n }");
1129 map = isl_map_transitive_closure(map, &exact);
1131 map2 = isl_map_read_from_str(ctx,
1132 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1133 "1 <= i and i < n and 1 <= j and j <= n and "
1134 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1135 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1136 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1137 assert(isl_map_is_equal(map, map2));
1141 map = isl_map_read_from_str(ctx,
1142 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1143 " 0 <= y and y <= n }");
1144 map = isl_map_transitive_closure(map, &exact);
1145 map2 = isl_map_read_from_str(ctx,
1146 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1147 " 0 <= y and y <= n }");
1148 assert(isl_map_is_equal(map, map2));
1152 /* COCOA example 2 */
1153 map = isl_map_read_from_str(ctx,
1154 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1155 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1156 "i2 = i + 2 and j2 = j - 2 and "
1157 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1158 map = isl_map_transitive_closure(map, &exact);
1160 map2 = isl_map_read_from_str(ctx,
1161 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1162 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1163 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1164 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1165 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1166 assert(isl_map_is_equal(map, map2));
1170 /* COCOA Fig.2 left */
1171 map = isl_map_read_from_str(ctx,
1172 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1173 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1175 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1176 "j <= 2 i - 3 and j <= n - 2 or "
1177 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1178 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1179 map = isl_map_transitive_closure(map, &exact);
1183 /* COCOA Fig.2 right */
1184 map = isl_map_read_from_str(ctx,
1185 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1186 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1188 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1189 "j <= 2 i - 4 and j <= n - 3 or "
1190 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1191 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1192 map = isl_map_power(map, &exact);
1196 /* COCOA Fig.2 right */
1197 map = isl_map_read_from_str(ctx,
1198 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1199 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1201 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1202 "j <= 2 i - 4 and j <= n - 3 or "
1203 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1204 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1205 map = isl_map_transitive_closure(map, &exact);
1207 map2 = isl_map_read_from_str(ctx,
1208 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1209 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1210 "j <= n and 3 + i + 2 j <= 3 n and "
1211 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1212 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1213 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1214 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1215 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1216 assert(isl_map_is_equal(map, map2));
1220 /* COCOA Fig.1 right */
1221 dom = isl_set_read_from_str(ctx,
1222 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1223 "2 x - 3 y + 3 >= 0 }");
1224 right = isl_map_read_from_str(ctx,
1225 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1226 up = isl_map_read_from_str(ctx,
1227 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1228 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1229 right = isl_map_intersect_range(right, isl_set_copy(dom));
1230 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1231 up = isl_map_intersect_range(up, dom);
1232 map = isl_map_union(up, right);
1233 map = isl_map_transitive_closure(map, &exact);
1235 map2 = isl_map_read_from_str(ctx,
1236 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1237 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1238 assert(isl_map_is_equal(map, map2));
1242 /* COCOA Theorem 1 counter example */
1243 map = isl_map_read_from_str(ctx,
1244 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1245 "i2 = 1 and j2 = j or "
1246 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1247 map = isl_map_transitive_closure(map, &exact);
1251 map = isl_map_read_from_str(ctx,
1252 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1253 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1254 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1255 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1256 map = isl_map_transitive_closure(map, &exact);
1260 /* Kelly et al 1996, fig 12 */
1261 map = isl_map_read_from_str(ctx,
1262 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1263 "1 <= i,j,j+1 <= n or "
1264 "j = n and j2 = 1 and i2 = i + 1 and "
1265 "1 <= i,i+1 <= n }");
1266 map = isl_map_transitive_closure(map, &exact);
1268 map2 = isl_map_read_from_str(ctx,
1269 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1270 "1 <= i <= n and i = i2 or "
1271 "1 <= i < i2 <= n and 1 <= j <= n and "
1273 assert(isl_map_is_equal(map, map2));
1277 /* Omega's closure4 */
1278 map = isl_map_read_from_str(ctx,
1279 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1280 "1 <= x,y <= 10 or "
1281 "x2 = x + 1 and y2 = y and "
1282 "1 <= x <= 20 && 5 <= y <= 15 }");
1283 map = isl_map_transitive_closure(map, &exact);
1287 map = isl_map_read_from_str(ctx,
1288 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1289 map = isl_map_transitive_closure(map, &exact);
1291 map2 = isl_map_read_from_str(ctx,
1292 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1293 assert(isl_map_is_equal(map, map2));
1297 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1298 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1299 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1300 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1301 map = isl_map_read_from_str(ctx, str);
1302 map = isl_map_transitive_closure(map, &exact);
1304 map2 = isl_map_read_from_str(ctx, str);
1305 assert(isl_map_is_equal(map, map2));
1309 str = "{[0] -> [1]; [2] -> [3]}";
1310 map = isl_map_read_from_str(ctx, str);
1311 map = isl_map_transitive_closure(map, &exact);
1313 map2 = isl_map_read_from_str(ctx, str);
1314 assert(isl_map_is_equal(map, map2));
1318 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1319 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1320 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1321 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1322 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1323 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1324 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1325 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1326 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1327 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1328 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1329 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1330 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1331 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1332 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1333 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1334 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1335 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1336 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1337 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1338 map = isl_map_read_from_str(ctx, str);
1339 map = isl_map_transitive_closure(map, NULL);
1344 void test_lex(struct isl_ctx *ctx)
1349 dim = isl_space_set_alloc(ctx, 0, 0);
1350 map = isl_map_lex_le(dim);
1351 assert(!isl_map_is_empty(map));
1355 static int test_lexmin(struct isl_ctx *ctx)
1359 isl_basic_map *bmap;
1360 isl_map *map, *map2;
1363 isl_pw_multi_aff *pma;
1365 str = "[p0, p1] -> { [] -> [] : "
1366 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1367 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1368 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1369 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1370 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1371 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1372 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1373 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1374 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1375 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1376 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1377 map = isl_map_read_from_str(ctx, str);
1378 map = isl_map_lexmin(map);
1381 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1382 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1383 set = isl_set_read_from_str(ctx, str);
1384 set = isl_set_lexmax(set);
1385 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1386 set2 = isl_set_read_from_str(ctx, str);
1387 set = isl_set_intersect(set, set2);
1388 assert(!isl_set_is_empty(set));
1391 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1392 map = isl_map_read_from_str(ctx, str);
1393 map = isl_map_lexmin(map);
1394 str = "{ [x] -> [5] : 6 <= x <= 8; "
1395 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1396 map2 = isl_map_read_from_str(ctx, str);
1397 assert(isl_map_is_equal(map, map2));
1401 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1402 map = isl_map_read_from_str(ctx, str);
1403 map2 = isl_map_copy(map);
1404 map = isl_map_lexmin(map);
1405 assert(isl_map_is_equal(map, map2));
1409 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1410 map = isl_map_read_from_str(ctx, str);
1411 map = isl_map_lexmin(map);
1412 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1413 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1414 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1415 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1416 map2 = isl_map_read_from_str(ctx, str);
1417 assert(isl_map_is_equal(map, map2));
1421 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1422 " 8i' <= i and 8i' >= -7 + i }";
1423 bmap = isl_basic_map_read_from_str(ctx, str);
1424 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1425 map2 = isl_map_from_pw_multi_aff(pma);
1426 map = isl_map_from_basic_map(bmap);
1427 assert(isl_map_is_equal(map, map2));
1431 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1432 map = isl_map_read_from_str(ctx, str);
1433 map = isl_map_lexmin(map);
1434 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1435 map2 = isl_map_read_from_str(ctx, str);
1436 assert(isl_map_is_equal(map, map2));
1440 /* Check that empty pieces are properly combined. */
1441 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1442 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1443 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1444 map = isl_map_read_from_str(ctx, str);
1445 map = isl_map_lexmin(map);
1446 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1447 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1449 map2 = isl_map_read_from_str(ctx, str);
1450 assert(isl_map_is_equal(map, map2));
1454 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1455 " 8i' <= i and 8i' >= -7 + i }";
1456 set = isl_set_read_from_str(ctx, str);
1457 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1458 set2 = isl_set_from_pw_multi_aff(pma);
1459 equal = isl_set_is_equal(set, set2);
1465 isl_die(ctx, isl_error_unknown,
1466 "unexpected difference between set and "
1467 "piecewise affine expression", return -1);
1477 static int collect_must_may(__isl_take isl_map *dep, int must,
1478 void *dep_user, void *user)
1480 struct must_may *mm = (struct must_may *)user;
1483 mm->must = isl_map_union(mm->must, dep);
1485 mm->may = isl_map_union(mm->may, dep);
1490 static int common_space(void *first, void *second)
1492 int depth = *(int *)first;
1496 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1504 map2 = isl_map_read_from_str(map->ctx, str);
1505 equal = isl_map_is_equal(map, map2);
1511 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1515 equal = map_is_equal(map, str);
1519 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1520 "result not as expected", return -1);
1524 void test_dep(struct isl_ctx *ctx)
1529 isl_access_info *ai;
1536 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1537 map = isl_map_read_from_str(ctx, str);
1538 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1540 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1541 map = isl_map_read_from_str(ctx, str);
1542 ai = isl_access_info_add_source(ai, map, 1, &depth);
1544 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1545 map = isl_map_read_from_str(ctx, str);
1546 ai = isl_access_info_add_source(ai, map, 1, &depth);
1548 flow = isl_access_info_compute_flow(ai);
1549 dim = isl_space_alloc(ctx, 0, 3, 3);
1550 mm.must = isl_map_empty(isl_space_copy(dim));
1551 mm.may = isl_map_empty(dim);
1553 isl_flow_foreach(flow, collect_must_may, &mm);
1555 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1556 " [1,10,0] -> [2,5,0] }";
1557 assert(map_is_equal(mm.must, str));
1558 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1559 assert(map_is_equal(mm.may, str));
1561 isl_map_free(mm.must);
1562 isl_map_free(mm.may);
1563 isl_flow_free(flow);
1566 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1567 map = isl_map_read_from_str(ctx, str);
1568 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1570 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1571 map = isl_map_read_from_str(ctx, str);
1572 ai = isl_access_info_add_source(ai, map, 1, &depth);
1574 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1575 map = isl_map_read_from_str(ctx, str);
1576 ai = isl_access_info_add_source(ai, map, 0, &depth);
1578 flow = isl_access_info_compute_flow(ai);
1579 dim = isl_space_alloc(ctx, 0, 3, 3);
1580 mm.must = isl_map_empty(isl_space_copy(dim));
1581 mm.may = isl_map_empty(dim);
1583 isl_flow_foreach(flow, collect_must_may, &mm);
1585 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1586 assert(map_is_equal(mm.must, str));
1587 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1588 assert(map_is_equal(mm.may, str));
1590 isl_map_free(mm.must);
1591 isl_map_free(mm.may);
1592 isl_flow_free(flow);
1595 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1596 map = isl_map_read_from_str(ctx, str);
1597 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1599 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1600 map = isl_map_read_from_str(ctx, str);
1601 ai = isl_access_info_add_source(ai, map, 0, &depth);
1603 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1604 map = isl_map_read_from_str(ctx, str);
1605 ai = isl_access_info_add_source(ai, map, 0, &depth);
1607 flow = isl_access_info_compute_flow(ai);
1608 dim = isl_space_alloc(ctx, 0, 3, 3);
1609 mm.must = isl_map_empty(isl_space_copy(dim));
1610 mm.may = isl_map_empty(dim);
1612 isl_flow_foreach(flow, collect_must_may, &mm);
1614 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1615 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1616 assert(map_is_equal(mm.may, str));
1617 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1618 assert(map_is_equal(mm.must, str));
1620 isl_map_free(mm.must);
1621 isl_map_free(mm.may);
1622 isl_flow_free(flow);
1625 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1626 map = isl_map_read_from_str(ctx, str);
1627 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1629 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1630 map = isl_map_read_from_str(ctx, str);
1631 ai = isl_access_info_add_source(ai, map, 0, &depth);
1633 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1634 map = isl_map_read_from_str(ctx, str);
1635 ai = isl_access_info_add_source(ai, map, 0, &depth);
1637 flow = isl_access_info_compute_flow(ai);
1638 dim = isl_space_alloc(ctx, 0, 3, 3);
1639 mm.must = isl_map_empty(isl_space_copy(dim));
1640 mm.may = isl_map_empty(dim);
1642 isl_flow_foreach(flow, collect_must_may, &mm);
1644 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1645 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1646 assert(map_is_equal(mm.may, str));
1647 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1648 assert(map_is_equal(mm.must, str));
1650 isl_map_free(mm.must);
1651 isl_map_free(mm.may);
1652 isl_flow_free(flow);
1655 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1656 map = isl_map_read_from_str(ctx, str);
1657 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1659 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1660 map = isl_map_read_from_str(ctx, str);
1661 ai = isl_access_info_add_source(ai, map, 0, &depth);
1663 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1664 map = isl_map_read_from_str(ctx, str);
1665 ai = isl_access_info_add_source(ai, map, 0, &depth);
1667 flow = isl_access_info_compute_flow(ai);
1668 dim = isl_space_alloc(ctx, 0, 3, 3);
1669 mm.must = isl_map_empty(isl_space_copy(dim));
1670 mm.may = isl_map_empty(dim);
1672 isl_flow_foreach(flow, collect_must_may, &mm);
1674 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1675 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1676 assert(map_is_equal(mm.may, str));
1677 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1678 assert(map_is_equal(mm.must, str));
1680 isl_map_free(mm.must);
1681 isl_map_free(mm.may);
1682 isl_flow_free(flow);
1687 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1688 map = isl_map_read_from_str(ctx, str);
1689 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1691 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1692 map = isl_map_read_from_str(ctx, str);
1693 ai = isl_access_info_add_source(ai, map, 1, &depth);
1695 flow = isl_access_info_compute_flow(ai);
1696 dim = isl_space_alloc(ctx, 0, 5, 5);
1697 mm.must = isl_map_empty(isl_space_copy(dim));
1698 mm.may = isl_map_empty(dim);
1700 isl_flow_foreach(flow, collect_must_may, &mm);
1702 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1703 assert(map_is_equal(mm.must, str));
1704 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1705 assert(map_is_equal(mm.may, str));
1707 isl_map_free(mm.must);
1708 isl_map_free(mm.may);
1709 isl_flow_free(flow);
1712 int test_sv(isl_ctx *ctx)
1716 isl_union_map *umap;
1719 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1720 map = isl_map_read_from_str(ctx, str);
1721 sv = isl_map_is_single_valued(map);
1726 isl_die(ctx, isl_error_internal,
1727 "map not detected as single valued", return -1);
1729 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1730 map = isl_map_read_from_str(ctx, str);
1731 sv = isl_map_is_single_valued(map);
1736 isl_die(ctx, isl_error_internal,
1737 "map detected as single valued", return -1);
1739 str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
1740 umap = isl_union_map_read_from_str(ctx, str);
1741 sv = isl_union_map_is_single_valued(umap);
1742 isl_union_map_free(umap);
1746 isl_die(ctx, isl_error_internal,
1747 "map not detected as single valued", return -1);
1749 str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
1750 umap = isl_union_map_read_from_str(ctx, str);
1751 sv = isl_union_map_is_single_valued(umap);
1752 isl_union_map_free(umap);
1756 isl_die(ctx, isl_error_internal,
1757 "map detected as single valued", return -1);
1762 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1766 map = isl_map_read_from_str(ctx, str);
1768 assert(isl_map_is_bijective(map));
1770 assert(!isl_map_is_bijective(map));
1774 void test_bijective(struct isl_ctx *ctx)
1776 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1777 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1778 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1779 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1780 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1781 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1782 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1783 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1784 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1785 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1786 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1787 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1788 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1791 void test_pwqp(struct isl_ctx *ctx)
1795 isl_pw_qpolynomial *pwqp1, *pwqp2;
1797 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1798 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1800 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1803 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1804 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1806 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1808 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1810 isl_pw_qpolynomial_free(pwqp1);
1812 str = "{ [i] -> i }";
1813 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1814 str = "{ [k] : exists a : k = 2a }";
1815 set = isl_set_read_from_str(ctx, str);
1816 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1817 str = "{ [i] -> i }";
1818 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1820 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1822 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1824 isl_pw_qpolynomial_free(pwqp1);
1826 str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
1827 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1829 set = isl_set_read_from_str(ctx, str);
1830 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1831 str = "{ [i] -> 16 }";
1832 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1834 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1836 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1838 isl_pw_qpolynomial_free(pwqp1);
1840 str = "{ [i] -> ([(i)/2]) }";
1841 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1842 str = "{ [k] : exists a : k = 2a+1 }";
1843 set = isl_set_read_from_str(ctx, str);
1844 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1845 str = "{ [i] -> -1/2 + 1/2 * i }";
1846 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1848 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1850 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1852 isl_pw_qpolynomial_free(pwqp1);
1854 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1855 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1856 str = "{ [i] -> ([(2 * [i/2])/5]) }";
1857 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1859 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1861 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1863 isl_pw_qpolynomial_free(pwqp1);
1865 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1866 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1867 str = "{ [x] -> x }";
1868 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1870 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1872 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1874 isl_pw_qpolynomial_free(pwqp1);
1876 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
1877 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1878 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1879 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
1880 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1881 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1882 isl_pw_qpolynomial_free(pwqp1);
1885 void test_split_periods(isl_ctx *ctx)
1888 isl_pw_qpolynomial *pwqp;
1890 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1891 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
1892 "U >= 0; [U,V] -> U^2 : U >= 100 }";
1893 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1895 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1898 isl_pw_qpolynomial_free(pwqp);
1901 void test_union(isl_ctx *ctx)
1904 isl_union_set *uset1, *uset2;
1905 isl_union_map *umap1, *umap2;
1907 str = "{ [i] : 0 <= i <= 1 }";
1908 uset1 = isl_union_set_read_from_str(ctx, str);
1909 str = "{ [1] -> [0] }";
1910 umap1 = isl_union_map_read_from_str(ctx, str);
1912 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1913 assert(isl_union_map_is_equal(umap1, umap2));
1915 isl_union_map_free(umap1);
1916 isl_union_map_free(umap2);
1918 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1919 umap1 = isl_union_map_read_from_str(ctx, str);
1920 str = "{ A[i]; B[i] }";
1921 uset1 = isl_union_set_read_from_str(ctx, str);
1923 uset2 = isl_union_map_domain(umap1);
1925 assert(isl_union_set_is_equal(uset1, uset2));
1927 isl_union_set_free(uset1);
1928 isl_union_set_free(uset2);
1931 void test_bound(isl_ctx *ctx)
1934 isl_pw_qpolynomial *pwqp;
1935 isl_pw_qpolynomial_fold *pwf;
1937 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1938 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1939 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1940 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
1941 isl_pw_qpolynomial_fold_free(pwf);
1943 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
1944 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1945 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1946 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
1947 isl_pw_qpolynomial_fold_free(pwf);
1950 void test_lift(isl_ctx *ctx)
1953 isl_basic_map *bmap;
1954 isl_basic_set *bset;
1956 str = "{ [i0] : exists e0 : i0 = 4e0 }";
1957 bset = isl_basic_set_read_from_str(ctx, str);
1958 bset = isl_basic_set_lift(bset);
1959 bmap = isl_basic_map_from_range(bset);
1960 bset = isl_basic_map_domain(bmap);
1961 isl_basic_set_free(bset);
1968 } subset_tests[] = {
1970 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1971 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1972 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
1974 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
1975 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
1976 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
1977 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
1978 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
1979 "256e0 <= 255i and 256e0 >= -255 + 255i and "
1980 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
1981 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
1982 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
1983 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
1984 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
1985 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
1986 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
1989 static int test_subset(isl_ctx *ctx)
1992 isl_set *set1, *set2;
1995 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
1996 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
1997 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
1998 subset = isl_set_is_subset(set1, set2);
2003 if (subset != subset_tests[i].subset)
2004 isl_die(ctx, isl_error_unknown,
2005 "incorrect subset result", return -1);
2012 const char *minuend;
2013 const char *subtrahend;
2014 const char *difference;
2015 } subtract_domain_tests[] = {
2016 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2017 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2018 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2021 static int test_subtract(isl_ctx *ctx)
2024 isl_union_map *umap1, *umap2;
2025 isl_union_set *uset;
2028 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2029 umap1 = isl_union_map_read_from_str(ctx,
2030 subtract_domain_tests[i].minuend);
2031 uset = isl_union_set_read_from_str(ctx,
2032 subtract_domain_tests[i].subtrahend);
2033 umap2 = isl_union_map_read_from_str(ctx,
2034 subtract_domain_tests[i].difference);
2035 umap1 = isl_union_map_subtract_domain(umap1, uset);
2036 equal = isl_union_map_is_equal(umap1, umap2);
2037 isl_union_map_free(umap1);
2038 isl_union_map_free(umap2);
2042 isl_die(ctx, isl_error_unknown,
2043 "incorrect subtract domain result", return -1);
2049 int test_factorize(isl_ctx *ctx)
2052 isl_basic_set *bset;
2055 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2056 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2057 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2058 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2059 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2060 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2061 "3i5 >= -2i0 - i2 + 3i4 }";
2062 bset = isl_basic_set_read_from_str(ctx, str);
2063 f = isl_basic_set_factorizer(bset);
2064 isl_basic_set_free(bset);
2065 isl_factorizer_free(f);
2067 isl_die(ctx, isl_error_unknown,
2068 "failed to construct factorizer", return -1);
2070 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2071 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2072 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2073 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2074 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2075 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2076 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2077 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2078 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2079 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2080 bset = isl_basic_set_read_from_str(ctx, str);
2081 f = isl_basic_set_factorizer(bset);
2082 isl_basic_set_free(bset);
2083 isl_factorizer_free(f);
2085 isl_die(ctx, isl_error_unknown,
2086 "failed to construct factorizer", return -1);
2091 static int check_injective(__isl_take isl_map *map, void *user)
2093 int *injective = user;
2095 *injective = isl_map_is_injective(map);
2098 if (*injective < 0 || !*injective)
2104 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2105 const char *r, const char *s, int tilable, int parallel)
2109 isl_union_map *W, *R, *S;
2110 isl_union_map *empty;
2111 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2112 isl_union_map *validity, *proximity;
2113 isl_union_map *schedule;
2114 isl_union_map *test;
2115 isl_union_set *delta;
2116 isl_union_set *domain;
2120 isl_schedule *sched;
2121 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2123 D = isl_union_set_read_from_str(ctx, d);
2124 W = isl_union_map_read_from_str(ctx, w);
2125 R = isl_union_map_read_from_str(ctx, r);
2126 S = isl_union_map_read_from_str(ctx, s);
2128 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2129 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2131 empty = isl_union_map_empty(isl_union_map_get_space(S));
2132 isl_union_map_compute_flow(isl_union_map_copy(R),
2133 isl_union_map_copy(W), empty,
2134 isl_union_map_copy(S),
2135 &dep_raw, NULL, NULL, NULL);
2136 isl_union_map_compute_flow(isl_union_map_copy(W),
2137 isl_union_map_copy(W),
2138 isl_union_map_copy(R),
2139 isl_union_map_copy(S),
2140 &dep_waw, &dep_war, NULL, NULL);
2142 dep = isl_union_map_union(dep_waw, dep_war);
2143 dep = isl_union_map_union(dep, dep_raw);
2144 validity = isl_union_map_copy(dep);
2145 proximity = isl_union_map_copy(dep);
2147 sched = isl_union_set_compute_schedule(isl_union_set_copy(D),
2148 validity, proximity);
2149 schedule = isl_schedule_get_map(sched);
2150 isl_schedule_free(sched);
2151 isl_union_map_free(W);
2152 isl_union_map_free(R);
2153 isl_union_map_free(S);
2156 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2158 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2159 is_complete = isl_union_set_is_subset(D, domain);
2160 isl_union_set_free(D);
2161 isl_union_set_free(domain);
2163 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2164 test = isl_union_map_apply_range(test, dep);
2165 test = isl_union_map_apply_range(test, schedule);
2167 delta = isl_union_map_deltas(test);
2168 if (isl_union_set_n_set(delta) == 0) {
2172 isl_union_set_free(delta);
2174 delta_set = isl_set_from_union_set(delta);
2176 slice = isl_set_universe(isl_set_get_space(delta_set));
2177 for (i = 0; i < tilable; ++i)
2178 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2179 is_tilable = isl_set_is_subset(delta_set, slice);
2180 isl_set_free(slice);
2182 slice = isl_set_universe(isl_set_get_space(delta_set));
2183 for (i = 0; i < parallel; ++i)
2184 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2185 is_parallel = isl_set_is_subset(delta_set, slice);
2186 isl_set_free(slice);
2188 origin = isl_set_universe(isl_set_get_space(delta_set));
2189 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2190 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2192 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2193 delta_set = isl_set_lexmin(delta_set);
2195 is_nonneg = isl_set_is_equal(delta_set, origin);
2197 isl_set_free(origin);
2198 isl_set_free(delta_set);
2201 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2202 is_injection < 0 || is_complete < 0)
2205 isl_die(ctx, isl_error_unknown,
2206 "generated schedule incomplete", return -1);
2208 isl_die(ctx, isl_error_unknown,
2209 "generated schedule not injective on each statement",
2212 isl_die(ctx, isl_error_unknown,
2213 "negative dependences in generated schedule",
2216 isl_die(ctx, isl_error_unknown,
2217 "generated schedule not as tilable as expected",
2220 isl_die(ctx, isl_error_unknown,
2221 "generated schedule not as parallel as expected",
2227 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2228 const char *domain, const char *validity, const char *proximity)
2232 isl_union_map *prox;
2233 isl_schedule *schedule;
2234 isl_union_map *sched;
2236 dom = isl_union_set_read_from_str(ctx, domain);
2237 dep = isl_union_map_read_from_str(ctx, validity);
2238 prox = isl_union_map_read_from_str(ctx, proximity);
2239 schedule = isl_union_set_compute_schedule(dom, dep, prox);
2240 sched = isl_schedule_get_map(schedule);
2241 isl_schedule_free(schedule);
2246 /* Check that a schedule can be constructed on the given domain
2247 * with the given validity and proximity constraints.
2249 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2250 const char *validity, const char *proximity)
2252 isl_union_map *sched;
2254 sched = compute_schedule(ctx, domain, validity, proximity);
2258 isl_union_map_free(sched);
2262 int test_special_schedule(isl_ctx *ctx, const char *domain,
2263 const char *validity, const char *proximity, const char *expected_sched)
2265 isl_union_map *sched1, *sched2;
2268 sched1 = compute_schedule(ctx, domain, validity, proximity);
2269 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2271 equal = isl_union_map_is_equal(sched1, sched2);
2272 isl_union_map_free(sched1);
2273 isl_union_map_free(sched2);
2278 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2284 /* Check that the schedule map is properly padded, even after being
2285 * reconstructed from the band forest.
2287 static int test_padded_schedule(isl_ctx *ctx)
2291 isl_union_map *validity, *proximity;
2292 isl_schedule *sched;
2293 isl_union_map *map1, *map2;
2294 isl_band_list *list;
2297 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2298 D = isl_union_set_read_from_str(ctx, str);
2299 validity = isl_union_map_empty(isl_union_set_get_space(D));
2300 proximity = isl_union_map_copy(validity);
2301 sched = isl_union_set_compute_schedule(D, validity, proximity);
2302 map1 = isl_schedule_get_map(sched);
2303 list = isl_schedule_get_band_forest(sched);
2304 isl_band_list_free(list);
2305 map2 = isl_schedule_get_map(sched);
2306 isl_schedule_free(sched);
2307 equal = isl_union_map_is_equal(map1, map2);
2308 isl_union_map_free(map1);
2309 isl_union_map_free(map2);
2314 isl_die(ctx, isl_error_unknown,
2315 "reconstructed schedule map not the same as original",
2321 int test_schedule(isl_ctx *ctx)
2323 const char *D, *W, *R, *V, *P, *S;
2325 /* Handle resulting schedule with zero bands. */
2326 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2330 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2331 W = "{ S1[t,i] -> a[t,i] }";
2332 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2333 "S1[t,i] -> a[t-1,i+1] }";
2334 S = "{ S1[t,i] -> [t,i] }";
2335 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2338 /* Fig. 5 of CC2008 */
2339 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2341 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2342 "j >= 2 and j <= -1 + N }";
2343 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2344 "j >= 2 and j <= -1 + N; "
2345 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2346 "j >= 2 and j <= -1 + N }";
2347 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2348 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2351 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2352 W = "{ S1[i] -> a[i] }";
2353 R = "{ S2[i] -> a[i+1] }";
2354 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2355 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2358 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2359 W = "{ S1[i] -> a[i] }";
2360 R = "{ S2[i] -> a[9-i] }";
2361 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2362 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2365 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2366 W = "{ S1[i] -> a[i] }";
2367 R = "[N] -> { S2[i] -> a[N-1-i] }";
2368 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2369 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2372 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2373 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2374 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2375 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2376 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2379 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2380 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2381 R = "{ S2[i,j] -> a[i-1,j] }";
2382 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2383 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2386 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2387 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2388 R = "{ S2[i,j] -> a[i,j-1] }";
2389 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2390 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2393 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2394 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2395 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2396 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2397 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2398 "S_0[] -> [0, 0, 0] }";
2399 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2401 ctx->opt->schedule_parametric = 0;
2402 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2404 ctx->opt->schedule_parametric = 1;
2406 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2407 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2408 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2409 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2410 "S4[i] -> a[i,N] }";
2411 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2412 "S4[i] -> [4,i,0] }";
2413 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2416 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2417 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2419 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2421 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2423 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2424 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2427 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2428 " S_2[t] : t >= 0 and t <= -1 + N; "
2429 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2431 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2432 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2433 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2434 "i >= 0 and i <= -1 + N }";
2435 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2436 "i >= 0 and i <= -1 + N; "
2437 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2438 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2439 " S_0[t] -> [0, t, 0] }";
2441 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2443 ctx->opt->schedule_parametric = 0;
2444 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2446 ctx->opt->schedule_parametric = 1;
2448 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2449 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2450 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2453 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2454 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2455 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2456 "j >= 0 and j <= -1 + N; "
2457 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2458 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2459 "j >= 0 and j <= -1 + N; "
2460 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2461 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2462 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2465 D = "{ S_0[i] : i >= 0 }";
2466 W = "{ S_0[i] -> a[i] : i >= 0 }";
2467 R = "{ S_0[i] -> a[0] : i >= 0 }";
2468 S = "{ S_0[i] -> [0, i, 0] }";
2469 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2472 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2473 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2474 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2475 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2476 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2479 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2480 "k <= -1 + n and k >= 0 }";
2481 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
2482 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2483 "k <= -1 + n and k >= 0; "
2484 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2485 "k <= -1 + n and k >= 0; "
2486 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2487 "k <= -1 + n and k >= 0 }";
2488 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2489 ctx->opt->schedule_outer_zero_distance = 1;
2490 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2492 ctx->opt->schedule_outer_zero_distance = 0;
2494 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
2495 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
2496 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
2497 "Stmt_for_body24[i0, i1, 1, 0]:"
2498 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
2499 "Stmt_for_body7[i0, i1, i2]:"
2500 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
2503 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
2504 "Stmt_for_body24[1, i1, i2, i3]:"
2505 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
2507 "Stmt_for_body24[0, i1, i2, i3] -> "
2508 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
2509 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
2511 "Stmt_for_body24[0, i1, i2, i3] ->"
2512 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
2513 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
2514 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
2515 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
2516 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
2517 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
2518 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
2519 "i1 <= 6 and i1 >= 0;"
2520 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
2521 "Stmt_for_body7[i0, i1, i2] -> "
2522 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
2523 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
2524 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
2525 "Stmt_for_body7[i0, i1, i2] -> "
2526 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
2527 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
2528 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
2530 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
2531 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
2532 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
2534 if (test_special_schedule(ctx, D, V, P, S) < 0)
2537 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
2538 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
2539 "j >= 1 and j <= 7;"
2540 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
2541 "j >= 1 and j <= 8 }";
2543 S = "{ S_0[i, j] -> [i + j, j] }";
2544 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2545 if (test_special_schedule(ctx, D, V, P, S) < 0)
2547 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2549 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
2550 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
2551 "j >= 0 and j <= -1 + i }";
2552 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
2553 "i <= -1 + N and j >= 0;"
2554 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
2557 S = "{ S_0[i, j] -> [i, j] }";
2558 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2559 if (test_special_schedule(ctx, D, V, P, S) < 0)
2561 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2563 /* Test both algorithms on a case with only proximity dependences. */
2564 D = "{ S[i,j] : 0 <= i <= 10 }";
2566 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
2567 S = "{ S[i, j] -> [j, i] }";
2568 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2569 if (test_special_schedule(ctx, D, V, P, S) < 0)
2571 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2572 if (test_special_schedule(ctx, D, V, P, S) < 0)
2575 D = "{ A[a]; B[] }";
2577 P = "{ A[a] -> B[] }";
2578 if (test_has_schedule(ctx, D, V, P) < 0)
2581 if (test_padded_schedule(ctx) < 0)
2584 /* Check that check for progress is not confused by rational
2587 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
2588 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
2590 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
2591 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
2593 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2594 if (test_has_schedule(ctx, D, V, P) < 0)
2596 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2601 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
2603 isl_union_map *umap;
2606 umap = isl_union_map_read_from_str(ctx, str);
2607 test = isl_union_map_plain_is_injective(umap);
2608 isl_union_map_free(umap);
2611 if (test == injective)
2614 isl_die(ctx, isl_error_unknown,
2615 "map not detected as injective", return -1);
2617 isl_die(ctx, isl_error_unknown,
2618 "map detected as injective", return -1);
2621 int test_injective(isl_ctx *ctx)
2625 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2627 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
2629 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
2631 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
2633 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2635 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2637 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2639 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2642 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2643 if (test_plain_injective(ctx, str, 1))
2645 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2646 if (test_plain_injective(ctx, str, 0))
2652 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
2660 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
2661 equal = isl_aff_plain_is_equal(aff, aff2);
2667 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
2671 equal = aff_plain_is_equal(aff, str);
2675 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
2676 "result not as expected", return -1);
2680 int test_aff(isl_ctx *ctx)
2685 isl_local_space *ls;
2689 space = isl_space_set_alloc(ctx, 0, 1);
2690 ls = isl_local_space_from_space(space);
2691 aff = isl_aff_zero_on_domain(ls);
2693 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2694 aff = isl_aff_scale_down_ui(aff, 3);
2695 aff = isl_aff_floor(aff);
2696 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2697 aff = isl_aff_scale_down_ui(aff, 2);
2698 aff = isl_aff_floor(aff);
2699 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2702 set = isl_set_read_from_str(ctx, str);
2703 aff = isl_aff_gist(aff, set);
2705 aff = isl_aff_add_constant_si(aff, -16);
2706 zero = isl_aff_plain_is_zero(aff);
2712 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2714 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
2715 aff = isl_aff_scale_down_ui(aff, 64);
2716 aff = isl_aff_floor(aff);
2717 equal = aff_check_plain_equal(aff, "{ [-1] }");
2725 int test_dim_max(isl_ctx *ctx)
2729 isl_set *set1, *set2;
2734 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
2735 set = isl_set_read_from_str(ctx, str);
2736 pwaff = isl_set_dim_max(set, 0);
2737 set1 = isl_set_from_pw_aff(pwaff);
2738 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
2739 set2 = isl_set_read_from_str(ctx, str);
2740 equal = isl_set_is_equal(set1, set2);
2746 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2748 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
2749 set = isl_set_read_from_str(ctx, str);
2750 pwaff = isl_set_dim_max(set, 0);
2751 set1 = isl_set_from_pw_aff(pwaff);
2752 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2753 set2 = isl_set_read_from_str(ctx, str);
2754 equal = isl_set_is_equal(set1, set2);
2760 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2762 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
2763 set = isl_set_read_from_str(ctx, str);
2764 pwaff = isl_set_dim_max(set, 0);
2765 set1 = isl_set_from_pw_aff(pwaff);
2766 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2767 set2 = isl_set_read_from_str(ctx, str);
2768 equal = isl_set_is_equal(set1, set2);
2774 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2776 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
2777 "0 <= i < N and 0 <= j < M }";
2778 map = isl_map_read_from_str(ctx, str);
2779 set = isl_map_range(map);
2781 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
2782 set1 = isl_set_from_pw_aff(pwaff);
2783 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
2784 set2 = isl_set_read_from_str(ctx, str);
2785 equal = isl_set_is_equal(set1, set2);
2789 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
2790 set1 = isl_set_from_pw_aff(pwaff);
2791 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
2792 set2 = isl_set_read_from_str(ctx, str);
2793 if (equal >= 0 && equal)
2794 equal = isl_set_is_equal(set1, set2);
2803 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2805 /* Check that solutions are properly merged. */
2806 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
2807 "c <= -1 + n - 4a - 2b and c >= -2b and "
2808 "4a >= -4 + n and c >= 0 }";
2809 set = isl_set_read_from_str(ctx, str);
2810 pwaff = isl_set_dim_min(set, 2);
2811 set1 = isl_set_from_pw_aff(pwaff);
2812 str = "[n] -> { [(0)] : n >= 1 }";
2813 set2 = isl_set_read_from_str(ctx, str);
2814 equal = isl_set_is_equal(set1, set2);
2821 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2826 int test_product(isl_ctx *ctx)
2830 isl_union_set *uset1, *uset2;
2834 set = isl_set_read_from_str(ctx, str);
2835 set = isl_set_product(set, isl_set_copy(set));
2836 ok = isl_set_is_wrapping(set);
2841 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2844 uset1 = isl_union_set_read_from_str(ctx, str);
2845 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
2846 str = "{ [[] -> []] }";
2847 uset2 = isl_union_set_read_from_str(ctx, str);
2848 ok = isl_union_set_is_equal(uset1, uset2);
2849 isl_union_set_free(uset1);
2850 isl_union_set_free(uset2);
2854 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2859 int test_equal(isl_ctx *ctx)
2862 isl_set *set, *set2;
2866 set = isl_set_read_from_str(ctx, str);
2868 set2 = isl_set_read_from_str(ctx, str);
2869 equal = isl_set_is_equal(set, set2);
2875 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2880 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
2881 enum isl_dim_type type, unsigned pos, int fixed)
2885 test = isl_map_plain_is_fixed(map, type, pos, NULL);
2892 isl_die(ctx, isl_error_unknown,
2893 "map not detected as fixed", return -1);
2895 isl_die(ctx, isl_error_unknown,
2896 "map detected as fixed", return -1);
2899 int test_fixed(isl_ctx *ctx)
2904 str = "{ [i] -> [i] }";
2905 map = isl_map_read_from_str(ctx, str);
2906 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
2908 str = "{ [i] -> [1] }";
2909 map = isl_map_read_from_str(ctx, str);
2910 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2912 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
2913 map = isl_map_read_from_str(ctx, str);
2914 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2916 map = isl_map_read_from_str(ctx, str);
2917 map = isl_map_neg(map);
2918 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2924 int test_vertices(isl_ctx *ctx)
2927 isl_basic_set *bset;
2928 isl_vertices *vertices;
2930 str = "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
2931 bset = isl_basic_set_read_from_str(ctx, str);
2932 vertices = isl_basic_set_compute_vertices(bset);
2933 isl_basic_set_free(bset);
2934 isl_vertices_free(vertices);
2938 str = "{ A[t, i] : t = 14 and i = 1 }";
2939 bset = isl_basic_set_read_from_str(ctx, str);
2940 vertices = isl_basic_set_compute_vertices(bset);
2941 isl_basic_set_free(bset);
2942 isl_vertices_free(vertices);
2949 int test_union_pw(isl_ctx *ctx)
2953 isl_union_set *uset;
2954 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
2956 str = "{ [x] -> x^2 }";
2957 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
2958 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
2959 uset = isl_union_pw_qpolynomial_domain(upwqp1);
2960 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
2961 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
2962 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
2963 isl_union_pw_qpolynomial_free(upwqp1);
2964 isl_union_pw_qpolynomial_free(upwqp2);
2968 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2973 int test_output(isl_ctx *ctx)
2981 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
2982 pa = isl_pw_aff_read_from_str(ctx, str);
2984 p = isl_printer_to_str(ctx);
2985 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2986 p = isl_printer_print_pw_aff(p, pa);
2987 s = isl_printer_get_str(p);
2988 isl_printer_free(p);
2989 isl_pw_aff_free(pa);
2993 equal = !strcmp(s, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
2998 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3003 int test_sample(isl_ctx *ctx)
3006 isl_basic_set *bset1, *bset2;
3009 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3010 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3011 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3012 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3013 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3014 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3015 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3016 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3017 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3018 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3019 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3020 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3021 "d - 1073741821e and "
3022 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3023 "3j >= 1 - a + b + 2e and "
3024 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3025 "3i <= 4 - a + 4b - e and "
3026 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3027 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3028 "c <= -1 + a and 3i >= -2 - a + 3e and "
3029 "1073741822e <= 1073741823 - a + 1073741822b + c and "
3030 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3031 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3032 "1073741823e >= 1 + 1073741823b - d and "
3033 "3i >= 1073741823b + c - 1073741823e - f and "
3034 "3i >= 1 + 2b + e + 3g }";
3035 bset1 = isl_basic_set_read_from_str(ctx, str);
3036 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3037 empty = isl_basic_set_is_empty(bset2);
3038 subset = isl_basic_set_is_subset(bset2, bset1);
3039 isl_basic_set_free(bset1);
3040 isl_basic_set_free(bset2);
3041 if (empty < 0 || subset < 0)
3044 isl_die(ctx, isl_error_unknown, "point not found", return -1);
3046 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
3051 int test_fixed_power(isl_ctx *ctx)
3059 str = "{ [i] -> [i + 1] }";
3060 map = isl_map_read_from_str(ctx, str);
3061 isl_int_set_si(exp, 23);
3062 map = isl_map_fixed_power(map, exp);
3063 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
3072 int test_slice(isl_ctx *ctx)
3078 str = "{ [i] -> [j] }";
3079 map = isl_map_read_from_str(ctx, str);
3080 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
3081 equal = map_check_equal(map, "{ [i] -> [i] }");
3086 str = "{ [i] -> [j] }";
3087 map = isl_map_read_from_str(ctx, str);
3088 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
3089 equal = map_check_equal(map, "{ [i] -> [j] }");
3094 str = "{ [i] -> [j] }";
3095 map = isl_map_read_from_str(ctx, str);
3096 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
3097 equal = map_check_equal(map, "{ [i] -> [-i] }");
3102 str = "{ [i] -> [j] }";
3103 map = isl_map_read_from_str(ctx, str);
3104 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
3105 equal = map_check_equal(map, "{ [0] -> [j] }");
3110 str = "{ [i] -> [j] }";
3111 map = isl_map_read_from_str(ctx, str);
3112 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3113 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
3118 str = "{ [i] -> [j] }";
3119 map = isl_map_read_from_str(ctx, str);
3120 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
3121 equal = map_check_equal(map, "{ [i] -> [j] : false }");
3129 int test_eliminate(isl_ctx *ctx)
3135 str = "{ [i] -> [j] : i = 2j }";
3136 map = isl_map_read_from_str(ctx, str);
3137 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
3138 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
3146 /* Check that isl_set_dim_residue_class detects that the values of j
3147 * in the set below are all odd and that it does not detect any spurious
3150 static int test_residue_class(isl_ctx *ctx)
3157 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3158 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3159 set = isl_set_read_from_str(ctx, str);
3162 res = isl_set_dim_residue_class(set, 1, &m, &r);
3164 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
3165 isl_die(ctx, isl_error_unknown, "incorrect residue class",
3174 int test_align_parameters(isl_ctx *ctx)
3178 isl_multi_aff *ma1, *ma2;
3181 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
3182 ma1 = isl_multi_aff_read_from_str(ctx, str);
3184 space = isl_space_params_alloc(ctx, 1);
3185 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
3186 ma1 = isl_multi_aff_align_params(ma1, space);
3188 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
3189 ma2 = isl_multi_aff_read_from_str(ctx, str);
3191 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3193 isl_multi_aff_free(ma1);
3194 isl_multi_aff_free(ma2);
3199 isl_die(ctx, isl_error_unknown,
3200 "result not as expected", return -1);
3205 static int test_list(isl_ctx *ctx)
3207 isl_id *a, *b, *c, *d, *id;
3211 a = isl_id_alloc(ctx, "a", NULL);
3212 b = isl_id_alloc(ctx, "b", NULL);
3213 c = isl_id_alloc(ctx, "c", NULL);
3214 d = isl_id_alloc(ctx, "d", NULL);
3216 list = isl_id_list_alloc(ctx, 4);
3217 list = isl_id_list_add(list, a);
3218 list = isl_id_list_add(list, b);
3219 list = isl_id_list_add(list, c);
3220 list = isl_id_list_add(list, d);
3221 list = isl_id_list_drop(list, 1, 1);
3223 if (isl_id_list_n_id(list) != 3) {
3224 isl_id_list_free(list);
3225 isl_die(ctx, isl_error_unknown,
3226 "unexpected number of elements in list", return -1);
3229 id = isl_id_list_get_id(list, 0);
3232 id = isl_id_list_get_id(list, 1);
3235 id = isl_id_list_get_id(list, 2);
3239 isl_id_list_free(list);
3242 isl_die(ctx, isl_error_unknown,
3243 "unexpected elements in list", return -1);
3248 const char *set_conversion_tests[] = {
3249 "[N] -> { [i] : N - 1 <= 2 i <= N }",
3250 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
3251 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3252 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3255 /* Check that converting from isl_set to isl_pw_multi_aff and back
3256 * to isl_set produces the original isl_set.
3258 static int test_set_conversion(isl_ctx *ctx)
3262 isl_set *set1, *set2;
3263 isl_pw_multi_aff *pma;
3266 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
3267 str = set_conversion_tests[i];
3268 set1 = isl_set_read_from_str(ctx, str);
3269 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
3270 set2 = isl_set_from_pw_multi_aff(pma);
3271 equal = isl_set_is_equal(set1, set2);
3278 isl_die(ctx, isl_error_unknown, "bad conversion",
3285 /* Check that converting from isl_map to isl_pw_multi_aff and back
3286 * to isl_map produces the original isl_map.
3288 static int test_map_conversion(isl_ctx *ctx)
3291 isl_map *map1, *map2;
3292 isl_pw_multi_aff *pma;
3295 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
3296 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
3297 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
3298 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
3300 map1 = isl_map_read_from_str(ctx, str);
3301 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
3302 map2 = isl_map_from_pw_multi_aff(pma);
3303 equal = isl_map_is_equal(map1, map2);
3310 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
3315 static int test_conversion(isl_ctx *ctx)
3317 if (test_set_conversion(ctx) < 0)
3319 if (test_map_conversion(ctx) < 0)
3324 /* Check that isl_basic_map_curry does not modify input.
3326 static int test_curry(isl_ctx *ctx)
3329 isl_basic_map *bmap1, *bmap2;
3332 str = "{ [A[] -> B[]] -> C[] }";
3333 bmap1 = isl_basic_map_read_from_str(ctx, str);
3334 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
3335 equal = isl_basic_map_is_equal(bmap1, bmap2);
3336 isl_basic_map_free(bmap1);
3337 isl_basic_map_free(bmap2);
3342 isl_die(ctx, isl_error_unknown,
3343 "curried map should not be equal to original",
3353 } preimage_tests[] = {
3354 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
3355 "{ A[j,i] -> B[i,j] }",
3356 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
3357 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3358 "{ A[a,b] -> B[a/2,b/6] }",
3359 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
3360 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3361 "{ A[a,b] -> B[a/2,b/6] }",
3362 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
3363 "exists i,j : a = 2 i and b = 6 j }" },
3364 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
3365 "[n] -> { : 0 <= n <= 100 }" },
3366 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3367 "{ A[a] -> B[2a] }",
3368 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
3369 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3370 "{ A[a] -> B[([a/2])] }",
3371 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
3372 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
3373 "{ A[a] -> B[a,a,a/3] }",
3374 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
3375 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
3376 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
3379 static int test_preimage_basic_set(isl_ctx *ctx)
3382 isl_basic_set *bset1, *bset2;
3386 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
3387 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
3388 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
3389 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
3390 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
3391 equal = isl_basic_set_is_equal(bset1, bset2);
3392 isl_basic_set_free(bset1);
3393 isl_basic_set_free(bset2);
3397 isl_die(ctx, isl_error_unknown, "bad preimage",
3408 } preimage_domain_tests[] = {
3409 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
3410 "{ A[j,i] -> B[i,j] }",
3411 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
3412 { "{ B[i] -> C[i]; D[i] -> E[i] }",
3413 "{ A[i] -> B[i + 1] }",
3414 "{ A[i] -> C[i + 1] }" },
3415 { "{ B[i] -> C[i]; B[i] -> E[i] }",
3416 "{ A[i] -> B[i + 1] }",
3417 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
3418 { "{ B[i] -> C[([i/2])] }",
3419 "{ A[i] -> B[2i] }",
3420 "{ A[i] -> C[i] }" },
3421 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
3422 "{ A[i] -> B[([i/5]), ([i/7])] }",
3423 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
3424 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
3425 "[N] -> { A[] -> B[([N/5])] }",
3426 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
3427 { "{ B[i] -> C[i] : exists a : i = 5 a }",
3428 "{ A[i] -> B[2i] }",
3429 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
3430 { "{ B[i] -> C[i] : exists a : i = 2 a; "
3431 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
3432 "{ A[i] -> B[2i] }",
3433 "{ A[i] -> C[2i] }" },
3436 static int test_preimage_union_map(isl_ctx *ctx)
3439 isl_union_map *umap1, *umap2;
3443 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
3444 umap1 = isl_union_map_read_from_str(ctx,
3445 preimage_domain_tests[i].map);
3446 ma = isl_multi_aff_read_from_str(ctx,
3447 preimage_domain_tests[i].ma);
3448 umap2 = isl_union_map_read_from_str(ctx,
3449 preimage_domain_tests[i].res);
3450 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
3451 equal = isl_union_map_is_equal(umap1, umap2);
3452 isl_union_map_free(umap1);
3453 isl_union_map_free(umap2);
3457 isl_die(ctx, isl_error_unknown, "bad preimage",
3464 static int test_preimage(isl_ctx *ctx)
3466 if (test_preimage_basic_set(ctx) < 0)
3468 if (test_preimage_union_map(ctx) < 0)
3478 } pullback_tests[] = {
3479 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
3480 "{ A[a,b] -> C[b + 2a] }" },
3481 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
3482 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
3483 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
3484 "{ A[a] -> C[(a)/6] }" },
3485 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
3486 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
3487 "{ A[a] -> C[(2a)/3] }" },
3488 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
3489 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
3490 "{ A[i,j] -> C[i + j, i + j] }"},
3491 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
3492 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
3493 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
3496 static int test_pullback(isl_ctx *ctx)
3499 isl_multi_aff *ma1, *ma2;
3503 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
3504 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
3505 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
3506 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
3507 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
3508 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3509 isl_multi_aff_free(ma1);
3510 isl_multi_aff_free(ma2);
3514 isl_die(ctx, isl_error_unknown, "bad pullback",
3521 /* Check that negation is printed correctly.
3523 static int test_ast(isl_ctx *ctx)
3525 isl_ast_expr *expr, *expr1, *expr2, *expr3;
3529 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
3530 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
3531 expr = isl_ast_expr_add(expr1, expr2);
3532 expr = isl_ast_expr_neg(expr);
3533 str = isl_ast_expr_to_str(expr);
3534 ok = str ? !strcmp(str, "-(A + B)") : -1;
3536 isl_ast_expr_free(expr);
3541 isl_die(ctx, isl_error_unknown,
3542 "isl_ast_expr printed incorrectly", return -1);
3544 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
3545 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
3546 expr = isl_ast_expr_add(expr1, expr2);
3547 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
3548 expr = isl_ast_expr_sub(expr3, expr);
3549 str = isl_ast_expr_to_str(expr);
3550 ok = str ? !strcmp(str, "C - (A + B)") : -1;
3552 isl_ast_expr_free(expr);
3557 isl_die(ctx, isl_error_unknown,
3558 "isl_ast_expr printed incorrectly", return -1);
3563 /* Internal data structure for before_for and after_for callbacks.
3565 * depth is the current depth
3566 * before is the number of times before_for has been called
3567 * after is the number of times after_for has been called
3569 struct isl_test_codegen_data {
3575 /* This function is called before each for loop in the AST generated
3576 * from test_ast_gen1.
3578 * Increment the number of calls and the depth.
3579 * Check that the space returned by isl_ast_build_get_schedule_space
3580 * matches the target space of the schedule returned by
3581 * isl_ast_build_get_schedule.
3582 * Return an isl_id that is checked by the corresponding call
3585 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
3588 struct isl_test_codegen_data *data = user;
3591 isl_union_map *schedule;
3592 isl_union_set *uset;
3597 ctx = isl_ast_build_get_ctx(build);
3599 if (data->before >= 3)
3600 isl_die(ctx, isl_error_unknown,
3601 "unexpected number of for nodes", return NULL);
3602 if (data->depth >= 2)
3603 isl_die(ctx, isl_error_unknown,
3604 "unexpected depth", return NULL);
3606 snprintf(name, sizeof(name), "d%d", data->depth);
3610 schedule = isl_ast_build_get_schedule(build);
3611 uset = isl_union_map_range(schedule);
3614 if (isl_union_set_n_set(uset) != 1) {
3615 isl_union_set_free(uset);
3616 isl_die(ctx, isl_error_unknown,
3617 "expecting single range space", return NULL);
3620 space = isl_ast_build_get_schedule_space(build);
3621 set = isl_union_set_extract_set(uset, space);
3622 isl_union_set_free(uset);
3623 empty = isl_set_is_empty(set);
3629 isl_die(ctx, isl_error_unknown,
3630 "spaces don't match", return NULL);
3632 return isl_id_alloc(ctx, name, NULL);
3635 /* This function is called after each for loop in the AST generated
3636 * from test_ast_gen1.
3638 * Increment the number of calls and decrement the depth.
3639 * Check that the annotation attached to the node matches
3640 * the isl_id returned by the corresponding call to before_for.
3642 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
3643 __isl_keep isl_ast_build *build, void *user)
3645 struct isl_test_codegen_data *data = user;
3653 if (data->after > data->before)
3654 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3655 "mismatch in number of for nodes",
3656 return isl_ast_node_free(node));
3658 id = isl_ast_node_get_annotation(node);
3660 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3661 "missing annotation", return isl_ast_node_free(node));
3663 name = isl_id_get_name(id);
3664 valid = name && atoi(name + 1) == data->depth;
3668 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3669 "wrong annotation", return isl_ast_node_free(node));
3674 /* Check that the before_each_for and after_each_for callbacks
3675 * are called for each for loop in the generated code,
3676 * that they are called in the right order and that the isl_id
3677 * returned from the before_each_for callback is attached to
3678 * the isl_ast_node passed to the corresponding after_each_for call.
3680 static int test_ast_gen1(isl_ctx *ctx)
3684 isl_union_map *schedule;
3685 isl_ast_build *build;
3687 struct isl_test_codegen_data data;
3689 str = "[N] -> { : N >= 10 }";
3690 set = isl_set_read_from_str(ctx, str);
3691 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
3692 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
3693 schedule = isl_union_map_read_from_str(ctx, str);
3698 build = isl_ast_build_from_context(set);
3699 build = isl_ast_build_set_before_each_for(build,
3700 &before_for, &data);
3701 build = isl_ast_build_set_after_each_for(build,
3703 tree = isl_ast_build_ast_from_schedule(build, schedule);
3704 isl_ast_build_free(build);
3708 isl_ast_node_free(tree);
3710 if (data.before != 3 || data.after != 3)
3711 isl_die(ctx, isl_error_unknown,
3712 "unexpected number of for nodes", return -1);
3717 /* Check that the AST generator handles domains that are integrally disjoint
3718 * but not ratinoally disjoint.
3720 static int test_ast_gen2(isl_ctx *ctx)
3724 isl_union_map *schedule;
3725 isl_union_map *options;
3726 isl_ast_build *build;
3729 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
3730 schedule = isl_union_map_read_from_str(ctx, str);
3731 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3732 build = isl_ast_build_from_context(set);
3734 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
3735 options = isl_union_map_read_from_str(ctx, str);
3736 build = isl_ast_build_set_options(build, options);
3737 tree = isl_ast_build_ast_from_schedule(build, schedule);
3738 isl_ast_build_free(build);
3741 isl_ast_node_free(tree);
3746 /* Increment *user on each call.
3748 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
3749 __isl_keep isl_ast_build *build, void *user)
3758 /* Test that unrolling tries to minimize the number of instances.
3759 * In particular, for the schedule given below, make sure it generates
3760 * 3 nodes (rather than 101).
3762 static int test_ast_gen3(isl_ctx *ctx)
3766 isl_union_map *schedule;
3767 isl_union_map *options;
3768 isl_ast_build *build;
3772 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
3773 schedule = isl_union_map_read_from_str(ctx, str);
3774 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3776 str = "{ [i] -> unroll[0] }";
3777 options = isl_union_map_read_from_str(ctx, str);
3779 build = isl_ast_build_from_context(set);
3780 build = isl_ast_build_set_options(build, options);
3781 build = isl_ast_build_set_at_each_domain(build,
3782 &count_domains, &n_domain);
3783 tree = isl_ast_build_ast_from_schedule(build, schedule);
3784 isl_ast_build_free(build);
3788 isl_ast_node_free(tree);
3791 isl_die(ctx, isl_error_unknown,
3792 "unexpected number of for nodes", return -1);
3797 /* Check that if the ast_build_exploit_nested_bounds options is set,
3798 * we do not get an outer if node in the generated AST,
3799 * while we do get such an outer if node if the options is not set.
3801 static int test_ast_gen4(isl_ctx *ctx)
3805 isl_union_map *schedule;
3806 isl_ast_build *build;
3808 enum isl_ast_node_type type;
3811 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
3812 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
3814 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
3816 schedule = isl_union_map_read_from_str(ctx, str);
3817 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3818 build = isl_ast_build_from_context(set);
3819 tree = isl_ast_build_ast_from_schedule(build, schedule);
3820 isl_ast_build_free(build);
3824 type = isl_ast_node_get_type(tree);
3825 isl_ast_node_free(tree);
3827 if (type == isl_ast_node_if)
3828 isl_die(ctx, isl_error_unknown,
3829 "not expecting if node", return -1);
3831 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
3833 schedule = isl_union_map_read_from_str(ctx, str);
3834 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3835 build = isl_ast_build_from_context(set);
3836 tree = isl_ast_build_ast_from_schedule(build, schedule);
3837 isl_ast_build_free(build);
3841 type = isl_ast_node_get_type(tree);
3842 isl_ast_node_free(tree);
3844 if (type != isl_ast_node_if)
3845 isl_die(ctx, isl_error_unknown,
3846 "expecting if node", return -1);
3848 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
3853 /* This function is called for each leaf in the AST generated
3854 * from test_ast_gen5.
3856 * We finalize the AST generation by extending the outer schedule
3857 * with a zero-dimensional schedule. If this results in any for loops,
3858 * then this means that we did not pass along enough information
3859 * about the outer schedule to the inner AST generation.
3861 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
3864 isl_union_map *schedule, *extra;
3867 schedule = isl_ast_build_get_schedule(build);
3868 extra = isl_union_map_copy(schedule);
3869 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
3870 schedule = isl_union_map_range_product(schedule, extra);
3871 tree = isl_ast_build_ast_from_schedule(build, schedule);
3872 isl_ast_build_free(build);
3877 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
3878 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
3879 "code should not contain any for loop",
3880 return isl_ast_node_free(tree));
3885 /* Check that we do not lose any information when going back and
3886 * forth between internal and external schedule.
3888 * In particular, we create an AST where we unroll the only
3889 * non-constant dimension in the schedule. We therefore do
3890 * not expect any for loops in the AST. However, older versions
3891 * of isl would not pass along enough information about the outer
3892 * schedule when performing an inner code generation from a create_leaf
3893 * callback, resulting in the inner code generation producing a for loop.
3895 static int test_ast_gen5(isl_ctx *ctx)
3899 isl_union_map *schedule, *options;
3900 isl_ast_build *build;
3903 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
3904 schedule = isl_union_map_read_from_str(ctx, str);
3906 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
3907 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
3908 options = isl_union_map_read_from_str(ctx, str);
3910 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3911 build = isl_ast_build_from_context(set);
3912 build = isl_ast_build_set_options(build, options);
3913 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
3914 tree = isl_ast_build_ast_from_schedule(build, schedule);
3915 isl_ast_build_free(build);
3916 isl_ast_node_free(tree);
3923 static int test_ast_gen(isl_ctx *ctx)
3925 if (test_ast_gen1(ctx) < 0)
3927 if (test_ast_gen2(ctx) < 0)
3929 if (test_ast_gen3(ctx) < 0)
3931 if (test_ast_gen4(ctx) < 0)
3933 if (test_ast_gen5(ctx) < 0)
3938 /* Check if dropping output dimensions from an isl_pw_multi_aff
3941 static int test_pw_multi_aff(isl_ctx *ctx)
3944 isl_pw_multi_aff *pma1, *pma2;
3947 str = "{ [i,j] -> [i+j, 4i-j] }";
3948 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3949 str = "{ [i,j] -> [4i-j] }";
3950 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3952 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
3954 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
3956 isl_pw_multi_aff_free(pma1);
3957 isl_pw_multi_aff_free(pma2);
3961 isl_die(ctx, isl_error_unknown,
3962 "expressions not equal", return -1);
3967 /* This is a regression test for a bug where isl_basic_map_simplify
3968 * would end up in an infinite loop. In particular, we construct
3969 * an empty basic set that is not obviously empty.
3970 * isl_basic_set_is_empty marks the basic set as empty.
3971 * After projecting out i3, the variable can be dropped completely,
3972 * but isl_basic_map_simplify refrains from doing so if the basic set
3973 * is empty and would end up in an infinite loop if it didn't test
3974 * explicitly for empty basic maps in the outer loop.
3976 static int test_simplify(isl_ctx *ctx)
3979 isl_basic_set *bset;
3982 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
3983 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
3984 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
3986 bset = isl_basic_set_read_from_str(ctx, str);
3987 empty = isl_basic_set_is_empty(bset);
3988 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
3989 isl_basic_set_free(bset);
3993 isl_die(ctx, isl_error_unknown,
3994 "basic set should be empty", return -1);
3999 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
4000 * with gbr context would fail to disable the use of the shifted tableau
4001 * when transferring equalities for the input to the context, resulting
4002 * in invalid sample values.
4004 static int test_partial_lexmin(isl_ctx *ctx)
4007 isl_basic_set *bset;
4008 isl_basic_map *bmap;
4011 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
4012 bmap = isl_basic_map_read_from_str(ctx, str);
4013 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
4014 bset = isl_basic_set_read_from_str(ctx, str);
4015 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
4024 /* Check that the variable compression performed on the existentially
4025 * quantified variables inside isl_basic_set_compute_divs is not confused
4026 * by the implicit equalities among the parameters.
4028 static int test_compute_divs(isl_ctx *ctx)
4031 isl_basic_set *bset;
4034 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
4035 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
4036 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
4037 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
4038 bset = isl_basic_set_read_from_str(ctx, str);
4039 set = isl_basic_set_compute_divs(bset);
4049 int (*fn)(isl_ctx *ctx);
4051 { "compute divs", &test_compute_divs },
4052 { "partial lexmin", &test_partial_lexmin },
4053 { "simplify", &test_simplify },
4054 { "curry", &test_curry },
4055 { "piecewise multi affine expressions", &test_pw_multi_aff },
4056 { "conversion", &test_conversion },
4057 { "list", &test_list },
4058 { "align parameters", &test_align_parameters },
4059 { "preimage", &test_preimage },
4060 { "pullback", &test_pullback },
4061 { "AST", &test_ast },
4062 { "AST generation", &test_ast_gen },
4063 { "eliminate", &test_eliminate },
4064 { "residue class", &test_residue_class },
4065 { "div", &test_div },
4066 { "slice", &test_slice },
4067 { "fixed power", &test_fixed_power },
4068 { "sample", &test_sample },
4069 { "output", &test_output },
4070 { "vertices", &test_vertices },
4071 { "fixed", &test_fixed },
4072 { "equal", &test_equal },
4073 { "product", &test_product },
4074 { "dim_max", &test_dim_max },
4075 { "affine", &test_aff },
4076 { "injective", &test_injective },
4077 { "schedule", &test_schedule },
4078 { "union_pw", &test_union_pw },
4079 { "parse", &test_parse },
4080 { "single-valued", &test_sv },
4081 { "affine hull", &test_affine_hull },
4082 { "coalesce", &test_coalesce },
4083 { "factorize", &test_factorize },
4084 { "subset", &test_subset },
4085 { "subtract", &test_subtract },
4086 { "lexmin", &test_lexmin },
4092 struct isl_ctx *ctx;
4094 srcdir = getenv("srcdir");
4097 ctx = isl_ctx_alloc();
4098 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
4099 printf("%s\n", tests[i].name);
4100 if (tests[i].fn(ctx) < 0)
4106 test_split_periods(ctx);
4109 test_bijective(ctx);
4113 test_construction(ctx);
4115 test_application(ctx);
4116 test_convex_hull(ctx);