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)
211 void test_read(struct isl_ctx
*ctx
)
215 struct isl_basic_set
*bset1
, *bset2
;
216 const char *str
= "{[y]: Exists ( alpha : 2alpha = y)}";
218 filename
= get_filename(ctx
, "set", "omega");
220 input
= fopen(filename
, "r");
223 bset1
= isl_basic_set_read_from_file(ctx
, input
);
224 bset2
= isl_basic_set_read_from_str(ctx
, str
);
226 assert(isl_basic_set_is_equal(bset1
, bset2
) == 1);
228 isl_basic_set_free(bset1
);
229 isl_basic_set_free(bset2
);
235 void test_bounded(struct isl_ctx
*ctx
)
240 set
= isl_set_read_from_str(ctx
, "[n] -> {[i] : 0 <= i <= n }");
241 bounded
= isl_set_is_bounded(set
);
245 set
= isl_set_read_from_str(ctx
, "{[n, i] : 0 <= i <= n }");
246 bounded
= isl_set_is_bounded(set
);
250 set
= isl_set_read_from_str(ctx
, "[n] -> {[i] : i <= n }");
251 bounded
= isl_set_is_bounded(set
);
256 /* Construct the basic set { [i] : 5 <= i <= N } */
257 void test_construction(struct isl_ctx
*ctx
)
262 struct isl_basic_set
*bset
;
263 struct isl_constraint
*c
;
267 dim
= isl_space_set_alloc(ctx
, 1, 1);
268 bset
= isl_basic_set_universe(isl_space_copy(dim
));
269 ls
= isl_local_space_from_space(dim
);
271 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
272 isl_int_set_si(v
, -1);
273 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
274 isl_int_set_si(v
, 1);
275 isl_constraint_set_coefficient(c
, isl_dim_param
, 0, v
);
276 bset
= isl_basic_set_add_constraint(bset
, c
);
278 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
279 isl_int_set_si(v
, 1);
280 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
281 isl_int_set_si(v
, -5);
282 isl_constraint_set_constant(c
, v
);
283 bset
= isl_basic_set_add_constraint(bset
, c
);
285 isl_local_space_free(ls
);
286 isl_basic_set_free(bset
);
291 void test_dim(struct isl_ctx
*ctx
)
294 isl_map
*map1
, *map2
;
296 map1
= isl_map_read_from_str(ctx
,
297 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
298 map1
= isl_map_add_dims(map1
, isl_dim_in
, 1);
299 map2
= isl_map_read_from_str(ctx
,
300 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
301 assert(isl_map_is_equal(map1
, map2
));
304 map1
= isl_map_project_out(map1
, isl_dim_in
, 0, 1);
305 map2
= isl_map_read_from_str(ctx
, "[n] -> { [i] -> [j] : n >= 0 }");
306 assert(isl_map_is_equal(map1
, map2
));
311 str
= "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
312 map1
= isl_map_read_from_str(ctx
, str
);
313 str
= "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
314 map2
= isl_map_read_from_str(ctx
, str
);
315 map1
= isl_map_move_dims(map1
, isl_dim_out
, 0, isl_dim_param
, 0, 1);
316 assert(isl_map_is_equal(map1
, map2
));
322 static int test_div(isl_ctx
*ctx
)
331 struct isl_basic_set
*bset
;
332 struct isl_constraint
*c
;
337 dim
= isl_space_set_alloc(ctx
, 0, 3);
338 bset
= isl_basic_set_universe(isl_space_copy(dim
));
339 ls
= isl_local_space_from_space(dim
);
341 c
= isl_equality_alloc(isl_local_space_copy(ls
));
342 isl_int_set_si(v
, -1);
343 isl_constraint_set_constant(c
, v
);
344 isl_int_set_si(v
, 1);
345 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
346 isl_int_set_si(v
, 3);
347 isl_constraint_set_coefficient(c
, isl_dim_set
, 1, v
);
348 bset
= isl_basic_set_add_constraint(bset
, c
);
350 c
= isl_equality_alloc(isl_local_space_copy(ls
));
351 isl_int_set_si(v
, 1);
352 isl_constraint_set_constant(c
, v
);
353 isl_int_set_si(v
, -1);
354 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
355 isl_int_set_si(v
, 3);
356 isl_constraint_set_coefficient(c
, isl_dim_set
, 2, v
);
357 bset
= isl_basic_set_add_constraint(bset
, c
);
359 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 1, 2);
361 assert(bset
&& bset
->n_div
== 1);
362 isl_local_space_free(ls
);
363 isl_basic_set_free(bset
);
366 dim
= isl_space_set_alloc(ctx
, 0, 3);
367 bset
= isl_basic_set_universe(isl_space_copy(dim
));
368 ls
= isl_local_space_from_space(dim
);
370 c
= isl_equality_alloc(isl_local_space_copy(ls
));
371 isl_int_set_si(v
, 1);
372 isl_constraint_set_constant(c
, v
);
373 isl_int_set_si(v
, -1);
374 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
375 isl_int_set_si(v
, 3);
376 isl_constraint_set_coefficient(c
, isl_dim_set
, 1, v
);
377 bset
= isl_basic_set_add_constraint(bset
, c
);
379 c
= isl_equality_alloc(isl_local_space_copy(ls
));
380 isl_int_set_si(v
, -1);
381 isl_constraint_set_constant(c
, v
);
382 isl_int_set_si(v
, 1);
383 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
384 isl_int_set_si(v
, 3);
385 isl_constraint_set_coefficient(c
, isl_dim_set
, 2, v
);
386 bset
= isl_basic_set_add_constraint(bset
, c
);
388 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 1, 2);
390 assert(bset
&& bset
->n_div
== 1);
391 isl_local_space_free(ls
);
392 isl_basic_set_free(bset
);
395 dim
= isl_space_set_alloc(ctx
, 0, 3);
396 bset
= isl_basic_set_universe(isl_space_copy(dim
));
397 ls
= isl_local_space_from_space(dim
);
399 c
= isl_equality_alloc(isl_local_space_copy(ls
));
400 isl_int_set_si(v
, 1);
401 isl_constraint_set_constant(c
, v
);
402 isl_int_set_si(v
, -1);
403 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
404 isl_int_set_si(v
, 3);
405 isl_constraint_set_coefficient(c
, isl_dim_set
, 1, v
);
406 bset
= isl_basic_set_add_constraint(bset
, c
);
408 c
= isl_equality_alloc(isl_local_space_copy(ls
));
409 isl_int_set_si(v
, -3);
410 isl_constraint_set_constant(c
, v
);
411 isl_int_set_si(v
, 1);
412 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
413 isl_int_set_si(v
, 4);
414 isl_constraint_set_coefficient(c
, isl_dim_set
, 2, v
);
415 bset
= isl_basic_set_add_constraint(bset
, c
);
417 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 1, 2);
419 assert(bset
&& bset
->n_div
== 1);
420 isl_local_space_free(ls
);
421 isl_basic_set_free(bset
);
424 dim
= isl_space_set_alloc(ctx
, 0, 3);
425 bset
= isl_basic_set_universe(isl_space_copy(dim
));
426 ls
= isl_local_space_from_space(dim
);
428 c
= isl_equality_alloc(isl_local_space_copy(ls
));
429 isl_int_set_si(v
, 2);
430 isl_constraint_set_constant(c
, v
);
431 isl_int_set_si(v
, -1);
432 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
433 isl_int_set_si(v
, 3);
434 isl_constraint_set_coefficient(c
, isl_dim_set
, 1, v
);
435 bset
= isl_basic_set_add_constraint(bset
, c
);
437 c
= isl_equality_alloc(isl_local_space_copy(ls
));
438 isl_int_set_si(v
, -1);
439 isl_constraint_set_constant(c
, v
);
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 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 1, 2);
448 assert(isl_basic_set_is_empty(bset
));
449 isl_local_space_free(ls
);
450 isl_basic_set_free(bset
);
453 dim
= isl_space_set_alloc(ctx
, 0, 3);
454 bset
= isl_basic_set_universe(isl_space_copy(dim
));
455 ls
= isl_local_space_from_space(dim
);
457 c
= isl_equality_alloc(isl_local_space_copy(ls
));
458 isl_int_set_si(v
, -1);
459 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
460 isl_int_set_si(v
, 3);
461 isl_constraint_set_coefficient(c
, isl_dim_set
, 2, v
);
462 bset
= isl_basic_set_add_constraint(bset
, c
);
464 c
= isl_equality_alloc(isl_local_space_copy(ls
));
465 isl_int_set_si(v
, 1);
466 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
467 isl_int_set_si(v
, -3);
468 isl_constraint_set_coefficient(c
, isl_dim_set
, 1, v
);
469 bset
= isl_basic_set_add_constraint(bset
, c
);
471 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 2, 1);
473 assert(bset
&& bset
->n_div
== 0);
474 isl_basic_set_free(bset
);
475 isl_local_space_free(ls
);
478 dim
= isl_space_set_alloc(ctx
, 0, 3);
479 bset
= isl_basic_set_universe(isl_space_copy(dim
));
480 ls
= isl_local_space_from_space(dim
);
482 c
= isl_equality_alloc(isl_local_space_copy(ls
));
483 isl_int_set_si(v
, -1);
484 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
485 isl_int_set_si(v
, 6);
486 isl_constraint_set_coefficient(c
, isl_dim_set
, 2, v
);
487 bset
= isl_basic_set_add_constraint(bset
, c
);
489 c
= isl_equality_alloc(isl_local_space_copy(ls
));
490 isl_int_set_si(v
, 1);
491 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
492 isl_int_set_si(v
, -3);
493 isl_constraint_set_coefficient(c
, isl_dim_set
, 1, v
);
494 bset
= isl_basic_set_add_constraint(bset
, c
);
496 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 2, 1);
498 assert(bset
&& bset
->n_div
== 1);
499 isl_basic_set_free(bset
);
500 isl_local_space_free(ls
);
503 /* This test is a bit tricky. We set up an equality
505 * Normalization of divs creates _two_ divs
508 * Afterwards e0 is removed again because it has coefficient -1
509 * and we end up with the original equality and div again.
510 * Perhaps we can avoid the introduction of this temporary div.
512 dim
= isl_space_set_alloc(ctx
, 0, 4);
513 bset
= isl_basic_set_universe(isl_space_copy(dim
));
514 ls
= isl_local_space_from_space(dim
);
516 c
= isl_equality_alloc(isl_local_space_copy(ls
));
517 isl_int_set_si(v
, -1);
518 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
519 isl_int_set_si(v
, -3);
520 isl_constraint_set_coefficient(c
, isl_dim_set
, 1, v
);
521 isl_int_set_si(v
, -3);
522 isl_constraint_set_coefficient(c
, isl_dim_set
, 2, v
);
523 isl_int_set_si(v
, 6);
524 isl_constraint_set_coefficient(c
, isl_dim_set
, 3, v
);
525 bset
= isl_basic_set_add_constraint(bset
, c
);
527 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 3, 1);
529 /* Test disabled for now */
531 assert(bset && bset->n_div == 1);
533 isl_local_space_free(ls
);
534 isl_basic_set_free(bset
);
537 dim
= isl_space_set_alloc(ctx
, 0, 5);
538 bset
= isl_basic_set_universe(isl_space_copy(dim
));
539 ls
= isl_local_space_from_space(dim
);
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
, 1, v
);
546 isl_int_set_si(v
, -3);
547 isl_constraint_set_coefficient(c
, isl_dim_set
, 3, v
);
548 isl_int_set_si(v
, 6);
549 isl_constraint_set_coefficient(c
, isl_dim_set
, 4, v
);
550 bset
= isl_basic_set_add_constraint(bset
, c
);
552 c
= isl_equality_alloc(isl_local_space_copy(ls
));
553 isl_int_set_si(v
, -1);
554 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
555 isl_int_set_si(v
, 1);
556 isl_constraint_set_coefficient(c
, isl_dim_set
, 2, v
);
557 isl_int_set_si(v
, 1);
558 isl_constraint_set_constant(c
, v
);
559 bset
= isl_basic_set_add_constraint(bset
, c
);
561 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 4, 1);
563 /* Test disabled for now */
565 assert(bset && bset->n_div == 1);
567 isl_local_space_free(ls
);
568 isl_basic_set_free(bset
);
571 dim
= isl_space_set_alloc(ctx
, 0, 4);
572 bset
= isl_basic_set_universe(isl_space_copy(dim
));
573 ls
= isl_local_space_from_space(dim
);
575 c
= isl_equality_alloc(isl_local_space_copy(ls
));
576 isl_int_set_si(v
, 1);
577 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
578 isl_int_set_si(v
, -1);
579 isl_constraint_set_coefficient(c
, isl_dim_set
, 1, v
);
580 isl_int_set_si(v
, -2);
581 isl_constraint_set_coefficient(c
, isl_dim_set
, 2, v
);
582 bset
= isl_basic_set_add_constraint(bset
, c
);
584 c
= isl_equality_alloc(isl_local_space_copy(ls
));
585 isl_int_set_si(v
, -1);
586 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
587 isl_int_set_si(v
, 3);
588 isl_constraint_set_coefficient(c
, isl_dim_set
, 3, v
);
589 isl_int_set_si(v
, 2);
590 isl_constraint_set_constant(c
, v
);
591 bset
= isl_basic_set_add_constraint(bset
, c
);
593 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 2, 2);
595 bset
= isl_basic_set_fix_si(bset
, isl_dim_set
, 0, 2);
597 assert(!isl_basic_set_is_empty(bset
));
599 isl_local_space_free(ls
);
600 isl_basic_set_free(bset
);
603 dim
= isl_space_set_alloc(ctx
, 0, 3);
604 bset
= isl_basic_set_universe(isl_space_copy(dim
));
605 ls
= isl_local_space_from_space(dim
);
607 c
= isl_equality_alloc(isl_local_space_copy(ls
));
608 isl_int_set_si(v
, 1);
609 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
610 isl_int_set_si(v
, -2);
611 isl_constraint_set_coefficient(c
, isl_dim_set
, 2, v
);
612 bset
= isl_basic_set_add_constraint(bset
, c
);
614 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 2, 1);
616 bset
= isl_basic_set_fix_si(bset
, isl_dim_set
, 0, 2);
618 isl_local_space_free(ls
);
619 isl_basic_set_free(bset
);
623 str
= "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
624 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
625 set
= isl_set_read_from_str(ctx
, str
);
626 set
= isl_set_compute_divs(set
);
631 str
= "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
632 bset
= isl_basic_set_read_from_str(ctx
, str
);
633 n
= isl_basic_set_dim(bset
, isl_dim_div
);
634 isl_basic_set_free(bset
);
638 isl_die(ctx
, isl_error_unknown
,
639 "expecting no existentials", return -1);
641 str
= "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
642 set
= isl_set_read_from_str(ctx
, str
);
643 set
= isl_set_remove_divs_involving_dims(set
, isl_dim_set
, 0, 2);
644 set
= isl_set_fix_si(set
, isl_dim_set
, 2, -3);
645 empty
= isl_set_is_empty(set
);
650 isl_die(ctx
, isl_error_unknown
,
651 "result not as accurate as expected", return -1);
656 void test_application_case(struct isl_ctx
*ctx
, const char *name
)
660 struct isl_basic_set
*bset1
, *bset2
;
661 struct isl_basic_map
*bmap
;
663 filename
= get_filename(ctx
, name
, "omega");
665 input
= fopen(filename
, "r");
668 bset1
= isl_basic_set_read_from_file(ctx
, input
);
669 bmap
= isl_basic_map_read_from_file(ctx
, input
);
671 bset1
= isl_basic_set_apply(bset1
, bmap
);
673 bset2
= isl_basic_set_read_from_file(ctx
, input
);
675 assert(isl_basic_set_is_equal(bset1
, bset2
) == 1);
677 isl_basic_set_free(bset1
);
678 isl_basic_set_free(bset2
);
684 void test_application(struct isl_ctx
*ctx
)
686 test_application_case(ctx
, "application");
687 test_application_case(ctx
, "application2");
690 void test_affine_hull_case(struct isl_ctx
*ctx
, const char *name
)
694 struct isl_basic_set
*bset1
, *bset2
;
696 filename
= get_filename(ctx
, name
, "polylib");
698 input
= fopen(filename
, "r");
701 bset1
= isl_basic_set_read_from_file(ctx
, input
);
702 bset2
= isl_basic_set_read_from_file(ctx
, input
);
704 bset1
= isl_basic_set_affine_hull(bset1
);
706 assert(isl_basic_set_is_equal(bset1
, bset2
) == 1);
708 isl_basic_set_free(bset1
);
709 isl_basic_set_free(bset2
);
715 int test_affine_hull(struct isl_ctx
*ctx
)
722 test_affine_hull_case(ctx
, "affine2");
723 test_affine_hull_case(ctx
, "affine");
724 test_affine_hull_case(ctx
, "affine3");
726 str
= "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
727 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
728 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
729 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
730 set
= isl_set_read_from_str(ctx
, str
);
731 bset
= isl_set_affine_hull(set
);
732 n
= isl_basic_set_dim(bset
, isl_dim_div
);
733 isl_basic_set_free(bset
);
735 isl_die(ctx
, isl_error_unknown
, "not expecting any divs",
741 void test_convex_hull_case(struct isl_ctx
*ctx
, const char *name
)
745 struct isl_basic_set
*bset1
, *bset2
;
748 filename
= get_filename(ctx
, name
, "polylib");
750 input
= fopen(filename
, "r");
753 bset1
= isl_basic_set_read_from_file(ctx
, input
);
754 bset2
= isl_basic_set_read_from_file(ctx
, input
);
756 set
= isl_basic_set_union(bset1
, bset2
);
757 bset1
= isl_set_convex_hull(set
);
759 bset2
= isl_basic_set_read_from_file(ctx
, input
);
761 assert(isl_basic_set_is_equal(bset1
, bset2
) == 1);
763 isl_basic_set_free(bset1
);
764 isl_basic_set_free(bset2
);
770 void test_convex_hull_algo(struct isl_ctx
*ctx
, int convex
)
772 const char *str1
, *str2
;
773 isl_set
*set1
, *set2
;
774 int orig_convex
= ctx
->opt
->convex
;
775 ctx
->opt
->convex
= convex
;
777 test_convex_hull_case(ctx
, "convex0");
778 test_convex_hull_case(ctx
, "convex1");
779 test_convex_hull_case(ctx
, "convex2");
780 test_convex_hull_case(ctx
, "convex3");
781 test_convex_hull_case(ctx
, "convex4");
782 test_convex_hull_case(ctx
, "convex5");
783 test_convex_hull_case(ctx
, "convex6");
784 test_convex_hull_case(ctx
, "convex7");
785 test_convex_hull_case(ctx
, "convex8");
786 test_convex_hull_case(ctx
, "convex9");
787 test_convex_hull_case(ctx
, "convex10");
788 test_convex_hull_case(ctx
, "convex11");
789 test_convex_hull_case(ctx
, "convex12");
790 test_convex_hull_case(ctx
, "convex13");
791 test_convex_hull_case(ctx
, "convex14");
792 test_convex_hull_case(ctx
, "convex15");
794 str1
= "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
795 "(i0 = 1 and i1 = 0 and i2 = 1) or "
796 "(i0 = 0 and i1 = 0 and i2 = 0) }";
797 str2
= "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
798 set1
= isl_set_read_from_str(ctx
, str1
);
799 set2
= isl_set_read_from_str(ctx
, str2
);
800 set1
= isl_set_from_basic_set(isl_set_convex_hull(set1
));
801 assert(isl_set_is_equal(set1
, set2
));
805 ctx
->opt
->convex
= orig_convex
;
808 void test_convex_hull(struct isl_ctx
*ctx
)
810 test_convex_hull_algo(ctx
, ISL_CONVEX_HULL_FM
);
811 test_convex_hull_algo(ctx
, ISL_CONVEX_HULL_WRAP
);
814 void test_gist_case(struct isl_ctx
*ctx
, const char *name
)
818 struct isl_basic_set
*bset1
, *bset2
;
820 filename
= get_filename(ctx
, name
, "polylib");
822 input
= fopen(filename
, "r");
825 bset1
= isl_basic_set_read_from_file(ctx
, input
);
826 bset2
= isl_basic_set_read_from_file(ctx
, input
);
828 bset1
= isl_basic_set_gist(bset1
, bset2
);
830 bset2
= isl_basic_set_read_from_file(ctx
, input
);
832 assert(isl_basic_set_is_equal(bset1
, bset2
) == 1);
834 isl_basic_set_free(bset1
);
835 isl_basic_set_free(bset2
);
841 void test_gist(struct isl_ctx
*ctx
)
844 isl_basic_set
*bset1
, *bset2
;
846 test_gist_case(ctx
, "gist1");
848 str
= "[p0, p2, p3, p5, p6, p10] -> { [] : "
849 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
850 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
851 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
852 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
853 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
854 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
855 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
856 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
857 bset1
= isl_basic_set_read_from_str(ctx
, str
);
858 str
= "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
859 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
860 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
861 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
862 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
863 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
864 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
865 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
866 bset2
= isl_basic_set_read_from_str(ctx
, str
);
867 bset1
= isl_basic_set_gist(bset1
, bset2
);
868 assert(bset1
&& bset1
->n_div
== 0);
869 isl_basic_set_free(bset1
);
872 int test_coalesce_set(isl_ctx
*ctx
, const char *str
, int check_one
)
878 set
= isl_set_read_from_str(ctx
, str
);
879 set
= isl_set_coalesce(set
);
880 set2
= isl_set_read_from_str(ctx
, str
);
881 equal
= isl_set_is_equal(set
, set2
);
882 one
= set
&& set
->n
== 1;
889 isl_die(ctx
, isl_error_unknown
,
890 "coalesced set not equal to input", return -1);
891 if (check_one
&& !one
)
892 isl_die(ctx
, isl_error_unknown
,
893 "coalesced set should not be a union", return -1);
898 int test_coalesce_unbounded_wrapping(isl_ctx
*ctx
)
903 bounded
= isl_options_get_coalesce_bounded_wrapping(ctx
);
904 isl_options_set_coalesce_bounded_wrapping(ctx
, 0);
906 if (test_coalesce_set(ctx
,
907 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
908 "-x - y + 1 >= 0 and -3 <= z <= 3;"
909 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
910 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
911 "-10 <= y <= 0}", 1) < 0)
913 if (test_coalesce_set(ctx
,
914 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
916 if (test_coalesce_set(ctx
,
917 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
925 isl_options_set_coalesce_bounded_wrapping(ctx
, bounded
);
930 int test_coalesce(struct isl_ctx
*ctx
)
933 struct isl_set
*set
, *set2
;
934 struct isl_map
*map
, *map2
;
936 set
= isl_set_read_from_str(ctx
,
937 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
938 "y >= x & x >= 2 & 5 >= y }");
939 set
= isl_set_coalesce(set
);
940 assert(set
&& set
->n
== 1);
943 set
= isl_set_read_from_str(ctx
,
944 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
945 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}");
946 set
= isl_set_coalesce(set
);
947 assert(set
&& set
->n
== 1);
950 set
= isl_set_read_from_str(ctx
,
951 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
952 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}");
953 set
= isl_set_coalesce(set
);
954 assert(set
&& set
->n
== 2);
957 set
= isl_set_read_from_str(ctx
,
958 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
959 "y >= 0 & x >= 6 & x <= 10 & y <= x}");
960 set
= isl_set_coalesce(set
);
961 assert(set
&& set
->n
== 1);
964 set
= isl_set_read_from_str(ctx
,
965 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
966 "y >= 0 & x >= 7 & x <= 10 & y <= x}");
967 set
= isl_set_coalesce(set
);
968 assert(set
&& set
->n
== 2);
971 set
= isl_set_read_from_str(ctx
,
972 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
973 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}");
974 set
= isl_set_coalesce(set
);
975 assert(set
&& set
->n
== 2);
978 set
= isl_set_read_from_str(ctx
,
979 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
980 "y >= 0 & x = 6 & y <= 6}");
981 set
= isl_set_coalesce(set
);
982 assert(set
&& set
->n
== 1);
985 set
= isl_set_read_from_str(ctx
,
986 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
987 "y >= 0 & x = 7 & y <= 6}");
988 set
= isl_set_coalesce(set
);
989 assert(set
&& set
->n
== 2);
992 set
= isl_set_read_from_str(ctx
,
993 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
994 "y >= 0 & x = 6 & y <= 5}");
995 set
= isl_set_coalesce(set
);
996 assert(set
&& set
->n
== 1);
997 set2
= isl_set_read_from_str(ctx
,
998 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
999 "y >= 0 & x = 6 & y <= 5}");
1000 assert(isl_set_is_equal(set
, set2
));
1004 set
= isl_set_read_from_str(ctx
,
1005 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1006 "y >= 0 & x = 6 & y <= 7}");
1007 set
= isl_set_coalesce(set
);
1008 assert(set
&& set
->n
== 2);
1011 set
= isl_set_read_from_str(ctx
,
1012 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
1013 set
= isl_set_coalesce(set
);
1014 assert(set
&& set
->n
== 1);
1015 set2
= isl_set_read_from_str(ctx
,
1016 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
1017 assert(isl_set_is_equal(set
, set2
));
1021 set
= isl_set_read_from_str(ctx
,
1022 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
1023 set
= isl_set_coalesce(set
);
1024 set2
= isl_set_read_from_str(ctx
,
1025 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
1026 assert(isl_set_is_equal(set
, set2
));
1030 set
= isl_set_read_from_str(ctx
,
1031 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
1032 "2 <= i and i <= n }");
1033 set
= isl_set_coalesce(set
);
1034 assert(set
&& set
->n
== 1);
1035 set2
= isl_set_read_from_str(ctx
,
1036 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
1037 "2 <= i and i <= n }");
1038 assert(isl_set_is_equal(set
, set2
));
1042 map
= isl_map_read_from_str(ctx
,
1043 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1044 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1045 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1046 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1047 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1048 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1049 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1050 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1051 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1052 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1053 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1054 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1055 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1056 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1057 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1058 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1059 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1060 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
1061 map
= isl_map_coalesce(map
);
1062 map2
= isl_map_read_from_str(ctx
,
1063 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1064 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1065 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1066 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1067 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1068 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1069 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1070 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1071 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1072 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1073 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1074 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1075 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1076 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1077 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1078 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1079 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1080 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
1081 assert(isl_map_is_equal(map
, map2
));
1085 str
= "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1086 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1087 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1088 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
1089 map
= isl_map_read_from_str(ctx
, str
);
1090 map
= isl_map_coalesce(map
);
1091 map2
= isl_map_read_from_str(ctx
, str
);
1092 assert(isl_map_is_equal(map
, map2
));
1096 str
= "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
1097 "[o0, o1, o2, o3, o4, o5, o6] : "
1098 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1099 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1100 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1101 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1102 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1103 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1104 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1105 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1106 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1107 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1108 "o6 >= i3 + i6 - o3 and M >= 0 and "
1109 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1110 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
1111 map
= isl_map_read_from_str(ctx
, str
);
1112 map
= isl_map_coalesce(map
);
1113 map2
= isl_map_read_from_str(ctx
, str
);
1114 assert(isl_map_is_equal(map
, map2
));
1118 str
= "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1119 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1120 "(o0 = 0 and M >= 2 and N >= 3) or "
1121 "(M = 0 and o0 = 0 and N >= 3) }";
1122 map
= isl_map_read_from_str(ctx
, str
);
1123 map
= isl_map_coalesce(map
);
1124 map2
= isl_map_read_from_str(ctx
, str
);
1125 assert(isl_map_is_equal(map
, map2
));
1129 str
= "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1130 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1131 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1132 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
1133 map
= isl_map_read_from_str(ctx
, str
);
1134 map
= isl_map_coalesce(map
);
1135 map2
= isl_map_read_from_str(ctx
, str
);
1136 assert(isl_map_is_equal(map
, map2
));
1140 test_coalesce_set(ctx
,
1141 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
1142 "(i1 = M and M >= 1) }", 0);
1143 test_coalesce_set(ctx
,
1144 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
1145 test_coalesce_set(ctx
,
1146 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1147 "(y = 3 and x = 1) }", 1);
1148 test_coalesce_set(ctx
,
1149 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1150 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1151 "i1 <= M and i3 <= M and i4 <= M) or "
1152 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1153 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1154 "i4 <= -1 + M) }", 1);
1155 test_coalesce_set(ctx
,
1156 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1157 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
1158 if (test_coalesce_unbounded_wrapping(ctx
) < 0)
1160 if (test_coalesce_set(ctx
, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0) < 0)
1162 if (test_coalesce_set(ctx
, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1) < 0)
1164 if (test_coalesce_set(ctx
, "{ [0,0]; [i,i] : 1 <= i <= 10 }", 1) < 0)
1166 if (test_coalesce_set(ctx
, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }", 0) < 0)
1168 if (test_coalesce_set(ctx
, "{ [0,0]; [i,2i] : 1 <= i <= 10 }", 1) < 0)
1170 if (test_coalesce_set(ctx
, "{ [0,0]; [i,2i] : 2 <= i <= 10 }", 0) < 0)
1172 if (test_coalesce_set(ctx
, "{ [1,0]; [i,2i] : 1 <= i <= 10 }", 0) < 0)
1174 if (test_coalesce_set(ctx
, "{ [0,1]; [i,2i] : 1 <= i <= 10 }", 0) < 0)
1176 if (test_coalesce_set(ctx
, "{ [a, b] : exists e : 2e = a and "
1177 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }", 0) < 0)
1179 if (test_coalesce_set(ctx
,
1180 "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1181 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1182 "j >= 1 and j' <= i + j - i' and i >= 1; "
1183 "[1, 1, 1, 1] }", 0) < 0)
1185 if (test_coalesce_set(ctx
, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1186 "[i,j] : exists a : j = 3a }", 1) < 0)
1188 if (test_coalesce_set(ctx
,
1189 "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1190 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1192 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1193 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }", 1) < 0)
1195 if (test_coalesce_set(ctx
,
1196 "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1197 "c <= 6 + 8a and a >= 3; "
1198 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1199 "c <= 7 + 8a and a >= 3 and a <= 4 }", 1) < 0)
1204 void test_closure(struct isl_ctx
*ctx
)
1208 isl_map
*up
, *right
;
1209 isl_map
*map
, *map2
;
1212 /* COCOA example 1 */
1213 map
= isl_map_read_from_str(ctx
,
1214 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1215 "1 <= i and i < n and 1 <= j and j < n or "
1216 "i2 = i + 1 and j2 = j - 1 and "
1217 "1 <= i and i < n and 2 <= j and j <= n }");
1218 map
= isl_map_power(map
, &exact
);
1222 /* COCOA example 1 */
1223 map
= isl_map_read_from_str(ctx
,
1224 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1225 "1 <= i and i < n and 1 <= j and j < n or "
1226 "i2 = i + 1 and j2 = j - 1 and "
1227 "1 <= i and i < n and 2 <= j and j <= n }");
1228 map
= isl_map_transitive_closure(map
, &exact
);
1230 map2
= isl_map_read_from_str(ctx
,
1231 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1232 "1 <= i and i < n and 1 <= j and j <= n and "
1233 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1234 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1235 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1236 assert(isl_map_is_equal(map
, map2
));
1240 map
= isl_map_read_from_str(ctx
,
1241 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1242 " 0 <= y and y <= n }");
1243 map
= isl_map_transitive_closure(map
, &exact
);
1244 map2
= isl_map_read_from_str(ctx
,
1245 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1246 " 0 <= y and y <= n }");
1247 assert(isl_map_is_equal(map
, map2
));
1251 /* COCOA example 2 */
1252 map
= isl_map_read_from_str(ctx
,
1253 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1254 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1255 "i2 = i + 2 and j2 = j - 2 and "
1256 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1257 map
= isl_map_transitive_closure(map
, &exact
);
1259 map2
= isl_map_read_from_str(ctx
,
1260 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1261 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1262 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1263 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1264 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1265 assert(isl_map_is_equal(map
, map2
));
1269 /* COCOA Fig.2 left */
1270 map
= isl_map_read_from_str(ctx
,
1271 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1272 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1274 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1275 "j <= 2 i - 3 and j <= n - 2 or "
1276 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1277 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1278 map
= isl_map_transitive_closure(map
, &exact
);
1282 /* COCOA Fig.2 right */
1283 map
= isl_map_read_from_str(ctx
,
1284 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1285 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1287 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1288 "j <= 2 i - 4 and j <= n - 3 or "
1289 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1290 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1291 map
= isl_map_power(map
, &exact
);
1295 /* COCOA Fig.2 right */
1296 map
= isl_map_read_from_str(ctx
,
1297 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1298 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1300 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1301 "j <= 2 i - 4 and j <= n - 3 or "
1302 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1303 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1304 map
= isl_map_transitive_closure(map
, &exact
);
1306 map2
= isl_map_read_from_str(ctx
,
1307 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1308 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1309 "j <= n and 3 + i + 2 j <= 3 n and "
1310 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1311 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1312 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1313 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1314 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1315 assert(isl_map_is_equal(map
, map2
));
1319 /* COCOA Fig.1 right */
1320 dom
= isl_set_read_from_str(ctx
,
1321 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1322 "2 x - 3 y + 3 >= 0 }");
1323 right
= isl_map_read_from_str(ctx
,
1324 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1325 up
= isl_map_read_from_str(ctx
,
1326 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1327 right
= isl_map_intersect_domain(right
, isl_set_copy(dom
));
1328 right
= isl_map_intersect_range(right
, isl_set_copy(dom
));
1329 up
= isl_map_intersect_domain(up
, isl_set_copy(dom
));
1330 up
= isl_map_intersect_range(up
, dom
);
1331 map
= isl_map_union(up
, right
);
1332 map
= isl_map_transitive_closure(map
, &exact
);
1334 map2
= isl_map_read_from_str(ctx
,
1335 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1336 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1337 assert(isl_map_is_equal(map
, map2
));
1341 /* COCOA Theorem 1 counter example */
1342 map
= isl_map_read_from_str(ctx
,
1343 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1344 "i2 = 1 and j2 = j or "
1345 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1346 map
= isl_map_transitive_closure(map
, &exact
);
1350 map
= isl_map_read_from_str(ctx
,
1351 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1352 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1353 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1354 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1355 map
= isl_map_transitive_closure(map
, &exact
);
1359 /* Kelly et al 1996, fig 12 */
1360 map
= isl_map_read_from_str(ctx
,
1361 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1362 "1 <= i,j,j+1 <= n or "
1363 "j = n and j2 = 1 and i2 = i + 1 and "
1364 "1 <= i,i+1 <= n }");
1365 map
= isl_map_transitive_closure(map
, &exact
);
1367 map2
= isl_map_read_from_str(ctx
,
1368 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1369 "1 <= i <= n and i = i2 or "
1370 "1 <= i < i2 <= n and 1 <= j <= n and "
1372 assert(isl_map_is_equal(map
, map2
));
1376 /* Omega's closure4 */
1377 map
= isl_map_read_from_str(ctx
,
1378 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1379 "1 <= x,y <= 10 or "
1380 "x2 = x + 1 and y2 = y and "
1381 "1 <= x <= 20 && 5 <= y <= 15 }");
1382 map
= isl_map_transitive_closure(map
, &exact
);
1386 map
= isl_map_read_from_str(ctx
,
1387 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1388 map
= isl_map_transitive_closure(map
, &exact
);
1390 map2
= isl_map_read_from_str(ctx
,
1391 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1392 assert(isl_map_is_equal(map
, map2
));
1396 str
= "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1397 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1398 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1399 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1400 map
= isl_map_read_from_str(ctx
, str
);
1401 map
= isl_map_transitive_closure(map
, &exact
);
1403 map2
= isl_map_read_from_str(ctx
, str
);
1404 assert(isl_map_is_equal(map
, map2
));
1408 str
= "{[0] -> [1]; [2] -> [3]}";
1409 map
= isl_map_read_from_str(ctx
, str
);
1410 map
= isl_map_transitive_closure(map
, &exact
);
1412 map2
= isl_map_read_from_str(ctx
, str
);
1413 assert(isl_map_is_equal(map
, map2
));
1417 str
= "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1418 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1419 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1420 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1421 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1422 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1423 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1424 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1425 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1426 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1427 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1428 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1429 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1430 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1431 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1432 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1433 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1434 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1435 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1436 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1437 map
= isl_map_read_from_str(ctx
, str
);
1438 map
= isl_map_transitive_closure(map
, NULL
);
1443 void test_lex(struct isl_ctx
*ctx
)
1448 dim
= isl_space_set_alloc(ctx
, 0, 0);
1449 map
= isl_map_lex_le(dim
);
1450 assert(!isl_map_is_empty(map
));
1454 void test_lexmin(struct isl_ctx
*ctx
)
1457 isl_basic_map
*bmap
;
1458 isl_map
*map
, *map2
;
1461 isl_pw_multi_aff
*pma
;
1463 str
= "[p0, p1] -> { [] -> [] : "
1464 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1465 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1466 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1467 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1468 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1469 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1470 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1471 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1472 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1473 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1474 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1475 map
= isl_map_read_from_str(ctx
, str
);
1476 map
= isl_map_lexmin(map
);
1479 str
= "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1480 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1481 set
= isl_set_read_from_str(ctx
, str
);
1482 set
= isl_set_lexmax(set
);
1483 str
= "[C] -> { [obj,a,b,c] : C = 8 }";
1484 set2
= isl_set_read_from_str(ctx
, str
);
1485 set
= isl_set_intersect(set
, set2
);
1486 assert(!isl_set_is_empty(set
));
1489 str
= "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1490 map
= isl_map_read_from_str(ctx
, str
);
1491 map
= isl_map_lexmin(map
);
1492 str
= "{ [x] -> [5] : 6 <= x <= 8; "
1493 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1494 map2
= isl_map_read_from_str(ctx
, str
);
1495 assert(isl_map_is_equal(map
, map2
));
1499 str
= "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1500 map
= isl_map_read_from_str(ctx
, str
);
1501 map2
= isl_map_copy(map
);
1502 map
= isl_map_lexmin(map
);
1503 assert(isl_map_is_equal(map
, map2
));
1507 str
= "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1508 map
= isl_map_read_from_str(ctx
, str
);
1509 map
= isl_map_lexmin(map
);
1510 str
= "{ [x] -> [y] : (4y = x and x >= 0) or "
1511 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1512 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1513 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1514 map2
= isl_map_read_from_str(ctx
, str
);
1515 assert(isl_map_is_equal(map
, map2
));
1519 str
= "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1520 " 8i' <= i and 8i' >= -7 + i }";
1521 bmap
= isl_basic_map_read_from_str(ctx
, str
);
1522 pma
= isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap
));
1523 map2
= isl_map_from_pw_multi_aff(pma
);
1524 map
= isl_map_from_basic_map(bmap
);
1525 assert(isl_map_is_equal(map
, map2
));
1529 str
= "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1530 map
= isl_map_read_from_str(ctx
, str
);
1531 map
= isl_map_lexmin(map
);
1532 str
= "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1533 map2
= isl_map_read_from_str(ctx
, str
);
1534 assert(isl_map_is_equal(map
, map2
));
1544 static int collect_must_may(__isl_take isl_map
*dep
, int must
,
1545 void *dep_user
, void *user
)
1547 struct must_may
*mm
= (struct must_may
*)user
;
1550 mm
->must
= isl_map_union(mm
->must
, dep
);
1552 mm
->may
= isl_map_union(mm
->may
, dep
);
1557 static int common_space(void *first
, void *second
)
1559 int depth
= *(int *)first
;
1563 static int map_is_equal(__isl_keep isl_map
*map
, const char *str
)
1571 map2
= isl_map_read_from_str(map
->ctx
, str
);
1572 equal
= isl_map_is_equal(map
, map2
);
1578 static int map_check_equal(__isl_keep isl_map
*map
, const char *str
)
1582 equal
= map_is_equal(map
, str
);
1586 isl_die(isl_map_get_ctx(map
), isl_error_unknown
,
1587 "result not as expected", return -1);
1591 void test_dep(struct isl_ctx
*ctx
)
1596 isl_access_info
*ai
;
1603 str
= "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1604 map
= isl_map_read_from_str(ctx
, str
);
1605 ai
= isl_access_info_alloc(map
, &depth
, &common_space
, 2);
1607 str
= "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1608 map
= isl_map_read_from_str(ctx
, str
);
1609 ai
= isl_access_info_add_source(ai
, map
, 1, &depth
);
1611 str
= "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1612 map
= isl_map_read_from_str(ctx
, str
);
1613 ai
= isl_access_info_add_source(ai
, map
, 1, &depth
);
1615 flow
= isl_access_info_compute_flow(ai
);
1616 dim
= isl_space_alloc(ctx
, 0, 3, 3);
1617 mm
.must
= isl_map_empty(isl_space_copy(dim
));
1618 mm
.may
= isl_map_empty(dim
);
1620 isl_flow_foreach(flow
, collect_must_may
, &mm
);
1622 str
= "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1623 " [1,10,0] -> [2,5,0] }";
1624 assert(map_is_equal(mm
.must
, str
));
1625 str
= "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1626 assert(map_is_equal(mm
.may
, str
));
1628 isl_map_free(mm
.must
);
1629 isl_map_free(mm
.may
);
1630 isl_flow_free(flow
);
1633 str
= "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1634 map
= isl_map_read_from_str(ctx
, str
);
1635 ai
= isl_access_info_alloc(map
, &depth
, &common_space
, 2);
1637 str
= "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1638 map
= isl_map_read_from_str(ctx
, str
);
1639 ai
= isl_access_info_add_source(ai
, map
, 1, &depth
);
1641 str
= "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1642 map
= isl_map_read_from_str(ctx
, str
);
1643 ai
= isl_access_info_add_source(ai
, map
, 0, &depth
);
1645 flow
= isl_access_info_compute_flow(ai
);
1646 dim
= isl_space_alloc(ctx
, 0, 3, 3);
1647 mm
.must
= isl_map_empty(isl_space_copy(dim
));
1648 mm
.may
= isl_map_empty(dim
);
1650 isl_flow_foreach(flow
, collect_must_may
, &mm
);
1652 str
= "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1653 assert(map_is_equal(mm
.must
, str
));
1654 str
= "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1655 assert(map_is_equal(mm
.may
, str
));
1657 isl_map_free(mm
.must
);
1658 isl_map_free(mm
.may
);
1659 isl_flow_free(flow
);
1662 str
= "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1663 map
= isl_map_read_from_str(ctx
, str
);
1664 ai
= isl_access_info_alloc(map
, &depth
, &common_space
, 2);
1666 str
= "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1667 map
= isl_map_read_from_str(ctx
, str
);
1668 ai
= isl_access_info_add_source(ai
, map
, 0, &depth
);
1670 str
= "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1671 map
= isl_map_read_from_str(ctx
, str
);
1672 ai
= isl_access_info_add_source(ai
, map
, 0, &depth
);
1674 flow
= isl_access_info_compute_flow(ai
);
1675 dim
= isl_space_alloc(ctx
, 0, 3, 3);
1676 mm
.must
= isl_map_empty(isl_space_copy(dim
));
1677 mm
.may
= isl_map_empty(dim
);
1679 isl_flow_foreach(flow
, collect_must_may
, &mm
);
1681 str
= "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1682 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1683 assert(map_is_equal(mm
.may
, str
));
1684 str
= "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1685 assert(map_is_equal(mm
.must
, str
));
1687 isl_map_free(mm
.must
);
1688 isl_map_free(mm
.may
);
1689 isl_flow_free(flow
);
1692 str
= "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1693 map
= isl_map_read_from_str(ctx
, str
);
1694 ai
= isl_access_info_alloc(map
, &depth
, &common_space
, 2);
1696 str
= "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1697 map
= isl_map_read_from_str(ctx
, str
);
1698 ai
= isl_access_info_add_source(ai
, map
, 0, &depth
);
1700 str
= "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1701 map
= isl_map_read_from_str(ctx
, str
);
1702 ai
= isl_access_info_add_source(ai
, map
, 0, &depth
);
1704 flow
= isl_access_info_compute_flow(ai
);
1705 dim
= isl_space_alloc(ctx
, 0, 3, 3);
1706 mm
.must
= isl_map_empty(isl_space_copy(dim
));
1707 mm
.may
= isl_map_empty(dim
);
1709 isl_flow_foreach(flow
, collect_must_may
, &mm
);
1711 str
= "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1712 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1713 assert(map_is_equal(mm
.may
, str
));
1714 str
= "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1715 assert(map_is_equal(mm
.must
, str
));
1717 isl_map_free(mm
.must
);
1718 isl_map_free(mm
.may
);
1719 isl_flow_free(flow
);
1722 str
= "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1723 map
= isl_map_read_from_str(ctx
, str
);
1724 ai
= isl_access_info_alloc(map
, &depth
, &common_space
, 2);
1726 str
= "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1727 map
= isl_map_read_from_str(ctx
, str
);
1728 ai
= isl_access_info_add_source(ai
, map
, 0, &depth
);
1730 str
= "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1731 map
= isl_map_read_from_str(ctx
, str
);
1732 ai
= isl_access_info_add_source(ai
, map
, 0, &depth
);
1734 flow
= isl_access_info_compute_flow(ai
);
1735 dim
= isl_space_alloc(ctx
, 0, 3, 3);
1736 mm
.must
= isl_map_empty(isl_space_copy(dim
));
1737 mm
.may
= isl_map_empty(dim
);
1739 isl_flow_foreach(flow
, collect_must_may
, &mm
);
1741 str
= "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1742 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1743 assert(map_is_equal(mm
.may
, str
));
1744 str
= "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1745 assert(map_is_equal(mm
.must
, str
));
1747 isl_map_free(mm
.must
);
1748 isl_map_free(mm
.may
);
1749 isl_flow_free(flow
);
1754 str
= "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1755 map
= isl_map_read_from_str(ctx
, str
);
1756 ai
= isl_access_info_alloc(map
, &depth
, &common_space
, 1);
1758 str
= "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1759 map
= isl_map_read_from_str(ctx
, str
);
1760 ai
= isl_access_info_add_source(ai
, map
, 1, &depth
);
1762 flow
= isl_access_info_compute_flow(ai
);
1763 dim
= isl_space_alloc(ctx
, 0, 5, 5);
1764 mm
.must
= isl_map_empty(isl_space_copy(dim
));
1765 mm
.may
= isl_map_empty(dim
);
1767 isl_flow_foreach(flow
, collect_must_may
, &mm
);
1769 str
= "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1770 assert(map_is_equal(mm
.must
, str
));
1771 str
= "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1772 assert(map_is_equal(mm
.may
, str
));
1774 isl_map_free(mm
.must
);
1775 isl_map_free(mm
.may
);
1776 isl_flow_free(flow
);
1779 int test_sv(isl_ctx
*ctx
)
1783 isl_union_map
*umap
;
1786 str
= "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1787 map
= isl_map_read_from_str(ctx
, str
);
1788 sv
= isl_map_is_single_valued(map
);
1793 isl_die(ctx
, isl_error_internal
,
1794 "map not detected as single valued", return -1);
1796 str
= "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1797 map
= isl_map_read_from_str(ctx
, str
);
1798 sv
= isl_map_is_single_valued(map
);
1803 isl_die(ctx
, isl_error_internal
,
1804 "map detected as single valued", return -1);
1806 str
= "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
1807 umap
= isl_union_map_read_from_str(ctx
, str
);
1808 sv
= isl_union_map_is_single_valued(umap
);
1809 isl_union_map_free(umap
);
1813 isl_die(ctx
, isl_error_internal
,
1814 "map not detected as single valued", return -1);
1816 str
= "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }";
1817 umap
= isl_union_map_read_from_str(ctx
, str
);
1818 sv
= isl_union_map_is_single_valued(umap
);
1819 isl_union_map_free(umap
);
1823 isl_die(ctx
, isl_error_internal
,
1824 "map detected as single valued", return -1);
1829 void test_bijective_case(struct isl_ctx
*ctx
, const char *str
, int bijective
)
1833 map
= isl_map_read_from_str(ctx
, str
);
1835 assert(isl_map_is_bijective(map
));
1837 assert(!isl_map_is_bijective(map
));
1841 void test_bijective(struct isl_ctx
*ctx
)
1843 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [i]}", 0);
1844 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1845 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1846 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1847 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [j,i]}", 1);
1848 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [i+j]}", 0);
1849 test_bijective_case(ctx
, "[N,M]->{[i,j] -> []}", 0);
1850 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1851 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [2i]}", 0);
1852 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [i,i]}", 0);
1853 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [2i,i]}", 0);
1854 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [2i,j]}", 1);
1855 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1858 void test_pwqp(struct isl_ctx
*ctx
)
1862 isl_pw_qpolynomial
*pwqp1
, *pwqp2
;
1864 str
= "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1865 pwqp1
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1867 pwqp1
= isl_pw_qpolynomial_move_dims(pwqp1
, isl_dim_param
, 0,
1870 str
= "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1871 pwqp2
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1873 pwqp1
= isl_pw_qpolynomial_sub(pwqp1
, pwqp2
);
1875 assert(isl_pw_qpolynomial_is_zero(pwqp1
));
1877 isl_pw_qpolynomial_free(pwqp1
);
1879 str
= "{ [i] -> i }";
1880 pwqp1
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1881 str
= "{ [k] : exists a : k = 2a }";
1882 set
= isl_set_read_from_str(ctx
, str
);
1883 pwqp1
= isl_pw_qpolynomial_gist(pwqp1
, set
);
1884 str
= "{ [i] -> i }";
1885 pwqp2
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1887 pwqp1
= isl_pw_qpolynomial_sub(pwqp1
, pwqp2
);
1889 assert(isl_pw_qpolynomial_is_zero(pwqp1
));
1891 isl_pw_qpolynomial_free(pwqp1
);
1893 str
= "{ [i] -> i + [ (i + [i/3])/2 ] }";
1894 pwqp1
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1896 set
= isl_set_read_from_str(ctx
, str
);
1897 pwqp1
= isl_pw_qpolynomial_gist(pwqp1
, set
);
1898 str
= "{ [i] -> 16 }";
1899 pwqp2
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1901 pwqp1
= isl_pw_qpolynomial_sub(pwqp1
, pwqp2
);
1903 assert(isl_pw_qpolynomial_is_zero(pwqp1
));
1905 isl_pw_qpolynomial_free(pwqp1
);
1907 str
= "{ [i] -> ([(i)/2]) }";
1908 pwqp1
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1909 str
= "{ [k] : exists a : k = 2a+1 }";
1910 set
= isl_set_read_from_str(ctx
, str
);
1911 pwqp1
= isl_pw_qpolynomial_gist(pwqp1
, set
);
1912 str
= "{ [i] -> -1/2 + 1/2 * i }";
1913 pwqp2
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1915 pwqp1
= isl_pw_qpolynomial_sub(pwqp1
, pwqp2
);
1917 assert(isl_pw_qpolynomial_is_zero(pwqp1
));
1919 isl_pw_qpolynomial_free(pwqp1
);
1921 str
= "{ [i] -> ([([i/2] + [i/2])/5]) }";
1922 pwqp1
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1923 str
= "{ [i] -> ([(2 * [i/2])/5]) }";
1924 pwqp2
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1926 pwqp1
= isl_pw_qpolynomial_sub(pwqp1
, pwqp2
);
1928 assert(isl_pw_qpolynomial_is_zero(pwqp1
));
1930 isl_pw_qpolynomial_free(pwqp1
);
1932 str
= "{ [x] -> ([x/2] + [(x+1)/2]) }";
1933 pwqp1
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1934 str
= "{ [x] -> x }";
1935 pwqp2
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1937 pwqp1
= isl_pw_qpolynomial_sub(pwqp1
, pwqp2
);
1939 assert(isl_pw_qpolynomial_is_zero(pwqp1
));
1941 isl_pw_qpolynomial_free(pwqp1
);
1943 str
= "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
1944 pwqp1
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1945 pwqp2
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1946 pwqp1
= isl_pw_qpolynomial_coalesce(pwqp1
);
1947 pwqp1
= isl_pw_qpolynomial_sub(pwqp1
, pwqp2
);
1948 assert(isl_pw_qpolynomial_is_zero(pwqp1
));
1949 isl_pw_qpolynomial_free(pwqp1
);
1952 void test_split_periods(isl_ctx
*ctx
)
1955 isl_pw_qpolynomial
*pwqp
;
1957 str
= "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1958 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
1959 "U >= 0; [U,V] -> U^2 : U >= 100 }";
1960 pwqp
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1962 pwqp
= isl_pw_qpolynomial_split_periods(pwqp
, 2);
1965 isl_pw_qpolynomial_free(pwqp
);
1968 void test_union(isl_ctx
*ctx
)
1971 isl_union_set
*uset1
, *uset2
;
1972 isl_union_map
*umap1
, *umap2
;
1974 str
= "{ [i] : 0 <= i <= 1 }";
1975 uset1
= isl_union_set_read_from_str(ctx
, str
);
1976 str
= "{ [1] -> [0] }";
1977 umap1
= isl_union_map_read_from_str(ctx
, str
);
1979 umap2
= isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1
), uset1
);
1980 assert(isl_union_map_is_equal(umap1
, umap2
));
1982 isl_union_map_free(umap1
);
1983 isl_union_map_free(umap2
);
1985 str
= "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1986 umap1
= isl_union_map_read_from_str(ctx
, str
);
1987 str
= "{ A[i]; B[i] }";
1988 uset1
= isl_union_set_read_from_str(ctx
, str
);
1990 uset2
= isl_union_map_domain(umap1
);
1992 assert(isl_union_set_is_equal(uset1
, uset2
));
1994 isl_union_set_free(uset1
);
1995 isl_union_set_free(uset2
);
1998 void test_bound(isl_ctx
*ctx
)
2001 isl_pw_qpolynomial
*pwqp
;
2002 isl_pw_qpolynomial_fold
*pwf
;
2004 str
= "{ [[a, b, c, d] -> [e]] -> 0 }";
2005 pwqp
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
2006 pwf
= isl_pw_qpolynomial_bound(pwqp
, isl_fold_max
, NULL
);
2007 assert(isl_pw_qpolynomial_fold_dim(pwf
, isl_dim_in
) == 4);
2008 isl_pw_qpolynomial_fold_free(pwf
);
2010 str
= "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2011 pwqp
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
2012 pwf
= isl_pw_qpolynomial_bound(pwqp
, isl_fold_max
, NULL
);
2013 assert(isl_pw_qpolynomial_fold_dim(pwf
, isl_dim_in
) == 1);
2014 isl_pw_qpolynomial_fold_free(pwf
);
2017 void test_lift(isl_ctx
*ctx
)
2020 isl_basic_map
*bmap
;
2021 isl_basic_set
*bset
;
2023 str
= "{ [i0] : exists e0 : i0 = 4e0 }";
2024 bset
= isl_basic_set_read_from_str(ctx
, str
);
2025 bset
= isl_basic_set_lift(bset
);
2026 bmap
= isl_basic_map_from_range(bset
);
2027 bset
= isl_basic_map_domain(bmap
);
2028 isl_basic_set_free(bset
);
2035 } subset_tests
[] = {
2037 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2038 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2039 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2041 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2042 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2043 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2044 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2045 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2046 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2047 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2048 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2049 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2050 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2051 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2052 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2053 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2056 static int test_subset(isl_ctx
*ctx
)
2059 isl_set
*set1
, *set2
;
2062 for (i
= 0; i
< ARRAY_SIZE(subset_tests
); ++i
) {
2063 set1
= isl_set_read_from_str(ctx
, subset_tests
[i
].set1
);
2064 set2
= isl_set_read_from_str(ctx
, subset_tests
[i
].set2
);
2065 subset
= isl_set_is_subset(set1
, set2
);
2070 if (subset
!= subset_tests
[i
].subset
)
2071 isl_die(ctx
, isl_error_unknown
,
2072 "incorrect subset result", return -1);
2079 const char *minuend
;
2080 const char *subtrahend
;
2081 const char *difference
;
2082 } subtract_domain_tests
[] = {
2083 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2084 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2085 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2088 static int test_subtract(isl_ctx
*ctx
)
2091 isl_union_map
*umap1
, *umap2
;
2092 isl_union_set
*uset
;
2095 for (i
= 0; i
< ARRAY_SIZE(subtract_domain_tests
); ++i
) {
2096 umap1
= isl_union_map_read_from_str(ctx
,
2097 subtract_domain_tests
[i
].minuend
);
2098 uset
= isl_union_set_read_from_str(ctx
,
2099 subtract_domain_tests
[i
].subtrahend
);
2100 umap2
= isl_union_map_read_from_str(ctx
,
2101 subtract_domain_tests
[i
].difference
);
2102 umap1
= isl_union_map_subtract_domain(umap1
, uset
);
2103 equal
= isl_union_map_is_equal(umap1
, umap2
);
2104 isl_union_map_free(umap1
);
2105 isl_union_map_free(umap2
);
2109 isl_die(ctx
, isl_error_unknown
,
2110 "incorrect subtract domain result", return -1);
2116 int test_factorize(isl_ctx
*ctx
)
2119 isl_basic_set
*bset
;
2122 str
= "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2123 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2124 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2125 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2126 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2127 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2128 "3i5 >= -2i0 - i2 + 3i4 }";
2129 bset
= isl_basic_set_read_from_str(ctx
, str
);
2130 f
= isl_basic_set_factorizer(bset
);
2131 isl_basic_set_free(bset
);
2132 isl_factorizer_free(f
);
2134 isl_die(ctx
, isl_error_unknown
,
2135 "failed to construct factorizer", return -1);
2137 str
= "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2138 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2139 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2140 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2141 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2142 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2143 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2144 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2145 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2146 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2147 bset
= isl_basic_set_read_from_str(ctx
, str
);
2148 f
= isl_basic_set_factorizer(bset
);
2149 isl_basic_set_free(bset
);
2150 isl_factorizer_free(f
);
2152 isl_die(ctx
, isl_error_unknown
,
2153 "failed to construct factorizer", return -1);
2158 static int check_injective(__isl_take isl_map
*map
, void *user
)
2160 int *injective
= user
;
2162 *injective
= isl_map_is_injective(map
);
2165 if (*injective
< 0 || !*injective
)
2171 int test_one_schedule(isl_ctx
*ctx
, const char *d
, const char *w
,
2172 const char *r
, const char *s
, int tilable
, int parallel
)
2176 isl_union_map
*W
, *R
, *S
;
2177 isl_union_map
*empty
;
2178 isl_union_map
*dep_raw
, *dep_war
, *dep_waw
, *dep
;
2179 isl_union_map
*validity
, *proximity
;
2180 isl_union_map
*schedule
;
2181 isl_union_map
*test
;
2182 isl_union_set
*delta
;
2183 isl_union_set
*domain
;
2187 isl_schedule
*sched
;
2188 int is_nonneg
, is_parallel
, is_tilable
, is_injection
, is_complete
;
2190 D
= isl_union_set_read_from_str(ctx
, d
);
2191 W
= isl_union_map_read_from_str(ctx
, w
);
2192 R
= isl_union_map_read_from_str(ctx
, r
);
2193 S
= isl_union_map_read_from_str(ctx
, s
);
2195 W
= isl_union_map_intersect_domain(W
, isl_union_set_copy(D
));
2196 R
= isl_union_map_intersect_domain(R
, isl_union_set_copy(D
));
2198 empty
= isl_union_map_empty(isl_union_map_get_space(S
));
2199 isl_union_map_compute_flow(isl_union_map_copy(R
),
2200 isl_union_map_copy(W
), empty
,
2201 isl_union_map_copy(S
),
2202 &dep_raw
, NULL
, NULL
, NULL
);
2203 isl_union_map_compute_flow(isl_union_map_copy(W
),
2204 isl_union_map_copy(W
),
2205 isl_union_map_copy(R
),
2206 isl_union_map_copy(S
),
2207 &dep_waw
, &dep_war
, NULL
, NULL
);
2209 dep
= isl_union_map_union(dep_waw
, dep_war
);
2210 dep
= isl_union_map_union(dep
, dep_raw
);
2211 validity
= isl_union_map_copy(dep
);
2212 proximity
= isl_union_map_copy(dep
);
2214 sched
= isl_union_set_compute_schedule(isl_union_set_copy(D
),
2215 validity
, proximity
);
2216 schedule
= isl_schedule_get_map(sched
);
2217 isl_schedule_free(sched
);
2218 isl_union_map_free(W
);
2219 isl_union_map_free(R
);
2220 isl_union_map_free(S
);
2223 isl_union_map_foreach_map(schedule
, &check_injective
, &is_injection
);
2225 domain
= isl_union_map_domain(isl_union_map_copy(schedule
));
2226 is_complete
= isl_union_set_is_subset(D
, domain
);
2227 isl_union_set_free(D
);
2228 isl_union_set_free(domain
);
2230 test
= isl_union_map_reverse(isl_union_map_copy(schedule
));
2231 test
= isl_union_map_apply_range(test
, dep
);
2232 test
= isl_union_map_apply_range(test
, schedule
);
2234 delta
= isl_union_map_deltas(test
);
2235 if (isl_union_set_n_set(delta
) == 0) {
2239 isl_union_set_free(delta
);
2241 delta_set
= isl_set_from_union_set(delta
);
2243 slice
= isl_set_universe(isl_set_get_space(delta_set
));
2244 for (i
= 0; i
< tilable
; ++i
)
2245 slice
= isl_set_lower_bound_si(slice
, isl_dim_set
, i
, 0);
2246 is_tilable
= isl_set_is_subset(delta_set
, slice
);
2247 isl_set_free(slice
);
2249 slice
= isl_set_universe(isl_set_get_space(delta_set
));
2250 for (i
= 0; i
< parallel
; ++i
)
2251 slice
= isl_set_fix_si(slice
, isl_dim_set
, i
, 0);
2252 is_parallel
= isl_set_is_subset(delta_set
, slice
);
2253 isl_set_free(slice
);
2255 origin
= isl_set_universe(isl_set_get_space(delta_set
));
2256 for (i
= 0; i
< isl_set_dim(origin
, isl_dim_set
); ++i
)
2257 origin
= isl_set_fix_si(origin
, isl_dim_set
, i
, 0);
2259 delta_set
= isl_set_union(delta_set
, isl_set_copy(origin
));
2260 delta_set
= isl_set_lexmin(delta_set
);
2262 is_nonneg
= isl_set_is_equal(delta_set
, origin
);
2264 isl_set_free(origin
);
2265 isl_set_free(delta_set
);
2268 if (is_nonneg
< 0 || is_parallel
< 0 || is_tilable
< 0 ||
2269 is_injection
< 0 || is_complete
< 0)
2272 isl_die(ctx
, isl_error_unknown
,
2273 "generated schedule incomplete", return -1);
2275 isl_die(ctx
, isl_error_unknown
,
2276 "generated schedule not injective on each statement",
2279 isl_die(ctx
, isl_error_unknown
,
2280 "negative dependences in generated schedule",
2283 isl_die(ctx
, isl_error_unknown
,
2284 "generated schedule not as tilable as expected",
2287 isl_die(ctx
, isl_error_unknown
,
2288 "generated schedule not as parallel as expected",
2294 static __isl_give isl_union_map
*compute_schedule(isl_ctx
*ctx
,
2295 const char *domain
, const char *validity
, const char *proximity
)
2299 isl_union_map
*prox
;
2300 isl_schedule
*schedule
;
2301 isl_union_map
*sched
;
2303 dom
= isl_union_set_read_from_str(ctx
, domain
);
2304 dep
= isl_union_map_read_from_str(ctx
, validity
);
2305 prox
= isl_union_map_read_from_str(ctx
, proximity
);
2306 schedule
= isl_union_set_compute_schedule(dom
, dep
, prox
);
2307 sched
= isl_schedule_get_map(schedule
);
2308 isl_schedule_free(schedule
);
2313 /* Check that a schedule can be constructed on the given domain
2314 * with the given validity and proximity constraints.
2316 static int test_has_schedule(isl_ctx
*ctx
, const char *domain
,
2317 const char *validity
, const char *proximity
)
2319 isl_union_map
*sched
;
2321 sched
= compute_schedule(ctx
, domain
, validity
, proximity
);
2325 isl_union_map_free(sched
);
2329 int test_special_schedule(isl_ctx
*ctx
, const char *domain
,
2330 const char *validity
, const char *proximity
, const char *expected_sched
)
2332 isl_union_map
*sched1
, *sched2
;
2335 sched1
= compute_schedule(ctx
, domain
, validity
, proximity
);
2336 sched2
= isl_union_map_read_from_str(ctx
, expected_sched
);
2338 equal
= isl_union_map_is_equal(sched1
, sched2
);
2339 isl_union_map_free(sched1
);
2340 isl_union_map_free(sched2
);
2345 isl_die(ctx
, isl_error_unknown
, "unexpected schedule",
2351 int test_schedule(isl_ctx
*ctx
)
2353 const char *D
, *W
, *R
, *V
, *P
, *S
;
2355 /* Handle resulting schedule with zero bands. */
2356 if (test_one_schedule(ctx
, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2360 D
= "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2361 W
= "{ S1[t,i] -> a[t,i] }";
2362 R
= "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2363 "S1[t,i] -> a[t-1,i+1] }";
2364 S
= "{ S1[t,i] -> [t,i] }";
2365 if (test_one_schedule(ctx
, D
, W
, R
, S
, 2, 0) < 0)
2368 /* Fig. 5 of CC2008 */
2369 D
= "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2371 W
= "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2372 "j >= 2 and j <= -1 + N }";
2373 R
= "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2374 "j >= 2 and j <= -1 + N; "
2375 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2376 "j >= 2 and j <= -1 + N }";
2377 S
= "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2378 if (test_one_schedule(ctx
, D
, W
, R
, S
, 2, 0) < 0)
2381 D
= "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2382 W
= "{ S1[i] -> a[i] }";
2383 R
= "{ S2[i] -> a[i+1] }";
2384 S
= "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2385 if (test_one_schedule(ctx
, D
, W
, R
, S
, 1, 1) < 0)
2388 D
= "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2389 W
= "{ S1[i] -> a[i] }";
2390 R
= "{ S2[i] -> a[9-i] }";
2391 S
= "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2392 if (test_one_schedule(ctx
, D
, W
, R
, S
, 1, 1) < 0)
2395 D
= "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2396 W
= "{ S1[i] -> a[i] }";
2397 R
= "[N] -> { S2[i] -> a[N-1-i] }";
2398 S
= "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2399 if (test_one_schedule(ctx
, D
, W
, R
, S
, 1, 1) < 0)
2402 D
= "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2403 W
= "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2404 R
= "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2405 S
= "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2406 if (test_one_schedule(ctx
, D
, W
, R
, S
, 0, 0) < 0)
2409 D
= "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2410 W
= "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2411 R
= "{ S2[i,j] -> a[i-1,j] }";
2412 S
= "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2413 if (test_one_schedule(ctx
, D
, W
, R
, S
, 2, 1) < 0)
2416 D
= "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2417 W
= "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2418 R
= "{ S2[i,j] -> a[i,j-1] }";
2419 S
= "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2420 if (test_one_schedule(ctx
, D
, W
, R
, S
, 2, 1) < 0)
2423 D
= "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2424 W
= "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2425 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2426 R
= "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2427 S
= "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2428 "S_0[] -> [0, 0, 0] }";
2429 if (test_one_schedule(ctx
, D
, W
, R
, S
, 1, 0) < 0)
2431 ctx
->opt
->schedule_parametric
= 0;
2432 if (test_one_schedule(ctx
, D
, W
, R
, S
, 0, 0) < 0)
2434 ctx
->opt
->schedule_parametric
= 1;
2436 D
= "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2437 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2438 W
= "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2439 R
= "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2440 "S4[i] -> a[i,N] }";
2441 S
= "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2442 "S4[i] -> [4,i,0] }";
2443 if (test_one_schedule(ctx
, D
, W
, R
, S
, 2, 0) < 0)
2446 D
= "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2447 W
= "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2449 R
= "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2451 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2453 S
= "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2454 if (test_one_schedule(ctx
, D
, W
, R
, S
, 0, 0) < 0)
2457 D
= "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2458 " S_2[t] : t >= 0 and t <= -1 + N; "
2459 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2461 W
= "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2462 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2463 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2464 "i >= 0 and i <= -1 + N }";
2465 R
= "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2466 "i >= 0 and i <= -1 + N; "
2467 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2468 S
= "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2469 " S_0[t] -> [0, t, 0] }";
2471 if (test_one_schedule(ctx
, D
, W
, R
, S
, 2, 1) < 0)
2473 ctx
->opt
->schedule_parametric
= 0;
2474 if (test_one_schedule(ctx
, D
, W
, R
, S
, 0, 0) < 0)
2476 ctx
->opt
->schedule_parametric
= 1;
2478 D
= "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2479 S
= "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2480 if (test_one_schedule(ctx
, D
, "{}", "{}", S
, 2, 2) < 0)
2483 D
= "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2484 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2485 W
= "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2486 "j >= 0 and j <= -1 + N; "
2487 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2488 R
= "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2489 "j >= 0 and j <= -1 + N; "
2490 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2491 S
= "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2492 if (test_one_schedule(ctx
, D
, W
, R
, S
, 0, 0) < 0)
2495 D
= "{ S_0[i] : i >= 0 }";
2496 W
= "{ S_0[i] -> a[i] : i >= 0 }";
2497 R
= "{ S_0[i] -> a[0] : i >= 0 }";
2498 S
= "{ S_0[i] -> [0, i, 0] }";
2499 if (test_one_schedule(ctx
, D
, W
, R
, S
, 0, 0) < 0)
2502 D
= "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2503 W
= "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2504 R
= "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2505 S
= "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2506 if (test_one_schedule(ctx
, D
, W
, R
, S
, 0, 0) < 0)
2509 D
= "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2510 "k <= -1 + n and k >= 0 }";
2511 W
= "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
2512 R
= "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2513 "k <= -1 + n and k >= 0; "
2514 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2515 "k <= -1 + n and k >= 0; "
2516 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2517 "k <= -1 + n and k >= 0 }";
2518 S
= "[n] -> { S_0[j, k] -> [2, j, k] }";
2519 ctx
->opt
->schedule_outer_zero_distance
= 1;
2520 if (test_one_schedule(ctx
, D
, W
, R
, S
, 0, 0) < 0)
2522 ctx
->opt
->schedule_outer_zero_distance
= 0;
2524 D
= "{Stmt_for_body24[i0, i1, i2, i3]:"
2525 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
2526 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
2527 "Stmt_for_body24[i0, i1, 1, 0]:"
2528 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
2529 "Stmt_for_body7[i0, i1, i2]:"
2530 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
2533 V
= "{Stmt_for_body24[0, i1, i2, i3] -> "
2534 "Stmt_for_body24[1, i1, i2, i3]:"
2535 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
2537 "Stmt_for_body24[0, i1, i2, i3] -> "
2538 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
2539 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
2541 "Stmt_for_body24[0, i1, i2, i3] ->"
2542 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
2543 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
2544 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
2545 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
2546 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
2547 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
2548 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
2549 "i1 <= 6 and i1 >= 0;"
2550 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
2551 "Stmt_for_body7[i0, i1, i2] -> "
2552 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
2553 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
2554 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
2555 "Stmt_for_body7[i0, i1, i2] -> "
2556 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
2557 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
2558 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
2560 S
= "{ Stmt_for_body24[i0, i1, i2, i3] -> "
2561 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
2562 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
2564 if (test_special_schedule(ctx
, D
, V
, P
, S
) < 0)
2567 D
= "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
2568 V
= "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
2569 "j >= 1 and j <= 7;"
2570 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
2571 "j >= 1 and j <= 8 }";
2573 S
= "{ S_0[i, j] -> [i + j, j] }";
2574 ctx
->opt
->schedule_algorithm
= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
;
2575 if (test_special_schedule(ctx
, D
, V
, P
, S
) < 0)
2577 ctx
->opt
->schedule_algorithm
= ISL_SCHEDULE_ALGORITHM_ISL
;
2579 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
2580 D
= "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
2581 "j >= 0 and j <= -1 + i }";
2582 V
= "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
2583 "i <= -1 + N and j >= 0;"
2584 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
2587 S
= "{ S_0[i, j] -> [i, j] }";
2588 ctx
->opt
->schedule_algorithm
= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
;
2589 if (test_special_schedule(ctx
, D
, V
, P
, S
) < 0)
2591 ctx
->opt
->schedule_algorithm
= ISL_SCHEDULE_ALGORITHM_ISL
;
2593 /* Test both algorithms on a case with only proximity dependences. */
2594 D
= "{ S[i,j] : 0 <= i <= 10 }";
2596 P
= "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
2597 S
= "{ S[i, j] -> [j, i] }";
2598 ctx
->opt
->schedule_algorithm
= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
;
2599 if (test_special_schedule(ctx
, D
, V
, P
, S
) < 0)
2601 ctx
->opt
->schedule_algorithm
= ISL_SCHEDULE_ALGORITHM_ISL
;
2602 if (test_special_schedule(ctx
, D
, V
, P
, S
) < 0)
2605 D
= "{ A[a]; B[] }";
2607 P
= "{ A[a] -> B[] }";
2608 if (test_has_schedule(ctx
, D
, V
, P
) < 0)
2614 int test_plain_injective(isl_ctx
*ctx
, const char *str
, int injective
)
2616 isl_union_map
*umap
;
2619 umap
= isl_union_map_read_from_str(ctx
, str
);
2620 test
= isl_union_map_plain_is_injective(umap
);
2621 isl_union_map_free(umap
);
2624 if (test
== injective
)
2627 isl_die(ctx
, isl_error_unknown
,
2628 "map not detected as injective", return -1);
2630 isl_die(ctx
, isl_error_unknown
,
2631 "map detected as injective", return -1);
2634 int test_injective(isl_ctx
*ctx
)
2638 if (test_plain_injective(ctx
, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2640 if (test_plain_injective(ctx
, "{S[] -> A[0]; T[] -> B[0]}", 1))
2642 if (test_plain_injective(ctx
, "{S[] -> A[0]; T[] -> A[1]}", 1))
2644 if (test_plain_injective(ctx
, "{S[] -> A[0]; T[] -> A[0]}", 0))
2646 if (test_plain_injective(ctx
, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2648 if (test_plain_injective(ctx
, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2650 if (test_plain_injective(ctx
, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2652 if (test_plain_injective(ctx
, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2655 str
= "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2656 if (test_plain_injective(ctx
, str
, 1))
2658 str
= "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2659 if (test_plain_injective(ctx
, str
, 0))
2665 static int aff_plain_is_equal(__isl_keep isl_aff
*aff
, const char *str
)
2673 aff2
= isl_aff_read_from_str(isl_aff_get_ctx(aff
), str
);
2674 equal
= isl_aff_plain_is_equal(aff
, aff2
);
2680 static int aff_check_plain_equal(__isl_keep isl_aff
*aff
, const char *str
)
2684 equal
= aff_plain_is_equal(aff
, str
);
2688 isl_die(isl_aff_get_ctx(aff
), isl_error_unknown
,
2689 "result not as expected", return -1);
2693 int test_aff(isl_ctx
*ctx
)
2698 isl_local_space
*ls
;
2702 space
= isl_space_set_alloc(ctx
, 0, 1);
2703 ls
= isl_local_space_from_space(space
);
2704 aff
= isl_aff_zero_on_domain(ls
);
2706 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
2707 aff
= isl_aff_scale_down_ui(aff
, 3);
2708 aff
= isl_aff_floor(aff
);
2709 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
2710 aff
= isl_aff_scale_down_ui(aff
, 2);
2711 aff
= isl_aff_floor(aff
);
2712 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
2715 set
= isl_set_read_from_str(ctx
, str
);
2716 aff
= isl_aff_gist(aff
, set
);
2718 aff
= isl_aff_add_constant_si(aff
, -16);
2719 zero
= isl_aff_plain_is_zero(aff
);
2725 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2727 aff
= isl_aff_read_from_str(ctx
, "{ [-1] }");
2728 aff
= isl_aff_scale_down_ui(aff
, 64);
2729 aff
= isl_aff_floor(aff
);
2730 equal
= aff_check_plain_equal(aff
, "{ [-1] }");
2738 int test_dim_max(isl_ctx
*ctx
)
2742 isl_set
*set1
, *set2
;
2747 str
= "[N] -> { [i] : 0 <= i <= min(N,10) }";
2748 set
= isl_set_read_from_str(ctx
, str
);
2749 pwaff
= isl_set_dim_max(set
, 0);
2750 set1
= isl_set_from_pw_aff(pwaff
);
2751 str
= "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
2752 set2
= isl_set_read_from_str(ctx
, str
);
2753 equal
= isl_set_is_equal(set1
, set2
);
2759 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2761 str
= "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
2762 set
= isl_set_read_from_str(ctx
, str
);
2763 pwaff
= isl_set_dim_max(set
, 0);
2764 set1
= isl_set_from_pw_aff(pwaff
);
2765 str
= "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2766 set2
= isl_set_read_from_str(ctx
, str
);
2767 equal
= isl_set_is_equal(set1
, set2
);
2773 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2775 str
= "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
2776 set
= isl_set_read_from_str(ctx
, str
);
2777 pwaff
= isl_set_dim_max(set
, 0);
2778 set1
= isl_set_from_pw_aff(pwaff
);
2779 str
= "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2780 set2
= isl_set_read_from_str(ctx
, str
);
2781 equal
= isl_set_is_equal(set1
, set2
);
2787 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2789 str
= "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
2790 "0 <= i < N and 0 <= j < M }";
2791 map
= isl_map_read_from_str(ctx
, str
);
2792 set
= isl_map_range(map
);
2794 pwaff
= isl_set_dim_max(isl_set_copy(set
), 0);
2795 set1
= isl_set_from_pw_aff(pwaff
);
2796 str
= "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
2797 set2
= isl_set_read_from_str(ctx
, str
);
2798 equal
= isl_set_is_equal(set1
, set2
);
2802 pwaff
= isl_set_dim_max(isl_set_copy(set
), 3);
2803 set1
= isl_set_from_pw_aff(pwaff
);
2804 str
= "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
2805 set2
= isl_set_read_from_str(ctx
, str
);
2806 if (equal
>= 0 && equal
)
2807 equal
= isl_set_is_equal(set1
, set2
);
2816 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2818 /* Check that solutions are properly merged. */
2819 str
= "[n] -> { [a, b, c] : c >= -4a - 2b and "
2820 "c <= -1 + n - 4a - 2b and c >= -2b and "
2821 "4a >= -4 + n and c >= 0 }";
2822 set
= isl_set_read_from_str(ctx
, str
);
2823 pwaff
= isl_set_dim_min(set
, 2);
2824 set1
= isl_set_from_pw_aff(pwaff
);
2825 str
= "[n] -> { [(0)] : n >= 1 }";
2826 set2
= isl_set_read_from_str(ctx
, str
);
2827 equal
= isl_set_is_equal(set1
, set2
);
2834 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2839 int test_product(isl_ctx
*ctx
)
2843 isl_union_set
*uset1
, *uset2
;
2847 set
= isl_set_read_from_str(ctx
, str
);
2848 set
= isl_set_product(set
, isl_set_copy(set
));
2849 ok
= isl_set_is_wrapping(set
);
2854 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2857 uset1
= isl_union_set_read_from_str(ctx
, str
);
2858 uset1
= isl_union_set_product(uset1
, isl_union_set_copy(uset1
));
2859 str
= "{ [[] -> []] }";
2860 uset2
= isl_union_set_read_from_str(ctx
, str
);
2861 ok
= isl_union_set_is_equal(uset1
, uset2
);
2862 isl_union_set_free(uset1
);
2863 isl_union_set_free(uset2
);
2867 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2872 int test_equal(isl_ctx
*ctx
)
2875 isl_set
*set
, *set2
;
2879 set
= isl_set_read_from_str(ctx
, str
);
2881 set2
= isl_set_read_from_str(ctx
, str
);
2882 equal
= isl_set_is_equal(set
, set2
);
2888 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2893 static int test_plain_fixed(isl_ctx
*ctx
, __isl_take isl_map
*map
,
2894 enum isl_dim_type type
, unsigned pos
, int fixed
)
2898 test
= isl_map_plain_is_fixed(map
, type
, pos
, NULL
);
2905 isl_die(ctx
, isl_error_unknown
,
2906 "map not detected as fixed", return -1);
2908 isl_die(ctx
, isl_error_unknown
,
2909 "map detected as fixed", return -1);
2912 int test_fixed(isl_ctx
*ctx
)
2917 str
= "{ [i] -> [i] }";
2918 map
= isl_map_read_from_str(ctx
, str
);
2919 if (test_plain_fixed(ctx
, map
, isl_dim_out
, 0, 0))
2921 str
= "{ [i] -> [1] }";
2922 map
= isl_map_read_from_str(ctx
, str
);
2923 if (test_plain_fixed(ctx
, map
, isl_dim_out
, 0, 1))
2925 str
= "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
2926 map
= isl_map_read_from_str(ctx
, str
);
2927 if (test_plain_fixed(ctx
, map
, isl_dim_out
, 0, 1))
2929 map
= isl_map_read_from_str(ctx
, str
);
2930 map
= isl_map_neg(map
);
2931 if (test_plain_fixed(ctx
, map
, isl_dim_out
, 0, 1))
2937 int test_vertices(isl_ctx
*ctx
)
2940 isl_basic_set
*bset
;
2941 isl_vertices
*vertices
;
2943 str
= "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
2944 bset
= isl_basic_set_read_from_str(ctx
, str
);
2945 vertices
= isl_basic_set_compute_vertices(bset
);
2946 isl_basic_set_free(bset
);
2947 isl_vertices_free(vertices
);
2951 str
= "{ A[t, i] : t = 14 and i = 1 }";
2952 bset
= isl_basic_set_read_from_str(ctx
, str
);
2953 vertices
= isl_basic_set_compute_vertices(bset
);
2954 isl_basic_set_free(bset
);
2955 isl_vertices_free(vertices
);
2962 int test_union_pw(isl_ctx
*ctx
)
2966 isl_union_set
*uset
;
2967 isl_union_pw_qpolynomial
*upwqp1
, *upwqp2
;
2969 str
= "{ [x] -> x^2 }";
2970 upwqp1
= isl_union_pw_qpolynomial_read_from_str(ctx
, str
);
2971 upwqp2
= isl_union_pw_qpolynomial_copy(upwqp1
);
2972 uset
= isl_union_pw_qpolynomial_domain(upwqp1
);
2973 upwqp1
= isl_union_pw_qpolynomial_copy(upwqp2
);
2974 upwqp1
= isl_union_pw_qpolynomial_intersect_domain(upwqp1
, uset
);
2975 equal
= isl_union_pw_qpolynomial_plain_is_equal(upwqp1
, upwqp2
);
2976 isl_union_pw_qpolynomial_free(upwqp1
);
2977 isl_union_pw_qpolynomial_free(upwqp2
);
2981 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2986 int test_output(isl_ctx
*ctx
)
2994 str
= "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
2995 pa
= isl_pw_aff_read_from_str(ctx
, str
);
2997 p
= isl_printer_to_str(ctx
);
2998 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
2999 p
= isl_printer_print_pw_aff(p
, pa
);
3000 s
= isl_printer_get_str(p
);
3001 isl_printer_free(p
);
3002 isl_pw_aff_free(pa
);
3006 equal
= !strcmp(s
, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
3011 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
3016 int test_sample(isl_ctx
*ctx
)
3019 isl_basic_set
*bset1
, *bset2
;
3022 str
= "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3023 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3024 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3025 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3026 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3027 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3028 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3029 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3030 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3031 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3032 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3033 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3034 "d - 1073741821e and "
3035 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3036 "3j >= 1 - a + b + 2e and "
3037 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3038 "3i <= 4 - a + 4b - e and "
3039 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3040 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3041 "c <= -1 + a and 3i >= -2 - a + 3e and "
3042 "1073741822e <= 1073741823 - a + 1073741822b + c and "
3043 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3044 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3045 "1073741823e >= 1 + 1073741823b - d and "
3046 "3i >= 1073741823b + c - 1073741823e - f and "
3047 "3i >= 1 + 2b + e + 3g }";
3048 bset1
= isl_basic_set_read_from_str(ctx
, str
);
3049 bset2
= isl_basic_set_sample(isl_basic_set_copy(bset1
));
3050 empty
= isl_basic_set_is_empty(bset2
);
3051 subset
= isl_basic_set_is_subset(bset2
, bset1
);
3052 isl_basic_set_free(bset1
);
3053 isl_basic_set_free(bset2
);
3054 if (empty
< 0 || subset
< 0)
3057 isl_die(ctx
, isl_error_unknown
, "point not found", return -1);
3059 isl_die(ctx
, isl_error_unknown
, "bad point found", return -1);
3064 int test_fixed_power(isl_ctx
*ctx
)
3072 str
= "{ [i] -> [i + 1] }";
3073 map
= isl_map_read_from_str(ctx
, str
);
3074 isl_int_set_si(exp
, 23);
3075 map
= isl_map_fixed_power(map
, exp
);
3076 equal
= map_check_equal(map
, "{ [i] -> [i + 23] }");
3085 int test_slice(isl_ctx
*ctx
)
3091 str
= "{ [i] -> [j] }";
3092 map
= isl_map_read_from_str(ctx
, str
);
3093 map
= isl_map_equate(map
, isl_dim_in
, 0, isl_dim_out
, 0);
3094 equal
= map_check_equal(map
, "{ [i] -> [i] }");
3099 str
= "{ [i] -> [j] }";
3100 map
= isl_map_read_from_str(ctx
, str
);
3101 map
= isl_map_equate(map
, isl_dim_in
, 0, isl_dim_in
, 0);
3102 equal
= map_check_equal(map
, "{ [i] -> [j] }");
3107 str
= "{ [i] -> [j] }";
3108 map
= isl_map_read_from_str(ctx
, str
);
3109 map
= isl_map_oppose(map
, isl_dim_in
, 0, isl_dim_out
, 0);
3110 equal
= map_check_equal(map
, "{ [i] -> [-i] }");
3115 str
= "{ [i] -> [j] }";
3116 map
= isl_map_read_from_str(ctx
, str
);
3117 map
= isl_map_oppose(map
, isl_dim_in
, 0, isl_dim_in
, 0);
3118 equal
= map_check_equal(map
, "{ [0] -> [j] }");
3123 str
= "{ [i] -> [j] }";
3124 map
= isl_map_read_from_str(ctx
, str
);
3125 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_out
, 0);
3126 equal
= map_check_equal(map
, "{ [i] -> [j] : i > j }");
3131 str
= "{ [i] -> [j] }";
3132 map
= isl_map_read_from_str(ctx
, str
);
3133 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_in
, 0);
3134 equal
= map_check_equal(map
, "{ [i] -> [j] : false }");
3142 int test_eliminate(isl_ctx
*ctx
)
3148 str
= "{ [i] -> [j] : i = 2j }";
3149 map
= isl_map_read_from_str(ctx
, str
);
3150 map
= isl_map_eliminate(map
, isl_dim_out
, 0, 1);
3151 equal
= map_check_equal(map
, "{ [i] -> [j] : exists a : i = 2a }");
3159 /* Check that isl_set_dim_residue_class detects that the values of j
3160 * in the set below are all odd and that it does not detect any spurious
3163 static int test_residue_class(isl_ctx
*ctx
)
3170 str
= "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3171 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3172 set
= isl_set_read_from_str(ctx
, str
);
3175 res
= isl_set_dim_residue_class(set
, 1, &m
, &r
);
3177 (isl_int_cmp_si(m
, 2) != 0 || isl_int_cmp_si(r
, 1) != 0))
3178 isl_die(ctx
, isl_error_unknown
, "incorrect residue class",
3187 int test_align_parameters(isl_ctx
*ctx
)
3191 isl_multi_aff
*ma1
, *ma2
;
3194 str
= "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
3195 ma1
= isl_multi_aff_read_from_str(ctx
, str
);
3197 space
= isl_space_params_alloc(ctx
, 1);
3198 space
= isl_space_set_dim_name(space
, isl_dim_param
, 0, "N");
3199 ma1
= isl_multi_aff_align_params(ma1
, space
);
3201 str
= "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
3202 ma2
= isl_multi_aff_read_from_str(ctx
, str
);
3204 equal
= isl_multi_aff_plain_is_equal(ma1
, ma2
);
3206 isl_multi_aff_free(ma1
);
3207 isl_multi_aff_free(ma2
);
3212 isl_die(ctx
, isl_error_unknown
,
3213 "result not as expected", return -1);
3218 static int test_list(isl_ctx
*ctx
)
3220 isl_id
*a
, *b
, *c
, *d
, *id
;
3224 a
= isl_id_alloc(ctx
, "a", NULL
);
3225 b
= isl_id_alloc(ctx
, "b", NULL
);
3226 c
= isl_id_alloc(ctx
, "c", NULL
);
3227 d
= isl_id_alloc(ctx
, "d", NULL
);
3229 list
= isl_id_list_alloc(ctx
, 4);
3230 list
= isl_id_list_add(list
, a
);
3231 list
= isl_id_list_add(list
, b
);
3232 list
= isl_id_list_add(list
, c
);
3233 list
= isl_id_list_add(list
, d
);
3234 list
= isl_id_list_drop(list
, 1, 1);
3236 if (isl_id_list_n_id(list
) != 3) {
3237 isl_id_list_free(list
);
3238 isl_die(ctx
, isl_error_unknown
,
3239 "unexpected number of elements in list", return -1);
3242 id
= isl_id_list_get_id(list
, 0);
3245 id
= isl_id_list_get_id(list
, 1);
3248 id
= isl_id_list_get_id(list
, 2);
3252 isl_id_list_free(list
);
3255 isl_die(ctx
, isl_error_unknown
,
3256 "unexpected elements in list", return -1);
3261 const char *set_conversion_tests
[] = {
3262 "[N] -> { [i] : N - 1 <= 2 i <= N }",
3263 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
3264 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3265 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3268 /* Check that converting from isl_set to isl_pw_multi_aff and back
3269 * to isl_set produces the original isl_set.
3271 static int test_set_conversion(isl_ctx
*ctx
)
3275 isl_set
*set1
, *set2
;
3276 isl_pw_multi_aff
*pma
;
3279 for (i
= 0; i
< ARRAY_SIZE(set_conversion_tests
); ++i
) {
3280 str
= set_conversion_tests
[i
];
3281 set1
= isl_set_read_from_str(ctx
, str
);
3282 pma
= isl_pw_multi_aff_from_set(isl_set_copy(set1
));
3283 set2
= isl_set_from_pw_multi_aff(pma
);
3284 equal
= isl_set_is_equal(set1
, set2
);
3291 isl_die(ctx
, isl_error_unknown
, "bad conversion",
3298 /* Check that converting from isl_map to isl_pw_multi_aff and back
3299 * to isl_map produces the original isl_map.
3301 static int test_map_conversion(isl_ctx
*ctx
)
3304 isl_map
*map1
, *map2
;
3305 isl_pw_multi_aff
*pma
;
3308 str
= "{ [a, b, c, d] -> s0[a, b, e, f] : "
3309 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
3310 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
3311 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
3313 map1
= isl_map_read_from_str(ctx
, str
);
3314 pma
= isl_pw_multi_aff_from_map(isl_map_copy(map1
));
3315 map2
= isl_map_from_pw_multi_aff(pma
);
3316 equal
= isl_map_is_equal(map1
, map2
);
3323 isl_die(ctx
, isl_error_unknown
, "bad conversion", return -1);
3328 static int test_conversion(isl_ctx
*ctx
)
3330 if (test_set_conversion(ctx
) < 0)
3332 if (test_map_conversion(ctx
) < 0)
3337 /* Check that isl_basic_map_curry does not modify input.
3339 static int test_curry(isl_ctx
*ctx
)
3342 isl_basic_map
*bmap1
, *bmap2
;
3345 str
= "{ [A[] -> B[]] -> C[] }";
3346 bmap1
= isl_basic_map_read_from_str(ctx
, str
);
3347 bmap2
= isl_basic_map_curry(isl_basic_map_copy(bmap1
));
3348 equal
= isl_basic_map_is_equal(bmap1
, bmap2
);
3349 isl_basic_map_free(bmap1
);
3350 isl_basic_map_free(bmap2
);
3355 isl_die(ctx
, isl_error_unknown
,
3356 "curried map should not be equal to original",
3366 } preimage_tests
[] = {
3367 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
3368 "{ A[j,i] -> B[i,j] }",
3369 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
3370 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3371 "{ A[a,b] -> B[a/2,b/6] }",
3372 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
3373 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3374 "{ A[a,b] -> B[a/2,b/6] }",
3375 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
3376 "exists i,j : a = 2 i and b = 6 j }" },
3377 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
3378 "[n] -> { : 0 <= n <= 100 }" },
3379 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3380 "{ A[a] -> B[2a] }",
3381 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
3382 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3383 "{ A[a] -> B[([a/2])] }",
3384 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
3385 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
3386 "{ A[a] -> B[a,a,a/3] }",
3387 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
3388 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
3389 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
3392 int test_preimage(isl_ctx
*ctx
)
3395 isl_basic_set
*bset1
, *bset2
;
3399 for (i
= 0; i
< ARRAY_SIZE(preimage_tests
); ++i
) {
3400 bset1
= isl_basic_set_read_from_str(ctx
, preimage_tests
[i
].set
);
3401 ma
= isl_multi_aff_read_from_str(ctx
, preimage_tests
[i
].ma
);
3402 bset2
= isl_basic_set_read_from_str(ctx
, preimage_tests
[i
].res
);
3403 bset1
= isl_basic_set_preimage_multi_aff(bset1
, ma
);
3404 equal
= isl_basic_set_is_equal(bset1
, bset2
);
3405 isl_basic_set_free(bset1
);
3406 isl_basic_set_free(bset2
);
3410 isl_die(ctx
, isl_error_unknown
, "bad preimage",
3421 } pullback_tests
[] = {
3422 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
3423 "{ A[a,b] -> C[b + 2a] }" },
3424 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
3425 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
3426 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
3427 "{ A[a] -> C[(a)/6] }" },
3428 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
3429 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
3430 "{ A[a] -> C[(2a)/3] }" },
3431 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
3432 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
3433 "{ A[i,j] -> C[i + j, i + j] }"},
3434 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
3435 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
3436 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
3439 static int test_pullback(isl_ctx
*ctx
)
3442 isl_multi_aff
*ma1
, *ma2
;
3446 for (i
= 0; i
< ARRAY_SIZE(pullback_tests
); ++i
) {
3447 ma1
= isl_multi_aff_read_from_str(ctx
, pullback_tests
[i
].ma1
);
3448 ma
= isl_multi_aff_read_from_str(ctx
, pullback_tests
[i
].ma
);
3449 ma2
= isl_multi_aff_read_from_str(ctx
, pullback_tests
[i
].res
);
3450 ma1
= isl_multi_aff_pullback_multi_aff(ma1
, ma
);
3451 equal
= isl_multi_aff_plain_is_equal(ma1
, ma2
);
3452 isl_multi_aff_free(ma1
);
3453 isl_multi_aff_free(ma2
);
3457 isl_die(ctx
, isl_error_unknown
, "bad pullback",
3464 /* Check that negation is printed correctly.
3466 static int test_ast(isl_ctx
*ctx
)
3468 isl_ast_expr
*expr
, *expr1
, *expr2
, *expr3
;
3472 expr1
= isl_ast_expr_from_id(isl_id_alloc(ctx
, "A", NULL
));
3473 expr2
= isl_ast_expr_from_id(isl_id_alloc(ctx
, "B", NULL
));
3474 expr
= isl_ast_expr_add(expr1
, expr2
);
3475 expr
= isl_ast_expr_neg(expr
);
3476 str
= isl_ast_expr_to_str(expr
);
3477 ok
= str
? !strcmp(str
, "-(A + B)") : -1;
3479 isl_ast_expr_free(expr
);
3484 isl_die(ctx
, isl_error_unknown
,
3485 "isl_ast_expr printed incorrectly", return -1);
3487 expr1
= isl_ast_expr_from_id(isl_id_alloc(ctx
, "A", NULL
));
3488 expr2
= isl_ast_expr_from_id(isl_id_alloc(ctx
, "B", NULL
));
3489 expr
= isl_ast_expr_add(expr1
, expr2
);
3490 expr3
= isl_ast_expr_from_id(isl_id_alloc(ctx
, "C", NULL
));
3491 expr
= isl_ast_expr_sub(expr3
, expr
);
3492 str
= isl_ast_expr_to_str(expr
);
3493 ok
= str
? !strcmp(str
, "C - (A + B)") : -1;
3495 isl_ast_expr_free(expr
);
3500 isl_die(ctx
, isl_error_unknown
,
3501 "isl_ast_expr printed incorrectly", return -1);
3506 /* Internal data structure for before_for and after_for callbacks.
3508 * depth is the current depth
3509 * before is the number of times before_for has been called
3510 * after is the number of times after_for has been called
3512 struct isl_test_codegen_data
{
3518 /* This function is called before each for loop in the AST generated
3519 * from test_ast_gen1.
3521 * Increment the number of calls and the depth.
3522 * Check that the space returned by isl_ast_build_get_schedule_space
3523 * matches the target space of the schedule returned by
3524 * isl_ast_build_get_schedule.
3525 * Return an isl_id that is checked by the corresponding call
3528 static __isl_give isl_id
*before_for(__isl_keep isl_ast_build
*build
,
3531 struct isl_test_codegen_data
*data
= user
;
3534 isl_union_map
*schedule
;
3535 isl_union_set
*uset
;
3540 ctx
= isl_ast_build_get_ctx(build
);
3542 if (data
->before
>= 3)
3543 isl_die(ctx
, isl_error_unknown
,
3544 "unexpected number of for nodes", return NULL
);
3545 if (data
->depth
>= 2)
3546 isl_die(ctx
, isl_error_unknown
,
3547 "unexpected depth", return NULL
);
3549 snprintf(name
, sizeof(name
), "d%d", data
->depth
);
3553 schedule
= isl_ast_build_get_schedule(build
);
3554 uset
= isl_union_map_range(schedule
);
3557 if (isl_union_set_n_set(uset
) != 1) {
3558 isl_union_set_free(uset
);
3559 isl_die(ctx
, isl_error_unknown
,
3560 "expecting single range space", return NULL
);
3563 space
= isl_ast_build_get_schedule_space(build
);
3564 set
= isl_union_set_extract_set(uset
, space
);
3565 isl_union_set_free(uset
);
3566 empty
= isl_set_is_empty(set
);
3572 isl_die(ctx
, isl_error_unknown
,
3573 "spaces don't match", return NULL
);
3575 return isl_id_alloc(ctx
, name
, NULL
);
3578 /* This function is called after each for loop in the AST generated
3579 * from test_ast_gen1.
3581 * Increment the number of calls and decrement the depth.
3582 * Check that the annotation attached to the node matches
3583 * the isl_id returned by the corresponding call to before_for.
3585 static __isl_give isl_ast_node
*after_for(__isl_take isl_ast_node
*node
,
3586 __isl_keep isl_ast_build
*build
, void *user
)
3588 struct isl_test_codegen_data
*data
= user
;
3596 if (data
->after
> data
->before
)
3597 isl_die(isl_ast_node_get_ctx(node
), isl_error_unknown
,
3598 "mismatch in number of for nodes",
3599 return isl_ast_node_free(node
));
3601 id
= isl_ast_node_get_annotation(node
);
3603 isl_die(isl_ast_node_get_ctx(node
), isl_error_unknown
,
3604 "missing annotation", return isl_ast_node_free(node
));
3606 name
= isl_id_get_name(id
);
3607 valid
= name
&& atoi(name
+ 1) == data
->depth
;
3611 isl_die(isl_ast_node_get_ctx(node
), isl_error_unknown
,
3612 "wrong annotation", return isl_ast_node_free(node
));
3617 /* Check that the before_each_for and after_each_for callbacks
3618 * are called for each for loop in the generated code,
3619 * that they are called in the right order and that the isl_id
3620 * returned from the before_each_for callback is attached to
3621 * the isl_ast_node passed to the corresponding after_each_for call.
3623 static int test_ast_gen1(isl_ctx
*ctx
)
3627 isl_union_map
*schedule
;
3628 isl_ast_build
*build
;
3630 struct isl_test_codegen_data data
;
3632 str
= "[N] -> { : N >= 10 }";
3633 set
= isl_set_read_from_str(ctx
, str
);
3634 str
= "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
3635 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
3636 schedule
= isl_union_map_read_from_str(ctx
, str
);
3641 build
= isl_ast_build_from_context(set
);
3642 build
= isl_ast_build_set_before_each_for(build
,
3643 &before_for
, &data
);
3644 build
= isl_ast_build_set_after_each_for(build
,
3646 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
3647 isl_ast_build_free(build
);
3651 isl_ast_node_free(tree
);
3653 if (data
.before
!= 3 || data
.after
!= 3)
3654 isl_die(ctx
, isl_error_unknown
,
3655 "unexpected number of for nodes", return -1);
3660 /* Check that the AST generator handles domains that are integrally disjoint
3661 * but not ratinoally disjoint.
3663 static int test_ast_gen2(isl_ctx
*ctx
)
3667 isl_union_map
*schedule
;
3668 isl_union_map
*options
;
3669 isl_ast_build
*build
;
3672 str
= "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
3673 schedule
= isl_union_map_read_from_str(ctx
, str
);
3674 set
= isl_set_universe(isl_space_params_alloc(ctx
, 0));
3675 build
= isl_ast_build_from_context(set
);
3677 str
= "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
3678 options
= isl_union_map_read_from_str(ctx
, str
);
3679 build
= isl_ast_build_set_options(build
, options
);
3680 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
3681 isl_ast_build_free(build
);
3684 isl_ast_node_free(tree
);
3689 /* Increment *user on each call.
3691 static __isl_give isl_ast_node
*count_domains(__isl_take isl_ast_node
*node
,
3692 __isl_keep isl_ast_build
*build
, void *user
)
3701 /* Test that unrolling tries to minimize the number of instances.
3702 * In particular, for the schedule given below, make sure it generates
3703 * 3 nodes (rather than 101).
3705 static int test_ast_gen3(isl_ctx
*ctx
)
3709 isl_union_map
*schedule
;
3710 isl_union_map
*options
;
3711 isl_ast_build
*build
;
3715 str
= "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
3716 schedule
= isl_union_map_read_from_str(ctx
, str
);
3717 set
= isl_set_universe(isl_space_params_alloc(ctx
, 0));
3719 str
= "{ [i] -> unroll[0] }";
3720 options
= isl_union_map_read_from_str(ctx
, str
);
3722 build
= isl_ast_build_from_context(set
);
3723 build
= isl_ast_build_set_options(build
, options
);
3724 build
= isl_ast_build_set_at_each_domain(build
,
3725 &count_domains
, &n_domain
);
3726 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
3727 isl_ast_build_free(build
);
3731 isl_ast_node_free(tree
);
3734 isl_die(ctx
, isl_error_unknown
,
3735 "unexpected number of for nodes", return -1);
3740 /* Check that if the ast_build_exploit_nested_bounds options is set,
3741 * we do not get an outer if node in the generated AST,
3742 * while we do get such an outer if node if the options is not set.
3744 static int test_ast_gen4(isl_ctx
*ctx
)
3748 isl_union_map
*schedule
;
3749 isl_ast_build
*build
;
3751 enum isl_ast_node_type type
;
3754 enb
= isl_options_get_ast_build_exploit_nested_bounds(ctx
);
3755 str
= "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
3757 isl_options_set_ast_build_exploit_nested_bounds(ctx
, 1);
3759 schedule
= isl_union_map_read_from_str(ctx
, str
);
3760 set
= isl_set_universe(isl_space_params_alloc(ctx
, 0));
3761 build
= isl_ast_build_from_context(set
);
3762 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
3763 isl_ast_build_free(build
);
3767 type
= isl_ast_node_get_type(tree
);
3768 isl_ast_node_free(tree
);
3770 if (type
== isl_ast_node_if
)
3771 isl_die(ctx
, isl_error_unknown
,
3772 "not expecting if node", return -1);
3774 isl_options_set_ast_build_exploit_nested_bounds(ctx
, 0);
3776 schedule
= isl_union_map_read_from_str(ctx
, str
);
3777 set
= isl_set_universe(isl_space_params_alloc(ctx
, 0));
3778 build
= isl_ast_build_from_context(set
);
3779 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
3780 isl_ast_build_free(build
);
3784 type
= isl_ast_node_get_type(tree
);
3785 isl_ast_node_free(tree
);
3787 if (type
!= isl_ast_node_if
)
3788 isl_die(ctx
, isl_error_unknown
,
3789 "expecting if node", return -1);
3791 isl_options_set_ast_build_exploit_nested_bounds(ctx
, enb
);
3796 static int test_ast_gen(isl_ctx
*ctx
)
3798 if (test_ast_gen1(ctx
) < 0)
3800 if (test_ast_gen2(ctx
) < 0)
3802 if (test_ast_gen3(ctx
) < 0)
3804 if (test_ast_gen4(ctx
) < 0)
3809 /* Check if dropping output dimensions from an isl_pw_multi_aff
3812 static int test_pw_multi_aff(isl_ctx
*ctx
)
3815 isl_pw_multi_aff
*pma1
, *pma2
;
3818 str
= "{ [i,j] -> [i+j, 4i-j] }";
3819 pma1
= isl_pw_multi_aff_read_from_str(ctx
, str
);
3820 str
= "{ [i,j] -> [4i-j] }";
3821 pma2
= isl_pw_multi_aff_read_from_str(ctx
, str
);
3823 pma1
= isl_pw_multi_aff_drop_dims(pma1
, isl_dim_out
, 0, 1);
3825 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
3827 isl_pw_multi_aff_free(pma1
);
3828 isl_pw_multi_aff_free(pma2
);
3832 isl_die(ctx
, isl_error_unknown
,
3833 "expressions not equal", return -1);
3838 /* This is a regression test for a bug where isl_basic_map_simplify
3839 * would end up in an infinite loop. In particular, we construct
3840 * an empty basic set that is not obviously empty.
3841 * isl_basic_set_is_empty marks the basic set as empty.
3842 * After projecting out i3, the variable can be dropped completely,
3843 * but isl_basic_map_simplify refrains from doing so if the basic set
3844 * is empty and would end up in an infinite loop if it didn't test
3845 * explicitly for empty basic maps in the outer loop.
3847 static int test_simplify(isl_ctx
*ctx
)
3850 isl_basic_set
*bset
;
3853 str
= "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
3854 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
3855 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
3857 bset
= isl_basic_set_read_from_str(ctx
, str
);
3858 empty
= isl_basic_set_is_empty(bset
);
3859 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 3, 1);
3860 isl_basic_set_free(bset
);
3864 isl_die(ctx
, isl_error_unknown
,
3865 "basic set should be empty", return -1);
3870 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
3871 * with gbr context would fail to disable the use of the shifted tableau
3872 * when transferring equalities for the input to the context, resulting
3873 * in invalid sample values.
3875 static int test_partial_lexmin(isl_ctx
*ctx
)
3878 isl_basic_set
*bset
;
3879 isl_basic_map
*bmap
;
3882 str
= "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
3883 bmap
= isl_basic_map_read_from_str(ctx
, str
);
3884 str
= "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
3885 bset
= isl_basic_set_read_from_str(ctx
, str
);
3886 map
= isl_basic_map_partial_lexmin(bmap
, bset
, NULL
);
3897 int (*fn
)(isl_ctx
*ctx
);
3899 { "partial lexmin", &test_partial_lexmin
},
3900 { "simplify", &test_simplify
},
3901 { "curry", &test_curry
},
3902 { "piecewise multi affine expressions", &test_pw_multi_aff
},
3903 { "conversion", &test_conversion
},
3904 { "list", &test_list
},
3905 { "align parameters", &test_align_parameters
},
3906 { "preimage", &test_preimage
},
3907 { "pullback", &test_pullback
},
3908 { "AST", &test_ast
},
3909 { "AST generation", &test_ast_gen
},
3910 { "eliminate", &test_eliminate
},
3911 { "residue class", &test_residue_class
},
3912 { "div", &test_div
},
3913 { "slice", &test_slice
},
3914 { "fixed power", &test_fixed_power
},
3915 { "sample", &test_sample
},
3916 { "output", &test_output
},
3917 { "vertices", &test_vertices
},
3918 { "fixed", &test_fixed
},
3919 { "equal", &test_equal
},
3920 { "product", &test_product
},
3921 { "dim_max", &test_dim_max
},
3922 { "affine", &test_aff
},
3923 { "injective", &test_injective
},
3924 { "schedule", &test_schedule
},
3925 { "union_pw", &test_union_pw
},
3926 { "parse", &test_parse
},
3927 { "single-valued", &test_sv
},
3928 { "affine hull", &test_affine_hull
},
3929 { "coalesce", &test_coalesce
},
3930 { "factorize", &test_factorize
},
3931 { "subset", &test_subset
},
3932 { "subtract", &test_subtract
},
3938 struct isl_ctx
*ctx
;
3940 srcdir
= getenv("srcdir");
3943 ctx
= isl_ctx_alloc();
3944 for (i
= 0; i
< ARRAY_SIZE(tests
); ++i
) {
3945 printf("%s\n", tests
[i
].name
);
3946 if (tests
[i
].fn(ctx
) < 0)
3952 test_split_periods(ctx
);
3955 test_bijective(ctx
);
3959 test_construction(ctx
);
3961 test_application(ctx
);
3962 test_convex_hull(ctx
);