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)
725 test_affine_hull_case(ctx, "affine2");
726 test_affine_hull_case(ctx, "affine");
727 test_affine_hull_case(ctx, "affine3");
729 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
730 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
731 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
732 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
733 set = isl_set_read_from_str(ctx, str);
734 bset = isl_set_affine_hull(set);
735 n = isl_basic_set_dim(bset, isl_dim_div);
736 isl_basic_set_free(bset);
738 isl_die(ctx, isl_error_unknown, "not expecting any divs",
744 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
748 struct isl_basic_set *bset1, *bset2;
751 filename = get_filename(ctx, name, "polylib");
753 input = fopen(filename, "r");
756 bset1 = isl_basic_set_read_from_file(ctx, input);
757 bset2 = isl_basic_set_read_from_file(ctx, input);
759 set = isl_basic_set_union(bset1, bset2);
760 bset1 = isl_set_convex_hull(set);
762 bset2 = isl_basic_set_read_from_file(ctx, input);
764 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
766 isl_basic_set_free(bset1);
767 isl_basic_set_free(bset2);
773 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
775 const char *str1, *str2;
776 isl_set *set1, *set2;
777 int orig_convex = ctx->opt->convex;
778 ctx->opt->convex = convex;
780 test_convex_hull_case(ctx, "convex0");
781 test_convex_hull_case(ctx, "convex1");
782 test_convex_hull_case(ctx, "convex2");
783 test_convex_hull_case(ctx, "convex3");
784 test_convex_hull_case(ctx, "convex4");
785 test_convex_hull_case(ctx, "convex5");
786 test_convex_hull_case(ctx, "convex6");
787 test_convex_hull_case(ctx, "convex7");
788 test_convex_hull_case(ctx, "convex8");
789 test_convex_hull_case(ctx, "convex9");
790 test_convex_hull_case(ctx, "convex10");
791 test_convex_hull_case(ctx, "convex11");
792 test_convex_hull_case(ctx, "convex12");
793 test_convex_hull_case(ctx, "convex13");
794 test_convex_hull_case(ctx, "convex14");
795 test_convex_hull_case(ctx, "convex15");
797 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
798 "(i0 = 1 and i1 = 0 and i2 = 1) or "
799 "(i0 = 0 and i1 = 0 and i2 = 0) }";
800 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
801 set1 = isl_set_read_from_str(ctx, str1);
802 set2 = isl_set_read_from_str(ctx, str2);
803 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
804 assert(isl_set_is_equal(set1, set2));
808 ctx->opt->convex = orig_convex;
811 void test_convex_hull(struct isl_ctx *ctx)
813 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
814 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
817 void test_gist_case(struct isl_ctx *ctx, const char *name)
821 struct isl_basic_set *bset1, *bset2;
823 filename = get_filename(ctx, name, "polylib");
825 input = fopen(filename, "r");
828 bset1 = isl_basic_set_read_from_file(ctx, input);
829 bset2 = isl_basic_set_read_from_file(ctx, input);
831 bset1 = isl_basic_set_gist(bset1, bset2);
833 bset2 = isl_basic_set_read_from_file(ctx, input);
835 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
837 isl_basic_set_free(bset1);
838 isl_basic_set_free(bset2);
844 void test_gist(struct isl_ctx *ctx)
847 isl_basic_set *bset1, *bset2;
849 test_gist_case(ctx, "gist1");
851 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
852 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
853 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
854 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
855 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
856 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
857 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
858 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
859 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
860 bset1 = isl_basic_set_read_from_str(ctx, str);
861 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
862 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
863 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
864 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
865 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
866 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
867 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
868 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
869 bset2 = isl_basic_set_read_from_str(ctx, str);
870 bset1 = isl_basic_set_gist(bset1, bset2);
871 assert(bset1 && bset1->n_div == 0);
872 isl_basic_set_free(bset1);
875 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
881 set = isl_set_read_from_str(ctx, str);
882 set = isl_set_coalesce(set);
883 set2 = isl_set_read_from_str(ctx, str);
884 equal = isl_set_is_equal(set, set2);
885 one = set && set->n == 1;
892 isl_die(ctx, isl_error_unknown,
893 "coalesced set not equal to input", return -1);
894 if (check_one && !one)
895 isl_die(ctx, isl_error_unknown,
896 "coalesced set should not be a union", return -1);
901 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
906 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
907 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
909 if (test_coalesce_set(ctx,
910 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
911 "-x - y + 1 >= 0 and -3 <= z <= 3;"
912 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
913 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
914 "-10 <= y <= 0}", 1) < 0)
916 if (test_coalesce_set(ctx,
917 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
919 if (test_coalesce_set(ctx,
920 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
928 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
933 /* Inputs for coalescing tests.
934 * "str" is a string representation of the input set.
935 * "single_disjunct" is set if we expect the result to consist of
941 } coalesce_tests[] = {
942 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
943 "y >= x & x >= 2 & 5 >= y }" },
944 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
945 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
946 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
947 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
948 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
949 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
950 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
951 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
952 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
953 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
954 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
955 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
956 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
957 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
958 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
959 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
960 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
961 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
962 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
963 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
964 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
965 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
966 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
967 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
968 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
969 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
970 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
971 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
972 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
973 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
974 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
975 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
976 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
977 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
978 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
979 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
980 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
981 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
982 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
983 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
984 "[o0, o1, o2, o3, o4, o5, o6]] : "
985 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
986 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
987 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
988 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
989 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
990 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
991 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
992 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
993 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
994 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
995 "o6 >= i3 + i6 - o3 and M >= 0 and "
996 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
997 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
998 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
999 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1000 "(o0 = 0 and M >= 2 and N >= 3) or "
1001 "(M = 0 and o0 = 0 and N >= 3) }" },
1002 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1003 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1004 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1005 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1006 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1007 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1008 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1009 "(y = 3 and x = 1) }" },
1010 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1011 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1012 "i1 <= M and i3 <= M and i4 <= M) or "
1013 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1014 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1015 "i4 <= -1 + M) }" },
1016 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1017 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1018 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1019 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1020 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1021 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1022 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1023 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1024 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1025 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1026 { 0, "{ [a, b] : exists e : 2e = a and "
1027 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1028 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1029 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1030 "j >= 1 and j' <= i + j - i' and i >= 1; "
1032 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1033 "[i,j] : exists a : j = 3a }" },
1034 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1035 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1037 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1038 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1039 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1040 "c <= 6 + 8a and a >= 3; "
1041 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1042 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1045 /* Test the functionality of isl_set_coalesce.
1046 * That is, check that the output is always equal to the input
1047 * and in some cases that the result consists of a single disjunct.
1049 static int test_coalesce(struct isl_ctx *ctx)
1053 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1054 const char *str = coalesce_tests[i].str;
1055 int check_one = coalesce_tests[i].single_disjunct;
1056 if (test_coalesce_set(ctx, str, check_one) < 0)
1060 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1066 void test_closure(struct isl_ctx *ctx)
1070 isl_map *up, *right;
1071 isl_map *map, *map2;
1074 /* COCOA example 1 */
1075 map = isl_map_read_from_str(ctx,
1076 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1077 "1 <= i and i < n and 1 <= j and j < n or "
1078 "i2 = i + 1 and j2 = j - 1 and "
1079 "1 <= i and i < n and 2 <= j and j <= n }");
1080 map = isl_map_power(map, &exact);
1084 /* COCOA example 1 */
1085 map = isl_map_read_from_str(ctx,
1086 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1087 "1 <= i and i < n and 1 <= j and j < n or "
1088 "i2 = i + 1 and j2 = j - 1 and "
1089 "1 <= i and i < n and 2 <= j and j <= n }");
1090 map = isl_map_transitive_closure(map, &exact);
1092 map2 = isl_map_read_from_str(ctx,
1093 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1094 "1 <= i and i < n and 1 <= j and j <= n and "
1095 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1096 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1097 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1098 assert(isl_map_is_equal(map, map2));
1102 map = isl_map_read_from_str(ctx,
1103 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1104 " 0 <= y and y <= n }");
1105 map = isl_map_transitive_closure(map, &exact);
1106 map2 = isl_map_read_from_str(ctx,
1107 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1108 " 0 <= y and y <= n }");
1109 assert(isl_map_is_equal(map, map2));
1113 /* COCOA example 2 */
1114 map = isl_map_read_from_str(ctx,
1115 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1116 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1117 "i2 = i + 2 and j2 = j - 2 and "
1118 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1119 map = isl_map_transitive_closure(map, &exact);
1121 map2 = isl_map_read_from_str(ctx,
1122 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1123 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1124 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1125 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1126 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1127 assert(isl_map_is_equal(map, map2));
1131 /* COCOA Fig.2 left */
1132 map = isl_map_read_from_str(ctx,
1133 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1134 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1136 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1137 "j <= 2 i - 3 and j <= n - 2 or "
1138 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1139 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1140 map = isl_map_transitive_closure(map, &exact);
1144 /* COCOA Fig.2 right */
1145 map = isl_map_read_from_str(ctx,
1146 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1147 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1149 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1150 "j <= 2 i - 4 and j <= n - 3 or "
1151 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1152 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1153 map = isl_map_power(map, &exact);
1157 /* COCOA Fig.2 right */
1158 map = isl_map_read_from_str(ctx,
1159 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1160 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1162 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1163 "j <= 2 i - 4 and j <= n - 3 or "
1164 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1165 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1166 map = isl_map_transitive_closure(map, &exact);
1168 map2 = isl_map_read_from_str(ctx,
1169 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1170 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1171 "j <= n and 3 + i + 2 j <= 3 n and "
1172 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1173 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1174 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1175 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1176 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1177 assert(isl_map_is_equal(map, map2));
1181 /* COCOA Fig.1 right */
1182 dom = isl_set_read_from_str(ctx,
1183 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1184 "2 x - 3 y + 3 >= 0 }");
1185 right = isl_map_read_from_str(ctx,
1186 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1187 up = isl_map_read_from_str(ctx,
1188 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1189 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1190 right = isl_map_intersect_range(right, isl_set_copy(dom));
1191 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1192 up = isl_map_intersect_range(up, dom);
1193 map = isl_map_union(up, right);
1194 map = isl_map_transitive_closure(map, &exact);
1196 map2 = isl_map_read_from_str(ctx,
1197 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1198 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1199 assert(isl_map_is_equal(map, map2));
1203 /* COCOA Theorem 1 counter example */
1204 map = isl_map_read_from_str(ctx,
1205 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1206 "i2 = 1 and j2 = j or "
1207 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1208 map = isl_map_transitive_closure(map, &exact);
1212 map = isl_map_read_from_str(ctx,
1213 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1214 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1215 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1216 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1217 map = isl_map_transitive_closure(map, &exact);
1221 /* Kelly et al 1996, fig 12 */
1222 map = isl_map_read_from_str(ctx,
1223 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1224 "1 <= i,j,j+1 <= n or "
1225 "j = n and j2 = 1 and i2 = i + 1 and "
1226 "1 <= i,i+1 <= n }");
1227 map = isl_map_transitive_closure(map, &exact);
1229 map2 = isl_map_read_from_str(ctx,
1230 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1231 "1 <= i <= n and i = i2 or "
1232 "1 <= i < i2 <= n and 1 <= j <= n and "
1234 assert(isl_map_is_equal(map, map2));
1238 /* Omega's closure4 */
1239 map = isl_map_read_from_str(ctx,
1240 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1241 "1 <= x,y <= 10 or "
1242 "x2 = x + 1 and y2 = y and "
1243 "1 <= x <= 20 && 5 <= y <= 15 }");
1244 map = isl_map_transitive_closure(map, &exact);
1248 map = isl_map_read_from_str(ctx,
1249 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1250 map = isl_map_transitive_closure(map, &exact);
1252 map2 = isl_map_read_from_str(ctx,
1253 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1254 assert(isl_map_is_equal(map, map2));
1258 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1259 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1260 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1261 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1262 map = isl_map_read_from_str(ctx, str);
1263 map = isl_map_transitive_closure(map, &exact);
1265 map2 = isl_map_read_from_str(ctx, str);
1266 assert(isl_map_is_equal(map, map2));
1270 str = "{[0] -> [1]; [2] -> [3]}";
1271 map = isl_map_read_from_str(ctx, str);
1272 map = isl_map_transitive_closure(map, &exact);
1274 map2 = isl_map_read_from_str(ctx, str);
1275 assert(isl_map_is_equal(map, map2));
1279 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1280 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1281 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1282 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1283 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1284 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1285 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1286 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1287 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1288 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1289 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1290 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1291 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1292 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1293 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1294 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1295 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1296 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1297 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1298 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1299 map = isl_map_read_from_str(ctx, str);
1300 map = isl_map_transitive_closure(map, NULL);
1305 void test_lex(struct isl_ctx *ctx)
1310 dim = isl_space_set_alloc(ctx, 0, 0);
1311 map = isl_map_lex_le(dim);
1312 assert(!isl_map_is_empty(map));
1316 static int test_lexmin(struct isl_ctx *ctx)
1320 isl_basic_map *bmap;
1321 isl_map *map, *map2;
1324 isl_pw_multi_aff *pma;
1326 str = "[p0, p1] -> { [] -> [] : "
1327 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1328 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1329 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1330 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1331 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1332 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1333 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1334 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1335 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1336 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1337 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1338 map = isl_map_read_from_str(ctx, str);
1339 map = isl_map_lexmin(map);
1342 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1343 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1344 set = isl_set_read_from_str(ctx, str);
1345 set = isl_set_lexmax(set);
1346 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1347 set2 = isl_set_read_from_str(ctx, str);
1348 set = isl_set_intersect(set, set2);
1349 assert(!isl_set_is_empty(set));
1352 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1353 map = isl_map_read_from_str(ctx, str);
1354 map = isl_map_lexmin(map);
1355 str = "{ [x] -> [5] : 6 <= x <= 8; "
1356 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1357 map2 = isl_map_read_from_str(ctx, str);
1358 assert(isl_map_is_equal(map, map2));
1362 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1363 map = isl_map_read_from_str(ctx, str);
1364 map2 = isl_map_copy(map);
1365 map = isl_map_lexmin(map);
1366 assert(isl_map_is_equal(map, map2));
1370 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1371 map = isl_map_read_from_str(ctx, str);
1372 map = isl_map_lexmin(map);
1373 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1374 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1375 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1376 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1377 map2 = isl_map_read_from_str(ctx, str);
1378 assert(isl_map_is_equal(map, map2));
1382 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1383 " 8i' <= i and 8i' >= -7 + i }";
1384 bmap = isl_basic_map_read_from_str(ctx, str);
1385 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1386 map2 = isl_map_from_pw_multi_aff(pma);
1387 map = isl_map_from_basic_map(bmap);
1388 assert(isl_map_is_equal(map, map2));
1392 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1393 map = isl_map_read_from_str(ctx, str);
1394 map = isl_map_lexmin(map);
1395 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1396 map2 = isl_map_read_from_str(ctx, str);
1397 assert(isl_map_is_equal(map, map2));
1401 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1402 " 8i' <= i and 8i' >= -7 + i }";
1403 set = isl_set_read_from_str(ctx, str);
1404 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1405 set2 = isl_set_from_pw_multi_aff(pma);
1406 equal = isl_set_is_equal(set, set2);
1412 isl_die(ctx, isl_error_unknown,
1413 "unexpected difference between set and "
1414 "piecewise affine expression", return -1);
1424 static int collect_must_may(__isl_take isl_map *dep, int must,
1425 void *dep_user, void *user)
1427 struct must_may *mm = (struct must_may *)user;
1430 mm->must = isl_map_union(mm->must, dep);
1432 mm->may = isl_map_union(mm->may, dep);
1437 static int common_space(void *first, void *second)
1439 int depth = *(int *)first;
1443 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1451 map2 = isl_map_read_from_str(map->ctx, str);
1452 equal = isl_map_is_equal(map, map2);
1458 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1462 equal = map_is_equal(map, str);
1466 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1467 "result not as expected", return -1);
1471 void test_dep(struct isl_ctx *ctx)
1476 isl_access_info *ai;
1483 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1484 map = isl_map_read_from_str(ctx, str);
1485 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1487 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1488 map = isl_map_read_from_str(ctx, str);
1489 ai = isl_access_info_add_source(ai, map, 1, &depth);
1491 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1492 map = isl_map_read_from_str(ctx, str);
1493 ai = isl_access_info_add_source(ai, map, 1, &depth);
1495 flow = isl_access_info_compute_flow(ai);
1496 dim = isl_space_alloc(ctx, 0, 3, 3);
1497 mm.must = isl_map_empty(isl_space_copy(dim));
1498 mm.may = isl_map_empty(dim);
1500 isl_flow_foreach(flow, collect_must_may, &mm);
1502 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1503 " [1,10,0] -> [2,5,0] }";
1504 assert(map_is_equal(mm.must, str));
1505 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1506 assert(map_is_equal(mm.may, str));
1508 isl_map_free(mm.must);
1509 isl_map_free(mm.may);
1510 isl_flow_free(flow);
1513 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1514 map = isl_map_read_from_str(ctx, str);
1515 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1517 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1518 map = isl_map_read_from_str(ctx, str);
1519 ai = isl_access_info_add_source(ai, map, 1, &depth);
1521 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1522 map = isl_map_read_from_str(ctx, str);
1523 ai = isl_access_info_add_source(ai, map, 0, &depth);
1525 flow = isl_access_info_compute_flow(ai);
1526 dim = isl_space_alloc(ctx, 0, 3, 3);
1527 mm.must = isl_map_empty(isl_space_copy(dim));
1528 mm.may = isl_map_empty(dim);
1530 isl_flow_foreach(flow, collect_must_may, &mm);
1532 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1533 assert(map_is_equal(mm.must, str));
1534 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1535 assert(map_is_equal(mm.may, str));
1537 isl_map_free(mm.must);
1538 isl_map_free(mm.may);
1539 isl_flow_free(flow);
1542 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1543 map = isl_map_read_from_str(ctx, str);
1544 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1546 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1547 map = isl_map_read_from_str(ctx, str);
1548 ai = isl_access_info_add_source(ai, map, 0, &depth);
1550 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1551 map = isl_map_read_from_str(ctx, str);
1552 ai = isl_access_info_add_source(ai, map, 0, &depth);
1554 flow = isl_access_info_compute_flow(ai);
1555 dim = isl_space_alloc(ctx, 0, 3, 3);
1556 mm.must = isl_map_empty(isl_space_copy(dim));
1557 mm.may = isl_map_empty(dim);
1559 isl_flow_foreach(flow, collect_must_may, &mm);
1561 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1562 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1563 assert(map_is_equal(mm.may, str));
1564 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1565 assert(map_is_equal(mm.must, str));
1567 isl_map_free(mm.must);
1568 isl_map_free(mm.may);
1569 isl_flow_free(flow);
1572 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1573 map = isl_map_read_from_str(ctx, str);
1574 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1576 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1577 map = isl_map_read_from_str(ctx, str);
1578 ai = isl_access_info_add_source(ai, map, 0, &depth);
1580 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1581 map = isl_map_read_from_str(ctx, str);
1582 ai = isl_access_info_add_source(ai, map, 0, &depth);
1584 flow = isl_access_info_compute_flow(ai);
1585 dim = isl_space_alloc(ctx, 0, 3, 3);
1586 mm.must = isl_map_empty(isl_space_copy(dim));
1587 mm.may = isl_map_empty(dim);
1589 isl_flow_foreach(flow, collect_must_may, &mm);
1591 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1592 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1593 assert(map_is_equal(mm.may, str));
1594 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1595 assert(map_is_equal(mm.must, str));
1597 isl_map_free(mm.must);
1598 isl_map_free(mm.may);
1599 isl_flow_free(flow);
1602 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1603 map = isl_map_read_from_str(ctx, str);
1604 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1606 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1607 map = isl_map_read_from_str(ctx, str);
1608 ai = isl_access_info_add_source(ai, map, 0, &depth);
1610 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1611 map = isl_map_read_from_str(ctx, str);
1612 ai = isl_access_info_add_source(ai, map, 0, &depth);
1614 flow = isl_access_info_compute_flow(ai);
1615 dim = isl_space_alloc(ctx, 0, 3, 3);
1616 mm.must = isl_map_empty(isl_space_copy(dim));
1617 mm.may = isl_map_empty(dim);
1619 isl_flow_foreach(flow, collect_must_may, &mm);
1621 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1622 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1623 assert(map_is_equal(mm.may, str));
1624 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1625 assert(map_is_equal(mm.must, str));
1627 isl_map_free(mm.must);
1628 isl_map_free(mm.may);
1629 isl_flow_free(flow);
1634 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1635 map = isl_map_read_from_str(ctx, str);
1636 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1638 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1639 map = isl_map_read_from_str(ctx, str);
1640 ai = isl_access_info_add_source(ai, map, 1, &depth);
1642 flow = isl_access_info_compute_flow(ai);
1643 dim = isl_space_alloc(ctx, 0, 5, 5);
1644 mm.must = isl_map_empty(isl_space_copy(dim));
1645 mm.may = isl_map_empty(dim);
1647 isl_flow_foreach(flow, collect_must_may, &mm);
1649 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1650 assert(map_is_equal(mm.must, str));
1651 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1652 assert(map_is_equal(mm.may, str));
1654 isl_map_free(mm.must);
1655 isl_map_free(mm.may);
1656 isl_flow_free(flow);
1659 int test_sv(isl_ctx *ctx)
1663 isl_union_map *umap;
1666 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1667 map = isl_map_read_from_str(ctx, str);
1668 sv = isl_map_is_single_valued(map);
1673 isl_die(ctx, isl_error_internal,
1674 "map not detected as single valued", return -1);
1676 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1677 map = isl_map_read_from_str(ctx, str);
1678 sv = isl_map_is_single_valued(map);
1683 isl_die(ctx, isl_error_internal,
1684 "map detected as single valued", return -1);
1686 str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
1687 umap = isl_union_map_read_from_str(ctx, str);
1688 sv = isl_union_map_is_single_valued(umap);
1689 isl_union_map_free(umap);
1693 isl_die(ctx, isl_error_internal,
1694 "map not detected as single valued", return -1);
1696 str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
1697 umap = isl_union_map_read_from_str(ctx, str);
1698 sv = isl_union_map_is_single_valued(umap);
1699 isl_union_map_free(umap);
1703 isl_die(ctx, isl_error_internal,
1704 "map detected as single valued", return -1);
1709 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1713 map = isl_map_read_from_str(ctx, str);
1715 assert(isl_map_is_bijective(map));
1717 assert(!isl_map_is_bijective(map));
1721 void test_bijective(struct isl_ctx *ctx)
1723 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1724 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1725 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1726 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1727 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1728 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1729 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1730 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1731 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1732 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1733 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1734 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1735 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1738 void test_pwqp(struct isl_ctx *ctx)
1742 isl_pw_qpolynomial *pwqp1, *pwqp2;
1744 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1745 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1747 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1750 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1751 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1753 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1755 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1757 isl_pw_qpolynomial_free(pwqp1);
1759 str = "{ [i] -> i }";
1760 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1761 str = "{ [k] : exists a : k = 2a }";
1762 set = isl_set_read_from_str(ctx, str);
1763 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1764 str = "{ [i] -> i }";
1765 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1767 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1769 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1771 isl_pw_qpolynomial_free(pwqp1);
1773 str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
1774 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1776 set = isl_set_read_from_str(ctx, str);
1777 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1778 str = "{ [i] -> 16 }";
1779 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1781 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1783 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1785 isl_pw_qpolynomial_free(pwqp1);
1787 str = "{ [i] -> ([(i)/2]) }";
1788 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1789 str = "{ [k] : exists a : k = 2a+1 }";
1790 set = isl_set_read_from_str(ctx, str);
1791 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1792 str = "{ [i] -> -1/2 + 1/2 * i }";
1793 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1795 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1797 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1799 isl_pw_qpolynomial_free(pwqp1);
1801 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1802 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1803 str = "{ [i] -> ([(2 * [i/2])/5]) }";
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 = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1813 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1814 str = "{ [x] -> x }";
1815 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1817 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1819 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1821 isl_pw_qpolynomial_free(pwqp1);
1823 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
1824 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1825 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1826 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
1827 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1828 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1829 isl_pw_qpolynomial_free(pwqp1);
1832 void test_split_periods(isl_ctx *ctx)
1835 isl_pw_qpolynomial *pwqp;
1837 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1838 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
1839 "U >= 0; [U,V] -> U^2 : U >= 100 }";
1840 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1842 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1845 isl_pw_qpolynomial_free(pwqp);
1848 void test_union(isl_ctx *ctx)
1851 isl_union_set *uset1, *uset2;
1852 isl_union_map *umap1, *umap2;
1854 str = "{ [i] : 0 <= i <= 1 }";
1855 uset1 = isl_union_set_read_from_str(ctx, str);
1856 str = "{ [1] -> [0] }";
1857 umap1 = isl_union_map_read_from_str(ctx, str);
1859 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1860 assert(isl_union_map_is_equal(umap1, umap2));
1862 isl_union_map_free(umap1);
1863 isl_union_map_free(umap2);
1865 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1866 umap1 = isl_union_map_read_from_str(ctx, str);
1867 str = "{ A[i]; B[i] }";
1868 uset1 = isl_union_set_read_from_str(ctx, str);
1870 uset2 = isl_union_map_domain(umap1);
1872 assert(isl_union_set_is_equal(uset1, uset2));
1874 isl_union_set_free(uset1);
1875 isl_union_set_free(uset2);
1878 void test_bound(isl_ctx *ctx)
1881 isl_pw_qpolynomial *pwqp;
1882 isl_pw_qpolynomial_fold *pwf;
1884 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1885 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1886 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1887 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
1888 isl_pw_qpolynomial_fold_free(pwf);
1890 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
1891 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1892 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1893 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
1894 isl_pw_qpolynomial_fold_free(pwf);
1897 void test_lift(isl_ctx *ctx)
1900 isl_basic_map *bmap;
1901 isl_basic_set *bset;
1903 str = "{ [i0] : exists e0 : i0 = 4e0 }";
1904 bset = isl_basic_set_read_from_str(ctx, str);
1905 bset = isl_basic_set_lift(bset);
1906 bmap = isl_basic_map_from_range(bset);
1907 bset = isl_basic_map_domain(bmap);
1908 isl_basic_set_free(bset);
1915 } subset_tests[] = {
1917 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1918 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1919 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
1921 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
1922 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
1923 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
1924 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
1925 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
1926 "256e0 <= 255i and 256e0 >= -255 + 255i and "
1927 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
1928 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
1929 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
1930 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
1931 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
1932 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
1933 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
1936 static int test_subset(isl_ctx *ctx)
1939 isl_set *set1, *set2;
1942 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
1943 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
1944 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
1945 subset = isl_set_is_subset(set1, set2);
1950 if (subset != subset_tests[i].subset)
1951 isl_die(ctx, isl_error_unknown,
1952 "incorrect subset result", return -1);
1959 const char *minuend;
1960 const char *subtrahend;
1961 const char *difference;
1962 } subtract_domain_tests[] = {
1963 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
1964 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
1965 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
1968 static int test_subtract(isl_ctx *ctx)
1971 isl_union_map *umap1, *umap2;
1972 isl_union_set *uset;
1975 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
1976 umap1 = isl_union_map_read_from_str(ctx,
1977 subtract_domain_tests[i].minuend);
1978 uset = isl_union_set_read_from_str(ctx,
1979 subtract_domain_tests[i].subtrahend);
1980 umap2 = isl_union_map_read_from_str(ctx,
1981 subtract_domain_tests[i].difference);
1982 umap1 = isl_union_map_subtract_domain(umap1, uset);
1983 equal = isl_union_map_is_equal(umap1, umap2);
1984 isl_union_map_free(umap1);
1985 isl_union_map_free(umap2);
1989 isl_die(ctx, isl_error_unknown,
1990 "incorrect subtract domain result", return -1);
1996 int test_factorize(isl_ctx *ctx)
1999 isl_basic_set *bset;
2002 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2003 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2004 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2005 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2006 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2007 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2008 "3i5 >= -2i0 - i2 + 3i4 }";
2009 bset = isl_basic_set_read_from_str(ctx, str);
2010 f = isl_basic_set_factorizer(bset);
2011 isl_basic_set_free(bset);
2012 isl_factorizer_free(f);
2014 isl_die(ctx, isl_error_unknown,
2015 "failed to construct factorizer", return -1);
2017 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2018 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2019 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2020 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2021 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2022 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2023 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2024 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2025 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2026 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2027 bset = isl_basic_set_read_from_str(ctx, str);
2028 f = isl_basic_set_factorizer(bset);
2029 isl_basic_set_free(bset);
2030 isl_factorizer_free(f);
2032 isl_die(ctx, isl_error_unknown,
2033 "failed to construct factorizer", return -1);
2038 static int check_injective(__isl_take isl_map *map, void *user)
2040 int *injective = user;
2042 *injective = isl_map_is_injective(map);
2045 if (*injective < 0 || !*injective)
2051 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2052 const char *r, const char *s, int tilable, int parallel)
2056 isl_union_map *W, *R, *S;
2057 isl_union_map *empty;
2058 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2059 isl_union_map *validity, *proximity;
2060 isl_union_map *schedule;
2061 isl_union_map *test;
2062 isl_union_set *delta;
2063 isl_union_set *domain;
2067 isl_schedule *sched;
2068 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2070 D = isl_union_set_read_from_str(ctx, d);
2071 W = isl_union_map_read_from_str(ctx, w);
2072 R = isl_union_map_read_from_str(ctx, r);
2073 S = isl_union_map_read_from_str(ctx, s);
2075 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2076 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2078 empty = isl_union_map_empty(isl_union_map_get_space(S));
2079 isl_union_map_compute_flow(isl_union_map_copy(R),
2080 isl_union_map_copy(W), empty,
2081 isl_union_map_copy(S),
2082 &dep_raw, NULL, NULL, NULL);
2083 isl_union_map_compute_flow(isl_union_map_copy(W),
2084 isl_union_map_copy(W),
2085 isl_union_map_copy(R),
2086 isl_union_map_copy(S),
2087 &dep_waw, &dep_war, NULL, NULL);
2089 dep = isl_union_map_union(dep_waw, dep_war);
2090 dep = isl_union_map_union(dep, dep_raw);
2091 validity = isl_union_map_copy(dep);
2092 proximity = isl_union_map_copy(dep);
2094 sched = isl_union_set_compute_schedule(isl_union_set_copy(D),
2095 validity, proximity);
2096 schedule = isl_schedule_get_map(sched);
2097 isl_schedule_free(sched);
2098 isl_union_map_free(W);
2099 isl_union_map_free(R);
2100 isl_union_map_free(S);
2103 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2105 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2106 is_complete = isl_union_set_is_subset(D, domain);
2107 isl_union_set_free(D);
2108 isl_union_set_free(domain);
2110 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2111 test = isl_union_map_apply_range(test, dep);
2112 test = isl_union_map_apply_range(test, schedule);
2114 delta = isl_union_map_deltas(test);
2115 if (isl_union_set_n_set(delta) == 0) {
2119 isl_union_set_free(delta);
2121 delta_set = isl_set_from_union_set(delta);
2123 slice = isl_set_universe(isl_set_get_space(delta_set));
2124 for (i = 0; i < tilable; ++i)
2125 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2126 is_tilable = isl_set_is_subset(delta_set, slice);
2127 isl_set_free(slice);
2129 slice = isl_set_universe(isl_set_get_space(delta_set));
2130 for (i = 0; i < parallel; ++i)
2131 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2132 is_parallel = isl_set_is_subset(delta_set, slice);
2133 isl_set_free(slice);
2135 origin = isl_set_universe(isl_set_get_space(delta_set));
2136 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2137 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2139 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2140 delta_set = isl_set_lexmin(delta_set);
2142 is_nonneg = isl_set_is_equal(delta_set, origin);
2144 isl_set_free(origin);
2145 isl_set_free(delta_set);
2148 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2149 is_injection < 0 || is_complete < 0)
2152 isl_die(ctx, isl_error_unknown,
2153 "generated schedule incomplete", return -1);
2155 isl_die(ctx, isl_error_unknown,
2156 "generated schedule not injective on each statement",
2159 isl_die(ctx, isl_error_unknown,
2160 "negative dependences in generated schedule",
2163 isl_die(ctx, isl_error_unknown,
2164 "generated schedule not as tilable as expected",
2167 isl_die(ctx, isl_error_unknown,
2168 "generated schedule not as parallel as expected",
2174 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2175 const char *domain, const char *validity, const char *proximity)
2179 isl_union_map *prox;
2180 isl_schedule *schedule;
2181 isl_union_map *sched;
2183 dom = isl_union_set_read_from_str(ctx, domain);
2184 dep = isl_union_map_read_from_str(ctx, validity);
2185 prox = isl_union_map_read_from_str(ctx, proximity);
2186 schedule = isl_union_set_compute_schedule(dom, dep, prox);
2187 sched = isl_schedule_get_map(schedule);
2188 isl_schedule_free(schedule);
2193 /* Check that a schedule can be constructed on the given domain
2194 * with the given validity and proximity constraints.
2196 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2197 const char *validity, const char *proximity)
2199 isl_union_map *sched;
2201 sched = compute_schedule(ctx, domain, validity, proximity);
2205 isl_union_map_free(sched);
2209 int test_special_schedule(isl_ctx *ctx, const char *domain,
2210 const char *validity, const char *proximity, const char *expected_sched)
2212 isl_union_map *sched1, *sched2;
2215 sched1 = compute_schedule(ctx, domain, validity, proximity);
2216 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2218 equal = isl_union_map_is_equal(sched1, sched2);
2219 isl_union_map_free(sched1);
2220 isl_union_map_free(sched2);
2225 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2231 int test_schedule(isl_ctx *ctx)
2233 const char *D, *W, *R, *V, *P, *S;
2235 /* Handle resulting schedule with zero bands. */
2236 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2240 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2241 W = "{ S1[t,i] -> a[t,i] }";
2242 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2243 "S1[t,i] -> a[t-1,i+1] }";
2244 S = "{ S1[t,i] -> [t,i] }";
2245 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2248 /* Fig. 5 of CC2008 */
2249 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2251 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2252 "j >= 2 and j <= -1 + N }";
2253 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2254 "j >= 2 and j <= -1 + N; "
2255 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2256 "j >= 2 and j <= -1 + N }";
2257 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2258 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2261 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2262 W = "{ S1[i] -> a[i] }";
2263 R = "{ S2[i] -> a[i+1] }";
2264 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2265 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2268 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2269 W = "{ S1[i] -> a[i] }";
2270 R = "{ S2[i] -> a[9-i] }";
2271 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2272 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2275 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2276 W = "{ S1[i] -> a[i] }";
2277 R = "[N] -> { S2[i] -> a[N-1-i] }";
2278 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2279 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2282 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2283 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2284 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2285 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2286 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2289 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2290 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2291 R = "{ S2[i,j] -> a[i-1,j] }";
2292 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2293 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2296 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2297 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2298 R = "{ S2[i,j] -> a[i,j-1] }";
2299 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2300 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2303 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2304 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2305 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2306 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2307 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2308 "S_0[] -> [0, 0, 0] }";
2309 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2311 ctx->opt->schedule_parametric = 0;
2312 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2314 ctx->opt->schedule_parametric = 1;
2316 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2317 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2318 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2319 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2320 "S4[i] -> a[i,N] }";
2321 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2322 "S4[i] -> [4,i,0] }";
2323 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2326 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2327 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2329 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2331 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2333 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2334 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2337 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2338 " S_2[t] : t >= 0 and t <= -1 + N; "
2339 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2341 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2342 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2343 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2344 "i >= 0 and i <= -1 + N }";
2345 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2346 "i >= 0 and i <= -1 + N; "
2347 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2348 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2349 " S_0[t] -> [0, t, 0] }";
2351 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2353 ctx->opt->schedule_parametric = 0;
2354 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2356 ctx->opt->schedule_parametric = 1;
2358 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2359 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2360 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2363 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2364 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2365 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2366 "j >= 0 and j <= -1 + N; "
2367 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2368 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2369 "j >= 0 and j <= -1 + N; "
2370 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2371 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2372 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2375 D = "{ S_0[i] : i >= 0 }";
2376 W = "{ S_0[i] -> a[i] : i >= 0 }";
2377 R = "{ S_0[i] -> a[0] : i >= 0 }";
2378 S = "{ S_0[i] -> [0, i, 0] }";
2379 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2382 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2383 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2384 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2385 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2386 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2389 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2390 "k <= -1 + n and k >= 0 }";
2391 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
2392 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2393 "k <= -1 + n and k >= 0; "
2394 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2395 "k <= -1 + n and k >= 0; "
2396 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2397 "k <= -1 + n and k >= 0 }";
2398 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2399 ctx->opt->schedule_outer_zero_distance = 1;
2400 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2402 ctx->opt->schedule_outer_zero_distance = 0;
2404 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
2405 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
2406 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
2407 "Stmt_for_body24[i0, i1, 1, 0]:"
2408 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
2409 "Stmt_for_body7[i0, i1, i2]:"
2410 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
2413 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
2414 "Stmt_for_body24[1, i1, i2, i3]:"
2415 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
2417 "Stmt_for_body24[0, i1, i2, i3] -> "
2418 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
2419 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
2421 "Stmt_for_body24[0, i1, i2, i3] ->"
2422 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
2423 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
2424 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
2425 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
2426 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
2427 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
2428 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
2429 "i1 <= 6 and i1 >= 0;"
2430 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
2431 "Stmt_for_body7[i0, i1, i2] -> "
2432 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
2433 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
2434 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
2435 "Stmt_for_body7[i0, i1, i2] -> "
2436 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
2437 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
2438 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
2440 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
2441 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
2442 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
2444 if (test_special_schedule(ctx, D, V, P, S) < 0)
2447 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
2448 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
2449 "j >= 1 and j <= 7;"
2450 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
2451 "j >= 1 and j <= 8 }";
2453 S = "{ S_0[i, j] -> [i + j, j] }";
2454 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2455 if (test_special_schedule(ctx, D, V, P, S) < 0)
2457 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2459 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
2460 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
2461 "j >= 0 and j <= -1 + i }";
2462 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
2463 "i <= -1 + N and j >= 0;"
2464 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
2467 S = "{ S_0[i, j] -> [i, j] }";
2468 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2469 if (test_special_schedule(ctx, D, V, P, S) < 0)
2471 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2473 /* Test both algorithms on a case with only proximity dependences. */
2474 D = "{ S[i,j] : 0 <= i <= 10 }";
2476 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
2477 S = "{ S[i, j] -> [j, i] }";
2478 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2479 if (test_special_schedule(ctx, D, V, P, S) < 0)
2481 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2482 if (test_special_schedule(ctx, D, V, P, S) < 0)
2485 D = "{ A[a]; B[] }";
2487 P = "{ A[a] -> B[] }";
2488 if (test_has_schedule(ctx, D, V, P) < 0)
2494 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
2496 isl_union_map *umap;
2499 umap = isl_union_map_read_from_str(ctx, str);
2500 test = isl_union_map_plain_is_injective(umap);
2501 isl_union_map_free(umap);
2504 if (test == injective)
2507 isl_die(ctx, isl_error_unknown,
2508 "map not detected as injective", return -1);
2510 isl_die(ctx, isl_error_unknown,
2511 "map detected as injective", return -1);
2514 int test_injective(isl_ctx *ctx)
2518 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2520 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
2522 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
2524 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
2526 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2528 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2530 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2532 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2535 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2536 if (test_plain_injective(ctx, str, 1))
2538 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2539 if (test_plain_injective(ctx, str, 0))
2545 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
2553 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
2554 equal = isl_aff_plain_is_equal(aff, aff2);
2560 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
2564 equal = aff_plain_is_equal(aff, str);
2568 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
2569 "result not as expected", return -1);
2573 int test_aff(isl_ctx *ctx)
2578 isl_local_space *ls;
2582 space = isl_space_set_alloc(ctx, 0, 1);
2583 ls = isl_local_space_from_space(space);
2584 aff = isl_aff_zero_on_domain(ls);
2586 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2587 aff = isl_aff_scale_down_ui(aff, 3);
2588 aff = isl_aff_floor(aff);
2589 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2590 aff = isl_aff_scale_down_ui(aff, 2);
2591 aff = isl_aff_floor(aff);
2592 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2595 set = isl_set_read_from_str(ctx, str);
2596 aff = isl_aff_gist(aff, set);
2598 aff = isl_aff_add_constant_si(aff, -16);
2599 zero = isl_aff_plain_is_zero(aff);
2605 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2607 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
2608 aff = isl_aff_scale_down_ui(aff, 64);
2609 aff = isl_aff_floor(aff);
2610 equal = aff_check_plain_equal(aff, "{ [-1] }");
2618 int test_dim_max(isl_ctx *ctx)
2622 isl_set *set1, *set2;
2627 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
2628 set = isl_set_read_from_str(ctx, str);
2629 pwaff = isl_set_dim_max(set, 0);
2630 set1 = isl_set_from_pw_aff(pwaff);
2631 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
2632 set2 = isl_set_read_from_str(ctx, str);
2633 equal = isl_set_is_equal(set1, set2);
2639 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2641 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
2642 set = isl_set_read_from_str(ctx, str);
2643 pwaff = isl_set_dim_max(set, 0);
2644 set1 = isl_set_from_pw_aff(pwaff);
2645 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2646 set2 = isl_set_read_from_str(ctx, str);
2647 equal = isl_set_is_equal(set1, set2);
2653 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2655 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
2656 set = isl_set_read_from_str(ctx, str);
2657 pwaff = isl_set_dim_max(set, 0);
2658 set1 = isl_set_from_pw_aff(pwaff);
2659 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2660 set2 = isl_set_read_from_str(ctx, str);
2661 equal = isl_set_is_equal(set1, set2);
2667 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2669 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
2670 "0 <= i < N and 0 <= j < M }";
2671 map = isl_map_read_from_str(ctx, str);
2672 set = isl_map_range(map);
2674 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
2675 set1 = isl_set_from_pw_aff(pwaff);
2676 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
2677 set2 = isl_set_read_from_str(ctx, str);
2678 equal = isl_set_is_equal(set1, set2);
2682 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
2683 set1 = isl_set_from_pw_aff(pwaff);
2684 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
2685 set2 = isl_set_read_from_str(ctx, str);
2686 if (equal >= 0 && equal)
2687 equal = isl_set_is_equal(set1, set2);
2696 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2698 /* Check that solutions are properly merged. */
2699 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
2700 "c <= -1 + n - 4a - 2b and c >= -2b and "
2701 "4a >= -4 + n and c >= 0 }";
2702 set = isl_set_read_from_str(ctx, str);
2703 pwaff = isl_set_dim_min(set, 2);
2704 set1 = isl_set_from_pw_aff(pwaff);
2705 str = "[n] -> { [(0)] : n >= 1 }";
2706 set2 = isl_set_read_from_str(ctx, str);
2707 equal = isl_set_is_equal(set1, set2);
2714 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2719 int test_product(isl_ctx *ctx)
2723 isl_union_set *uset1, *uset2;
2727 set = isl_set_read_from_str(ctx, str);
2728 set = isl_set_product(set, isl_set_copy(set));
2729 ok = isl_set_is_wrapping(set);
2734 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2737 uset1 = isl_union_set_read_from_str(ctx, str);
2738 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
2739 str = "{ [[] -> []] }";
2740 uset2 = isl_union_set_read_from_str(ctx, str);
2741 ok = isl_union_set_is_equal(uset1, uset2);
2742 isl_union_set_free(uset1);
2743 isl_union_set_free(uset2);
2747 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2752 int test_equal(isl_ctx *ctx)
2755 isl_set *set, *set2;
2759 set = isl_set_read_from_str(ctx, str);
2761 set2 = isl_set_read_from_str(ctx, str);
2762 equal = isl_set_is_equal(set, set2);
2768 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2773 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
2774 enum isl_dim_type type, unsigned pos, int fixed)
2778 test = isl_map_plain_is_fixed(map, type, pos, NULL);
2785 isl_die(ctx, isl_error_unknown,
2786 "map not detected as fixed", return -1);
2788 isl_die(ctx, isl_error_unknown,
2789 "map detected as fixed", return -1);
2792 int test_fixed(isl_ctx *ctx)
2797 str = "{ [i] -> [i] }";
2798 map = isl_map_read_from_str(ctx, str);
2799 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
2801 str = "{ [i] -> [1] }";
2802 map = isl_map_read_from_str(ctx, str);
2803 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2805 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
2806 map = isl_map_read_from_str(ctx, str);
2807 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2809 map = isl_map_read_from_str(ctx, str);
2810 map = isl_map_neg(map);
2811 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2817 int test_vertices(isl_ctx *ctx)
2820 isl_basic_set *bset;
2821 isl_vertices *vertices;
2823 str = "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
2824 bset = isl_basic_set_read_from_str(ctx, str);
2825 vertices = isl_basic_set_compute_vertices(bset);
2826 isl_basic_set_free(bset);
2827 isl_vertices_free(vertices);
2831 str = "{ A[t, i] : t = 14 and i = 1 }";
2832 bset = isl_basic_set_read_from_str(ctx, str);
2833 vertices = isl_basic_set_compute_vertices(bset);
2834 isl_basic_set_free(bset);
2835 isl_vertices_free(vertices);
2842 int test_union_pw(isl_ctx *ctx)
2846 isl_union_set *uset;
2847 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
2849 str = "{ [x] -> x^2 }";
2850 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
2851 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
2852 uset = isl_union_pw_qpolynomial_domain(upwqp1);
2853 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
2854 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
2855 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
2856 isl_union_pw_qpolynomial_free(upwqp1);
2857 isl_union_pw_qpolynomial_free(upwqp2);
2861 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2866 int test_output(isl_ctx *ctx)
2874 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
2875 pa = isl_pw_aff_read_from_str(ctx, str);
2877 p = isl_printer_to_str(ctx);
2878 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2879 p = isl_printer_print_pw_aff(p, pa);
2880 s = isl_printer_get_str(p);
2881 isl_printer_free(p);
2882 isl_pw_aff_free(pa);
2886 equal = !strcmp(s, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
2891 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2896 int test_sample(isl_ctx *ctx)
2899 isl_basic_set *bset1, *bset2;
2902 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
2903 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
2904 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
2905 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
2906 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
2907 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
2908 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
2909 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
2910 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
2911 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
2912 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
2913 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
2914 "d - 1073741821e and "
2915 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
2916 "3j >= 1 - a + b + 2e and "
2917 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
2918 "3i <= 4 - a + 4b - e and "
2919 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
2920 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
2921 "c <= -1 + a and 3i >= -2 - a + 3e and "
2922 "1073741822e <= 1073741823 - a + 1073741822b + c and "
2923 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
2924 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
2925 "1073741823e >= 1 + 1073741823b - d and "
2926 "3i >= 1073741823b + c - 1073741823e - f and "
2927 "3i >= 1 + 2b + e + 3g }";
2928 bset1 = isl_basic_set_read_from_str(ctx, str);
2929 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
2930 empty = isl_basic_set_is_empty(bset2);
2931 subset = isl_basic_set_is_subset(bset2, bset1);
2932 isl_basic_set_free(bset1);
2933 isl_basic_set_free(bset2);
2934 if (empty < 0 || subset < 0)
2937 isl_die(ctx, isl_error_unknown, "point not found", return -1);
2939 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
2944 int test_fixed_power(isl_ctx *ctx)
2952 str = "{ [i] -> [i + 1] }";
2953 map = isl_map_read_from_str(ctx, str);
2954 isl_int_set_si(exp, 23);
2955 map = isl_map_fixed_power(map, exp);
2956 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
2965 int test_slice(isl_ctx *ctx)
2971 str = "{ [i] -> [j] }";
2972 map = isl_map_read_from_str(ctx, str);
2973 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
2974 equal = map_check_equal(map, "{ [i] -> [i] }");
2979 str = "{ [i] -> [j] }";
2980 map = isl_map_read_from_str(ctx, str);
2981 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
2982 equal = map_check_equal(map, "{ [i] -> [j] }");
2987 str = "{ [i] -> [j] }";
2988 map = isl_map_read_from_str(ctx, str);
2989 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
2990 equal = map_check_equal(map, "{ [i] -> [-i] }");
2995 str = "{ [i] -> [j] }";
2996 map = isl_map_read_from_str(ctx, str);
2997 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
2998 equal = map_check_equal(map, "{ [0] -> [j] }");
3003 str = "{ [i] -> [j] }";
3004 map = isl_map_read_from_str(ctx, str);
3005 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3006 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
3011 str = "{ [i] -> [j] }";
3012 map = isl_map_read_from_str(ctx, str);
3013 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
3014 equal = map_check_equal(map, "{ [i] -> [j] : false }");
3022 int test_eliminate(isl_ctx *ctx)
3028 str = "{ [i] -> [j] : i = 2j }";
3029 map = isl_map_read_from_str(ctx, str);
3030 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
3031 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
3039 /* Check that isl_set_dim_residue_class detects that the values of j
3040 * in the set below are all odd and that it does not detect any spurious
3043 static int test_residue_class(isl_ctx *ctx)
3050 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3051 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3052 set = isl_set_read_from_str(ctx, str);
3055 res = isl_set_dim_residue_class(set, 1, &m, &r);
3057 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
3058 isl_die(ctx, isl_error_unknown, "incorrect residue class",
3067 int test_align_parameters(isl_ctx *ctx)
3071 isl_multi_aff *ma1, *ma2;
3074 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
3075 ma1 = isl_multi_aff_read_from_str(ctx, str);
3077 space = isl_space_params_alloc(ctx, 1);
3078 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
3079 ma1 = isl_multi_aff_align_params(ma1, space);
3081 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
3082 ma2 = isl_multi_aff_read_from_str(ctx, str);
3084 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3086 isl_multi_aff_free(ma1);
3087 isl_multi_aff_free(ma2);
3092 isl_die(ctx, isl_error_unknown,
3093 "result not as expected", return -1);
3098 static int test_list(isl_ctx *ctx)
3100 isl_id *a, *b, *c, *d, *id;
3104 a = isl_id_alloc(ctx, "a", NULL);
3105 b = isl_id_alloc(ctx, "b", NULL);
3106 c = isl_id_alloc(ctx, "c", NULL);
3107 d = isl_id_alloc(ctx, "d", NULL);
3109 list = isl_id_list_alloc(ctx, 4);
3110 list = isl_id_list_add(list, a);
3111 list = isl_id_list_add(list, b);
3112 list = isl_id_list_add(list, c);
3113 list = isl_id_list_add(list, d);
3114 list = isl_id_list_drop(list, 1, 1);
3116 if (isl_id_list_n_id(list) != 3) {
3117 isl_id_list_free(list);
3118 isl_die(ctx, isl_error_unknown,
3119 "unexpected number of elements in list", return -1);
3122 id = isl_id_list_get_id(list, 0);
3125 id = isl_id_list_get_id(list, 1);
3128 id = isl_id_list_get_id(list, 2);
3132 isl_id_list_free(list);
3135 isl_die(ctx, isl_error_unknown,
3136 "unexpected elements in list", return -1);
3141 const char *set_conversion_tests[] = {
3142 "[N] -> { [i] : N - 1 <= 2 i <= N }",
3143 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
3144 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3145 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3148 /* Check that converting from isl_set to isl_pw_multi_aff and back
3149 * to isl_set produces the original isl_set.
3151 static int test_set_conversion(isl_ctx *ctx)
3155 isl_set *set1, *set2;
3156 isl_pw_multi_aff *pma;
3159 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
3160 str = set_conversion_tests[i];
3161 set1 = isl_set_read_from_str(ctx, str);
3162 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
3163 set2 = isl_set_from_pw_multi_aff(pma);
3164 equal = isl_set_is_equal(set1, set2);
3171 isl_die(ctx, isl_error_unknown, "bad conversion",
3178 /* Check that converting from isl_map to isl_pw_multi_aff and back
3179 * to isl_map produces the original isl_map.
3181 static int test_map_conversion(isl_ctx *ctx)
3184 isl_map *map1, *map2;
3185 isl_pw_multi_aff *pma;
3188 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
3189 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
3190 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
3191 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
3193 map1 = isl_map_read_from_str(ctx, str);
3194 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
3195 map2 = isl_map_from_pw_multi_aff(pma);
3196 equal = isl_map_is_equal(map1, map2);
3203 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
3208 static int test_conversion(isl_ctx *ctx)
3210 if (test_set_conversion(ctx) < 0)
3212 if (test_map_conversion(ctx) < 0)
3217 /* Check that isl_basic_map_curry does not modify input.
3219 static int test_curry(isl_ctx *ctx)
3222 isl_basic_map *bmap1, *bmap2;
3225 str = "{ [A[] -> B[]] -> C[] }";
3226 bmap1 = isl_basic_map_read_from_str(ctx, str);
3227 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
3228 equal = isl_basic_map_is_equal(bmap1, bmap2);
3229 isl_basic_map_free(bmap1);
3230 isl_basic_map_free(bmap2);
3235 isl_die(ctx, isl_error_unknown,
3236 "curried map should not be equal to original",
3246 } preimage_tests[] = {
3247 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
3248 "{ A[j,i] -> B[i,j] }",
3249 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
3250 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3251 "{ A[a,b] -> B[a/2,b/6] }",
3252 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
3253 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3254 "{ A[a,b] -> B[a/2,b/6] }",
3255 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
3256 "exists i,j : a = 2 i and b = 6 j }" },
3257 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
3258 "[n] -> { : 0 <= n <= 100 }" },
3259 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3260 "{ A[a] -> B[2a] }",
3261 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
3262 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3263 "{ A[a] -> B[([a/2])] }",
3264 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
3265 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
3266 "{ A[a] -> B[a,a,a/3] }",
3267 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
3268 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
3269 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
3272 int test_preimage(isl_ctx *ctx)
3275 isl_basic_set *bset1, *bset2;
3279 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
3280 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
3281 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
3282 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
3283 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
3284 equal = isl_basic_set_is_equal(bset1, bset2);
3285 isl_basic_set_free(bset1);
3286 isl_basic_set_free(bset2);
3290 isl_die(ctx, isl_error_unknown, "bad preimage",
3301 } pullback_tests[] = {
3302 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
3303 "{ A[a,b] -> C[b + 2a] }" },
3304 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
3305 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
3306 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
3307 "{ A[a] -> C[(a)/6] }" },
3308 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
3309 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
3310 "{ A[a] -> C[(2a)/3] }" },
3311 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
3312 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
3313 "{ A[i,j] -> C[i + j, i + j] }"},
3314 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
3315 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
3316 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
3319 static int test_pullback(isl_ctx *ctx)
3322 isl_multi_aff *ma1, *ma2;
3326 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
3327 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
3328 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
3329 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
3330 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
3331 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
3332 isl_multi_aff_free(ma1);
3333 isl_multi_aff_free(ma2);
3337 isl_die(ctx, isl_error_unknown, "bad pullback",
3344 /* Check that negation is printed correctly.
3346 static int test_ast(isl_ctx *ctx)
3348 isl_ast_expr *expr, *expr1, *expr2, *expr3;
3352 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
3353 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
3354 expr = isl_ast_expr_add(expr1, expr2);
3355 expr = isl_ast_expr_neg(expr);
3356 str = isl_ast_expr_to_str(expr);
3357 ok = str ? !strcmp(str, "-(A + B)") : -1;
3359 isl_ast_expr_free(expr);
3364 isl_die(ctx, isl_error_unknown,
3365 "isl_ast_expr printed incorrectly", return -1);
3367 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
3368 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
3369 expr = isl_ast_expr_add(expr1, expr2);
3370 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
3371 expr = isl_ast_expr_sub(expr3, expr);
3372 str = isl_ast_expr_to_str(expr);
3373 ok = str ? !strcmp(str, "C - (A + B)") : -1;
3375 isl_ast_expr_free(expr);
3380 isl_die(ctx, isl_error_unknown,
3381 "isl_ast_expr printed incorrectly", return -1);
3386 /* Internal data structure for before_for and after_for callbacks.
3388 * depth is the current depth
3389 * before is the number of times before_for has been called
3390 * after is the number of times after_for has been called
3392 struct isl_test_codegen_data {
3398 /* This function is called before each for loop in the AST generated
3399 * from test_ast_gen1.
3401 * Increment the number of calls and the depth.
3402 * Check that the space returned by isl_ast_build_get_schedule_space
3403 * matches the target space of the schedule returned by
3404 * isl_ast_build_get_schedule.
3405 * Return an isl_id that is checked by the corresponding call
3408 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
3411 struct isl_test_codegen_data *data = user;
3414 isl_union_map *schedule;
3415 isl_union_set *uset;
3420 ctx = isl_ast_build_get_ctx(build);
3422 if (data->before >= 3)
3423 isl_die(ctx, isl_error_unknown,
3424 "unexpected number of for nodes", return NULL);
3425 if (data->depth >= 2)
3426 isl_die(ctx, isl_error_unknown,
3427 "unexpected depth", return NULL);
3429 snprintf(name, sizeof(name), "d%d", data->depth);
3433 schedule = isl_ast_build_get_schedule(build);
3434 uset = isl_union_map_range(schedule);
3437 if (isl_union_set_n_set(uset) != 1) {
3438 isl_union_set_free(uset);
3439 isl_die(ctx, isl_error_unknown,
3440 "expecting single range space", return NULL);
3443 space = isl_ast_build_get_schedule_space(build);
3444 set = isl_union_set_extract_set(uset, space);
3445 isl_union_set_free(uset);
3446 empty = isl_set_is_empty(set);
3452 isl_die(ctx, isl_error_unknown,
3453 "spaces don't match", return NULL);
3455 return isl_id_alloc(ctx, name, NULL);
3458 /* This function is called after each for loop in the AST generated
3459 * from test_ast_gen1.
3461 * Increment the number of calls and decrement the depth.
3462 * Check that the annotation attached to the node matches
3463 * the isl_id returned by the corresponding call to before_for.
3465 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
3466 __isl_keep isl_ast_build *build, void *user)
3468 struct isl_test_codegen_data *data = user;
3476 if (data->after > data->before)
3477 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3478 "mismatch in number of for nodes",
3479 return isl_ast_node_free(node));
3481 id = isl_ast_node_get_annotation(node);
3483 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3484 "missing annotation", return isl_ast_node_free(node));
3486 name = isl_id_get_name(id);
3487 valid = name && atoi(name + 1) == data->depth;
3491 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
3492 "wrong annotation", return isl_ast_node_free(node));
3497 /* Check that the before_each_for and after_each_for callbacks
3498 * are called for each for loop in the generated code,
3499 * that they are called in the right order and that the isl_id
3500 * returned from the before_each_for callback is attached to
3501 * the isl_ast_node passed to the corresponding after_each_for call.
3503 static int test_ast_gen1(isl_ctx *ctx)
3507 isl_union_map *schedule;
3508 isl_ast_build *build;
3510 struct isl_test_codegen_data data;
3512 str = "[N] -> { : N >= 10 }";
3513 set = isl_set_read_from_str(ctx, str);
3514 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
3515 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
3516 schedule = isl_union_map_read_from_str(ctx, str);
3521 build = isl_ast_build_from_context(set);
3522 build = isl_ast_build_set_before_each_for(build,
3523 &before_for, &data);
3524 build = isl_ast_build_set_after_each_for(build,
3526 tree = isl_ast_build_ast_from_schedule(build, schedule);
3527 isl_ast_build_free(build);
3531 isl_ast_node_free(tree);
3533 if (data.before != 3 || data.after != 3)
3534 isl_die(ctx, isl_error_unknown,
3535 "unexpected number of for nodes", return -1);
3540 /* Check that the AST generator handles domains that are integrally disjoint
3541 * but not ratinoally disjoint.
3543 static int test_ast_gen2(isl_ctx *ctx)
3547 isl_union_map *schedule;
3548 isl_union_map *options;
3549 isl_ast_build *build;
3552 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
3553 schedule = isl_union_map_read_from_str(ctx, str);
3554 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3555 build = isl_ast_build_from_context(set);
3557 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
3558 options = isl_union_map_read_from_str(ctx, str);
3559 build = isl_ast_build_set_options(build, options);
3560 tree = isl_ast_build_ast_from_schedule(build, schedule);
3561 isl_ast_build_free(build);
3564 isl_ast_node_free(tree);
3569 /* Increment *user on each call.
3571 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
3572 __isl_keep isl_ast_build *build, void *user)
3581 /* Test that unrolling tries to minimize the number of instances.
3582 * In particular, for the schedule given below, make sure it generates
3583 * 3 nodes (rather than 101).
3585 static int test_ast_gen3(isl_ctx *ctx)
3589 isl_union_map *schedule;
3590 isl_union_map *options;
3591 isl_ast_build *build;
3595 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
3596 schedule = isl_union_map_read_from_str(ctx, str);
3597 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3599 str = "{ [i] -> unroll[0] }";
3600 options = isl_union_map_read_from_str(ctx, str);
3602 build = isl_ast_build_from_context(set);
3603 build = isl_ast_build_set_options(build, options);
3604 build = isl_ast_build_set_at_each_domain(build,
3605 &count_domains, &n_domain);
3606 tree = isl_ast_build_ast_from_schedule(build, schedule);
3607 isl_ast_build_free(build);
3611 isl_ast_node_free(tree);
3614 isl_die(ctx, isl_error_unknown,
3615 "unexpected number of for nodes", return -1);
3620 /* Check that if the ast_build_exploit_nested_bounds options is set,
3621 * we do not get an outer if node in the generated AST,
3622 * while we do get such an outer if node if the options is not set.
3624 static int test_ast_gen4(isl_ctx *ctx)
3628 isl_union_map *schedule;
3629 isl_ast_build *build;
3631 enum isl_ast_node_type type;
3634 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
3635 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
3637 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
3639 schedule = isl_union_map_read_from_str(ctx, str);
3640 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3641 build = isl_ast_build_from_context(set);
3642 tree = isl_ast_build_ast_from_schedule(build, schedule);
3643 isl_ast_build_free(build);
3647 type = isl_ast_node_get_type(tree);
3648 isl_ast_node_free(tree);
3650 if (type == isl_ast_node_if)
3651 isl_die(ctx, isl_error_unknown,
3652 "not expecting if node", return -1);
3654 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
3656 schedule = isl_union_map_read_from_str(ctx, str);
3657 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
3658 build = isl_ast_build_from_context(set);
3659 tree = isl_ast_build_ast_from_schedule(build, schedule);
3660 isl_ast_build_free(build);
3664 type = isl_ast_node_get_type(tree);
3665 isl_ast_node_free(tree);
3667 if (type != isl_ast_node_if)
3668 isl_die(ctx, isl_error_unknown,
3669 "expecting if node", return -1);
3671 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
3676 static int test_ast_gen(isl_ctx *ctx)
3678 if (test_ast_gen1(ctx) < 0)
3680 if (test_ast_gen2(ctx) < 0)
3682 if (test_ast_gen3(ctx) < 0)
3684 if (test_ast_gen4(ctx) < 0)
3689 /* Check if dropping output dimensions from an isl_pw_multi_aff
3692 static int test_pw_multi_aff(isl_ctx *ctx)
3695 isl_pw_multi_aff *pma1, *pma2;
3698 str = "{ [i,j] -> [i+j, 4i-j] }";
3699 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3700 str = "{ [i,j] -> [4i-j] }";
3701 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3703 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
3705 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
3707 isl_pw_multi_aff_free(pma1);
3708 isl_pw_multi_aff_free(pma2);
3712 isl_die(ctx, isl_error_unknown,
3713 "expressions not equal", return -1);
3718 /* This is a regression test for a bug where isl_basic_map_simplify
3719 * would end up in an infinite loop. In particular, we construct
3720 * an empty basic set that is not obviously empty.
3721 * isl_basic_set_is_empty marks the basic set as empty.
3722 * After projecting out i3, the variable can be dropped completely,
3723 * but isl_basic_map_simplify refrains from doing so if the basic set
3724 * is empty and would end up in an infinite loop if it didn't test
3725 * explicitly for empty basic maps in the outer loop.
3727 static int test_simplify(isl_ctx *ctx)
3730 isl_basic_set *bset;
3733 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
3734 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
3735 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
3737 bset = isl_basic_set_read_from_str(ctx, str);
3738 empty = isl_basic_set_is_empty(bset);
3739 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
3740 isl_basic_set_free(bset);
3744 isl_die(ctx, isl_error_unknown,
3745 "basic set should be empty", return -1);
3750 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
3751 * with gbr context would fail to disable the use of the shifted tableau
3752 * when transferring equalities for the input to the context, resulting
3753 * in invalid sample values.
3755 static int test_partial_lexmin(isl_ctx *ctx)
3758 isl_basic_set *bset;
3759 isl_basic_map *bmap;
3762 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
3763 bmap = isl_basic_map_read_from_str(ctx, str);
3764 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
3765 bset = isl_basic_set_read_from_str(ctx, str);
3766 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
3775 /* Check that the variable compression performed on the existentially
3776 * quantified variables inside isl_basic_set_compute_divs is not confused
3777 * by the implicit equalities among the parameters.
3779 static int test_compute_divs(isl_ctx *ctx)
3782 isl_basic_set *bset;
3785 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
3786 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
3787 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
3788 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
3789 bset = isl_basic_set_read_from_str(ctx, str);
3790 set = isl_basic_set_compute_divs(bset);
3800 int (*fn)(isl_ctx *ctx);
3802 { "compute divs", &test_compute_divs },
3803 { "partial lexmin", &test_partial_lexmin },
3804 { "simplify", &test_simplify },
3805 { "curry", &test_curry },
3806 { "piecewise multi affine expressions", &test_pw_multi_aff },
3807 { "conversion", &test_conversion },
3808 { "list", &test_list },
3809 { "align parameters", &test_align_parameters },
3810 { "preimage", &test_preimage },
3811 { "pullback", &test_pullback },
3812 { "AST", &test_ast },
3813 { "AST generation", &test_ast_gen },
3814 { "eliminate", &test_eliminate },
3815 { "residue class", &test_residue_class },
3816 { "div", &test_div },
3817 { "slice", &test_slice },
3818 { "fixed power", &test_fixed_power },
3819 { "sample", &test_sample },
3820 { "output", &test_output },
3821 { "vertices", &test_vertices },
3822 { "fixed", &test_fixed },
3823 { "equal", &test_equal },
3824 { "product", &test_product },
3825 { "dim_max", &test_dim_max },
3826 { "affine", &test_aff },
3827 { "injective", &test_injective },
3828 { "schedule", &test_schedule },
3829 { "union_pw", &test_union_pw },
3830 { "parse", &test_parse },
3831 { "single-valued", &test_sv },
3832 { "affine hull", &test_affine_hull },
3833 { "coalesce", &test_coalesce },
3834 { "factorize", &test_factorize },
3835 { "subset", &test_subset },
3836 { "subtract", &test_subtract },
3837 { "lexmin", &test_lexmin },
3843 struct isl_ctx *ctx;
3845 srcdir = getenv("srcdir");
3848 ctx = isl_ctx_alloc();
3849 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
3850 printf("%s\n", tests[i].name);
3851 if (tests[i].fn(ctx) < 0)
3857 test_split_periods(ctx);
3860 test_bijective(ctx);
3864 test_construction(ctx);
3866 test_application(ctx);
3867 test_convex_hull(ctx);