2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_aff_private.h>
18 #include <isl/constraint.h>
19 #include <isl/polynomial.h>
20 #include <isl/union_map.h>
21 #include <isl_factorization.h>
22 #include <isl/schedule.h>
23 #include <isl_options_private.h>
24 #include <isl/vertices.h>
25 #include <isl/ast_build.h>
27 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
31 static char *get_filename(isl_ctx
*ctx
, const char *name
, const char *suffix
) {
34 char *pattern
= "%s/test_inputs/%s.%s";
36 length
= strlen(pattern
) - 6 + strlen(srcdir
) + strlen(name
)
38 filename
= isl_alloc_array(ctx
, char, length
);
43 sprintf(filename
, pattern
, srcdir
, name
, suffix
);
48 void test_parse_map(isl_ctx
*ctx
, const char *str
)
52 map
= isl_map_read_from_str(ctx
, str
);
57 int test_parse_map_equal(isl_ctx
*ctx
, const char *str
, const char *str2
)
62 map
= isl_map_read_from_str(ctx
, str
);
63 map2
= isl_map_read_from_str(ctx
, str2
);
64 equal
= isl_map_is_equal(map
, map2
);
71 isl_die(ctx
, isl_error_unknown
, "maps not equal",
77 void test_parse_pwqp(isl_ctx
*ctx
, const char *str
)
79 isl_pw_qpolynomial
*pwqp
;
81 pwqp
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
83 isl_pw_qpolynomial_free(pwqp
);
86 static void test_parse_pwaff(isl_ctx
*ctx
, const char *str
)
90 pwaff
= isl_pw_aff_read_from_str(ctx
, str
);
92 isl_pw_aff_free(pwaff
);
95 int test_parse(struct isl_ctx
*ctx
)
98 const char *str
, *str2
;
100 str
= "{ [i] -> [-i] }";
101 map
= isl_map_read_from_str(ctx
, str
);
105 str
= "{ A[i] -> L[([i/3])] }";
106 map
= isl_map_read_from_str(ctx
, str
);
110 test_parse_map(ctx
, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
111 test_parse_map(ctx
, "{ [p1, y1, y2] -> [2, y1, y2] : "
112 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
114 str
= "{ [x,y] : [([x/2]+y)/3] >= 1 }";
115 str2
= "{ [x, y] : 2y >= 6 - x }";
116 if (test_parse_map_equal(ctx
, str
, str2
) < 0)
119 if (test_parse_map_equal(ctx
, "{ [x,y] : x <= min(y, 2*y+3) }",
120 "{ [x,y] : x <= y, 2*y + 3 }") < 0)
122 str
= "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
123 if (test_parse_map_equal(ctx
, "{ [x,y] : x >= min(y, 2*y+3) }",
127 str
= "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
128 map
= isl_map_read_from_str(ctx
, str
);
129 str
= "{ [new, old] -> [o0, o1] : "
130 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
131 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
132 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
133 map2
= isl_map_read_from_str(ctx
, str
);
134 assert(isl_map_is_equal(map
, map2
));
138 str
= "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
139 map
= isl_map_read_from_str(ctx
, str
);
140 str
= "{[new,old] -> [(new+1)%2,(old+1)%2]}";
141 map2
= isl_map_read_from_str(ctx
, str
);
142 assert(isl_map_is_equal(map
, map2
));
146 str
= "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
147 str2
= "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
148 if (test_parse_map_equal(ctx
, str
, str2
) < 0)
151 str
= "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
152 str2
= "{ [i,j] -> [min(i,j)] }";
153 if (test_parse_map_equal(ctx
, str
, str2
) < 0)
156 str
= "{ [i,j] : i != j }";
157 str2
= "{ [i,j] : i < j or i > j }";
158 if (test_parse_map_equal(ctx
, str
, str2
) < 0)
161 str
= "{ [i,j] : (i+1)*2 >= j }";
162 str2
= "{ [i, j] : j <= 2 + 2i }";
163 if (test_parse_map_equal(ctx
, str
, str2
) < 0)
166 str
= "{ [i] -> [i > 0 ? 4 : 5] }";
167 str2
= "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
168 if (test_parse_map_equal(ctx
, str
, str2
) < 0)
171 str
= "[N=2,M] -> { [i=[(M+N)/4]] }";
172 str2
= "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
173 if (test_parse_map_equal(ctx
, str
, str2
) < 0)
176 str
= "{ [x] : x >= 0 }";
177 str2
= "{ [x] : x-0 >= 0 }";
178 if (test_parse_map_equal(ctx
, str
, str2
) < 0)
181 str
= "{ [i] : ((i > 10)) }";
182 str2
= "{ [i] : i >= 11 }";
183 if (test_parse_map_equal(ctx
, str
, str2
) < 0)
186 str
= "{ [i] -> [0] }";
187 str2
= "{ [i] -> [0 * i] }";
188 if (test_parse_map_equal(ctx
, str
, str2
) < 0)
191 test_parse_pwqp(ctx
, "{ [i] -> i + [ (i + [i/3])/2 ] }");
192 test_parse_map(ctx
, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
193 test_parse_pwaff(ctx
, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
194 test_parse_pwqp(ctx
, "{ [x] -> ([(x)/2] * [(x)/3]) }");
196 if (test_parse_map_equal(ctx
, "{ [a] -> [b] : (not false) }",
197 "{ [a] -> [b] : true }") < 0)
200 if (test_parse_map_equal(ctx
, "{ [i] : i/2 <= 5 }",
201 "{ [i] : i <= 10 }") < 0)
204 if (test_parse_map_equal(ctx
, "{Sym=[n] [i] : i <= n }",
205 "[n] -> { [i] : i <= n }") < 0)
208 if (test_parse_map_equal(ctx
, "{ [*] }", "{ [a] }") < 0)
214 void test_read(struct isl_ctx
*ctx
)
218 struct isl_basic_set
*bset1
, *bset2
;
219 const char *str
= "{[y]: Exists ( alpha : 2alpha = y)}";
221 filename
= get_filename(ctx
, "set", "omega");
223 input
= fopen(filename
, "r");
226 bset1
= isl_basic_set_read_from_file(ctx
, input
);
227 bset2
= isl_basic_set_read_from_str(ctx
, str
);
229 assert(isl_basic_set_is_equal(bset1
, bset2
) == 1);
231 isl_basic_set_free(bset1
);
232 isl_basic_set_free(bset2
);
238 void test_bounded(struct isl_ctx
*ctx
)
243 set
= isl_set_read_from_str(ctx
, "[n] -> {[i] : 0 <= i <= n }");
244 bounded
= isl_set_is_bounded(set
);
248 set
= isl_set_read_from_str(ctx
, "{[n, i] : 0 <= i <= n }");
249 bounded
= isl_set_is_bounded(set
);
253 set
= isl_set_read_from_str(ctx
, "[n] -> {[i] : i <= n }");
254 bounded
= isl_set_is_bounded(set
);
259 /* Construct the basic set { [i] : 5 <= i <= N } */
260 void test_construction(struct isl_ctx
*ctx
)
265 struct isl_basic_set
*bset
;
266 struct isl_constraint
*c
;
270 dim
= isl_space_set_alloc(ctx
, 1, 1);
271 bset
= isl_basic_set_universe(isl_space_copy(dim
));
272 ls
= isl_local_space_from_space(dim
);
274 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
275 isl_int_set_si(v
, -1);
276 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
277 isl_int_set_si(v
, 1);
278 isl_constraint_set_coefficient(c
, isl_dim_param
, 0, v
);
279 bset
= isl_basic_set_add_constraint(bset
, c
);
281 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
282 isl_int_set_si(v
, 1);
283 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
284 isl_int_set_si(v
, -5);
285 isl_constraint_set_constant(c
, v
);
286 bset
= isl_basic_set_add_constraint(bset
, c
);
288 isl_local_space_free(ls
);
289 isl_basic_set_free(bset
);
294 void test_dim(struct isl_ctx
*ctx
)
297 isl_map
*map1
, *map2
;
299 map1
= isl_map_read_from_str(ctx
,
300 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
301 map1
= isl_map_add_dims(map1
, isl_dim_in
, 1);
302 map2
= isl_map_read_from_str(ctx
,
303 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
304 assert(isl_map_is_equal(map1
, map2
));
307 map1
= isl_map_project_out(map1
, isl_dim_in
, 0, 1);
308 map2
= isl_map_read_from_str(ctx
, "[n] -> { [i] -> [j] : n >= 0 }");
309 assert(isl_map_is_equal(map1
, map2
));
314 str
= "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
315 map1
= isl_map_read_from_str(ctx
, str
);
316 str
= "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
317 map2
= isl_map_read_from_str(ctx
, str
);
318 map1
= isl_map_move_dims(map1
, isl_dim_out
, 0, isl_dim_param
, 0, 1);
319 assert(isl_map_is_equal(map1
, map2
));
325 static int test_div(isl_ctx
*ctx
)
334 struct isl_basic_set
*bset
;
335 struct isl_constraint
*c
;
340 dim
= isl_space_set_alloc(ctx
, 0, 3);
341 bset
= isl_basic_set_universe(isl_space_copy(dim
));
342 ls
= isl_local_space_from_space(dim
);
344 c
= isl_equality_alloc(isl_local_space_copy(ls
));
345 isl_int_set_si(v
, -1);
346 isl_constraint_set_constant(c
, v
);
347 isl_int_set_si(v
, 1);
348 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
349 isl_int_set_si(v
, 3);
350 isl_constraint_set_coefficient(c
, isl_dim_set
, 1, v
);
351 bset
= isl_basic_set_add_constraint(bset
, c
);
353 c
= isl_equality_alloc(isl_local_space_copy(ls
));
354 isl_int_set_si(v
, 1);
355 isl_constraint_set_constant(c
, v
);
356 isl_int_set_si(v
, -1);
357 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
358 isl_int_set_si(v
, 3);
359 isl_constraint_set_coefficient(c
, isl_dim_set
, 2, v
);
360 bset
= isl_basic_set_add_constraint(bset
, c
);
362 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 1, 2);
364 assert(bset
&& bset
->n_div
== 1);
365 isl_local_space_free(ls
);
366 isl_basic_set_free(bset
);
369 dim
= isl_space_set_alloc(ctx
, 0, 3);
370 bset
= isl_basic_set_universe(isl_space_copy(dim
));
371 ls
= isl_local_space_from_space(dim
);
373 c
= isl_equality_alloc(isl_local_space_copy(ls
));
374 isl_int_set_si(v
, 1);
375 isl_constraint_set_constant(c
, v
);
376 isl_int_set_si(v
, -1);
377 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
378 isl_int_set_si(v
, 3);
379 isl_constraint_set_coefficient(c
, isl_dim_set
, 1, v
);
380 bset
= isl_basic_set_add_constraint(bset
, c
);
382 c
= isl_equality_alloc(isl_local_space_copy(ls
));
383 isl_int_set_si(v
, -1);
384 isl_constraint_set_constant(c
, v
);
385 isl_int_set_si(v
, 1);
386 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
387 isl_int_set_si(v
, 3);
388 isl_constraint_set_coefficient(c
, isl_dim_set
, 2, v
);
389 bset
= isl_basic_set_add_constraint(bset
, c
);
391 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 1, 2);
393 assert(bset
&& bset
->n_div
== 1);
394 isl_local_space_free(ls
);
395 isl_basic_set_free(bset
);
398 dim
= isl_space_set_alloc(ctx
, 0, 3);
399 bset
= isl_basic_set_universe(isl_space_copy(dim
));
400 ls
= isl_local_space_from_space(dim
);
402 c
= isl_equality_alloc(isl_local_space_copy(ls
));
403 isl_int_set_si(v
, 1);
404 isl_constraint_set_constant(c
, v
);
405 isl_int_set_si(v
, -1);
406 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
407 isl_int_set_si(v
, 3);
408 isl_constraint_set_coefficient(c
, isl_dim_set
, 1, v
);
409 bset
= isl_basic_set_add_constraint(bset
, c
);
411 c
= isl_equality_alloc(isl_local_space_copy(ls
));
412 isl_int_set_si(v
, -3);
413 isl_constraint_set_constant(c
, v
);
414 isl_int_set_si(v
, 1);
415 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
416 isl_int_set_si(v
, 4);
417 isl_constraint_set_coefficient(c
, isl_dim_set
, 2, v
);
418 bset
= isl_basic_set_add_constraint(bset
, c
);
420 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 1, 2);
422 assert(bset
&& bset
->n_div
== 1);
423 isl_local_space_free(ls
);
424 isl_basic_set_free(bset
);
427 dim
= isl_space_set_alloc(ctx
, 0, 3);
428 bset
= isl_basic_set_universe(isl_space_copy(dim
));
429 ls
= isl_local_space_from_space(dim
);
431 c
= isl_equality_alloc(isl_local_space_copy(ls
));
432 isl_int_set_si(v
, 2);
433 isl_constraint_set_constant(c
, v
);
434 isl_int_set_si(v
, -1);
435 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
436 isl_int_set_si(v
, 3);
437 isl_constraint_set_coefficient(c
, isl_dim_set
, 1, v
);
438 bset
= isl_basic_set_add_constraint(bset
, c
);
440 c
= isl_equality_alloc(isl_local_space_copy(ls
));
441 isl_int_set_si(v
, -1);
442 isl_constraint_set_constant(c
, v
);
443 isl_int_set_si(v
, 1);
444 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
445 isl_int_set_si(v
, 6);
446 isl_constraint_set_coefficient(c
, isl_dim_set
, 2, v
);
447 bset
= isl_basic_set_add_constraint(bset
, c
);
449 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 1, 2);
451 assert(isl_basic_set_is_empty(bset
));
452 isl_local_space_free(ls
);
453 isl_basic_set_free(bset
);
456 dim
= isl_space_set_alloc(ctx
, 0, 3);
457 bset
= isl_basic_set_universe(isl_space_copy(dim
));
458 ls
= isl_local_space_from_space(dim
);
460 c
= isl_equality_alloc(isl_local_space_copy(ls
));
461 isl_int_set_si(v
, -1);
462 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
463 isl_int_set_si(v
, 3);
464 isl_constraint_set_coefficient(c
, isl_dim_set
, 2, v
);
465 bset
= isl_basic_set_add_constraint(bset
, c
);
467 c
= isl_equality_alloc(isl_local_space_copy(ls
));
468 isl_int_set_si(v
, 1);
469 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
470 isl_int_set_si(v
, -3);
471 isl_constraint_set_coefficient(c
, isl_dim_set
, 1, v
);
472 bset
= isl_basic_set_add_constraint(bset
, c
);
474 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 2, 1);
476 assert(bset
&& bset
->n_div
== 0);
477 isl_basic_set_free(bset
);
478 isl_local_space_free(ls
);
481 dim
= isl_space_set_alloc(ctx
, 0, 3);
482 bset
= isl_basic_set_universe(isl_space_copy(dim
));
483 ls
= isl_local_space_from_space(dim
);
485 c
= isl_equality_alloc(isl_local_space_copy(ls
));
486 isl_int_set_si(v
, -1);
487 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
488 isl_int_set_si(v
, 6);
489 isl_constraint_set_coefficient(c
, isl_dim_set
, 2, v
);
490 bset
= isl_basic_set_add_constraint(bset
, c
);
492 c
= isl_equality_alloc(isl_local_space_copy(ls
));
493 isl_int_set_si(v
, 1);
494 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
495 isl_int_set_si(v
, -3);
496 isl_constraint_set_coefficient(c
, isl_dim_set
, 1, v
);
497 bset
= isl_basic_set_add_constraint(bset
, c
);
499 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 2, 1);
501 assert(bset
&& bset
->n_div
== 1);
502 isl_basic_set_free(bset
);
503 isl_local_space_free(ls
);
506 /* This test is a bit tricky. We set up an equality
508 * Normalization of divs creates _two_ divs
511 * Afterwards e0 is removed again because it has coefficient -1
512 * and we end up with the original equality and div again.
513 * Perhaps we can avoid the introduction of this temporary div.
515 dim
= isl_space_set_alloc(ctx
, 0, 4);
516 bset
= isl_basic_set_universe(isl_space_copy(dim
));
517 ls
= isl_local_space_from_space(dim
);
519 c
= isl_equality_alloc(isl_local_space_copy(ls
));
520 isl_int_set_si(v
, -1);
521 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
522 isl_int_set_si(v
, -3);
523 isl_constraint_set_coefficient(c
, isl_dim_set
, 1, v
);
524 isl_int_set_si(v
, -3);
525 isl_constraint_set_coefficient(c
, isl_dim_set
, 2, v
);
526 isl_int_set_si(v
, 6);
527 isl_constraint_set_coefficient(c
, isl_dim_set
, 3, v
);
528 bset
= isl_basic_set_add_constraint(bset
, c
);
530 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 3, 1);
532 /* Test disabled for now */
534 assert(bset && bset->n_div == 1);
536 isl_local_space_free(ls
);
537 isl_basic_set_free(bset
);
540 dim
= isl_space_set_alloc(ctx
, 0, 5);
541 bset
= isl_basic_set_universe(isl_space_copy(dim
));
542 ls
= isl_local_space_from_space(dim
);
544 c
= isl_equality_alloc(isl_local_space_copy(ls
));
545 isl_int_set_si(v
, -1);
546 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
547 isl_int_set_si(v
, -3);
548 isl_constraint_set_coefficient(c
, isl_dim_set
, 1, v
);
549 isl_int_set_si(v
, -3);
550 isl_constraint_set_coefficient(c
, isl_dim_set
, 3, v
);
551 isl_int_set_si(v
, 6);
552 isl_constraint_set_coefficient(c
, isl_dim_set
, 4, v
);
553 bset
= isl_basic_set_add_constraint(bset
, c
);
555 c
= isl_equality_alloc(isl_local_space_copy(ls
));
556 isl_int_set_si(v
, -1);
557 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
558 isl_int_set_si(v
, 1);
559 isl_constraint_set_coefficient(c
, isl_dim_set
, 2, v
);
560 isl_int_set_si(v
, 1);
561 isl_constraint_set_constant(c
, v
);
562 bset
= isl_basic_set_add_constraint(bset
, c
);
564 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 4, 1);
566 /* Test disabled for now */
568 assert(bset && bset->n_div == 1);
570 isl_local_space_free(ls
);
571 isl_basic_set_free(bset
);
574 dim
= isl_space_set_alloc(ctx
, 0, 4);
575 bset
= isl_basic_set_universe(isl_space_copy(dim
));
576 ls
= isl_local_space_from_space(dim
);
578 c
= isl_equality_alloc(isl_local_space_copy(ls
));
579 isl_int_set_si(v
, 1);
580 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
581 isl_int_set_si(v
, -1);
582 isl_constraint_set_coefficient(c
, isl_dim_set
, 1, v
);
583 isl_int_set_si(v
, -2);
584 isl_constraint_set_coefficient(c
, isl_dim_set
, 2, v
);
585 bset
= isl_basic_set_add_constraint(bset
, c
);
587 c
= isl_equality_alloc(isl_local_space_copy(ls
));
588 isl_int_set_si(v
, -1);
589 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
590 isl_int_set_si(v
, 3);
591 isl_constraint_set_coefficient(c
, isl_dim_set
, 3, v
);
592 isl_int_set_si(v
, 2);
593 isl_constraint_set_constant(c
, v
);
594 bset
= isl_basic_set_add_constraint(bset
, c
);
596 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 2, 2);
598 bset
= isl_basic_set_fix_si(bset
, isl_dim_set
, 0, 2);
600 assert(!isl_basic_set_is_empty(bset
));
602 isl_local_space_free(ls
);
603 isl_basic_set_free(bset
);
606 dim
= isl_space_set_alloc(ctx
, 0, 3);
607 bset
= isl_basic_set_universe(isl_space_copy(dim
));
608 ls
= isl_local_space_from_space(dim
);
610 c
= isl_equality_alloc(isl_local_space_copy(ls
));
611 isl_int_set_si(v
, 1);
612 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, v
);
613 isl_int_set_si(v
, -2);
614 isl_constraint_set_coefficient(c
, isl_dim_set
, 2, v
);
615 bset
= isl_basic_set_add_constraint(bset
, c
);
617 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 2, 1);
619 bset
= isl_basic_set_fix_si(bset
, isl_dim_set
, 0, 2);
621 isl_local_space_free(ls
);
622 isl_basic_set_free(bset
);
626 str
= "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
627 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
628 set
= isl_set_read_from_str(ctx
, str
);
629 set
= isl_set_compute_divs(set
);
634 str
= "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
635 bset
= isl_basic_set_read_from_str(ctx
, str
);
636 n
= isl_basic_set_dim(bset
, isl_dim_div
);
637 isl_basic_set_free(bset
);
641 isl_die(ctx
, isl_error_unknown
,
642 "expecting no existentials", return -1);
644 str
= "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
645 set
= isl_set_read_from_str(ctx
, str
);
646 set
= isl_set_remove_divs_involving_dims(set
, isl_dim_set
, 0, 2);
647 set
= isl_set_fix_si(set
, isl_dim_set
, 2, -3);
648 empty
= isl_set_is_empty(set
);
653 isl_die(ctx
, isl_error_unknown
,
654 "result not as accurate as expected", return -1);
659 void test_application_case(struct isl_ctx
*ctx
, const char *name
)
663 struct isl_basic_set
*bset1
, *bset2
;
664 struct isl_basic_map
*bmap
;
666 filename
= get_filename(ctx
, name
, "omega");
668 input
= fopen(filename
, "r");
671 bset1
= isl_basic_set_read_from_file(ctx
, input
);
672 bmap
= isl_basic_map_read_from_file(ctx
, input
);
674 bset1
= isl_basic_set_apply(bset1
, bmap
);
676 bset2
= isl_basic_set_read_from_file(ctx
, input
);
678 assert(isl_basic_set_is_equal(bset1
, bset2
) == 1);
680 isl_basic_set_free(bset1
);
681 isl_basic_set_free(bset2
);
687 void test_application(struct isl_ctx
*ctx
)
689 test_application_case(ctx
, "application");
690 test_application_case(ctx
, "application2");
693 void test_affine_hull_case(struct isl_ctx
*ctx
, const char *name
)
697 struct isl_basic_set
*bset1
, *bset2
;
699 filename
= get_filename(ctx
, name
, "polylib");
701 input
= fopen(filename
, "r");
704 bset1
= isl_basic_set_read_from_file(ctx
, input
);
705 bset2
= isl_basic_set_read_from_file(ctx
, input
);
707 bset1
= isl_basic_set_affine_hull(bset1
);
709 assert(isl_basic_set_is_equal(bset1
, bset2
) == 1);
711 isl_basic_set_free(bset1
);
712 isl_basic_set_free(bset2
);
718 int test_affine_hull(struct isl_ctx
*ctx
)
725 test_affine_hull_case(ctx
, "affine2");
726 test_affine_hull_case(ctx
, "affine");
727 test_affine_hull_case(ctx
, "affine3");
729 str
= "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
730 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
731 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
732 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
733 set
= isl_set_read_from_str(ctx
, str
);
734 bset
= isl_set_affine_hull(set
);
735 n
= isl_basic_set_dim(bset
, isl_dim_div
);
736 isl_basic_set_free(bset
);
738 isl_die(ctx
, isl_error_unknown
, "not expecting any divs",
744 void test_convex_hull_case(struct isl_ctx
*ctx
, const char *name
)
748 struct isl_basic_set
*bset1
, *bset2
;
751 filename
= get_filename(ctx
, name
, "polylib");
753 input
= fopen(filename
, "r");
756 bset1
= isl_basic_set_read_from_file(ctx
, input
);
757 bset2
= isl_basic_set_read_from_file(ctx
, input
);
759 set
= isl_basic_set_union(bset1
, bset2
);
760 bset1
= isl_set_convex_hull(set
);
762 bset2
= isl_basic_set_read_from_file(ctx
, input
);
764 assert(isl_basic_set_is_equal(bset1
, bset2
) == 1);
766 isl_basic_set_free(bset1
);
767 isl_basic_set_free(bset2
);
773 void test_convex_hull_algo(struct isl_ctx
*ctx
, int convex
)
775 const char *str1
, *str2
;
776 isl_set
*set1
, *set2
;
777 int orig_convex
= ctx
->opt
->convex
;
778 ctx
->opt
->convex
= convex
;
780 test_convex_hull_case(ctx
, "convex0");
781 test_convex_hull_case(ctx
, "convex1");
782 test_convex_hull_case(ctx
, "convex2");
783 test_convex_hull_case(ctx
, "convex3");
784 test_convex_hull_case(ctx
, "convex4");
785 test_convex_hull_case(ctx
, "convex5");
786 test_convex_hull_case(ctx
, "convex6");
787 test_convex_hull_case(ctx
, "convex7");
788 test_convex_hull_case(ctx
, "convex8");
789 test_convex_hull_case(ctx
, "convex9");
790 test_convex_hull_case(ctx
, "convex10");
791 test_convex_hull_case(ctx
, "convex11");
792 test_convex_hull_case(ctx
, "convex12");
793 test_convex_hull_case(ctx
, "convex13");
794 test_convex_hull_case(ctx
, "convex14");
795 test_convex_hull_case(ctx
, "convex15");
797 str1
= "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
798 "(i0 = 1 and i1 = 0 and i2 = 1) or "
799 "(i0 = 0 and i1 = 0 and i2 = 0) }";
800 str2
= "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
801 set1
= isl_set_read_from_str(ctx
, str1
);
802 set2
= isl_set_read_from_str(ctx
, str2
);
803 set1
= isl_set_from_basic_set(isl_set_convex_hull(set1
));
804 assert(isl_set_is_equal(set1
, set2
));
808 ctx
->opt
->convex
= orig_convex
;
811 void test_convex_hull(struct isl_ctx
*ctx
)
813 test_convex_hull_algo(ctx
, ISL_CONVEX_HULL_FM
);
814 test_convex_hull_algo(ctx
, ISL_CONVEX_HULL_WRAP
);
817 void test_gist_case(struct isl_ctx
*ctx
, const char *name
)
821 struct isl_basic_set
*bset1
, *bset2
;
823 filename
= get_filename(ctx
, name
, "polylib");
825 input
= fopen(filename
, "r");
828 bset1
= isl_basic_set_read_from_file(ctx
, input
);
829 bset2
= isl_basic_set_read_from_file(ctx
, input
);
831 bset1
= isl_basic_set_gist(bset1
, bset2
);
833 bset2
= isl_basic_set_read_from_file(ctx
, input
);
835 assert(isl_basic_set_is_equal(bset1
, bset2
) == 1);
837 isl_basic_set_free(bset1
);
838 isl_basic_set_free(bset2
);
844 void test_gist(struct isl_ctx
*ctx
)
847 isl_basic_set
*bset1
, *bset2
;
849 test_gist_case(ctx
, "gist1");
851 str
= "[p0, p2, p3, p5, p6, p10] -> { [] : "
852 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
853 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
854 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
855 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
856 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
857 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
858 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
859 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
860 bset1
= isl_basic_set_read_from_str(ctx
, str
);
861 str
= "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
862 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
863 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
864 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
865 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
866 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
867 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
868 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
869 bset2
= isl_basic_set_read_from_str(ctx
, str
);
870 bset1
= isl_basic_set_gist(bset1
, bset2
);
871 assert(bset1
&& bset1
->n_div
== 0);
872 isl_basic_set_free(bset1
);
875 int test_coalesce_set(isl_ctx
*ctx
, const char *str
, int check_one
)
881 set
= isl_set_read_from_str(ctx
, str
);
882 set
= isl_set_coalesce(set
);
883 set2
= isl_set_read_from_str(ctx
, str
);
884 equal
= isl_set_is_equal(set
, set2
);
885 one
= set
&& set
->n
== 1;
892 isl_die(ctx
, isl_error_unknown
,
893 "coalesced set not equal to input", return -1);
894 if (check_one
&& !one
)
895 isl_die(ctx
, isl_error_unknown
,
896 "coalesced set should not be a union", return -1);
901 int test_coalesce_unbounded_wrapping(isl_ctx
*ctx
)
906 bounded
= isl_options_get_coalesce_bounded_wrapping(ctx
);
907 isl_options_set_coalesce_bounded_wrapping(ctx
, 0);
909 if (test_coalesce_set(ctx
,
910 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
911 "-x - y + 1 >= 0 and -3 <= z <= 3;"
912 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
913 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
914 "-10 <= y <= 0}", 1) < 0)
916 if (test_coalesce_set(ctx
,
917 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
919 if (test_coalesce_set(ctx
,
920 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
928 isl_options_set_coalesce_bounded_wrapping(ctx
, bounded
);
933 int test_coalesce(struct isl_ctx
*ctx
)
936 struct isl_set
*set
, *set2
;
937 struct isl_map
*map
, *map2
;
939 set
= isl_set_read_from_str(ctx
,
940 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
941 "y >= x & x >= 2 & 5 >= y }");
942 set
= isl_set_coalesce(set
);
943 assert(set
&& set
->n
== 1);
946 set
= isl_set_read_from_str(ctx
,
947 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
948 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}");
949 set
= isl_set_coalesce(set
);
950 assert(set
&& set
->n
== 1);
953 set
= isl_set_read_from_str(ctx
,
954 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
955 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}");
956 set
= isl_set_coalesce(set
);
957 assert(set
&& set
->n
== 2);
960 set
= isl_set_read_from_str(ctx
,
961 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
962 "y >= 0 & x >= 6 & x <= 10 & y <= x}");
963 set
= isl_set_coalesce(set
);
964 assert(set
&& set
->n
== 1);
967 set
= isl_set_read_from_str(ctx
,
968 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
969 "y >= 0 & x >= 7 & x <= 10 & y <= x}");
970 set
= isl_set_coalesce(set
);
971 assert(set
&& set
->n
== 2);
974 set
= isl_set_read_from_str(ctx
,
975 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
976 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}");
977 set
= isl_set_coalesce(set
);
978 assert(set
&& set
->n
== 2);
981 set
= isl_set_read_from_str(ctx
,
982 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
983 "y >= 0 & x = 6 & y <= 6}");
984 set
= isl_set_coalesce(set
);
985 assert(set
&& set
->n
== 1);
988 set
= isl_set_read_from_str(ctx
,
989 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
990 "y >= 0 & x = 7 & y <= 6}");
991 set
= isl_set_coalesce(set
);
992 assert(set
&& set
->n
== 2);
995 set
= isl_set_read_from_str(ctx
,
996 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
997 "y >= 0 & x = 6 & y <= 5}");
998 set
= isl_set_coalesce(set
);
999 assert(set
&& set
->n
== 1);
1000 set2
= isl_set_read_from_str(ctx
,
1001 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1002 "y >= 0 & x = 6 & y <= 5}");
1003 assert(isl_set_is_equal(set
, set2
));
1007 set
= isl_set_read_from_str(ctx
,
1008 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1009 "y >= 0 & x = 6 & y <= 7}");
1010 set
= isl_set_coalesce(set
);
1011 assert(set
&& set
->n
== 2);
1014 set
= isl_set_read_from_str(ctx
,
1015 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
1016 set
= isl_set_coalesce(set
);
1017 assert(set
&& set
->n
== 1);
1018 set2
= isl_set_read_from_str(ctx
,
1019 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }");
1020 assert(isl_set_is_equal(set
, set2
));
1024 set
= isl_set_read_from_str(ctx
,
1025 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
1026 set
= isl_set_coalesce(set
);
1027 set2
= isl_set_read_from_str(ctx
,
1028 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}");
1029 assert(isl_set_is_equal(set
, set2
));
1033 set
= isl_set_read_from_str(ctx
,
1034 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
1035 "2 <= i and i <= n }");
1036 set
= isl_set_coalesce(set
);
1037 assert(set
&& set
->n
== 1);
1038 set2
= isl_set_read_from_str(ctx
,
1039 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
1040 "2 <= i and i <= n }");
1041 assert(isl_set_is_equal(set
, set2
));
1045 map
= isl_map_read_from_str(ctx
,
1046 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1047 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1048 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1049 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1050 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1051 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1052 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1053 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1054 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1055 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1056 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1057 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1058 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1059 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1060 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1061 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1062 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1063 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
1064 map
= isl_map_coalesce(map
);
1065 map2
= isl_map_read_from_str(ctx
,
1066 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1067 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1068 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1069 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1070 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1071 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1072 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1073 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1074 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1075 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1076 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1077 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1078 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1079 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1080 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1081 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1082 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1083 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }");
1084 assert(isl_map_is_equal(map
, map2
));
1088 str
= "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1089 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1090 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1091 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
1092 map
= isl_map_read_from_str(ctx
, str
);
1093 map
= isl_map_coalesce(map
);
1094 map2
= isl_map_read_from_str(ctx
, str
);
1095 assert(isl_map_is_equal(map
, map2
));
1099 str
= "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
1100 "[o0, o1, o2, o3, o4, o5, o6] : "
1101 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1102 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1103 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1104 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1105 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1106 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1107 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1108 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1109 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1110 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1111 "o6 >= i3 + i6 - o3 and M >= 0 and "
1112 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1113 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
1114 map
= isl_map_read_from_str(ctx
, str
);
1115 map
= isl_map_coalesce(map
);
1116 map2
= isl_map_read_from_str(ctx
, str
);
1117 assert(isl_map_is_equal(map
, map2
));
1121 str
= "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1122 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1123 "(o0 = 0 and M >= 2 and N >= 3) or "
1124 "(M = 0 and o0 = 0 and N >= 3) }";
1125 map
= isl_map_read_from_str(ctx
, str
);
1126 map
= isl_map_coalesce(map
);
1127 map2
= isl_map_read_from_str(ctx
, str
);
1128 assert(isl_map_is_equal(map
, map2
));
1132 str
= "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1133 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1134 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1135 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
1136 map
= isl_map_read_from_str(ctx
, str
);
1137 map
= isl_map_coalesce(map
);
1138 map2
= isl_map_read_from_str(ctx
, str
);
1139 assert(isl_map_is_equal(map
, map2
));
1143 test_coalesce_set(ctx
,
1144 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
1145 "(i1 = M and M >= 1) }", 0);
1146 test_coalesce_set(ctx
,
1147 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
1148 test_coalesce_set(ctx
,
1149 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1150 "(y = 3 and x = 1) }", 1);
1151 test_coalesce_set(ctx
,
1152 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1153 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1154 "i1 <= M and i3 <= M and i4 <= M) or "
1155 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1156 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1157 "i4 <= -1 + M) }", 1);
1158 test_coalesce_set(ctx
,
1159 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1160 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
1161 if (test_coalesce_unbounded_wrapping(ctx
) < 0)
1163 if (test_coalesce_set(ctx
, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0) < 0)
1165 if (test_coalesce_set(ctx
, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1) < 0)
1167 if (test_coalesce_set(ctx
, "{ [0,0]; [i,i] : 1 <= i <= 10 }", 1) < 0)
1169 if (test_coalesce_set(ctx
, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }", 0) < 0)
1171 if (test_coalesce_set(ctx
, "{ [0,0]; [i,2i] : 1 <= i <= 10 }", 1) < 0)
1173 if (test_coalesce_set(ctx
, "{ [0,0]; [i,2i] : 2 <= i <= 10 }", 0) < 0)
1175 if (test_coalesce_set(ctx
, "{ [1,0]; [i,2i] : 1 <= i <= 10 }", 0) < 0)
1177 if (test_coalesce_set(ctx
, "{ [0,1]; [i,2i] : 1 <= i <= 10 }", 0) < 0)
1179 if (test_coalesce_set(ctx
, "{ [a, b] : exists e : 2e = a and "
1180 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }", 0) < 0)
1182 if (test_coalesce_set(ctx
,
1183 "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1184 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1185 "j >= 1 and j' <= i + j - i' and i >= 1; "
1186 "[1, 1, 1, 1] }", 0) < 0)
1188 if (test_coalesce_set(ctx
, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1189 "[i,j] : exists a : j = 3a }", 1) < 0)
1194 void test_closure(struct isl_ctx
*ctx
)
1198 isl_map
*up
, *right
;
1199 isl_map
*map
, *map2
;
1202 /* COCOA example 1 */
1203 map
= isl_map_read_from_str(ctx
,
1204 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1205 "1 <= i and i < n and 1 <= j and j < n or "
1206 "i2 = i + 1 and j2 = j - 1 and "
1207 "1 <= i and i < n and 2 <= j and j <= n }");
1208 map
= isl_map_power(map
, &exact
);
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_transitive_closure(map
, &exact
);
1220 map2
= isl_map_read_from_str(ctx
,
1221 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1222 "1 <= i and i < n and 1 <= j and j <= n and "
1223 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1224 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1225 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1226 assert(isl_map_is_equal(map
, map2
));
1230 map
= isl_map_read_from_str(ctx
,
1231 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1232 " 0 <= y and y <= n }");
1233 map
= isl_map_transitive_closure(map
, &exact
);
1234 map2
= isl_map_read_from_str(ctx
,
1235 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1236 " 0 <= y and y <= n }");
1237 assert(isl_map_is_equal(map
, map2
));
1241 /* COCOA example 2 */
1242 map
= isl_map_read_from_str(ctx
,
1243 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1244 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1245 "i2 = i + 2 and j2 = j - 2 and "
1246 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1247 map
= isl_map_transitive_closure(map
, &exact
);
1249 map2
= isl_map_read_from_str(ctx
,
1250 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1251 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1252 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1253 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1254 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1255 assert(isl_map_is_equal(map
, map2
));
1259 /* COCOA Fig.2 left */
1260 map
= isl_map_read_from_str(ctx
,
1261 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1262 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1264 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1265 "j <= 2 i - 3 and j <= n - 2 or "
1266 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1267 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1268 map
= isl_map_transitive_closure(map
, &exact
);
1272 /* COCOA Fig.2 right */
1273 map
= isl_map_read_from_str(ctx
,
1274 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1275 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1277 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1278 "j <= 2 i - 4 and j <= n - 3 or "
1279 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1280 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1281 map
= isl_map_power(map
, &exact
);
1285 /* COCOA Fig.2 right */
1286 map
= isl_map_read_from_str(ctx
,
1287 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1288 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1290 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1291 "j <= 2 i - 4 and j <= n - 3 or "
1292 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1293 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1294 map
= isl_map_transitive_closure(map
, &exact
);
1296 map2
= isl_map_read_from_str(ctx
,
1297 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1298 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1299 "j <= n and 3 + i + 2 j <= 3 n and "
1300 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1301 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1302 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1303 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1304 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1305 assert(isl_map_is_equal(map
, map2
));
1309 /* COCOA Fig.1 right */
1310 dom
= isl_set_read_from_str(ctx
,
1311 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1312 "2 x - 3 y + 3 >= 0 }");
1313 right
= isl_map_read_from_str(ctx
,
1314 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1315 up
= isl_map_read_from_str(ctx
,
1316 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1317 right
= isl_map_intersect_domain(right
, isl_set_copy(dom
));
1318 right
= isl_map_intersect_range(right
, isl_set_copy(dom
));
1319 up
= isl_map_intersect_domain(up
, isl_set_copy(dom
));
1320 up
= isl_map_intersect_range(up
, dom
);
1321 map
= isl_map_union(up
, right
);
1322 map
= isl_map_transitive_closure(map
, &exact
);
1324 map2
= isl_map_read_from_str(ctx
,
1325 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1326 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1327 assert(isl_map_is_equal(map
, map2
));
1331 /* COCOA Theorem 1 counter example */
1332 map
= isl_map_read_from_str(ctx
,
1333 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1334 "i2 = 1 and j2 = j or "
1335 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1336 map
= isl_map_transitive_closure(map
, &exact
);
1340 map
= isl_map_read_from_str(ctx
,
1341 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1342 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1343 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1344 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1345 map
= isl_map_transitive_closure(map
, &exact
);
1349 /* Kelly et al 1996, fig 12 */
1350 map
= isl_map_read_from_str(ctx
,
1351 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1352 "1 <= i,j,j+1 <= n or "
1353 "j = n and j2 = 1 and i2 = i + 1 and "
1354 "1 <= i,i+1 <= n }");
1355 map
= isl_map_transitive_closure(map
, &exact
);
1357 map2
= isl_map_read_from_str(ctx
,
1358 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1359 "1 <= i <= n and i = i2 or "
1360 "1 <= i < i2 <= n and 1 <= j <= n and "
1362 assert(isl_map_is_equal(map
, map2
));
1366 /* Omega's closure4 */
1367 map
= isl_map_read_from_str(ctx
,
1368 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1369 "1 <= x,y <= 10 or "
1370 "x2 = x + 1 and y2 = y and "
1371 "1 <= x <= 20 && 5 <= y <= 15 }");
1372 map
= isl_map_transitive_closure(map
, &exact
);
1376 map
= isl_map_read_from_str(ctx
,
1377 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1378 map
= isl_map_transitive_closure(map
, &exact
);
1380 map2
= isl_map_read_from_str(ctx
,
1381 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1382 assert(isl_map_is_equal(map
, map2
));
1386 str
= "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1387 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1388 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1389 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1390 map
= isl_map_read_from_str(ctx
, str
);
1391 map
= isl_map_transitive_closure(map
, &exact
);
1393 map2
= isl_map_read_from_str(ctx
, str
);
1394 assert(isl_map_is_equal(map
, map2
));
1398 str
= "{[0] -> [1]; [2] -> [3]}";
1399 map
= isl_map_read_from_str(ctx
, str
);
1400 map
= isl_map_transitive_closure(map
, &exact
);
1402 map2
= isl_map_read_from_str(ctx
, str
);
1403 assert(isl_map_is_equal(map
, map2
));
1407 str
= "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1408 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1409 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1410 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1411 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1412 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1413 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1414 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1415 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1416 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1417 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1418 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1419 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 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, 2]] -> "
1423 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1424 "exists (e0 = [(3 - n)/3]: i5 >= 1 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 map
= isl_map_read_from_str(ctx
, str
);
1428 map
= isl_map_transitive_closure(map
, NULL
);
1433 void test_lex(struct isl_ctx
*ctx
)
1438 dim
= isl_space_set_alloc(ctx
, 0, 0);
1439 map
= isl_map_lex_le(dim
);
1440 assert(!isl_map_is_empty(map
));
1444 void test_lexmin(struct isl_ctx
*ctx
)
1447 isl_basic_map
*bmap
;
1448 isl_map
*map
, *map2
;
1451 isl_pw_multi_aff
*pma
;
1453 str
= "[p0, p1] -> { [] -> [] : "
1454 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1455 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1456 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1457 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1458 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1459 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1460 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1461 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1462 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1463 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1464 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1465 map
= isl_map_read_from_str(ctx
, str
);
1466 map
= isl_map_lexmin(map
);
1469 str
= "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1470 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1471 set
= isl_set_read_from_str(ctx
, str
);
1472 set
= isl_set_lexmax(set
);
1473 str
= "[C] -> { [obj,a,b,c] : C = 8 }";
1474 set2
= isl_set_read_from_str(ctx
, str
);
1475 set
= isl_set_intersect(set
, set2
);
1476 assert(!isl_set_is_empty(set
));
1479 str
= "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1480 map
= isl_map_read_from_str(ctx
, str
);
1481 map
= isl_map_lexmin(map
);
1482 str
= "{ [x] -> [5] : 6 <= x <= 8; "
1483 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1484 map2
= isl_map_read_from_str(ctx
, str
);
1485 assert(isl_map_is_equal(map
, map2
));
1489 str
= "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1490 map
= isl_map_read_from_str(ctx
, str
);
1491 map2
= isl_map_copy(map
);
1492 map
= isl_map_lexmin(map
);
1493 assert(isl_map_is_equal(map
, map2
));
1497 str
= "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1498 map
= isl_map_read_from_str(ctx
, str
);
1499 map
= isl_map_lexmin(map
);
1500 str
= "{ [x] -> [y] : (4y = x and x >= 0) or "
1501 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1502 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1503 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1504 map2
= isl_map_read_from_str(ctx
, str
);
1505 assert(isl_map_is_equal(map
, map2
));
1509 str
= "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1510 " 8i' <= i and 8i' >= -7 + i }";
1511 bmap
= isl_basic_map_read_from_str(ctx
, str
);
1512 pma
= isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap
));
1513 map2
= isl_map_from_pw_multi_aff(pma
);
1514 map
= isl_map_from_basic_map(bmap
);
1515 assert(isl_map_is_equal(map
, map2
));
1519 str
= "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1520 map
= isl_map_read_from_str(ctx
, str
);
1521 map
= isl_map_lexmin(map
);
1522 str
= "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1523 map2
= isl_map_read_from_str(ctx
, str
);
1524 assert(isl_map_is_equal(map
, map2
));
1534 static int collect_must_may(__isl_take isl_map
*dep
, int must
,
1535 void *dep_user
, void *user
)
1537 struct must_may
*mm
= (struct must_may
*)user
;
1540 mm
->must
= isl_map_union(mm
->must
, dep
);
1542 mm
->may
= isl_map_union(mm
->may
, dep
);
1547 static int common_space(void *first
, void *second
)
1549 int depth
= *(int *)first
;
1553 static int map_is_equal(__isl_keep isl_map
*map
, const char *str
)
1561 map2
= isl_map_read_from_str(map
->ctx
, str
);
1562 equal
= isl_map_is_equal(map
, map2
);
1568 static int map_check_equal(__isl_keep isl_map
*map
, const char *str
)
1572 equal
= map_is_equal(map
, str
);
1576 isl_die(isl_map_get_ctx(map
), isl_error_unknown
,
1577 "result not as expected", return -1);
1581 void test_dep(struct isl_ctx
*ctx
)
1586 isl_access_info
*ai
;
1593 str
= "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1594 map
= isl_map_read_from_str(ctx
, str
);
1595 ai
= isl_access_info_alloc(map
, &depth
, &common_space
, 2);
1597 str
= "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1598 map
= isl_map_read_from_str(ctx
, str
);
1599 ai
= isl_access_info_add_source(ai
, map
, 1, &depth
);
1601 str
= "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1602 map
= isl_map_read_from_str(ctx
, str
);
1603 ai
= isl_access_info_add_source(ai
, map
, 1, &depth
);
1605 flow
= isl_access_info_compute_flow(ai
);
1606 dim
= isl_space_alloc(ctx
, 0, 3, 3);
1607 mm
.must
= isl_map_empty(isl_space_copy(dim
));
1608 mm
.may
= isl_map_empty(dim
);
1610 isl_flow_foreach(flow
, collect_must_may
, &mm
);
1612 str
= "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1613 " [1,10,0] -> [2,5,0] }";
1614 assert(map_is_equal(mm
.must
, str
));
1615 str
= "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1616 assert(map_is_equal(mm
.may
, str
));
1618 isl_map_free(mm
.must
);
1619 isl_map_free(mm
.may
);
1620 isl_flow_free(flow
);
1623 str
= "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1624 map
= isl_map_read_from_str(ctx
, str
);
1625 ai
= isl_access_info_alloc(map
, &depth
, &common_space
, 2);
1627 str
= "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1628 map
= isl_map_read_from_str(ctx
, str
);
1629 ai
= isl_access_info_add_source(ai
, map
, 1, &depth
);
1631 str
= "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1632 map
= isl_map_read_from_str(ctx
, str
);
1633 ai
= isl_access_info_add_source(ai
, map
, 0, &depth
);
1635 flow
= isl_access_info_compute_flow(ai
);
1636 dim
= isl_space_alloc(ctx
, 0, 3, 3);
1637 mm
.must
= isl_map_empty(isl_space_copy(dim
));
1638 mm
.may
= isl_map_empty(dim
);
1640 isl_flow_foreach(flow
, collect_must_may
, &mm
);
1642 str
= "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1643 assert(map_is_equal(mm
.must
, str
));
1644 str
= "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1645 assert(map_is_equal(mm
.may
, str
));
1647 isl_map_free(mm
.must
);
1648 isl_map_free(mm
.may
);
1649 isl_flow_free(flow
);
1652 str
= "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1653 map
= isl_map_read_from_str(ctx
, str
);
1654 ai
= isl_access_info_alloc(map
, &depth
, &common_space
, 2);
1656 str
= "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1657 map
= isl_map_read_from_str(ctx
, str
);
1658 ai
= isl_access_info_add_source(ai
, map
, 0, &depth
);
1660 str
= "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1661 map
= isl_map_read_from_str(ctx
, str
);
1662 ai
= isl_access_info_add_source(ai
, map
, 0, &depth
);
1664 flow
= isl_access_info_compute_flow(ai
);
1665 dim
= isl_space_alloc(ctx
, 0, 3, 3);
1666 mm
.must
= isl_map_empty(isl_space_copy(dim
));
1667 mm
.may
= isl_map_empty(dim
);
1669 isl_flow_foreach(flow
, collect_must_may
, &mm
);
1671 str
= "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1672 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1673 assert(map_is_equal(mm
.may
, str
));
1674 str
= "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1675 assert(map_is_equal(mm
.must
, str
));
1677 isl_map_free(mm
.must
);
1678 isl_map_free(mm
.may
);
1679 isl_flow_free(flow
);
1682 str
= "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1683 map
= isl_map_read_from_str(ctx
, str
);
1684 ai
= isl_access_info_alloc(map
, &depth
, &common_space
, 2);
1686 str
= "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1687 map
= isl_map_read_from_str(ctx
, str
);
1688 ai
= isl_access_info_add_source(ai
, map
, 0, &depth
);
1690 str
= "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1691 map
= isl_map_read_from_str(ctx
, str
);
1692 ai
= isl_access_info_add_source(ai
, map
, 0, &depth
);
1694 flow
= isl_access_info_compute_flow(ai
);
1695 dim
= isl_space_alloc(ctx
, 0, 3, 3);
1696 mm
.must
= isl_map_empty(isl_space_copy(dim
));
1697 mm
.may
= isl_map_empty(dim
);
1699 isl_flow_foreach(flow
, collect_must_may
, &mm
);
1701 str
= "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1702 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1703 assert(map_is_equal(mm
.may
, str
));
1704 str
= "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1705 assert(map_is_equal(mm
.must
, str
));
1707 isl_map_free(mm
.must
);
1708 isl_map_free(mm
.may
);
1709 isl_flow_free(flow
);
1712 str
= "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1713 map
= isl_map_read_from_str(ctx
, str
);
1714 ai
= isl_access_info_alloc(map
, &depth
, &common_space
, 2);
1716 str
= "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1717 map
= isl_map_read_from_str(ctx
, str
);
1718 ai
= isl_access_info_add_source(ai
, map
, 0, &depth
);
1720 str
= "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1721 map
= isl_map_read_from_str(ctx
, str
);
1722 ai
= isl_access_info_add_source(ai
, map
, 0, &depth
);
1724 flow
= isl_access_info_compute_flow(ai
);
1725 dim
= isl_space_alloc(ctx
, 0, 3, 3);
1726 mm
.must
= isl_map_empty(isl_space_copy(dim
));
1727 mm
.may
= isl_map_empty(dim
);
1729 isl_flow_foreach(flow
, collect_must_may
, &mm
);
1731 str
= "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1732 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1733 assert(map_is_equal(mm
.may
, str
));
1734 str
= "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1735 assert(map_is_equal(mm
.must
, str
));
1737 isl_map_free(mm
.must
);
1738 isl_map_free(mm
.may
);
1739 isl_flow_free(flow
);
1744 str
= "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1745 map
= isl_map_read_from_str(ctx
, str
);
1746 ai
= isl_access_info_alloc(map
, &depth
, &common_space
, 1);
1748 str
= "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1749 map
= isl_map_read_from_str(ctx
, str
);
1750 ai
= isl_access_info_add_source(ai
, map
, 1, &depth
);
1752 flow
= isl_access_info_compute_flow(ai
);
1753 dim
= isl_space_alloc(ctx
, 0, 5, 5);
1754 mm
.must
= isl_map_empty(isl_space_copy(dim
));
1755 mm
.may
= isl_map_empty(dim
);
1757 isl_flow_foreach(flow
, collect_must_may
, &mm
);
1759 str
= "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1760 assert(map_is_equal(mm
.must
, str
));
1761 str
= "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1762 assert(map_is_equal(mm
.may
, str
));
1764 isl_map_free(mm
.must
);
1765 isl_map_free(mm
.may
);
1766 isl_flow_free(flow
);
1769 int test_sv(isl_ctx
*ctx
)
1773 isl_union_map
*umap
;
1776 str
= "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1777 map
= isl_map_read_from_str(ctx
, str
);
1778 sv
= isl_map_is_single_valued(map
);
1783 isl_die(ctx
, isl_error_internal
,
1784 "map not detected as single valued", return -1);
1786 str
= "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
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 detected as single valued", return -1);
1796 str
= "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }";
1797 umap
= isl_union_map_read_from_str(ctx
, str
);
1798 sv
= isl_union_map_is_single_valued(umap
);
1799 isl_union_map_free(umap
);
1803 isl_die(ctx
, isl_error_internal
,
1804 "map not detected as single valued", return -1);
1806 str
= "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[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 detected as single valued", return -1);
1819 void test_bijective_case(struct isl_ctx
*ctx
, const char *str
, int bijective
)
1823 map
= isl_map_read_from_str(ctx
, str
);
1825 assert(isl_map_is_bijective(map
));
1827 assert(!isl_map_is_bijective(map
));
1831 void test_bijective(struct isl_ctx
*ctx
)
1833 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [i]}", 0);
1834 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1835 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1836 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1837 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [j,i]}", 1);
1838 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [i+j]}", 0);
1839 test_bijective_case(ctx
, "[N,M]->{[i,j] -> []}", 0);
1840 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1841 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [2i]}", 0);
1842 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [i,i]}", 0);
1843 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [2i,i]}", 0);
1844 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [2i,j]}", 1);
1845 test_bijective_case(ctx
, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1848 void test_pwqp(struct isl_ctx
*ctx
)
1852 isl_pw_qpolynomial
*pwqp1
, *pwqp2
;
1854 str
= "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1855 pwqp1
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1857 pwqp1
= isl_pw_qpolynomial_move_dims(pwqp1
, isl_dim_param
, 0,
1860 str
= "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1861 pwqp2
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1863 pwqp1
= isl_pw_qpolynomial_sub(pwqp1
, pwqp2
);
1865 assert(isl_pw_qpolynomial_is_zero(pwqp1
));
1867 isl_pw_qpolynomial_free(pwqp1
);
1869 str
= "{ [i] -> i }";
1870 pwqp1
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1871 str
= "{ [k] : exists a : k = 2a }";
1872 set
= isl_set_read_from_str(ctx
, str
);
1873 pwqp1
= isl_pw_qpolynomial_gist(pwqp1
, set
);
1874 str
= "{ [i] -> i }";
1875 pwqp2
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1877 pwqp1
= isl_pw_qpolynomial_sub(pwqp1
, pwqp2
);
1879 assert(isl_pw_qpolynomial_is_zero(pwqp1
));
1881 isl_pw_qpolynomial_free(pwqp1
);
1883 str
= "{ [i] -> i + [ (i + [i/3])/2 ] }";
1884 pwqp1
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1886 set
= isl_set_read_from_str(ctx
, str
);
1887 pwqp1
= isl_pw_qpolynomial_gist(pwqp1
, set
);
1888 str
= "{ [i] -> 16 }";
1889 pwqp2
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1891 pwqp1
= isl_pw_qpolynomial_sub(pwqp1
, pwqp2
);
1893 assert(isl_pw_qpolynomial_is_zero(pwqp1
));
1895 isl_pw_qpolynomial_free(pwqp1
);
1897 str
= "{ [i] -> ([(i)/2]) }";
1898 pwqp1
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1899 str
= "{ [k] : exists a : k = 2a+1 }";
1900 set
= isl_set_read_from_str(ctx
, str
);
1901 pwqp1
= isl_pw_qpolynomial_gist(pwqp1
, set
);
1902 str
= "{ [i] -> -1/2 + 1/2 * i }";
1903 pwqp2
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1905 pwqp1
= isl_pw_qpolynomial_sub(pwqp1
, pwqp2
);
1907 assert(isl_pw_qpolynomial_is_zero(pwqp1
));
1909 isl_pw_qpolynomial_free(pwqp1
);
1911 str
= "{ [i] -> ([([i/2] + [i/2])/5]) }";
1912 pwqp1
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1913 str
= "{ [i] -> ([(2 * [i/2])/5]) }";
1914 pwqp2
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1916 pwqp1
= isl_pw_qpolynomial_sub(pwqp1
, pwqp2
);
1918 assert(isl_pw_qpolynomial_is_zero(pwqp1
));
1920 isl_pw_qpolynomial_free(pwqp1
);
1922 str
= "{ [x] -> ([x/2] + [(x+1)/2]) }";
1923 pwqp1
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1924 str
= "{ [x] -> x }";
1925 pwqp2
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1927 pwqp1
= isl_pw_qpolynomial_sub(pwqp1
, pwqp2
);
1929 assert(isl_pw_qpolynomial_is_zero(pwqp1
));
1931 isl_pw_qpolynomial_free(pwqp1
);
1933 str
= "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
1934 pwqp1
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1935 pwqp2
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1936 pwqp1
= isl_pw_qpolynomial_coalesce(pwqp1
);
1937 pwqp1
= isl_pw_qpolynomial_sub(pwqp1
, pwqp2
);
1938 assert(isl_pw_qpolynomial_is_zero(pwqp1
));
1939 isl_pw_qpolynomial_free(pwqp1
);
1942 void test_split_periods(isl_ctx
*ctx
)
1945 isl_pw_qpolynomial
*pwqp
;
1947 str
= "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1948 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
1949 "U >= 0; [U,V] -> U^2 : U >= 100 }";
1950 pwqp
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1952 pwqp
= isl_pw_qpolynomial_split_periods(pwqp
, 2);
1955 isl_pw_qpolynomial_free(pwqp
);
1958 void test_union(isl_ctx
*ctx
)
1961 isl_union_set
*uset1
, *uset2
;
1962 isl_union_map
*umap1
, *umap2
;
1964 str
= "{ [i] : 0 <= i <= 1 }";
1965 uset1
= isl_union_set_read_from_str(ctx
, str
);
1966 str
= "{ [1] -> [0] }";
1967 umap1
= isl_union_map_read_from_str(ctx
, str
);
1969 umap2
= isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1
), uset1
);
1970 assert(isl_union_map_is_equal(umap1
, umap2
));
1972 isl_union_map_free(umap1
);
1973 isl_union_map_free(umap2
);
1975 str
= "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1976 umap1
= isl_union_map_read_from_str(ctx
, str
);
1977 str
= "{ A[i]; B[i] }";
1978 uset1
= isl_union_set_read_from_str(ctx
, str
);
1980 uset2
= isl_union_map_domain(umap1
);
1982 assert(isl_union_set_is_equal(uset1
, uset2
));
1984 isl_union_set_free(uset1
);
1985 isl_union_set_free(uset2
);
1988 void test_bound(isl_ctx
*ctx
)
1991 isl_pw_qpolynomial
*pwqp
;
1992 isl_pw_qpolynomial_fold
*pwf
;
1994 str
= "{ [[a, b, c, d] -> [e]] -> 0 }";
1995 pwqp
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
1996 pwf
= isl_pw_qpolynomial_bound(pwqp
, isl_fold_max
, NULL
);
1997 assert(isl_pw_qpolynomial_fold_dim(pwf
, isl_dim_in
) == 4);
1998 isl_pw_qpolynomial_fold_free(pwf
);
2000 str
= "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2001 pwqp
= isl_pw_qpolynomial_read_from_str(ctx
, str
);
2002 pwf
= isl_pw_qpolynomial_bound(pwqp
, isl_fold_max
, NULL
);
2003 assert(isl_pw_qpolynomial_fold_dim(pwf
, isl_dim_in
) == 1);
2004 isl_pw_qpolynomial_fold_free(pwf
);
2007 void test_lift(isl_ctx
*ctx
)
2010 isl_basic_map
*bmap
;
2011 isl_basic_set
*bset
;
2013 str
= "{ [i0] : exists e0 : i0 = 4e0 }";
2014 bset
= isl_basic_set_read_from_str(ctx
, str
);
2015 bset
= isl_basic_set_lift(bset
);
2016 bmap
= isl_basic_map_from_range(bset
);
2017 bset
= isl_basic_map_domain(bmap
);
2018 isl_basic_set_free(bset
);
2025 } subset_tests
[] = {
2027 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2028 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2029 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2031 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2032 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2033 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2034 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2035 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2036 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2037 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2038 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2039 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2040 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2041 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2042 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2043 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2046 static int test_subset(isl_ctx
*ctx
)
2049 isl_set
*set1
, *set2
;
2052 for (i
= 0; i
< ARRAY_SIZE(subset_tests
); ++i
) {
2053 set1
= isl_set_read_from_str(ctx
, subset_tests
[i
].set1
);
2054 set2
= isl_set_read_from_str(ctx
, subset_tests
[i
].set2
);
2055 subset
= isl_set_is_subset(set1
, set2
);
2060 if (subset
!= subset_tests
[i
].subset
)
2061 isl_die(ctx
, isl_error_unknown
,
2062 "incorrect subset result", return -1);
2069 const char *minuend
;
2070 const char *subtrahend
;
2071 const char *difference
;
2072 } subtract_domain_tests
[] = {
2073 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2074 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2075 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2078 static int test_subtract(isl_ctx
*ctx
)
2081 isl_union_map
*umap1
, *umap2
;
2082 isl_union_set
*uset
;
2085 for (i
= 0; i
< ARRAY_SIZE(subtract_domain_tests
); ++i
) {
2086 umap1
= isl_union_map_read_from_str(ctx
,
2087 subtract_domain_tests
[i
].minuend
);
2088 uset
= isl_union_set_read_from_str(ctx
,
2089 subtract_domain_tests
[i
].subtrahend
);
2090 umap2
= isl_union_map_read_from_str(ctx
,
2091 subtract_domain_tests
[i
].difference
);
2092 umap1
= isl_union_map_subtract_domain(umap1
, uset
);
2093 equal
= isl_union_map_is_equal(umap1
, umap2
);
2094 isl_union_map_free(umap1
);
2095 isl_union_map_free(umap2
);
2099 isl_die(ctx
, isl_error_unknown
,
2100 "incorrect subtract domain result", return -1);
2106 int test_factorize(isl_ctx
*ctx
)
2109 isl_basic_set
*bset
;
2112 str
= "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2113 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2114 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2115 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2116 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2117 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2118 "3i5 >= -2i0 - i2 + 3i4 }";
2119 bset
= isl_basic_set_read_from_str(ctx
, str
);
2120 f
= isl_basic_set_factorizer(bset
);
2121 isl_basic_set_free(bset
);
2122 isl_factorizer_free(f
);
2124 isl_die(ctx
, isl_error_unknown
,
2125 "failed to construct factorizer", return -1);
2127 str
= "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2128 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2129 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2130 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2131 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2132 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2133 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2134 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2135 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2136 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2137 bset
= isl_basic_set_read_from_str(ctx
, str
);
2138 f
= isl_basic_set_factorizer(bset
);
2139 isl_basic_set_free(bset
);
2140 isl_factorizer_free(f
);
2142 isl_die(ctx
, isl_error_unknown
,
2143 "failed to construct factorizer", return -1);
2148 static int check_injective(__isl_take isl_map
*map
, void *user
)
2150 int *injective
= user
;
2152 *injective
= isl_map_is_injective(map
);
2155 if (*injective
< 0 || !*injective
)
2161 int test_one_schedule(isl_ctx
*ctx
, const char *d
, const char *w
,
2162 const char *r
, const char *s
, int tilable
, int parallel
)
2166 isl_union_map
*W
, *R
, *S
;
2167 isl_union_map
*empty
;
2168 isl_union_map
*dep_raw
, *dep_war
, *dep_waw
, *dep
;
2169 isl_union_map
*validity
, *proximity
;
2170 isl_union_map
*schedule
;
2171 isl_union_map
*test
;
2172 isl_union_set
*delta
;
2173 isl_union_set
*domain
;
2177 isl_schedule
*sched
;
2178 int is_nonneg
, is_parallel
, is_tilable
, is_injection
, is_complete
;
2180 D
= isl_union_set_read_from_str(ctx
, d
);
2181 W
= isl_union_map_read_from_str(ctx
, w
);
2182 R
= isl_union_map_read_from_str(ctx
, r
);
2183 S
= isl_union_map_read_from_str(ctx
, s
);
2185 W
= isl_union_map_intersect_domain(W
, isl_union_set_copy(D
));
2186 R
= isl_union_map_intersect_domain(R
, isl_union_set_copy(D
));
2188 empty
= isl_union_map_empty(isl_union_map_get_space(S
));
2189 isl_union_map_compute_flow(isl_union_map_copy(R
),
2190 isl_union_map_copy(W
), empty
,
2191 isl_union_map_copy(S
),
2192 &dep_raw
, NULL
, NULL
, NULL
);
2193 isl_union_map_compute_flow(isl_union_map_copy(W
),
2194 isl_union_map_copy(W
),
2195 isl_union_map_copy(R
),
2196 isl_union_map_copy(S
),
2197 &dep_waw
, &dep_war
, NULL
, NULL
);
2199 dep
= isl_union_map_union(dep_waw
, dep_war
);
2200 dep
= isl_union_map_union(dep
, dep_raw
);
2201 validity
= isl_union_map_copy(dep
);
2202 proximity
= isl_union_map_copy(dep
);
2204 sched
= isl_union_set_compute_schedule(isl_union_set_copy(D
),
2205 validity
, proximity
);
2206 schedule
= isl_schedule_get_map(sched
);
2207 isl_schedule_free(sched
);
2208 isl_union_map_free(W
);
2209 isl_union_map_free(R
);
2210 isl_union_map_free(S
);
2213 isl_union_map_foreach_map(schedule
, &check_injective
, &is_injection
);
2215 domain
= isl_union_map_domain(isl_union_map_copy(schedule
));
2216 is_complete
= isl_union_set_is_subset(D
, domain
);
2217 isl_union_set_free(D
);
2218 isl_union_set_free(domain
);
2220 test
= isl_union_map_reverse(isl_union_map_copy(schedule
));
2221 test
= isl_union_map_apply_range(test
, dep
);
2222 test
= isl_union_map_apply_range(test
, schedule
);
2224 delta
= isl_union_map_deltas(test
);
2225 if (isl_union_set_n_set(delta
) == 0) {
2229 isl_union_set_free(delta
);
2231 delta_set
= isl_set_from_union_set(delta
);
2233 slice
= isl_set_universe(isl_set_get_space(delta_set
));
2234 for (i
= 0; i
< tilable
; ++i
)
2235 slice
= isl_set_lower_bound_si(slice
, isl_dim_set
, i
, 0);
2236 is_tilable
= isl_set_is_subset(delta_set
, slice
);
2237 isl_set_free(slice
);
2239 slice
= isl_set_universe(isl_set_get_space(delta_set
));
2240 for (i
= 0; i
< parallel
; ++i
)
2241 slice
= isl_set_fix_si(slice
, isl_dim_set
, i
, 0);
2242 is_parallel
= isl_set_is_subset(delta_set
, slice
);
2243 isl_set_free(slice
);
2245 origin
= isl_set_universe(isl_set_get_space(delta_set
));
2246 for (i
= 0; i
< isl_set_dim(origin
, isl_dim_set
); ++i
)
2247 origin
= isl_set_fix_si(origin
, isl_dim_set
, i
, 0);
2249 delta_set
= isl_set_union(delta_set
, isl_set_copy(origin
));
2250 delta_set
= isl_set_lexmin(delta_set
);
2252 is_nonneg
= isl_set_is_equal(delta_set
, origin
);
2254 isl_set_free(origin
);
2255 isl_set_free(delta_set
);
2258 if (is_nonneg
< 0 || is_parallel
< 0 || is_tilable
< 0 ||
2259 is_injection
< 0 || is_complete
< 0)
2262 isl_die(ctx
, isl_error_unknown
,
2263 "generated schedule incomplete", return -1);
2265 isl_die(ctx
, isl_error_unknown
,
2266 "generated schedule not injective on each statement",
2269 isl_die(ctx
, isl_error_unknown
,
2270 "negative dependences in generated schedule",
2273 isl_die(ctx
, isl_error_unknown
,
2274 "generated schedule not as tilable as expected",
2277 isl_die(ctx
, isl_error_unknown
,
2278 "generated schedule not as parallel as expected",
2284 static __isl_give isl_union_map
*compute_schedule(isl_ctx
*ctx
,
2285 const char *domain
, const char *validity
, const char *proximity
)
2289 isl_union_map
*prox
;
2290 isl_schedule
*schedule
;
2291 isl_union_map
*sched
;
2293 dom
= isl_union_set_read_from_str(ctx
, domain
);
2294 dep
= isl_union_map_read_from_str(ctx
, validity
);
2295 prox
= isl_union_map_read_from_str(ctx
, proximity
);
2296 schedule
= isl_union_set_compute_schedule(dom
, dep
, prox
);
2297 sched
= isl_schedule_get_map(schedule
);
2298 isl_schedule_free(schedule
);
2303 /* Check that a schedule can be constructed on the given domain
2304 * with the given validity and proximity constraints.
2306 static int test_has_schedule(isl_ctx
*ctx
, const char *domain
,
2307 const char *validity
, const char *proximity
)
2309 isl_union_map
*sched
;
2311 sched
= compute_schedule(ctx
, domain
, validity
, proximity
);
2315 isl_union_map_free(sched
);
2319 int test_special_schedule(isl_ctx
*ctx
, const char *domain
,
2320 const char *validity
, const char *proximity
, const char *expected_sched
)
2322 isl_union_map
*sched1
, *sched2
;
2325 sched1
= compute_schedule(ctx
, domain
, validity
, proximity
);
2326 sched2
= isl_union_map_read_from_str(ctx
, expected_sched
);
2328 equal
= isl_union_map_is_equal(sched1
, sched2
);
2329 isl_union_map_free(sched1
);
2330 isl_union_map_free(sched2
);
2335 isl_die(ctx
, isl_error_unknown
, "unexpected schedule",
2341 int test_schedule(isl_ctx
*ctx
)
2343 const char *D
, *W
, *R
, *V
, *P
, *S
;
2345 /* Handle resulting schedule with zero bands. */
2346 if (test_one_schedule(ctx
, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2350 D
= "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2351 W
= "{ S1[t,i] -> a[t,i] }";
2352 R
= "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2353 "S1[t,i] -> a[t-1,i+1] }";
2354 S
= "{ S1[t,i] -> [t,i] }";
2355 if (test_one_schedule(ctx
, D
, W
, R
, S
, 2, 0) < 0)
2358 /* Fig. 5 of CC2008 */
2359 D
= "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2361 W
= "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2362 "j >= 2 and j <= -1 + N }";
2363 R
= "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2364 "j >= 2 and j <= -1 + N; "
2365 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2366 "j >= 2 and j <= -1 + N }";
2367 S
= "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2368 if (test_one_schedule(ctx
, D
, W
, R
, S
, 2, 0) < 0)
2371 D
= "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2372 W
= "{ S1[i] -> a[i] }";
2373 R
= "{ S2[i] -> a[i+1] }";
2374 S
= "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2375 if (test_one_schedule(ctx
, D
, W
, R
, S
, 1, 1) < 0)
2378 D
= "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2379 W
= "{ S1[i] -> a[i] }";
2380 R
= "{ S2[i] -> a[9-i] }";
2381 S
= "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2382 if (test_one_schedule(ctx
, D
, W
, R
, S
, 1, 1) < 0)
2385 D
= "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2386 W
= "{ S1[i] -> a[i] }";
2387 R
= "[N] -> { S2[i] -> a[N-1-i] }";
2388 S
= "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2389 if (test_one_schedule(ctx
, D
, W
, R
, S
, 1, 1) < 0)
2392 D
= "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2393 W
= "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2394 R
= "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2395 S
= "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2396 if (test_one_schedule(ctx
, D
, W
, R
, S
, 0, 0) < 0)
2399 D
= "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2400 W
= "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2401 R
= "{ S2[i,j] -> a[i-1,j] }";
2402 S
= "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2403 if (test_one_schedule(ctx
, D
, W
, R
, S
, 2, 1) < 0)
2406 D
= "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2407 W
= "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2408 R
= "{ S2[i,j] -> a[i,j-1] }";
2409 S
= "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2410 if (test_one_schedule(ctx
, D
, W
, R
, S
, 2, 1) < 0)
2413 D
= "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2414 W
= "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2415 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2416 R
= "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2417 S
= "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2418 "S_0[] -> [0, 0, 0] }";
2419 if (test_one_schedule(ctx
, D
, W
, R
, S
, 1, 0) < 0)
2421 ctx
->opt
->schedule_parametric
= 0;
2422 if (test_one_schedule(ctx
, D
, W
, R
, S
, 0, 0) < 0)
2424 ctx
->opt
->schedule_parametric
= 1;
2426 D
= "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2427 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2428 W
= "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2429 R
= "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2430 "S4[i] -> a[i,N] }";
2431 S
= "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2432 "S4[i] -> [4,i,0] }";
2433 if (test_one_schedule(ctx
, D
, W
, R
, S
, 2, 0) < 0)
2436 D
= "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2437 W
= "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2439 R
= "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2441 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2443 S
= "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2444 if (test_one_schedule(ctx
, D
, W
, R
, S
, 0, 0) < 0)
2447 D
= "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2448 " S_2[t] : t >= 0 and t <= -1 + N; "
2449 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2451 W
= "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2452 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2453 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2454 "i >= 0 and i <= -1 + N }";
2455 R
= "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
2456 "i >= 0 and i <= -1 + N; "
2457 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
2458 S
= "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
2459 " S_0[t] -> [0, t, 0] }";
2461 if (test_one_schedule(ctx
, D
, W
, R
, S
, 2, 1) < 0)
2463 ctx
->opt
->schedule_parametric
= 0;
2464 if (test_one_schedule(ctx
, D
, W
, R
, S
, 0, 0) < 0)
2466 ctx
->opt
->schedule_parametric
= 1;
2468 D
= "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
2469 S
= "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
2470 if (test_one_schedule(ctx
, D
, "{}", "{}", S
, 2, 2) < 0)
2473 D
= "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
2474 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
2475 W
= "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
2476 "j >= 0 and j <= -1 + N; "
2477 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2478 R
= "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
2479 "j >= 0 and j <= -1 + N; "
2480 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
2481 S
= "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
2482 if (test_one_schedule(ctx
, D
, W
, R
, S
, 0, 0) < 0)
2485 D
= "{ S_0[i] : i >= 0 }";
2486 W
= "{ S_0[i] -> a[i] : i >= 0 }";
2487 R
= "{ S_0[i] -> a[0] : i >= 0 }";
2488 S
= "{ S_0[i] -> [0, i, 0] }";
2489 if (test_one_schedule(ctx
, D
, W
, R
, S
, 0, 0) < 0)
2492 D
= "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
2493 W
= "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
2494 R
= "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
2495 S
= "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
2496 if (test_one_schedule(ctx
, D
, W
, R
, S
, 0, 0) < 0)
2499 D
= "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
2500 "k <= -1 + n and k >= 0 }";
2501 W
= "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
2502 R
= "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
2503 "k <= -1 + n and k >= 0; "
2504 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
2505 "k <= -1 + n and k >= 0; "
2506 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
2507 "k <= -1 + n and k >= 0 }";
2508 S
= "[n] -> { S_0[j, k] -> [2, j, k] }";
2509 ctx
->opt
->schedule_outer_zero_distance
= 1;
2510 if (test_one_schedule(ctx
, D
, W
, R
, S
, 0, 0) < 0)
2512 ctx
->opt
->schedule_outer_zero_distance
= 0;
2514 D
= "{Stmt_for_body24[i0, i1, i2, i3]:"
2515 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
2516 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
2517 "Stmt_for_body24[i0, i1, 1, 0]:"
2518 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
2519 "Stmt_for_body7[i0, i1, i2]:"
2520 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
2523 V
= "{Stmt_for_body24[0, i1, i2, i3] -> "
2524 "Stmt_for_body24[1, i1, i2, i3]:"
2525 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
2527 "Stmt_for_body24[0, i1, i2, i3] -> "
2528 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
2529 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
2531 "Stmt_for_body24[0, i1, i2, i3] ->"
2532 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
2533 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
2534 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
2535 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
2536 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
2537 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
2538 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
2539 "i1 <= 6 and i1 >= 0;"
2540 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
2541 "Stmt_for_body7[i0, i1, i2] -> "
2542 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
2543 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
2544 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
2545 "Stmt_for_body7[i0, i1, i2] -> "
2546 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
2547 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
2548 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
2550 S
= "{ Stmt_for_body24[i0, i1, i2, i3] -> "
2551 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
2552 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
2554 if (test_special_schedule(ctx
, D
, V
, P
, S
) < 0)
2557 D
= "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
2558 V
= "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
2559 "j >= 1 and j <= 7;"
2560 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
2561 "j >= 1 and j <= 8 }";
2563 S
= "{ S_0[i, j] -> [i + j, j] }";
2564 ctx
->opt
->schedule_algorithm
= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
;
2565 if (test_special_schedule(ctx
, D
, V
, P
, S
) < 0)
2567 ctx
->opt
->schedule_algorithm
= ISL_SCHEDULE_ALGORITHM_ISL
;
2569 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
2570 D
= "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
2571 "j >= 0 and j <= -1 + i }";
2572 V
= "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
2573 "i <= -1 + N and j >= 0;"
2574 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
2577 S
= "{ S_0[i, j] -> [i, j] }";
2578 ctx
->opt
->schedule_algorithm
= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
;
2579 if (test_special_schedule(ctx
, D
, V
, P
, S
) < 0)
2581 ctx
->opt
->schedule_algorithm
= ISL_SCHEDULE_ALGORITHM_ISL
;
2583 /* Test both algorithms on a case with only proximity dependences. */
2584 D
= "{ S[i,j] : 0 <= i <= 10 }";
2586 P
= "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
2587 S
= "{ S[i, j] -> [j, i] }";
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
;
2592 if (test_special_schedule(ctx
, D
, V
, P
, S
) < 0)
2595 D
= "{ A[a]; B[] }";
2597 P
= "{ A[a] -> B[] }";
2598 if (test_has_schedule(ctx
, D
, V
, P
) < 0)
2604 int test_plain_injective(isl_ctx
*ctx
, const char *str
, int injective
)
2606 isl_union_map
*umap
;
2609 umap
= isl_union_map_read_from_str(ctx
, str
);
2610 test
= isl_union_map_plain_is_injective(umap
);
2611 isl_union_map_free(umap
);
2614 if (test
== injective
)
2617 isl_die(ctx
, isl_error_unknown
,
2618 "map not detected as injective", return -1);
2620 isl_die(ctx
, isl_error_unknown
,
2621 "map detected as injective", return -1);
2624 int test_injective(isl_ctx
*ctx
)
2628 if (test_plain_injective(ctx
, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
2630 if (test_plain_injective(ctx
, "{S[] -> A[0]; T[] -> B[0]}", 1))
2632 if (test_plain_injective(ctx
, "{S[] -> A[0]; T[] -> A[1]}", 1))
2634 if (test_plain_injective(ctx
, "{S[] -> A[0]; T[] -> A[0]}", 0))
2636 if (test_plain_injective(ctx
, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
2638 if (test_plain_injective(ctx
, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
2640 if (test_plain_injective(ctx
, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
2642 if (test_plain_injective(ctx
, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
2645 str
= "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
2646 if (test_plain_injective(ctx
, str
, 1))
2648 str
= "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
2649 if (test_plain_injective(ctx
, str
, 0))
2655 static int aff_plain_is_equal(__isl_keep isl_aff
*aff
, const char *str
)
2663 aff2
= isl_aff_read_from_str(isl_aff_get_ctx(aff
), str
);
2664 equal
= isl_aff_plain_is_equal(aff
, aff2
);
2670 static int aff_check_plain_equal(__isl_keep isl_aff
*aff
, const char *str
)
2674 equal
= aff_plain_is_equal(aff
, str
);
2678 isl_die(isl_aff_get_ctx(aff
), isl_error_unknown
,
2679 "result not as expected", return -1);
2683 int test_aff(isl_ctx
*ctx
)
2688 isl_local_space
*ls
;
2692 space
= isl_space_set_alloc(ctx
, 0, 1);
2693 ls
= isl_local_space_from_space(space
);
2694 aff
= isl_aff_zero_on_domain(ls
);
2696 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
2697 aff
= isl_aff_scale_down_ui(aff
, 3);
2698 aff
= isl_aff_floor(aff
);
2699 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
2700 aff
= isl_aff_scale_down_ui(aff
, 2);
2701 aff
= isl_aff_floor(aff
);
2702 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
2705 set
= isl_set_read_from_str(ctx
, str
);
2706 aff
= isl_aff_gist(aff
, set
);
2708 aff
= isl_aff_add_constant_si(aff
, -16);
2709 zero
= isl_aff_plain_is_zero(aff
);
2715 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2717 aff
= isl_aff_read_from_str(ctx
, "{ [-1] }");
2718 aff
= isl_aff_scale_down_ui(aff
, 64);
2719 aff
= isl_aff_floor(aff
);
2720 equal
= aff_check_plain_equal(aff
, "{ [-1] }");
2728 int test_dim_max(isl_ctx
*ctx
)
2732 isl_set
*set1
, *set2
;
2737 str
= "[N] -> { [i] : 0 <= i <= min(N,10) }";
2738 set
= isl_set_read_from_str(ctx
, str
);
2739 pwaff
= isl_set_dim_max(set
, 0);
2740 set1
= isl_set_from_pw_aff(pwaff
);
2741 str
= "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
2742 set2
= isl_set_read_from_str(ctx
, str
);
2743 equal
= isl_set_is_equal(set1
, set2
);
2749 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2751 str
= "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
2752 set
= isl_set_read_from_str(ctx
, str
);
2753 pwaff
= isl_set_dim_max(set
, 0);
2754 set1
= isl_set_from_pw_aff(pwaff
);
2755 str
= "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2756 set2
= isl_set_read_from_str(ctx
, str
);
2757 equal
= isl_set_is_equal(set1
, set2
);
2763 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2765 str
= "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
2766 set
= isl_set_read_from_str(ctx
, str
);
2767 pwaff
= isl_set_dim_max(set
, 0);
2768 set1
= isl_set_from_pw_aff(pwaff
);
2769 str
= "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
2770 set2
= isl_set_read_from_str(ctx
, str
);
2771 equal
= isl_set_is_equal(set1
, set2
);
2777 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2779 str
= "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
2780 "0 <= i < N and 0 <= j < M }";
2781 map
= isl_map_read_from_str(ctx
, str
);
2782 set
= isl_map_range(map
);
2784 pwaff
= isl_set_dim_max(isl_set_copy(set
), 0);
2785 set1
= isl_set_from_pw_aff(pwaff
);
2786 str
= "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
2787 set2
= isl_set_read_from_str(ctx
, str
);
2788 equal
= isl_set_is_equal(set1
, set2
);
2792 pwaff
= isl_set_dim_max(isl_set_copy(set
), 3);
2793 set1
= isl_set_from_pw_aff(pwaff
);
2794 str
= "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
2795 set2
= isl_set_read_from_str(ctx
, str
);
2796 if (equal
>= 0 && equal
)
2797 equal
= isl_set_is_equal(set1
, set2
);
2806 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2811 int test_product(isl_ctx
*ctx
)
2815 isl_union_set
*uset1
, *uset2
;
2819 set
= isl_set_read_from_str(ctx
, str
);
2820 set
= isl_set_product(set
, isl_set_copy(set
));
2821 ok
= isl_set_is_wrapping(set
);
2826 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2829 uset1
= isl_union_set_read_from_str(ctx
, str
);
2830 uset1
= isl_union_set_product(uset1
, isl_union_set_copy(uset1
));
2831 str
= "{ [[] -> []] }";
2832 uset2
= isl_union_set_read_from_str(ctx
, str
);
2833 ok
= isl_union_set_is_equal(uset1
, uset2
);
2834 isl_union_set_free(uset1
);
2835 isl_union_set_free(uset2
);
2839 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2844 int test_equal(isl_ctx
*ctx
)
2847 isl_set
*set
, *set2
;
2851 set
= isl_set_read_from_str(ctx
, str
);
2853 set2
= isl_set_read_from_str(ctx
, str
);
2854 equal
= isl_set_is_equal(set
, set2
);
2860 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2865 static int test_plain_fixed(isl_ctx
*ctx
, __isl_take isl_map
*map
,
2866 enum isl_dim_type type
, unsigned pos
, int fixed
)
2870 test
= isl_map_plain_is_fixed(map
, type
, pos
, NULL
);
2877 isl_die(ctx
, isl_error_unknown
,
2878 "map not detected as fixed", return -1);
2880 isl_die(ctx
, isl_error_unknown
,
2881 "map detected as fixed", return -1);
2884 int test_fixed(isl_ctx
*ctx
)
2889 str
= "{ [i] -> [i] }";
2890 map
= isl_map_read_from_str(ctx
, str
);
2891 if (test_plain_fixed(ctx
, map
, isl_dim_out
, 0, 0))
2893 str
= "{ [i] -> [1] }";
2894 map
= isl_map_read_from_str(ctx
, str
);
2895 if (test_plain_fixed(ctx
, map
, isl_dim_out
, 0, 1))
2897 str
= "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
2898 map
= isl_map_read_from_str(ctx
, str
);
2899 if (test_plain_fixed(ctx
, map
, isl_dim_out
, 0, 1))
2901 map
= isl_map_read_from_str(ctx
, str
);
2902 map
= isl_map_neg(map
);
2903 if (test_plain_fixed(ctx
, map
, isl_dim_out
, 0, 1))
2909 int test_vertices(isl_ctx
*ctx
)
2912 isl_basic_set
*bset
;
2913 isl_vertices
*vertices
;
2915 str
= "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }";
2916 bset
= isl_basic_set_read_from_str(ctx
, str
);
2917 vertices
= isl_basic_set_compute_vertices(bset
);
2918 isl_basic_set_free(bset
);
2919 isl_vertices_free(vertices
);
2923 str
= "{ A[t, i] : t = 14 and i = 1 }";
2924 bset
= isl_basic_set_read_from_str(ctx
, str
);
2925 vertices
= isl_basic_set_compute_vertices(bset
);
2926 isl_basic_set_free(bset
);
2927 isl_vertices_free(vertices
);
2934 int test_union_pw(isl_ctx
*ctx
)
2938 isl_union_set
*uset
;
2939 isl_union_pw_qpolynomial
*upwqp1
, *upwqp2
;
2941 str
= "{ [x] -> x^2 }";
2942 upwqp1
= isl_union_pw_qpolynomial_read_from_str(ctx
, str
);
2943 upwqp2
= isl_union_pw_qpolynomial_copy(upwqp1
);
2944 uset
= isl_union_pw_qpolynomial_domain(upwqp1
);
2945 upwqp1
= isl_union_pw_qpolynomial_copy(upwqp2
);
2946 upwqp1
= isl_union_pw_qpolynomial_intersect_domain(upwqp1
, uset
);
2947 equal
= isl_union_pw_qpolynomial_plain_is_equal(upwqp1
, upwqp2
);
2948 isl_union_pw_qpolynomial_free(upwqp1
);
2949 isl_union_pw_qpolynomial_free(upwqp2
);
2953 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2958 int test_output(isl_ctx
*ctx
)
2966 str
= "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
2967 pa
= isl_pw_aff_read_from_str(ctx
, str
);
2969 p
= isl_printer_to_str(ctx
);
2970 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
2971 p
= isl_printer_print_pw_aff(p
, pa
);
2972 s
= isl_printer_get_str(p
);
2973 isl_printer_free(p
);
2974 isl_pw_aff_free(pa
);
2978 equal
= !strcmp(s
, "(2 - x + 4*floord(x, 4) >= 0) ? (1) : 2");
2983 isl_die(ctx
, isl_error_unknown
, "unexpected result", return -1);
2988 int test_sample(isl_ctx
*ctx
)
2991 isl_basic_set
*bset1
, *bset2
;
2994 str
= "{ [a, b, c, d, e, f, g, h, i, j, k] : "
2995 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
2996 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
2997 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
2998 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
2999 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3000 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3001 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3002 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3003 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3004 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3005 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3006 "d - 1073741821e and "
3007 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3008 "3j >= 1 - a + b + 2e and "
3009 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3010 "3i <= 4 - a + 4b - e and "
3011 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3012 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3013 "c <= -1 + a and 3i >= -2 - a + 3e and "
3014 "1073741822e <= 1073741823 - a + 1073741822b + c and "
3015 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3016 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3017 "1073741823e >= 1 + 1073741823b - d and "
3018 "3i >= 1073741823b + c - 1073741823e - f and "
3019 "3i >= 1 + 2b + e + 3g }";
3020 bset1
= isl_basic_set_read_from_str(ctx
, str
);
3021 bset2
= isl_basic_set_sample(isl_basic_set_copy(bset1
));
3022 empty
= isl_basic_set_is_empty(bset2
);
3023 subset
= isl_basic_set_is_subset(bset2
, bset1
);
3024 isl_basic_set_free(bset1
);
3025 isl_basic_set_free(bset2
);
3026 if (empty
< 0 || subset
< 0)
3029 isl_die(ctx
, isl_error_unknown
, "point not found", return -1);
3031 isl_die(ctx
, isl_error_unknown
, "bad point found", return -1);
3036 int test_fixed_power(isl_ctx
*ctx
)
3044 str
= "{ [i] -> [i + 1] }";
3045 map
= isl_map_read_from_str(ctx
, str
);
3046 isl_int_set_si(exp
, 23);
3047 map
= isl_map_fixed_power(map
, exp
);
3048 equal
= map_check_equal(map
, "{ [i] -> [i + 23] }");
3057 int test_slice(isl_ctx
*ctx
)
3063 str
= "{ [i] -> [j] }";
3064 map
= isl_map_read_from_str(ctx
, str
);
3065 map
= isl_map_equate(map
, isl_dim_in
, 0, isl_dim_out
, 0);
3066 equal
= map_check_equal(map
, "{ [i] -> [i] }");
3071 str
= "{ [i] -> [j] }";
3072 map
= isl_map_read_from_str(ctx
, str
);
3073 map
= isl_map_equate(map
, isl_dim_in
, 0, isl_dim_in
, 0);
3074 equal
= map_check_equal(map
, "{ [i] -> [j] }");
3079 str
= "{ [i] -> [j] }";
3080 map
= isl_map_read_from_str(ctx
, str
);
3081 map
= isl_map_oppose(map
, isl_dim_in
, 0, isl_dim_out
, 0);
3082 equal
= map_check_equal(map
, "{ [i] -> [-i] }");
3087 str
= "{ [i] -> [j] }";
3088 map
= isl_map_read_from_str(ctx
, str
);
3089 map
= isl_map_oppose(map
, isl_dim_in
, 0, isl_dim_in
, 0);
3090 equal
= map_check_equal(map
, "{ [0] -> [j] }");
3095 str
= "{ [i] -> [j] }";
3096 map
= isl_map_read_from_str(ctx
, str
);
3097 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_out
, 0);
3098 equal
= map_check_equal(map
, "{ [i] -> [j] : i > j }");
3103 str
= "{ [i] -> [j] }";
3104 map
= isl_map_read_from_str(ctx
, str
);
3105 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_in
, 0);
3106 equal
= map_check_equal(map
, "{ [i] -> [j] : false }");
3114 int test_eliminate(isl_ctx
*ctx
)
3120 str
= "{ [i] -> [j] : i = 2j }";
3121 map
= isl_map_read_from_str(ctx
, str
);
3122 map
= isl_map_eliminate(map
, isl_dim_out
, 0, 1);
3123 equal
= map_check_equal(map
, "{ [i] -> [j] : exists a : i = 2a }");
3131 /* Check that isl_set_dim_residue_class detects that the values of j
3132 * in the set below are all odd and that it does not detect any spurious
3135 static int test_residue_class(isl_ctx
*ctx
)
3142 str
= "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3143 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3144 set
= isl_set_read_from_str(ctx
, str
);
3147 res
= isl_set_dim_residue_class(set
, 1, &m
, &r
);
3149 (isl_int_cmp_si(m
, 2) != 0 || isl_int_cmp_si(r
, 1) != 0))
3150 isl_die(ctx
, isl_error_unknown
, "incorrect residue class",
3159 int test_align_parameters(isl_ctx
*ctx
)
3163 isl_multi_aff
*ma1
, *ma2
;
3166 str
= "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
3167 ma1
= isl_multi_aff_read_from_str(ctx
, str
);
3169 space
= isl_space_params_alloc(ctx
, 1);
3170 space
= isl_space_set_dim_name(space
, isl_dim_param
, 0, "N");
3171 ma1
= isl_multi_aff_align_params(ma1
, space
);
3173 str
= "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
3174 ma2
= isl_multi_aff_read_from_str(ctx
, str
);
3176 equal
= isl_multi_aff_plain_is_equal(ma1
, ma2
);
3178 isl_multi_aff_free(ma1
);
3179 isl_multi_aff_free(ma2
);
3184 isl_die(ctx
, isl_error_unknown
,
3185 "result not as expected", return -1);
3190 static int test_list(isl_ctx
*ctx
)
3192 isl_id
*a
, *b
, *c
, *d
, *id
;
3196 a
= isl_id_alloc(ctx
, "a", NULL
);
3197 b
= isl_id_alloc(ctx
, "b", NULL
);
3198 c
= isl_id_alloc(ctx
, "c", NULL
);
3199 d
= isl_id_alloc(ctx
, "d", NULL
);
3201 list
= isl_id_list_alloc(ctx
, 4);
3202 list
= isl_id_list_add(list
, a
);
3203 list
= isl_id_list_add(list
, b
);
3204 list
= isl_id_list_add(list
, c
);
3205 list
= isl_id_list_add(list
, d
);
3206 list
= isl_id_list_drop(list
, 1, 1);
3208 if (isl_id_list_n_id(list
) != 3) {
3209 isl_id_list_free(list
);
3210 isl_die(ctx
, isl_error_unknown
,
3211 "unexpected number of elements in list", return -1);
3214 id
= isl_id_list_get_id(list
, 0);
3217 id
= isl_id_list_get_id(list
, 1);
3220 id
= isl_id_list_get_id(list
, 2);
3224 isl_id_list_free(list
);
3227 isl_die(ctx
, isl_error_unknown
,
3228 "unexpected elements in list", return -1);
3233 const char *set_conversion_tests
[] = {
3234 "[N] -> { [i] : N - 1 <= 2 i <= N }",
3235 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
3236 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3237 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
3240 /* Check that converting from isl_set to isl_pw_multi_aff and back
3241 * to isl_set produces the original isl_set.
3243 static int test_set_conversion(isl_ctx
*ctx
)
3247 isl_set
*set1
, *set2
;
3248 isl_pw_multi_aff
*pma
;
3251 for (i
= 0; i
< ARRAY_SIZE(set_conversion_tests
); ++i
) {
3252 str
= set_conversion_tests
[i
];
3253 set1
= isl_set_read_from_str(ctx
, str
);
3254 pma
= isl_pw_multi_aff_from_set(isl_set_copy(set1
));
3255 set2
= isl_set_from_pw_multi_aff(pma
);
3256 equal
= isl_set_is_equal(set1
, set2
);
3263 isl_die(ctx
, isl_error_unknown
, "bad conversion",
3270 /* Check that converting from isl_map to isl_pw_multi_aff and back
3271 * to isl_map produces the original isl_map.
3273 static int test_map_conversion(isl_ctx
*ctx
)
3276 isl_map
*map1
, *map2
;
3277 isl_pw_multi_aff
*pma
;
3280 str
= "{ [a, b, c, d] -> s0[a, b, e, f] : "
3281 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
3282 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
3283 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
3285 map1
= isl_map_read_from_str(ctx
, str
);
3286 pma
= isl_pw_multi_aff_from_map(isl_map_copy(map1
));
3287 map2
= isl_map_from_pw_multi_aff(pma
);
3288 equal
= isl_map_is_equal(map1
, map2
);
3295 isl_die(ctx
, isl_error_unknown
, "bad conversion", return -1);
3300 static int test_conversion(isl_ctx
*ctx
)
3302 if (test_set_conversion(ctx
) < 0)
3304 if (test_map_conversion(ctx
) < 0)
3309 /* Check that isl_basic_map_curry does not modify input.
3311 static int test_curry(isl_ctx
*ctx
)
3314 isl_basic_map
*bmap1
, *bmap2
;
3317 str
= "{ [A[] -> B[]] -> C[] }";
3318 bmap1
= isl_basic_map_read_from_str(ctx
, str
);
3319 bmap2
= isl_basic_map_curry(isl_basic_map_copy(bmap1
));
3320 equal
= isl_basic_map_is_equal(bmap1
, bmap2
);
3321 isl_basic_map_free(bmap1
);
3322 isl_basic_map_free(bmap2
);
3327 isl_die(ctx
, isl_error_unknown
,
3328 "curried map should not be equal to original",
3338 } preimage_tests
[] = {
3339 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
3340 "{ A[j,i] -> B[i,j] }",
3341 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
3342 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3343 "{ A[a,b] -> B[a/2,b/6] }",
3344 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
3345 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
3346 "{ A[a,b] -> B[a/2,b/6] }",
3347 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
3348 "exists i,j : a = 2 i and b = 6 j }" },
3349 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
3350 "[n] -> { : 0 <= n <= 100 }" },
3351 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3352 "{ A[a] -> B[2a] }",
3353 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
3354 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
3355 "{ A[a] -> B[([a/2])] }",
3356 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
3357 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
3358 "{ A[a] -> B[a,a,a/3] }",
3359 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
3360 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
3361 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
3364 int test_preimage(isl_ctx
*ctx
)
3367 isl_basic_set
*bset1
, *bset2
;
3371 for (i
= 0; i
< ARRAY_SIZE(preimage_tests
); ++i
) {
3372 bset1
= isl_basic_set_read_from_str(ctx
, preimage_tests
[i
].set
);
3373 ma
= isl_multi_aff_read_from_str(ctx
, preimage_tests
[i
].ma
);
3374 bset2
= isl_basic_set_read_from_str(ctx
, preimage_tests
[i
].res
);
3375 bset1
= isl_basic_set_preimage_multi_aff(bset1
, ma
);
3376 equal
= isl_basic_set_is_equal(bset1
, bset2
);
3377 isl_basic_set_free(bset1
);
3378 isl_basic_set_free(bset2
);
3382 isl_die(ctx
, isl_error_unknown
, "bad preimage",
3393 } pullback_tests
[] = {
3394 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
3395 "{ A[a,b] -> C[b + 2a] }" },
3396 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
3397 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
3398 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
3399 "{ A[a] -> C[(a)/6] }" },
3400 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
3401 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
3402 "{ A[a] -> C[(2a)/3] }" },
3403 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
3404 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
3405 "{ A[i,j] -> C[i + j, i + j] }"},
3406 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
3407 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
3408 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
3411 static int test_pullback(isl_ctx
*ctx
)
3414 isl_multi_aff
*ma1
, *ma2
;
3418 for (i
= 0; i
< ARRAY_SIZE(pullback_tests
); ++i
) {
3419 ma1
= isl_multi_aff_read_from_str(ctx
, pullback_tests
[i
].ma1
);
3420 ma
= isl_multi_aff_read_from_str(ctx
, pullback_tests
[i
].ma
);
3421 ma2
= isl_multi_aff_read_from_str(ctx
, pullback_tests
[i
].res
);
3422 ma1
= isl_multi_aff_pullback_multi_aff(ma1
, ma
);
3423 equal
= isl_multi_aff_plain_is_equal(ma1
, ma2
);
3424 isl_multi_aff_free(ma1
);
3425 isl_multi_aff_free(ma2
);
3429 isl_die(ctx
, isl_error_unknown
, "bad pullback",
3436 /* Check that negation is printed correctly.
3438 static int test_ast(isl_ctx
*ctx
)
3440 isl_ast_expr
*expr
, *expr1
, *expr2
, *expr3
;
3444 expr1
= isl_ast_expr_from_id(isl_id_alloc(ctx
, "A", NULL
));
3445 expr2
= isl_ast_expr_from_id(isl_id_alloc(ctx
, "B", NULL
));
3446 expr
= isl_ast_expr_add(expr1
, expr2
);
3447 expr
= isl_ast_expr_neg(expr
);
3448 str
= isl_ast_expr_to_str(expr
);
3449 ok
= str
? !strcmp(str
, "-(A + B)") : -1;
3451 isl_ast_expr_free(expr
);
3456 isl_die(ctx
, isl_error_unknown
,
3457 "isl_ast_expr printed incorrectly", return -1);
3459 expr1
= isl_ast_expr_from_id(isl_id_alloc(ctx
, "A", NULL
));
3460 expr2
= isl_ast_expr_from_id(isl_id_alloc(ctx
, "B", NULL
));
3461 expr
= isl_ast_expr_add(expr1
, expr2
);
3462 expr3
= isl_ast_expr_from_id(isl_id_alloc(ctx
, "C", NULL
));
3463 expr
= isl_ast_expr_sub(expr3
, expr
);
3464 str
= isl_ast_expr_to_str(expr
);
3465 ok
= str
? !strcmp(str
, "C - (A + B)") : -1;
3467 isl_ast_expr_free(expr
);
3472 isl_die(ctx
, isl_error_unknown
,
3473 "isl_ast_expr printed incorrectly", return -1);
3478 /* Internal data structure for before_for and after_for callbacks.
3480 * depth is the current depth
3481 * before is the number of times before_for has been called
3482 * after is the number of times after_for has been called
3484 struct isl_test_codegen_data
{
3490 /* This function is called before each for loop in the AST generated
3491 * from test_ast_gen1.
3493 * Increment the number of calls and the depth.
3494 * Check that the space returned by isl_ast_build_get_schedule_space
3495 * matches the target space of the schedule returned by
3496 * isl_ast_build_get_schedule.
3497 * Return an isl_id that is checked by the corresponding call
3500 static __isl_give isl_id
*before_for(__isl_keep isl_ast_build
*build
,
3503 struct isl_test_codegen_data
*data
= user
;
3506 isl_union_map
*schedule
;
3507 isl_union_set
*uset
;
3512 ctx
= isl_ast_build_get_ctx(build
);
3514 if (data
->before
>= 3)
3515 isl_die(ctx
, isl_error_unknown
,
3516 "unexpected number of for nodes", return NULL
);
3517 if (data
->depth
>= 2)
3518 isl_die(ctx
, isl_error_unknown
,
3519 "unexpected depth", return NULL
);
3521 snprintf(name
, sizeof(name
), "d%d", data
->depth
);
3525 schedule
= isl_ast_build_get_schedule(build
);
3526 uset
= isl_union_map_range(schedule
);
3529 if (isl_union_set_n_set(uset
) != 1) {
3530 isl_union_set_free(uset
);
3531 isl_die(ctx
, isl_error_unknown
,
3532 "expecting single range space", return NULL
);
3535 space
= isl_ast_build_get_schedule_space(build
);
3536 set
= isl_union_set_extract_set(uset
, space
);
3537 isl_union_set_free(uset
);
3538 empty
= isl_set_is_empty(set
);
3544 isl_die(ctx
, isl_error_unknown
,
3545 "spaces don't match", return NULL
);
3547 return isl_id_alloc(ctx
, name
, NULL
);
3550 /* This function is called after each for loop in the AST generated
3551 * from test_ast_gen1.
3553 * Increment the number of calls and decrement the depth.
3554 * Check that the annotation attached to the node matches
3555 * the isl_id returned by the corresponding call to before_for.
3557 static __isl_give isl_ast_node
*after_for(__isl_take isl_ast_node
*node
,
3558 __isl_keep isl_ast_build
*build
, void *user
)
3560 struct isl_test_codegen_data
*data
= user
;
3568 if (data
->after
> data
->before
)
3569 isl_die(isl_ast_node_get_ctx(node
), isl_error_unknown
,
3570 "mismatch in number of for nodes",
3571 return isl_ast_node_free(node
));
3573 id
= isl_ast_node_get_annotation(node
);
3575 isl_die(isl_ast_node_get_ctx(node
), isl_error_unknown
,
3576 "missing annotation", return isl_ast_node_free(node
));
3578 name
= isl_id_get_name(id
);
3579 valid
= name
&& atoi(name
+ 1) == data
->depth
;
3583 isl_die(isl_ast_node_get_ctx(node
), isl_error_unknown
,
3584 "wrong annotation", return isl_ast_node_free(node
));
3589 /* Check that the before_each_for and after_each_for callbacks
3590 * are called for each for loop in the generated code,
3591 * that they are called in the right order and that the isl_id
3592 * returned from the before_each_for callback is attached to
3593 * the isl_ast_node passed to the corresponding after_each_for call.
3595 static int test_ast_gen1(isl_ctx
*ctx
)
3599 isl_union_map
*schedule
;
3600 isl_ast_build
*build
;
3602 struct isl_test_codegen_data data
;
3604 str
= "[N] -> { : N >= 10 }";
3605 set
= isl_set_read_from_str(ctx
, str
);
3606 str
= "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
3607 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
3608 schedule
= isl_union_map_read_from_str(ctx
, str
);
3613 build
= isl_ast_build_from_context(set
);
3614 build
= isl_ast_build_set_before_each_for(build
,
3615 &before_for
, &data
);
3616 build
= isl_ast_build_set_after_each_for(build
,
3618 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
3619 isl_ast_build_free(build
);
3623 isl_ast_node_free(tree
);
3625 if (data
.before
!= 3 || data
.after
!= 3)
3626 isl_die(ctx
, isl_error_unknown
,
3627 "unexpected number of for nodes", return -1);
3632 /* Check that the AST generator handles domains that are integrally disjoint
3633 * but not ratinoally disjoint.
3635 static int test_ast_gen2(isl_ctx
*ctx
)
3639 isl_union_map
*schedule
;
3640 isl_union_map
*options
;
3641 isl_ast_build
*build
;
3644 str
= "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
3645 schedule
= isl_union_map_read_from_str(ctx
, str
);
3646 set
= isl_set_universe(isl_space_params_alloc(ctx
, 0));
3647 build
= isl_ast_build_from_context(set
);
3649 str
= "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
3650 options
= isl_union_map_read_from_str(ctx
, str
);
3651 build
= isl_ast_build_set_options(build
, options
);
3652 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
3653 isl_ast_build_free(build
);
3656 isl_ast_node_free(tree
);
3661 /* Increment *user on each call.
3663 static __isl_give isl_ast_node
*count_domains(__isl_take isl_ast_node
*node
,
3664 __isl_keep isl_ast_build
*build
, void *user
)
3673 /* Test that unrolling tries to minimize the number of instances.
3674 * In particular, for the schedule given below, make sure it generates
3675 * 3 nodes (rather than 101).
3677 static int test_ast_gen3(isl_ctx
*ctx
)
3681 isl_union_map
*schedule
;
3682 isl_union_map
*options
;
3683 isl_ast_build
*build
;
3687 str
= "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
3688 schedule
= isl_union_map_read_from_str(ctx
, str
);
3689 set
= isl_set_universe(isl_space_params_alloc(ctx
, 0));
3691 str
= "{ [i] -> unroll[0] }";
3692 options
= isl_union_map_read_from_str(ctx
, str
);
3694 build
= isl_ast_build_from_context(set
);
3695 build
= isl_ast_build_set_options(build
, options
);
3696 build
= isl_ast_build_set_at_each_domain(build
,
3697 &count_domains
, &n_domain
);
3698 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
3699 isl_ast_build_free(build
);
3703 isl_ast_node_free(tree
);
3706 isl_die(ctx
, isl_error_unknown
,
3707 "unexpected number of for nodes", return -1);
3712 /* Check that if the ast_build_exploit_nested_bounds options is set,
3713 * we do not get an outer if node in the generated AST,
3714 * while we do get such an outer if node if the options is not set.
3716 static int test_ast_gen4(isl_ctx
*ctx
)
3720 isl_union_map
*schedule
;
3721 isl_ast_build
*build
;
3723 enum isl_ast_node_type type
;
3726 enb
= isl_options_get_ast_build_exploit_nested_bounds(ctx
);
3727 str
= "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
3729 isl_options_set_ast_build_exploit_nested_bounds(ctx
, 1);
3731 schedule
= isl_union_map_read_from_str(ctx
, str
);
3732 set
= isl_set_universe(isl_space_params_alloc(ctx
, 0));
3733 build
= isl_ast_build_from_context(set
);
3734 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
3735 isl_ast_build_free(build
);
3739 type
= isl_ast_node_get_type(tree
);
3740 isl_ast_node_free(tree
);
3742 if (type
== isl_ast_node_if
)
3743 isl_die(ctx
, isl_error_unknown
,
3744 "not expecting if node", return -1);
3746 isl_options_set_ast_build_exploit_nested_bounds(ctx
, 0);
3748 schedule
= isl_union_map_read_from_str(ctx
, str
);
3749 set
= isl_set_universe(isl_space_params_alloc(ctx
, 0));
3750 build
= isl_ast_build_from_context(set
);
3751 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
3752 isl_ast_build_free(build
);
3756 type
= isl_ast_node_get_type(tree
);
3757 isl_ast_node_free(tree
);
3759 if (type
!= isl_ast_node_if
)
3760 isl_die(ctx
, isl_error_unknown
,
3761 "expecting if node", return -1);
3763 isl_options_set_ast_build_exploit_nested_bounds(ctx
, enb
);
3768 static int test_ast_gen(isl_ctx
*ctx
)
3770 if (test_ast_gen1(ctx
) < 0)
3772 if (test_ast_gen2(ctx
) < 0)
3774 if (test_ast_gen3(ctx
) < 0)
3776 if (test_ast_gen4(ctx
) < 0)
3781 /* Check if dropping output dimensions from an isl_pw_multi_aff
3784 static int test_pw_multi_aff(isl_ctx
*ctx
)
3787 isl_pw_multi_aff
*pma1
, *pma2
;
3790 str
= "{ [i,j] -> [i+j, 4i-j] }";
3791 pma1
= isl_pw_multi_aff_read_from_str(ctx
, str
);
3792 str
= "{ [i,j] -> [4i-j] }";
3793 pma2
= isl_pw_multi_aff_read_from_str(ctx
, str
);
3795 pma1
= isl_pw_multi_aff_drop_dims(pma1
, isl_dim_out
, 0, 1);
3797 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
3799 isl_pw_multi_aff_free(pma1
);
3800 isl_pw_multi_aff_free(pma2
);
3804 isl_die(ctx
, isl_error_unknown
,
3805 "expressions not equal", return -1);
3812 int (*fn
)(isl_ctx
*ctx
);
3814 { "curry", &test_curry
},
3815 { "piecewise multi affine expressions", &test_pw_multi_aff
},
3816 { "conversion", &test_conversion
},
3817 { "list", &test_list
},
3818 { "align parameters", &test_align_parameters
},
3819 { "preimage", &test_preimage
},
3820 { "pullback", &test_pullback
},
3821 { "AST", &test_ast
},
3822 { "AST generation", &test_ast_gen
},
3823 { "eliminate", &test_eliminate
},
3824 { "residue class", &test_residue_class
},
3825 { "div", &test_div
},
3826 { "slice", &test_slice
},
3827 { "fixed power", &test_fixed_power
},
3828 { "sample", &test_sample
},
3829 { "output", &test_output
},
3830 { "vertices", &test_vertices
},
3831 { "fixed", &test_fixed
},
3832 { "equal", &test_equal
},
3833 { "product", &test_product
},
3834 { "dim_max", &test_dim_max
},
3835 { "affine", &test_aff
},
3836 { "injective", &test_injective
},
3837 { "schedule", &test_schedule
},
3838 { "union_pw", &test_union_pw
},
3839 { "parse", &test_parse
},
3840 { "single-valued", &test_sv
},
3841 { "affine hull", &test_affine_hull
},
3842 { "coalesce", &test_coalesce
},
3843 { "factorize", &test_factorize
},
3844 { "subset", &test_subset
},
3845 { "subtract", &test_subtract
},
3851 struct isl_ctx
*ctx
;
3853 srcdir
= getenv("srcdir");
3856 ctx
= isl_ctx_alloc();
3857 for (i
= 0; i
< ARRAY_SIZE(tests
); ++i
) {
3858 printf("%s\n", tests
[i
].name
);
3859 if (tests
[i
].fn(ctx
) < 0)
3865 test_split_periods(ctx
);
3868 test_bijective(ctx
);
3872 test_construction(ctx
);
3874 test_application(ctx
);
3875 test_convex_hull(ctx
);