add isl_*_list_reverse
[isl.git] / isl_test.c
blob3412e171d4773f63ba5061dddef57acd48827cad
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_2exp, "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_2exp, "-3", "1/8" },
714 { &isl_val_2exp, "-1", "1/2" },
715 { &isl_val_2exp, "1", "2" },
716 { &isl_val_2exp, "2", "4" },
717 { &isl_val_2exp, "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 unsigned 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 (!bset)
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 int n;
1261 int 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 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1277 return -1);
1279 /* Check that isl_map_affine_hull is not confused by
1280 * the reordering of divs in isl_map_align_divs.
1282 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1283 "32e0 = b and 32e1 = c); "
1284 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1285 set = isl_set_read_from_str(ctx, str);
1286 bset = isl_set_affine_hull(set);
1287 isl_basic_set_free(bset);
1288 if (!bset)
1289 return -1;
1291 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1292 "32e2 = 31 + 31e0 }";
1293 set = isl_set_read_from_str(ctx, str);
1294 bset = isl_set_affine_hull(set);
1295 str = "{ [a] : exists e : a = 32 e }";
1296 bset2 = isl_basic_set_read_from_str(ctx, str);
1297 subset = isl_basic_set_is_subset(bset, bset2);
1298 isl_basic_set_free(bset);
1299 isl_basic_set_free(bset2);
1300 if (subset < 0)
1301 return -1;
1302 if (!subset)
1303 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1304 return -1);
1306 return 0;
1309 /* Pairs of maps and the corresponding expected results of
1310 * isl_map_plain_unshifted_simple_hull.
1312 struct {
1313 const char *map;
1314 const char *hull;
1315 } plain_unshifted_simple_hull_tests[] = {
1316 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1317 "{ [i] -> [j] : i >= 1 }" },
1318 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1319 "(j mod 4 = 2 and k mod 6 = n) }",
1320 "{ [n] -> [i,j,k] : j mod 4 = 2 }" },
1323 /* Basic tests for isl_map_plain_unshifted_simple_hull.
1325 static int test_plain_unshifted_simple_hull(isl_ctx *ctx)
1327 int i;
1328 isl_map *map;
1329 isl_basic_map *hull, *expected;
1330 isl_bool equal;
1332 for (i = 0; i < ARRAY_SIZE(plain_unshifted_simple_hull_tests); ++i) {
1333 const char *str;
1334 str = plain_unshifted_simple_hull_tests[i].map;
1335 map = isl_map_read_from_str(ctx, str);
1336 str = plain_unshifted_simple_hull_tests[i].hull;
1337 expected = isl_basic_map_read_from_str(ctx, str);
1338 hull = isl_map_plain_unshifted_simple_hull(map);
1339 equal = isl_basic_map_is_equal(hull, expected);
1340 isl_basic_map_free(hull);
1341 isl_basic_map_free(expected);
1342 if (equal < 0)
1343 return -1;
1344 if (!equal)
1345 isl_die(ctx, isl_error_unknown, "unexpected hull",
1346 return -1);
1349 return 0;
1352 /* Pairs of sets and the corresponding expected results of
1353 * isl_set_unshifted_simple_hull.
1355 struct {
1356 const char *set;
1357 const char *hull;
1358 } unshifted_simple_hull_tests[] = {
1359 { "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1360 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1363 /* Basic tests for isl_set_unshifted_simple_hull.
1365 static int test_unshifted_simple_hull(isl_ctx *ctx)
1367 int i;
1368 isl_set *set;
1369 isl_basic_set *hull, *expected;
1370 isl_bool equal;
1372 for (i = 0; i < ARRAY_SIZE(unshifted_simple_hull_tests); ++i) {
1373 const char *str;
1374 str = unshifted_simple_hull_tests[i].set;
1375 set = isl_set_read_from_str(ctx, str);
1376 str = unshifted_simple_hull_tests[i].hull;
1377 expected = isl_basic_set_read_from_str(ctx, str);
1378 hull = isl_set_unshifted_simple_hull(set);
1379 equal = isl_basic_set_is_equal(hull, expected);
1380 isl_basic_set_free(hull);
1381 isl_basic_set_free(expected);
1382 if (equal < 0)
1383 return -1;
1384 if (!equal)
1385 isl_die(ctx, isl_error_unknown, "unexpected hull",
1386 return -1);
1389 return 0;
1392 static int test_simple_hull(struct isl_ctx *ctx)
1394 const char *str;
1395 isl_set *set;
1396 isl_basic_set *bset;
1397 isl_bool is_empty;
1399 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1400 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1401 set = isl_set_read_from_str(ctx, str);
1402 bset = isl_set_simple_hull(set);
1403 is_empty = isl_basic_set_is_empty(bset);
1404 isl_basic_set_free(bset);
1406 if (is_empty == isl_bool_error)
1407 return -1;
1409 if (is_empty == isl_bool_false)
1410 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1411 return -1);
1413 if (test_plain_unshifted_simple_hull(ctx) < 0)
1414 return -1;
1415 if (test_unshifted_simple_hull(ctx) < 0)
1416 return -1;
1418 return 0;
1421 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1423 char *filename;
1424 FILE *input;
1425 struct isl_basic_set *bset1, *bset2;
1426 struct isl_set *set;
1428 filename = get_filename(ctx, name, "polylib");
1429 assert(filename);
1430 input = fopen(filename, "r");
1431 assert(input);
1433 bset1 = isl_basic_set_read_from_file(ctx, input);
1434 bset2 = isl_basic_set_read_from_file(ctx, input);
1436 set = isl_basic_set_union(bset1, bset2);
1437 bset1 = isl_set_convex_hull(set);
1439 bset2 = isl_basic_set_read_from_file(ctx, input);
1441 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1443 isl_basic_set_free(bset1);
1444 isl_basic_set_free(bset2);
1445 free(filename);
1447 fclose(input);
1450 struct {
1451 const char *set;
1452 const char *hull;
1453 } convex_hull_tests[] = {
1454 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1455 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1456 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1457 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1458 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1459 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1460 "i2 <= 5 and i2 >= 4; "
1461 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1462 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1463 "i2 <= 5 + i0 and i2 >= i0 }" },
1464 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1465 "{ [x, y] : 1 = 0 }" },
1466 { "{ [x, y, z] : 0 <= x, y, z <= 10; [x, y, 0] : x >= 0 and y > 0; "
1467 "[x, y, 0] : x >= 0 and y < 0 }",
1468 "{ [x, y, z] : x >= 0 and 0 <= z <= 10 }" },
1471 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1473 int i;
1474 int orig_convex = ctx->opt->convex;
1475 ctx->opt->convex = convex;
1477 test_convex_hull_case(ctx, "convex0");
1478 test_convex_hull_case(ctx, "convex1");
1479 test_convex_hull_case(ctx, "convex2");
1480 test_convex_hull_case(ctx, "convex3");
1481 test_convex_hull_case(ctx, "convex4");
1482 test_convex_hull_case(ctx, "convex5");
1483 test_convex_hull_case(ctx, "convex6");
1484 test_convex_hull_case(ctx, "convex7");
1485 test_convex_hull_case(ctx, "convex8");
1486 test_convex_hull_case(ctx, "convex9");
1487 test_convex_hull_case(ctx, "convex10");
1488 test_convex_hull_case(ctx, "convex11");
1489 test_convex_hull_case(ctx, "convex12");
1490 test_convex_hull_case(ctx, "convex13");
1491 test_convex_hull_case(ctx, "convex14");
1492 test_convex_hull_case(ctx, "convex15");
1494 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1495 isl_set *set1, *set2;
1496 int equal;
1498 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1499 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1500 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1501 equal = isl_set_is_equal(set1, set2);
1502 isl_set_free(set1);
1503 isl_set_free(set2);
1505 if (equal < 0)
1506 return -1;
1507 if (!equal)
1508 isl_die(ctx, isl_error_unknown,
1509 "unexpected convex hull", return -1);
1512 ctx->opt->convex = orig_convex;
1514 return 0;
1517 static int test_convex_hull(isl_ctx *ctx)
1519 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1520 return -1;
1521 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1522 return -1;
1523 return 0;
1526 void test_gist_case(struct isl_ctx *ctx, const char *name)
1528 char *filename;
1529 FILE *input;
1530 struct isl_basic_set *bset1, *bset2;
1532 filename = get_filename(ctx, name, "polylib");
1533 assert(filename);
1534 input = fopen(filename, "r");
1535 assert(input);
1537 bset1 = isl_basic_set_read_from_file(ctx, input);
1538 bset2 = isl_basic_set_read_from_file(ctx, input);
1540 bset1 = isl_basic_set_gist(bset1, bset2);
1542 bset2 = isl_basic_set_read_from_file(ctx, input);
1544 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1546 isl_basic_set_free(bset1);
1547 isl_basic_set_free(bset2);
1548 free(filename);
1550 fclose(input);
1553 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1555 struct {
1556 const char *map;
1557 const char *context;
1558 const char *gist;
1559 } plain_gist_tests[] = {
1560 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1561 "{ [i] -> [j] : i >= 1 }",
1562 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1563 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1564 "(j mod 4 = 2 and k mod 6 = n) }",
1565 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1566 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1567 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1568 "{ [i] -> [j] : i > j }",
1569 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1572 /* Basic tests for isl_map_plain_gist_basic_map.
1574 static int test_plain_gist(isl_ctx *ctx)
1576 int i;
1578 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1579 const char *str;
1580 int equal;
1581 isl_map *map, *gist;
1582 isl_basic_map *context;
1584 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1585 str = plain_gist_tests[i].context;
1586 context = isl_basic_map_read_from_str(ctx, str);
1587 map = isl_map_plain_gist_basic_map(map, context);
1588 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1589 equal = isl_map_is_equal(map, gist);
1590 isl_map_free(map);
1591 isl_map_free(gist);
1592 if (equal < 0)
1593 return -1;
1594 if (!equal)
1595 isl_die(ctx, isl_error_unknown,
1596 "incorrect gist result", return -1);
1599 return 0;
1602 struct {
1603 const char *set;
1604 const char *context;
1605 const char *gist;
1606 } gist_tests[] = {
1607 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1608 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1609 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1610 "{ [a, b, c] : a <= 15 }" },
1611 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1612 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1613 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1614 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1615 { "{ [m, n, a, b] : a <= 2147 + n }",
1616 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1617 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1618 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1619 "b >= 0) }",
1620 "{ [m, n, ku, kl] }" },
1621 { "{ [a, a, b] : a >= 10 }",
1622 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1623 "{ [a, a, b] : a >= 10 }" },
1624 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1625 "{ [0, j] : j >= 0 }" },
1626 /* Check that no constraints on i6 are introduced in the gist */
1627 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1628 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1629 "5e0 <= 381 - t1 and i4 <= 1) }",
1630 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1631 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1632 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1633 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1634 "20e0 >= 1511 - 4t1 - 5i4) }" },
1635 /* Check that no constraints on i6 are introduced in the gist */
1636 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1637 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1638 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1639 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1640 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1641 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1642 "20e1 <= 1530 - 4t1 - 5i4 and "
1643 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1644 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1645 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1646 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1647 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1648 "10e0 <= -91 + 5i4 + 4i6 and "
1649 "10e0 >= -105 + 5i4 + 4i6) }",
1650 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1651 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1652 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1653 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1654 "{ [a, b, q, p] : b >= 1 + a }",
1655 "{ [a, b, q, p] : false }" },
1656 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1657 "[n] -> { [x] : x mod 32 = 0 }",
1658 "[n] -> { [x = n] }" },
1659 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1660 "{ [x] : x mod 2 = 0 }" },
1661 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1662 "{ [x] : x mod 128 = 0 }" },
1663 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1664 "{ [x] : x mod 3200 = 0 }" },
1665 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1666 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1667 "{ [a, b, c = a] }" },
1668 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1669 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1670 "{ [a, b, c = a] : a mod 3 = 0 }" },
1671 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1672 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1673 "{ [x] }" },
1674 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1675 "{ [x,y] : 1 <= y <= 3 }",
1676 "{ [x,y] }" },
1679 /* Check that isl_set_gist behaves as expected.
1681 * For the test cases in gist_tests, besides checking that the result
1682 * is as expected, also check that applying the gist operation does
1683 * not modify the input set (an earlier version of isl would do that) and
1684 * that the test case is consistent, i.e., that the gist has the same
1685 * intersection with the context as the input set.
1687 static int test_gist(struct isl_ctx *ctx)
1689 int i;
1690 const char *str;
1691 isl_basic_set *bset1, *bset2;
1692 isl_map *map1, *map2;
1693 int equal;
1695 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1696 int equal_input, equal_intersection;
1697 isl_set *set1, *set2, *copy, *context;
1699 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1700 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1701 copy = isl_set_copy(set1);
1702 set1 = isl_set_gist(set1, isl_set_copy(context));
1703 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1704 equal = isl_set_is_equal(set1, set2);
1705 isl_set_free(set1);
1706 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1707 equal_input = isl_set_is_equal(set1, copy);
1708 isl_set_free(copy);
1709 set1 = isl_set_intersect(set1, isl_set_copy(context));
1710 set2 = isl_set_intersect(set2, context);
1711 equal_intersection = isl_set_is_equal(set1, set2);
1712 isl_set_free(set2);
1713 isl_set_free(set1);
1714 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1715 return -1;
1716 if (!equal)
1717 isl_die(ctx, isl_error_unknown,
1718 "incorrect gist result", return -1);
1719 if (!equal_input)
1720 isl_die(ctx, isl_error_unknown,
1721 "gist modified input", return -1);
1722 if (!equal_input)
1723 isl_die(ctx, isl_error_unknown,
1724 "inconsistent gist test case", return -1);
1727 test_gist_case(ctx, "gist1");
1729 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1730 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1731 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1732 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1733 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1734 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1735 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1736 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1737 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1738 bset1 = isl_basic_set_read_from_str(ctx, str);
1739 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1740 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1741 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1742 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1743 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1744 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1745 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1746 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1747 bset2 = isl_basic_set_read_from_str(ctx, str);
1748 bset1 = isl_basic_set_gist(bset1, bset2);
1749 assert(bset1 && bset1->n_div == 0);
1750 isl_basic_set_free(bset1);
1752 /* Check that the integer divisions of the second disjunct
1753 * do not spread to the first disjunct.
1755 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1756 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1757 "(exists (e0 = [(-1 + t1)/16], "
1758 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1759 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1760 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1761 "o0 <= 4294967295 and t1 <= -1)) }";
1762 map1 = isl_map_read_from_str(ctx, str);
1763 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1764 map2 = isl_map_read_from_str(ctx, str);
1765 map1 = isl_map_gist(map1, map2);
1766 if (!map1)
1767 return -1;
1768 if (map1->n != 1)
1769 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1770 isl_map_free(map1); return -1);
1771 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1772 isl_die(ctx, isl_error_unknown, "expecting single div",
1773 isl_map_free(map1); return -1);
1774 isl_map_free(map1);
1776 if (test_plain_gist(ctx) < 0)
1777 return -1;
1779 return 0;
1782 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1784 isl_set *set, *set2;
1785 int equal;
1786 int one;
1788 set = isl_set_read_from_str(ctx, str);
1789 set = isl_set_coalesce(set);
1790 set2 = isl_set_read_from_str(ctx, str);
1791 equal = isl_set_is_equal(set, set2);
1792 one = set && set->n == 1;
1793 isl_set_free(set);
1794 isl_set_free(set2);
1796 if (equal < 0)
1797 return -1;
1798 if (!equal)
1799 isl_die(ctx, isl_error_unknown,
1800 "coalesced set not equal to input", return -1);
1801 if (check_one && !one)
1802 isl_die(ctx, isl_error_unknown,
1803 "coalesced set should not be a union", return -1);
1805 return 0;
1808 /* Inputs for coalescing tests with unbounded wrapping.
1809 * "str" is a string representation of the input set.
1810 * "single_disjunct" is set if we expect the result to consist of
1811 * a single disjunct.
1813 struct {
1814 int single_disjunct;
1815 const char *str;
1816 } coalesce_unbounded_tests[] = {
1817 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1818 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1819 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1820 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1821 "-10 <= y <= 0}" },
1822 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1823 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1824 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1825 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1828 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1829 * option turned off.
1831 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1833 int i;
1834 int r = 0;
1835 int bounded;
1837 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1838 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1840 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1841 const char *str = coalesce_unbounded_tests[i].str;
1842 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1843 if (test_coalesce_set(ctx, str, check_one) >= 0)
1844 continue;
1845 r = -1;
1846 break;
1849 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1851 return r;
1854 /* Inputs for coalescing tests.
1855 * "str" is a string representation of the input set.
1856 * "single_disjunct" is set if we expect the result to consist of
1857 * a single disjunct.
1859 struct {
1860 int single_disjunct;
1861 const char *str;
1862 } coalesce_tests[] = {
1863 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1864 "y >= x & x >= 2 & 5 >= y }" },
1865 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1866 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1867 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1868 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1869 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1870 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1871 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1872 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1873 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1874 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1875 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1876 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1877 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1878 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1879 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1880 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1881 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1882 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1883 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1884 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1885 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1886 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1887 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1888 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1889 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1890 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1891 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1892 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1893 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1894 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1895 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1896 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1897 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1898 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1899 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1900 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1901 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1902 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1903 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1904 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1905 "[o0, o1, o2, o3, o4, o5, o6]] : "
1906 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1907 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1908 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1909 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1910 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1911 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1912 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1913 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1914 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1915 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1916 "o6 >= i3 + i6 - o3 and M >= 0 and "
1917 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1918 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1919 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1920 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1921 "(o0 = 0 and M >= 2 and N >= 3) or "
1922 "(M = 0 and o0 = 0 and N >= 3) }" },
1923 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1924 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1925 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1926 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1927 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1928 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1929 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1930 "(y = 3 and x = 1) }" },
1931 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1932 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1933 "i1 <= M and i3 <= M and i4 <= M) or "
1934 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1935 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1936 "i4 <= -1 + M) }" },
1937 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1938 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1939 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1940 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1941 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1942 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1943 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1944 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1945 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1946 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1947 { 0, "{ [a, b] : exists e : 2e = a and "
1948 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1949 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1950 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1951 "j >= 1 and j' <= i + j - i' and i >= 1; "
1952 "[1, 1, 1, 1] }" },
1953 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1954 "[i,j] : exists a : j = 3a }" },
1955 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1956 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1957 "a >= 3) or "
1958 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1959 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1960 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1961 "c <= 6 + 8a and a >= 3; "
1962 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1963 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1964 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1965 "[x,0] : 3 <= x <= 4 }" },
1966 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1967 "[x,0] : 4 <= x <= 5 }" },
1968 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1969 "[x,0] : 3 <= x <= 5 }" },
1970 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1971 "[x,0] : 3 <= x <= 4 }" },
1972 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1973 "i1 <= 0; "
1974 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1975 { 1, "{ [0,0]; [1,1] }" },
1976 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
1977 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
1978 "ii <= k;"
1979 "[k, 0, k] : k <= 6 and k >= 1 }" },
1980 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
1981 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
1982 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
1983 { 1, "[n] -> { [1] : n >= 0;"
1984 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
1985 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
1986 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
1987 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
1988 "2e0 <= x and 2e0 <= n);"
1989 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
1990 "n >= 0) }" },
1991 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
1992 "128e0 >= -134 + 127t1 and t1 >= 2 and "
1993 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
1994 "t1 = 1 }" },
1995 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
1996 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
1997 "[0, 0] }" },
1998 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
1999 "t1 >= 13 and t1 <= 16);"
2000 "[t1] : t1 <= 15 and t1 >= 12 }" },
2001 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
2002 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
2003 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
2004 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
2005 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
2006 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
2007 "i <= 4j + 2 }" },
2008 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
2009 "(exists (e0 : 3e0 = -2 + c0)) }" },
2010 { 0, "[n, b0, t0] -> "
2011 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2012 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2013 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2014 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2015 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
2016 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
2017 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
2018 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
2019 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2020 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2021 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2022 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
2023 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
2024 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
2025 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
2026 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
2027 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
2028 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
2029 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
2030 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
2031 { 0, "{ [i0, i1, i2] : "
2032 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
2033 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
2034 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
2035 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
2036 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
2037 "32e0 <= 31 + i0)) or "
2038 "i0 >= 0 }" },
2039 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
2040 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
2041 "2*floor((c)/2) = c and 0 <= a <= 192;"
2042 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
2044 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
2045 "(0 <= a <= b <= n) }" },
2046 { 1, "{ [a, b] : 0 <= a <= 2 and b >= 0 and "
2047 "((0 < b <= 13) or (2*floor((a + b)/2) >= -5 + a + 2b)) }" },
2048 { 1, "{ [a] : (2 <= a <= 5) or (a mod 2 = 1 and 1 <= a <= 5) }" },
2049 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2050 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2051 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2052 "b = 3 and 9e0 <= -19 + 2c)) }" },
2053 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2054 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2055 "(a = 4 and b = 3 and "
2056 "9*floor((-16 + 2c)/9) <= -19 + 2c) }" },
2057 { 0, "{ [a, b, c] : (b <= 2 and b <= -2 + a) or "
2058 "(b = -1 + a and 0 < a <= 3 and "
2059 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2060 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2061 "b = 3 and 9e0 <= -19 + 2c)) }" },
2062 { 1, "{ [y, x] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2063 "[1, 0] }" },
2064 { 1, "{ [x, y] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2065 "[0, 1] }" },
2066 { 1, "{ [1, y] : -1 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2067 { 1, "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2068 { 1, "{ [x, y] : 0 <= x <= 10 and x - 4*floor(x/4) <= 1 and y <= 0; "
2069 "[x, y] : 0 <= x <= 10 and x - 4*floor(x/4) > 1 and y <= 0; "
2070 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) <= 1 and 0 < y; "
2071 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) > 1 and 0 < y }" },
2072 { 1, "{ [x, 0] : 0 <= x <= 10 and x mod 2 = 0; "
2073 "[x, 0] : 0 <= x <= 10 and x mod 2 = 1; "
2074 "[x, y] : 0 <= x <= 10 and 1 <= y <= 10 }" },
2075 { 1, "{ [a] : a <= 8 and "
2076 "(a mod 10 = 7 or a mod 10 = 8 or a mod 10 = 9) }" },
2077 { 1, "{ [x, y] : 2y = -x and x <= 0 or "
2078 "x <= -1 and 2y <= -x - 1 and 2y >= x - 1 }" },
2079 { 0, "{ [x, y] : 2y = -x and x <= 0 or "
2080 "x <= -2 and 2y <= -x - 1 and 2y >= x - 1 }" },
2081 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2082 "(a < 0 and 3*floor((a)/3) < a) }" },
2083 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2084 "(a < -1 and 3*floor((a)/3) < a) }" },
2085 { 1, "{ [a, b] : a <= 1024 and b >= 0 and "
2086 "((-31 - a + b <= 32*floor((-1 - a)/32) <= -33 + b and "
2087 "32*floor((-1 - a)/32) <= -16 + b + 16*floor((-1 - a)/16))"
2088 "or (2 <= a <= 15 and b < a)) }" },
2089 { 1, "{ [a] : a > 0 and ((16*floor((a)/16) < a and "
2090 "32*floor((a)/32) < a) or a <= 15) }" },
2091 { 1, "{ [a, b, c, d] : (-a + d) mod 64 = 0 and a <= 8 and b <= 1 and "
2092 "10 - a <= c <= 3 and d >= 5 and 9 - 64b <= d <= 70;"
2093 "[a, b = 1, c, d] : (-a + d) mod 64 = 0 and a <= 8 and c >= 4 and "
2094 "10 - a <= c <= 5 and 5 <= d <= 73 - c }" },
2095 { 1, "[n, m] -> { S_0[i] : (-n + i) mod 3 = 0 and m >= 3 + n and "
2096 "i >= n and 3*floor((2 + n + 2m)/3) <= n + 3m - i; "
2097 "S_0[n] : n <= m <= 2 + n }" },
2098 { 1, "{ [a, b] : exists (e0: 0 <= a <= 1 and b >= 0 and "
2099 "2e0 >= -5 + a + 2b and 2e0 >= -1 + a + b and "
2100 "2e0 <= a + b); "
2101 "[a, b] : exists (e0: 0 <= a <= 1 and 2e0 >= -5 + a + 2b and "
2102 "2e0 >= -1 - a + b and 2e0 <= -a + b and "
2103 "2e0 < -a + 2b) }" },
2104 { 1, "{ [i, j, i - 8j] : 8 <= i <= 63 and -7 + i <= 8j <= i; "
2105 "[i, 0, i] : 0 <= i <= 7 }" },
2106 { 1, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [1, 1] }" },
2107 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [0, 2] }" },
2108 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [-1, 3] }" },
2109 { 1, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [1, 1] }" },
2110 { 0, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [2, 1] }" },
2111 { 0, "{ [a, c] : (2 + a) mod 4 = 0 or "
2112 "(c = 4 + a and 4 * floor((a)/4) = a and a >= 0 and a <= 4) or "
2113 "(c = 3 + a and 4 * floor((-1 + a)/4) = -1 + a and "
2114 "a > 0 and a <= 5) }" },
2117 /* A specialized coalescing test case that would result
2118 * in a segmentation fault or a failed assertion in earlier versions of isl.
2120 static int test_coalesce_special(struct isl_ctx *ctx)
2122 const char *str;
2123 isl_map *map1, *map2;
2125 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2126 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
2127 "(y = 201 and o1 <= 239 and o1 >= 212) or "
2128 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
2129 "o1 <= 239 and o1 >= 212)) or "
2130 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
2131 "o1 <= 241 and o1 >= 240));"
2132 "[S_L220_OUT[] -> T7[]] -> "
2133 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
2134 "(y = 2 and o1 <= 241 and o1 >= 212) or "
2135 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
2136 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
2137 map1 = isl_map_read_from_str(ctx, str);
2138 map1 = isl_map_align_divs_internal(map1);
2139 map1 = isl_map_coalesce(map1);
2140 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2141 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
2142 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
2143 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
2144 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
2145 map2 = isl_map_read_from_str(ctx, str);
2146 map2 = isl_map_union(map2, map1);
2147 map2 = isl_map_align_divs_internal(map2);
2148 map2 = isl_map_coalesce(map2);
2149 isl_map_free(map2);
2150 if (!map2)
2151 return -1;
2153 return 0;
2156 /* A specialized coalescing test case that would result in an assertion
2157 * in an earlier version of isl.
2158 * The explicit call to isl_basic_set_union prevents the implicit
2159 * equality constraints in the first basic map from being detected prior
2160 * to the call to isl_set_coalesce, at least at the point
2161 * where this test case was introduced.
2163 static int test_coalesce_special2(struct isl_ctx *ctx)
2165 const char *str;
2166 isl_basic_set *bset1, *bset2;
2167 isl_set *set;
2169 str = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
2170 bset1 = isl_basic_set_read_from_str(ctx, str);
2171 str = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }" ;
2172 bset2 = isl_basic_set_read_from_str(ctx, str);
2173 set = isl_basic_set_union(bset1, bset2);
2174 set = isl_set_coalesce(set);
2175 isl_set_free(set);
2177 if (!set)
2178 return -1;
2179 return 0;
2182 /* Check that calling isl_set_coalesce does not leave other sets
2183 * that may share some information with the input to isl_set_coalesce
2184 * in an inconsistent state.
2185 * In particular, older versions of isl would modify all copies
2186 * of the basic sets in the isl_set_coalesce input in a way
2187 * that could leave them in an inconsistent state.
2188 * The result of printing any other set containing one of these
2189 * basic sets would then result in an invalid set description.
2191 static int test_coalesce_special3(isl_ctx *ctx)
2193 const char *str;
2194 char *s;
2195 isl_set *set1, *set2;
2196 isl_printer *p;
2198 set1 = isl_set_read_from_str(ctx, "{ [0, 0, 0] }");
2199 str = "{ [a, b, a + b] : a >= 0 and b >= 0 and 0 < a + b }";
2200 set2 = isl_set_read_from_str(ctx, str);
2201 set1 = isl_set_union(set1, isl_set_copy(set2));
2202 set1 = isl_set_coalesce(set1);
2203 isl_set_free(set1);
2205 p = isl_printer_to_str(ctx);
2206 p = isl_printer_print_set(p, set2);
2207 isl_set_free(set2);
2208 s = isl_printer_get_str(p);
2209 isl_printer_free(p);
2210 set1 = isl_set_read_from_str(ctx, s);
2211 free(s);
2212 isl_set_free(set1);
2214 if (!set1)
2215 return -1;
2217 return 0;
2220 /* Test the functionality of isl_set_coalesce.
2221 * That is, check that the output is always equal to the input
2222 * and in some cases that the result consists of a single disjunct.
2224 static int test_coalesce(struct isl_ctx *ctx)
2226 int i;
2228 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
2229 const char *str = coalesce_tests[i].str;
2230 int check_one = coalesce_tests[i].single_disjunct;
2231 if (test_coalesce_set(ctx, str, check_one) < 0)
2232 return -1;
2235 if (test_coalesce_unbounded_wrapping(ctx) < 0)
2236 return -1;
2237 if (test_coalesce_special(ctx) < 0)
2238 return -1;
2239 if (test_coalesce_special2(ctx) < 0)
2240 return -1;
2241 if (test_coalesce_special3(ctx) < 0)
2242 return -1;
2244 return 0;
2247 /* Construct a representation of the graph on the right of Figure 1
2248 * in "Computing the Transitive Closure of a Union of
2249 * Affine Integer Tuple Relations".
2251 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2253 isl_set *dom;
2254 isl_map *up, *right;
2256 dom = isl_set_read_from_str(ctx,
2257 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2258 "2 x - 3 y + 3 >= 0 }");
2259 right = isl_map_read_from_str(ctx,
2260 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2261 up = isl_map_read_from_str(ctx,
2262 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2263 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2264 right = isl_map_intersect_range(right, isl_set_copy(dom));
2265 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2266 up = isl_map_intersect_range(up, dom);
2267 return isl_map_union(up, right);
2270 /* Construct a representation of the power of the graph
2271 * on the right of Figure 1 in "Computing the Transitive Closure of
2272 * a Union of Affine Integer Tuple Relations".
2274 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2276 return isl_map_read_from_str(ctx,
2277 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2278 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2279 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2282 /* Construct a representation of the transitive closure of the graph
2283 * on the right of Figure 1 in "Computing the Transitive Closure of
2284 * a Union of Affine Integer Tuple Relations".
2286 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2288 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2291 static int test_closure(isl_ctx *ctx)
2293 const char *str;
2294 isl_map *map, *map2;
2295 int exact, equal;
2297 /* COCOA example 1 */
2298 map = isl_map_read_from_str(ctx,
2299 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2300 "1 <= i and i < n and 1 <= j and j < n or "
2301 "i2 = i + 1 and j2 = j - 1 and "
2302 "1 <= i and i < n and 2 <= j and j <= n }");
2303 map = isl_map_power(map, &exact);
2304 assert(exact);
2305 isl_map_free(map);
2307 /* COCOA example 1 */
2308 map = isl_map_read_from_str(ctx,
2309 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2310 "1 <= i and i < n and 1 <= j and j < n or "
2311 "i2 = i + 1 and j2 = j - 1 and "
2312 "1 <= i and i < n and 2 <= j and j <= n }");
2313 map = isl_map_transitive_closure(map, &exact);
2314 assert(exact);
2315 map2 = isl_map_read_from_str(ctx,
2316 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2317 "1 <= i and i < n and 1 <= j and j <= n and "
2318 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2319 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2320 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2321 assert(isl_map_is_equal(map, map2));
2322 isl_map_free(map2);
2323 isl_map_free(map);
2325 map = isl_map_read_from_str(ctx,
2326 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2327 " 0 <= y and y <= n }");
2328 map = isl_map_transitive_closure(map, &exact);
2329 map2 = isl_map_read_from_str(ctx,
2330 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2331 " 0 <= y and y <= n }");
2332 assert(isl_map_is_equal(map, map2));
2333 isl_map_free(map2);
2334 isl_map_free(map);
2336 /* COCOA example 2 */
2337 map = isl_map_read_from_str(ctx,
2338 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2339 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2340 "i2 = i + 2 and j2 = j - 2 and "
2341 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2342 map = isl_map_transitive_closure(map, &exact);
2343 assert(exact);
2344 map2 = isl_map_read_from_str(ctx,
2345 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2346 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2347 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2348 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2349 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2350 assert(isl_map_is_equal(map, map2));
2351 isl_map_free(map);
2352 isl_map_free(map2);
2354 /* COCOA Fig.2 left */
2355 map = isl_map_read_from_str(ctx,
2356 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2357 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2358 "j <= n or "
2359 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2360 "j <= 2 i - 3 and j <= n - 2 or "
2361 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2362 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2363 map = isl_map_transitive_closure(map, &exact);
2364 assert(exact);
2365 isl_map_free(map);
2367 /* COCOA Fig.2 right */
2368 map = isl_map_read_from_str(ctx,
2369 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2370 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2371 "j <= n or "
2372 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2373 "j <= 2 i - 4 and j <= n - 3 or "
2374 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2375 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2376 map = isl_map_power(map, &exact);
2377 assert(exact);
2378 isl_map_free(map);
2380 /* COCOA Fig.2 right */
2381 map = isl_map_read_from_str(ctx,
2382 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2383 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2384 "j <= n or "
2385 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2386 "j <= 2 i - 4 and j <= n - 3 or "
2387 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2388 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2389 map = isl_map_transitive_closure(map, &exact);
2390 assert(exact);
2391 map2 = isl_map_read_from_str(ctx,
2392 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2393 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2394 "j <= n and 3 + i + 2 j <= 3 n and "
2395 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2396 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2397 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2398 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2399 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2400 assert(isl_map_is_equal(map, map2));
2401 isl_map_free(map2);
2402 isl_map_free(map);
2404 map = cocoa_fig_1_right_graph(ctx);
2405 map = isl_map_transitive_closure(map, &exact);
2406 assert(exact);
2407 map2 = cocoa_fig_1_right_tc(ctx);
2408 assert(isl_map_is_equal(map, map2));
2409 isl_map_free(map2);
2410 isl_map_free(map);
2412 map = cocoa_fig_1_right_graph(ctx);
2413 map = isl_map_power(map, &exact);
2414 map2 = cocoa_fig_1_right_power(ctx);
2415 equal = isl_map_is_equal(map, map2);
2416 isl_map_free(map2);
2417 isl_map_free(map);
2418 if (equal < 0)
2419 return -1;
2420 if (!exact)
2421 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2422 if (!equal)
2423 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2425 /* COCOA Theorem 1 counter example */
2426 map = isl_map_read_from_str(ctx,
2427 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2428 "i2 = 1 and j2 = j or "
2429 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2430 map = isl_map_transitive_closure(map, &exact);
2431 assert(exact);
2432 isl_map_free(map);
2434 map = isl_map_read_from_str(ctx,
2435 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2436 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2437 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2438 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2439 map = isl_map_transitive_closure(map, &exact);
2440 assert(exact);
2441 isl_map_free(map);
2443 /* Kelly et al 1996, fig 12 */
2444 map = isl_map_read_from_str(ctx,
2445 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2446 "1 <= i,j,j+1 <= n or "
2447 "j = n and j2 = 1 and i2 = i + 1 and "
2448 "1 <= i,i+1 <= n }");
2449 map = isl_map_transitive_closure(map, &exact);
2450 assert(exact);
2451 map2 = isl_map_read_from_str(ctx,
2452 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2453 "1 <= i <= n and i = i2 or "
2454 "1 <= i < i2 <= n and 1 <= j <= n and "
2455 "1 <= j2 <= n }");
2456 assert(isl_map_is_equal(map, map2));
2457 isl_map_free(map2);
2458 isl_map_free(map);
2460 /* Omega's closure4 */
2461 map = isl_map_read_from_str(ctx,
2462 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2463 "1 <= x,y <= 10 or "
2464 "x2 = x + 1 and y2 = y and "
2465 "1 <= x <= 20 && 5 <= y <= 15 }");
2466 map = isl_map_transitive_closure(map, &exact);
2467 assert(exact);
2468 isl_map_free(map);
2470 map = isl_map_read_from_str(ctx,
2471 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2472 map = isl_map_transitive_closure(map, &exact);
2473 assert(!exact);
2474 map2 = isl_map_read_from_str(ctx,
2475 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2476 assert(isl_map_is_equal(map, map2));
2477 isl_map_free(map);
2478 isl_map_free(map2);
2480 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2481 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2482 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2483 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2484 map = isl_map_read_from_str(ctx, str);
2485 map = isl_map_transitive_closure(map, &exact);
2486 assert(exact);
2487 map2 = isl_map_read_from_str(ctx, str);
2488 assert(isl_map_is_equal(map, map2));
2489 isl_map_free(map);
2490 isl_map_free(map2);
2492 str = "{[0] -> [1]; [2] -> [3]}";
2493 map = isl_map_read_from_str(ctx, str);
2494 map = isl_map_transitive_closure(map, &exact);
2495 assert(exact);
2496 map2 = isl_map_read_from_str(ctx, str);
2497 assert(isl_map_is_equal(map, map2));
2498 isl_map_free(map);
2499 isl_map_free(map2);
2501 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2502 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2503 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2504 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2505 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2506 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2507 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2508 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2509 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2510 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2511 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2512 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2513 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2514 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2515 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2516 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2517 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2518 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2519 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2520 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2521 map = isl_map_read_from_str(ctx, str);
2522 map = isl_map_transitive_closure(map, NULL);
2523 assert(map);
2524 isl_map_free(map);
2526 return 0;
2529 static int test_lex(struct isl_ctx *ctx)
2531 isl_space *dim;
2532 isl_map *map;
2533 int empty;
2535 dim = isl_space_set_alloc(ctx, 0, 0);
2536 map = isl_map_lex_le(dim);
2537 empty = isl_map_is_empty(map);
2538 isl_map_free(map);
2540 if (empty < 0)
2541 return -1;
2542 if (empty)
2543 isl_die(ctx, isl_error_unknown,
2544 "expecting non-empty result", return -1);
2546 return 0;
2549 /* Inputs for isl_map_lexmin tests.
2550 * "map" is the input and "lexmin" is the expected result.
2552 struct {
2553 const char *map;
2554 const char *lexmin;
2555 } lexmin_tests [] = {
2556 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
2557 "{ [x] -> [5] : 6 <= x <= 8; "
2558 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
2559 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
2560 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
2561 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
2562 "{ [x] -> [y] : (4y = x and x >= 0) or "
2563 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2564 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2565 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
2566 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
2567 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
2568 /* Check that empty pieces are properly combined. */
2569 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2570 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2571 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
2572 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2573 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2574 "x >= 4 }" },
2575 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
2576 "a <= 255 and c <= 255 and d <= 255 - j and "
2577 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
2578 "247d <= 247 + k - j and 247d <= 247 + k - b and "
2579 "247d <= 247 + i and 248 - b <= 248d <= c and "
2580 "254d >= i - a + b and 254d >= -a + b and "
2581 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
2582 "{ [i, k, j] -> "
2583 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
2584 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
2585 "247j >= 62738 - i and 509j <= 129795 + i and "
2586 "742j >= 188724 - i; "
2587 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
2588 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
2589 "16*floor((8 + b)/16) <= 7 + b; "
2590 "[a] -> [1] }",
2591 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
2592 "[a] -> [b = 0] : 0 < a <= 509 }" },
2593 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
2594 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
2597 static int test_lexmin(struct isl_ctx *ctx)
2599 int i;
2600 int equal;
2601 const char *str;
2602 isl_basic_map *bmap;
2603 isl_map *map, *map2;
2604 isl_set *set;
2605 isl_set *set2;
2606 isl_pw_multi_aff *pma;
2608 str = "[p0, p1] -> { [] -> [] : "
2609 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2610 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2611 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2612 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2613 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2614 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2615 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2616 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2617 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2618 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2619 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2620 map = isl_map_read_from_str(ctx, str);
2621 map = isl_map_lexmin(map);
2622 isl_map_free(map);
2624 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2625 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2626 set = isl_set_read_from_str(ctx, str);
2627 set = isl_set_lexmax(set);
2628 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2629 set2 = isl_set_read_from_str(ctx, str);
2630 set = isl_set_intersect(set, set2);
2631 assert(!isl_set_is_empty(set));
2632 isl_set_free(set);
2634 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
2635 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
2636 map = isl_map_lexmin(map);
2637 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
2638 equal = isl_map_is_equal(map, map2);
2639 isl_map_free(map);
2640 isl_map_free(map2);
2642 if (equal < 0)
2643 return -1;
2644 if (!equal)
2645 isl_die(ctx, isl_error_unknown,
2646 "unexpected result", return -1);
2649 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2650 " 8i' <= i and 8i' >= -7 + i }";
2651 bmap = isl_basic_map_read_from_str(ctx, str);
2652 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2653 map2 = isl_map_from_pw_multi_aff(pma);
2654 map = isl_map_from_basic_map(bmap);
2655 assert(isl_map_is_equal(map, map2));
2656 isl_map_free(map);
2657 isl_map_free(map2);
2659 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2660 " 8i' <= i and 8i' >= -7 + i }";
2661 set = isl_set_read_from_str(ctx, str);
2662 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2663 set2 = isl_set_from_pw_multi_aff(pma);
2664 equal = isl_set_is_equal(set, set2);
2665 isl_set_free(set);
2666 isl_set_free(set2);
2667 if (equal < 0)
2668 return -1;
2669 if (!equal)
2670 isl_die(ctx, isl_error_unknown,
2671 "unexpected difference between set and "
2672 "piecewise affine expression", return -1);
2674 return 0;
2677 /* A specialized isl_set_min_val test case that would return the wrong result
2678 * in earlier versions of isl.
2679 * The explicit call to isl_basic_set_union prevents the second basic set
2680 * from being determined to be empty prior to the call to isl_set_min_val,
2681 * at least at the point where this test case was introduced.
2683 static int test_min_special(isl_ctx *ctx)
2685 const char *str;
2686 isl_basic_set *bset1, *bset2;
2687 isl_set *set;
2688 isl_aff *obj;
2689 isl_val *res;
2690 int ok;
2692 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
2693 bset1 = isl_basic_set_read_from_str(ctx, str);
2694 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
2695 bset2 = isl_basic_set_read_from_str(ctx, str);
2696 set = isl_basic_set_union(bset1, bset2);
2697 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
2699 res = isl_set_min_val(set, obj);
2700 ok = isl_val_cmp_si(res, 5) == 0;
2702 isl_aff_free(obj);
2703 isl_set_free(set);
2704 isl_val_free(res);
2706 if (!res)
2707 return -1;
2708 if (!ok)
2709 isl_die(ctx, isl_error_unknown, "unexpected minimum",
2710 return -1);
2712 return 0;
2715 /* A specialized isl_set_min_val test case that would return an error
2716 * in earlier versions of isl.
2718 static int test_min_special2(isl_ctx *ctx)
2720 const char *str;
2721 isl_basic_set *bset;
2722 isl_aff *obj;
2723 isl_val *res;
2725 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
2726 bset = isl_basic_set_read_from_str(ctx, str);
2728 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
2730 res = isl_basic_set_max_val(bset, obj);
2732 isl_basic_set_free(bset);
2733 isl_aff_free(obj);
2734 isl_val_free(res);
2736 if (!res)
2737 return -1;
2739 return 0;
2742 struct {
2743 const char *set;
2744 const char *obj;
2745 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2746 __isl_keep isl_aff *obj);
2747 const char *res;
2748 } opt_tests[] = {
2749 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2750 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2751 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2752 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2753 &isl_set_max_val, "30" },
2757 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2758 * In particular, check the results on non-convex inputs.
2760 static int test_min(struct isl_ctx *ctx)
2762 int i;
2763 isl_set *set;
2764 isl_aff *obj;
2765 isl_val *val, *res;
2766 isl_bool ok;
2768 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2769 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2770 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2771 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2772 val = opt_tests[i].fn(set, obj);
2773 ok = isl_val_eq(res, val);
2774 isl_val_free(res);
2775 isl_val_free(val);
2776 isl_aff_free(obj);
2777 isl_set_free(set);
2779 if (ok < 0)
2780 return -1;
2781 if (!ok)
2782 isl_die(ctx, isl_error_unknown,
2783 "unexpected optimum", return -1);
2786 if (test_min_special(ctx) < 0)
2787 return -1;
2788 if (test_min_special2(ctx) < 0)
2789 return -1;
2791 return 0;
2794 struct must_may {
2795 isl_map *must;
2796 isl_map *may;
2799 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2800 void *dep_user, void *user)
2802 struct must_may *mm = (struct must_may *)user;
2804 if (must)
2805 mm->must = isl_map_union(mm->must, dep);
2806 else
2807 mm->may = isl_map_union(mm->may, dep);
2809 return isl_stat_ok;
2812 static int common_space(void *first, void *second)
2814 int depth = *(int *)first;
2815 return 2 * depth;
2818 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2820 isl_map *map2;
2821 int equal;
2823 if (!map)
2824 return -1;
2826 map2 = isl_map_read_from_str(map->ctx, str);
2827 equal = isl_map_is_equal(map, map2);
2828 isl_map_free(map2);
2830 return equal;
2833 static int map_check_equal(__isl_keep isl_map *map, const char *str)
2835 int equal;
2837 equal = map_is_equal(map, str);
2838 if (equal < 0)
2839 return -1;
2840 if (!equal)
2841 isl_die(isl_map_get_ctx(map), isl_error_unknown,
2842 "result not as expected", return -1);
2843 return 0;
2846 static int test_dep(struct isl_ctx *ctx)
2848 const char *str;
2849 isl_space *dim;
2850 isl_map *map;
2851 isl_access_info *ai;
2852 isl_flow *flow;
2853 int depth;
2854 struct must_may mm;
2856 depth = 3;
2858 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2859 map = isl_map_read_from_str(ctx, str);
2860 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2862 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2863 map = isl_map_read_from_str(ctx, str);
2864 ai = isl_access_info_add_source(ai, map, 1, &depth);
2866 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2867 map = isl_map_read_from_str(ctx, str);
2868 ai = isl_access_info_add_source(ai, map, 1, &depth);
2870 flow = isl_access_info_compute_flow(ai);
2871 dim = isl_space_alloc(ctx, 0, 3, 3);
2872 mm.must = isl_map_empty(isl_space_copy(dim));
2873 mm.may = isl_map_empty(dim);
2875 isl_flow_foreach(flow, collect_must_may, &mm);
2877 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
2878 " [1,10,0] -> [2,5,0] }";
2879 assert(map_is_equal(mm.must, str));
2880 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2881 assert(map_is_equal(mm.may, str));
2883 isl_map_free(mm.must);
2884 isl_map_free(mm.may);
2885 isl_flow_free(flow);
2888 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2889 map = isl_map_read_from_str(ctx, str);
2890 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2892 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2893 map = isl_map_read_from_str(ctx, str);
2894 ai = isl_access_info_add_source(ai, map, 1, &depth);
2896 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2897 map = isl_map_read_from_str(ctx, str);
2898 ai = isl_access_info_add_source(ai, map, 0, &depth);
2900 flow = isl_access_info_compute_flow(ai);
2901 dim = isl_space_alloc(ctx, 0, 3, 3);
2902 mm.must = isl_map_empty(isl_space_copy(dim));
2903 mm.may = isl_map_empty(dim);
2905 isl_flow_foreach(flow, collect_must_may, &mm);
2907 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
2908 assert(map_is_equal(mm.must, str));
2909 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2910 assert(map_is_equal(mm.may, str));
2912 isl_map_free(mm.must);
2913 isl_map_free(mm.may);
2914 isl_flow_free(flow);
2917 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
2918 map = isl_map_read_from_str(ctx, str);
2919 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2921 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2922 map = isl_map_read_from_str(ctx, str);
2923 ai = isl_access_info_add_source(ai, map, 0, &depth);
2925 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
2926 map = isl_map_read_from_str(ctx, str);
2927 ai = isl_access_info_add_source(ai, map, 0, &depth);
2929 flow = isl_access_info_compute_flow(ai);
2930 dim = isl_space_alloc(ctx, 0, 3, 3);
2931 mm.must = isl_map_empty(isl_space_copy(dim));
2932 mm.may = isl_map_empty(dim);
2934 isl_flow_foreach(flow, collect_must_may, &mm);
2936 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2937 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2938 assert(map_is_equal(mm.may, str));
2939 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2940 assert(map_is_equal(mm.must, str));
2942 isl_map_free(mm.must);
2943 isl_map_free(mm.may);
2944 isl_flow_free(flow);
2947 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2948 map = isl_map_read_from_str(ctx, str);
2949 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2951 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2952 map = isl_map_read_from_str(ctx, str);
2953 ai = isl_access_info_add_source(ai, map, 0, &depth);
2955 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2956 map = isl_map_read_from_str(ctx, str);
2957 ai = isl_access_info_add_source(ai, map, 0, &depth);
2959 flow = isl_access_info_compute_flow(ai);
2960 dim = isl_space_alloc(ctx, 0, 3, 3);
2961 mm.must = isl_map_empty(isl_space_copy(dim));
2962 mm.may = isl_map_empty(dim);
2964 isl_flow_foreach(flow, collect_must_may, &mm);
2966 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2967 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2968 assert(map_is_equal(mm.may, str));
2969 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2970 assert(map_is_equal(mm.must, str));
2972 isl_map_free(mm.must);
2973 isl_map_free(mm.may);
2974 isl_flow_free(flow);
2977 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2978 map = isl_map_read_from_str(ctx, str);
2979 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2981 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2982 map = isl_map_read_from_str(ctx, str);
2983 ai = isl_access_info_add_source(ai, map, 0, &depth);
2985 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2986 map = isl_map_read_from_str(ctx, str);
2987 ai = isl_access_info_add_source(ai, map, 0, &depth);
2989 flow = isl_access_info_compute_flow(ai);
2990 dim = isl_space_alloc(ctx, 0, 3, 3);
2991 mm.must = isl_map_empty(isl_space_copy(dim));
2992 mm.may = isl_map_empty(dim);
2994 isl_flow_foreach(flow, collect_must_may, &mm);
2996 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2997 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2998 assert(map_is_equal(mm.may, str));
2999 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3000 assert(map_is_equal(mm.must, str));
3002 isl_map_free(mm.must);
3003 isl_map_free(mm.may);
3004 isl_flow_free(flow);
3007 depth = 5;
3009 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3010 map = isl_map_read_from_str(ctx, str);
3011 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
3013 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3014 map = isl_map_read_from_str(ctx, str);
3015 ai = isl_access_info_add_source(ai, map, 1, &depth);
3017 flow = isl_access_info_compute_flow(ai);
3018 dim = isl_space_alloc(ctx, 0, 5, 5);
3019 mm.must = isl_map_empty(isl_space_copy(dim));
3020 mm.may = isl_map_empty(dim);
3022 isl_flow_foreach(flow, collect_must_may, &mm);
3024 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
3025 assert(map_is_equal(mm.must, str));
3026 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
3027 assert(map_is_equal(mm.may, str));
3029 isl_map_free(mm.must);
3030 isl_map_free(mm.may);
3031 isl_flow_free(flow);
3033 return 0;
3036 /* Check that the dependence analysis proceeds without errors.
3037 * Earlier versions of isl would break down during the analysis
3038 * due to the use of the wrong spaces.
3040 static int test_flow(isl_ctx *ctx)
3042 const char *str;
3043 isl_union_map *access, *schedule;
3044 isl_union_map *must_dep, *may_dep;
3045 int r;
3047 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
3048 access = isl_union_map_read_from_str(ctx, str);
3049 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
3050 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
3051 "S2[] -> [1,0,0,0]; "
3052 "S3[] -> [-1,0,0,0] }";
3053 schedule = isl_union_map_read_from_str(ctx, str);
3054 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
3055 isl_union_map_copy(access), schedule,
3056 &must_dep, &may_dep, NULL, NULL);
3057 isl_union_map_free(may_dep);
3058 isl_union_map_free(must_dep);
3060 return r;
3063 struct {
3064 const char *map;
3065 int sv;
3066 } sv_tests[] = {
3067 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
3068 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
3069 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
3070 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
3071 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
3072 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
3073 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3074 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3075 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
3078 int test_sv(isl_ctx *ctx)
3080 isl_union_map *umap;
3081 int i;
3082 int sv;
3084 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
3085 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
3086 sv = isl_union_map_is_single_valued(umap);
3087 isl_union_map_free(umap);
3088 if (sv < 0)
3089 return -1;
3090 if (sv_tests[i].sv && !sv)
3091 isl_die(ctx, isl_error_internal,
3092 "map not detected as single valued", return -1);
3093 if (!sv_tests[i].sv && sv)
3094 isl_die(ctx, isl_error_internal,
3095 "map detected as single valued", return -1);
3098 return 0;
3101 struct {
3102 const char *str;
3103 int bijective;
3104 } bijective_tests[] = {
3105 { "[N,M]->{[i,j] -> [i]}", 0 },
3106 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
3107 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
3108 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
3109 { "[N,M]->{[i,j] -> [j,i]}", 1 },
3110 { "[N,M]->{[i,j] -> [i+j]}", 0 },
3111 { "[N,M]->{[i,j] -> []}", 0 },
3112 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
3113 { "[N,M]->{[i,j] -> [2i]}", 0 },
3114 { "[N,M]->{[i,j] -> [i,i]}", 0 },
3115 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
3116 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
3117 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
3120 static int test_bijective(struct isl_ctx *ctx)
3122 isl_map *map;
3123 int i;
3124 int bijective;
3126 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
3127 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
3128 bijective = isl_map_is_bijective(map);
3129 isl_map_free(map);
3130 if (bijective < 0)
3131 return -1;
3132 if (bijective_tests[i].bijective && !bijective)
3133 isl_die(ctx, isl_error_internal,
3134 "map not detected as bijective", return -1);
3135 if (!bijective_tests[i].bijective && bijective)
3136 isl_die(ctx, isl_error_internal,
3137 "map detected as bijective", return -1);
3140 return 0;
3143 /* Inputs for isl_pw_qpolynomial_gist tests.
3144 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
3146 struct {
3147 const char *pwqp;
3148 const char *set;
3149 const char *gist;
3150 } pwqp_gist_tests[] = {
3151 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
3152 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
3153 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
3154 "{ [i] -> -1/2 + 1/2 * i }" },
3155 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
3158 static int test_pwqp(struct isl_ctx *ctx)
3160 int i;
3161 const char *str;
3162 isl_set *set;
3163 isl_pw_qpolynomial *pwqp1, *pwqp2;
3164 int equal;
3166 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3167 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3169 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
3170 isl_dim_in, 1, 1);
3172 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3173 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3175 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3177 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3179 isl_pw_qpolynomial_free(pwqp1);
3181 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
3182 str = pwqp_gist_tests[i].pwqp;
3183 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3184 str = pwqp_gist_tests[i].set;
3185 set = isl_set_read_from_str(ctx, str);
3186 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
3187 str = pwqp_gist_tests[i].gist;
3188 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3189 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3190 equal = isl_pw_qpolynomial_is_zero(pwqp1);
3191 isl_pw_qpolynomial_free(pwqp1);
3193 if (equal < 0)
3194 return -1;
3195 if (!equal)
3196 isl_die(ctx, isl_error_unknown,
3197 "unexpected result", return -1);
3200 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
3201 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3202 str = "{ [i] -> ([(2 * [i/2])/5]) }";
3203 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3205 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3207 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3209 isl_pw_qpolynomial_free(pwqp1);
3211 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
3212 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3213 str = "{ [x] -> x }";
3214 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3216 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3218 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3220 isl_pw_qpolynomial_free(pwqp1);
3222 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3223 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3224 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3225 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3226 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3227 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3228 isl_pw_qpolynomial_free(pwqp1);
3230 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3231 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3232 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3233 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3234 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3235 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3236 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3237 isl_pw_qpolynomial_free(pwqp1);
3238 isl_pw_qpolynomial_free(pwqp2);
3239 if (equal < 0)
3240 return -1;
3241 if (!equal)
3242 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3244 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3245 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3246 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3247 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3248 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3249 isl_val_one(ctx));
3250 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3251 isl_pw_qpolynomial_free(pwqp1);
3252 isl_pw_qpolynomial_free(pwqp2);
3253 if (equal < 0)
3254 return -1;
3255 if (!equal)
3256 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3258 return 0;
3261 static int test_split_periods(isl_ctx *ctx)
3263 const char *str;
3264 isl_pw_qpolynomial *pwqp;
3266 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3267 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3268 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3269 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3271 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3273 isl_pw_qpolynomial_free(pwqp);
3275 if (!pwqp)
3276 return -1;
3278 return 0;
3281 static int test_union(isl_ctx *ctx)
3283 const char *str;
3284 isl_union_set *uset1, *uset2;
3285 isl_union_map *umap1, *umap2;
3286 int equal;
3288 str = "{ [i] : 0 <= i <= 1 }";
3289 uset1 = isl_union_set_read_from_str(ctx, str);
3290 str = "{ [1] -> [0] }";
3291 umap1 = isl_union_map_read_from_str(ctx, str);
3293 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3294 equal = isl_union_map_is_equal(umap1, umap2);
3296 isl_union_map_free(umap1);
3297 isl_union_map_free(umap2);
3299 if (equal < 0)
3300 return -1;
3301 if (!equal)
3302 isl_die(ctx, isl_error_unknown, "union maps not equal",
3303 return -1);
3305 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3306 umap1 = isl_union_map_read_from_str(ctx, str);
3307 str = "{ A[i]; B[i] }";
3308 uset1 = isl_union_set_read_from_str(ctx, str);
3310 uset2 = isl_union_map_domain(umap1);
3312 equal = isl_union_set_is_equal(uset1, uset2);
3314 isl_union_set_free(uset1);
3315 isl_union_set_free(uset2);
3317 if (equal < 0)
3318 return -1;
3319 if (!equal)
3320 isl_die(ctx, isl_error_unknown, "union sets not equal",
3321 return -1);
3323 return 0;
3326 /* Check that computing a bound of a non-zero polynomial over an unbounded
3327 * domain does not produce a rational value.
3328 * In particular, check that the upper bound is infinity.
3330 static int test_bound_unbounded_domain(isl_ctx *ctx)
3332 const char *str;
3333 isl_pw_qpolynomial *pwqp;
3334 isl_pw_qpolynomial_fold *pwf, *pwf2;
3335 isl_bool equal;
3337 str = "{ [m,n] -> -m * n }";
3338 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3339 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3340 str = "{ infty }";
3341 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3342 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3343 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
3344 isl_pw_qpolynomial_fold_free(pwf);
3345 isl_pw_qpolynomial_fold_free(pwf2);
3347 if (equal < 0)
3348 return -1;
3349 if (!equal)
3350 isl_die(ctx, isl_error_unknown,
3351 "expecting infinite polynomial bound", return -1);
3353 return 0;
3356 static int test_bound(isl_ctx *ctx)
3358 const char *str;
3359 unsigned dim;
3360 isl_pw_qpolynomial *pwqp;
3361 isl_pw_qpolynomial_fold *pwf;
3363 if (test_bound_unbounded_domain(ctx) < 0)
3364 return -1;
3366 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
3367 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3368 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3369 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3370 isl_pw_qpolynomial_fold_free(pwf);
3371 if (dim != 4)
3372 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3373 return -1);
3375 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
3376 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3377 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3378 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3379 isl_pw_qpolynomial_fold_free(pwf);
3380 if (dim != 1)
3381 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3382 return -1);
3384 return 0;
3387 /* Check that the conversion from 'set' to 'basic set list' works as expected.
3389 static isl_stat test_get_list_bset_from_set(isl_ctx *ctx)
3391 int i;
3392 isl_bool equal;
3393 isl_set *set, *set2;
3394 isl_basic_set_list *bset_list;
3396 set = isl_set_read_from_str(ctx, "{ [0]; [2]; [3] }");
3397 bset_list = isl_set_get_basic_set_list(set);
3399 set2 = isl_set_empty(isl_set_get_space(set));
3401 for (i = 0; i < isl_basic_set_list_n_basic_set(bset_list); i++) {
3402 isl_basic_set *bset;
3403 bset = isl_basic_set_list_get_basic_set(bset_list, i);
3404 set2 = isl_set_union(set2, isl_set_from_basic_set(bset));
3407 equal = isl_set_is_equal(set, set2);
3409 isl_set_free(set);
3410 isl_set_free(set2);
3411 isl_basic_set_list_free(bset_list);
3413 if (equal < 0)
3414 return isl_stat_error;
3416 if (!equal)
3417 isl_die(ctx, isl_error_unknown, "sets are not equal",
3418 return isl_stat_error);
3420 return isl_stat_ok;
3423 /* Check that the conversion from 'union set' to 'basic set list' works as
3424 * expected.
3426 static isl_stat test_get_list_bset_from_uset(isl_ctx *ctx)
3428 int i;
3429 isl_bool equal;
3430 isl_union_set *uset, *uset2;
3431 isl_basic_set_list *bset_list;
3433 uset = isl_union_set_read_from_str(ctx, "{ A[0]; B[2]; B[3] }");
3434 bset_list = isl_union_set_get_basic_set_list(uset);
3436 uset2 = isl_union_set_empty(isl_union_set_get_space(uset));
3438 for (i = 0; i < isl_basic_set_list_n_basic_set(bset_list); i++) {
3439 isl_basic_set *bset;
3440 bset = isl_basic_set_list_get_basic_set(bset_list, i);
3441 uset2 = isl_union_set_union(uset2,
3442 isl_union_set_from_basic_set(bset));
3445 equal = isl_union_set_is_equal(uset, uset2);
3447 isl_union_set_free(uset);
3448 isl_union_set_free(uset2);
3449 isl_basic_set_list_free(bset_list);
3451 if (equal < 0)
3452 return isl_stat_error;
3454 if (!equal)
3455 isl_die(ctx, isl_error_unknown, "sets are not equal",
3456 return isl_stat_error);
3458 return isl_stat_ok;
3461 /* Check that the conversion from 'union set' to 'set list' works as expected.
3463 static isl_stat test_get_list_set_from_uset(isl_ctx *ctx)
3465 int i;
3466 isl_bool equal;
3467 isl_union_set *uset, *uset2;
3468 isl_set_list *set_list;
3470 uset = isl_union_set_read_from_str(ctx, "{ A[0]; A[2]; B[3] }");
3471 set_list = isl_union_set_get_set_list(uset);
3473 uset2 = isl_union_set_empty(isl_union_set_get_space(uset));
3475 for (i = 0; i < isl_set_list_n_set(set_list); i++) {
3476 isl_set *set;
3477 set = isl_set_list_get_set(set_list, i);
3478 uset2 = isl_union_set_union(uset2, isl_union_set_from_set(set));
3481 equal = isl_union_set_is_equal(uset, uset2);
3483 isl_union_set_free(uset);
3484 isl_union_set_free(uset2);
3485 isl_set_list_free(set_list);
3487 if (equal < 0)
3488 return isl_stat_error;
3490 if (!equal)
3491 isl_die(ctx, isl_error_unknown, "union sets are not equal",
3492 return isl_stat_error);
3494 return isl_stat_ok;
3497 /* Check that the conversion from 'map' to 'basic map list' works as expected.
3499 static isl_stat test_get_list_bmap_from_map(isl_ctx *ctx)
3501 int i;
3502 isl_bool equal;
3503 isl_map *map, *map2;
3504 isl_basic_map_list *bmap_list;
3506 map = isl_map_read_from_str(ctx,
3507 "{ [0] -> [0]; [2] -> [0]; [3] -> [0] }");
3508 bmap_list = isl_map_get_basic_map_list(map);
3510 map2 = isl_map_empty(isl_map_get_space(map));
3512 for (i = 0; i < isl_basic_map_list_n_basic_map(bmap_list); i++) {
3513 isl_basic_map *bmap;
3514 bmap = isl_basic_map_list_get_basic_map(bmap_list, i);
3515 map2 = isl_map_union(map2, isl_map_from_basic_map(bmap));
3518 equal = isl_map_is_equal(map, map2);
3520 isl_map_free(map);
3521 isl_map_free(map2);
3522 isl_basic_map_list_free(bmap_list);
3524 if (equal < 0)
3525 return isl_stat_error;
3527 if (!equal)
3528 isl_die(ctx, isl_error_unknown, "maps are not equal",
3529 return isl_stat_error);
3531 return isl_stat_ok;
3534 /* Check that the conversion from 'union map' to 'map list' works as expected.
3536 static isl_stat test_get_list_map_from_umap(isl_ctx *ctx)
3538 int i;
3539 isl_bool equal;
3540 isl_union_map *umap, *umap2;
3541 isl_map_list *map_list;
3543 umap = isl_union_map_read_from_str(ctx,
3544 "{ A[0] -> [0]; A[2] -> [0]; B[3] -> [0] }");
3545 map_list = isl_union_map_get_map_list(umap);
3547 umap2 = isl_union_map_empty(isl_union_map_get_space(umap));
3549 for (i = 0; i < isl_map_list_n_map(map_list); i++) {
3550 isl_map *map;
3551 map = isl_map_list_get_map(map_list, i);
3552 umap2 = isl_union_map_union(umap2, isl_union_map_from_map(map));
3555 equal = isl_union_map_is_equal(umap, umap2);
3557 isl_union_map_free(umap);
3558 isl_union_map_free(umap2);
3559 isl_map_list_free(map_list);
3561 if (equal < 0)
3562 return isl_stat_error;
3564 if (!equal)
3565 isl_die(ctx, isl_error_unknown, "union maps are not equal",
3566 return isl_stat_error);
3568 return isl_stat_ok;
3571 /* Check that the conversion from isl objects to lists works as expected.
3573 static int test_get_list(isl_ctx *ctx)
3575 if (test_get_list_bset_from_set(ctx))
3576 return -1;
3577 if (test_get_list_bset_from_uset(ctx))
3578 return -1;
3579 if (test_get_list_set_from_uset(ctx))
3580 return -1;
3581 if (test_get_list_bmap_from_map(ctx))
3582 return -1;
3583 if (test_get_list_map_from_umap(ctx))
3584 return -1;
3586 return 0;
3589 static int test_lift(isl_ctx *ctx)
3591 const char *str;
3592 isl_basic_map *bmap;
3593 isl_basic_set *bset;
3595 str = "{ [i0] : exists e0 : i0 = 4e0 }";
3596 bset = isl_basic_set_read_from_str(ctx, str);
3597 bset = isl_basic_set_lift(bset);
3598 bmap = isl_basic_map_from_range(bset);
3599 bset = isl_basic_map_domain(bmap);
3600 isl_basic_set_free(bset);
3602 return 0;
3605 /* Check that isl_set_is_subset is not confused by identical
3606 * integer divisions.
3607 * The call to isl_set_normalize ensures that the equality constraints
3608 * a = b = 0 are discovered, turning e0 and e1 into identical
3609 * integer divisions. Any further simplification would remove
3610 * the duplicate integer divisions.
3612 static isl_stat test_subset_duplicate_integer_divisions(isl_ctx *ctx)
3614 const char *str;
3615 isl_bool is_subset;
3616 isl_set *set1, *set2;
3618 str = "{ [a, b, c, d] : "
3619 "exists (e0 = floor((a + d)/4), e1 = floor((d)/4), "
3620 "e2 = floor((-a - d + 4 *floor((a + d)/4))/10), "
3621 "e3 = floor((-d + 4*floor((d)/4))/10): "
3622 "10e2 = -a - 2c - d + 4e0 and 10e3 = -2c - d + 4e1 and "
3623 "b >= 0 and a <= 0 and b <= a) }";
3624 set1 = isl_set_read_from_str(ctx, str);
3625 set2 = isl_set_read_from_str(ctx, str);
3626 set2 = isl_set_normalize(set2);
3628 is_subset = isl_set_is_subset(set1, set2);
3630 isl_set_free(set1);
3631 isl_set_free(set2);
3633 if (is_subset < 0)
3634 return isl_stat_error;
3635 if (!is_subset)
3636 isl_die(ctx, isl_error_unknown,
3637 "set is not considered to be a subset of itself",
3638 return isl_stat_error);
3640 return isl_stat_ok;
3643 struct {
3644 const char *set1;
3645 const char *set2;
3646 int subset;
3647 } subset_tests[] = {
3648 { "{ [112, 0] }",
3649 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
3650 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
3651 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
3652 { "{ [65] }",
3653 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
3654 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
3655 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
3656 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
3657 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
3658 "256e0 <= 255i and 256e0 >= -255 + 255i and "
3659 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
3660 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
3661 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
3662 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
3663 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
3664 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
3665 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
3666 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
3667 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
3668 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
3669 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
3670 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
3671 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
3672 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
3673 "4e0 <= -57 + i0 + i1)) or "
3674 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
3675 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
3676 "4e0 >= -61 + i0 + i1)) or "
3677 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
3678 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
3681 static int test_subset(isl_ctx *ctx)
3683 int i;
3684 isl_set *set1, *set2;
3685 int subset;
3687 if (test_subset_duplicate_integer_divisions(ctx) < 0)
3688 return -1;
3690 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3691 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3692 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3693 subset = isl_set_is_subset(set1, set2);
3694 isl_set_free(set1);
3695 isl_set_free(set2);
3696 if (subset < 0)
3697 return -1;
3698 if (subset != subset_tests[i].subset)
3699 isl_die(ctx, isl_error_unknown,
3700 "incorrect subset result", return -1);
3703 return 0;
3706 struct {
3707 const char *minuend;
3708 const char *subtrahend;
3709 const char *difference;
3710 } subtract_domain_tests[] = {
3711 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3712 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3713 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3716 static int test_subtract(isl_ctx *ctx)
3718 int i;
3719 isl_union_map *umap1, *umap2;
3720 isl_union_pw_multi_aff *upma1, *upma2;
3721 isl_union_set *uset;
3722 int equal;
3724 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3725 umap1 = isl_union_map_read_from_str(ctx,
3726 subtract_domain_tests[i].minuend);
3727 uset = isl_union_set_read_from_str(ctx,
3728 subtract_domain_tests[i].subtrahend);
3729 umap2 = isl_union_map_read_from_str(ctx,
3730 subtract_domain_tests[i].difference);
3731 umap1 = isl_union_map_subtract_domain(umap1, uset);
3732 equal = isl_union_map_is_equal(umap1, umap2);
3733 isl_union_map_free(umap1);
3734 isl_union_map_free(umap2);
3735 if (equal < 0)
3736 return -1;
3737 if (!equal)
3738 isl_die(ctx, isl_error_unknown,
3739 "incorrect subtract domain result", return -1);
3742 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3743 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3744 subtract_domain_tests[i].minuend);
3745 uset = isl_union_set_read_from_str(ctx,
3746 subtract_domain_tests[i].subtrahend);
3747 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3748 subtract_domain_tests[i].difference);
3749 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3750 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3751 isl_union_pw_multi_aff_free(upma1);
3752 isl_union_pw_multi_aff_free(upma2);
3753 if (equal < 0)
3754 return -1;
3755 if (!equal)
3756 isl_die(ctx, isl_error_unknown,
3757 "incorrect subtract domain result", return -1);
3760 return 0;
3763 /* Check that intersecting the empty basic set with another basic set
3764 * does not increase the number of constraints. In particular,
3765 * the empty basic set should maintain its canonical representation.
3767 static int test_intersect_1(isl_ctx *ctx)
3769 int n1, n2;
3770 isl_basic_set *bset1, *bset2;
3772 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3773 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3774 n1 = isl_basic_set_n_constraint(bset1);
3775 bset1 = isl_basic_set_intersect(bset1, bset2);
3776 n2 = isl_basic_set_n_constraint(bset1);
3777 isl_basic_set_free(bset1);
3778 if (!bset1)
3779 return -1;
3780 if (n1 != n2)
3781 isl_die(ctx, isl_error_unknown,
3782 "number of constraints of empty set changed",
3783 return -1);
3785 return 0;
3788 /* Check that intersecting a set with itself does not cause
3789 * an explosion in the number of disjuncts.
3791 static isl_stat test_intersect_2(isl_ctx *ctx)
3793 int i;
3794 isl_set *set;
3796 set = isl_set_read_from_str(ctx, "{ [x,y] : x >= 0 or y >= 0 }");
3797 for (i = 0; i < 100; ++i)
3798 set = isl_set_intersect(set, isl_set_copy(set));
3799 isl_set_free(set);
3800 if (!set)
3801 return isl_stat_error;
3802 return isl_stat_ok;
3805 /* Perform some intersection tests.
3807 static int test_intersect(isl_ctx *ctx)
3809 if (test_intersect_1(ctx) < 0)
3810 return -1;
3811 if (test_intersect_2(ctx) < 0)
3812 return -1;
3814 return 0;
3817 int test_factorize(isl_ctx *ctx)
3819 const char *str;
3820 isl_basic_set *bset;
3821 isl_factorizer *f;
3823 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3824 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3825 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3826 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3827 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3828 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3829 "3i5 >= -2i0 - i2 + 3i4 }";
3830 bset = isl_basic_set_read_from_str(ctx, str);
3831 f = isl_basic_set_factorizer(bset);
3832 isl_basic_set_free(bset);
3833 isl_factorizer_free(f);
3834 if (!f)
3835 isl_die(ctx, isl_error_unknown,
3836 "failed to construct factorizer", return -1);
3838 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3839 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3840 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3841 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3842 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3843 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3844 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3845 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3846 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3847 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3848 bset = isl_basic_set_read_from_str(ctx, str);
3849 f = isl_basic_set_factorizer(bset);
3850 isl_basic_set_free(bset);
3851 isl_factorizer_free(f);
3852 if (!f)
3853 isl_die(ctx, isl_error_unknown,
3854 "failed to construct factorizer", return -1);
3856 return 0;
3859 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3861 int *injective = user;
3863 *injective = isl_map_is_injective(map);
3864 isl_map_free(map);
3866 if (*injective < 0 || !*injective)
3867 return isl_stat_error;
3869 return isl_stat_ok;
3872 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3873 const char *r, const char *s, int tilable, int parallel)
3875 int i;
3876 isl_union_set *D;
3877 isl_union_map *W, *R, *S;
3878 isl_union_map *empty;
3879 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3880 isl_union_map *validity, *proximity, *coincidence;
3881 isl_union_map *schedule;
3882 isl_union_map *test;
3883 isl_union_set *delta;
3884 isl_union_set *domain;
3885 isl_set *delta_set;
3886 isl_set *slice;
3887 isl_set *origin;
3888 isl_schedule_constraints *sc;
3889 isl_schedule *sched;
3890 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3892 D = isl_union_set_read_from_str(ctx, d);
3893 W = isl_union_map_read_from_str(ctx, w);
3894 R = isl_union_map_read_from_str(ctx, r);
3895 S = isl_union_map_read_from_str(ctx, s);
3897 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3898 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3900 empty = isl_union_map_empty(isl_union_map_get_space(S));
3901 isl_union_map_compute_flow(isl_union_map_copy(R),
3902 isl_union_map_copy(W), empty,
3903 isl_union_map_copy(S),
3904 &dep_raw, NULL, NULL, NULL);
3905 isl_union_map_compute_flow(isl_union_map_copy(W),
3906 isl_union_map_copy(W),
3907 isl_union_map_copy(R),
3908 isl_union_map_copy(S),
3909 &dep_waw, &dep_war, NULL, NULL);
3911 dep = isl_union_map_union(dep_waw, dep_war);
3912 dep = isl_union_map_union(dep, dep_raw);
3913 validity = isl_union_map_copy(dep);
3914 coincidence = isl_union_map_copy(dep);
3915 proximity = isl_union_map_copy(dep);
3917 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3918 sc = isl_schedule_constraints_set_validity(sc, validity);
3919 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3920 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3921 sched = isl_schedule_constraints_compute_schedule(sc);
3922 schedule = isl_schedule_get_map(sched);
3923 isl_schedule_free(sched);
3924 isl_union_map_free(W);
3925 isl_union_map_free(R);
3926 isl_union_map_free(S);
3928 is_injection = 1;
3929 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3931 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3932 is_complete = isl_union_set_is_subset(D, domain);
3933 isl_union_set_free(D);
3934 isl_union_set_free(domain);
3936 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3937 test = isl_union_map_apply_range(test, dep);
3938 test = isl_union_map_apply_range(test, schedule);
3940 delta = isl_union_map_deltas(test);
3941 if (isl_union_set_n_set(delta) == 0) {
3942 is_tilable = 1;
3943 is_parallel = 1;
3944 is_nonneg = 1;
3945 isl_union_set_free(delta);
3946 } else {
3947 delta_set = isl_set_from_union_set(delta);
3949 slice = isl_set_universe(isl_set_get_space(delta_set));
3950 for (i = 0; i < tilable; ++i)
3951 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3952 is_tilable = isl_set_is_subset(delta_set, slice);
3953 isl_set_free(slice);
3955 slice = isl_set_universe(isl_set_get_space(delta_set));
3956 for (i = 0; i < parallel; ++i)
3957 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3958 is_parallel = isl_set_is_subset(delta_set, slice);
3959 isl_set_free(slice);
3961 origin = isl_set_universe(isl_set_get_space(delta_set));
3962 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
3963 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
3965 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
3966 delta_set = isl_set_lexmin(delta_set);
3968 is_nonneg = isl_set_is_equal(delta_set, origin);
3970 isl_set_free(origin);
3971 isl_set_free(delta_set);
3974 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
3975 is_injection < 0 || is_complete < 0)
3976 return -1;
3977 if (!is_complete)
3978 isl_die(ctx, isl_error_unknown,
3979 "generated schedule incomplete", return -1);
3980 if (!is_injection)
3981 isl_die(ctx, isl_error_unknown,
3982 "generated schedule not injective on each statement",
3983 return -1);
3984 if (!is_nonneg)
3985 isl_die(ctx, isl_error_unknown,
3986 "negative dependences in generated schedule",
3987 return -1);
3988 if (!is_tilable)
3989 isl_die(ctx, isl_error_unknown,
3990 "generated schedule not as tilable as expected",
3991 return -1);
3992 if (!is_parallel)
3993 isl_die(ctx, isl_error_unknown,
3994 "generated schedule not as parallel as expected",
3995 return -1);
3997 return 0;
4000 /* Compute a schedule for the given instance set, validity constraints,
4001 * proximity constraints and context and return a corresponding union map
4002 * representation.
4004 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
4005 const char *domain, const char *validity, const char *proximity,
4006 const char *context)
4008 isl_set *con;
4009 isl_union_set *dom;
4010 isl_union_map *dep;
4011 isl_union_map *prox;
4012 isl_schedule_constraints *sc;
4013 isl_schedule *schedule;
4014 isl_union_map *sched;
4016 con = isl_set_read_from_str(ctx, context);
4017 dom = isl_union_set_read_from_str(ctx, domain);
4018 dep = isl_union_map_read_from_str(ctx, validity);
4019 prox = isl_union_map_read_from_str(ctx, proximity);
4020 sc = isl_schedule_constraints_on_domain(dom);
4021 sc = isl_schedule_constraints_set_context(sc, con);
4022 sc = isl_schedule_constraints_set_validity(sc, dep);
4023 sc = isl_schedule_constraints_set_proximity(sc, prox);
4024 schedule = isl_schedule_constraints_compute_schedule(sc);
4025 sched = isl_schedule_get_map(schedule);
4026 isl_schedule_free(schedule);
4028 return sched;
4031 /* Compute a schedule for the given instance set, validity constraints and
4032 * proximity constraints and return a corresponding union map representation.
4034 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
4035 const char *domain, const char *validity, const char *proximity)
4037 return compute_schedule_with_context(ctx, domain, validity, proximity,
4038 "{ : }");
4041 /* Check that a schedule can be constructed on the given domain
4042 * with the given validity and proximity constraints.
4044 static int test_has_schedule(isl_ctx *ctx, const char *domain,
4045 const char *validity, const char *proximity)
4047 isl_union_map *sched;
4049 sched = compute_schedule(ctx, domain, validity, proximity);
4050 if (!sched)
4051 return -1;
4053 isl_union_map_free(sched);
4054 return 0;
4057 int test_special_schedule(isl_ctx *ctx, const char *domain,
4058 const char *validity, const char *proximity, const char *expected_sched)
4060 isl_union_map *sched1, *sched2;
4061 int equal;
4063 sched1 = compute_schedule(ctx, domain, validity, proximity);
4064 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
4066 equal = isl_union_map_is_equal(sched1, sched2);
4067 isl_union_map_free(sched1);
4068 isl_union_map_free(sched2);
4070 if (equal < 0)
4071 return -1;
4072 if (!equal)
4073 isl_die(ctx, isl_error_unknown, "unexpected schedule",
4074 return -1);
4076 return 0;
4079 /* Check that the schedule map is properly padded, i.e., that the range
4080 * lives in a single space.
4082 static int test_padded_schedule(isl_ctx *ctx)
4084 const char *str;
4085 isl_union_set *D;
4086 isl_union_map *validity, *proximity;
4087 isl_schedule_constraints *sc;
4088 isl_schedule *sched;
4089 isl_union_map *umap;
4090 isl_union_set *range;
4091 isl_set *set;
4093 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
4094 D = isl_union_set_read_from_str(ctx, str);
4095 validity = isl_union_map_empty(isl_union_set_get_space(D));
4096 proximity = isl_union_map_copy(validity);
4097 sc = isl_schedule_constraints_on_domain(D);
4098 sc = isl_schedule_constraints_set_validity(sc, validity);
4099 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4100 sched = isl_schedule_constraints_compute_schedule(sc);
4101 umap = isl_schedule_get_map(sched);
4102 isl_schedule_free(sched);
4103 range = isl_union_map_range(umap);
4104 set = isl_set_from_union_set(range);
4105 isl_set_free(set);
4107 if (!set)
4108 return -1;
4110 return 0;
4113 /* Check that conditional validity constraints are also taken into
4114 * account across bands.
4115 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
4116 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
4117 * and then check that the adjacent order constraint C[2,1]->D[2,0]
4118 * is enforced by the rest of the schedule.
4120 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
4122 const char *str;
4123 isl_union_set *domain;
4124 isl_union_map *validity, *proximity, *condition;
4125 isl_union_map *sink, *source, *dep;
4126 isl_schedule_constraints *sc;
4127 isl_schedule *schedule;
4128 isl_union_access_info *access;
4129 isl_union_flow *flow;
4130 int empty;
4132 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4133 "A[k] : k >= 1 and k <= -1 + n; "
4134 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4135 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
4136 domain = isl_union_set_read_from_str(ctx, str);
4137 sc = isl_schedule_constraints_on_domain(domain);
4138 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
4139 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4140 "D[k, i] -> C[1 + k, i] : "
4141 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4142 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
4143 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
4144 validity = isl_union_map_read_from_str(ctx, str);
4145 sc = isl_schedule_constraints_set_validity(sc, validity);
4146 str = "[n] -> { C[k, i] -> D[k, i] : "
4147 "0 <= i <= -1 + k and k <= -1 + n }";
4148 proximity = isl_union_map_read_from_str(ctx, str);
4149 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4150 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
4151 "i <= -1 + k and i >= 1 and k <= -2 + n; "
4152 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
4153 "k <= -1 + n and i >= 0 and i <= -2 + k }";
4154 condition = isl_union_map_read_from_str(ctx, str);
4155 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
4156 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4157 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
4158 "i >= 0 and i <= -1 + k and k <= -1 + n and "
4159 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
4160 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
4161 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4162 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
4163 "k <= -1 + n and i >= 0 and i <= -1 + k and "
4164 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
4165 validity = isl_union_map_read_from_str(ctx, str);
4166 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4167 validity);
4168 schedule = isl_schedule_constraints_compute_schedule(sc);
4169 str = "{ D[2,0] -> [] }";
4170 sink = isl_union_map_read_from_str(ctx, str);
4171 access = isl_union_access_info_from_sink(sink);
4172 str = "{ C[2,1] -> [] }";
4173 source = isl_union_map_read_from_str(ctx, str);
4174 access = isl_union_access_info_set_must_source(access, source);
4175 access = isl_union_access_info_set_schedule(access, schedule);
4176 flow = isl_union_access_info_compute_flow(access);
4177 dep = isl_union_flow_get_must_dependence(flow);
4178 isl_union_flow_free(flow);
4179 empty = isl_union_map_is_empty(dep);
4180 isl_union_map_free(dep);
4182 if (empty < 0)
4183 return -1;
4184 if (empty)
4185 isl_die(ctx, isl_error_unknown,
4186 "conditional validity not respected", return -1);
4188 return 0;
4191 /* Check that the test for violated conditional validity constraints
4192 * is not confused by domain compression.
4193 * In particular, earlier versions of isl would apply
4194 * a schedule on the compressed domains to the original domains,
4195 * resulting in a failure to detect that the default schedule
4196 * violates the conditional validity constraints.
4198 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
4200 const char *str;
4201 isl_bool empty;
4202 isl_union_set *domain;
4203 isl_union_map *validity, *condition;
4204 isl_schedule_constraints *sc;
4205 isl_schedule *schedule;
4206 isl_union_map *umap;
4207 isl_map *map, *ge;
4209 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
4210 domain = isl_union_set_read_from_str(ctx, str);
4211 sc = isl_schedule_constraints_on_domain(domain);
4212 str = "{ B[1, i] -> A[0, i + 1] }";
4213 condition = isl_union_map_read_from_str(ctx, str);
4214 str = "{ A[0, i] -> B[1, i - 1] }";
4215 validity = isl_union_map_read_from_str(ctx, str);
4216 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4217 isl_union_map_copy(validity));
4218 schedule = isl_schedule_constraints_compute_schedule(sc);
4219 umap = isl_schedule_get_map(schedule);
4220 isl_schedule_free(schedule);
4221 validity = isl_union_map_apply_domain(validity,
4222 isl_union_map_copy(umap));
4223 validity = isl_union_map_apply_range(validity, umap);
4224 map = isl_map_from_union_map(validity);
4225 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4226 map = isl_map_intersect(map, ge);
4227 empty = isl_map_is_empty(map);
4228 isl_map_free(map);
4230 if (empty < 0)
4231 return -1;
4232 if (!empty)
4233 isl_die(ctx, isl_error_unknown,
4234 "conditional validity constraints not satisfied",
4235 return -1);
4237 return 0;
4240 /* Input for testing of schedule construction based on
4241 * conditional constraints.
4243 * domain is the iteration domain
4244 * flow are the flow dependences, which determine the validity and
4245 * proximity constraints
4246 * condition are the conditions on the conditional validity constraints
4247 * conditional_validity are the conditional validity constraints
4248 * outer_band_n is the expected number of members in the outer band
4250 struct {
4251 const char *domain;
4252 const char *flow;
4253 const char *condition;
4254 const char *conditional_validity;
4255 int outer_band_n;
4256 } live_range_tests[] = {
4257 /* Contrived example that illustrates that we need to keep
4258 * track of tagged condition dependences and
4259 * tagged conditional validity dependences
4260 * in isl_sched_edge separately.
4261 * In particular, the conditional validity constraints on A
4262 * cannot be satisfied,
4263 * but they can be ignored because there are no corresponding
4264 * condition constraints. However, we do have an additional
4265 * conditional validity constraint that maps to the same
4266 * dependence relation
4267 * as the condition constraint on B. If we did not make a distinction
4268 * between tagged condition and tagged conditional validity
4269 * dependences, then we
4270 * could end up treating this shared dependence as an condition
4271 * constraint on A, forcing a localization of the conditions,
4272 * which is impossible.
4274 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
4275 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
4276 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
4277 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4278 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4279 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
4282 /* TACO 2013 Fig. 7 */
4283 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4284 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4285 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4286 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
4287 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
4288 "0 <= i < n and 0 <= j < n - 1 }",
4289 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
4290 "0 <= i < n and 0 <= j < j' < n;"
4291 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
4292 "0 <= i < i' < n and 0 <= j,j' < n;"
4293 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
4294 "0 <= i,j,j' < n and j < j' }",
4297 /* TACO 2013 Fig. 7, without tags */
4298 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4299 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4300 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4301 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4302 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4303 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
4304 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
4305 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
4308 /* TACO 2013 Fig. 12 */
4309 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
4310 "S3[i,3] : 0 <= i <= 1 }",
4311 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
4312 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
4313 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
4314 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
4315 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4316 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
4317 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4318 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
4319 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
4320 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
4321 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
4326 /* Test schedule construction based on conditional constraints.
4327 * In particular, check the number of members in the outer band node
4328 * as an indication of whether tiling is possible or not.
4330 static int test_conditional_schedule_constraints(isl_ctx *ctx)
4332 int i;
4333 isl_union_set *domain;
4334 isl_union_map *condition;
4335 isl_union_map *flow;
4336 isl_union_map *validity;
4337 isl_schedule_constraints *sc;
4338 isl_schedule *schedule;
4339 isl_schedule_node *node;
4340 int n_member;
4342 if (test_special_conditional_schedule_constraints(ctx) < 0)
4343 return -1;
4344 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
4345 return -1;
4347 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
4348 domain = isl_union_set_read_from_str(ctx,
4349 live_range_tests[i].domain);
4350 flow = isl_union_map_read_from_str(ctx,
4351 live_range_tests[i].flow);
4352 condition = isl_union_map_read_from_str(ctx,
4353 live_range_tests[i].condition);
4354 validity = isl_union_map_read_from_str(ctx,
4355 live_range_tests[i].conditional_validity);
4356 sc = isl_schedule_constraints_on_domain(domain);
4357 sc = isl_schedule_constraints_set_validity(sc,
4358 isl_union_map_copy(flow));
4359 sc = isl_schedule_constraints_set_proximity(sc, flow);
4360 sc = isl_schedule_constraints_set_conditional_validity(sc,
4361 condition, validity);
4362 schedule = isl_schedule_constraints_compute_schedule(sc);
4363 node = isl_schedule_get_root(schedule);
4364 while (node &&
4365 isl_schedule_node_get_type(node) != isl_schedule_node_band)
4366 node = isl_schedule_node_first_child(node);
4367 n_member = isl_schedule_node_band_n_member(node);
4368 isl_schedule_node_free(node);
4369 isl_schedule_free(schedule);
4371 if (!schedule)
4372 return -1;
4373 if (n_member != live_range_tests[i].outer_band_n)
4374 isl_die(ctx, isl_error_unknown,
4375 "unexpected number of members in outer band",
4376 return -1);
4378 return 0;
4381 /* Check that the schedule computed for the given instance set and
4382 * dependence relation strongly satisfies the dependences.
4383 * In particular, check that no instance is scheduled before
4384 * or together with an instance on which it depends.
4385 * Earlier versions of isl would produce a schedule that
4386 * only weakly satisfies the dependences.
4388 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
4390 const char *domain, *dep;
4391 isl_union_map *D, *schedule;
4392 isl_map *map, *ge;
4393 int empty;
4395 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
4396 "A[i0] : 0 <= i0 <= 1 }";
4397 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
4398 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
4399 schedule = compute_schedule(ctx, domain, dep, dep);
4400 D = isl_union_map_read_from_str(ctx, dep);
4401 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
4402 D = isl_union_map_apply_range(D, schedule);
4403 map = isl_map_from_union_map(D);
4404 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4405 map = isl_map_intersect(map, ge);
4406 empty = isl_map_is_empty(map);
4407 isl_map_free(map);
4409 if (empty < 0)
4410 return -1;
4411 if (!empty)
4412 isl_die(ctx, isl_error_unknown,
4413 "dependences not strongly satisfied", return -1);
4415 return 0;
4418 /* Compute a schedule for input where the instance set constraints
4419 * conflict with the context constraints.
4420 * Earlier versions of isl did not properly handle this situation.
4422 static int test_conflicting_context_schedule(isl_ctx *ctx)
4424 isl_union_map *schedule;
4425 const char *domain, *context;
4427 domain = "[n] -> { A[] : n >= 0 }";
4428 context = "[n] -> { : n < 0 }";
4429 schedule = compute_schedule_with_context(ctx,
4430 domain, "{}", "{}", context);
4431 isl_union_map_free(schedule);
4433 if (!schedule)
4434 return -1;
4436 return 0;
4439 /* Check that the dependence carrying step is not confused by
4440 * a bound on the coefficient size.
4441 * In particular, force the scheduler to move to a dependence carrying
4442 * step by demanding outer coincidence and bound the size of
4443 * the coefficients. Earlier versions of isl would take this
4444 * bound into account while carrying dependences, breaking
4445 * fundamental assumptions.
4446 * On the other hand, the dependence carrying step now tries
4447 * to prevent loop coalescing by default, so check that indeed
4448 * no loop coalescing occurs by comparing the computed schedule
4449 * to the expected non-coalescing schedule.
4451 static int test_bounded_coefficients_schedule(isl_ctx *ctx)
4453 const char *domain, *dep;
4454 isl_union_set *I;
4455 isl_union_map *D;
4456 isl_schedule_constraints *sc;
4457 isl_schedule *schedule;
4458 isl_union_map *sched1, *sched2;
4459 isl_bool equal;
4461 domain = "{ C[i0, i1] : 2 <= i0 <= 3999 and 0 <= i1 <= -1 + i0 }";
4462 dep = "{ C[i0, i1] -> C[i0, 1 + i1] : i0 <= 3999 and i1 >= 0 and "
4463 "i1 <= -2 + i0; "
4464 "C[i0, -1 + i0] -> C[1 + i0, 0] : i0 <= 3998 and i0 >= 1 }";
4465 I = isl_union_set_read_from_str(ctx, domain);
4466 D = isl_union_map_read_from_str(ctx, dep);
4467 sc = isl_schedule_constraints_on_domain(I);
4468 sc = isl_schedule_constraints_set_validity(sc, isl_union_map_copy(D));
4469 sc = isl_schedule_constraints_set_coincidence(sc, D);
4470 isl_options_set_schedule_outer_coincidence(ctx, 1);
4471 isl_options_set_schedule_max_coefficient(ctx, 20);
4472 schedule = isl_schedule_constraints_compute_schedule(sc);
4473 isl_options_set_schedule_max_coefficient(ctx, -1);
4474 isl_options_set_schedule_outer_coincidence(ctx, 0);
4475 sched1 = isl_schedule_get_map(schedule);
4476 isl_schedule_free(schedule);
4478 sched2 = isl_union_map_read_from_str(ctx, "{ C[x,y] -> [x,y] }");
4479 equal = isl_union_map_is_equal(sched1, sched2);
4480 isl_union_map_free(sched1);
4481 isl_union_map_free(sched2);
4483 if (equal < 0)
4484 return -1;
4485 if (!equal)
4486 isl_die(ctx, isl_error_unknown,
4487 "unexpected schedule", return -1);
4489 return 0;
4492 /* Check that the bounds on the coefficients are respected.
4493 * This function checks for a particular output schedule,
4494 * but the exact output is not important, only that it does
4495 * not contain any coefficients greater than 4.
4496 * It is, however, easier to check for a particular output.
4497 * This test is only run for the whole component scheduler
4498 * because the incremental scheduler produces a slightly different schedule.
4500 static int test_bounded_coefficients_schedule_whole(isl_ctx *ctx)
4502 const char *domain, *dep, *str;
4503 isl_union_set *I;
4504 isl_union_map *D;
4505 isl_schedule_constraints *sc;
4506 isl_schedule *schedule;
4507 isl_union_map *sched1, *sched2;
4508 isl_bool equal;
4510 domain = "{ S_4[i, j, k] : 0 <= i < j <= 10 and 0 <= k <= 100; "
4511 "S_2[i, j] : 0 <= i < j <= 10; S_6[i, j] : 0 <= i < j <= 10 }";
4512 dep = "{ S_2[0, j] -> S_4[0, j, 0] : 0 < j <= 10; "
4513 "S_4[0, j, 100] -> S_6[0, j] : 0 < j <= 10 }";
4514 I = isl_union_set_read_from_str(ctx, domain);
4515 D = isl_union_map_read_from_str(ctx, dep);
4516 sc = isl_schedule_constraints_on_domain(I);
4517 sc = isl_schedule_constraints_set_validity(sc, D);
4518 isl_options_set_schedule_max_constant_term(ctx, 10);
4519 isl_options_set_schedule_max_coefficient(ctx, 4);
4520 schedule = isl_schedule_constraints_compute_schedule(sc);
4521 isl_options_set_schedule_max_coefficient(ctx, -1);
4522 isl_options_set_schedule_max_constant_term(ctx, -1);
4523 sched1 = isl_schedule_get_map(schedule);
4524 isl_schedule_free(schedule);
4526 str = "{ S_4[i, j, k] -> [i, j, 10 - k]; "
4527 "S_2[i, j] -> [0, i, j]; S_6[i, j] -> [0, 10 + i, j] }";
4528 sched2 = isl_union_map_read_from_str(ctx, str);
4529 equal = isl_union_map_is_equal(sched1, sched2);
4530 isl_union_map_free(sched1);
4531 isl_union_map_free(sched2);
4533 if (equal < 0)
4534 return -1;
4535 if (!equal)
4536 isl_die(ctx, isl_error_unknown,
4537 "unexpected schedule", return -1);
4539 return 0;
4542 /* Check that a set of schedule constraints that only allow for
4543 * a coalescing schedule still produces a schedule even if the user
4544 * request a non-coalescing schedule. Earlier versions of isl
4545 * would not handle this case correctly.
4547 static int test_coalescing_schedule(isl_ctx *ctx)
4549 const char *domain, *dep;
4550 isl_union_set *I;
4551 isl_union_map *D;
4552 isl_schedule_constraints *sc;
4553 isl_schedule *schedule;
4554 int treat_coalescing;
4556 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
4557 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
4558 I = isl_union_set_read_from_str(ctx, domain);
4559 D = isl_union_map_read_from_str(ctx, dep);
4560 sc = isl_schedule_constraints_on_domain(I);
4561 sc = isl_schedule_constraints_set_validity(sc, D);
4562 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4563 isl_options_set_schedule_treat_coalescing(ctx, 1);
4564 schedule = isl_schedule_constraints_compute_schedule(sc);
4565 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4566 isl_schedule_free(schedule);
4567 if (!schedule)
4568 return -1;
4569 return 0;
4572 /* Check that the scheduler does not perform any needless
4573 * compound skewing. Earlier versions of isl would compute
4574 * schedules in terms of transformed schedule coefficients and
4575 * would not accurately keep track of the sum of the original
4576 * schedule coefficients. It could then produce the schedule
4577 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
4578 * for the input below instead of the schedule below.
4580 static int test_skewing_schedule(isl_ctx *ctx)
4582 const char *D, *V, *P, *S;
4584 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
4585 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
4586 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
4587 "-1 <= a-i + b-j + c-k <= 1 }";
4588 P = "{ }";
4589 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
4591 return test_special_schedule(ctx, D, V, P, S);
4594 int test_schedule(isl_ctx *ctx)
4596 const char *D, *W, *R, *V, *P, *S;
4597 int max_coincidence;
4598 int treat_coalescing;
4600 /* Handle resulting schedule with zero bands. */
4601 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
4602 return -1;
4604 /* Jacobi */
4605 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
4606 W = "{ S1[t,i] -> a[t,i] }";
4607 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
4608 "S1[t,i] -> a[t-1,i+1] }";
4609 S = "{ S1[t,i] -> [t,i] }";
4610 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4611 return -1;
4613 /* Fig. 5 of CC2008 */
4614 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
4615 "j <= -1 + N }";
4616 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
4617 "j >= 2 and j <= -1 + N }";
4618 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
4619 "j >= 2 and j <= -1 + N; "
4620 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
4621 "j >= 2 and j <= -1 + N }";
4622 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4623 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4624 return -1;
4626 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
4627 W = "{ S1[i] -> a[i] }";
4628 R = "{ S2[i] -> a[i+1] }";
4629 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4630 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4631 return -1;
4633 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
4634 W = "{ S1[i] -> a[i] }";
4635 R = "{ S2[i] -> a[9-i] }";
4636 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4637 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4638 return -1;
4640 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
4641 W = "{ S1[i] -> a[i] }";
4642 R = "[N] -> { S2[i] -> a[N-1-i] }";
4643 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4644 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4645 return -1;
4647 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
4648 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
4649 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
4650 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
4651 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4652 return -1;
4654 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4655 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
4656 R = "{ S2[i,j] -> a[i-1,j] }";
4657 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4658 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4659 return -1;
4661 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4662 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
4663 R = "{ S2[i,j] -> a[i,j-1] }";
4664 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4665 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4666 return -1;
4668 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
4669 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
4670 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
4671 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
4672 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
4673 "S_0[] -> [0, 0, 0] }";
4674 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
4675 return -1;
4676 ctx->opt->schedule_parametric = 0;
4677 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4678 return -1;
4679 ctx->opt->schedule_parametric = 1;
4681 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
4682 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
4683 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
4684 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
4685 "S4[i] -> a[i,N] }";
4686 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
4687 "S4[i] -> [4,i,0] }";
4688 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
4689 isl_options_set_schedule_maximize_coincidence(ctx, 0);
4690 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4691 return -1;
4692 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
4694 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
4695 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4696 "j <= N }";
4697 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4698 "j <= N; "
4699 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
4700 "j <= N }";
4701 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4702 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4703 return -1;
4705 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
4706 " S_2[t] : t >= 0 and t <= -1 + N; "
4707 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
4708 "i <= -1 + N }";
4709 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
4710 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
4711 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
4712 "i >= 0 and i <= -1 + N }";
4713 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
4714 "i >= 0 and i <= -1 + N; "
4715 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
4716 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
4717 " S_0[t] -> [0, t, 0] }";
4719 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4720 return -1;
4721 ctx->opt->schedule_parametric = 0;
4722 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4723 return -1;
4724 ctx->opt->schedule_parametric = 1;
4726 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
4727 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
4728 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
4729 return -1;
4731 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
4732 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
4733 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
4734 "j >= 0 and j <= -1 + N; "
4735 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4736 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
4737 "j >= 0 and j <= -1 + N; "
4738 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4739 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
4740 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4741 return -1;
4743 D = "{ S_0[i] : i >= 0 }";
4744 W = "{ S_0[i] -> a[i] : i >= 0 }";
4745 R = "{ S_0[i] -> a[0] : i >= 0 }";
4746 S = "{ S_0[i] -> [0, i, 0] }";
4747 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4748 return -1;
4750 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
4751 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
4752 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
4753 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
4754 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4755 return -1;
4757 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
4758 "k <= -1 + n and k >= 0 }";
4759 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
4760 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
4761 "k <= -1 + n and k >= 0; "
4762 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
4763 "k <= -1 + n and k >= 0; "
4764 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
4765 "k <= -1 + n and k >= 0 }";
4766 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
4767 ctx->opt->schedule_outer_coincidence = 1;
4768 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4769 return -1;
4770 ctx->opt->schedule_outer_coincidence = 0;
4772 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
4773 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
4774 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
4775 "Stmt_for_body24[i0, i1, 1, 0]:"
4776 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
4777 "Stmt_for_body7[i0, i1, i2]:"
4778 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
4779 "i2 <= 7 }";
4781 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
4782 "Stmt_for_body24[1, i1, i2, i3]:"
4783 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
4784 "i2 >= 1;"
4785 "Stmt_for_body24[0, i1, i2, i3] -> "
4786 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
4787 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
4788 "i3 >= 0;"
4789 "Stmt_for_body24[0, i1, i2, i3] ->"
4790 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4791 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4792 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4793 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4794 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4795 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4796 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4797 "i1 <= 6 and i1 >= 0;"
4798 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4799 "Stmt_for_body7[i0, i1, i2] -> "
4800 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4801 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4802 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4803 "Stmt_for_body7[i0, i1, i2] -> "
4804 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4805 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4806 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4807 P = V;
4809 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4810 isl_options_set_schedule_treat_coalescing(ctx, 0);
4811 if (test_has_schedule(ctx, D, V, P) < 0)
4812 return -1;
4813 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4815 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4816 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4817 "j >= 1 and j <= 7;"
4818 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4819 "j >= 1 and j <= 8 }";
4820 P = "{ }";
4821 S = "{ S_0[i, j] -> [i + j, i] }";
4822 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4823 if (test_special_schedule(ctx, D, V, P, S) < 0)
4824 return -1;
4825 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4827 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4828 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4829 "j >= 0 and j <= -1 + i }";
4830 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4831 "i <= -1 + N and j >= 0;"
4832 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4833 "i <= -2 + N }";
4834 P = "{ }";
4835 S = "{ S_0[i, j] -> [i, j] }";
4836 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4837 if (test_special_schedule(ctx, D, V, P, S) < 0)
4838 return -1;
4839 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4841 /* Test both algorithms on a case with only proximity dependences. */
4842 D = "{ S[i,j] : 0 <= i <= 10 }";
4843 V = "{ }";
4844 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4845 S = "{ S[i, j] -> [j, i] }";
4846 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4847 if (test_special_schedule(ctx, D, V, P, S) < 0)
4848 return -1;
4849 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4850 if (test_special_schedule(ctx, D, V, P, S) < 0)
4851 return -1;
4853 D = "{ A[a]; B[] }";
4854 V = "{}";
4855 P = "{ A[a] -> B[] }";
4856 if (test_has_schedule(ctx, D, V, P) < 0)
4857 return -1;
4859 if (test_padded_schedule(ctx) < 0)
4860 return -1;
4862 /* Check that check for progress is not confused by rational
4863 * solution.
4865 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4866 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4867 "i0 <= -2 + N; "
4868 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4869 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4870 P = "{}";
4871 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4872 if (test_has_schedule(ctx, D, V, P) < 0)
4873 return -1;
4874 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4876 /* Check that we allow schedule rows that are only non-trivial
4877 * on some full-dimensional domains.
4879 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4880 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4881 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4882 P = "{}";
4883 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4884 if (test_has_schedule(ctx, D, V, P) < 0)
4885 return -1;
4886 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4888 if (test_conditional_schedule_constraints(ctx) < 0)
4889 return -1;
4891 if (test_strongly_satisfying_schedule(ctx) < 0)
4892 return -1;
4894 if (test_conflicting_context_schedule(ctx) < 0)
4895 return -1;
4897 if (test_bounded_coefficients_schedule(ctx) < 0)
4898 return -1;
4899 if (test_coalescing_schedule(ctx) < 0)
4900 return -1;
4901 if (test_skewing_schedule(ctx) < 0)
4902 return -1;
4904 return 0;
4907 /* Perform scheduling tests using the whole component scheduler.
4909 static int test_schedule_whole(isl_ctx *ctx)
4911 int whole;
4912 int r;
4914 whole = isl_options_get_schedule_whole_component(ctx);
4915 isl_options_set_schedule_whole_component(ctx, 1);
4916 r = test_schedule(ctx);
4917 if (r >= 0)
4918 r = test_bounded_coefficients_schedule_whole(ctx);
4919 isl_options_set_schedule_whole_component(ctx, whole);
4921 return r;
4924 /* Perform scheduling tests using the incremental scheduler.
4926 static int test_schedule_incremental(isl_ctx *ctx)
4928 int whole;
4929 int r;
4931 whole = isl_options_get_schedule_whole_component(ctx);
4932 isl_options_set_schedule_whole_component(ctx, 0);
4933 r = test_schedule(ctx);
4934 isl_options_set_schedule_whole_component(ctx, whole);
4936 return r;
4939 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4941 isl_union_map *umap;
4942 int test;
4944 umap = isl_union_map_read_from_str(ctx, str);
4945 test = isl_union_map_plain_is_injective(umap);
4946 isl_union_map_free(umap);
4947 if (test < 0)
4948 return -1;
4949 if (test == injective)
4950 return 0;
4951 if (injective)
4952 isl_die(ctx, isl_error_unknown,
4953 "map not detected as injective", return -1);
4954 else
4955 isl_die(ctx, isl_error_unknown,
4956 "map detected as injective", return -1);
4959 int test_injective(isl_ctx *ctx)
4961 const char *str;
4963 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4964 return -1;
4965 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4966 return -1;
4967 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4968 return -1;
4969 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4970 return -1;
4971 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4972 return -1;
4973 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4974 return -1;
4975 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4976 return -1;
4977 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4978 return -1;
4980 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4981 if (test_plain_injective(ctx, str, 1))
4982 return -1;
4983 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4984 if (test_plain_injective(ctx, str, 0))
4985 return -1;
4987 return 0;
4990 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4992 isl_aff *aff2;
4993 int equal;
4995 if (!aff)
4996 return -1;
4998 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4999 equal = isl_aff_plain_is_equal(aff, aff2);
5000 isl_aff_free(aff2);
5002 return equal;
5005 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
5007 int equal;
5009 equal = aff_plain_is_equal(aff, str);
5010 if (equal < 0)
5011 return -1;
5012 if (!equal)
5013 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
5014 "result not as expected", return -1);
5015 return 0;
5018 /* Is "upa" obviously equal to the isl_union_pw_aff represented by "str"?
5020 static isl_bool union_pw_aff_plain_is_equal(__isl_keep isl_union_pw_aff *upa,
5021 const char *str)
5023 isl_ctx *ctx;
5024 isl_union_pw_aff *upa2;
5025 isl_bool equal;
5027 if (!upa)
5028 return isl_bool_error;
5030 ctx = isl_union_pw_aff_get_ctx(upa);
5031 upa2 = isl_union_pw_aff_read_from_str(ctx, str);
5032 equal = isl_union_pw_aff_plain_is_equal(upa, upa2);
5033 isl_union_pw_aff_free(upa2);
5035 return equal;
5038 /* Check that "upa" is obviously equal to the isl_union_pw_aff
5039 * represented by "str".
5041 static isl_stat union_pw_aff_check_plain_equal(__isl_keep isl_union_pw_aff *upa,
5042 const char *str)
5044 isl_bool equal;
5046 equal = union_pw_aff_plain_is_equal(upa, str);
5047 if (equal < 0)
5048 return isl_stat_error;
5049 if (!equal)
5050 isl_die(isl_union_pw_aff_get_ctx(upa), isl_error_unknown,
5051 "result not as expected", return isl_stat_error);
5052 return isl_stat_ok;
5055 /* Basic tests on isl_union_pw_aff.
5057 * In particular, check that isl_union_pw_aff_aff_on_domain
5058 * aligns the parameters of the input objects and
5059 * that isl_union_pw_aff_param_on_domain_id properly
5060 * introduces the parameter.
5062 static int test_upa(isl_ctx *ctx)
5064 const char *str;
5065 isl_id *id;
5066 isl_aff *aff;
5067 isl_union_set *domain;
5068 isl_union_pw_aff *upa;
5069 isl_stat ok;
5071 aff = isl_aff_read_from_str(ctx, "[N] -> { [N] }");
5072 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
5073 domain = isl_union_set_read_from_str(ctx, str);
5074 upa = isl_union_pw_aff_aff_on_domain(domain, aff);
5075 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
5076 ok = union_pw_aff_check_plain_equal(upa, str);
5077 isl_union_pw_aff_free(upa);
5078 if (ok < 0)
5079 return -1;
5081 id = isl_id_alloc(ctx, "N", NULL);
5082 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
5083 domain = isl_union_set_read_from_str(ctx, str);
5084 upa = isl_union_pw_aff_param_on_domain_id(domain, id);
5085 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
5086 ok = union_pw_aff_check_plain_equal(upa, str);
5087 isl_union_pw_aff_free(upa);
5088 if (ok < 0)
5089 return -1;
5091 return 0;
5094 struct {
5095 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5096 __isl_take isl_aff *aff2);
5097 } aff_bin_op[] = {
5098 ['+'] = { &isl_aff_add },
5099 ['-'] = { &isl_aff_sub },
5100 ['*'] = { &isl_aff_mul },
5101 ['/'] = { &isl_aff_div },
5104 struct {
5105 const char *arg1;
5106 unsigned char op;
5107 const char *arg2;
5108 const char *res;
5109 } aff_bin_tests[] = {
5110 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
5111 "{ [i] -> [2i] }" },
5112 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
5113 "{ [i] -> [0] }" },
5114 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
5115 "{ [i] -> [2i] }" },
5116 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
5117 "{ [i] -> [2i] }" },
5118 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
5119 "{ [i] -> [i/2] }" },
5120 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
5121 "{ [i] -> [i] }" },
5122 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
5123 "{ [i] -> [NaN] }" },
5124 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
5125 "{ [i] -> [NaN] }" },
5126 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
5127 "{ [i] -> [NaN] }" },
5128 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
5129 "{ [i] -> [NaN] }" },
5130 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
5131 "{ [i] -> [NaN] }" },
5132 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
5133 "{ [i] -> [NaN] }" },
5134 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
5135 "{ [i] -> [NaN] }" },
5136 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
5137 "{ [i] -> [NaN] }" },
5138 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
5139 "{ [i] -> [NaN] }" },
5140 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
5141 "{ [i] -> [NaN] }" },
5142 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
5143 "{ [i] -> [NaN] }" },
5144 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
5145 "{ [i] -> [NaN] }" },
5148 /* Perform some basic tests of binary operations on isl_aff objects.
5150 static int test_bin_aff(isl_ctx *ctx)
5152 int i;
5153 isl_aff *aff1, *aff2, *res;
5154 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5155 __isl_take isl_aff *aff2);
5156 int ok;
5158 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
5159 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
5160 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
5161 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
5162 fn = aff_bin_op[aff_bin_tests[i].op].fn;
5163 aff1 = fn(aff1, aff2);
5164 if (isl_aff_is_nan(res))
5165 ok = isl_aff_is_nan(aff1);
5166 else
5167 ok = isl_aff_plain_is_equal(aff1, res);
5168 isl_aff_free(aff1);
5169 isl_aff_free(res);
5170 if (ok < 0)
5171 return -1;
5172 if (!ok)
5173 isl_die(ctx, isl_error_unknown,
5174 "unexpected result", return -1);
5177 return 0;
5180 struct {
5181 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
5182 __isl_take isl_pw_aff *pa2);
5183 } pw_aff_bin_op[] = {
5184 ['m'] = { &isl_pw_aff_min },
5185 ['M'] = { &isl_pw_aff_max },
5188 /* Inputs for binary isl_pw_aff operation tests.
5189 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
5190 * defined by pw_aff_bin_op, and "res" is the expected result.
5192 struct {
5193 const char *arg1;
5194 unsigned char op;
5195 const char *arg2;
5196 const char *res;
5197 } pw_aff_bin_tests[] = {
5198 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
5199 "{ [i] -> [i] }" },
5200 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
5201 "{ [i] -> [i] }" },
5202 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
5203 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
5204 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
5205 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
5206 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
5207 "{ [i] -> [NaN] }" },
5208 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
5209 "{ [i] -> [NaN] }" },
5212 /* Perform some basic tests of binary operations on isl_pw_aff objects.
5214 static int test_bin_pw_aff(isl_ctx *ctx)
5216 int i;
5217 isl_bool ok;
5218 isl_pw_aff *pa1, *pa2, *res;
5220 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
5221 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
5222 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
5223 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
5224 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
5225 if (isl_pw_aff_involves_nan(res))
5226 ok = isl_pw_aff_involves_nan(pa1);
5227 else
5228 ok = isl_pw_aff_plain_is_equal(pa1, res);
5229 isl_pw_aff_free(pa1);
5230 isl_pw_aff_free(res);
5231 if (ok < 0)
5232 return -1;
5233 if (!ok)
5234 isl_die(ctx, isl_error_unknown,
5235 "unexpected result", return -1);
5238 return 0;
5241 struct {
5242 __isl_give isl_union_pw_multi_aff *(*fn)(
5243 __isl_take isl_union_pw_multi_aff *upma1,
5244 __isl_take isl_union_pw_multi_aff *upma2);
5245 const char *arg1;
5246 const char *arg2;
5247 const char *res;
5248 } upma_bin_tests[] = {
5249 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
5250 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
5251 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
5252 "{ B[x] -> [2] : x >= 0 }",
5253 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
5254 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
5255 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
5256 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
5257 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
5258 "D[i] -> B[2] : i >= 5 }" },
5259 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5260 "{ B[x] -> C[2] : x > 0 }",
5261 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
5262 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5263 "{ B[x] -> A[2] : x >= 0 }",
5264 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
5267 /* Perform some basic tests of binary operations on
5268 * isl_union_pw_multi_aff objects.
5270 static int test_bin_upma(isl_ctx *ctx)
5272 int i;
5273 isl_union_pw_multi_aff *upma1, *upma2, *res;
5274 int ok;
5276 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
5277 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5278 upma_bin_tests[i].arg1);
5279 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5280 upma_bin_tests[i].arg2);
5281 res = isl_union_pw_multi_aff_read_from_str(ctx,
5282 upma_bin_tests[i].res);
5283 upma1 = upma_bin_tests[i].fn(upma1, upma2);
5284 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
5285 isl_union_pw_multi_aff_free(upma1);
5286 isl_union_pw_multi_aff_free(res);
5287 if (ok < 0)
5288 return -1;
5289 if (!ok)
5290 isl_die(ctx, isl_error_unknown,
5291 "unexpected result", return -1);
5294 return 0;
5297 struct {
5298 __isl_give isl_union_pw_multi_aff *(*fn)(
5299 __isl_take isl_union_pw_multi_aff *upma1,
5300 __isl_take isl_union_pw_multi_aff *upma2);
5301 const char *arg1;
5302 const char *arg2;
5303 } upma_bin_fail_tests[] = {
5304 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5305 "{ B[x] -> C[2] : x >= 0 }" },
5308 /* Perform some basic tests of binary operations on
5309 * isl_union_pw_multi_aff objects that are expected to fail.
5311 static int test_bin_upma_fail(isl_ctx *ctx)
5313 int i, n;
5314 isl_union_pw_multi_aff *upma1, *upma2;
5315 int on_error;
5317 on_error = isl_options_get_on_error(ctx);
5318 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
5319 n = ARRAY_SIZE(upma_bin_fail_tests);
5320 for (i = 0; i < n; ++i) {
5321 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5322 upma_bin_fail_tests[i].arg1);
5323 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5324 upma_bin_fail_tests[i].arg2);
5325 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
5326 isl_union_pw_multi_aff_free(upma1);
5327 if (upma1)
5328 break;
5330 isl_options_set_on_error(ctx, on_error);
5331 if (i < n)
5332 isl_die(ctx, isl_error_unknown,
5333 "operation not expected to succeed", return -1);
5335 return 0;
5338 /* Inputs for basic tests of unary operations on isl_multi_pw_aff objects.
5339 * "fn" is the function that is tested.
5340 * "arg" is a string description of the input.
5341 * "res" is a string description of the expected result.
5343 struct {
5344 __isl_give isl_multi_pw_aff *(*fn)(__isl_take isl_multi_pw_aff *mpa);
5345 const char *arg;
5346 const char *res;
5347 } mpa_un_tests[] = {
5348 { &isl_multi_pw_aff_range_factor_domain,
5349 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5350 "{ A[x] -> B[(1 : x >= 5)] }" },
5351 { &isl_multi_pw_aff_range_factor_range,
5352 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5353 "{ A[y] -> C[(2 : y <= 10)] }" },
5354 { &isl_multi_pw_aff_range_factor_domain,
5355 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5356 "{ A[x] -> B[(1 : x >= 5)] }" },
5357 { &isl_multi_pw_aff_range_factor_range,
5358 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5359 "{ A[y] -> C[] }" },
5360 { &isl_multi_pw_aff_range_factor_domain,
5361 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5362 "{ A[x] -> B[] }" },
5363 { &isl_multi_pw_aff_range_factor_range,
5364 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5365 "{ A[y] -> C[(2 : y <= 10)] }" },
5366 { &isl_multi_pw_aff_range_factor_domain,
5367 "{ A[x] -> [B[] -> C[]] }",
5368 "{ A[x] -> B[] }" },
5369 { &isl_multi_pw_aff_range_factor_range,
5370 "{ A[x] -> [B[] -> C[]] }",
5371 "{ A[y] -> C[] }" },
5372 { &isl_multi_pw_aff_factor_range,
5373 "{ [B[] -> C[]] }",
5374 "{ C[] }" },
5375 { &isl_multi_pw_aff_range_factor_domain,
5376 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5377 "{ A[x] -> B[] : x >= 0 }" },
5378 { &isl_multi_pw_aff_range_factor_range,
5379 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5380 "{ A[y] -> C[] : y >= 0 }" },
5381 { &isl_multi_pw_aff_factor_range,
5382 "[N] -> { [B[] -> C[]] : N >= 0 }",
5383 "[N] -> { C[] : N >= 0 }" },
5386 /* Perform some basic tests of unary operations on isl_multi_pw_aff objects.
5388 static int test_un_mpa(isl_ctx *ctx)
5390 int i;
5391 isl_bool ok;
5392 isl_multi_pw_aff *mpa, *res;
5394 for (i = 0; i < ARRAY_SIZE(mpa_un_tests); ++i) {
5395 mpa = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].arg);
5396 res = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].res);
5397 mpa = mpa_un_tests[i].fn(mpa);
5398 ok = isl_multi_pw_aff_plain_is_equal(mpa, res);
5399 isl_multi_pw_aff_free(mpa);
5400 isl_multi_pw_aff_free(res);
5401 if (ok < 0)
5402 return -1;
5403 if (!ok)
5404 isl_die(ctx, isl_error_unknown,
5405 "unexpected result", return -1);
5408 return 0;
5411 /* Inputs for basic tests of binary operations on isl_multi_pw_aff objects.
5412 * "fn" is the function that is tested.
5413 * "arg1" and "arg2" are string descriptions of the inputs.
5414 * "res" is a string description of the expected result.
5416 struct {
5417 __isl_give isl_multi_pw_aff *(*fn)(
5418 __isl_take isl_multi_pw_aff *mpa1,
5419 __isl_take isl_multi_pw_aff *mpa2);
5420 const char *arg1;
5421 const char *arg2;
5422 const char *res;
5423 } mpa_bin_tests[] = {
5424 { &isl_multi_pw_aff_add, "{ A[] -> [1] }", "{ A[] -> [2] }",
5425 "{ A[] -> [3] }" },
5426 { &isl_multi_pw_aff_add, "{ A[x] -> [(1 : x >= 5)] }",
5427 "{ A[x] -> [(x : x <= 10)] }",
5428 "{ A[x] -> [(1 + x : 5 <= x <= 10)] }" },
5429 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5430 "{ A[x] -> [] : x <= 10 }",
5431 "{ A[x] -> [] : 5 <= x <= 10 }" },
5432 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5433 "[N] -> { A[x] -> [] : x <= N }",
5434 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5435 { &isl_multi_pw_aff_add,
5436 "[N] -> { A[x] -> [] : x <= N }",
5437 "{ A[x] -> [] : x >= 5 }",
5438 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5439 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5440 "{ A[y] -> C[(2 : y <= 10)] }",
5441 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5442 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5443 "{ A[y] -> C[2] : y <= 10 }",
5444 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5445 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5446 "[N] -> { A[y] -> C[2] : y <= N }",
5447 "[N] -> { A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= N)]] }" },
5448 { &isl_multi_pw_aff_range_product, "[N] -> { A[x] -> B[1] : x >= N }",
5449 "{ A[y] -> C[2] : y <= 10 }",
5450 "[N] -> { A[x] -> [B[(1 : x >= N)] -> C[(2 : x <= 10)]] }" },
5451 { &isl_multi_pw_aff_range_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5452 "{ A[] -> [B[1] -> C[2]] }" },
5453 { &isl_multi_pw_aff_range_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5454 "{ A[] -> [B[] -> C[]] }" },
5455 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5456 "{ A[y] -> C[] : y <= 10 }",
5457 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] : x <= 10 }" },
5458 { &isl_multi_pw_aff_range_product, "{ A[y] -> C[] : y <= 10 }",
5459 "{ A[x] -> B[(1 : x >= 5)] }",
5460 "{ A[x] -> [C[] -> B[(1 : x >= 5)]] : x <= 10 }" },
5461 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5462 "{ A[y] -> C[(2 : y <= 10)] }",
5463 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[(2 : y <= 10)]] }" },
5464 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5465 "{ A[y] -> C[] : y <= 10 }",
5466 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= 10 }" },
5467 { &isl_multi_pw_aff_product, "{ A[y] -> C[] : y <= 10 }",
5468 "{ A[x] -> B[(1 : x >= 5)] }",
5469 "{ [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= 10 }" },
5470 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5471 "[N] -> { A[y] -> C[] : y <= N }",
5472 "[N] -> { [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= N }" },
5473 { &isl_multi_pw_aff_product, "[N] -> { A[y] -> C[] : y <= N }",
5474 "{ A[x] -> B[(1 : x >= 5)] }",
5475 "[N] -> { [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= N }" },
5476 { &isl_multi_pw_aff_product, "{ A[x] -> B[] : x >= 5 }",
5477 "{ A[y] -> C[] : y <= 10 }",
5478 "{ [A[x] -> A[y]] -> [B[] -> C[]] : x >= 5 and y <= 10 }" },
5479 { &isl_multi_pw_aff_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5480 "{ [A[] -> A[]] -> [B[1] -> C[2]] }" },
5481 { &isl_multi_pw_aff_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5482 "{ [A[] -> A[]] -> [B[] -> C[]] }" },
5483 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5484 "{ B[i,j] -> C[i + 2j] }", "{ A[a,b] -> B[b,a] }",
5485 "{ A[a,b] -> C[b + 2a] }" },
5486 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5487 "{ B[i,j] -> C[i + 2j] }",
5488 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5489 "{ A[a,b] -> C[(b + 2a : b > a)] }" },
5490 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5491 "{ B[i,j] -> C[(i + 2j : j > 4)] }",
5492 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5493 "{ A[a,b] -> C[(b + 2a : b > a > 4)] }" },
5494 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5495 "{ B[i,j] -> C[] }",
5496 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5497 "{ A[a,b] -> C[] }" },
5498 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5499 "{ B[i,j] -> C[] : i > j }",
5500 "{ A[a,b] -> B[b,a] }",
5501 "{ A[a,b] -> C[] : b > a }" },
5502 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5503 "{ B[i,j] -> C[] : j > 5 }",
5504 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5505 "{ A[a,b] -> C[] : b > a > 5 }" },
5506 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5507 "[N] -> { B[i,j] -> C[] : j > N }",
5508 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5509 "[N] -> { A[a,b] -> C[] : b > a > N }" },
5510 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5511 "[M,N] -> { B[] -> C[] : N > 5 }",
5512 "[M,N] -> { A[] -> B[] : M > N }",
5513 "[M,N] -> { A[] -> C[] : M > N > 5 }" },
5516 /* Perform some basic tests of binary operations on isl_multi_pw_aff objects.
5518 static int test_bin_mpa(isl_ctx *ctx)
5520 int i;
5521 isl_bool ok;
5522 isl_multi_pw_aff *mpa1, *mpa2, *res;
5524 for (i = 0; i < ARRAY_SIZE(mpa_bin_tests); ++i) {
5525 mpa1 = isl_multi_pw_aff_read_from_str(ctx,
5526 mpa_bin_tests[i].arg1);
5527 mpa2 = isl_multi_pw_aff_read_from_str(ctx,
5528 mpa_bin_tests[i].arg2);
5529 res = isl_multi_pw_aff_read_from_str(ctx,
5530 mpa_bin_tests[i].res);
5531 mpa1 = mpa_bin_tests[i].fn(mpa1, mpa2);
5532 ok = isl_multi_pw_aff_plain_is_equal(mpa1, res);
5533 isl_multi_pw_aff_free(mpa1);
5534 isl_multi_pw_aff_free(res);
5535 if (ok < 0)
5536 return -1;
5537 if (!ok)
5538 isl_die(ctx, isl_error_unknown,
5539 "unexpected result", return -1);
5542 return 0;
5545 /* Inputs for basic tests of unary operations on
5546 * isl_multi_union_pw_aff objects.
5547 * "fn" is the function that is tested.
5548 * "arg" is a string description of the input.
5549 * "res" is a string description of the expected result.
5551 struct {
5552 __isl_give isl_multi_union_pw_aff *(*fn)(
5553 __isl_take isl_multi_union_pw_aff *mupa);
5554 const char *arg;
5555 const char *res;
5556 } mupa_un_tests[] = {
5557 { &isl_multi_union_pw_aff_factor_range,
5558 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]",
5559 "C[{ A[] -> [2] }]" },
5560 { &isl_multi_union_pw_aff_factor_range,
5561 "[B[] -> C[{ A[] -> [2] }]]",
5562 "C[{ A[] -> [2] }]" },
5563 { &isl_multi_union_pw_aff_factor_range,
5564 "[B[{ A[] -> [1] }] -> C[]]",
5565 "C[]" },
5566 { &isl_multi_union_pw_aff_factor_range,
5567 "[B[] -> C[]]",
5568 "C[]" },
5569 { &isl_multi_union_pw_aff_factor_range,
5570 "([B[] -> C[]] : { A[x] : x >= 0 })",
5571 "(C[] : { A[x] : x >= 0 })" },
5572 { &isl_multi_union_pw_aff_factor_range,
5573 "[N] -> ([B[] -> C[]] : { A[x] : x <= N })",
5574 "[N] -> (C[] : { A[x] : x <= N })" },
5575 { &isl_multi_union_pw_aff_factor_range,
5576 "[N] -> ([B[] -> C[]] : { : N >= 0 })",
5577 "[N] -> (C[] : { : N >= 0 })" },
5580 /* Perform some basic tests of unary operations on
5581 * isl_multi_union_pw_aff objects.
5583 static int test_un_mupa(isl_ctx *ctx)
5585 int i;
5586 isl_bool ok;
5587 isl_multi_union_pw_aff *mupa, *res;
5589 for (i = 0; i < ARRAY_SIZE(mupa_un_tests); ++i) {
5590 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5591 mupa_un_tests[i].arg);
5592 res = isl_multi_union_pw_aff_read_from_str(ctx,
5593 mupa_un_tests[i].res);
5594 mupa = mupa_un_tests[i].fn(mupa);
5595 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5596 isl_multi_union_pw_aff_free(mupa);
5597 isl_multi_union_pw_aff_free(res);
5598 if (ok < 0)
5599 return -1;
5600 if (!ok)
5601 isl_die(ctx, isl_error_unknown,
5602 "unexpected result", return -1);
5605 return 0;
5608 /* Inputs for basic tests of binary operations on
5609 * isl_multi_union_pw_aff objects.
5610 * "fn" is the function that is tested.
5611 * "arg1" and "arg2" are string descriptions of the inputs.
5612 * "res" is a string description of the expected result.
5614 struct {
5615 __isl_give isl_multi_union_pw_aff *(*fn)(
5616 __isl_take isl_multi_union_pw_aff *mupa1,
5617 __isl_take isl_multi_union_pw_aff *mupa2);
5618 const char *arg1;
5619 const char *arg2;
5620 const char *res;
5621 } mupa_bin_tests[] = {
5622 { &isl_multi_union_pw_aff_add, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5623 "[{ A[] -> [3] }]" },
5624 { &isl_multi_union_pw_aff_sub, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5625 "[{ A[] -> [-1] }]" },
5626 { &isl_multi_union_pw_aff_add,
5627 "[{ A[] -> [1]; B[] -> [4] }]",
5628 "[{ A[] -> [2]; C[] -> [5] }]",
5629 "[{ A[] -> [3] }]" },
5630 { &isl_multi_union_pw_aff_union_add,
5631 "[{ A[] -> [1]; B[] -> [4] }]",
5632 "[{ A[] -> [2]; C[] -> [5] }]",
5633 "[{ A[] -> [3]; B[] -> [4]; C[] -> [5] }]" },
5634 { &isl_multi_union_pw_aff_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5635 "[{ A[x] -> [(x)] : x <= 10 }]",
5636 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10 }]" },
5637 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5638 "([] : { A[x] : x <= 10 })",
5639 "([] : { A[x] : 5 <= x <= 10 })" },
5640 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5641 "[N] -> ([] : { A[x] : x <= N })",
5642 "[N] -> ([] : { A[x] : 5 <= x <= N })" },
5643 { &isl_multi_union_pw_aff_add, "[N] -> ([] : { A[x] : x >= N })",
5644 "([] : { A[x] : x <= 10 })",
5645 "[N] -> ([] : { A[x] : N <= x <= 10 })" },
5646 { &isl_multi_union_pw_aff_union_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5647 "[{ A[x] -> [(x)] : x <= 10 }]",
5648 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10; "
5649 "A[x] -> [(1)] : x > 10; A[x] -> [(x)] : x < 5 }]" },
5650 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 5 })",
5651 "([] : { A[x] : x <= 10 })",
5652 "([] : { A[x] })" },
5653 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 0 })",
5654 "[N] -> ([] : { A[x] : x >= N })",
5655 "[N] -> ([] : { A[x] : x >= 0 or x >= N })" },
5656 { &isl_multi_union_pw_aff_union_add,
5657 "[N] -> ([] : { A[] : N >= 0})",
5658 "[N] -> ([] : { A[] : N <= 0})",
5659 "[N] -> ([] : { A[] })" },
5660 { &isl_multi_union_pw_aff_union_add,
5661 "[N] -> ([] : { A[] })",
5662 "[N] -> ([] : { : })",
5663 "[N] -> ([] : { : })" },
5664 { &isl_multi_union_pw_aff_union_add,
5665 "[N] -> ([] : { : })",
5666 "[N] -> ([] : { A[] })",
5667 "[N] -> ([] : { : })" },
5668 { &isl_multi_union_pw_aff_union_add,
5669 "[N] -> ([] : { : N >= 0})",
5670 "[N] -> ([] : { : N <= 0})",
5671 "[N] -> ([] : { : })" },
5672 { &isl_multi_union_pw_aff_range_product,
5673 "B[{ A[] -> [1] }]",
5674 "C[{ A[] -> [2] }]",
5675 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]" },
5676 { &isl_multi_union_pw_aff_range_product,
5677 "(B[] : { A[x] : x >= 5 })",
5678 "(C[] : { A[x] : x <= 10 })",
5679 "([B[] -> C[]] : { A[x] : 5 <= x <= 10 })" },
5680 { &isl_multi_union_pw_aff_range_product,
5681 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5682 "(C[] : { A[x] : x <= 10 })",
5683 "[B[{ A[x] -> [x + 1] : 5 <= x <= 10 }] -> C[]]" },
5684 { &isl_multi_union_pw_aff_range_product,
5685 "(C[] : { A[x] : x <= 10 })",
5686 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5687 "[C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= 10 }]]" },
5688 { &isl_multi_union_pw_aff_range_product,
5689 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5690 "[N] -> (C[] : { A[x] : x <= N })",
5691 "[N] -> [B[{ A[x] -> [x + 1] : 5 <= x <= N }] -> C[]]" },
5692 { &isl_multi_union_pw_aff_range_product,
5693 "[N] -> (C[] : { A[x] : x <= N })",
5694 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5695 "[N] -> [C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= N }]]" },
5696 { &isl_multi_union_pw_aff_range_product,
5697 "B[{ A[] -> [1]; D[] -> [3] }]",
5698 "C[{ A[] -> [2] }]",
5699 "[B[{ A[] -> [1]; D[] -> [3] }] -> C[{ A[] -> [2] }]]" },
5700 { &isl_multi_union_pw_aff_range_product,
5701 "B[] }]",
5702 "(C[] : { A[x] })",
5703 "([B[] -> C[]] : { A[x] })" },
5704 { &isl_multi_union_pw_aff_range_product,
5705 "(B[] : { A[x] })",
5706 "C[] }]",
5707 "([B[] -> C[]] : { A[x] })" },
5710 /* Perform some basic tests of binary operations on
5711 * isl_multi_union_pw_aff objects.
5713 static int test_bin_mupa(isl_ctx *ctx)
5715 int i;
5716 isl_bool ok;
5717 isl_multi_union_pw_aff *mupa1, *mupa2, *res;
5719 for (i = 0; i < ARRAY_SIZE(mupa_bin_tests); ++i) {
5720 mupa1 = isl_multi_union_pw_aff_read_from_str(ctx,
5721 mupa_bin_tests[i].arg1);
5722 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx,
5723 mupa_bin_tests[i].arg2);
5724 res = isl_multi_union_pw_aff_read_from_str(ctx,
5725 mupa_bin_tests[i].res);
5726 mupa1 = mupa_bin_tests[i].fn(mupa1, mupa2);
5727 ok = isl_multi_union_pw_aff_plain_is_equal(mupa1, res);
5728 isl_multi_union_pw_aff_free(mupa1);
5729 isl_multi_union_pw_aff_free(res);
5730 if (ok < 0)
5731 return -1;
5732 if (!ok)
5733 isl_die(ctx, isl_error_unknown,
5734 "unexpected result", return -1);
5737 return 0;
5740 /* Inputs for basic tests of binary operations on
5741 * pairs of isl_multi_union_pw_aff and isl_set objects.
5742 * "fn" is the function that is tested.
5743 * "arg1" and "arg2" are string descriptions of the inputs.
5744 * "res" is a string description of the expected result.
5746 struct {
5747 __isl_give isl_multi_union_pw_aff *(*fn)(
5748 __isl_take isl_multi_union_pw_aff *mupa,
5749 __isl_take isl_set *set);
5750 const char *arg1;
5751 const char *arg2;
5752 const char *res;
5753 } mupa_set_tests[] = {
5754 { &isl_multi_union_pw_aff_intersect_range,
5755 "C[{ B[i,j] -> [i + 2j] }]", "{ C[1] }",
5756 "C[{ B[i,j] -> [i + 2j] : i + 2j = 1 }]" },
5757 { &isl_multi_union_pw_aff_intersect_range,
5758 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[N] }",
5759 "[N] -> C[{ B[i,j] -> [i + 2j] : i + 2j = N }]" },
5760 { &isl_multi_union_pw_aff_intersect_range,
5761 "[N] -> C[{ B[i,j] -> [i + 2j + N] }]", "{ C[1] }",
5762 "[N] -> C[{ B[i,j] -> [i + 2j + N] : i + 2j + N = 1 }]" },
5763 { &isl_multi_union_pw_aff_intersect_range,
5764 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[x] : N >= 0 }",
5765 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0 }]" },
5766 { &isl_multi_union_pw_aff_intersect_range,
5767 "C[]", "{ C[] }", "C[]" },
5768 { &isl_multi_union_pw_aff_intersect_range,
5769 "[N] -> (C[] : { : N >= 0 })",
5770 "{ C[] }",
5771 "[N] -> (C[] : { : N >= 0 })" },
5772 { &isl_multi_union_pw_aff_intersect_range,
5773 "(C[] : { A[a,b] })",
5774 "{ C[] }",
5775 "(C[] : { A[a,b] })" },
5776 { &isl_multi_union_pw_aff_intersect_range,
5777 "[N] -> (C[] : { A[a,b] : a,b <= N })",
5778 "{ C[] }",
5779 "[N] -> (C[] : { A[a,b] : a,b <= N })" },
5780 { &isl_multi_union_pw_aff_intersect_range,
5781 "C[]",
5782 "[N] -> { C[] : N >= 0 }",
5783 "[N] -> (C[] : { : N >= 0 })" },
5784 { &isl_multi_union_pw_aff_intersect_range,
5785 "(C[] : { A[a,b] })",
5786 "[N] -> { C[] : N >= 0 }",
5787 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5788 { &isl_multi_union_pw_aff_intersect_range,
5789 "[N] -> (C[] : { : N >= 0 })",
5790 "[N] -> { C[] : N < 1024 }",
5791 "[N] -> (C[] : { : 0 <= N < 1024 })" },
5792 { &isl_multi_union_pw_aff_intersect_params,
5793 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { : N >= 0 }",
5794 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0}]" },
5795 { &isl_multi_union_pw_aff_intersect_params,
5796 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "[N] -> { : N >= 0 }",
5797 "[N] -> C[{ B[i,j] -> [i + 2j] : 0 <= N <= 256 }]" },
5798 { &isl_multi_union_pw_aff_intersect_params,
5799 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "{ : }",
5800 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]" },
5801 { &isl_multi_union_pw_aff_intersect_params,
5802 "C[]", "[N] -> { : N >= 0 }",
5803 "[N] -> (C[] : { : N >= 0 })" },
5804 { &isl_multi_union_pw_aff_intersect_params,
5805 "(C[] : { A[a,b] })", "[N] -> { : N >= 0 }",
5806 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5807 { &isl_multi_union_pw_aff_intersect_params,
5808 "[N] -> (C[] : { A[a,N] })", "{ : }",
5809 "[N] -> (C[] : { A[a,N] })" },
5810 { &isl_multi_union_pw_aff_intersect_params,
5811 "[N] -> (C[] : { A[a,b] : N <= 256 })", "[N] -> { : N >= 0 }",
5812 "[N] -> (C[] : { A[a,b] : 0 <= N <= 256 })" },
5815 /* Perform some basic tests of binary operations on
5816 * pairs of isl_multi_union_pw_aff and isl_set objects.
5818 static int test_mupa_set(isl_ctx *ctx)
5820 int i;
5821 isl_bool ok;
5822 isl_multi_union_pw_aff *mupa, *res;
5823 isl_set *set;
5825 for (i = 0; i < ARRAY_SIZE(mupa_set_tests); ++i) {
5826 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5827 mupa_set_tests[i].arg1);
5828 set = isl_set_read_from_str(ctx, mupa_set_tests[i].arg2);
5829 res = isl_multi_union_pw_aff_read_from_str(ctx,
5830 mupa_set_tests[i].res);
5831 mupa = mupa_set_tests[i].fn(mupa, set);
5832 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5833 isl_multi_union_pw_aff_free(mupa);
5834 isl_multi_union_pw_aff_free(res);
5835 if (ok < 0)
5836 return -1;
5837 if (!ok)
5838 isl_die(ctx, isl_error_unknown,
5839 "unexpected result", return -1);
5842 return 0;
5845 /* Inputs for basic tests of binary operations on
5846 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5847 * "fn" is the function that is tested.
5848 * "arg1" and "arg2" are string descriptions of the inputs.
5849 * "res" is a string description of the expected result.
5851 struct {
5852 __isl_give isl_multi_union_pw_aff *(*fn)(
5853 __isl_take isl_multi_union_pw_aff *mupa,
5854 __isl_take isl_union_set *uset);
5855 const char *arg1;
5856 const char *arg2;
5857 const char *res;
5858 } mupa_uset_tests[] = {
5859 { &isl_multi_union_pw_aff_intersect_domain,
5860 "C[{ B[i,j] -> [i + 2j] }]", "{ B[i,i] }",
5861 "C[{ B[i,i] -> [3i] }]" },
5862 { &isl_multi_union_pw_aff_intersect_domain,
5863 "(C[] : { B[i,j] })", "{ B[i,i] }",
5864 "(C[] : { B[i,i] })" },
5865 { &isl_multi_union_pw_aff_intersect_domain,
5866 "(C[] : { B[i,j] })", "[N] -> { B[N,N] }",
5867 "[N] -> (C[] : { B[N,N] })" },
5868 { &isl_multi_union_pw_aff_intersect_domain,
5869 "C[]", "{ B[i,i] }",
5870 "(C[] : { B[i,i] })" },
5871 { &isl_multi_union_pw_aff_intersect_domain,
5872 "[N] -> (C[] : { : N >= 0 })", "{ B[i,i] }",
5873 "[N] -> (C[] : { B[i,i] : N >= 0 })" },
5876 /* Perform some basic tests of binary operations on
5877 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5879 static int test_mupa_uset(isl_ctx *ctx)
5881 int i;
5882 isl_bool ok;
5883 isl_multi_union_pw_aff *mupa, *res;
5884 isl_union_set *uset;
5886 for (i = 0; i < ARRAY_SIZE(mupa_uset_tests); ++i) {
5887 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5888 mupa_uset_tests[i].arg1);
5889 uset = isl_union_set_read_from_str(ctx,
5890 mupa_uset_tests[i].arg2);
5891 res = isl_multi_union_pw_aff_read_from_str(ctx,
5892 mupa_uset_tests[i].res);
5893 mupa = mupa_uset_tests[i].fn(mupa, uset);
5894 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5895 isl_multi_union_pw_aff_free(mupa);
5896 isl_multi_union_pw_aff_free(res);
5897 if (ok < 0)
5898 return -1;
5899 if (!ok)
5900 isl_die(ctx, isl_error_unknown,
5901 "unexpected result", return -1);
5904 return 0;
5907 /* Inputs for basic tests of binary operations on
5908 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5909 * "fn" is the function that is tested.
5910 * "arg1" and "arg2" are string descriptions of the inputs.
5911 * "res" is a string description of the expected result.
5913 struct {
5914 __isl_give isl_multi_union_pw_aff *(*fn)(
5915 __isl_take isl_multi_union_pw_aff *mupa,
5916 __isl_take isl_multi_aff *ma);
5917 const char *arg1;
5918 const char *arg2;
5919 const char *res;
5920 } mupa_ma_tests[] = {
5921 { &isl_multi_union_pw_aff_apply_multi_aff,
5922 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5923 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5924 "{ C[a,b] -> D[b,a] }",
5925 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
5926 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
5927 { &isl_multi_union_pw_aff_apply_multi_aff,
5928 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
5929 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5930 "{ C[a,b] -> D[b,a] }",
5931 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
5932 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
5933 { &isl_multi_union_pw_aff_apply_multi_aff,
5934 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5935 "[N] -> { C[a] -> D[a + N] }",
5936 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }] " },
5937 { &isl_multi_union_pw_aff_apply_multi_aff,
5938 "C[]",
5939 "{ C[] -> D[] }",
5940 "D[]" },
5941 { &isl_multi_union_pw_aff_apply_multi_aff,
5942 "[N] -> (C[] : { : N >= 0 })",
5943 "{ C[] -> D[] }",
5944 "[N] -> (D[] : { : N >= 0 })" },
5945 { &isl_multi_union_pw_aff_apply_multi_aff,
5946 "C[]",
5947 "[N] -> { C[] -> D[N] }",
5948 "[N] -> D[{ [N] }]" },
5949 { &isl_multi_union_pw_aff_apply_multi_aff,
5950 "(C[] : { A[i,j] : i >= j })",
5951 "{ C[] -> D[] }",
5952 "(D[] : { A[i,j] : i >= j })" },
5953 { &isl_multi_union_pw_aff_apply_multi_aff,
5954 "[N] -> (C[] : { A[i,j] : N >= 0 })",
5955 "{ C[] -> D[] }",
5956 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
5957 { &isl_multi_union_pw_aff_apply_multi_aff,
5958 "(C[] : { A[i,j] : i >= j })",
5959 "[N] -> { C[] -> D[N] }",
5960 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
5963 /* Perform some basic tests of binary operations on
5964 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5966 static int test_mupa_ma(isl_ctx *ctx)
5968 int i;
5969 isl_bool ok;
5970 isl_multi_union_pw_aff *mupa, *res;
5971 isl_multi_aff *ma;
5973 for (i = 0; i < ARRAY_SIZE(mupa_ma_tests); ++i) {
5974 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5975 mupa_ma_tests[i].arg1);
5976 ma = isl_multi_aff_read_from_str(ctx, mupa_ma_tests[i].arg2);
5977 res = isl_multi_union_pw_aff_read_from_str(ctx,
5978 mupa_ma_tests[i].res);
5979 mupa = mupa_ma_tests[i].fn(mupa, ma);
5980 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5981 isl_multi_union_pw_aff_free(mupa);
5982 isl_multi_union_pw_aff_free(res);
5983 if (ok < 0)
5984 return -1;
5985 if (!ok)
5986 isl_die(ctx, isl_error_unknown,
5987 "unexpected result", return -1);
5990 return 0;
5993 /* Inputs for basic tests of binary operations on
5994 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
5995 * "fn" is the function that is tested.
5996 * "arg1" and "arg2" are string descriptions of the inputs.
5997 * "res" is a string description of the expected result.
5999 struct {
6000 __isl_give isl_union_pw_aff *(*fn)(
6001 __isl_take isl_multi_union_pw_aff *mupa,
6002 __isl_take isl_pw_aff *pa);
6003 const char *arg1;
6004 const char *arg2;
6005 const char *res;
6006 } mupa_pa_tests[] = {
6007 { &isl_multi_union_pw_aff_apply_pw_aff,
6008 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6009 "[N] -> { C[a] -> [a + N] }",
6010 "[N] -> { A[i,j] -> [i + N]; B[i,j] -> [j + N] }" },
6011 { &isl_multi_union_pw_aff_apply_pw_aff,
6012 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6013 "{ C[a] -> [a] : a >= 0; C[a] -> [-a] : a < 0 }",
6014 "{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
6015 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }" },
6016 { &isl_multi_union_pw_aff_apply_pw_aff,
6017 "C[]",
6018 "[N] -> { C[] -> [N] }",
6019 "[N] -> { [N] }" },
6020 { &isl_multi_union_pw_aff_apply_pw_aff,
6021 "C[]",
6022 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
6023 "[N] -> { [N] : N >= 0; [-N] : N < 0 }" },
6024 { &isl_multi_union_pw_aff_apply_pw_aff,
6025 "[N] -> (C[] : { : N >= 0 })",
6026 "[N] -> { C[] -> [N] }",
6027 "[N] -> { [N] : N >= 0 }" },
6028 { &isl_multi_union_pw_aff_apply_pw_aff,
6029 "[N] -> (C[] : { : N >= 0 })",
6030 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
6031 "[N] -> { [N] : N >= 0 }" },
6032 { &isl_multi_union_pw_aff_apply_pw_aff,
6033 "[N] -> (C[] : { : N >= 0 })",
6034 "{ C[] -> [0] }",
6035 "[N] -> { [0] : N >= 0 }" },
6036 { &isl_multi_union_pw_aff_apply_pw_aff,
6037 "(C[] : { A[i,j] : i >= j })",
6038 "[N] -> { C[] -> [N] }",
6039 "[N] -> { A[i,j] -> [N] : i >= j }" },
6040 { &isl_multi_union_pw_aff_apply_pw_aff,
6041 "(C[] : { A[i,j] : i >= j })",
6042 "[N] -> { C[] -> [N] : N >= 0 }",
6043 "[N] -> { A[i,j] -> [N] : i >= j and N >= 0 }" },
6046 /* Perform some basic tests of binary operations on
6047 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
6049 static int test_mupa_pa(isl_ctx *ctx)
6051 int i;
6052 isl_bool ok;
6053 isl_multi_union_pw_aff *mupa;
6054 isl_union_pw_aff *upa, *res;
6055 isl_pw_aff *pa;
6057 for (i = 0; i < ARRAY_SIZE(mupa_pa_tests); ++i) {
6058 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6059 mupa_pa_tests[i].arg1);
6060 pa = isl_pw_aff_read_from_str(ctx, mupa_pa_tests[i].arg2);
6061 res = isl_union_pw_aff_read_from_str(ctx,
6062 mupa_pa_tests[i].res);
6063 upa = mupa_pa_tests[i].fn(mupa, pa);
6064 ok = isl_union_pw_aff_plain_is_equal(upa, res);
6065 isl_union_pw_aff_free(upa);
6066 isl_union_pw_aff_free(res);
6067 if (ok < 0)
6068 return -1;
6069 if (!ok)
6070 isl_die(ctx, isl_error_unknown,
6071 "unexpected result", return -1);
6074 return 0;
6077 /* Inputs for basic tests of binary operations on
6078 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6079 * "fn" is the function that is tested.
6080 * "arg1" and "arg2" are string descriptions of the inputs.
6081 * "res" is a string description of the expected result.
6083 struct {
6084 __isl_give isl_multi_union_pw_aff *(*fn)(
6085 __isl_take isl_multi_union_pw_aff *mupa,
6086 __isl_take isl_pw_multi_aff *pma);
6087 const char *arg1;
6088 const char *arg2;
6089 const char *res;
6090 } mupa_pma_tests[] = {
6091 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6092 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6093 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6094 "{ C[a,b] -> D[b,a] }",
6095 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
6096 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
6097 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6098 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
6099 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6100 "{ C[a,b] -> D[b,a] }",
6101 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
6102 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
6103 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6104 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6105 "[N] -> { C[a] -> D[a + N] }",
6106 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }]" },
6107 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6108 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6109 "{ C[a] -> D[a] : a >= 0; C[a] -> D[-a] : a < 0 }",
6110 "D[{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
6111 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }]" },
6112 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6113 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6114 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6115 "{ C[a,b] -> D[a,b] : a >= b; C[a,b] -> D[b,a] : a < b }",
6116 "D[{ A[i,j] -> [i] : i >= j; A[i,j] -> [j] : i < j; "
6117 "B[i,j] -> [j] : i <= j; B[i,j] -> [i] : i > j }, "
6118 "{ A[i,j] -> [j] : i >= j; A[i,j] -> [i] : i < j; "
6119 "B[i,j] -> [i] : i <= j; B[i,j] -> [j] : i > j }]" },
6120 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6121 "C[]",
6122 "{ C[] -> D[] }",
6123 "D[]" },
6124 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6125 "[N] -> (C[] : { : N >= 0 })",
6126 "{ C[] -> D[] }",
6127 "[N] -> (D[] : { : N >= 0 })" },
6128 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6129 "C[]",
6130 "[N] -> { C[] -> D[N] }",
6131 "[N] -> D[{ [N] }]" },
6132 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6133 "(C[] : { A[i,j] : i >= j })",
6134 "{ C[] -> D[] }",
6135 "(D[] : { A[i,j] : i >= j })" },
6136 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6137 "[N] -> (C[] : { A[i,j] : N >= 0 })",
6138 "{ C[] -> D[] }",
6139 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
6140 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6141 "(C[] : { A[i,j] : i >= j })",
6142 "[N] -> { C[] -> D[N] }",
6143 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
6144 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6145 "C[]",
6146 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6147 "[N] -> D[{ [N] : N >= 0; [-N] : N < 0 }]" },
6148 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6149 "[N] -> (C[] : { : N >= 0 })",
6150 "[N] -> { C[] -> D[N] }",
6151 "[N] -> D[{ [N] : N >= 0 }]" },
6152 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6153 "[N] -> (C[] : { : N >= 0 })",
6154 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6155 "[N] -> D[{ [N] : N >= 0 }]" },
6156 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6157 "[N] -> (C[] : { : N >= 0 })",
6158 "{ C[] -> D[0] }",
6159 "[N] -> D[{ [0] : N >= 0 }]" },
6160 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6161 "(C[] : { A[i,j] : i >= j })",
6162 "[N] -> { C[] -> D[N] : N >= 0 }",
6163 "[N] -> D[{ A[i,j] -> [N] : i >= j and N >= 0 }]" },
6166 /* Perform some basic tests of binary operations on
6167 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6169 static int test_mupa_pma(isl_ctx *ctx)
6171 int i;
6172 isl_bool ok;
6173 isl_multi_union_pw_aff *mupa, *res;
6174 isl_pw_multi_aff *pma;
6176 for (i = 0; i < ARRAY_SIZE(mupa_pma_tests); ++i) {
6177 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6178 mupa_pma_tests[i].arg1);
6179 pma = isl_pw_multi_aff_read_from_str(ctx,
6180 mupa_pma_tests[i].arg2);
6181 res = isl_multi_union_pw_aff_read_from_str(ctx,
6182 mupa_pma_tests[i].res);
6183 mupa = mupa_pma_tests[i].fn(mupa, pma);
6184 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6185 isl_multi_union_pw_aff_free(mupa);
6186 isl_multi_union_pw_aff_free(res);
6187 if (ok < 0)
6188 return -1;
6189 if (!ok)
6190 isl_die(ctx, isl_error_unknown,
6191 "unexpected result", return -1);
6194 return 0;
6197 /* Inputs for basic tests of binary operations on
6198 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6199 * "fn" is the function that is tested.
6200 * "arg1" and "arg2" are string descriptions of the inputs.
6201 * "res" is a string description of the expected result.
6203 struct {
6204 __isl_give isl_multi_union_pw_aff *(*fn)(
6205 __isl_take isl_multi_union_pw_aff *mupa,
6206 __isl_take isl_union_pw_multi_aff *upma);
6207 const char *arg1;
6208 const char *arg2;
6209 const char *res;
6210 } mupa_upma_tests[] = {
6211 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6212 "C[{ B[i,j] -> [i + 2j] }]", "{ A[a,b] -> B[b,a] }",
6213 "C[{ A[a,b] -> [b + 2a] }]" },
6214 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6215 "C[{ B[i,j] -> [i + 2j] }]",
6216 "{ A[a,b] -> B[b,a] : b > a }",
6217 "C[{ A[a,b] -> [b + 2a] : b > a }]" },
6218 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6219 "C[{ B[i,j] -> [i + 2j] : j > 4 }]",
6220 "{ A[a,b] -> B[b,a] : b > a }",
6221 "C[{ A[a,b] -> [b + 2a] : b > a > 4 }]" },
6222 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6223 "C[{ B[i,j] -> [i + 2j] }]",
6224 "{ A[a,b] -> B[b,a] : a > b; A[a,b] -> B[a,b] : a <= b }",
6225 "C[{ A[a,b] -> [b + 2a] : a > b; A[a,b] -> [a + 2b] : a <= b }]" },
6226 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6227 "(C[] : { B[a,b] })",
6228 "{ A[a,b] -> B[b,a] }",
6229 "(C[] : { A[a,b] })" },
6230 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6231 "(C[] : { B[a,b] })",
6232 "{ B[a,b] -> A[b,a] }",
6233 "(C[] : { })" },
6234 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6235 "(C[] : { B[a,b] })",
6236 "{ A[a,b] -> B[b,a] : a > b }",
6237 "(C[] : { A[a,b] : a > b })" },
6238 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6239 "(C[] : { B[a,b] : a > b })",
6240 "{ A[a,b] -> B[b,a] }",
6241 "(C[] : { A[a,b] : b > a })" },
6242 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6243 "[N] -> (C[] : { B[a,b] : a > N })",
6244 "{ A[a,b] -> B[b,a] : a > b }",
6245 "[N] -> (C[] : { A[a,b] : a > b > N })" },
6246 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6247 "(C[] : { B[a,b] : a > b })",
6248 "[N] -> { A[a,b] -> B[b,a] : a > N }",
6249 "[N] -> (C[] : { A[a,b] : b > a > N })" },
6250 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6251 "C[]",
6252 "{ A[a,b] -> B[b,a] }",
6253 "C[]" },
6254 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6255 "[N] -> (C[] : { : N >= 0 })",
6256 "{ A[a,b] -> B[b,a] }",
6257 "[N] -> (C[] : { : N >= 0 })" },
6258 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6259 "C[]",
6260 "[N] -> { A[a,b] -> B[b,a] : N >= 0 }",
6261 "[N] -> (C[] : { : N >= 0 })" },
6264 /* Perform some basic tests of binary operations on
6265 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6267 static int test_mupa_upma(isl_ctx *ctx)
6269 int i;
6270 isl_bool ok;
6271 isl_multi_union_pw_aff *mupa, *res;
6272 isl_union_pw_multi_aff *upma;
6274 for (i = 0; i < ARRAY_SIZE(mupa_upma_tests); ++i) {
6275 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6276 mupa_upma_tests[i].arg1);
6277 upma = isl_union_pw_multi_aff_read_from_str(ctx,
6278 mupa_upma_tests[i].arg2);
6279 res = isl_multi_union_pw_aff_read_from_str(ctx,
6280 mupa_upma_tests[i].res);
6281 mupa = mupa_upma_tests[i].fn(mupa, upma);
6282 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6283 isl_multi_union_pw_aff_free(mupa);
6284 isl_multi_union_pw_aff_free(res);
6285 if (ok < 0)
6286 return -1;
6287 if (!ok)
6288 isl_die(ctx, isl_error_unknown,
6289 "unexpected result", return -1);
6292 return 0;
6295 /* Check that the input tuple of an isl_aff can be set properly.
6297 static isl_stat test_aff_set_tuple_id(isl_ctx *ctx)
6299 isl_id *id;
6300 isl_aff *aff;
6301 int equal;
6303 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x + 1] }");
6304 id = isl_id_alloc(ctx, "A", NULL);
6305 aff = isl_aff_set_tuple_id(aff, isl_dim_in, id);
6306 equal = aff_check_plain_equal(aff, "{ A[x] -> [x + 1] }");
6307 isl_aff_free(aff);
6308 if (equal < 0)
6309 return isl_stat_error;
6311 return isl_stat_ok;
6314 int test_aff(isl_ctx *ctx)
6316 const char *str;
6317 isl_set *set;
6318 isl_space *space;
6319 isl_local_space *ls;
6320 isl_aff *aff;
6321 int zero, equal;
6323 if (test_upa(ctx) < 0)
6324 return -1;
6325 if (test_bin_aff(ctx) < 0)
6326 return -1;
6327 if (test_bin_pw_aff(ctx) < 0)
6328 return -1;
6329 if (test_bin_upma(ctx) < 0)
6330 return -1;
6331 if (test_bin_upma_fail(ctx) < 0)
6332 return -1;
6333 if (test_un_mpa(ctx) < 0)
6334 return -1;
6335 if (test_bin_mpa(ctx) < 0)
6336 return -1;
6337 if (test_un_mupa(ctx) < 0)
6338 return -1;
6339 if (test_bin_mupa(ctx) < 0)
6340 return -1;
6341 if (test_mupa_set(ctx) < 0)
6342 return -1;
6343 if (test_mupa_uset(ctx) < 0)
6344 return -1;
6345 if (test_mupa_ma(ctx) < 0)
6346 return -1;
6347 if (test_mupa_pa(ctx) < 0)
6348 return -1;
6349 if (test_mupa_pma(ctx) < 0)
6350 return -1;
6351 if (test_mupa_upma(ctx) < 0)
6352 return -1;
6354 space = isl_space_set_alloc(ctx, 0, 1);
6355 ls = isl_local_space_from_space(space);
6356 aff = isl_aff_zero_on_domain(ls);
6358 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6359 aff = isl_aff_scale_down_ui(aff, 3);
6360 aff = isl_aff_floor(aff);
6361 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6362 aff = isl_aff_scale_down_ui(aff, 2);
6363 aff = isl_aff_floor(aff);
6364 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6366 str = "{ [10] }";
6367 set = isl_set_read_from_str(ctx, str);
6368 aff = isl_aff_gist(aff, set);
6370 aff = isl_aff_add_constant_si(aff, -16);
6371 zero = isl_aff_plain_is_zero(aff);
6372 isl_aff_free(aff);
6374 if (zero < 0)
6375 return -1;
6376 if (!zero)
6377 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6379 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
6380 aff = isl_aff_scale_down_ui(aff, 64);
6381 aff = isl_aff_floor(aff);
6382 equal = aff_check_plain_equal(aff, "{ [-1] }");
6383 isl_aff_free(aff);
6384 if (equal < 0)
6385 return -1;
6387 if (test_aff_set_tuple_id(ctx) < 0)
6388 return -1;
6390 return 0;
6393 /* Check that "pa" consists of a single expression.
6395 static int check_single_piece(isl_ctx *ctx, __isl_take isl_pw_aff *pa)
6397 int n;
6399 n = isl_pw_aff_n_piece(pa);
6400 isl_pw_aff_free(pa);
6402 if (!pa)
6403 return -1;
6404 if (n != 1)
6405 isl_die(ctx, isl_error_unknown, "expecting single expression",
6406 return -1);
6408 return 0;
6411 /* Check that the computation below results in a single expression.
6412 * One or two expressions may result depending on which constraint
6413 * ends up being considered as redundant with respect to the other
6414 * constraints after the projection that is performed internally
6415 * by isl_set_dim_min.
6417 static int test_dim_max_1(isl_ctx *ctx)
6419 const char *str;
6420 isl_set *set;
6421 isl_pw_aff *pa;
6423 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
6424 "-4a <= b <= 3 and b < n - 4a }";
6425 set = isl_set_read_from_str(ctx, str);
6426 pa = isl_set_dim_min(set, 0);
6427 return check_single_piece(ctx, pa);
6430 /* Check that the computation below results in a single expression.
6431 * The PIP problem corresponding to these constraints has a row
6432 * that causes a split of the solution domain. The solver should
6433 * first pick rows that split off empty parts such that the actual
6434 * solution domain does not get split.
6435 * Note that the description contains some redundant constraints.
6436 * If these constraints get removed first, then the row mentioned
6437 * above does not appear in the PIP problem.
6439 static int test_dim_max_2(isl_ctx *ctx)
6441 const char *str;
6442 isl_set *set;
6443 isl_pw_aff *pa;
6445 str = "[P, N] -> { [a] : a < N and a >= 0 and N > P and a <= P and "
6446 "N > 0 and P >= 0 }";
6447 set = isl_set_read_from_str(ctx, str);
6448 pa = isl_set_dim_max(set, 0);
6449 return check_single_piece(ctx, pa);
6452 int test_dim_max(isl_ctx *ctx)
6454 int equal;
6455 const char *str;
6456 isl_set *set1, *set2;
6457 isl_set *set;
6458 isl_map *map;
6459 isl_pw_aff *pwaff;
6461 if (test_dim_max_1(ctx) < 0)
6462 return -1;
6463 if (test_dim_max_2(ctx) < 0)
6464 return -1;
6466 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
6467 set = isl_set_read_from_str(ctx, str);
6468 pwaff = isl_set_dim_max(set, 0);
6469 set1 = isl_set_from_pw_aff(pwaff);
6470 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
6471 set2 = isl_set_read_from_str(ctx, str);
6472 equal = isl_set_is_equal(set1, set2);
6473 isl_set_free(set1);
6474 isl_set_free(set2);
6475 if (equal < 0)
6476 return -1;
6477 if (!equal)
6478 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6480 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
6481 set = isl_set_read_from_str(ctx, str);
6482 pwaff = isl_set_dim_max(set, 0);
6483 set1 = isl_set_from_pw_aff(pwaff);
6484 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
6485 set2 = isl_set_read_from_str(ctx, str);
6486 equal = isl_set_is_equal(set1, set2);
6487 isl_set_free(set1);
6488 isl_set_free(set2);
6489 if (equal < 0)
6490 return -1;
6491 if (!equal)
6492 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6494 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
6495 set = isl_set_read_from_str(ctx, str);
6496 pwaff = isl_set_dim_max(set, 0);
6497 set1 = isl_set_from_pw_aff(pwaff);
6498 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
6499 set2 = isl_set_read_from_str(ctx, str);
6500 equal = isl_set_is_equal(set1, set2);
6501 isl_set_free(set1);
6502 isl_set_free(set2);
6503 if (equal < 0)
6504 return -1;
6505 if (!equal)
6506 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6508 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
6509 "0 <= i < N and 0 <= j < M }";
6510 map = isl_map_read_from_str(ctx, str);
6511 set = isl_map_range(map);
6513 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
6514 set1 = isl_set_from_pw_aff(pwaff);
6515 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
6516 set2 = isl_set_read_from_str(ctx, str);
6517 equal = isl_set_is_equal(set1, set2);
6518 isl_set_free(set1);
6519 isl_set_free(set2);
6521 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
6522 set1 = isl_set_from_pw_aff(pwaff);
6523 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
6524 set2 = isl_set_read_from_str(ctx, str);
6525 if (equal >= 0 && equal)
6526 equal = isl_set_is_equal(set1, set2);
6527 isl_set_free(set1);
6528 isl_set_free(set2);
6530 isl_set_free(set);
6532 if (equal < 0)
6533 return -1;
6534 if (!equal)
6535 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6537 /* Check that solutions are properly merged. */
6538 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
6539 "c <= -1 + n - 4a - 2b and c >= -2b and "
6540 "4a >= -4 + n and c >= 0 }";
6541 set = isl_set_read_from_str(ctx, str);
6542 pwaff = isl_set_dim_min(set, 2);
6543 set1 = isl_set_from_pw_aff(pwaff);
6544 str = "[n] -> { [(0)] : n >= 1 }";
6545 set2 = isl_set_read_from_str(ctx, str);
6546 equal = isl_set_is_equal(set1, set2);
6547 isl_set_free(set1);
6548 isl_set_free(set2);
6550 if (equal < 0)
6551 return -1;
6552 if (!equal)
6553 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6555 /* Check that empty solution lie in the right space. */
6556 str = "[n] -> { [t,a] : 1 = 0 }";
6557 set = isl_set_read_from_str(ctx, str);
6558 pwaff = isl_set_dim_max(set, 0);
6559 set1 = isl_set_from_pw_aff(pwaff);
6560 str = "[n] -> { [t] : 1 = 0 }";
6561 set2 = isl_set_read_from_str(ctx, str);
6562 equal = isl_set_is_equal(set1, set2);
6563 isl_set_free(set1);
6564 isl_set_free(set2);
6566 if (equal < 0)
6567 return -1;
6568 if (!equal)
6569 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6571 return 0;
6574 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
6576 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
6577 const char *str)
6579 isl_ctx *ctx;
6580 isl_pw_multi_aff *pma2;
6581 int equal;
6583 if (!pma)
6584 return -1;
6586 ctx = isl_pw_multi_aff_get_ctx(pma);
6587 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6588 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
6589 isl_pw_multi_aff_free(pma2);
6591 return equal;
6594 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
6595 * represented by "str".
6597 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
6598 const char *str)
6600 int equal;
6602 equal = pw_multi_aff_plain_is_equal(pma, str);
6603 if (equal < 0)
6604 return -1;
6605 if (!equal)
6606 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
6607 "result not as expected", return -1);
6608 return 0;
6611 /* Basic test for isl_pw_multi_aff_product.
6613 * Check that multiple pieces are properly handled.
6615 static int test_product_pma(isl_ctx *ctx)
6617 int equal;
6618 const char *str;
6619 isl_pw_multi_aff *pma1, *pma2;
6621 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
6622 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
6623 str = "{ C[] -> D[] }";
6624 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6625 pma1 = isl_pw_multi_aff_product(pma1, pma2);
6626 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
6627 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
6628 equal = pw_multi_aff_check_plain_equal(pma1, str);
6629 isl_pw_multi_aff_free(pma1);
6630 if (equal < 0)
6631 return -1;
6633 return 0;
6636 int test_product(isl_ctx *ctx)
6638 const char *str;
6639 isl_set *set;
6640 isl_union_set *uset1, *uset2;
6641 int ok;
6643 str = "{ A[i] }";
6644 set = isl_set_read_from_str(ctx, str);
6645 set = isl_set_product(set, isl_set_copy(set));
6646 ok = isl_set_is_wrapping(set);
6647 isl_set_free(set);
6648 if (ok < 0)
6649 return -1;
6650 if (!ok)
6651 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6653 str = "{ [] }";
6654 uset1 = isl_union_set_read_from_str(ctx, str);
6655 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
6656 str = "{ [[] -> []] }";
6657 uset2 = isl_union_set_read_from_str(ctx, str);
6658 ok = isl_union_set_is_equal(uset1, uset2);
6659 isl_union_set_free(uset1);
6660 isl_union_set_free(uset2);
6661 if (ok < 0)
6662 return -1;
6663 if (!ok)
6664 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6666 if (test_product_pma(ctx) < 0)
6667 return -1;
6669 return 0;
6672 /* Check that two sets are not considered disjoint just because
6673 * they have a different set of (named) parameters.
6675 static int test_disjoint(isl_ctx *ctx)
6677 const char *str;
6678 isl_set *set, *set2;
6679 int disjoint;
6681 str = "[n] -> { [[]->[]] }";
6682 set = isl_set_read_from_str(ctx, str);
6683 str = "{ [[]->[]] }";
6684 set2 = isl_set_read_from_str(ctx, str);
6685 disjoint = isl_set_is_disjoint(set, set2);
6686 isl_set_free(set);
6687 isl_set_free(set2);
6688 if (disjoint < 0)
6689 return -1;
6690 if (disjoint)
6691 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6693 return 0;
6696 /* Inputs for isl_pw_multi_aff_is_equal tests.
6697 * "f1" and "f2" are the two function that need to be compared.
6698 * "equal" is the expected result.
6700 struct {
6701 int equal;
6702 const char *f1;
6703 const char *f2;
6704 } pma_equal_tests[] = {
6705 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
6706 "[N] -> { [0] : 0 <= N <= 1 }" },
6707 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
6708 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
6709 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
6710 "[N] -> { [0] : 0 <= N <= 1 }" },
6711 { 0, "{ [NaN] }", "{ [NaN] }" },
6714 int test_equal(isl_ctx *ctx)
6716 int i;
6717 const char *str;
6718 isl_set *set, *set2;
6719 int equal;
6721 str = "{ S_6[i] }";
6722 set = isl_set_read_from_str(ctx, str);
6723 str = "{ S_7[i] }";
6724 set2 = isl_set_read_from_str(ctx, str);
6725 equal = isl_set_is_equal(set, set2);
6726 isl_set_free(set);
6727 isl_set_free(set2);
6728 if (equal < 0)
6729 return -1;
6730 if (equal)
6731 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6733 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
6734 int expected = pma_equal_tests[i].equal;
6735 isl_pw_multi_aff *f1, *f2;
6737 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
6738 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
6739 equal = isl_pw_multi_aff_is_equal(f1, f2);
6740 isl_pw_multi_aff_free(f1);
6741 isl_pw_multi_aff_free(f2);
6742 if (equal < 0)
6743 return -1;
6744 if (equal != expected)
6745 isl_die(ctx, isl_error_unknown,
6746 "unexpected equality result", return -1);
6749 return 0;
6752 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
6753 enum isl_dim_type type, unsigned pos, int fixed)
6755 isl_bool test;
6757 test = isl_map_plain_is_fixed(map, type, pos, NULL);
6758 isl_map_free(map);
6759 if (test < 0)
6760 return -1;
6761 if (test == fixed)
6762 return 0;
6763 if (fixed)
6764 isl_die(ctx, isl_error_unknown,
6765 "map not detected as fixed", return -1);
6766 else
6767 isl_die(ctx, isl_error_unknown,
6768 "map detected as fixed", return -1);
6771 int test_fixed(isl_ctx *ctx)
6773 const char *str;
6774 isl_map *map;
6776 str = "{ [i] -> [i] }";
6777 map = isl_map_read_from_str(ctx, str);
6778 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
6779 return -1;
6780 str = "{ [i] -> [1] }";
6781 map = isl_map_read_from_str(ctx, str);
6782 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6783 return -1;
6784 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
6785 map = isl_map_read_from_str(ctx, str);
6786 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6787 return -1;
6788 map = isl_map_read_from_str(ctx, str);
6789 map = isl_map_neg(map);
6790 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6791 return -1;
6793 return 0;
6796 struct isl_vertices_test_data {
6797 const char *set;
6798 int n;
6799 const char *vertex[6];
6800 } vertices_tests[] = {
6801 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
6802 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
6803 { "{ A[t, i] : t = 14 and i = 1 }",
6804 1, { "{ A[14, 1] }" } },
6805 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
6806 "c <= m and m <= n and m > 0 }",
6807 6, {
6808 "[n, m] -> { [n, m, m] : 0 < m <= n }",
6809 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
6810 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
6811 "[n, m] -> { [m, m, m] : 0 < m <= n }",
6812 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
6813 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
6814 } },
6817 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
6819 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
6821 struct isl_vertices_test_data *data = user;
6822 isl_ctx *ctx;
6823 isl_multi_aff *ma;
6824 isl_basic_set *bset;
6825 isl_pw_multi_aff *pma;
6826 int i;
6827 isl_bool equal;
6829 ctx = isl_vertex_get_ctx(vertex);
6830 bset = isl_vertex_get_domain(vertex);
6831 ma = isl_vertex_get_expr(vertex);
6832 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
6834 for (i = 0; i < data->n; ++i) {
6835 isl_pw_multi_aff *pma_i;
6837 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
6838 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
6839 isl_pw_multi_aff_free(pma_i);
6841 if (equal < 0 || equal)
6842 break;
6845 isl_pw_multi_aff_free(pma);
6846 isl_vertex_free(vertex);
6848 if (equal < 0)
6849 return isl_stat_error;
6851 return equal ? isl_stat_ok : isl_stat_error;
6854 int test_vertices(isl_ctx *ctx)
6856 int i;
6858 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
6859 isl_basic_set *bset;
6860 isl_vertices *vertices;
6861 int ok = 1;
6862 int n;
6864 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
6865 vertices = isl_basic_set_compute_vertices(bset);
6866 n = isl_vertices_get_n_vertices(vertices);
6867 if (vertices_tests[i].n != n)
6868 ok = 0;
6869 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
6870 &vertices_tests[i]) < 0)
6871 ok = 0;
6872 isl_vertices_free(vertices);
6873 isl_basic_set_free(bset);
6875 if (!vertices)
6876 return -1;
6877 if (!ok)
6878 isl_die(ctx, isl_error_unknown, "unexpected vertices",
6879 return -1);
6882 return 0;
6885 int test_union_pw(isl_ctx *ctx)
6887 int equal;
6888 const char *str;
6889 isl_union_set *uset;
6890 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
6892 str = "{ [x] -> x^2 }";
6893 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
6894 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
6895 uset = isl_union_pw_qpolynomial_domain(upwqp1);
6896 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
6897 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
6898 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
6899 isl_union_pw_qpolynomial_free(upwqp1);
6900 isl_union_pw_qpolynomial_free(upwqp2);
6901 if (equal < 0)
6902 return -1;
6903 if (!equal)
6904 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6906 return 0;
6909 /* Inputs for basic tests of functions that select
6910 * subparts of the domain of an isl_multi_union_pw_aff.
6911 * "fn" is the function that is tested.
6912 * "arg" is a string description of the input.
6913 * "res" is a string description of the expected result.
6915 struct {
6916 __isl_give isl_union_set *(*fn)(
6917 __isl_take isl_multi_union_pw_aff *mupa);
6918 const char *arg;
6919 const char *res;
6920 } un_locus_tests[] = {
6921 { &isl_multi_union_pw_aff_zero_union_set,
6922 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6923 "{ A[0,j]; B[0,j] }" },
6924 { &isl_multi_union_pw_aff_zero_union_set,
6925 "F[{ A[i,j] -> [i-j]; B[i,j] -> [i-j] : i >= 0 }]",
6926 "{ A[i,i]; B[i,i] : i >= 0 }" },
6927 { &isl_multi_union_pw_aff_zero_union_set,
6928 "(F[] : { A[i,j]; B[i,i] : i >= 0 })",
6929 "{ A[i,j]; B[i,i] : i >= 0 }" },
6932 /* Perform some basic tests of functions that select
6933 * subparts of the domain of an isl_multi_union_pw_aff.
6935 static int test_un_locus(isl_ctx *ctx)
6937 int i;
6938 isl_bool ok;
6939 isl_union_set *uset, *res;
6940 isl_multi_union_pw_aff *mupa;
6942 for (i = 0; i < ARRAY_SIZE(un_locus_tests); ++i) {
6943 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6944 un_locus_tests[i].arg);
6945 res = isl_union_set_read_from_str(ctx, un_locus_tests[i].res);
6946 uset = un_locus_tests[i].fn(mupa);
6947 ok = isl_union_set_is_equal(uset, res);
6948 isl_union_set_free(uset);
6949 isl_union_set_free(res);
6950 if (ok < 0)
6951 return -1;
6952 if (!ok)
6953 isl_die(ctx, isl_error_unknown,
6954 "unexpected result", return -1);
6957 return 0;
6960 /* Inputs for basic tests of functions that select
6961 * subparts of an isl_union_map based on a relation
6962 * specified by an isl_multi_union_pw_aff.
6963 * "fn" is the function that is tested.
6964 * "arg1" and "arg2" are string descriptions of the inputs.
6965 * "res" is a string description of the expected result.
6967 struct {
6968 __isl_give isl_union_map *(*fn)(
6969 __isl_take isl_union_map *umap,
6970 __isl_take isl_multi_union_pw_aff *mupa);
6971 const char *arg1;
6972 const char *arg2;
6973 const char *res;
6974 } bin_locus_tests[] = {
6975 { &isl_union_map_eq_at_multi_union_pw_aff,
6976 "{ A[i,j] -> B[i',j'] }",
6977 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6978 "{ A[i,j] -> B[i,j'] }" },
6979 { &isl_union_map_eq_at_multi_union_pw_aff,
6980 "{ A[i,j] -> B[i',j'] }",
6981 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6982 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6983 "{ A[i,j] -> B[i,j] }" },
6984 { &isl_union_map_eq_at_multi_union_pw_aff,
6985 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6986 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6987 "{ A[i,j] -> B[i,j'] }" },
6988 { &isl_union_map_eq_at_multi_union_pw_aff,
6989 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6990 "F[{ A[i,j] -> [i]; B[i,j] -> [i]; C[i,j] -> [0] }]",
6991 "{ A[i,j] -> B[i,j']; A[0,j] -> C[i',j'] }" },
6992 { &isl_union_map_eq_at_multi_union_pw_aff,
6993 "{ A[i,j] -> B[i',j'] }",
6994 "F[{ A[i,j] -> [i] : i > j; B[i,j] -> [i] }]",
6995 "{ A[i,j] -> B[i,j'] : i > j }" },
6996 { &isl_union_map_lex_lt_at_multi_union_pw_aff,
6997 "{ A[i,j] -> B[i',j'] }",
6998 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6999 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
7000 "{ A[i,j] -> B[i',j'] : i,j << i',j' }" },
7001 { &isl_union_map_lex_gt_at_multi_union_pw_aff,
7002 "{ A[i,j] -> B[i',j'] }",
7003 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
7004 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
7005 "{ A[i,j] -> B[i',j'] : i,j >> i',j' }" },
7006 { &isl_union_map_eq_at_multi_union_pw_aff,
7007 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
7008 "(F[] : { A[i,j]; B[i,j] })",
7009 "{ A[i,j] -> B[i',j'] }" },
7010 { &isl_union_map_eq_at_multi_union_pw_aff,
7011 "{ A[i,j] -> B[i',j'] }",
7012 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
7013 "{ A[i,j] -> B[i',j'] : i > j and i' < j' }" },
7014 { &isl_union_map_eq_at_multi_union_pw_aff,
7015 "[N] -> { A[i,j] -> B[i',j'] : i,i' <= N }",
7016 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
7017 "[N] -> { A[i,j] -> B[i',j'] : i > j and i' < j' and i,i' <= N }" },
7018 { &isl_union_map_eq_at_multi_union_pw_aff,
7019 "{ A[i,j] -> B[i',j'] }",
7020 "[N] -> (F[] : { A[i,j] : i < N; B[i,j] : i < N })",
7021 "[N] -> { A[i,j] -> B[i',j'] : i,i' < N }" },
7022 { &isl_union_map_eq_at_multi_union_pw_aff,
7023 "{ A[i,j] -> B[i',j'] }",
7024 "[N] -> (F[] : { : N >= 0 })",
7025 "[N] -> { A[i,j] -> B[i',j'] : N >= 0 }" },
7028 /* Perform some basic tests of functions that select
7029 * subparts of an isl_union_map based on a relation
7030 * specified by an isl_multi_union_pw_aff.
7032 static int test_bin_locus(isl_ctx *ctx)
7034 int i;
7035 isl_bool ok;
7036 isl_union_map *umap, *res;
7037 isl_multi_union_pw_aff *mupa;
7039 for (i = 0; i < ARRAY_SIZE(bin_locus_tests); ++i) {
7040 umap = isl_union_map_read_from_str(ctx,
7041 bin_locus_tests[i].arg1);
7042 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
7043 bin_locus_tests[i].arg2);
7044 res = isl_union_map_read_from_str(ctx, bin_locus_tests[i].res);
7045 umap = bin_locus_tests[i].fn(umap, mupa);
7046 ok = isl_union_map_is_equal(umap, res);
7047 isl_union_map_free(umap);
7048 isl_union_map_free(res);
7049 if (ok < 0)
7050 return -1;
7051 if (!ok)
7052 isl_die(ctx, isl_error_unknown,
7053 "unexpected result", return -1);
7056 return 0;
7059 /* Perform basic locus tests.
7061 static int test_locus(isl_ctx *ctx)
7063 if (test_un_locus(ctx) < 0)
7064 return -1;
7065 if (test_bin_locus(ctx) < 0)
7066 return -1;
7067 return 0;
7070 /* Test that isl_union_pw_qpolynomial_eval picks up the function
7071 * defined over the correct domain space.
7073 static int test_eval_1(isl_ctx *ctx)
7075 const char *str;
7076 isl_point *pnt;
7077 isl_set *set;
7078 isl_union_pw_qpolynomial *upwqp;
7079 isl_val *v;
7080 int cmp;
7082 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
7083 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
7084 str = "{ A[6] }";
7085 set = isl_set_read_from_str(ctx, str);
7086 pnt = isl_set_sample_point(set);
7087 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
7088 cmp = isl_val_cmp_si(v, 36);
7089 isl_val_free(v);
7091 if (!v)
7092 return -1;
7093 if (cmp != 0)
7094 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
7096 return 0;
7099 /* Check that isl_qpolynomial_eval handles getting called on a void point.
7101 static int test_eval_2(isl_ctx *ctx)
7103 const char *str;
7104 isl_point *pnt;
7105 isl_set *set;
7106 isl_qpolynomial *qp;
7107 isl_val *v;
7108 isl_bool ok;
7110 str = "{ A[x] -> [x] }";
7111 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
7112 str = "{ A[x] : false }";
7113 set = isl_set_read_from_str(ctx, str);
7114 pnt = isl_set_sample_point(set);
7115 v = isl_qpolynomial_eval(qp, pnt);
7116 ok = isl_val_is_nan(v);
7117 isl_val_free(v);
7119 if (ok < 0)
7120 return -1;
7121 if (!ok)
7122 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
7124 return 0;
7127 /* Inputs for isl_pw_aff_eval test.
7128 * "f" is the affine function.
7129 * "p" is the point where the function should be evaluated.
7130 * "res" is the expected result.
7132 struct {
7133 const char *f;
7134 const char *p;
7135 const char *res;
7136 } aff_eval_tests[] = {
7137 { "{ [i] -> [2 * i] }", "{ [4] }", "8" },
7138 { "{ [i] -> [2 * i] }", "{ [x] : false }", "NaN" },
7139 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [0] }", "0" },
7140 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [1] }", "1" },
7141 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [2] }", "3" },
7142 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [3] }", "5" },
7143 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [4] }", "7" },
7144 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [0] }", "0" },
7145 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [1] }", "0" },
7146 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [2] }", "0" },
7147 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [3] }", "0" },
7148 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [4] }", "1" },
7149 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [6] }", "1" },
7150 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [8] }", "2" },
7151 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [4] }", "4" },
7152 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [-2] }", "2" },
7153 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [0] }", "NaN" },
7154 { "[N] -> { [2 * N] }", "[N] -> { : N = 4 }", "8" },
7155 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 1] }", "1" },
7156 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 2] }", "3/2" },
7157 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [4] }", "4" },
7158 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [3] }", "NaN" },
7159 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [x] : false }", "NaN" },
7162 /* Perform basic isl_pw_aff_eval tests.
7164 static int test_eval_aff(isl_ctx *ctx)
7166 int i;
7168 for (i = 0; i < ARRAY_SIZE(aff_eval_tests); ++i) {
7169 isl_stat r;
7170 isl_pw_aff *pa;
7171 isl_set *set;
7172 isl_point *pnt;
7173 isl_val *v;
7175 pa = isl_pw_aff_read_from_str(ctx, aff_eval_tests[i].f);
7176 set = isl_set_read_from_str(ctx, aff_eval_tests[i].p);
7177 pnt = isl_set_sample_point(set);
7178 v = isl_pw_aff_eval(pa, pnt);
7179 r = val_check_equal(v, aff_eval_tests[i].res);
7180 isl_val_free(v);
7181 if (r < 0)
7182 return -1;
7184 return 0;
7187 /* Perform basic evaluation tests.
7189 static int test_eval(isl_ctx *ctx)
7191 if (test_eval_1(ctx) < 0)
7192 return -1;
7193 if (test_eval_2(ctx) < 0)
7194 return -1;
7195 if (test_eval_aff(ctx) < 0)
7196 return -1;
7197 return 0;
7200 /* Descriptions of sets that are tested for reparsing after printing.
7202 const char *output_tests[] = {
7203 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
7204 "{ [x] : 1 = 0 }",
7205 "{ [x] : false }",
7206 "{ [x] : x mod 2 = 0 }",
7207 "{ [x] : x mod 2 = 1 }",
7208 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) < x }",
7209 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) < x }",
7210 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7211 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7212 "[n] -> { [y, x] : 2*((x + 2y) mod 3) = n }",
7213 "{ [x, y] : (2*floor(x/3) + 3*floor(y/4)) mod 5 = x }",
7216 /* Check that printing a set and reparsing a set from the printed output
7217 * results in the same set.
7219 static int test_output_set(isl_ctx *ctx)
7221 int i;
7222 char *str;
7223 isl_set *set1, *set2;
7224 isl_bool equal;
7226 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
7227 set1 = isl_set_read_from_str(ctx, output_tests[i]);
7228 str = isl_set_to_str(set1);
7229 set2 = isl_set_read_from_str(ctx, str);
7230 free(str);
7231 equal = isl_set_is_equal(set1, set2);
7232 isl_set_free(set1);
7233 isl_set_free(set2);
7234 if (equal < 0)
7235 return -1;
7236 if (!equal)
7237 isl_die(ctx, isl_error_unknown,
7238 "parsed output not the same", return -1);
7241 return 0;
7244 /* Check that an isl_multi_aff is printed using a consistent space.
7246 static isl_stat test_output_ma(isl_ctx *ctx)
7248 char *str;
7249 isl_bool equal;
7250 isl_aff *aff;
7251 isl_multi_aff *ma, *ma2;
7253 ma = isl_multi_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7254 aff = isl_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7255 ma = isl_multi_aff_set_aff(ma, 0, aff);
7256 str = isl_multi_aff_to_str(ma);
7257 ma2 = isl_multi_aff_read_from_str(ctx, str);
7258 free(str);
7259 equal = isl_multi_aff_plain_is_equal(ma, ma2);
7260 isl_multi_aff_free(ma2);
7261 isl_multi_aff_free(ma);
7263 if (equal < 0)
7264 return isl_stat_error;
7265 if (!equal)
7266 isl_die(ctx, isl_error_unknown, "bad conversion",
7267 return isl_stat_error);
7269 return isl_stat_ok;
7272 /* Check that an isl_multi_pw_aff is printed using a consistent space.
7274 static isl_stat test_output_mpa(isl_ctx *ctx)
7276 char *str;
7277 isl_bool equal;
7278 isl_pw_aff *pa;
7279 isl_multi_pw_aff *mpa, *mpa2;
7281 mpa = isl_multi_pw_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7282 pa = isl_pw_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7283 mpa = isl_multi_pw_aff_set_pw_aff(mpa, 0, pa);
7284 str = isl_multi_pw_aff_to_str(mpa);
7285 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
7286 free(str);
7287 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
7288 isl_multi_pw_aff_free(mpa2);
7289 isl_multi_pw_aff_free(mpa);
7291 if (equal < 0)
7292 return isl_stat_error;
7293 if (!equal)
7294 isl_die(ctx, isl_error_unknown, "bad conversion",
7295 return isl_stat_error);
7297 return isl_stat_ok;
7300 int test_output(isl_ctx *ctx)
7302 char *s;
7303 const char *str;
7304 isl_pw_aff *pa;
7305 isl_printer *p;
7306 int equal;
7308 if (test_output_set(ctx) < 0)
7309 return -1;
7310 if (test_output_ma(ctx) < 0)
7311 return -1;
7312 if (test_output_mpa(ctx) < 0)
7313 return -1;
7315 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
7316 pa = isl_pw_aff_read_from_str(ctx, str);
7318 p = isl_printer_to_str(ctx);
7319 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
7320 p = isl_printer_print_pw_aff(p, pa);
7321 s = isl_printer_get_str(p);
7322 isl_printer_free(p);
7323 isl_pw_aff_free(pa);
7324 if (!s)
7325 equal = -1;
7326 else
7327 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
7328 free(s);
7329 if (equal < 0)
7330 return -1;
7331 if (!equal)
7332 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7334 return 0;
7337 int test_sample(isl_ctx *ctx)
7339 const char *str;
7340 isl_basic_set *bset1, *bset2;
7341 int empty, subset;
7343 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
7344 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
7345 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
7346 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
7347 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
7348 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
7349 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
7350 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
7351 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
7352 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
7353 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
7354 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
7355 "d - 1073741821e and "
7356 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
7357 "3j >= 1 - a + b + 2e and "
7358 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
7359 "3i <= 4 - a + 4b - e and "
7360 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
7361 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
7362 "c <= -1 + a and 3i >= -2 - a + 3e and "
7363 "1073741822e <= 1073741823 - a + 1073741822b + c and "
7364 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
7365 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
7366 "1073741823e >= 1 + 1073741823b - d and "
7367 "3i >= 1073741823b + c - 1073741823e - f and "
7368 "3i >= 1 + 2b + e + 3g }";
7369 bset1 = isl_basic_set_read_from_str(ctx, str);
7370 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
7371 empty = isl_basic_set_is_empty(bset2);
7372 subset = isl_basic_set_is_subset(bset2, bset1);
7373 isl_basic_set_free(bset1);
7374 isl_basic_set_free(bset2);
7375 if (empty < 0 || subset < 0)
7376 return -1;
7377 if (empty)
7378 isl_die(ctx, isl_error_unknown, "point not found", return -1);
7379 if (!subset)
7380 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
7382 return 0;
7385 int test_fixed_power(isl_ctx *ctx)
7387 const char *str;
7388 isl_map *map;
7389 isl_val *exp;
7390 int equal;
7392 str = "{ [i] -> [i + 1] }";
7393 map = isl_map_read_from_str(ctx, str);
7394 exp = isl_val_int_from_si(ctx, 23);
7395 map = isl_map_fixed_power_val(map, exp);
7396 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
7397 isl_map_free(map);
7398 if (equal < 0)
7399 return -1;
7401 return 0;
7404 int test_slice(isl_ctx *ctx)
7406 const char *str;
7407 isl_map *map;
7408 int equal;
7410 str = "{ [i] -> [j] }";
7411 map = isl_map_read_from_str(ctx, str);
7412 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
7413 equal = map_check_equal(map, "{ [i] -> [i] }");
7414 isl_map_free(map);
7415 if (equal < 0)
7416 return -1;
7418 str = "{ [i] -> [j] }";
7419 map = isl_map_read_from_str(ctx, str);
7420 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
7421 equal = map_check_equal(map, "{ [i] -> [j] }");
7422 isl_map_free(map);
7423 if (equal < 0)
7424 return -1;
7426 str = "{ [i] -> [j] }";
7427 map = isl_map_read_from_str(ctx, str);
7428 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
7429 equal = map_check_equal(map, "{ [i] -> [-i] }");
7430 isl_map_free(map);
7431 if (equal < 0)
7432 return -1;
7434 str = "{ [i] -> [j] }";
7435 map = isl_map_read_from_str(ctx, str);
7436 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
7437 equal = map_check_equal(map, "{ [0] -> [j] }");
7438 isl_map_free(map);
7439 if (equal < 0)
7440 return -1;
7442 str = "{ [i] -> [j] }";
7443 map = isl_map_read_from_str(ctx, str);
7444 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
7445 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
7446 isl_map_free(map);
7447 if (equal < 0)
7448 return -1;
7450 str = "{ [i] -> [j] }";
7451 map = isl_map_read_from_str(ctx, str);
7452 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
7453 equal = map_check_equal(map, "{ [i] -> [j] : false }");
7454 isl_map_free(map);
7455 if (equal < 0)
7456 return -1;
7458 return 0;
7461 int test_eliminate(isl_ctx *ctx)
7463 const char *str;
7464 isl_map *map;
7465 int equal;
7467 str = "{ [i] -> [j] : i = 2j }";
7468 map = isl_map_read_from_str(ctx, str);
7469 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
7470 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
7471 isl_map_free(map);
7472 if (equal < 0)
7473 return -1;
7475 return 0;
7478 /* Check that isl_set_dim_residue_class detects that the values of j
7479 * in the set below are all odd and that it does not detect any spurious
7480 * strides.
7482 static int test_residue_class(isl_ctx *ctx)
7484 const char *str;
7485 isl_set *set;
7486 isl_int m, r;
7487 isl_stat res;
7489 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
7490 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
7491 set = isl_set_read_from_str(ctx, str);
7492 isl_int_init(m);
7493 isl_int_init(r);
7494 res = isl_set_dim_residue_class(set, 1, &m, &r);
7495 if (res >= 0 &&
7496 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
7497 isl_die(ctx, isl_error_unknown, "incorrect residue class",
7498 res = isl_stat_error);
7499 isl_int_clear(r);
7500 isl_int_clear(m);
7501 isl_set_free(set);
7503 return res;
7506 static int test_align_parameters_1(isl_ctx *ctx)
7508 const char *str;
7509 isl_space *space;
7510 isl_multi_aff *ma1, *ma2;
7511 int equal;
7513 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
7514 ma1 = isl_multi_aff_read_from_str(ctx, str);
7516 space = isl_space_params_alloc(ctx, 1);
7517 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
7518 ma1 = isl_multi_aff_align_params(ma1, space);
7520 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
7521 ma2 = isl_multi_aff_read_from_str(ctx, str);
7523 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
7525 isl_multi_aff_free(ma1);
7526 isl_multi_aff_free(ma2);
7528 if (equal < 0)
7529 return -1;
7530 if (!equal)
7531 isl_die(ctx, isl_error_unknown,
7532 "result not as expected", return -1);
7534 return 0;
7537 /* Check the isl_multi_*_from_*_list operation in case inputs
7538 * have unaligned parameters.
7539 * In particular, older versions of isl would simply fail
7540 * (without printing any error message).
7542 static isl_stat test_align_parameters_2(isl_ctx *ctx)
7544 isl_space *space;
7545 isl_map *map;
7546 isl_aff *aff;
7547 isl_multi_aff *ma;
7549 map = isl_map_read_from_str(ctx, "{ A[] -> M[x] }");
7550 space = isl_map_get_space(map);
7551 isl_map_free(map);
7553 aff = isl_aff_read_from_str(ctx, "[N] -> { A[] -> [N] }");
7554 ma = isl_multi_aff_from_aff_list(space, isl_aff_list_from_aff(aff));
7555 isl_multi_aff_free(ma);
7557 if (!ma)
7558 return isl_stat_error;
7559 return isl_stat_ok;
7562 /* Perform basic parameter alignment tests.
7564 static int test_align_parameters(isl_ctx *ctx)
7566 if (test_align_parameters_1(ctx) < 0)
7567 return -1;
7568 if (test_align_parameters_2(ctx) < 0)
7569 return -1;
7571 return 0;
7574 /* Check that isl_*_drop_unused_params actually drops the unused parameters
7575 * by comparing the result using isl_*_plain_is_equal.
7576 * Note that this assumes that isl_*_plain_is_equal does not consider
7577 * objects that only differ by unused parameters to be equal.
7579 int test_drop_unused_parameters(isl_ctx *ctx)
7581 const char *str_with, *str_without;
7582 isl_basic_set *bset1, *bset2;
7583 isl_set *set1, *set2;
7584 isl_pw_aff *pwa1, *pwa2;
7585 int equal;
7587 str_with = "[n, m, o] -> { [m] }";
7588 str_without = "[m] -> { [m] }";
7590 bset1 = isl_basic_set_read_from_str(ctx, str_with);
7591 bset2 = isl_basic_set_read_from_str(ctx, str_without);
7592 bset1 = isl_basic_set_drop_unused_params(bset1);
7593 equal = isl_basic_set_plain_is_equal(bset1, bset2);
7594 isl_basic_set_free(bset1);
7595 isl_basic_set_free(bset2);
7597 if (equal < 0)
7598 return -1;
7599 if (!equal)
7600 isl_die(ctx, isl_error_unknown,
7601 "result not as expected", return -1);
7603 set1 = isl_set_read_from_str(ctx, str_with);
7604 set2 = isl_set_read_from_str(ctx, str_without);
7605 set1 = isl_set_drop_unused_params(set1);
7606 equal = isl_set_plain_is_equal(set1, set2);
7607 isl_set_free(set1);
7608 isl_set_free(set2);
7610 if (equal < 0)
7611 return -1;
7612 if (!equal)
7613 isl_die(ctx, isl_error_unknown,
7614 "result not as expected", return -1);
7616 pwa1 = isl_pw_aff_read_from_str(ctx, str_with);
7617 pwa2 = isl_pw_aff_read_from_str(ctx, str_without);
7618 pwa1 = isl_pw_aff_drop_unused_params(pwa1);
7619 equal = isl_pw_aff_plain_is_equal(pwa1, pwa2);
7620 isl_pw_aff_free(pwa1);
7621 isl_pw_aff_free(pwa2);
7623 if (equal < 0)
7624 return -1;
7625 if (!equal)
7626 isl_die(ctx, isl_error_unknown,
7627 "result not as expected", return -1);
7629 return 0;
7632 static int test_list(isl_ctx *ctx)
7634 isl_id *a, *b, *c, *d, *id;
7635 isl_id_list *list;
7636 int ok;
7638 a = isl_id_alloc(ctx, "a", NULL);
7639 b = isl_id_alloc(ctx, "b", NULL);
7640 c = isl_id_alloc(ctx, "c", NULL);
7641 d = isl_id_alloc(ctx, "d", NULL);
7643 list = isl_id_list_alloc(ctx, 4);
7644 list = isl_id_list_add(list, b);
7645 list = isl_id_list_insert(list, 0, a);
7646 list = isl_id_list_add(list, c);
7647 list = isl_id_list_add(list, d);
7648 list = isl_id_list_drop(list, 1, 1);
7650 if (!list)
7651 return -1;
7652 if (isl_id_list_n_id(list) != 3) {
7653 isl_id_list_free(list);
7654 isl_die(ctx, isl_error_unknown,
7655 "unexpected number of elements in list", return -1);
7658 id = isl_id_list_get_id(list, 0);
7659 ok = id == a;
7660 isl_id_free(id);
7661 id = isl_id_list_get_id(list, 1);
7662 ok = ok && id == c;
7663 isl_id_free(id);
7664 id = isl_id_list_get_id(list, 2);
7665 ok = ok && id == d;
7666 isl_id_free(id);
7668 isl_id_list_free(list);
7670 if (!ok)
7671 isl_die(ctx, isl_error_unknown,
7672 "unexpected elements in list", return -1);
7674 return 0;
7677 const char *set_conversion_tests[] = {
7678 "[N] -> { [i] : N - 1 <= 2 i <= N }",
7679 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
7680 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
7681 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
7682 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
7683 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
7684 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
7685 "-3 + c <= d <= 28 + c) }",
7688 /* Check that converting from isl_set to isl_pw_multi_aff and back
7689 * to isl_set produces the original isl_set.
7691 static int test_set_conversion(isl_ctx *ctx)
7693 int i;
7694 const char *str;
7695 isl_set *set1, *set2;
7696 isl_pw_multi_aff *pma;
7697 int equal;
7699 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
7700 str = set_conversion_tests[i];
7701 set1 = isl_set_read_from_str(ctx, str);
7702 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
7703 set2 = isl_set_from_pw_multi_aff(pma);
7704 equal = isl_set_is_equal(set1, set2);
7705 isl_set_free(set1);
7706 isl_set_free(set2);
7708 if (equal < 0)
7709 return -1;
7710 if (!equal)
7711 isl_die(ctx, isl_error_unknown, "bad conversion",
7712 return -1);
7715 return 0;
7718 const char *conversion_tests[] = {
7719 "{ [a, b, c, d] -> s0[a, b, e, f] : "
7720 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
7721 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
7722 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
7723 "9e <= -2 - 2a) }",
7724 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
7725 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
7726 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
7729 /* Check that converting from isl_map to isl_pw_multi_aff and back
7730 * to isl_map produces the original isl_map.
7732 static int test_map_conversion(isl_ctx *ctx)
7734 int i;
7735 isl_map *map1, *map2;
7736 isl_pw_multi_aff *pma;
7737 int equal;
7739 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
7740 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
7741 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
7742 map2 = isl_map_from_pw_multi_aff(pma);
7743 equal = isl_map_is_equal(map1, map2);
7744 isl_map_free(map1);
7745 isl_map_free(map2);
7747 if (equal < 0)
7748 return -1;
7749 if (!equal)
7750 isl_die(ctx, isl_error_unknown, "bad conversion",
7751 return -1);
7754 return 0;
7757 /* Descriptions of isl_pw_multi_aff objects for testing conversion
7758 * to isl_multi_pw_aff and back.
7760 const char *mpa_conversion_tests[] = {
7761 "{ [x] -> A[x] }",
7762 "{ [x] -> A[x] : x >= 0 }",
7763 "{ [x] -> A[x] : x >= 0; [x] -> A[-x] : x < 0 }",
7764 "{ [x] -> A[x, x + 1] }",
7765 "{ [x] -> A[] }",
7766 "{ [x] -> A[] : x >= 0 }",
7769 /* Check that conversion from isl_pw_multi_aff to isl_multi_pw_aff and
7770 * back to isl_pw_multi_aff preserves the original meaning.
7772 static int test_mpa_conversion(isl_ctx *ctx)
7774 int i;
7775 isl_pw_multi_aff *pma1, *pma2;
7776 isl_multi_pw_aff *mpa;
7777 int equal;
7779 for (i = 0; i < ARRAY_SIZE(mpa_conversion_tests); ++i) {
7780 const char *str;
7781 str = mpa_conversion_tests[i];
7782 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
7783 pma2 = isl_pw_multi_aff_copy(pma1);
7784 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma1);
7785 pma1 = isl_pw_multi_aff_from_multi_pw_aff(mpa);
7786 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7787 isl_pw_multi_aff_free(pma1);
7788 isl_pw_multi_aff_free(pma2);
7790 if (equal < 0)
7791 return -1;
7792 if (!equal)
7793 isl_die(ctx, isl_error_unknown, "bad conversion",
7794 return -1);
7797 return 0;
7800 /* Descriptions of union maps that should be convertible
7801 * to an isl_multi_union_pw_aff.
7803 const char *umap_mupa_conversion_tests[] = {
7804 "{ [a, b, c, d] -> s0[a, b, e, f] : "
7805 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
7806 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
7807 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
7808 "9e <= -2 - 2a) }",
7809 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
7810 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
7811 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
7812 "{ A[] -> B[0]; C[] -> B[1] }",
7813 "{ A[] -> B[]; C[] -> B[] }",
7816 /* Check that converting from isl_union_map to isl_multi_union_pw_aff and back
7817 * to isl_union_map produces the original isl_union_map.
7819 static int test_union_map_mupa_conversion(isl_ctx *ctx)
7821 int i;
7822 isl_union_map *umap1, *umap2;
7823 isl_multi_union_pw_aff *mupa;
7824 int equal;
7826 for (i = 0; i < ARRAY_SIZE(umap_mupa_conversion_tests); ++i) {
7827 const char *str;
7828 str = umap_mupa_conversion_tests[i];
7829 umap1 = isl_union_map_read_from_str(ctx, str);
7830 umap2 = isl_union_map_copy(umap1);
7831 mupa = isl_multi_union_pw_aff_from_union_map(umap2);
7832 umap2 = isl_union_map_from_multi_union_pw_aff(mupa);
7833 equal = isl_union_map_is_equal(umap1, umap2);
7834 isl_union_map_free(umap1);
7835 isl_union_map_free(umap2);
7837 if (equal < 0)
7838 return -1;
7839 if (!equal)
7840 isl_die(ctx, isl_error_unknown, "bad conversion",
7841 return -1);
7844 return 0;
7847 static int test_conversion(isl_ctx *ctx)
7849 if (test_set_conversion(ctx) < 0)
7850 return -1;
7851 if (test_map_conversion(ctx) < 0)
7852 return -1;
7853 if (test_mpa_conversion(ctx) < 0)
7854 return -1;
7855 if (test_union_map_mupa_conversion(ctx) < 0)
7856 return -1;
7857 return 0;
7860 /* Check that isl_basic_map_curry does not modify input.
7862 static int test_curry(isl_ctx *ctx)
7864 const char *str;
7865 isl_basic_map *bmap1, *bmap2;
7866 int equal;
7868 str = "{ [A[] -> B[]] -> C[] }";
7869 bmap1 = isl_basic_map_read_from_str(ctx, str);
7870 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
7871 equal = isl_basic_map_is_equal(bmap1, bmap2);
7872 isl_basic_map_free(bmap1);
7873 isl_basic_map_free(bmap2);
7875 if (equal < 0)
7876 return -1;
7877 if (equal)
7878 isl_die(ctx, isl_error_unknown,
7879 "curried map should not be equal to original",
7880 return -1);
7882 return 0;
7885 struct {
7886 const char *set;
7887 const char *ma;
7888 const char *res;
7889 } preimage_tests[] = {
7890 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
7891 "{ A[j,i] -> B[i,j] }",
7892 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
7893 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
7894 "{ A[a,b] -> B[a/2,b/6] }",
7895 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
7896 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
7897 "{ A[a,b] -> B[a/2,b/6] }",
7898 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
7899 "exists i,j : a = 2 i and b = 6 j }" },
7900 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
7901 "[n] -> { : 0 <= n <= 100 }" },
7902 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
7903 "{ A[a] -> B[2a] }",
7904 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
7905 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
7906 "{ A[a] -> B[([a/2])] }",
7907 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
7908 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
7909 "{ A[a] -> B[a,a,a/3] }",
7910 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
7911 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
7912 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
7915 static int test_preimage_basic_set(isl_ctx *ctx)
7917 int i;
7918 isl_basic_set *bset1, *bset2;
7919 isl_multi_aff *ma;
7920 int equal;
7922 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
7923 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
7924 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
7925 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
7926 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
7927 equal = isl_basic_set_is_equal(bset1, bset2);
7928 isl_basic_set_free(bset1);
7929 isl_basic_set_free(bset2);
7930 if (equal < 0)
7931 return -1;
7932 if (!equal)
7933 isl_die(ctx, isl_error_unknown, "bad preimage",
7934 return -1);
7937 return 0;
7940 struct {
7941 const char *map;
7942 const char *ma;
7943 const char *res;
7944 } preimage_domain_tests[] = {
7945 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
7946 "{ A[j,i] -> B[i,j] }",
7947 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
7948 { "{ B[i] -> C[i]; D[i] -> E[i] }",
7949 "{ A[i] -> B[i + 1] }",
7950 "{ A[i] -> C[i + 1] }" },
7951 { "{ B[i] -> C[i]; B[i] -> E[i] }",
7952 "{ A[i] -> B[i + 1] }",
7953 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
7954 { "{ B[i] -> C[([i/2])] }",
7955 "{ A[i] -> B[2i] }",
7956 "{ A[i] -> C[i] }" },
7957 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
7958 "{ A[i] -> B[([i/5]), ([i/7])] }",
7959 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
7960 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
7961 "[N] -> { A[] -> B[([N/5])] }",
7962 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
7963 { "{ B[i] -> C[i] : exists a : i = 5 a }",
7964 "{ A[i] -> B[2i] }",
7965 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
7966 { "{ B[i] -> C[i] : exists a : i = 2 a; "
7967 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
7968 "{ A[i] -> B[2i] }",
7969 "{ A[i] -> C[2i] }" },
7970 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
7971 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
7974 static int test_preimage_union_map(isl_ctx *ctx)
7976 int i;
7977 isl_union_map *umap1, *umap2;
7978 isl_multi_aff *ma;
7979 int equal;
7981 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
7982 umap1 = isl_union_map_read_from_str(ctx,
7983 preimage_domain_tests[i].map);
7984 ma = isl_multi_aff_read_from_str(ctx,
7985 preimage_domain_tests[i].ma);
7986 umap2 = isl_union_map_read_from_str(ctx,
7987 preimage_domain_tests[i].res);
7988 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
7989 equal = isl_union_map_is_equal(umap1, umap2);
7990 isl_union_map_free(umap1);
7991 isl_union_map_free(umap2);
7992 if (equal < 0)
7993 return -1;
7994 if (!equal)
7995 isl_die(ctx, isl_error_unknown, "bad preimage",
7996 return -1);
7999 return 0;
8002 static int test_preimage(isl_ctx *ctx)
8004 if (test_preimage_basic_set(ctx) < 0)
8005 return -1;
8006 if (test_preimage_union_map(ctx) < 0)
8007 return -1;
8009 return 0;
8012 struct {
8013 const char *ma1;
8014 const char *ma;
8015 const char *res;
8016 } pullback_tests[] = {
8017 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
8018 "{ A[a,b] -> C[b + 2a] }" },
8019 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
8020 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
8021 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
8022 "{ A[a] -> C[(a)/6] }" },
8023 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
8024 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
8025 "{ A[a] -> C[(2a)/3] }" },
8026 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
8027 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
8028 "{ A[i,j] -> C[i + j, i + j] }"},
8029 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
8030 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
8031 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
8032 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
8033 "{ [i, j] -> [floor((i)/3), j] }",
8034 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
8037 static int test_pullback(isl_ctx *ctx)
8039 int i;
8040 isl_multi_aff *ma1, *ma2;
8041 isl_multi_aff *ma;
8042 int equal;
8044 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
8045 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
8046 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
8047 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
8048 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
8049 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
8050 isl_multi_aff_free(ma1);
8051 isl_multi_aff_free(ma2);
8052 if (equal < 0)
8053 return -1;
8054 if (!equal)
8055 isl_die(ctx, isl_error_unknown, "bad pullback",
8056 return -1);
8059 return 0;
8062 /* Check that negation is printed correctly and that equal expressions
8063 * are correctly identified.
8065 static int test_ast(isl_ctx *ctx)
8067 isl_ast_expr *expr, *expr1, *expr2, *expr3;
8068 char *str;
8069 int ok, equal;
8071 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
8072 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
8073 expr = isl_ast_expr_add(expr1, expr2);
8074 expr2 = isl_ast_expr_copy(expr);
8075 expr = isl_ast_expr_neg(expr);
8076 expr2 = isl_ast_expr_neg(expr2);
8077 equal = isl_ast_expr_is_equal(expr, expr2);
8078 str = isl_ast_expr_to_C_str(expr);
8079 ok = str ? !strcmp(str, "-(A + B)") : -1;
8080 free(str);
8081 isl_ast_expr_free(expr);
8082 isl_ast_expr_free(expr2);
8084 if (ok < 0 || equal < 0)
8085 return -1;
8086 if (!equal)
8087 isl_die(ctx, isl_error_unknown,
8088 "equal expressions not considered equal", return -1);
8089 if (!ok)
8090 isl_die(ctx, isl_error_unknown,
8091 "isl_ast_expr printed incorrectly", return -1);
8093 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
8094 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
8095 expr = isl_ast_expr_add(expr1, expr2);
8096 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
8097 expr = isl_ast_expr_sub(expr3, expr);
8098 str = isl_ast_expr_to_C_str(expr);
8099 ok = str ? !strcmp(str, "C - (A + B)") : -1;
8100 free(str);
8101 isl_ast_expr_free(expr);
8103 if (ok < 0)
8104 return -1;
8105 if (!ok)
8106 isl_die(ctx, isl_error_unknown,
8107 "isl_ast_expr printed incorrectly", return -1);
8109 return 0;
8112 /* Check that isl_ast_build_expr_from_set returns a valid expression
8113 * for an empty set. Note that isl_ast_build_expr_from_set getting
8114 * called on an empty set probably indicates a bug in the caller.
8116 static int test_ast_build(isl_ctx *ctx)
8118 isl_set *set;
8119 isl_ast_build *build;
8120 isl_ast_expr *expr;
8122 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8123 build = isl_ast_build_from_context(set);
8125 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
8126 expr = isl_ast_build_expr_from_set(build, set);
8128 isl_ast_expr_free(expr);
8129 isl_ast_build_free(build);
8131 if (!expr)
8132 return -1;
8134 return 0;
8137 /* Internal data structure for before_for and after_for callbacks.
8139 * depth is the current depth
8140 * before is the number of times before_for has been called
8141 * after is the number of times after_for has been called
8143 struct isl_test_codegen_data {
8144 int depth;
8145 int before;
8146 int after;
8149 /* This function is called before each for loop in the AST generated
8150 * from test_ast_gen1.
8152 * Increment the number of calls and the depth.
8153 * Check that the space returned by isl_ast_build_get_schedule_space
8154 * matches the target space of the schedule returned by
8155 * isl_ast_build_get_schedule.
8156 * Return an isl_id that is checked by the corresponding call
8157 * to after_for.
8159 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
8160 void *user)
8162 struct isl_test_codegen_data *data = user;
8163 isl_ctx *ctx;
8164 isl_space *space;
8165 isl_union_map *schedule;
8166 isl_union_set *uset;
8167 isl_set *set;
8168 int empty;
8169 char name[] = "d0";
8171 ctx = isl_ast_build_get_ctx(build);
8173 if (data->before >= 3)
8174 isl_die(ctx, isl_error_unknown,
8175 "unexpected number of for nodes", return NULL);
8176 if (data->depth >= 2)
8177 isl_die(ctx, isl_error_unknown,
8178 "unexpected depth", return NULL);
8180 snprintf(name, sizeof(name), "d%d", data->depth);
8181 data->before++;
8182 data->depth++;
8184 schedule = isl_ast_build_get_schedule(build);
8185 uset = isl_union_map_range(schedule);
8186 if (!uset)
8187 return NULL;
8188 if (isl_union_set_n_set(uset) != 1) {
8189 isl_union_set_free(uset);
8190 isl_die(ctx, isl_error_unknown,
8191 "expecting single range space", return NULL);
8194 space = isl_ast_build_get_schedule_space(build);
8195 set = isl_union_set_extract_set(uset, space);
8196 isl_union_set_free(uset);
8197 empty = isl_set_is_empty(set);
8198 isl_set_free(set);
8200 if (empty < 0)
8201 return NULL;
8202 if (empty)
8203 isl_die(ctx, isl_error_unknown,
8204 "spaces don't match", return NULL);
8206 return isl_id_alloc(ctx, name, NULL);
8209 /* This function is called after each for loop in the AST generated
8210 * from test_ast_gen1.
8212 * Increment the number of calls and decrement the depth.
8213 * Check that the annotation attached to the node matches
8214 * the isl_id returned by the corresponding call to before_for.
8216 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
8217 __isl_keep isl_ast_build *build, void *user)
8219 struct isl_test_codegen_data *data = user;
8220 isl_id *id;
8221 const char *name;
8222 int valid;
8224 data->after++;
8225 data->depth--;
8227 if (data->after > data->before)
8228 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8229 "mismatch in number of for nodes",
8230 return isl_ast_node_free(node));
8232 id = isl_ast_node_get_annotation(node);
8233 if (!id)
8234 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8235 "missing annotation", return isl_ast_node_free(node));
8237 name = isl_id_get_name(id);
8238 valid = name && atoi(name + 1) == data->depth;
8239 isl_id_free(id);
8241 if (!valid)
8242 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8243 "wrong annotation", return isl_ast_node_free(node));
8245 return node;
8248 /* Check that the before_each_for and after_each_for callbacks
8249 * are called for each for loop in the generated code,
8250 * that they are called in the right order and that the isl_id
8251 * returned from the before_each_for callback is attached to
8252 * the isl_ast_node passed to the corresponding after_each_for call.
8254 static int test_ast_gen1(isl_ctx *ctx)
8256 const char *str;
8257 isl_set *set;
8258 isl_union_map *schedule;
8259 isl_ast_build *build;
8260 isl_ast_node *tree;
8261 struct isl_test_codegen_data data;
8263 str = "[N] -> { : N >= 10 }";
8264 set = isl_set_read_from_str(ctx, str);
8265 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
8266 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
8267 schedule = isl_union_map_read_from_str(ctx, str);
8269 data.before = 0;
8270 data.after = 0;
8271 data.depth = 0;
8272 build = isl_ast_build_from_context(set);
8273 build = isl_ast_build_set_before_each_for(build,
8274 &before_for, &data);
8275 build = isl_ast_build_set_after_each_for(build,
8276 &after_for, &data);
8277 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8278 isl_ast_build_free(build);
8279 if (!tree)
8280 return -1;
8282 isl_ast_node_free(tree);
8284 if (data.before != 3 || data.after != 3)
8285 isl_die(ctx, isl_error_unknown,
8286 "unexpected number of for nodes", return -1);
8288 return 0;
8291 /* Check that the AST generator handles domains that are integrally disjoint
8292 * but not rationally disjoint.
8294 static int test_ast_gen2(isl_ctx *ctx)
8296 const char *str;
8297 isl_set *set;
8298 isl_union_map *schedule;
8299 isl_union_map *options;
8300 isl_ast_build *build;
8301 isl_ast_node *tree;
8303 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
8304 schedule = isl_union_map_read_from_str(ctx, str);
8305 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8306 build = isl_ast_build_from_context(set);
8308 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
8309 options = isl_union_map_read_from_str(ctx, str);
8310 build = isl_ast_build_set_options(build, options);
8311 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8312 isl_ast_build_free(build);
8313 if (!tree)
8314 return -1;
8315 isl_ast_node_free(tree);
8317 return 0;
8320 /* Increment *user on each call.
8322 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
8323 __isl_keep isl_ast_build *build, void *user)
8325 int *n = user;
8327 (*n)++;
8329 return node;
8332 /* Test that unrolling tries to minimize the number of instances.
8333 * In particular, for the schedule given below, make sure it generates
8334 * 3 nodes (rather than 101).
8336 static int test_ast_gen3(isl_ctx *ctx)
8338 const char *str;
8339 isl_set *set;
8340 isl_union_map *schedule;
8341 isl_union_map *options;
8342 isl_ast_build *build;
8343 isl_ast_node *tree;
8344 int n_domain = 0;
8346 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
8347 schedule = isl_union_map_read_from_str(ctx, str);
8348 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8350 str = "{ [i] -> unroll[0] }";
8351 options = isl_union_map_read_from_str(ctx, str);
8353 build = isl_ast_build_from_context(set);
8354 build = isl_ast_build_set_options(build, options);
8355 build = isl_ast_build_set_at_each_domain(build,
8356 &count_domains, &n_domain);
8357 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8358 isl_ast_build_free(build);
8359 if (!tree)
8360 return -1;
8362 isl_ast_node_free(tree);
8364 if (n_domain != 3)
8365 isl_die(ctx, isl_error_unknown,
8366 "unexpected number of for nodes", return -1);
8368 return 0;
8371 /* Check that if the ast_build_exploit_nested_bounds options is set,
8372 * we do not get an outer if node in the generated AST,
8373 * while we do get such an outer if node if the options is not set.
8375 static int test_ast_gen4(isl_ctx *ctx)
8377 const char *str;
8378 isl_set *set;
8379 isl_union_map *schedule;
8380 isl_ast_build *build;
8381 isl_ast_node *tree;
8382 enum isl_ast_node_type type;
8383 int enb;
8385 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
8386 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
8388 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
8390 schedule = isl_union_map_read_from_str(ctx, str);
8391 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8392 build = isl_ast_build_from_context(set);
8393 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8394 isl_ast_build_free(build);
8395 if (!tree)
8396 return -1;
8398 type = isl_ast_node_get_type(tree);
8399 isl_ast_node_free(tree);
8401 if (type == isl_ast_node_if)
8402 isl_die(ctx, isl_error_unknown,
8403 "not expecting if node", return -1);
8405 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
8407 schedule = isl_union_map_read_from_str(ctx, str);
8408 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8409 build = isl_ast_build_from_context(set);
8410 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8411 isl_ast_build_free(build);
8412 if (!tree)
8413 return -1;
8415 type = isl_ast_node_get_type(tree);
8416 isl_ast_node_free(tree);
8418 if (type != isl_ast_node_if)
8419 isl_die(ctx, isl_error_unknown,
8420 "expecting if node", return -1);
8422 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
8424 return 0;
8427 /* This function is called for each leaf in the AST generated
8428 * from test_ast_gen5.
8430 * We finalize the AST generation by extending the outer schedule
8431 * with a zero-dimensional schedule. If this results in any for loops,
8432 * then this means that we did not pass along enough information
8433 * about the outer schedule to the inner AST generation.
8435 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
8436 void *user)
8438 isl_union_map *schedule, *extra;
8439 isl_ast_node *tree;
8441 schedule = isl_ast_build_get_schedule(build);
8442 extra = isl_union_map_copy(schedule);
8443 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
8444 schedule = isl_union_map_range_product(schedule, extra);
8445 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8446 isl_ast_build_free(build);
8448 if (!tree)
8449 return NULL;
8451 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
8452 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
8453 "code should not contain any for loop",
8454 return isl_ast_node_free(tree));
8456 return tree;
8459 /* Check that we do not lose any information when going back and
8460 * forth between internal and external schedule.
8462 * In particular, we create an AST where we unroll the only
8463 * non-constant dimension in the schedule. We therefore do
8464 * not expect any for loops in the AST. However, older versions
8465 * of isl would not pass along enough information about the outer
8466 * schedule when performing an inner code generation from a create_leaf
8467 * callback, resulting in the inner code generation producing a for loop.
8469 static int test_ast_gen5(isl_ctx *ctx)
8471 const char *str;
8472 isl_set *set;
8473 isl_union_map *schedule, *options;
8474 isl_ast_build *build;
8475 isl_ast_node *tree;
8477 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
8478 schedule = isl_union_map_read_from_str(ctx, str);
8480 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
8481 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
8482 options = isl_union_map_read_from_str(ctx, str);
8484 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8485 build = isl_ast_build_from_context(set);
8486 build = isl_ast_build_set_options(build, options);
8487 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
8488 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8489 isl_ast_build_free(build);
8490 isl_ast_node_free(tree);
8491 if (!tree)
8492 return -1;
8494 return 0;
8497 /* Check that the expression
8499 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
8501 * is not combined into
8503 * min(n/2, 0)
8505 * as this would result in n/2 being evaluated in parts of
8506 * the definition domain where n is not a multiple of 2.
8508 static int test_ast_expr(isl_ctx *ctx)
8510 const char *str;
8511 isl_pw_aff *pa;
8512 isl_ast_build *build;
8513 isl_ast_expr *expr;
8514 int min_max;
8515 int is_min;
8517 min_max = isl_options_get_ast_build_detect_min_max(ctx);
8518 isl_options_set_ast_build_detect_min_max(ctx, 1);
8520 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
8521 pa = isl_pw_aff_read_from_str(ctx, str);
8522 build = isl_ast_build_alloc(ctx);
8523 expr = isl_ast_build_expr_from_pw_aff(build, pa);
8524 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
8525 isl_ast_expr_get_op_type(expr) == isl_ast_op_min;
8526 isl_ast_build_free(build);
8527 isl_ast_expr_free(expr);
8529 isl_options_set_ast_build_detect_min_max(ctx, min_max);
8531 if (!expr)
8532 return -1;
8533 if (is_min)
8534 isl_die(ctx, isl_error_unknown,
8535 "expressions should not be combined", return -1);
8537 return 0;
8540 static int test_ast_gen(isl_ctx *ctx)
8542 if (test_ast_gen1(ctx) < 0)
8543 return -1;
8544 if (test_ast_gen2(ctx) < 0)
8545 return -1;
8546 if (test_ast_gen3(ctx) < 0)
8547 return -1;
8548 if (test_ast_gen4(ctx) < 0)
8549 return -1;
8550 if (test_ast_gen5(ctx) < 0)
8551 return -1;
8552 if (test_ast_expr(ctx) < 0)
8553 return -1;
8554 return 0;
8557 /* Check if dropping output dimensions from an isl_pw_multi_aff
8558 * works properly.
8560 static int test_pw_multi_aff(isl_ctx *ctx)
8562 const char *str;
8563 isl_pw_multi_aff *pma1, *pma2;
8564 int equal;
8566 str = "{ [i,j] -> [i+j, 4i-j] }";
8567 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
8568 str = "{ [i,j] -> [4i-j] }";
8569 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
8571 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
8573 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
8575 isl_pw_multi_aff_free(pma1);
8576 isl_pw_multi_aff_free(pma2);
8577 if (equal < 0)
8578 return -1;
8579 if (!equal)
8580 isl_die(ctx, isl_error_unknown,
8581 "expressions not equal", return -1);
8583 return 0;
8586 /* Check that we can properly parse multi piecewise affine expressions
8587 * where the piecewise affine expressions have different domains.
8589 static int test_multi_pw_aff_1(isl_ctx *ctx)
8591 const char *str;
8592 isl_set *dom, *dom2;
8593 isl_multi_pw_aff *mpa1, *mpa2;
8594 isl_pw_aff *pa;
8595 int equal;
8596 int equal_domain;
8598 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
8599 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
8600 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
8601 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
8602 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
8603 str = "{ [i] -> [(i : i > 0), 2i] }";
8604 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
8606 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
8608 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
8609 dom = isl_pw_aff_domain(pa);
8610 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
8611 dom2 = isl_pw_aff_domain(pa);
8612 equal_domain = isl_set_is_equal(dom, dom2);
8614 isl_set_free(dom);
8615 isl_set_free(dom2);
8616 isl_multi_pw_aff_free(mpa1);
8617 isl_multi_pw_aff_free(mpa2);
8619 if (equal < 0)
8620 return -1;
8621 if (!equal)
8622 isl_die(ctx, isl_error_unknown,
8623 "expressions not equal", return -1);
8625 if (equal_domain < 0)
8626 return -1;
8627 if (equal_domain)
8628 isl_die(ctx, isl_error_unknown,
8629 "domains unexpectedly equal", return -1);
8631 return 0;
8634 /* Check that the dimensions in the explicit domain
8635 * of a multi piecewise affine expression are properly
8636 * taken into account.
8638 static int test_multi_pw_aff_2(isl_ctx *ctx)
8640 const char *str;
8641 isl_bool involves1, involves2, involves3, equal;
8642 isl_multi_pw_aff *mpa, *mpa1, *mpa2;
8644 str = "{ A[x,y] -> B[] : x >= y }";
8645 mpa = isl_multi_pw_aff_read_from_str(ctx, str);
8646 involves1 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 2);
8647 mpa1 = isl_multi_pw_aff_copy(mpa);
8649 mpa = isl_multi_pw_aff_insert_dims(mpa, isl_dim_in, 0, 1);
8650 involves2 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 1);
8651 involves3 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 1, 2);
8652 str = "{ [a,x,y] -> B[] : x >= y }";
8653 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
8654 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
8655 isl_multi_pw_aff_free(mpa2);
8657 mpa = isl_multi_pw_aff_drop_dims(mpa, isl_dim_in, 0, 1);
8658 mpa = isl_multi_pw_aff_set_tuple_name(mpa, isl_dim_in, "A");
8659 if (equal >= 0 && equal)
8660 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa1);
8661 isl_multi_pw_aff_free(mpa1);
8662 isl_multi_pw_aff_free(mpa);
8664 if (involves1 < 0 || involves2 < 0 || involves3 < 0 || equal < 0)
8665 return -1;
8666 if (!equal)
8667 isl_die(ctx, isl_error_unknown,
8668 "incorrect result of dimension insertion/removal",
8669 return isl_stat_error);
8670 if (!involves1 || involves2 || !involves3)
8671 isl_die(ctx, isl_error_unknown,
8672 "incorrect characterization of involved dimensions",
8673 return isl_stat_error);
8675 return 0;
8678 /* Check that isl_multi_union_pw_aff_multi_val_on_domain
8679 * sets the explicit domain of a zero-dimensional result,
8680 * such that it can be converted to an isl_union_map.
8682 static isl_stat test_multi_pw_aff_3(isl_ctx *ctx)
8684 isl_space *space;
8685 isl_union_set *dom;
8686 isl_multi_val *mv;
8687 isl_multi_union_pw_aff *mupa;
8688 isl_union_map *umap;
8690 dom = isl_union_set_read_from_str(ctx, "{ A[]; B[] }");
8691 space = isl_union_set_get_space(dom);
8692 mv = isl_multi_val_zero(isl_space_set_from_params(space));
8693 mupa = isl_multi_union_pw_aff_multi_val_on_domain(dom, mv);
8694 umap = isl_union_map_from_multi_union_pw_aff(mupa);
8695 isl_union_map_free(umap);
8696 if (!umap)
8697 return isl_stat_error;
8699 return isl_stat_ok;
8702 /* Perform some tests on multi piecewise affine expressions.
8704 static int test_multi_pw_aff(isl_ctx *ctx)
8706 if (test_multi_pw_aff_1(ctx) < 0)
8707 return -1;
8708 if (test_multi_pw_aff_2(ctx) < 0)
8709 return -1;
8710 if (test_multi_pw_aff_3(ctx) < 0)
8711 return -1;
8712 return 0;
8715 /* This is a regression test for a bug where isl_basic_map_simplify
8716 * would end up in an infinite loop. In particular, we construct
8717 * an empty basic set that is not obviously empty.
8718 * isl_basic_set_is_empty marks the basic set as empty.
8719 * After projecting out i3, the variable can be dropped completely,
8720 * but isl_basic_map_simplify refrains from doing so if the basic set
8721 * is empty and would end up in an infinite loop if it didn't test
8722 * explicitly for empty basic maps in the outer loop.
8724 static int test_simplify_1(isl_ctx *ctx)
8726 const char *str;
8727 isl_basic_set *bset;
8728 int empty;
8730 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
8731 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
8732 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
8733 "i3 >= i2 }";
8734 bset = isl_basic_set_read_from_str(ctx, str);
8735 empty = isl_basic_set_is_empty(bset);
8736 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
8737 isl_basic_set_free(bset);
8738 if (!bset)
8739 return -1;
8740 if (!empty)
8741 isl_die(ctx, isl_error_unknown,
8742 "basic set should be empty", return -1);
8744 return 0;
8747 /* Check that the equality in the set description below
8748 * is simplified away.
8750 static int test_simplify_2(isl_ctx *ctx)
8752 const char *str;
8753 isl_basic_set *bset;
8754 isl_bool universe;
8756 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
8757 bset = isl_basic_set_read_from_str(ctx, str);
8758 universe = isl_basic_set_plain_is_universe(bset);
8759 isl_basic_set_free(bset);
8761 if (universe < 0)
8762 return -1;
8763 if (!universe)
8764 isl_die(ctx, isl_error_unknown,
8765 "equality not simplified away", return -1);
8766 return 0;
8769 /* Some simplification tests.
8771 static int test_simplify(isl_ctx *ctx)
8773 if (test_simplify_1(ctx) < 0)
8774 return -1;
8775 if (test_simplify_2(ctx) < 0)
8776 return -1;
8777 return 0;
8780 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
8781 * with gbr context would fail to disable the use of the shifted tableau
8782 * when transferring equalities for the input to the context, resulting
8783 * in invalid sample values.
8785 static int test_partial_lexmin(isl_ctx *ctx)
8787 const char *str;
8788 isl_basic_set *bset;
8789 isl_basic_map *bmap;
8790 isl_map *map;
8792 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
8793 bmap = isl_basic_map_read_from_str(ctx, str);
8794 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
8795 bset = isl_basic_set_read_from_str(ctx, str);
8796 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
8797 isl_map_free(map);
8799 if (!map)
8800 return -1;
8802 return 0;
8805 /* Check that the variable compression performed on the existentially
8806 * quantified variables inside isl_basic_set_compute_divs is not confused
8807 * by the implicit equalities among the parameters.
8809 static int test_compute_divs(isl_ctx *ctx)
8811 const char *str;
8812 isl_basic_set *bset;
8813 isl_set *set;
8815 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
8816 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
8817 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
8818 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
8819 bset = isl_basic_set_read_from_str(ctx, str);
8820 set = isl_basic_set_compute_divs(bset);
8821 isl_set_free(set);
8822 if (!set)
8823 return -1;
8825 return 0;
8828 /* Check that isl_schedule_get_map is not confused by a schedule tree
8829 * with divergent filter node parameters, as can result from a call
8830 * to isl_schedule_intersect_domain.
8832 static int test_schedule_tree(isl_ctx *ctx)
8834 const char *str;
8835 isl_union_set *uset;
8836 isl_schedule *sched1, *sched2;
8837 isl_union_map *umap;
8839 uset = isl_union_set_read_from_str(ctx, "{ A[i] }");
8840 sched1 = isl_schedule_from_domain(uset);
8841 uset = isl_union_set_read_from_str(ctx, "{ B[] }");
8842 sched2 = isl_schedule_from_domain(uset);
8844 sched1 = isl_schedule_sequence(sched1, sched2);
8845 str = "[n] -> { A[i] : 0 <= i < n; B[] }";
8846 uset = isl_union_set_read_from_str(ctx, str);
8847 sched1 = isl_schedule_intersect_domain(sched1, uset);
8848 umap = isl_schedule_get_map(sched1);
8849 isl_schedule_free(sched1);
8850 isl_union_map_free(umap);
8851 if (!umap)
8852 return -1;
8854 return 0;
8857 /* Check that a zero-dimensional prefix schedule keeps track
8858 * of the domain and outer filters.
8860 static int test_schedule_tree_prefix(isl_ctx *ctx)
8862 const char *str;
8863 isl_bool equal;
8864 isl_union_set *uset;
8865 isl_union_set_list *filters;
8866 isl_multi_union_pw_aff *mupa, *mupa2;
8867 isl_schedule_node *node;
8869 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
8870 uset = isl_union_set_read_from_str(ctx, str);
8871 node = isl_schedule_node_from_domain(uset);
8872 node = isl_schedule_node_child(node, 0);
8874 str = "{ S1[i,j] : i > j }";
8875 uset = isl_union_set_read_from_str(ctx, str);
8876 filters = isl_union_set_list_from_union_set(uset);
8877 str = "{ S1[i,j] : i <= j; S2[i,j] }";
8878 uset = isl_union_set_read_from_str(ctx, str);
8879 filters = isl_union_set_list_add(filters, uset);
8880 node = isl_schedule_node_insert_sequence(node, filters);
8882 node = isl_schedule_node_child(node, 0);
8883 node = isl_schedule_node_child(node, 0);
8884 mupa = isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
8885 str = "([] : { S1[i,j] : i > j })";
8886 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx, str);
8887 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
8888 isl_multi_union_pw_aff_free(mupa2);
8889 isl_multi_union_pw_aff_free(mupa);
8890 isl_schedule_node_free(node);
8892 if (equal < 0)
8893 return -1;
8894 if (!equal)
8895 isl_die(ctx, isl_error_unknown, "unexpected prefix schedule",
8896 return -1);
8898 return 0;
8901 /* Check that the reaching domain elements and the prefix schedule
8902 * at a leaf node are the same before and after grouping.
8904 static int test_schedule_tree_group_1(isl_ctx *ctx)
8906 int equal;
8907 const char *str;
8908 isl_id *id;
8909 isl_union_set *uset;
8910 isl_multi_union_pw_aff *mupa;
8911 isl_union_pw_multi_aff *upma1, *upma2;
8912 isl_union_set *domain1, *domain2;
8913 isl_union_map *umap1, *umap2;
8914 isl_schedule_node *node;
8916 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
8917 uset = isl_union_set_read_from_str(ctx, str);
8918 node = isl_schedule_node_from_domain(uset);
8919 node = isl_schedule_node_child(node, 0);
8920 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
8921 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8922 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8923 node = isl_schedule_node_child(node, 0);
8924 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
8925 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8926 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8927 node = isl_schedule_node_child(node, 0);
8928 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
8929 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
8930 domain1 = isl_schedule_node_get_domain(node);
8931 id = isl_id_alloc(ctx, "group", NULL);
8932 node = isl_schedule_node_parent(node);
8933 node = isl_schedule_node_group(node, id);
8934 node = isl_schedule_node_child(node, 0);
8935 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
8936 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
8937 domain2 = isl_schedule_node_get_domain(node);
8938 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
8939 if (equal >= 0 && equal)
8940 equal = isl_union_set_is_equal(domain1, domain2);
8941 if (equal >= 0 && equal)
8942 equal = isl_union_map_is_equal(umap1, umap2);
8943 isl_union_map_free(umap1);
8944 isl_union_map_free(umap2);
8945 isl_union_set_free(domain1);
8946 isl_union_set_free(domain2);
8947 isl_union_pw_multi_aff_free(upma1);
8948 isl_union_pw_multi_aff_free(upma2);
8949 isl_schedule_node_free(node);
8951 if (equal < 0)
8952 return -1;
8953 if (!equal)
8954 isl_die(ctx, isl_error_unknown,
8955 "expressions not equal", return -1);
8957 return 0;
8960 /* Check that we can have nested groupings and that the union map
8961 * schedule representation is the same before and after the grouping.
8962 * Note that after the grouping, the union map representation contains
8963 * the domain constraints from the ranges of the expansion nodes,
8964 * while they are missing from the union map representation of
8965 * the tree without expansion nodes.
8967 * Also check that the global expansion is as expected.
8969 static int test_schedule_tree_group_2(isl_ctx *ctx)
8971 int equal, equal_expansion;
8972 const char *str;
8973 isl_id *id;
8974 isl_union_set *uset;
8975 isl_union_map *umap1, *umap2;
8976 isl_union_map *expansion1, *expansion2;
8977 isl_union_set_list *filters;
8978 isl_multi_union_pw_aff *mupa;
8979 isl_schedule *schedule;
8980 isl_schedule_node *node;
8982 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
8983 "S3[i,j] : 0 <= i,j < 10 }";
8984 uset = isl_union_set_read_from_str(ctx, str);
8985 node = isl_schedule_node_from_domain(uset);
8986 node = isl_schedule_node_child(node, 0);
8987 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
8988 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8989 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8990 node = isl_schedule_node_child(node, 0);
8991 str = "{ S1[i,j] }";
8992 uset = isl_union_set_read_from_str(ctx, str);
8993 filters = isl_union_set_list_from_union_set(uset);
8994 str = "{ S2[i,j]; S3[i,j] }";
8995 uset = isl_union_set_read_from_str(ctx, str);
8996 filters = isl_union_set_list_add(filters, uset);
8997 node = isl_schedule_node_insert_sequence(node, filters);
8998 node = isl_schedule_node_child(node, 1);
8999 node = isl_schedule_node_child(node, 0);
9000 str = "{ S2[i,j] }";
9001 uset = isl_union_set_read_from_str(ctx, str);
9002 filters = isl_union_set_list_from_union_set(uset);
9003 str = "{ S3[i,j] }";
9004 uset = isl_union_set_read_from_str(ctx, str);
9005 filters = isl_union_set_list_add(filters, uset);
9006 node = isl_schedule_node_insert_sequence(node, filters);
9008 schedule = isl_schedule_node_get_schedule(node);
9009 umap1 = isl_schedule_get_map(schedule);
9010 uset = isl_schedule_get_domain(schedule);
9011 umap1 = isl_union_map_intersect_domain(umap1, uset);
9012 isl_schedule_free(schedule);
9014 node = isl_schedule_node_parent(node);
9015 node = isl_schedule_node_parent(node);
9016 id = isl_id_alloc(ctx, "group1", NULL);
9017 node = isl_schedule_node_group(node, id);
9018 node = isl_schedule_node_child(node, 1);
9019 node = isl_schedule_node_child(node, 0);
9020 id = isl_id_alloc(ctx, "group2", NULL);
9021 node = isl_schedule_node_group(node, id);
9023 schedule = isl_schedule_node_get_schedule(node);
9024 umap2 = isl_schedule_get_map(schedule);
9025 isl_schedule_free(schedule);
9027 node = isl_schedule_node_root(node);
9028 node = isl_schedule_node_child(node, 0);
9029 expansion1 = isl_schedule_node_get_subtree_expansion(node);
9030 isl_schedule_node_free(node);
9032 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
9033 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
9034 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
9036 expansion2 = isl_union_map_read_from_str(ctx, str);
9038 equal = isl_union_map_is_equal(umap1, umap2);
9039 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
9041 isl_union_map_free(umap1);
9042 isl_union_map_free(umap2);
9043 isl_union_map_free(expansion1);
9044 isl_union_map_free(expansion2);
9046 if (equal < 0 || equal_expansion < 0)
9047 return -1;
9048 if (!equal)
9049 isl_die(ctx, isl_error_unknown,
9050 "expressions not equal", return -1);
9051 if (!equal_expansion)
9052 isl_die(ctx, isl_error_unknown,
9053 "unexpected expansion", return -1);
9055 return 0;
9058 /* Some tests for the isl_schedule_node_group function.
9060 static int test_schedule_tree_group(isl_ctx *ctx)
9062 if (test_schedule_tree_group_1(ctx) < 0)
9063 return -1;
9064 if (test_schedule_tree_group_2(ctx) < 0)
9065 return -1;
9066 return 0;
9069 struct {
9070 const char *set;
9071 const char *dual;
9072 } coef_tests[] = {
9073 { "{ rat: [i] : 0 <= i <= 10 }",
9074 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
9075 { "{ rat: [i] : FALSE }",
9076 "{ rat: coefficients[[cst] -> [a]] }" },
9077 { "{ rat: [i] : }",
9078 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
9081 struct {
9082 const char *set;
9083 const char *dual;
9084 } sol_tests[] = {
9085 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
9086 "{ rat: [i] : 0 <= i <= 10 }" },
9087 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
9088 "{ rat: [i] }" },
9089 { "{ rat: coefficients[[cst] -> [a]] }",
9090 "{ rat: [i] : FALSE }" },
9093 /* Test the basic functionality of isl_basic_set_coefficients and
9094 * isl_basic_set_solutions.
9096 static int test_dual(isl_ctx *ctx)
9098 int i;
9100 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
9101 int equal;
9102 isl_basic_set *bset1, *bset2;
9104 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
9105 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
9106 bset1 = isl_basic_set_coefficients(bset1);
9107 equal = isl_basic_set_is_equal(bset1, bset2);
9108 isl_basic_set_free(bset1);
9109 isl_basic_set_free(bset2);
9110 if (equal < 0)
9111 return -1;
9112 if (!equal)
9113 isl_die(ctx, isl_error_unknown,
9114 "incorrect dual", return -1);
9117 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
9118 int equal;
9119 isl_basic_set *bset1, *bset2;
9121 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
9122 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
9123 bset1 = isl_basic_set_solutions(bset1);
9124 equal = isl_basic_set_is_equal(bset1, bset2);
9125 isl_basic_set_free(bset1);
9126 isl_basic_set_free(bset2);
9127 if (equal < 0)
9128 return -1;
9129 if (!equal)
9130 isl_die(ctx, isl_error_unknown,
9131 "incorrect dual", return -1);
9134 return 0;
9137 struct {
9138 int scale_tile;
9139 int shift_point;
9140 const char *domain;
9141 const char *schedule;
9142 const char *sizes;
9143 const char *tile;
9144 const char *point;
9145 } tile_tests[] = {
9146 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
9147 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9148 "{ [32,32] }",
9149 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
9150 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9152 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
9153 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9154 "{ [32,32] }",
9155 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
9156 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9158 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
9159 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9160 "{ [32,32] }",
9161 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
9162 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
9164 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
9165 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9166 "{ [32,32] }",
9167 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
9168 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
9172 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
9173 * tile the band and then check if the tile and point bands have the
9174 * expected partial schedule.
9176 static int test_tile(isl_ctx *ctx)
9178 int i;
9179 int scale;
9180 int shift;
9182 scale = isl_options_get_tile_scale_tile_loops(ctx);
9183 shift = isl_options_get_tile_shift_point_loops(ctx);
9185 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
9186 int opt;
9187 int equal;
9188 const char *str;
9189 isl_union_set *domain;
9190 isl_multi_union_pw_aff *mupa, *mupa2;
9191 isl_schedule_node *node;
9192 isl_multi_val *sizes;
9194 opt = tile_tests[i].scale_tile;
9195 isl_options_set_tile_scale_tile_loops(ctx, opt);
9196 opt = tile_tests[i].shift_point;
9197 isl_options_set_tile_shift_point_loops(ctx, opt);
9199 str = tile_tests[i].domain;
9200 domain = isl_union_set_read_from_str(ctx, str);
9201 node = isl_schedule_node_from_domain(domain);
9202 node = isl_schedule_node_child(node, 0);
9203 str = tile_tests[i].schedule;
9204 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9205 node = isl_schedule_node_insert_partial_schedule(node, mupa);
9206 str = tile_tests[i].sizes;
9207 sizes = isl_multi_val_read_from_str(ctx, str);
9208 node = isl_schedule_node_band_tile(node, sizes);
9210 str = tile_tests[i].tile;
9211 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9212 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9213 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
9214 isl_multi_union_pw_aff_free(mupa);
9215 isl_multi_union_pw_aff_free(mupa2);
9217 node = isl_schedule_node_child(node, 0);
9219 str = tile_tests[i].point;
9220 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9221 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9222 if (equal >= 0 && equal)
9223 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
9224 mupa2);
9225 isl_multi_union_pw_aff_free(mupa);
9226 isl_multi_union_pw_aff_free(mupa2);
9228 isl_schedule_node_free(node);
9230 if (equal < 0)
9231 return -1;
9232 if (!equal)
9233 isl_die(ctx, isl_error_unknown,
9234 "unexpected result", return -1);
9237 isl_options_set_tile_scale_tile_loops(ctx, scale);
9238 isl_options_set_tile_shift_point_loops(ctx, shift);
9240 return 0;
9243 /* Check that the domain hash of a space is equal to the hash
9244 * of the domain of the space.
9246 static int test_domain_hash(isl_ctx *ctx)
9248 isl_map *map;
9249 isl_space *space;
9250 uint32_t hash1, hash2;
9252 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
9253 space = isl_map_get_space(map);
9254 isl_map_free(map);
9255 hash1 = isl_space_get_domain_hash(space);
9256 space = isl_space_domain(space);
9257 hash2 = isl_space_get_hash(space);
9258 isl_space_free(space);
9260 if (!space)
9261 return -1;
9262 if (hash1 != hash2)
9263 isl_die(ctx, isl_error_unknown,
9264 "domain hash not equal to hash of domain", return -1);
9266 return 0;
9269 /* Check that a universe basic set that is not obviously equal to the universe
9270 * is still recognized as being equal to the universe.
9272 static int test_universe(isl_ctx *ctx)
9274 const char *s;
9275 isl_basic_set *bset;
9276 isl_bool is_univ;
9278 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
9279 bset = isl_basic_set_read_from_str(ctx, s);
9280 is_univ = isl_basic_set_is_universe(bset);
9281 isl_basic_set_free(bset);
9283 if (is_univ < 0)
9284 return -1;
9285 if (!is_univ)
9286 isl_die(ctx, isl_error_unknown,
9287 "not recognized as universe set", return -1);
9289 return 0;
9292 /* Sets for which chambers are computed and checked.
9294 const char *chambers_tests[] = {
9295 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
9296 "z >= 0 and z <= C - y and z <= B - x - y }",
9299 /* Add the domain of "cell" to "cells".
9301 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
9303 isl_basic_set_list **cells = user;
9304 isl_basic_set *dom;
9306 dom = isl_cell_get_domain(cell);
9307 isl_cell_free(cell);
9308 *cells = isl_basic_set_list_add(*cells, dom);
9310 return *cells ? isl_stat_ok : isl_stat_error;
9313 /* Check that the elements of "list" are pairwise disjoint.
9315 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
9317 int i, j, n;
9319 if (!list)
9320 return isl_stat_error;
9322 n = isl_basic_set_list_n_basic_set(list);
9323 for (i = 0; i < n; ++i) {
9324 isl_basic_set *bset_i;
9326 bset_i = isl_basic_set_list_get_basic_set(list, i);
9327 for (j = i + 1; j < n; ++j) {
9328 isl_basic_set *bset_j;
9329 isl_bool disjoint;
9331 bset_j = isl_basic_set_list_get_basic_set(list, j);
9332 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
9333 isl_basic_set_free(bset_j);
9334 if (!disjoint)
9335 isl_die(isl_basic_set_list_get_ctx(list),
9336 isl_error_unknown, "not disjoint",
9337 break);
9338 if (disjoint < 0 || !disjoint)
9339 break;
9341 isl_basic_set_free(bset_i);
9342 if (j < n)
9343 return isl_stat_error;
9346 return isl_stat_ok;
9349 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
9350 * are pairwise disjoint.
9352 static int test_chambers(isl_ctx *ctx)
9354 int i;
9356 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
9357 isl_basic_set *bset;
9358 isl_vertices *vertices;
9359 isl_basic_set_list *cells;
9360 isl_stat ok;
9362 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
9363 vertices = isl_basic_set_compute_vertices(bset);
9364 cells = isl_basic_set_list_alloc(ctx, 0);
9365 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
9366 &cells) < 0)
9367 cells = isl_basic_set_list_free(cells);
9368 ok = check_pairwise_disjoint(cells);
9369 isl_basic_set_list_free(cells);
9370 isl_vertices_free(vertices);
9371 isl_basic_set_free(bset);
9373 if (ok < 0)
9374 return -1;
9377 return 0;
9380 struct {
9381 const char *name;
9382 int (*fn)(isl_ctx *ctx);
9383 } tests [] = {
9384 { "universe", &test_universe },
9385 { "domain hash", &test_domain_hash },
9386 { "dual", &test_dual },
9387 { "dependence analysis", &test_flow },
9388 { "val", &test_val },
9389 { "compute divs", &test_compute_divs },
9390 { "partial lexmin", &test_partial_lexmin },
9391 { "simplify", &test_simplify },
9392 { "curry", &test_curry },
9393 { "piecewise multi affine expressions", &test_pw_multi_aff },
9394 { "multi piecewise affine expressions", &test_multi_pw_aff },
9395 { "conversion", &test_conversion },
9396 { "list", &test_list },
9397 { "align parameters", &test_align_parameters },
9398 { "drop unused parameters", &test_drop_unused_parameters },
9399 { "preimage", &test_preimage },
9400 { "pullback", &test_pullback },
9401 { "AST", &test_ast },
9402 { "AST build", &test_ast_build },
9403 { "AST generation", &test_ast_gen },
9404 { "eliminate", &test_eliminate },
9405 { "residue class", &test_residue_class },
9406 { "div", &test_div },
9407 { "slice", &test_slice },
9408 { "fixed power", &test_fixed_power },
9409 { "sample", &test_sample },
9410 { "output", &test_output },
9411 { "vertices", &test_vertices },
9412 { "chambers", &test_chambers },
9413 { "fixed", &test_fixed },
9414 { "equal", &test_equal },
9415 { "disjoint", &test_disjoint },
9416 { "product", &test_product },
9417 { "dim_max", &test_dim_max },
9418 { "affine", &test_aff },
9419 { "injective", &test_injective },
9420 { "schedule (whole component)", &test_schedule_whole },
9421 { "schedule (incremental)", &test_schedule_incremental },
9422 { "schedule tree", &test_schedule_tree },
9423 { "schedule tree prefix", &test_schedule_tree_prefix },
9424 { "schedule tree grouping", &test_schedule_tree_group },
9425 { "tile", &test_tile },
9426 { "union_pw", &test_union_pw },
9427 { "locus", &test_locus },
9428 { "eval", &test_eval },
9429 { "parse", &test_parse },
9430 { "single-valued", &test_sv },
9431 { "affine hull", &test_affine_hull },
9432 { "simple_hull", &test_simple_hull },
9433 { "coalesce", &test_coalesce },
9434 { "factorize", &test_factorize },
9435 { "subset", &test_subset },
9436 { "subtract", &test_subtract },
9437 { "intersect", &test_intersect },
9438 { "lexmin", &test_lexmin },
9439 { "min", &test_min },
9440 { "gist", &test_gist },
9441 { "piecewise quasi-polynomials", &test_pwqp },
9442 { "lift", &test_lift },
9443 { "bound", &test_bound },
9444 { "get lists", &test_get_list },
9445 { "union", &test_union },
9446 { "split periods", &test_split_periods },
9447 { "lexicographic order", &test_lex },
9448 { "bijectivity", &test_bijective },
9449 { "dataflow analysis", &test_dep },
9450 { "reading", &test_read },
9451 { "bounded", &test_bounded },
9452 { "construction", &test_construction },
9453 { "dimension manipulation", &test_dim },
9454 { "map application", &test_application },
9455 { "convex hull", &test_convex_hull },
9456 { "transitive closure", &test_closure },
9459 int main(int argc, char **argv)
9461 int i;
9462 struct isl_ctx *ctx;
9463 struct isl_options *options;
9465 options = isl_options_new_with_defaults();
9466 assert(options);
9467 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
9469 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
9470 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
9471 printf("%s\n", tests[i].name);
9472 if (tests[i].fn(ctx) < 0)
9473 goto error;
9475 isl_ctx_free(ctx);
9476 return 0;
9477 error:
9478 isl_ctx_free(ctx);
9479 return -1;