isl_tab.c: context_tab_insert_div: use isl_bool_ok
[isl.git] / isl_test.c
blob307f3adb074c2848b952c5fca96db18abaef4a75
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <assert.h>
19 #include <stdio.h>
20 #include <limits.h>
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl_space_private.h>
25 #include <isl/id.h>
26 #include <isl/set.h>
27 #include <isl/flow.h>
28 #include <isl_constraint_private.h>
29 #include <isl/polynomial.h>
30 #include <isl/union_set.h>
31 #include <isl/union_map.h>
32 #include <isl_factorization.h>
33 #include <isl/schedule.h>
34 #include <isl/schedule_node.h>
35 #include <isl_options_private.h>
36 #include <isl_vertices_private.h>
37 #include <isl/ast_build.h>
38 #include <isl/val.h>
39 #include <isl/ilp.h>
40 #include <isl_ast_build_expr.h>
41 #include <isl/options.h>
43 #include "isl_srcdir.c"
45 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
47 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
48 char *filename;
49 int length;
50 char *pattern = "%s/test_inputs/%s.%s";
52 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
53 + strlen(suffix) + 1;
54 filename = isl_alloc_array(ctx, char, length);
56 if (!filename)
57 return NULL;
59 sprintf(filename, pattern, srcdir, name, suffix);
61 return filename;
64 void test_parse_map(isl_ctx *ctx, const char *str)
66 isl_map *map;
68 map = isl_map_read_from_str(ctx, str);
69 assert(map);
70 isl_map_free(map);
73 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
75 isl_map *map, *map2;
76 int equal;
78 map = isl_map_read_from_str(ctx, str);
79 map2 = isl_map_read_from_str(ctx, str2);
80 equal = isl_map_is_equal(map, map2);
81 isl_map_free(map);
82 isl_map_free(map2);
84 if (equal < 0)
85 return -1;
86 if (!equal)
87 isl_die(ctx, isl_error_unknown, "maps not equal",
88 return -1);
90 return 0;
93 void test_parse_pwqp(isl_ctx *ctx, const char *str)
95 isl_pw_qpolynomial *pwqp;
97 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
98 assert(pwqp);
99 isl_pw_qpolynomial_free(pwqp);
102 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
104 isl_pw_aff *pwaff;
106 pwaff = isl_pw_aff_read_from_str(ctx, str);
107 assert(pwaff);
108 isl_pw_aff_free(pwaff);
111 /* Check that we can read an isl_multi_val from "str" without errors.
113 static int test_parse_multi_val(isl_ctx *ctx, const char *str)
115 isl_multi_val *mv;
117 mv = isl_multi_val_read_from_str(ctx, str);
118 isl_multi_val_free(mv);
120 return mv ? 0 : -1;
123 /* Check that printing "mpa" and parsing the output results
124 * in the same expression.
126 static isl_stat check_reparse_mpa(isl_ctx *ctx,
127 __isl_take isl_multi_pw_aff *mpa)
129 char *str;
130 isl_bool equal;
131 isl_multi_pw_aff *mpa2;
133 str = isl_multi_pw_aff_to_str(mpa);
134 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
135 free(str);
136 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
137 isl_multi_pw_aff_free(mpa);
138 isl_multi_pw_aff_free(mpa2);
139 if (equal < 0)
140 return isl_stat_error;
141 if (!equal)
142 isl_die(ctx, isl_error_unknown,
143 "parsed function not equal to original",
144 return isl_stat_error);
146 return isl_stat_ok;
149 /* String descriptions of multi piecewise affine expressions
150 * that are used for testing printing and parsing.
152 const char *parse_multi_mpa_tests[] = {
153 "{ A[x, y] -> [] : x + y >= 0 }",
154 "{ A[x, y] -> B[] : x + y >= 0 }",
155 "{ A[x, y] -> [x] : x + y >= 0 }",
156 "[N] -> { A[x, y] -> [x] : x + y <= N }",
157 "{ A[x, y] -> [x, y] : x + y >= 0 }",
158 "{ A[x, y] -> [(x : x >= 0), (y : y >= 0)] : x + y >= 0 }",
159 "[N] -> { [] : N >= 0 }",
160 "[N] -> { [] : N >= 0 }",
161 "[N] -> { [N] : N >= 0 }",
162 "[N] -> { [N, N + 1] : N >= 0 }",
163 "[N, M] -> { [(N : N >= 0), (M : M >= 0)] : N + M >= 0 }",
166 /* Test parsing of multi piecewise affine expressions by printing
167 * the expressions and checking that parsing the output results
168 * in the same expression.
169 * Do this for a couple of manually constructed expressions and
170 * a set of expressions parsed from strings.
172 static int test_parse_mpa(isl_ctx *ctx)
174 int i;
175 isl_space *space;
176 isl_set *dom;
177 isl_multi_pw_aff *mpa;
178 isl_stat r;
180 space = isl_space_set_alloc(ctx, 0, 0);
181 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
182 mpa = isl_multi_pw_aff_zero(space);
183 r = check_reparse_mpa(ctx, mpa);
184 if (r < 0)
185 return -1;
187 space = isl_space_set_alloc(ctx, 1, 0);
188 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
189 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
190 dom = isl_set_universe(isl_space_params(isl_space_copy(space)));
191 dom = isl_set_lower_bound_si(dom, isl_dim_param, 0, 5);
192 mpa = isl_multi_pw_aff_zero(space);
193 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
194 r = check_reparse_mpa(ctx, mpa);
195 if (r < 0)
196 return -1;
198 for (i = 0; i < ARRAY_SIZE(parse_multi_mpa_tests); ++i) {
199 const char *str;
201 str = parse_multi_mpa_tests[i];
202 mpa = isl_multi_pw_aff_read_from_str(ctx, str);
203 r = check_reparse_mpa(ctx, mpa);
204 if (r < 0)
205 return -1;
208 return 0;
211 /* Check that printing "mupa" and parsing the output results
212 * in the same expression.
214 static isl_stat check_reparse_mupa(isl_ctx *ctx,
215 __isl_take isl_multi_union_pw_aff *mupa)
217 char *str;
218 isl_bool equal;
219 isl_multi_union_pw_aff *mupa2;
221 str = isl_multi_union_pw_aff_to_str(mupa);
222 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx, str);
223 free(str);
224 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
225 isl_multi_union_pw_aff_free(mupa);
226 isl_multi_union_pw_aff_free(mupa2);
227 if (equal < 0)
228 return isl_stat_error;
229 if (!equal)
230 isl_die(ctx, isl_error_unknown,
231 "parsed function not equal to original",
232 return isl_stat_error);
234 return isl_stat_ok;
237 /* String descriptions of multi union piecewise affine expressions
238 * that are used for testing printing and parsing.
240 const char *parse_multi_mupa_tests[] = {
241 "[]",
242 "A[]",
243 "A[B[] -> C[]]",
244 "(A[] : { S[x] : x > 0; T[y] : y >= 0 })",
245 "(A[] : { })",
246 "[N] -> (A[] : { })",
247 "[N] -> (A[] : { : N >= 0 })",
248 "[N] -> (A[] : { S[x] : x > N; T[y] : y >= 0 })",
249 "(A[] : [N] -> { S[x] : x > N; T[y] : y >= 0 })",
250 "A[{ S[x] -> [x + 1]; T[x] -> [x] }]",
251 "(A[{ S[x] -> [x + 1]; T[x] -> [x] }] : "
252 "{ S[x] : x > 0; T[y] : y >= 0 })",
255 /* Test parsing of multi union piecewise affine expressions by printing
256 * the expressions and checking that parsing the output results
257 * in the same expression.
258 * Do this for a couple of manually constructed expressions and
259 * a set of expressions parsed from strings.
261 static int test_parse_mupa(isl_ctx *ctx)
263 int i;
264 isl_space *space;
265 isl_multi_union_pw_aff *mupa;
266 isl_set *dom;
267 isl_union_set *uset;
268 isl_stat r;
270 space = isl_space_set_alloc(ctx, 0, 0);
271 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
272 mupa = isl_multi_union_pw_aff_zero(space);
273 r = check_reparse_mupa(ctx, mupa);
274 if (r < 0)
275 return -1;
277 space = isl_space_set_alloc(ctx, 1, 0);
278 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
279 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
280 dom = isl_set_universe(space);
281 dom = isl_set_lower_bound_si(dom, isl_dim_param, 0, 5);
282 uset = isl_union_set_from_set(dom);
283 space = isl_space_set_alloc(ctx, 1, 0);
284 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
285 space = isl_space_set_tuple_name(space, isl_dim_set, "B");
286 mupa = isl_multi_union_pw_aff_zero(space);
287 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, uset);
288 r = check_reparse_mupa(ctx, mupa);
289 if (r < 0)
290 return -1;
292 for (i = 0; i < ARRAY_SIZE(parse_multi_mupa_tests); ++i) {
293 const char *str;
295 str = parse_multi_mupa_tests[i];
296 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
297 r = check_reparse_mupa(ctx, mupa);
298 if (r < 0)
299 return -1;
302 return 0;
305 /* Test parsing of multi expressions.
307 static int test_parse_multi(isl_ctx *ctx)
309 if (test_parse_mpa(ctx) < 0)
310 return -1;
311 if (test_parse_mupa(ctx) < 0)
312 return -1;
314 return 0;
317 /* Pairs of binary relation representations that should represent
318 * the same binary relations.
320 struct {
321 const char *map1;
322 const char *map2;
323 } parse_map_equal_tests[] = {
324 { "{ [x,y] : [([x/2]+y)/3] >= 1 }",
325 "{ [x, y] : 2y >= 6 - x }" },
326 { "{ [x,y] : x <= min(y, 2*y+3) }",
327 "{ [x,y] : x <= y, 2*y + 3 }" },
328 { "{ [x,y] : x >= min(y, 2*y+3) }",
329 "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }" },
330 { "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }",
331 "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }" },
332 { "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }",
333 "{ [i,j] -> [min(i,j)] }" },
334 { "{ [i,j] : i != j }",
335 "{ [i,j] : i < j or i > j }" },
336 { "{ [i,j] : (i+1)*2 >= j }",
337 "{ [i, j] : j <= 2 + 2i }" },
338 { "{ [i] -> [i > 0 ? 4 : 5] }",
339 "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }" },
340 { "[N=2,M] -> { [i=[(M+N)/4]] }",
341 "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }" },
342 { "{ [x] : x >= 0 }",
343 "{ [x] : x-0 >= 0 }" },
344 { "{ [i] : ((i > 10)) }",
345 "{ [i] : i >= 11 }" },
346 { "{ [i] -> [0] }",
347 "{ [i] -> [0 * i] }" },
348 { "{ [a] -> [b] : (not false) }",
349 "{ [a] -> [b] : true }" },
350 { "{ [i] : i/2 <= 5 }",
351 "{ [i] : i <= 10 }" },
352 { "{Sym=[n] [i] : i <= n }",
353 "[n] -> { [i] : i <= n }" },
354 { "{ [*] }",
355 "{ [a] }" },
356 { "{ [i] : 2*floor(i/2) = i }",
357 "{ [i] : exists a : i = 2 a }" },
358 { "{ [a] -> [b] : a = 5 implies b = 5 }",
359 "{ [a] -> [b] : a != 5 or b = 5 }" },
360 { "{ [a] -> [a - 1 : a > 0] }",
361 "{ [a] -> [a - 1] : a > 0 }" },
362 { "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
363 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }" },
364 { "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
365 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
366 { "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
367 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
368 { "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
369 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
370 { "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
371 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
372 { "{ [a,b] -> [i,j] : a,b << i,j }",
373 "{ [a,b] -> [i,j] : a < i or (a = i and b < j) }" },
374 { "{ [a,b] -> [i,j] : a,b <<= i,j }",
375 "{ [a,b] -> [i,j] : a < i or (a = i and b <= j) }" },
376 { "{ [a,b] -> [i,j] : a,b >> i,j }",
377 "{ [a,b] -> [i,j] : a > i or (a = i and b > j) }" },
378 { "{ [a,b] -> [i,j] : a,b >>= i,j }",
379 "{ [a,b] -> [i,j] : a > i or (a = i and b >= j) }" },
380 { "{ [n] -> [i] : exists (a, b, c: 8b <= i - 32a and "
381 "8b >= -7 + i - 32 a and b >= 0 and b <= 3 and "
382 "8c < n - 32a and i < n and c >= 0 and "
383 "c <= 3 and c >= -4a) }",
384 "{ [n] -> [i] : 0 <= i < n }" },
385 { "{ [x] -> [] : exists (a, b: 0 <= a <= 1 and 0 <= b <= 3 and "
386 "2b <= x - 8a and 2b >= -1 + x - 8a) }",
387 "{ [x] -> [] : 0 <= x <= 15 }" },
388 { "{ [x] -> [x] : }",
389 "{ [x] -> [x] }" },
392 int test_parse(struct isl_ctx *ctx)
394 int i;
395 isl_map *map, *map2;
396 const char *str, *str2;
398 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
399 return -1;
400 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
401 return -1;
402 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
403 return -1;
404 if (test_parse_multi(ctx) < 0)
405 return -1;
407 str = "{ [i] -> [-i] }";
408 map = isl_map_read_from_str(ctx, str);
409 assert(map);
410 isl_map_free(map);
412 str = "{ A[i] -> L[([i/3])] }";
413 map = isl_map_read_from_str(ctx, str);
414 assert(map);
415 isl_map_free(map);
417 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
418 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
419 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
421 for (i = 0; i < ARRAY_SIZE(parse_map_equal_tests); ++i) {
422 str = parse_map_equal_tests[i].map1;
423 str2 = parse_map_equal_tests[i].map2;
424 if (test_parse_map_equal(ctx, str, str2) < 0)
425 return -1;
428 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
429 map = isl_map_read_from_str(ctx, str);
430 str = "{ [new, old] -> [o0, o1] : "
431 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
432 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
433 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
434 map2 = isl_map_read_from_str(ctx, str);
435 assert(isl_map_is_equal(map, map2));
436 isl_map_free(map);
437 isl_map_free(map2);
439 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
440 map = isl_map_read_from_str(ctx, str);
441 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
442 map2 = isl_map_read_from_str(ctx, str);
443 assert(isl_map_is_equal(map, map2));
444 isl_map_free(map);
445 isl_map_free(map2);
447 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
448 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
449 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
450 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
451 test_parse_pwaff(ctx, "{ [] -> [(100)] }");
453 return 0;
456 static int test_read(isl_ctx *ctx)
458 char *filename;
459 FILE *input;
460 isl_basic_set *bset1, *bset2;
461 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
462 int equal;
464 filename = get_filename(ctx, "set", "omega");
465 assert(filename);
466 input = fopen(filename, "r");
467 assert(input);
469 bset1 = isl_basic_set_read_from_file(ctx, input);
470 bset2 = isl_basic_set_read_from_str(ctx, str);
472 equal = isl_basic_set_is_equal(bset1, bset2);
474 isl_basic_set_free(bset1);
475 isl_basic_set_free(bset2);
476 free(filename);
478 fclose(input);
480 if (equal < 0)
481 return -1;
482 if (!equal)
483 isl_die(ctx, isl_error_unknown,
484 "read sets not equal", return -1);
486 return 0;
489 static int test_bounded(isl_ctx *ctx)
491 isl_set *set;
492 isl_bool bounded;
494 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
495 bounded = isl_set_is_bounded(set);
496 isl_set_free(set);
498 if (bounded < 0)
499 return -1;
500 if (!bounded)
501 isl_die(ctx, isl_error_unknown,
502 "set not considered bounded", return -1);
504 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
505 bounded = isl_set_is_bounded(set);
506 assert(!bounded);
507 isl_set_free(set);
509 if (bounded < 0)
510 return -1;
511 if (bounded)
512 isl_die(ctx, isl_error_unknown,
513 "set considered bounded", return -1);
515 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
516 bounded = isl_set_is_bounded(set);
517 isl_set_free(set);
519 if (bounded < 0)
520 return -1;
521 if (bounded)
522 isl_die(ctx, isl_error_unknown,
523 "set considered bounded", return -1);
525 return 0;
528 /* Construct the basic set { [i] : 5 <= i <= N } */
529 static int test_construction_1(isl_ctx *ctx)
531 isl_space *dim;
532 isl_local_space *ls;
533 isl_basic_set *bset;
534 isl_constraint *c;
536 dim = isl_space_set_alloc(ctx, 1, 1);
537 bset = isl_basic_set_universe(isl_space_copy(dim));
538 ls = isl_local_space_from_space(dim);
540 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
541 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
542 c = isl_constraint_set_coefficient_si(c, isl_dim_param, 0, 1);
543 bset = isl_basic_set_add_constraint(bset, c);
545 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
546 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
547 c = isl_constraint_set_constant_si(c, -5);
548 bset = isl_basic_set_add_constraint(bset, c);
550 isl_local_space_free(ls);
551 isl_basic_set_free(bset);
553 return 0;
556 /* Construct the basic set { [x] : -100 <= x <= 100 }
557 * using isl_basic_set_{lower,upper}_bound_val and
558 * check that it is equal the same basic set parsed from a string.
560 static int test_construction_2(isl_ctx *ctx)
562 isl_bool equal;
563 isl_val *v;
564 isl_space *space;
565 isl_basic_set *bset1, *bset2;
567 v = isl_val_int_from_si(ctx, 100);
568 space = isl_space_set_alloc(ctx, 0, 1);
569 bset1 = isl_basic_set_universe(space);
570 bset1 = isl_basic_set_upper_bound_val(bset1, isl_dim_set, 0,
571 isl_val_copy(v));
572 bset1 = isl_basic_set_lower_bound_val(bset1, isl_dim_set, 0,
573 isl_val_neg(v));
574 bset2 = isl_basic_set_read_from_str(ctx, "{ [x] : -100 <= x <= 100 }");
575 equal = isl_basic_set_is_equal(bset1, bset2);
576 isl_basic_set_free(bset1);
577 isl_basic_set_free(bset2);
579 if (equal < 0)
580 return -1;
581 if (!equal)
582 isl_die(ctx, isl_error_unknown,
583 "failed construction", return -1);
585 return 0;
588 /* Basic tests for constructing basic sets.
590 static int test_construction(isl_ctx *ctx)
592 if (test_construction_1(ctx) < 0)
593 return -1;
594 if (test_construction_2(ctx) < 0)
595 return -1;
596 return 0;
599 static int test_dim(isl_ctx *ctx)
601 const char *str;
602 isl_map *map1, *map2;
603 int equal;
605 map1 = isl_map_read_from_str(ctx,
606 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
607 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
608 map2 = isl_map_read_from_str(ctx,
609 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
610 equal = isl_map_is_equal(map1, map2);
611 isl_map_free(map2);
613 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
614 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
615 if (equal >= 0 && equal)
616 equal = isl_map_is_equal(map1, map2);
618 isl_map_free(map1);
619 isl_map_free(map2);
621 if (equal < 0)
622 return -1;
623 if (!equal)
624 isl_die(ctx, isl_error_unknown,
625 "unexpected result", return -1);
627 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
628 map1 = isl_map_read_from_str(ctx, str);
629 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
630 map2 = isl_map_read_from_str(ctx, str);
631 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
632 equal = isl_map_is_equal(map1, map2);
633 isl_map_free(map1);
634 isl_map_free(map2);
636 if (equal < 0)
637 return -1;
638 if (!equal)
639 isl_die(ctx, isl_error_unknown,
640 "unexpected result", return -1);
642 return 0;
645 /* Check that "val" is equal to the value described by "str".
646 * If "str" is "NaN", then check for a NaN value explicitly.
648 static isl_stat val_check_equal(__isl_keep isl_val *val, const char *str)
650 isl_bool ok, is_nan;
651 isl_ctx *ctx;
652 isl_val *res;
654 if (!val)
655 return isl_stat_error;
657 ctx = isl_val_get_ctx(val);
658 res = isl_val_read_from_str(ctx, str);
659 is_nan = isl_val_is_nan(res);
660 if (is_nan < 0)
661 ok = isl_bool_error;
662 else if (is_nan)
663 ok = isl_val_is_nan(val);
664 else
665 ok = isl_val_eq(val, res);
666 isl_val_free(res);
667 if (ok < 0)
668 return isl_stat_error;
669 if (!ok)
670 isl_die(ctx, isl_error_unknown,
671 "unexpected result", return isl_stat_error);
672 return isl_stat_ok;
675 struct {
676 __isl_give isl_val *(*op)(__isl_take isl_val *v);
677 const char *arg;
678 const char *res;
679 } val_un_tests[] = {
680 { &isl_val_neg, "0", "0" },
681 { &isl_val_abs, "0", "0" },
682 { &isl_val_pow2, "0", "1" },
683 { &isl_val_floor, "0", "0" },
684 { &isl_val_ceil, "0", "0" },
685 { &isl_val_neg, "1", "-1" },
686 { &isl_val_neg, "-1", "1" },
687 { &isl_val_neg, "1/2", "-1/2" },
688 { &isl_val_neg, "-1/2", "1/2" },
689 { &isl_val_neg, "infty", "-infty" },
690 { &isl_val_neg, "-infty", "infty" },
691 { &isl_val_neg, "NaN", "NaN" },
692 { &isl_val_abs, "1", "1" },
693 { &isl_val_abs, "-1", "1" },
694 { &isl_val_abs, "1/2", "1/2" },
695 { &isl_val_abs, "-1/2", "1/2" },
696 { &isl_val_abs, "infty", "infty" },
697 { &isl_val_abs, "-infty", "infty" },
698 { &isl_val_abs, "NaN", "NaN" },
699 { &isl_val_floor, "1", "1" },
700 { &isl_val_floor, "-1", "-1" },
701 { &isl_val_floor, "1/2", "0" },
702 { &isl_val_floor, "-1/2", "-1" },
703 { &isl_val_floor, "infty", "infty" },
704 { &isl_val_floor, "-infty", "-infty" },
705 { &isl_val_floor, "NaN", "NaN" },
706 { &isl_val_ceil, "1", "1" },
707 { &isl_val_ceil, "-1", "-1" },
708 { &isl_val_ceil, "1/2", "1" },
709 { &isl_val_ceil, "-1/2", "0" },
710 { &isl_val_ceil, "infty", "infty" },
711 { &isl_val_ceil, "-infty", "-infty" },
712 { &isl_val_ceil, "NaN", "NaN" },
713 { &isl_val_pow2, "-3", "1/8" },
714 { &isl_val_pow2, "-1", "1/2" },
715 { &isl_val_pow2, "1", "2" },
716 { &isl_val_pow2, "2", "4" },
717 { &isl_val_pow2, "3", "8" },
718 { &isl_val_inv, "1", "1" },
719 { &isl_val_inv, "2", "1/2" },
720 { &isl_val_inv, "1/2", "2" },
721 { &isl_val_inv, "-2", "-1/2" },
722 { &isl_val_inv, "-1/2", "-2" },
723 { &isl_val_inv, "0", "NaN" },
724 { &isl_val_inv, "NaN", "NaN" },
725 { &isl_val_inv, "infty", "0" },
726 { &isl_val_inv, "-infty", "0" },
729 /* Perform some basic tests of unary operations on isl_val objects.
731 static int test_un_val(isl_ctx *ctx)
733 int i;
734 isl_val *v;
735 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
737 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
738 isl_stat r;
740 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
741 fn = val_un_tests[i].op;
742 v = fn(v);
743 r = val_check_equal(v, val_un_tests[i].res);
744 isl_val_free(v);
745 if (r < 0)
746 return -1;
749 return 0;
752 struct {
753 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
754 __isl_take isl_val *v2);
755 } val_bin_op[] = {
756 ['+'] = { &isl_val_add },
757 ['-'] = { &isl_val_sub },
758 ['*'] = { &isl_val_mul },
759 ['/'] = { &isl_val_div },
760 ['g'] = { &isl_val_gcd },
761 ['m'] = { &isl_val_min },
762 ['M'] = { &isl_val_max },
765 struct {
766 const char *arg1;
767 unsigned char op;
768 const char *arg2;
769 const char *res;
770 } val_bin_tests[] = {
771 { "0", '+', "0", "0" },
772 { "1", '+', "0", "1" },
773 { "1", '+', "1", "2" },
774 { "1", '-', "1", "0" },
775 { "1", '*', "1", "1" },
776 { "1", '/', "1", "1" },
777 { "2", '*', "3", "6" },
778 { "2", '*', "1/2", "1" },
779 { "2", '*', "1/3", "2/3" },
780 { "2/3", '*', "3/5", "2/5" },
781 { "2/3", '*', "7/5", "14/15" },
782 { "2", '/', "1/2", "4" },
783 { "-2", '/', "-1/2", "4" },
784 { "-2", '/', "1/2", "-4" },
785 { "2", '/', "-1/2", "-4" },
786 { "2", '/', "2", "1" },
787 { "2", '/', "3", "2/3" },
788 { "2/3", '/', "5/3", "2/5" },
789 { "2/3", '/', "5/7", "14/15" },
790 { "0", '/', "0", "NaN" },
791 { "42", '/', "0", "NaN" },
792 { "-42", '/', "0", "NaN" },
793 { "infty", '/', "0", "NaN" },
794 { "-infty", '/', "0", "NaN" },
795 { "NaN", '/', "0", "NaN" },
796 { "0", '/', "NaN", "NaN" },
797 { "42", '/', "NaN", "NaN" },
798 { "-42", '/', "NaN", "NaN" },
799 { "infty", '/', "NaN", "NaN" },
800 { "-infty", '/', "NaN", "NaN" },
801 { "NaN", '/', "NaN", "NaN" },
802 { "0", '/', "infty", "0" },
803 { "42", '/', "infty", "0" },
804 { "-42", '/', "infty", "0" },
805 { "infty", '/', "infty", "NaN" },
806 { "-infty", '/', "infty", "NaN" },
807 { "NaN", '/', "infty", "NaN" },
808 { "0", '/', "-infty", "0" },
809 { "42", '/', "-infty", "0" },
810 { "-42", '/', "-infty", "0" },
811 { "infty", '/', "-infty", "NaN" },
812 { "-infty", '/', "-infty", "NaN" },
813 { "NaN", '/', "-infty", "NaN" },
814 { "1", '-', "1/3", "2/3" },
815 { "1/3", '+', "1/2", "5/6" },
816 { "1/2", '+', "1/2", "1" },
817 { "3/4", '-', "1/4", "1/2" },
818 { "1/2", '-', "1/3", "1/6" },
819 { "infty", '+', "42", "infty" },
820 { "infty", '+', "infty", "infty" },
821 { "42", '+', "infty", "infty" },
822 { "infty", '-', "infty", "NaN" },
823 { "infty", '*', "infty", "infty" },
824 { "infty", '*', "-infty", "-infty" },
825 { "-infty", '*', "infty", "-infty" },
826 { "-infty", '*', "-infty", "infty" },
827 { "0", '*', "infty", "NaN" },
828 { "1", '*', "infty", "infty" },
829 { "infty", '*', "0", "NaN" },
830 { "infty", '*', "42", "infty" },
831 { "42", '-', "infty", "-infty" },
832 { "infty", '+', "-infty", "NaN" },
833 { "4", 'g', "6", "2" },
834 { "5", 'g', "6", "1" },
835 { "42", 'm', "3", "3" },
836 { "42", 'M', "3", "42" },
837 { "3", 'm', "42", "3" },
838 { "3", 'M', "42", "42" },
839 { "42", 'm', "infty", "42" },
840 { "42", 'M', "infty", "infty" },
841 { "42", 'm', "-infty", "-infty" },
842 { "42", 'M', "-infty", "42" },
843 { "42", 'm', "NaN", "NaN" },
844 { "42", 'M', "NaN", "NaN" },
845 { "infty", 'm', "-infty", "-infty" },
846 { "infty", 'M', "-infty", "infty" },
849 /* Perform some basic tests of binary operations on isl_val objects.
851 static int test_bin_val(isl_ctx *ctx)
853 int i;
854 isl_val *v1, *v2, *res;
855 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
856 __isl_take isl_val *v2);
857 int ok;
859 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
860 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
861 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
862 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
863 fn = val_bin_op[val_bin_tests[i].op].fn;
864 v1 = fn(v1, v2);
865 if (isl_val_is_nan(res))
866 ok = isl_val_is_nan(v1);
867 else
868 ok = isl_val_eq(v1, res);
869 isl_val_free(v1);
870 isl_val_free(res);
871 if (ok < 0)
872 return -1;
873 if (!ok)
874 isl_die(ctx, isl_error_unknown,
875 "unexpected result", return -1);
878 return 0;
881 /* Perform some basic tests on isl_val objects.
883 static int test_val(isl_ctx *ctx)
885 if (test_un_val(ctx) < 0)
886 return -1;
887 if (test_bin_val(ctx) < 0)
888 return -1;
889 return 0;
892 /* Sets described using existentially quantified variables that
893 * can also be described without.
895 static const char *elimination_tests[] = {
896 "{ [i,j] : 2 * [i/2] + 3 * [j/4] <= 10 and 2 i = j }",
897 "{ [m, w] : exists a : w - 2m - 5 <= 3a <= m - 2w }",
898 "{ [m, w] : exists a : w >= 0 and a < m and -1 + w <= a <= 2m - w }",
901 /* Check that redundant existentially quantified variables are
902 * getting removed.
904 static int test_elimination(isl_ctx *ctx)
906 int i;
907 isl_size n;
908 isl_basic_set *bset;
910 for (i = 0; i < ARRAY_SIZE(elimination_tests); ++i) {
911 bset = isl_basic_set_read_from_str(ctx, elimination_tests[i]);
912 n = isl_basic_set_dim(bset, isl_dim_div);
913 isl_basic_set_free(bset);
914 if (n < 0)
915 return -1;
916 if (n != 0)
917 isl_die(ctx, isl_error_unknown,
918 "expecting no existentials", return -1);
921 return 0;
924 static int test_div(isl_ctx *ctx)
926 const char *str;
927 int empty;
928 isl_space *dim;
929 isl_set *set;
930 isl_local_space *ls;
931 struct isl_basic_set *bset;
932 struct isl_constraint *c;
934 /* test 1 */
935 dim = isl_space_set_alloc(ctx, 0, 3);
936 bset = isl_basic_set_universe(isl_space_copy(dim));
937 ls = isl_local_space_from_space(dim);
939 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
940 c = isl_constraint_set_constant_si(c, -1);
941 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
942 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
943 bset = isl_basic_set_add_constraint(bset, c);
945 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
946 c = isl_constraint_set_constant_si(c, 1);
947 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
948 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
949 bset = isl_basic_set_add_constraint(bset, c);
951 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
953 assert(bset && bset->n_div == 1);
954 isl_local_space_free(ls);
955 isl_basic_set_free(bset);
957 /* test 2 */
958 dim = isl_space_set_alloc(ctx, 0, 3);
959 bset = isl_basic_set_universe(isl_space_copy(dim));
960 ls = isl_local_space_from_space(dim);
962 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
963 c = isl_constraint_set_constant_si(c, 1);
964 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
965 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
966 bset = isl_basic_set_add_constraint(bset, c);
968 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
969 c = isl_constraint_set_constant_si(c, -1);
970 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
971 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
972 bset = isl_basic_set_add_constraint(bset, c);
974 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
976 assert(bset && bset->n_div == 1);
977 isl_local_space_free(ls);
978 isl_basic_set_free(bset);
980 /* test 3 */
981 dim = isl_space_set_alloc(ctx, 0, 3);
982 bset = isl_basic_set_universe(isl_space_copy(dim));
983 ls = isl_local_space_from_space(dim);
985 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
986 c = isl_constraint_set_constant_si(c, 1);
987 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
988 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
989 bset = isl_basic_set_add_constraint(bset, c);
991 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
992 c = isl_constraint_set_constant_si(c, -3);
993 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
994 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 4);
995 bset = isl_basic_set_add_constraint(bset, c);
997 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
999 assert(bset && bset->n_div == 1);
1000 isl_local_space_free(ls);
1001 isl_basic_set_free(bset);
1003 /* test 4 */
1004 dim = isl_space_set_alloc(ctx, 0, 3);
1005 bset = isl_basic_set_universe(isl_space_copy(dim));
1006 ls = isl_local_space_from_space(dim);
1008 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1009 c = isl_constraint_set_constant_si(c, 2);
1010 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1011 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1012 bset = isl_basic_set_add_constraint(bset, c);
1014 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1015 c = isl_constraint_set_constant_si(c, -1);
1016 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1017 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
1018 bset = isl_basic_set_add_constraint(bset, c);
1020 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1022 assert(isl_basic_set_is_empty(bset));
1023 isl_local_space_free(ls);
1024 isl_basic_set_free(bset);
1026 /* test 5 */
1027 dim = isl_space_set_alloc(ctx, 0, 3);
1028 bset = isl_basic_set_universe(isl_space_copy(dim));
1029 ls = isl_local_space_from_space(dim);
1031 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1032 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1033 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
1034 bset = isl_basic_set_add_constraint(bset, c);
1036 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1037 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1038 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1039 bset = isl_basic_set_add_constraint(bset, c);
1041 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1043 assert(bset && bset->n_div == 0);
1044 isl_basic_set_free(bset);
1045 isl_local_space_free(ls);
1047 /* test 6 */
1048 dim = isl_space_set_alloc(ctx, 0, 3);
1049 bset = isl_basic_set_universe(isl_space_copy(dim));
1050 ls = isl_local_space_from_space(dim);
1052 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1053 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1054 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
1055 bset = isl_basic_set_add_constraint(bset, c);
1057 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1058 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1059 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1060 bset = isl_basic_set_add_constraint(bset, c);
1062 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1064 assert(bset && bset->n_div == 1);
1065 isl_basic_set_free(bset);
1066 isl_local_space_free(ls);
1068 /* test 7 */
1069 /* This test is a bit tricky. We set up an equality
1070 * a + 3b + 3c = 6 e0
1071 * Normalization of divs creates _two_ divs
1072 * a = 3 e0
1073 * c - b - e0 = 2 e1
1074 * Afterwards e0 is removed again because it has coefficient -1
1075 * and we end up with the original equality and div again.
1076 * Perhaps we can avoid the introduction of this temporary div.
1078 dim = isl_space_set_alloc(ctx, 0, 4);
1079 bset = isl_basic_set_universe(isl_space_copy(dim));
1080 ls = isl_local_space_from_space(dim);
1082 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1083 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1084 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1085 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -3);
1086 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 6);
1087 bset = isl_basic_set_add_constraint(bset, c);
1089 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
1091 /* Test disabled for now */
1093 assert(bset && bset->n_div == 1);
1095 isl_local_space_free(ls);
1096 isl_basic_set_free(bset);
1098 /* test 8 */
1099 dim = isl_space_set_alloc(ctx, 0, 5);
1100 bset = isl_basic_set_universe(isl_space_copy(dim));
1101 ls = isl_local_space_from_space(dim);
1103 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1104 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1105 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1106 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, -3);
1107 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 4, 6);
1108 bset = isl_basic_set_add_constraint(bset, c);
1110 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1111 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1112 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 1);
1113 c = isl_constraint_set_constant_si(c, 1);
1114 bset = isl_basic_set_add_constraint(bset, c);
1116 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
1118 /* Test disabled for now */
1120 assert(bset && bset->n_div == 1);
1122 isl_local_space_free(ls);
1123 isl_basic_set_free(bset);
1125 /* test 9 */
1126 dim = isl_space_set_alloc(ctx, 0, 4);
1127 bset = isl_basic_set_universe(isl_space_copy(dim));
1128 ls = isl_local_space_from_space(dim);
1130 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1131 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1132 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -1);
1133 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
1134 bset = isl_basic_set_add_constraint(bset, c);
1136 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1137 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1138 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 3);
1139 c = isl_constraint_set_constant_si(c, 2);
1140 bset = isl_basic_set_add_constraint(bset, c);
1142 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
1144 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1146 assert(!isl_basic_set_is_empty(bset));
1148 isl_local_space_free(ls);
1149 isl_basic_set_free(bset);
1151 /* test 10 */
1152 dim = isl_space_set_alloc(ctx, 0, 3);
1153 bset = isl_basic_set_universe(isl_space_copy(dim));
1154 ls = isl_local_space_from_space(dim);
1156 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1157 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1158 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
1159 bset = isl_basic_set_add_constraint(bset, c);
1161 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1163 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1165 isl_local_space_free(ls);
1166 isl_basic_set_free(bset);
1168 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1169 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
1170 set = isl_set_read_from_str(ctx, str);
1171 set = isl_set_compute_divs(set);
1172 isl_set_free(set);
1173 if (!set)
1174 return -1;
1176 if (test_elimination(ctx) < 0)
1177 return -1;
1179 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
1180 set = isl_set_read_from_str(ctx, str);
1181 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
1182 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
1183 empty = isl_set_is_empty(set);
1184 isl_set_free(set);
1185 if (empty < 0)
1186 return -1;
1187 if (!empty)
1188 isl_die(ctx, isl_error_unknown,
1189 "result not as accurate as expected", return -1);
1191 return 0;
1194 void test_application_case(struct isl_ctx *ctx, const char *name)
1196 char *filename;
1197 FILE *input;
1198 struct isl_basic_set *bset1, *bset2;
1199 struct isl_basic_map *bmap;
1201 filename = get_filename(ctx, name, "omega");
1202 assert(filename);
1203 input = fopen(filename, "r");
1204 assert(input);
1206 bset1 = isl_basic_set_read_from_file(ctx, input);
1207 bmap = isl_basic_map_read_from_file(ctx, input);
1209 bset1 = isl_basic_set_apply(bset1, bmap);
1211 bset2 = isl_basic_set_read_from_file(ctx, input);
1213 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1215 isl_basic_set_free(bset1);
1216 isl_basic_set_free(bset2);
1217 free(filename);
1219 fclose(input);
1222 static int test_application(isl_ctx *ctx)
1224 test_application_case(ctx, "application");
1225 test_application_case(ctx, "application2");
1227 return 0;
1230 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1232 char *filename;
1233 FILE *input;
1234 struct isl_basic_set *bset1, *bset2;
1236 filename = get_filename(ctx, name, "polylib");
1237 assert(filename);
1238 input = fopen(filename, "r");
1239 assert(input);
1241 bset1 = isl_basic_set_read_from_file(ctx, input);
1242 bset2 = isl_basic_set_read_from_file(ctx, input);
1244 bset1 = isl_basic_set_affine_hull(bset1);
1246 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1248 isl_basic_set_free(bset1);
1249 isl_basic_set_free(bset2);
1250 free(filename);
1252 fclose(input);
1255 int test_affine_hull(struct isl_ctx *ctx)
1257 const char *str;
1258 isl_set *set;
1259 isl_basic_set *bset, *bset2;
1260 isl_size n;
1261 isl_bool subset;
1263 test_affine_hull_case(ctx, "affine2");
1264 test_affine_hull_case(ctx, "affine");
1265 test_affine_hull_case(ctx, "affine3");
1267 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1268 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1269 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1270 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1271 set = isl_set_read_from_str(ctx, str);
1272 bset = isl_set_affine_hull(set);
1273 n = isl_basic_set_dim(bset, isl_dim_div);
1274 isl_basic_set_free(bset);
1275 if (n < 0)
1276 return -1;
1277 if (n != 0)
1278 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1279 return -1);
1281 /* Check that isl_map_affine_hull is not confused by
1282 * the reordering of divs in isl_map_align_divs.
1284 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1285 "32e0 = b and 32e1 = c); "
1286 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1287 set = isl_set_read_from_str(ctx, str);
1288 bset = isl_set_affine_hull(set);
1289 isl_basic_set_free(bset);
1290 if (!bset)
1291 return -1;
1293 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1294 "32e2 = 31 + 31e0 }";
1295 set = isl_set_read_from_str(ctx, str);
1296 bset = isl_set_affine_hull(set);
1297 str = "{ [a] : exists e : a = 32 e }";
1298 bset2 = isl_basic_set_read_from_str(ctx, str);
1299 subset = isl_basic_set_is_subset(bset, bset2);
1300 isl_basic_set_free(bset);
1301 isl_basic_set_free(bset2);
1302 if (subset < 0)
1303 return -1;
1304 if (!subset)
1305 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1306 return -1);
1308 return 0;
1311 /* Pairs of maps and the corresponding expected results of
1312 * isl_map_plain_unshifted_simple_hull.
1314 struct {
1315 const char *map;
1316 const char *hull;
1317 } plain_unshifted_simple_hull_tests[] = {
1318 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1319 "{ [i] -> [j] : i >= 1 }" },
1320 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1321 "(j mod 4 = 2 and k mod 6 = n) }",
1322 "{ [n] -> [i,j,k] : j mod 4 = 2 }" },
1325 /* Basic tests for isl_map_plain_unshifted_simple_hull.
1327 static int test_plain_unshifted_simple_hull(isl_ctx *ctx)
1329 int i;
1330 isl_map *map;
1331 isl_basic_map *hull, *expected;
1332 isl_bool equal;
1334 for (i = 0; i < ARRAY_SIZE(plain_unshifted_simple_hull_tests); ++i) {
1335 const char *str;
1336 str = plain_unshifted_simple_hull_tests[i].map;
1337 map = isl_map_read_from_str(ctx, str);
1338 str = plain_unshifted_simple_hull_tests[i].hull;
1339 expected = isl_basic_map_read_from_str(ctx, str);
1340 hull = isl_map_plain_unshifted_simple_hull(map);
1341 equal = isl_basic_map_is_equal(hull, expected);
1342 isl_basic_map_free(hull);
1343 isl_basic_map_free(expected);
1344 if (equal < 0)
1345 return -1;
1346 if (!equal)
1347 isl_die(ctx, isl_error_unknown, "unexpected hull",
1348 return -1);
1351 return 0;
1354 /* Pairs of sets and the corresponding expected results of
1355 * isl_set_unshifted_simple_hull.
1357 struct {
1358 const char *set;
1359 const char *hull;
1360 } unshifted_simple_hull_tests[] = {
1361 { "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1362 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1365 /* Basic tests for isl_set_unshifted_simple_hull.
1367 static int test_unshifted_simple_hull(isl_ctx *ctx)
1369 int i;
1370 isl_set *set;
1371 isl_basic_set *hull, *expected;
1372 isl_bool equal;
1374 for (i = 0; i < ARRAY_SIZE(unshifted_simple_hull_tests); ++i) {
1375 const char *str;
1376 str = unshifted_simple_hull_tests[i].set;
1377 set = isl_set_read_from_str(ctx, str);
1378 str = unshifted_simple_hull_tests[i].hull;
1379 expected = isl_basic_set_read_from_str(ctx, str);
1380 hull = isl_set_unshifted_simple_hull(set);
1381 equal = isl_basic_set_is_equal(hull, expected);
1382 isl_basic_set_free(hull);
1383 isl_basic_set_free(expected);
1384 if (equal < 0)
1385 return -1;
1386 if (!equal)
1387 isl_die(ctx, isl_error_unknown, "unexpected hull",
1388 return -1);
1391 return 0;
1394 static int test_simple_hull(struct isl_ctx *ctx)
1396 const char *str;
1397 isl_set *set;
1398 isl_basic_set *bset;
1399 isl_bool is_empty;
1401 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1402 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1403 set = isl_set_read_from_str(ctx, str);
1404 bset = isl_set_simple_hull(set);
1405 is_empty = isl_basic_set_is_empty(bset);
1406 isl_basic_set_free(bset);
1408 if (is_empty == isl_bool_error)
1409 return -1;
1411 if (is_empty == isl_bool_false)
1412 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1413 return -1);
1415 if (test_plain_unshifted_simple_hull(ctx) < 0)
1416 return -1;
1417 if (test_unshifted_simple_hull(ctx) < 0)
1418 return -1;
1420 return 0;
1423 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1425 char *filename;
1426 FILE *input;
1427 struct isl_basic_set *bset1, *bset2;
1428 struct isl_set *set;
1430 filename = get_filename(ctx, name, "polylib");
1431 assert(filename);
1432 input = fopen(filename, "r");
1433 assert(input);
1435 bset1 = isl_basic_set_read_from_file(ctx, input);
1436 bset2 = isl_basic_set_read_from_file(ctx, input);
1438 set = isl_basic_set_union(bset1, bset2);
1439 bset1 = isl_set_convex_hull(set);
1441 bset2 = isl_basic_set_read_from_file(ctx, input);
1443 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1445 isl_basic_set_free(bset1);
1446 isl_basic_set_free(bset2);
1447 free(filename);
1449 fclose(input);
1452 struct {
1453 const char *set;
1454 const char *hull;
1455 } convex_hull_tests[] = {
1456 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1457 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1458 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1459 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1460 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1461 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1462 "i2 <= 5 and i2 >= 4; "
1463 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1464 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1465 "i2 <= 5 + i0 and i2 >= i0 }" },
1466 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1467 "{ [x, y] : 1 = 0 }" },
1468 { "{ [x, y, z] : 0 <= x, y, z <= 10; [x, y, 0] : x >= 0 and y > 0; "
1469 "[x, y, 0] : x >= 0 and y < 0 }",
1470 "{ [x, y, z] : x >= 0 and 0 <= z <= 10 }" },
1473 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1475 int i;
1476 int orig_convex = ctx->opt->convex;
1477 ctx->opt->convex = convex;
1479 test_convex_hull_case(ctx, "convex0");
1480 test_convex_hull_case(ctx, "convex1");
1481 test_convex_hull_case(ctx, "convex2");
1482 test_convex_hull_case(ctx, "convex3");
1483 test_convex_hull_case(ctx, "convex4");
1484 test_convex_hull_case(ctx, "convex5");
1485 test_convex_hull_case(ctx, "convex6");
1486 test_convex_hull_case(ctx, "convex7");
1487 test_convex_hull_case(ctx, "convex8");
1488 test_convex_hull_case(ctx, "convex9");
1489 test_convex_hull_case(ctx, "convex10");
1490 test_convex_hull_case(ctx, "convex11");
1491 test_convex_hull_case(ctx, "convex12");
1492 test_convex_hull_case(ctx, "convex13");
1493 test_convex_hull_case(ctx, "convex14");
1494 test_convex_hull_case(ctx, "convex15");
1496 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1497 isl_set *set1, *set2;
1498 int equal;
1500 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1501 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1502 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1503 equal = isl_set_is_equal(set1, set2);
1504 isl_set_free(set1);
1505 isl_set_free(set2);
1507 if (equal < 0)
1508 return -1;
1509 if (!equal)
1510 isl_die(ctx, isl_error_unknown,
1511 "unexpected convex hull", return -1);
1514 ctx->opt->convex = orig_convex;
1516 return 0;
1519 static int test_convex_hull(isl_ctx *ctx)
1521 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1522 return -1;
1523 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1524 return -1;
1525 return 0;
1528 void test_gist_case(struct isl_ctx *ctx, const char *name)
1530 char *filename;
1531 FILE *input;
1532 struct isl_basic_set *bset1, *bset2;
1534 filename = get_filename(ctx, name, "polylib");
1535 assert(filename);
1536 input = fopen(filename, "r");
1537 assert(input);
1539 bset1 = isl_basic_set_read_from_file(ctx, input);
1540 bset2 = isl_basic_set_read_from_file(ctx, input);
1542 bset1 = isl_basic_set_gist(bset1, bset2);
1544 bset2 = isl_basic_set_read_from_file(ctx, input);
1546 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1548 isl_basic_set_free(bset1);
1549 isl_basic_set_free(bset2);
1550 free(filename);
1552 fclose(input);
1555 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1557 struct {
1558 const char *map;
1559 const char *context;
1560 const char *gist;
1561 } plain_gist_tests[] = {
1562 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1563 "{ [i] -> [j] : i >= 1 }",
1564 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1565 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1566 "(j mod 4 = 2 and k mod 6 = n) }",
1567 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1568 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1569 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1570 "{ [i] -> [j] : i > j }",
1571 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1574 /* Basic tests for isl_map_plain_gist_basic_map.
1576 static int test_plain_gist(isl_ctx *ctx)
1578 int i;
1580 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1581 const char *str;
1582 int equal;
1583 isl_map *map, *gist;
1584 isl_basic_map *context;
1586 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1587 str = plain_gist_tests[i].context;
1588 context = isl_basic_map_read_from_str(ctx, str);
1589 map = isl_map_plain_gist_basic_map(map, context);
1590 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1591 equal = isl_map_is_equal(map, gist);
1592 isl_map_free(map);
1593 isl_map_free(gist);
1594 if (equal < 0)
1595 return -1;
1596 if (!equal)
1597 isl_die(ctx, isl_error_unknown,
1598 "incorrect gist result", return -1);
1601 return 0;
1604 /* Inputs for isl_basic_set_gist tests that are expected to fail.
1606 struct {
1607 const char *set;
1608 const char *context;
1609 } gist_fail_tests[] = {
1610 { "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1611 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }",
1612 "{ [i] : i >= 0 }" },
1615 /* Check that isl_basic_set_gist fails (gracefully) when expected.
1616 * In particular, the user should be able to recover from the failure.
1618 static isl_stat test_gist_fail(struct isl_ctx *ctx)
1620 int i, n;
1621 int on_error;
1623 on_error = isl_options_get_on_error(ctx);
1624 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
1625 n = ARRAY_SIZE(gist_fail_tests);
1626 for (i = 0; i < n; ++i) {
1627 const char *str;
1628 isl_basic_set *bset, *context;
1630 bset = isl_basic_set_read_from_str(ctx, gist_fail_tests[i].set);
1631 str = gist_fail_tests[i].context;
1632 context = isl_basic_set_read_from_str(ctx, str);
1633 bset = isl_basic_set_gist(bset, context);
1634 isl_basic_set_free(bset);
1635 if (bset)
1636 break;
1638 isl_options_set_on_error(ctx, on_error);
1639 if (i < n)
1640 isl_die(ctx, isl_error_unknown,
1641 "operation not expected to succeed",
1642 return isl_stat_error);
1644 return isl_stat_ok;
1647 struct {
1648 const char *set;
1649 const char *context;
1650 const char *gist;
1651 } gist_tests[] = {
1652 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1653 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1654 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1655 "{ [a, b, c] : a <= 15 }" },
1656 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1657 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1658 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1659 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1660 { "{ [m, n, a, b] : a <= 2147 + n }",
1661 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1662 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1663 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1664 "b >= 0) }",
1665 "{ [m, n, ku, kl] }" },
1666 { "{ [a, a, b] : a >= 10 }",
1667 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1668 "{ [a, a, b] : a >= 10 }" },
1669 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1670 "{ [0, j] : j >= 0 }" },
1671 /* Check that no constraints on i6 are introduced in the gist */
1672 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1673 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1674 "5e0 <= 381 - t1 and i4 <= 1) }",
1675 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1676 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1677 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1678 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1679 "20e0 >= 1511 - 4t1 - 5i4) }" },
1680 /* Check that no constraints on i6 are introduced in the gist */
1681 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1682 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1683 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1684 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1685 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1686 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1687 "20e1 <= 1530 - 4t1 - 5i4 and "
1688 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1689 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1690 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1691 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1692 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1693 "10e0 <= -91 + 5i4 + 4i6 and "
1694 "10e0 >= -105 + 5i4 + 4i6) }",
1695 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1696 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1697 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1698 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1699 "{ [a, b, q, p] : b >= 1 + a }",
1700 "{ [a, b, q, p] : false }" },
1701 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1702 "[n] -> { [x] : x mod 32 = 0 }",
1703 "[n] -> { [x = n] }" },
1704 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1705 "{ [x] : x mod 2 = 0 }" },
1706 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1707 "{ [x] : x mod 128 = 0 }" },
1708 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1709 "{ [x] : x mod 3200 = 0 }" },
1710 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1711 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1712 "{ [a, b, c = a] }" },
1713 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1714 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1715 "{ [a, b, c = a] : a mod 3 = 0 }" },
1716 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1717 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1718 "{ [x] }" },
1719 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1720 "{ [x,y] : 1 <= y <= 3 }",
1721 "{ [x,y] }" },
1724 /* Check that isl_set_gist behaves as expected.
1726 * For the test cases in gist_tests, besides checking that the result
1727 * is as expected, also check that applying the gist operation does
1728 * not modify the input set (an earlier version of isl would do that) and
1729 * that the test case is consistent, i.e., that the gist has the same
1730 * intersection with the context as the input set.
1732 static int test_gist(struct isl_ctx *ctx)
1734 int i;
1735 const char *str;
1736 isl_basic_set *bset1, *bset2;
1737 isl_map *map1, *map2;
1738 isl_bool equal;
1739 isl_size n_div;
1741 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1742 isl_bool equal_input, equal_intersection;
1743 isl_set *set1, *set2, *copy, *context;
1745 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1746 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1747 copy = isl_set_copy(set1);
1748 set1 = isl_set_gist(set1, isl_set_copy(context));
1749 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1750 equal = isl_set_is_equal(set1, set2);
1751 isl_set_free(set1);
1752 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1753 equal_input = isl_set_is_equal(set1, copy);
1754 isl_set_free(copy);
1755 set1 = isl_set_intersect(set1, isl_set_copy(context));
1756 set2 = isl_set_intersect(set2, context);
1757 equal_intersection = isl_set_is_equal(set1, set2);
1758 isl_set_free(set2);
1759 isl_set_free(set1);
1760 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1761 return -1;
1762 if (!equal)
1763 isl_die(ctx, isl_error_unknown,
1764 "incorrect gist result", return -1);
1765 if (!equal_input)
1766 isl_die(ctx, isl_error_unknown,
1767 "gist modified input", return -1);
1768 if (!equal_input)
1769 isl_die(ctx, isl_error_unknown,
1770 "inconsistent gist test case", return -1);
1773 if (test_gist_fail(ctx) < 0)
1774 return -1;
1776 test_gist_case(ctx, "gist1");
1778 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1779 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1780 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1781 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1782 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1783 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1784 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1785 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1786 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1787 bset1 = isl_basic_set_read_from_str(ctx, str);
1788 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1789 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1790 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1791 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1792 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1793 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1794 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1795 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1796 bset2 = isl_basic_set_read_from_str(ctx, str);
1797 bset1 = isl_basic_set_gist(bset1, bset2);
1798 assert(bset1 && bset1->n_div == 0);
1799 isl_basic_set_free(bset1);
1801 /* Check that the integer divisions of the second disjunct
1802 * do not spread to the first disjunct.
1804 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1805 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1806 "(exists (e0 = [(-1 + t1)/16], "
1807 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1808 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1809 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1810 "o0 <= 4294967295 and t1 <= -1)) }";
1811 map1 = isl_map_read_from_str(ctx, str);
1812 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1813 map2 = isl_map_read_from_str(ctx, str);
1814 map1 = isl_map_gist(map1, map2);
1815 if (!map1)
1816 return -1;
1817 if (map1->n != 1)
1818 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1819 isl_map_free(map1); return -1);
1820 n_div = isl_basic_map_dim(map1->p[0], isl_dim_div);
1821 isl_map_free(map1);
1822 if (n_div < 0)
1823 return -1;
1824 if (n_div != 1)
1825 isl_die(ctx, isl_error_unknown, "expecting single div",
1826 return -1);
1828 if (test_plain_gist(ctx) < 0)
1829 return -1;
1831 return 0;
1834 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1836 isl_set *set, *set2;
1837 int equal;
1838 int one;
1840 set = isl_set_read_from_str(ctx, str);
1841 set = isl_set_coalesce(set);
1842 set2 = isl_set_read_from_str(ctx, str);
1843 equal = isl_set_is_equal(set, set2);
1844 one = set && set->n == 1;
1845 isl_set_free(set);
1846 isl_set_free(set2);
1848 if (equal < 0)
1849 return -1;
1850 if (!equal)
1851 isl_die(ctx, isl_error_unknown,
1852 "coalesced set not equal to input", return -1);
1853 if (check_one && !one)
1854 isl_die(ctx, isl_error_unknown,
1855 "coalesced set should not be a union", return -1);
1857 return 0;
1860 /* Inputs for coalescing tests with unbounded wrapping.
1861 * "str" is a string representation of the input set.
1862 * "single_disjunct" is set if we expect the result to consist of
1863 * a single disjunct.
1865 struct {
1866 int single_disjunct;
1867 const char *str;
1868 } coalesce_unbounded_tests[] = {
1869 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1870 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1871 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1872 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1873 "-10 <= y <= 0}" },
1874 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1875 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1876 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1877 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1880 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1881 * option turned off.
1883 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1885 int i;
1886 int r = 0;
1887 int bounded;
1889 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1890 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1892 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1893 const char *str = coalesce_unbounded_tests[i].str;
1894 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1895 if (test_coalesce_set(ctx, str, check_one) >= 0)
1896 continue;
1897 r = -1;
1898 break;
1901 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1903 return r;
1906 /* Inputs for coalescing tests.
1907 * "str" is a string representation of the input set.
1908 * "single_disjunct" is set if we expect the result to consist of
1909 * a single disjunct.
1911 struct {
1912 int single_disjunct;
1913 const char *str;
1914 } coalesce_tests[] = {
1915 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1916 "y >= x & x >= 2 & 5 >= y }" },
1917 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1918 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1919 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1920 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1921 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1922 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1923 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1924 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1925 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1926 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1927 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1928 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1929 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1930 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1931 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1932 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1933 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1934 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1935 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1936 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1937 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1938 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1939 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1940 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1941 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1942 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1943 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1944 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1945 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1946 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1947 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1948 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1949 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1950 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1951 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1952 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1953 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1954 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1955 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1956 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1957 "[o0, o1, o2, o3, o4, o5, o6]] : "
1958 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1959 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1960 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1961 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1962 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1963 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1964 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1965 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1966 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1967 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1968 "o6 >= i3 + i6 - o3 and M >= 0 and "
1969 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1970 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1971 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1972 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1973 "(o0 = 0 and M >= 2 and N >= 3) or "
1974 "(M = 0 and o0 = 0 and N >= 3) }" },
1975 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1976 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1977 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1978 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1979 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1980 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1981 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1982 "(y = 3 and x = 1) }" },
1983 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1984 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1985 "i1 <= M and i3 <= M and i4 <= M) or "
1986 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1987 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1988 "i4 <= -1 + M) }" },
1989 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1990 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1991 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1992 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1993 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1994 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1995 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1996 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1997 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1998 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1999 { 0, "{ [a, b] : exists e : 2e = a and "
2000 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
2001 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
2002 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
2003 "j >= 1 and j' <= i + j - i' and i >= 1; "
2004 "[1, 1, 1, 1] }" },
2005 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
2006 "[i,j] : exists a : j = 3a }" },
2007 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
2008 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
2009 "a >= 3) or "
2010 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
2011 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
2012 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
2013 "c <= 6 + 8a and a >= 3; "
2014 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
2015 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
2016 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2017 "[x,0] : 3 <= x <= 4 }" },
2018 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
2019 "[x,0] : 4 <= x <= 5 }" },
2020 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2021 "[x,0] : 3 <= x <= 5 }" },
2022 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
2023 "[x,0] : 3 <= x <= 4 }" },
2024 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
2025 "i1 <= 0; "
2026 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
2027 { 1, "{ [0,0]; [1,1] }" },
2028 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
2029 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
2030 "ii <= k;"
2031 "[k, 0, k] : k <= 6 and k >= 1 }" },
2032 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
2033 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
2034 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
2035 { 1, "[n] -> { [1] : n >= 0;"
2036 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
2037 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
2038 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
2039 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
2040 "2e0 <= x and 2e0 <= n);"
2041 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
2042 "n >= 0) }" },
2043 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
2044 "128e0 >= -134 + 127t1 and t1 >= 2 and "
2045 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
2046 "t1 = 1 }" },
2047 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
2048 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
2049 "[0, 0] }" },
2050 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
2051 "t1 >= 13 and t1 <= 16);"
2052 "[t1] : t1 <= 15 and t1 >= 12 }" },
2053 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
2054 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
2055 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
2056 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
2057 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
2058 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
2059 "i <= 4j + 2 }" },
2060 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
2061 "(exists (e0 : 3e0 = -2 + c0)) }" },
2062 { 0, "[n, b0, t0] -> "
2063 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2064 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2065 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2066 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2067 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
2068 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
2069 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
2070 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
2071 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2072 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2073 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2074 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
2075 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
2076 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
2077 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
2078 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
2079 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
2080 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
2081 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
2082 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
2083 { 0, "{ [i0, i1, i2] : "
2084 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
2085 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
2086 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
2087 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
2088 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
2089 "32e0 <= 31 + i0)) or "
2090 "i0 >= 0 }" },
2091 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
2092 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
2093 "2*floor((c)/2) = c and 0 <= a <= 192;"
2094 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
2096 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
2097 "(0 <= a <= b <= n) }" },
2098 { 1, "{ [a, b] : 0 <= a <= 2 and b >= 0 and "
2099 "((0 < b <= 13) or (2*floor((a + b)/2) >= -5 + a + 2b)) }" },
2100 { 1, "{ [a] : (2 <= a <= 5) or (a mod 2 = 1 and 1 <= a <= 5) }" },
2101 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2102 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2103 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2104 "b = 3 and 9e0 <= -19 + 2c)) }" },
2105 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2106 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2107 "(a = 4 and b = 3 and "
2108 "9*floor((-16 + 2c)/9) <= -19 + 2c) }" },
2109 { 0, "{ [a, b, c] : (b <= 2 and b <= -2 + a) or "
2110 "(b = -1 + a and 0 < a <= 3 and "
2111 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2112 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2113 "b = 3 and 9e0 <= -19 + 2c)) }" },
2114 { 1, "{ [y, x] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2115 "[1, 0] }" },
2116 { 1, "{ [x, y] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2117 "[0, 1] }" },
2118 { 1, "{ [1, y] : -1 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2119 { 1, "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2120 { 1, "{ [x, y] : 0 <= x <= 10 and x - 4*floor(x/4) <= 1 and y <= 0; "
2121 "[x, y] : 0 <= x <= 10 and x - 4*floor(x/4) > 1 and y <= 0; "
2122 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) <= 1 and 0 < y; "
2123 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) > 1 and 0 < y }" },
2124 { 1, "{ [x, 0] : 0 <= x <= 10 and x mod 2 = 0; "
2125 "[x, 0] : 0 <= x <= 10 and x mod 2 = 1; "
2126 "[x, y] : 0 <= x <= 10 and 1 <= y <= 10 }" },
2127 { 1, "{ [a] : a <= 8 and "
2128 "(a mod 10 = 7 or a mod 10 = 8 or a mod 10 = 9) }" },
2129 { 1, "{ [x, y] : 2y = -x and x <= 0 or "
2130 "x <= -1 and 2y <= -x - 1 and 2y >= x - 1 }" },
2131 { 0, "{ [x, y] : 2y = -x and x <= 0 or "
2132 "x <= -2 and 2y <= -x - 1 and 2y >= x - 1 }" },
2133 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2134 "(a < 0 and 3*floor((a)/3) < a) }" },
2135 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2136 "(a < -1 and 3*floor((a)/3) < a) }" },
2137 { 1, "{ [a, b] : a <= 1024 and b >= 0 and "
2138 "((-31 - a + b <= 32*floor((-1 - a)/32) <= -33 + b and "
2139 "32*floor((-1 - a)/32) <= -16 + b + 16*floor((-1 - a)/16))"
2140 "or (2 <= a <= 15 and b < a)) }" },
2141 { 1, "{ [a] : a > 0 and ((16*floor((a)/16) < a and "
2142 "32*floor((a)/32) < a) or a <= 15) }" },
2143 { 1, "{ [a, b, c, d] : (-a + d) mod 64 = 0 and a <= 8 and b <= 1 and "
2144 "10 - a <= c <= 3 and d >= 5 and 9 - 64b <= d <= 70;"
2145 "[a, b = 1, c, d] : (-a + d) mod 64 = 0 and a <= 8 and c >= 4 and "
2146 "10 - a <= c <= 5 and 5 <= d <= 73 - c }" },
2147 { 1, "[n, m] -> { S_0[i] : (-n + i) mod 3 = 0 and m >= 3 + n and "
2148 "i >= n and 3*floor((2 + n + 2m)/3) <= n + 3m - i; "
2149 "S_0[n] : n <= m <= 2 + n }" },
2150 { 1, "{ [a, b] : exists (e0: 0 <= a <= 1 and b >= 0 and "
2151 "2e0 >= -5 + a + 2b and 2e0 >= -1 + a + b and "
2152 "2e0 <= a + b); "
2153 "[a, b] : exists (e0: 0 <= a <= 1 and 2e0 >= -5 + a + 2b and "
2154 "2e0 >= -1 - a + b and 2e0 <= -a + b and "
2155 "2e0 < -a + 2b) }" },
2156 { 1, "{ [i, j, i - 8j] : 8 <= i <= 63 and -7 + i <= 8j <= i; "
2157 "[i, 0, i] : 0 <= i <= 7 }" },
2158 { 1, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [1, 1] }" },
2159 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [0, 2] }" },
2160 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [-1, 3] }" },
2161 { 1, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [1, 1] }" },
2162 { 0, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [2, 1] }" },
2163 { 0, "{ [a, c] : (2 + a) mod 4 = 0 or "
2164 "(c = 4 + a and 4 * floor((a)/4) = a and a >= 0 and a <= 4) or "
2165 "(c = 3 + a and 4 * floor((-1 + a)/4) = -1 + a and "
2166 "a > 0 and a <= 5) }" },
2167 { 1, "{ [1, 0, 0]; [a, b, c] : -1 <= -a < b <= 0 and 2c > b }" },
2170 /* A specialized coalescing test case that would result
2171 * in a segmentation fault or a failed assertion in earlier versions of isl.
2173 static int test_coalesce_special(struct isl_ctx *ctx)
2175 const char *str;
2176 isl_map *map1, *map2;
2178 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2179 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
2180 "(y = 201 and o1 <= 239 and o1 >= 212) or "
2181 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
2182 "o1 <= 239 and o1 >= 212)) or "
2183 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
2184 "o1 <= 241 and o1 >= 240));"
2185 "[S_L220_OUT[] -> T7[]] -> "
2186 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
2187 "(y = 2 and o1 <= 241 and o1 >= 212) or "
2188 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
2189 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
2190 map1 = isl_map_read_from_str(ctx, str);
2191 map1 = isl_map_align_divs_internal(map1);
2192 map1 = isl_map_coalesce(map1);
2193 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2194 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
2195 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
2196 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
2197 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
2198 map2 = isl_map_read_from_str(ctx, str);
2199 map2 = isl_map_union(map2, map1);
2200 map2 = isl_map_align_divs_internal(map2);
2201 map2 = isl_map_coalesce(map2);
2202 isl_map_free(map2);
2203 if (!map2)
2204 return -1;
2206 return 0;
2209 /* A specialized coalescing test case that would result in an assertion
2210 * in an earlier version of isl.
2211 * The explicit call to isl_basic_set_union prevents the implicit
2212 * equality constraints in the first basic map from being detected prior
2213 * to the call to isl_set_coalesce, at least at the point
2214 * where this test case was introduced.
2216 static int test_coalesce_special2(struct isl_ctx *ctx)
2218 const char *str;
2219 isl_basic_set *bset1, *bset2;
2220 isl_set *set;
2222 str = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
2223 bset1 = isl_basic_set_read_from_str(ctx, str);
2224 str = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }" ;
2225 bset2 = isl_basic_set_read_from_str(ctx, str);
2226 set = isl_basic_set_union(bset1, bset2);
2227 set = isl_set_coalesce(set);
2228 isl_set_free(set);
2230 if (!set)
2231 return -1;
2232 return 0;
2235 /* Check that calling isl_set_coalesce does not leave other sets
2236 * that may share some information with the input to isl_set_coalesce
2237 * in an inconsistent state.
2238 * In particular, older versions of isl would modify all copies
2239 * of the basic sets in the isl_set_coalesce input in a way
2240 * that could leave them in an inconsistent state.
2241 * The result of printing any other set containing one of these
2242 * basic sets would then result in an invalid set description.
2244 static int test_coalesce_special3(isl_ctx *ctx)
2246 const char *str;
2247 char *s;
2248 isl_set *set1, *set2;
2249 isl_printer *p;
2251 set1 = isl_set_read_from_str(ctx, "{ [0, 0, 0] }");
2252 str = "{ [a, b, a + b] : a >= 0 and b >= 0 and 0 < a + b }";
2253 set2 = isl_set_read_from_str(ctx, str);
2254 set1 = isl_set_union(set1, isl_set_copy(set2));
2255 set1 = isl_set_coalesce(set1);
2256 isl_set_free(set1);
2258 p = isl_printer_to_str(ctx);
2259 p = isl_printer_print_set(p, set2);
2260 isl_set_free(set2);
2261 s = isl_printer_get_str(p);
2262 isl_printer_free(p);
2263 set1 = isl_set_read_from_str(ctx, s);
2264 free(s);
2265 isl_set_free(set1);
2267 if (!set1)
2268 return -1;
2270 return 0;
2273 /* Check that calling isl_set_coalesce on the intersection of
2274 * the sets described by "s1" and "s2" does not leave other sets
2275 * that may share some information with the input to isl_set_coalesce
2276 * in an inconsistent state.
2277 * In particular, when isl_set_coalesce detects equality constraints,
2278 * it does not immediately perform Gaussian elimination on them,
2279 * but then it needs to ensure that it is performed at some point.
2280 * The input set has implicit equality constraints in the first disjunct.
2281 * It is constructed as an intersection, because otherwise
2282 * those equality constraints would already be detected during parsing.
2284 static isl_stat test_coalesce_intersection(isl_ctx *ctx,
2285 const char *s1, const char *s2)
2287 isl_set *set1, *set2;
2289 set1 = isl_set_read_from_str(ctx, s1);
2290 set2 = isl_set_read_from_str(ctx, s2);
2291 set1 = isl_set_intersect(set1, set2);
2292 isl_set_free(isl_set_coalesce(isl_set_copy(set1)));
2293 set1 = isl_set_coalesce(set1);
2294 isl_set_free(set1);
2296 if (!set1)
2297 return isl_stat_error;
2299 return isl_stat_ok;
2302 /* Check that calling isl_set_coalesce does not leave other sets
2303 * that may share some information with the input to isl_set_coalesce
2304 * in an inconsistent state, for the case where one disjunct
2305 * is a subset of the other.
2307 static isl_stat test_coalesce_special4(isl_ctx *ctx)
2309 const char *s1, *s2;
2311 s1 = "{ [a, b] : b <= 0 or a <= 1 }";
2312 s2 = "{ [a, b] : -1 <= -a < b }";
2313 return test_coalesce_intersection(ctx, s1, s2);
2316 /* Check that calling isl_set_coalesce does not leave other sets
2317 * that may share some information with the input to isl_set_coalesce
2318 * in an inconsistent state, for the case where two disjuncts
2319 * can be fused.
2321 static isl_stat test_coalesce_special5(isl_ctx *ctx)
2323 const char *s1, *s2;
2325 s1 = "{ [a, b, c] : b <= 0 }";
2326 s2 = "{ [a, b, c] : -1 <= -a < b and (c >= 0 or c < 0) }";
2327 return test_coalesce_intersection(ctx, s1, s2);
2330 /* Test the functionality of isl_set_coalesce.
2331 * That is, check that the output is always equal to the input
2332 * and in some cases that the result consists of a single disjunct.
2334 static int test_coalesce(struct isl_ctx *ctx)
2336 int i;
2338 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
2339 const char *str = coalesce_tests[i].str;
2340 int check_one = coalesce_tests[i].single_disjunct;
2341 if (test_coalesce_set(ctx, str, check_one) < 0)
2342 return -1;
2345 if (test_coalesce_unbounded_wrapping(ctx) < 0)
2346 return -1;
2347 if (test_coalesce_special(ctx) < 0)
2348 return -1;
2349 if (test_coalesce_special2(ctx) < 0)
2350 return -1;
2351 if (test_coalesce_special3(ctx) < 0)
2352 return -1;
2353 if (test_coalesce_special4(ctx) < 0)
2354 return -1;
2355 if (test_coalesce_special5(ctx) < 0)
2356 return -1;
2358 return 0;
2361 /* Construct a representation of the graph on the right of Figure 1
2362 * in "Computing the Transitive Closure of a Union of
2363 * Affine Integer Tuple Relations".
2365 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2367 isl_set *dom;
2368 isl_map *up, *right;
2370 dom = isl_set_read_from_str(ctx,
2371 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2372 "2 x - 3 y + 3 >= 0 }");
2373 right = isl_map_read_from_str(ctx,
2374 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2375 up = isl_map_read_from_str(ctx,
2376 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2377 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2378 right = isl_map_intersect_range(right, isl_set_copy(dom));
2379 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2380 up = isl_map_intersect_range(up, dom);
2381 return isl_map_union(up, right);
2384 /* Construct a representation of the power of the graph
2385 * on the right of Figure 1 in "Computing the Transitive Closure of
2386 * a Union of Affine Integer Tuple Relations".
2388 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2390 return isl_map_read_from_str(ctx,
2391 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2392 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2393 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2396 /* Construct a representation of the transitive closure of the graph
2397 * on the right of Figure 1 in "Computing the Transitive Closure of
2398 * a Union of Affine Integer Tuple Relations".
2400 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2402 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2405 static int test_closure(isl_ctx *ctx)
2407 const char *str;
2408 isl_map *map, *map2;
2409 int exact, equal;
2411 /* COCOA example 1 */
2412 map = isl_map_read_from_str(ctx,
2413 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2414 "1 <= i and i < n and 1 <= j and j < n or "
2415 "i2 = i + 1 and j2 = j - 1 and "
2416 "1 <= i and i < n and 2 <= j and j <= n }");
2417 map = isl_map_power(map, &exact);
2418 assert(exact);
2419 isl_map_free(map);
2421 /* COCOA example 1 */
2422 map = isl_map_read_from_str(ctx,
2423 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2424 "1 <= i and i < n and 1 <= j and j < n or "
2425 "i2 = i + 1 and j2 = j - 1 and "
2426 "1 <= i and i < n and 2 <= j and j <= n }");
2427 map = isl_map_transitive_closure(map, &exact);
2428 assert(exact);
2429 map2 = isl_map_read_from_str(ctx,
2430 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2431 "1 <= i and i < n and 1 <= j and j <= n and "
2432 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2433 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2434 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2435 assert(isl_map_is_equal(map, map2));
2436 isl_map_free(map2);
2437 isl_map_free(map);
2439 map = isl_map_read_from_str(ctx,
2440 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2441 " 0 <= y and y <= n }");
2442 map = isl_map_transitive_closure(map, &exact);
2443 map2 = isl_map_read_from_str(ctx,
2444 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2445 " 0 <= y and y <= n }");
2446 assert(isl_map_is_equal(map, map2));
2447 isl_map_free(map2);
2448 isl_map_free(map);
2450 /* COCOA example 2 */
2451 map = isl_map_read_from_str(ctx,
2452 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2453 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2454 "i2 = i + 2 and j2 = j - 2 and "
2455 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2456 map = isl_map_transitive_closure(map, &exact);
2457 assert(exact);
2458 map2 = isl_map_read_from_str(ctx,
2459 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2460 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2461 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2462 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2463 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2464 assert(isl_map_is_equal(map, map2));
2465 isl_map_free(map);
2466 isl_map_free(map2);
2468 /* COCOA Fig.2 left */
2469 map = isl_map_read_from_str(ctx,
2470 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2471 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2472 "j <= n or "
2473 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2474 "j <= 2 i - 3 and j <= n - 2 or "
2475 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2476 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2477 map = isl_map_transitive_closure(map, &exact);
2478 assert(exact);
2479 isl_map_free(map);
2481 /* COCOA Fig.2 right */
2482 map = isl_map_read_from_str(ctx,
2483 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2484 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2485 "j <= n or "
2486 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2487 "j <= 2 i - 4 and j <= n - 3 or "
2488 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2489 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2490 map = isl_map_power(map, &exact);
2491 assert(exact);
2492 isl_map_free(map);
2494 /* COCOA Fig.2 right */
2495 map = isl_map_read_from_str(ctx,
2496 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2497 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2498 "j <= n or "
2499 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2500 "j <= 2 i - 4 and j <= n - 3 or "
2501 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2502 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2503 map = isl_map_transitive_closure(map, &exact);
2504 assert(exact);
2505 map2 = isl_map_read_from_str(ctx,
2506 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2507 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2508 "j <= n and 3 + i + 2 j <= 3 n and "
2509 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2510 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2511 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2512 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2513 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2514 assert(isl_map_is_equal(map, map2));
2515 isl_map_free(map2);
2516 isl_map_free(map);
2518 map = cocoa_fig_1_right_graph(ctx);
2519 map = isl_map_transitive_closure(map, &exact);
2520 assert(exact);
2521 map2 = cocoa_fig_1_right_tc(ctx);
2522 assert(isl_map_is_equal(map, map2));
2523 isl_map_free(map2);
2524 isl_map_free(map);
2526 map = cocoa_fig_1_right_graph(ctx);
2527 map = isl_map_power(map, &exact);
2528 map2 = cocoa_fig_1_right_power(ctx);
2529 equal = isl_map_is_equal(map, map2);
2530 isl_map_free(map2);
2531 isl_map_free(map);
2532 if (equal < 0)
2533 return -1;
2534 if (!exact)
2535 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2536 if (!equal)
2537 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2539 /* COCOA Theorem 1 counter example */
2540 map = isl_map_read_from_str(ctx,
2541 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2542 "i2 = 1 and j2 = j or "
2543 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2544 map = isl_map_transitive_closure(map, &exact);
2545 assert(exact);
2546 isl_map_free(map);
2548 map = isl_map_read_from_str(ctx,
2549 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2550 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2551 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2552 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2553 map = isl_map_transitive_closure(map, &exact);
2554 assert(exact);
2555 isl_map_free(map);
2557 /* Kelly et al 1996, fig 12 */
2558 map = isl_map_read_from_str(ctx,
2559 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2560 "1 <= i,j,j+1 <= n or "
2561 "j = n and j2 = 1 and i2 = i + 1 and "
2562 "1 <= i,i+1 <= n }");
2563 map = isl_map_transitive_closure(map, &exact);
2564 assert(exact);
2565 map2 = isl_map_read_from_str(ctx,
2566 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2567 "1 <= i <= n and i = i2 or "
2568 "1 <= i < i2 <= n and 1 <= j <= n and "
2569 "1 <= j2 <= n }");
2570 assert(isl_map_is_equal(map, map2));
2571 isl_map_free(map2);
2572 isl_map_free(map);
2574 /* Omega's closure4 */
2575 map = isl_map_read_from_str(ctx,
2576 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2577 "1 <= x,y <= 10 or "
2578 "x2 = x + 1 and y2 = y and "
2579 "1 <= x <= 20 && 5 <= y <= 15 }");
2580 map = isl_map_transitive_closure(map, &exact);
2581 assert(exact);
2582 isl_map_free(map);
2584 map = isl_map_read_from_str(ctx,
2585 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2586 map = isl_map_transitive_closure(map, &exact);
2587 assert(!exact);
2588 map2 = isl_map_read_from_str(ctx,
2589 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2590 assert(isl_map_is_equal(map, map2));
2591 isl_map_free(map);
2592 isl_map_free(map2);
2594 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2595 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2596 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2597 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2598 map = isl_map_read_from_str(ctx, str);
2599 map = isl_map_transitive_closure(map, &exact);
2600 assert(exact);
2601 map2 = isl_map_read_from_str(ctx, str);
2602 assert(isl_map_is_equal(map, map2));
2603 isl_map_free(map);
2604 isl_map_free(map2);
2606 str = "{[0] -> [1]; [2] -> [3]}";
2607 map = isl_map_read_from_str(ctx, str);
2608 map = isl_map_transitive_closure(map, &exact);
2609 assert(exact);
2610 map2 = isl_map_read_from_str(ctx, str);
2611 assert(isl_map_is_equal(map, map2));
2612 isl_map_free(map);
2613 isl_map_free(map2);
2615 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2616 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2617 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2618 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2619 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2620 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2621 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2622 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2623 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2624 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2625 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2626 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2627 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2628 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2629 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2630 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2631 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2632 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2633 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2634 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2635 map = isl_map_read_from_str(ctx, str);
2636 map = isl_map_transitive_closure(map, NULL);
2637 assert(map);
2638 isl_map_free(map);
2640 return 0;
2643 /* Check that the actual result of a boolean operation is equal
2644 * to the expected result.
2646 static isl_stat check_bool(isl_ctx *ctx, isl_bool actual, isl_bool expected)
2648 if (actual != expected)
2649 isl_die(ctx, isl_error_unknown,
2650 "incorrect boolean operation", return isl_stat_error);
2651 return isl_stat_ok;
2654 /* Test operations on isl_bool values.
2656 * This tests:
2658 * isl_bool_not
2659 * isl_bool_ok
2661 static int test_isl_bool(isl_ctx *ctx)
2663 if (check_bool(ctx, isl_bool_not(isl_bool_true), isl_bool_false) < 0)
2664 return -1;
2665 if (check_bool(ctx, isl_bool_not(isl_bool_false), isl_bool_true) < 0)
2666 return -1;
2667 if (check_bool(ctx, isl_bool_not(isl_bool_error), isl_bool_error) < 0)
2668 return -1;
2669 if (check_bool(ctx, isl_bool_ok(0), isl_bool_false) < 0)
2670 return -1;
2671 if (check_bool(ctx, isl_bool_ok(1), isl_bool_true) < 0)
2672 return -1;
2673 if (check_bool(ctx, isl_bool_ok(-1), isl_bool_true) < 0)
2674 return -1;
2675 if (check_bool(ctx, isl_bool_ok(2), isl_bool_true) < 0)
2676 return -1;
2677 if (check_bool(ctx, isl_bool_ok(-2), isl_bool_true) < 0)
2678 return -1;
2680 return 0;
2683 static int test_lex(struct isl_ctx *ctx)
2685 isl_space *dim;
2686 isl_map *map;
2687 int empty;
2689 dim = isl_space_set_alloc(ctx, 0, 0);
2690 map = isl_map_lex_le(dim);
2691 empty = isl_map_is_empty(map);
2692 isl_map_free(map);
2694 if (empty < 0)
2695 return -1;
2696 if (empty)
2697 isl_die(ctx, isl_error_unknown,
2698 "expecting non-empty result", return -1);
2700 return 0;
2703 /* Inputs for isl_map_lexmin tests.
2704 * "map" is the input and "lexmin" is the expected result.
2706 struct {
2707 const char *map;
2708 const char *lexmin;
2709 } lexmin_tests [] = {
2710 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
2711 "{ [x] -> [5] : 6 <= x <= 8; "
2712 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
2713 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
2714 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
2715 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
2716 "{ [x] -> [y] : (4y = x and x >= 0) or "
2717 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2718 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2719 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
2720 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
2721 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
2722 /* Check that empty pieces are properly combined. */
2723 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2724 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2725 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
2726 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2727 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2728 "x >= 4 }" },
2729 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
2730 "a <= 255 and c <= 255 and d <= 255 - j and "
2731 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
2732 "247d <= 247 + k - j and 247d <= 247 + k - b and "
2733 "247d <= 247 + i and 248 - b <= 248d <= c and "
2734 "254d >= i - a + b and 254d >= -a + b and "
2735 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
2736 "{ [i, k, j] -> "
2737 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
2738 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
2739 "247j >= 62738 - i and 509j <= 129795 + i and "
2740 "742j >= 188724 - i; "
2741 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
2742 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
2743 "16*floor((8 + b)/16) <= 7 + b; "
2744 "[a] -> [1] }",
2745 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
2746 "[a] -> [b = 0] : 0 < a <= 509 }" },
2747 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
2748 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
2751 static int test_lexmin(struct isl_ctx *ctx)
2753 int i;
2754 int equal;
2755 const char *str;
2756 isl_basic_map *bmap;
2757 isl_map *map, *map2;
2758 isl_set *set;
2759 isl_set *set2;
2760 isl_pw_multi_aff *pma;
2762 str = "[p0, p1] -> { [] -> [] : "
2763 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2764 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2765 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2766 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2767 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2768 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2769 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2770 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2771 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2772 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2773 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2774 map = isl_map_read_from_str(ctx, str);
2775 map = isl_map_lexmin(map);
2776 isl_map_free(map);
2778 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2779 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2780 set = isl_set_read_from_str(ctx, str);
2781 set = isl_set_lexmax(set);
2782 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2783 set2 = isl_set_read_from_str(ctx, str);
2784 set = isl_set_intersect(set, set2);
2785 assert(!isl_set_is_empty(set));
2786 isl_set_free(set);
2788 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
2789 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
2790 map = isl_map_lexmin(map);
2791 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
2792 equal = isl_map_is_equal(map, map2);
2793 isl_map_free(map);
2794 isl_map_free(map2);
2796 if (equal < 0)
2797 return -1;
2798 if (!equal)
2799 isl_die(ctx, isl_error_unknown,
2800 "unexpected result", return -1);
2803 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2804 " 8i' <= i and 8i' >= -7 + i }";
2805 bmap = isl_basic_map_read_from_str(ctx, str);
2806 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2807 map2 = isl_map_from_pw_multi_aff(pma);
2808 map = isl_map_from_basic_map(bmap);
2809 assert(isl_map_is_equal(map, map2));
2810 isl_map_free(map);
2811 isl_map_free(map2);
2813 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2814 " 8i' <= i and 8i' >= -7 + i }";
2815 set = isl_set_read_from_str(ctx, str);
2816 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2817 set2 = isl_set_from_pw_multi_aff(pma);
2818 equal = isl_set_is_equal(set, set2);
2819 isl_set_free(set);
2820 isl_set_free(set2);
2821 if (equal < 0)
2822 return -1;
2823 if (!equal)
2824 isl_die(ctx, isl_error_unknown,
2825 "unexpected difference between set and "
2826 "piecewise affine expression", return -1);
2828 return 0;
2831 /* A specialized isl_set_min_val test case that would return the wrong result
2832 * in earlier versions of isl.
2833 * The explicit call to isl_basic_set_union prevents the second basic set
2834 * from being determined to be empty prior to the call to isl_set_min_val,
2835 * at least at the point where this test case was introduced.
2837 static int test_min_special(isl_ctx *ctx)
2839 const char *str;
2840 isl_basic_set *bset1, *bset2;
2841 isl_set *set;
2842 isl_aff *obj;
2843 isl_val *res;
2844 int ok;
2846 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
2847 bset1 = isl_basic_set_read_from_str(ctx, str);
2848 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
2849 bset2 = isl_basic_set_read_from_str(ctx, str);
2850 set = isl_basic_set_union(bset1, bset2);
2851 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
2853 res = isl_set_min_val(set, obj);
2854 ok = isl_val_cmp_si(res, 5) == 0;
2856 isl_aff_free(obj);
2857 isl_set_free(set);
2858 isl_val_free(res);
2860 if (!res)
2861 return -1;
2862 if (!ok)
2863 isl_die(ctx, isl_error_unknown, "unexpected minimum",
2864 return -1);
2866 return 0;
2869 /* A specialized isl_set_min_val test case that would return an error
2870 * in earlier versions of isl.
2872 static int test_min_special2(isl_ctx *ctx)
2874 const char *str;
2875 isl_basic_set *bset;
2876 isl_aff *obj;
2877 isl_val *res;
2879 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
2880 bset = isl_basic_set_read_from_str(ctx, str);
2882 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
2884 res = isl_basic_set_max_val(bset, obj);
2886 isl_basic_set_free(bset);
2887 isl_aff_free(obj);
2888 isl_val_free(res);
2890 if (!res)
2891 return -1;
2893 return 0;
2896 struct {
2897 const char *set;
2898 const char *obj;
2899 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2900 __isl_keep isl_aff *obj);
2901 const char *res;
2902 } opt_tests[] = {
2903 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2904 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2905 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2906 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2907 &isl_set_max_val, "30" },
2911 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2912 * In particular, check the results on non-convex inputs.
2914 static int test_min(struct isl_ctx *ctx)
2916 int i;
2917 isl_set *set;
2918 isl_aff *obj;
2919 isl_val *val, *res;
2920 isl_bool ok;
2922 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2923 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2924 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2925 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2926 val = opt_tests[i].fn(set, obj);
2927 ok = isl_val_eq(res, val);
2928 isl_val_free(res);
2929 isl_val_free(val);
2930 isl_aff_free(obj);
2931 isl_set_free(set);
2933 if (ok < 0)
2934 return -1;
2935 if (!ok)
2936 isl_die(ctx, isl_error_unknown,
2937 "unexpected optimum", return -1);
2940 if (test_min_special(ctx) < 0)
2941 return -1;
2942 if (test_min_special2(ctx) < 0)
2943 return -1;
2945 return 0;
2948 struct must_may {
2949 isl_map *must;
2950 isl_map *may;
2953 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2954 void *dep_user, void *user)
2956 struct must_may *mm = (struct must_may *)user;
2958 if (must)
2959 mm->must = isl_map_union(mm->must, dep);
2960 else
2961 mm->may = isl_map_union(mm->may, dep);
2963 return isl_stat_ok;
2966 static int common_space(void *first, void *second)
2968 int depth = *(int *)first;
2969 return 2 * depth;
2972 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2974 isl_map *map2;
2975 int equal;
2977 if (!map)
2978 return -1;
2980 map2 = isl_map_read_from_str(map->ctx, str);
2981 equal = isl_map_is_equal(map, map2);
2982 isl_map_free(map2);
2984 return equal;
2987 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2989 int equal;
2991 equal = map_is_equal(map, str);
2992 if (equal < 0)
2993 return -1;
2994 if (!equal)
2995 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2996 "result not as expected", return -1);
2997 return 0;
3000 static int test_dep(struct isl_ctx *ctx)
3002 const char *str;
3003 isl_space *dim;
3004 isl_map *map;
3005 isl_access_info *ai;
3006 isl_flow *flow;
3007 int depth;
3008 struct must_may mm;
3010 depth = 3;
3012 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3013 map = isl_map_read_from_str(ctx, str);
3014 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3016 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3017 map = isl_map_read_from_str(ctx, str);
3018 ai = isl_access_info_add_source(ai, map, 1, &depth);
3020 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3021 map = isl_map_read_from_str(ctx, str);
3022 ai = isl_access_info_add_source(ai, map, 1, &depth);
3024 flow = isl_access_info_compute_flow(ai);
3025 dim = isl_space_alloc(ctx, 0, 3, 3);
3026 mm.must = isl_map_empty(isl_space_copy(dim));
3027 mm.may = isl_map_empty(dim);
3029 isl_flow_foreach(flow, collect_must_may, &mm);
3031 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
3032 " [1,10,0] -> [2,5,0] }";
3033 assert(map_is_equal(mm.must, str));
3034 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3035 assert(map_is_equal(mm.may, str));
3037 isl_map_free(mm.must);
3038 isl_map_free(mm.may);
3039 isl_flow_free(flow);
3042 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3043 map = isl_map_read_from_str(ctx, str);
3044 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3046 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3047 map = isl_map_read_from_str(ctx, str);
3048 ai = isl_access_info_add_source(ai, map, 1, &depth);
3050 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3051 map = isl_map_read_from_str(ctx, str);
3052 ai = isl_access_info_add_source(ai, map, 0, &depth);
3054 flow = isl_access_info_compute_flow(ai);
3055 dim = isl_space_alloc(ctx, 0, 3, 3);
3056 mm.must = isl_map_empty(isl_space_copy(dim));
3057 mm.may = isl_map_empty(dim);
3059 isl_flow_foreach(flow, collect_must_may, &mm);
3061 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
3062 assert(map_is_equal(mm.must, str));
3063 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3064 assert(map_is_equal(mm.may, str));
3066 isl_map_free(mm.must);
3067 isl_map_free(mm.may);
3068 isl_flow_free(flow);
3071 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3072 map = isl_map_read_from_str(ctx, str);
3073 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3075 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3076 map = isl_map_read_from_str(ctx, str);
3077 ai = isl_access_info_add_source(ai, map, 0, &depth);
3079 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3080 map = isl_map_read_from_str(ctx, str);
3081 ai = isl_access_info_add_source(ai, map, 0, &depth);
3083 flow = isl_access_info_compute_flow(ai);
3084 dim = isl_space_alloc(ctx, 0, 3, 3);
3085 mm.must = isl_map_empty(isl_space_copy(dim));
3086 mm.may = isl_map_empty(dim);
3088 isl_flow_foreach(flow, collect_must_may, &mm);
3090 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
3091 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3092 assert(map_is_equal(mm.may, str));
3093 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3094 assert(map_is_equal(mm.must, str));
3096 isl_map_free(mm.must);
3097 isl_map_free(mm.may);
3098 isl_flow_free(flow);
3101 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
3102 map = isl_map_read_from_str(ctx, str);
3103 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3105 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3106 map = isl_map_read_from_str(ctx, str);
3107 ai = isl_access_info_add_source(ai, map, 0, &depth);
3109 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
3110 map = isl_map_read_from_str(ctx, str);
3111 ai = isl_access_info_add_source(ai, map, 0, &depth);
3113 flow = isl_access_info_compute_flow(ai);
3114 dim = isl_space_alloc(ctx, 0, 3, 3);
3115 mm.must = isl_map_empty(isl_space_copy(dim));
3116 mm.may = isl_map_empty(dim);
3118 isl_flow_foreach(flow, collect_must_may, &mm);
3120 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
3121 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
3122 assert(map_is_equal(mm.may, str));
3123 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3124 assert(map_is_equal(mm.must, str));
3126 isl_map_free(mm.must);
3127 isl_map_free(mm.may);
3128 isl_flow_free(flow);
3131 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
3132 map = isl_map_read_from_str(ctx, str);
3133 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3135 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3136 map = isl_map_read_from_str(ctx, str);
3137 ai = isl_access_info_add_source(ai, map, 0, &depth);
3139 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
3140 map = isl_map_read_from_str(ctx, str);
3141 ai = isl_access_info_add_source(ai, map, 0, &depth);
3143 flow = isl_access_info_compute_flow(ai);
3144 dim = isl_space_alloc(ctx, 0, 3, 3);
3145 mm.must = isl_map_empty(isl_space_copy(dim));
3146 mm.may = isl_map_empty(dim);
3148 isl_flow_foreach(flow, collect_must_may, &mm);
3150 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
3151 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
3152 assert(map_is_equal(mm.may, str));
3153 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3154 assert(map_is_equal(mm.must, str));
3156 isl_map_free(mm.must);
3157 isl_map_free(mm.may);
3158 isl_flow_free(flow);
3161 depth = 5;
3163 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3164 map = isl_map_read_from_str(ctx, str);
3165 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
3167 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3168 map = isl_map_read_from_str(ctx, str);
3169 ai = isl_access_info_add_source(ai, map, 1, &depth);
3171 flow = isl_access_info_compute_flow(ai);
3172 dim = isl_space_alloc(ctx, 0, 5, 5);
3173 mm.must = isl_map_empty(isl_space_copy(dim));
3174 mm.may = isl_map_empty(dim);
3176 isl_flow_foreach(flow, collect_must_may, &mm);
3178 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
3179 assert(map_is_equal(mm.must, str));
3180 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
3181 assert(map_is_equal(mm.may, str));
3183 isl_map_free(mm.must);
3184 isl_map_free(mm.may);
3185 isl_flow_free(flow);
3187 return 0;
3190 /* Check that the dependence analysis proceeds without errors.
3191 * Earlier versions of isl would break down during the analysis
3192 * due to the use of the wrong spaces.
3194 static int test_flow(isl_ctx *ctx)
3196 const char *str;
3197 isl_union_map *access, *schedule;
3198 isl_union_map *must_dep, *may_dep;
3199 int r;
3201 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
3202 access = isl_union_map_read_from_str(ctx, str);
3203 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
3204 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
3205 "S2[] -> [1,0,0,0]; "
3206 "S3[] -> [-1,0,0,0] }";
3207 schedule = isl_union_map_read_from_str(ctx, str);
3208 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
3209 isl_union_map_copy(access), schedule,
3210 &must_dep, &may_dep, NULL, NULL);
3211 isl_union_map_free(may_dep);
3212 isl_union_map_free(must_dep);
3214 return r;
3217 struct {
3218 const char *map;
3219 int sv;
3220 } sv_tests[] = {
3221 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
3222 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
3223 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
3224 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
3225 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
3226 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
3227 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3228 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3229 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
3232 int test_sv(isl_ctx *ctx)
3234 isl_union_map *umap;
3235 int i;
3236 int sv;
3238 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
3239 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
3240 sv = isl_union_map_is_single_valued(umap);
3241 isl_union_map_free(umap);
3242 if (sv < 0)
3243 return -1;
3244 if (sv_tests[i].sv && !sv)
3245 isl_die(ctx, isl_error_internal,
3246 "map not detected as single valued", return -1);
3247 if (!sv_tests[i].sv && sv)
3248 isl_die(ctx, isl_error_internal,
3249 "map detected as single valued", return -1);
3252 return 0;
3255 struct {
3256 const char *str;
3257 int bijective;
3258 } bijective_tests[] = {
3259 { "[N,M]->{[i,j] -> [i]}", 0 },
3260 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
3261 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
3262 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
3263 { "[N,M]->{[i,j] -> [j,i]}", 1 },
3264 { "[N,M]->{[i,j] -> [i+j]}", 0 },
3265 { "[N,M]->{[i,j] -> []}", 0 },
3266 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
3267 { "[N,M]->{[i,j] -> [2i]}", 0 },
3268 { "[N,M]->{[i,j] -> [i,i]}", 0 },
3269 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
3270 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
3271 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
3274 static int test_bijective(struct isl_ctx *ctx)
3276 isl_map *map;
3277 int i;
3278 int bijective;
3280 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
3281 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
3282 bijective = isl_map_is_bijective(map);
3283 isl_map_free(map);
3284 if (bijective < 0)
3285 return -1;
3286 if (bijective_tests[i].bijective && !bijective)
3287 isl_die(ctx, isl_error_internal,
3288 "map not detected as bijective", return -1);
3289 if (!bijective_tests[i].bijective && bijective)
3290 isl_die(ctx, isl_error_internal,
3291 "map detected as bijective", return -1);
3294 return 0;
3297 /* Inputs for isl_pw_qpolynomial_gist tests.
3298 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
3300 struct {
3301 const char *pwqp;
3302 const char *set;
3303 const char *gist;
3304 } pwqp_gist_tests[] = {
3305 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
3306 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
3307 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
3308 "{ [i] -> -1/2 + 1/2 * i }" },
3309 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
3312 static int test_pwqp(struct isl_ctx *ctx)
3314 int i;
3315 const char *str;
3316 isl_set *set;
3317 isl_pw_qpolynomial *pwqp1, *pwqp2;
3318 int equal;
3320 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3321 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3323 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
3324 isl_dim_in, 1, 1);
3326 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3327 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3329 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3331 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3333 isl_pw_qpolynomial_free(pwqp1);
3335 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
3336 str = pwqp_gist_tests[i].pwqp;
3337 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3338 str = pwqp_gist_tests[i].set;
3339 set = isl_set_read_from_str(ctx, str);
3340 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
3341 str = pwqp_gist_tests[i].gist;
3342 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3343 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3344 equal = isl_pw_qpolynomial_is_zero(pwqp1);
3345 isl_pw_qpolynomial_free(pwqp1);
3347 if (equal < 0)
3348 return -1;
3349 if (!equal)
3350 isl_die(ctx, isl_error_unknown,
3351 "unexpected result", return -1);
3354 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
3355 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3356 str = "{ [i] -> ([(2 * [i/2])/5]) }";
3357 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3359 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3361 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3363 isl_pw_qpolynomial_free(pwqp1);
3365 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
3366 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3367 str = "{ [x] -> x }";
3368 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3370 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3372 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3374 isl_pw_qpolynomial_free(pwqp1);
3376 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3377 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3378 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3379 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3380 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3381 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3382 isl_pw_qpolynomial_free(pwqp1);
3384 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3385 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3386 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3387 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3388 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3389 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3390 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3391 isl_pw_qpolynomial_free(pwqp1);
3392 isl_pw_qpolynomial_free(pwqp2);
3393 if (equal < 0)
3394 return -1;
3395 if (!equal)
3396 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3398 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3399 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3400 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3401 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3402 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3403 isl_val_one(ctx));
3404 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3405 isl_pw_qpolynomial_free(pwqp1);
3406 isl_pw_qpolynomial_free(pwqp2);
3407 if (equal < 0)
3408 return -1;
3409 if (!equal)
3410 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3412 return 0;
3415 static int test_split_periods(isl_ctx *ctx)
3417 const char *str;
3418 isl_pw_qpolynomial *pwqp;
3420 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3421 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3422 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3423 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3425 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3427 isl_pw_qpolynomial_free(pwqp);
3429 if (!pwqp)
3430 return -1;
3432 return 0;
3435 static int test_union(isl_ctx *ctx)
3437 const char *str;
3438 isl_union_set *uset1, *uset2;
3439 isl_union_map *umap1, *umap2;
3440 int equal;
3442 str = "{ [i] : 0 <= i <= 1 }";
3443 uset1 = isl_union_set_read_from_str(ctx, str);
3444 str = "{ [1] -> [0] }";
3445 umap1 = isl_union_map_read_from_str(ctx, str);
3447 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3448 equal = isl_union_map_is_equal(umap1, umap2);
3450 isl_union_map_free(umap1);
3451 isl_union_map_free(umap2);
3453 if (equal < 0)
3454 return -1;
3455 if (!equal)
3456 isl_die(ctx, isl_error_unknown, "union maps not equal",
3457 return -1);
3459 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3460 umap1 = isl_union_map_read_from_str(ctx, str);
3461 str = "{ A[i]; B[i] }";
3462 uset1 = isl_union_set_read_from_str(ctx, str);
3464 uset2 = isl_union_map_domain(umap1);
3466 equal = isl_union_set_is_equal(uset1, uset2);
3468 isl_union_set_free(uset1);
3469 isl_union_set_free(uset2);
3471 if (equal < 0)
3472 return -1;
3473 if (!equal)
3474 isl_die(ctx, isl_error_unknown, "union sets not equal",
3475 return -1);
3477 return 0;
3480 /* Check that computing a bound of a non-zero polynomial over an unbounded
3481 * domain does not produce a rational value.
3482 * In particular, check that the upper bound is infinity.
3484 static int test_bound_unbounded_domain(isl_ctx *ctx)
3486 const char *str;
3487 isl_pw_qpolynomial *pwqp;
3488 isl_pw_qpolynomial_fold *pwf, *pwf2;
3489 isl_bool equal;
3491 str = "{ [m,n] -> -m * n }";
3492 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3493 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3494 str = "{ infty }";
3495 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3496 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3497 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
3498 isl_pw_qpolynomial_fold_free(pwf);
3499 isl_pw_qpolynomial_fold_free(pwf2);
3501 if (equal < 0)
3502 return -1;
3503 if (!equal)
3504 isl_die(ctx, isl_error_unknown,
3505 "expecting infinite polynomial bound", return -1);
3507 return 0;
3510 static int test_bound(isl_ctx *ctx)
3512 const char *str;
3513 isl_size dim;
3514 isl_pw_qpolynomial *pwqp;
3515 isl_pw_qpolynomial_fold *pwf;
3517 if (test_bound_unbounded_domain(ctx) < 0)
3518 return -1;
3520 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
3521 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3522 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3523 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3524 isl_pw_qpolynomial_fold_free(pwf);
3525 if (dim < 0)
3526 return -1;
3527 if (dim != 4)
3528 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3529 return -1);
3531 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
3532 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3533 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3534 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3535 isl_pw_qpolynomial_fold_free(pwf);
3536 if (dim < 0)
3537 return -1;
3538 if (dim != 1)
3539 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3540 return -1);
3542 return 0;
3545 /* isl_set is defined to isl_map internally, so the corresponding elements
3546 * are isl_basic_map objects.
3548 #undef EL_BASE
3549 #undef SET_BASE
3550 #define EL_BASE basic_map
3551 #define SET_BASE set
3552 #include "isl_test_list_templ.c"
3554 #undef EL_BASE
3555 #undef SET_BASE
3556 #define EL_BASE basic_set
3557 #define SET_BASE union_set
3558 #include "isl_test_list_templ.c"
3560 #undef EL_BASE
3561 #undef SET_BASE
3562 #define EL_BASE set
3563 #define SET_BASE union_set
3564 #include "isl_test_list_templ.c"
3566 #undef EL_BASE
3567 #undef SET_BASE
3568 #define EL_BASE basic_map
3569 #define SET_BASE map
3570 #include "isl_test_list_templ.c"
3572 #undef EL_BASE
3573 #undef SET_BASE
3574 #define EL_BASE map
3575 #define SET_BASE union_map
3576 #include "isl_test_list_templ.c"
3578 /* Check that the conversion from isl objects to lists works as expected.
3580 static int test_get_list(isl_ctx *ctx)
3582 if (test_get_list_basic_map_from_set(ctx, "{ [0]; [2]; [3] }"))
3583 return -1;
3584 if (test_get_list_basic_set_from_union_set(ctx, "{ A[0]; B[2]; B[3] }"))
3585 return -1;
3586 if (test_get_list_set_from_union_set(ctx, "{ A[0]; A[2]; B[3] }"))
3587 return -1;
3588 if (test_get_list_basic_map_from_map(ctx,
3589 "{ [0] -> [0]; [2] -> [0]; [3] -> [0] }"))
3590 return -1;
3591 if (test_get_list_map_from_union_map(ctx,
3592 "{ A[0] -> [0]; A[2] -> [0]; B[3] -> [0] }"))
3593 return -1;
3595 return 0;
3598 static int test_lift(isl_ctx *ctx)
3600 const char *str;
3601 isl_basic_map *bmap;
3602 isl_basic_set *bset;
3604 str = "{ [i0] : exists e0 : i0 = 4e0 }";
3605 bset = isl_basic_set_read_from_str(ctx, str);
3606 bset = isl_basic_set_lift(bset);
3607 bmap = isl_basic_map_from_range(bset);
3608 bset = isl_basic_map_domain(bmap);
3609 isl_basic_set_free(bset);
3611 return 0;
3614 /* Check that isl_set_is_subset is not confused by identical
3615 * integer divisions.
3616 * The call to isl_set_normalize ensures that the equality constraints
3617 * a = b = 0 are discovered, turning e0 and e1 into identical
3618 * integer divisions. Any further simplification would remove
3619 * the duplicate integer divisions.
3621 static isl_stat test_subset_duplicate_integer_divisions(isl_ctx *ctx)
3623 const char *str;
3624 isl_bool is_subset;
3625 isl_set *set1, *set2;
3627 str = "{ [a, b, c, d] : "
3628 "exists (e0 = floor((a + d)/4), e1 = floor((d)/4), "
3629 "e2 = floor((-a - d + 4 *floor((a + d)/4))/10), "
3630 "e3 = floor((-d + 4*floor((d)/4))/10): "
3631 "10e2 = -a - 2c - d + 4e0 and 10e3 = -2c - d + 4e1 and "
3632 "b >= 0 and a <= 0 and b <= a) }";
3633 set1 = isl_set_read_from_str(ctx, str);
3634 set2 = isl_set_read_from_str(ctx, str);
3635 set2 = isl_set_normalize(set2);
3637 is_subset = isl_set_is_subset(set1, set2);
3639 isl_set_free(set1);
3640 isl_set_free(set2);
3642 if (is_subset < 0)
3643 return isl_stat_error;
3644 if (!is_subset)
3645 isl_die(ctx, isl_error_unknown,
3646 "set is not considered to be a subset of itself",
3647 return isl_stat_error);
3649 return isl_stat_ok;
3652 struct {
3653 const char *set1;
3654 const char *set2;
3655 int subset;
3656 } subset_tests[] = {
3657 { "{ [112, 0] }",
3658 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
3659 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
3660 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
3661 { "{ [65] }",
3662 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
3663 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
3664 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
3665 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
3666 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
3667 "256e0 <= 255i and 256e0 >= -255 + 255i and "
3668 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
3669 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
3670 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
3671 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
3672 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
3673 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
3674 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
3675 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
3676 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
3677 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
3678 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
3679 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
3680 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
3681 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
3682 "4e0 <= -57 + i0 + i1)) or "
3683 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
3684 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
3685 "4e0 >= -61 + i0 + i1)) or "
3686 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
3687 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
3690 static int test_subset(isl_ctx *ctx)
3692 int i;
3693 isl_set *set1, *set2;
3694 int subset;
3696 if (test_subset_duplicate_integer_divisions(ctx) < 0)
3697 return -1;
3699 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3700 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3701 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3702 subset = isl_set_is_subset(set1, set2);
3703 isl_set_free(set1);
3704 isl_set_free(set2);
3705 if (subset < 0)
3706 return -1;
3707 if (subset != subset_tests[i].subset)
3708 isl_die(ctx, isl_error_unknown,
3709 "incorrect subset result", return -1);
3712 return 0;
3715 struct {
3716 const char *minuend;
3717 const char *subtrahend;
3718 const char *difference;
3719 } subtract_domain_tests[] = {
3720 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3721 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3722 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3725 static int test_subtract(isl_ctx *ctx)
3727 int i;
3728 isl_union_map *umap1, *umap2;
3729 isl_union_pw_multi_aff *upma1, *upma2;
3730 isl_union_set *uset;
3731 int equal;
3733 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3734 umap1 = isl_union_map_read_from_str(ctx,
3735 subtract_domain_tests[i].minuend);
3736 uset = isl_union_set_read_from_str(ctx,
3737 subtract_domain_tests[i].subtrahend);
3738 umap2 = isl_union_map_read_from_str(ctx,
3739 subtract_domain_tests[i].difference);
3740 umap1 = isl_union_map_subtract_domain(umap1, uset);
3741 equal = isl_union_map_is_equal(umap1, umap2);
3742 isl_union_map_free(umap1);
3743 isl_union_map_free(umap2);
3744 if (equal < 0)
3745 return -1;
3746 if (!equal)
3747 isl_die(ctx, isl_error_unknown,
3748 "incorrect subtract domain result", return -1);
3751 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3752 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3753 subtract_domain_tests[i].minuend);
3754 uset = isl_union_set_read_from_str(ctx,
3755 subtract_domain_tests[i].subtrahend);
3756 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3757 subtract_domain_tests[i].difference);
3758 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3759 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3760 isl_union_pw_multi_aff_free(upma1);
3761 isl_union_pw_multi_aff_free(upma2);
3762 if (equal < 0)
3763 return -1;
3764 if (!equal)
3765 isl_die(ctx, isl_error_unknown,
3766 "incorrect subtract domain result", return -1);
3769 return 0;
3772 /* Check that intersecting the empty basic set with another basic set
3773 * does not increase the number of constraints. In particular,
3774 * the empty basic set should maintain its canonical representation.
3776 static int test_intersect_1(isl_ctx *ctx)
3778 isl_size n1, n2;
3779 isl_basic_set *bset1, *bset2;
3781 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3782 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3783 n1 = isl_basic_set_n_constraint(bset1);
3784 bset1 = isl_basic_set_intersect(bset1, bset2);
3785 n2 = isl_basic_set_n_constraint(bset1);
3786 isl_basic_set_free(bset1);
3787 if (n1 < 0 || n2 < 0)
3788 return -1;
3789 if (n1 != n2)
3790 isl_die(ctx, isl_error_unknown,
3791 "number of constraints of empty set changed",
3792 return -1);
3794 return 0;
3797 /* Check that intersecting a set with itself does not cause
3798 * an explosion in the number of disjuncts.
3800 static isl_stat test_intersect_2(isl_ctx *ctx)
3802 int i;
3803 isl_set *set;
3805 set = isl_set_read_from_str(ctx, "{ [x,y] : x >= 0 or y >= 0 }");
3806 for (i = 0; i < 100; ++i)
3807 set = isl_set_intersect(set, isl_set_copy(set));
3808 isl_set_free(set);
3809 if (!set)
3810 return isl_stat_error;
3811 return isl_stat_ok;
3814 /* Perform some intersection tests.
3816 static int test_intersect(isl_ctx *ctx)
3818 if (test_intersect_1(ctx) < 0)
3819 return -1;
3820 if (test_intersect_2(ctx) < 0)
3821 return -1;
3823 return 0;
3826 int test_factorize(isl_ctx *ctx)
3828 const char *str;
3829 isl_basic_set *bset;
3830 isl_factorizer *f;
3832 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3833 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3834 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3835 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3836 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3837 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3838 "3i5 >= -2i0 - i2 + 3i4 }";
3839 bset = isl_basic_set_read_from_str(ctx, str);
3840 f = isl_basic_set_factorizer(bset);
3841 isl_basic_set_free(bset);
3842 isl_factorizer_free(f);
3843 if (!f)
3844 isl_die(ctx, isl_error_unknown,
3845 "failed to construct factorizer", return -1);
3847 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3848 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3849 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3850 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3851 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3852 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3853 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3854 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3855 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3856 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3857 bset = isl_basic_set_read_from_str(ctx, str);
3858 f = isl_basic_set_factorizer(bset);
3859 isl_basic_set_free(bset);
3860 isl_factorizer_free(f);
3861 if (!f)
3862 isl_die(ctx, isl_error_unknown,
3863 "failed to construct factorizer", return -1);
3865 return 0;
3868 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3870 int *injective = user;
3872 *injective = isl_map_is_injective(map);
3873 isl_map_free(map);
3875 if (*injective < 0 || !*injective)
3876 return isl_stat_error;
3878 return isl_stat_ok;
3881 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3882 const char *r, const char *s, int tilable, int parallel)
3884 int i;
3885 isl_union_set *D;
3886 isl_union_map *W, *R, *S;
3887 isl_union_map *empty;
3888 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3889 isl_union_map *validity, *proximity, *coincidence;
3890 isl_union_map *schedule;
3891 isl_union_map *test;
3892 isl_union_set *delta;
3893 isl_union_set *domain;
3894 isl_set *delta_set;
3895 isl_set *slice;
3896 isl_set *origin;
3897 isl_schedule_constraints *sc;
3898 isl_schedule *sched;
3899 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3900 isl_size n;
3902 D = isl_union_set_read_from_str(ctx, d);
3903 W = isl_union_map_read_from_str(ctx, w);
3904 R = isl_union_map_read_from_str(ctx, r);
3905 S = isl_union_map_read_from_str(ctx, s);
3907 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3908 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3910 empty = isl_union_map_empty(isl_union_map_get_space(S));
3911 isl_union_map_compute_flow(isl_union_map_copy(R),
3912 isl_union_map_copy(W), empty,
3913 isl_union_map_copy(S),
3914 &dep_raw, NULL, NULL, NULL);
3915 isl_union_map_compute_flow(isl_union_map_copy(W),
3916 isl_union_map_copy(W),
3917 isl_union_map_copy(R),
3918 isl_union_map_copy(S),
3919 &dep_waw, &dep_war, NULL, NULL);
3921 dep = isl_union_map_union(dep_waw, dep_war);
3922 dep = isl_union_map_union(dep, dep_raw);
3923 validity = isl_union_map_copy(dep);
3924 coincidence = isl_union_map_copy(dep);
3925 proximity = isl_union_map_copy(dep);
3927 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3928 sc = isl_schedule_constraints_set_validity(sc, validity);
3929 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3930 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3931 sched = isl_schedule_constraints_compute_schedule(sc);
3932 schedule = isl_schedule_get_map(sched);
3933 isl_schedule_free(sched);
3934 isl_union_map_free(W);
3935 isl_union_map_free(R);
3936 isl_union_map_free(S);
3938 is_injection = 1;
3939 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3941 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3942 is_complete = isl_union_set_is_subset(D, domain);
3943 isl_union_set_free(D);
3944 isl_union_set_free(domain);
3946 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3947 test = isl_union_map_apply_range(test, dep);
3948 test = isl_union_map_apply_range(test, schedule);
3950 delta = isl_union_map_deltas(test);
3951 n = isl_union_set_n_set(delta);
3952 if (n < 0) {
3953 isl_union_set_free(delta);
3954 return -1;
3956 if (n == 0) {
3957 is_tilable = 1;
3958 is_parallel = 1;
3959 is_nonneg = 1;
3960 isl_union_set_free(delta);
3961 } else {
3962 isl_size dim;
3964 delta_set = isl_set_from_union_set(delta);
3966 slice = isl_set_universe(isl_set_get_space(delta_set));
3967 for (i = 0; i < tilable; ++i)
3968 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3969 is_tilable = isl_set_is_subset(delta_set, slice);
3970 isl_set_free(slice);
3972 slice = isl_set_universe(isl_set_get_space(delta_set));
3973 for (i = 0; i < parallel; ++i)
3974 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3975 is_parallel = isl_set_is_subset(delta_set, slice);
3976 isl_set_free(slice);
3978 origin = isl_set_universe(isl_set_get_space(delta_set));
3979 dim = isl_set_dim(origin, isl_dim_set);
3980 if (dim < 0)
3981 origin = isl_set_free(origin);
3982 for (i = 0; i < dim; ++i)
3983 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3985 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3986 delta_set = isl_set_lexmin(delta_set);
3988 is_nonneg = isl_set_is_equal(delta_set, origin);
3990 isl_set_free(origin);
3991 isl_set_free(delta_set);
3994 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3995 is_injection < 0 || is_complete < 0)
3996 return -1;
3997 if (!is_complete)
3998 isl_die(ctx, isl_error_unknown,
3999 "generated schedule incomplete", return -1);
4000 if (!is_injection)
4001 isl_die(ctx, isl_error_unknown,
4002 "generated schedule not injective on each statement",
4003 return -1);
4004 if (!is_nonneg)
4005 isl_die(ctx, isl_error_unknown,
4006 "negative dependences in generated schedule",
4007 return -1);
4008 if (!is_tilable)
4009 isl_die(ctx, isl_error_unknown,
4010 "generated schedule not as tilable as expected",
4011 return -1);
4012 if (!is_parallel)
4013 isl_die(ctx, isl_error_unknown,
4014 "generated schedule not as parallel as expected",
4015 return -1);
4017 return 0;
4020 /* Compute a schedule for the given instance set, validity constraints,
4021 * proximity constraints and context and return a corresponding union map
4022 * representation.
4024 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
4025 const char *domain, const char *validity, const char *proximity,
4026 const char *context)
4028 isl_set *con;
4029 isl_union_set *dom;
4030 isl_union_map *dep;
4031 isl_union_map *prox;
4032 isl_schedule_constraints *sc;
4033 isl_schedule *schedule;
4034 isl_union_map *sched;
4036 con = isl_set_read_from_str(ctx, context);
4037 dom = isl_union_set_read_from_str(ctx, domain);
4038 dep = isl_union_map_read_from_str(ctx, validity);
4039 prox = isl_union_map_read_from_str(ctx, proximity);
4040 sc = isl_schedule_constraints_on_domain(dom);
4041 sc = isl_schedule_constraints_set_context(sc, con);
4042 sc = isl_schedule_constraints_set_validity(sc, dep);
4043 sc = isl_schedule_constraints_set_proximity(sc, prox);
4044 schedule = isl_schedule_constraints_compute_schedule(sc);
4045 sched = isl_schedule_get_map(schedule);
4046 isl_schedule_free(schedule);
4048 return sched;
4051 /* Compute a schedule for the given instance set, validity constraints and
4052 * proximity constraints and return a corresponding union map representation.
4054 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
4055 const char *domain, const char *validity, const char *proximity)
4057 return compute_schedule_with_context(ctx, domain, validity, proximity,
4058 "{ : }");
4061 /* Check that a schedule can be constructed on the given domain
4062 * with the given validity and proximity constraints.
4064 static int test_has_schedule(isl_ctx *ctx, const char *domain,
4065 const char *validity, const char *proximity)
4067 isl_union_map *sched;
4069 sched = compute_schedule(ctx, domain, validity, proximity);
4070 if (!sched)
4071 return -1;
4073 isl_union_map_free(sched);
4074 return 0;
4077 int test_special_schedule(isl_ctx *ctx, const char *domain,
4078 const char *validity, const char *proximity, const char *expected_sched)
4080 isl_union_map *sched1, *sched2;
4081 int equal;
4083 sched1 = compute_schedule(ctx, domain, validity, proximity);
4084 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
4086 equal = isl_union_map_is_equal(sched1, sched2);
4087 isl_union_map_free(sched1);
4088 isl_union_map_free(sched2);
4090 if (equal < 0)
4091 return -1;
4092 if (!equal)
4093 isl_die(ctx, isl_error_unknown, "unexpected schedule",
4094 return -1);
4096 return 0;
4099 /* Check that the schedule map is properly padded, i.e., that the range
4100 * lives in a single space.
4102 static int test_padded_schedule(isl_ctx *ctx)
4104 const char *str;
4105 isl_union_set *D;
4106 isl_union_map *validity, *proximity;
4107 isl_schedule_constraints *sc;
4108 isl_schedule *sched;
4109 isl_union_map *umap;
4110 isl_union_set *range;
4111 isl_set *set;
4113 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
4114 D = isl_union_set_read_from_str(ctx, str);
4115 validity = isl_union_map_empty(isl_union_set_get_space(D));
4116 proximity = isl_union_map_copy(validity);
4117 sc = isl_schedule_constraints_on_domain(D);
4118 sc = isl_schedule_constraints_set_validity(sc, validity);
4119 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4120 sched = isl_schedule_constraints_compute_schedule(sc);
4121 umap = isl_schedule_get_map(sched);
4122 isl_schedule_free(sched);
4123 range = isl_union_map_range(umap);
4124 set = isl_set_from_union_set(range);
4125 isl_set_free(set);
4127 if (!set)
4128 return -1;
4130 return 0;
4133 /* Check that conditional validity constraints are also taken into
4134 * account across bands.
4135 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
4136 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
4137 * and then check that the adjacent order constraint C[2,1]->D[2,0]
4138 * is enforced by the rest of the schedule.
4140 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
4142 const char *str;
4143 isl_union_set *domain;
4144 isl_union_map *validity, *proximity, *condition;
4145 isl_union_map *sink, *source, *dep;
4146 isl_schedule_constraints *sc;
4147 isl_schedule *schedule;
4148 isl_union_access_info *access;
4149 isl_union_flow *flow;
4150 int empty;
4152 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4153 "A[k] : k >= 1 and k <= -1 + n; "
4154 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4155 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
4156 domain = isl_union_set_read_from_str(ctx, str);
4157 sc = isl_schedule_constraints_on_domain(domain);
4158 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
4159 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4160 "D[k, i] -> C[1 + k, i] : "
4161 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4162 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
4163 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
4164 validity = isl_union_map_read_from_str(ctx, str);
4165 sc = isl_schedule_constraints_set_validity(sc, validity);
4166 str = "[n] -> { C[k, i] -> D[k, i] : "
4167 "0 <= i <= -1 + k and k <= -1 + n }";
4168 proximity = isl_union_map_read_from_str(ctx, str);
4169 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4170 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
4171 "i <= -1 + k and i >= 1 and k <= -2 + n; "
4172 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
4173 "k <= -1 + n and i >= 0 and i <= -2 + k }";
4174 condition = isl_union_map_read_from_str(ctx, str);
4175 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
4176 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4177 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
4178 "i >= 0 and i <= -1 + k and k <= -1 + n and "
4179 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
4180 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
4181 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4182 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
4183 "k <= -1 + n and i >= 0 and i <= -1 + k and "
4184 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
4185 validity = isl_union_map_read_from_str(ctx, str);
4186 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4187 validity);
4188 schedule = isl_schedule_constraints_compute_schedule(sc);
4189 str = "{ D[2,0] -> [] }";
4190 sink = isl_union_map_read_from_str(ctx, str);
4191 access = isl_union_access_info_from_sink(sink);
4192 str = "{ C[2,1] -> [] }";
4193 source = isl_union_map_read_from_str(ctx, str);
4194 access = isl_union_access_info_set_must_source(access, source);
4195 access = isl_union_access_info_set_schedule(access, schedule);
4196 flow = isl_union_access_info_compute_flow(access);
4197 dep = isl_union_flow_get_must_dependence(flow);
4198 isl_union_flow_free(flow);
4199 empty = isl_union_map_is_empty(dep);
4200 isl_union_map_free(dep);
4202 if (empty < 0)
4203 return -1;
4204 if (empty)
4205 isl_die(ctx, isl_error_unknown,
4206 "conditional validity not respected", return -1);
4208 return 0;
4211 /* Check that the test for violated conditional validity constraints
4212 * is not confused by domain compression.
4213 * In particular, earlier versions of isl would apply
4214 * a schedule on the compressed domains to the original domains,
4215 * resulting in a failure to detect that the default schedule
4216 * violates the conditional validity constraints.
4218 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
4220 const char *str;
4221 isl_bool empty;
4222 isl_union_set *domain;
4223 isl_union_map *validity, *condition;
4224 isl_schedule_constraints *sc;
4225 isl_schedule *schedule;
4226 isl_union_map *umap;
4227 isl_map *map, *ge;
4229 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
4230 domain = isl_union_set_read_from_str(ctx, str);
4231 sc = isl_schedule_constraints_on_domain(domain);
4232 str = "{ B[1, i] -> A[0, i + 1] }";
4233 condition = isl_union_map_read_from_str(ctx, str);
4234 str = "{ A[0, i] -> B[1, i - 1] }";
4235 validity = isl_union_map_read_from_str(ctx, str);
4236 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4237 isl_union_map_copy(validity));
4238 schedule = isl_schedule_constraints_compute_schedule(sc);
4239 umap = isl_schedule_get_map(schedule);
4240 isl_schedule_free(schedule);
4241 validity = isl_union_map_apply_domain(validity,
4242 isl_union_map_copy(umap));
4243 validity = isl_union_map_apply_range(validity, umap);
4244 map = isl_map_from_union_map(validity);
4245 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4246 map = isl_map_intersect(map, ge);
4247 empty = isl_map_is_empty(map);
4248 isl_map_free(map);
4250 if (empty < 0)
4251 return -1;
4252 if (!empty)
4253 isl_die(ctx, isl_error_unknown,
4254 "conditional validity constraints not satisfied",
4255 return -1);
4257 return 0;
4260 /* Input for testing of schedule construction based on
4261 * conditional constraints.
4263 * domain is the iteration domain
4264 * flow are the flow dependences, which determine the validity and
4265 * proximity constraints
4266 * condition are the conditions on the conditional validity constraints
4267 * conditional_validity are the conditional validity constraints
4268 * outer_band_n is the expected number of members in the outer band
4270 struct {
4271 const char *domain;
4272 const char *flow;
4273 const char *condition;
4274 const char *conditional_validity;
4275 int outer_band_n;
4276 } live_range_tests[] = {
4277 /* Contrived example that illustrates that we need to keep
4278 * track of tagged condition dependences and
4279 * tagged conditional validity dependences
4280 * in isl_sched_edge separately.
4281 * In particular, the conditional validity constraints on A
4282 * cannot be satisfied,
4283 * but they can be ignored because there are no corresponding
4284 * condition constraints. However, we do have an additional
4285 * conditional validity constraint that maps to the same
4286 * dependence relation
4287 * as the condition constraint on B. If we did not make a distinction
4288 * between tagged condition and tagged conditional validity
4289 * dependences, then we
4290 * could end up treating this shared dependence as an condition
4291 * constraint on A, forcing a localization of the conditions,
4292 * which is impossible.
4294 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
4295 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
4296 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
4297 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4298 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4299 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
4302 /* TACO 2013 Fig. 7 */
4303 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4304 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4305 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4306 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
4307 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
4308 "0 <= i < n and 0 <= j < n - 1 }",
4309 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
4310 "0 <= i < n and 0 <= j < j' < n;"
4311 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
4312 "0 <= i < i' < n and 0 <= j,j' < n;"
4313 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
4314 "0 <= i,j,j' < n and j < j' }",
4317 /* TACO 2013 Fig. 7, without tags */
4318 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4319 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4320 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4321 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4322 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4323 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
4324 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
4325 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
4328 /* TACO 2013 Fig. 12 */
4329 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
4330 "S3[i,3] : 0 <= i <= 1 }",
4331 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
4332 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
4333 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
4334 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
4335 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4336 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
4337 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4338 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
4339 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
4340 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
4341 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
4346 /* Test schedule construction based on conditional constraints.
4347 * In particular, check the number of members in the outer band node
4348 * as an indication of whether tiling is possible or not.
4350 static int test_conditional_schedule_constraints(isl_ctx *ctx)
4352 int i;
4353 isl_union_set *domain;
4354 isl_union_map *condition;
4355 isl_union_map *flow;
4356 isl_union_map *validity;
4357 isl_schedule_constraints *sc;
4358 isl_schedule *schedule;
4359 isl_schedule_node *node;
4360 isl_size n_member;
4362 if (test_special_conditional_schedule_constraints(ctx) < 0)
4363 return -1;
4364 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
4365 return -1;
4367 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
4368 domain = isl_union_set_read_from_str(ctx,
4369 live_range_tests[i].domain);
4370 flow = isl_union_map_read_from_str(ctx,
4371 live_range_tests[i].flow);
4372 condition = isl_union_map_read_from_str(ctx,
4373 live_range_tests[i].condition);
4374 validity = isl_union_map_read_from_str(ctx,
4375 live_range_tests[i].conditional_validity);
4376 sc = isl_schedule_constraints_on_domain(domain);
4377 sc = isl_schedule_constraints_set_validity(sc,
4378 isl_union_map_copy(flow));
4379 sc = isl_schedule_constraints_set_proximity(sc, flow);
4380 sc = isl_schedule_constraints_set_conditional_validity(sc,
4381 condition, validity);
4382 schedule = isl_schedule_constraints_compute_schedule(sc);
4383 node = isl_schedule_get_root(schedule);
4384 while (node &&
4385 isl_schedule_node_get_type(node) != isl_schedule_node_band)
4386 node = isl_schedule_node_first_child(node);
4387 n_member = isl_schedule_node_band_n_member(node);
4388 isl_schedule_node_free(node);
4389 isl_schedule_free(schedule);
4391 if (!schedule || n_member < 0)
4392 return -1;
4393 if (n_member != live_range_tests[i].outer_band_n)
4394 isl_die(ctx, isl_error_unknown,
4395 "unexpected number of members in outer band",
4396 return -1);
4398 return 0;
4401 /* Check that the schedule computed for the given instance set and
4402 * dependence relation strongly satisfies the dependences.
4403 * In particular, check that no instance is scheduled before
4404 * or together with an instance on which it depends.
4405 * Earlier versions of isl would produce a schedule that
4406 * only weakly satisfies the dependences.
4408 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
4410 const char *domain, *dep;
4411 isl_union_map *D, *schedule;
4412 isl_map *map, *ge;
4413 int empty;
4415 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
4416 "A[i0] : 0 <= i0 <= 1 }";
4417 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
4418 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
4419 schedule = compute_schedule(ctx, domain, dep, dep);
4420 D = isl_union_map_read_from_str(ctx, dep);
4421 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
4422 D = isl_union_map_apply_range(D, schedule);
4423 map = isl_map_from_union_map(D);
4424 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4425 map = isl_map_intersect(map, ge);
4426 empty = isl_map_is_empty(map);
4427 isl_map_free(map);
4429 if (empty < 0)
4430 return -1;
4431 if (!empty)
4432 isl_die(ctx, isl_error_unknown,
4433 "dependences not strongly satisfied", return -1);
4435 return 0;
4438 /* Compute a schedule for input where the instance set constraints
4439 * conflict with the context constraints.
4440 * Earlier versions of isl did not properly handle this situation.
4442 static int test_conflicting_context_schedule(isl_ctx *ctx)
4444 isl_union_map *schedule;
4445 const char *domain, *context;
4447 domain = "[n] -> { A[] : n >= 0 }";
4448 context = "[n] -> { : n < 0 }";
4449 schedule = compute_schedule_with_context(ctx,
4450 domain, "{}", "{}", context);
4451 isl_union_map_free(schedule);
4453 if (!schedule)
4454 return -1;
4456 return 0;
4459 /* Check that a set of schedule constraints that only allow for
4460 * a coalescing schedule still produces a schedule even if the user
4461 * request a non-coalescing schedule. Earlier versions of isl
4462 * would not handle this case correctly.
4464 static int test_coalescing_schedule(isl_ctx *ctx)
4466 const char *domain, *dep;
4467 isl_union_set *I;
4468 isl_union_map *D;
4469 isl_schedule_constraints *sc;
4470 isl_schedule *schedule;
4471 int treat_coalescing;
4473 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
4474 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
4475 I = isl_union_set_read_from_str(ctx, domain);
4476 D = isl_union_map_read_from_str(ctx, dep);
4477 sc = isl_schedule_constraints_on_domain(I);
4478 sc = isl_schedule_constraints_set_validity(sc, D);
4479 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4480 isl_options_set_schedule_treat_coalescing(ctx, 1);
4481 schedule = isl_schedule_constraints_compute_schedule(sc);
4482 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4483 isl_schedule_free(schedule);
4484 if (!schedule)
4485 return -1;
4486 return 0;
4489 /* Check that the scheduler does not perform any needless
4490 * compound skewing. Earlier versions of isl would compute
4491 * schedules in terms of transformed schedule coefficients and
4492 * would not accurately keep track of the sum of the original
4493 * schedule coefficients. It could then produce the schedule
4494 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
4495 * for the input below instead of the schedule below.
4497 static int test_skewing_schedule(isl_ctx *ctx)
4499 const char *D, *V, *P, *S;
4501 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
4502 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
4503 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
4504 "-1 <= a-i + b-j + c-k <= 1 }";
4505 P = "{ }";
4506 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
4508 return test_special_schedule(ctx, D, V, P, S);
4511 int test_schedule(isl_ctx *ctx)
4513 const char *D, *W, *R, *V, *P, *S;
4514 int max_coincidence;
4515 int treat_coalescing;
4517 /* Handle resulting schedule with zero bands. */
4518 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
4519 return -1;
4521 /* Jacobi */
4522 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
4523 W = "{ S1[t,i] -> a[t,i] }";
4524 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
4525 "S1[t,i] -> a[t-1,i+1] }";
4526 S = "{ S1[t,i] -> [t,i] }";
4527 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4528 return -1;
4530 /* Fig. 5 of CC2008 */
4531 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
4532 "j <= -1 + N }";
4533 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
4534 "j >= 2 and j <= -1 + N }";
4535 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
4536 "j >= 2 and j <= -1 + N; "
4537 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
4538 "j >= 2 and j <= -1 + N }";
4539 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4540 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4541 return -1;
4543 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
4544 W = "{ S1[i] -> a[i] }";
4545 R = "{ S2[i] -> a[i+1] }";
4546 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4547 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4548 return -1;
4550 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
4551 W = "{ S1[i] -> a[i] }";
4552 R = "{ S2[i] -> a[9-i] }";
4553 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4554 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4555 return -1;
4557 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
4558 W = "{ S1[i] -> a[i] }";
4559 R = "[N] -> { S2[i] -> a[N-1-i] }";
4560 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4561 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4562 return -1;
4564 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
4565 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
4566 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
4567 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
4568 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4569 return -1;
4571 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4572 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
4573 R = "{ S2[i,j] -> a[i-1,j] }";
4574 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4575 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4576 return -1;
4578 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4579 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
4580 R = "{ S2[i,j] -> a[i,j-1] }";
4581 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4582 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4583 return -1;
4585 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
4586 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
4587 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
4588 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
4589 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
4590 "S_0[] -> [0, 0, 0] }";
4591 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
4592 return -1;
4593 ctx->opt->schedule_parametric = 0;
4594 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4595 return -1;
4596 ctx->opt->schedule_parametric = 1;
4598 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
4599 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
4600 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
4601 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
4602 "S4[i] -> a[i,N] }";
4603 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
4604 "S4[i] -> [4,i,0] }";
4605 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
4606 isl_options_set_schedule_maximize_coincidence(ctx, 0);
4607 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4608 return -1;
4609 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
4611 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
4612 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4613 "j <= N }";
4614 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4615 "j <= N; "
4616 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
4617 "j <= N }";
4618 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4619 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4620 return -1;
4622 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
4623 " S_2[t] : t >= 0 and t <= -1 + N; "
4624 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
4625 "i <= -1 + N }";
4626 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
4627 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
4628 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
4629 "i >= 0 and i <= -1 + N }";
4630 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
4631 "i >= 0 and i <= -1 + N; "
4632 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
4633 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
4634 " S_0[t] -> [0, t, 0] }";
4636 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4637 return -1;
4638 ctx->opt->schedule_parametric = 0;
4639 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4640 return -1;
4641 ctx->opt->schedule_parametric = 1;
4643 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
4644 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
4645 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
4646 return -1;
4648 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
4649 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
4650 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
4651 "j >= 0 and j <= -1 + N; "
4652 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4653 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
4654 "j >= 0 and j <= -1 + N; "
4655 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4656 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
4657 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4658 return -1;
4660 D = "{ S_0[i] : i >= 0 }";
4661 W = "{ S_0[i] -> a[i] : i >= 0 }";
4662 R = "{ S_0[i] -> a[0] : i >= 0 }";
4663 S = "{ S_0[i] -> [0, i, 0] }";
4664 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4665 return -1;
4667 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
4668 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
4669 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
4670 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
4671 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4672 return -1;
4674 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
4675 "k <= -1 + n and k >= 0 }";
4676 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
4677 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
4678 "k <= -1 + n and k >= 0; "
4679 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
4680 "k <= -1 + n and k >= 0; "
4681 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
4682 "k <= -1 + n and k >= 0 }";
4683 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
4684 ctx->opt->schedule_outer_coincidence = 1;
4685 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4686 return -1;
4687 ctx->opt->schedule_outer_coincidence = 0;
4689 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
4690 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
4691 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
4692 "Stmt_for_body24[i0, i1, 1, 0]:"
4693 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
4694 "Stmt_for_body7[i0, i1, i2]:"
4695 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
4696 "i2 <= 7 }";
4698 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
4699 "Stmt_for_body24[1, i1, i2, i3]:"
4700 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
4701 "i2 >= 1;"
4702 "Stmt_for_body24[0, i1, i2, i3] -> "
4703 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
4704 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
4705 "i3 >= 0;"
4706 "Stmt_for_body24[0, i1, i2, i3] ->"
4707 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4708 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4709 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4710 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4711 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4712 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4713 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4714 "i1 <= 6 and i1 >= 0;"
4715 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4716 "Stmt_for_body7[i0, i1, i2] -> "
4717 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4718 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4719 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4720 "Stmt_for_body7[i0, i1, i2] -> "
4721 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4722 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4723 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4724 P = V;
4726 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4727 isl_options_set_schedule_treat_coalescing(ctx, 0);
4728 if (test_has_schedule(ctx, D, V, P) < 0)
4729 return -1;
4730 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4732 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4733 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4734 "j >= 1 and j <= 7;"
4735 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4736 "j >= 1 and j <= 8 }";
4737 P = "{ }";
4738 S = "{ S_0[i, j] -> [i + j, i] }";
4739 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4740 if (test_special_schedule(ctx, D, V, P, S) < 0)
4741 return -1;
4742 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4744 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4745 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4746 "j >= 0 and j <= -1 + i }";
4747 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4748 "i <= -1 + N and j >= 0;"
4749 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4750 "i <= -2 + N }";
4751 P = "{ }";
4752 S = "{ S_0[i, j] -> [i, j] }";
4753 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4754 if (test_special_schedule(ctx, D, V, P, S) < 0)
4755 return -1;
4756 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4758 /* Test both algorithms on a case with only proximity dependences. */
4759 D = "{ S[i,j] : 0 <= i <= 10 }";
4760 V = "{ }";
4761 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4762 S = "{ S[i, j] -> [j, i] }";
4763 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4764 if (test_special_schedule(ctx, D, V, P, S) < 0)
4765 return -1;
4766 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4767 if (test_special_schedule(ctx, D, V, P, S) < 0)
4768 return -1;
4770 D = "{ A[a]; B[] }";
4771 V = "{}";
4772 P = "{ A[a] -> B[] }";
4773 if (test_has_schedule(ctx, D, V, P) < 0)
4774 return -1;
4776 if (test_padded_schedule(ctx) < 0)
4777 return -1;
4779 /* Check that check for progress is not confused by rational
4780 * solution.
4782 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4783 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4784 "i0 <= -2 + N; "
4785 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4786 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4787 P = "{}";
4788 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4789 if (test_has_schedule(ctx, D, V, P) < 0)
4790 return -1;
4791 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4793 /* Check that we allow schedule rows that are only non-trivial
4794 * on some full-dimensional domains.
4796 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4797 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4798 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4799 P = "{}";
4800 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4801 if (test_has_schedule(ctx, D, V, P) < 0)
4802 return -1;
4803 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4805 if (test_conditional_schedule_constraints(ctx) < 0)
4806 return -1;
4808 if (test_strongly_satisfying_schedule(ctx) < 0)
4809 return -1;
4811 if (test_conflicting_context_schedule(ctx) < 0)
4812 return -1;
4814 if (test_coalescing_schedule(ctx) < 0)
4815 return -1;
4816 if (test_skewing_schedule(ctx) < 0)
4817 return -1;
4819 return 0;
4822 /* Perform scheduling tests using the whole component scheduler.
4824 static int test_schedule_whole(isl_ctx *ctx)
4826 int whole;
4827 int r;
4829 whole = isl_options_get_schedule_whole_component(ctx);
4830 isl_options_set_schedule_whole_component(ctx, 1);
4831 r = test_schedule(ctx);
4832 isl_options_set_schedule_whole_component(ctx, whole);
4834 return r;
4837 /* Perform scheduling tests using the incremental scheduler.
4839 static int test_schedule_incremental(isl_ctx *ctx)
4841 int whole;
4842 int r;
4844 whole = isl_options_get_schedule_whole_component(ctx);
4845 isl_options_set_schedule_whole_component(ctx, 0);
4846 r = test_schedule(ctx);
4847 isl_options_set_schedule_whole_component(ctx, whole);
4849 return r;
4852 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4854 isl_union_map *umap;
4855 int test;
4857 umap = isl_union_map_read_from_str(ctx, str);
4858 test = isl_union_map_plain_is_injective(umap);
4859 isl_union_map_free(umap);
4860 if (test < 0)
4861 return -1;
4862 if (test == injective)
4863 return 0;
4864 if (injective)
4865 isl_die(ctx, isl_error_unknown,
4866 "map not detected as injective", return -1);
4867 else
4868 isl_die(ctx, isl_error_unknown,
4869 "map detected as injective", return -1);
4872 int test_injective(isl_ctx *ctx)
4874 const char *str;
4876 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4877 return -1;
4878 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4879 return -1;
4880 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4881 return -1;
4882 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4883 return -1;
4884 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4885 return -1;
4886 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4887 return -1;
4888 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4889 return -1;
4890 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4891 return -1;
4893 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4894 if (test_plain_injective(ctx, str, 1))
4895 return -1;
4896 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4897 if (test_plain_injective(ctx, str, 0))
4898 return -1;
4900 return 0;
4903 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4905 isl_aff *aff2;
4906 int equal;
4908 if (!aff)
4909 return -1;
4911 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4912 equal = isl_aff_plain_is_equal(aff, aff2);
4913 isl_aff_free(aff2);
4915 return equal;
4918 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4920 int equal;
4922 equal = aff_plain_is_equal(aff, str);
4923 if (equal < 0)
4924 return -1;
4925 if (!equal)
4926 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4927 "result not as expected", return -1);
4928 return 0;
4931 /* Is "upa" obviously equal to the isl_union_pw_aff represented by "str"?
4933 static isl_bool union_pw_aff_plain_is_equal(__isl_keep isl_union_pw_aff *upa,
4934 const char *str)
4936 isl_ctx *ctx;
4937 isl_union_pw_aff *upa2;
4938 isl_bool equal;
4940 if (!upa)
4941 return isl_bool_error;
4943 ctx = isl_union_pw_aff_get_ctx(upa);
4944 upa2 = isl_union_pw_aff_read_from_str(ctx, str);
4945 equal = isl_union_pw_aff_plain_is_equal(upa, upa2);
4946 isl_union_pw_aff_free(upa2);
4948 return equal;
4951 /* Check that "upa" is obviously equal to the isl_union_pw_aff
4952 * represented by "str".
4954 static isl_stat union_pw_aff_check_plain_equal(__isl_keep isl_union_pw_aff *upa,
4955 const char *str)
4957 isl_bool equal;
4959 equal = union_pw_aff_plain_is_equal(upa, str);
4960 if (equal < 0)
4961 return isl_stat_error;
4962 if (!equal)
4963 isl_die(isl_union_pw_aff_get_ctx(upa), isl_error_unknown,
4964 "result not as expected", return isl_stat_error);
4965 return isl_stat_ok;
4968 /* Basic tests on isl_union_pw_aff.
4970 * In particular, check that isl_union_pw_aff_aff_on_domain
4971 * aligns the parameters of the input objects and
4972 * that isl_union_pw_aff_param_on_domain_id properly
4973 * introduces the parameter.
4975 static int test_upa(isl_ctx *ctx)
4977 const char *str;
4978 isl_id *id;
4979 isl_aff *aff;
4980 isl_union_set *domain;
4981 isl_union_pw_aff *upa;
4982 isl_stat ok;
4984 aff = isl_aff_read_from_str(ctx, "[N] -> { [N] }");
4985 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
4986 domain = isl_union_set_read_from_str(ctx, str);
4987 upa = isl_union_pw_aff_aff_on_domain(domain, aff);
4988 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
4989 ok = union_pw_aff_check_plain_equal(upa, str);
4990 isl_union_pw_aff_free(upa);
4991 if (ok < 0)
4992 return -1;
4994 id = isl_id_alloc(ctx, "N", NULL);
4995 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
4996 domain = isl_union_set_read_from_str(ctx, str);
4997 upa = isl_union_pw_aff_param_on_domain_id(domain, id);
4998 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
4999 ok = union_pw_aff_check_plain_equal(upa, str);
5000 isl_union_pw_aff_free(upa);
5001 if (ok < 0)
5002 return -1;
5004 return 0;
5007 struct {
5008 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5009 __isl_take isl_aff *aff2);
5010 } aff_bin_op[] = {
5011 ['+'] = { &isl_aff_add },
5012 ['-'] = { &isl_aff_sub },
5013 ['*'] = { &isl_aff_mul },
5014 ['/'] = { &isl_aff_div },
5017 struct {
5018 const char *arg1;
5019 unsigned char op;
5020 const char *arg2;
5021 const char *res;
5022 } aff_bin_tests[] = {
5023 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
5024 "{ [i] -> [2i] }" },
5025 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
5026 "{ [i] -> [0] }" },
5027 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
5028 "{ [i] -> [2i] }" },
5029 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
5030 "{ [i] -> [2i] }" },
5031 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
5032 "{ [i] -> [i/2] }" },
5033 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
5034 "{ [i] -> [i] }" },
5035 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
5036 "{ [i] -> [NaN] }" },
5037 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
5038 "{ [i] -> [NaN] }" },
5039 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
5040 "{ [i] -> [NaN] }" },
5041 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
5042 "{ [i] -> [NaN] }" },
5043 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
5044 "{ [i] -> [NaN] }" },
5045 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
5046 "{ [i] -> [NaN] }" },
5047 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
5048 "{ [i] -> [NaN] }" },
5049 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
5050 "{ [i] -> [NaN] }" },
5051 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
5052 "{ [i] -> [NaN] }" },
5053 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
5054 "{ [i] -> [NaN] }" },
5055 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
5056 "{ [i] -> [NaN] }" },
5057 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
5058 "{ [i] -> [NaN] }" },
5061 /* Perform some basic tests of binary operations on isl_aff objects.
5063 static int test_bin_aff(isl_ctx *ctx)
5065 int i;
5066 isl_aff *aff1, *aff2, *res;
5067 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5068 __isl_take isl_aff *aff2);
5069 int ok;
5071 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
5072 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
5073 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
5074 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
5075 fn = aff_bin_op[aff_bin_tests[i].op].fn;
5076 aff1 = fn(aff1, aff2);
5077 if (isl_aff_is_nan(res))
5078 ok = isl_aff_is_nan(aff1);
5079 else
5080 ok = isl_aff_plain_is_equal(aff1, res);
5081 isl_aff_free(aff1);
5082 isl_aff_free(res);
5083 if (ok < 0)
5084 return -1;
5085 if (!ok)
5086 isl_die(ctx, isl_error_unknown,
5087 "unexpected result", return -1);
5090 return 0;
5093 struct {
5094 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
5095 __isl_take isl_pw_aff *pa2);
5096 } pw_aff_bin_op[] = {
5097 ['m'] = { &isl_pw_aff_min },
5098 ['M'] = { &isl_pw_aff_max },
5101 /* Inputs for binary isl_pw_aff operation tests.
5102 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
5103 * defined by pw_aff_bin_op, and "res" is the expected result.
5105 struct {
5106 const char *arg1;
5107 unsigned char op;
5108 const char *arg2;
5109 const char *res;
5110 } pw_aff_bin_tests[] = {
5111 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
5112 "{ [i] -> [i] }" },
5113 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
5114 "{ [i] -> [i] }" },
5115 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
5116 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
5117 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
5118 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
5119 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
5120 "{ [i] -> [NaN] }" },
5121 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
5122 "{ [i] -> [NaN] }" },
5125 /* Perform some basic tests of binary operations on isl_pw_aff objects.
5127 static int test_bin_pw_aff(isl_ctx *ctx)
5129 int i;
5130 isl_bool ok;
5131 isl_pw_aff *pa1, *pa2, *res;
5133 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
5134 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
5135 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
5136 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
5137 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
5138 if (isl_pw_aff_involves_nan(res))
5139 ok = isl_pw_aff_involves_nan(pa1);
5140 else
5141 ok = isl_pw_aff_plain_is_equal(pa1, res);
5142 isl_pw_aff_free(pa1);
5143 isl_pw_aff_free(res);
5144 if (ok < 0)
5145 return -1;
5146 if (!ok)
5147 isl_die(ctx, isl_error_unknown,
5148 "unexpected result", return -1);
5151 return 0;
5154 struct {
5155 __isl_give isl_union_pw_multi_aff *(*fn)(
5156 __isl_take isl_union_pw_multi_aff *upma1,
5157 __isl_take isl_union_pw_multi_aff *upma2);
5158 const char *arg1;
5159 const char *arg2;
5160 const char *res;
5161 } upma_bin_tests[] = {
5162 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
5163 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
5164 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
5165 "{ B[x] -> [2] : x >= 0 }",
5166 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
5167 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
5168 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
5169 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
5170 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
5171 "D[i] -> B[2] : i >= 5 }" },
5172 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5173 "{ B[x] -> C[2] : x > 0 }",
5174 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
5175 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5176 "{ B[x] -> A[2] : x >= 0 }",
5177 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
5180 /* Perform some basic tests of binary operations on
5181 * isl_union_pw_multi_aff objects.
5183 static int test_bin_upma(isl_ctx *ctx)
5185 int i;
5186 isl_union_pw_multi_aff *upma1, *upma2, *res;
5187 int ok;
5189 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
5190 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5191 upma_bin_tests[i].arg1);
5192 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5193 upma_bin_tests[i].arg2);
5194 res = isl_union_pw_multi_aff_read_from_str(ctx,
5195 upma_bin_tests[i].res);
5196 upma1 = upma_bin_tests[i].fn(upma1, upma2);
5197 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
5198 isl_union_pw_multi_aff_free(upma1);
5199 isl_union_pw_multi_aff_free(res);
5200 if (ok < 0)
5201 return -1;
5202 if (!ok)
5203 isl_die(ctx, isl_error_unknown,
5204 "unexpected result", return -1);
5207 return 0;
5210 struct {
5211 __isl_give isl_union_pw_multi_aff *(*fn)(
5212 __isl_take isl_union_pw_multi_aff *upma1,
5213 __isl_take isl_union_pw_multi_aff *upma2);
5214 const char *arg1;
5215 const char *arg2;
5216 } upma_bin_fail_tests[] = {
5217 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5218 "{ B[x] -> C[2] : x >= 0 }" },
5221 /* Perform some basic tests of binary operations on
5222 * isl_union_pw_multi_aff objects that are expected to fail.
5224 static int test_bin_upma_fail(isl_ctx *ctx)
5226 int i, n;
5227 isl_union_pw_multi_aff *upma1, *upma2;
5228 int on_error;
5230 on_error = isl_options_get_on_error(ctx);
5231 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
5232 n = ARRAY_SIZE(upma_bin_fail_tests);
5233 for (i = 0; i < n; ++i) {
5234 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5235 upma_bin_fail_tests[i].arg1);
5236 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5237 upma_bin_fail_tests[i].arg2);
5238 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
5239 isl_union_pw_multi_aff_free(upma1);
5240 if (upma1)
5241 break;
5243 isl_options_set_on_error(ctx, on_error);
5244 if (i < n)
5245 isl_die(ctx, isl_error_unknown,
5246 "operation not expected to succeed", return -1);
5248 return 0;
5251 /* Inputs for basic tests of unary operations on isl_multi_pw_aff objects.
5252 * "fn" is the function that is tested.
5253 * "arg" is a string description of the input.
5254 * "res" is a string description of the expected result.
5256 struct {
5257 __isl_give isl_multi_pw_aff *(*fn)(__isl_take isl_multi_pw_aff *mpa);
5258 const char *arg;
5259 const char *res;
5260 } mpa_un_tests[] = {
5261 { &isl_multi_pw_aff_range_factor_domain,
5262 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5263 "{ A[x] -> B[(1 : x >= 5)] }" },
5264 { &isl_multi_pw_aff_range_factor_range,
5265 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5266 "{ A[y] -> C[(2 : y <= 10)] }" },
5267 { &isl_multi_pw_aff_range_factor_domain,
5268 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5269 "{ A[x] -> B[(1 : x >= 5)] }" },
5270 { &isl_multi_pw_aff_range_factor_range,
5271 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5272 "{ A[y] -> C[] }" },
5273 { &isl_multi_pw_aff_range_factor_domain,
5274 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5275 "{ A[x] -> B[] }" },
5276 { &isl_multi_pw_aff_range_factor_range,
5277 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5278 "{ A[y] -> C[(2 : y <= 10)] }" },
5279 { &isl_multi_pw_aff_range_factor_domain,
5280 "{ A[x] -> [B[] -> C[]] }",
5281 "{ A[x] -> B[] }" },
5282 { &isl_multi_pw_aff_range_factor_range,
5283 "{ A[x] -> [B[] -> C[]] }",
5284 "{ A[y] -> C[] }" },
5285 { &isl_multi_pw_aff_factor_range,
5286 "{ [B[] -> C[]] }",
5287 "{ C[] }" },
5288 { &isl_multi_pw_aff_range_factor_domain,
5289 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5290 "{ A[x] -> B[] : x >= 0 }" },
5291 { &isl_multi_pw_aff_range_factor_range,
5292 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5293 "{ A[y] -> C[] : y >= 0 }" },
5294 { &isl_multi_pw_aff_factor_range,
5295 "[N] -> { [B[] -> C[]] : N >= 0 }",
5296 "[N] -> { C[] : N >= 0 }" },
5299 /* Perform some basic tests of unary operations on isl_multi_pw_aff objects.
5301 static int test_un_mpa(isl_ctx *ctx)
5303 int i;
5304 isl_bool ok;
5305 isl_multi_pw_aff *mpa, *res;
5307 for (i = 0; i < ARRAY_SIZE(mpa_un_tests); ++i) {
5308 mpa = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].arg);
5309 res = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].res);
5310 mpa = mpa_un_tests[i].fn(mpa);
5311 ok = isl_multi_pw_aff_plain_is_equal(mpa, res);
5312 isl_multi_pw_aff_free(mpa);
5313 isl_multi_pw_aff_free(res);
5314 if (ok < 0)
5315 return -1;
5316 if (!ok)
5317 isl_die(ctx, isl_error_unknown,
5318 "unexpected result", return -1);
5321 return 0;
5324 /* Inputs for basic tests of binary operations on isl_multi_pw_aff objects.
5325 * "fn" is the function that is tested.
5326 * "arg1" and "arg2" are string descriptions of the inputs.
5327 * "res" is a string description of the expected result.
5329 struct {
5330 __isl_give isl_multi_pw_aff *(*fn)(
5331 __isl_take isl_multi_pw_aff *mpa1,
5332 __isl_take isl_multi_pw_aff *mpa2);
5333 const char *arg1;
5334 const char *arg2;
5335 const char *res;
5336 } mpa_bin_tests[] = {
5337 { &isl_multi_pw_aff_add, "{ A[] -> [1] }", "{ A[] -> [2] }",
5338 "{ A[] -> [3] }" },
5339 { &isl_multi_pw_aff_add, "{ A[x] -> [(1 : x >= 5)] }",
5340 "{ A[x] -> [(x : x <= 10)] }",
5341 "{ A[x] -> [(1 + x : 5 <= x <= 10)] }" },
5342 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5343 "{ A[x] -> [] : x <= 10 }",
5344 "{ A[x] -> [] : 5 <= x <= 10 }" },
5345 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5346 "[N] -> { A[x] -> [] : x <= N }",
5347 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5348 { &isl_multi_pw_aff_add,
5349 "[N] -> { A[x] -> [] : x <= N }",
5350 "{ A[x] -> [] : x >= 5 }",
5351 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5352 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5353 "{ A[y] -> C[(2 : y <= 10)] }",
5354 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5355 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5356 "{ A[y] -> C[2] : y <= 10 }",
5357 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5358 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5359 "[N] -> { A[y] -> C[2] : y <= N }",
5360 "[N] -> { A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= N)]] }" },
5361 { &isl_multi_pw_aff_range_product, "[N] -> { A[x] -> B[1] : x >= N }",
5362 "{ A[y] -> C[2] : y <= 10 }",
5363 "[N] -> { A[x] -> [B[(1 : x >= N)] -> C[(2 : x <= 10)]] }" },
5364 { &isl_multi_pw_aff_range_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5365 "{ A[] -> [B[1] -> C[2]] }" },
5366 { &isl_multi_pw_aff_range_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5367 "{ A[] -> [B[] -> C[]] }" },
5368 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5369 "{ A[y] -> C[] : y <= 10 }",
5370 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] : x <= 10 }" },
5371 { &isl_multi_pw_aff_range_product, "{ A[y] -> C[] : y <= 10 }",
5372 "{ A[x] -> B[(1 : x >= 5)] }",
5373 "{ A[x] -> [C[] -> B[(1 : x >= 5)]] : x <= 10 }" },
5374 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5375 "{ A[y] -> C[(2 : y <= 10)] }",
5376 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[(2 : y <= 10)]] }" },
5377 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5378 "{ A[y] -> C[] : y <= 10 }",
5379 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= 10 }" },
5380 { &isl_multi_pw_aff_product, "{ A[y] -> C[] : y <= 10 }",
5381 "{ A[x] -> B[(1 : x >= 5)] }",
5382 "{ [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= 10 }" },
5383 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5384 "[N] -> { A[y] -> C[] : y <= N }",
5385 "[N] -> { [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= N }" },
5386 { &isl_multi_pw_aff_product, "[N] -> { A[y] -> C[] : y <= N }",
5387 "{ A[x] -> B[(1 : x >= 5)] }",
5388 "[N] -> { [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= N }" },
5389 { &isl_multi_pw_aff_product, "{ A[x] -> B[] : x >= 5 }",
5390 "{ A[y] -> C[] : y <= 10 }",
5391 "{ [A[x] -> A[y]] -> [B[] -> C[]] : x >= 5 and y <= 10 }" },
5392 { &isl_multi_pw_aff_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5393 "{ [A[] -> A[]] -> [B[1] -> C[2]] }" },
5394 { &isl_multi_pw_aff_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5395 "{ [A[] -> A[]] -> [B[] -> C[]] }" },
5396 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5397 "{ B[i,j] -> C[i + 2j] }", "{ A[a,b] -> B[b,a] }",
5398 "{ A[a,b] -> C[b + 2a] }" },
5399 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5400 "{ B[i,j] -> C[i + 2j] }",
5401 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5402 "{ A[a,b] -> C[(b + 2a : b > a)] }" },
5403 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5404 "{ B[i,j] -> C[(i + 2j : j > 4)] }",
5405 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5406 "{ A[a,b] -> C[(b + 2a : b > a > 4)] }" },
5407 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5408 "{ B[i,j] -> C[] }",
5409 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5410 "{ A[a,b] -> C[] }" },
5411 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5412 "{ B[i,j] -> C[] : i > j }",
5413 "{ A[a,b] -> B[b,a] }",
5414 "{ A[a,b] -> C[] : b > a }" },
5415 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5416 "{ B[i,j] -> C[] : j > 5 }",
5417 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5418 "{ A[a,b] -> C[] : b > a > 5 }" },
5419 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5420 "[N] -> { B[i,j] -> C[] : j > N }",
5421 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5422 "[N] -> { A[a,b] -> C[] : b > a > N }" },
5423 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5424 "[M,N] -> { B[] -> C[] : N > 5 }",
5425 "[M,N] -> { A[] -> B[] : M > N }",
5426 "[M,N] -> { A[] -> C[] : M > N > 5 }" },
5429 /* Perform some basic tests of binary operations on isl_multi_pw_aff objects.
5431 static int test_bin_mpa(isl_ctx *ctx)
5433 int i;
5434 isl_bool ok;
5435 isl_multi_pw_aff *mpa1, *mpa2, *res;
5437 for (i = 0; i < ARRAY_SIZE(mpa_bin_tests); ++i) {
5438 mpa1 = isl_multi_pw_aff_read_from_str(ctx,
5439 mpa_bin_tests[i].arg1);
5440 mpa2 = isl_multi_pw_aff_read_from_str(ctx,
5441 mpa_bin_tests[i].arg2);
5442 res = isl_multi_pw_aff_read_from_str(ctx,
5443 mpa_bin_tests[i].res);
5444 mpa1 = mpa_bin_tests[i].fn(mpa1, mpa2);
5445 ok = isl_multi_pw_aff_plain_is_equal(mpa1, res);
5446 isl_multi_pw_aff_free(mpa1);
5447 isl_multi_pw_aff_free(res);
5448 if (ok < 0)
5449 return -1;
5450 if (!ok)
5451 isl_die(ctx, isl_error_unknown,
5452 "unexpected result", return -1);
5455 return 0;
5458 /* Inputs for basic tests of unary operations on
5459 * isl_multi_union_pw_aff objects.
5460 * "fn" is the function that is tested.
5461 * "arg" is a string description of the input.
5462 * "res" is a string description of the expected result.
5464 struct {
5465 __isl_give isl_multi_union_pw_aff *(*fn)(
5466 __isl_take isl_multi_union_pw_aff *mupa);
5467 const char *arg;
5468 const char *res;
5469 } mupa_un_tests[] = {
5470 { &isl_multi_union_pw_aff_factor_range,
5471 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]",
5472 "C[{ A[] -> [2] }]" },
5473 { &isl_multi_union_pw_aff_factor_range,
5474 "[B[] -> C[{ A[] -> [2] }]]",
5475 "C[{ A[] -> [2] }]" },
5476 { &isl_multi_union_pw_aff_factor_range,
5477 "[B[{ A[] -> [1] }] -> C[]]",
5478 "C[]" },
5479 { &isl_multi_union_pw_aff_factor_range,
5480 "[B[] -> C[]]",
5481 "C[]" },
5482 { &isl_multi_union_pw_aff_factor_range,
5483 "([B[] -> C[]] : { A[x] : x >= 0 })",
5484 "(C[] : { A[x] : x >= 0 })" },
5485 { &isl_multi_union_pw_aff_factor_range,
5486 "[N] -> ([B[] -> C[]] : { A[x] : x <= N })",
5487 "[N] -> (C[] : { A[x] : x <= N })" },
5488 { &isl_multi_union_pw_aff_factor_range,
5489 "[N] -> ([B[] -> C[]] : { : N >= 0 })",
5490 "[N] -> (C[] : { : N >= 0 })" },
5493 /* Perform some basic tests of unary operations on
5494 * isl_multi_union_pw_aff objects.
5496 static int test_un_mupa(isl_ctx *ctx)
5498 int i;
5499 isl_bool ok;
5500 isl_multi_union_pw_aff *mupa, *res;
5502 for (i = 0; i < ARRAY_SIZE(mupa_un_tests); ++i) {
5503 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5504 mupa_un_tests[i].arg);
5505 res = isl_multi_union_pw_aff_read_from_str(ctx,
5506 mupa_un_tests[i].res);
5507 mupa = mupa_un_tests[i].fn(mupa);
5508 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5509 isl_multi_union_pw_aff_free(mupa);
5510 isl_multi_union_pw_aff_free(res);
5511 if (ok < 0)
5512 return -1;
5513 if (!ok)
5514 isl_die(ctx, isl_error_unknown,
5515 "unexpected result", return -1);
5518 return 0;
5521 /* Inputs for basic tests of binary operations on
5522 * isl_multi_union_pw_aff objects.
5523 * "fn" is the function that is tested.
5524 * "arg1" and "arg2" are string descriptions of the inputs.
5525 * "res" is a string description of the expected result.
5527 struct {
5528 __isl_give isl_multi_union_pw_aff *(*fn)(
5529 __isl_take isl_multi_union_pw_aff *mupa1,
5530 __isl_take isl_multi_union_pw_aff *mupa2);
5531 const char *arg1;
5532 const char *arg2;
5533 const char *res;
5534 } mupa_bin_tests[] = {
5535 { &isl_multi_union_pw_aff_add, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5536 "[{ A[] -> [3] }]" },
5537 { &isl_multi_union_pw_aff_sub, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5538 "[{ A[] -> [-1] }]" },
5539 { &isl_multi_union_pw_aff_add,
5540 "[{ A[] -> [1]; B[] -> [4] }]",
5541 "[{ A[] -> [2]; C[] -> [5] }]",
5542 "[{ A[] -> [3] }]" },
5543 { &isl_multi_union_pw_aff_union_add,
5544 "[{ A[] -> [1]; B[] -> [4] }]",
5545 "[{ A[] -> [2]; C[] -> [5] }]",
5546 "[{ A[] -> [3]; B[] -> [4]; C[] -> [5] }]" },
5547 { &isl_multi_union_pw_aff_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5548 "[{ A[x] -> [(x)] : x <= 10 }]",
5549 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10 }]" },
5550 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5551 "([] : { A[x] : x <= 10 })",
5552 "([] : { A[x] : 5 <= x <= 10 })" },
5553 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5554 "[N] -> ([] : { A[x] : x <= N })",
5555 "[N] -> ([] : { A[x] : 5 <= x <= N })" },
5556 { &isl_multi_union_pw_aff_add, "[N] -> ([] : { A[x] : x >= N })",
5557 "([] : { A[x] : x <= 10 })",
5558 "[N] -> ([] : { A[x] : N <= x <= 10 })" },
5559 { &isl_multi_union_pw_aff_union_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5560 "[{ A[x] -> [(x)] : x <= 10 }]",
5561 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10; "
5562 "A[x] -> [(1)] : x > 10; A[x] -> [(x)] : x < 5 }]" },
5563 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 5 })",
5564 "([] : { A[x] : x <= 10 })",
5565 "([] : { A[x] })" },
5566 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 0 })",
5567 "[N] -> ([] : { A[x] : x >= N })",
5568 "[N] -> ([] : { A[x] : x >= 0 or x >= N })" },
5569 { &isl_multi_union_pw_aff_union_add,
5570 "[N] -> ([] : { A[] : N >= 0})",
5571 "[N] -> ([] : { A[] : N <= 0})",
5572 "[N] -> ([] : { A[] })" },
5573 { &isl_multi_union_pw_aff_union_add,
5574 "[N] -> ([] : { A[] })",
5575 "[N] -> ([] : { : })",
5576 "[N] -> ([] : { : })" },
5577 { &isl_multi_union_pw_aff_union_add,
5578 "[N] -> ([] : { : })",
5579 "[N] -> ([] : { A[] })",
5580 "[N] -> ([] : { : })" },
5581 { &isl_multi_union_pw_aff_union_add,
5582 "[N] -> ([] : { : N >= 0})",
5583 "[N] -> ([] : { : N <= 0})",
5584 "[N] -> ([] : { : })" },
5585 { &isl_multi_union_pw_aff_range_product,
5586 "B[{ A[] -> [1] }]",
5587 "C[{ A[] -> [2] }]",
5588 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]" },
5589 { &isl_multi_union_pw_aff_range_product,
5590 "(B[] : { A[x] : x >= 5 })",
5591 "(C[] : { A[x] : x <= 10 })",
5592 "([B[] -> C[]] : { A[x] : 5 <= x <= 10 })" },
5593 { &isl_multi_union_pw_aff_range_product,
5594 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5595 "(C[] : { A[x] : x <= 10 })",
5596 "[B[{ A[x] -> [x + 1] : 5 <= x <= 10 }] -> C[]]" },
5597 { &isl_multi_union_pw_aff_range_product,
5598 "(C[] : { A[x] : x <= 10 })",
5599 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5600 "[C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= 10 }]]" },
5601 { &isl_multi_union_pw_aff_range_product,
5602 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5603 "[N] -> (C[] : { A[x] : x <= N })",
5604 "[N] -> [B[{ A[x] -> [x + 1] : 5 <= x <= N }] -> C[]]" },
5605 { &isl_multi_union_pw_aff_range_product,
5606 "[N] -> (C[] : { A[x] : x <= N })",
5607 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5608 "[N] -> [C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= N }]]" },
5609 { &isl_multi_union_pw_aff_range_product,
5610 "B[{ A[] -> [1]; D[] -> [3] }]",
5611 "C[{ A[] -> [2] }]",
5612 "[B[{ A[] -> [1]; D[] -> [3] }] -> C[{ A[] -> [2] }]]" },
5613 { &isl_multi_union_pw_aff_range_product,
5614 "B[] }]",
5615 "(C[] : { A[x] })",
5616 "([B[] -> C[]] : { A[x] })" },
5617 { &isl_multi_union_pw_aff_range_product,
5618 "(B[] : { A[x] })",
5619 "C[] }]",
5620 "([B[] -> C[]] : { A[x] })" },
5623 /* Perform some basic tests of binary operations on
5624 * isl_multi_union_pw_aff objects.
5626 static int test_bin_mupa(isl_ctx *ctx)
5628 int i;
5629 isl_bool ok;
5630 isl_multi_union_pw_aff *mupa1, *mupa2, *res;
5632 for (i = 0; i < ARRAY_SIZE(mupa_bin_tests); ++i) {
5633 mupa1 = isl_multi_union_pw_aff_read_from_str(ctx,
5634 mupa_bin_tests[i].arg1);
5635 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx,
5636 mupa_bin_tests[i].arg2);
5637 res = isl_multi_union_pw_aff_read_from_str(ctx,
5638 mupa_bin_tests[i].res);
5639 mupa1 = mupa_bin_tests[i].fn(mupa1, mupa2);
5640 ok = isl_multi_union_pw_aff_plain_is_equal(mupa1, res);
5641 isl_multi_union_pw_aff_free(mupa1);
5642 isl_multi_union_pw_aff_free(res);
5643 if (ok < 0)
5644 return -1;
5645 if (!ok)
5646 isl_die(ctx, isl_error_unknown,
5647 "unexpected result", return -1);
5650 return 0;
5653 /* Inputs for basic tests of binary operations on
5654 * pairs of isl_multi_union_pw_aff and isl_set objects.
5655 * "fn" is the function that is tested.
5656 * "arg1" and "arg2" are string descriptions of the inputs.
5657 * "res" is a string description of the expected result.
5659 struct {
5660 __isl_give isl_multi_union_pw_aff *(*fn)(
5661 __isl_take isl_multi_union_pw_aff *mupa,
5662 __isl_take isl_set *set);
5663 const char *arg1;
5664 const char *arg2;
5665 const char *res;
5666 } mupa_set_tests[] = {
5667 { &isl_multi_union_pw_aff_intersect_range,
5668 "C[{ B[i,j] -> [i + 2j] }]", "{ C[1] }",
5669 "C[{ B[i,j] -> [i + 2j] : i + 2j = 1 }]" },
5670 { &isl_multi_union_pw_aff_intersect_range,
5671 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[N] }",
5672 "[N] -> C[{ B[i,j] -> [i + 2j] : i + 2j = N }]" },
5673 { &isl_multi_union_pw_aff_intersect_range,
5674 "[N] -> C[{ B[i,j] -> [i + 2j + N] }]", "{ C[1] }",
5675 "[N] -> C[{ B[i,j] -> [i + 2j + N] : i + 2j + N = 1 }]" },
5676 { &isl_multi_union_pw_aff_intersect_range,
5677 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[x] : N >= 0 }",
5678 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0 }]" },
5679 { &isl_multi_union_pw_aff_intersect_range,
5680 "C[]", "{ C[] }", "C[]" },
5681 { &isl_multi_union_pw_aff_intersect_range,
5682 "[N] -> (C[] : { : N >= 0 })",
5683 "{ C[] }",
5684 "[N] -> (C[] : { : N >= 0 })" },
5685 { &isl_multi_union_pw_aff_intersect_range,
5686 "(C[] : { A[a,b] })",
5687 "{ C[] }",
5688 "(C[] : { A[a,b] })" },
5689 { &isl_multi_union_pw_aff_intersect_range,
5690 "[N] -> (C[] : { A[a,b] : a,b <= N })",
5691 "{ C[] }",
5692 "[N] -> (C[] : { A[a,b] : a,b <= N })" },
5693 { &isl_multi_union_pw_aff_intersect_range,
5694 "C[]",
5695 "[N] -> { C[] : N >= 0 }",
5696 "[N] -> (C[] : { : N >= 0 })" },
5697 { &isl_multi_union_pw_aff_intersect_range,
5698 "(C[] : { A[a,b] })",
5699 "[N] -> { C[] : N >= 0 }",
5700 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5701 { &isl_multi_union_pw_aff_intersect_range,
5702 "[N] -> (C[] : { : N >= 0 })",
5703 "[N] -> { C[] : N < 1024 }",
5704 "[N] -> (C[] : { : 0 <= N < 1024 })" },
5705 { &isl_multi_union_pw_aff_intersect_params,
5706 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { : N >= 0 }",
5707 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0}]" },
5708 { &isl_multi_union_pw_aff_intersect_params,
5709 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "[N] -> { : N >= 0 }",
5710 "[N] -> C[{ B[i,j] -> [i + 2j] : 0 <= N <= 256 }]" },
5711 { &isl_multi_union_pw_aff_intersect_params,
5712 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "{ : }",
5713 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]" },
5714 { &isl_multi_union_pw_aff_intersect_params,
5715 "C[]", "[N] -> { : N >= 0 }",
5716 "[N] -> (C[] : { : N >= 0 })" },
5717 { &isl_multi_union_pw_aff_intersect_params,
5718 "(C[] : { A[a,b] })", "[N] -> { : N >= 0 }",
5719 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5720 { &isl_multi_union_pw_aff_intersect_params,
5721 "[N] -> (C[] : { A[a,N] })", "{ : }",
5722 "[N] -> (C[] : { A[a,N] })" },
5723 { &isl_multi_union_pw_aff_intersect_params,
5724 "[N] -> (C[] : { A[a,b] : N <= 256 })", "[N] -> { : N >= 0 }",
5725 "[N] -> (C[] : { A[a,b] : 0 <= N <= 256 })" },
5728 /* Perform some basic tests of binary operations on
5729 * pairs of isl_multi_union_pw_aff and isl_set objects.
5731 static int test_mupa_set(isl_ctx *ctx)
5733 int i;
5734 isl_bool ok;
5735 isl_multi_union_pw_aff *mupa, *res;
5736 isl_set *set;
5738 for (i = 0; i < ARRAY_SIZE(mupa_set_tests); ++i) {
5739 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5740 mupa_set_tests[i].arg1);
5741 set = isl_set_read_from_str(ctx, mupa_set_tests[i].arg2);
5742 res = isl_multi_union_pw_aff_read_from_str(ctx,
5743 mupa_set_tests[i].res);
5744 mupa = mupa_set_tests[i].fn(mupa, set);
5745 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5746 isl_multi_union_pw_aff_free(mupa);
5747 isl_multi_union_pw_aff_free(res);
5748 if (ok < 0)
5749 return -1;
5750 if (!ok)
5751 isl_die(ctx, isl_error_unknown,
5752 "unexpected result", return -1);
5755 return 0;
5758 /* Inputs for basic tests of binary operations on
5759 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5760 * "fn" is the function that is tested.
5761 * "arg1" and "arg2" are string descriptions of the inputs.
5762 * "res" is a string description of the expected result.
5764 struct {
5765 __isl_give isl_multi_union_pw_aff *(*fn)(
5766 __isl_take isl_multi_union_pw_aff *mupa,
5767 __isl_take isl_union_set *uset);
5768 const char *arg1;
5769 const char *arg2;
5770 const char *res;
5771 } mupa_uset_tests[] = {
5772 { &isl_multi_union_pw_aff_intersect_domain,
5773 "C[{ B[i,j] -> [i + 2j] }]", "{ B[i,i] }",
5774 "C[{ B[i,i] -> [3i] }]" },
5775 { &isl_multi_union_pw_aff_intersect_domain,
5776 "(C[] : { B[i,j] })", "{ B[i,i] }",
5777 "(C[] : { B[i,i] })" },
5778 { &isl_multi_union_pw_aff_intersect_domain,
5779 "(C[] : { B[i,j] })", "[N] -> { B[N,N] }",
5780 "[N] -> (C[] : { B[N,N] })" },
5781 { &isl_multi_union_pw_aff_intersect_domain,
5782 "C[]", "{ B[i,i] }",
5783 "(C[] : { B[i,i] })" },
5784 { &isl_multi_union_pw_aff_intersect_domain,
5785 "[N] -> (C[] : { : N >= 0 })", "{ B[i,i] }",
5786 "[N] -> (C[] : { B[i,i] : N >= 0 })" },
5789 /* Perform some basic tests of binary operations on
5790 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5792 static int test_mupa_uset(isl_ctx *ctx)
5794 int i;
5795 isl_bool ok;
5796 isl_multi_union_pw_aff *mupa, *res;
5797 isl_union_set *uset;
5799 for (i = 0; i < ARRAY_SIZE(mupa_uset_tests); ++i) {
5800 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5801 mupa_uset_tests[i].arg1);
5802 uset = isl_union_set_read_from_str(ctx,
5803 mupa_uset_tests[i].arg2);
5804 res = isl_multi_union_pw_aff_read_from_str(ctx,
5805 mupa_uset_tests[i].res);
5806 mupa = mupa_uset_tests[i].fn(mupa, uset);
5807 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5808 isl_multi_union_pw_aff_free(mupa);
5809 isl_multi_union_pw_aff_free(res);
5810 if (ok < 0)
5811 return -1;
5812 if (!ok)
5813 isl_die(ctx, isl_error_unknown,
5814 "unexpected result", return -1);
5817 return 0;
5820 /* Inputs for basic tests of binary operations on
5821 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5822 * "fn" is the function that is tested.
5823 * "arg1" and "arg2" are string descriptions of the inputs.
5824 * "res" is a string description of the expected result.
5826 struct {
5827 __isl_give isl_multi_union_pw_aff *(*fn)(
5828 __isl_take isl_multi_union_pw_aff *mupa,
5829 __isl_take isl_multi_aff *ma);
5830 const char *arg1;
5831 const char *arg2;
5832 const char *res;
5833 } mupa_ma_tests[] = {
5834 { &isl_multi_union_pw_aff_apply_multi_aff,
5835 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5836 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5837 "{ C[a,b] -> D[b,a] }",
5838 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
5839 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
5840 { &isl_multi_union_pw_aff_apply_multi_aff,
5841 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
5842 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5843 "{ C[a,b] -> D[b,a] }",
5844 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
5845 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
5846 { &isl_multi_union_pw_aff_apply_multi_aff,
5847 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5848 "[N] -> { C[a] -> D[a + N] }",
5849 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }] " },
5850 { &isl_multi_union_pw_aff_apply_multi_aff,
5851 "C[]",
5852 "{ C[] -> D[] }",
5853 "D[]" },
5854 { &isl_multi_union_pw_aff_apply_multi_aff,
5855 "[N] -> (C[] : { : N >= 0 })",
5856 "{ C[] -> D[] }",
5857 "[N] -> (D[] : { : N >= 0 })" },
5858 { &isl_multi_union_pw_aff_apply_multi_aff,
5859 "C[]",
5860 "[N] -> { C[] -> D[N] }",
5861 "[N] -> D[{ [N] }]" },
5862 { &isl_multi_union_pw_aff_apply_multi_aff,
5863 "(C[] : { A[i,j] : i >= j })",
5864 "{ C[] -> D[] }",
5865 "(D[] : { A[i,j] : i >= j })" },
5866 { &isl_multi_union_pw_aff_apply_multi_aff,
5867 "[N] -> (C[] : { A[i,j] : N >= 0 })",
5868 "{ C[] -> D[] }",
5869 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
5870 { &isl_multi_union_pw_aff_apply_multi_aff,
5871 "(C[] : { A[i,j] : i >= j })",
5872 "[N] -> { C[] -> D[N] }",
5873 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
5876 /* Perform some basic tests of binary operations on
5877 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5879 static int test_mupa_ma(isl_ctx *ctx)
5881 int i;
5882 isl_bool ok;
5883 isl_multi_union_pw_aff *mupa, *res;
5884 isl_multi_aff *ma;
5886 for (i = 0; i < ARRAY_SIZE(mupa_ma_tests); ++i) {
5887 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5888 mupa_ma_tests[i].arg1);
5889 ma = isl_multi_aff_read_from_str(ctx, mupa_ma_tests[i].arg2);
5890 res = isl_multi_union_pw_aff_read_from_str(ctx,
5891 mupa_ma_tests[i].res);
5892 mupa = mupa_ma_tests[i].fn(mupa, ma);
5893 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5894 isl_multi_union_pw_aff_free(mupa);
5895 isl_multi_union_pw_aff_free(res);
5896 if (ok < 0)
5897 return -1;
5898 if (!ok)
5899 isl_die(ctx, isl_error_unknown,
5900 "unexpected result", return -1);
5903 return 0;
5906 /* Inputs for basic tests of binary operations on
5907 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
5908 * "fn" is the function that is tested.
5909 * "arg1" and "arg2" are string descriptions of the inputs.
5910 * "res" is a string description of the expected result.
5912 struct {
5913 __isl_give isl_union_pw_aff *(*fn)(
5914 __isl_take isl_multi_union_pw_aff *mupa,
5915 __isl_take isl_pw_aff *pa);
5916 const char *arg1;
5917 const char *arg2;
5918 const char *res;
5919 } mupa_pa_tests[] = {
5920 { &isl_multi_union_pw_aff_apply_pw_aff,
5921 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5922 "[N] -> { C[a] -> [a + N] }",
5923 "[N] -> { A[i,j] -> [i + N]; B[i,j] -> [j + N] }" },
5924 { &isl_multi_union_pw_aff_apply_pw_aff,
5925 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5926 "{ C[a] -> [a] : a >= 0; C[a] -> [-a] : a < 0 }",
5927 "{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
5928 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }" },
5929 { &isl_multi_union_pw_aff_apply_pw_aff,
5930 "C[]",
5931 "[N] -> { C[] -> [N] }",
5932 "[N] -> { [N] }" },
5933 { &isl_multi_union_pw_aff_apply_pw_aff,
5934 "C[]",
5935 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
5936 "[N] -> { [N] : N >= 0; [-N] : N < 0 }" },
5937 { &isl_multi_union_pw_aff_apply_pw_aff,
5938 "[N] -> (C[] : { : N >= 0 })",
5939 "[N] -> { C[] -> [N] }",
5940 "[N] -> { [N] : N >= 0 }" },
5941 { &isl_multi_union_pw_aff_apply_pw_aff,
5942 "[N] -> (C[] : { : N >= 0 })",
5943 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
5944 "[N] -> { [N] : N >= 0 }" },
5945 { &isl_multi_union_pw_aff_apply_pw_aff,
5946 "[N] -> (C[] : { : N >= 0 })",
5947 "{ C[] -> [0] }",
5948 "[N] -> { [0] : N >= 0 }" },
5949 { &isl_multi_union_pw_aff_apply_pw_aff,
5950 "(C[] : { A[i,j] : i >= j })",
5951 "[N] -> { C[] -> [N] }",
5952 "[N] -> { A[i,j] -> [N] : i >= j }" },
5953 { &isl_multi_union_pw_aff_apply_pw_aff,
5954 "(C[] : { A[i,j] : i >= j })",
5955 "[N] -> { C[] -> [N] : N >= 0 }",
5956 "[N] -> { A[i,j] -> [N] : i >= j and N >= 0 }" },
5959 /* Perform some basic tests of binary operations on
5960 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
5962 static int test_mupa_pa(isl_ctx *ctx)
5964 int i;
5965 isl_bool ok;
5966 isl_multi_union_pw_aff *mupa;
5967 isl_union_pw_aff *upa, *res;
5968 isl_pw_aff *pa;
5970 for (i = 0; i < ARRAY_SIZE(mupa_pa_tests); ++i) {
5971 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5972 mupa_pa_tests[i].arg1);
5973 pa = isl_pw_aff_read_from_str(ctx, mupa_pa_tests[i].arg2);
5974 res = isl_union_pw_aff_read_from_str(ctx,
5975 mupa_pa_tests[i].res);
5976 upa = mupa_pa_tests[i].fn(mupa, pa);
5977 ok = isl_union_pw_aff_plain_is_equal(upa, res);
5978 isl_union_pw_aff_free(upa);
5979 isl_union_pw_aff_free(res);
5980 if (ok < 0)
5981 return -1;
5982 if (!ok)
5983 isl_die(ctx, isl_error_unknown,
5984 "unexpected result", return -1);
5987 return 0;
5990 /* Inputs for basic tests of binary operations on
5991 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
5992 * "fn" is the function that is tested.
5993 * "arg1" and "arg2" are string descriptions of the inputs.
5994 * "res" is a string description of the expected result.
5996 struct {
5997 __isl_give isl_multi_union_pw_aff *(*fn)(
5998 __isl_take isl_multi_union_pw_aff *mupa,
5999 __isl_take isl_pw_multi_aff *pma);
6000 const char *arg1;
6001 const char *arg2;
6002 const char *res;
6003 } mupa_pma_tests[] = {
6004 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6005 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6006 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6007 "{ C[a,b] -> D[b,a] }",
6008 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
6009 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
6010 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6011 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
6012 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6013 "{ C[a,b] -> D[b,a] }",
6014 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
6015 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
6016 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6017 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6018 "[N] -> { C[a] -> D[a + N] }",
6019 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }]" },
6020 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6021 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6022 "{ C[a] -> D[a] : a >= 0; C[a] -> D[-a] : a < 0 }",
6023 "D[{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
6024 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }]" },
6025 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6026 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6027 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6028 "{ C[a,b] -> D[a,b] : a >= b; C[a,b] -> D[b,a] : a < b }",
6029 "D[{ A[i,j] -> [i] : i >= j; A[i,j] -> [j] : i < j; "
6030 "B[i,j] -> [j] : i <= j; B[i,j] -> [i] : i > j }, "
6031 "{ A[i,j] -> [j] : i >= j; A[i,j] -> [i] : i < j; "
6032 "B[i,j] -> [i] : i <= j; B[i,j] -> [j] : i > j }]" },
6033 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6034 "C[]",
6035 "{ C[] -> D[] }",
6036 "D[]" },
6037 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6038 "[N] -> (C[] : { : N >= 0 })",
6039 "{ C[] -> D[] }",
6040 "[N] -> (D[] : { : N >= 0 })" },
6041 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6042 "C[]",
6043 "[N] -> { C[] -> D[N] }",
6044 "[N] -> D[{ [N] }]" },
6045 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6046 "(C[] : { A[i,j] : i >= j })",
6047 "{ C[] -> D[] }",
6048 "(D[] : { A[i,j] : i >= j })" },
6049 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6050 "[N] -> (C[] : { A[i,j] : N >= 0 })",
6051 "{ C[] -> D[] }",
6052 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
6053 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6054 "(C[] : { A[i,j] : i >= j })",
6055 "[N] -> { C[] -> D[N] }",
6056 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
6057 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6058 "C[]",
6059 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6060 "[N] -> D[{ [N] : N >= 0; [-N] : N < 0 }]" },
6061 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6062 "[N] -> (C[] : { : N >= 0 })",
6063 "[N] -> { C[] -> D[N] }",
6064 "[N] -> D[{ [N] : N >= 0 }]" },
6065 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6066 "[N] -> (C[] : { : N >= 0 })",
6067 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6068 "[N] -> D[{ [N] : N >= 0 }]" },
6069 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6070 "[N] -> (C[] : { : N >= 0 })",
6071 "{ C[] -> D[0] }",
6072 "[N] -> D[{ [0] : N >= 0 }]" },
6073 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6074 "(C[] : { A[i,j] : i >= j })",
6075 "[N] -> { C[] -> D[N] : N >= 0 }",
6076 "[N] -> D[{ A[i,j] -> [N] : i >= j and N >= 0 }]" },
6079 /* Perform some basic tests of binary operations on
6080 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6082 static int test_mupa_pma(isl_ctx *ctx)
6084 int i;
6085 isl_bool ok;
6086 isl_multi_union_pw_aff *mupa, *res;
6087 isl_pw_multi_aff *pma;
6089 for (i = 0; i < ARRAY_SIZE(mupa_pma_tests); ++i) {
6090 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6091 mupa_pma_tests[i].arg1);
6092 pma = isl_pw_multi_aff_read_from_str(ctx,
6093 mupa_pma_tests[i].arg2);
6094 res = isl_multi_union_pw_aff_read_from_str(ctx,
6095 mupa_pma_tests[i].res);
6096 mupa = mupa_pma_tests[i].fn(mupa, pma);
6097 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6098 isl_multi_union_pw_aff_free(mupa);
6099 isl_multi_union_pw_aff_free(res);
6100 if (ok < 0)
6101 return -1;
6102 if (!ok)
6103 isl_die(ctx, isl_error_unknown,
6104 "unexpected result", return -1);
6107 return 0;
6110 /* Inputs for basic tests of binary operations on
6111 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6112 * "fn" is the function that is tested.
6113 * "arg1" and "arg2" are string descriptions of the inputs.
6114 * "res" is a string description of the expected result.
6116 struct {
6117 __isl_give isl_multi_union_pw_aff *(*fn)(
6118 __isl_take isl_multi_union_pw_aff *mupa,
6119 __isl_take isl_union_pw_multi_aff *upma);
6120 const char *arg1;
6121 const char *arg2;
6122 const char *res;
6123 } mupa_upma_tests[] = {
6124 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6125 "C[{ B[i,j] -> [i + 2j] }]", "{ A[a,b] -> B[b,a] }",
6126 "C[{ A[a,b] -> [b + 2a] }]" },
6127 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6128 "C[{ B[i,j] -> [i + 2j] }]",
6129 "{ A[a,b] -> B[b,a] : b > a }",
6130 "C[{ A[a,b] -> [b + 2a] : b > a }]" },
6131 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6132 "C[{ B[i,j] -> [i + 2j] : j > 4 }]",
6133 "{ A[a,b] -> B[b,a] : b > a }",
6134 "C[{ A[a,b] -> [b + 2a] : b > a > 4 }]" },
6135 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6136 "C[{ B[i,j] -> [i + 2j] }]",
6137 "{ A[a,b] -> B[b,a] : a > b; A[a,b] -> B[a,b] : a <= b }",
6138 "C[{ A[a,b] -> [b + 2a] : a > b; A[a,b] -> [a + 2b] : a <= b }]" },
6139 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6140 "(C[] : { B[a,b] })",
6141 "{ A[a,b] -> B[b,a] }",
6142 "(C[] : { A[a,b] })" },
6143 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6144 "(C[] : { B[a,b] })",
6145 "{ B[a,b] -> A[b,a] }",
6146 "(C[] : { })" },
6147 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6148 "(C[] : { B[a,b] })",
6149 "{ A[a,b] -> B[b,a] : a > b }",
6150 "(C[] : { A[a,b] : a > b })" },
6151 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6152 "(C[] : { B[a,b] : a > b })",
6153 "{ A[a,b] -> B[b,a] }",
6154 "(C[] : { A[a,b] : b > a })" },
6155 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6156 "[N] -> (C[] : { B[a,b] : a > N })",
6157 "{ A[a,b] -> B[b,a] : a > b }",
6158 "[N] -> (C[] : { A[a,b] : a > b > N })" },
6159 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6160 "(C[] : { B[a,b] : a > b })",
6161 "[N] -> { A[a,b] -> B[b,a] : a > N }",
6162 "[N] -> (C[] : { A[a,b] : b > a > N })" },
6163 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6164 "C[]",
6165 "{ A[a,b] -> B[b,a] }",
6166 "C[]" },
6167 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6168 "[N] -> (C[] : { : N >= 0 })",
6169 "{ A[a,b] -> B[b,a] }",
6170 "[N] -> (C[] : { : N >= 0 })" },
6171 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6172 "C[]",
6173 "[N] -> { A[a,b] -> B[b,a] : N >= 0 }",
6174 "[N] -> (C[] : { : N >= 0 })" },
6177 /* Perform some basic tests of binary operations on
6178 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6180 static int test_mupa_upma(isl_ctx *ctx)
6182 int i;
6183 isl_bool ok;
6184 isl_multi_union_pw_aff *mupa, *res;
6185 isl_union_pw_multi_aff *upma;
6187 for (i = 0; i < ARRAY_SIZE(mupa_upma_tests); ++i) {
6188 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6189 mupa_upma_tests[i].arg1);
6190 upma = isl_union_pw_multi_aff_read_from_str(ctx,
6191 mupa_upma_tests[i].arg2);
6192 res = isl_multi_union_pw_aff_read_from_str(ctx,
6193 mupa_upma_tests[i].res);
6194 mupa = mupa_upma_tests[i].fn(mupa, upma);
6195 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6196 isl_multi_union_pw_aff_free(mupa);
6197 isl_multi_union_pw_aff_free(res);
6198 if (ok < 0)
6199 return -1;
6200 if (!ok)
6201 isl_die(ctx, isl_error_unknown,
6202 "unexpected result", return -1);
6205 return 0;
6208 /* Check that the input tuple of an isl_aff can be set properly.
6210 static isl_stat test_aff_set_tuple_id(isl_ctx *ctx)
6212 isl_id *id;
6213 isl_aff *aff;
6214 int equal;
6216 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x + 1] }");
6217 id = isl_id_alloc(ctx, "A", NULL);
6218 aff = isl_aff_set_tuple_id(aff, isl_dim_in, id);
6219 equal = aff_check_plain_equal(aff, "{ A[x] -> [x + 1] }");
6220 isl_aff_free(aff);
6221 if (equal < 0)
6222 return isl_stat_error;
6224 return isl_stat_ok;
6227 int test_aff(isl_ctx *ctx)
6229 const char *str;
6230 isl_set *set;
6231 isl_space *space;
6232 isl_local_space *ls;
6233 isl_aff *aff;
6234 int zero, equal;
6236 if (test_upa(ctx) < 0)
6237 return -1;
6238 if (test_bin_aff(ctx) < 0)
6239 return -1;
6240 if (test_bin_pw_aff(ctx) < 0)
6241 return -1;
6242 if (test_bin_upma(ctx) < 0)
6243 return -1;
6244 if (test_bin_upma_fail(ctx) < 0)
6245 return -1;
6246 if (test_un_mpa(ctx) < 0)
6247 return -1;
6248 if (test_bin_mpa(ctx) < 0)
6249 return -1;
6250 if (test_un_mupa(ctx) < 0)
6251 return -1;
6252 if (test_bin_mupa(ctx) < 0)
6253 return -1;
6254 if (test_mupa_set(ctx) < 0)
6255 return -1;
6256 if (test_mupa_uset(ctx) < 0)
6257 return -1;
6258 if (test_mupa_ma(ctx) < 0)
6259 return -1;
6260 if (test_mupa_pa(ctx) < 0)
6261 return -1;
6262 if (test_mupa_pma(ctx) < 0)
6263 return -1;
6264 if (test_mupa_upma(ctx) < 0)
6265 return -1;
6267 space = isl_space_set_alloc(ctx, 0, 1);
6268 ls = isl_local_space_from_space(space);
6269 aff = isl_aff_zero_on_domain(ls);
6271 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6272 aff = isl_aff_scale_down_ui(aff, 3);
6273 aff = isl_aff_floor(aff);
6274 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6275 aff = isl_aff_scale_down_ui(aff, 2);
6276 aff = isl_aff_floor(aff);
6277 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6279 str = "{ [10] }";
6280 set = isl_set_read_from_str(ctx, str);
6281 aff = isl_aff_gist(aff, set);
6283 aff = isl_aff_add_constant_si(aff, -16);
6284 zero = isl_aff_plain_is_zero(aff);
6285 isl_aff_free(aff);
6287 if (zero < 0)
6288 return -1;
6289 if (!zero)
6290 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6292 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
6293 aff = isl_aff_scale_down_ui(aff, 64);
6294 aff = isl_aff_floor(aff);
6295 equal = aff_check_plain_equal(aff, "{ [-1] }");
6296 isl_aff_free(aff);
6297 if (equal < 0)
6298 return -1;
6300 if (test_aff_set_tuple_id(ctx) < 0)
6301 return -1;
6303 return 0;
6306 /* Check that "pa" consists of a single expression.
6308 static int check_single_piece(isl_ctx *ctx, __isl_take isl_pw_aff *pa)
6310 isl_size n;
6312 n = isl_pw_aff_n_piece(pa);
6313 isl_pw_aff_free(pa);
6315 if (n < 0)
6316 return -1;
6317 if (n != 1)
6318 isl_die(ctx, isl_error_unknown, "expecting single expression",
6319 return -1);
6321 return 0;
6324 /* Check that the computation below results in a single expression.
6325 * One or two expressions may result depending on which constraint
6326 * ends up being considered as redundant with respect to the other
6327 * constraints after the projection that is performed internally
6328 * by isl_set_dim_min.
6330 static int test_dim_max_1(isl_ctx *ctx)
6332 const char *str;
6333 isl_set *set;
6334 isl_pw_aff *pa;
6336 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
6337 "-4a <= b <= 3 and b < n - 4a }";
6338 set = isl_set_read_from_str(ctx, str);
6339 pa = isl_set_dim_min(set, 0);
6340 return check_single_piece(ctx, pa);
6343 /* Check that the computation below results in a single expression.
6344 * The PIP problem corresponding to these constraints has a row
6345 * that causes a split of the solution domain. The solver should
6346 * first pick rows that split off empty parts such that the actual
6347 * solution domain does not get split.
6348 * Note that the description contains some redundant constraints.
6349 * If these constraints get removed first, then the row mentioned
6350 * above does not appear in the PIP problem.
6352 static int test_dim_max_2(isl_ctx *ctx)
6354 const char *str;
6355 isl_set *set;
6356 isl_pw_aff *pa;
6358 str = "[P, N] -> { [a] : a < N and a >= 0 and N > P and a <= P and "
6359 "N > 0 and P >= 0 }";
6360 set = isl_set_read_from_str(ctx, str);
6361 pa = isl_set_dim_max(set, 0);
6362 return check_single_piece(ctx, pa);
6365 int test_dim_max(isl_ctx *ctx)
6367 int equal;
6368 const char *str;
6369 isl_set *set1, *set2;
6370 isl_set *set;
6371 isl_map *map;
6372 isl_pw_aff *pwaff;
6374 if (test_dim_max_1(ctx) < 0)
6375 return -1;
6376 if (test_dim_max_2(ctx) < 0)
6377 return -1;
6379 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
6380 set = isl_set_read_from_str(ctx, str);
6381 pwaff = isl_set_dim_max(set, 0);
6382 set1 = isl_set_from_pw_aff(pwaff);
6383 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
6384 set2 = isl_set_read_from_str(ctx, str);
6385 equal = isl_set_is_equal(set1, set2);
6386 isl_set_free(set1);
6387 isl_set_free(set2);
6388 if (equal < 0)
6389 return -1;
6390 if (!equal)
6391 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6393 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
6394 set = isl_set_read_from_str(ctx, str);
6395 pwaff = isl_set_dim_max(set, 0);
6396 set1 = isl_set_from_pw_aff(pwaff);
6397 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
6398 set2 = isl_set_read_from_str(ctx, str);
6399 equal = isl_set_is_equal(set1, set2);
6400 isl_set_free(set1);
6401 isl_set_free(set2);
6402 if (equal < 0)
6403 return -1;
6404 if (!equal)
6405 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6407 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
6408 set = isl_set_read_from_str(ctx, str);
6409 pwaff = isl_set_dim_max(set, 0);
6410 set1 = isl_set_from_pw_aff(pwaff);
6411 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
6412 set2 = isl_set_read_from_str(ctx, str);
6413 equal = isl_set_is_equal(set1, set2);
6414 isl_set_free(set1);
6415 isl_set_free(set2);
6416 if (equal < 0)
6417 return -1;
6418 if (!equal)
6419 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6421 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
6422 "0 <= i < N and 0 <= j < M }";
6423 map = isl_map_read_from_str(ctx, str);
6424 set = isl_map_range(map);
6426 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
6427 set1 = isl_set_from_pw_aff(pwaff);
6428 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
6429 set2 = isl_set_read_from_str(ctx, str);
6430 equal = isl_set_is_equal(set1, set2);
6431 isl_set_free(set1);
6432 isl_set_free(set2);
6434 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
6435 set1 = isl_set_from_pw_aff(pwaff);
6436 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
6437 set2 = isl_set_read_from_str(ctx, str);
6438 if (equal >= 0 && equal)
6439 equal = isl_set_is_equal(set1, set2);
6440 isl_set_free(set1);
6441 isl_set_free(set2);
6443 isl_set_free(set);
6445 if (equal < 0)
6446 return -1;
6447 if (!equal)
6448 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6450 /* Check that solutions are properly merged. */
6451 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
6452 "c <= -1 + n - 4a - 2b and c >= -2b and "
6453 "4a >= -4 + n and c >= 0 }";
6454 set = isl_set_read_from_str(ctx, str);
6455 pwaff = isl_set_dim_min(set, 2);
6456 set1 = isl_set_from_pw_aff(pwaff);
6457 str = "[n] -> { [(0)] : n >= 1 }";
6458 set2 = isl_set_read_from_str(ctx, str);
6459 equal = isl_set_is_equal(set1, set2);
6460 isl_set_free(set1);
6461 isl_set_free(set2);
6463 if (equal < 0)
6464 return -1;
6465 if (!equal)
6466 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6468 /* Check that empty solution lie in the right space. */
6469 str = "[n] -> { [t,a] : 1 = 0 }";
6470 set = isl_set_read_from_str(ctx, str);
6471 pwaff = isl_set_dim_max(set, 0);
6472 set1 = isl_set_from_pw_aff(pwaff);
6473 str = "[n] -> { [t] : 1 = 0 }";
6474 set2 = isl_set_read_from_str(ctx, str);
6475 equal = isl_set_is_equal(set1, set2);
6476 isl_set_free(set1);
6477 isl_set_free(set2);
6479 if (equal < 0)
6480 return -1;
6481 if (!equal)
6482 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6484 return 0;
6487 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
6489 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
6490 const char *str)
6492 isl_ctx *ctx;
6493 isl_pw_multi_aff *pma2;
6494 int equal;
6496 if (!pma)
6497 return -1;
6499 ctx = isl_pw_multi_aff_get_ctx(pma);
6500 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6501 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
6502 isl_pw_multi_aff_free(pma2);
6504 return equal;
6507 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
6508 * represented by "str".
6510 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
6511 const char *str)
6513 int equal;
6515 equal = pw_multi_aff_plain_is_equal(pma, str);
6516 if (equal < 0)
6517 return -1;
6518 if (!equal)
6519 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
6520 "result not as expected", return -1);
6521 return 0;
6524 /* Basic test for isl_pw_multi_aff_product.
6526 * Check that multiple pieces are properly handled.
6528 static int test_product_pma(isl_ctx *ctx)
6530 int equal;
6531 const char *str;
6532 isl_pw_multi_aff *pma1, *pma2;
6534 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
6535 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
6536 str = "{ C[] -> D[] }";
6537 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6538 pma1 = isl_pw_multi_aff_product(pma1, pma2);
6539 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
6540 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
6541 equal = pw_multi_aff_check_plain_equal(pma1, str);
6542 isl_pw_multi_aff_free(pma1);
6543 if (equal < 0)
6544 return -1;
6546 return 0;
6549 int test_product(isl_ctx *ctx)
6551 const char *str;
6552 isl_set *set;
6553 isl_union_set *uset1, *uset2;
6554 int ok;
6556 str = "{ A[i] }";
6557 set = isl_set_read_from_str(ctx, str);
6558 set = isl_set_product(set, isl_set_copy(set));
6559 ok = isl_set_is_wrapping(set);
6560 isl_set_free(set);
6561 if (ok < 0)
6562 return -1;
6563 if (!ok)
6564 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6566 str = "{ [] }";
6567 uset1 = isl_union_set_read_from_str(ctx, str);
6568 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
6569 str = "{ [[] -> []] }";
6570 uset2 = isl_union_set_read_from_str(ctx, str);
6571 ok = isl_union_set_is_equal(uset1, uset2);
6572 isl_union_set_free(uset1);
6573 isl_union_set_free(uset2);
6574 if (ok < 0)
6575 return -1;
6576 if (!ok)
6577 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6579 if (test_product_pma(ctx) < 0)
6580 return -1;
6582 return 0;
6585 /* Check that two sets are not considered disjoint just because
6586 * they have a different set of (named) parameters.
6588 static int test_disjoint(isl_ctx *ctx)
6590 const char *str;
6591 isl_set *set, *set2;
6592 int disjoint;
6594 str = "[n] -> { [[]->[]] }";
6595 set = isl_set_read_from_str(ctx, str);
6596 str = "{ [[]->[]] }";
6597 set2 = isl_set_read_from_str(ctx, str);
6598 disjoint = isl_set_is_disjoint(set, set2);
6599 isl_set_free(set);
6600 isl_set_free(set2);
6601 if (disjoint < 0)
6602 return -1;
6603 if (disjoint)
6604 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6606 return 0;
6609 /* Inputs for isl_pw_multi_aff_is_equal tests.
6610 * "f1" and "f2" are the two function that need to be compared.
6611 * "equal" is the expected result.
6613 struct {
6614 int equal;
6615 const char *f1;
6616 const char *f2;
6617 } pma_equal_tests[] = {
6618 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
6619 "[N] -> { [0] : 0 <= N <= 1 }" },
6620 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
6621 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
6622 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
6623 "[N] -> { [0] : 0 <= N <= 1 }" },
6624 { 0, "{ [NaN] }", "{ [NaN] }" },
6627 int test_equal(isl_ctx *ctx)
6629 int i;
6630 const char *str;
6631 isl_set *set, *set2;
6632 int equal;
6634 str = "{ S_6[i] }";
6635 set = isl_set_read_from_str(ctx, str);
6636 str = "{ S_7[i] }";
6637 set2 = isl_set_read_from_str(ctx, str);
6638 equal = isl_set_is_equal(set, set2);
6639 isl_set_free(set);
6640 isl_set_free(set2);
6641 if (equal < 0)
6642 return -1;
6643 if (equal)
6644 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6646 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
6647 int expected = pma_equal_tests[i].equal;
6648 isl_pw_multi_aff *f1, *f2;
6650 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
6651 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
6652 equal = isl_pw_multi_aff_is_equal(f1, f2);
6653 isl_pw_multi_aff_free(f1);
6654 isl_pw_multi_aff_free(f2);
6655 if (equal < 0)
6656 return -1;
6657 if (equal != expected)
6658 isl_die(ctx, isl_error_unknown,
6659 "unexpected equality result", return -1);
6662 return 0;
6665 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
6666 enum isl_dim_type type, unsigned pos, int fixed)
6668 isl_bool test;
6670 test = isl_map_plain_is_fixed(map, type, pos, NULL);
6671 isl_map_free(map);
6672 if (test < 0)
6673 return -1;
6674 if (test == fixed)
6675 return 0;
6676 if (fixed)
6677 isl_die(ctx, isl_error_unknown,
6678 "map not detected as fixed", return -1);
6679 else
6680 isl_die(ctx, isl_error_unknown,
6681 "map detected as fixed", return -1);
6684 int test_fixed(isl_ctx *ctx)
6686 const char *str;
6687 isl_map *map;
6689 str = "{ [i] -> [i] }";
6690 map = isl_map_read_from_str(ctx, str);
6691 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
6692 return -1;
6693 str = "{ [i] -> [1] }";
6694 map = isl_map_read_from_str(ctx, str);
6695 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6696 return -1;
6697 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
6698 map = isl_map_read_from_str(ctx, str);
6699 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6700 return -1;
6701 map = isl_map_read_from_str(ctx, str);
6702 map = isl_map_neg(map);
6703 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6704 return -1;
6706 return 0;
6709 struct isl_vertices_test_data {
6710 const char *set;
6711 int n;
6712 const char *vertex[6];
6713 } vertices_tests[] = {
6714 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
6715 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
6716 { "{ A[t, i] : t = 14 and i = 1 }",
6717 1, { "{ A[14, 1] }" } },
6718 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
6719 "c <= m and m <= n and m > 0 }",
6720 6, {
6721 "[n, m] -> { [n, m, m] : 0 < m <= n }",
6722 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
6723 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
6724 "[n, m] -> { [m, m, m] : 0 < m <= n }",
6725 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
6726 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
6727 } },
6730 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
6732 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
6734 struct isl_vertices_test_data *data = user;
6735 isl_ctx *ctx;
6736 isl_multi_aff *ma;
6737 isl_basic_set *bset;
6738 isl_pw_multi_aff *pma;
6739 int i;
6740 isl_bool equal;
6742 ctx = isl_vertex_get_ctx(vertex);
6743 bset = isl_vertex_get_domain(vertex);
6744 ma = isl_vertex_get_expr(vertex);
6745 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
6747 for (i = 0; i < data->n; ++i) {
6748 isl_pw_multi_aff *pma_i;
6750 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
6751 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
6752 isl_pw_multi_aff_free(pma_i);
6754 if (equal < 0 || equal)
6755 break;
6758 isl_pw_multi_aff_free(pma);
6759 isl_vertex_free(vertex);
6761 if (equal < 0)
6762 return isl_stat_error;
6764 return equal ? isl_stat_ok : isl_stat_error;
6767 int test_vertices(isl_ctx *ctx)
6769 int i;
6771 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
6772 isl_basic_set *bset;
6773 isl_vertices *vertices;
6774 int ok = 1;
6775 isl_size n;
6777 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
6778 vertices = isl_basic_set_compute_vertices(bset);
6779 n = isl_vertices_get_n_vertices(vertices);
6780 if (vertices_tests[i].n != n)
6781 ok = 0;
6782 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
6783 &vertices_tests[i]) < 0)
6784 ok = 0;
6785 isl_vertices_free(vertices);
6786 isl_basic_set_free(bset);
6788 if (n < 0)
6789 return -1;
6790 if (!ok)
6791 isl_die(ctx, isl_error_unknown, "unexpected vertices",
6792 return -1);
6795 return 0;
6798 int test_union_pw(isl_ctx *ctx)
6800 int equal;
6801 const char *str;
6802 isl_union_set *uset;
6803 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
6805 str = "{ [x] -> x^2 }";
6806 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
6807 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
6808 uset = isl_union_pw_qpolynomial_domain(upwqp1);
6809 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
6810 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
6811 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
6812 isl_union_pw_qpolynomial_free(upwqp1);
6813 isl_union_pw_qpolynomial_free(upwqp2);
6814 if (equal < 0)
6815 return -1;
6816 if (!equal)
6817 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6819 return 0;
6822 /* Inputs for basic tests of functions that select
6823 * subparts of the domain of an isl_multi_union_pw_aff.
6824 * "fn" is the function that is tested.
6825 * "arg" is a string description of the input.
6826 * "res" is a string description of the expected result.
6828 struct {
6829 __isl_give isl_union_set *(*fn)(
6830 __isl_take isl_multi_union_pw_aff *mupa);
6831 const char *arg;
6832 const char *res;
6833 } un_locus_tests[] = {
6834 { &isl_multi_union_pw_aff_zero_union_set,
6835 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6836 "{ A[0,j]; B[0,j] }" },
6837 { &isl_multi_union_pw_aff_zero_union_set,
6838 "F[{ A[i,j] -> [i-j]; B[i,j] -> [i-j] : i >= 0 }]",
6839 "{ A[i,i]; B[i,i] : i >= 0 }" },
6840 { &isl_multi_union_pw_aff_zero_union_set,
6841 "(F[] : { A[i,j]; B[i,i] : i >= 0 })",
6842 "{ A[i,j]; B[i,i] : i >= 0 }" },
6845 /* Perform some basic tests of functions that select
6846 * subparts of the domain of an isl_multi_union_pw_aff.
6848 static int test_un_locus(isl_ctx *ctx)
6850 int i;
6851 isl_bool ok;
6852 isl_union_set *uset, *res;
6853 isl_multi_union_pw_aff *mupa;
6855 for (i = 0; i < ARRAY_SIZE(un_locus_tests); ++i) {
6856 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6857 un_locus_tests[i].arg);
6858 res = isl_union_set_read_from_str(ctx, un_locus_tests[i].res);
6859 uset = un_locus_tests[i].fn(mupa);
6860 ok = isl_union_set_is_equal(uset, res);
6861 isl_union_set_free(uset);
6862 isl_union_set_free(res);
6863 if (ok < 0)
6864 return -1;
6865 if (!ok)
6866 isl_die(ctx, isl_error_unknown,
6867 "unexpected result", return -1);
6870 return 0;
6873 /* Inputs for basic tests of functions that select
6874 * subparts of an isl_union_map based on a relation
6875 * specified by an isl_multi_union_pw_aff.
6876 * "fn" is the function that is tested.
6877 * "arg1" and "arg2" are string descriptions of the inputs.
6878 * "res" is a string description of the expected result.
6880 struct {
6881 __isl_give isl_union_map *(*fn)(
6882 __isl_take isl_union_map *umap,
6883 __isl_take isl_multi_union_pw_aff *mupa);
6884 const char *arg1;
6885 const char *arg2;
6886 const char *res;
6887 } bin_locus_tests[] = {
6888 { &isl_union_map_eq_at_multi_union_pw_aff,
6889 "{ A[i,j] -> B[i',j'] }",
6890 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6891 "{ A[i,j] -> B[i,j'] }" },
6892 { &isl_union_map_eq_at_multi_union_pw_aff,
6893 "{ A[i,j] -> B[i',j'] }",
6894 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6895 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6896 "{ A[i,j] -> B[i,j] }" },
6897 { &isl_union_map_eq_at_multi_union_pw_aff,
6898 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6899 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6900 "{ A[i,j] -> B[i,j'] }" },
6901 { &isl_union_map_eq_at_multi_union_pw_aff,
6902 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6903 "F[{ A[i,j] -> [i]; B[i,j] -> [i]; C[i,j] -> [0] }]",
6904 "{ A[i,j] -> B[i,j']; A[0,j] -> C[i',j'] }" },
6905 { &isl_union_map_eq_at_multi_union_pw_aff,
6906 "{ A[i,j] -> B[i',j'] }",
6907 "F[{ A[i,j] -> [i] : i > j; B[i,j] -> [i] }]",
6908 "{ A[i,j] -> B[i,j'] : i > j }" },
6909 { &isl_union_map_lex_lt_at_multi_union_pw_aff,
6910 "{ A[i,j] -> B[i',j'] }",
6911 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6912 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6913 "{ A[i,j] -> B[i',j'] : i,j << i',j' }" },
6914 { &isl_union_map_lex_gt_at_multi_union_pw_aff,
6915 "{ A[i,j] -> B[i',j'] }",
6916 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6917 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6918 "{ A[i,j] -> B[i',j'] : i,j >> i',j' }" },
6919 { &isl_union_map_eq_at_multi_union_pw_aff,
6920 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6921 "(F[] : { A[i,j]; B[i,j] })",
6922 "{ A[i,j] -> B[i',j'] }" },
6923 { &isl_union_map_eq_at_multi_union_pw_aff,
6924 "{ A[i,j] -> B[i',j'] }",
6925 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
6926 "{ A[i,j] -> B[i',j'] : i > j and i' < j' }" },
6927 { &isl_union_map_eq_at_multi_union_pw_aff,
6928 "[N] -> { A[i,j] -> B[i',j'] : i,i' <= N }",
6929 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
6930 "[N] -> { A[i,j] -> B[i',j'] : i > j and i' < j' and i,i' <= N }" },
6931 { &isl_union_map_eq_at_multi_union_pw_aff,
6932 "{ A[i,j] -> B[i',j'] }",
6933 "[N] -> (F[] : { A[i,j] : i < N; B[i,j] : i < N })",
6934 "[N] -> { A[i,j] -> B[i',j'] : i,i' < N }" },
6935 { &isl_union_map_eq_at_multi_union_pw_aff,
6936 "{ A[i,j] -> B[i',j'] }",
6937 "[N] -> (F[] : { : N >= 0 })",
6938 "[N] -> { A[i,j] -> B[i',j'] : N >= 0 }" },
6941 /* Perform some basic tests of functions that select
6942 * subparts of an isl_union_map based on a relation
6943 * specified by an isl_multi_union_pw_aff.
6945 static int test_bin_locus(isl_ctx *ctx)
6947 int i;
6948 isl_bool ok;
6949 isl_union_map *umap, *res;
6950 isl_multi_union_pw_aff *mupa;
6952 for (i = 0; i < ARRAY_SIZE(bin_locus_tests); ++i) {
6953 umap = isl_union_map_read_from_str(ctx,
6954 bin_locus_tests[i].arg1);
6955 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6956 bin_locus_tests[i].arg2);
6957 res = isl_union_map_read_from_str(ctx, bin_locus_tests[i].res);
6958 umap = bin_locus_tests[i].fn(umap, mupa);
6959 ok = isl_union_map_is_equal(umap, res);
6960 isl_union_map_free(umap);
6961 isl_union_map_free(res);
6962 if (ok < 0)
6963 return -1;
6964 if (!ok)
6965 isl_die(ctx, isl_error_unknown,
6966 "unexpected result", return -1);
6969 return 0;
6972 /* Perform basic locus tests.
6974 static int test_locus(isl_ctx *ctx)
6976 if (test_un_locus(ctx) < 0)
6977 return -1;
6978 if (test_bin_locus(ctx) < 0)
6979 return -1;
6980 return 0;
6983 /* Test that isl_union_pw_qpolynomial_eval picks up the function
6984 * defined over the correct domain space.
6986 static int test_eval_1(isl_ctx *ctx)
6988 const char *str;
6989 isl_point *pnt;
6990 isl_set *set;
6991 isl_union_pw_qpolynomial *upwqp;
6992 isl_val *v;
6993 int cmp;
6995 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
6996 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
6997 str = "{ A[6] }";
6998 set = isl_set_read_from_str(ctx, str);
6999 pnt = isl_set_sample_point(set);
7000 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
7001 cmp = isl_val_cmp_si(v, 36);
7002 isl_val_free(v);
7004 if (!v)
7005 return -1;
7006 if (cmp != 0)
7007 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
7009 return 0;
7012 /* Check that isl_qpolynomial_eval handles getting called on a void point.
7014 static int test_eval_2(isl_ctx *ctx)
7016 const char *str;
7017 isl_point *pnt;
7018 isl_set *set;
7019 isl_qpolynomial *qp;
7020 isl_val *v;
7021 isl_bool ok;
7023 str = "{ A[x] -> [x] }";
7024 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
7025 str = "{ A[x] : false }";
7026 set = isl_set_read_from_str(ctx, str);
7027 pnt = isl_set_sample_point(set);
7028 v = isl_qpolynomial_eval(qp, pnt);
7029 ok = isl_val_is_nan(v);
7030 isl_val_free(v);
7032 if (ok < 0)
7033 return -1;
7034 if (!ok)
7035 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
7037 return 0;
7040 /* Check that a polynomial (without local variables) can be evaluated
7041 * in a rational point.
7043 static isl_stat test_eval_3(isl_ctx *ctx)
7045 isl_pw_qpolynomial *pwqp;
7046 isl_point *pnt;
7047 isl_val *v;
7048 isl_stat r;
7050 pwqp = isl_pw_qpolynomial_read_from_str(ctx, "{ [x] -> x^2 }");
7051 pnt = isl_point_zero(isl_pw_qpolynomial_get_domain_space(pwqp));
7052 v = isl_val_read_from_str(ctx, "1/2");
7053 pnt = isl_point_set_coordinate_val(pnt, isl_dim_set, 0, v);
7054 v = isl_pw_qpolynomial_eval(pwqp, pnt);
7055 r = val_check_equal(v, "1/4");
7056 isl_val_free(v);
7058 return r;
7061 /* Inputs for isl_pw_aff_eval test.
7062 * "f" is the affine function.
7063 * "p" is the point where the function should be evaluated.
7064 * "res" is the expected result.
7066 struct {
7067 const char *f;
7068 const char *p;
7069 const char *res;
7070 } aff_eval_tests[] = {
7071 { "{ [i] -> [2 * i] }", "{ [4] }", "8" },
7072 { "{ [i] -> [2 * i] }", "{ [x] : false }", "NaN" },
7073 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [0] }", "0" },
7074 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [1] }", "1" },
7075 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [2] }", "3" },
7076 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [3] }", "5" },
7077 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [4] }", "7" },
7078 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [0] }", "0" },
7079 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [1] }", "0" },
7080 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [2] }", "0" },
7081 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [3] }", "0" },
7082 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [4] }", "1" },
7083 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [6] }", "1" },
7084 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [8] }", "2" },
7085 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [4] }", "4" },
7086 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [-2] }", "2" },
7087 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [0] }", "NaN" },
7088 { "[N] -> { [2 * N] }", "[N] -> { : N = 4 }", "8" },
7089 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 1] }", "1" },
7090 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 2] }", "3/2" },
7091 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [4] }", "4" },
7092 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [3] }", "NaN" },
7093 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [x] : false }", "NaN" },
7096 /* Perform basic isl_pw_aff_eval tests.
7098 static int test_eval_aff(isl_ctx *ctx)
7100 int i;
7102 for (i = 0; i < ARRAY_SIZE(aff_eval_tests); ++i) {
7103 isl_stat r;
7104 isl_pw_aff *pa;
7105 isl_set *set;
7106 isl_point *pnt;
7107 isl_val *v;
7109 pa = isl_pw_aff_read_from_str(ctx, aff_eval_tests[i].f);
7110 set = isl_set_read_from_str(ctx, aff_eval_tests[i].p);
7111 pnt = isl_set_sample_point(set);
7112 v = isl_pw_aff_eval(pa, pnt);
7113 r = val_check_equal(v, aff_eval_tests[i].res);
7114 isl_val_free(v);
7115 if (r < 0)
7116 return -1;
7118 return 0;
7121 /* Perform basic evaluation tests.
7123 static int test_eval(isl_ctx *ctx)
7125 if (test_eval_1(ctx) < 0)
7126 return -1;
7127 if (test_eval_2(ctx) < 0)
7128 return -1;
7129 if (test_eval_3(ctx) < 0)
7130 return -1;
7131 if (test_eval_aff(ctx) < 0)
7132 return -1;
7133 return 0;
7136 /* Descriptions of sets that are tested for reparsing after printing.
7138 const char *output_tests[] = {
7139 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
7140 "{ [x] : 1 = 0 }",
7141 "{ [x] : false }",
7142 "{ [x] : x mod 2 = 0 }",
7143 "{ [x] : x mod 2 = 1 }",
7144 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) < x }",
7145 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) < x }",
7146 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7147 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7148 "[n] -> { [y, x] : 2*((x + 2y) mod 3) = n }",
7149 "{ [x, y] : (2*floor(x/3) + 3*floor(y/4)) mod 5 = x }",
7152 /* Check that printing a set and reparsing a set from the printed output
7153 * results in the same set.
7155 static int test_output_set(isl_ctx *ctx)
7157 int i;
7158 char *str;
7159 isl_set *set1, *set2;
7160 isl_bool equal;
7162 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
7163 set1 = isl_set_read_from_str(ctx, output_tests[i]);
7164 str = isl_set_to_str(set1);
7165 set2 = isl_set_read_from_str(ctx, str);
7166 free(str);
7167 equal = isl_set_is_equal(set1, set2);
7168 isl_set_free(set1);
7169 isl_set_free(set2);
7170 if (equal < 0)
7171 return -1;
7172 if (!equal)
7173 isl_die(ctx, isl_error_unknown,
7174 "parsed output not the same", return -1);
7177 return 0;
7180 /* Check that an isl_multi_aff is printed using a consistent space.
7182 static isl_stat test_output_ma(isl_ctx *ctx)
7184 char *str;
7185 isl_bool equal;
7186 isl_aff *aff;
7187 isl_multi_aff *ma, *ma2;
7189 ma = isl_multi_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7190 aff = isl_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7191 ma = isl_multi_aff_set_aff(ma, 0, aff);
7192 str = isl_multi_aff_to_str(ma);
7193 ma2 = isl_multi_aff_read_from_str(ctx, str);
7194 free(str);
7195 equal = isl_multi_aff_plain_is_equal(ma, ma2);
7196 isl_multi_aff_free(ma2);
7197 isl_multi_aff_free(ma);
7199 if (equal < 0)
7200 return isl_stat_error;
7201 if (!equal)
7202 isl_die(ctx, isl_error_unknown, "bad conversion",
7203 return isl_stat_error);
7205 return isl_stat_ok;
7208 /* Check that an isl_multi_pw_aff is printed using a consistent space.
7210 static isl_stat test_output_mpa(isl_ctx *ctx)
7212 char *str;
7213 isl_bool equal;
7214 isl_pw_aff *pa;
7215 isl_multi_pw_aff *mpa, *mpa2;
7217 mpa = isl_multi_pw_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7218 pa = isl_pw_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7219 mpa = isl_multi_pw_aff_set_pw_aff(mpa, 0, pa);
7220 str = isl_multi_pw_aff_to_str(mpa);
7221 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
7222 free(str);
7223 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
7224 isl_multi_pw_aff_free(mpa2);
7225 isl_multi_pw_aff_free(mpa);
7227 if (equal < 0)
7228 return isl_stat_error;
7229 if (!equal)
7230 isl_die(ctx, isl_error_unknown, "bad conversion",
7231 return isl_stat_error);
7233 return isl_stat_ok;
7236 int test_output(isl_ctx *ctx)
7238 char *s;
7239 const char *str;
7240 isl_pw_aff *pa;
7241 isl_printer *p;
7242 int equal;
7244 if (test_output_set(ctx) < 0)
7245 return -1;
7246 if (test_output_ma(ctx) < 0)
7247 return -1;
7248 if (test_output_mpa(ctx) < 0)
7249 return -1;
7251 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
7252 pa = isl_pw_aff_read_from_str(ctx, str);
7254 p = isl_printer_to_str(ctx);
7255 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
7256 p = isl_printer_print_pw_aff(p, pa);
7257 s = isl_printer_get_str(p);
7258 isl_printer_free(p);
7259 isl_pw_aff_free(pa);
7260 if (!s)
7261 equal = -1;
7262 else
7263 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
7264 free(s);
7265 if (equal < 0)
7266 return -1;
7267 if (!equal)
7268 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7270 return 0;
7273 int test_sample(isl_ctx *ctx)
7275 const char *str;
7276 isl_basic_set *bset1, *bset2;
7277 int empty, subset;
7279 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
7280 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
7281 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
7282 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
7283 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
7284 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
7285 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
7286 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
7287 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
7288 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
7289 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
7290 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
7291 "d - 1073741821e and "
7292 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
7293 "3j >= 1 - a + b + 2e and "
7294 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
7295 "3i <= 4 - a + 4b - e and "
7296 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
7297 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
7298 "c <= -1 + a and 3i >= -2 - a + 3e and "
7299 "1073741822e <= 1073741823 - a + 1073741822b + c and "
7300 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
7301 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
7302 "1073741823e >= 1 + 1073741823b - d and "
7303 "3i >= 1073741823b + c - 1073741823e - f and "
7304 "3i >= 1 + 2b + e + 3g }";
7305 bset1 = isl_basic_set_read_from_str(ctx, str);
7306 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
7307 empty = isl_basic_set_is_empty(bset2);
7308 subset = isl_basic_set_is_subset(bset2, bset1);
7309 isl_basic_set_free(bset1);
7310 isl_basic_set_free(bset2);
7311 if (empty < 0 || subset < 0)
7312 return -1;
7313 if (empty)
7314 isl_die(ctx, isl_error_unknown, "point not found", return -1);
7315 if (!subset)
7316 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
7318 return 0;
7321 int test_fixed_power(isl_ctx *ctx)
7323 const char *str;
7324 isl_map *map;
7325 isl_val *exp;
7326 int equal;
7328 str = "{ [i] -> [i + 1] }";
7329 map = isl_map_read_from_str(ctx, str);
7330 exp = isl_val_int_from_si(ctx, 23);
7331 map = isl_map_fixed_power_val(map, exp);
7332 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
7333 isl_map_free(map);
7334 if (equal < 0)
7335 return -1;
7337 return 0;
7340 int test_slice(isl_ctx *ctx)
7342 const char *str;
7343 isl_map *map;
7344 int equal;
7346 str = "{ [i] -> [j] }";
7347 map = isl_map_read_from_str(ctx, str);
7348 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
7349 equal = map_check_equal(map, "{ [i] -> [i] }");
7350 isl_map_free(map);
7351 if (equal < 0)
7352 return -1;
7354 str = "{ [i] -> [j] }";
7355 map = isl_map_read_from_str(ctx, str);
7356 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
7357 equal = map_check_equal(map, "{ [i] -> [j] }");
7358 isl_map_free(map);
7359 if (equal < 0)
7360 return -1;
7362 str = "{ [i] -> [j] }";
7363 map = isl_map_read_from_str(ctx, str);
7364 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
7365 equal = map_check_equal(map, "{ [i] -> [-i] }");
7366 isl_map_free(map);
7367 if (equal < 0)
7368 return -1;
7370 str = "{ [i] -> [j] }";
7371 map = isl_map_read_from_str(ctx, str);
7372 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
7373 equal = map_check_equal(map, "{ [0] -> [j] }");
7374 isl_map_free(map);
7375 if (equal < 0)
7376 return -1;
7378 str = "{ [i] -> [j] }";
7379 map = isl_map_read_from_str(ctx, str);
7380 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
7381 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
7382 isl_map_free(map);
7383 if (equal < 0)
7384 return -1;
7386 str = "{ [i] -> [j] }";
7387 map = isl_map_read_from_str(ctx, str);
7388 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
7389 equal = map_check_equal(map, "{ [i] -> [j] : false }");
7390 isl_map_free(map);
7391 if (equal < 0)
7392 return -1;
7394 return 0;
7397 int test_eliminate(isl_ctx *ctx)
7399 const char *str;
7400 isl_map *map;
7401 int equal;
7403 str = "{ [i] -> [j] : i = 2j }";
7404 map = isl_map_read_from_str(ctx, str);
7405 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
7406 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
7407 isl_map_free(map);
7408 if (equal < 0)
7409 return -1;
7411 return 0;
7414 /* Check that isl_set_dim_residue_class detects that the values of j
7415 * in the set below are all odd and that it does not detect any spurious
7416 * strides.
7418 static int test_residue_class(isl_ctx *ctx)
7420 const char *str;
7421 isl_set *set;
7422 isl_int m, r;
7423 isl_stat res;
7425 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
7426 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
7427 set = isl_set_read_from_str(ctx, str);
7428 isl_int_init(m);
7429 isl_int_init(r);
7430 res = isl_set_dim_residue_class(set, 1, &m, &r);
7431 if (res >= 0 &&
7432 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
7433 isl_die(ctx, isl_error_unknown, "incorrect residue class",
7434 res = isl_stat_error);
7435 isl_int_clear(r);
7436 isl_int_clear(m);
7437 isl_set_free(set);
7439 return res;
7442 static int test_align_parameters_1(isl_ctx *ctx)
7444 const char *str;
7445 isl_space *space;
7446 isl_multi_aff *ma1, *ma2;
7447 int equal;
7449 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
7450 ma1 = isl_multi_aff_read_from_str(ctx, str);
7452 space = isl_space_params_alloc(ctx, 1);
7453 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
7454 ma1 = isl_multi_aff_align_params(ma1, space);
7456 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
7457 ma2 = isl_multi_aff_read_from_str(ctx, str);
7459 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
7461 isl_multi_aff_free(ma1);
7462 isl_multi_aff_free(ma2);
7464 if (equal < 0)
7465 return -1;
7466 if (!equal)
7467 isl_die(ctx, isl_error_unknown,
7468 "result not as expected", return -1);
7470 return 0;
7473 /* Check the isl_multi_*_from_*_list operation in case inputs
7474 * have unaligned parameters.
7475 * In particular, older versions of isl would simply fail
7476 * (without printing any error message).
7478 static isl_stat test_align_parameters_2(isl_ctx *ctx)
7480 isl_space *space;
7481 isl_map *map;
7482 isl_aff *aff;
7483 isl_multi_aff *ma;
7485 map = isl_map_read_from_str(ctx, "{ A[] -> M[x] }");
7486 space = isl_map_get_space(map);
7487 isl_map_free(map);
7489 aff = isl_aff_read_from_str(ctx, "[N] -> { A[] -> [N] }");
7490 ma = isl_multi_aff_from_aff_list(space, isl_aff_list_from_aff(aff));
7491 isl_multi_aff_free(ma);
7493 if (!ma)
7494 return isl_stat_error;
7495 return isl_stat_ok;
7498 /* Perform basic parameter alignment tests.
7500 static int test_align_parameters(isl_ctx *ctx)
7502 if (test_align_parameters_1(ctx) < 0)
7503 return -1;
7504 if (test_align_parameters_2(ctx) < 0)
7505 return -1;
7507 return 0;
7510 /* Check that isl_*_drop_unused_params actually drops the unused parameters
7511 * by comparing the result using isl_*_plain_is_equal.
7512 * Note that this assumes that isl_*_plain_is_equal does not consider
7513 * objects that only differ by unused parameters to be equal.
7515 int test_drop_unused_parameters(isl_ctx *ctx)
7517 const char *str_with, *str_without;
7518 isl_basic_set *bset1, *bset2;
7519 isl_set *set1, *set2;
7520 isl_pw_aff *pwa1, *pwa2;
7521 int equal;
7523 str_with = "[n, m, o] -> { [m] }";
7524 str_without = "[m] -> { [m] }";
7526 bset1 = isl_basic_set_read_from_str(ctx, str_with);
7527 bset2 = isl_basic_set_read_from_str(ctx, str_without);
7528 bset1 = isl_basic_set_drop_unused_params(bset1);
7529 equal = isl_basic_set_plain_is_equal(bset1, bset2);
7530 isl_basic_set_free(bset1);
7531 isl_basic_set_free(bset2);
7533 if (equal < 0)
7534 return -1;
7535 if (!equal)
7536 isl_die(ctx, isl_error_unknown,
7537 "result not as expected", return -1);
7539 set1 = isl_set_read_from_str(ctx, str_with);
7540 set2 = isl_set_read_from_str(ctx, str_without);
7541 set1 = isl_set_drop_unused_params(set1);
7542 equal = isl_set_plain_is_equal(set1, set2);
7543 isl_set_free(set1);
7544 isl_set_free(set2);
7546 if (equal < 0)
7547 return -1;
7548 if (!equal)
7549 isl_die(ctx, isl_error_unknown,
7550 "result not as expected", return -1);
7552 pwa1 = isl_pw_aff_read_from_str(ctx, str_with);
7553 pwa2 = isl_pw_aff_read_from_str(ctx, str_without);
7554 pwa1 = isl_pw_aff_drop_unused_params(pwa1);
7555 equal = isl_pw_aff_plain_is_equal(pwa1, pwa2);
7556 isl_pw_aff_free(pwa1);
7557 isl_pw_aff_free(pwa2);
7559 if (equal < 0)
7560 return -1;
7561 if (!equal)
7562 isl_die(ctx, isl_error_unknown,
7563 "result not as expected", return -1);
7565 return 0;
7568 static int test_list(isl_ctx *ctx)
7570 isl_id *a, *b, *c, *d, *id;
7571 isl_id_list *list;
7572 isl_size n;
7573 int ok;
7575 a = isl_id_alloc(ctx, "a", NULL);
7576 b = isl_id_alloc(ctx, "b", NULL);
7577 c = isl_id_alloc(ctx, "c", NULL);
7578 d = isl_id_alloc(ctx, "d", NULL);
7580 list = isl_id_list_alloc(ctx, 4);
7581 list = isl_id_list_add(list, b);
7582 list = isl_id_list_insert(list, 0, a);
7583 list = isl_id_list_add(list, c);
7584 list = isl_id_list_add(list, d);
7585 list = isl_id_list_drop(list, 1, 1);
7587 n = isl_id_list_n_id(list);
7588 if (n < 0)
7589 return -1;
7590 if (n != 3) {
7591 isl_id_list_free(list);
7592 isl_die(ctx, isl_error_unknown,
7593 "unexpected number of elements in list", return -1);
7596 id = isl_id_list_get_id(list, 0);
7597 ok = id == a;
7598 isl_id_free(id);
7599 id = isl_id_list_get_id(list, 1);
7600 ok = ok && id == c;
7601 isl_id_free(id);
7602 id = isl_id_list_get_id(list, 2);
7603 ok = ok && id == d;
7604 isl_id_free(id);
7606 isl_id_list_free(list);
7608 if (!ok)
7609 isl_die(ctx, isl_error_unknown,
7610 "unexpected elements in list", return -1);
7612 return 0;
7615 /* Check the conversion from an isl_multi_aff to an isl_basic_set.
7617 static isl_stat test_ma_conversion(isl_ctx *ctx)
7619 const char *str;
7620 isl_bool equal;
7621 isl_multi_aff *ma;
7622 isl_basic_set *bset1, *bset2;
7624 str = "[N] -> { A[0, N + 1] }";
7625 ma = isl_multi_aff_read_from_str(ctx, str);
7626 bset1 = isl_basic_set_read_from_str(ctx, str);
7627 bset2 = isl_basic_set_from_multi_aff(ma);
7628 equal = isl_basic_set_is_equal(bset1, bset2);
7629 isl_basic_set_free(bset1);
7630 isl_basic_set_free(bset2);
7631 if (equal < 0)
7632 return isl_stat_error;
7633 if (!equal)
7634 isl_die(ctx, isl_error_unknown, "bad conversion",
7635 return isl_stat_error);
7636 return isl_stat_ok;
7639 const char *set_conversion_tests[] = {
7640 "[N] -> { [i] : N - 1 <= 2 i <= N }",
7641 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
7642 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
7643 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
7644 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
7645 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
7646 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
7647 "-3 + c <= d <= 28 + c) }",
7650 /* Check that converting from isl_set to isl_pw_multi_aff and back
7651 * to isl_set produces the original isl_set.
7653 static int test_set_conversion(isl_ctx *ctx)
7655 int i;
7656 const char *str;
7657 isl_set *set1, *set2;
7658 isl_pw_multi_aff *pma;
7659 int equal;
7661 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
7662 str = set_conversion_tests[i];
7663 set1 = isl_set_read_from_str(ctx, str);
7664 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
7665 set2 = isl_set_from_pw_multi_aff(pma);
7666 equal = isl_set_is_equal(set1, set2);
7667 isl_set_free(set1);
7668 isl_set_free(set2);
7670 if (equal < 0)
7671 return -1;
7672 if (!equal)
7673 isl_die(ctx, isl_error_unknown, "bad conversion",
7674 return -1);
7677 return 0;
7680 const char *conversion_tests[] = {
7681 "{ [a, b, c, d] -> s0[a, b, e, f] : "
7682 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
7683 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
7684 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
7685 "9e <= -2 - 2a) }",
7686 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
7687 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
7688 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
7691 /* Check that converting from isl_map to isl_pw_multi_aff and back
7692 * to isl_map produces the original isl_map.
7694 static int test_map_conversion(isl_ctx *ctx)
7696 int i;
7697 isl_map *map1, *map2;
7698 isl_pw_multi_aff *pma;
7699 int equal;
7701 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
7702 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
7703 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
7704 map2 = isl_map_from_pw_multi_aff(pma);
7705 equal = isl_map_is_equal(map1, map2);
7706 isl_map_free(map1);
7707 isl_map_free(map2);
7709 if (equal < 0)
7710 return -1;
7711 if (!equal)
7712 isl_die(ctx, isl_error_unknown, "bad conversion",
7713 return -1);
7716 return 0;
7719 /* Descriptions of isl_pw_multi_aff objects for testing conversion
7720 * to isl_multi_pw_aff and back.
7722 const char *mpa_conversion_tests[] = {
7723 "{ [x] -> A[x] }",
7724 "{ [x] -> A[x] : x >= 0 }",
7725 "{ [x] -> A[x] : x >= 0; [x] -> A[-x] : x < 0 }",
7726 "{ [x] -> A[x, x + 1] }",
7727 "{ [x] -> A[] }",
7728 "{ [x] -> A[] : x >= 0 }",
7731 /* Check that conversion from isl_pw_multi_aff to isl_multi_pw_aff and
7732 * back to isl_pw_multi_aff preserves the original meaning.
7734 static int test_mpa_conversion(isl_ctx *ctx)
7736 int i;
7737 isl_pw_multi_aff *pma1, *pma2;
7738 isl_multi_pw_aff *mpa;
7739 int equal;
7741 for (i = 0; i < ARRAY_SIZE(mpa_conversion_tests); ++i) {
7742 const char *str;
7743 str = mpa_conversion_tests[i];
7744 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
7745 pma2 = isl_pw_multi_aff_copy(pma1);
7746 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma1);
7747 pma1 = isl_pw_multi_aff_from_multi_pw_aff(mpa);
7748 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7749 isl_pw_multi_aff_free(pma1);
7750 isl_pw_multi_aff_free(pma2);
7752 if (equal < 0)
7753 return -1;
7754 if (!equal)
7755 isl_die(ctx, isl_error_unknown, "bad conversion",
7756 return -1);
7759 return 0;
7762 /* Descriptions of union maps that should be convertible
7763 * to an isl_multi_union_pw_aff.
7765 const char *umap_mupa_conversion_tests[] = {
7766 "{ [a, b, c, d] -> s0[a, b, e, f] : "
7767 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
7768 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
7769 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
7770 "9e <= -2 - 2a) }",
7771 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
7772 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
7773 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
7774 "{ A[] -> B[0]; C[] -> B[1] }",
7775 "{ A[] -> B[]; C[] -> B[] }",
7778 /* Check that converting from isl_union_map to isl_multi_union_pw_aff and back
7779 * to isl_union_map produces the original isl_union_map.
7781 static int test_union_map_mupa_conversion(isl_ctx *ctx)
7783 int i;
7784 isl_union_map *umap1, *umap2;
7785 isl_multi_union_pw_aff *mupa;
7786 int equal;
7788 for (i = 0; i < ARRAY_SIZE(umap_mupa_conversion_tests); ++i) {
7789 const char *str;
7790 str = umap_mupa_conversion_tests[i];
7791 umap1 = isl_union_map_read_from_str(ctx, str);
7792 umap2 = isl_union_map_copy(umap1);
7793 mupa = isl_multi_union_pw_aff_from_union_map(umap2);
7794 umap2 = isl_union_map_from_multi_union_pw_aff(mupa);
7795 equal = isl_union_map_is_equal(umap1, umap2);
7796 isl_union_map_free(umap1);
7797 isl_union_map_free(umap2);
7799 if (equal < 0)
7800 return -1;
7801 if (!equal)
7802 isl_die(ctx, isl_error_unknown, "bad conversion",
7803 return -1);
7806 return 0;
7809 static int test_conversion(isl_ctx *ctx)
7811 if (test_ma_conversion(ctx) < 0)
7812 return -1;
7813 if (test_set_conversion(ctx) < 0)
7814 return -1;
7815 if (test_map_conversion(ctx) < 0)
7816 return -1;
7817 if (test_mpa_conversion(ctx) < 0)
7818 return -1;
7819 if (test_union_map_mupa_conversion(ctx) < 0)
7820 return -1;
7821 return 0;
7824 /* Check that isl_basic_map_curry does not modify input.
7826 static int test_curry(isl_ctx *ctx)
7828 const char *str;
7829 isl_basic_map *bmap1, *bmap2;
7830 int equal;
7832 str = "{ [A[] -> B[]] -> C[] }";
7833 bmap1 = isl_basic_map_read_from_str(ctx, str);
7834 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
7835 equal = isl_basic_map_is_equal(bmap1, bmap2);
7836 isl_basic_map_free(bmap1);
7837 isl_basic_map_free(bmap2);
7839 if (equal < 0)
7840 return -1;
7841 if (equal)
7842 isl_die(ctx, isl_error_unknown,
7843 "curried map should not be equal to original",
7844 return -1);
7846 return 0;
7849 struct {
7850 const char *set;
7851 const char *ma;
7852 const char *res;
7853 } preimage_tests[] = {
7854 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
7855 "{ A[j,i] -> B[i,j] }",
7856 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
7857 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
7858 "{ A[a,b] -> B[a/2,b/6] }",
7859 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
7860 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
7861 "{ A[a,b] -> B[a/2,b/6] }",
7862 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
7863 "exists i,j : a = 2 i and b = 6 j }" },
7864 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
7865 "[n] -> { : 0 <= n <= 100 }" },
7866 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
7867 "{ A[a] -> B[2a] }",
7868 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
7869 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
7870 "{ A[a] -> B[([a/2])] }",
7871 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
7872 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
7873 "{ A[a] -> B[a,a,a/3] }",
7874 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
7875 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
7876 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
7879 static int test_preimage_basic_set(isl_ctx *ctx)
7881 int i;
7882 isl_basic_set *bset1, *bset2;
7883 isl_multi_aff *ma;
7884 int equal;
7886 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
7887 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
7888 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
7889 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
7890 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
7891 equal = isl_basic_set_is_equal(bset1, bset2);
7892 isl_basic_set_free(bset1);
7893 isl_basic_set_free(bset2);
7894 if (equal < 0)
7895 return -1;
7896 if (!equal)
7897 isl_die(ctx, isl_error_unknown, "bad preimage",
7898 return -1);
7901 return 0;
7904 struct {
7905 const char *map;
7906 const char *ma;
7907 const char *res;
7908 } preimage_domain_tests[] = {
7909 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
7910 "{ A[j,i] -> B[i,j] }",
7911 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
7912 { "{ B[i] -> C[i]; D[i] -> E[i] }",
7913 "{ A[i] -> B[i + 1] }",
7914 "{ A[i] -> C[i + 1] }" },
7915 { "{ B[i] -> C[i]; B[i] -> E[i] }",
7916 "{ A[i] -> B[i + 1] }",
7917 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
7918 { "{ B[i] -> C[([i/2])] }",
7919 "{ A[i] -> B[2i] }",
7920 "{ A[i] -> C[i] }" },
7921 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
7922 "{ A[i] -> B[([i/5]), ([i/7])] }",
7923 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
7924 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
7925 "[N] -> { A[] -> B[([N/5])] }",
7926 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
7927 { "{ B[i] -> C[i] : exists a : i = 5 a }",
7928 "{ A[i] -> B[2i] }",
7929 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
7930 { "{ B[i] -> C[i] : exists a : i = 2 a; "
7931 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
7932 "{ A[i] -> B[2i] }",
7933 "{ A[i] -> C[2i] }" },
7934 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
7935 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
7938 static int test_preimage_union_map(isl_ctx *ctx)
7940 int i;
7941 isl_union_map *umap1, *umap2;
7942 isl_multi_aff *ma;
7943 int equal;
7945 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
7946 umap1 = isl_union_map_read_from_str(ctx,
7947 preimage_domain_tests[i].map);
7948 ma = isl_multi_aff_read_from_str(ctx,
7949 preimage_domain_tests[i].ma);
7950 umap2 = isl_union_map_read_from_str(ctx,
7951 preimage_domain_tests[i].res);
7952 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
7953 equal = isl_union_map_is_equal(umap1, umap2);
7954 isl_union_map_free(umap1);
7955 isl_union_map_free(umap2);
7956 if (equal < 0)
7957 return -1;
7958 if (!equal)
7959 isl_die(ctx, isl_error_unknown, "bad preimage",
7960 return -1);
7963 return 0;
7966 static int test_preimage(isl_ctx *ctx)
7968 if (test_preimage_basic_set(ctx) < 0)
7969 return -1;
7970 if (test_preimage_union_map(ctx) < 0)
7971 return -1;
7973 return 0;
7976 struct {
7977 const char *ma1;
7978 const char *ma;
7979 const char *res;
7980 } pullback_tests[] = {
7981 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
7982 "{ A[a,b] -> C[b + 2a] }" },
7983 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
7984 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
7985 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
7986 "{ A[a] -> C[(a)/6] }" },
7987 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
7988 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
7989 "{ A[a] -> C[(2a)/3] }" },
7990 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
7991 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
7992 "{ A[i,j] -> C[i + j, i + j] }"},
7993 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
7994 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
7995 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
7996 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
7997 "{ [i, j] -> [floor((i)/3), j] }",
7998 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
8001 static int test_pullback(isl_ctx *ctx)
8003 int i;
8004 isl_multi_aff *ma1, *ma2;
8005 isl_multi_aff *ma;
8006 int equal;
8008 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
8009 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
8010 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
8011 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
8012 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
8013 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
8014 isl_multi_aff_free(ma1);
8015 isl_multi_aff_free(ma2);
8016 if (equal < 0)
8017 return -1;
8018 if (!equal)
8019 isl_die(ctx, isl_error_unknown, "bad pullback",
8020 return -1);
8023 return 0;
8026 /* Check that negation is printed correctly and that equal expressions
8027 * are correctly identified.
8029 static int test_ast(isl_ctx *ctx)
8031 isl_ast_expr *expr, *expr1, *expr2, *expr3;
8032 char *str;
8033 int ok, equal;
8035 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
8036 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
8037 expr = isl_ast_expr_add(expr1, expr2);
8038 expr2 = isl_ast_expr_copy(expr);
8039 expr = isl_ast_expr_neg(expr);
8040 expr2 = isl_ast_expr_neg(expr2);
8041 equal = isl_ast_expr_is_equal(expr, expr2);
8042 str = isl_ast_expr_to_C_str(expr);
8043 ok = str ? !strcmp(str, "-(A + B)") : -1;
8044 free(str);
8045 isl_ast_expr_free(expr);
8046 isl_ast_expr_free(expr2);
8048 if (ok < 0 || equal < 0)
8049 return -1;
8050 if (!equal)
8051 isl_die(ctx, isl_error_unknown,
8052 "equal expressions not considered equal", return -1);
8053 if (!ok)
8054 isl_die(ctx, isl_error_unknown,
8055 "isl_ast_expr printed incorrectly", return -1);
8057 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
8058 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
8059 expr = isl_ast_expr_add(expr1, expr2);
8060 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
8061 expr = isl_ast_expr_sub(expr3, expr);
8062 str = isl_ast_expr_to_C_str(expr);
8063 ok = str ? !strcmp(str, "C - (A + B)") : -1;
8064 free(str);
8065 isl_ast_expr_free(expr);
8067 if (ok < 0)
8068 return -1;
8069 if (!ok)
8070 isl_die(ctx, isl_error_unknown,
8071 "isl_ast_expr printed incorrectly", return -1);
8073 return 0;
8076 /* Check that isl_ast_build_expr_from_set returns a valid expression
8077 * for an empty set. Note that isl_ast_build_expr_from_set getting
8078 * called on an empty set probably indicates a bug in the caller.
8080 static int test_ast_build(isl_ctx *ctx)
8082 isl_set *set;
8083 isl_ast_build *build;
8084 isl_ast_expr *expr;
8086 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8087 build = isl_ast_build_from_context(set);
8089 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
8090 expr = isl_ast_build_expr_from_set(build, set);
8092 isl_ast_expr_free(expr);
8093 isl_ast_build_free(build);
8095 if (!expr)
8096 return -1;
8098 return 0;
8101 /* Internal data structure for before_for and after_for callbacks.
8103 * depth is the current depth
8104 * before is the number of times before_for has been called
8105 * after is the number of times after_for has been called
8107 struct isl_test_codegen_data {
8108 int depth;
8109 int before;
8110 int after;
8113 /* This function is called before each for loop in the AST generated
8114 * from test_ast_gen1.
8116 * Increment the number of calls and the depth.
8117 * Check that the space returned by isl_ast_build_get_schedule_space
8118 * matches the target space of the schedule returned by
8119 * isl_ast_build_get_schedule.
8120 * Return an isl_id that is checked by the corresponding call
8121 * to after_for.
8123 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
8124 void *user)
8126 struct isl_test_codegen_data *data = user;
8127 isl_ctx *ctx;
8128 isl_space *space;
8129 isl_union_map *schedule;
8130 isl_union_set *uset;
8131 isl_set *set;
8132 isl_bool empty;
8133 isl_size n;
8134 char name[] = "d0";
8136 ctx = isl_ast_build_get_ctx(build);
8138 if (data->before >= 3)
8139 isl_die(ctx, isl_error_unknown,
8140 "unexpected number of for nodes", return NULL);
8141 if (data->depth >= 2)
8142 isl_die(ctx, isl_error_unknown,
8143 "unexpected depth", return NULL);
8145 snprintf(name, sizeof(name), "d%d", data->depth);
8146 data->before++;
8147 data->depth++;
8149 schedule = isl_ast_build_get_schedule(build);
8150 uset = isl_union_map_range(schedule);
8151 n = isl_union_set_n_set(uset);
8152 if (n != 1) {
8153 isl_union_set_free(uset);
8154 if (n < 0)
8155 return NULL;
8156 isl_die(ctx, isl_error_unknown,
8157 "expecting single range space", return NULL);
8160 space = isl_ast_build_get_schedule_space(build);
8161 set = isl_union_set_extract_set(uset, space);
8162 isl_union_set_free(uset);
8163 empty = isl_set_is_empty(set);
8164 isl_set_free(set);
8166 if (empty < 0)
8167 return NULL;
8168 if (empty)
8169 isl_die(ctx, isl_error_unknown,
8170 "spaces don't match", return NULL);
8172 return isl_id_alloc(ctx, name, NULL);
8175 /* This function is called after each for loop in the AST generated
8176 * from test_ast_gen1.
8178 * Increment the number of calls and decrement the depth.
8179 * Check that the annotation attached to the node matches
8180 * the isl_id returned by the corresponding call to before_for.
8182 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
8183 __isl_keep isl_ast_build *build, void *user)
8185 struct isl_test_codegen_data *data = user;
8186 isl_id *id;
8187 const char *name;
8188 int valid;
8190 data->after++;
8191 data->depth--;
8193 if (data->after > data->before)
8194 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8195 "mismatch in number of for nodes",
8196 return isl_ast_node_free(node));
8198 id = isl_ast_node_get_annotation(node);
8199 if (!id)
8200 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8201 "missing annotation", return isl_ast_node_free(node));
8203 name = isl_id_get_name(id);
8204 valid = name && atoi(name + 1) == data->depth;
8205 isl_id_free(id);
8207 if (!valid)
8208 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8209 "wrong annotation", return isl_ast_node_free(node));
8211 return node;
8214 /* Check that the before_each_for and after_each_for callbacks
8215 * are called for each for loop in the generated code,
8216 * that they are called in the right order and that the isl_id
8217 * returned from the before_each_for callback is attached to
8218 * the isl_ast_node passed to the corresponding after_each_for call.
8220 static int test_ast_gen1(isl_ctx *ctx)
8222 const char *str;
8223 isl_set *set;
8224 isl_union_map *schedule;
8225 isl_ast_build *build;
8226 isl_ast_node *tree;
8227 struct isl_test_codegen_data data;
8229 str = "[N] -> { : N >= 10 }";
8230 set = isl_set_read_from_str(ctx, str);
8231 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
8232 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
8233 schedule = isl_union_map_read_from_str(ctx, str);
8235 data.before = 0;
8236 data.after = 0;
8237 data.depth = 0;
8238 build = isl_ast_build_from_context(set);
8239 build = isl_ast_build_set_before_each_for(build,
8240 &before_for, &data);
8241 build = isl_ast_build_set_after_each_for(build,
8242 &after_for, &data);
8243 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8244 isl_ast_build_free(build);
8245 if (!tree)
8246 return -1;
8248 isl_ast_node_free(tree);
8250 if (data.before != 3 || data.after != 3)
8251 isl_die(ctx, isl_error_unknown,
8252 "unexpected number of for nodes", return -1);
8254 return 0;
8257 /* Check that the AST generator handles domains that are integrally disjoint
8258 * but not rationally disjoint.
8260 static int test_ast_gen2(isl_ctx *ctx)
8262 const char *str;
8263 isl_set *set;
8264 isl_union_map *schedule;
8265 isl_union_map *options;
8266 isl_ast_build *build;
8267 isl_ast_node *tree;
8269 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
8270 schedule = isl_union_map_read_from_str(ctx, str);
8271 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8272 build = isl_ast_build_from_context(set);
8274 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
8275 options = isl_union_map_read_from_str(ctx, str);
8276 build = isl_ast_build_set_options(build, options);
8277 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8278 isl_ast_build_free(build);
8279 if (!tree)
8280 return -1;
8281 isl_ast_node_free(tree);
8283 return 0;
8286 /* Increment *user on each call.
8288 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
8289 __isl_keep isl_ast_build *build, void *user)
8291 int *n = user;
8293 (*n)++;
8295 return node;
8298 /* Test that unrolling tries to minimize the number of instances.
8299 * In particular, for the schedule given below, make sure it generates
8300 * 3 nodes (rather than 101).
8302 static int test_ast_gen3(isl_ctx *ctx)
8304 const char *str;
8305 isl_set *set;
8306 isl_union_map *schedule;
8307 isl_union_map *options;
8308 isl_ast_build *build;
8309 isl_ast_node *tree;
8310 int n_domain = 0;
8312 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
8313 schedule = isl_union_map_read_from_str(ctx, str);
8314 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8316 str = "{ [i] -> unroll[0] }";
8317 options = isl_union_map_read_from_str(ctx, str);
8319 build = isl_ast_build_from_context(set);
8320 build = isl_ast_build_set_options(build, options);
8321 build = isl_ast_build_set_at_each_domain(build,
8322 &count_domains, &n_domain);
8323 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8324 isl_ast_build_free(build);
8325 if (!tree)
8326 return -1;
8328 isl_ast_node_free(tree);
8330 if (n_domain != 3)
8331 isl_die(ctx, isl_error_unknown,
8332 "unexpected number of for nodes", return -1);
8334 return 0;
8337 /* Check that if the ast_build_exploit_nested_bounds options is set,
8338 * we do not get an outer if node in the generated AST,
8339 * while we do get such an outer if node if the options is not set.
8341 static int test_ast_gen4(isl_ctx *ctx)
8343 const char *str;
8344 isl_set *set;
8345 isl_union_map *schedule;
8346 isl_ast_build *build;
8347 isl_ast_node *tree;
8348 enum isl_ast_node_type type;
8349 int enb;
8351 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
8352 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
8354 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
8356 schedule = isl_union_map_read_from_str(ctx, str);
8357 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8358 build = isl_ast_build_from_context(set);
8359 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8360 isl_ast_build_free(build);
8361 if (!tree)
8362 return -1;
8364 type = isl_ast_node_get_type(tree);
8365 isl_ast_node_free(tree);
8367 if (type == isl_ast_node_if)
8368 isl_die(ctx, isl_error_unknown,
8369 "not expecting if node", return -1);
8371 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
8373 schedule = isl_union_map_read_from_str(ctx, str);
8374 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8375 build = isl_ast_build_from_context(set);
8376 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8377 isl_ast_build_free(build);
8378 if (!tree)
8379 return -1;
8381 type = isl_ast_node_get_type(tree);
8382 isl_ast_node_free(tree);
8384 if (type != isl_ast_node_if)
8385 isl_die(ctx, isl_error_unknown,
8386 "expecting if node", return -1);
8388 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
8390 return 0;
8393 /* This function is called for each leaf in the AST generated
8394 * from test_ast_gen5.
8396 * We finalize the AST generation by extending the outer schedule
8397 * with a zero-dimensional schedule. If this results in any for loops,
8398 * then this means that we did not pass along enough information
8399 * about the outer schedule to the inner AST generation.
8401 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
8402 void *user)
8404 isl_union_map *schedule, *extra;
8405 isl_ast_node *tree;
8407 schedule = isl_ast_build_get_schedule(build);
8408 extra = isl_union_map_copy(schedule);
8409 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
8410 schedule = isl_union_map_range_product(schedule, extra);
8411 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8412 isl_ast_build_free(build);
8414 if (!tree)
8415 return NULL;
8417 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
8418 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
8419 "code should not contain any for loop",
8420 return isl_ast_node_free(tree));
8422 return tree;
8425 /* Check that we do not lose any information when going back and
8426 * forth between internal and external schedule.
8428 * In particular, we create an AST where we unroll the only
8429 * non-constant dimension in the schedule. We therefore do
8430 * not expect any for loops in the AST. However, older versions
8431 * of isl would not pass along enough information about the outer
8432 * schedule when performing an inner code generation from a create_leaf
8433 * callback, resulting in the inner code generation producing a for loop.
8435 static int test_ast_gen5(isl_ctx *ctx)
8437 const char *str;
8438 isl_set *set;
8439 isl_union_map *schedule, *options;
8440 isl_ast_build *build;
8441 isl_ast_node *tree;
8443 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
8444 schedule = isl_union_map_read_from_str(ctx, str);
8446 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
8447 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
8448 options = isl_union_map_read_from_str(ctx, str);
8450 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8451 build = isl_ast_build_from_context(set);
8452 build = isl_ast_build_set_options(build, options);
8453 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
8454 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8455 isl_ast_build_free(build);
8456 isl_ast_node_free(tree);
8457 if (!tree)
8458 return -1;
8460 return 0;
8463 /* Check that the expression
8465 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
8467 * is not combined into
8469 * min(n/2, 0)
8471 * as this would result in n/2 being evaluated in parts of
8472 * the definition domain where n is not a multiple of 2.
8474 static int test_ast_expr(isl_ctx *ctx)
8476 const char *str;
8477 isl_pw_aff *pa;
8478 isl_ast_build *build;
8479 isl_ast_expr *expr;
8480 int min_max;
8481 int is_min;
8483 min_max = isl_options_get_ast_build_detect_min_max(ctx);
8484 isl_options_set_ast_build_detect_min_max(ctx, 1);
8486 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
8487 pa = isl_pw_aff_read_from_str(ctx, str);
8488 build = isl_ast_build_alloc(ctx);
8489 expr = isl_ast_build_expr_from_pw_aff(build, pa);
8490 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
8491 isl_ast_expr_get_op_type(expr) == isl_ast_op_min;
8492 isl_ast_build_free(build);
8493 isl_ast_expr_free(expr);
8495 isl_options_set_ast_build_detect_min_max(ctx, min_max);
8497 if (!expr)
8498 return -1;
8499 if (is_min)
8500 isl_die(ctx, isl_error_unknown,
8501 "expressions should not be combined", return -1);
8503 return 0;
8506 static int test_ast_gen(isl_ctx *ctx)
8508 if (test_ast_gen1(ctx) < 0)
8509 return -1;
8510 if (test_ast_gen2(ctx) < 0)
8511 return -1;
8512 if (test_ast_gen3(ctx) < 0)
8513 return -1;
8514 if (test_ast_gen4(ctx) < 0)
8515 return -1;
8516 if (test_ast_gen5(ctx) < 0)
8517 return -1;
8518 if (test_ast_expr(ctx) < 0)
8519 return -1;
8520 return 0;
8523 /* Check if dropping output dimensions from an isl_pw_multi_aff
8524 * works properly.
8526 static int test_pw_multi_aff(isl_ctx *ctx)
8528 const char *str;
8529 isl_pw_multi_aff *pma1, *pma2;
8530 int equal;
8532 str = "{ [i,j] -> [i+j, 4i-j] }";
8533 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
8534 str = "{ [i,j] -> [4i-j] }";
8535 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
8537 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
8539 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
8541 isl_pw_multi_aff_free(pma1);
8542 isl_pw_multi_aff_free(pma2);
8543 if (equal < 0)
8544 return -1;
8545 if (!equal)
8546 isl_die(ctx, isl_error_unknown,
8547 "expressions not equal", return -1);
8549 return 0;
8552 /* Check that we can properly parse multi piecewise affine expressions
8553 * where the piecewise affine expressions have different domains.
8555 static int test_multi_pw_aff_1(isl_ctx *ctx)
8557 const char *str;
8558 isl_set *dom, *dom2;
8559 isl_multi_pw_aff *mpa1, *mpa2;
8560 isl_pw_aff *pa;
8561 int equal;
8562 int equal_domain;
8564 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
8565 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
8566 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
8567 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
8568 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
8569 str = "{ [i] -> [(i : i > 0), 2i] }";
8570 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
8572 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
8574 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
8575 dom = isl_pw_aff_domain(pa);
8576 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
8577 dom2 = isl_pw_aff_domain(pa);
8578 equal_domain = isl_set_is_equal(dom, dom2);
8580 isl_set_free(dom);
8581 isl_set_free(dom2);
8582 isl_multi_pw_aff_free(mpa1);
8583 isl_multi_pw_aff_free(mpa2);
8585 if (equal < 0)
8586 return -1;
8587 if (!equal)
8588 isl_die(ctx, isl_error_unknown,
8589 "expressions not equal", return -1);
8591 if (equal_domain < 0)
8592 return -1;
8593 if (equal_domain)
8594 isl_die(ctx, isl_error_unknown,
8595 "domains unexpectedly equal", return -1);
8597 return 0;
8600 /* Check that the dimensions in the explicit domain
8601 * of a multi piecewise affine expression are properly
8602 * taken into account.
8604 static int test_multi_pw_aff_2(isl_ctx *ctx)
8606 const char *str;
8607 isl_bool involves1, involves2, involves3, equal;
8608 isl_multi_pw_aff *mpa, *mpa1, *mpa2;
8610 str = "{ A[x,y] -> B[] : x >= y }";
8611 mpa = isl_multi_pw_aff_read_from_str(ctx, str);
8612 involves1 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 2);
8613 mpa1 = isl_multi_pw_aff_copy(mpa);
8615 mpa = isl_multi_pw_aff_insert_dims(mpa, isl_dim_in, 0, 1);
8616 involves2 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 1);
8617 involves3 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 1, 2);
8618 str = "{ [a,x,y] -> B[] : x >= y }";
8619 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
8620 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
8621 isl_multi_pw_aff_free(mpa2);
8623 mpa = isl_multi_pw_aff_drop_dims(mpa, isl_dim_in, 0, 1);
8624 mpa = isl_multi_pw_aff_set_tuple_name(mpa, isl_dim_in, "A");
8625 if (equal >= 0 && equal)
8626 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa1);
8627 isl_multi_pw_aff_free(mpa1);
8628 isl_multi_pw_aff_free(mpa);
8630 if (involves1 < 0 || involves2 < 0 || involves3 < 0 || equal < 0)
8631 return -1;
8632 if (!equal)
8633 isl_die(ctx, isl_error_unknown,
8634 "incorrect result of dimension insertion/removal",
8635 return isl_stat_error);
8636 if (!involves1 || involves2 || !involves3)
8637 isl_die(ctx, isl_error_unknown,
8638 "incorrect characterization of involved dimensions",
8639 return isl_stat_error);
8641 return 0;
8644 /* Check that isl_multi_union_pw_aff_multi_val_on_domain
8645 * sets the explicit domain of a zero-dimensional result,
8646 * such that it can be converted to an isl_union_map.
8648 static isl_stat test_multi_pw_aff_3(isl_ctx *ctx)
8650 isl_space *space;
8651 isl_union_set *dom;
8652 isl_multi_val *mv;
8653 isl_multi_union_pw_aff *mupa;
8654 isl_union_map *umap;
8656 dom = isl_union_set_read_from_str(ctx, "{ A[]; B[] }");
8657 space = isl_union_set_get_space(dom);
8658 mv = isl_multi_val_zero(isl_space_set_from_params(space));
8659 mupa = isl_multi_union_pw_aff_multi_val_on_domain(dom, mv);
8660 umap = isl_union_map_from_multi_union_pw_aff(mupa);
8661 isl_union_map_free(umap);
8662 if (!umap)
8663 return isl_stat_error;
8665 return isl_stat_ok;
8668 /* Perform some tests on multi piecewise affine expressions.
8670 static int test_multi_pw_aff(isl_ctx *ctx)
8672 if (test_multi_pw_aff_1(ctx) < 0)
8673 return -1;
8674 if (test_multi_pw_aff_2(ctx) < 0)
8675 return -1;
8676 if (test_multi_pw_aff_3(ctx) < 0)
8677 return -1;
8678 return 0;
8681 /* This is a regression test for a bug where isl_basic_map_simplify
8682 * would end up in an infinite loop. In particular, we construct
8683 * an empty basic set that is not obviously empty.
8684 * isl_basic_set_is_empty marks the basic set as empty.
8685 * After projecting out i3, the variable can be dropped completely,
8686 * but isl_basic_map_simplify refrains from doing so if the basic set
8687 * is empty and would end up in an infinite loop if it didn't test
8688 * explicitly for empty basic maps in the outer loop.
8690 static int test_simplify_1(isl_ctx *ctx)
8692 const char *str;
8693 isl_basic_set *bset;
8694 int empty;
8696 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
8697 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
8698 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
8699 "i3 >= i2 }";
8700 bset = isl_basic_set_read_from_str(ctx, str);
8701 empty = isl_basic_set_is_empty(bset);
8702 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
8703 isl_basic_set_free(bset);
8704 if (!bset)
8705 return -1;
8706 if (!empty)
8707 isl_die(ctx, isl_error_unknown,
8708 "basic set should be empty", return -1);
8710 return 0;
8713 /* Check that the equality in the set description below
8714 * is simplified away.
8716 static int test_simplify_2(isl_ctx *ctx)
8718 const char *str;
8719 isl_basic_set *bset;
8720 isl_bool universe;
8722 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
8723 bset = isl_basic_set_read_from_str(ctx, str);
8724 universe = isl_basic_set_plain_is_universe(bset);
8725 isl_basic_set_free(bset);
8727 if (universe < 0)
8728 return -1;
8729 if (!universe)
8730 isl_die(ctx, isl_error_unknown,
8731 "equality not simplified away", return -1);
8732 return 0;
8735 /* Some simplification tests.
8737 static int test_simplify(isl_ctx *ctx)
8739 if (test_simplify_1(ctx) < 0)
8740 return -1;
8741 if (test_simplify_2(ctx) < 0)
8742 return -1;
8743 return 0;
8746 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
8747 * with gbr context would fail to disable the use of the shifted tableau
8748 * when transferring equalities for the input to the context, resulting
8749 * in invalid sample values.
8751 static int test_partial_lexmin(isl_ctx *ctx)
8753 const char *str;
8754 isl_basic_set *bset;
8755 isl_basic_map *bmap;
8756 isl_map *map;
8758 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
8759 bmap = isl_basic_map_read_from_str(ctx, str);
8760 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
8761 bset = isl_basic_set_read_from_str(ctx, str);
8762 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
8763 isl_map_free(map);
8765 if (!map)
8766 return -1;
8768 return 0;
8771 /* Check that the variable compression performed on the existentially
8772 * quantified variables inside isl_basic_set_compute_divs is not confused
8773 * by the implicit equalities among the parameters.
8775 static int test_compute_divs(isl_ctx *ctx)
8777 const char *str;
8778 isl_basic_set *bset;
8779 isl_set *set;
8781 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
8782 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
8783 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
8784 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
8785 bset = isl_basic_set_read_from_str(ctx, str);
8786 set = isl_basic_set_compute_divs(bset);
8787 isl_set_free(set);
8788 if (!set)
8789 return -1;
8791 return 0;
8794 /* Check that isl_schedule_get_map is not confused by a schedule tree
8795 * with divergent filter node parameters, as can result from a call
8796 * to isl_schedule_intersect_domain.
8798 static int test_schedule_tree(isl_ctx *ctx)
8800 const char *str;
8801 isl_union_set *uset;
8802 isl_schedule *sched1, *sched2;
8803 isl_union_map *umap;
8805 uset = isl_union_set_read_from_str(ctx, "{ A[i] }");
8806 sched1 = isl_schedule_from_domain(uset);
8807 uset = isl_union_set_read_from_str(ctx, "{ B[] }");
8808 sched2 = isl_schedule_from_domain(uset);
8810 sched1 = isl_schedule_sequence(sched1, sched2);
8811 str = "[n] -> { A[i] : 0 <= i < n; B[] }";
8812 uset = isl_union_set_read_from_str(ctx, str);
8813 sched1 = isl_schedule_intersect_domain(sched1, uset);
8814 umap = isl_schedule_get_map(sched1);
8815 isl_schedule_free(sched1);
8816 isl_union_map_free(umap);
8817 if (!umap)
8818 return -1;
8820 return 0;
8823 /* Check that a zero-dimensional prefix schedule keeps track
8824 * of the domain and outer filters.
8826 static int test_schedule_tree_prefix(isl_ctx *ctx)
8828 const char *str;
8829 isl_bool equal;
8830 isl_union_set *uset;
8831 isl_union_set_list *filters;
8832 isl_multi_union_pw_aff *mupa, *mupa2;
8833 isl_schedule_node *node;
8835 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
8836 uset = isl_union_set_read_from_str(ctx, str);
8837 node = isl_schedule_node_from_domain(uset);
8838 node = isl_schedule_node_child(node, 0);
8840 str = "{ S1[i,j] : i > j }";
8841 uset = isl_union_set_read_from_str(ctx, str);
8842 filters = isl_union_set_list_from_union_set(uset);
8843 str = "{ S1[i,j] : i <= j; S2[i,j] }";
8844 uset = isl_union_set_read_from_str(ctx, str);
8845 filters = isl_union_set_list_add(filters, uset);
8846 node = isl_schedule_node_insert_sequence(node, filters);
8848 node = isl_schedule_node_child(node, 0);
8849 node = isl_schedule_node_child(node, 0);
8850 mupa = isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
8851 str = "([] : { S1[i,j] : i > j })";
8852 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx, str);
8853 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
8854 isl_multi_union_pw_aff_free(mupa2);
8855 isl_multi_union_pw_aff_free(mupa);
8856 isl_schedule_node_free(node);
8858 if (equal < 0)
8859 return -1;
8860 if (!equal)
8861 isl_die(ctx, isl_error_unknown, "unexpected prefix schedule",
8862 return -1);
8864 return 0;
8867 /* Check that the reaching domain elements and the prefix schedule
8868 * at a leaf node are the same before and after grouping.
8870 static int test_schedule_tree_group_1(isl_ctx *ctx)
8872 int equal;
8873 const char *str;
8874 isl_id *id;
8875 isl_union_set *uset;
8876 isl_multi_union_pw_aff *mupa;
8877 isl_union_pw_multi_aff *upma1, *upma2;
8878 isl_union_set *domain1, *domain2;
8879 isl_union_map *umap1, *umap2;
8880 isl_schedule_node *node;
8882 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
8883 uset = isl_union_set_read_from_str(ctx, str);
8884 node = isl_schedule_node_from_domain(uset);
8885 node = isl_schedule_node_child(node, 0);
8886 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
8887 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8888 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8889 node = isl_schedule_node_child(node, 0);
8890 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
8891 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8892 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8893 node = isl_schedule_node_child(node, 0);
8894 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
8895 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
8896 domain1 = isl_schedule_node_get_domain(node);
8897 id = isl_id_alloc(ctx, "group", NULL);
8898 node = isl_schedule_node_parent(node);
8899 node = isl_schedule_node_group(node, id);
8900 node = isl_schedule_node_child(node, 0);
8901 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
8902 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
8903 domain2 = isl_schedule_node_get_domain(node);
8904 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
8905 if (equal >= 0 && equal)
8906 equal = isl_union_set_is_equal(domain1, domain2);
8907 if (equal >= 0 && equal)
8908 equal = isl_union_map_is_equal(umap1, umap2);
8909 isl_union_map_free(umap1);
8910 isl_union_map_free(umap2);
8911 isl_union_set_free(domain1);
8912 isl_union_set_free(domain2);
8913 isl_union_pw_multi_aff_free(upma1);
8914 isl_union_pw_multi_aff_free(upma2);
8915 isl_schedule_node_free(node);
8917 if (equal < 0)
8918 return -1;
8919 if (!equal)
8920 isl_die(ctx, isl_error_unknown,
8921 "expressions not equal", return -1);
8923 return 0;
8926 /* Check that we can have nested groupings and that the union map
8927 * schedule representation is the same before and after the grouping.
8928 * Note that after the grouping, the union map representation contains
8929 * the domain constraints from the ranges of the expansion nodes,
8930 * while they are missing from the union map representation of
8931 * the tree without expansion nodes.
8933 * Also check that the global expansion is as expected.
8935 static int test_schedule_tree_group_2(isl_ctx *ctx)
8937 int equal, equal_expansion;
8938 const char *str;
8939 isl_id *id;
8940 isl_union_set *uset;
8941 isl_union_map *umap1, *umap2;
8942 isl_union_map *expansion1, *expansion2;
8943 isl_union_set_list *filters;
8944 isl_multi_union_pw_aff *mupa;
8945 isl_schedule *schedule;
8946 isl_schedule_node *node;
8948 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
8949 "S3[i,j] : 0 <= i,j < 10 }";
8950 uset = isl_union_set_read_from_str(ctx, str);
8951 node = isl_schedule_node_from_domain(uset);
8952 node = isl_schedule_node_child(node, 0);
8953 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
8954 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8955 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8956 node = isl_schedule_node_child(node, 0);
8957 str = "{ S1[i,j] }";
8958 uset = isl_union_set_read_from_str(ctx, str);
8959 filters = isl_union_set_list_from_union_set(uset);
8960 str = "{ S2[i,j]; S3[i,j] }";
8961 uset = isl_union_set_read_from_str(ctx, str);
8962 filters = isl_union_set_list_add(filters, uset);
8963 node = isl_schedule_node_insert_sequence(node, filters);
8964 node = isl_schedule_node_child(node, 1);
8965 node = isl_schedule_node_child(node, 0);
8966 str = "{ S2[i,j] }";
8967 uset = isl_union_set_read_from_str(ctx, str);
8968 filters = isl_union_set_list_from_union_set(uset);
8969 str = "{ S3[i,j] }";
8970 uset = isl_union_set_read_from_str(ctx, str);
8971 filters = isl_union_set_list_add(filters, uset);
8972 node = isl_schedule_node_insert_sequence(node, filters);
8974 schedule = isl_schedule_node_get_schedule(node);
8975 umap1 = isl_schedule_get_map(schedule);
8976 uset = isl_schedule_get_domain(schedule);
8977 umap1 = isl_union_map_intersect_domain(umap1, uset);
8978 isl_schedule_free(schedule);
8980 node = isl_schedule_node_parent(node);
8981 node = isl_schedule_node_parent(node);
8982 id = isl_id_alloc(ctx, "group1", NULL);
8983 node = isl_schedule_node_group(node, id);
8984 node = isl_schedule_node_child(node, 1);
8985 node = isl_schedule_node_child(node, 0);
8986 id = isl_id_alloc(ctx, "group2", NULL);
8987 node = isl_schedule_node_group(node, id);
8989 schedule = isl_schedule_node_get_schedule(node);
8990 umap2 = isl_schedule_get_map(schedule);
8991 isl_schedule_free(schedule);
8993 node = isl_schedule_node_root(node);
8994 node = isl_schedule_node_child(node, 0);
8995 expansion1 = isl_schedule_node_get_subtree_expansion(node);
8996 isl_schedule_node_free(node);
8998 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
8999 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
9000 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
9002 expansion2 = isl_union_map_read_from_str(ctx, str);
9004 equal = isl_union_map_is_equal(umap1, umap2);
9005 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
9007 isl_union_map_free(umap1);
9008 isl_union_map_free(umap2);
9009 isl_union_map_free(expansion1);
9010 isl_union_map_free(expansion2);
9012 if (equal < 0 || equal_expansion < 0)
9013 return -1;
9014 if (!equal)
9015 isl_die(ctx, isl_error_unknown,
9016 "expressions not equal", return -1);
9017 if (!equal_expansion)
9018 isl_die(ctx, isl_error_unknown,
9019 "unexpected expansion", return -1);
9021 return 0;
9024 /* Some tests for the isl_schedule_node_group function.
9026 static int test_schedule_tree_group(isl_ctx *ctx)
9028 if (test_schedule_tree_group_1(ctx) < 0)
9029 return -1;
9030 if (test_schedule_tree_group_2(ctx) < 0)
9031 return -1;
9032 return 0;
9035 struct {
9036 const char *set;
9037 const char *dual;
9038 } coef_tests[] = {
9039 { "{ rat: [i] : 0 <= i <= 10 }",
9040 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
9041 { "{ rat: [i] : FALSE }",
9042 "{ rat: coefficients[[cst] -> [a]] }" },
9043 { "{ rat: [i] : }",
9044 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
9047 struct {
9048 const char *set;
9049 const char *dual;
9050 } sol_tests[] = {
9051 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
9052 "{ rat: [i] : 0 <= i <= 10 }" },
9053 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
9054 "{ rat: [i] }" },
9055 { "{ rat: coefficients[[cst] -> [a]] }",
9056 "{ rat: [i] : FALSE }" },
9059 /* Test the basic functionality of isl_basic_set_coefficients and
9060 * isl_basic_set_solutions.
9062 static int test_dual(isl_ctx *ctx)
9064 int i;
9066 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
9067 int equal;
9068 isl_basic_set *bset1, *bset2;
9070 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
9071 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
9072 bset1 = isl_basic_set_coefficients(bset1);
9073 equal = isl_basic_set_is_equal(bset1, bset2);
9074 isl_basic_set_free(bset1);
9075 isl_basic_set_free(bset2);
9076 if (equal < 0)
9077 return -1;
9078 if (!equal)
9079 isl_die(ctx, isl_error_unknown,
9080 "incorrect dual", return -1);
9083 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
9084 int equal;
9085 isl_basic_set *bset1, *bset2;
9087 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
9088 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
9089 bset1 = isl_basic_set_solutions(bset1);
9090 equal = isl_basic_set_is_equal(bset1, bset2);
9091 isl_basic_set_free(bset1);
9092 isl_basic_set_free(bset2);
9093 if (equal < 0)
9094 return -1;
9095 if (!equal)
9096 isl_die(ctx, isl_error_unknown,
9097 "incorrect dual", return -1);
9100 return 0;
9103 struct {
9104 int scale_tile;
9105 int shift_point;
9106 const char *domain;
9107 const char *schedule;
9108 const char *sizes;
9109 const char *tile;
9110 const char *point;
9111 } tile_tests[] = {
9112 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
9113 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9114 "{ [32,32] }",
9115 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
9116 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9118 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
9119 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9120 "{ [32,32] }",
9121 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
9122 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9124 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
9125 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9126 "{ [32,32] }",
9127 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
9128 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
9130 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
9131 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9132 "{ [32,32] }",
9133 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
9134 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
9138 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
9139 * tile the band and then check if the tile and point bands have the
9140 * expected partial schedule.
9142 static int test_tile(isl_ctx *ctx)
9144 int i;
9145 int scale;
9146 int shift;
9148 scale = isl_options_get_tile_scale_tile_loops(ctx);
9149 shift = isl_options_get_tile_shift_point_loops(ctx);
9151 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
9152 int opt;
9153 int equal;
9154 const char *str;
9155 isl_union_set *domain;
9156 isl_multi_union_pw_aff *mupa, *mupa2;
9157 isl_schedule_node *node;
9158 isl_multi_val *sizes;
9160 opt = tile_tests[i].scale_tile;
9161 isl_options_set_tile_scale_tile_loops(ctx, opt);
9162 opt = tile_tests[i].shift_point;
9163 isl_options_set_tile_shift_point_loops(ctx, opt);
9165 str = tile_tests[i].domain;
9166 domain = isl_union_set_read_from_str(ctx, str);
9167 node = isl_schedule_node_from_domain(domain);
9168 node = isl_schedule_node_child(node, 0);
9169 str = tile_tests[i].schedule;
9170 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9171 node = isl_schedule_node_insert_partial_schedule(node, mupa);
9172 str = tile_tests[i].sizes;
9173 sizes = isl_multi_val_read_from_str(ctx, str);
9174 node = isl_schedule_node_band_tile(node, sizes);
9176 str = tile_tests[i].tile;
9177 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9178 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9179 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
9180 isl_multi_union_pw_aff_free(mupa);
9181 isl_multi_union_pw_aff_free(mupa2);
9183 node = isl_schedule_node_child(node, 0);
9185 str = tile_tests[i].point;
9186 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9187 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9188 if (equal >= 0 && equal)
9189 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
9190 mupa2);
9191 isl_multi_union_pw_aff_free(mupa);
9192 isl_multi_union_pw_aff_free(mupa2);
9194 isl_schedule_node_free(node);
9196 if (equal < 0)
9197 return -1;
9198 if (!equal)
9199 isl_die(ctx, isl_error_unknown,
9200 "unexpected result", return -1);
9203 isl_options_set_tile_scale_tile_loops(ctx, scale);
9204 isl_options_set_tile_shift_point_loops(ctx, shift);
9206 return 0;
9209 /* Check that the domain hash of a space is equal to the hash
9210 * of the domain of the space.
9212 static int test_domain_hash(isl_ctx *ctx)
9214 isl_map *map;
9215 isl_space *space;
9216 uint32_t hash1, hash2;
9218 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
9219 space = isl_map_get_space(map);
9220 isl_map_free(map);
9221 hash1 = isl_space_get_domain_hash(space);
9222 space = isl_space_domain(space);
9223 hash2 = isl_space_get_hash(space);
9224 isl_space_free(space);
9226 if (!space)
9227 return -1;
9228 if (hash1 != hash2)
9229 isl_die(ctx, isl_error_unknown,
9230 "domain hash not equal to hash of domain", return -1);
9232 return 0;
9235 /* Check that a universe basic set that is not obviously equal to the universe
9236 * is still recognized as being equal to the universe.
9238 static int test_universe(isl_ctx *ctx)
9240 const char *s;
9241 isl_basic_set *bset;
9242 isl_bool is_univ;
9244 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
9245 bset = isl_basic_set_read_from_str(ctx, s);
9246 is_univ = isl_basic_set_is_universe(bset);
9247 isl_basic_set_free(bset);
9249 if (is_univ < 0)
9250 return -1;
9251 if (!is_univ)
9252 isl_die(ctx, isl_error_unknown,
9253 "not recognized as universe set", return -1);
9255 return 0;
9258 /* Sets for which chambers are computed and checked.
9260 const char *chambers_tests[] = {
9261 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
9262 "z >= 0 and z <= C - y and z <= B - x - y }",
9265 /* Add the domain of "cell" to "cells".
9267 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
9269 isl_basic_set_list **cells = user;
9270 isl_basic_set *dom;
9272 dom = isl_cell_get_domain(cell);
9273 isl_cell_free(cell);
9274 *cells = isl_basic_set_list_add(*cells, dom);
9276 return *cells ? isl_stat_ok : isl_stat_error;
9279 /* Check that the elements of "list" are pairwise disjoint.
9281 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
9283 int i, j;
9284 isl_size n;
9286 n = isl_basic_set_list_n_basic_set(list);
9287 if (n < 0)
9288 return isl_stat_error;
9290 for (i = 0; i < n; ++i) {
9291 isl_basic_set *bset_i;
9293 bset_i = isl_basic_set_list_get_basic_set(list, i);
9294 for (j = i + 1; j < n; ++j) {
9295 isl_basic_set *bset_j;
9296 isl_bool disjoint;
9298 bset_j = isl_basic_set_list_get_basic_set(list, j);
9299 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
9300 isl_basic_set_free(bset_j);
9301 if (!disjoint)
9302 isl_die(isl_basic_set_list_get_ctx(list),
9303 isl_error_unknown, "not disjoint",
9304 break);
9305 if (disjoint < 0 || !disjoint)
9306 break;
9308 isl_basic_set_free(bset_i);
9309 if (j < n)
9310 return isl_stat_error;
9313 return isl_stat_ok;
9316 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
9317 * are pairwise disjoint.
9319 static int test_chambers(isl_ctx *ctx)
9321 int i;
9323 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
9324 isl_basic_set *bset;
9325 isl_vertices *vertices;
9326 isl_basic_set_list *cells;
9327 isl_stat ok;
9329 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
9330 vertices = isl_basic_set_compute_vertices(bset);
9331 cells = isl_basic_set_list_alloc(ctx, 0);
9332 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
9333 &cells) < 0)
9334 cells = isl_basic_set_list_free(cells);
9335 ok = check_pairwise_disjoint(cells);
9336 isl_basic_set_list_free(cells);
9337 isl_vertices_free(vertices);
9338 isl_basic_set_free(bset);
9340 if (ok < 0)
9341 return -1;
9344 return 0;
9347 struct {
9348 const char *name;
9349 int (*fn)(isl_ctx *ctx);
9350 } tests [] = {
9351 { "universe", &test_universe },
9352 { "domain hash", &test_domain_hash },
9353 { "dual", &test_dual },
9354 { "dependence analysis", &test_flow },
9355 { "val", &test_val },
9356 { "compute divs", &test_compute_divs },
9357 { "partial lexmin", &test_partial_lexmin },
9358 { "simplify", &test_simplify },
9359 { "curry", &test_curry },
9360 { "piecewise multi affine expressions", &test_pw_multi_aff },
9361 { "multi piecewise affine expressions", &test_multi_pw_aff },
9362 { "conversion", &test_conversion },
9363 { "list", &test_list },
9364 { "align parameters", &test_align_parameters },
9365 { "drop unused parameters", &test_drop_unused_parameters },
9366 { "preimage", &test_preimage },
9367 { "pullback", &test_pullback },
9368 { "AST", &test_ast },
9369 { "AST build", &test_ast_build },
9370 { "AST generation", &test_ast_gen },
9371 { "eliminate", &test_eliminate },
9372 { "residue class", &test_residue_class },
9373 { "div", &test_div },
9374 { "slice", &test_slice },
9375 { "fixed power", &test_fixed_power },
9376 { "sample", &test_sample },
9377 { "output", &test_output },
9378 { "vertices", &test_vertices },
9379 { "chambers", &test_chambers },
9380 { "fixed", &test_fixed },
9381 { "equal", &test_equal },
9382 { "disjoint", &test_disjoint },
9383 { "product", &test_product },
9384 { "dim_max", &test_dim_max },
9385 { "affine", &test_aff },
9386 { "injective", &test_injective },
9387 { "schedule (whole component)", &test_schedule_whole },
9388 { "schedule (incremental)", &test_schedule_incremental },
9389 { "schedule tree", &test_schedule_tree },
9390 { "schedule tree prefix", &test_schedule_tree_prefix },
9391 { "schedule tree grouping", &test_schedule_tree_group },
9392 { "tile", &test_tile },
9393 { "union_pw", &test_union_pw },
9394 { "locus", &test_locus },
9395 { "eval", &test_eval },
9396 { "parse", &test_parse },
9397 { "single-valued", &test_sv },
9398 { "affine hull", &test_affine_hull },
9399 { "simple_hull", &test_simple_hull },
9400 { "coalesce", &test_coalesce },
9401 { "factorize", &test_factorize },
9402 { "subset", &test_subset },
9403 { "subtract", &test_subtract },
9404 { "intersect", &test_intersect },
9405 { "lexmin", &test_lexmin },
9406 { "min", &test_min },
9407 { "gist", &test_gist },
9408 { "piecewise quasi-polynomials", &test_pwqp },
9409 { "lift", &test_lift },
9410 { "bound", &test_bound },
9411 { "get lists", &test_get_list },
9412 { "union", &test_union },
9413 { "split periods", &test_split_periods },
9414 { "lexicographic order", &test_lex },
9415 { "bijectivity", &test_bijective },
9416 { "dataflow analysis", &test_dep },
9417 { "reading", &test_read },
9418 { "bounded", &test_bounded },
9419 { "construction", &test_construction },
9420 { "dimension manipulation", &test_dim },
9421 { "map application", &test_application },
9422 { "convex hull", &test_convex_hull },
9423 { "transitive closure", &test_closure },
9424 { "isl_bool", &test_isl_bool},
9427 int main(int argc, char **argv)
9429 int i;
9430 struct isl_ctx *ctx;
9431 struct isl_options *options;
9433 options = isl_options_new_with_defaults();
9434 assert(options);
9435 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
9437 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
9438 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
9439 printf("%s\n", tests[i].name);
9440 if (tests[i].fn(ctx) < 0)
9441 goto error;
9443 isl_ctx_free(ctx);
9444 return 0;
9445 error:
9446 isl_ctx_free(ctx);
9447 return -1;