2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 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>
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>
28 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
31 char *pattern = "%s/test_inputs/%s.%s";
33 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
35 filename = isl_alloc_array(ctx, char, length);
40 sprintf(filename, pattern, srcdir, name, suffix);
45 void test_parse_map(isl_ctx *ctx, const char *str)
49 map = isl_map_read_from_str(ctx, str);
54 void test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
58 map = isl_map_read_from_str(ctx, str);
59 map2 = isl_map_read_from_str(ctx, str2);
60 assert(map && map2 && isl_map_is_equal(map, map2));
65 void test_parse_pwqp(isl_ctx *ctx, const char *str)
67 isl_pw_qpolynomial *pwqp;
69 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
71 isl_pw_qpolynomial_free(pwqp);
74 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
78 pwaff = isl_pw_aff_read_from_str(ctx, str);
80 isl_pw_aff_free(pwaff);
83 void test_parse(struct isl_ctx *ctx)
86 const char *str, *str2;
88 str = "{ [i] -> [-i] }";
89 map = isl_map_read_from_str(ctx, str);
93 str = "{ A[i] -> L[([i/3])] }";
94 map = isl_map_read_from_str(ctx, str);
98 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
99 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
100 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
102 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
103 str2 = "{ [x, y] : 2y >= 6 - x }";
104 test_parse_map_equal(ctx, str, str2);
106 test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
107 "{ [x,y] : x <= y, 2*y + 3 }");
108 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
109 test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }", str);
111 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
112 map = isl_map_read_from_str(ctx, str);
113 str = "{ [new, old] -> [o0, o1] : "
114 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
115 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
116 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
117 map2 = isl_map_read_from_str(ctx, str);
118 assert(isl_map_is_equal(map, map2));
122 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
123 map = isl_map_read_from_str(ctx, str);
124 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
125 map2 = isl_map_read_from_str(ctx, str);
126 assert(isl_map_is_equal(map, map2));
130 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
131 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
132 test_parse_map_equal(ctx, str, str2);
134 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
135 str2 = "{ [i,j] -> [min(i,j)] }";
136 test_parse_map_equal(ctx, str, str2);
138 str = "{ [i,j] : i != j }";
139 str2 = "{ [i,j] : i < j or i > j }";
140 test_parse_map_equal(ctx, str, str2);
142 str = "{ [i,j] : (i+1)*2 >= j }";
143 str2 = "{ [i, j] : j <= 2 + 2i }";
144 test_parse_map_equal(ctx, str, str2);
146 str = "{ [i] -> [i > 0 ? 4 : 5] }";
147 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
148 test_parse_map_equal(ctx, str, str2);
150 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
151 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
152 test_parse_map_equal(ctx, str, str2);
154 str = "{ [x] : x >= 0 }";
155 str2 = "{ [x] : x-0 >= 0 }";
156 test_parse_map_equal(ctx, str, str2);
158 str = "{ [i] : ((i > 10)) }";
159 str2 = "{ [i] : i >= 11 }";
160 test_parse_map_equal(ctx, str, str2);
162 str = "{ [i] -> [0] }";
163 str2 = "{ [i] -> [0 * i] }";
164 test_parse_map_equal(ctx, str, str2);
166 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
167 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
168 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
169 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
172 void test_read(struct isl_ctx *ctx)
176 struct isl_basic_set *bset1, *bset2;
177 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
179 filename = get_filename(ctx, "set", "omega");
181 input = fopen(filename, "r");
184 bset1 = isl_basic_set_read_from_file(ctx, input);
185 bset2 = isl_basic_set_read_from_str(ctx, str);
187 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
189 isl_basic_set_free(bset1);
190 isl_basic_set_free(bset2);
196 void test_bounded(struct isl_ctx *ctx)
201 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
202 bounded = isl_set_is_bounded(set);
206 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
207 bounded = isl_set_is_bounded(set);
211 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
212 bounded = isl_set_is_bounded(set);
217 /* Construct the basic set { [i] : 5 <= i <= N } */
218 void test_construction(struct isl_ctx *ctx)
223 struct isl_basic_set *bset;
224 struct isl_constraint *c;
228 dim = isl_space_set_alloc(ctx, 1, 1);
229 bset = isl_basic_set_universe(isl_space_copy(dim));
230 ls = isl_local_space_from_space(dim);
232 c = isl_inequality_alloc(isl_local_space_copy(ls));
233 isl_int_set_si(v, -1);
234 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
235 isl_int_set_si(v, 1);
236 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
237 bset = isl_basic_set_add_constraint(bset, c);
239 c = isl_inequality_alloc(isl_local_space_copy(ls));
240 isl_int_set_si(v, 1);
241 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
242 isl_int_set_si(v, -5);
243 isl_constraint_set_constant(c, v);
244 bset = isl_basic_set_add_constraint(bset, c);
246 isl_local_space_free(ls);
247 isl_basic_set_free(bset);
252 void test_dim(struct isl_ctx *ctx)
255 isl_map *map1, *map2;
257 map1 = isl_map_read_from_str(ctx,
258 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
259 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
260 map2 = isl_map_read_from_str(ctx,
261 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
262 assert(isl_map_is_equal(map1, map2));
265 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
266 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
267 assert(isl_map_is_equal(map1, map2));
272 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
273 map1 = isl_map_read_from_str(ctx, str);
274 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
275 map2 = isl_map_read_from_str(ctx, str);
276 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
277 assert(isl_map_is_equal(map1, map2));
283 void test_div(struct isl_ctx *ctx)
288 struct isl_basic_set *bset;
289 struct isl_constraint *c;
294 dim = isl_space_set_alloc(ctx, 0, 3);
295 bset = isl_basic_set_universe(isl_space_copy(dim));
296 ls = isl_local_space_from_space(dim);
298 c = isl_equality_alloc(isl_local_space_copy(ls));
299 isl_int_set_si(v, -1);
300 isl_constraint_set_constant(c, v);
301 isl_int_set_si(v, 1);
302 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
303 isl_int_set_si(v, 3);
304 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
305 bset = isl_basic_set_add_constraint(bset, c);
307 c = isl_equality_alloc(isl_local_space_copy(ls));
308 isl_int_set_si(v, 1);
309 isl_constraint_set_constant(c, v);
310 isl_int_set_si(v, -1);
311 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
312 isl_int_set_si(v, 3);
313 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
314 bset = isl_basic_set_add_constraint(bset, c);
316 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
318 assert(bset && bset->n_div == 1);
319 isl_local_space_free(ls);
320 isl_basic_set_free(bset);
323 dim = isl_space_set_alloc(ctx, 0, 3);
324 bset = isl_basic_set_universe(isl_space_copy(dim));
325 ls = isl_local_space_from_space(dim);
327 c = isl_equality_alloc(isl_local_space_copy(ls));
328 isl_int_set_si(v, 1);
329 isl_constraint_set_constant(c, v);
330 isl_int_set_si(v, -1);
331 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
332 isl_int_set_si(v, 3);
333 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
334 bset = isl_basic_set_add_constraint(bset, c);
336 c = isl_equality_alloc(isl_local_space_copy(ls));
337 isl_int_set_si(v, -1);
338 isl_constraint_set_constant(c, v);
339 isl_int_set_si(v, 1);
340 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
341 isl_int_set_si(v, 3);
342 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
343 bset = isl_basic_set_add_constraint(bset, c);
345 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
347 assert(bset && bset->n_div == 1);
348 isl_local_space_free(ls);
349 isl_basic_set_free(bset);
352 dim = isl_space_set_alloc(ctx, 0, 3);
353 bset = isl_basic_set_universe(isl_space_copy(dim));
354 ls = isl_local_space_from_space(dim);
356 c = isl_equality_alloc(isl_local_space_copy(ls));
357 isl_int_set_si(v, 1);
358 isl_constraint_set_constant(c, v);
359 isl_int_set_si(v, -1);
360 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
361 isl_int_set_si(v, 3);
362 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
363 bset = isl_basic_set_add_constraint(bset, c);
365 c = isl_equality_alloc(isl_local_space_copy(ls));
366 isl_int_set_si(v, -3);
367 isl_constraint_set_constant(c, v);
368 isl_int_set_si(v, 1);
369 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
370 isl_int_set_si(v, 4);
371 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
372 bset = isl_basic_set_add_constraint(bset, c);
374 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
376 assert(bset && bset->n_div == 1);
377 isl_local_space_free(ls);
378 isl_basic_set_free(bset);
381 dim = isl_space_set_alloc(ctx, 0, 3);
382 bset = isl_basic_set_universe(isl_space_copy(dim));
383 ls = isl_local_space_from_space(dim);
385 c = isl_equality_alloc(isl_local_space_copy(ls));
386 isl_int_set_si(v, 2);
387 isl_constraint_set_constant(c, v);
388 isl_int_set_si(v, -1);
389 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
390 isl_int_set_si(v, 3);
391 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
392 bset = isl_basic_set_add_constraint(bset, c);
394 c = isl_equality_alloc(isl_local_space_copy(ls));
395 isl_int_set_si(v, -1);
396 isl_constraint_set_constant(c, v);
397 isl_int_set_si(v, 1);
398 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
399 isl_int_set_si(v, 6);
400 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
401 bset = isl_basic_set_add_constraint(bset, c);
403 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
405 assert(isl_basic_set_is_empty(bset));
406 isl_local_space_free(ls);
407 isl_basic_set_free(bset);
410 dim = isl_space_set_alloc(ctx, 0, 3);
411 bset = isl_basic_set_universe(isl_space_copy(dim));
412 ls = isl_local_space_from_space(dim);
414 c = isl_equality_alloc(isl_local_space_copy(ls));
415 isl_int_set_si(v, -1);
416 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
417 isl_int_set_si(v, 3);
418 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
419 bset = isl_basic_set_add_constraint(bset, c);
421 c = isl_equality_alloc(isl_local_space_copy(ls));
422 isl_int_set_si(v, 1);
423 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
424 isl_int_set_si(v, -3);
425 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
426 bset = isl_basic_set_add_constraint(bset, c);
428 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
430 assert(bset && bset->n_div == 0);
431 isl_basic_set_free(bset);
432 isl_local_space_free(ls);
435 dim = isl_space_set_alloc(ctx, 0, 3);
436 bset = isl_basic_set_universe(isl_space_copy(dim));
437 ls = isl_local_space_from_space(dim);
439 c = isl_equality_alloc(isl_local_space_copy(ls));
440 isl_int_set_si(v, -1);
441 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
442 isl_int_set_si(v, 6);
443 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
444 bset = isl_basic_set_add_constraint(bset, c);
446 c = isl_equality_alloc(isl_local_space_copy(ls));
447 isl_int_set_si(v, 1);
448 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
449 isl_int_set_si(v, -3);
450 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
451 bset = isl_basic_set_add_constraint(bset, c);
453 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
455 assert(bset && bset->n_div == 1);
456 isl_basic_set_free(bset);
457 isl_local_space_free(ls);
460 /* This test is a bit tricky. We set up an equality
462 * Normalization of divs creates _two_ divs
465 * Afterwards e0 is removed again because it has coefficient -1
466 * and we end up with the original equality and div again.
467 * Perhaps we can avoid the introduction of this temporary div.
469 dim = isl_space_set_alloc(ctx, 0, 4);
470 bset = isl_basic_set_universe(isl_space_copy(dim));
471 ls = isl_local_space_from_space(dim);
473 c = isl_equality_alloc(isl_local_space_copy(ls));
474 isl_int_set_si(v, -1);
475 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
476 isl_int_set_si(v, -3);
477 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
478 isl_int_set_si(v, -3);
479 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
480 isl_int_set_si(v, 6);
481 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
482 bset = isl_basic_set_add_constraint(bset, c);
484 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
486 /* Test disabled for now */
488 assert(bset && bset->n_div == 1);
490 isl_local_space_free(ls);
491 isl_basic_set_free(bset);
494 dim = isl_space_set_alloc(ctx, 0, 5);
495 bset = isl_basic_set_universe(isl_space_copy(dim));
496 ls = isl_local_space_from_space(dim);
498 c = isl_equality_alloc(isl_local_space_copy(ls));
499 isl_int_set_si(v, -1);
500 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
501 isl_int_set_si(v, -3);
502 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
503 isl_int_set_si(v, -3);
504 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
505 isl_int_set_si(v, 6);
506 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
507 bset = isl_basic_set_add_constraint(bset, c);
509 c = isl_equality_alloc(isl_local_space_copy(ls));
510 isl_int_set_si(v, -1);
511 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
512 isl_int_set_si(v, 1);
513 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
514 isl_int_set_si(v, 1);
515 isl_constraint_set_constant(c, v);
516 bset = isl_basic_set_add_constraint(bset, c);
518 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
520 /* Test disabled for now */
522 assert(bset && bset->n_div == 1);
524 isl_local_space_free(ls);
525 isl_basic_set_free(bset);
528 dim = isl_space_set_alloc(ctx, 0, 4);
529 bset = isl_basic_set_universe(isl_space_copy(dim));
530 ls = isl_local_space_from_space(dim);
532 c = isl_equality_alloc(isl_local_space_copy(ls));
533 isl_int_set_si(v, 1);
534 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
535 isl_int_set_si(v, -1);
536 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
537 isl_int_set_si(v, -2);
538 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
539 bset = isl_basic_set_add_constraint(bset, c);
541 c = isl_equality_alloc(isl_local_space_copy(ls));
542 isl_int_set_si(v, -1);
543 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
544 isl_int_set_si(v, 3);
545 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
546 isl_int_set_si(v, 2);
547 isl_constraint_set_constant(c, v);
548 bset = isl_basic_set_add_constraint(bset, c);
550 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
552 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
554 assert(!isl_basic_set_is_empty(bset));
556 isl_local_space_free(ls);
557 isl_basic_set_free(bset);
560 dim = isl_space_set_alloc(ctx, 0, 3);
561 bset = isl_basic_set_universe(isl_space_copy(dim));
562 ls = isl_local_space_from_space(dim);
564 c = isl_equality_alloc(isl_local_space_copy(ls));
565 isl_int_set_si(v, 1);
566 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
567 isl_int_set_si(v, -2);
568 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
569 bset = isl_basic_set_add_constraint(bset, c);
571 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
573 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
575 isl_local_space_free(ls);
576 isl_basic_set_free(bset);
581 void test_application_case(struct isl_ctx *ctx, const char *name)
585 struct isl_basic_set *bset1, *bset2;
586 struct isl_basic_map *bmap;
588 filename = get_filename(ctx, name, "omega");
590 input = fopen(filename, "r");
593 bset1 = isl_basic_set_read_from_file(ctx, input);
594 bmap = isl_basic_map_read_from_file(ctx, input);
596 bset1 = isl_basic_set_apply(bset1, bmap);
598 bset2 = isl_basic_set_read_from_file(ctx, input);
600 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
602 isl_basic_set_free(bset1);
603 isl_basic_set_free(bset2);
609 void test_application(struct isl_ctx *ctx)
611 test_application_case(ctx, "application");
612 test_application_case(ctx, "application2");
615 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
619 struct isl_basic_set *bset1, *bset2;
621 filename = get_filename(ctx, name, "polylib");
623 input = fopen(filename, "r");
626 bset1 = isl_basic_set_read_from_file(ctx, input);
627 bset2 = isl_basic_set_read_from_file(ctx, input);
629 bset1 = isl_basic_set_affine_hull(bset1);
631 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
633 isl_basic_set_free(bset1);
634 isl_basic_set_free(bset2);
640 void test_affine_hull(struct isl_ctx *ctx)
642 test_affine_hull_case(ctx, "affine2");
643 test_affine_hull_case(ctx, "affine");
644 test_affine_hull_case(ctx, "affine3");
647 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
651 struct isl_basic_set *bset1, *bset2;
654 filename = get_filename(ctx, name, "polylib");
656 input = fopen(filename, "r");
659 bset1 = isl_basic_set_read_from_file(ctx, input);
660 bset2 = isl_basic_set_read_from_file(ctx, input);
662 set = isl_basic_set_union(bset1, bset2);
663 bset1 = isl_set_convex_hull(set);
665 bset2 = isl_basic_set_read_from_file(ctx, input);
667 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
669 isl_basic_set_free(bset1);
670 isl_basic_set_free(bset2);
676 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
678 const char *str1, *str2;
679 isl_set *set1, *set2;
680 int orig_convex = ctx->opt->convex;
681 ctx->opt->convex = convex;
683 test_convex_hull_case(ctx, "convex0");
684 test_convex_hull_case(ctx, "convex1");
685 test_convex_hull_case(ctx, "convex2");
686 test_convex_hull_case(ctx, "convex3");
687 test_convex_hull_case(ctx, "convex4");
688 test_convex_hull_case(ctx, "convex5");
689 test_convex_hull_case(ctx, "convex6");
690 test_convex_hull_case(ctx, "convex7");
691 test_convex_hull_case(ctx, "convex8");
692 test_convex_hull_case(ctx, "convex9");
693 test_convex_hull_case(ctx, "convex10");
694 test_convex_hull_case(ctx, "convex11");
695 test_convex_hull_case(ctx, "convex12");
696 test_convex_hull_case(ctx, "convex13");
697 test_convex_hull_case(ctx, "convex14");
698 test_convex_hull_case(ctx, "convex15");
700 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
701 "(i0 = 1 and i1 = 0 and i2 = 1) or "
702 "(i0 = 0 and i1 = 0 and i2 = 0) }";
703 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
704 set1 = isl_set_read_from_str(ctx, str1);
705 set2 = isl_set_read_from_str(ctx, str2);
706 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
707 assert(isl_set_is_equal(set1, set2));
711 ctx->opt->convex = orig_convex;
714 void test_convex_hull(struct isl_ctx *ctx)
716 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
717 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
720 void test_gist_case(struct isl_ctx *ctx, const char *name)
724 struct isl_basic_set *bset1, *bset2;
726 filename = get_filename(ctx, name, "polylib");
728 input = fopen(filename, "r");
731 bset1 = isl_basic_set_read_from_file(ctx, input);
732 bset2 = isl_basic_set_read_from_file(ctx, input);
734 bset1 = isl_basic_set_gist(bset1, bset2);
736 bset2 = isl_basic_set_read_from_file(ctx, input);
738 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
740 isl_basic_set_free(bset1);
741 isl_basic_set_free(bset2);
747 void test_gist(struct isl_ctx *ctx)
750 isl_basic_set *bset1, *bset2;
752 test_gist_case(ctx, "gist1");
754 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
755 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
756 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
757 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
758 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
759 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
760 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
761 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
762 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
763 bset1 = isl_basic_set_read_from_str(ctx, str);
764 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
765 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
766 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
767 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
768 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
769 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
770 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
771 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
772 bset2 = isl_basic_set_read_from_str(ctx, str);
773 bset1 = isl_basic_set_gist(bset1, bset2);
774 assert(bset1 && bset1->n_div == 0);
775 isl_basic_set_free(bset1);
778 void test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
782 set = isl_set_read_from_str(ctx, str);
783 set = isl_set_coalesce(set);
784 set2 = isl_set_read_from_str(ctx, str);
785 assert(isl_set_is_equal(set, set2));
787 assert(set && set->n == 1);
792 void test_coalesce(struct isl_ctx *ctx)
795 struct isl_set *set, *set2;
796 struct isl_map *map, *map2;
798 set = isl_set_read_from_str(ctx,
799 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
800 "y >= x & x >= 2 & 5 >= y }");
801 set = isl_set_coalesce(set);
802 assert(set && set->n == 1);
805 set = isl_set_read_from_str(ctx,
806 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
807 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}");
808 set = isl_set_coalesce(set);
809 assert(set && set->n == 1);
812 set = isl_set_read_from_str(ctx,
813 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
814 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}");
815 set = isl_set_coalesce(set);
816 assert(set && set->n == 2);
819 set = isl_set_read_from_str(ctx,
820 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
821 "y >= 0 & x >= 6 & x <= 10 & y <= x}");
822 set = isl_set_coalesce(set);
823 assert(set && set->n == 1);
826 set = isl_set_read_from_str(ctx,
827 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
828 "y >= 0 & x >= 7 & x <= 10 & y <= x}");
829 set = isl_set_coalesce(set);
830 assert(set && set->n == 2);
833 set = isl_set_read_from_str(ctx,
834 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
835 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}");
836 set = isl_set_coalesce(set);
837 assert(set && set->n == 2);
840 set = isl_set_read_from_str(ctx,
841 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
842 "y >= 0 & x = 6 & y <= 6}");
843 set = isl_set_coalesce(set);
844 assert(set && set->n == 1);
847 set = isl_set_read_from_str(ctx,
848 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
849 "y >= 0 & x = 7 & y <= 6}");
850 set = isl_set_coalesce(set);
851 assert(set && set->n == 2);
854 set = isl_set_read_from_str(ctx,
855 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
856 "y >= 0 & x = 6 & y <= 5}");
857 set = isl_set_coalesce(set);
858 assert(set && set->n == 1);
859 set2 = isl_set_read_from_str(ctx,
860 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
861 "y >= 0 & x = 6 & y <= 5}");
862 assert(isl_set_is_equal(set, set2));
866 set = isl_set_read_from_str(ctx,
867 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
868 "y >= 0 & x = 6 & y <= 7}");
869 set = isl_set_coalesce(set);
870 assert(set && set->n == 2);
873 set = isl_set_read_from_str(ctx,
874 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
875 set = isl_set_coalesce(set);
876 assert(set && set->n == 1);
877 set2 = isl_set_read_from_str(ctx,
878 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
879 assert(isl_set_is_equal(set, set2));
883 set = isl_set_read_from_str(ctx,
884 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
885 set = isl_set_coalesce(set);
886 set2 = isl_set_read_from_str(ctx,
887 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
888 assert(isl_set_is_equal(set, set2));
892 set = isl_set_read_from_str(ctx,
893 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
894 "2 <= i and i <= n }");
895 set = isl_set_coalesce(set);
896 assert(set && set->n == 1);
897 set2 = isl_set_read_from_str(ctx,
898 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
899 "2 <= i and i <= n }");
900 assert(isl_set_is_equal(set, set2));
904 map = isl_map_read_from_str(ctx,
905 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
906 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
907 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
908 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
909 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
910 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
911 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
912 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
913 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
914 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
915 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
916 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
917 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
918 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
919 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
920 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
921 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
922 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
923 map = isl_map_coalesce(map);
924 map2 = isl_map_read_from_str(ctx,
925 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
926 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
927 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
928 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
929 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
930 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
931 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
932 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
933 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
934 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
935 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
936 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
937 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
938 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
939 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
940 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
941 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
942 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
943 assert(isl_map_is_equal(map, map2));
947 str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
948 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
949 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
950 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
951 map = isl_map_read_from_str(ctx, str);
952 map = isl_map_coalesce(map);
953 map2 = isl_map_read_from_str(ctx, str);
954 assert(isl_map_is_equal(map, map2));
958 str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
959 "[o0, o1, o2, o3, o4, o5, o6] : "
960 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
961 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
962 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
963 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
964 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
965 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
966 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
967 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
968 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
969 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
970 "o6 >= i3 + i6 - o3 and M >= 0 and "
971 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
972 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
973 map = isl_map_read_from_str(ctx, str);
974 map = isl_map_coalesce(map);
975 map2 = isl_map_read_from_str(ctx, str);
976 assert(isl_map_is_equal(map, map2));
980 str = "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
981 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
982 "(o0 = 0 and M >= 2 and N >= 3) or "
983 "(M = 0 and o0 = 0 and N >= 3) }";
984 map = isl_map_read_from_str(ctx, str);
985 map = isl_map_coalesce(map);
986 map2 = isl_map_read_from_str(ctx, str);
987 assert(isl_map_is_equal(map, map2));
991 str = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
992 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
993 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
994 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
995 map = isl_map_read_from_str(ctx, str);
996 map = isl_map_coalesce(map);
997 map2 = isl_map_read_from_str(ctx, str);
998 assert(isl_map_is_equal(map, map2));
1002 test_coalesce_set(ctx,
1003 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
1004 "(i1 = M and M >= 1) }", 0);
1005 test_coalesce_set(ctx,
1006 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
1007 test_coalesce_set(ctx,
1008 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1009 "(y = 3 and x = 1) }", 1);
1010 test_coalesce_set(ctx,
1011 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1012 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1013 "i1 <= M and i3 <= M and i4 <= M) or "
1014 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1015 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1016 "i4 <= -1 + M) }", 1);
1017 test_coalesce_set(ctx,
1018 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1019 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
1020 test_coalesce_set(ctx,
1021 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1022 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1023 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1024 "x-z + 20 >= 0 and x+z + 20 >= 0 and -10 <= y <= 0}", 1);
1025 test_coalesce_set(ctx,
1026 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1);
1027 test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0);
1028 test_coalesce_set(ctx,
1029 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1);
1030 test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1);
1031 test_coalesce_set(ctx, "{ [0,0]; [i,i] : 1 <= i <= 10 }", 1);
1032 test_coalesce_set(ctx, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }", 0);
1033 test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 1 <= i <= 10 }", 1);
1034 test_coalesce_set(ctx, "{ [0,0]; [i,2i] : 2 <= i <= 10 }", 0);
1035 test_coalesce_set(ctx, "{ [1,0]; [i,2i] : 1 <= i <= 10 }", 0);
1036 test_coalesce_set(ctx, "{ [0,1]; [i,2i] : 1 <= i <= 10 }", 0);
1039 void test_closure(struct isl_ctx *ctx)
1043 isl_map *up, *right;
1044 isl_map *map, *map2;
1047 /* COCOA example 1 */
1048 map = isl_map_read_from_str(ctx,
1049 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1050 "1 <= i and i < n and 1 <= j and j < n or "
1051 "i2 = i + 1 and j2 = j - 1 and "
1052 "1 <= i and i < n and 2 <= j and j <= n }");
1053 map = isl_map_power(map, &exact);
1057 /* COCOA example 1 */
1058 map = isl_map_read_from_str(ctx,
1059 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1060 "1 <= i and i < n and 1 <= j and j < n or "
1061 "i2 = i + 1 and j2 = j - 1 and "
1062 "1 <= i and i < n and 2 <= j and j <= n }");
1063 map = isl_map_transitive_closure(map, &exact);
1065 map2 = isl_map_read_from_str(ctx,
1066 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1067 "1 <= i and i < n and 1 <= j and j <= n and "
1068 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1069 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1070 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1071 assert(isl_map_is_equal(map, map2));
1075 map = isl_map_read_from_str(ctx,
1076 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1077 " 0 <= y and y <= n }");
1078 map = isl_map_transitive_closure(map, &exact);
1079 map2 = isl_map_read_from_str(ctx,
1080 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1081 " 0 <= y and y <= n }");
1082 assert(isl_map_is_equal(map, map2));
1086 /* COCOA example 2 */
1087 map = isl_map_read_from_str(ctx,
1088 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1089 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1090 "i2 = i + 2 and j2 = j - 2 and "
1091 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1092 map = isl_map_transitive_closure(map, &exact);
1094 map2 = isl_map_read_from_str(ctx,
1095 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1096 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1097 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1098 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1099 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1100 assert(isl_map_is_equal(map, map2));
1104 /* COCOA Fig.2 left */
1105 map = isl_map_read_from_str(ctx,
1106 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1107 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1109 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1110 "j <= 2 i - 3 and j <= n - 2 or "
1111 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1112 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1113 map = isl_map_transitive_closure(map, &exact);
1117 /* COCOA Fig.2 right */
1118 map = isl_map_read_from_str(ctx,
1119 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1120 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1122 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1123 "j <= 2 i - 4 and j <= n - 3 or "
1124 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1125 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1126 map = isl_map_power(map, &exact);
1130 /* COCOA Fig.2 right */
1131 map = isl_map_read_from_str(ctx,
1132 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1133 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1135 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1136 "j <= 2 i - 4 and j <= n - 3 or "
1137 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1138 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1139 map = isl_map_transitive_closure(map, &exact);
1141 map2 = isl_map_read_from_str(ctx,
1142 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1143 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1144 "j <= n and 3 + i + 2 j <= 3 n and "
1145 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1146 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1147 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1148 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1149 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1150 assert(isl_map_is_equal(map, map2));
1154 /* COCOA Fig.1 right */
1155 dom = isl_set_read_from_str(ctx,
1156 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1157 "2 x - 3 y + 3 >= 0 }");
1158 right = isl_map_read_from_str(ctx,
1159 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1160 up = isl_map_read_from_str(ctx,
1161 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1162 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1163 right = isl_map_intersect_range(right, isl_set_copy(dom));
1164 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1165 up = isl_map_intersect_range(up, dom);
1166 map = isl_map_union(up, right);
1167 map = isl_map_transitive_closure(map, &exact);
1169 map2 = isl_map_read_from_str(ctx,
1170 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1171 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1172 assert(isl_map_is_equal(map, map2));
1176 /* COCOA Theorem 1 counter example */
1177 map = isl_map_read_from_str(ctx,
1178 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1179 "i2 = 1 and j2 = j or "
1180 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1181 map = isl_map_transitive_closure(map, &exact);
1185 map = isl_map_read_from_str(ctx,
1186 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1187 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1188 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1189 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1190 map = isl_map_transitive_closure(map, &exact);
1194 /* Kelly et al 1996, fig 12 */
1195 map = isl_map_read_from_str(ctx,
1196 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1197 "1 <= i,j,j+1 <= n or "
1198 "j = n and j2 = 1 and i2 = i + 1 and "
1199 "1 <= i,i+1 <= n }");
1200 map = isl_map_transitive_closure(map, &exact);
1202 map2 = isl_map_read_from_str(ctx,
1203 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1204 "1 <= i <= n and i = i2 or "
1205 "1 <= i < i2 <= n and 1 <= j <= n and "
1207 assert(isl_map_is_equal(map, map2));
1211 /* Omega's closure4 */
1212 map = isl_map_read_from_str(ctx,
1213 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1214 "1 <= x,y <= 10 or "
1215 "x2 = x + 1 and y2 = y and "
1216 "1 <= x <= 20 && 5 <= y <= 15 }");
1217 map = isl_map_transitive_closure(map, &exact);
1221 map = isl_map_read_from_str(ctx,
1222 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1223 map = isl_map_transitive_closure(map, &exact);
1225 map2 = isl_map_read_from_str(ctx,
1226 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1227 assert(isl_map_is_equal(map, map2));
1231 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1232 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1233 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1234 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1235 map = isl_map_read_from_str(ctx, str);
1236 map = isl_map_transitive_closure(map, &exact);
1238 map2 = isl_map_read_from_str(ctx, str);
1239 assert(isl_map_is_equal(map, map2));
1243 str = "{[0] -> [1]; [2] -> [3]}";
1244 map = isl_map_read_from_str(ctx, str);
1245 map = isl_map_transitive_closure(map, &exact);
1247 map2 = isl_map_read_from_str(ctx, str);
1248 assert(isl_map_is_equal(map, map2));
1252 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1253 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1254 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1255 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1256 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1257 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1258 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1259 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1260 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1261 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1262 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1263 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1264 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1265 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1266 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1267 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1268 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1269 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1270 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1271 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1272 map = isl_map_read_from_str(ctx, str);
1273 map = isl_map_transitive_closure(map, NULL);
1278 void test_lex(struct isl_ctx *ctx)
1283 dim = isl_space_set_alloc(ctx, 0, 0);
1284 map = isl_map_lex_le(dim);
1285 assert(!isl_map_is_empty(map));
1289 void test_lexmin(struct isl_ctx *ctx)
1292 isl_basic_map *bmap;
1293 isl_map *map, *map2;
1296 isl_pw_multi_aff *pma;
1298 str = "[p0, p1] -> { [] -> [] : "
1299 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1300 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1301 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1302 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1303 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1304 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1305 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1306 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1307 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1308 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1309 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1310 map = isl_map_read_from_str(ctx, str);
1311 map = isl_map_lexmin(map);
1314 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1315 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1316 set = isl_set_read_from_str(ctx, str);
1317 set = isl_set_lexmax(set);
1318 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1319 set2 = isl_set_read_from_str(ctx, str);
1320 set = isl_set_intersect(set, set2);
1321 assert(!isl_set_is_empty(set));
1324 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1325 map = isl_map_read_from_str(ctx, str);
1326 map = isl_map_lexmin(map);
1327 str = "{ [x] -> [5] : 6 <= x <= 8; "
1328 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1329 map2 = isl_map_read_from_str(ctx, str);
1330 assert(isl_map_is_equal(map, map2));
1334 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1335 map = isl_map_read_from_str(ctx, str);
1336 map2 = isl_map_copy(map);
1337 map = isl_map_lexmin(map);
1338 assert(isl_map_is_equal(map, map2));
1342 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1343 map = isl_map_read_from_str(ctx, str);
1344 map = isl_map_lexmin(map);
1345 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1346 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1347 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1348 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1349 map2 = isl_map_read_from_str(ctx, str);
1350 assert(isl_map_is_equal(map, map2));
1354 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1355 " 8i' <= i and 8i' >= -7 + i }";
1356 bmap = isl_basic_map_read_from_str(ctx, str);
1357 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1358 map2 = isl_map_from_pw_multi_aff(pma);
1359 map = isl_map_from_basic_map(bmap);
1360 assert(isl_map_is_equal(map, map2));
1364 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1365 map = isl_map_read_from_str(ctx, str);
1366 map = isl_map_lexmin(map);
1367 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1368 map2 = isl_map_read_from_str(ctx, str);
1369 assert(isl_map_is_equal(map, map2));
1379 static int collect_must_may(__isl_take isl_map *dep, int must,
1380 void *dep_user, void *user)
1382 struct must_may *mm = (struct must_may *)user;
1385 mm->must = isl_map_union(mm->must, dep);
1387 mm->may = isl_map_union(mm->may, dep);
1392 static int common_space(void *first, void *second)
1394 int depth = *(int *)first;
1398 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1406 map2 = isl_map_read_from_str(map->ctx, str);
1407 equal = isl_map_is_equal(map, map2);
1413 void test_dep(struct isl_ctx *ctx)
1418 isl_access_info *ai;
1425 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1426 map = isl_map_read_from_str(ctx, str);
1427 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1429 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1430 map = isl_map_read_from_str(ctx, str);
1431 ai = isl_access_info_add_source(ai, map, 1, &depth);
1433 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1434 map = isl_map_read_from_str(ctx, str);
1435 ai = isl_access_info_add_source(ai, map, 1, &depth);
1437 flow = isl_access_info_compute_flow(ai);
1438 dim = isl_space_alloc(ctx, 0, 3, 3);
1439 mm.must = isl_map_empty(isl_space_copy(dim));
1440 mm.may = isl_map_empty(dim);
1442 isl_flow_foreach(flow, collect_must_may, &mm);
1444 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1445 " [1,10,0] -> [2,5,0] }";
1446 assert(map_is_equal(mm.must, str));
1447 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1448 assert(map_is_equal(mm.may, str));
1450 isl_map_free(mm.must);
1451 isl_map_free(mm.may);
1452 isl_flow_free(flow);
1455 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1456 map = isl_map_read_from_str(ctx, str);
1457 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1459 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1460 map = isl_map_read_from_str(ctx, str);
1461 ai = isl_access_info_add_source(ai, map, 1, &depth);
1463 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1464 map = isl_map_read_from_str(ctx, str);
1465 ai = isl_access_info_add_source(ai, map, 0, &depth);
1467 flow = isl_access_info_compute_flow(ai);
1468 dim = isl_space_alloc(ctx, 0, 3, 3);
1469 mm.must = isl_map_empty(isl_space_copy(dim));
1470 mm.may = isl_map_empty(dim);
1472 isl_flow_foreach(flow, collect_must_may, &mm);
1474 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1475 assert(map_is_equal(mm.must, str));
1476 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1477 assert(map_is_equal(mm.may, str));
1479 isl_map_free(mm.must);
1480 isl_map_free(mm.may);
1481 isl_flow_free(flow);
1484 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1485 map = isl_map_read_from_str(ctx, str);
1486 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1488 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1489 map = isl_map_read_from_str(ctx, str);
1490 ai = isl_access_info_add_source(ai, map, 0, &depth);
1492 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1493 map = isl_map_read_from_str(ctx, str);
1494 ai = isl_access_info_add_source(ai, map, 0, &depth);
1496 flow = isl_access_info_compute_flow(ai);
1497 dim = isl_space_alloc(ctx, 0, 3, 3);
1498 mm.must = isl_map_empty(isl_space_copy(dim));
1499 mm.may = isl_map_empty(dim);
1501 isl_flow_foreach(flow, collect_must_may, &mm);
1503 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1504 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1505 assert(map_is_equal(mm.may, str));
1506 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1507 assert(map_is_equal(mm.must, str));
1509 isl_map_free(mm.must);
1510 isl_map_free(mm.may);
1511 isl_flow_free(flow);
1514 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1515 map = isl_map_read_from_str(ctx, str);
1516 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1518 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1519 map = isl_map_read_from_str(ctx, str);
1520 ai = isl_access_info_add_source(ai, map, 0, &depth);
1522 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1523 map = isl_map_read_from_str(ctx, str);
1524 ai = isl_access_info_add_source(ai, map, 0, &depth);
1526 flow = isl_access_info_compute_flow(ai);
1527 dim = isl_space_alloc(ctx, 0, 3, 3);
1528 mm.must = isl_map_empty(isl_space_copy(dim));
1529 mm.may = isl_map_empty(dim);
1531 isl_flow_foreach(flow, collect_must_may, &mm);
1533 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1534 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1535 assert(map_is_equal(mm.may, str));
1536 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1537 assert(map_is_equal(mm.must, str));
1539 isl_map_free(mm.must);
1540 isl_map_free(mm.may);
1541 isl_flow_free(flow);
1544 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1545 map = isl_map_read_from_str(ctx, str);
1546 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1548 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1549 map = isl_map_read_from_str(ctx, str);
1550 ai = isl_access_info_add_source(ai, map, 0, &depth);
1552 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1553 map = isl_map_read_from_str(ctx, str);
1554 ai = isl_access_info_add_source(ai, map, 0, &depth);
1556 flow = isl_access_info_compute_flow(ai);
1557 dim = isl_space_alloc(ctx, 0, 3, 3);
1558 mm.must = isl_map_empty(isl_space_copy(dim));
1559 mm.may = isl_map_empty(dim);
1561 isl_flow_foreach(flow, collect_must_may, &mm);
1563 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1564 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1565 assert(map_is_equal(mm.may, str));
1566 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1567 assert(map_is_equal(mm.must, str));
1569 isl_map_free(mm.must);
1570 isl_map_free(mm.may);
1571 isl_flow_free(flow);
1576 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1577 map = isl_map_read_from_str(ctx, str);
1578 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1580 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1581 map = isl_map_read_from_str(ctx, str);
1582 ai = isl_access_info_add_source(ai, map, 1, &depth);
1584 flow = isl_access_info_compute_flow(ai);
1585 dim = isl_space_alloc(ctx, 0, 5, 5);
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,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1592 assert(map_is_equal(mm.must, str));
1593 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1594 assert(map_is_equal(mm.may, str));
1596 isl_map_free(mm.must);
1597 isl_map_free(mm.may);
1598 isl_flow_free(flow);
1601 int test_sv(isl_ctx *ctx)
1605 isl_union_map *umap;
1608 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1609 map = isl_map_read_from_str(ctx, str);
1610 sv = isl_map_is_single_valued(map);
1615 isl_die(ctx, isl_error_internal,
1616 "map not detected as single valued", return -1);
1618 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1619 map = isl_map_read_from_str(ctx, str);
1620 sv = isl_map_is_single_valued(map);
1625 isl_die(ctx, isl_error_internal,
1626 "map detected as single valued", return -1);
1628 str = "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
1629 umap = isl_union_map_read_from_str(ctx, str);
1630 sv = isl_union_map_is_single_valued(umap);
1631 isl_union_map_free(umap);
1635 isl_die(ctx, isl_error_internal,
1636 "map not detected as single valued", return -1);
1638 str = "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
1639 umap = isl_union_map_read_from_str(ctx, str);
1640 sv = isl_union_map_is_single_valued(umap);
1641 isl_union_map_free(umap);
1645 isl_die(ctx, isl_error_internal,
1646 "map detected as single valued", return -1);
1651 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1655 map = isl_map_read_from_str(ctx, str);
1657 assert(isl_map_is_bijective(map));
1659 assert(!isl_map_is_bijective(map));
1663 void test_bijective(struct isl_ctx *ctx)
1665 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1666 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1667 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1668 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1669 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1670 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1671 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1672 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1673 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1674 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1675 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1676 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1677 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1680 void test_pwqp(struct isl_ctx *ctx)
1684 isl_pw_qpolynomial *pwqp1, *pwqp2;
1686 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1687 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1689 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1692 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1693 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1695 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1697 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1699 isl_pw_qpolynomial_free(pwqp1);
1701 str = "{ [i] -> i }";
1702 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1703 str = "{ [k] : exists a : k = 2a }";
1704 set = isl_set_read_from_str(ctx, str);
1705 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1706 str = "{ [i] -> i }";
1707 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1709 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1711 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1713 isl_pw_qpolynomial_free(pwqp1);
1715 str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
1716 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1718 set = isl_set_read_from_str(ctx, str);
1719 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1720 str = "{ [i] -> 16 }";
1721 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1723 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1725 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1727 isl_pw_qpolynomial_free(pwqp1);
1729 str = "{ [i] -> ([(i)/2]) }";
1730 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1731 str = "{ [k] : exists a : k = 2a+1 }";
1732 set = isl_set_read_from_str(ctx, str);
1733 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1734 str = "{ [i] -> -1/2 + 1/2 * i }";
1735 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1737 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1739 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1741 isl_pw_qpolynomial_free(pwqp1);
1743 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1744 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1745 str = "{ [i] -> ([(2 * [i/2])/5]) }";
1746 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1748 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1750 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1752 isl_pw_qpolynomial_free(pwqp1);
1754 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1755 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1756 str = "{ [x] -> x }";
1757 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1759 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1761 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1763 isl_pw_qpolynomial_free(pwqp1);
1765 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
1766 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1767 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1768 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
1769 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1770 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1771 isl_pw_qpolynomial_free(pwqp1);
1774 void test_split_periods(isl_ctx *ctx)
1777 isl_pw_qpolynomial *pwqp;
1779 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1780 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
1781 "U >= 0; [U,V] -> U^2 : U >= 100 }";
1782 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1784 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1787 isl_pw_qpolynomial_free(pwqp);
1790 void test_union(isl_ctx *ctx)
1793 isl_union_set *uset1, *uset2;
1794 isl_union_map *umap1, *umap2;
1796 str = "{ [i] : 0 <= i <= 1 }";
1797 uset1 = isl_union_set_read_from_str(ctx, str);
1798 str = "{ [1] -> [0] }";
1799 umap1 = isl_union_map_read_from_str(ctx, str);
1801 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1802 assert(isl_union_map_is_equal(umap1, umap2));
1804 isl_union_map_free(umap1);
1805 isl_union_map_free(umap2);
1807 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1808 umap1 = isl_union_map_read_from_str(ctx, str);
1809 str = "{ A[i]; B[i] }";
1810 uset1 = isl_union_set_read_from_str(ctx, str);
1812 uset2 = isl_union_map_domain(umap1);
1814 assert(isl_union_set_is_equal(uset1, uset2));
1816 isl_union_set_free(uset1);
1817 isl_union_set_free(uset2);
1820 void test_bound(isl_ctx *ctx)
1823 isl_pw_qpolynomial *pwqp;
1824 isl_pw_qpolynomial_fold *pwf;
1826 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1827 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1828 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1829 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
1830 isl_pw_qpolynomial_fold_free(pwf);
1832 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
1833 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1834 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1835 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
1836 isl_pw_qpolynomial_fold_free(pwf);
1839 void test_lift(isl_ctx *ctx)
1842 isl_basic_map *bmap;
1843 isl_basic_set *bset;
1845 str = "{ [i0] : exists e0 : i0 = 4e0 }";
1846 bset = isl_basic_set_read_from_str(ctx, str);
1847 bset = isl_basic_set_lift(bset);
1848 bmap = isl_basic_map_from_range(bset);
1849 bset = isl_basic_map_domain(bmap);
1850 isl_basic_set_free(bset);
1853 void test_subset(isl_ctx *ctx)
1856 isl_set *set1, *set2;
1858 str = "{ [112, 0] }";
1859 set1 = isl_set_read_from_str(ctx, str);
1860 str = "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1861 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1862 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }";
1863 set2 = isl_set_read_from_str(ctx, str);
1864 assert(isl_set_is_subset(set1, set2));
1869 void test_factorize(isl_ctx *ctx)
1872 isl_basic_set *bset;
1875 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
1876 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
1877 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
1878 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
1879 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
1880 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
1881 "3i5 >= -2i0 - i2 + 3i4 }";
1882 bset = isl_basic_set_read_from_str(ctx, str);
1883 f = isl_basic_set_factorizer(bset);
1885 isl_basic_set_free(bset);
1886 isl_factorizer_free(f);
1888 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
1889 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
1890 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
1891 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
1892 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
1893 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
1894 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
1895 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
1896 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
1897 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
1898 bset = isl_basic_set_read_from_str(ctx, str);
1899 f = isl_basic_set_factorizer(bset);
1901 isl_basic_set_free(bset);
1902 isl_factorizer_free(f);
1905 static int check_injective(__isl_take isl_map *map, void *user)
1907 int *injective = user;
1909 *injective = isl_map_is_injective(map);
1912 if (*injective < 0 || !*injective)
1918 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
1919 const char *r, const char *s, int tilable, int parallel)
1923 isl_union_map *W, *R, *S;
1924 isl_union_map *empty;
1925 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
1926 isl_union_map *validity, *proximity;
1927 isl_union_map *schedule;
1928 isl_union_map *test;
1929 isl_union_set *delta;
1930 isl_union_set *domain;
1934 isl_schedule *sched;
1935 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
1937 D = isl_union_set_read_from_str(ctx, d);
1938 W = isl_union_map_read_from_str(ctx, w);
1939 R = isl_union_map_read_from_str(ctx, r);
1940 S = isl_union_map_read_from_str(ctx, s);
1942 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
1943 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
1945 empty = isl_union_map_empty(isl_union_map_get_space(S));
1946 isl_union_map_compute_flow(isl_union_map_copy(R),
1947 isl_union_map_copy(W), empty,
1948 isl_union_map_copy(S),
1949 &dep_raw, NULL, NULL, NULL);
1950 isl_union_map_compute_flow(isl_union_map_copy(W),
1951 isl_union_map_copy(W),
1952 isl_union_map_copy(R),
1953 isl_union_map_copy(S),
1954 &dep_waw, &dep_war, NULL, NULL);
1956 dep = isl_union_map_union(dep_waw, dep_war);
1957 dep = isl_union_map_union(dep, dep_raw);
1958 validity = isl_union_map_copy(dep);
1959 proximity = isl_union_map_copy(dep);
1961 sched = isl_union_set_compute_schedule(isl_union_set_copy(D),
1962 validity, proximity);
1963 schedule = isl_schedule_get_map(sched);
1964 isl_schedule_free(sched);
1965 isl_union_map_free(W);
1966 isl_union_map_free(R);
1967 isl_union_map_free(S);
1970 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
1972 domain = isl_union_map_domain(isl_union_map_copy(schedule));
1973 is_complete = isl_union_set_is_subset(D, domain);
1974 isl_union_set_free(D);
1975 isl_union_set_free(domain);
1977 test = isl_union_map_reverse(isl_union_map_copy(schedule));
1978 test = isl_union_map_apply_range(test, dep);
1979 test = isl_union_map_apply_range(test, schedule);
1981 delta = isl_union_map_deltas(test);
1982 if (isl_union_set_n_set(delta) == 0) {
1986 isl_union_set_free(delta);
1988 delta_set = isl_set_from_union_set(delta);
1990 slice = isl_set_universe(isl_set_get_space(delta_set));
1991 for (i = 0; i < tilable; ++i)
1992 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
1993 is_tilable = isl_set_is_subset(delta_set, slice);
1994 isl_set_free(slice);
1996 slice = isl_set_universe(isl_set_get_space(delta_set));
1997 for (i = 0; i < parallel; ++i)
1998 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
1999 is_parallel = isl_set_is_subset(delta_set, slice);
2000 isl_set_free(slice);
2002 origin = isl_set_universe(isl_set_get_space(delta_set));
2003 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2004 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2006 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2007 delta_set = isl_set_lexmin(delta_set);
2009 is_nonneg = isl_set_is_equal(delta_set, origin);
2011 isl_set_free(origin);
2012 isl_set_free(delta_set);
2015 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2016 is_injection < 0 || is_complete < 0)
2019 isl_die(ctx, isl_error_unknown,
2020 "generated schedule incomplete", return -1);
2022 isl_die(ctx, isl_error_unknown,
2023 "generated schedule not injective on each statement",
2026 isl_die(ctx, isl_error_unknown,
2027 "negative dependences in generated schedule",
2030 isl_die(ctx, isl_error_unknown,
2031 "generated schedule not as tilable as expected",
2034 isl_die(ctx, isl_error_unknown,
2035 "generated schedule not as parallel as expected",
2041 int test_special_schedule(isl_ctx *ctx, const char *domain,
2042 const char *validity, const char *proximity, const char *expected_sched)
2046 isl_union_map *prox;
2047 isl_union_map *sched1, *sched2;
2048 isl_schedule *schedule;
2051 dom = isl_union_set_read_from_str(ctx, domain);
2052 dep = isl_union_map_read_from_str(ctx, validity);
2053 prox = isl_union_map_read_from_str(ctx, proximity);
2054 schedule = isl_union_set_compute_schedule(dom, dep, prox);
2055 sched1 = isl_schedule_get_map(schedule);
2056 isl_schedule_free(schedule);
2058 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2060 equal = isl_union_map_is_equal(sched1, sched2);
2061 isl_union_map_free(sched1);
2062 isl_union_map_free(sched2);
2067 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2073 int test_schedule(isl_ctx *ctx)
2075 const char *D, *W, *R, *V, *P, *S;
2078 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2079 W = "{ S1[t,i] -> a[t,i] }";
2080 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2081 "S1[t,i] -> a[t-1,i+1] }";
2082 S = "{ S1[t,i] -> [t,i] }";
2083 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2086 /* Fig. 5 of CC2008 */
2087 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2089 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2090 "j >= 2 and j <= -1 + N }";
2091 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2092 "j >= 2 and j <= -1 + N; "
2093 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2094 "j >= 2 and j <= -1 + N }";
2095 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2096 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2099 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2100 W = "{ S1[i] -> a[i] }";
2101 R = "{ S2[i] -> a[i+1] }";
2102 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2103 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2106 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2107 W = "{ S1[i] -> a[i] }";
2108 R = "{ S2[i] -> a[9-i] }";
2109 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2110 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2113 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2114 W = "{ S1[i] -> a[i] }";
2115 R = "[N] -> { S2[i] -> a[N-1-i] }";
2116 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2117 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2120 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2121 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2122 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2123 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2124 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2127 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2128 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2129 R = "{ S2[i,j] -> a[i-1,j] }";
2130 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2131 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2134 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2135 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2136 R = "{ S2[i,j] -> a[i,j-1] }";
2137 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2138 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2141 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2142 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2143 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2144 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2145 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2146 "S_0[] -> [0, 0, 0] }";
2147 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2149 ctx->opt->schedule_parametric = 0;
2150 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2152 ctx->opt->schedule_parametric = 1;
2154 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2155 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2156 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2157 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2158 "S4[i] -> a[i,N] }";
2159 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2160 "S4[i] -> [4,i,0] }";
2161 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2164 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2165 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2167 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2169 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2171 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2172 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2175 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2176 " S_2[t] : t >= 0 and t <= -1 + N; "
2177 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2179 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2180 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2181 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2182 "i >= 0 and i <= -1 + N }";
2183 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2184 "i >= 0 and i <= -1 + N; "
2185 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2186 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2187 " S_0[t] -> [0, t, 0] }";
2189 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2191 ctx->opt->schedule_parametric = 0;
2192 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2194 ctx->opt->schedule_parametric = 1;
2196 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2197 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2198 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
2201 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2202 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2203 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2204 "j >= 0 and j <= -1 + N; "
2205 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2206 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2207 "j >= 0 and j <= -1 + N; "
2208 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2209 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2210 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2213 D = "{ S_0[i] : i >= 0 }";
2214 W = "{ S_0[i] -> a[i] : i >= 0 }";
2215 R = "{ S_0[i] -> a[0] : i >= 0 }";
2216 S = "{ S_0[i] -> [0, i, 0] }";
2217 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2220 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2221 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2222 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2223 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2224 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2227 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2228 "k <= -1 + n and k >= 0 }";
2229 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
2230 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2231 "k <= -1 + n and k >= 0; "
2232 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2233 "k <= -1 + n and k >= 0; "
2234 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2235 "k <= -1 + n and k >= 0 }";
2236 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
2237 ctx->opt->schedule_outer_zero_distance = 1;
2238 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2240 ctx->opt->schedule_outer_zero_distance = 0;
2242 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
2243 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
2244 "j >= 1 and j <= 7;"
2245 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
2246 "j >= 1 and j <= 8 }";
2248 S = "{ S_0[i, j] -> [i + j, j] }";
2249 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2250 if (test_special_schedule(ctx, D, V, P, S) < 0)
2252 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2254 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
2255 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
2256 "j >= 0 and j <= -1 + i }";
2257 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
2258 "i <= -1 + N and j >= 0;"
2259 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
2262 S = "{ S_0[i, j] -> [i, j] }";
2263 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2264 if (test_special_schedule(ctx, D, V, P, S) < 0)
2266 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2268 /* Test both algorithms on a case with only proximity dependences. */
2269 D = "{ S[i,j] : 0 <= i <= 10 }";
2271 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
2272 S = "{ S[i, j] -> [j, i] }";
2273 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
2274 if (test_special_schedule(ctx, D, V, P, S) < 0)
2276 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
2277 return test_special_schedule(ctx, D, V, P, S);
2280 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
2282 isl_union_map *umap;
2285 umap = isl_union_map_read_from_str(ctx, str);
2286 test = isl_union_map_plain_is_injective(umap);
2287 isl_union_map_free(umap);
2290 if (test == injective)
2293 isl_die(ctx, isl_error_unknown,
2294 "map not detected as injective", return -1);
2296 isl_die(ctx, isl_error_unknown,
2297 "map detected as injective", return -1);
2300 int test_injective(isl_ctx *ctx)
2304 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2306 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
2308 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
2310 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
2312 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2314 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2316 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2318 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2321 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2322 if (test_plain_injective(ctx, str, 1))
2324 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2325 if (test_plain_injective(ctx, str, 0))
2331 int test_aff(isl_ctx *ctx)
2336 isl_local_space *ls;
2340 dim = isl_space_set_alloc(ctx, 0, 1);
2341 ls = isl_local_space_from_space(dim);
2342 aff = isl_aff_zero_on_domain(ls);
2344 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2345 aff = isl_aff_scale_down_ui(aff, 3);
2346 aff = isl_aff_floor(aff);
2347 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2348 aff = isl_aff_scale_down_ui(aff, 2);
2349 aff = isl_aff_floor(aff);
2350 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2353 set = isl_set_read_from_str(ctx, str);
2354 aff = isl_aff_gist(aff, set);
2356 aff = isl_aff_add_constant_si(aff, -16);
2357 zero = isl_aff_plain_is_zero(aff);
2363 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2368 int test_dim_max(isl_ctx *ctx)
2372 isl_set *set1, *set2;
2377 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
2378 set = isl_set_read_from_str(ctx, str);
2379 pwaff = isl_set_dim_max(set, 0);
2380 set1 = isl_set_from_pw_aff(pwaff);
2381 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
2382 set2 = isl_set_read_from_str(ctx, str);
2383 equal = isl_set_is_equal(set1, set2);
2389 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2391 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
2392 set = isl_set_read_from_str(ctx, str);
2393 pwaff = isl_set_dim_max(set, 0);
2394 set1 = isl_set_from_pw_aff(pwaff);
2395 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2396 set2 = isl_set_read_from_str(ctx, str);
2397 equal = isl_set_is_equal(set1, set2);
2403 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2405 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
2406 set = isl_set_read_from_str(ctx, str);
2407 pwaff = isl_set_dim_max(set, 0);
2408 set1 = isl_set_from_pw_aff(pwaff);
2409 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2410 set2 = isl_set_read_from_str(ctx, str);
2411 equal = isl_set_is_equal(set1, set2);
2417 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2419 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
2420 "0 <= i < N and 0 <= j < M }";
2421 map = isl_map_read_from_str(ctx, str);
2422 set = isl_map_range(map);
2424 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
2425 set1 = isl_set_from_pw_aff(pwaff);
2426 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
2427 set2 = isl_set_read_from_str(ctx, str);
2428 equal = isl_set_is_equal(set1, set2);
2432 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
2433 set1 = isl_set_from_pw_aff(pwaff);
2434 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
2435 set2 = isl_set_read_from_str(ctx, str);
2436 if (equal >= 0 && equal)
2437 equal = isl_set_is_equal(set1, set2);
2446 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2451 int test_product(isl_ctx *ctx)
2455 isl_union_set *uset1, *uset2;
2459 set = isl_set_read_from_str(ctx, str);
2460 set = isl_set_product(set, isl_set_copy(set));
2461 ok = isl_set_is_wrapping(set);
2466 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2469 uset1 = isl_union_set_read_from_str(ctx, str);
2470 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
2471 str = "{ [[] -> []] }";
2472 uset2 = isl_union_set_read_from_str(ctx, str);
2473 ok = isl_union_set_is_equal(uset1, uset2);
2474 isl_union_set_free(uset1);
2475 isl_union_set_free(uset2);
2479 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2484 int test_equal(isl_ctx *ctx)
2487 isl_set *set, *set2;
2491 set = isl_set_read_from_str(ctx, str);
2493 set2 = isl_set_read_from_str(ctx, str);
2494 equal = isl_set_is_equal(set, set2);
2500 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2505 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
2506 enum isl_dim_type type, unsigned pos, int fixed)
2510 test = isl_map_plain_is_fixed(map, type, pos, NULL);
2517 isl_die(ctx, isl_error_unknown,
2518 "map not detected as fixed", return -1);
2520 isl_die(ctx, isl_error_unknown,
2521 "map detected as fixed", return -1);
2524 int test_fixed(isl_ctx *ctx)
2529 str = "{ [i] -> [i] }";
2530 map = isl_map_read_from_str(ctx, str);
2531 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
2533 str = "{ [i] -> [1] }";
2534 map = isl_map_read_from_str(ctx, str);
2535 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2537 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
2538 map = isl_map_read_from_str(ctx, str);
2539 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2541 map = isl_map_read_from_str(ctx, str);
2542 map = isl_map_neg(map);
2543 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
2549 int test_vertices(isl_ctx *ctx)
2552 isl_basic_set *bset;
2553 isl_vertices *vertices;
2555 str = "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
2556 bset = isl_basic_set_read_from_str(ctx, str);
2557 vertices = isl_basic_set_compute_vertices(bset);
2558 isl_basic_set_free(bset);
2559 isl_vertices_free(vertices);
2563 str = "{ A[t, i] : t = 14 and i = 1 }";
2564 bset = isl_basic_set_read_from_str(ctx, str);
2565 vertices = isl_basic_set_compute_vertices(bset);
2566 isl_basic_set_free(bset);
2567 isl_vertices_free(vertices);
2574 int test_union_pw(isl_ctx *ctx)
2578 isl_union_set *uset;
2579 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
2581 str = "{ [x] -> x^2 }";
2582 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
2583 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
2584 uset = isl_union_pw_qpolynomial_domain(upwqp1);
2585 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
2586 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
2587 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
2588 isl_union_pw_qpolynomial_free(upwqp1);
2589 isl_union_pw_qpolynomial_free(upwqp2);
2593 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2600 struct isl_ctx *ctx;
2602 srcdir = getenv("srcdir");
2605 ctx = isl_ctx_alloc();
2606 if (test_vertices(ctx) < 0)
2608 if (test_fixed(ctx) < 0)
2610 if (test_equal(ctx) < 0)
2612 if (test_product(ctx) < 0)
2614 if (test_dim_max(ctx) < 0)
2616 if (test_aff(ctx) < 0)
2618 if (test_injective(ctx) < 0)
2620 if (test_schedule(ctx) < 0)
2622 if (test_union_pw(ctx) < 0)
2624 test_factorize(ctx);
2629 test_split_periods(ctx);
2633 if (test_sv(ctx) < 0)
2635 test_bijective(ctx);
2639 test_construction(ctx);
2642 test_application(ctx);
2643 test_affine_hull(ctx);
2644 test_convex_hull(ctx);